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