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>
52 typedef struct rfc2231_parameter {
57 struct rfc2231_parameter *next;
60 DO_INIT(rfc2231_parameter, rfc2231_parameter);
61 static inline void rfc2231_parameter_wipe(rfc2231_parameter *param)
63 p_delete(¶m->attribute);
64 p_delete(¶m->value);
66 DO_NEW(rfc2231_parameter, rfc2231_parameter);
67 DO_DELETE(rfc2231_parameter, rfc2231_parameter);
70 /* insert parameter into an ordered list.
72 * Primary sorting key: attribute
73 * Secondary sorting key: idx
76 rfc2231_list_insert(rfc2231_parameter **list, rfc2231_parameter *par)
78 rfc2231_parameter **last = list;
79 rfc2231_parameter *p = *list, *q;
87 c = m_strcmp(par->value, q->value);
88 if ((c > 0) || (c == 0 && par->idx >= q->idx))
97 static void rfc2231_decode_one(char *dst, const char *src)
103 && (h1 = hexval(src[1])) >= 0 && (h2 = hexval(src[2])) >= 0)
105 *dst++ = (h1 << 4) | h2;
115 /* ---------------- TODO FIXME READ MARK ---------------- */
117 static char *rfc2231_get_charset(char *value, char *charset, size_t chslen)
121 t = strchr(value, '\'');
128 m_strcpy(charset, chslen, value);
130 if ((u = strchr(t + 1, '\''))) {
138 static void rfc2231_join_continuations(PARAMETER **, rfc2231_parameter *);
140 static void purge_empty_parameters (PARAMETER **headp)
142 PARAMETER *p, *q, **last;
144 for (last = headp, p = *headp; p; p = q) {
146 if (!p->attribute || !p->value) {
149 mutt_free_parameter (&p);
157 void rfc2231_decode_parameters (PARAMETER ** headp)
159 PARAMETER *head = NULL;
163 rfc2231_parameter *conthead = NULL;
164 rfc2231_parameter *conttmp;
167 char charset[STRING];
171 short dirty = 0; /* set to 1 when we may have created
178 purge_empty_parameters (headp);
180 for (last = &head, p = *headp; p; p = q) {
183 if (!(s = strchr (p->attribute, '*'))) {
186 * Using RFC 2047 encoding in MIME parameters is explicitly
187 * forbidden by that document. Nevertheless, it's being
188 * generated by some software, including certain Lotus Notes to
189 * Internet Gateways. So we actually decode it.
192 if (option (OPTRFC2047PARAMS) && p->value && strstr (p->value, "=?"))
193 rfc2047_decode (&p->value);
194 else if (!option (OPTSTRICTMIME)) {
195 if (ascii_strcasecmp (AssumedCharset, "us-ascii"))
196 mutt_convert_nonmime_string (&p->value);
203 else if (*(s + 1) == '\0') {
206 s = rfc2231_get_charset (p->value, charset, sizeof (charset));
207 rfc2231_decode_one (p->value, s);
208 mutt_convert_string (&p->value, charset, Charset, M_ICONV_HOOK_FROM);
218 s++; /* let s point to the first character of idx. */
219 for (t = s; *t && isdigit ((unsigned char) *t); t++);
220 encoded = (*t == '*');
225 conttmp = rfc2231_parameter_new ();
226 conttmp->attribute = p->attribute;
227 conttmp->value = p->value;
228 conttmp->encoded = encoded;
235 rfc2231_list_insert (&conthead, conttmp);
240 rfc2231_join_continuations (last, conthead);
247 purge_empty_parameters (headp);
250 /* process continuation parameters */
253 rfc2231_join_continuations(PARAMETER **head, rfc2231_parameter *par)
255 rfc2231_parameter *q;
257 char attribute[STRING];
258 char charset[STRING];
269 m_strcpy(attribute, sizeof(attribute), par->attribute);
271 if ((encoded = par->encoded))
272 valp = rfc2231_get_charset (par->value, charset, sizeof (charset));
277 if (encoded && par->encoded)
278 rfc2231_decode_one (par->value, valp);
280 vl = m_strlen(par->value);
282 p_realloc(&value, l + vl + 1);
283 strcpy (value + l, par->value); /* __STRCPY_CHECKED__ */
287 rfc2231_parameter_delete (&par);
290 } while (par && !m_strcmp(par->attribute, attribute));
294 mutt_convert_string (&value, charset, Charset, M_ICONV_HOOK_FROM);
295 *head = mutt_new_parameter ();
296 (*head)->attribute = m_strdup(attribute);
297 (*head)->value = value;
298 head = &(*head)->next;
303 int rfc2231_encode_string (char **pd)
305 int ext = 0, encode = 0;
306 char *charset, *s, *t, *e, *d = 0;
307 size_t slen, dlen = 0;
310 * A shortcut to detect pure 7bit data.
312 * This should prevent the worst when character set handling
316 for (s = *pd; *s; s++)
323 if (!Charset || !SendCharset ||
324 !(charset = mutt_choose_charset (Charset, SendCharset,
325 *pd, m_strlen(*pd), &d, &dlen))) {
326 charset = m_strdup(Charset ? Charset : "unknown-8bit");
331 if (!mutt_is_us_ascii (charset))
334 for (s = d, slen = dlen; slen; s++, slen--)
335 if (*s < 0x20 || *s >= 0x7f)
337 else if (strchr (MimeSpecials, *s) || strchr ("*'%", *s))
341 e = p_new(char, dlen + 2 * ext + m_strlen(charset) + 3);
342 sprintf (e, "%s''", charset); /* __SPRINTF_CHECKED__ */
344 for (s = d, slen = dlen; slen; s++, slen--)
345 if (*s < 0x20 || *s >= 0x7f ||
346 strchr (MimeSpecials, *s) || strchr ("*'%", *s)) {
347 sprintf (t, "%%%02X", (unsigned char) *s);