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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
|
/*
* Purple
*
* Purple is the legal property of its developers, whose names are too
* numerous to list here. Please refer to the COPYRIGHT file distributed
* with this source distribution
*
* 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, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301,
* USA.
*/
#include <stdio.h>
#include <string.h>
#include <glib.h>
#include "internal.h"
/* this should become "util.h" if we ever get this into purple proper */
#include "debug.h"
#include "mime.h"
#include "util.h"
/**
* @struct mime_fields
*
* Utility structure used in both MIME document and parts, which maps
* field names to their values, and keeps an easily accessible list of
* keys.
*/
struct mime_fields {
GHashTable *map;
GList *keys;
};
struct _PurpleMimeDocument {
struct mime_fields fields;
GList *parts;
};
struct _PurpleMimePart {
struct mime_fields fields;
struct _PurpleMimeDocument *doc;
GString *data;
};
static void
fields_set(struct mime_fields *mf, const char *key, const char *val)
{
char *k, *v;
g_return_if_fail(mf != NULL);
g_return_if_fail(mf->map != NULL);
k = g_ascii_strdown(key, -1);
v = g_strdup(val);
/* append to the keys list only if it's not already there */
if(! g_hash_table_lookup(mf->map, k)) {
mf->keys = g_list_append(mf->keys, k);
}
/* important to use insert. If the key is already in the table, then
it's already in the keys list. Insert will free the new instance
of the key rather than the old instance. */
g_hash_table_insert(mf->map, k, v);
}
static const char *
fields_get(struct mime_fields *mf, const char *key)
{
char *kdown;
const char *ret;
g_return_val_if_fail(mf != NULL, NULL);
g_return_val_if_fail(mf->map != NULL, NULL);
kdown = g_ascii_strdown(key, -1);
ret = g_hash_table_lookup(mf->map, kdown);
g_free(kdown);
return ret;
}
static void
fields_init(struct mime_fields *mf)
{
g_return_if_fail(mf != NULL);
mf->map = g_hash_table_new_full(g_str_hash, g_str_equal,
g_free, g_free);
}
static void
fields_loadline(struct mime_fields *mf, const char *line, gsize len)
{
/* split the line into key: value */
char *key, *newkey, *val;
char **tokens;
/* feh, need it to be NUL terminated */
key = g_strndup(line, len);
/* split */
val = strchr(key, ':');
if(! val) {
g_free(key);
return;
}
*val++ = '\0';
/* normalize whitespace (sorta) and trim on key and value */
tokens = g_strsplit(key, "\t\r\n", 0);
newkey = g_strjoinv("", tokens);
g_strstrip(newkey);
g_strfreev(tokens);
tokens = g_strsplit(val, "\t\r\n", 0);
val = g_strjoinv("", tokens);
g_strstrip(val);
g_strfreev(tokens);
fields_set(mf, newkey, val);
g_free(newkey);
g_free(key);
g_free(val);
}
static void
fields_load(struct mime_fields *mf, char **buf, gsize *len)
{
char *tail;
while ((tail = g_strstr_len(*buf, *len, "\r\n")))
{
char *line;
gsize ln;
/* determine the current line */
line = *buf;
ln = tail - line;
/* advance our search space past the CRLF */
*buf = tail + 2;
*len -= (ln + 2);
/* empty line, end of headers */
if(! ln) return;
/* look out for line continuations */
if(line[ln-1] == ';') {
tail = g_strstr_len(*buf, *len, "\r\n");
if(tail) {
gsize cln;
cln = tail - *buf;
ln = tail - line;
/* advance our search space past the CRLF (again) */
*buf = tail + 2;
*len -= (cln + 2);
}
}
/* process our super-cool line */
fields_loadline(mf, line, ln);
}
}
static void
field_write(const char *key, const char *val, GString *str)
{
g_string_append_printf(str, "%s: %s\r\n", key, val);
}
static void
fields_write(struct mime_fields *mf, GString *str)
{
g_return_if_fail(mf != NULL);
g_hash_table_foreach(mf->map, (GHFunc) field_write, str);
g_string_append(str, "\r\n");
}
static void
fields_destroy(struct mime_fields *mf)
{
g_return_if_fail(mf != NULL);
g_hash_table_destroy(mf->map);
g_list_free(mf->keys);
mf->map = NULL;
mf->keys = NULL;
}
static PurpleMimePart *
part_new(PurpleMimeDocument *doc)
{
PurpleMimePart *part;
part = g_new0(PurpleMimePart, 1);
fields_init(&part->fields);
part->doc = doc;
part->data = g_string_new(NULL);
doc->parts = g_list_prepend(doc->parts, part);
return part;
}
static void
part_load(PurpleMimePart *part, const char *buf, gsize len)
{
char *b = (char *) buf;
gsize n = len;
fields_load(&part->fields, &b, &n);
/* the remainder will have a blank line, if there's anything at all,
so check if there's anything then trim off the trailing four
bytes, \r\n\r\n */
if(n > 4) n -= 4;
g_string_append_len(part->data, b, n);
}
static void
part_write(PurpleMimePart *part, GString *str)
{
fields_write(&part->fields, str);
g_string_append_printf(str, "%s\r\n\r\n", part->data->str);
}
static void
part_free(PurpleMimePart *part)
{
fields_destroy(&part->fields);
g_string_free(part->data, TRUE);
part->data = NULL;
g_free(part);
}
PurpleMimePart *
purple_mime_part_new(PurpleMimeDocument *doc)
{
g_return_val_if_fail(doc != NULL, NULL);
return part_new(doc);
}
GList *
purple_mime_part_get_fields(PurpleMimePart *part)
{
g_return_val_if_fail(part != NULL, NULL);
return part->fields.keys;
}
const char *
purple_mime_part_get_field(PurpleMimePart *part, const char *field)
{
g_return_val_if_fail(part != NULL, NULL);
return fields_get(&part->fields, field);
}
char *
purple_mime_part_get_field_decoded(PurpleMimePart *part, const char *field)
{
const char *f;
g_return_val_if_fail(part != NULL, NULL);
f = fields_get(&part->fields, field);
return purple_mime_decode_field(f);
}
void
purple_mime_part_set_field(PurpleMimePart *part, const char *field, const char *value)
{
g_return_if_fail(part != NULL);
fields_set(&part->fields, field, value);
}
const char *
purple_mime_part_get_data(PurpleMimePart *part)
{
g_return_val_if_fail(part != NULL, NULL);
g_return_val_if_fail(part->data != NULL, NULL);
return part->data->str;
}
void
purple_mime_part_get_data_decoded(PurpleMimePart *part, guchar **data, gsize *len)
{
const char *enc;
g_return_if_fail(part != NULL);
g_return_if_fail(data != NULL);
g_return_if_fail(len != NULL);
g_return_if_fail(part->data != NULL);
enc = purple_mime_part_get_field(part, "content-transfer-encoding");
if(! enc) {
*data = (guchar *)g_strdup(part->data->str);
*len = part->data->len;
} else if(! g_ascii_strcasecmp(enc, "7bit")) {
*data = (guchar *)g_strdup(part->data->str);
*len = part->data->len;
} else if(! g_ascii_strcasecmp(enc, "8bit")) {
*data = (guchar *)g_strdup(part->data->str);
*len = part->data->len;
} else if(! g_ascii_strcasecmp(enc, "base16")) {
*data = purple_base16_decode(part->data->str, len);
} else if(! g_ascii_strcasecmp(enc, "base64")) {
*data = purple_base64_decode(part->data->str, len);
} else if(! g_ascii_strcasecmp(enc, "quoted-printable")) {
*data = purple_quotedp_decode(part->data->str, len);
} else {
purple_debug_warning("mime", "purple_mime_part_get_data_decoded:"
" unknown encoding '%s'\n", enc);
*data = NULL;
*len = 0;
}
}
gsize
purple_mime_part_get_length(PurpleMimePart *part)
{
g_return_val_if_fail(part != NULL, 0);
g_return_val_if_fail(part->data != NULL, 0);
return part->data->len;
}
void
purple_mime_part_set_data(PurpleMimePart *part, const char *data)
{
g_return_if_fail(part != NULL);
g_string_free(part->data, TRUE);
part->data = g_string_new(data);
}
PurpleMimeDocument *
purple_mime_document_new()
{
PurpleMimeDocument *doc;
doc = g_new0(PurpleMimeDocument, 1);
fields_init(&doc->fields);
return doc;
}
static void
doc_parts_load(PurpleMimeDocument *doc, const char *boundary, const char *buf, gsize len)
{
char *b = (char *) buf;
gsize n = len;
char *bnd;
gsize bl;
bnd = g_strdup_printf("--%s", boundary);
bl = strlen(bnd);
for(b = g_strstr_len(b, n, bnd); b; ) {
char *tail;
/* skip the boundary */
b += bl;
n -= bl;
/* skip the trailing \r\n or -- as well */
if(n >= 2) {
b += 2;
n -= 2;
}
/* find the next boundary */
tail = g_strstr_len(b, n, bnd);
if(tail) {
gsize sl;
sl = tail - b;
if(sl) {
PurpleMimePart *part = part_new(doc);
part_load(part, b, sl);
}
}
b = tail;
}
g_free(bnd);
}
PurpleMimeDocument *
purple_mime_document_parsen(const char *buf, gsize len)
{
PurpleMimeDocument *doc;
char *b = (char *) buf;
gsize n = len;
g_return_val_if_fail(buf != NULL, NULL);
doc = purple_mime_document_new();
if (!len)
return doc;
fields_load(&doc->fields, &b, &n);
{
const char *ct = fields_get(&doc->fields, "content-type");
if(ct && purple_str_has_prefix(ct, "multipart")) {
char *bd = strrchr(ct, '=');
if(bd++) {
doc_parts_load(doc, bd, b, n);
}
}
}
return doc;
}
PurpleMimeDocument *
purple_mime_document_parse(const char *buf)
{
g_return_val_if_fail(buf != NULL, NULL);
return purple_mime_document_parsen(buf, strlen(buf));
}
void
purple_mime_document_write(PurpleMimeDocument *doc, GString *str)
{
const char *bd = NULL;
g_return_if_fail(doc != NULL);
g_return_if_fail(str != NULL);
{
const char *ct = fields_get(&doc->fields, "content-type");
if(ct && purple_str_has_prefix(ct, "multipart")) {
char *b = strrchr(ct, '=');
if(b++) bd = b;
}
}
fields_write(&doc->fields, str);
if(bd) {
GList *l;
for(l = doc->parts; l; l = l->next) {
g_string_append_printf(str, "--%s\r\n", bd);
part_write(l->data, str);
if(! l->next) {
g_string_append_printf(str, "--%s--\r\n", bd);
}
}
}
}
GList *
purple_mime_document_get_fields(PurpleMimeDocument *doc)
{
g_return_val_if_fail(doc != NULL, NULL);
return doc->fields.keys;
}
const char *
purple_mime_document_get_field(PurpleMimeDocument *doc, const char *field)
{
g_return_val_if_fail(doc != NULL, NULL);
return fields_get(&doc->fields, field);
}
void
purple_mime_document_set_field(PurpleMimeDocument *doc, const char *field, const char *value)
{
g_return_if_fail(doc != NULL);
fields_set(&doc->fields, field, value);
}
GList *
purple_mime_document_get_parts(PurpleMimeDocument *doc)
{
g_return_val_if_fail(doc != NULL, NULL);
return doc->parts;
}
void
purple_mime_document_free(PurpleMimeDocument *doc)
{
if (!doc)
return;
fields_destroy(&doc->fields);
while(doc->parts) {
part_free(doc->parts->data);
doc->parts = g_list_delete_link(doc->parts, doc->parts);
}
g_free(doc);
}
|