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       else if (!option (OPTSTRICTMIME))
121       {
122         if (ascii_strcasecmp (AssumedCharset, "us-ascii"))
123           mutt_convert_nonmime_string (&p->value);
124       }
125
126       *last = p;
127       last = &p->next;
128       p->next = NULL;
129     }
130     else if (*(s + 1) == '\0')
131     {
132       *s = '\0';
133       
134       s = rfc2231_get_charset (p->value, charset, sizeof (charset));
135       rfc2231_decode_one (p->value, s);
136       mutt_convert_string (&p->value, charset, Charset, M_ICONV_HOOK_FROM);
137
138       *last = p;
139       last = &p->next;
140       p->next = NULL;
141       
142       dirty = 1;
143     }
144     else
145     {
146       *s = '\0'; s++; /* let s point to the first character of index. */
147       for (t = s; *t && isdigit ((unsigned char) *t); t++)
148         ;
149       encoded = (*t == '*');
150       *t = '\0';
151
152       index = atoi (s);
153
154       conttmp = rfc2231_new_parameter ();
155       conttmp->attribute = p->attribute;
156       conttmp->value = p->value;
157       conttmp->encoded = encoded;
158       conttmp->index = index;
159       
160       p->attribute = NULL;
161       p->value = NULL;
162       FREE (&p);
163
164       rfc2231_list_insert (&conthead, conttmp);
165     }
166   }
167
168   if (conthead)
169   {
170     rfc2231_join_continuations (last, conthead);
171     dirty = 1;
172   }
173   
174   *headp = head;
175   
176   if (dirty)
177     purge_empty_parameters (headp);
178 }
179   
180 static struct rfc2231_parameter *rfc2231_new_parameter (void)
181 {
182   return safe_calloc (sizeof (struct rfc2231_parameter), 1);
183 }
184
185 static void rfc2231_free_parameter (struct rfc2231_parameter **p)
186 {
187   if (*p)
188   {
189     FREE (&(*p)->attribute);
190     FREE (&(*p)->value);
191     FREE (p);
192   }
193 }
194
195 static char *rfc2231_get_charset (char *value, char *charset, size_t chslen)
196 {
197   char *t, *u;
198   
199   if (!(t = strchr (value, '\'')))
200   {
201     charset[0] = '\0';
202     return value;
203   }
204   
205   *t = '\0';
206   strfcpy (charset, value, chslen);
207   
208   if ((u = strchr (t + 1, '\'')))
209     return u + 1;
210   else
211     return t + 1;
212 }
213
214 static void rfc2231_decode_one (char *dest, char *src)
215 {
216   char *d;
217
218   for (d = dest; *src; src++)
219   {
220     if (*src == '%' &&
221         isxdigit ((unsigned char) *(src + 1)) &&
222         isxdigit ((unsigned char) *(src + 2)))
223     {
224       *d++ = (hexval (*(src + 1)) << 4) | (hexval (*(src + 2)));
225       src += 2;
226     }
227     else
228       *d++ = *src;
229   }
230   
231   *d = '\0';
232 }
233
234 /* insert parameter into an ordered list.
235  * 
236  * Primary sorting key: attribute
237  * Secondary sorting key: index
238  */
239
240 static void rfc2231_list_insert (struct rfc2231_parameter **list,
241                                  struct rfc2231_parameter *par)
242 {
243   struct rfc2231_parameter **last = list;
244   struct rfc2231_parameter *p = *list, *q;
245   int c;
246   
247   while (p)
248   {
249     last = &p->next;
250     q = p; p = p->next;
251
252     c = strcmp (par->value, q->value);
253     if ((c > 0) || (c == 0 && par->index >= q->index))
254       break;
255   }
256   
257   par->next = p;
258   *last = par;
259 }
260
261 /* process continuation parameters */
262
263 static void rfc2231_join_continuations (PARAMETER **head,
264                                         struct rfc2231_parameter *par)
265 {
266   struct rfc2231_parameter *q;
267
268   char attribute[STRING];
269   char charset[STRING];
270   char *value = NULL;
271   char *valp;
272   int encoded;
273
274   size_t l, vl;
275   
276   while (par)
277   {
278     value = NULL; l = 0;
279     
280     strfcpy (attribute, par->attribute, sizeof (attribute));
281
282     if ((encoded = par->encoded))
283       valp = rfc2231_get_charset (par->value, charset, sizeof (charset));
284     else
285       valp = par->value;
286
287     do 
288     {
289       if (encoded && par->encoded)
290         rfc2231_decode_one (par->value, valp);
291       
292       vl = strlen (par->value);
293       
294       safe_realloc (&value, l + vl + 1);
295       strcpy (value + l, par->value);   /* __STRCPY_CHECKED__ */
296       l += vl;
297
298       q = par->next;
299       rfc2231_free_parameter (&par);
300       if ((par = q))
301         valp = par->value;
302     } while (par && !strcmp (par->attribute, attribute));
303     
304     if (value)
305     {
306       if (encoded)
307         mutt_convert_string (&value, charset, Charset, M_ICONV_HOOK_FROM);
308       *head = mutt_new_parameter ();
309       (*head)->attribute = safe_strdup (attribute);
310       (*head)->value = value;
311       head = &(*head)->next;
312     }
313   }
314 }
315
316 int rfc2231_encode_string (char **pd)
317 {
318   int ext = 0, encode = 0;
319   char *charset, *s, *t, *e, *d = 0;
320   size_t slen, dlen = 0;
321
322   /* 
323    * A shortcut to detect pure 7bit data.
324    * 
325    * This should prevent the worst when character set handling
326    * is flawed.
327    */
328
329   for (s = *pd; *s; s++)
330     if (*s & 0x80)
331       break;
332   
333   if (!*s)
334     return 0;
335   
336   if (!Charset || !SendCharset ||
337       !(charset = mutt_choose_charset (Charset, SendCharset,
338                                   *pd, strlen (*pd), &d, &dlen)))
339   {
340     charset = safe_strdup (Charset ? Charset : "unknown-8bit");
341     d = *pd;
342     dlen = strlen (d);
343   }
344
345   if (!mutt_is_us_ascii (charset))
346     encode = 1;
347
348   for (s = d, slen = dlen; slen; s++, slen--)
349     if (*s < 0x20 || *s >= 0x7f)
350       encode = 1, ++ext;
351     else if (strchr (MimeSpecials, *s) || strchr ("*'%", *s))
352       ++ext;
353
354   if (encode)
355   {
356     e = safe_malloc (dlen + 2*ext + strlen (charset) + 3);
357     sprintf (e, "%s''", charset);       /* __SPRINTF_CHECKED__ */
358     t = e + strlen (e);
359     for (s = d, slen = dlen; slen; s++, slen--)
360       if (*s < 0x20 || *s >= 0x7f ||
361           strchr (MimeSpecials, *s) || strchr ("*'%", *s))
362       {
363         sprintf (t, "%%%02X", (unsigned char)*s);
364         t += 3;
365       }
366       else
367         *t++ = *s;
368     *t = '\0';
369
370     if (d != *pd)
371       FREE (&d);
372     FREE (pd);
373     *pd = e;
374   }
375   else if (d != *pd)
376   {
377     FREE (pd);
378     *pd = d;
379   }
380   
381   FREE (&charset);
382   
383   return encode;
384 }
385