]>
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.
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
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
39 #define char64(c) (((unsigned char) (c) > 127) ? -1 : index_64[(unsigned char) (c)])
42 unqp (unsigned char byte1
, unsigned char byte2
)
44 if (hexindex
[byte1
] == -1 || hexindex
[byte2
] == -1)
46 return (hexindex
[byte1
] << 4 | hexindex
[byte2
]);
49 /* Check if character is linear whitespace */
50 #define is_lws(c) ((c) == ' ' || (c) == '\t' || (c) == '\n')
54 * Decode the string as a RFC-2047 header field
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)
61 decode_rfc2047 (char *str
, char *dst
, size_t dstlen
)
64 char *startofmime
, *endofmime
;
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? */
71 int use_iconv
= 0; /* are we converting encoding with iconv? */
74 char *saveq
, *convbuf
= NULL
;
82 * Do a quick and dirty check for the '=' character.
83 * This should quickly eliminate many cases.
85 if (!strchr (str
, '='))
88 for (p
= str
, q
= dst
; *p
; p
++) {
98 * If we had an '=' character pending from
99 * last iteration, then add it first.
101 if (equals_pending
) {
104 between_encodings
= 0; /* we have added non-whitespace text */
108 /* count linear whitespace while between encodings */
109 if (between_encodings
&& is_lws(*p
))
112 between_encodings
= 0; /* we have added non-whitespace text */
117 equals_pending
= 1; /* we have a '=' pending */
119 /* Check for initial =? */
120 if (*p
== '=' && p
[1] && p
[1] == '?' && p
[2]) {
123 /* Scan ahead for the next '?' character */
124 for (pp
= startofmime
; *pp
&& *pp
!= '?'; pp
++)
130 /* Check if character set can be handled natively */
131 if (!check_charset(startofmime
, pp
- startofmime
)) {
133 /* .. it can't. We'll use iconv then. */
135 cd
= iconv_open(get_charset(), startofmime
);
136 fromutf8
= !mh_strcasecmp(startofmime
, "UTF-8");
138 if (cd
== (iconv_t
)-1) continue;
145 startofmime
= pp
+ 1;
147 /* Check for valid encoding type */
148 if (*startofmime
!= 'B' && *startofmime
!= 'b' &&
149 *startofmime
!= 'Q' && *startofmime
!= 'q')
152 /* Is encoding quoted printable or base64? */
153 quoted_printable
= (*startofmime
== 'Q' || *startofmime
== 'q');
156 /* Check for next '?' character */
157 if (*startofmime
!= '?')
162 * Scan ahead for the ending ?=
164 * While doing this, we will also check if encoded
165 * word has any embedded linear whitespace.
168 for (pp
= startofmime
; *pp
&& *(pp
+1); pp
++) {
171 } else if (*pp
== '?' && pp
[1] == '=') {
176 if (is_lws(*pp
) || endofmime
== NULL
)
180 * We've found an encoded word, so we can drop
181 * the '=' that was pending
186 * If we are between two encoded words separated only by
187 * linear whitespace, then we ignore the whitespace.
188 * We will roll back the buffer the number of whitespace
189 * characters we've seen since last encoded word.
191 if (between_encodings
) {
193 dstlen
+= whitespace
;
198 * empty encoded text. This ensures that we don't
199 * malloc 0 bytes but skip on to the end
201 if (endofmime
== startofmime
&& use_iconv
) {
209 q
= convbuf
= (char *) mh_xmalloc(endofmime
- startofmime
);
211 /* ADDCHR2 is for adding characters when q is or might be convbuf:
212 * in this case on buffer-full we want to run iconv before returning.
213 * I apologise for the dreadful name.
215 #define ADDCHR2(C) do { *q++ = (C); dstlen--; if (!dstlen) goto iconvbuffull; } while (0)
217 #define ADDCHR2(C) ADDCHR(C)
220 /* Now decode the text */
221 if (quoted_printable
) {
222 for (pp
= startofmime
; pp
< endofmime
; pp
++) {
224 c
= unqp (pp
[1], pp
[2]);
230 } else if (*pp
== '_') {
239 c1
= c2
= c3
= c4
= -1;
242 while (pp
< endofmime
) {
244 while ((pp
< endofmime
) &&
245 ((c1
= char64(*pp
)) == -1)) {
248 if (pp
< endofmime
) {
251 while ((pp
< endofmime
) &&
252 ((c2
= char64(*pp
)) == -1)) {
255 if (pp
< endofmime
&& c1
!= -1 && c2
!= -1) {
256 ADDCHR2((c1
<< 2) | (c2
>> 4));
260 while ((pp
< endofmime
) &&
261 ((c3
= char64(*pp
)) == -1)) {
264 if (pp
< endofmime
&& c2
!= -1 && c3
!= -1) {
265 ADDCHR2(((c2
& 0xF) << 4) | (c3
>> 2));
269 while ((pp
< endofmime
) &&
270 ((c4
= char64(*pp
)) == -1)) {
273 if (pp
< endofmime
&& c3
!= -1 && c4
!= -1) {
274 ADDCHR2(((c3
& 0x3) << 6) | (c4
));
282 /* NB that the string at convbuf is not necessarily NUL terminated here:
283 * q points to the first byte after the valid part.
285 /* Convert to native character set */
287 size_t inbytes
= q
- convbuf
;
288 ICONV_CONST
char *start
= convbuf
;
291 if (iconv(cd
, &start
, &inbytes
, &saveq
, &savedstlen
) ==
293 if (errno
!= EILSEQ
) break;
294 /* character couldn't be converted. we output a `?'
295 * and try to carry on which won't work if
296 * either encoding was stateful */
297 iconv (cd
, 0, 0, &saveq
, &savedstlen
);
304 /* skip to next input character */
306 for (start
++;(start
< q
) && ((*start
& 192) == 128);start
++)
315 /* Stop now if (1) we hit the end of the buffer trying to do
316 * MIME decoding and have just iconv-converted a partial string
317 * or (2) our iconv-conversion hit the end of the buffer.
319 if (!dstlen
|| !savedstlen
)
327 * Now that we are done decoding this particular
328 * encoded word, advance string to trailing '='.
332 encoding_found
= 1; /* we found (at least 1) encoded word */
333 between_encodings
= 1; /* we have just decoded something */
334 whitespace
= 0; /* re-initialize amount of whitespace */
338 if (use_iconv
) iconv_close(cd
);
341 /* If an equals was pending at end of string, add it now. */
346 return encoding_found
;
349 /* q is currently just off the end of the buffer, so rewind to NUL terminate */
352 return encoding_found
;