2 * Copyright (C) 1999-2000 Thomas Roessler <roessler@does-not-exist.org>
4 * This program is free software; you can redistribute it
5 * and/or modify it under the terms of the GNU General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later
10 * This program is distributed in the hope that it will be
11 * useful, but WITHOUT ANY WARRANTY; without even the implied
12 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
13 * PURPOSE. See the GNU General Public License for more
16 * You should have received a copy of the GNU General Public
17 * License along with this program; if not, write to the Free
18 * Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111, USA.
23 * Yet another MIME encoding for header data. This time, it's
24 * parameters, specified in RFC 2231, and modeled after the
25 * encoding used in URLs.
27 * Additionally, continuations and encoding are mixed in an, errrm,
46 struct rfc2231_parameter {
51 struct rfc2231_parameter
55 static char *rfc2231_get_charset (char *, char *, size_t);
56 static struct rfc2231_parameter *rfc2231_new_parameter (void);
57 static void rfc2231_decode_one (char *, char *);
58 static void rfc2231_free_parameter (struct rfc2231_parameter **);
59 static void rfc2231_join_continuations (PARAMETER **,
60 struct rfc2231_parameter *);
61 static void rfc2231_list_insert (struct rfc2231_parameter **,
62 struct rfc2231_parameter *);
64 static void purge_empty_parameters (PARAMETER ** headp)
66 PARAMETER *p, *q, **last;
68 for (last = headp, p = *headp; p; p = q) {
70 if (!p->attribute || !p->value) {
73 mutt_free_parameter (&p);
81 void rfc2231_decode_parameters (PARAMETER ** headp)
83 PARAMETER *head = NULL;
87 struct rfc2231_parameter *conthead = NULL;
88 struct rfc2231_parameter *conttmp;
95 short dirty = 0; /* set to 1 when we may have created
102 purge_empty_parameters (headp);
104 for (last = &head, p = *headp; p; p = q) {
107 if (!(s = strchr (p->attribute, '*'))) {
110 * Using RFC 2047 encoding in MIME parameters is explicitly
111 * forbidden by that document. Nevertheless, it's being
112 * generated by some software, including certain Lotus Notes to
113 * Internet Gateways. So we actually decode it.
116 if (option (OPTRFC2047PARAMS) && p->value && strstr (p->value, "=?"))
117 rfc2047_decode (&p->value);
118 else if (!option (OPTSTRICTMIME)) {
119 if (ascii_strcasecmp (AssumedCharset, "us-ascii"))
120 mutt_convert_nonmime_string (&p->value);
127 else if (*(s + 1) == '\0') {
130 s = rfc2231_get_charset (p->value, charset, sizeof (charset));
131 rfc2231_decode_one (p->value, s);
132 mutt_convert_string (&p->value, charset, Charset, M_ICONV_HOOK_FROM);
142 s++; /* let s point to the first character of index. */
143 for (t = s; *t && isdigit ((unsigned char) *t); t++);
144 encoded = (*t == '*');
149 conttmp = rfc2231_new_parameter ();
150 conttmp->attribute = p->attribute;
151 conttmp->value = p->value;
152 conttmp->encoded = encoded;
153 conttmp->index = index;
159 rfc2231_list_insert (&conthead, conttmp);
164 rfc2231_join_continuations (last, conthead);
171 purge_empty_parameters (headp);
174 static struct rfc2231_parameter *rfc2231_new_parameter (void)
176 return safe_calloc (sizeof (struct rfc2231_parameter), 1);
179 static void rfc2231_free_parameter (struct rfc2231_parameter **p)
182 FREE (&(*p)->attribute);
188 static char *rfc2231_get_charset (char *value, char *charset, size_t chslen)
192 if (!(t = strchr (value, '\''))) {
198 strfcpy (charset, value, chslen);
200 if ((u = strchr (t + 1, '\'')))
206 static void rfc2231_decode_one (char *dest, char *src)
210 for (d = dest; *src; src++) {
212 isxdigit ((unsigned char) *(src + 1)) &&
213 isxdigit ((unsigned char) *(src + 2))) {
214 *d++ = (hexval (*(src + 1)) << 4) | (hexval (*(src + 2)));
224 /* insert parameter into an ordered list.
226 * Primary sorting key: attribute
227 * Secondary sorting key: index
230 static void rfc2231_list_insert (struct rfc2231_parameter **list,
231 struct rfc2231_parameter *par)
233 struct rfc2231_parameter **last = list;
234 struct rfc2231_parameter *p = *list, *q;
242 c = strcmp (par->value, q->value);
243 if ((c > 0) || (c == 0 && par->index >= q->index))
251 /* process continuation parameters */
253 static void rfc2231_join_continuations (PARAMETER ** head,
254 struct rfc2231_parameter *par)
256 struct rfc2231_parameter *q;
258 char attribute[STRING];
259 char charset[STRING];
270 strfcpy (attribute, par->attribute, sizeof (attribute));
272 if ((encoded = par->encoded))
273 valp = rfc2231_get_charset (par->value, charset, sizeof (charset));
278 if (encoded && par->encoded)
279 rfc2231_decode_one (par->value, valp);
281 vl = strlen (par->value);
283 safe_realloc (&value, l + vl + 1);
284 strcpy (value + l, par->value); /* __STRCPY_CHECKED__ */
288 rfc2231_free_parameter (&par);
291 } while (par && !strcmp (par->attribute, attribute));
295 mutt_convert_string (&value, charset, Charset, M_ICONV_HOOK_FROM);
296 *head = mutt_new_parameter ();
297 (*head)->attribute = safe_strdup (attribute);
298 (*head)->value = value;
299 head = &(*head)->next;
304 int rfc2231_encode_string (char **pd)
306 int ext = 0, encode = 0;
307 char *charset, *s, *t, *e, *d = 0;
308 size_t slen, dlen = 0;
311 * A shortcut to detect pure 7bit data.
313 * This should prevent the worst when character set handling
317 for (s = *pd; *s; s++)
324 if (!Charset || !SendCharset ||
325 !(charset = mutt_choose_charset (Charset, SendCharset,
326 *pd, strlen (*pd), &d, &dlen))) {
327 charset = safe_strdup (Charset ? Charset : "unknown-8bit");
332 if (!mutt_is_us_ascii (charset))
335 for (s = d, slen = dlen; slen; s++, slen--)
336 if (*s < 0x20 || *s >= 0x7f)
338 else if (strchr (MimeSpecials, *s) || strchr ("*'%", *s))
342 e = safe_malloc (dlen + 2 * ext + strlen (charset) + 3);
343 sprintf (e, "%s''", charset); /* __SPRINTF_CHECKED__ */
345 for (s = d, slen = dlen; slen; s++, slen--)
346 if (*s < 0x20 || *s >= 0x7f ||
347 strchr (MimeSpecials, *s) || strchr ("*'%", *s)) {
348 sprintf (t, "%%%02X", (unsigned char) *s);