2 * Copyright (C) 1999-2001 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., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, 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 (AssumedCharset && *AssumedCharset)
121 convert_nonmime_string (&p->value);
127 else if (*(s + 1) == '\0')
131 s = rfc2231_get_charset (p->value, charset, sizeof (charset));
132 rfc2231_decode_one (p->value, s);
133 mutt_convert_string (&p->value, charset, Charset, M_ICONV_HOOK_FROM);
134 mutt_filter_unprintable (&p->value);
144 *s = '\0'; s++; /* let s point to the first character of index. */
145 for (t = s; *t && isdigit ((unsigned char) *t); t++)
147 encoded = (*t == '*');
152 conttmp = rfc2231_new_parameter ();
153 conttmp->attribute = p->attribute;
154 conttmp->value = p->value;
155 conttmp->encoded = encoded;
156 conttmp->index = index;
162 rfc2231_list_insert (&conthead, conttmp);
168 rfc2231_join_continuations (last, conthead);
175 purge_empty_parameters (headp);
178 static struct rfc2231_parameter *rfc2231_new_parameter (void)
180 return safe_calloc (sizeof (struct rfc2231_parameter), 1);
183 static void rfc2231_free_parameter (struct rfc2231_parameter **p)
187 FREE (&(*p)->attribute);
189 FREE (p); /* __FREE_CHECKED__ */
193 static char *rfc2231_get_charset (char *value, char *charset, size_t chslen)
197 if (!(t = strchr (value, '\'')))
204 strfcpy (charset, value, chslen);
206 if ((u = strchr (t + 1, '\'')))
212 static void rfc2231_decode_one (char *dest, char *src)
216 for (d = dest; *src; src++)
219 isxdigit ((unsigned char) *(src + 1)) &&
220 isxdigit ((unsigned char) *(src + 2)))
222 *d++ = (hexval (*(src + 1)) << 4) | (hexval (*(src + 2)));
232 /* insert parameter into an ordered list.
234 * Primary sorting key: attribute
235 * Secondary sorting key: index
238 static void rfc2231_list_insert (struct rfc2231_parameter **list,
239 struct rfc2231_parameter *par)
241 struct rfc2231_parameter **last = list;
242 struct rfc2231_parameter *p = *list, *q;
250 c = strcmp (par->value, q->value);
251 if ((c > 0) || (c == 0 && par->index >= q->index))
259 /* process continuation parameters */
261 static void rfc2231_join_continuations (PARAMETER **head,
262 struct rfc2231_parameter *par)
264 struct rfc2231_parameter *q;
266 char attribute[STRING];
267 char charset[STRING];
278 strfcpy (attribute, par->attribute, sizeof (attribute));
280 if ((encoded = par->encoded))
281 valp = rfc2231_get_charset (par->value, charset, sizeof (charset));
287 if (encoded && par->encoded)
288 rfc2231_decode_one (par->value, valp);
290 vl = strlen (par->value);
292 safe_realloc (&value, l + vl + 1);
293 strcpy (value + l, par->value); /* __STRCPY_CHECKED__ */
297 rfc2231_free_parameter (&par);
300 } while (par && !strcmp (par->attribute, attribute));
305 mutt_convert_string (&value, charset, Charset, M_ICONV_HOOK_FROM);
306 *head = mutt_new_parameter ();
307 (*head)->attribute = safe_strdup (attribute);
308 (*head)->value = value;
309 head = &(*head)->next;
314 int rfc2231_encode_string (char **pd)
316 int ext = 0, encode = 0;
317 char *charset, *s, *t, *e, *d = 0;
318 size_t slen, dlen = 0;
321 * A shortcut to detect pure 7bit data.
323 * This should prevent the worst when character set handling
327 for (s = *pd; *s; s++)
334 if (!Charset || !SendCharset ||
335 !(charset = mutt_choose_charset (Charset, SendCharset,
336 *pd, strlen (*pd), &d, &dlen)))
338 charset = safe_strdup (Charset ? Charset : "unknown-8bit");
343 if (!mutt_is_us_ascii (charset))
346 for (s = d, slen = dlen; slen; s++, slen--)
347 if (*s < 0x20 || *s >= 0x7f)
349 else if (strchr (MimeSpecials, *s) || strchr ("*'%", *s))
354 e = safe_malloc (dlen + 2*ext + strlen (charset) + 3);
355 sprintf (e, "%s''", charset); /* __SPRINTF_CHECKED__ */
357 for (s = d, slen = dlen; slen; s++, slen--)
358 if (*s < 0x20 || *s >= 0x7f ||
359 strchr (MimeSpecials, *s) || strchr ("*'%", *s))
361 sprintf (t, "%%%02X", (unsigned char)*s);
370 FREE (pd); /* __FREE_CHECKED__ */
375 FREE (pd); /* __FREE_CHECKED__ */