]>
diplodocus.org Git - nmh/blob - sbr/fmt_rfc2047.c
3 * fmt_rfc2047.c -- decode RFC-2047 header format
7 * This code is Copyright (c) 2002, by the authors of nmh. See the
8 * COPYRIGHT file in the root directory of the nmh distribution for
9 * complete copyright information.
18 static signed char hexindex
[] = {
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 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
22 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,-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,10,11,12,13,14,15,-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
29 static signed char index_64
[128] = {
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,62, -1,-1,-1,63,
33 52,53,54,55, 56,57,58,59, 60,61,-1,-1, -1,-1,-1,-1,
34 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10, 11,12,13,14,
35 15,16,17,18, 19,20,21,22, 23,24,25,-1, -1,-1,-1,-1,
36 -1,26,27,28, 29,30,31,32, 33,34,35,36, 37,38,39,40,
37 41,42,43,44, 45,46,47,48, 49,50,51,-1, -1,-1,-1,-1
40 #define char64(c) (((unsigned char) (c) > 127) ? -1 : index_64[(unsigned char) (c)])
43 unqp (unsigned char byte1
, unsigned char byte2
)
45 if (hexindex
[byte1
] == -1 || hexindex
[byte2
] == -1)
47 return (hexindex
[byte1
] << 4 | hexindex
[byte2
]);
50 /* Check if character is linear whitespace */
51 #define is_lws(c) ((c) == ' ' || (c) == '\t' || (c) == '\n')
55 * Decode the string as a RFC-2047 header field
59 decode_rfc2047 (char *str
, char *dst
)
62 char *startofmime
, *endofmime
;
63 int c
, quoted_printable
;
64 int encoding_found
= 0; /* did we decode anything? */
65 int between_encodings
= 0; /* are we between two encodings? */
66 int equals_pending
= 0; /* is there a '=' pending? */
67 int whitespace
= 0; /* how much whitespace between encodings? */
69 int use_iconv
= 0; /* are we converting encoding with iconv? */
72 char *saveq
, *convbuf
;
79 * Do a quick and dirty check for the '=' character.
80 * This should quickly eliminate many cases.
82 if (!strchr (str
, '='))
85 for (p
= str
, q
= dst
; *p
; p
++) {
95 * If we had an '=' character pending from
96 * last iteration, then add it first.
101 between_encodings
= 0; /* we have added non-whitespace text */
105 /* count linear whitespace while between encodings */
106 if (between_encodings
&& is_lws(*p
))
109 between_encodings
= 0; /* we have added non-whitespace text */
114 equals_pending
= 1; /* we have a '=' pending */
116 /* Check for initial =? */
117 if (*p
== '=' && p
[1] && p
[1] == '?' && p
[2]) {
120 /* Scan ahead for the next '?' character */
121 for (pp
= startofmime
; *pp
&& *pp
!= '?'; pp
++)
127 /* Check if character set can be handled natively */
128 if (!check_charset(startofmime
, pp
- startofmime
)) {
130 /* .. it can't. We'll use iconv then. */
132 cd
= iconv_open(get_charset(), startofmime
);
133 fromutf8
= !strcasecmp(startofmime
, "UTF-8");
135 if (cd
== (iconv_t
)-1) continue;
142 startofmime
= pp
+ 1;
144 /* Check for valid encoding type */
145 if (*startofmime
!= 'B' && *startofmime
!= 'b' &&
146 *startofmime
!= 'Q' && *startofmime
!= 'q')
149 /* Is encoding quoted printable or base64? */
150 quoted_printable
= (*startofmime
== 'Q' || *startofmime
== 'q');
153 /* Check for next '?' character */
154 if (*startofmime
!= '?')
159 * Scan ahead for the ending ?=
161 * While doing this, we will also check if encoded
162 * word has any embedded linear whitespace.
165 for (pp
= startofmime
; *pp
&& *(pp
+1); pp
++) {
168 } else if (*pp
== '?' && pp
[1] == '=') {
173 if (is_lws(*pp
) || endofmime
== NULL
)
177 * We've found an encoded word, so we can drop
178 * the '=' that was pending
183 * If we are between two encoded words separated only by
184 * linear whitespace, then we ignore the whitespace.
185 * We will roll back the buffer the number of whitespace
186 * characters we've seen since last encoded word.
188 if (between_encodings
)
194 if (!(q
= convbuf
= (char *)malloc(endofmime
- startofmime
)))
199 /* Now decode the text */
200 if (quoted_printable
) {
201 for (pp
= startofmime
; pp
< endofmime
; pp
++) {
203 c
= unqp (pp
[1], pp
[2]);
209 } else if (*pp
== '_') {
220 while (pp
< endofmime
) {
222 while ((pp
< endofmime
) &&
223 ((c1
= char64(*pp
)) == -1)) {
226 if (pp
< endofmime
) {
229 while ((pp
< endofmime
) &&
230 ((c2
= char64(*pp
)) == -1)) {
233 if (pp
< endofmime
&& c1
!= -1 && c2
!= -1) {
234 *q
++ = (c1
<< 2) | (c2
>> 4);
238 while ((pp
< endofmime
) &&
239 ((c3
= char64(*pp
)) == -1)) {
242 if (pp
< endofmime
&& c2
!= -1 && c3
!= -1) {
243 *q
++ = ((c2
& 0xF) << 4) | (c3
>> 2);
247 while ((pp
< endofmime
) &&
248 ((c4
= char64(*pp
)) == -1)) {
251 if (pp
< endofmime
&& c3
!= -1 && c4
!= -1) {
252 *q
++ = ((c3
& 0x3) << 6) | (c4
);
259 /* Convert to native character set */
261 size_t inbytes
= q
- convbuf
;
262 size_t outbytes
= BUFSIZ
;
263 ICONV_CONST
char *start
= convbuf
;
266 if (iconv(cd
, &start
, &inbytes
, &saveq
, &outbytes
) ==
268 if (errno
!= EILSEQ
) break;
269 /* character couldn't be converted. we output a `?'
270 * and try to carry on which won't work if
271 * either encoding was stateful */
272 iconv (cd
, 0, 0, &saveq
, &outbytes
);
274 /* skip to next input character */
276 for (start
++;(*start
& 192) == 128;start
++)
288 * Now that we are done decoding this particular
289 * encoded word, advance string to trailing '='.
293 encoding_found
= 1; /* we found (at least 1) encoded word */
294 between_encodings
= 1; /* we have just decoded something */
295 whitespace
= 0; /* re-initialize amount of whitespace */
299 if (use_iconv
) iconv_close(cd
);
302 /* If an equals was pending at end of string, add it now. */
307 return encoding_found
;