5 Author: Pekka Riikonen <priikone@silcnet.org>
7 Copyright (C) 2005 Pekka Riikonen
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; version 2 of the License.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
20 #include "silcincludes.h"
23 struct SilcMimeStruct {
31 struct SilcMimeAssemblerStruct {
32 SilcMimeComplete complete;
33 void *complete_context;
34 SilcHashTable fragments;
37 static void silc_mime_field_dest(void *key, void *context, void *user_context)
43 SilcMime silc_mime_alloc(void)
47 mime = silc_calloc(1, sizeof(*mime));
51 mime->fields = silc_hash_table_alloc(0, silc_hash_string, mime,
52 silc_hash_string_compare, mime,
53 silc_mime_field_dest, mime, TRUE);
62 void silc_mime_free(SilcMime mime)
67 silc_hash_table_free(mime->fields);
69 if (mime->multiparts) {
70 silc_dlist_start(mime->multiparts);
71 while ((m = silc_dlist_get(mime->multiparts)) != SILC_LIST_END)
73 silc_dlist_uninit(mime->multiparts);
75 silc_free(mime->boundary);
76 silc_free(mime->data);
80 static void silc_mime_assembler_dest(void *key, void *context,
84 silc_hash_table_free(context);
87 SilcMimeAssembler silc_mime_assembler_alloc(SilcMimeComplete complete,
88 void *complete_context)
90 SilcMimeAssembler assembler;
92 assembler = silc_calloc(1, sizeof(*assembler));
96 assembler->complete = complete;
97 assembler->complete_context = complete_context;
98 assembler->fragments =
99 silc_hash_table_alloc(0, silc_hash_string, NULL,
100 silc_hash_string_compare, NULL,
101 silc_mime_assembler_dest, assembler, TRUE);
102 if (!assembler->fragments) {
103 silc_mime_assembler_free(assembler);
110 void silc_mime_assembler_free(SilcMimeAssembler assembler)
112 silc_hash_table_free(assembler->fragments);
113 silc_free(assembler);
116 SilcMime silc_mime_decode(const unsigned char *data, SilcUInt32 data_len)
120 char *tmp, *field, *value, *line;
122 SILC_LOG_DEBUG(("Parsing MIME message"));
127 mime = silc_mime_alloc();
131 /* Parse the fields */
132 line = tmp = (char *)data;
133 for (i = 0; i < data_len; i++) {
135 if (data_len - i >= 2 && tmp[i] == '\r' && tmp[i + 1] == '\n') {
137 field = strchr(line, ':');
140 field = silc_memdup(line, field - line);
144 /* Get value. Remove whitespaces too. */
145 value = strchr(line, ':');
146 if ((tmp + i) - value < 2)
149 for (k = 0; k < (tmp + i) - value; k++) {
150 if (value[k] == '\r')
152 if (value[k] != ' ' && value[k] != '\t')
156 if ((tmp + i) - value < 1)
158 value = silc_memdup(value, (tmp + i) - value);
162 SILC_LOG_DEBUG(("Header '%s' '%s'", field, value));
164 /* Add field and value */
165 silc_mime_add_field(mime, field, value);
169 /* Mark start of next line */
170 line = (tmp + i) + 2;
173 /* Break if this is last header */
174 if (data_len - i >= 2 &&
175 tmp[i] == '\r' && tmp[i + 1] == '\n') {
182 /* Parse multiparts if present */
183 field = (char *)silc_mime_get_field(mime, "Content-Type");
184 if (field && strstr(field, "multipart")) {
188 mime->multiparts = silc_dlist_init();
189 if (!mime->multiparts)
193 value = strrchr(field, '=');
194 if (value && strlen(value) > 1) {
197 SILC_LOG_DEBUG(("Boundary '%s'", value));
199 memset(b, 0, sizeof(b));
200 line = strdup(value);
201 if (strrchr(line, '"')) {
202 *strrchr(line, '"') = '\0';
203 snprintf(b, sizeof(b) - 1, "--%s", line + 1);
204 mime->boundary = strdup(line + 1);
206 snprintf(b, sizeof(b) - 1, "--%s", line);
207 mime->boundary = strdup(line);
211 for (i = i; i < data_len; i++) {
212 /* Get boundary data */
213 if (data_len - i >= strlen(b) &&
214 tmp[i] == '-' && tmp[i + 1] == '-') {
215 if (memcmp(tmp + i, b, strlen(b)))
220 if (data_len - i >= 4 &&
221 tmp[i ] == '\r' && tmp[i + 1] == '\n' &&
222 tmp[i + 2] == '\r' && tmp[i + 3] == '\n')
224 else if (data_len - i >= 2 &&
225 tmp[i] == '\r' && tmp[i + 1] == '\n')
227 else if (data_len - i >= 2 &&
228 tmp[i] == '-' && tmp[i + 1] == '-')
233 /* Find end of boundary */
234 for (k = i; k < data_len; k++)
235 if (data_len - k >= strlen(b) &&
236 tmp[k] == '-' && tmp[k + 1] == '-')
237 if (!memcmp(tmp + k, b, strlen(b)))
242 /* Remove preceding CRLF */
246 p = silc_mime_decode(line, k - i);
250 silc_dlist_add(mime->multiparts, p);
259 SILC_LOG_DEBUG(("Data len %d", data_len - i));
260 silc_mime_add_data(mime, tmp + i, data_len - i);
266 silc_mime_free(mime);
270 unsigned char *silc_mime_encode(SilcMime mime, SilcUInt32 *encoded_len)
273 SilcHashTableList htl;
274 SilcBufferStruct buf;
276 char *field, *value, tmp[1024], tmp2[4];
280 SILC_LOG_DEBUG(("Encoding MIME message"));
285 memset(&buf, 0, sizeof(buf));
287 /* Encode the headers. Order doesn't matter */
289 silc_hash_table_list(mime->fields, &htl);
290 while (silc_hash_table_get(&htl, (void **)&field, (void **)&value)) {
291 memset(tmp, 0, sizeof(tmp));
292 SILC_LOG_DEBUG(("Header %s: %s", field, value));
293 snprintf(tmp, sizeof(tmp) - 1, "%s: %s\r\n", field, value);
294 silc_buffer_strformat(&buf, tmp, SILC_STRFMT_END);
297 silc_hash_table_list_reset(&htl);
299 silc_buffer_strformat(&buf, "\r\n", SILC_STRFMT_END);
301 /* Assemble the whole buffer */
302 buffer = silc_buffer_alloc_size(mime->data_len + buf.len);
308 silc_buffer_put(buffer, buf.head, buf.len);
309 silc_buffer_pull(buffer, buf.len);
314 SILC_LOG_DEBUG(("Data len %d", mime->data_len));
315 silc_buffer_put(buffer, mime->data, mime->data_len);
319 if (mime->multiparts) {
320 SILC_LOG_DEBUG(("Encoding multiparts"));
322 silc_dlist_start(mime->multiparts);
324 while ((part = silc_dlist_get(mime->multiparts)) != SILC_LIST_END) {
328 /* Recursive encoding */
329 pd = silc_mime_encode(part, &pd_len);
333 memset(tmp, 0, sizeof(tmp));
334 memset(tmp2, 0, sizeof(tmp2));
336 /* If fields are not present, add extra CRLF */
337 if (!silc_hash_table_count(part->fields))
338 snprintf(tmp2, sizeof(tmp2) - 1, "\r\n");
339 snprintf(tmp, sizeof(tmp) - 1, "%s--%s\r\n%s",
340 i != 0 ? "\r\n" : "", mime->boundary, tmp2);
343 buffer = silc_buffer_realloc(buffer, buffer->truelen + pd_len +
347 silc_buffer_put_tail(buffer, tmp, strlen(tmp));
348 silc_buffer_pull_tail(buffer, strlen(tmp));
349 silc_buffer_put_tail(buffer, pd, pd_len);
350 silc_buffer_pull_tail(buffer, pd_len);
354 memset(tmp, 0, sizeof(tmp));
355 snprintf(tmp, sizeof(tmp) - 1, "\r\n--%s--\r\n", mime->boundary);
356 buffer = silc_buffer_realloc(buffer, buffer->truelen + strlen(tmp));
359 silc_buffer_put_tail(buffer, tmp, strlen(tmp));
360 silc_buffer_pull_tail(buffer, strlen(tmp));
363 ret = silc_buffer_steal(buffer, encoded_len);
364 silc_buffer_free(buffer);
369 static void silc_mime_assemble_dest(void *key, void *context,
372 silc_mime_free(context);
375 void silc_mime_assemble(SilcMimeAssembler assembler, SilcMime partial)
377 char *type, *id = NULL, *tmp;
379 SilcMime p, complete;
380 int i, number, total = -1;
381 const unsigned char *data;
383 SilcBuffer compbuf = NULL;
385 SILC_LOG_DEBUG(("Assembling MIME fragments"));
387 if (!assembler || !partial)
390 type = (char *)silc_mime_get_field(partial, "Content-Type");
395 tmp = strstr(type, "id=");
398 if (strlen(tmp) <= 4)
405 *strchr(id, ';') = '\0';
406 if (strrchr(id, '"'))
407 *strrchr(id, '"') = '\0';
409 SILC_LOG_DEBUG(("Fragment ID %s", id));
411 /* Get fragment number */
412 tmp = strstr(type, "number=");
415 tmp = strchr(tmp, '=');
419 if (strchr(tmp, ';')) {
421 *strchr(tmp, ';') = '\0';
428 SILC_LOG_DEBUG(("Fragment number %d", number));
430 /* Find fragments with this ID. */
431 if (!silc_hash_table_find(assembler->fragments, (void *)id,
432 NULL, (void **)&f)) {
433 /* This is new fragment to new message. Add to hash table and return. */
434 f = silc_hash_table_alloc(0, silc_hash_uint, NULL, NULL, NULL,
435 silc_mime_assemble_dest, NULL, TRUE);
438 silc_hash_table_add(f, SILC_32_TO_PTR(number), partial);
439 silc_hash_table_add(assembler->fragments, id, f);
443 /* Try to get total number */
444 tmp = strstr(type, "total=");
446 tmp = strchr(tmp, '=');
450 if (strchr(tmp, ';')) {
452 *strchr(tmp, ';') = '\0';
459 SILC_LOG_DEBUG(("Fragment total %d", total));
462 /* If more fragments to come, add to hash table */
463 if (number != total) {
464 silc_hash_table_add(f, SILC_32_TO_PTR(number), partial);
468 silc_hash_table_add(f, SILC_32_TO_PTR(number), partial);
470 /* Verify that we really have all the fragments */
471 if (silc_hash_table_count(f) < total)
474 /* Assemble the complete MIME message now. We get them in order from
476 for (i = 1; i <= total; i++) {
477 if (!silc_hash_table_find(f, SILC_32_TO_PTR(i), NULL, (void **)&p))
480 /* The fragment is in the data portion of the partial message */
481 data = silc_mime_get_data(p, &data_len);
487 compbuf = silc_buffer_alloc_size(data_len);
490 silc_buffer_put(compbuf, data, data_len);
492 compbuf = silc_buffer_realloc(compbuf, compbuf->truelen + data_len);
495 silc_buffer_put_tail(compbuf, data, data_len);
496 silc_buffer_pull_tail(compbuf, data_len);
500 /* Now parse the complete MIME message and deliver it */
501 complete = silc_mime_decode((const unsigned char *)compbuf->head,
506 if (assembler->complete)
507 assembler->complete(complete, assembler->complete_context);
509 /* Delete the hash table entry. Destructors will free memory */
510 silc_hash_table_del(assembler->fragments, (void *)id);
513 silc_buffer_free(compbuf);
519 silc_buffer_free(compbuf);
520 silc_mime_free(partial);
523 SilcDList silc_mime_encode_partial(SilcMime mime, int max_size)
525 unsigned char *buf, *tmp;
526 SilcUInt32 buf_len, len, tmp_len, off;
530 char type[128], id[64];
533 SILC_LOG_DEBUG(("Fragmenting MIME message"));
535 /* Encode as normal */
536 buf = silc_mime_encode(mime, &buf_len);
540 list = silc_dlist_init();
542 /* Fragment if it is too large */
543 if (buf_len > max_size) {
544 memset(id, 0, sizeof(id));
545 memset(type, 0, sizeof(type));
546 gethostname(type, sizeof(type) - 1);
547 srand((time(NULL) + buf_len) ^ rand());
548 snprintf(id, sizeof(id) - 1, "%X%X%X%s",
549 (unsigned int)rand(), (unsigned int)time(NULL),
550 (unsigned int)buf_len, type);
552 SILC_LOG_DEBUG(("Fragment ID %s", id));
554 partial = silc_mime_alloc();
558 silc_mime_add_field(partial, "MIME-Version", "1.0");
559 memset(type, 0, sizeof(type));
560 snprintf(type, sizeof(type) - 1,
561 "message/partial; id=\"%s\"; number=1", id);
562 silc_mime_add_field(partial, "Content-Type", type);
563 silc_mime_add_data(partial, buf, max_size);
565 tmp = silc_mime_encode(partial, &tmp_len);
568 silc_mime_free(partial);
571 buffer = silc_buffer_alloc_size(tmp_len);
574 silc_buffer_put(buffer, tmp, tmp_len);
575 silc_dlist_add(list, buffer);
578 len = buf_len - max_size;
582 partial = silc_mime_alloc();
586 memset(type, 0, sizeof(type));
587 silc_mime_add_field(partial, "MIME-Version", "1.0");
589 if (len > max_size) {
590 snprintf(type, sizeof(type) - 1,
591 "message/partial; id=\"%s\"; number=%d",
593 silc_mime_add_data(partial, buf + off, max_size);
597 snprintf(type, sizeof(type) - 1,
598 "message/partial; id=\"%s\"; number=%d; total=%d",
600 silc_mime_add_data(partial, buf + off, len);
604 silc_mime_add_field(partial, "Content-Type", type);
606 tmp = silc_mime_encode(partial, &tmp_len);
609 silc_mime_free(partial);
612 buffer = silc_buffer_alloc_size(tmp_len);
615 silc_buffer_put(buffer, tmp, tmp_len);
616 silc_dlist_add(list, buffer);
620 /* No need to fragment */
621 buffer = silc_buffer_alloc_size(buf_len);
624 silc_buffer_put(buffer, buf, buf_len);
625 silc_dlist_add(list, buffer);
633 void silc_mime_partial_free(SilcDList partials)
640 silc_dlist_start(partials);
641 while ((buf = silc_dlist_get(partials)) != SILC_LIST_END)
642 silc_buffer_free(buf);
643 silc_dlist_uninit(partials);
646 void silc_mime_add_field(SilcMime mime, const char *field, const char *value)
648 if (!mime || !field || !value)
651 silc_hash_table_add(mime->fields, strdup(field), strdup(value));
654 const char *silc_mime_get_field(SilcMime mime, const char *field)
661 if (!silc_hash_table_find(mime->fields, (void *)field,
662 NULL, (void **)&value))
665 return (const char *)value;
668 void silc_mime_add_data(SilcMime mime, const unsigned char *data,
675 silc_free(mime->data);
677 mime->data = silc_memdup(data, data_len);
678 mime->data_len = data_len;
681 const unsigned char *silc_mime_get_data(SilcMime mime, SilcUInt32 *data_len)
687 *data_len = mime->data_len;
692 bool silc_mime_is_partial(SilcMime mime)
694 const char *type = silc_mime_get_field(mime, "Content-Type");
698 if (!strstr(type, "message/partial"))
704 void silc_mime_set_multipart(SilcMime mime, const char *type,
705 const char *boundary)
709 if (!mime || !type || !boundary)
712 memset(tmp, 0, sizeof(tmp));
713 snprintf(tmp, sizeof(tmp) - 1, "multipart/%s; boundary=%s", type, boundary);
714 silc_mime_add_field(mime, "Content-Type", tmp);
715 silc_free(mime->boundary);
716 mime->boundary = strdup(boundary);
718 if (mime->multiparts)
720 mime->multiparts = silc_dlist_init();
723 bool silc_mime_add_multipart(SilcMime mime, SilcMime part)
725 if (!mime || !mime->multiparts || !part)
728 silc_dlist_add(mime->multiparts, part);
732 bool silc_mime_is_multipart(SilcMime mime)
737 return mime->multiparts != NULL;
740 SilcDList silc_mime_get_multiparts(SilcMime mime)
745 return mime->multiparts;