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.
30 #include <lib-lib/mem.h>
31 #include <lib-lib/str.h>
32 #include <lib-lib/ascii.h>
34 #include <lib-mime/mime.h>
46 /* If you are debugging this file, comment out the following line. */
55 #define ENCWORD_LEN_MAX 75
56 #define ENCWORD_LEN_MIN 9 /* m_strlen("=?.?.?.?=") */
58 #define HSPACE(x) ((x) == '\0' || (x) == ' ' || (x) == '\t')
60 #define CONTINUATION_BYTE(c) (((c) & 0xc0) == 0x80)
62 typedef size_t (*encoder_t) (char *, const char *, size_t,
65 /* converts f of len flen and charset from
66 into *t of len *tlen and charset to
69 returns number of converted chars from f, see iconv(3)
72 convert_string(const char *from, const char *f, size_t flen,
73 const char *to, char **t, size_t *tlen)
81 cd = mutt_iconv_open(to, from, 0);
83 if (cd == (iconv_t)(-1))
87 ob = buf = p_new(char, obl);
88 n = my_iconv(cd, &f, &flen, &ob, &obl);
90 if (n < 0 || my_iconv(cd, 0, 0, &ob, &obl) < 0) {
101 p_realloc(&buf, ob - buf + 1);
108 char *mutt_choose_charset(const char *fromcode, const char *charsets,
109 char *u, size_t ulen, char **d, size_t *dlen)
111 char canonical_buff[LONG_STRING];
112 char *e = 0, *tocode = 0;
113 size_t elen = 0, bestn = 0;
116 for (p = charsets; p; p = q ? q + 1 : 0) {
122 n = q ? q - p : m_strlen(p);
125 /* Assume that we never need more than 12 characters of
126 encoded-text to encode a single character. */
127 n > (ENCWORD_LEN_MAX - ENCWORD_LEN_MIN + 2 - 12))
132 n = convert_string(fromcode, u, ulen, t, &s, &slen);
133 if (n == (size_t) (-1))
136 if (!tocode || n < bestn) {
161 mutt_canonical_charset(canonical_buff, sizeof(canonical_buff), tocode);
162 m_strreplace(&tocode, canonical_buff);
168 static size_t b_encoder (char *s, const char *d, size_t dlen,
173 memcpy (s, "=?", 2), s += 2;
174 memcpy (s, tocode, m_strlen(tocode)), s += m_strlen(tocode);
175 memcpy (s, "?B?", 3), s += 3;
179 else if (dlen == 1) {
180 *s++ = __m_b64chars[(*d >> 2) & 0x3f];
181 *s++ = __m_b64chars[(*d & 0x03) << 4];
186 else if (dlen == 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];
194 *s++ = __m_b64chars[(*d >> 2) & 0x3f];
195 *s++ = __m_b64chars[((*d & 0x03) << 4) | ((d[1] >> 4) & 0x0f)];
196 *s++ = __m_b64chars[((d[1] & 0x0f) << 2) | ((d[2] >> 6) & 0x03)];
197 *s++ = __m_b64chars[d[2] & 0x3f];
201 memcpy (s, "?=", 2), s += 2;
205 static size_t q_encoder (char *s, const char *d, size_t dlen,
210 memcpy (s, "=?", 2), s += 2;
211 memcpy (s, tocode, m_strlen(tocode)), s += m_strlen(tocode);
212 memcpy (s, "?Q?", 3), s += 3;
214 unsigned char c = *d++;
218 else if (c >= 0x7f || c < 0x20 || c == '_' || strchr (MimeSpecials, c)) {
220 *s++ = __m_b36chars_upper[c >> 4];
221 *s++ = __m_b36chars_upper[c & 0xf];
226 memcpy (s, "?=", 2), s += 2;
231 * Return 0 if and set *encoder and *wlen if the data (d, dlen) could
232 * be converted to an encoded word of length *wlen using *encoder.
233 * Otherwise return an upper bound on the maximum length of the data
234 * which could be converted.
235 * The data is converted from fromcode (which must be stateless) to
236 * tocode, unless fromcode is 0, in which case the data is assumed to
237 * be already in tocode, which should be 8-bit and stateless.
239 static size_t try_block (const char *d, size_t dlen,
240 const char *fromcode, const char *tocode,
241 encoder_t * encoder, size_t * wlen)
243 char buf1[ENCWORD_LEN_MAX - ENCWORD_LEN_MIN + 1];
248 int count, len, len_b, len_q;
251 cd = mutt_iconv_open (tocode, fromcode, 0);
252 assert (cd != (iconv_t) (-1));
253 ib = d, ibl = dlen, ob = buf1, obl = sizeof (buf1) - m_strlen(tocode);
254 if (my_iconv(cd, &ib, &ibl, &ob, &obl) < 0
255 || my_iconv(cd, 0, 0, &ob, &obl) < 0)
257 assert (errno == E2BIG);
260 return (ib - d == dlen) ? dlen : ib - d + 1;
265 if (dlen > sizeof (buf1) - m_strlen(tocode))
266 return sizeof (buf1) - m_strlen(tocode) + 1;
267 memcpy (buf1, d, dlen);
272 for (p = buf1; p < ob; p++) {
273 unsigned char c = *p;
275 assert (strchr (MimeSpecials, '?'));
276 if (c >= 0x7f || c < 0x20 || *p == '_' ||
277 (c != ' ' && strchr (MimeSpecials, *p)))
281 len = ENCWORD_LEN_MIN - 2 + m_strlen(tocode);
282 len_b = len + (((ob - buf1) + 2) / 3) * 4;
283 len_q = len + (ob - buf1) + 2 * count;
285 /* Apparently RFC 1468 says to use B encoding for iso-2022-jp. */
286 if (!ascii_strcasecmp (tocode, "ISO-2022-JP"))
287 len_q = ENCWORD_LEN_MAX + 1;
289 if (len_b < len_q && len_b <= ENCWORD_LEN_MAX) {
290 *encoder = b_encoder;
294 else if (len_q <= ENCWORD_LEN_MAX) {
295 *encoder = q_encoder;
304 * Encode the data (d, dlen) into s using the encoder.
305 * Return the length of the encoded word.
307 static size_t encode_block (char *s, char *d, size_t dlen,
308 const char *fromcode, const char *tocode,
311 char buf1[ENCWORD_LEN_MAX - ENCWORD_LEN_MIN + 1];
315 size_t ibl, obl, n1, n2;
318 cd = mutt_iconv_open (tocode, fromcode, 0);
319 assert (cd != (iconv_t) (-1));
320 ib = d, ibl = dlen, ob = buf1, obl = sizeof (buf1) - m_strlen(tocode);
321 n1 = my_iconv(cd, &ib, &ibl, &ob, &obl);
322 n2 = my_iconv(cd, 0, 0, &ob, &obl);
323 assert (n1 != (size_t) (-1) && n2 != (size_t) (-1));
325 return (*encoder) (s, buf1, ob - buf1, tocode);
328 return (*encoder) (s, d, dlen, tocode);
332 * Discover how much of the data (d, dlen) can be converted into
333 * a single encoded word. Return how much data can be converted,
334 * and set the length *wlen of the encoded word and *encoder.
335 * We start in column col, which limits the length of the word.
337 static size_t choose_block (char *d, size_t dlen, int col,
338 const char *fromcode, const char *tocode,
339 encoder_t * encoder, size_t * wlen)
342 int utf8 = fromcode && !ascii_strcasecmp (fromcode, "UTF-8");
347 nn = try_block (d, n, fromcode, tocode, encoder, wlen);
348 if (!nn && (col + *wlen <= ENCWORD_LEN_MAX + 1 || n <= 1))
350 n = (nn ? nn : n) - 1;
353 while (n > 1 && CONTINUATION_BYTE (d[n]))
360 * Place the result of RFC-2047-encoding (d, dlen) into the dynamically
361 * allocated buffer (e, elen). The input data is in charset fromcode
362 * and is converted into a charset chosen from charsets.
363 * Return 1 if the conversion to UTF-8 failed, 2 if conversion from UTF-8
364 * failed, otherwise 0. If conversion failed, fromcode is assumed to be
365 * compatible with us-ascii and the original data is used.
366 * The input data is assumed to be a single line starting at column col;
367 * if col is non-zero, the preceding character was a space.
369 static int rfc2047_encode (const char *d, size_t dlen, int col,
370 const char *fromcode, const char *charsets,
371 char **e, size_t * elen, char *specials)
375 size_t bufpos, buflen;
376 char *u, *t0, *t1, *t;
378 size_t ulen, r, n, wlen;
382 const char *icode = "UTF-8";
384 /* Try to convert to UTF-8. */
385 if (convert_string(fromcode, d, dlen, icode, &u, &ulen)) {
388 u = p_dupstr(d, ulen = dlen);
391 /* Find earliest and latest things we must encode. */
392 s0 = s1 = t0 = t1 = 0;
393 for (t = u; t < u + ulen; t++) {
395 (*t == '=' && t[1] == '?' && (t == u || HSPACE (*(t - 1))))) {
400 else if (specials && strchr (specials, *t)) {
407 /* If we have something to encode, include RFC822 specials */
408 if (t0 && s0 && s0 < t0)
410 if (t1 && s1 && s1 > t1)
414 /* No encoding is required. */
420 /* Choose target charset. */
423 if ((tocode1 = mutt_choose_charset (icode, charsets, u, ulen, 0, 0)))
429 /* Hack to avoid labelling 8-bit data as us-ascii. */
430 if (!icode && mutt_is_us_ascii (tocode))
431 tocode = "unknown-8bit";
433 /* Adjust t0 for maximum length of line. */
434 t = u + (ENCWORD_LEN_MAX + 1) - col - ENCWORD_LEN_MIN;
441 /* Adjust t0 until we can encode a character after a space. */
442 for (; t0 > u; t0--) {
443 if (!HSPACE (*(t0 - 1)))
447 while (t < u + ulen && CONTINUATION_BYTE (*t))
449 if (!try_block (t0, t - t0, icode, tocode, &encoder, &wlen) &&
450 col + (t0 - u) + wlen <= ENCWORD_LEN_MAX + 1)
454 /* Adjust t1 until we can encode a character before a space. */
455 for (; t1 < u + ulen; t1++) {
460 while (CONTINUATION_BYTE (*t))
462 if (!try_block (t, t1 - t, icode, tocode, &encoder, &wlen) &&
463 1 + wlen + (u + ulen - t1) <= ENCWORD_LEN_MAX + 1)
467 /* We shall encode the region [t0,t1). */
469 /* Initialise the output buffer with the us-ascii prefix. */
471 buf = p_new(char, buflen);
473 memcpy (buf, u, t0 - u);
479 /* Find how much we can encode. */
480 n = choose_block (t, t1 - t, col, icode, tocode, &encoder, &wlen);
482 /* See if we can fit the us-ascii suffix, too. */
483 if (col + wlen + (u + ulen - t1) <= ENCWORD_LEN_MAX + 1)
487 while (CONTINUATION_BYTE (t[n]))
491 /* This should only happen in the really stupid case where the
492 only word that needs encoding is one character long, but
493 there is too much us-ascii stuff after it to use a single
494 encoded word. We add the next word to the encoded region
496 assert (t1 < u + ulen);
497 for (t1++; t1 < u + ulen && !HSPACE (*t1); t1++);
500 n = choose_block (t, n, col, icode, tocode, &encoder, &wlen);
503 /* Add to output buffer. */
504 #define LINEBREAK "\n\t"
505 if (bufpos + wlen + m_strlen(LINEBREAK) > buflen) {
506 buflen = bufpos + wlen + m_strlen(LINEBREAK);
507 p_realloc(&buf, buflen);
509 r = encode_block (buf + bufpos, t, n, icode, tocode, encoder);
512 memcpy (buf + bufpos, LINEBREAK, m_strlen(LINEBREAK));
513 bufpos += m_strlen(LINEBREAK);
521 /* Add last encoded word and us-ascii suffix to buffer. */
522 buflen = bufpos + wlen + (u + ulen - t1);
523 p_realloc(&buf, buflen + 1);
524 r = encode_block (buf + bufpos, t, t1 - t, icode, tocode, encoder);
527 memcpy (buf + bufpos, t1, u + ulen - t1);
539 void _rfc2047_encode_string (char **pd, int encode_specials, int col)
543 const char *charsets;
545 if (!Charset || !*pd)
548 charsets = SendCharset;
549 if (!charsets || !*charsets)
552 rfc2047_encode (*pd, m_strlen(*pd), col,
553 Charset, charsets, &e, &elen,
554 encode_specials ? RFC822Specials : NULL);
560 void rfc2047_encode_string(char **pd) {
561 _rfc2047_encode_string(pd, 0, 32);
564 void rfc2047_encode_adrlist (address_t * addr, const char *tag)
566 address_t *ptr = addr;
567 int col = tag ? m_strlen(tag) + 2 : 32;
571 _rfc2047_encode_string (&ptr->personal, 1, col);
576 static int rfc2047_decode_word (char *d, const char *s, size_t len)
578 const char *pp, *pp1;
581 int enc = 0, count = 0;
582 char *charset = NULL;
584 pd = d0 = p_new(char, m_strlen(s));
586 for (pp = s; (pp1 = strchr (pp, '?')); pp = pp1 + 1) {
590 /* ignore language specification a la RFC 2231 */
592 if ((t1 = memchr (pp, '*', t - pp)))
594 charset = p_dupstr(pp, t - pp);
597 if (toupper ((unsigned char) *pp) == 'Q')
598 enc = ENCQUOTEDPRINTABLE;
599 else if (toupper ((unsigned char) *pp) == 'B')
608 if (enc == ENCQUOTEDPRINTABLE) {
609 for (; pp < pp1; pp++) {
612 else if (*pp == '=' && hexval(pp[1]) >= 0 && hexval(pp[2]) >= 0) {
613 *pd++ = (hexval (pp[1]) << 4) | hexval (pp[2]);
621 else if (enc == ENCBASE64) {
624 for (; pp < pp1; pp++) {
627 if ((c = base64val(*pp)) < 0)
631 *pd++ = b | (c >> k);
646 mutt_convert_string (&d0, charset, Charset, M_ICONV_HOOK_FROM);
647 m_strcpy(d, len, d0);
654 * Find the start and end of the first encoded word in the string.
655 * We use the grammar in section 2 of RFC 2047, but the "encoding"
656 * must be B or Q. Also, we don't require the encoded word to be
657 * separated by linear-white-space (section 5(1)).
659 static const char *find_encoded_word (const char *s, const char **x)
664 while ((p = strstr (q, "=?"))) {
666 0x20 < *q && *q < 0x7f && !strchr ("()<>@,;:\"/[]?.=", *q); q++);
667 if (q[0] != '?' || !strchr ("BbQq", q[1]) || q[2] != '?')
669 for (q = q + 3; 0x20 <= *q && *q < 0x7f && *q != '?'; q++);
670 if (q[0] != '?' || q[1] != '=') {
682 /* return length of linear white space */
683 static size_t lwslen (const char *s, size_t n)
691 for (; p < s + n; p++)
692 if (!strchr (" \t\r\n", *p)) {
693 len = (size_t) (p - s);
696 if (strchr ("\r\n", *(p - 1))) /* LWS doesn't end with CRLF */
701 /* return length of linear white space : reverse */
702 static size_t lwsrlen (const char *s, size_t n)
704 const char *p = s + n - 1;
710 if (strchr ("\r\n", *p)) /* LWS doesn't end with CRLF */
714 if (!strchr (" \t\r\n", *p)) {
715 len = (size_t) (s + n - 1 - p);
721 /* try to decode anything that looks like a valid RFC2047 encoded
722 * header field, ignoring RFC822 parsing rules
724 void rfc2047_decode (char **pd)
728 int found_encoded = 0;
736 dlen = 4 * m_strlen(s); /* should be enough */
737 d = d0 = p_new(char, dlen + 1);
739 while (*s && dlen > 0) {
740 if (!(p = find_encoded_word (s, &q))) {
741 /* no encoded words */
742 if (!option (OPTSTRICTMIME)) {
744 if (found_encoded && (m = lwslen (s, n)) != 0) {
746 *d = ' ', d++, dlen--;
749 if (ascii_strcasecmp (AssumedCharset, "us-ascii")) {
754 if (mutt_convert_nonmime_string (&t) == 0) {
756 strncpy (d, t, tlen);
767 strncpy (d, s, dlen);
773 n = (size_t) (p - s);
774 /* ignore spaces between encoded words
775 * and linear white spaces between encoded word and *text */
776 if (!option (OPTSTRICTMIME)) {
777 if (found_encoded && (m = lwslen (s, n)) != 0) {
779 *d = ' ', d++, dlen--;
783 if ((m = n - lwsrlen (s, n)) != 0) {
790 *d = ' ', d++, dlen--;
793 else if (!found_encoded || strspn (s, " \t\r\n") != n) {
802 rfc2047_decode_word (d, p, dlen);
816 void rfc2047_decode_adrlist (address_t * a)
820 rfc2047_decode (&a->personal);
825 void rfc2047_decode_envelope (ENVELOPE* e) {
830 /* do RFC2047 decoding */
831 rfc2047_decode_adrlist (e->from);
832 rfc2047_decode_adrlist (e->to);
833 rfc2047_decode_adrlist (e->cc);
834 rfc2047_decode_adrlist (e->bcc);
835 rfc2047_decode_adrlist (e->reply_to);
836 rfc2047_decode_adrlist (e->mail_followup_to);
837 rfc2047_decode_adrlist (e->return_path);
838 rfc2047_decode_adrlist (e->sender);
841 rfc2047_decode (&e->subject);
842 mutt_adjust_subject (e);