use memset to clear the msgstats in folder_realloc
[mmh] / sbr / fmt_rfc2047.c
1 /*
2 ** fmt_rfc2047.c -- decode RFC-2047 header format
3 **
4 ** This code is Copyright (c) 2002, by the authors of nmh.  See the
5 ** COPYRIGHT file in the root directory of the nmh distribution for
6 ** complete copyright information.
7 */
8
9 #include <h/mh.h>
10 #include <h/utils.h>
11 #ifdef HAVE_ICONV
12 #  include <iconv.h>
13 #  include <errno.h>
14 #endif
15
16 static signed char hexindex[] = {
17         -1,-1,-1,-1,-1,-1,-1,-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,
19         -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
20          0, 1, 2, 3, 4, 5, 6, 7, 8, 9,-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         -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 };
26
27 static signed char index_64[128] = {
28         -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
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,62, -1,-1,-1,63,
31         52,53,54,55, 56,57,58,59, 60,61,-1,-1, -1,-1,-1,-1,
32         -1, 0, 1, 2,  3, 4, 5, 6,  7, 8, 9,10, 11,12,13,14,
33         15,16,17,18, 19,20,21,22, 23,24,25,-1, -1,-1,-1,-1,
34         -1,26,27,28, 29,30,31,32, 33,34,35,36, 37,38,39,40,
35         41,42,43,44, 45,46,47,48, 49,50,51,-1, -1,-1,-1,-1
36 };
37
38 #define char64(c) (((unsigned char) (c) > 127) ? -1 : index_64[(unsigned char) (c)])
39
40 static int
41 unqp(unsigned char byte1, unsigned char byte2)
42 {
43         if (hexindex[byte1] == -1 || hexindex[byte2] == -1)
44                 return -1;
45         return (hexindex[byte1] << 4 | hexindex[byte2]);
46 }
47
48 /* Check if character is linear whitespace */
49 #define is_lws(c)  ((c) == ' ' || (c) == '\t' || (c) == '\n')
50
51
52 /*
53 ** Decode the string as a RFC-2047 header field
54 */
55
56 /* Add character to the destination buffer, and bomb out if it fills up */
57 #define ADDCHR(C) do { *q++ = (C); dstlen--; if (!dstlen) goto buffull; } while (0)
58
59 int
60 decode_rfc2047(char *str, char *dst, size_t dstlen)
61 {
62         char *p, *q, *pp;
63         char *startofmime, *endofmime;
64         int c, quoted_printable;
65         int encoding_found = 0;  /* did we decode anything? */
66         int between_encodings = 0;  /* are we between two encodings? */
67         int equals_pending = 0;  /* is there a '=' pending?  */
68         int whitespace = 0;  /* how much whitespace between encodings? */
69 #ifdef HAVE_ICONV
70         int use_iconv = 0;  /* are we converting encoding with iconv? */
71         iconv_t cd = NULL;
72         int fromutf8 = 0;
73         char *saveq, *convbuf = NULL;
74         size_t savedstlen;
75 #endif
76
77         if (!str)
78                 return 0;
79
80         /*
81         ** Do a quick and dirty check for the '=' character.
82         ** This should quickly eliminate many cases.
83         */
84         if (!strchr(str, '='))
85                 return 0;
86
87         for (p = str, q = dst; *p; p++) {
88
89                 /* reset iconv */
90 #ifdef HAVE_ICONV
91                 if (use_iconv) {
92                         iconv_close(cd);
93                         use_iconv = 0;
94                 }
95 #endif
96                 /*
97                 ** If we had an '=' character pending from
98                 ** last iteration, then add it first.
99                 */
100                 if (equals_pending) {
101                         ADDCHR('=');
102                         equals_pending = 0;
103                         between_encodings = 0;  /* we have added non-whitespace text */
104                 }
105
106                 if (*p != '=') {
107                         /* count linear whitespace while between encodings */
108                         if (between_encodings && is_lws(*p))
109                                 whitespace++;
110                         else
111                                 between_encodings = 0;  /* we have added non-whitespace text */
112                         ADDCHR(*p);
113                         continue;
114                 }
115
116                 equals_pending = 1;  /* we have a '=' pending */
117
118                 /* Check for initial =? */
119                 if (*p == '=' && p[1] && p[1] == '?' && p[2]) {
120                         startofmime = p + 2;
121
122                         /* Scan ahead for the next '?' character */
123                         for (pp = startofmime; *pp && *pp != '?'; pp++)
124                                 ;
125
126                         if (!*pp)
127                                 continue;
128                         *pp = '\0';
129
130                         /* Check if character set can be handled natively */
131                         if (!is_native_charset(startofmime)) {
132 #ifdef HAVE_ICONV
133                                 /* .. it can't. We'll use iconv then. */
134                                 cd = iconv_open(get_charset(), startofmime);
135                                 fromutf8 = !mh_strcasecmp(startofmime, "UTF-8");
136                                 *pp = '?';
137                                 if (cd == (iconv_t)-1)
138                                         continue;
139                                 use_iconv = 1;
140 #else
141                                 *pp = '?';
142                                 continue;
143 #endif
144                         }
145
146                         *pp = '?';
147                         startofmime = pp + 1;
148
149                         /* Check for valid encoding type */
150                         if (*startofmime != 'B' && *startofmime != 'b' &&
151                                 *startofmime != 'Q' && *startofmime != 'q')
152                                 continue;
153
154                         /* Is encoding quoted printable or base64? */
155                         quoted_printable = (*startofmime == 'Q' || *startofmime == 'q');
156                         startofmime++;
157
158                         /* Check for next '?' character */
159                         if (*startofmime != '?')
160                                 continue;
161                         startofmime++;
162
163                         /*
164                         ** Scan ahead for the ending ?=
165                         **
166                         ** While doing this, we will also check if encoded
167                         ** word has any embedded linear whitespace.
168                         */
169                         endofmime = NULL;
170                         for (pp = startofmime; *pp && *(pp+1); pp++) {
171                                 if (is_lws(*pp)) {
172                                         break;
173                                 } else if (*pp == '?' && pp[1] == '=') {
174                                         endofmime = pp;
175                                         break;
176                                 }
177                         }
178                         if (is_lws(*pp) || endofmime == NULL)
179                                 continue;
180
181                         /*
182                         ** We've found an encoded word, so we can drop
183                         ** the '=' that was pending
184                         */
185                         equals_pending = 0;
186
187                         /*
188                         ** If we are between two encoded words separated
189                         ** only by linear whitespace, then we ignore
190                         ** the whitespace.  We will roll back the buffer
191                         ** the number of whitespace characters we've seen
192                         ** since last encoded word.
193                         */
194                         if (between_encodings) {
195                                 q -= whitespace;
196                                 dstlen += whitespace;
197                         }
198
199 #ifdef HAVE_ICONV
200                         /*
201                         ** empty encoded text. This ensures that we don't
202                         ** malloc 0 bytes but skip on to the end
203                         */
204                         if (endofmime == startofmime && use_iconv) {
205                                 use_iconv = 0;
206                                 iconv_close(cd);
207                         }
208
209                         if (use_iconv) {
210                                 saveq = q;
211                                 savedstlen = dstlen;
212                                 q = convbuf = mh_xcalloc(endofmime - startofmime, sizeof(char));
213                         }
214 /*
215 ** ADDCHR2 is for adding characters when q is or might be convbuf:
216 ** in this case on buffer-full we want to run iconv before returning.
217 ** I apologise for the dreadful name.
218 */
219 # define ADDCHR2(C) do { *q++ = (C); dstlen--; if (!dstlen) goto iconvbuffull; } while (0)
220 #else
221 # define ADDCHR2(C) ADDCHR(C)
222 #endif
223
224                         /* Now decode the text */
225                         if (quoted_printable) {
226                                 for (pp = startofmime; pp < endofmime; pp++) {
227                                         if (*pp == '=') {
228                                                 c = unqp(pp[1], pp[2]);
229                                                 if (c == -1)
230                                                         continue;
231                                                 if (c != 0)
232                                                         *q++ = c;
233                                                 pp += 2;
234                                         } else if (*pp == '_') {
235                                                 ADDCHR2(' ');
236                                         } else {
237                                                 ADDCHR2(*pp);
238                                         }
239                                 }
240                         } else {
241                                 /* base64 */
242                                 int c1, c2, c3, c4;
243                                 c1 = c2 = c3 = c4 = -1;
244
245                                 pp = startofmime;
246                                 while (pp < endofmime) {
247                                         /* 6 + 2 bits */
248                                         while ((pp < endofmime) &&
249                                                         ((c1 = char64(*pp))
250                                                         == -1)) {
251                                                 pp++;
252                                         }
253                                         if (pp < endofmime) {
254                                                 pp++;
255                                         }
256                                         while ((pp < endofmime) &&
257                                                         ((c2 = char64(*pp))
258                                                         == -1)) {
259                                                 pp++;
260                                         }
261                                         if (pp < endofmime && c1 != -1 && c2 != -1) {
262                                                 ADDCHR2((c1 << 2) | (c2 >> 4));
263                                                 pp++;
264                                         }
265                                         /* 4 + 4 bits */
266                                         while ((pp < endofmime) &&
267                                                         ((c3 = char64(*pp))
268                                                         == -1)) {
269                                                 pp++;
270                                         }
271                                         if (pp < endofmime && c2 != -1 && c3 != -1) {
272                                                 ADDCHR2(((c2 & 0xF) << 4) | (c3 >> 2));
273                                                 pp++;
274                                         }
275                                         /* 2 + 6 bits */
276                                         while ((pp < endofmime) &&
277                                                         ((c4 = char64(*pp))
278                                                         == -1)) {
279                                                 pp++;
280                                         }
281                                         if (pp < endofmime && c3 != -1 && c4 != -1) {
282                                                 ADDCHR2(((c3 & 0x3) << 6) | (c4));
283                                                 pp++;
284                                         }
285                                 }
286                         }
287
288 #ifdef HAVE_ICONV
289                 iconvbuffull:
290                         /*
291                         ** NB that the string at convbuf is not necessarily
292                         ** NUL terminated here:
293                         ** q points to the first byte after the valid part.
294                         */
295                         /* Convert to native character set */
296                         if (use_iconv) {
297                                 size_t inbytes = q - convbuf;
298                                 ICONV_CONST char *start = convbuf;
299
300                                 while (inbytes) {
301                                         if (iconv(cd, &start, &inbytes, &saveq, &savedstlen) ==
302                                                 (size_t)-1) {
303                                                 if (errno != EILSEQ)
304                                                         break;
305                                                 /*
306                                                 ** character couldn't be
307                                                 ** converted. we output a
308                                                 ** `?'  and try to carry on
309                                                 ** which won't work if either
310                                                 ** encoding was stateful
311                                                 */
312                                                 iconv(cd, 0, 0, &saveq, &savedstlen);
313                                                 if (!savedstlen)
314                                                         break;
315                                                 *saveq++ = '?';
316                                                 savedstlen--;
317                                                 if (!savedstlen)
318                                                         break;
319                                                 /* skip to next input character */
320                                                 if (fromutf8) {
321                                                         inbytes--;
322                                                         start++;
323                                                         while ((start < q) && ((*start & 192) == 128)) {
324                                                                 start++;
325                                                                 inbytes--;
326                                                         }
327                                                 } else
328                                                         start++, inbytes--;
329                                                 if (start >= q)
330                                                         break;
331                                         }
332                                 }
333                                 q = saveq;
334                                 /*
335                                 ** Stop now if (1) we hit the end of the
336                                 ** buffer trying to do MIME decoding and
337                                 ** have just iconv-converted a partial
338                                 ** string or (2) our iconv-conversion hit
339                                 ** the end of the buffer.
340                                 */
341                                 if (!dstlen || !savedstlen)
342                                         goto buffull;
343                                 dstlen = savedstlen;
344                                 mh_free0(&convbuf);
345                         }
346 #endif
347
348                         /*
349                         ** Now that we are done decoding this particular
350                         ** encoded word, advance string to trailing '='.
351                         */
352                         p = endofmime + 1;
353
354                         encoding_found = 1;  /* we found (at least 1) encoded word */
355                         between_encodings = 1;  /* we have just decoded something */
356                         whitespace = 0;  /* re-initialize amount of whitespace */
357                 }
358         }
359 #ifdef HAVE_ICONV
360         if (use_iconv) iconv_close(cd);
361 #endif
362
363         /* If an equals was pending at end of string, add it now. */
364         if (equals_pending)
365                 ADDCHR('=');
366         *q = '\0';
367
368         return encoding_found;
369
370   buffull:
371         /*
372         ** q is currently just off the end of the buffer,
373         ** so rewind to NUL terminate
374         */
375         q--;
376         *q = '\0';
377         return encoding_found;
378 }