3 * fmt_rfc2047.c -- decode RFC-2047 header format
7 * This code is Copyright (c) 2002, by the authors of nmh. See the
8 * COPYRIGHT file in the root directory of the nmh distribution for
9 * complete copyright information.
14 static signed char hexindex[] = {
15 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
16 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
17 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
18 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,-1,-1,-1,-1,-1,-1,
19 -1,10,11,12,13,14,15,-1,-1,-1,-1,-1,-1,-1,-1,-1,
20 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-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
25 static signed char index_64[128] = {
26 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
27 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
28 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,62, -1,-1,-1,63,
29 52,53,54,55, 56,57,58,59, 60,61,-1,-1, -1,-1,-1,-1,
30 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10, 11,12,13,14,
31 15,16,17,18, 19,20,21,22, 23,24,25,-1, -1,-1,-1,-1,
32 -1,26,27,28, 29,30,31,32, 33,34,35,36, 37,38,39,40,
33 41,42,43,44, 45,46,47,48, 49,50,51,-1, -1,-1,-1,-1
36 #define char64(c) (((unsigned char) (c) > 127) ? -1 : index_64[(unsigned char) (c)])
39 unqp (unsigned char byte1, unsigned char byte2)
41 if (hexindex[byte1] == -1 || hexindex[byte2] == -1)
43 return (hexindex[byte1] << 4 | hexindex[byte2]);
46 /* Check if character is linear whitespace */
47 #define is_lws(c) ((c) == ' ' || (c) == '\t' || (c) == '\n')
51 * Decode the string as a RFC-2047 header field
55 decode_rfc2047 (char *str, char *dst)
58 char *startofmime, *endofmime;
59 int c, quoted_printable;
60 int encoding_found = 0; /* did we decode anything? */
61 int between_encodings = 0; /* are we between two encodings? */
62 int equals_pending = 0; /* is there a '=' pending? */
63 int whitespace = 0; /* how much whitespace between encodings? */
69 * Do a quick and dirty check for the '=' character.
70 * This should quickly eliminate many cases.
72 if (!strchr (str, '='))
75 for (p = str, q = dst; *p; p++) {
77 * If we had an '=' character pending from
78 * last iteration, then add it first.
83 between_encodings = 0; /* we have added non-whitespace text */
87 /* count linear whitespace while between encodings */
88 if (between_encodings && is_lws(*p))
91 between_encodings = 0; /* we have added non-whitespace text */
96 equals_pending = 1; /* we have a '=' pending */
98 /* Check for initial =? */
99 if (*p == '=' && p[1] && p[1] == '?' && p[2]) {
102 /* Scan ahead for the next '?' character */
103 for (pp = startofmime; *pp && *pp != '?'; pp++)
109 /* Check if character set is OK */
110 if (!check_charset(startofmime, pp - startofmime))
113 startofmime = pp + 1;
115 /* Check for valid encoding type */
116 if (*startofmime != 'B' && *startofmime != 'b' &&
117 *startofmime != 'Q' && *startofmime != 'q')
120 /* Is encoding quoted printable or base64? */
121 quoted_printable = (*startofmime == 'Q' || *startofmime == 'q');
124 /* Check for next '?' character */
125 if (*startofmime != '?')
130 * Scan ahead for the ending ?=
132 * While doing this, we will also check if encoded
133 * word has any embedded linear whitespace.
136 for (pp = startofmime; *pp && *(pp+1); pp++) {
139 } else if (*pp == '?' && pp[1] == '=') {
144 if (is_lws(*pp) || endofmime == NULL)
148 * We've found an encoded word, so we can drop
149 * the '=' that was pending
154 * If we are between two encoded words separated only by
155 * linear whitespace, then we ignore the whitespace.
156 * We will roll back the buffer the number of whitespace
157 * characters we've seen since last encoded word.
159 if (between_encodings)
162 /* Now decode the text */
163 if (quoted_printable) {
164 for (pp = startofmime; pp < endofmime; pp++) {
166 c = unqp (pp[1], pp[2]);
172 } else if (*pp == '_') {
183 while (pp < endofmime) {
185 while ((pp < endofmime) &&
186 ((c1 = char64(*pp)) == -1)) {
189 if (pp < endofmime) {
192 while ((pp < endofmime) &&
193 ((c2 = char64(*pp)) == -1)) {
196 if (pp < endofmime && c1 != -1 && c2 != -1) {
197 *q++ = (c1 << 2) | (c2 >> 4);
201 while ((pp < endofmime) &&
202 ((c3 = char64(*pp)) == -1)) {
205 if (pp < endofmime && c2 != -1 && c3 != -1) {
206 *q++ = ((c2 & 0xF) << 4) | (c3 >> 2);
210 while ((pp < endofmime) &&
211 ((c4 = char64(*pp)) == -1)) {
214 if (pp < endofmime && c3 != -1 && c4 != -1) {
215 *q++ = ((c3 & 0x3) << 6) | (c4);
222 * Now that we are done decoding this particular
223 * encoded word, advance string to trailing '='.
227 encoding_found = 1; /* we found (at least 1) encoded word */
228 between_encodings = 1; /* we have just decoded something */
229 whitespace = 0; /* re-initialize amount of whitespace */
233 /* If an equals was pending at end of string, add it now. */
238 return encoding_found;