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