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,
34 struct rfc2231_parameter {
39 struct rfc2231_parameter
43 static char *rfc2231_get_charset (char *, char *, size_t);
44 static struct rfc2231_parameter *rfc2231_new_parameter (void);
45 static void rfc2231_decode_one (char *, char *);
46 static void rfc2231_free_parameter (struct rfc2231_parameter **);
47 static void rfc2231_join_continuations (PARAMETER **,
48 struct rfc2231_parameter *);
49 static void rfc2231_list_insert (struct rfc2231_parameter **,
50 struct rfc2231_parameter *);
52 static void purge_empty_parameters (PARAMETER ** headp)
54 PARAMETER *p, *q, **last;
56 for (last = headp, p = *headp; p; p = q) {
58 if (!p->attribute || !p->value) {
61 mutt_free_parameter (&p);
69 void rfc2231_decode_parameters (PARAMETER ** headp)
71 PARAMETER *head = NULL;
75 struct rfc2231_parameter *conthead = NULL;
76 struct rfc2231_parameter *conttmp;
83 short dirty = 0; /* set to 1 when we may have created
90 purge_empty_parameters (headp);
92 for (last = &head, p = *headp; p; p = q) {
95 if (!(s = strchr (p->attribute, '*'))) {
98 * Using RFC 2047 encoding in MIME parameters is explicitly
99 * forbidden by that document. Nevertheless, it's being
100 * generated by some software, including certain Lotus Notes to
101 * Internet Gateways. So we actually decode it.
104 if (option (OPTRFC2047PARAMS) && p->value && strstr (p->value, "=?"))
105 rfc2047_decode (&p->value);
106 else if (!option (OPTSTRICTMIME)) {
107 if (ascii_strcasecmp (AssumedCharset, "us-ascii"))
108 mutt_convert_nonmime_string (&p->value);
115 else if (*(s + 1) == '\0') {
118 s = rfc2231_get_charset (p->value, charset, sizeof (charset));
119 rfc2231_decode_one (p->value, s);
120 mutt_convert_string (&p->value, charset, Charset, M_ICONV_HOOK_FROM);
130 s++; /* let s point to the first character of index. */
131 for (t = s; *t && isdigit ((unsigned char) *t); t++);
132 encoded = (*t == '*');
137 conttmp = rfc2231_new_parameter ();
138 conttmp->attribute = p->attribute;
139 conttmp->value = p->value;
140 conttmp->encoded = encoded;
141 conttmp->index = index;
147 rfc2231_list_insert (&conthead, conttmp);
152 rfc2231_join_continuations (last, conthead);
159 purge_empty_parameters (headp);
162 static struct rfc2231_parameter *rfc2231_new_parameter (void)
164 return safe_calloc (sizeof (struct rfc2231_parameter), 1);
167 static void rfc2231_free_parameter (struct rfc2231_parameter **p)
170 FREE (&(*p)->attribute);
176 static char *rfc2231_get_charset (char *value, char *charset, size_t chslen)
180 if (!(t = strchr (value, '\''))) {
186 strfcpy (charset, value, chslen);
188 if ((u = strchr (t + 1, '\'')))
194 static void rfc2231_decode_one (char *dest, char *src)
198 for (d = dest; *src; src++) {
200 isxdigit ((unsigned char) *(src + 1)) &&
201 isxdigit ((unsigned char) *(src + 2))) {
202 *d++ = (hexval (*(src + 1)) << 4) | (hexval (*(src + 2)));
212 /* insert parameter into an ordered list.
214 * Primary sorting key: attribute
215 * Secondary sorting key: index
218 static void rfc2231_list_insert (struct rfc2231_parameter **list,
219 struct rfc2231_parameter *par)
221 struct rfc2231_parameter **last = list;
222 struct rfc2231_parameter *p = *list, *q;
230 c = strcmp (par->value, q->value);
231 if ((c > 0) || (c == 0 && par->index >= q->index))
239 /* process continuation parameters */
241 static void rfc2231_join_continuations (PARAMETER ** head,
242 struct rfc2231_parameter *par)
244 struct rfc2231_parameter *q;
246 char attribute[STRING];
247 char charset[STRING];
258 strfcpy (attribute, par->attribute, sizeof (attribute));
260 if ((encoded = par->encoded))
261 valp = rfc2231_get_charset (par->value, charset, sizeof (charset));
266 if (encoded && par->encoded)
267 rfc2231_decode_one (par->value, valp);
269 vl = mutt_strlen (par->value);
271 safe_realloc (&value, l + vl + 1);
272 strcpy (value + l, par->value); /* __STRCPY_CHECKED__ */
276 rfc2231_free_parameter (&par);
279 } while (par && !strcmp (par->attribute, attribute));
283 mutt_convert_string (&value, charset, Charset, M_ICONV_HOOK_FROM);
284 *head = mutt_new_parameter ();
285 (*head)->attribute = safe_strdup (attribute);
286 (*head)->value = value;
287 head = &(*head)->next;
292 int rfc2231_encode_string (char **pd)
294 int ext = 0, encode = 0;
295 char *charset, *s, *t, *e, *d = 0;
296 size_t slen, dlen = 0;
299 * A shortcut to detect pure 7bit data.
301 * This should prevent the worst when character set handling
305 for (s = *pd; *s; s++)
312 if (!Charset || !SendCharset ||
313 !(charset = mutt_choose_charset (Charset, SendCharset,
314 *pd, mutt_strlen (*pd), &d, &dlen))) {
315 charset = safe_strdup (Charset ? Charset : "unknown-8bit");
317 dlen = mutt_strlen (d);
320 if (!mutt_is_us_ascii (charset))
323 for (s = d, slen = dlen; slen; s++, slen--)
324 if (*s < 0x20 || *s >= 0x7f)
326 else if (strchr (MimeSpecials, *s) || strchr ("*'%", *s))
330 e = safe_malloc (dlen + 2 * ext + mutt_strlen (charset) + 3);
331 sprintf (e, "%s''", charset); /* __SPRINTF_CHECKED__ */
332 t = e + mutt_strlen (e);
333 for (s = d, slen = dlen; slen; s++, slen--)
334 if (*s < 0x20 || *s >= 0x7f ||
335 strchr (MimeSpecials, *s) || strchr ("*'%", *s)) {
336 sprintf (t, "%%%02X", (unsigned char) *s);