forked from neomutt/neomutt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rfc2231.c
355 lines (304 loc) · 7.76 KB
/
rfc2231.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
/**
* @file
* RFC2231 MIME Charset routines
*
* @authors
* Copyright (C) 1999-2001 Thomas Roessler <[email protected]>
*
* @copyright
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation, either version 2 of the License, or (at your option) any later
* version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*
* Yet another MIME encoding for header data. This time, it's
* parameters, specified in RFC2231, and modeled after the
* encoding used in URLs.
*
* Additionally, continuations and encoding are mixed in an, errrm,
* interesting manner.
*/
#include "config.h"
#include <ctype.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "mutt/mutt.h"
#include "rfc2231.h"
#include "globals.h"
#include "options.h"
/**
* struct Rfc2231Parameter - MIME section parameter
*/
struct Rfc2231Parameter
{
char *attribute;
char *value;
int index;
bool encoded;
struct Rfc2231Parameter *next;
};
static void purge_empty_parameters(struct ParameterList *p)
{
struct Parameter *np, *tmp;
TAILQ_FOREACH_SAFE(np, p, entries, tmp)
{
if (!np->attribute || !np->value)
{
TAILQ_REMOVE(p, np, entries);
mutt_param_free_one(&np);
}
}
}
static char *rfc2231_get_charset(char *value, char *charset, size_t chslen)
{
char *t = NULL, *u = NULL;
t = strchr(value, '\'');
if (!t)
{
charset[0] = '\0';
return value;
}
*t = '\0';
mutt_str_strfcpy(charset, value, chslen);
u = strchr(t + 1, '\'');
if (u)
return u + 1;
else
return t + 1;
}
static void rfc2231_decode_one(char *dest, char *src)
{
char *d = NULL;
for (d = dest; *src; src++)
{
if (*src == '%' && isxdigit((unsigned char) *(src + 1)) &&
isxdigit((unsigned char) *(src + 2)))
{
*d++ = (hexval(*(src + 1)) << 4) | (hexval(*(src + 2)));
src += 2;
}
else
*d++ = *src;
}
*d = '\0';
}
static struct Rfc2231Parameter *rfc2231_new_parameter(void)
{
return mutt_mem_calloc(1, sizeof(struct Rfc2231Parameter));
}
/**
* rfc2231_list_insert - insert parameter into an ordered list
*
* Primary sorting key: attribute
* Secondary sorting key: index
*/
static void rfc2231_list_insert(struct Rfc2231Parameter **list, struct Rfc2231Parameter *par)
{
struct Rfc2231Parameter **last = list;
struct Rfc2231Parameter *p = *list;
while (p)
{
const int c = strcmp(par->attribute, p->attribute);
if ((c < 0) || (c == 0 && par->index <= p->index))
break;
last = &p->next;
p = p->next;
}
par->next = p;
*last = par;
}
static void rfc2231_free_parameter(struct Rfc2231Parameter **p)
{
if (*p)
{
FREE(&(*p)->attribute);
FREE(&(*p)->value);
FREE(p);
}
}
/**
* rfc2231_join_continuations - process continuation parameters
*/
static void rfc2231_join_continuations(struct ParameterList *p, struct Rfc2231Parameter *par)
{
char attribute[STRING];
char charset[STRING];
while (par)
{
char *value = NULL;
size_t l = 0;
mutt_str_strfcpy(attribute, par->attribute, sizeof(attribute));
const bool encoded = par->encoded;
char *valp;
if (encoded)
valp = rfc2231_get_charset(par->value, charset, sizeof(charset));
else
valp = par->value;
do
{
if (encoded && par->encoded)
rfc2231_decode_one(par->value, valp);
const size_t vl = strlen(par->value);
mutt_mem_realloc(&value, l + vl + 1);
strcpy(value + l, par->value);
l += vl;
struct Rfc2231Parameter *q = par->next;
rfc2231_free_parameter(&par);
par = q;
if (par)
valp = par->value;
} while (par && (strcmp(par->attribute, attribute) == 0));
if (encoded)
mutt_ch_convert_string(&value, charset, Charset, MUTT_ICONV_HOOK_FROM);
struct Parameter *np = mutt_param_new();
np->attribute = mutt_str_strdup(attribute);
np->value = value;
TAILQ_INSERT_HEAD(p, np, entries);
}
}
void rfc2231_decode_parameters(struct ParameterList *p)
{
struct Rfc2231Parameter *conthead = NULL;
struct Rfc2231Parameter *conttmp = NULL;
char *s = NULL, *t = NULL;
char charset[STRING];
bool encoded;
int index;
bool dirty = false; /* set to 1 when we may have created
* empty parameters. */
if (!p)
return;
purge_empty_parameters(p);
struct Parameter *np, *tmp;
TAILQ_FOREACH_SAFE(np, p, entries, tmp)
{
s = strchr(np->attribute, '*');
if (!s)
{
/*
* Using RFC2047 encoding in MIME parameters is explicitly
* forbidden by that document. Nevertheless, it's being
* generated by some software, including certain Lotus Notes to
* Internet Gateways. So we actually decode it.
*/
if (Rfc2047Parameters && np->value && strstr(np->value, "=?"))
mutt_rfc2047_decode(&np->value);
else if (AssumedCharset && *AssumedCharset)
mutt_ch_convert_nonmime_string(&np->value);
}
else if (*(s + 1) == '\0')
{
*s = '\0';
s = rfc2231_get_charset(np->value, charset, sizeof(charset));
rfc2231_decode_one(np->value, s);
mutt_ch_convert_string(&np->value, charset, Charset, MUTT_ICONV_HOOK_FROM);
mutt_mb_filter_unprintable(&np->value);
dirty = true;
}
else
{
*s = '\0';
s++; /* let s point to the first character of index. */
for (t = s; *t && isdigit((unsigned char) *t); t++)
;
encoded = (*t == '*');
*t = '\0';
index = atoi(s);
conttmp = rfc2231_new_parameter();
conttmp->attribute = np->attribute;
conttmp->value = np->value;
conttmp->encoded = encoded;
conttmp->index = index;
np->attribute = NULL;
np->value = NULL;
TAILQ_REMOVE(p, np, entries);
FREE(&np);
rfc2231_list_insert(&conthead, conttmp);
}
}
if (conthead)
{
rfc2231_join_continuations(p, conthead);
dirty = true;
}
if (dirty)
purge_empty_parameters(p);
}
int rfc2231_encode_string(char **pd)
{
int ext = 0, encode = 0;
char *charset = NULL, *s = NULL, *t = NULL, *e = NULL, *d = NULL;
size_t slen, dlen = 0;
/*
* A shortcut to detect pure 7bit data.
*
* This should prevent the worst when character set handling
* is flawed.
*/
for (s = *pd; *s; s++)
if (*s & 0x80)
break;
if (!*s)
return 0;
if (!Charset || !SendCharset ||
!(charset = mutt_ch_choose(Charset, SendCharset, *pd, strlen(*pd), &d, &dlen)))
{
charset = mutt_str_strdup(Charset ? Charset : "unknown-8bit");
FREE(&d);
d = *pd;
dlen = strlen(d);
}
if (!mutt_ch_is_us_ascii(charset))
encode = 1;
for (s = d, slen = dlen; slen; s++, slen--)
{
if (*s < 0x20 || *s >= 0x7f)
{
encode = 1;
ext++;
}
else if (strchr(MimeSpecials, *s) || strchr("*'%", *s))
ext++;
}
if (encode)
{
e = mutt_mem_malloc(dlen + 2 * ext + strlen(charset) + 3);
sprintf(e, "%s''", charset);
t = e + strlen(e);
for (s = d, slen = dlen; slen; s++, slen--)
{
if (*s < 0x20 || *s >= 0x7f || strchr(MimeSpecials, *s) || strchr("*'%", *s))
{
sprintf(t, "%%%02X", (unsigned char) *s);
t += 3;
}
else
{
*t++ = *s;
}
}
*t = '\0';
if (d != *pd)
FREE(&d);
FREE(pd);
*pd = e;
}
else if (d != *pd)
{
FREE(pd);
*pd = d;
}
FREE(&charset);
return encode;
}