a87fc0e8680a24740661bdaffc818dd53276379b
[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 #ifdef HAVE_ICONV
14 #  include <iconv.h>
15 #  include <errno.h>
16 #endif
17
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
27 };
28
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
38 };
39
40 #define char64(c) (((unsigned char) (c) > 127) ? -1 : index_64[(unsigned char) (c)])
41
42 static int
43 unqp (unsigned char byte1, unsigned char byte2)
44 {
45     if (hexindex[byte1] == -1 || hexindex[byte2] == -1)
46         return -1;
47     return (hexindex[byte1] << 4 | hexindex[byte2]);
48 }
49
50 /* Check if character is linear whitespace */
51 #define is_lws(c)  ((c) == ' ' || (c) == '\t' || (c) == '\n')
52
53
54 /*
55  * Decode the string as a RFC-2047 header field
56  */
57
58 int
59 decode_rfc2047 (char *str, char *dst) 
60 {
61     char *p, *q, *pp;
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? */
68 #ifdef HAVE_ICONV
69     int use_iconv = 0;          /* are we converting encoding with iconv? */
70     iconv_t cd;
71     int fromutf8;
72     char *saveq, *convbuf;
73 #endif
74
75     if (!str)
76         return 0;
77
78     /*
79      * Do a quick and dirty check for the '=' character.
80      * This should quickly eliminate many cases.
81      */
82     if (!strchr (str, '='))
83         return 0;
84
85     for (p = str, q = dst; *p; p++) {
86
87         /* reset iconv */
88 #ifdef HAVE_ICONV
89         if (use_iconv) {
90             iconv_close(cd);
91             use_iconv = 0;
92         }
93 #endif
94         /*
95          * If we had an '=' character pending from
96          * last iteration, then add it first.
97          */
98         if (equals_pending) {
99             *q++ = '=';
100             equals_pending = 0;
101             between_encodings = 0;      /* we have added non-whitespace text */
102         }
103
104         if (*p != '=') {
105             /* count linear whitespace while between encodings */
106             if (between_encodings && is_lws(*p))
107                 whitespace++;
108             else
109                 between_encodings = 0;  /* we have added non-whitespace text */
110             *q++ = *p;
111             continue;
112         }
113
114         equals_pending = 1;     /* we have a '=' pending */
115
116         /* Check for initial =? */
117         if (*p == '=' && p[1] && p[1] == '?' && p[2]) {
118             startofmime = p + 2;
119
120             /* Scan ahead for the next '?' character */
121             for (pp = startofmime; *pp && *pp != '?'; pp++)
122                 ;
123
124             if (!*pp)
125                 continue;
126
127             /* Check if character set can be handled natively */
128             if (!check_charset(startofmime, pp - startofmime)) {
129 #ifdef HAVE_ICONV
130                 /* .. it can't. We'll use iconv then. */
131                 *pp = '\0';
132                 cd = iconv_open(get_charset(), startofmime);
133                 fromutf8 = !strcasecmp(startofmime, "UTF-8");
134                 *pp = '?';
135                 if (cd == (iconv_t)-1) continue;
136                 use_iconv = 1;
137 #else
138                 continue;
139 #endif
140             }
141
142             startofmime = pp + 1;
143
144             /* Check for valid encoding type */
145             if (*startofmime != 'B' && *startofmime != 'b' &&
146                 *startofmime != 'Q' && *startofmime != 'q')
147                 continue;
148
149             /* Is encoding quoted printable or base64? */
150             quoted_printable = (*startofmime == 'Q' || *startofmime == 'q');
151             startofmime++;
152
153             /* Check for next '?' character */
154             if (*startofmime != '?')
155                 continue;
156             startofmime++;
157
158             /*
159              * Scan ahead for the ending ?=
160              *
161              * While doing this, we will also check if encoded
162              * word has any embedded linear whitespace.
163              */
164             endofmime = NULL;
165             for (pp = startofmime; *pp && *(pp+1); pp++) {
166                 if (is_lws(*pp)) {
167                     break;
168                 } else if (*pp == '?' && pp[1] == '=') {
169                     endofmime = pp;
170                     break;
171                 }
172             }
173             if (is_lws(*pp) || endofmime == NULL)
174                 continue;
175
176             /*
177              * We've found an encoded word, so we can drop
178              * the '=' that was pending
179              */
180             equals_pending = 0;
181
182             /*
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.
187              */
188             if (between_encodings)
189                 q -= whitespace;
190
191 #ifdef HAVE_ICONV
192             if (use_iconv) {
193                 saveq = q;
194                 if (!(q = convbuf = (char *)malloc(endofmime - startofmime)))
195                     continue;
196             }
197 #endif
198
199             /* Now decode the text */
200             if (quoted_printable) {
201                 for (pp = startofmime; pp < endofmime; pp++) {
202                     if (*pp == '=') {
203                         c = unqp (pp[1], pp[2]);
204                         if (c == -1)
205                             continue;
206                         if (c != 0)
207                             *q++ = c;
208                         pp += 2;
209                     } else if (*pp == '_') {
210                         *q++ = ' ';
211                     } else {
212                         *q++ = *pp;
213                     }
214                 }
215             } else {
216                 /* base64 */
217                 int c1, c2, c3, c4;
218
219                 pp = startofmime;
220                 while (pp < endofmime) {
221                     /* 6 + 2 bits */
222                     while ((pp < endofmime) &&
223                            ((c1 = char64(*pp)) == -1)) {
224                         pp++;
225                     }
226                     if (pp < endofmime) {
227                         pp++;
228                     }
229                     while ((pp < endofmime) &&
230                            ((c2 = char64(*pp)) == -1)) {
231                         pp++;
232                     }
233                     if (pp < endofmime && c1 != -1 && c2 != -1) {
234                         *q++ = (c1 << 2) | (c2 >> 4);
235                         pp++;
236                     }
237                     /* 4 + 4 bits */
238                     while ((pp < endofmime) &&
239                            ((c3 = char64(*pp)) == -1)) {
240                         pp++;
241                     }
242                     if (pp < endofmime && c2 != -1 && c3 != -1) {
243                         *q++ = ((c2 & 0xF) << 4) | (c3 >> 2);
244                         pp++;
245                     }
246                     /* 2 + 6 bits */
247                     while ((pp < endofmime) &&
248                            ((c4 = char64(*pp)) == -1)) {
249                         pp++;
250                     }
251                     if (pp < endofmime && c3 != -1 && c4 != -1) {
252                         *q++ = ((c3 & 0x3) << 6) | (c4);
253                         pp++;
254                     }
255                 }
256             }
257
258 #ifdef HAVE_ICONV
259             /* Convert to native character set */
260             if (use_iconv) {
261                 size_t inbytes = q - convbuf;
262                 size_t outbytes = BUFSIZ;
263                 ICONV_CONST char *start = convbuf;
264                 
265                 while (inbytes) {
266                     if (iconv(cd, &start, &inbytes, &saveq, &outbytes) ==
267                             (size_t)-1) {
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);
273                         *saveq++ = '?';
274                         /* skip to next input character */
275                         if (fromutf8) {
276                             for (start++;(*start & 192) == 128;start++)
277                                 inbytes--;
278                         } else
279                             start++, inbytes--;
280                     }
281                 }
282                 q = saveq;
283                 free(convbuf);
284             }
285 #endif
286             
287             /*
288              * Now that we are done decoding this particular
289              * encoded word, advance string to trailing '='.
290              */
291             p = endofmime + 1;
292
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 */
296         }
297     }
298 #ifdef HAVE_ICONV
299     if (use_iconv) iconv_close(cd);
300 #endif
301
302     /* If an equals was pending at end of string, add it now. */
303     if (equals_pending)
304         *q++ = '=';
305     *q = '\0';
306
307     return encoding_found;
308 }