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