2 ** fmt_rfc2047.c -- decode RFC-2047 header format
4 ** This code is Copyright (c) 2002, by the authors of nmh. See the
5 ** COPYRIGHT file in the root directory of the nmh distribution for
6 ** complete copyright information.
16 static signed char hexindex[] = {
17 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
18 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
19 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
20 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,-1,-1,-1,-1,-1,-1,
21 -1,10,11,12,13,14,15,-1,-1,-1,-1,-1,-1,-1,-1,-1,
22 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
23 -1,10,11,12,13,14,15,-1,-1,-1,-1,-1,-1,-1,-1,-1,
24 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1
27 static signed char index_64[128] = {
28 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
29 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
30 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,62, -1,-1,-1,63,
31 52,53,54,55, 56,57,58,59, 60,61,-1,-1, -1,-1,-1,-1,
32 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10, 11,12,13,14,
33 15,16,17,18, 19,20,21,22, 23,24,25,-1, -1,-1,-1,-1,
34 -1,26,27,28, 29,30,31,32, 33,34,35,36, 37,38,39,40,
35 41,42,43,44, 45,46,47,48, 49,50,51,-1, -1,-1,-1,-1
38 #define char64(c) (((unsigned char) (c) > 127) ? -1 : index_64[(unsigned char) (c)])
41 unqp(unsigned char byte1, unsigned char byte2)
43 if (hexindex[byte1] == -1 || hexindex[byte2] == -1)
45 return (hexindex[byte1] << 4 | hexindex[byte2]);
48 /* Check if character is linear whitespace */
49 #define is_lws(c) ((c) == ' ' || (c) == '\t' || (c) == '\n')
53 ** Decode the string as a RFC-2047 header field
56 /* Add character to the destination buffer, and bomb out if it fills up */
57 #define ADDCHR(C) do { *q++ = (C); dstlen--; if (!dstlen) goto buffull; } while (0)
60 decode_rfc2047(char *str, char *dst, size_t dstlen)
63 char *startofmime, *endofmime;
64 int c, quoted_printable;
65 int encoding_found = 0; /* did we decode anything? */
66 int between_encodings = 0; /* are we between two encodings? */
67 int equals_pending = 0; /* is there a '=' pending? */
68 int whitespace = 0; /* how much whitespace between encodings? */
70 int use_iconv = 0; /* are we converting encoding with iconv? */
73 char *saveq, *convbuf = NULL;
81 ** Do a quick and dirty check for the '=' character.
82 ** This should quickly eliminate many cases.
84 if (!strchr(str, '='))
87 for (p = str, q = dst; *p; p++) {
97 ** If we had an '=' character pending from
98 ** last iteration, then add it first.
100 if (equals_pending) {
103 between_encodings = 0; /* we have added non-whitespace text */
107 /* count linear whitespace while between encodings */
108 if (between_encodings && is_lws(*p))
111 between_encodings = 0; /* we have added non-whitespace text */
116 equals_pending = 1; /* we have a '=' pending */
118 /* Check for initial =? */
119 if (*p == '=' && p[1] && p[1] == '?' && p[2]) {
122 /* Scan ahead for the next '?' character */
123 for (pp = startofmime; *pp && *pp != '?'; pp++)
129 /* Check if character set can be handled natively */
130 if (!check_charset(startofmime, pp - startofmime)) {
132 /* .. it can't. We'll use iconv then. */
134 cd = iconv_open(get_charset(), startofmime);
135 fromutf8 = !mh_strcasecmp(startofmime, "UTF-8");
137 if (cd == (iconv_t)-1) continue;
144 startofmime = pp + 1;
146 /* Check for valid encoding type */
147 if (*startofmime != 'B' && *startofmime != 'b' &&
148 *startofmime != 'Q' && *startofmime != 'q')
151 /* Is encoding quoted printable or base64? */
152 quoted_printable = (*startofmime == 'Q' || *startofmime == 'q');
155 /* Check for next '?' character */
156 if (*startofmime != '?')
161 ** Scan ahead for the ending ?=
163 ** While doing this, we will also check if encoded
164 ** word has any embedded linear whitespace.
167 for (pp = startofmime; *pp && *(pp+1); pp++) {
170 } else if (*pp == '?' && pp[1] == '=') {
175 if (is_lws(*pp) || endofmime == NULL)
179 ** We've found an encoded word, so we can drop
180 ** the '=' that was pending
185 ** If we are between two encoded words separated
186 ** only by linear whitespace, then we ignore
187 ** the whitespace. We will roll back the buffer
188 ** the number of whitespace characters we've seen
189 ** since last encoded word.
191 if (between_encodings) {
193 dstlen += whitespace;
198 ** empty encoded text. This ensures that we don't
199 ** malloc 0 bytes but skip on to the end
201 if (endofmime == startofmime && use_iconv) {
209 q = convbuf = (char *) mh_xmalloc(endofmime - startofmime);
212 ** ADDCHR2 is for adding characters when q is or might be convbuf:
213 ** in this case on buffer-full we want to run iconv before returning.
214 ** I apologise for the dreadful name.
216 # define ADDCHR2(C) do { *q++ = (C); dstlen--; if (!dstlen) goto iconvbuffull; } while (0)
218 # define ADDCHR2(C) ADDCHR(C)
221 /* Now decode the text */
222 if (quoted_printable) {
223 for (pp = startofmime; pp < endofmime; pp++) {
225 c = unqp(pp[1], pp[2]);
231 } else if (*pp == '_') {
240 c1 = c2 = c3 = c4 = -1;
243 while (pp < endofmime) {
245 while ((pp < endofmime) &&
246 ((c1 = char64(*pp)) == -1)) {
249 if (pp < endofmime) {
252 while ((pp < endofmime) &&
253 ((c2 = char64(*pp)) == -1)) {
256 if (pp < endofmime && c1 != -1 && c2 != -1) {
257 ADDCHR2((c1 << 2) | (c2 >> 4));
261 while ((pp < endofmime) &&
262 ((c3 = char64(*pp)) == -1)) {
265 if (pp < endofmime && c2 != -1 && c3 != -1) {
266 ADDCHR2(((c2 & 0xF) << 4) | (c3 >> 2));
270 while ((pp < endofmime) &&
271 ((c4 = char64(*pp)) == -1)) {
274 if (pp < endofmime && c3 != -1 && c4 != -1) {
275 ADDCHR2(((c3 & 0x3) << 6) | (c4));
284 ** NB that the string at convbuf is not necessarily
285 ** NUL terminated here:
286 ** q points to the first byte after the valid part.
288 /* Convert to native character set */
290 size_t inbytes = q - convbuf;
291 ICONV_CONST char *start = convbuf;
294 if (iconv(cd, &start, &inbytes, &saveq, &savedstlen) ==
299 ** character couldn't be
300 ** converted. we output a
301 ** `?' and try to carry on
302 ** which won't work if either
303 ** encoding was stateful
305 iconv(cd, 0, 0, &saveq, &savedstlen);
312 /* skip to next input character */
314 for (start++;(start < q) && ((*start & 192) == 128);start++)
324 ** Stop now if (1) we hit the end of the
325 ** buffer trying to do MIME decoding and
326 ** have just iconv-converted a partial
327 ** string or (2) our iconv-conversion hit
328 ** the end of the buffer.
330 if (!dstlen || !savedstlen)
338 ** Now that we are done decoding this particular
339 ** encoded word, advance string to trailing '='.
343 encoding_found = 1; /* we found (at least 1) encoded word */
344 between_encodings = 1; /* we have just decoded something */
345 whitespace = 0; /* re-initialize amount of whitespace */
349 if (use_iconv) iconv_close(cd);
352 /* If an equals was pending at end of string, add it now. */
357 return encoding_found;
361 ** q is currently just off the end of the buffer,
362 ** so rewind to NUL terminate
366 return encoding_found;