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