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.
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.
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,
17 * Copyright © 2006 Pierre Habouzit
21 * Copyright notice from original mutt:
22 * Copyright (C) 1999-2000 Thomas Roessler <roessler@does-not-exist.org>
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.
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.
34 * Additionally, continuations and encoding are mixed in an, errrm,
43 #include <lib-lib/mem.h>
44 #include <lib-lib/str.h>
45 #include <lib-lib/ascii.h>
47 #include <lib-mime/mime.h>
51 typedef struct rfc2231_param {
52 struct rfc2231_param *next;
60 DO_INIT(rfc2231_param, rfc2231_param);
61 static inline void rfc2231_param_wipe(rfc2231_param *param)
63 p_delete(¶m->attribute);
64 p_delete(¶m->value);
66 DO_NEW(rfc2231_param, rfc2231_param);
67 DO_DELETE(rfc2231_param, rfc2231_param);
69 /* TODO: MC: replace with a str_unescape */
70 static void rfc2231_decode_one(char *dst, const char *src)
76 && (h1 = hexval(src[1])) >= 0 && (h2 = hexval(src[2])) >= 0)
78 *dst++ = (h1 << 4) | h2;
88 /* read the <charset>'foo' part into charset, and skip that */
89 static char *rfc2231_get_charset(char *value, char *charset, size_t chslen)
93 t = strchr(value, '\'');
100 m_strcpy(charset, chslen, value);
102 if ((u = strchr(t + 1, '\''))) {
110 /* insert parameter into an ordered list.
112 * Primary sorting key: attribute
113 * Secondary sorting key: idx
115 * XXX: MC: looks very unclear to me
118 rfc2231_list_insert(rfc2231_param **list, rfc2231_param *par)
123 rfc2231_param *q = *list;
125 list = &(*list)->next;
127 c = m_strcmp(par->value, q->value);
128 if ((c > 0) || (c == 0 && par->idx >= q->idx))
136 static void purge_empty_parameters(PARAMETER **headp)
139 PARAMETER *p = *headp;
141 if (!p->attribute || !p->value) {
144 parameter_list_wipe(&p);
146 headp = &(*headp)->next;
151 /* process continuation parameters */
152 /* XXX: MC: not read */
154 rfc2231_join_continuations(PARAMETER **head, rfc2231_param *par)
158 char attribute[STRING];
159 char charset[STRING];
170 m_strcpy(attribute, sizeof(attribute), par->attribute);
172 if ((encoded = par->encoded))
173 valp = rfc2231_get_charset (par->value, charset, sizeof (charset));
178 if (encoded && par->encoded)
179 rfc2231_decode_one (par->value, valp);
181 vl = m_strlen(par->value);
183 p_realloc(&value, l + vl + 1);
184 strcpy (value + l, par->value); /* __STRCPY_CHECKED__ */
188 rfc2231_param_delete (&par);
191 } while (par && !m_strcmp(par->attribute, attribute));
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;
204 /****************************************************************************/
206 /****************************************************************************/
208 /* XXX: MC: not read */
209 void rfc2231_decode_parameters (PARAMETER ** headp)
211 PARAMETER *head = NULL;
215 rfc2231_param *conthead = NULL;
216 rfc2231_param *conttmp;
219 char charset[STRING];
223 short dirty = 0; /* 1 when we may have created empty parameters. */
228 purge_empty_parameters (headp);
230 for (last = &head, p = *headp; p; p = q) {
233 if (!(s = strchr (p->attribute, '*'))) {
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.
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);
253 else if (*(s + 1) == '\0') {
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);
268 s++; /* let s point to the first character of idx. */
269 for (t = s; *t && isdigit ((unsigned char) *t); t++);
270 encoded = (*t == '*');
275 conttmp = rfc2231_param_new ();
276 conttmp->attribute = p->attribute;
277 conttmp->value = p->value;
278 conttmp->encoded = encoded;
285 rfc2231_list_insert (&conthead, conttmp);
290 rfc2231_join_continuations (last, conthead);
297 purge_empty_parameters (headp);
300 #define RFC2231_SPECIALS "@.,;:<>[]\\\"()?/= \t*'%"
302 int rfc2231_encode_string(char **s)
304 char *charset = NULL;
305 char *e, *p, *t, *d = NULL;
310 * A shortcut to detect pure 7bit data.
312 * This should prevent the worst when character set handling is flawed.
315 for (p = *s; ; p++) {
322 if (Charset && SendCharset) {
323 charset = mutt_choose_charset(Charset, SendCharset,
324 *s, m_strlen(*s), &d, &dlen);
328 charset = m_strdup(Charset ? Charset : "unknown-8bit");
333 for (p = d; *p; p++) {
334 if (*p < 0x20 || *p >= 0x7f || strchr(RFC2231_SPECIALS, *p)) {
339 e = p_new(char, dlen + 2 * escapes + m_strlen(charset) + 3);
341 t = e + sprintf(e, "%s''", charset);
342 for (p = d; *p; p++) {
343 if (*p < 0x20 || *p >= 0x7f || strchr(RFC2231_SPECIALS, *p)) {
345 *t++ = __m_b36chars_upper[*p >> 4];
346 *t++ = __m_b36chars_upper[*p & 0xf];