d8e4a5d3ac22926c62ee31e334125cc767c6c3d5
[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 <lib-lib/mem.h>
16 #include <lib-lib/str.h>
17 #include <lib-lib/ascii.h>
18
19 #include <lib-mime/mime.h>
20
21 #include "mutt.h"
22 #include "charset.h"
23 #include "rfc2047.h"
24 #include "thread.h"
25
26
27 #include <ctype.h>
28 #include <errno.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32
33 /* If you are debugging this file, comment out the following line. */
34 /*#define NDEBUG*/
35
36 #ifdef NDEBUG
37 #define assert(x)
38 #else
39 #include <assert.h>
40 #endif
41
42 #define ENCWORD_LEN_MAX 75
43 #define ENCWORD_LEN_MIN 9       /* m_strlen("=?.?.?.?=") */
44
45 #define HSPACE(x) ((x) == '\0' || (x) == ' ' || (x) == '\t')
46
47 #define CONTINUATION_BYTE(c) (((c) & 0xc0) == 0x80)
48
49 extern char RFC822Specials[];
50
51 typedef size_t (*encoder_t) (char *, const char *, size_t,
52                              const char *);
53
54 static size_t convert_string (const char *f, size_t flen,
55                               const char *from, const char *to,
56                               char **t, size_t * tlen)
57 {
58   iconv_t cd;
59   char *buf, *ob;
60   size_t obl, n;
61   int e;
62
63   cd = mutt_iconv_open (to, from, 0);
64   if (cd == (iconv_t) (-1))
65     return (size_t) (-1);
66   obl = 4 * flen + 1;
67   ob = buf = p_new(char, obl);
68   n = my_iconv(cd, &f, &flen, &ob, &obl);
69   if (n == (size_t) (-1) || my_iconv(cd, 0, 0, &ob, &obl) == (size_t) (-1)) {
70     e = errno;
71     p_delete(&buf);
72     iconv_close (cd);
73     errno = e;
74     return (size_t) (-1);
75   }
76   *ob = '\0';
77
78   *tlen = ob - buf;
79
80   p_realloc(&buf, ob - buf + 1);
81   *t = buf;
82   iconv_close (cd);
83
84   return n;
85 }
86
87 char *mutt_choose_charset (const char *fromcode, const char *charsets,
88                            char *u, size_t ulen, char **d, size_t * dlen)
89 {
90   char canonical_buff[LONG_STRING];
91   char *e = 0, *tocode = 0;
92   size_t elen = 0, bestn = 0;
93   const char *p, *q;
94
95   for (p = charsets; p; p = q ? q + 1 : 0) {
96     char *s, *t;
97     size_t slen, n;
98
99     q = strchr (p, ':');
100
101     n = q ? q - p : m_strlen(p);
102
103     if (!n ||
104         /* Assume that we never need more than 12 characters of
105            encoded-text to encode a single character. */
106         n > (ENCWORD_LEN_MAX - ENCWORD_LEN_MIN + 2 - 12))
107       continue;
108
109     t = p_dupstr(p, n);
110
111     n = convert_string (u, ulen, fromcode, t, &s, &slen);
112     if (n == (size_t) (-1))
113       continue;
114
115     if (!tocode || n < bestn) {
116       bestn = n;
117       p_delete(&tocode);
118       tocode = t;
119       if (d) {
120         p_delete(&e);
121         e = s;
122       }
123       else
124         p_delete(&s);
125       elen = slen;
126       if (!bestn)
127         break;
128     }
129     else {
130       p_delete(&t);
131       p_delete(&s);
132     }
133   }
134   if (tocode) {
135     if (d)
136       *d = e;
137     if (dlen)
138       *dlen = elen;
139
140     mutt_canonical_charset (canonical_buff, sizeof (canonical_buff), tocode);
141     str_replace (&tocode, canonical_buff);
142   }
143   return tocode;
144 }
145
146 static size_t b_encoder (char *s, const char *d, size_t dlen,
147                          const char *tocode)
148 {
149   char *s0 = s;
150
151   memcpy (s, "=?", 2), s += 2;
152   memcpy (s, tocode, m_strlen(tocode)), s += m_strlen(tocode);
153   memcpy (s, "?B?", 3), s += 3;
154   for (;;) {
155     if (!dlen)
156       break;
157     else if (dlen == 1) {
158       *s++ = __m_b64chars[(*d >> 2) & 0x3f];
159       *s++ = __m_b64chars[(*d & 0x03) << 4];
160       *s++ = '=';
161       *s++ = '=';
162       break;
163     }
164     else if (dlen == 2) {
165       *s++ = __m_b64chars[(*d >> 2) & 0x3f];
166       *s++ = __m_b64chars[((*d & 0x03) << 4) | ((d[1] >> 4) & 0x0f)];
167       *s++ = __m_b64chars[(d[1] & 0x0f) << 2];
168       *s++ = '=';
169       break;
170     }
171     else {
172       *s++ = __m_b64chars[(*d >> 2) & 0x3f];
173       *s++ = __m_b64chars[((*d & 0x03) << 4) | ((d[1] >> 4) & 0x0f)];
174       *s++ = __m_b64chars[((d[1] & 0x0f) << 2) | ((d[2] >> 6) & 0x03)];
175       *s++ = __m_b64chars[d[2] & 0x3f];
176       d += 3, dlen -= 3;
177     }
178   }
179   memcpy (s, "?=", 2), s += 2;
180   return s - s0;
181 }
182
183 static size_t q_encoder (char *s, const char *d, size_t dlen,
184                          const char *tocode)
185 {
186   char hex[] = "0123456789ABCDEF";
187   char *s0 = s;
188
189   memcpy (s, "=?", 2), s += 2;
190   memcpy (s, tocode, m_strlen(tocode)), s += m_strlen(tocode);
191   memcpy (s, "?Q?", 3), s += 3;
192   while (dlen--) {
193     unsigned char c = *d++;
194
195     if (c == ' ')
196       *s++ = '_';
197     else if (c >= 0x7f || c < 0x20 || c == '_' || strchr (MimeSpecials, c)) {
198       *s++ = '=';
199       *s++ = hex[(c & 0xf0) >> 4];
200       *s++ = hex[c & 0x0f];
201     }
202     else
203       *s++ = c;
204   }
205   memcpy (s, "?=", 2), s += 2;
206   return s - s0;
207 }
208
209 /*
210  * Return 0 if and set *encoder and *wlen if the data (d, dlen) could
211  * be converted to an encoded word of length *wlen using *encoder.
212  * Otherwise return an upper bound on the maximum length of the data
213  * which could be converted.
214  * The data is converted from fromcode (which must be stateless) to
215  * tocode, unless fromcode is 0, in which case the data is assumed to
216  * be already in tocode, which should be 8-bit and stateless.
217  */
218 static size_t try_block (const char *d, size_t dlen,
219                          const char *fromcode, const char *tocode,
220                          encoder_t * encoder, size_t * wlen)
221 {
222   char buf1[ENCWORD_LEN_MAX - ENCWORD_LEN_MIN + 1];
223   iconv_t cd;
224   const char *ib;
225   char *ob, *p;
226   size_t ibl, obl;
227   int count, len, len_b, len_q;
228
229   if (fromcode) {
230     cd = mutt_iconv_open (tocode, fromcode, 0);
231     assert (cd != (iconv_t) (-1));
232     ib = d, ibl = dlen, ob = buf1, obl = sizeof (buf1) - m_strlen(tocode);
233     if (my_iconv(cd, &ib, &ibl, &ob, &obl) == (size_t) (-1) ||
234         my_iconv(cd, 0, 0, &ob, &obl) == (size_t) (-1)) {
235       assert (errno == E2BIG);
236       iconv_close (cd);
237       assert (ib > d);
238       return (ib - d == dlen) ? dlen : ib - d + 1;
239     }
240     iconv_close (cd);
241   }
242   else {
243     if (dlen > sizeof (buf1) - m_strlen(tocode))
244       return sizeof (buf1) - m_strlen(tocode) + 1;
245     memcpy (buf1, d, dlen);
246     ob = buf1 + dlen;
247   }
248
249   count = 0;
250   for (p = buf1; p < ob; p++) {
251     unsigned char c = *p;
252
253     assert (strchr (MimeSpecials, '?'));
254     if (c >= 0x7f || c < 0x20 || *p == '_' ||
255         (c != ' ' && strchr (MimeSpecials, *p)))
256       ++count;
257   }
258
259   len = ENCWORD_LEN_MIN - 2 + m_strlen(tocode);
260   len_b = len + (((ob - buf1) + 2) / 3) * 4;
261   len_q = len + (ob - buf1) + 2 * count;
262
263   /* Apparently RFC 1468 says to use B encoding for iso-2022-jp. */
264   if (!ascii_strcasecmp (tocode, "ISO-2022-JP"))
265     len_q = ENCWORD_LEN_MAX + 1;
266
267   if (len_b < len_q && len_b <= ENCWORD_LEN_MAX) {
268     *encoder = b_encoder;
269     *wlen = len_b;
270     return 0;
271   }
272   else if (len_q <= ENCWORD_LEN_MAX) {
273     *encoder = q_encoder;
274     *wlen = len_q;
275     return 0;
276   }
277   else
278     return dlen;
279 }
280
281 /*
282  * Encode the data (d, dlen) into s using the encoder.
283  * Return the length of the encoded word.
284  */
285 static size_t encode_block (char *s, char *d, size_t dlen,
286                             const char *fromcode, const char *tocode,
287                             encoder_t encoder)
288 {
289   char buf1[ENCWORD_LEN_MAX - ENCWORD_LEN_MIN + 1];
290   iconv_t cd;
291   const char *ib;
292   char *ob;
293   size_t ibl, obl, n1, n2;
294
295   if (fromcode) {
296     cd = mutt_iconv_open (tocode, fromcode, 0);
297     assert (cd != (iconv_t) (-1));
298     ib = d, ibl = dlen, ob = buf1, obl = sizeof (buf1) - m_strlen(tocode);
299     n1 = my_iconv(cd, &ib, &ibl, &ob, &obl);
300     n2 = my_iconv(cd, 0, 0, &ob, &obl);
301     assert (n1 != (size_t) (-1) && n2 != (size_t) (-1));
302     iconv_close (cd);
303     return (*encoder) (s, buf1, ob - buf1, tocode);
304   }
305   else
306     return (*encoder) (s, d, dlen, tocode);
307 }
308
309 /*
310  * Discover how much of the data (d, dlen) can be converted into
311  * a single encoded word. Return how much data can be converted,
312  * and set the length *wlen of the encoded word and *encoder.
313  * We start in column col, which limits the length of the word.
314  */
315 static size_t choose_block (char *d, size_t dlen, int col,
316                             const char *fromcode, const char *tocode,
317                             encoder_t * encoder, size_t * wlen)
318 {
319   size_t n, nn;
320   int utf8 = fromcode && !ascii_strcasecmp (fromcode, "UTF-8");
321
322   n = dlen;
323   for (;;) {
324     assert (d + n > d);
325     nn = try_block (d, n, fromcode, tocode, encoder, wlen);
326     if (!nn && (col + *wlen <= ENCWORD_LEN_MAX + 1 || n <= 1))
327       break;
328     n = (nn ? nn : n) - 1;
329     assert (n > 0);
330     if (utf8)
331       while (n > 1 && CONTINUATION_BYTE (d[n]))
332         --n;
333   }
334   return n;
335 }
336
337 /*
338  * Place the result of RFC-2047-encoding (d, dlen) into the dynamically
339  * allocated buffer (e, elen). The input data is in charset fromcode
340  * and is converted into a charset chosen from charsets.
341  * Return 1 if the conversion to UTF-8 failed, 2 if conversion from UTF-8
342  * failed, otherwise 0. If conversion failed, fromcode is assumed to be
343  * compatible with us-ascii and the original data is used.
344  * The input data is assumed to be a single line starting at column col;
345  * if col is non-zero, the preceding character was a space.
346  */
347 static int rfc2047_encode (const char *d, size_t dlen, int col,
348                            const char *fromcode, const char *charsets,
349                            char **e, size_t * elen, char *specials)
350 {
351   int ret = 0;
352   char *buf;
353   size_t bufpos, buflen;
354   char *u, *t0, *t1, *t;
355   char *s0, *s1;
356   size_t ulen, r, n, wlen;
357   encoder_t encoder;
358   char *tocode1 = 0;
359   const char *tocode;
360   const char *icode = "UTF-8";
361
362   /* Try to convert to UTF-8. */
363   if (convert_string (d, dlen, fromcode, icode, &u, &ulen)) {
364     ret = 1;
365     icode = 0;
366     u = p_dupstr(d, ulen = dlen);
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 = p_new(char, 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 + m_strlen(LINEBREAK) > buflen) {
484       buflen = bufpos + wlen + m_strlen(LINEBREAK);
485       p_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, m_strlen(LINEBREAK));
491     bufpos += m_strlen(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   p_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   p_delete(&tocode1);
508   p_delete(&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   const 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, m_strlen(*pd), col,
531                   Charset, charsets, &e, &elen,
532                   encode_specials ? RFC822Specials : NULL);
533
534   p_delete(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 ? m_strlen(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 = p_new(char, m_strlen(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 = p_dupstr(pp, t - pp);
569       break;
570     case 3:
571       if (toupper ((unsigned char) *pp) == 'Q')
572         enc = ENCQUOTEDPRINTABLE;
573       else if (toupper ((unsigned char) *pp) == 'B')
574         enc = ENCBASE64;
575       else {
576         p_delete(&charset);
577         p_delete(&d0);
578         return (-1);
579       }
580       break;
581     case 4:
582       if (enc == ENCQUOTEDPRINTABLE) {
583         for (; pp < pp1; pp++) {
584           if (*pp == '_')
585             *pd++ = ' ';
586           else if (*pp == '=' &&
587                    (!(pp[1] & ~127) && hexval (pp[1]) != -1) &&
588                    (!(pp[2] & ~127) && hexval (pp[2]) != -1)) {
589             *pd++ = (hexval (pp[1]) << 4) | hexval (pp[2]);
590             pp += 2;
591           }
592           else
593             *pd++ = *pp;
594         }
595         *pd = 0;
596       }
597       else if (enc == ENCBASE64) {
598         int c, b = 0, k = 0;
599
600         for (; pp < pp1; pp++) {
601           if (*pp == '=')
602             break;
603           if ((c = base64val(*pp)) < 0)
604             continue;
605           if (k + 6 >= 8) {
606             k -= 2;
607             *pd++ = b | (c >> k);
608             b = c << (8 - k);
609           }
610           else {
611             b |= c << (k + 2);
612             k += 6;
613           }
614         }
615         *pd = 0;
616       }
617       break;
618     }
619   }
620
621   if (charset)
622     mutt_convert_string (&d0, charset, Charset, M_ICONV_HOOK_FROM);
623   m_strcpy(d, len, d0);
624   p_delete(&charset);
625   p_delete(&d0);
626   return (0);
627 }
628
629 /*
630  * Find the start and end of the first encoded word in the string.
631  * We use the grammar in section 2 of RFC 2047, but the "encoding"
632  * must be B or Q. Also, we don't require the encoded word to be
633  * separated by linear-white-space (section 5(1)).
634  */
635 static const char *find_encoded_word (const char *s, const char **x)
636 {
637   const char *p, *q;
638
639   q = s;
640   while ((p = strstr (q, "=?"))) {
641     for (q = p + 2;
642          0x20 < *q && *q < 0x7f && !strchr ("()<>@,;:\"/[]?.=", *q); q++);
643     if (q[0] != '?' || !strchr ("BbQq", q[1]) || q[2] != '?')
644       continue;
645     for (q = q + 3; 0x20 <= *q && *q < 0x7f && *q != '?'; q++);
646     if (q[0] != '?' || q[1] != '=') {
647       --q;
648       continue;
649     }
650
651     *x = q + 2;
652     return p;
653   }
654
655   return 0;
656 }
657
658 /* return length of linear white space */
659 static size_t lwslen (const char *s, size_t n)
660 {
661   const char *p = s;
662   size_t len = n;
663
664   if (n <= 0)
665     return 0;
666
667   for (; p < s + n; p++)
668     if (!strchr (" \t\r\n", *p)) {
669       len = (size_t) (p - s);
670       break;
671     }
672   if (strchr ("\r\n", *(p - 1)))        /* LWS doesn't end with CRLF */
673     len = (size_t) 0;
674   return len;
675 }
676
677 /* return length of linear white space : reverse */
678 static size_t lwsrlen (const char *s, size_t n)
679 {
680   const char *p = s + n - 1;
681   size_t len = n;
682
683   if (n <= 0)
684     return 0;
685
686   if (strchr ("\r\n", *p))      /* LWS doesn't end with CRLF */
687     return (size_t) 0;
688
689   for (; p >= s; p--)
690     if (!strchr (" \t\r\n", *p)) {
691       len = (size_t) (s + n - 1 - p);
692       break;
693     }
694   return len;
695 }
696
697 /* try to decode anything that looks like a valid RFC2047 encoded
698  * header field, ignoring RFC822 parsing rules
699  */
700 void rfc2047_decode (char **pd)
701 {
702   const char *p, *q;
703   size_t m, n;
704   int found_encoded = 0;
705   char *d0, *d;
706   const char *s = *pd;
707   size_t dlen;
708
709   if (!s || !*s)
710     return;
711
712   dlen = 4 * m_strlen(s);        /* should be enough */
713   d = d0 = p_new(char, dlen + 1);
714
715   while (*s && dlen > 0) {
716     if (!(p = find_encoded_word (s, &q))) {
717       /* no encoded words */
718       if (!option (OPTSTRICTMIME)) {
719         n = m_strlen(s);
720         if (found_encoded && (m = lwslen (s, n)) != 0) {
721           if (m != n)
722             *d = ' ', d++, dlen--;
723           n -= m, s += m;
724         }
725         if (ascii_strcasecmp (AssumedCharset, "us-ascii")) {
726           char *t;
727           size_t tlen;
728
729           t = p_dupstr(s, n);
730           if (mutt_convert_nonmime_string (&t) == 0) {
731             tlen = m_strlen(t);
732             strncpy (d, t, tlen);
733             d += tlen;
734           }
735           else {
736             strncpy (d, s, n);
737             d += n;
738           }
739           p_delete(&t);
740           break;
741         }
742       }
743       strncpy (d, s, dlen);
744       d += dlen;
745       break;
746     }
747
748     if (p != s) {
749       n = (size_t) (p - s);
750       /* ignore spaces between encoded words
751        * and linear white spaces between encoded word and *text */
752       if (!option (OPTSTRICTMIME)) {
753         if (found_encoded && (m = lwslen (s, n)) != 0) {
754           if (m != n)
755             *d = ' ', d++, dlen--;
756           n -= m, s += m;
757         }
758
759         if ((m = n - lwsrlen (s, n)) != 0) {
760           if (m > dlen)
761             m = dlen;
762           memcpy (d, s, m);
763           d += m;
764           dlen -= m;
765           if (m != n)
766             *d = ' ', d++, dlen--;
767         }
768       }
769       else if (!found_encoded || strspn (s, " \t\r\n") != n) {
770         if (n > dlen)
771           n = dlen;
772         memcpy (d, s, n);
773         d += n;
774         dlen -= n;
775       }
776     }
777
778     rfc2047_decode_word (d, p, dlen);
779     found_encoded = 1;
780     s = q;
781     n = m_strlen(d);
782     dlen -= n;
783     d += n;
784   }
785   *d = 0;
786
787   p_delete(pd);
788   *pd = d0;
789   str_adjust (pd);
790 }
791
792 void rfc2047_decode_adrlist (ADDRESS * a)
793 {
794   while (a) {
795     if (a->personal)
796       rfc2047_decode (&a->personal);
797     a = a->next;
798   }
799 }
800
801 void rfc2047_decode_envelope (ENVELOPE* e) {
802
803   if (!e)
804     return;
805
806   /* do RFC2047 decoding */
807   rfc2047_decode_adrlist (e->from);
808   rfc2047_decode_adrlist (e->to);
809   rfc2047_decode_adrlist (e->cc);
810   rfc2047_decode_adrlist (e->bcc);
811   rfc2047_decode_adrlist (e->reply_to);
812   rfc2047_decode_adrlist (e->mail_followup_to);
813   rfc2047_decode_adrlist (e->return_path);
814   rfc2047_decode_adrlist (e->sender);
815
816   if (e->subject) {
817     rfc2047_decode (&e->subject);
818     mutt_adjust_subject (e);
819   }
820 }