]> diplodocus.org Git - nmh/blob - sbr/fmt_rfc2047.c
fea731308631f5983a1bf07a01e49351bc0c16b0
[nmh] / sbr / fmt_rfc2047.c
1
2 /*
3 * fmt_rfc2047.c -- decode RFC-2047 header format
4 *
5 * This code is Copyright (c) 2002, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * complete copyright information.
8 */
9
10 #include <h/mh.h>
11 #include <h/utils.h>
12 #ifdef HAVE_ICONV
13 # include <iconv.h>
14 # include <errno.h>
15 #endif
16
17 static signed char hexindex[] = {
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 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
21 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,-1,-1,-1,-1,-1,-1,
22 -1,10,11,12,13,14,15,-1,-1,-1,-1,-1,-1,-1,-1,-1,
23 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
24 -1,10,11,12,13,14,15,-1,-1,-1,-1,-1,-1,-1,-1,-1,
25 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1
26 };
27
28 static signed char index_64[128] = {
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,-1, -1,-1,-1,-1,
31 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,62, -1,-1,-1,63,
32 52,53,54,55, 56,57,58,59, 60,61,-1,-1, -1,-1,-1,-1,
33 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10, 11,12,13,14,
34 15,16,17,18, 19,20,21,22, 23,24,25,-1, -1,-1,-1,-1,
35 -1,26,27,28, 29,30,31,32, 33,34,35,36, 37,38,39,40,
36 41,42,43,44, 45,46,47,48, 49,50,51,-1, -1,-1,-1,-1
37 };
38
39 #define char64(c) (((unsigned char) (c) > 127) ? -1 : index_64[(unsigned char) (c)])
40
41 static int
42 unqp (unsigned char byte1, unsigned char byte2)
43 {
44 if (hexindex[byte1] == -1 || hexindex[byte2] == -1)
45 return -1;
46 return (hexindex[byte1] << 4 | hexindex[byte2]);
47 }
48
49 /* Check if character is linear whitespace */
50 #define is_lws(c) ((c) == ' ' || (c) == '\t' || (c) == '\n')
51
52
53 /*
54 * Decode the string as a RFC-2047 header field
55 */
56
57 /* Add character to the destination buffer, and bomb out if it fills up */
58 #define ADDCHR(C) do { *q++ = (C); dstlen--; if (!dstlen) goto buffull; } while (0)
59
60 int
61 decode_rfc2047 (char *str, char *dst, size_t dstlen)
62 {
63 char *p, *q, *pp;
64 char *startofmime, *endofmime, *endofcharset;
65 int c, quoted_printable;
66 int encoding_found = 0; /* did we decode anything? */
67 int between_encodings = 0; /* are we between two encodings? */
68 int equals_pending = 0; /* is there a '=' pending? */
69 int whitespace = 0; /* how much whitespace between encodings? */
70 #ifdef HAVE_ICONV
71 int use_iconv = 0; /* are we converting encoding with iconv? */
72 iconv_t cd = NULL;
73 int fromutf8 = 0;
74 char *saveq, *convbuf = NULL;
75 size_t savedstlen;
76 #endif
77
78 if (!str)
79 return 0;
80
81 /*
82 * Do a quick and dirty check for the '=' character.
83 * This should quickly eliminate many cases.
84 */
85 if (!strchr (str, '='))
86 return 0;
87
88 for (p = str, q = dst; *p; p++) {
89
90 /* reset iconv */
91 #ifdef HAVE_ICONV
92 if (use_iconv) {
93 iconv_close(cd);
94 use_iconv = 0;
95 }
96 #endif
97 /*
98 * If we had an '=' character pending from
99 * last iteration, then add it first.
100 */
101 if (equals_pending) {
102 ADDCHR('=');
103 equals_pending = 0;
104 between_encodings = 0; /* we have added non-whitespace text */
105 }
106
107 if (*p != '=') {
108 /* count linear whitespace while between encodings */
109 if (between_encodings && is_lws(*p))
110 whitespace++;
111 else
112 between_encodings = 0; /* we have added non-whitespace text */
113 ADDCHR(*p);
114 continue;
115 }
116
117 equals_pending = 1; /* we have a '=' pending */
118
119 /* Check for initial =? */
120 if (*p == '=' && p[1] && p[1] == '?' && p[2]) {
121 startofmime = p + 2;
122
123 /* Scan ahead for the next '?' character */
124 for (pp = startofmime; *pp && *pp != '?'; pp++)
125 ;
126
127 if (!*pp)
128 continue;
129
130 /*
131 * RFC 2231 specifies that language information can appear
132 * in a charset specification like so:
133 *
134 * =?us-ascii*en?Q?Foo?=
135 *
136 * Right now we don't use language information, so ignore it.
137 */
138
139 for (endofcharset = startofmime;
140 *endofcharset != '*' && endofcharset < pp;
141 endofcharset++)
142 ;
143
144 /* Check if character set can be handled natively */
145 if (!check_charset(startofmime, endofcharset - startofmime)) {
146 #ifdef HAVE_ICONV
147 /* .. it can't. We'll use iconv then. */
148 *endofcharset = '\0';
149 cd = iconv_open(get_charset(), startofmime);
150 fromutf8 = !strcasecmp(startofmime, "UTF-8");
151 *pp = '?';
152 if (cd == (iconv_t)-1) continue;
153 use_iconv = 1;
154 #else
155 continue;
156 #endif
157 }
158
159 startofmime = pp + 1;
160
161 /* Check for valid encoding type */
162 if (*startofmime != 'B' && *startofmime != 'b' &&
163 *startofmime != 'Q' && *startofmime != 'q')
164 continue;
165
166 /* Is encoding quoted printable or base64? */
167 quoted_printable = (*startofmime == 'Q' || *startofmime == 'q');
168 startofmime++;
169
170 /* Check for next '?' character */
171 if (*startofmime != '?')
172 continue;
173 startofmime++;
174
175 /*
176 * Scan ahead for the ending ?=
177 *
178 * While doing this, we will also check if encoded
179 * word has any embedded linear whitespace.
180 */
181 endofmime = NULL;
182 for (pp = startofmime; *pp && *(pp+1); pp++) {
183 if (is_lws(*pp)) {
184 break;
185 } else if (*pp == '?' && pp[1] == '=') {
186 endofmime = pp;
187 break;
188 }
189 }
190 if (is_lws(*pp) || endofmime == NULL)
191 continue;
192
193 /*
194 * We've found an encoded word, so we can drop
195 * the '=' that was pending
196 */
197 equals_pending = 0;
198
199 /*
200 * If we are between two encoded words separated only by
201 * linear whitespace, then we ignore the whitespace.
202 * We will roll back the buffer the number of whitespace
203 * characters we've seen since last encoded word.
204 */
205 if (between_encodings) {
206 q -= whitespace;
207 dstlen += whitespace;
208 }
209
210 #ifdef HAVE_ICONV
211 /*
212 * empty encoded text. This ensures that we don't
213 * malloc 0 bytes but skip on to the end
214 */
215 if (endofmime == startofmime && use_iconv) {
216 use_iconv = 0;
217 iconv_close(cd);
218 }
219
220 if (use_iconv) {
221 saveq = q;
222 savedstlen = dstlen;
223 q = convbuf = (char *) mh_xmalloc(endofmime - startofmime);
224 }
225 /* ADDCHR2 is for adding characters when q is or might be convbuf:
226 * in this case on buffer-full we want to run iconv before returning.
227 * I apologise for the dreadful name.
228 */
229 #define ADDCHR2(C) do { *q++ = (C); dstlen--; if (!dstlen) goto iconvbuffull; } while (0)
230 #else
231 #define ADDCHR2(C) ADDCHR(C)
232 #endif
233
234 /* Now decode the text */
235 if (quoted_printable) {
236 for (pp = startofmime; pp < endofmime; pp++) {
237 if (*pp == '=') {
238 c = unqp (pp[1], pp[2]);
239 if (c == -1)
240 continue;
241 if (c != 0)
242 *q++ = c;
243 pp += 2;
244 } else if (*pp == '_') {
245 ADDCHR2(' ');
246 } else {
247 ADDCHR2(*pp);
248 }
249 }
250 } else {
251 /* base64 */
252 int c1, c2, c3, c4;
253 c1 = c2 = c3 = c4 = -1;
254
255 pp = startofmime;
256 while (pp < endofmime) {
257 /* 6 + 2 bits */
258 while ((pp < endofmime) &&
259 ((c1 = char64(*pp)) == -1)) {
260 pp++;
261 }
262 if (pp < endofmime) {
263 pp++;
264 }
265 while ((pp < endofmime) &&
266 ((c2 = char64(*pp)) == -1)) {
267 pp++;
268 }
269 if (pp < endofmime && c1 != -1 && c2 != -1) {
270 ADDCHR2((c1 << 2) | (c2 >> 4));
271 pp++;
272 }
273 /* 4 + 4 bits */
274 while ((pp < endofmime) &&
275 ((c3 = char64(*pp)) == -1)) {
276 pp++;
277 }
278 if (pp < endofmime && c2 != -1 && c3 != -1) {
279 ADDCHR2(((c2 & 0xF) << 4) | (c3 >> 2));
280 pp++;
281 }
282 /* 2 + 6 bits */
283 while ((pp < endofmime) &&
284 ((c4 = char64(*pp)) == -1)) {
285 pp++;
286 }
287 if (pp < endofmime && c3 != -1 && c4 != -1) {
288 ADDCHR2(((c3 & 0x3) << 6) | (c4));
289 pp++;
290 }
291 }
292 }
293
294 #ifdef HAVE_ICONV
295 iconvbuffull:
296 /* NB that the string at convbuf is not necessarily NUL terminated here:
297 * q points to the first byte after the valid part.
298 */
299 /* Convert to native character set */
300 if (use_iconv) {
301 size_t inbytes = q - convbuf;
302 ICONV_CONST char *start = convbuf;
303
304 while (inbytes) {
305 if (iconv(cd, &start, &inbytes, &saveq, &savedstlen) ==
306 (size_t)-1) {
307 if (errno != EILSEQ) break;
308 /* character couldn't be converted. we output a `?'
309 * and try to carry on which won't work if
310 * either encoding was stateful */
311 iconv (cd, 0, 0, &saveq, &savedstlen);
312 if (!savedstlen)
313 break;
314 *saveq++ = '?';
315 savedstlen--;
316 if (!savedstlen)
317 break;
318 /* skip to next input character */
319 if (fromutf8) {
320 for (++start, --inbytes;
321 start < q && (*start & 192) == 128;
322 ++start, --inbytes)
323 continue;
324 } else
325 start++, inbytes--;
326 if (start >= q)
327 break;
328 }
329 }
330 q = saveq;
331 /* Stop now if (1) we hit the end of the buffer trying to do
332 * MIME decoding and have just iconv-converted a partial string
333 * or (2) our iconv-conversion hit the end of the buffer.
334 */
335 if (!dstlen || !savedstlen)
336 goto buffull;
337 dstlen = savedstlen;
338 free(convbuf);
339 }
340 #endif
341
342 /*
343 * Now that we are done decoding this particular
344 * encoded word, advance string to trailing '='.
345 */
346 p = endofmime + 1;
347
348 encoding_found = 1; /* we found (at least 1) encoded word */
349 between_encodings = 1; /* we have just decoded something */
350 whitespace = 0; /* re-initialize amount of whitespace */
351 }
352 }
353 #ifdef HAVE_ICONV
354 if (use_iconv) iconv_close(cd);
355 #endif
356
357 /* If an equals was pending at end of string, add it now. */
358 if (equals_pending)
359 ADDCHR('=');
360 *q = '\0';
361
362 return encoding_found;
363
364 buffull:
365 /* q is currently just off the end of the buffer, so rewind to NUL terminate */
366 q--;
367 *q = '\0';
368 return encoding_found;
369 }