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,
36 struct rfc2231_parameter {
41 struct rfc2231_parameter
45 static char *rfc2231_get_charset (char *, char *, size_t);
46 static struct rfc2231_parameter *rfc2231_new_parameter (void);
47 static void rfc2231_decode_one (char *, char *);
48 static void rfc2231_free_parameter (struct rfc2231_parameter **);
49 static void rfc2231_join_continuations (PARAMETER **,
50 struct rfc2231_parameter *);
51 static void rfc2231_list_insert (struct rfc2231_parameter **,
52 struct rfc2231_parameter *);
54 static void purge_empty_parameters (PARAMETER ** headp)
56 PARAMETER *p, *q, **last;
58 for (last = headp, p = *headp; p; p = q) {
60 if (!p->attribute || !p->value) {
63 mutt_free_parameter (&p);
71 void rfc2231_decode_parameters (PARAMETER ** headp)
73 PARAMETER *head = NULL;
77 struct rfc2231_parameter *conthead = NULL;
78 struct rfc2231_parameter *conttmp;
85 short dirty = 0; /* set to 1 when we may have created
92 purge_empty_parameters (headp);
94 for (last = &head, p = *headp; p; p = q) {
97 if (!(s = strchr (p->attribute, '*'))) {
100 * Using RFC 2047 encoding in MIME parameters is explicitly
101 * forbidden by that document. Nevertheless, it's being
102 * generated by some software, including certain Lotus Notes to
103 * Internet Gateways. So we actually decode it.
106 if (option (OPTRFC2047PARAMS) && p->value && strstr (p->value, "=?"))
107 rfc2047_decode (&p->value);
108 else if (!option (OPTSTRICTMIME)) {
109 if (ascii_strcasecmp (AssumedCharset, "us-ascii"))
110 mutt_convert_nonmime_string (&p->value);
117 else if (*(s + 1) == '\0') {
120 s = rfc2231_get_charset (p->value, charset, sizeof (charset));
121 rfc2231_decode_one (p->value, s);
122 mutt_convert_string (&p->value, charset, Charset, M_ICONV_HOOK_FROM);
132 s++; /* let s point to the first character of index. */
133 for (t = s; *t && isdigit ((unsigned char) *t); t++);
134 encoded = (*t == '*');
139 conttmp = rfc2231_new_parameter ();
140 conttmp->attribute = p->attribute;
141 conttmp->value = p->value;
142 conttmp->encoded = encoded;
143 conttmp->index = index;
149 rfc2231_list_insert (&conthead, conttmp);
154 rfc2231_join_continuations (last, conthead);
161 purge_empty_parameters (headp);
164 static struct rfc2231_parameter *rfc2231_new_parameter (void)
166 return safe_calloc (sizeof (struct rfc2231_parameter), 1);
169 static void rfc2231_free_parameter (struct rfc2231_parameter **p)
172 FREE (&(*p)->attribute);
178 static char *rfc2231_get_charset (char *value, char *charset, size_t chslen)
182 if (!(t = strchr (value, '\''))) {
188 strfcpy (charset, value, chslen);
190 if ((u = strchr (t + 1, '\'')))
196 static void rfc2231_decode_one (char *dest, char *src)
200 for (d = dest; *src; src++) {
202 isxdigit ((unsigned char) *(src + 1)) &&
203 isxdigit ((unsigned char) *(src + 2))) {
204 *d++ = (hexval (*(src + 1)) << 4) | (hexval (*(src + 2)));
214 /* insert parameter into an ordered list.
216 * Primary sorting key: attribute
217 * Secondary sorting key: index
220 static void rfc2231_list_insert (struct rfc2231_parameter **list,
221 struct rfc2231_parameter *par)
223 struct rfc2231_parameter **last = list;
224 struct rfc2231_parameter *p = *list, *q;
232 c = strcmp (par->value, q->value);
233 if ((c > 0) || (c == 0 && par->index >= q->index))
241 /* process continuation parameters */
243 static void rfc2231_join_continuations (PARAMETER ** head,
244 struct rfc2231_parameter *par)
246 struct rfc2231_parameter *q;
248 char attribute[STRING];
249 char charset[STRING];
260 strfcpy (attribute, par->attribute, sizeof (attribute));
262 if ((encoded = par->encoded))
263 valp = rfc2231_get_charset (par->value, charset, sizeof (charset));
268 if (encoded && par->encoded)
269 rfc2231_decode_one (par->value, valp);
271 vl = mutt_strlen (par->value);
273 safe_realloc (&value, l + vl + 1);
274 strcpy (value + l, par->value); /* __STRCPY_CHECKED__ */
278 rfc2231_free_parameter (&par);
281 } while (par && !strcmp (par->attribute, attribute));
285 mutt_convert_string (&value, charset, Charset, M_ICONV_HOOK_FROM);
286 *head = mutt_new_parameter ();
287 (*head)->attribute = safe_strdup (attribute);
288 (*head)->value = value;
289 head = &(*head)->next;
294 int rfc2231_encode_string (char **pd)
296 int ext = 0, encode = 0;
297 char *charset, *s, *t, *e, *d = 0;
298 size_t slen, dlen = 0;
301 * A shortcut to detect pure 7bit data.
303 * This should prevent the worst when character set handling
307 for (s = *pd; *s; s++)
314 if (!Charset || !SendCharset ||
315 !(charset = mutt_choose_charset (Charset, SendCharset,
316 *pd, mutt_strlen (*pd), &d, &dlen))) {
317 charset = safe_strdup (Charset ? Charset : "unknown-8bit");
319 dlen = mutt_strlen (d);
322 if (!mutt_is_us_ascii (charset))
325 for (s = d, slen = dlen; slen; s++, slen--)
326 if (*s < 0x20 || *s >= 0x7f)
328 else if (strchr (MimeSpecials, *s) || strchr ("*'%", *s))
332 e = safe_malloc (dlen + 2 * ext + mutt_strlen (charset) + 3);
333 sprintf (e, "%s''", charset); /* __SPRINTF_CHECKED__ */
334 t = e + mutt_strlen (e);
335 for (s = d, slen = dlen; slen; s++, slen--)
336 if (*s < 0x20 || *s >= 0x7f ||
337 strchr (MimeSpecials, *s) || strchr ("*'%", *s)) {
338 sprintf (t, "%%%02X", (unsigned char) *s);