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>
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.
29 /* If you are debugging this file, comment out the following line. */
38 #define ENCWORD_LEN_MAX 75
39 #define ENCWORD_LEN_MIN 9 /* mutt_strlen ("=?.?.?.?=") */
41 #define HSPACE(x) ((x) == '\0' || (x) == ' ' || (x) == '\t')
43 #define CONTINUATION_BYTE(c) (((c) & 0xc0) == 0x80)
45 extern char RFC822Specials[];
47 typedef size_t (*encoder_t) (char *, ICONV_CONST char *, size_t,
50 static size_t convert_string (ICONV_CONST char *f, size_t flen,
51 const char *from, const char *to,
52 char **t, size_t * tlen)
59 cd = mutt_iconv_open (to, from, 0);
60 if (cd == (iconv_t) (-1))
63 ob = buf = safe_malloc (obl);
64 n = iconv (cd, &f, &flen, &ob, &obl);
65 if (n == (size_t) (-1) || iconv (cd, 0, 0, &ob, &obl) == (size_t) (-1)) {
76 safe_realloc (&buf, ob - buf + 1);
83 char *mutt_choose_charset (const char *fromcode, const char *charsets,
84 char *u, size_t ulen, char **d, size_t * dlen)
86 char canonical_buff[LONG_STRING];
87 char *e = 0, *tocode = 0;
88 size_t elen = 0, bestn = 0;
91 for (p = charsets; p; p = q ? q + 1 : 0) {
97 n = q ? q - p : mutt_strlen (p);
100 /* Assume that we never need more than 12 characters of
101 encoded-text to encode a single character. */
102 n > (ENCWORD_LEN_MAX - ENCWORD_LEN_MIN + 2 - 12))
105 t = safe_malloc (n + 1);
109 n = convert_string (u, ulen, fromcode, t, &s, &slen);
110 if (n == (size_t) (-1))
113 if (!tocode || n < bestn) {
138 mutt_canonical_charset (canonical_buff, sizeof (canonical_buff), tocode);
139 mutt_str_replace (&tocode, canonical_buff);
144 static size_t b_encoder (char *s, ICONV_CONST char *d, size_t dlen,
149 memcpy (s, "=?", 2), s += 2;
150 memcpy (s, tocode, mutt_strlen (tocode)), s += mutt_strlen (tocode);
151 memcpy (s, "?B?", 3), s += 3;
155 else if (dlen == 1) {
156 *s++ = B64Chars[(*d >> 2) & 0x3f];
157 *s++ = B64Chars[(*d & 0x03) << 4];
162 else if (dlen == 2) {
163 *s++ = B64Chars[(*d >> 2) & 0x3f];
164 *s++ = B64Chars[((*d & 0x03) << 4) | ((d[1] >> 4) & 0x0f)];
165 *s++ = B64Chars[(d[1] & 0x0f) << 2];
170 *s++ = B64Chars[(*d >> 2) & 0x3f];
171 *s++ = B64Chars[((*d & 0x03) << 4) | ((d[1] >> 4) & 0x0f)];
172 *s++ = B64Chars[((d[1] & 0x0f) << 2) | ((d[2] >> 6) & 0x03)];
173 *s++ = B64Chars[d[2] & 0x3f];
177 memcpy (s, "?=", 2), s += 2;
181 static size_t q_encoder (char *s, ICONV_CONST char *d, size_t dlen,
184 char hex[] = "0123456789ABCDEF";
187 memcpy (s, "=?", 2), s += 2;
188 memcpy (s, tocode, mutt_strlen (tocode)), s += mutt_strlen (tocode);
189 memcpy (s, "?Q?", 3), s += 3;
191 unsigned char c = *d++;
195 else if (c >= 0x7f || c < 0x20 || c == '_' || strchr (MimeSpecials, c)) {
197 *s++ = hex[(c & 0xf0) >> 4];
198 *s++ = hex[c & 0x0f];
203 memcpy (s, "?=", 2), s += 2;
208 * Return 0 if and set *encoder and *wlen if the data (d, dlen) could
209 * be converted to an encoded word of length *wlen using *encoder.
210 * Otherwise return an upper bound on the maximum length of the data
211 * which could be converted.
212 * The data is converted from fromcode (which must be stateless) to
213 * tocode, unless fromcode is 0, in which case the data is assumed to
214 * be already in tocode, which should be 8-bit and stateless.
216 static size_t try_block (ICONV_CONST char *d, size_t dlen,
217 const char *fromcode, const char *tocode,
218 encoder_t * encoder, size_t * wlen)
220 char buf1[ENCWORD_LEN_MAX - ENCWORD_LEN_MIN + 1];
222 ICONV_CONST char *ib;
225 int count, len, len_b, len_q;
228 cd = mutt_iconv_open (tocode, fromcode, 0);
229 assert (cd != (iconv_t) (-1));
230 ib = d, ibl = dlen, ob = buf1, obl = sizeof (buf1) - mutt_strlen (tocode);
231 if (iconv (cd, &ib, &ibl, &ob, &obl) == (size_t) (-1) ||
232 iconv (cd, 0, 0, &ob, &obl) == (size_t) (-1)) {
233 assert (errno == E2BIG);
236 return (ib - d == dlen) ? dlen : ib - d + 1;
241 if (dlen > sizeof (buf1) - mutt_strlen (tocode))
242 return sizeof (buf1) - mutt_strlen (tocode) + 1;
243 memcpy (buf1, d, dlen);
248 for (p = buf1; p < ob; p++) {
249 unsigned char c = *p;
251 assert (strchr (MimeSpecials, '?'));
252 if (c >= 0x7f || c < 0x20 || *p == '_' ||
253 (c != ' ' && strchr (MimeSpecials, *p)))
257 len = ENCWORD_LEN_MIN - 2 + mutt_strlen (tocode);
258 len_b = len + (((ob - buf1) + 2) / 3) * 4;
259 len_q = len + (ob - buf1) + 2 * count;
261 /* Apparently RFC 1468 says to use B encoding for iso-2022-jp. */
262 if (!ascii_strcasecmp (tocode, "ISO-2022-JP"))
263 len_q = ENCWORD_LEN_MAX + 1;
265 if (len_b < len_q && len_b <= ENCWORD_LEN_MAX) {
266 *encoder = b_encoder;
270 else if (len_q <= ENCWORD_LEN_MAX) {
271 *encoder = q_encoder;
280 * Encode the data (d, dlen) into s using the encoder.
281 * Return the length of the encoded word.
283 static size_t encode_block (char *s, char *d, size_t dlen,
284 const char *fromcode, const char *tocode,
287 char buf1[ENCWORD_LEN_MAX - ENCWORD_LEN_MIN + 1];
289 ICONV_CONST char *ib;
291 size_t ibl, obl, n1, n2;
294 cd = mutt_iconv_open (tocode, fromcode, 0);
295 assert (cd != (iconv_t) (-1));
296 ib = d, ibl = dlen, ob = buf1, obl = sizeof (buf1) - mutt_strlen (tocode);
297 n1 = iconv (cd, &ib, &ibl, &ob, &obl);
298 n2 = iconv (cd, 0, 0, &ob, &obl);
299 assert (n1 != (size_t) (-1) && n2 != (size_t) (-1));
301 return (*encoder) (s, buf1, ob - buf1, tocode);
304 return (*encoder) (s, d, dlen, tocode);
308 * Discover how much of the data (d, dlen) can be converted into
309 * a single encoded word. Return how much data can be converted,
310 * and set the length *wlen of the encoded word and *encoder.
311 * We start in column col, which limits the length of the word.
313 static size_t choose_block (char *d, size_t dlen, int col,
314 const char *fromcode, const char *tocode,
315 encoder_t * encoder, size_t * wlen)
318 int utf8 = fromcode && !ascii_strcasecmp (fromcode, "UTF-8");
323 nn = try_block (d, n, fromcode, tocode, encoder, wlen);
324 if (!nn && (col + *wlen <= ENCWORD_LEN_MAX + 1 || n <= 1))
326 n = (nn ? nn : n) - 1;
329 while (n > 1 && CONTINUATION_BYTE (d[n]))
336 * Place the result of RFC-2047-encoding (d, dlen) into the dynamically
337 * allocated buffer (e, elen). The input data is in charset fromcode
338 * and is converted into a charset chosen from charsets.
339 * Return 1 if the conversion to UTF-8 failed, 2 if conversion from UTF-8
340 * failed, otherwise 0. If conversion failed, fromcode is assumed to be
341 * compatible with us-ascii and the original data is used.
342 * The input data is assumed to be a single line starting at column col;
343 * if col is non-zero, the preceding character was a space.
345 static int rfc2047_encode (ICONV_CONST char *d, size_t dlen, int col,
346 const char *fromcode, const char *charsets,
347 char **e, size_t * elen, char *specials)
351 size_t bufpos, buflen;
352 char *u, *t0, *t1, *t;
354 size_t ulen, r, n, wlen;
358 char *icode = "UTF-8";
360 /* Try to convert to UTF-8. */
361 if (convert_string (d, dlen, fromcode, icode, &u, &ulen)) {
364 u = safe_malloc ((ulen = dlen) + 1);
369 /* Find earliest and latest things we must encode. */
370 s0 = s1 = t0 = t1 = 0;
371 for (t = u; t < u + ulen; t++) {
373 (*t == '=' && t[1] == '?' && (t == u || HSPACE (*(t - 1))))) {
378 else if (specials && strchr (specials, *t)) {
385 /* If we have something to encode, include RFC822 specials */
386 if (t0 && s0 && s0 < t0)
388 if (t1 && s1 && s1 > t1)
392 /* No encoding is required. */
398 /* Choose target charset. */
401 if ((tocode1 = mutt_choose_charset (icode, charsets, u, ulen, 0, 0)))
407 /* Hack to avoid labelling 8-bit data as us-ascii. */
408 if (!icode && mutt_is_us_ascii (tocode))
409 tocode = "unknown-8bit";
411 /* Adjust t0 for maximum length of line. */
412 t = u + (ENCWORD_LEN_MAX + 1) - col - ENCWORD_LEN_MIN;
419 /* Adjust t0 until we can encode a character after a space. */
420 for (; t0 > u; t0--) {
421 if (!HSPACE (*(t0 - 1)))
425 while (t < u + ulen && CONTINUATION_BYTE (*t))
427 if (!try_block (t0, t - t0, icode, tocode, &encoder, &wlen) &&
428 col + (t0 - u) + wlen <= ENCWORD_LEN_MAX + 1)
432 /* Adjust t1 until we can encode a character before a space. */
433 for (; t1 < u + ulen; t1++) {
438 while (CONTINUATION_BYTE (*t))
440 if (!try_block (t, t1 - t, icode, tocode, &encoder, &wlen) &&
441 1 + wlen + (u + ulen - t1) <= ENCWORD_LEN_MAX + 1)
445 /* We shall encode the region [t0,t1). */
447 /* Initialise the output buffer with the us-ascii prefix. */
449 buf = safe_malloc (buflen);
451 memcpy (buf, u, t0 - u);
457 /* Find how much we can encode. */
458 n = choose_block (t, t1 - t, col, icode, tocode, &encoder, &wlen);
460 /* See if we can fit the us-ascii suffix, too. */
461 if (col + wlen + (u + ulen - t1) <= ENCWORD_LEN_MAX + 1)
465 while (CONTINUATION_BYTE (t[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
474 assert (t1 < u + ulen);
475 for (t1++; t1 < u + ulen && !HSPACE (*t1); t1++);
478 n = choose_block (t, n, col, icode, tocode, &encoder, &wlen);
481 /* Add to output buffer. */
482 #define LINEBREAK "\n\t"
483 if (bufpos + wlen + mutt_strlen (LINEBREAK) > buflen) {
484 buflen = bufpos + wlen + mutt_strlen (LINEBREAK);
485 safe_realloc (&buf, buflen);
487 r = encode_block (buf + bufpos, t, n, icode, tocode, encoder);
490 memcpy (buf + bufpos, LINEBREAK, mutt_strlen (LINEBREAK));
491 bufpos += mutt_strlen (LINEBREAK);
499 /* Add last encoded word and us-ascii suffix to buffer. */
500 buflen = bufpos + wlen + (u + ulen - t1);
501 safe_realloc (&buf, buflen + 1);
502 r = encode_block (buf + bufpos, t, t1 - t, icode, tocode, encoder);
505 memcpy (buf + bufpos, t1, u + ulen - t1);
517 void _rfc2047_encode_string (char **pd, int encode_specials, int col)
523 if (!Charset || !*pd)
526 charsets = SendCharset;
527 if (!charsets || !*charsets)
530 rfc2047_encode (*pd, mutt_strlen (*pd), col,
531 Charset, charsets, &e, &elen,
532 encode_specials ? RFC822Specials : NULL);
538 void rfc2047_encode_adrlist (ADDRESS * addr, const char *tag)
541 int col = tag ? mutt_strlen (tag) + 2 : 32;
545 _rfc2047_encode_string (&ptr->personal, 1, col);
548 _rfc2047_encode_string (&ptr->val, 1, col);
554 static int rfc2047_decode_word (char *d, const char *s, size_t len)
556 const char *pp, *pp1;
559 int enc = 0, count = 0;
560 char *charset = NULL;
562 pd = d0 = safe_malloc (mutt_strlen (s));
564 for (pp = s; (pp1 = strchr (pp, '?')); pp = pp1 + 1) {
568 /* ignore language specification a la RFC 2231 */
570 if ((t1 = memchr (pp, '*', t - pp)))
572 charset = safe_malloc (t - pp + 1);
573 memcpy (charset, pp, t - pp);
574 charset[t - pp] = '\0';
577 if (toupper ((unsigned char) *pp) == 'Q')
578 enc = ENCQUOTEDPRINTABLE;
579 else if (toupper ((unsigned char) *pp) == 'B')
588 if (enc == ENCQUOTEDPRINTABLE) {
589 for (; pp < pp1; pp++) {
592 else if (*pp == '=' &&
593 (!(pp[1] & ~127) && hexval (pp[1]) != -1) &&
594 (!(pp[2] & ~127) && hexval (pp[2]) != -1)) {
595 *pd++ = (hexval (pp[1]) << 4) | hexval (pp[2]);
603 else if (enc == ENCBASE64) {
606 for (; pp < pp1; pp++) {
609 if ((*pp & ~127) || (c = base64val (*pp)) == -1)
613 *pd++ = b | (c >> k);
628 mutt_convert_string (&d0, charset, Charset, M_ICONV_HOOK_FROM);
629 strfcpy (d, d0, len);
636 * Find the start and end of the first encoded word in the string.
637 * We use the grammar in section 2 of RFC 2047, but the "encoding"
638 * must be B or Q. Also, we don't require the encoded word to be
639 * separated by linear-white-space (section 5(1)).
641 static const char *find_encoded_word (const char *s, const char **x)
646 while ((p = strstr (q, "=?"))) {
648 0x20 < *q && *q < 0x7f && !strchr ("()<>@,;:\"/[]?.=", *q); q++);
649 if (q[0] != '?' || !strchr ("BbQq", q[1]) || q[2] != '?')
651 for (q = q + 3; 0x20 <= *q && *q < 0x7f && *q != '?'; q++);
652 if (q[0] != '?' || q[1] != '=') {
664 /* return length of linear white space */
665 static size_t lwslen (const char *s, size_t n)
673 for (; p < s + n; p++)
674 if (!strchr (" \t\r\n", *p)) {
675 len = (size_t) (p - s);
678 if (strchr ("\r\n", *(p - 1))) /* LWS doesn't end with CRLF */
683 /* return length of linear white space : reverse */
684 static size_t lwsrlen (const char *s, size_t n)
686 const char *p = s + n - 1;
692 if (strchr ("\r\n", *p)) /* LWS doesn't end with CRLF */
696 if (!strchr (" \t\r\n", *p)) {
697 len = (size_t) (s + n - 1 - p);
703 /* try to decode anything that looks like a valid RFC2047 encoded
704 * header field, ignoring RFC822 parsing rules
706 void rfc2047_decode (char **pd)
710 int found_encoded = 0;
718 dlen = 4 * mutt_strlen (s); /* should be enough */
719 d = d0 = safe_malloc (dlen + 1);
721 while (*s && dlen > 0) {
722 if (!(p = find_encoded_word (s, &q))) {
723 /* no encoded words */
724 if (!option (OPTSTRICTMIME)) {
726 if (found_encoded && (m = lwslen (s, n)) != 0) {
728 *d = ' ', d++, dlen--;
731 if (ascii_strcasecmp (AssumedCharset, "us-ascii")) {
735 t = safe_malloc (n + 1);
736 strfcpy (t, s, n + 1);
737 if (mutt_convert_nonmime_string (&t) == 0) {
738 tlen = mutt_strlen (t);
739 strncpy (d, t, tlen);
750 strncpy (d, s, dlen);
756 n = (size_t) (p - s);
757 /* ignore spaces between encoded words
758 * and linear white spaces between encoded word and *text */
759 if (!option (OPTSTRICTMIME)) {
760 if (found_encoded && (m = lwslen (s, n)) != 0) {
762 *d = ' ', d++, dlen--;
766 if ((m = n - lwsrlen (s, n)) != 0) {
773 *d = ' ', d++, dlen--;
776 else if (!found_encoded || strspn (s, " \t\r\n") != n) {
785 rfc2047_decode_word (d, p, dlen);
796 mutt_str_adjust (pd);
799 void rfc2047_decode_adrlist (ADDRESS * a)
803 rfc2047_decode (&a->personal);
805 if (a->val && strstr (a->val, "=?") != NULL)
806 rfc2047_decode (&a->val);