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>
57 struct rfc2231_parameter {
62 struct rfc2231_parameter
66 static char *rfc2231_get_charset (char *, char *, size_t);
67 static struct rfc2231_parameter *rfc2231_new_parameter (void);
68 static void rfc2231_decode_one (char *, char *);
69 static void rfc2231_free_parameter (struct rfc2231_parameter **);
70 static void rfc2231_join_continuations (PARAMETER **,
71 struct rfc2231_parameter *);
72 static void rfc2231_list_insert (struct rfc2231_parameter **,
73 struct rfc2231_parameter *);
75 static void purge_empty_parameters (PARAMETER ** headp)
77 PARAMETER *p, *q, **last;
79 for (last = headp, p = *headp; p; p = q) {
81 if (!p->attribute || !p->value) {
84 mutt_free_parameter (&p);
92 void rfc2231_decode_parameters (PARAMETER ** headp)
94 PARAMETER *head = NULL;
98 struct rfc2231_parameter *conthead = NULL;
99 struct rfc2231_parameter *conttmp;
102 char charset[STRING];
106 short dirty = 0; /* set to 1 when we may have created
113 purge_empty_parameters (headp);
115 for (last = &head, p = *headp; p; p = q) {
118 if (!(s = strchr (p->attribute, '*'))) {
121 * Using RFC 2047 encoding in MIME parameters is explicitly
122 * forbidden by that document. Nevertheless, it's being
123 * generated by some software, including certain Lotus Notes to
124 * Internet Gateways. So we actually decode it.
127 if (option (OPTRFC2047PARAMS) && p->value && strstr (p->value, "=?"))
128 rfc2047_decode (&p->value);
129 else if (!option (OPTSTRICTMIME)) {
130 if (ascii_strcasecmp (AssumedCharset, "us-ascii"))
131 mutt_convert_nonmime_string (&p->value);
138 else if (*(s + 1) == '\0') {
141 s = rfc2231_get_charset (p->value, charset, sizeof (charset));
142 rfc2231_decode_one (p->value, s);
143 mutt_convert_string (&p->value, charset, Charset, M_ICONV_HOOK_FROM);
153 s++; /* let s point to the first character of index. */
154 for (t = s; *t && isdigit ((unsigned char) *t); t++);
155 encoded = (*t == '*');
160 conttmp = rfc2231_new_parameter ();
161 conttmp->attribute = p->attribute;
162 conttmp->value = p->value;
163 conttmp->encoded = encoded;
164 conttmp->index = index;
170 rfc2231_list_insert (&conthead, conttmp);
175 rfc2231_join_continuations (last, conthead);
182 purge_empty_parameters (headp);
185 static struct rfc2231_parameter *rfc2231_new_parameter (void)
187 return p_new(struct rfc2231_parameter, 1);
190 static void rfc2231_free_parameter (struct rfc2231_parameter **p)
193 p_delete(&(*p)->attribute);
194 p_delete(&(*p)->value);
199 static char *rfc2231_get_charset (char *value, char *charset, size_t chslen)
203 if (!(t = strchr (value, '\''))) {
209 m_strcpy(charset, chslen, value);
211 if ((u = strchr (t + 1, '\'')))
217 static void rfc2231_decode_one (char *dest, char *src)
221 for (d = dest; *src; src++) {
223 isxdigit ((unsigned char) *(src + 1)) &&
224 isxdigit ((unsigned char) *(src + 2))) {
225 *d++ = (hexval (*(src + 1)) << 4) | (hexval (*(src + 2)));
235 /* insert parameter into an ordered list.
237 * Primary sorting key: attribute
238 * Secondary sorting key: index
241 static void rfc2231_list_insert (struct rfc2231_parameter **list,
242 struct rfc2231_parameter *par)
244 struct rfc2231_parameter **last = list;
245 struct rfc2231_parameter *p = *list, *q;
253 c = m_strcmp(par->value, q->value);
254 if ((c > 0) || (c == 0 && par->index >= q->index))
262 /* process continuation parameters */
264 static void rfc2231_join_continuations (PARAMETER ** head,
265 struct rfc2231_parameter *par)
267 struct rfc2231_parameter *q;
269 char attribute[STRING];
270 char charset[STRING];
281 m_strcpy(attribute, sizeof(attribute), par->attribute);
283 if ((encoded = par->encoded))
284 valp = rfc2231_get_charset (par->value, charset, sizeof (charset));
289 if (encoded && par->encoded)
290 rfc2231_decode_one (par->value, valp);
292 vl = m_strlen(par->value);
294 p_realloc(&value, l + vl + 1);
295 strcpy (value + l, par->value); /* __STRCPY_CHECKED__ */
299 rfc2231_free_parameter (&par);
302 } while (par && !m_strcmp(par->attribute, attribute));
306 mutt_convert_string (&value, charset, Charset, M_ICONV_HOOK_FROM);
307 *head = mutt_new_parameter ();
308 (*head)->attribute = m_strdup(attribute);
309 (*head)->value = value;
310 head = &(*head)->next;
315 int rfc2231_encode_string (char **pd)
317 int ext = 0, encode = 0;
318 char *charset, *s, *t, *e, *d = 0;
319 size_t slen, dlen = 0;
322 * A shortcut to detect pure 7bit data.
324 * This should prevent the worst when character set handling
328 for (s = *pd; *s; s++)
335 if (!Charset || !SendCharset ||
336 !(charset = mutt_choose_charset (Charset, SendCharset,
337 *pd, m_strlen(*pd), &d, &dlen))) {
338 charset = m_strdup(Charset ? Charset : "unknown-8bit");
343 if (!mutt_is_us_ascii (charset))
346 for (s = d, slen = dlen; slen; s++, slen--)
347 if (*s < 0x20 || *s >= 0x7f)
349 else if (strchr (MimeSpecials, *s) || strchr ("*'%", *s))
353 e = p_new(char, dlen + 2 * ext + m_strlen(charset) + 3);
354 sprintf (e, "%s''", charset); /* __SPRINTF_CHECKED__ */
356 for (s = d, slen = dlen; slen; s++, slen--)
357 if (*s < 0x20 || *s >= 0x7f ||
358 strchr (MimeSpecials, *s) || strchr ("*'%", *s)) {
359 sprintf (t, "%%%02X", (unsigned char) *s);