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