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,
42 struct rfc2231_parameter
48 struct rfc2231_parameter
52 static char *rfc2231_get_charset (char *, char *, size_t);
53 static struct rfc2231_parameter *rfc2231_new_parameter (void);
54 static void rfc2231_decode_one (char *, char *);
55 static void rfc2231_free_parameter (struct rfc2231_parameter **);
56 static void rfc2231_join_continuations (PARAMETER **, struct rfc2231_parameter *);
57 static void rfc2231_list_insert (struct rfc2231_parameter **, struct rfc2231_parameter *);
59 static void purge_empty_parameters (PARAMETER **headp)
61 PARAMETER *p, *q, **last;
63 for (last = headp, p = *headp; p; p = q)
66 if (!p->attribute || !p->value)
70 mutt_free_parameter (&p);
78 void rfc2231_decode_parameters (PARAMETER **headp)
80 PARAMETER *head = NULL;
84 struct rfc2231_parameter *conthead = NULL;
85 struct rfc2231_parameter *conttmp;
92 short dirty = 0; /* set to 1 when we may have created
98 purge_empty_parameters (headp);
100 for (last = &head, p = *headp; p; p = q)
104 if (!(s = strchr (p->attribute, '*')))
108 * Using RFC 2047 encoding in MIME parameters is explicitly
109 * forbidden by that document. Nevertheless, it's being
110 * generated by some software, including certain Lotus Notes to
111 * Internet Gateways. So we actually decode it.
114 if (option (OPTRFC2047PARAMS) && p->value && strstr (p->value, "=?"))
115 rfc2047_decode (&p->value);
121 else if (*(s + 1) == '\0')
125 s = rfc2231_get_charset (p->value, charset, sizeof (charset));
126 rfc2231_decode_one (p->value, s);
127 mutt_convert_string (&p->value, charset, Charset, M_ICONV_HOOK_FROM);
137 *s = '\0'; s++; /* let s point to the first character of index. */
138 for (t = s; *t && isdigit ((unsigned char) *t); t++)
140 encoded = (*t == '*');
145 conttmp = rfc2231_new_parameter ();
146 conttmp->attribute = p->attribute;
147 conttmp->value = p->value;
148 conttmp->encoded = encoded;
149 conttmp->index = index;
155 rfc2231_list_insert (&conthead, conttmp);
161 rfc2231_join_continuations (last, conthead);
168 purge_empty_parameters (headp);
171 static struct rfc2231_parameter *rfc2231_new_parameter (void)
173 return safe_calloc (sizeof (struct rfc2231_parameter), 1);
176 static void rfc2231_free_parameter (struct rfc2231_parameter **p)
180 FREE (&(*p)->attribute);
186 static char *rfc2231_get_charset (char *value, char *charset, size_t chslen)
190 if (!(t = strchr (value, '\'')))
197 strfcpy (charset, value, chslen);
199 if ((u = strchr (t + 1, '\'')))
205 static void rfc2231_decode_one (char *dest, char *src)
209 for (d = dest; *src; src++)
212 isxdigit ((unsigned char) *(src + 1)) &&
213 isxdigit ((unsigned char) *(src + 2)))
215 *d++ = (hexval (*(src + 1)) << 4) | (hexval (*(src + 2)));
225 /* insert parameter into an ordered list.
227 * Primary sorting key: attribute
228 * Secondary sorting key: index
231 static void rfc2231_list_insert (struct rfc2231_parameter **list,
232 struct rfc2231_parameter *par)
234 struct rfc2231_parameter **last = list;
235 struct rfc2231_parameter *p = *list, *q;
243 c = strcmp (par->value, q->value);
244 if ((c > 0) || (c == 0 && par->index >= q->index))
252 /* process continuation parameters */
254 static void rfc2231_join_continuations (PARAMETER **head,
255 struct rfc2231_parameter *par)
257 struct rfc2231_parameter *q;
259 char attribute[STRING];
260 char charset[STRING];
271 strfcpy (attribute, par->attribute, sizeof (attribute));
273 if ((encoded = par->encoded))
274 valp = rfc2231_get_charset (par->value, charset, sizeof (charset));
280 if (encoded && par->encoded)
281 rfc2231_decode_one (par->value, valp);
283 vl = strlen (par->value);
285 safe_realloc (&value, l + vl + 1);
286 strcpy (value + l, par->value); /* __STRCPY_CHECKED__ */
290 rfc2231_free_parameter (&par);
293 } while (par && !strcmp (par->attribute, attribute));
298 mutt_convert_string (&value, charset, Charset, M_ICONV_HOOK_FROM);
299 *head = mutt_new_parameter ();
300 (*head)->attribute = safe_strdup (attribute);
301 (*head)->value = value;
302 head = &(*head)->next;
307 int rfc2231_encode_string (char **pd)
309 int ext = 0, encode = 0;
310 char *charset, *s, *t, *e, *d = 0;
311 size_t slen, dlen = 0;
314 * A shortcut to detect pure 7bit data.
316 * This should prevent the worst when character set handling
320 for (s = *pd; *s; s++)
327 if (!Charset || !SendCharset ||
328 !(charset = mutt_choose_charset (Charset, SendCharset,
329 *pd, strlen (*pd), &d, &dlen)))
331 charset = safe_strdup (Charset ? Charset : "unknown-8bit");
336 if (!mutt_is_us_ascii (charset))
339 for (s = d, slen = dlen; slen; s++, slen--)
340 if (*s < 0x20 || *s >= 0x7f)
342 else if (strchr (MimeSpecials, *s) || strchr ("*'%", *s))
347 e = safe_malloc (dlen + 2*ext + strlen (charset) + 3);
348 sprintf (e, "%s''", charset); /* __SPRINTF_CHECKED__ */
350 for (s = d, slen = dlen; slen; s++, slen--)
351 if (*s < 0x20 || *s >= 0x7f ||
352 strchr (MimeSpecials, *s) || strchr ("*'%", *s))
354 sprintf (t, "%%%02X", (unsigned char)*s);