Coverage Report

Created: 2026-12-01 06:28

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/httrack/src/htsmhtml.c
Line
Count
Source
1
/* ------------------------------------------------------------ */
2
/*
3
HTTrack Website Copier, Offline Browser for Windows and Unix
4
Copyright (C) 1998 Xavier Roche and other contributors
5
6
SPDX-License-Identifier: GPL-3.0-or-later
7
8
This program is free software: you can redistribute it and/or modify
9
it under the terms of the GNU General Public License as published by
10
the Free Software Foundation, either version 3 of the License, or
11
(at your option) any later version.
12
13
This program is distributed in the hope that it will be useful,
14
but WITHOUT ANY WARRANTY; without even the implied warranty of
15
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
GNU General Public License for more details.
17
18
You should have received a copy of the GNU General Public License
19
along with this program. If not, see <http://www.gnu.org/licenses/>.
20
21
Ethical use: we kindly ask that you NOT use this software to harvest email
22
addresses or to collect any other private information about people. Doing so
23
would dishonor our work and waste the many hours we have spent on it.
24
25
Please visit our Website: http://www.httrack.com
26
*/
27
28
/* ------------------------------------------------------------ */
29
/* File: MIME-HTML (index.mht) writer                           */
30
/* Author: Xavier Roche                                         */
31
/* ------------------------------------------------------------ */
32
33
#include "htscore.h"
34
#include "htsbase.h"
35
#include "htsio.h"
36
37
/* MIME/MHTML writer states, held in opt->state.mimehtml_created. */
38
0
#define MIMEHTML_NONE 0    /* no part written yet */
39
0
#define MIMEHTML_WRITING 1 /* writing index.mht */
40
0
#define MIMEHTML_HELD 2    /* holding parts in ~index.mht.part, no root yet */
41
0
#define MIMEHTML_FAILED (-1)
42
43
/* Without start=, RFC 2387 defaults the root to the first part, whichever file
44
   the mirror happened to finish first. `startcid` is NULL when the archive
45
   holds no page. */
46
0
static void mimehtml_write_header(httrackp *opt, const char *startcid) {
47
0
  char BIGSTK currtime[256];
48
49
0
  time_gmt_rfc822(HTS_BUF(currtime));
50
0
  fprintf(opt->state.mimefp,
51
0
          "From: HTTrack Website Copier <nobody@localhost>\r\n"
52
0
          "Subject: Local mirror\r\n"
53
0
          "Date: %s\r\n"
54
0
          "Message-ID: <httrack_%d_%d@localhost>\r\n"
55
0
          "Content-Type: multipart/related;\r\n"
56
0
          "\tboundary=\"%s\";\r\n"
57
0
          "\ttype=\"text/html\"",
58
0
          currtime, (int) time(NULL), (int) rand(),
59
0
          StringBuff(opt->state.mimemid));
60
0
  if (startcid != NULL) {
61
0
    fprintf(opt->state.mimefp, ";\r\n\tstart=\"<%s>\"", startcid);
62
0
  }
63
0
  fprintf(opt->state.mimefp,
64
0
          "\r\nMIME-Version: 1.0\r\n"
65
0
          "\r\nThis message is a RFC MIME-compliant multipart message.\r\n"
66
0
          "\r\n");
67
0
}
68
69
/* Create index.mht with `startcid` as its root, and move in the parts held in
70
   opt->state.mimefp. */
