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,
39 #include <lib-lib/lib-lib.h>
41 #include <lib-mime/mime.h>
46 typedef struct rfc2231_param {
47 struct rfc2231_param *next;
55 DO_INIT(rfc2231_param, rfc2231_param);
56 static inline void rfc2231_param_wipe(rfc2231_param *param)
58 p_delete(¶m->attribute);
59 p_delete(¶m->value);
61 DO_NEW(rfc2231_param, rfc2231_param);
62 DO_DELETE(rfc2231_param, rfc2231_param);
64 /* TODO: MC: replace with a str_unescape */
65 static void rfc2231_decode_one(char *dst, const char *src)
71 && (h1 = hexval(src[1])) >= 0 && (h2 = hexval(src[2])) >= 0)
73 *dst++ = (h1 << 4) | h2;
83 /* read the <charset>'foo' part into charset, and skip that */
84 static char *rfc2231_get_charset(char *value, char *charset, size_t chslen)
88 t = strchr(value, '\'');
95 m_strcpy(charset, chslen, value);
97 if ((u = strchr(t + 1, '\''))) {
105 /* insert parameter into an ordered list.
107 * Primary sorting key: attribute
108 * Secondary sorting key: idx
110 * XXX: MC: looks very unclear to me
113 rfc2231_list_insert(rfc2231_param **list, rfc2231_param *par)
118 rfc2231_param *q = *list;
120 list = &(*list)->next;
122 c = m_strcmp(par->value, q->value);
123 if ((c > 0) || (c == 0 && par->idx >= q->idx))
131 /* process continuation parameters */
132 /* XXX: MC: not read */
134 rfc2231_join_continuations(parameters_t *head, rfc2231_param *par)
138 char attribute[STRING];
139 char charset[STRING];
149 m_strcpy(attribute, sizeof(attribute), par->attribute);
151 if ((encoded = par->encoded))
152 valp = rfc2231_get_charset (par->value, charset, sizeof (charset));
157 if (encoded && par->encoded)
158 rfc2231_decode_one (par->value, valp);
160 vl = m_strlen(par->value);
162 p_realloc(&value, l + vl + 1);
163 m_strcpy(value + l, vl + 1, par->value);
167 rfc2231_param_delete (&par);
170 } while (par && !m_strcmp(par->attribute, attribute));
175 mutt_convert_string (&value, charset, mod_cset.charset, M_ICONV_HOOK_FROM);
176 parameter_setval(head, attribute, value);
181 /****************************************************************************/
183 /****************************************************************************/
185 /* XXX: MC: not read */
186 void rfc2231_decode_parameters(parameters_t *headp)
189 parameter_t *head = NULL;
193 rfc2231_param *conthead = NULL;
194 rfc2231_param *conttmp;
197 char charset[STRING];
201 short dirty = 0; /* 1 when we may have created empty parameters. */
206 parameter_purge_empty(headp);
208 for (last = &head, p = *headp; p; p = q) {
211 if (!(s = strchr (p->attribute, '*'))) {
214 * Using RFC 2047 encoding in MIME parameters is explicitly
215 * forbidden by that document. Nevertheless, it's being
216 * generated by some software, including certain Lotus Notes to
217 * Internet Gateways. So we actually decode it.
220 if (p->value && strstr(p->value, "=?")) {
221 rfc2047_decode(&p->value);
223 if (mime_which_token(mod_cset.assumed_charset, -1) == MIME_US_ASCII)
224 mutt_convert_nonmime_string(&p->value);
231 else if (*(s + 1) == '\0') {
234 s = rfc2231_get_charset (p->value, charset, sizeof (charset));
235 rfc2231_decode_one (p->value, s);
236 mutt_convert_string (&p->value, charset, mod_cset.charset, M_ICONV_HOOK_FROM);
246 s++; /* let s point to the first character of idx. */
247 for (t = s; *t && isdigit ((unsigned char) *t); t++);
248 encoded = (*t == '*');
253 conttmp = rfc2231_param_new ();
254 conttmp->attribute = p->attribute;
255 conttmp->value = p->value;
256 conttmp->encoded = encoded;
263 rfc2231_list_insert (&conthead, conttmp);
268 rfc2231_join_continuations (last, conthead);
275 parameter_purge_empty(headp);
279 #define RFC2231_SPECIALS "@.,;:<>[]\\\"()?/= \t*'%"
281 int rfc2231_encode_string(char **s)
283 char *charset = NULL;
284 char *e, *p, *t, *d = NULL;
289 * A shortcut to detect pure 7bit data.
291 * This should prevent the worst when character set handling is flawed.
294 for (p = *s; ; p++) {
301 if (mod_cset.charset && mod_cset.send_charset) {
302 charset = mutt_choose_charset(mod_cset.charset, mod_cset.send_charset,
303 *s, m_strlen(*s), &d, &dlen);
307 charset = m_strdup(mod_cset.charset ?: "unknown-8bit");
312 for (p = d; *p; p++) {
313 if (*p < 0x20 || *p >= 0x7f || strchr(RFC2231_SPECIALS, *p)) {
318 e = p_new(char, dlen + 2 * escapes + m_strlen(charset) + 3);
320 t = e + sprintf(e, "%s''", charset);
321 for (p = d; *p; p++) {
322 if (*p < 0x20 || *p >= 0x7f || strchr(RFC2231_SPECIALS, *p)) {
324 *t++ = __m_b36chars_upper[*p >> 4];
325 *t++ = __m_b36chars_upper[*p & 0xf];