]>
diplodocus.org Git - nmh/blob - sbr/fmt_rfc2047.c
3 * fmt_rfc2047.c -- decode RFC-2047 header format
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.
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,
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
35 static 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
46 #define char64(c) (((unsigned char) (c) > 127) ? -1 : index_64[(unsigned char) (c)])
49 * Decode two quoted-pair characters
53 decode_qp (unsigned char byte1
, unsigned char byte2
)
55 if (hexindex
[byte1
] == -1 || hexindex
[byte2
] == -1)
57 return (hexindex
[byte1
] << 4 | hexindex
[byte2
]);
60 /* Check if character is linear whitespace */
61 #define is_lws(c) ((c) == ' ' || (c) == '\t' || (c) == '\n')
65 * Decode the string as a RFC-2047 header field
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)
72 decode_rfc2047 (char *str
, char *dst
, size_t dstlen
)
75 char *startofmime
, *endofmime
, *endofcharset
;
76 int c
, quoted_printable
;
77 int encoding_found
= 0; /* did we decode anything? */
78 int between_encodings
= 0; /* are we between two encodings? */
79 int equals_pending
= 0; /* is there a '=' pending? */
80 int whitespace
= 0; /* how much whitespace between encodings? */
82 int use_iconv
= 0; /* are we converting encoding with iconv? */
85 char *saveq
, *convbuf
= NULL
;
93 * Do a quick and dirty check for the '=' character.
94 * This should quickly eliminate many cases.
96 if (!strchr (str
, '='))
99 for (p
= str
, q
= dst
; *p
; p
++) {
109 * If we had an '=' character pending from
110 * last iteration, then add it first.
112 if (equals_pending
) {
115 between_encodings
= 0; /* we have added non-whitespace text */
119 /* count linear whitespace while between encodings */
120 if (between_encodings
&& is_lws(*p
))
123 between_encodings
= 0; /* we have added non-whitespace text */
128 equals_pending
= 1; /* we have a '=' pending */
130 /* Check for initial =? */
131 if (*p
== '=' && p
[1] && p
[1] == '?' && p
[2]) {
134 /* Scan ahead for the next '?' character */
135 for (pp
= startofmime
; *pp
&& *pp
!= '?'; pp
++)
142 * RFC 2231 specifies that language information can appear
143 * in a charset specification like so:
145 * =?us-ascii*en?Q?Foo?=
147 * Right now we don't use language information, so ignore it.
150 for (endofcharset
= startofmime
;
151 *endofcharset
!= '*' && endofcharset
< pp
;
155 /* Check if character set can be handled natively */
156 if (!check_charset(startofmime
, endofcharset
- startofmime
)) {
158 /* .. it can't. We'll use iconv then. */
159 *endofcharset
= '\0';
160 cd
= iconv_open(get_charset(), startofmime
);
161 fromutf8
= !strcasecmp(startofmime
, "UTF-8");
163 if (cd
== (iconv_t
)-1) continue;
170 startofmime
= pp
+ 1;
172 /* Check for valid encoding type */
173 if (*startofmime
!= 'B' && *startofmime
!= 'b' &&
174 *startofmime
!= 'Q' && *startofmime
!= 'q')
177 /* Is encoding quoted printable or base64? */
178 quoted_printable
= (*startofmime
== 'Q' || *startofmime
== 'q');
181 /* Check for next '?' character */
182 if (*startofmime
!= '?')
187 * Scan ahead for the ending ?=
189 * While doing this, we will also check if encoded
190 * word has any embedded linear whitespace.
193 for (pp
= startofmime
; *pp
&& *(pp
+1); pp
++) {
196 } else if (*pp
== '?' && pp
[1] == '=') {
201 if (is_lws(*pp
) || endofmime
== NULL
)
205 * We've found an encoded word, so we can drop
206 * the '=' that was pending
211 * If we are between two encoded words separated only by
212 * linear whitespace, then we ignore the whitespace.
213 * We will roll back the buffer the number of whitespace
214 * characters we've seen since last encoded word.
216 if (between_encodings
) {
218 dstlen
+= whitespace
;
223 * empty encoded text. This ensures that we don't
224 * malloc 0 bytes but skip on to the end
226 if (endofmime
== startofmime
&& use_iconv
) {
234 q
= convbuf
= (char *) mh_xmalloc(endofmime
- startofmime
);
236 /* ADDCHR2 is for adding characters when q is or might be convbuf:
237 * in this case on buffer-full we want to run iconv before returning.
238 * I apologise for the dreadful name.
240 #define ADDCHR2(C) do { *q++ = (C); dstlen--; if (!dstlen) goto iconvbuffull; } while (0)
242 #define ADDCHR2(C) ADDCHR(C)
245 /* Now decode the text */
246 if (quoted_printable
) {
247 for (pp
= startofmime
; pp
< endofmime
; pp
++) {
249 c
= decode_qp (pp
[1], pp
[2]);
255 } else if (*pp
== '_') {
264 c1
= c2
= c3
= c4
= -1;
267 while (pp
< endofmime
) {
269 while ((pp
< endofmime
) &&
270 ((c1
= char64(*pp
)) == -1)) {
273 if (pp
< endofmime
) {
276 while ((pp
< endofmime
) &&
277 ((c2
= char64(*pp
)) == -1)) {
280 if (pp
< endofmime
&& c1
!= -1 && c2
!= -1) {
281 ADDCHR2((c1
<< 2) | (c2
>> 4));
285 while ((pp
< endofmime
) &&
286 ((c3
= char64(*pp
)) == -1)) {
289 if (pp
< endofmime
&& c2
!= -1 && c3
!= -1) {
290 ADDCHR2(((c2
& 0xF) << 4) | (c3
>> 2));
294 while ((pp
< endofmime
) &&
295 ((c4
= char64(*pp
)) == -1)) {
298 if (pp
< endofmime
&& c3
!= -1 && c4
!= -1) {
299 ADDCHR2(((c3
& 0x3) << 6) | (c4
));
307 /* NB that the string at convbuf is not necessarily NUL terminated here:
308 * q points to the first byte after the valid part.
310 /* Convert to native character set */
312 size_t inbytes
= q
- convbuf
;
313 ICONV_CONST
char *start
= convbuf
;
316 if (iconv(cd
, &start
, &inbytes
, &saveq
, &savedstlen
) ==
318 if (errno
!= EILSEQ
) break;
319 /* character couldn't be converted. we output a `?'
320 * and try to carry on which won't work if
321 * either encoding was stateful */
322 iconv (cd
, 0, 0, &saveq
, &savedstlen
);
329 /* skip to next input character */
331 for (++start
, --inbytes
;
332 start
< q
&& (*start
& 192) == 128;
342 /* Stop now if (1) we hit the end of the buffer trying to do
343 * MIME decoding and have just iconv-converted a partial string
344 * or (2) our iconv-conversion hit the end of the buffer.
346 if (!dstlen
|| !savedstlen
)
354 * Now that we are done decoding this particular
355 * encoded word, advance string to trailing '='.
359 encoding_found
= 1; /* we found (at least 1) encoded word */
360 between_encodings
= 1; /* we have just decoded something */
361 whitespace
= 0; /* re-initialize amount of whitespace */
365 if (use_iconv
) iconv_close(cd
);
368 /* If an equals was pending at end of string, add it now. */
373 return encoding_found
;
376 /* q is currently just off the end of the buffer, so rewind to NUL terminate */
379 return encoding_found
;