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