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