moved whitespace around
[citadel.git] / webcit-ng / server / html2html.c
1 // Output an HTML message, modifying it slightly to make sure it plays nice
2 // with the rest of our web framework.
3 //
4 // Copyright (c) 2005-2024 by the citadel.org team
5 //
6 // This program is open source software.  Use, duplication, or disclosure is subject to the GNU General Public License v3.
7
8 #include "webcit.h"
9
10
11 // Strip surrounding single or double quotes from a string.
12 void stripquotes(char *s) {
13         int len;
14
15         if (!s) {
16                 return;
17         }
18
19         len = strlen(s);
20         if (len < 2) {
21                 return;
22         }
23
24         if (((s[0] == '\"') && (s[len - 1] == '\"')) || ((s[0] == '\'') && (s[len - 1] == '\''))) {
25                 s[len - 1] = 0;
26                 strcpy(s, &s[1]);
27         }
28 }
29
30
31 // Check to see if a META tag has overridden the declared MIME character set.
32 //
33 // charset              Character set name (left unchanged if we don't do anything)
34 // meta_http_equiv      Content of the "http-equiv" portion of the META tag
35 // meta_content         Content of the "content" portion of the META tag
36 void extract_charset_from_meta(char *charset, char *meta_http_equiv, char *meta_content) {
37         char *ptr;
38         char buf[64];
39
40         if (!charset) {
41                 return;
42         }
43         if (!meta_http_equiv) {
44                 return;
45         }
46         if (!meta_content) {
47                 return;
48         }
49
50         if (strcasecmp(meta_http_equiv, "Content-type")) {
51                 return;
52         }
53
54         ptr = strchr(meta_content, ';');
55         if (!ptr) {
56                 return;
57         }
58
59         safestrncpy(buf, ++ptr, sizeof buf);
60         string_trim(buf);
61         if (!strncasecmp(buf, "charset=", 8)) {
62                 strcpy(charset, &buf[8]);
63
64                 // The brain-damaged webmail program in Microsoft Exchange declares
65                 // a charset of "unicode" when they really mean "UTF-8".  GNU iconv
66                 // treats "unicode" as an alias for "UTF-16" so we have to manually
67                 // fix this here, otherwise messages generated in Exchange webmail
68                 // show up as a big pile of weird characters.
69                 if (!strcasecmp(charset, "unicode")) {
70                         strcpy(charset, "UTF-8");
71                 }
72
73                 // Remove wandering punctuation
74                 if ((ptr = strchr(charset, '\"'))) {
75                         *ptr = 0;
76                 }
77                 string_trim(charset);
78         }
79 }
80
81
82 // Sanitize and enhance an HTML message for display.
83 // Also convert weird character sets to UTF-8 if necessary.
84 // Also fixup img src="cid:..." type inline images to fetch the image
85 StrBuf *html2html(const char *charset_in, int treat_as_wiki, char *roomname, long msgnum, StrBuf *Source) {
86         char buf[SIZ];
87         char *msg;
88         char *ptr;
89         char *msgstart;
90         char *msgend;
91         StrBuf *converted_msg;
92         int buffer_length = 1;
93         int line_length = 0;
94         int content_length = 0;
95         char new_window[SIZ];
96         int brak = 0;
97         int alevel = 0;
98         int scriptlevel = 0;
99         int script_start_pos = (-1);
100         int i;
101         int linklen;
102         char charset[128];
103         StrBuf *BodyArea = NULL;
104
105         iconv_t ic = (iconv_t) (-1);
106         char *ibuf;             // Buffer of characters to be converted
107         char *obuf;             // Buffer for converted characters
108         size_t ibuflen;         // Length of input buffer
109         size_t obuflen;         // Length of output buffer
110         char *osav;             // Saved pointer to output buffer
111
112         StrBuf *Target = NewStrBuf();
113         if (Target == NULL) {
114                 return (NULL);
115         }
116
117         safestrncpy(charset, charset_in, sizeof charset);
118         sprintf(new_window, "<a target=\"%s\" href=", TARGET);
119
120         content_length = StrLength(Source);
121         msg = (char *) ChrPtr(Source);
122         buffer_length = content_length;
123
124         // Do a first pass to isolate the message body
125         ptr = msg + 1;
126         msgstart = msg;
127         msgend = &msg[content_length];
128
129         while (ptr < msgend) {
130
131                 // Advance to next tag
132                 ptr = strchr(ptr, '<');
133                 if ((ptr == NULL) || (ptr >= msgend))
134                         break;
135                 ++ptr;
136                 if ((ptr == NULL) || (ptr >= msgend))
137                         break;
138
139                 //  Look for META tags.  Some messages (particularly in
140                 //  Asian locales) illegally declare a message's character
141                 //  set in the HTML instead of in the MIME headers.  This
142                 //  is wrong but we have to work around it anyway.
143                 if (!strncasecmp(ptr, "META", 4)) {
144
145                         char *meta_start;
146                         char *meta_end;
147                         int meta_length;
148                         char *meta;
149                         char *meta_http_equiv;
150                         char *meta_content;
151                         char *spaceptr;
152
153                         meta_start = &ptr[4];
154                         meta_end = strchr(ptr, '>');
155                         if ((meta_end != NULL) && (meta_end <= msgend)) {
156                                 meta_length = meta_end - meta_start + 1;
157                                 meta = malloc(meta_length + 1);
158                                 safestrncpy(meta, meta_start, meta_length);
159                                 meta[meta_length] = 0;
160                                 string_trim(meta);
161                                 if (!strncasecmp(meta, "HTTP-EQUIV=", 11)) {
162                                         meta_http_equiv = strdup(&meta[11]);
163                                         spaceptr = strchr(meta_http_equiv, ' ');
164                                         if (spaceptr != NULL) {
165                                                 *spaceptr = 0;
166                                                 meta_content = strdup(++spaceptr);
167                                                 if (!strncasecmp(meta_content, "content=", 8)) {
168                                                         strcpy(meta_content, &meta_content[8]);
169                                                         stripquotes(meta_http_equiv);
170                                                         stripquotes(meta_content);
171                                                         extract_charset_from_meta(charset, meta_http_equiv, meta_content);
172                                                 }
173                                                 free(meta_content);
174                                         }
175                                         free(meta_http_equiv);
176                                 }
177                                 free(meta);
178                         }
179                 }
180
181                 // Any of these tags cause everything up to and including
182                 // the tag to be removed.
183                 if (    (!strncasecmp(ptr, "HTML", 4))
184                         || (!strncasecmp(ptr, "HEAD", 4))
185                         || (!strncasecmp(ptr, "/HEAD", 5))
186                         || (!strncasecmp(ptr, "BODY", 4))
187                 ) {
188                         char *pBody = NULL;
189
190                         if (!strncasecmp(ptr, "BODY", 4)) {
191                                 pBody = ptr;
192                         }
193                         ptr = strchr(ptr, '>');
194                         if ((ptr == NULL) || (ptr >= msgend))
195                                 break;
196                         if ((pBody != NULL) && (ptr - pBody > 4)) {
197                                 char *src;
198                                 char *cid_start, *cid_end;
199
200                                 *ptr = '\0';
201                                 pBody += 4;
202                                 while ((isspace(*pBody)) && (pBody < ptr))
203                                         pBody++;
204                                 BodyArea = NewStrBufPlain(NULL, ptr - pBody);
205
206                                 if (pBody < ptr) {
207                                         src = strstr(pBody, "cid:");
208                                         if (src) {
209                                                 cid_start = src + 4;
210                                                 cid_end = cid_start;
211                                                 while ((*cid_end != '"') && !isspace(*cid_end) && (cid_end < ptr))
212                                                         cid_end++;
213
214                                                 // copy tag and attributes up to src="cid:
215                                                 StrBufAppendBufPlain(BodyArea, pBody, src - pBody, 0);
216
217                                                 // add in /webcit/mimepart/<msgno>/CID/ 
218                                                 // trailing / stops dumb URL filters getting excited
219                                                 StrBufAppendPrintf(BodyArea, "/webcit/mimepart/%ld/", msgnum);
220                                                 StrBufAppendBufPlain(BodyArea, cid_start, cid_end - cid_start, 0);
221
222                                                 if (ptr - cid_end > 0)
223                                                         StrBufAppendBufPlain(BodyArea, cid_end + 1, ptr - cid_end, 0);
224                                         }
225                                         else {
226                                                 StrBufAppendBufPlain(BodyArea, pBody, ptr - pBody, 0);
227                                         }
228                                 }
229                                 *ptr = '>';
230                         }
231                         ++ptr;
232                         if ((ptr == NULL) || (ptr >= msgend))
233                                 break;
234                         msgstart = ptr;
235                 }
236
237                 // Any of these tags cause everything including and following
238                 // the tag to be removed.
239                 if ((!strncasecmp(ptr, "/HTML", 5)) || (!strncasecmp(ptr, "/BODY", 5))) {
240                         --ptr;
241                         msgend = ptr;
242                         strcpy(ptr, "");
243                 }
244
245                 ++ptr;
246         }
247         if (msgstart > msg) {
248                 strcpy(msg, msgstart);
249         }
250
251         // Now go through the message, parsing tags as necessary.
252         converted_msg = NewStrBufPlain(NULL, content_length + 8192);
253
254         // Convert foreign character sets to UTF-8 if necessary
255         if (    (strcasecmp(charset, "us-ascii"))
256                 && (strcasecmp(charset, "UTF-8"))
257                 && (strcasecmp(charset, ""))
258         ) {
259                 syslog(LOG_DEBUG, "Converting %s to UTF-8", charset);
260                 ctdl_iconv_open("UTF-8", charset, &ic);
261                 if (ic == (iconv_t) (-1)) {
262                         syslog(LOG_WARNING, "%s:%d iconv_open() failed: %s", __FILE__, __LINE__, strerror(errno));
263                 }
264         }
265         if (Source == NULL) {
266                 if (ic != (iconv_t) (-1)) {
267                         ibuf = msg;
268                         ibuflen = content_length;
269                         obuflen = content_length + (content_length / 2);
270                         obuf = (char *) malloc(obuflen);
271                         osav = obuf;
272                         iconv(ic, &ibuf, &ibuflen, &obuf, &obuflen);
273                         content_length = content_length + (content_length / 2) - obuflen;
274                         osav[content_length] = 0;
275                         free(msg);
276                         msg = osav;
277                         iconv_close(ic);
278                 }
279         }
280         else {
281                 if (ic != (iconv_t) (-1)) {
282                         StrBuf *Buf = NewStrBufPlain(NULL, StrLength(Source) + 8096);;
283                         StrBufConvert(Source, Buf, &ic);
284                         FreeStrBuf(&Buf);
285                         iconv_close(ic);
286                         msg = (char *) ChrPtr(Source);  // TODO: get rid of this.
287                 }
288         }
289
290         // At this point, the message has been stripped down to
291         // only the content inside the <BODY></BODY> tags, and has
292         // been converted to UTF-8 if it was originally in a foreign
293         // character set.  The text is also guaranteed to be null
294         // terminated now.
295
296         if (converted_msg == NULL) {
297                 StrBufAppendPrintf(Target, "Error %d: %s<br>%s:%d", errno, strerror(errno), __FILE__, __LINE__);
298                 goto BAIL;
299         }
300
301         if (BodyArea != NULL) { // Any attributes that were declared in the <body> tag
302                 StrBufAppendBufPlain(converted_msg, HKEY("<div "), 0);  // are instead declared in this <div> tag
303                 StrBufAppendBuf(converted_msg, BodyArea, 0);
304                 StrBufAppendBufPlain(converted_msg, HKEY(">"), 0);
305         }
306         ptr = msg;
307         msgend = strchr(msg, 0);
308         while (ptr < msgend) {
309
310                 // Try to sanitize the html of any rogue scripts
311                 if (!strncasecmp(ptr, "<script", 7)) {
312                         if (scriptlevel == 0) {
313                                 script_start_pos = StrLength(converted_msg);
314                         }
315                         ++scriptlevel;
316                 }
317                 if (!strncasecmp(ptr, "</script", 8)) {
318                         --scriptlevel;
319                 }
320
321                 // Change mailto: links to WebCit mail, by replacing the
322                 // link with one that points back to our mail room.  Due to
323                 // the way we parse URL's, it'll even handle mailto: links
324                 // that have "?subject=" in them.
325                 // FIXME change URL syntax for webcit-ng
326                 if (!strncasecmp(ptr, "<a href=\"mailto:", 16)) {
327                         content_length += 64;
328                         StrBufAppendPrintf(converted_msg, "<a href=\"display_enter?force_room=_MAIL_?recp=");
329                         ptr = &ptr[16];
330                         ++alevel;
331                         ++brak;
332                 }
333
334                 // Make external links open in a separate window
335                 else if (!strncasecmp(ptr, "<a href=\"", 9)) {
336                         ++alevel;
337                         ++brak;
338                         if (((strchr(ptr, ':') < strchr(ptr, '/'))) && ((strchr(ptr, '/') < strchr(ptr, '>')))) {
339                                 // open external links to new window
340                                 StrBufAppendPrintf(converted_msg, new_window);
341                                 ptr = &ptr[8];
342                         }
343                         else if ((treat_as_wiki)
344                                 && (strncasecmp(ptr, "<a href=\"wiki?", 14))
345                                 && (strncasecmp(ptr, "<a href=\"dotgoto?", 17))
346                                 && (strncasecmp(ptr, "<a href=\"knrooms?", 17))
347                         ) {
348                                 content_length += 64;
349                                 StrBufAppendPrintf(converted_msg, "<a href=\"wiki?go=");
350                                 //StrBufUrlescAppend(converted_msg, "FIXME ROOM NAME", NULL);                   // FIXME make compatible with webcit-ng
351                                 StrBufAppendPrintf(converted_msg, "?page=");
352                                 ptr = &ptr[9];
353                         }
354                         else {
355                                 StrBufAppendPrintf(converted_msg, "<a href=\"");
356                                 ptr = &ptr[9];
357                         }
358                 }
359
360                 // Fixup <img src="cid:... ...> to fetch the mime part
361                 else if (!strncasecmp(ptr, "<img ", 5)) {
362                         char *cid_start, *cid_end;
363                         char *tag_end = strchr(ptr, '>');
364                         char *src;
365                         // FIXME - handle this situation (maybe someone opened an <img cid... 
366                         // and then ended the message)
367                         if (!tag_end) {
368                                 syslog(LOG_DEBUG, "tag_end is null and ptr is:");
369                                 syslog(LOG_DEBUG, "%s", ptr);
370                                 syslog(LOG_DEBUG, "Theoretical bytes remaining: %d", (int) (msgend - ptr));
371                         }
372
373                         src = strstr(ptr, "src=\"cid:");
374                         ++brak;
375
376                         if (    src
377                                 && isspace(*(src - 1))
378                                 && tag_end && (cid_start = strchr(src, ':'))
379                                 && (cid_end = strchr(cid_start, '"'))
380                                 && (cid_end < tag_end)
381                         ) {
382                                 // copy tag and attributes up to src="cid:
383                                 StrBufAppendBufPlain(converted_msg, ptr, src - ptr, 0);
384                                 cid_start++;
385
386                                 // add in /webcit/mimepart/<msgnum>/CID/ 
387                                 // trailing / stops dumb URL filters getting excited
388                                 StrBufAppendPrintf(converted_msg, " src=\"/ctdl/r/");
389                                 StrBufXMLEscAppend(converted_msg, NULL, roomname, strlen(roomname), 0);
390                                 syslog(LOG_DEBUG, "room name is '%s'", roomname);
391                                 StrBufAppendPrintf(converted_msg, "/%ld/", msgnum);
392                                 StrBufAppendBufPlain(converted_msg, cid_start, cid_end - cid_start, 0);
393                                 StrBufAppendBufPlain(converted_msg, "\"", -1, 0);
394                                 ptr = cid_end + 1;
395                         }
396                         StrBufAppendBufPlain(converted_msg, ptr, tag_end - ptr, 0);
397                         ptr = tag_end;
398                 }
399
400                 // Turn anything that looks like a URL into a real link, as long
401                 // as it's not inside a tag already
402                 else if ((brak == 0) && (alevel == 0) && ((!strncasecmp(ptr, "http://", 7)) || (!strncasecmp(ptr, "https://", 8)))) {
403                         // Find the end of the link
404                         int strlenptr;
405                         linklen = 0;
406
407                         strlenptr = strlen(ptr);
408                         for (i = 0; i <= strlenptr; ++i) {
409                                 if (    (ptr[i] == 0)
410                                         || (isspace(ptr[i]))
411                                         || (ptr[i] == 10)
412                                         || (ptr[i] == 13)
413                                         || (ptr[i] == '(')
414                                         || (ptr[i] == ')')
415                                         || (ptr[i] == '<')
416                                         || (ptr[i] == '>')
417                                         || (ptr[i] == '[')
418                                         || (ptr[i] == ']')
419                                         || (ptr[i] == '"')
420                                         || (ptr[i] == '\'')
421                                 ) {
422                                         linklen = i;
423                                 }
424                                 // entity tag?
425                                 if (ptr[i] == '&') {
426                                         if ((ptr[i + 2] == ';') ||
427                                             (ptr[i + 3] == ';') ||
428                                             (ptr[i + 5] == ';') || (ptr[i + 6] == ';') || (ptr[i + 7] == ';'))
429                                                 linklen = i;
430                                 }
431                                 if (linklen > 0)
432                                         break;
433                         }
434                         if (linklen > 0) {
435                                 char *ltreviewptr;
436                                 char *nbspreviewptr;
437                                 char linkedchar;
438                                 int len;
439
440                                 len = linklen;
441                                 linkedchar = ptr[len];
442                                 ptr[len] = '\0';
443                                 // spot for some subject strings tinymce tends to give us.
444                                 ltreviewptr = strchr(ptr, '<');
445                                 if (ltreviewptr != NULL) {
446                                         *ltreviewptr = '\0';
447                                         linklen = ltreviewptr - ptr;
448                                 }
449
450                                 nbspreviewptr = strstr(ptr, "&nbsp;");
451                                 if (nbspreviewptr != NULL) {
452                                         // nbspreviewptr = '\0';
453                                         linklen = nbspreviewptr - ptr;
454                                 }
455                                 if (ltreviewptr != 0)
456                                         *ltreviewptr = '<';
457
458                                 ptr[len] = linkedchar;
459
460                                 content_length += (32 + linklen);
461                                 StrBufAppendPrintf(converted_msg, "%s\"", new_window);
462                                 StrBufAppendBufPlain(converted_msg, ptr, linklen, 0);
463                                 StrBufAppendPrintf(converted_msg, "\">");
464                                 StrBufAppendBufPlain(converted_msg, ptr, linklen, 0);
465                                 ptr += linklen;
466                                 StrBufAppendPrintf(converted_msg, "</a>");
467                         }
468                 }
469                 else {
470                         StrBufAppendBufPlain(converted_msg, ptr, 1, 0);
471                         ptr++;
472                 }
473
474                 if ((ptr >= msg) && (ptr <= msgend)) {
475                         // We need to know when we're inside a tag,
476                         // so we don't turn things that look like URL's into
477                         // links, when they're already links - or image sources.
478                         if ((ptr > msg) && (*(ptr - 1) == '<')) {
479                                 ++brak;
480                         }
481                         if ((ptr > msg) && (*(ptr - 1) == '>')) {
482                                 --brak;
483                                 if ((scriptlevel == 0) && (script_start_pos >= 0)) {
484                                         StrBufCutRight(converted_msg, StrLength(converted_msg) - script_start_pos);
485                                         script_start_pos = (-1);
486                                 }
487                         }
488                         if (!strncasecmp(ptr, "</a>", 3))
489                                 --alevel;
490                 }
491         }
492
493         if (BodyArea != NULL) {
494                 StrBufAppendBufPlain(converted_msg, HKEY("</div>"), 0); // Close the div where we declared attributes copied
495                 FreeStrBuf(&BodyArea);  // from the original <body> tag
496         }
497
498         //      uncomment these two lines to override conversion
499         //      memcpy(converted_msg, msg, content_length);
500         //      output_length = content_length;
501
502         // Output our big pile of markup
503         StrBufAppendBuf(Target, converted_msg, 0);
504
505 BAIL:   // A little trailing vertical whitespace...
506         StrBufAppendPrintf(Target, "<br>\n");
507
508         // Now give back the memory
509         FreeStrBuf(&converted_msg);
510         if ((msg != NULL) && (Source == NULL))
511                 free(msg);
512         return (Target);
513 }
514
515
516 // Look for URL's embedded in a buffer and make them linkable.  We use a
517 // target window in order to keep the Citadel session in its own window.
518 void UrlizeText(StrBuf * Target, StrBuf * Source, StrBuf * WrkBuf) {
519         int len, UrlLen, Offset, TrailerLen;
520         const char *start, *end, *pos;
521
522         FlushStrBuf(Target);
523         start = NULL;
524         len = StrLength(Source);
525         end = ChrPtr(Source) + len;
526         for (pos = ChrPtr(Source); (pos < end) && (start == NULL); ++pos) {
527                 if (!strncasecmp(pos, "http://", 7))
528                         start = pos;
529                 else if (!strncasecmp(pos, "ftp://", 6))
530                         start = pos;
531         }
532
533         if (start == NULL) {
534                 StrBufAppendBuf(Target, Source, 0);
535                 return;
536         }
537         FlushStrBuf(WrkBuf);
538
539         for (pos = ChrPtr(Source) + len; pos > start; --pos) {
540                 if ((!isprint(*pos))
541                     || (isspace(*pos))
542                     || (*pos == '{')
543                     || (*pos == '}')
544                     || (*pos == '|')
545                     || (*pos == '\\')
546                     || (*pos == '^')
547                     || (*pos == '[')
548                     || (*pos == ']')
549                     || (*pos == '`')
550                     || (*pos == '<')
551                     || (*pos == '>')
552                     || (*pos == '(')
553                     || (*pos == ')')
554                     ) {
555                         end = pos;
556                 }
557         }
558
559         UrlLen = end - start;
560         StrBufAppendBufPlain(WrkBuf, start, UrlLen, 0);
561
562         Offset = start - ChrPtr(Source);
563         if (Offset != 0)
564                 StrBufAppendBufPlain(Target, ChrPtr(Source), Offset, 0);
565         StrBufAppendPrintf(Target, "%ca href=%c%s%c TARGET=%c%s%c%c%s%c/A%c",
566                            LB, QU, ChrPtr(WrkBuf), QU, QU, TARGET, QU, RB, ChrPtr(WrkBuf), LB, RB);
567
568         TrailerLen = StrLength(Source) - (end - ChrPtr(Source));
569         if (TrailerLen > 0)
570                 StrBufAppendBufPlain(Target, end, TrailerLen, 0);
571 }
572
573
574 void url(char *buf, size_t bufsize) {
575         int len, UrlLen, Offset, TrailerLen, outpos;
576         char *start, *end, *pos;
577         char urlbuf[SIZ];
578         char outbuf[SIZ];
579
580         start = NULL;
581         len = strlen(buf);
582         if (len > bufsize) {
583                 syslog(LOG_WARNING, "URL: content longer than buffer!");
584                 return;
585         }
586         end = buf + len;
587         for (pos = buf; (pos < end) && (start == NULL); ++pos) {
588                 if (!strncasecmp(pos, "http://", 7))
589                         start = pos;
590                 if (!strncasecmp(pos, "ftp://", 6))
591                         start = pos;
592         }
593
594         if (start == NULL)
595                 return;
596
597         for (pos = buf + len; pos > start; --pos) {
598                 if ((!isprint(*pos))
599                     || (isspace(*pos))
600                     || (*pos == '{')
601                     || (*pos == '}')
602                     || (*pos == '|')
603                     || (*pos == '\\')
604                     || (*pos == '^')
605                     || (*pos == '[')
606                     || (*pos == ']')
607                     || (*pos == '`')
608                     || (*pos == '<')
609                     || (*pos == '>')
610                     || (*pos == '(')
611                     || (*pos == ')')
612                     ) {
613                         end = pos;
614                 }
615         }
616
617         UrlLen = end - start;
618         if (UrlLen > sizeof(urlbuf)) {
619                 syslog(LOG_WARNING, "URL: content longer than buffer!");
620                 return;
621         }
622         memcpy(urlbuf, start, UrlLen);
623         urlbuf[UrlLen] = '\0';
624
625         Offset = start - buf;
626         if ((Offset != 0) && (Offset < sizeof(outbuf)))
627                 memcpy(outbuf, buf, Offset);
628         outpos = snprintf(&outbuf[Offset], sizeof(outbuf) - Offset,
629                           "%ca href=%c%s%c TARGET=%c%s%c%c%s%c/A%c", LB, QU, urlbuf, QU, QU, TARGET, QU, RB, urlbuf, LB, RB);
630         if (outpos >= sizeof(outbuf) - Offset) {
631                 syslog(LOG_WARNING, "URL: content longer than buffer!");
632                 return;
633         }
634
635         TrailerLen = len - (end - start);
636         if (TrailerLen > 0)
637                 memcpy(outbuf + Offset + outpos, end, TrailerLen);
638         if (Offset + outpos + TrailerLen > bufsize) {
639                 syslog(LOG_WARNING, "URL: content longer than buffer!");
640                 return;
641         }
642         memcpy(buf, outbuf, Offset + outpos + TrailerLen);
643         *(buf + Offset + outpos + TrailerLen) = '\0';
644 }