fix regressions
[apps/madmutt.git] / rfc2231.c
1 /*
2  * Copyright notice from original mutt:
3  * Copyright (C) 1999-2000 Thomas Roessler <roessler@does-not-exist.org>
4  *
5  * This file is part of mutt-ng, see http://www.muttng.org/.
6  * It's licensed under the GNU General Public License,
7  * please see the file GPL in the top level source directory.
8  */
9
10 /*
11  * Yet another MIME encoding for header data.  This time, it's
12  * parameters, specified in RFC 2231, and modeled after the
13  * encoding used in URLs.
14  * 
15  * Additionally, continuations and encoding are mixed in an, errrm,
16  * interesting manner.
17  *
18  */
19
20 #if HAVE_CONFIG_H
21 # include "config.h"
22 #endif
23
24 #include <lib-lib/mem.h>
25
26 #include "mutt.h"
27 #include "ascii.h"
28 #include "mime.h"
29 #include "charset.h"
30 #include "lib/str.h"
31 #include "rfc2047.h"
32 #include "rfc2231.h"
33
34 #include "lib/mem.h"
35
36 #include <ctype.h>
37 #include <string.h>
38 #include <stdlib.h>
39
40 struct rfc2231_parameter {
41   char *attribute;
42   char *value;
43   int index;
44   int encoded;
45   struct rfc2231_parameter
46                    *next;
47 };
48
49 static char *rfc2231_get_charset (char *, char *, size_t);
50 static struct rfc2231_parameter *rfc2231_new_parameter (void);
51 static void rfc2231_decode_one (char *, char *);
52 static void rfc2231_free_parameter (struct rfc2231_parameter **);
53 static void rfc2231_join_continuations (PARAMETER **,
54                                         struct rfc2231_parameter *);
55 static void rfc2231_list_insert (struct rfc2231_parameter **,
56                                  struct rfc2231_parameter *);
57
58 static void purge_empty_parameters (PARAMETER ** headp)
59 {
60   PARAMETER *p, *q, **last;
61
62   for (last = headp, p = *headp; p; p = q) {
63     q = p->next;
64     if (!p->attribute || !p->value) {
65       *last = q;
66       p->next = NULL;
67       mutt_free_parameter (&p);
68     }
69     else
70       last = &p->next;
71   }
72 }
73
74
75 void rfc2231_decode_parameters (PARAMETER ** headp)
76 {
77   PARAMETER *head = NULL;
78   PARAMETER **last;
79   PARAMETER *p, *q;
80
81   struct rfc2231_parameter *conthead = NULL;
82   struct rfc2231_parameter *conttmp;
83
84   char *s, *t;
85   char charset[STRING];
86
87   int encoded;
88   int index;
89   short dirty = 0;              /* set to 1 when we may have created
90                                  * empty parameters.
91                                  */
92
93   if (!headp)
94     return;
95
96   purge_empty_parameters (headp);
97
98   for (last = &head, p = *headp; p; p = q) {
99     q = p->next;
100
101     if (!(s = strchr (p->attribute, '*'))) {
102
103       /* 
104        * Using RFC 2047 encoding in MIME parameters is explicitly
105        * forbidden by that document.  Nevertheless, it's being
106        * generated by some software, including certain Lotus Notes to 
107        * Internet Gateways.  So we actually decode it.
108        */
109
110       if (option (OPTRFC2047PARAMS) && p->value && strstr (p->value, "=?"))
111         rfc2047_decode (&p->value);
112       else if (!option (OPTSTRICTMIME)) {
113         if (ascii_strcasecmp (AssumedCharset, "us-ascii"))
114           mutt_convert_nonmime_string (&p->value);
115       }
116
117       *last = p;
118       last = &p->next;
119       p->next = NULL;
120     }
121     else if (*(s + 1) == '\0') {
122       *s = '\0';
123
124       s = rfc2231_get_charset (p->value, charset, sizeof (charset));
125       rfc2231_decode_one (p->value, s);
126       mutt_convert_string (&p->value, charset, Charset, M_ICONV_HOOK_FROM);
127
128       *last = p;
129       last = &p->next;
130       p->next = NULL;
131
132       dirty = 1;
133     }
134     else {
135       *s = '\0';
136       s++;                      /* let s point to the first character of index. */
137       for (t = s; *t && isdigit ((unsigned char) *t); t++);
138       encoded = (*t == '*');
139       *t = '\0';
140
141       index = atoi (s);
142
143       conttmp = rfc2231_new_parameter ();
144       conttmp->attribute = p->attribute;
145       conttmp->value = p->value;
146       conttmp->encoded = encoded;
147       conttmp->index = index;
148
149       p->attribute = NULL;
150       p->value = NULL;
151       p_delete(&p);
152
153       rfc2231_list_insert (&conthead, conttmp);
154     }
155   }
156
157   if (conthead) {
158     rfc2231_join_continuations (last, conthead);
159     dirty = 1;
160   }
161
162   *headp = head;
163
164   if (dirty)
165     purge_empty_parameters (headp);
166 }
167
168 static struct rfc2231_parameter *rfc2231_new_parameter (void)
169 {
170   return p_new(struct rfc2231_parameter, 1);
171 }
172
173 static void rfc2231_free_parameter (struct rfc2231_parameter **p)
174 {
175   if (*p) {
176     p_delete(&(*p)->attribute);
177     p_delete(&(*p)->value);
178     p_delete(p);
179   }
180 }
181
182 static char *rfc2231_get_charset (char *value, char *charset, size_t chslen)
183 {
184   char *t, *u;
185
186   if (!(t = strchr (value, '\''))) {
187     charset[0] = '\0';
188     return value;
189   }
190
191   *t = '\0';
192   strfcpy (charset, value, chslen);
193
194   if ((u = strchr (t + 1, '\'')))
195     return u + 1;
196   else
197     return t + 1;
198 }
199
200 static void rfc2231_decode_one (char *dest, char *src)
201 {
202   char *d;
203
204   for (d = dest; *src; src++) {
205     if (*src == '%' &&
206         isxdigit ((unsigned char) *(src + 1)) &&
207         isxdigit ((unsigned char) *(src + 2))) {
208       *d++ = (hexval (*(src + 1)) << 4) | (hexval (*(src + 2)));
209       src += 2;
210     }
211     else
212       *d++ = *src;
213   }
214
215   *d = '\0';
216 }
217
218 /* insert parameter into an ordered list.
219  * 
220  * Primary sorting key: attribute
221  * Secondary sorting key: index
222  */
223
224 static void rfc2231_list_insert (struct rfc2231_parameter **list,
225                                  struct rfc2231_parameter *par)
226 {
227   struct rfc2231_parameter **last = list;
228   struct rfc2231_parameter *p = *list, *q;
229   int c;
230
231   while (p) {
232     last = &p->next;
233     q = p;
234     p = p->next;
235
236     c = str_cmp (par->value, q->value);
237     if ((c > 0) || (c == 0 && par->index >= q->index))
238       break;
239   }
240
241   par->next = p;
242   *last = par;
243 }
244
245 /* process continuation parameters */
246
247 static void rfc2231_join_continuations (PARAMETER ** head,
248                                         struct rfc2231_parameter *par)
249 {
250   struct rfc2231_parameter *q;
251
252   char attribute[STRING];
253   char charset[STRING];
254   char *value = NULL;
255   char *valp;
256   int encoded;
257
258   size_t l, vl;
259
260   while (par) {
261     value = NULL;
262     l = 0;
263
264     strfcpy (attribute, par->attribute, sizeof (attribute));
265
266     if ((encoded = par->encoded))
267       valp = rfc2231_get_charset (par->value, charset, sizeof (charset));
268     else
269       valp = par->value;
270
271     do {
272       if (encoded && par->encoded)
273         rfc2231_decode_one (par->value, valp);
274
275       vl = str_len (par->value);
276
277       mem_realloc (&value, l + vl + 1);
278       strcpy (value + l, par->value);   /* __STRCPY_CHECKED__ */
279       l += vl;
280
281       q = par->next;
282       rfc2231_free_parameter (&par);
283       if ((par = q))
284         valp = par->value;
285     } while (par && !str_cmp (par->attribute, attribute));
286
287     if (value) {
288       if (encoded)
289         mutt_convert_string (&value, charset, Charset, M_ICONV_HOOK_FROM);
290       *head = mutt_new_parameter ();
291       (*head)->attribute = str_dup (attribute);
292       (*head)->value = value;
293       head = &(*head)->next;
294     }
295   }
296 }
297
298 int rfc2231_encode_string (char **pd)
299 {
300   int ext = 0, encode = 0;
301   char *charset, *s, *t, *e, *d = 0;
302   size_t slen, dlen = 0;
303
304   /* 
305    * A shortcut to detect pure 7bit data.
306    * 
307    * This should prevent the worst when character set handling
308    * is flawed.
309    */
310
311   for (s = *pd; *s; s++)
312     if (*s & 0x80)
313       break;
314
315   if (!*s)
316     return 0;
317
318   if (!Charset || !SendCharset ||
319       !(charset = mutt_choose_charset (Charset, SendCharset,
320                                        *pd, str_len (*pd), &d, &dlen))) {
321     charset = str_dup (Charset ? Charset : "unknown-8bit");
322     d = *pd;
323     dlen = str_len (d);
324   }
325
326   if (!mutt_is_us_ascii (charset))
327     encode = 1;
328
329   for (s = d, slen = dlen; slen; s++, slen--)
330     if (*s < 0x20 || *s >= 0x7f)
331       encode = 1, ++ext;
332     else if (strchr (MimeSpecials, *s) || strchr ("*'%", *s))
333       ++ext;
334
335   if (encode) {
336     e = p_new(char, dlen + 2 * ext + str_len(charset) + 3);
337     sprintf (e, "%s''", charset);       /* __SPRINTF_CHECKED__ */
338     t = e + str_len (e);
339     for (s = d, slen = dlen; slen; s++, slen--)
340       if (*s < 0x20 || *s >= 0x7f ||
341           strchr (MimeSpecials, *s) || strchr ("*'%", *s)) {
342         sprintf (t, "%%%02X", (unsigned char) *s);
343         t += 3;
344       }
345       else
346         *t++ = *s;
347     *t = '\0';
348
349     if (d != *pd)
350       p_delete(&d);
351     p_delete(pd);
352     *pd = e;
353   }
354   else if (d != *pd) {
355     p_delete(pd);
356     *pd = d;
357   }
358
359   p_delete(&charset);
360
361   return encode;
362 }