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>
25 #include <lib-lib/str.h>
38 struct rfc2231_parameter {
43 struct rfc2231_parameter
47 static char *rfc2231_get_charset (char *, char *, size_t);
48 static struct rfc2231_parameter *rfc2231_new_parameter (void);
49 static void rfc2231_decode_one (char *, char *);
50 static void rfc2231_free_parameter (struct rfc2231_parameter **);
51 static void rfc2231_join_continuations (PARAMETER **,
52 struct rfc2231_parameter *);
53 static void rfc2231_list_insert (struct rfc2231_parameter **,
54 struct rfc2231_parameter *);
56 static void purge_empty_parameters (PARAMETER ** headp)
58 PARAMETER *p, *q, **last;
60 for (last = headp, p = *headp; p; p = q) {
62 if (!p->attribute || !p->value) {
65 mutt_free_parameter (&p);
73 void rfc2231_decode_parameters (PARAMETER ** headp)
75 PARAMETER *head = NULL;
79 struct rfc2231_parameter *conthead = NULL;
80 struct rfc2231_parameter *conttmp;
87 short dirty = 0; /* set to 1 when we may have created
94 purge_empty_parameters (headp);
96 for (last = &head, p = *headp; p; p = q) {
99 if (!(s = strchr (p->attribute, '*'))) {
102 * Using RFC 2047 encoding in MIME parameters is explicitly
103 * forbidden by that document. Nevertheless, it's being
104 * generated by some software, including certain Lotus Notes to
105 * Internet Gateways. So we actually decode it.
108 if (option (OPTRFC2047PARAMS) && p->value && strstr (p->value, "=?"))
109 rfc2047_decode (&p->value);
110 else if (!option (OPTSTRICTMIME)) {
111 if (ascii_strcasecmp (AssumedCharset, "us-ascii"))
112 mutt_convert_nonmime_string (&p->value);
119 else if (*(s + 1) == '\0') {
122 s = rfc2231_get_charset (p->value, charset, sizeof (charset));
123 rfc2231_decode_one (p->value, s);
124 mutt_convert_string (&p->value, charset, Charset, M_ICONV_HOOK_FROM);
134 s++; /* let s point to the first character of index. */
135 for (t = s; *t && isdigit ((unsigned char) *t); t++);
136 encoded = (*t == '*');
141 conttmp = rfc2231_new_parameter ();
142 conttmp->attribute = p->attribute;
143 conttmp->value = p->value;
144 conttmp->encoded = encoded;
145 conttmp->index = index;
151 rfc2231_list_insert (&conthead, conttmp);
156 rfc2231_join_continuations (last, conthead);
163 purge_empty_parameters (headp);
166 static struct rfc2231_parameter *rfc2231_new_parameter (void)
168 return p_new(struct rfc2231_parameter, 1);
171 static void rfc2231_free_parameter (struct rfc2231_parameter **p)
174 p_delete(&(*p)->attribute);
175 p_delete(&(*p)->value);
180 static char *rfc2231_get_charset (char *value, char *charset, size_t chslen)
184 if (!(t = strchr (value, '\''))) {
190 strfcpy (charset, value, chslen);
192 if ((u = strchr (t + 1, '\'')))
198 static void rfc2231_decode_one (char *dest, char *src)
202 for (d = dest; *src; src++) {
204 isxdigit ((unsigned char) *(src + 1)) &&
205 isxdigit ((unsigned char) *(src + 2))) {
206 *d++ = (hexval (*(src + 1)) << 4) | (hexval (*(src + 2)));
216 /* insert parameter into an ordered list.
218 * Primary sorting key: attribute
219 * Secondary sorting key: index
222 static void rfc2231_list_insert (struct rfc2231_parameter **list,
223 struct rfc2231_parameter *par)
225 struct rfc2231_parameter **last = list;
226 struct rfc2231_parameter *p = *list, *q;
234 c = str_cmp (par->value, q->value);
235 if ((c > 0) || (c == 0 && par->index >= q->index))
243 /* process continuation parameters */
245 static void rfc2231_join_continuations (PARAMETER ** head,
246 struct rfc2231_parameter *par)
248 struct rfc2231_parameter *q;
250 char attribute[STRING];
251 char charset[STRING];
262 strfcpy (attribute, par->attribute, sizeof (attribute));
264 if ((encoded = par->encoded))
265 valp = rfc2231_get_charset (par->value, charset, sizeof (charset));
270 if (encoded && par->encoded)
271 rfc2231_decode_one (par->value, valp);
273 vl = m_strlen(par->value);
275 p_realloc(&value, l + vl + 1);
276 strcpy (value + l, par->value); /* __STRCPY_CHECKED__ */
280 rfc2231_free_parameter (&par);
283 } while (par && !str_cmp (par->attribute, attribute));
287 mutt_convert_string (&value, charset, Charset, M_ICONV_HOOK_FROM);
288 *head = mutt_new_parameter ();
289 (*head)->attribute = m_strdup(attribute);
290 (*head)->value = value;
291 head = &(*head)->next;
296 int rfc2231_encode_string (char **pd)
298 int ext = 0, encode = 0;
299 char *charset, *s, *t, *e, *d = 0;
300 size_t slen, dlen = 0;
303 * A shortcut to detect pure 7bit data.
305 * This should prevent the worst when character set handling
309 for (s = *pd; *s; s++)
316 if (!Charset || !SendCharset ||
317 !(charset = mutt_choose_charset (Charset, SendCharset,
318 *pd, m_strlen(*pd), &d, &dlen))) {
319 charset = m_strdup(Charset ? Charset : "unknown-8bit");
324 if (!mutt_is_us_ascii (charset))
327 for (s = d, slen = dlen; slen; s++, slen--)
328 if (*s < 0x20 || *s >= 0x7f)
330 else if (strchr (MimeSpecials, *s) || strchr ("*'%", *s))
334 e = p_new(char, dlen + 2 * ext + m_strlen(charset) + 3);
335 sprintf (e, "%s''", charset); /* __SPRINTF_CHECKED__ */
337 for (s = d, slen = dlen; slen; s++, slen--)
338 if (*s < 0x20 || *s >= 0x7f ||
339 strchr (MimeSpecials, *s) || strchr ("*'%", *s)) {
340 sprintf (t, "%%%02X", (unsigned char) *s);