Merged silc_1_1_branch to trunk.
[silc.git] / lib / silcutil / silcmime.c
index 14fde956f14e7b2379ca973d535294ca5e1acc24..c0817ec0f594d1eaed43c27d1f88fac673929116 100644 (file)
@@ -4,7 +4,7 @@
 
   Author: Pekka Riikonen <priikone@silcnet.org>
 
-  Copyright (C) 2005 - 2006 Pekka Riikonen
+  Copyright (C) 2005 - 2007 Pekka Riikonen
 
   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
@@ -198,6 +198,7 @@ SilcMime silc_mime_decode(SilcMime mime, const unsigned char *data,
   if (field && strstr(field, "multipart")) {
     char b[1024];
     SilcMime p;
+    unsigned int len;
 
     mime->multiparts = silc_dlist_init();
     if (!mime->multiparts)
@@ -213,7 +214,10 @@ SilcMime silc_mime_decode(SilcMime mime, const unsigned char *data,
     if (!strchr(field, ';'))
       goto err;
     memset(b, 0, sizeof(b));
-    strncat(b, value, strchr(field, ';') - value);
+    len = (unsigned int)(strchr(field, ';') - value);
+    if (len > sizeof(b) - 1)
+      goto err;
+    strncpy(b, value, len);
     if (strchr(b, '"'))
       *strchr(b, '"') = '\0';
     mime->multitype = silc_memdup(b, strlen(b));
@@ -229,10 +233,10 @@ SilcMime silc_mime_decode(SilcMime mime, const unsigned char *data,
       line = strdup(value);
       if (strrchr(line, '"')) {
        *strrchr(line, '"') = '\0';
-       silc_silc_snprintf(b, sizeof(b) - 1, "--%s", line + 1);
+       silc_snprintf(b, sizeof(b) - 1, "--%s", line + 1);
        mime->boundary = strdup(line + 1);
       } else {
-       silc_silc_snprintf(b, sizeof(b) - 1, "--%s", line);
+       silc_snprintf(b, sizeof(b) - 1, "--%s", line);
        mime->boundary = strdup(line);
       }
       silc_free(line);
@@ -322,10 +326,10 @@ unsigned char *silc_mime_encode(SilcMime mime, SilcUInt32 *encoded_len)
   /* Encode the headers. Order doesn't matter */
   i = 0;
   silc_hash_table_list(mime->fields, &htl);
-  while (silc_hash_table_get(&htl, (void **)&field, (void **)&value)) {
+  while (silc_hash_table_get(&htl, (void *)&field, (void *)&value)) {
     memset(tmp, 0, sizeof(tmp));
     SILC_LOG_DEBUG(("Header %s: %s", field, value));
-    silc_silc_snprintf(tmp, sizeof(tmp) - 1, "%s: %s\r\n", field, value);
+    silc_snprintf(tmp, sizeof(tmp) - 1, "%s: %s\r\n", field, value);
     silc_buffer_strformat(&buf, tmp, SILC_STRFMT_END);
     i++;
   }
@@ -342,6 +346,7 @@ unsigned char *silc_mime_encode(SilcMime mime, SilcUInt32 *encoded_len)
   if (silc_buffer_len(&buf)) {
     silc_buffer_put(buffer, buf.head, silc_buffer_len(&buf));
     silc_buffer_pull(buffer, silc_buffer_len(&buf));
+    silc_buffer_purge(&buf);
   }
 
   /* Add data */
@@ -370,8 +375,8 @@ unsigned char *silc_mime_encode(SilcMime mime, SilcUInt32 *encoded_len)
 
       /* If fields are not present, add extra CRLF */
       if (!silc_hash_table_count(part->fields))
-       silc_silc_snprintf(tmp2, sizeof(tmp2) - 1, "\r\n");
-      silc_silc_snprintf(tmp, sizeof(tmp) - 1, "%s--%s\r\n%s",
+       silc_snprintf(tmp2, sizeof(tmp2) - 1, "\r\n");
+      silc_snprintf(tmp, sizeof(tmp) - 1, "%s--%s\r\n%s",
               i != 0 ? "\r\n" : "", mime->boundary, tmp2);
       i = 1;
 
@@ -387,7 +392,7 @@ unsigned char *silc_mime_encode(SilcMime mime, SilcUInt32 *encoded_len)
     }
 
     memset(tmp, 0, sizeof(tmp));
-    silc_silc_snprintf(tmp, sizeof(tmp) - 1, "\r\n--%s--\r\n", mime->boundary);
+    silc_snprintf(tmp, sizeof(tmp) - 1, "\r\n--%s--\r\n", mime->boundary);
     buffer = silc_buffer_realloc(buffer, silc_buffer_truelen(buffer) +
                                 strlen(tmp));
     if (!buffer)
@@ -461,7 +466,7 @@ SilcMime silc_mime_assemble(SilcMimeAssembler assembler, SilcMime partial)
 
   /* Find fragments with this ID. */
   if (!silc_hash_table_find(assembler->fragments, (void *)id,
-                           NULL, (void **)&f)) {
+                           NULL, (void *)&f)) {
     /* This is new fragment to new message.  Add to hash table and return. */
     f = silc_hash_table_alloc(0, silc_hash_uint, NULL, NULL, NULL,
                              silc_mime_assemble_dest, NULL, TRUE);
@@ -506,7 +511,7 @@ SilcMime silc_mime_assemble(SilcMimeAssembler assembler, SilcMime partial)
   /* Assemble the complete MIME message now. We get them in order from
      the hash table. */
   for (i = 1; i <= total; i++) {
-    if (!silc_hash_table_find(f, SILC_32_TO_PTR(i), NULL, (void **)&p))
+    if (!silc_hash_table_find(f, SILC_32_TO_PTR(i), NULL, (void *)&p))
       goto err;
 
     /* The fragment is in the data portion of the partial message */
@@ -578,7 +583,7 @@ SilcDList silc_mime_encode_partial(SilcMime mime, int max_size)
     memset(type, 0, sizeof(type));
     gethostname(type, sizeof(type) - 1);
     srand((time(NULL) + buf_len) ^ rand());
-    silc_silc_snprintf(id, sizeof(id) - 1, "%X%X%X%s",
+    silc_snprintf(id, sizeof(id) - 1, "%X%X%X%s",
             (unsigned int)rand(), (unsigned int)time(NULL),
             (unsigned int)buf_len, type);
 
@@ -590,7 +595,7 @@ SilcDList silc_mime_encode_partial(SilcMime mime, int max_size)
 
     silc_mime_add_field(partial, "MIME-Version", "1.0");
     memset(type, 0, sizeof(type));
-    silc_silc_snprintf(type, sizeof(type) - 1,
+    silc_snprintf(type, sizeof(type) - 1,
             "message/partial; id=\"%s\"; number=1", id);
     silc_mime_add_field(partial, "Content-Type", type);
     silc_mime_add_data(partial, buf, max_size);
@@ -620,14 +625,14 @@ SilcDList silc_mime_encode_partial(SilcMime mime, int max_size)
       silc_mime_add_field(partial, "MIME-Version", "1.0");
 
       if (len > max_size) {
-       silc_silc_snprintf(type, sizeof(type) - 1,
+       silc_snprintf(type, sizeof(type) - 1,
                 "message/partial; id=\"%s\"; number=%d",
                 id, num++);
        silc_mime_add_data(partial, buf + off, max_size);
        off += max_size;
        len -= max_size;
       } else {
-       silc_silc_snprintf(type, sizeof(type) - 1,
+       silc_snprintf(type, sizeof(type) - 1,
                 "message/partial; id=\"%s\"; number=%d; total=%d",
                 id, num, num);
        silc_mime_add_data(partial, buf + off, len);
@@ -698,7 +703,7 @@ const char *silc_mime_get_field(SilcMime mime, const char *field)
     return NULL;
 
   if (!silc_hash_table_find(mime->fields, (void *)field,
-                           NULL, (void **)&value))
+                           NULL, (void *)&value))
     return NULL;
 
   return (const char *)value;
@@ -777,7 +782,7 @@ void silc_mime_set_multipart(SilcMime mime, const char *type,
     return;
 
   memset(tmp, 0, sizeof(tmp));
-  silc_silc_snprintf(tmp, sizeof(tmp) - 1, "multipart/%s; boundary=%s", type, boundary);
+  silc_snprintf(tmp, sizeof(tmp) - 1, "multipart/%s; boundary=%s", type, boundary);
   silc_mime_add_field(mime, "Content-Type", tmp);
   silc_free(mime->boundary);
   mime->boundary = strdup(boundary);