2 * This code for parsing rfc2047 encoding of headers has been adapted
3 * from the Mutt 1.5.16 MUA by Martin Mares <mj@ucw.cz>.
5 * Copyright (C) 1996-2000 Michael R. Elkins <me@mutt.org>
6 * Copyright (C) 2000-2001 Edmund Grimley Evans <edmundo@rano.org>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26 #include <sys/types.h>
36 static char *system_charset;
40 #define strfcpy(A,B,C) strncpy(A,B,C), *(A+(C)-1)=0
48 static int Index_hex[128] = {
49 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
50 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
51 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
52 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,-1,-1, -1,-1,-1,-1,
53 -1,10,11,12, 13,14,15,-1, -1,-1,-1,-1, -1,-1,-1,-1,
54 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
55 -1,10,11,12, 13,14,15,-1, -1,-1,-1,-1, -1,-1,-1,-1,
56 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1
59 static int Index_64[128] = {
60 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
61 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
62 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,62, -1,-1,-1,63,
63 52,53,54,55, 56,57,58,59, 60,61,-1,-1, -1,-1,-1,-1,
64 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10, 11,12,13,14,
65 15,16,17,18, 19,20,21,22, 23,24,25,-1, -1,-1,-1,-1,
66 -1,26,27,28, 29,30,31,32, 33,34,35,36, 37,38,39,40,
67 41,42,43,44, 45,46,47,48, 49,50,51,-1, -1,-1,-1,-1
70 #define hexval(c) Index_hex[(unsigned int)(c)]
71 #define base64val(c) Index_64[(unsigned int)(c)]
73 #define OPTIGNORELWS 0
75 static int option(int opt UNUSED)
80 static size_t convert_string (ICONV_CONST char *f, size_t flen,
81 const char *from, const char *to,
82 char **t, size_t *tlen)
89 cd = iconv_open (to, from);
90 if (cd == (iconv_t)(-1))
93 ob = buf = xmalloc (obl);
94 n = iconv (cd, &f, &flen, &ob, &obl);
95 if (n == (size_t)(-1) || iconv (cd, 0, 0, &ob, &obl) == (size_t)(-1))
107 buf = xrealloc (buf, ob - buf + 1);
114 static int rfc2047_decode_word (char *d, const char *s, size_t len)
116 const char *pp, *pp1;
119 int enc = 0, count = 0;
120 char *charset = NULL;
122 pd = d0 = xmalloc (strlen (s));
124 for (pp = s; (pp1 = strchr (pp, '?')); pp = pp1 + 1)
130 /* ignore language specification a la RFC 2231 */
132 if ((t1 = memchr (pp, '*', t - pp)))
134 charset = xmalloc (t - pp + 1);
135 memcpy (charset, pp, t - pp);
136 charset[t-pp] = '\0';
139 if (toupper ((unsigned char) *pp) == 'Q')
140 enc = ENCQUOTEDPRINTABLE;
141 else if (toupper ((unsigned char) *pp) == 'B')
151 if (enc == ENCQUOTEDPRINTABLE)
153 for (; pp < pp1; pp++)
157 else if (*pp == '=' &&
158 (!(pp[1] & ~127) && hexval(pp[1]) != -1) &&
159 (!(pp[2] & ~127) && hexval(pp[2]) != -1))
161 *pd++ = (hexval(pp[1]) << 4) | hexval(pp[2]);
169 else if (enc == ENCBASE64)
173 for (; pp < pp1; pp++)
177 if ((*pp & ~127) || (c = base64val(*pp)) == -1)
182 *pd++ = b | (c >> k);
198 if (charset && system_charset)
199 convert_string (d0, strlen(d0), charset, system_charset, &d0, &dlen);
200 strfcpy (d, d0, len);
207 * Find the start and end of the first encoded word in the string.
208 * We use the grammar in section 2 of RFC 2047, but the "encoding"
209 * must be B or Q. Also, we don't require the encoded word to be
210 * separated by linear-white-space (section 5(1)).
212 static const char *find_encoded_word (const char *s, const char **x)
217 while ((p = strstr (q, "=?")))
220 0x20 < *q && *q < 0x7f && !strchr ("()<>@,;:\"/[]?.=", *q);
223 if (q[0] != '?' || !strchr ("BbQq", q[1]) || q[2] != '?')
225 for (q = q + 3; 0x20 < *q && *q < 0x7f && *q != '?'; q++)
227 if (q[0] != '?' || q[1] != '=')
240 /* return length of linear-white-space */
241 static size_t lwslen (const char *s, size_t n)
249 for (; p < s + n; p++)
250 if (!strchr (" \t\r\n", *p))
252 len = (size_t)(p - s);
255 if (strchr ("\r\n", *(p-1))) /* LWS doesn't end with CRLF */
260 /* return length of linear-white-space : reverse */
261 static size_t lwsrlen (const char *s, size_t n)
263 const char *p = s + n - 1;
269 if (strchr ("\r\n", *p)) /* LWS doesn't end with CRLF */
273 if (!strchr (" \t\r\n", *p))
275 len = (size_t)(s + n - 1 - p);
281 /* try to decode anything that looks like a valid RFC2047 encoded
282 * header field, ignoring RFC822 parsing rules
284 void rfc2047_decode (char **pd)
288 int found_encoded = 0;
296 dlen = 4 * strlen (s); /* should be enough */
297 d = d0 = xmalloc (dlen + 1);
299 while (*s && dlen > 0)
301 if (!(p = find_encoded_word (s, &q)))
303 /* no encoded words */
304 if (option (OPTIGNORELWS))
307 if (found_encoded && (m = lwslen (s, n)) != 0)
310 *d = ' ', d++, dlen--;
314 strncpy (d, s, dlen);
321 n = (size_t) (p - s);
322 /* ignore spaces between encoded word
323 * and linear-white-space between encoded word and *text */
324 if (option (OPTIGNORELWS))
326 if (found_encoded && (m = lwslen (s, n)) != 0)
329 *d = ' ', d++, dlen--;
333 if ((m = n - lwsrlen (s, n)) != 0)
341 *d = ' ', d++, dlen--;
344 else if (!found_encoded || strspn (s, " \t\r\n") != n)
354 rfc2047_decode_word (d, p, dlen);
367 /* Initialize the whole machinery */
368 void rfc2047_init(void)
370 setlocale(LC_CTYPE, "");
371 system_charset = nl_langinfo(CODESET);
372 if (!system_charset[0])
373 system_charset = NULL;
374 debug("Charset is %s\n", system_charset);