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.
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.
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,
17 * Copyright © 2006 Pierre Habouzit
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>
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.
34 #include <lib-lib/mem.h>
35 #include <lib-lib/str.h>
36 #include <lib-lib/ascii.h>
38 #include <lib-mime/mime.h>
50 /* If you are debugging this file, comment out the following line. */
59 #define ENCWORD_LEN_MAX 75
60 #define ENCWORD_LEN_MIN 9 /* m_strlen("=?.?.?.?=") */
62 #define HSPACE(x) ((x) == '\0' || (x) == ' ' || (x) == '\t')
64 #define CONTINUATION_BYTE(c) (((c) & 0xc0) == 0x80)
66 typedef size_t (*encoder_t) (char *, const char *, size_t,
69 static size_t convert_string (const char *f, size_t flen,
70 const char *from, const char *to,
71 char **t, size_t * tlen)
78 cd = mutt_iconv_open (to, from, 0);
79 if (cd == (iconv_t) (-1))
82 ob = buf = p_new(char, obl);
83 n = my_iconv(cd, &f, &flen, &ob, &obl);
84 if (n == (size_t) (-1) || my_iconv(cd, 0, 0, &ob, &obl) == (size_t) (-1)) {
95 p_realloc(&buf, ob - buf + 1);
102 char *mutt_choose_charset (const char *fromcode, const char *charsets,
103 char *u, size_t ulen, char **d, size_t * dlen)
105 char canonical_buff[LONG_STRING];
106 char *e = 0, *tocode = 0;
107 size_t elen = 0, bestn = 0;
110 for (p = charsets; p; p = q ? q + 1 : 0) {
116 n = q ? q - p : m_strlen(p);
119 /* Assume that we never need more than 12 characters of
120 encoded-text to encode a single character. */
121 n > (ENCWORD_LEN_MAX - ENCWORD_LEN_MIN + 2 - 12))
126 n = convert_string (u, ulen, fromcode, t, &s, &slen);
127 if (n == (size_t) (-1))
130 if (!tocode || n < bestn) {
155 mutt_canonical_charset (canonical_buff, sizeof (canonical_buff), tocode);
156 str_replace (&tocode, canonical_buff);
161 static size_t b_encoder (char *s, const char *d, size_t dlen,
166 memcpy (s, "=?", 2), s += 2;
167 memcpy (s, tocode, m_strlen(tocode)), s += m_strlen(tocode);
168 memcpy (s, "?B?", 3), s += 3;
172 else if (dlen == 1) {
173 *s++ = __m_b64chars[(*d >> 2) & 0x3f];
174 *s++ = __m_b64chars[(*d & 0x03) << 4];
179 else if (dlen == 2) {
180 *s++ = __m_b64chars[(*d >> 2) & 0x3f];
181 *s++ = __m_b64chars[((*d & 0x03) << 4) | ((d[1] >> 4) & 0x0f)];
182 *s++ = __m_b64chars[(d[1] & 0x0f) << 2];
187 *s++ = __m_b64chars[(*d >> 2) & 0x3f];
188 *s++ = __m_b64chars[((*d & 0x03) << 4) | ((d[1] >> 4) & 0x0f)];
189 *s++ = __m_b64chars[((d[1] & 0x0f) << 2) | ((d[2] >> 6) & 0x03)];
190 *s++ = __m_b64chars[d[2] & 0x3f];
194 memcpy (s, "?=", 2), s += 2;
198 static size_t q_encoder (char *s, const char *d, size_t dlen,
201 char hex[] = "0123456789ABCDEF";
204 memcpy (s, "=?", 2), s += 2;
205 memcpy (s, tocode, m_strlen(tocode)), s += m_strlen(tocode);
206 memcpy (s, "?Q?", 3), s += 3;
208 unsigned char c = *d++;
212 else if (c >= 0x7f || c < 0x20 || c == '_' || strchr (MimeSpecials, c)) {
214 *s++ = hex[(c & 0xf0) >> 4];
215 *s++ = hex[c & 0x0f];
220 memcpy (s, "?=", 2), s += 2;
225 * Return 0 if and set *encoder and *wlen if the data (d, dlen) could
226 * be converted to an encoded word of length *wlen using *encoder.
227 * Otherwise return an upper bound on the maximum length of the data
228 * which could be converted.
229 * The data is converted from fromcode (which must be stateless) to
230 * tocode, unless fromcode is 0, in which case the data is assumed to
231 * be already in tocode, which should be 8-bit and stateless.
233 static size_t try_block (const char *d, size_t dlen,
234 const char *fromcode, const char *tocode,
235 encoder_t * encoder, size_t * wlen)
237 char buf1[ENCWORD_LEN_MAX - ENCWORD_LEN_MIN + 1];
242 int count, len, len_b, len_q;
245 cd = mutt_iconv_open (tocode, fromcode, 0);
246 assert (cd != (iconv_t) (-1));
247 ib = d, ibl = dlen, ob = buf1, obl = sizeof (buf1) - m_strlen(tocode);
248 if (my_iconv(cd, &ib, &ibl, &ob, &obl) == (size_t) (-1) ||
249 my_iconv(cd, 0, 0, &ob, &obl) == (size_t) (-1)) {
250 assert (errno == E2BIG);
253 return (ib - d == dlen) ? dlen : ib - d + 1;
258 if (dlen > sizeof (buf1) - m_strlen(tocode))
259 return sizeof (buf1) - m_strlen(tocode) + 1;
260 memcpy (buf1, d, dlen);
265 for (p = buf1; p < ob; p++) {
266 unsigned char c = *p;
268 assert (strchr (MimeSpecials, '?'));
269 if (c >= 0x7f || c < 0x20 || *p == '_' ||
270 (c != ' ' && strchr (MimeSpecials, *p)))
274 len = ENCWORD_LEN_MIN - 2 + m_strlen(tocode);
275 len_b = len + (((ob - buf1) + 2) / 3) * 4;
276 len_q = len + (ob - buf1) + 2 * count;
278 /* Apparently RFC 1468 says to use B encoding for iso-2022-jp. */
279 if (!ascii_strcasecmp (tocode, "ISO-2022-JP"))
280 len_q = ENCWORD_LEN_MAX + 1;
282 if (len_b < len_q && len_b <= ENCWORD_LEN_MAX) {
283 *encoder = b_encoder;
287 else if (len_q <= ENCWORD_LEN_MAX) {
288 *encoder = q_encoder;
297 * Encode the data (d, dlen) into s using the encoder.
298 * Return the length of the encoded word.
300 static size_t encode_block (char *s, char *d, size_t dlen,
301 const char *fromcode, const char *tocode,
304 char buf1[ENCWORD_LEN_MAX - ENCWORD_LEN_MIN + 1];
308 size_t ibl, obl, n1, n2;
311 cd = mutt_iconv_open (tocode, fromcode, 0);
312 assert (cd != (iconv_t) (-1));
313 ib = d, ibl = dlen, ob = buf1, obl = sizeof (buf1) - m_strlen(tocode);
314 n1 = my_iconv(cd, &ib, &ibl, &ob, &obl);
315 n2 = my_iconv(cd, 0, 0, &ob, &obl);
316 assert (n1 != (size_t) (-1) && n2 != (size_t) (-1));
318 return (*encoder) (s, buf1, ob - buf1, tocode);
321 return (*encoder) (s, d, dlen, tocode);
325 * Discover how much of the data (d, dlen) can be converted into
326 * a single encoded word. Return how much data can be converted,
327 * and set the length *wlen of the encoded word and *encoder.
328 * We start in column col, which limits the length of the word.
330 static size_t choose_block (char *d, size_t dlen, int col,
331 const char *fromcode, const char *tocode,
332 encoder_t * encoder, size_t * wlen)
335 int utf8 = fromcode && !ascii_strcasecmp (fromcode, "UTF-8");
340 nn = try_block (d, n, fromcode, tocode, encoder, wlen);
341 if (!nn && (col + *wlen <= ENCWORD_LEN_MAX + 1 || n <= 1))
343 n = (nn ? nn : n) - 1;
346 while (n > 1 && CONTINUATION_BYTE (d[n]))
353 * Place the result of RFC-2047-encoding (d, dlen) into the dynamically
354 * allocated buffer (e, elen). The input data is in charset fromcode
355 * and is converted into a charset chosen from charsets.
356 * Return 1 if the conversion to UTF-8 failed, 2 if conversion from UTF-8
357 * failed, otherwise 0. If conversion failed, fromcode is assumed to be
358 * compatible with us-ascii and the original data is used.
359 * The input data is assumed to be a single line starting at column col;
360 * if col is non-zero, the preceding character was a space.
362 static int rfc2047_encode (const char *d, size_t dlen, int col,
363 const char *fromcode, const char *charsets,
364 char **e, size_t * elen, char *specials)
368 size_t bufpos, buflen;
369 char *u, *t0, *t1, *t;
371 size_t ulen, r, n, wlen;
375 const char *icode = "UTF-8";
377 /* Try to convert to UTF-8. */
378 if (convert_string (d, dlen, fromcode, icode, &u, &ulen)) {
381 u = p_dupstr(d, ulen = dlen);
384 /* Find earliest and latest things we must encode. */
385 s0 = s1 = t0 = t1 = 0;
386 for (t = u; t < u + ulen; t++) {
388 (*t == '=' && t[1] == '?' && (t == u || HSPACE (*(t - 1))))) {
393 else if (specials && strchr (specials, *t)) {
400 /* If we have something to encode, include RFC822 specials */
401 if (t0 && s0 && s0 < t0)
403 if (t1 && s1 && s1 > t1)
407 /* No encoding is required. */
413 /* Choose target charset. */
416 if ((tocode1 = mutt_choose_charset (icode, charsets, u, ulen, 0, 0)))
422 /* Hack to avoid labelling 8-bit data as us-ascii. */
423 if (!icode && mutt_is_us_ascii (tocode))
424 tocode = "unknown-8bit";
426 /* Adjust t0 for maximum length of line. */
427 t = u + (ENCWORD_LEN_MAX + 1) - col - ENCWORD_LEN_MIN;
434 /* Adjust t0 until we can encode a character after a space. */
435 for (; t0 > u; t0--) {
436 if (!HSPACE (*(t0 - 1)))
440 while (t < u + ulen && CONTINUATION_BYTE (*t))
442 if (!try_block (t0, t - t0, icode, tocode, &encoder, &wlen) &&
443 col + (t0 - u) + wlen <= ENCWORD_LEN_MAX + 1)
447 /* Adjust t1 until we can encode a character before a space. */
448 for (; t1 < u + ulen; t1++) {
453 while (CONTINUATION_BYTE (*t))
455 if (!try_block (t, t1 - t, icode, tocode, &encoder, &wlen) &&
456 1 + wlen + (u + ulen - t1) <= ENCWORD_LEN_MAX + 1)
460 /* We shall encode the region [t0,t1). */
462 /* Initialise the output buffer with the us-ascii prefix. */
464 buf = p_new(char, buflen);
466 memcpy (buf, u, t0 - u);
472 /* Find how much we can encode. */
473 n = choose_block (t, t1 - t, col, icode, tocode, &encoder, &wlen);
475 /* See if we can fit the us-ascii suffix, too. */
476 if (col + wlen + (u + ulen - t1) <= ENCWORD_LEN_MAX + 1)
480 while (CONTINUATION_BYTE (t[n]))
484 /* This should only happen in the really stupid case where the
485 only word that needs encoding is one character long, but
486 there is too much us-ascii stuff after it to use a single
487 encoded word. We add the next word to the encoded region
489 assert (t1 < u + ulen);
490 for (t1++; t1 < u + ulen && !HSPACE (*t1); t1++);
493 n = choose_block (t, n, col, icode, tocode, &encoder, &wlen);
496 /* Add to output buffer. */
497 #define LINEBREAK "\n\t"
498 if (bufpos + wlen + m_strlen(LINEBREAK) > buflen) {
499 buflen = bufpos + wlen + m_strlen(LINEBREAK);
500 p_realloc(&buf, buflen);
502 r = encode_block (buf + bufpos, t, n, icode, tocode, encoder);
505 memcpy (buf + bufpos, LINEBREAK, m_strlen(LINEBREAK));
506 bufpos += m_strlen(LINEBREAK);
514 /* Add last encoded word and us-ascii suffix to buffer. */
515 buflen = bufpos + wlen + (u + ulen - t1);
516 p_realloc(&buf, buflen + 1);
517 r = encode_block (buf + bufpos, t, t1 - t, icode, tocode, encoder);
520 memcpy (buf + bufpos, t1, u + ulen - t1);
532 void _rfc2047_encode_string (char **pd, int encode_specials, int col)
536 const char *charsets;
538 if (!Charset || !*pd)
541 charsets = SendCharset;
542 if (!charsets || !*charsets)
545 rfc2047_encode (*pd, m_strlen(*pd), col,
546 Charset, charsets, &e, &elen,
547 encode_specials ? RFC822Specials : NULL);
553 void rfc2047_encode_adrlist (address_t * addr, const char *tag)
555 address_t *ptr = addr;
556 int col = tag ? m_strlen(tag) + 2 : 32;
560 _rfc2047_encode_string (&ptr->personal, 1, col);
565 static int rfc2047_decode_word (char *d, const char *s, size_t len)
567 const char *pp, *pp1;
570 int enc = 0, count = 0;
571 char *charset = NULL;
573 pd = d0 = p_new(char, m_strlen(s));
575 for (pp = s; (pp1 = strchr (pp, '?')); pp = pp1 + 1) {
579 /* ignore language specification a la RFC 2231 */
581 if ((t1 = memchr (pp, '*', t - pp)))
583 charset = p_dupstr(pp, t - pp);
586 if (toupper ((unsigned char) *pp) == 'Q')
587 enc = ENCQUOTEDPRINTABLE;
588 else if (toupper ((unsigned char) *pp) == 'B')
597 if (enc == ENCQUOTEDPRINTABLE) {
598 for (; pp < pp1; pp++) {
601 else if (*pp == '=' && hexval(pp[1]) >= 0 && hexval(pp[2]) >= 0) {
602 *pd++ = (hexval (pp[1]) << 4) | hexval (pp[2]);
610 else if (enc == ENCBASE64) {
613 for (; pp < pp1; pp++) {
616 if ((c = base64val(*pp)) < 0)
620 *pd++ = b | (c >> k);
635 mutt_convert_string (&d0, charset, Charset, M_ICONV_HOOK_FROM);
636 m_strcpy(d, len, d0);
643 * Find the start and end of the first encoded word in the string.
644 * We use the grammar in section 2 of RFC 2047, but the "encoding"
645 * must be B or Q. Also, we don't require the encoded word to be
646 * separated by linear-white-space (section 5(1)).
648 static const char *find_encoded_word (const char *s, const char **x)
653 while ((p = strstr (q, "=?"))) {
655 0x20 < *q && *q < 0x7f && !strchr ("()<>@,;:\"/[]?.=", *q); q++);
656 if (q[0] != '?' || !strchr ("BbQq", q[1]) || q[2] != '?')
658 for (q = q + 3; 0x20 <= *q && *q < 0x7f && *q != '?'; q++);
659 if (q[0] != '?' || q[1] != '=') {
671 /* return length of linear white space */
672 static size_t lwslen (const char *s, size_t n)
680 for (; p < s + n; p++)
681 if (!strchr (" \t\r\n", *p)) {
682 len = (size_t) (p - s);
685 if (strchr ("\r\n", *(p - 1))) /* LWS doesn't end with CRLF */
690 /* return length of linear white space : reverse */
691 static size_t lwsrlen (const char *s, size_t n)
693 const char *p = s + n - 1;
699 if (strchr ("\r\n", *p)) /* LWS doesn't end with CRLF */
703 if (!strchr (" \t\r\n", *p)) {
704 len = (size_t) (s + n - 1 - p);
710 /* try to decode anything that looks like a valid RFC2047 encoded
711 * header field, ignoring RFC822 parsing rules
713 void rfc2047_decode (char **pd)
717 int found_encoded = 0;
725 dlen = 4 * m_strlen(s); /* should be enough */
726 d = d0 = p_new(char, dlen + 1);
728 while (*s && dlen > 0) {
729 if (!(p = find_encoded_word (s, &q))) {
730 /* no encoded words */
731 if (!option (OPTSTRICTMIME)) {
733 if (found_encoded && (m = lwslen (s, n)) != 0) {
735 *d = ' ', d++, dlen--;
738 if (ascii_strcasecmp (AssumedCharset, "us-ascii")) {
743 if (mutt_convert_nonmime_string (&t) == 0) {
745 strncpy (d, t, tlen);
756 strncpy (d, s, dlen);
762 n = (size_t) (p - s);
763 /* ignore spaces between encoded words
764 * and linear white spaces between encoded word and *text */
765 if (!option (OPTSTRICTMIME)) {
766 if (found_encoded && (m = lwslen (s, n)) != 0) {
768 *d = ' ', d++, dlen--;
772 if ((m = n - lwsrlen (s, n)) != 0) {
779 *d = ' ', d++, dlen--;
782 else if (!found_encoded || strspn (s, " \t\r\n") != n) {
791 rfc2047_decode_word (d, p, dlen);
805 void rfc2047_decode_adrlist (address_t * a)
809 rfc2047_decode (&a->personal);
814 void rfc2047_decode_envelope (ENVELOPE* e) {
819 /* do RFC2047 decoding */
820 rfc2047_decode_adrlist (e->from);
821 rfc2047_decode_adrlist (e->to);
822 rfc2047_decode_adrlist (e->cc);
823 rfc2047_decode_adrlist (e->bcc);
824 rfc2047_decode_adrlist (e->reply_to);
825 rfc2047_decode_adrlist (e->mail_followup_to);
826 rfc2047_decode_adrlist (e->return_path);
827 rfc2047_decode_adrlist (e->sender);
830 rfc2047_decode (&e->subject);
831 mutt_adjust_subject (e);