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