]>
diplodocus.org Git - nmh/blob - sbr/fmt_rfc2047.c
3 * fmt_rfc2047.c -- decode RFC-2047 header format
10 static signed char hexindex
[] = {
11 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
12 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
13 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
14 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,-1,-1,-1,-1,-1,-1,
15 -1,10,11,12,13,14,15,-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,10,11,12,13,14,15,-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
21 static signed char index_64
[128] = {
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,62, -1,-1,-1,63,
25 52,53,54,55, 56,57,58,59, 60,61,-1,-1, -1,-1,-1,-1,
26 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10, 11,12,13,14,
27 15,16,17,18, 19,20,21,22, 23,24,25,-1, -1,-1,-1,-1,
28 -1,26,27,28, 29,30,31,32, 33,34,35,36, 37,38,39,40,
29 41,42,43,44, 45,46,47,48, 49,50,51,-1, -1,-1,-1,-1
32 #define char64(c) (((unsigned char) (c) > 127) ? -1 : index_64[(unsigned char) (c)])
35 unqp (unsigned char byte1
, unsigned char byte2
)
37 if (hexindex
[byte1
] == -1 || hexindex
[byte2
] == -1)
39 return (hexindex
[byte1
] << 4 | hexindex
[byte2
]);
42 /* Check if character is linear whitespace */
43 #define is_lws(c) ((c) == ' ' || (c) == '\t' || (c) == '\n')
47 * Decode the string as a RFC-2047 header field
51 decode_rfc2047 (char *str
, char *dst
)
54 char *startofmime
, *endofmime
;
55 int c
, quoted_printable
;
56 int encoding_found
= 0; /* did we decode anything? */
57 int between_encodings
= 0; /* are we between two encodings? */
58 int equals_pending
= 0; /* is there a '=' pending? */
59 int whitespace
= 0; /* how much whitespace between encodings? */
65 * Do a quick and dirty check for the '=' character.
66 * This should quickly eliminate many cases.
68 if (!strchr (str
, '='))
71 for (p
= str
, q
= dst
; *p
; p
++) {
73 * If we had an '=' character pending from
74 * last iteration, then add it first.
79 between_encodings
= 0; /* we have added non-whitespace text */
83 /* count linear whitespace while between encodings */
84 if (between_encodings
&& is_lws(*p
))
87 between_encodings
= 0; /* we have added non-whitespace text */
92 equals_pending
= 1; /* we have a '=' pending */
94 /* Check for initial =? */
95 if (*p
== '=' && p
[1] && p
[1] == '?' && p
[2]) {
98 /* Scan ahead for the next '?' character */
99 for (pp
= startofmime
; *pp
&& *pp
!= '?'; pp
++)
105 /* Check if character set is OK */
106 if (!check_charset(startofmime
, pp
- startofmime
))
109 startofmime
= pp
+ 1;
111 /* Check for valid encoding type */
112 if (*startofmime
!= 'B' && *startofmime
!= 'b' &&
113 *startofmime
!= 'Q' && *startofmime
!= 'q')
116 /* Is encoding quoted printable or base64? */
117 quoted_printable
= (*startofmime
== 'Q' || *startofmime
== 'q');
120 /* Check for next '?' character */
121 if (*startofmime
!= '?')
126 * Scan ahead for the ending ?=
128 * While doing this, we will also check if encoded
129 * word has any embedded linear whitespace.
132 for (pp
= startofmime
; *pp
&& *(pp
+1); pp
++) {
135 } else if (*pp
== '?' && pp
[1] == '=') {
140 if (is_lws(*pp
) || endofmime
== NULL
)
144 * We've found an encoded word, so we can drop
145 * the '=' that was pending
150 * If we are between two encoded words separated only by
151 * linear whitespace, then we ignore the whitespace.
152 * We will roll back the buffer the number of whitespace
153 * characters we've seen since last encoded word.
155 if (between_encodings
)
158 /* Now decode the text */
159 if (quoted_printable
) {
160 for (pp
= startofmime
; pp
< endofmime
; pp
++) {
162 c
= unqp (pp
[1], pp
[2]);
168 } else if (*pp
== '_') {
179 while (pp
< endofmime
) {
181 while ((pp
< endofmime
) &&
182 ((c1
= char64(*pp
)) == -1)) {
185 if (pp
< endofmime
) {
188 while ((pp
< endofmime
) &&
189 ((c2
= char64(*pp
)) == -1)) {
192 if (pp
< endofmime
&& c1
!= -1 && c2
!= -1) {
193 *q
++ = (c1
<< 2) | (c2
>> 4);
197 while ((pp
< endofmime
) &&
198 ((c3
= char64(*pp
)) == -1)) {
201 if (pp
< endofmime
&& c2
!= -1 && c3
!= -1) {
202 *q
++ = ((c2
& 0xF) << 4) | (c3
>> 2);
206 while ((pp
< endofmime
) &&
207 ((c4
= char64(*pp
)) == -1)) {
210 if (pp
< endofmime
&& c3
!= -1 && c4
!= -1) {
211 *q
++ = ((c3
& 0x3) << 6) | (c4
);
218 * Now that we are done decoding this particular
219 * encoded word, advance string to trailing '='.
223 encoding_found
= 1; /* we found (at least 1) encoded word */
224 between_encodings
= 1; /* we have just decoded something */
225 whitespace
= 0; /* re-initialize amount of whitespace */
229 /* If an equals was pending at end of string, add it now. */
234 return encoding_found
;