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