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