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