2 * Copyright notice from original mutt:
3 * Copyright (C) 1999-2000 Thomas Roessler <roessler@does-not-exist.org>
5 * This file is part of mutt-ng, see http://www.muttng.org/.
6 * It's licensed under the GNU General Public License,
7 * please see the file GPL in the top level source directory.
11 * Yet another MIME encoding for header data. This time, it's
12 * parameters, specified in RFC 2231, and modeled after the
13 * encoding used in URLs.
15 * Additionally, continuations and encoding are mixed in an, errrm,
24 #include <lib-lib/mem.h>
40 struct rfc2231_parameter {
45 struct rfc2231_parameter
49 static char *rfc2231_get_charset (char *, char *, size_t);
50 static struct rfc2231_parameter *rfc2231_new_parameter (void);
51 static void rfc2231_decode_one (char *, char *);
52 static void rfc2231_free_parameter (struct rfc2231_parameter **);
53 static void rfc2231_join_continuations (PARAMETER **,
54 struct rfc2231_parameter *);
55 static void rfc2231_list_insert (struct rfc2231_parameter **,
56 struct rfc2231_parameter *);
58 static void purge_empty_parameters (PARAMETER ** headp)
60 PARAMETER *p, *q, **last;
62 for (last = headp, p = *headp; p; p = q) {
64 if (!p->attribute || !p->value) {
67 mutt_free_parameter (&p);
75 void rfc2231_decode_parameters (PARAMETER ** headp)
77 PARAMETER *head = NULL;
81 struct rfc2231_parameter *conthead = NULL;
82 struct rfc2231_parameter *conttmp;
89 short dirty = 0; /* set to 1 when we may have created
96 purge_empty_parameters (headp);
98 for (last = &head, p = *headp; p; p = q) {
101 if (!(s = strchr (p->attribute, '*'))) {
104 * Using RFC 2047 encoding in MIME parameters is explicitly
105 * forbidden by that document. Nevertheless, it's being
106 * generated by some software, including certain Lotus Notes to
107 * Internet Gateways. So we actually decode it.
110 if (option (OPTRFC2047PARAMS) && p->value && strstr (p->value, "=?"))
111 rfc2047_decode (&p->value);
112 else if (!option (OPTSTRICTMIME)) {
113 if (ascii_strcasecmp (AssumedCharset, "us-ascii"))
114 mutt_convert_nonmime_string (&p->value);
121 else if (*(s + 1) == '\0') {
124 s = rfc2231_get_charset (p->value, charset, sizeof (charset));
125 rfc2231_decode_one (p->value, s);
126 mutt_convert_string (&p->value, charset, Charset, M_ICONV_HOOK_FROM);
136 s++; /* let s point to the first character of index. */
137 for (t = s; *t && isdigit ((unsigned char) *t); t++);
138 encoded = (*t == '*');
143 conttmp = rfc2231_new_parameter ();
144 conttmp->attribute = p->attribute;
145 conttmp->value = p->value;
146 conttmp->encoded = encoded;
147 conttmp->index = index;
153 rfc2231_list_insert (&conthead, conttmp);
158 rfc2231_join_continuations (last, conthead);
165 purge_empty_parameters (headp);
168 static struct rfc2231_parameter *rfc2231_new_parameter (void)
170 return p_new(struct rfc2231_parameter, 1);
173 static void rfc2231_free_parameter (struct rfc2231_parameter **p)
176 p_delete(&(*p)->attribute);
177 p_delete(&(*p)->value);
182 static char *rfc2231_get_charset (char *value, char *charset, size_t chslen)
186 if (!(t = strchr (value, '\''))) {
192 strfcpy (charset, value, chslen);
194 if ((u = strchr (t + 1, '\'')))
200 static void rfc2231_decode_one (char *dest, char *src)
204 for (d = dest; *src; src++) {
206 isxdigit ((unsigned char) *(src + 1)) &&
207 isxdigit ((unsigned char) *(src + 2))) {
208 *d++ = (hexval (*(src + 1)) << 4) | (hexval (*(src + 2)));
218 /* insert parameter into an ordered list.
220 * Primary sorting key: attribute
221 * Secondary sorting key: index
224 static void rfc2231_list_insert (struct rfc2231_parameter **list,
225 struct rfc2231_parameter *par)
227 struct rfc2231_parameter **last = list;
228 struct rfc2231_parameter *p = *list, *q;
236 c = str_cmp (par->value, q->value);
237 if ((c > 0) || (c == 0 && par->index >= q->index))
245 /* process continuation parameters */
247 static void rfc2231_join_continuations (PARAMETER ** head,
248 struct rfc2231_parameter *par)
250 struct rfc2231_parameter *q;
252 char attribute[STRING];
253 char charset[STRING];
264 strfcpy (attribute, par->attribute, sizeof (attribute));
266 if ((encoded = par->encoded))
267 valp = rfc2231_get_charset (par->value, charset, sizeof (charset));
272 if (encoded && par->encoded)
273 rfc2231_decode_one (par->value, valp);
275 vl = str_len (par->value);
277 mem_realloc (&value, l + vl + 1);
278 strcpy (value + l, par->value); /* __STRCPY_CHECKED__ */
282 rfc2231_free_parameter (&par);
285 } while (par && !str_cmp (par->attribute, attribute));
289 mutt_convert_string (&value, charset, Charset, M_ICONV_HOOK_FROM);
290 *head = mutt_new_parameter ();
291 (*head)->attribute = str_dup (attribute);
292 (*head)->value = value;
293 head = &(*head)->next;
298 int rfc2231_encode_string (char **pd)
300 int ext = 0, encode = 0;
301 char *charset, *s, *t, *e, *d = 0;
302 size_t slen, dlen = 0;
305 * A shortcut to detect pure 7bit data.
307 * This should prevent the worst when character set handling
311 for (s = *pd; *s; s++)
318 if (!Charset || !SendCharset ||
319 !(charset = mutt_choose_charset (Charset, SendCharset,
320 *pd, str_len (*pd), &d, &dlen))) {
321 charset = str_dup (Charset ? Charset : "unknown-8bit");
326 if (!mutt_is_us_ascii (charset))
329 for (s = d, slen = dlen; slen; s++, slen--)
330 if (*s < 0x20 || *s >= 0x7f)
332 else if (strchr (MimeSpecials, *s) || strchr ("*'%", *s))
336 e = p_new(char, dlen + 2 * ext + str_len(charset) + 3);
337 sprintf (e, "%s''", charset); /* __SPRINTF_CHECKED__ */
339 for (s = d, slen = dlen; slen; s++, slen--)
340 if (*s < 0x20 || *s >= 0x7f ||
341 strchr (MimeSpecials, *s) || strchr ("*'%", *s)) {
342 sprintf (t, "%%%02X", (unsigned char) *s);