b4bf4eb893f194252c8753513d7a856d39f39fb6
[mmh] / sbr / fmt_rfc2047.c
1
2 /*
3  * fmt_rfc2047.c -- decode RFC-2047 header format 
4  *
5  * $Id$
6  *
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.
10  */
11
12 #include <h/mh.h>
13
14 static signed char hexindex[] = {
15     -1,-1,-1,-1,-1,-1,-1,-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,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
18      0, 1, 2, 3, 4, 5, 6, 7, 8, 9,-1,-1,-1,-1,-1,-1,
19     -1,10,11,12,13,14,15,-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,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 };
24
25 static signed char index_64[128] = {
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,62, -1,-1,-1,63,
29     52,53,54,55, 56,57,58,59, 60,61,-1,-1, -1,-1,-1,-1,
30     -1, 0, 1, 2,  3, 4, 5, 6,  7, 8, 9,10, 11,12,13,14,
31     15,16,17,18, 19,20,21,22, 23,24,25,-1, -1,-1,-1,-1,
32     -1,26,27,28, 29,30,31,32, 33,34,35,36, 37,38,39,40,
33     41,42,43,44, 45,46,47,48, 49,50,51,-1, -1,-1,-1,-1
34 };
35
36 #define char64(c) (((unsigned char) (c) > 127) ? -1 : index_64[(unsigned char) (c)])
37
38 static int
39 unqp (unsigned char byte1, unsigned char byte2)
40 {
41     if (hexindex[byte1] == -1 || hexindex[byte2] == -1)
42         return -1;
43     return (hexindex[byte1] << 4 | hexindex[byte2]);
44 }
45
46 /* Check if character is linear whitespace */
47 #define is_lws(c)  ((c) == ' ' || (c) == '\t' || (c) == '\n')
48
49
50 /*
51  * Decode the string as a RFC-2047 header field
52  */
53
54 int
55 decode_rfc2047 (char *str, char *dst) 
56 {
57     char *p, *q, *pp;
58     char *startofmime, *endofmime;
59     int c, quoted_printable;
60     int encoding_found = 0;     /* did we decode anything?                */
61     int between_encodings = 0;  /* are we between two encodings?          */
62     int equals_pending = 0;     /* is there a '=' pending?                */
63     int whitespace = 0;         /* how much whitespace between encodings? */
64
65     if (!str)
66         return 0;
67
68     /*
69      * Do a quick and dirty check for the '=' character.
70      * This should quickly eliminate many cases.
71      */
72     if (!strchr (str, '='))
73         return 0;
74
75     for (p = str, q = dst; *p; p++) {
76         /*
77          * If we had an '=' character pending from
78          * last iteration, then add it first.
79          */
80         if (equals_pending) {
81             *q++ = '=';
82             equals_pending = 0;
83             between_encodings = 0;      /* we have added non-whitespace text */
84         }
85
86         if (*p != '=') {
87             /* count linear whitespace while between encodings */
88             if (between_encodings && is_lws(*p))
89                 whitespace++;
90             else
91                 between_encodings = 0;  /* we have added non-whitespace text */
92             *q++ = *p;
93             continue;
94         }
95
96         equals_pending = 1;     /* we have a '=' pending */
97
98         /* Check for initial =? */
99         if (*p == '=' && p[1] && p[1] == '?' && p[2]) {
100             startofmime = p + 2;
101
102             /* Scan ahead for the next '?' character */
103             for (pp = startofmime; *pp && *pp != '?'; pp++)
104                 ;
105
106             if (!*pp)
107                 continue;
108
109             /* Check if character set is OK */
110             if (!check_charset(startofmime, pp - startofmime))
111                 continue;
112
113             startofmime = pp + 1;
114
115             /* Check for valid encoding type */
116             if (*startofmime != 'B' && *startofmime != 'b' &&
117                 *startofmime != 'Q' && *startofmime != 'q')
118                 continue;
119
120             /* Is encoding quoted printable or base64? */
121             quoted_printable = (*startofmime == 'Q' || *startofmime == 'q');
122             startofmime++;
123
124             /* Check for next '?' character */
125             if (*startofmime != '?')
126                 continue;
127             startofmime++;
128
129             /*
130              * Scan ahead for the ending ?=
131              *
132              * While doing this, we will also check if encoded
133              * word has any embedded linear whitespace.
134              */
135             endofmime = NULL;
136             for (pp = startofmime; *pp && *(pp+1); pp++) {
137                 if (is_lws(*pp)) {
138                     break;
139                 } else if (*pp == '?' && pp[1] == '=') {
140                     endofmime = pp;
141                     break;
142                 }
143             }
144             if (is_lws(*pp) || endofmime == NULL)
145                 continue;
146
147             /*
148              * We've found an encoded word, so we can drop
149              * the '=' that was pending
150              */
151             equals_pending = 0;
152
153             /*
154              * If we are between two encoded words separated only by
155              * linear whitespace, then we ignore the whitespace.
156              * We will roll back the buffer the number of whitespace
157              * characters we've seen since last encoded word.
158              */
159             if (between_encodings)
160                 q -= whitespace;
161
162             /* Now decode the text */
163             if (quoted_printable) {
164                 for (pp = startofmime; pp < endofmime; pp++) {
165                     if (*pp == '=') {
166                         c = unqp (pp[1], pp[2]);
167                         if (c == -1)
168                             continue;
169                         if (c != 0)
170                             *q++ = c;
171                         pp += 2;
172                     } else if (*pp == '_') {
173                         *q++ = ' ';
174                     } else {
175                         *q++ = *pp;
176                     }
177                 }
178             } else {
179                 /* base64 */
180                 int c1, c2, c3, c4;
181
182                 pp = startofmime;
183                 while (pp < endofmime) {
184                     /* 6 + 2 bits */
185                     while ((pp < endofmime) &&
186                            ((c1 = char64(*pp)) == -1)) {
187                         pp++;
188                     }
189                     if (pp < endofmime) {
190                         pp++;
191                     }
192                     while ((pp < endofmime) &&
193                            ((c2 = char64(*pp)) == -1)) {
194                         pp++;
195                     }
196                     if (pp < endofmime && c1 != -1 && c2 != -1) {
197                         *q++ = (c1 << 2) | (c2 >> 4);
198                         pp++;
199                     }
200                     /* 4 + 4 bits */
201                     while ((pp < endofmime) &&
202                            ((c3 = char64(*pp)) == -1)) {
203                         pp++;
204                     }
205                     if (pp < endofmime && c2 != -1 && c3 != -1) {
206                         *q++ = ((c2 & 0xF) << 4) | (c3 >> 2);
207                         pp++;
208                     }
209                     /* 2 + 6 bits */
210                     while ((pp < endofmime) &&
211                            ((c4 = char64(*pp)) == -1)) {
212                         pp++;
213                     }
214                     if (pp < endofmime && c3 != -1 && c4 != -1) {
215                         *q++ = ((c3 & 0x3) << 6) | (c4);
216                         pp++;
217                     }
218                 }
219             }
220
221             /*
222              * Now that we are done decoding this particular
223              * encoded word, advance string to trailing '='.
224              */
225             p = endofmime + 1;
226
227             encoding_found = 1;         /* we found (at least 1) encoded word */
228             between_encodings = 1;      /* we have just decoded something     */
229             whitespace = 0;             /* re-initialize amount of whitespace */
230         }
231     }
232
233     /* If an equals was pending at end of string, add it now. */
234     if (equals_pending)
235         *q++ = '=';
236     *q = '\0';
237
238     return encoding_found;
239 }