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