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>
47 /* 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, ssize_t flen,
73 const char *to, char **t, ssize_t *tlen)
79 cd = mutt_iconv_open(to, from, 0);
81 if (cd == (iconv_t)(-1))
85 ob = buf = p_new(char, obl);
86 n = my_iconv(cd, &f, &flen, &ob, &obl);
88 if (n < 0 || my_iconv(cd, 0, 0, &ob, &obl) < 0) {
104 /* choose the shortest encoding for u */
105 char *mutt_choose_charset(const char *fromcode, const char *charsets,
106 char *u, ssize_t ulen, char **dst, ssize_t *dlen)
114 const char *p = charsets;
117 char cset[SHORT_STRING];
124 n = m_strncpy(cset, sizeof(cset), p, q - p);
127 n = m_strcpy(cset, sizeof(cset), p);
132 /* Assume that we never need more than 12 characters of
133 encoded-text to encode a single character. */
134 n > (ENCWORD_LEN_MAX - ENCWORD_LEN_MIN + 2 - 12))
137 n = convert_string(fromcode, u, ulen, cset, &s, &slen);
141 if (!tocode || n < bestn) {
142 m_strreplace(&tocode, cset);
156 char buf[LONG_STRING];
165 mutt_canonical_charset(buf, sizeof(buf), tocode);
166 m_strreplace(&tocode, buf);
172 static size_t b_encoder (char *s, const char *d, size_t dlen,
177 memcpy (s, "=?", 2), s += 2;
178 memcpy (s, tocode, m_strlen(tocode)), s += m_strlen(tocode);
179 memcpy (s, "?B?", 3), s += 3;
183 else if (dlen == 1) {
184 *s++ = __m_b64chars[(*d >> 2) & 0x3f];
185 *s++ = __m_b64chars[(*d & 0x03) << 4];
190 else if (dlen == 2) {
191 *s++ = __m_b64chars[(*d >> 2) & 0x3f];
192 *s++ = __m_b64chars[((*d & 0x03) << 4) | ((d[1] >> 4) & 0x0f)];
193 *s++ = __m_b64chars[(d[1] & 0x0f) << 2];
198 *s++ = __m_b64chars[(*d >> 2) & 0x3f];
199 *s++ = __m_b64chars[((*d & 0x03) << 4) | ((d[1] >> 4) & 0x0f)];
200 *s++ = __m_b64chars[((d[1] & 0x0f) << 2) | ((d[2] >> 6) & 0x03)];
201 *s++ = __m_b64chars[d[2] & 0x3f];
205 memcpy (s, "?=", 2), s += 2;
209 static size_t q_encoder (char *s, const char *d, size_t dlen,
214 memcpy (s, "=?", 2), s += 2;
215 memcpy (s, tocode, m_strlen(tocode)), s += m_strlen(tocode);
216 memcpy (s, "?Q?", 3), s += 3;
218 unsigned char c = *d++;
222 else if (c >= 0x7f || c < 0x20 || c == '_' || strchr (MimeSpecials, c)) {
224 *s++ = __m_b36chars_upper[c >> 4];
225 *s++ = __m_b36chars_upper[c & 0xf];
230 memcpy (s, "?=", 2), s += 2;
235 * Return 0 if and set *encoder and *wlen if the data (d, dlen) could
236 * be converted to an encoded word of length *wlen using *encoder.
237 * Otherwise return an upper bound on the maximum length of the data
238 * which could be converted.
239 * The data is converted from fromcode (which must be stateless) to
240 * tocode, unless fromcode is 0, in which case the data is assumed to
241 * be already in tocode, which should be 8-bit and stateless.
243 static size_t try_block (const char *d, ssize_t dlen,
244 const char *fromcode, const char *tocode,
245 encoder_t * encoder, ssize_t *wlen)
247 char buf1[ENCWORD_LEN_MAX - ENCWORD_LEN_MIN + 1];
252 int count, len, len_b, len_q;
255 cd = mutt_iconv_open (tocode, fromcode, 0);
256 assert (cd != (iconv_t) (-1));
257 ib = d, ibl = dlen, ob = buf1, obl = sizeof (buf1) - m_strlen(tocode);
258 if (my_iconv(cd, &ib, &ibl, &ob, &obl) < 0
259 || my_iconv(cd, 0, 0, &ob, &obl) < 0)
261 assert (errno == E2BIG);
264 return (ib - d == dlen) ? dlen : ib - d + 1;
269 if (dlen > ssizeof(buf1) - m_strlen(tocode))
270 return ssizeof(buf1) - m_strlen(tocode) + 1;
271 memcpy (buf1, d, dlen);
276 for (p = buf1; p < ob; p++) {
277 unsigned char c = *p;
279 assert (strchr (MimeSpecials, '?'));
280 if (c >= 0x7f || c < 0x20 || *p == '_' ||
281 (c != ' ' && strchr (MimeSpecials, *p)))
285 len = ENCWORD_LEN_MIN - 2 + m_strlen(tocode);
286 len_b = len + (((ob - buf1) + 2) / 3) * 4;
287 len_q = len + (ob - buf1) + 2 * count;
289 /* Apparently RFC 1468 says to use B encoding for iso-2022-jp. */
290 if (!ascii_strcasecmp (tocode, "ISO-2022-JP"))
291 len_q = ENCWORD_LEN_MAX + 1;
293 if (len_b < len_q && len_b <= ENCWORD_LEN_MAX) {
294 *encoder = b_encoder;
298 else if (len_q <= ENCWORD_LEN_MAX) {
299 *encoder = q_encoder;
308 * Encode the data (d, dlen) into s using the encoder.
309 * Return the length of the encoded word.
311 static size_t encode_block (char *s, char *d, ssize_t dlen,
312 const char *fromcode, const char *tocode,
315 char buf1[ENCWORD_LEN_MAX - ENCWORD_LEN_MIN + 1];
316 ssize_t ibl, obl, n1, n2;
322 cd = mutt_iconv_open (tocode, fromcode, 0);
323 assert (cd != (iconv_t) (-1));
324 ib = d, ibl = dlen, ob = buf1, obl = sizeof (buf1) - m_strlen(tocode);
325 n1 = my_iconv(cd, &ib, &ibl, &ob, &obl);
326 n2 = my_iconv(cd, 0, 0, &ob, &obl);
327 assert (n1 >= 0 && n2 >= 0);
329 return (*encoder) (s, buf1, ob - buf1, tocode);
332 return (*encoder) (s, d, dlen, tocode);
336 * Discover how much of the data (d, dlen) can be converted into
337 * a single encoded word. Return how much data can be converted,
338 * and set the length *wlen of the encoded word and *encoder.
339 * We start in column col, which limits the length of the word.
341 static size_t choose_block(char *d, size_t dlen, int col,
342 const char *fromcode, const char *tocode,
343 encoder_t *encoder, ssize_t *wlen)
346 int utf8 = fromcode && !ascii_strcasecmp (fromcode, "UTF-8");
351 nn = try_block (d, n, fromcode, tocode, encoder, wlen);
352 if (!nn && (col + *wlen <= ENCWORD_LEN_MAX + 1 || n <= 1))
354 n = (nn ? nn : n) - 1;
357 while (n > 1 && CONTINUATION_BYTE (d[n]))
364 * Place the result of RFC-2047-encoding (d, dlen) into the dynamically
365 * allocated buffer (e, elen). The input data is in charset fromcode
366 * and is converted into a charset chosen from charsets.
367 * Return 1 if the conversion to UTF-8 failed, 2 if conversion from UTF-8
368 * failed, otherwise 0. If conversion failed, fromcode is assumed to be
369 * compatible with us-ascii and the original data is used.
370 * The input data is assumed to be a single line starting at column col;
371 * if col is non-zero, the preceding character was a space.
373 static int rfc2047_encode (const char *d, ssize_t dlen, int col,
374 const char *fromcode, const char *charsets,
375 char **e, ssize_t *elen, const char *specials)
379 ssize_t bufpos, buflen;
380 char *u, *t0, *t1, *t;
382 ssize_t ulen, r, n, wlen;
386 const char *icode = "UTF-8";
388 /* Try to convert to UTF-8. */
389 if (convert_string(fromcode, d, dlen, icode, &u, &ulen)) {
392 u = p_dupstr(d, ulen = dlen);
395 /* Find earliest and latest things we must encode. */
396 s0 = s1 = t0 = t1 = 0;
397 for (t = u; t < u + ulen; t++) {
399 (*t == '=' && t[1] == '?' && (t == u || HSPACE (*(t - 1))))) {
404 else if (specials && strchr (specials, *t)) {
411 /* If we have something to encode, include RFC822 specials */
412 if (t0 && s0 && s0 < t0)
414 if (t1 && s1 && s1 > t1)
418 /* No encoding is required. */
424 /* Choose target charset. */
427 if ((tocode1 = mutt_choose_charset(icode, charsets, u, ulen,
434 /* Hack to avoid labelling 8-bit data as us-ascii. */
435 if (!icode && mutt_is_us_ascii (tocode))
436 tocode = "unknown-8bit";
438 /* Adjust t0 for maximum length of line. */
439 t = u + (ENCWORD_LEN_MAX + 1) - col - ENCWORD_LEN_MIN;
446 /* Adjust t0 until we can encode a character after a space. */
447 for (; t0 > u; t0--) {
448 if (!HSPACE (*(t0 - 1)))
452 while (t < u + ulen && CONTINUATION_BYTE (*t))
454 if (!try_block (t0, t - t0, icode, tocode, &encoder, &wlen) &&
455 col + (t0 - u) + wlen <= ENCWORD_LEN_MAX + 1)
459 /* Adjust t1 until we can encode a character before a space. */
460 for (; t1 < u + ulen; t1++) {
465 while (CONTINUATION_BYTE (*t))
467 if (!try_block (t, t1 - t, icode, tocode, &encoder, &wlen) &&
468 1 + wlen + (u + ulen - t1) <= ENCWORD_LEN_MAX + 1)
472 /* We shall encode the region [t0,t1). */
474 /* Initialise the output buffer with the us-ascii prefix. */
476 buf = p_new(char, buflen);
478 memcpy (buf, u, t0 - u);
484 /* Find how much we can encode. */
485 n = choose_block (t, t1 - t, col, icode, tocode, &encoder, &wlen);
487 /* See if we can fit the us-ascii suffix, too. */
488 if (col + wlen + (u + ulen - t1) <= ENCWORD_LEN_MAX + 1)
492 while (CONTINUATION_BYTE (t[n]))
496 /* This should only happen in the really stupid case where the
497 only word that needs encoding is one character long, but
498 there is too much us-ascii stuff after it to use a single
499 encoded word. We add the next word to the encoded region
501 assert (t1 < u + ulen);
502 for (t1++; t1 < u + ulen && !HSPACE (*t1); t1++);
505 n = choose_block (t, n, col, icode, tocode, &encoder, &wlen);
508 /* Add to output buffer. */
509 #define LINEBREAK "\n\t"
510 if (bufpos + wlen + m_strlen(LINEBREAK) > buflen) {
511 buflen = bufpos + wlen + m_strlen(LINEBREAK);
512 p_realloc(&buf, buflen);
514 r = encode_block (buf + bufpos, t, n, icode, tocode, encoder);
517 memcpy (buf + bufpos, LINEBREAK, m_strlen(LINEBREAK));
518 bufpos += m_strlen(LINEBREAK);
526 /* Add last encoded word and us-ascii suffix to buffer. */
527 buflen = bufpos + wlen + (u + ulen - t1);
528 p_realloc(&buf, buflen + 1);
529 r = encode_block (buf + bufpos, t, t1 - t, icode, tocode, encoder);
532 memcpy (buf + bufpos, t1, u + ulen - t1);
544 void _rfc2047_encode_string (char **pd, int encode_specials, int col)
548 const char *charsets;
550 if (!Charset || !*pd)
553 charsets = SendCharset;
554 if (!charsets || !*charsets)
557 rfc2047_encode (*pd, m_strlen(*pd), col,
558 Charset, charsets, &e, &elen,
559 encode_specials ? RFC822Specials : NULL);
565 void rfc2047_encode_string(char **pd) {
566 _rfc2047_encode_string(pd, 0, 32);
569 void rfc2047_encode_adrlist (address_t * addr, const char *tag)
571 address_t *ptr = addr;
572 int col = tag ? m_strlen(tag) + 2 : 32;
576 _rfc2047_encode_string (&ptr->personal, 1, col);
581 static int rfc2047_decode_word (char *d, const char *s, size_t len)
583 const char *pp, *pp1;
586 int enc = 0, count = 0;
587 char *charset = NULL;
589 pd = d0 = p_new(char, m_strlen(s));
591 for (pp = s; (pp1 = strchr (pp, '?')); pp = pp1 + 1) {
595 /* ignore language specification a la RFC 2231 */
597 if ((t1 = memchr (pp, '*', t - pp)))
599 charset = p_dupstr(pp, t - pp);
602 if (toupper ((unsigned char) *pp) == 'Q')
603 enc = ENCQUOTEDPRINTABLE;
604 else if (toupper ((unsigned char) *pp) == 'B')
613 if (enc == ENCQUOTEDPRINTABLE) {
614 for (; pp < pp1; pp++) {
617 else if (*pp == '=' && hexval(pp[1]) >= 0 && hexval(pp[2]) >= 0) {
618 *pd++ = (hexval (pp[1]) << 4) | hexval (pp[2]);
626 else if (enc == ENCBASE64) {
629 for (; pp < pp1; pp++) {
632 if ((c = base64val(*pp)) < 0)
636 *pd++ = b | (c >> k);
651 mutt_convert_string (&d0, charset, Charset, M_ICONV_HOOK_FROM);
652 m_strcpy(d, len, d0);
659 * Find the start and end of the first encoded word in the string.
660 * We use the grammar in section 2 of RFC 2047, but the "encoding"
661 * must be B or Q. Also, we don't require the encoded word to be
662 * separated by linear-white-space (section 5(1)).
664 static const char *find_encoded_word (const char *s, const char **x)
669 while ((p = strstr (q, "=?"))) {
671 0x20 < *q && *q < 0x7f && !strchr ("()<>@,;:\"/[]?.=", *q); q++);
672 if (q[0] != '?' || !strchr ("BbQq", q[1]) || q[2] != '?')
674 for (q = q + 3; 0x20 <= *q && *q < 0x7f && *q != '?'; q++);
675 if (q[0] != '?' || q[1] != '=') {
687 /* return length of linear white space */
688 static size_t lwslen (const char *s, size_t n)
696 for (; p < s + n; p++)
697 if (!strchr (" \t\r\n", *p)) {
698 len = (size_t) (p - s);
701 if (strchr ("\r\n", *(p - 1))) /* LWS doesn't end with CRLF */
706 /* return length of linear white space : reverse */
707 static size_t lwsrlen (const char *s, size_t n)
709 const char *p = s + n - 1;
715 if (strchr ("\r\n", *p)) /* LWS doesn't end with CRLF */
719 if (!strchr (" \t\r\n", *p)) {
720 len = (size_t) (s + n - 1 - p);
726 /* try to decode anything that looks like a valid RFC2047 encoded
727 * header field, ignoring RFC822 parsing rules
729 void rfc2047_decode (char **pd)
733 int found_encoded = 0;
741 dlen = 4 * m_strlen(s); /* should be enough */
742 d = d0 = p_new(char, dlen + 1);
744 while (*s && dlen > 0) {
745 if (!(p = find_encoded_word (s, &q))) {
746 /* no encoded words */
747 if (!option (OPTSTRICTMIME)) {
749 if (found_encoded && (m = lwslen (s, n)) != 0) {
751 *d = ' ', d++, dlen--;
754 if (ascii_strcasecmp (AssumedCharset, "us-ascii")) {
759 if (mutt_convert_nonmime_string (&t) == 0) {
761 strncpy (d, t, tlen);
772 strncpy (d, s, dlen);
779 /* ignore spaces between encoded words
780 * and linear white spaces between encoded word and *text */
781 if (!option (OPTSTRICTMIME)) {
782 if (found_encoded && (m = lwslen (s, n)) != 0) {
784 *d = ' ', d++, dlen--;
788 if ((m = n - lwsrlen (s, n)) != 0) {
795 *d = ' ', d++, dlen--;
798 else if (!found_encoded || strspn (s, " \t\r\n") != n) {
807 rfc2047_decode_word (d, p, dlen);
821 void rfc2047_decode_adrlist(address_t *a)
825 rfc2047_decode(&a->personal);
830 void rfc2047_decode_envelope(ENVELOPE* e)
834 /* do RFC2047 decoding */
835 rfc2047_decode_adrlist(e->from);
836 rfc2047_decode_adrlist(e->to);
837 rfc2047_decode_adrlist(e->cc);
838 rfc2047_decode_adrlist(e->bcc);
839 rfc2047_decode_adrlist(e->reply_to);
840 rfc2047_decode_adrlist(e->mail_followup_to);
841 rfc2047_decode_adrlist(e->return_path);
842 rfc2047_decode_adrlist(e->sender);
845 rfc2047_decode(&e->subject);
846 mutt_adjust_subject(e);