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