move more things in the lib-mime
[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 "mutt.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_t **headp)
138 {
139     while (*headp) {
140         parameter_t *p = *headp;
141
142         if (!p->attribute || !p->value) {
143             p = parameter_list_pop(headp);
144             parameter_delete(&p);
145         } else {
146             headp = &(*headp)->next;
147         }
148     }
149 }
150
151 /* process continuation parameters */
152 /* XXX: MC: not read */
153 static void
154 rfc2231_join_continuations(parameter_t **head, rfc2231_param *par)
155 {
156     rfc2231_param *q;
157
158     char attribute[STRING];
159     char charset[STRING];
160     char *value = NULL;
161     char *valp;
162     int encoded;
163
164     size_t l, vl;
165
166     while (par) {
167         value = NULL;
168         l = 0;
169
170         m_strcpy(attribute, sizeof(attribute), par->attribute);
171
172         if ((encoded = par->encoded))
173             valp = rfc2231_get_charset (par->value, charset, sizeof (charset));
174         else
175             valp = par->value;
176
177         do {
178             if (encoded && par->encoded)
179                 rfc2231_decode_one (par->value, valp);
180
181             vl = m_strlen(par->value);
182
183             p_realloc(&value, l + vl + 1);
184             strcpy (value + l, par->value);   /* __STRCPY_CHECKED__ */
185             l += vl;
186
187             q = par->next;
188             rfc2231_param_delete (&par);
189             if ((par = q))
190                 valp = par->value;
191         } while (par && !m_strcmp(par->attribute, attribute));
192
193         if (value) {
194             if (encoded)
195                 mutt_convert_string (&value, charset, Charset, M_ICONV_HOOK_FROM);
196             *head = parameter_new();
197             (*head)->attribute = m_strdup(attribute);
198             (*head)->value = value;
199             head = &(*head)->next;
200         }
201     }
202 }
203
204 /****************************************************************************/
205 /* Public API                                                               */
206 /****************************************************************************/
207
208 /* XXX: MC: not read */
209 void rfc2231_decode_parameters (parameter_t ** headp)
210 {
211     parameter_t *head = NULL;
212     parameter_t **last;
213     parameter_t *p, *q;
214
215     rfc2231_param *conthead = NULL;
216     rfc2231_param *conttmp;
217
218     char *s, *t;
219     char charset[STRING];
220
221     int encoded;
222     int idx;
223     short dirty = 0;   /* 1 when we may have created empty parameters. */
224
225     if (!headp)
226         return;
227
228     purge_empty_parameters (headp);
229
230     for (last = &head, p = *headp; p; p = q) {
231         q = p->next;
232
233         if (!(s = strchr (p->attribute, '*'))) {
234
235             /* 
236              * Using RFC 2047 encoding in MIME parameters is explicitly
237              * forbidden by that document.  Nevertheless, it's being
238              * generated by some software, including certain Lotus Notes to 
239              * Internet Gateways.  So we actually decode it.
240              */
241
242             if (option (OPTRFC2047PARAMS) && p->value && strstr (p->value, "=?"))
243                 rfc2047_decode (&p->value);
244             else if (!option (OPTSTRICTMIME)) {
245                 if (mime_which_token(AssumedCharset, -1) == MIME_US_ASCII)
246                     mutt_convert_nonmime_string(&p->value);
247             }
248
249             *last = p;
250             last = &p->next;
251             p->next = NULL;
252         }
253         else if (*(s + 1) == '\0') {
254             *s = '\0';
255
256             s = rfc2231_get_charset (p->value, charset, sizeof (charset));
257             rfc2231_decode_one (p->value, s);
258             mutt_convert_string (&p->value, charset, Charset, M_ICONV_HOOK_FROM);
259
260             *last = p;
261             last = &p->next;
262             p->next = NULL;
263
264             dirty = 1;
265         }
266         else {
267             *s = '\0';
268             s++;                      /* let s point to the first character of idx. */
269             for (t = s; *t && isdigit ((unsigned char) *t); t++);
270             encoded = (*t == '*');
271             *t = '\0';
272
273             idx = atoi (s);
274
275             conttmp = rfc2231_param_new ();
276             conttmp->attribute = p->attribute;
277             conttmp->value = p->value;
278             conttmp->encoded = encoded;
279             conttmp->idx = idx;
280
281             p->attribute = NULL;
282             p->value = NULL;
283             p_delete(&p);
284
285             rfc2231_list_insert (&conthead, conttmp);
286         }
287     }
288
289     if (conthead) {
290         rfc2231_join_continuations (last, conthead);
291         dirty = 1;
292     }
293
294     *headp = head;
295
296     if (dirty)
297         purge_empty_parameters (headp);
298 }
299
300 #define RFC2231_SPECIALS  "@.,;:<>[]\\\"()?/= \t*'%"
301
302 int rfc2231_encode_string(char **s)
303 {
304     char *charset = NULL;
305     char *e, *p, *t, *d = NULL;
306     int escapes = 0;
307     ssize_t dlen = 0;
308
309     /*
310      * A shortcut to detect pure 7bit data.
311      *
312      * This should prevent the worst when character set handling is flawed.
313      */
314
315     for (p = *s; ; p++) {
316         if (*p & 0x80)
317             break;
318         if (!*p)
319             return 0;
320     }
321
322     if (Charset && SendCharset) {
323         charset = mutt_choose_charset(Charset, SendCharset,
324                                       *s, m_strlen(*s), &d, &dlen);
325     }
326
327     if (!charset) {
328         charset = m_strdup(Charset ? Charset : "unknown-8bit");
329         d = *s;
330         dlen = m_strlen(d);
331     }
332
333     for (p = d; *p; p++) {
334         if (*p < 0x20 || *p >= 0x7f || strchr(RFC2231_SPECIALS, *p)) {
335             ++escapes;
336         }
337     }
338
339     e = p_new(char, dlen + 2 * escapes + m_strlen(charset) + 3);
340
341     t = e + sprintf(e, "%s''", charset);
342     for (p = d; *p; p++) {
343         if (*p < 0x20 || *p >= 0x7f || strchr(RFC2231_SPECIALS, *p)) {
344             *t++ = '%';
345             *t++ = __m_b36chars_upper[*p >> 4];
346             *t++ = __m_b36chars_upper[*p & 0xf];
347         } else {
348             *t++ = *p;
349         }
350     }
351     *t = '\0';
352
353     if (d != *s)
354         p_delete(&d);
355     p_delete(s);
356     p_delete(&charset);
357
358     *s = e;
359     return 1;
360 }