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