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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
|
/*
** Copyright 2002-2011 Double Precision, Inc. See COPYING for
** distribution information.
*/
/*
*/
#if HAVE_CONFIG_H
#include "rfc2045_config.h"
#endif
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include "rfc2045.h"
#include "rfc822/rfc822.h"
#include <unicode.h>
/*
** Deallocate a link list of rfc2231param structures.
*/
void rfc2231_paramDestroy(struct rfc2231param *p)
{
while (p)
{
struct rfc2231param *q=p->next;
free(p);
p=q;
}
}
int rfc2231_buildAttrList(struct rfc2231param **paramList,
const char *name,
const char *attrName,
const char *attrValue)
{
int nameLen=strlen(name);
if (strncmp(attrName, name, nameLen) == 0 &&
(attrName[nameLen] == 0 ||
attrName[nameLen] == '*'))
{
struct rfc2231param *n
=malloc(sizeof(struct rfc2231param)), **o;
const char *p=attrName + nameLen;
if (!n)
{
rfc2231_paramDestroy(*paramList);
return -1;
}
/*
** A non-rfc 2231 parameter has paramnum set to 0, an
** rfc 2231 parameter has paramnum set to its number, plus 1.
*/
if (*p == 0)
{
n->paramnum=0;
}
else
{
p++;
n->paramnum=atoi(p)+1;
if (n->paramnum <= 0)
n->paramnum=1;
}
p=strrchr(attrName, '*');
n->encoded=p && p[1] == 0;
n->value=attrValue;
for (o=paramList; *o; o= &(*o)->next)
if ( (*o)->paramnum > n->paramnum)
break;
n->next= *o;
*o=n;
}
return 0;
}
/*
** Create a link list of rfc2231param structures for a specific attribute
**
** Returns: 0 - ok, < 0 - out of memory.
*/
static int rfc2231_paramCreate(struct rfc2045attr *attr,
const char *name,
struct rfc2231param **paramList)
{
*paramList=NULL;
while (attr)
{
if (rfc2231_buildAttrList(paramList, name, attr->name,
attr->value) < 0)
return (-1);
attr=attr->next;
}
return (0);
}
static const char rfc2231_xdigit[]="0123456789ABCDEFabcdef";
static int nyb(char c)
{
const char *p=strchr(rfc2231_xdigit, c);
int n;
if (!p)
return 0;
n=p-rfc2231_xdigit;
if (n >= 16)
n -= 6;
return n;
}
/*
** Decode an rfc2231param link list.
**
** charset, language, text, are decoded, if the corresponding args below are
** not null. Their corresponding lengths (including the null bytes) are
** always saved in the corresponding int args. rfc2231_decode() is called
** twice to get the lengths, then once again after the buffers are allocated.
*/
void rfc2231_paramDecode(struct rfc2231param *paramList,
char *charsetPtr,
char *langPtr,
char *textPtr,
int *charsetLen,
int *langLen,
int *textLen)
{
int first=1;
*charsetLen=*langLen=*textLen=1; /* null byte */
if (paramList && paramList->paramnum == 0 &&
paramList->next)
paramList=paramList->next;
/*
** Both a non-rfc2231 and an rfc2231 parameter was specified, so
** take the better one.
*/
while (paramList)
{
const char *p=paramList->value;
if (first && paramList->encoded)
{
const char *q=strchr(p, '\'');
if (q && strchr(q+1, '\''))
{
while (*p != '\'')
{
if (charsetPtr)
*charsetPtr++ = *p;
p++;
(*charsetLen)++;
}
p++;
while (*p != '\'')
{
if (langPtr)
*langPtr++ = *p;
p++;
(*langLen)++;
}
p++;
}
}
first=0;
while (*p)
{
if (*p == '%' && p[1] && p[2] && paramList->encoded)
{
if (textPtr)
*textPtr++ = nyb(p[1]) * 16 +
nyb(p[2]);
p += 3;
}
else
{
if (textPtr)
*textPtr++ = *p;
p++;
}
(*textLen)++;
}
paramList=paramList->next;
}
if (charsetPtr)
*charsetPtr=0;
if (langPtr)
*langPtr=0;
if (textPtr)
*textPtr=0;
}
/*
** Retrieve RFC 2231 information from a specific rfc2045attr list
**
** Returns 0 success, -1 for failure
*/
static int rfc2231_decode(struct rfc2045attr *attrList,
const char *name,
char **chsetPtr,
char **langPtr,
char **textPtr)
{
int chsetLen;
int langLen;
int textLen;
struct rfc2231param *paramList;
if (rfc2231_paramCreate(attrList, name, ¶mList) < 0)
return -1;
rfc2231_paramDecode(paramList, NULL, NULL, NULL,
&chsetLen,
&langLen,
&textLen);
if (chsetPtr)
*chsetPtr=NULL;
if (langPtr)
*langPtr=NULL;
if (textPtr)
*textPtr=NULL;
if ((chsetPtr && (*chsetPtr=malloc(chsetLen)) == NULL)
|| (langPtr && (*langPtr=malloc(langLen)) == NULL)
|| (textPtr && (*textPtr=malloc(textLen)) == NULL))
{
rfc2231_paramDestroy(paramList);
if (*chsetPtr)
free(*chsetPtr);
if (*langPtr)
free(*langPtr);
if (*textPtr)
free(*textPtr);
return (-1);
}
rfc2231_paramDecode(paramList,
chsetPtr ? *chsetPtr:NULL,
langPtr ? *langPtr:NULL,
textPtr ? *textPtr:NULL,
&chsetLen,
&langLen,
&textLen);
return 0;
}
int rfc2231_decodeType(struct rfc2045 *rfc, const char *name,
char **chsetPtr,
char **langPtr,
char **textPtr)
{
return rfc2231_decode(rfc->content_type_attr, name,
chsetPtr, langPtr, textPtr);
}
int rfc2231_decodeDisposition(struct rfc2045 *rfc, const char *name,
char **chsetPtr,
char **langPtr,
char **textPtr)
{
return rfc2231_decode(rfc->content_disposition_attr, name,
chsetPtr, langPtr, textPtr);
}
static int conv_unicode(char **text, const char *fromChset,
const char *toChset)
{
int err;
char *p;
if (!toChset)
toChset=unicode_default_chset();
if (!fromChset || !*fromChset)
return 0;
p=unicode_convert_tobuf(*text, fromChset, toChset, &err);
if (p && err)
{
free(p);
p=NULL;
}
if (!p)
return (-1);
free(*text);
*text=p;
return (0);
}
int rfc2231_udecodeType(struct rfc2045 *rfc, const char *name,
const char *myCharset,
char **textPtr)
{
char *text, *chset;
if (rfc2231_decodeType(rfc, name, &chset, NULL, &text) < 0)
return (-1);
if (conv_unicode(&text, chset, myCharset) < 0)
{
free(text);
free(chset);
return (-1);
}
*textPtr=text;
free(chset);
return (0);
}
int rfc2231_udecodeDisposition(struct rfc2045 *rfc, const char *name,
const char *myCharset,
char **textPtr)
{
char *text, *chset;
if (rfc2231_decodeDisposition(rfc, name, &chset, NULL, &text) < 0)
return (-1);
if (conv_unicode(&text, chset, myCharset) < 0)
{
free(text);
free(chset);
return (-1);
}
*textPtr=text;
free(chset);
return (0);
}
|