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
52 struct rfc2231_parameter
56 static char *rfc2231_get_charset (char *, char *, size_t);
57 static struct rfc2231_parameter *rfc2231_new_parameter (void);
58 static void rfc2231_decode_one (char *, char *);
59 static void rfc2231_free_parameter (struct rfc2231_parameter **);
60 static void rfc2231_join_continuations (PARAMETER **, struct rfc2231_parameter *);
61 static void rfc2231_list_insert (struct rfc2231_parameter **, struct rfc2231_parameter *);
63 static void purge_empty_parameters (PARAMETER **headp)
65 PARAMETER *p, *q, **last;
67 for (last = headp, p = *headp; p; p = q)
70 if (!p->attribute || !p->value)
74 mutt_free_parameter (&p);
82 void rfc2231_decode_parameters (PARAMETER **headp)
84 PARAMETER *head = NULL;
88 struct rfc2231_parameter *conthead = NULL;
89 struct rfc2231_parameter *conttmp;
96 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)
108 if (!(s = strchr (p->attribute, '*')))
112 * Using RFC 2047 encoding in MIME parameters is explicitly
113 * forbidden by that document. Nevertheless, it's being
114 * generated by some software, including certain Lotus Notes to
115 * Internet Gateways. So we actually decode it.
118 if (option (OPTRFC2047PARAMS) && p->value && strstr (p->value, "=?"))
119 rfc2047_decode (&p->value);
120 else if (!option (OPTSTRICTMIME))
122 if (ascii_strcasecmp (AssumedCharset, "us-ascii"))
123 mutt_convert_nonmime_string (&p->value);
130 else if (*(s + 1) == '\0')
134 s = rfc2231_get_charset (p->value, charset, sizeof (charset));
135 rfc2231_decode_one (p->value, s);
136 mutt_convert_string (&p->value, charset, Charset, M_ICONV_HOOK_FROM);
146 *s = '\0'; s++; /* let s point to the first character of index. */
147 for (t = s; *t && isdigit ((unsigned char) *t); t++)
149 encoded = (*t == '*');
154 conttmp = rfc2231_new_parameter ();
155 conttmp->attribute = p->attribute;
156 conttmp->value = p->value;
157 conttmp->encoded = encoded;
158 conttmp->index = index;
164 rfc2231_list_insert (&conthead, conttmp);
170 rfc2231_join_continuations (last, conthead);
177 purge_empty_parameters (headp);
180 static struct rfc2231_parameter *rfc2231_new_parameter (void)
182 return safe_calloc (sizeof (struct rfc2231_parameter), 1);
185 static void rfc2231_free_parameter (struct rfc2231_parameter **p)
189 FREE (&(*p)->attribute);
195 static char *rfc2231_get_charset (char *value, char *charset, size_t chslen)
199 if (!(t = strchr (value, '\'')))
206 strfcpy (charset, value, chslen);
208 if ((u = strchr (t + 1, '\'')))
214 static void rfc2231_decode_one (char *dest, char *src)
218 for (d = dest; *src; src++)
221 isxdigit ((unsigned char) *(src + 1)) &&
222 isxdigit ((unsigned char) *(src + 2)))
224 *d++ = (hexval (*(src + 1)) << 4) | (hexval (*(src + 2)));
234 /* insert parameter into an ordered list.
236 * Primary sorting key: attribute
237 * Secondary sorting key: index
240 static void rfc2231_list_insert (struct rfc2231_parameter **list,
241 struct rfc2231_parameter *par)
243 struct rfc2231_parameter **last = list;
244 struct rfc2231_parameter *p = *list, *q;
252 c = strcmp (par->value, q->value);
253 if ((c > 0) || (c == 0 && par->index >= q->index))
261 /* process continuation parameters */
263 static void rfc2231_join_continuations (PARAMETER **head,
264 struct rfc2231_parameter *par)
266 struct rfc2231_parameter *q;
268 char attribute[STRING];
269 char charset[STRING];
280 strfcpy (attribute, par->attribute, sizeof (attribute));
282 if ((encoded = par->encoded))
283 valp = rfc2231_get_charset (par->value, charset, sizeof (charset));
289 if (encoded && par->encoded)
290 rfc2231_decode_one (par->value, valp);
292 vl = strlen (par->value);
294 safe_realloc (&value, l + vl + 1);
295 strcpy (value + l, par->value); /* __STRCPY_CHECKED__ */
299 rfc2231_free_parameter (&par);
302 } while (par && !strcmp (par->attribute, attribute));
307 mutt_convert_string (&value, charset, Charset, M_ICONV_HOOK_FROM);
308 *head = mutt_new_parameter ();
309 (*head)->attribute = safe_strdup (attribute);
310 (*head)->value = value;
311 head = &(*head)->next;
316 int rfc2231_encode_string (char **pd)
318 int ext = 0, encode = 0;
319 char *charset, *s, *t, *e, *d = 0;
320 size_t slen, dlen = 0;
323 * A shortcut to detect pure 7bit data.
325 * This should prevent the worst when character set handling
329 for (s = *pd; *s; s++)
336 if (!Charset || !SendCharset ||
337 !(charset = mutt_choose_charset (Charset, SendCharset,
338 *pd, strlen (*pd), &d, &dlen)))
340 charset = safe_strdup (Charset ? Charset : "unknown-8bit");
345 if (!mutt_is_us_ascii (charset))
348 for (s = d, slen = dlen; slen; s++, slen--)
349 if (*s < 0x20 || *s >= 0x7f)
351 else if (strchr (MimeSpecials, *s) || strchr ("*'%", *s))
356 e = safe_malloc (dlen + 2*ext + strlen (charset) + 3);
357 sprintf (e, "%s''", charset); /* __SPRINTF_CHECKED__ */
359 for (s = d, slen = dlen; slen; s++, slen--)
360 if (*s < 0x20 || *s >= 0x7f ||
361 strchr (MimeSpecials, *s) || strchr ("*'%", *s))
363 sprintf (t, "%%%02X", (unsigned char)*s);