71
0
static void mimehtml_create(httrackp *opt, const char *startcid) {
72
0
  FILE *const held = opt->state.mimefp;
73
0
  char BIGSTK heldname[HTS_URLMAXSIZE * 2];
74
75
0
  strcpybuff(heldname,
76
0
             hts_optpath(opt, StringBuff(opt->path_html), "~index.mht.part"));
77
0
  opt->state.mimefp =
78
0
      FOPEN(hts_optpath(opt, StringBuff(opt->path_html), "index.mht"), "wb");
79
0
  if (opt->state.mimefp == NULL) {
80
0
    if (held != NULL) {
81
0
      fclose(held);
82
0
      (void) UNLINK(heldname);
83
0
    }
84
0
    opt->state.mimehtml_created = MIMEHTML_FAILED;
85
0
    hts_log_print(opt, LOG_ERROR, "unable to create index.mht");
86
0
    return;
87
0
  }
88
0
  (void) UNLINK(hts_optpath(opt, StringBuff(opt->path_html), "index.eml"));
89
0
#ifndef _WIN32
90
0
  if (symlink("index.mht",
91
0
              hts_optpath(opt, StringBuff(opt->path_html), "index.eml")) != 0) {
92
0
    if (errno != EPERM) {
93
0
      hts_log_print(
94
0
          opt, LOG_WARNING | LOG_ERRNO,
95
0
          "could not create symbolic link from index.mht to index.eml");
96
0
    }
97
0
  }
98
0
#endif
99
0
  mimehtml_write_header(opt, startcid);
100
0
  if (held != NULL) {
101
0
    char BIGSTK buff[32768];
102
0
    size_t len;
103
104
0
    rewind(held);
105
0
    while ((len = fread(buff, 1, sizeof(buff), held)) > 0) {
106
0
      if (fwrite(buff, 1, len, opt->state.mimefp) != len) {
107
0
        hts_log_print(opt, LOG_ERROR | LOG_ERRNO,
108
0
                      "index.mht is truncated, could not move in %s", heldname);
109
0
        break;
110
0
      }
111
0
    }
112
0
    fclose(held);
113
0
    (void) UNLINK(heldname);
114
0
  }
115
0
  opt->state.mimehtml_created = MIMEHTML_WRITING;
116
0
}
117
118
void mimehtml_add_file(httrackp *opt, const char *save, const char *adr,
119
0
                       const char *fil) {
120
  /* MIME-html archive to build */
121
0
  if (opt != NULL && opt->mimehtml) {
122
0
    if (adr != NULL && strcmp(adr, "primary") == 0) {
123
0
      adr = NULL;
124
0
    }
125
0
    if (save != NULL && opt != NULL && adr != NULL && adr[0] &&
126
0
        strnotempty(save) && fexist_utf8(save)) {
127
0
      const char *rsc_save = save;
128
0
      const char *rsc_fil = strrchr(fil, '/');
129
      /* Read the part before naming it as start=, since a part that fails to
130
         open must never become the root. */
131
0
      FILE *const fp = FOPEN(save, "rb");
132
0
      int n;
133
134
0
      if (rsc_fil == NULL)
135
0
        rsc_fil = fil;
136
0
      if (strncmp(fslash(OPT_GET_HTS_BUF(opt), save),
137
0
                  fslash(OPT_GET_HTS_BUF(opt), StringBuff(opt->path_html_utf8)),
138
0
                  (n = (int) strlen(StringBuff(opt->path_html_utf8)))) == 0) {
139
0
        rsc_save += n;
140
0
      }
141
142
0
      if (fp != NULL) {
143
0
        const int isHtml = (ishtml(opt, save) == 1);
144
0
        char BIGSTK cid[HTS_URLMAXSIZE * 3];
145
146
0
        make_content_id(adr, fil, HTS_BUF(cid));
147
0
        if (opt->state.mimehtml_created == MIMEHTML_NONE) {
148
0
          srand((unsigned int) time(NULL));
149
0
          StringRoom(opt->state.mimemid, 256);
150
0
          snprintf(StringBuffRW(opt->state.mimemid), 256,
151
0
                   "----=_MIMEPart_%d_%d_=----", (int) time(NULL),
152
0
                   (int) rand());
153
0
          StringSetLength(opt->state.mimemid, -1);
154
0
          if (isHtml) {
155
0
            mimehtml_create(opt, cid);
156
0
          } else {
157
            /* Hold it aside until a page turns up to be the root. A saved
158
               name never carries a '~', so no mirrored file lands here. */
159
0
            opt->state.mimefp = FOPEN(
160
0
                hts_optpath(opt, StringBuff(opt->path_html), "~index.mht.part"),
161
0
                "w+b");
162
0
            if (opt->state.mimefp == NULL) {
163
0
              opt->state.mimehtml_created = MIMEHTML_FAILED;
164
0
              hts_log_print(opt, LOG_ERROR, "unable to create index.mht");
165
0
            } else {
166
0
              opt->state.mimehtml_created = MIMEHTML_HELD;
167
0
            }
168
0
          }
169
0
        } else if (opt->state.mimehtml_created == MIMEHTML_HELD && isHtml) {
170
0
          mimehtml_create(opt, cid);
171
0
        }
172
0
        if (opt->state.mimefp != NULL) {
173
          /* whole base64 lines, so none straddles two code64() calls */
174
0
          char buff[HTS_BASE64_LINE_BYTES * 100];
175
0
          char mimebuff[256];
176
0
          size_t len;
177
178
0
          mimebuff[0] = '\0';
179
0
          guess_httptype_sized(opt, mimebuff, sizeof(mimebuff), save);
180
0
          fprintf(opt->state.mimefp, "--%s\r\n",
181
0
                  StringBuff(opt->state.mimemid));
182
0
          fprintf(opt->state.mimefp,
183
0
                  "Content-disposition: attachment; filename=\"%s\"\r\n",
184
0
                  rsc_save);
185
0
          fprintf(opt->state.mimefp,
186
0
                  "Content-Type: %s\r\n"
187
0
                  "Content-Transfer-Encoding: %s\r\n"
188
0
                  "Content-ID: <%s>\r\n"
189
0
                  "\r\n",
190
0
                  mimebuff, isHtml ? "8bit" : "base64", cid);
191
0
          while ((len = fread(buff, 1, sizeof(buff), fp)) > 0) {
192
0
            if (!isHtml) {
193
0
              char base64buff[HTS_BASE64_LINE_BYTES * 100 * 2];
194
195
0
              code64(hts_span_at(buff, len), HTS_BUF(base64buff), HTS_TRUE);
196
0
              fprintf(opt->state.mimefp, "%s", base64buff);
197
0
            } else {
198
0
              fwrite(buff, 1, len, opt->state.mimefp);
199
0
            }
200
0
            if (len < sizeof(buff))
201
0
              break;
202
0
          }
203
0
          fprintf(opt->state.mimefp, "\r\n\r\n");
204
0
        }
205
0
        fclose(fp);
206
0
      }
207
0
    } else if (save == NULL) {
208
      /* The mirror saved no page, so the archive gets no root. */
209
0
      if (opt->state.mimehtml_created == MIMEHTML_HELD) {
210
0
        mimehtml_create(opt, NULL);
211
0
      }
212
0
      if (opt->state.mimehtml_created == MIMEHTML_WRITING &&
213
0
          opt->state.mimefp != NULL) {
214
0
        fprintf(opt->state.mimefp, "--%s--\r\n",
215
0
                StringBuff(opt->state.mimemid));
216
0
        fclose(opt->state.mimefp);
217
        opt->state.mimefp = NULL;
218
0
      }
219
0
    }
220
0
  }
221
0
}