Rocco Rutte:
[apps/madmutt.git] / rfc2047.c
1 /*
2  * Copyright notice from original mutt:
3  * Copyright (C) 1996-2000 Michael R. Elkins <me@mutt.org>
4  * Copyright (C) 2000-2001 Edmund Grimley Evans <edmundo@rano.org>
5  *
6  * This file is part of mutt-ng, see http://www.muttng.org/.
7  * It's licensed under the GNU General Public License,
8  * please see the file GPL in the top level source directory.
9  */
10
11 #if HAVE_CONFIG_H
12 # include "config.h"
13 #endif
14
15 #include "mutt.h"
16 #include "mime.h"
17 #include "charset.h"
18 #include "rfc2047.h"
19
20 #include "lib/mem.h"
21 #include "lib/str.h"
22
23 #include <ctype.h>
24 #include <errno.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28
29 /* If you are debugging this file, comment out the following line. */
30 /*#define NDEBUG*/
31
32 #ifdef NDEBUG
33 #define assert(x)
34 #else
35 #include <assert.h>
36 #endif
37
38 #define ENCWORD_LEN_MAX 75
39 #define ENCWORD_LEN_MIN 9       /* str_len ("=?.?.?.?=") */
40
41 #define HSPACE(x) ((x) == '\0' || (x) == ' ' || (x) == '\t')
42
43 #define CONTINUATION_BYTE(c) (((c) & 0xc0) == 0x80)
44
45 extern char RFC822Specials[];
46
47 typedef size_t (*encoder_t) (char *, ICONV_CONST char *, size_t,
48                              const char *);
49
50 static size_t convert_string (ICONV_CONST char *f, size_t flen,
51                               const char *from, const char *to,
52                               char **t, size_t * tlen)
53 {
54   iconv_t cd;
55   char *buf, *ob;
56   size_t obl, n;
57   int e;
58
59   cd = mutt_iconv_open (to, from, 0);
60   if (cd == (iconv_t) (-1))
61     return (size_t) (-1);
62   obl = 4 * flen + 1;
63   ob = buf = safe_malloc (obl);
64   n = iconv (cd, &f, &flen, &ob, &obl);
65   if (n == (size_t) (-1) || iconv (cd, 0, 0, &ob, &obl) == (size_t) (-1)) {
66     e = errno;
67     FREE (&buf);
68     iconv_close (cd);
69     errno = e;
70     return (size_t) (-1);
71   }
72   *ob = '\0';
73
74   *tlen = ob - buf;
75
76   safe_realloc (&buf, ob - buf + 1);
77   *t = buf;
78   iconv_close (cd);
79
80   return n;
81 }
82
83 char *mutt_choose_charset (const char *fromcode, const char *charsets,
84                            char *u, size_t ulen, char **d, size_t * dlen)
85 {
86   char canonical_buff[LONG_STRING];
87   char *e = 0, *tocode = 0;
88   size_t elen = 0, bestn = 0;
89   const char *p, *q;
90
91   for (p = charsets; p; p = q ? q + 1 : 0) {
92     char *s, *t;
93     size_t slen, n;
94
95     q = strchr (p, ':');
96
97     n = q ? q - p : str_len (p);
98
99     if (!n ||
100         /* Assume that we never need more than 12 characters of
101            encoded-text to encode a single character. */
102         n > (ENCWORD_LEN_MAX - ENCWORD_LEN_MIN + 2 - 12))
103       continue;
104
105     t = safe_malloc (n + 1);
106     memcpy (t, p, n);
107     t[n] = '\0';
108
109     n = convert_string (u, ulen, fromcode, t, &s, &slen);
110     if (n == (size_t) (-1))
111       continue;
112
113     if (!tocode || n < bestn) {
114       bestn = n;
115       FREE (&tocode);
116       tocode = t;
117       if (d) {
118         FREE (&e);
119         e = s;
120       }
121       else
122         FREE (&s);
123       elen = slen;
124       if (!bestn)
125         break;
126     }
127     else {
128       FREE (&t);
129       FREE (&s);
130     }
131   }
132   if (tocode) {
133     if (d)
134       *d = e;
135     if (dlen)
136       *dlen = elen;
137
138     mutt_canonical_charset (canonical_buff, sizeof (canonical_buff), tocode);
139     str_replace (&tocode, canonical_buff);
140   }
141   return tocode;
142 }
143
144 static size_t b_encoder (char *s, ICONV_CONST char *d, size_t dlen,
145                          const char *tocode)
146 {
147   char *s0 = s;
148
149   memcpy (s, "=?", 2), s += 2;
150   memcpy (s, tocode, str_len (tocode)), s += str_len (tocode);
151   memcpy (s, "?B?", 3), s += 3;
152   for (;;) {
153     if (!dlen)
154       break;
155     else if (dlen == 1) {
156       *s++ = B64Chars[(*d >> 2) & 0x3f];
157       *s++ = B64Chars[(*d & 0x03) << 4];
158       *s++ = '=';
159       *s++ = '=';
160       break;
161     }
162     else if (dlen == 2) {
163       *s++ = B64Chars[(*d >> 2) & 0x3f];
164       *s++ = B64Chars[((*d & 0x03) << 4) | ((d[1] >> 4) & 0x0f)];
165       *s++ = B64Chars[(d[1] & 0x0f) << 2];
166       *s++ = '=';
167       break;
168     }
169     else {
170       *s++ = B64Chars[(*d >> 2) & 0x3f];
171       *s++ = B64Chars[((*d & 0x03) << 4) | ((d[1] >> 4) & 0x0f)];
172       *s++ = B64Chars[((d[1] & 0x0f) << 2) | ((d[2] >> 6) & 0x03)];
173       *s++ = B64Chars[d[2] & 0x3f];
174       d += 3, dlen -= 3;
175     }
176   }
177   memcpy (s, "?=", 2), s += 2;
178   return s - s0;
179 }
180
181 static size_t q_encoder (char *s, ICONV_CONST char *d, size_t dlen,
182                          const char *tocode)
183 {
184   char hex[] = "0123456789ABCDEF";
185   char *s0 = s;
186
187   memcpy (s, "=?", 2), s += 2;
188   memcpy (s, tocode, str_len (tocode)), s += str_len (tocode);
189   memcpy (s, "?Q?", 3), s += 3;
190   while (dlen--) {
191     unsigned char c = *d++;
192
193     if (c == ' ')
194       *s++ = '_';
195     else if (c >= 0x7f || c < 0x20 || c == '_' || strchr (MimeSpecials, c)) {
196       *s++ = '=';
197       *s++ = hex[(c & 0xf0) >> 4];
198       *s++ = hex[c & 0x0f];
199     }
200     else
201       *s++ = c;
202   }
203   memcpy (s, "?=", 2), s += 2;
204   return s - s0;
205 }
206
207 /*
208  * Return 0 if and set *encoder and *wlen if the data (d, dlen) could
209  * be converted to an encoded word of length *wlen using *encoder.
210  * Otherwise return an upper bound on the maximum length of the data
211  * which could be converted.
212  * The data is converted from fromcode (which must be stateless) to
213  * tocode, unless fromcode is 0, in which case the data is assumed to
214  * be already in tocode, which should be 8-bit and stateless.
215  */
216 static size_t try_block (ICONV_CONST char *d, size_t dlen,
217                          const char *fromcode, const char *tocode,
218                          encoder_t * encoder, size_t * wlen)
219 {
220   char buf1[ENCWORD_LEN_MAX - ENCWORD_LEN_MIN + 1];
221   iconv_t cd;
222   ICONV_CONST char *ib;
223   char *ob, *p;
224   size_t ibl, obl;
225   int count, len, len_b, len_q;
226
227   if (fromcode) {
228     cd = mutt_iconv_open (tocode, fromcode, 0);
229     assert (cd != (iconv_t) (-1));
230     ib = d, ibl = dlen, ob = buf1, obl = sizeof (buf1) - str_len (tocode);
231     if (iconv (cd, &ib, &ibl, &ob, &obl) == (size_t) (-1) ||
232         iconv (cd, 0, 0, &ob, &obl) == (size_t) (-1)) {
233       assert (errno == E2BIG);
234       iconv_close (cd);
235       assert (ib > d);
236       return (ib - d == dlen) ? dlen : ib - d + 1;
237     }
238     iconv_close (cd);
239   }
240   else {
241     if (dlen > sizeof (buf1) - str_len (tocode))
242       return sizeof (buf1) - str_len (tocode) + 1;
243     memcpy (buf1, d, dlen);
244     ob = buf1 + dlen;
245   }
246
247   count = 0;
248   for (p = buf1; p < ob; p++) {
249     unsigned char c = *p;
250
251     assert (strchr (MimeSpecials, '?'));
252     if (c >= 0x7f || c < 0x20 || *p == '_' ||
253         (c != ' ' && strchr (MimeSpecials, *p)))
254       ++count;
255   }
256
257   len = ENCWORD_LEN_MIN - 2 + str_len (tocode);
258   len_b = len + (((ob - buf1) + 2) / 3) * 4;
259   len_q = len + (ob - buf1) + 2 * count;
260
261   /* Apparently RFC 1468 says to use B encoding for iso-2022-jp. */
262   if (!ascii_strcasecmp (tocode, "ISO-2022-JP"))
263     len_q = ENCWORD_LEN_MAX + 1;
264
265   if (len_b < len_q && len_b <= ENCWORD_LEN_MAX) {
266     *encoder = b_encoder;
267     *wlen = len_b;
268     return 0;
269   }
270   else if (len_q <= ENCWORD_LEN_MAX) {
271     *encoder = q_encoder;
272     *wlen = len_q;
273     return 0;
274   }
275   else
276     return dlen;
277 }
278
279 /*
280  * Encode the data (d, dlen) into s using the encoder.
281  * Return the length of the encoded word.
282  */
283 static size_t encode_block (char *s, char *d, size_t dlen,
284                             const char *fromcode, const char *tocode,
285                             encoder_t encoder)
286 {
287   char buf1[ENCWORD_LEN_MAX - ENCWORD_LEN_MIN + 1];
288   iconv_t cd;
289   ICONV_CONST char *ib;
290   char *ob;
291   size_t ibl, obl, n1, n2;
292
293   if (fromcode) {
294     cd = mutt_iconv_open (tocode, fromcode, 0);
295     assert (cd != (iconv_t) (-1));
296     ib = d, ibl = dlen, ob = buf1, obl = sizeof (buf1) - str_len (tocode);
297     n1 = iconv (cd, &ib, &ibl, &ob, &obl);
298     n2 = iconv (cd, 0, 0, &ob, &obl);
299     assert (n1 != (size_t) (-1) && n2 != (size_t) (-1));
300     iconv_close (cd);
301     return (*encoder) (s, buf1, ob - buf1, tocode);
302   }
303   else
304     return (*encoder) (s, d, dlen, tocode);
305 }
306
307 /*
308  * Discover how much of the data (d, dlen) can be converted into
309  * a single encoded word. Return how much data can be converted,
310  * and set the length *wlen of the encoded word and *encoder.
311  * We start in column col, which limits the length of the word.
312  */
313 static size_t choose_block (char *d, size_t dlen, int col,
314                             const char *fromcode, const char *tocode,
315                             encoder_t * encoder, size_t * wlen)
316 {
317   size_t n, nn;
318   int utf8 = fromcode && !ascii_strcasecmp (fromcode, "UTF-8");
319
320   n = dlen;
321   for (;;) {
322     assert (d + n > d);
323     nn = try_block (d, n, fromcode, tocode, encoder, wlen);
324     if (!nn && (col + *wlen <= ENCWORD_LEN_MAX + 1 || n <= 1))
325       break;
326     n = (nn ? nn : n) - 1;
327     assert (n > 0);
328     if (utf8)
329       while (n > 1 && CONTINUATION_BYTE (d[n]))
330         --n;
331   }
332   return n;
333 }
334
335 /*
336  * Place the result of RFC-2047-encoding (d, dlen) into the dynamically
337  * allocated buffer (e, elen). The input data is in charset fromcode
338  * and is converted into a charset chosen from charsets.
339  * Return 1 if the conversion to UTF-8 failed, 2 if conversion from UTF-8
340  * failed, otherwise 0. If conversion failed, fromcode is assumed to be
341  * compatible with us-ascii and the original data is used.
342  * The input data is assumed to be a single line starting at column col;
343  * if col is non-zero, the preceding character was a space.
344  */
345 static int rfc2047_encode (ICONV_CONST char *d, size_t dlen, int col,
346                            const char *fromcode, const char *charsets,
347                            char **e, size_t * elen, char *specials)
348 {
349   int ret = 0;
350   char *buf;
351   size_t bufpos, buflen;
352   char *u, *t0, *t1, *t;
353   char *s0, *s1;
354   size_t ulen, r, n, wlen;
355   encoder_t encoder;
356   char *tocode1 = 0;
357   const char *tocode;
358   char *icode = "UTF-8";
359
360   /* Try to convert to UTF-8. */
361   if (convert_string (d, dlen, fromcode, icode, &u, &ulen)) {
362     ret = 1;
363     icode = 0;
364     u = safe_malloc ((ulen = dlen) + 1);
365     memcpy (u, d, dlen);
366     u[ulen] = 0;
367   }
368
369   /* Find earliest and latest things we must encode. */
370   s0 = s1 = t0 = t1 = 0;
371   for (t = u; t < u + ulen; t++) {
372     if ((*t & 0x80) ||
373         (*t == '=' && t[1] == '?' && (t == u || HSPACE (*(t - 1))))) {
374       if (!t0)
375         t0 = t;
376       t1 = t;
377     }
378     else if (specials && strchr (specials, *t)) {
379       if (!s0)
380         s0 = t;
381       s1 = t;
382     }
383   }
384
385   /* If we have something to encode, include RFC822 specials */
386   if (t0 && s0 && s0 < t0)
387     t0 = s0;
388   if (t1 && s1 && s1 > t1)
389     t1 = s1;
390
391   if (!t0) {
392     /* No encoding is required. */
393     *e = u;
394     *elen = ulen;
395     return ret;
396   }
397
398   /* Choose target charset. */
399   tocode = fromcode;
400   if (icode) {
401     if ((tocode1 = mutt_choose_charset (icode, charsets, u, ulen, 0, 0)))
402       tocode = tocode1;
403     else
404       ret = 2, icode = 0;
405   }
406
407   /* Hack to avoid labelling 8-bit data as us-ascii. */
408   if (!icode && mutt_is_us_ascii (tocode))
409     tocode = "unknown-8bit";
410
411   /* Adjust t0 for maximum length of line. */
412   t = u + (ENCWORD_LEN_MAX + 1) - col - ENCWORD_LEN_MIN;
413   if (t < u)
414     t = u;
415   if (t < t0)
416     t0 = t;
417
418
419   /* Adjust t0 until we can encode a character after a space. */
420   for (; t0 > u; t0--) {
421     if (!HSPACE (*(t0 - 1)))
422       continue;
423     t = t0 + 1;
424     if (icode)
425       while (t < u + ulen && CONTINUATION_BYTE (*t))
426         ++t;
427     if (!try_block (t0, t - t0, icode, tocode, &encoder, &wlen) &&
428         col + (t0 - u) + wlen <= ENCWORD_LEN_MAX + 1)
429       break;
430   }
431
432   /* Adjust t1 until we can encode a character before a space. */
433   for (; t1 < u + ulen; t1++) {
434     if (!HSPACE (*t1))
435       continue;
436     t = t1 - 1;
437     if (icode)
438       while (CONTINUATION_BYTE (*t))
439         --t;
440     if (!try_block (t, t1 - t, icode, tocode, &encoder, &wlen) &&
441         1 + wlen + (u + ulen - t1) <= ENCWORD_LEN_MAX + 1)
442       break;
443   }
444
445   /* We shall encode the region [t0,t1). */
446
447   /* Initialise the output buffer with the us-ascii prefix. */
448   buflen = 2 * ulen;
449   buf = safe_malloc (buflen);
450   bufpos = t0 - u;
451   memcpy (buf, u, t0 - u);
452
453   col += t0 - u;
454
455   t = t0;
456   for (;;) {
457     /* Find how much we can encode. */
458     n = choose_block (t, t1 - t, col, icode, tocode, &encoder, &wlen);
459     if (n == t1 - t) {
460       /* See if we can fit the us-ascii suffix, too. */
461       if (col + wlen + (u + ulen - t1) <= ENCWORD_LEN_MAX + 1)
462         break;
463       n = t1 - t - 1;
464       if (icode)
465         while (CONTINUATION_BYTE (t[n]))
466           --n;
467       assert (t + n >= t);
468       if (!n) {
469         /* This should only happen in the really stupid case where the
470            only word that needs encoding is one character long, but
471            there is too much us-ascii stuff after it to use a single
472            encoded word. We add the next word to the encoded region
473            and try again. */
474         assert (t1 < u + ulen);
475         for (t1++; t1 < u + ulen && !HSPACE (*t1); t1++);
476         continue;
477       }
478       n = choose_block (t, n, col, icode, tocode, &encoder, &wlen);
479     }
480
481     /* Add to output buffer. */
482 #define LINEBREAK "\n\t"
483     if (bufpos + wlen + str_len (LINEBREAK) > buflen) {
484       buflen = bufpos + wlen + str_len (LINEBREAK);
485       safe_realloc (&buf, buflen);
486     }
487     r = encode_block (buf + bufpos, t, n, icode, tocode, encoder);
488     assert (r == wlen);
489     bufpos += wlen;
490     memcpy (buf + bufpos, LINEBREAK, str_len (LINEBREAK));
491     bufpos += str_len (LINEBREAK);
492 #undef LINEBREAK
493
494     col = 1;
495
496     t += n;
497   }
498
499   /* Add last encoded word and us-ascii suffix to buffer. */
500   buflen = bufpos + wlen + (u + ulen - t1);
501   safe_realloc (&buf, buflen + 1);
502   r = encode_block (buf + bufpos, t, t1 - t, icode, tocode, encoder);
503   assert (r == wlen);
504   bufpos += wlen;
505   memcpy (buf + bufpos, t1, u + ulen - t1);
506
507   FREE (&tocode1);
508   FREE (&u);
509
510   buf[buflen] = '\0';
511
512   *e = buf;
513   *elen = buflen + 1;
514   return ret;
515 }
516
517 void _rfc2047_encode_string (char **pd, int encode_specials, int col)
518 {
519   char *e;
520   size_t elen;
521   char *charsets;
522
523   if (!Charset || !*pd)
524     return;
525
526   charsets = SendCharset;
527   if (!charsets || !*charsets)
528     charsets = "UTF-8";
529
530   rfc2047_encode (*pd, str_len (*pd), col,
531                   Charset, charsets, &e, &elen,
532                   encode_specials ? RFC822Specials : NULL);
533
534   FREE (pd);
535   *pd = e;
536 }
537
538 void rfc2047_encode_adrlist (ADDRESS * addr, const char *tag)
539 {
540   ADDRESS *ptr = addr;
541   int col = tag ? str_len (tag) + 2 : 32;
542
543   while (ptr) {
544     if (ptr->personal)
545       _rfc2047_encode_string (&ptr->personal, 1, col);
546     ptr = ptr->next;
547   }
548 }
549
550 static int rfc2047_decode_word (char *d, const char *s, size_t len)
551 {
552   const char *pp, *pp1;
553   char *pd, *d0;
554   const char *t, *t1;
555   int enc = 0, count = 0;
556   char *charset = NULL;
557
558   pd = d0 = safe_malloc (str_len (s));
559
560   for (pp = s; (pp1 = strchr (pp, '?')); pp = pp1 + 1) {
561     count++;
562     switch (count) {
563     case 2:
564       /* ignore language specification a la RFC 2231 */
565       t = pp1;
566       if ((t1 = memchr (pp, '*', t - pp)))
567         t = t1;
568       charset = safe_malloc (t - pp + 1);
569       memcpy (charset, pp, t - pp);
570       charset[t - pp] = '\0';
571       break;
572     case 3:
573       if (toupper ((unsigned char) *pp) == 'Q')
574         enc = ENCQUOTEDPRINTABLE;
575       else if (toupper ((unsigned char) *pp) == 'B')
576         enc = ENCBASE64;
577       else {
578         FREE (&charset);
579         FREE (&d0);
580         return (-1);
581       }
582       break;
583     case 4:
584       if (enc == ENCQUOTEDPRINTABLE) {
585         for (; pp < pp1; pp++) {
586           if (*pp == '_')
587             *pd++ = ' ';
588           else if (*pp == '=' &&
589                    (!(pp[1] & ~127) && hexval (pp[1]) != -1) &&
590                    (!(pp[2] & ~127) && hexval (pp[2]) != -1)) {
591             *pd++ = (hexval (pp[1]) << 4) | hexval (pp[2]);
592             pp += 2;
593           }
594           else
595             *pd++ = *pp;
596         }
597         *pd = 0;
598       }
599       else if (enc == ENCBASE64) {
600         int c, b = 0, k = 0;
601
602         for (; pp < pp1; pp++) {
603           if (*pp == '=')
604             break;
605           if ((*pp & ~127) || (c = base64val (*pp)) == -1)
606             continue;
607           if (k + 6 >= 8) {
608             k -= 2;
609             *pd++ = b | (c >> k);
610             b = c << (8 - k);
611           }
612           else {
613             b |= c << (k + 2);
614             k += 6;
615           }
616         }
617         *pd = 0;
618       }
619       break;
620     }
621   }
622
623   if (charset)
624     mutt_convert_string (&d0, charset, Charset, M_ICONV_HOOK_FROM);
625   strfcpy (d, d0, len);
626   FREE (&charset);
627   FREE (&d0);
628   return (0);
629 }
630
631 /*
632  * Find the start and end of the first encoded word in the string.
633  * We use the grammar in section 2 of RFC 2047, but the "encoding"
634  * must be B or Q. Also, we don't require the encoded word to be
635  * separated by linear-white-space (section 5(1)).
636  */
637 static const char *find_encoded_word (const char *s, const char **x)
638 {
639   const char *p, *q;
640
641   q = s;
642   while ((p = strstr (q, "=?"))) {
643     for (q = p + 2;
644          0x20 < *q && *q < 0x7f && !strchr ("()<>@,;:\"/[]?.=", *q); q++);
645     if (q[0] != '?' || !strchr ("BbQq", q[1]) || q[2] != '?')
646       continue;
647     for (q = q + 3; 0x20 <= *q && *q < 0x7f && *q != '?'; q++);
648     if (q[0] != '?' || q[1] != '=') {
649       --q;
650       continue;
651     }
652
653     *x = q + 2;
654     return p;
655   }
656
657   return 0;
658 }
659
660 /* return length of linear white space */
661 static size_t lwslen (const char *s, size_t n)
662 {
663   const char *p = s;
664   size_t len = n;
665
666   if (n <= 0)
667     return 0;
668
669   for (; p < s + n; p++)
670     if (!strchr (" \t\r\n", *p)) {
671       len = (size_t) (p - s);
672       break;
673     }
674   if (strchr ("\r\n", *(p - 1)))        /* LWS doesn't end with CRLF */
675     len = (size_t) 0;
676   return len;
677 }
678
679 /* return length of linear white space : reverse */
680 static size_t lwsrlen (const char *s, size_t n)
681 {
682   const char *p = s + n - 1;
683   size_t len = n;
684
685   if (n <= 0)
686     return 0;
687
688   if (strchr ("\r\n", *p))      /* LWS doesn't end with CRLF */
689     return (size_t) 0;
690
691   for (; p >= s; p--)
692     if (!strchr (" \t\r\n", *p)) {
693       len = (size_t) (s + n - 1 - p);
694       break;
695     }
696   return len;
697 }
698
699 /* try to decode anything that looks like a valid RFC2047 encoded
700  * header field, ignoring RFC822 parsing rules
701  */
702 void rfc2047_decode (char **pd)
703 {
704   const char *p, *q;
705   size_t m, n;
706   int found_encoded = 0;
707   char *d0, *d;
708   const char *s = *pd;
709   size_t dlen;
710
711   if (!s || !*s)
712     return;
713
714   dlen = 4 * str_len (s);        /* should be enough */
715   d = d0 = safe_malloc (dlen + 1);
716
717   while (*s && dlen > 0) {
718     if (!(p = find_encoded_word (s, &q))) {
719       /* no encoded words */
720       if (!option (OPTSTRICTMIME)) {
721         n = str_len (s);
722         if (found_encoded && (m = lwslen (s, n)) != 0) {
723           if (m != n)
724             *d = ' ', d++, dlen--;
725           n -= m, s += m;
726         }
727         if (ascii_strcasecmp (AssumedCharset, "us-ascii")) {
728           char *t;
729           size_t tlen;
730
731           t = safe_malloc (n + 1);
732           strfcpy (t, s, n + 1);
733           if (mutt_convert_nonmime_string (&t) == 0) {
734             tlen = str_len (t);
735             strncpy (d, t, tlen);
736             d += tlen;
737           }
738           else {
739             strncpy (d, s, n);
740             d += n;
741           }
742           FREE (&t);
743           break;
744         }
745       }
746       strncpy (d, s, dlen);
747       d += dlen;
748       break;
749     }
750
751     if (p != s) {
752       n = (size_t) (p - s);
753       /* ignore spaces between encoded words
754        * and linear white spaces between encoded word and *text */
755       if (!option (OPTSTRICTMIME)) {
756         if (found_encoded && (m = lwslen (s, n)) != 0) {
757           if (m != n)
758             *d = ' ', d++, dlen--;
759           n -= m, s += m;
760         }
761
762         if ((m = n - lwsrlen (s, n)) != 0) {
763           if (m > dlen)
764             m = dlen;
765           memcpy (d, s, m);
766           d += m;
767           dlen -= m;
768           if (m != n)
769             *d = ' ', d++, dlen--;
770         }
771       }
772       else if (!found_encoded || strspn (s, " \t\r\n") != n) {
773         if (n > dlen)
774           n = dlen;
775         memcpy (d, s, n);
776         d += n;
777         dlen -= n;
778       }
779     }
780
781     rfc2047_decode_word (d, p, dlen);
782     found_encoded = 1;
783     s = q;
784     n = str_len (d);
785     dlen -= n;
786     d += n;
787   }
788   *d = 0;
789
790   FREE (pd);
791   *pd = d0;
792   str_adjust (pd);
793 }
794
795 void rfc2047_decode_adrlist (ADDRESS * a)
796 {
797   while (a) {
798     if (a->personal)
799       rfc2047_decode (&a->personal);
800     a = a->next;
801   }
802 }