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,
37 struct rfc2231_parameter {
42 struct rfc2231_parameter
46 static char *rfc2231_get_charset (char *, char *, size_t);
47 static struct rfc2231_parameter *rfc2231_new_parameter (void);
48 static void rfc2231_decode_one (char *, char *);
49 static void rfc2231_free_parameter (struct rfc2231_parameter **);
50 static void rfc2231_join_continuations (PARAMETER **,
51 struct rfc2231_parameter *);
52 static void rfc2231_list_insert (struct rfc2231_parameter **,
53 struct rfc2231_parameter *);
55 static void purge_empty_parameters (PARAMETER ** headp)
57 PARAMETER *p, *q, **last;
59 for (last = headp, p = *headp; p; p = q) {
61 if (!p->attribute || !p->value) {
64 mutt_free_parameter (&p);
72 void rfc2231_decode_parameters (PARAMETER ** headp)
74 PARAMETER *head = NULL;
78 struct rfc2231_parameter *conthead = NULL;
79 struct rfc2231_parameter *conttmp;
86 short dirty = 0; /* set to 1 when we may have created
93 purge_empty_parameters (headp);
95 for (last = &head, p = *headp; p; p = q) {
98 if (!(s = strchr (p->attribute, '*'))) {
101 * Using RFC 2047 encoding in MIME parameters is explicitly
102 * forbidden by that document. Nevertheless, it's being
103 * generated by some software, including certain Lotus Notes to
104 * Internet Gateways. So we actually decode it.
107 if (option (OPTRFC2047PARAMS) && p->value && strstr (p->value, "=?"))
108 rfc2047_decode (&p->value);
109 else if (!option (OPTSTRICTMIME)) {
110 if (ascii_strcasecmp (AssumedCharset, "us-ascii"))
111 mutt_convert_nonmime_string (&p->value);
118 else if (*(s + 1) == '\0') {
121 s = rfc2231_get_charset (p->value, charset, sizeof (charset));
122 rfc2231_decode_one (p->value, s);
123 mutt_convert_string (&p->value, charset, Charset, M_ICONV_HOOK_FROM);
133 s++; /* let s point to the first character of index. */
134 for (t = s; *t && isdigit ((unsigned char) *t); t++);
135 encoded = (*t == '*');
140 conttmp = rfc2231_new_parameter ();
141 conttmp->attribute = p->attribute;
142 conttmp->value = p->value;
143 conttmp->encoded = encoded;
144 conttmp->index = index;
150 rfc2231_list_insert (&conthead, conttmp);
155 rfc2231_join_continuations (last, conthead);
162 purge_empty_parameters (headp);
165 static struct rfc2231_parameter *rfc2231_new_parameter (void)
167 return safe_calloc (sizeof (struct rfc2231_parameter), 1);
170 static void rfc2231_free_parameter (struct rfc2231_parameter **p)
173 FREE (&(*p)->attribute);
179 static char *rfc2231_get_charset (char *value, char *charset, size_t chslen)
183 if (!(t = strchr (value, '\''))) {
189 strfcpy (charset, value, chslen);
191 if ((u = strchr (t + 1, '\'')))
197 static void rfc2231_decode_one (char *dest, char *src)
201 for (d = dest; *src; src++) {
203 isxdigit ((unsigned char) *(src + 1)) &&
204 isxdigit ((unsigned char) *(src + 2))) {
205 *d++ = (hexval (*(src + 1)) << 4) | (hexval (*(src + 2)));
215 /* insert parameter into an ordered list.
217 * Primary sorting key: attribute
218 * Secondary sorting key: index
221 static void rfc2231_list_insert (struct rfc2231_parameter **list,
222 struct rfc2231_parameter *par)
224 struct rfc2231_parameter **last = list;
225 struct rfc2231_parameter *p = *list, *q;
233 c = mutt_strcmp (par->value, q->value);
234 if ((c > 0) || (c == 0 && par->index >= q->index))
242 /* process continuation parameters */
244 static void rfc2231_join_continuations (PARAMETER ** head,
245 struct rfc2231_parameter *par)
247 struct rfc2231_parameter *q;
249 char attribute[STRING];
250 char charset[STRING];
261 strfcpy (attribute, par->attribute, sizeof (attribute));
263 if ((encoded = par->encoded))
264 valp = rfc2231_get_charset (par->value, charset, sizeof (charset));
269 if (encoded && par->encoded)
270 rfc2231_decode_one (par->value, valp);
272 vl = mutt_strlen (par->value);
274 safe_realloc (&value, l + vl + 1);
275 strcpy (value + l, par->value); /* __STRCPY_CHECKED__ */
279 rfc2231_free_parameter (&par);
282 } while (par && !mutt_strcmp (par->attribute, attribute));
286 mutt_convert_string (&value, charset, Charset, M_ICONV_HOOK_FROM);
287 *head = mutt_new_parameter ();
288 (*head)->attribute = safe_strdup (attribute);
289 (*head)->value = value;
290 head = &(*head)->next;
295 int rfc2231_encode_string (char **pd)
297 int ext = 0, encode = 0;
298 char *charset, *s, *t, *e, *d = 0;
299 size_t slen, dlen = 0;
302 * A shortcut to detect pure 7bit data.
304 * This should prevent the worst when character set handling
308 for (s = *pd; *s; s++)
315 if (!Charset || !SendCharset ||
316 !(charset = mutt_choose_charset (Charset, SendCharset,
317 *pd, mutt_strlen (*pd), &d, &dlen))) {
318 charset = safe_strdup (Charset ? Charset : "unknown-8bit");
320 dlen = mutt_strlen (d);
323 if (!mutt_is_us_ascii (charset))
326 for (s = d, slen = dlen; slen; s++, slen--)
327 if (*s < 0x20 || *s >= 0x7f)
329 else if (strchr (MimeSpecials, *s) || strchr ("*'%", *s))
333 e = safe_malloc (dlen + 2 * ext + mutt_strlen (charset) + 3);
334 sprintf (e, "%s''", charset); /* __SPRINTF_CHECKED__ */
335 t = e + mutt_strlen (e);
336 for (s = d, slen = dlen; slen; s++, slen--)
337 if (*s < 0x20 || *s >= 0x7f ||
338 strchr (MimeSpecials, *s) || strchr ("*'%", *s)) {
339 sprintf (t, "%%%02X", (unsigned char) *s);