2 * Incoming Mail Checker: Charsets
4 * (c) 2007 Martin Mares <mj@ucw.cz>
6 * The code for parsing rfc2047 encoding of headers has been adapted
7 * from the Mutt 1.5.16 MUA. Here is the original copyright message:
9 * Copyright (C) 1996-2000 Michael R. Elkins <me@mutt.org>
10 * Copyright (C) 2000-2001 Edmund Grimley Evans <edmundo@rano.org>
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
41 static char *system_charset;
43 #define strfcpy(A,B,C) strncpy(A,B,C), *(A+(C)-1)=0
51 static int Index_hex[128] = {
52 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
53 -1,-1,-1,-1, -1,-1,-1,-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 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,-1,-1, -1,-1,-1,-1,
56 -1,10,11,12, 13,14,15,-1, -1,-1,-1,-1, -1,-1,-1,-1,
57 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
58 -1,10,11,12, 13,14,15,-1, -1,-1,-1,-1, -1,-1,-1,-1,
59 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1
62 static int Index_64[128] = {
63 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
64 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
65 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,62, -1,-1,-1,63,
66 52,53,54,55, 56,57,58,59, 60,61,-1,-1, -1,-1,-1,-1,
67 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10, 11,12,13,14,
68 15,16,17,18, 19,20,21,22, 23,24,25,-1, -1,-1,-1,-1,
69 -1,26,27,28, 29,30,31,32, 33,34,35,36, 37,38,39,40,
70 41,42,43,44, 45,46,47,48, 49,50,51,-1, -1,-1,-1,-1
73 #define hexval(c) Index_hex[(unsigned int)(c)]
74 #define base64val(c) Index_64[(unsigned int)(c)]
76 #define OPTIGNORELWS 0
78 static int option(int opt UNUSED)
83 static size_t convert_string (char *f, size_t flen,
84 const char *from, const char *to,
85 char **t, size_t *tlen)
92 cd = iconv_open (to, from);
93 if (cd == (iconv_t)(-1))
96 ob = buf = xmalloc (obl);
97 n = iconv (cd, &f, &flen, &ob, &obl);
98 if (n == (size_t)(-1) || iconv (cd, 0, 0, &ob, &obl) == (size_t)(-1))
110 buf = xrealloc (buf, ob - buf + 1);
117 static int rfc2047_decode_word (char *d, const char *s, size_t len)
119 const char *pp, *pp1;
122 int enc = 0, count = 0;
123 char *charset = NULL;
125 pd = d0 = xmalloc (strlen (s));
127 for (pp = s; (pp1 = strchr (pp, '?')); pp = pp1 + 1)
133 /* ignore language specification a la RFC 2231 */
135 if ((t1 = memchr (pp, '*', t - pp)))
137 charset = xmalloc (t - pp + 1);
138 memcpy (charset, pp, t - pp);
139 charset[t-pp] = '\0';
142 if (toupper ((unsigned char) *pp) == 'Q')
143 enc = ENCQUOTEDPRINTABLE;
144 else if (toupper ((unsigned char) *pp) == 'B')
154 if (enc == ENCQUOTEDPRINTABLE)
156 for (; pp < pp1; pp++)
160 else if (*pp == '=' &&
161 (!(pp[1] & ~127) && hexval(pp[1]) != -1) &&
162 (!(pp[2] & ~127) && hexval(pp[2]) != -1))
164 *pd++ = (hexval(pp[1]) << 4) | hexval(pp[2]);
172 else if (enc == ENCBASE64)
176 for (; pp < pp1; pp++)
180 if ((*pp & ~127) || (c = base64val(*pp)) == -1)
185 *pd++ = b | (c >> k);
201 if (charset && system_charset)
202 convert_string (d0, strlen(d0), charset, system_charset, &d0, &dlen);
203 strfcpy (d, d0, len);
210 * Find the start and end of the first encoded word in the string.
211 * We use the grammar in section 2 of RFC 2047, but the "encoding"
212 * must be B or Q. Also, we don't require the encoded word to be
213 * separated by linear-white-space (section 5(1)).
215 static const char *find_encoded_word (const char *s, const char **x)
220 while ((p = strstr (q, "=?")))
223 0x20 < *q && *q < 0x7f && !strchr ("()<>@,;:\"/[]?.=", *q);
226 if (q[0] != '?' || !strchr ("BbQq", q[1]) || q[2] != '?')
228 for (q = q + 3; 0x20 < *q && *q < 0x7f && *q != '?'; q++)
230 if (q[0] != '?' || q[1] != '=')
243 /* return length of linear-white-space */
244 static size_t lwslen (const char *s, size_t n)
252 for (; p < s + n; p++)
253 if (!strchr (" \t\r\n", *p))
255 len = (size_t)(p - s);
258 if (strchr ("\r\n", *(p-1))) /* LWS doesn't end with CRLF */
263 /* return length of linear-white-space : reverse */
264 static size_t lwsrlen (const char *s, size_t n)
266 const char *p = s + n - 1;
272 if (strchr ("\r\n", *p)) /* LWS doesn't end with CRLF */
276 if (!strchr (" \t\r\n", *p))
278 len = (size_t)(s + n - 1 - p);
284 /* try to decode anything that looks like a valid RFC2047 encoded
285 * header field, ignoring RFC822 parsing rules
287 static void rfc2047_decode (char **pd)
291 int found_encoded = 0;
299 dlen = 4 * strlen (s); /* should be enough */
300 d = d0 = xmalloc (dlen + 1);
302 while (*s && dlen > 0)
304 if (!(p = find_encoded_word (s, &q)))
306 /* no encoded words */
307 if (option (OPTIGNORELWS))
310 if (found_encoded && (m = lwslen (s, n)) != 0)
313 *d = ' ', d++, dlen--;
317 strncpy (d, s, dlen);
324 n = (size_t) (p - s);
325 /* ignore spaces between encoded word
326 * and linear-white-space between encoded word and *text */
327 if (option (OPTIGNORELWS))
329 if (found_encoded && (m = lwslen (s, n)) != 0)
332 *d = ' ', d++, dlen--;
336 if ((m = n - lwsrlen (s, n)) != 0)
344 *d = ' ', d++, dlen--;
347 else if (!found_encoded || strspn (s, " \t\r\n") != n)
357 rfc2047_decode_word (d, p, dlen);
370 /* Initialize the whole machinery */
374 setlocale(LC_CTYPE, "");
375 system_charset = nl_langinfo(CODESET);
376 if (!system_charset[0])
377 system_charset = NULL;
378 debug("Charset is %s\n", system_charset);
382 do_add_snippet(char **ppos, char *term, unsigned char *add)
386 mbtowc(NULL, NULL, 0);
388 while (pos + MB_CUR_MAX < term)
391 int l = mbtowc(&c, add, MB_CUR_MAX);
418 add_snippet(char **ppos, char *term, char *add)
420 char *buf = xstrdup(add);
421 rfc2047_decode(&buf);
422 do_add_snippet(ppos, term, buf);