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.
22 /************************ Static utility functions **************************/
24 /* MIME fields destructor */
26 static void silc_mime_field_dest(void *key, void *context, void *user_context)
32 /* Assembler fragment destructor */
34 static void silc_mime_assembler_dest(void *key, void *context,
38 silc_hash_table_free(context);
41 /* Assembler partial MIME destructor */
43 static void silc_mime_assemble_dest(void *key, void *context,
46 silc_mime_free(context);
50 /******************************* Public API *********************************/
52 /* Allocate MIME context */
54 SilcMime silc_mime_alloc(void)
58 mime = silc_calloc(1, sizeof(*mime));
62 mime->fields = silc_hash_table_alloc(0, silc_hash_string, mime,
63 silc_hash_string_compare, mime,
64 silc_mime_field_dest, mime, TRUE);
73 /* Free MIME context */
75 void silc_mime_free(SilcMime mime)
80 silc_hash_table_free(mime->fields);
82 if (mime->multiparts) {
83 silc_dlist_start(mime->multiparts);
84 while ((m = silc_dlist_get(mime->multiparts)) != SILC_LIST_END)
86 silc_dlist_uninit(mime->multiparts);
88 silc_free(mime->boundary);
89 silc_free(mime->multitype);
90 silc_free(mime->data);
94 /* Allocate MIME assembler */
96 SilcMimeAssembler silc_mime_assembler_alloc(void)
98 SilcMimeAssembler assembler;
100 assembler = silc_calloc(1, sizeof(*assembler));
104 assembler->fragments =
105 silc_hash_table_alloc(0, silc_hash_string, NULL,
106 silc_hash_string_compare, NULL,
107 silc_mime_assembler_dest, assembler, TRUE);
108 if (!assembler->fragments) {
109 silc_mime_assembler_free(assembler);
116 /* Free MIME assembler */
118 void silc_mime_assembler_free(SilcMimeAssembler assembler)
120 silc_hash_table_free(assembler->fragments);
121 silc_free(assembler);
124 /* Decode MIME message */
126 SilcMime silc_mime_decode(SilcMime mime, const unsigned char *data,
131 char *tmp, *field, *value, *line;
133 SILC_LOG_DEBUG(("Parsing MIME message"));
139 mime = silc_mime_alloc();
145 /* Parse the fields */
146 line = tmp = (char *)data;
147 for (i = 0; i < data_len; i++) {
149 if (data_len - i >= 2 && tmp[i] == '\r' && tmp[i + 1] == '\n') {
151 field = strchr(line, ':');
154 field = silc_memdup(line, field - line);
158 /* Get value. Remove whitespaces too. */
159 value = strchr(line, ':');
160 if ((tmp + i) - value < 2)
163 for (k = 0; k < (tmp + i) - value; k++) {
164 if (value[k] == '\r')
166 if (value[k] != ' ' && value[k] != '\t')
170 if ((tmp + i) - value < 1)
172 value = silc_memdup(value, (tmp + i) - value);
176 SILC_LOG_DEBUG(("Header '%s' '%s'", field, value));
178 /* Add field and value */
179 silc_mime_add_field(mime, field, value);
183 /* Mark start of next line */
184 line = (tmp + i) + 2;
187 /* Break if this is last header */
188 if (data_len - i >= 2 &&
189 tmp[i] == '\r' && tmp[i + 1] == '\n') {
196 /* Parse multiparts if present */
197 field = (char *)silc_mime_get_field(mime, "Content-Type");
198 if (field && strstr(field, "multipart")) {
202 mime->multiparts = silc_dlist_init();
203 if (!mime->multiparts)
206 /* Get multipart type */
207 value = strchr(field, '/');
211 if (strchr(field, '"'))
213 if (!strchr(field, ';'))
215 memset(b, 0, sizeof(b));
216 strncat(b, value, strchr(field, ';') - value);
218 *strchr(b, '"') = '\0';
219 mime->multitype = silc_memdup(b, strlen(b));
222 value = strrchr(field, '=');
223 if (value && strlen(value) > 1) {
226 SILC_LOG_DEBUG(("Boundary '%s'", value));
228 memset(b, 0, sizeof(b));
229 line = strdup(value);
230 if (strrchr(line, '"')) {
231 *strrchr(line, '"') = '\0';
232 snprintf(b, sizeof(b) - 1, "--%s", line + 1);
233 mime->boundary = strdup(line + 1);
235 snprintf(b, sizeof(b) - 1, "--%s", line);
236 mime->boundary = strdup(line);
240 for (i = i; i < data_len; i++) {
241 /* Get boundary data */
242 if (data_len - i >= strlen(b) &&
243 tmp[i] == '-' && tmp[i + 1] == '-') {
244 if (memcmp(tmp + i, b, strlen(b)))
249 if (data_len - i >= 4 &&
250 tmp[i ] == '\r' && tmp[i + 1] == '\n' &&
251 tmp[i + 2] == '\r' && tmp[i + 3] == '\n')
253 else if (data_len - i >= 2 &&
254 tmp[i] == '\r' && tmp[i + 1] == '\n')
256 else if (data_len - i >= 2 &&
257 tmp[i] == '-' && tmp[i + 1] == '-')
262 /* Find end of boundary */
263 for (k = i; k < data_len; k++)
264 if (data_len - k >= strlen(b) &&
265 tmp[k] == '-' && tmp[k + 1] == '-')
266 if (!memcmp(tmp + k, b, strlen(b)))
271 /* Remove preceding CRLF */
275 p = silc_mime_decode(NULL, line, k - i);
279 silc_dlist_add(mime->multiparts, p);
288 SILC_LOG_DEBUG(("Data len %d", data_len - i));
289 silc_mime_add_data(mime, tmp + i, data_len - i);
300 /* Encode MIME message */
302 unsigned char *silc_mime_encode(SilcMime mime, SilcUInt32 *encoded_len)
305 SilcHashTableList htl;
306 SilcBufferStruct buf;
308 char *field, *value, tmp[1024], tmp2[4];
312 SILC_LOG_DEBUG(("Encoding MIME message"));
317 memset(&buf, 0, sizeof(buf));
319 /* Encode the headers. Order doesn't matter */
321 silc_hash_table_list(mime->fields, &htl);
322 while (silc_hash_table_get(&htl, (void **)&field, (void **)&value)) {
323 memset(tmp, 0, sizeof(tmp));
324 SILC_LOG_DEBUG(("Header %s: %s", field, value));
325 snprintf(tmp, sizeof(tmp) - 1, "%s: %s\r\n", field, value);
326 silc_buffer_strformat(&buf, tmp, SILC_STRFMT_END);
329 silc_hash_table_list_reset(&htl);
331 silc_buffer_strformat(&buf, "\r\n", SILC_STRFMT_END);
333 /* Assemble the whole buffer */
334 buffer = silc_buffer_alloc_size(mime->data_len + silc_buffer_len(&buf));
339 if (silc_buffer_len(&buf)) {
340 silc_buffer_put(buffer, buf.head, silc_buffer_len(&buf));
341 silc_buffer_pull(buffer, silc_buffer_len(&buf));
346 SILC_LOG_DEBUG(("Data len %d", mime->data_len));
347 silc_buffer_put(buffer, mime->data, mime->data_len);
351 if (mime->multiparts) {
352 SILC_LOG_DEBUG(("Encoding multiparts"));
354 silc_dlist_start(mime->multiparts);
356 while ((part = silc_dlist_get(mime->multiparts)) != SILC_LIST_END) {
360 /* Recursive encoding */
361 pd = silc_mime_encode(part, &pd_len);
365 memset(tmp, 0, sizeof(tmp));
366 memset(tmp2, 0, sizeof(tmp2));
368 /* If fields are not present, add extra CRLF */
369 if (!silc_hash_table_count(part->fields))
370 snprintf(tmp2, sizeof(tmp2) - 1, "\r\n");
371 snprintf(tmp, sizeof(tmp) - 1, "%s--%s\r\n%s",
372 i != 0 ? "\r\n" : "", mime->boundary, tmp2);
375 buffer = silc_buffer_realloc(buffer, silc_buffer_truelen(buffer) +
376 pd_len + strlen(tmp));
379 silc_buffer_put_tail(buffer, tmp, strlen(tmp));
380 silc_buffer_pull_tail(buffer, strlen(tmp));
381 silc_buffer_put_tail(buffer, pd, pd_len);
382 silc_buffer_pull_tail(buffer, pd_len);
386 memset(tmp, 0, sizeof(tmp));
387 snprintf(tmp, sizeof(tmp) - 1, "\r\n--%s--\r\n", mime->boundary);
388 buffer = silc_buffer_realloc(buffer, silc_buffer_truelen(buffer) +
392 silc_buffer_put_tail(buffer, tmp, strlen(tmp));
393 silc_buffer_pull_tail(buffer, strlen(tmp));
396 ret = silc_buffer_steal(buffer, encoded_len);
397 silc_buffer_free(buffer);
402 /* Assembles MIME message from partial MIME messages */
404 SilcMime silc_mime_assemble(SilcMimeAssembler assembler, SilcMime partial)
406 char *type, *id = NULL, *tmp;
408 SilcMime p, complete;
409 int i, number, total = -1;
410 const unsigned char *data;
412 SilcBuffer compbuf = NULL;
414 SILC_LOG_DEBUG(("Assembling MIME fragments"));
416 if (!assembler || !partial)
419 type = (char *)silc_mime_get_field(partial, "Content-Type");
424 tmp = strstr(type, "id=");
427 if (strlen(tmp) <= 4)
434 *strchr(id, ';') = '\0';
435 if (strrchr(id, '"'))
436 *strrchr(id, '"') = '\0';
438 SILC_LOG_DEBUG(("Fragment ID %s", id));
440 /* Get fragment number */
441 tmp = strstr(type, "number=");
444 tmp = strchr(tmp, '=');
448 if (strchr(tmp, ';')) {
450 *strchr(tmp, ';') = '\0';
457 SILC_LOG_DEBUG(("Fragment number %d", number));
459 /* Find fragments with this ID. */
460 if (!silc_hash_table_find(assembler->fragments, (void *)id,
461 NULL, (void **)&f)) {
462 /* This is new fragment to new message. Add to hash table and return. */
463 f = silc_hash_table_alloc(0, silc_hash_uint, NULL, NULL, NULL,
464 silc_mime_assemble_dest, NULL, TRUE);
467 silc_hash_table_add(f, SILC_32_TO_PTR(number), partial);
468 silc_hash_table_add(assembler->fragments, id, f);
472 /* Try to get total number */
473 tmp = strstr(type, "total=");
475 tmp = strchr(tmp, '=');
479 if (strchr(tmp, ';')) {
481 *strchr(tmp, ';') = '\0';
488 SILC_LOG_DEBUG(("Fragment total %d", total));
491 /* If more fragments to come, add to hash table */
492 if (number != total) {
493 silc_hash_table_add(f, SILC_32_TO_PTR(number), partial);
497 silc_hash_table_add(f, SILC_32_TO_PTR(number), partial);
499 /* Verify that we really have all the fragments */
500 if (silc_hash_table_count(f) < total)
503 /* Assemble the complete MIME message now. We get them in order from
505 for (i = 1; i <= total; i++) {
506 if (!silc_hash_table_find(f, SILC_32_TO_PTR(i), NULL, (void **)&p))
509 /* The fragment is in the data portion of the partial message */
510 data = silc_mime_get_data(p, &data_len);
516 compbuf = silc_buffer_alloc_size(data_len);
519 silc_buffer_put(compbuf, data, data_len);
521 compbuf = silc_buffer_realloc(compbuf, silc_buffer_truelen(compbuf) +
525 silc_buffer_put_tail(compbuf, data, data_len);
526 silc_buffer_pull_tail(compbuf, data_len);
530 /* Now parse the complete MIME message and deliver it */
531 complete = silc_mime_decode(NULL, (const unsigned char *)compbuf->head,
532 silc_buffer_truelen(compbuf));
536 /* Delete the hash table entry. Destructors will free memory */
537 silc_hash_table_del(assembler->fragments, (void *)id);
539 silc_buffer_free(compbuf);
546 silc_buffer_free(compbuf);
547 silc_mime_free(partial);
551 /* Encodes partial MIME messages */
553 SilcDList silc_mime_encode_partial(SilcMime mime, int max_size)
555 unsigned char *buf, *tmp;
556 SilcUInt32 buf_len, len, tmp_len, off;
560 char type[128], id[64];
563 SILC_LOG_DEBUG(("Fragmenting MIME message"));
565 /* Encode as normal */
566 buf = silc_mime_encode(mime, &buf_len);
570 list = silc_dlist_init();
572 /* Fragment if it is too large */
573 if (buf_len > max_size) {
574 memset(id, 0, sizeof(id));
575 memset(type, 0, sizeof(type));
576 gethostname(type, sizeof(type) - 1);
577 srand((time(NULL) + buf_len) ^ rand());
578 snprintf(id, sizeof(id) - 1, "%X%X%X%s",
579 (unsigned int)rand(), (unsigned int)time(NULL),
580 (unsigned int)buf_len, type);
582 SILC_LOG_DEBUG(("Fragment ID %s", id));
584 partial = silc_mime_alloc();
588 silc_mime_add_field(partial, "MIME-Version", "1.0");
589 memset(type, 0, sizeof(type));
590 snprintf(type, sizeof(type) - 1,
591 "message/partial; id=\"%s\"; number=1", id);
592 silc_mime_add_field(partial, "Content-Type", type);
593 silc_mime_add_data(partial, buf, max_size);
595 tmp = silc_mime_encode(partial, &tmp_len);
598 silc_mime_free(partial);
601 buffer = silc_buffer_alloc_size(tmp_len);
604 silc_buffer_put(buffer, tmp, tmp_len);
605 silc_dlist_add(list, buffer);
608 len = buf_len - max_size;
612 partial = silc_mime_alloc();
616 memset(type, 0, sizeof(type));
617 silc_mime_add_field(partial, "MIME-Version", "1.0");
619 if (len > max_size) {
620 snprintf(type, sizeof(type) - 1,
621 "message/partial; id=\"%s\"; number=%d",
623 silc_mime_add_data(partial, buf + off, max_size);
627 snprintf(type, sizeof(type) - 1,
628 "message/partial; id=\"%s\"; number=%d; total=%d",
630 silc_mime_add_data(partial, buf + off, len);
634 silc_mime_add_field(partial, "Content-Type", type);
636 tmp = silc_mime_encode(partial, &tmp_len);
639 silc_mime_free(partial);
642 buffer = silc_buffer_alloc_size(tmp_len);
645 silc_buffer_put(buffer, tmp, tmp_len);
646 silc_dlist_add(list, buffer);
650 /* No need to fragment */
651 buffer = silc_buffer_alloc_size(buf_len);
654 silc_buffer_put(buffer, buf, buf_len);
655 silc_dlist_add(list, buffer);
663 /* Free partial MIME list */
665 void silc_mime_partial_free(SilcDList partials)
672 silc_dlist_start(partials);
673 while ((buf = silc_dlist_get(partials)) != SILC_LIST_END)
674 silc_buffer_free(buf);
675 silc_dlist_uninit(partials);
680 void silc_mime_add_field(SilcMime mime, const char *field, const char *value)
682 if (!mime || !field || !value)
685 silc_hash_table_add(mime->fields, strdup(field), strdup(value));
690 const char *silc_mime_get_field(SilcMime mime, const char *field)
697 if (!silc_hash_table_find(mime->fields, (void *)field,
698 NULL, (void **)&value))
701 return (const char *)value;
706 void silc_mime_add_data(SilcMime mime, const unsigned char *data,
713 silc_free(mime->data);
715 mime->data = silc_memdup(data, data_len);
716 mime->data_len = data_len;
721 const unsigned char *silc_mime_get_data(SilcMime mime, SilcUInt32 *data_len)
727 *data_len = mime->data_len;
732 /* Returns TRUE if partial message */
734 SilcBool silc_mime_is_partial(SilcMime mime)
736 const char *type = silc_mime_get_field(mime, "Content-Type");
740 if (!strstr(type, "message/partial"))
746 /* Set as multipart message */
748 void silc_mime_set_multipart(SilcMime mime, const char *type,
749 const char *boundary)
753 if (!mime || !type || !boundary)
756 memset(tmp, 0, sizeof(tmp));
757 snprintf(tmp, sizeof(tmp) - 1, "multipart/%s; boundary=%s", type, boundary);
758 silc_mime_add_field(mime, "Content-Type", tmp);
759 silc_free(mime->boundary);
760 mime->boundary = strdup(boundary);
762 if (mime->multiparts)
764 mime->multiparts = silc_dlist_init();
769 SilcBool silc_mime_add_multipart(SilcMime mime, SilcMime part)
771 if (!mime || !mime->multiparts || !part)
774 silc_dlist_add(mime->multiparts, part);
778 /* Return TRUE if has multiparts */
780 SilcBool silc_mime_is_multipart(SilcMime mime)
785 return mime->multiparts != NULL;
788 /* Returns multiparts */
790 SilcDList silc_mime_get_multiparts(SilcMime mime, const char **type)
796 *type = (const char *)mime->multitype;
798 return mime->multiparts;