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