Handle empty data area and empty header area properly.
authorPekka Riikonen <priikone@silcnet.org>
Wed, 11 Oct 2006 12:29:36 +0000 (12:29 +0000)
committerPekka Riikonen <priikone@silcnet.org>
Wed, 11 Oct 2006 12:29:36 +0000 (12:29 +0000)
lib/silcutil/silcmime.c
lib/silcutil/tests/test_silcmime.c

index 1ea22ab81221e6911df136b04a7a3ea8d1abb90c..d8b8c8f756015bac57ee549fb06476dd58067b79 100644 (file)
@@ -271,6 +271,8 @@ SilcMime silc_mime_decode(SilcMime mime, const unsigned char *data,
          /* Remove preceding CRLF */
          k -= 2;
 
+ SILC_LOG_HEXDUMP(("line %d", k - i), line, k - i);
+
          /* Parse the part */
          p = silc_mime_decode(NULL, line, k - i);
          if (!p)
@@ -282,11 +284,13 @@ SilcMime silc_mime_decode(SilcMime mime, const unsigned char *data,
       }
     }
   } else {
-    /* Get data area */
-    if (i > data_len)
+    /* Get data area.  If we are at the end and we have fields present
+       there is no data area present, but, if fields are not present we
+       only have data area. */
+    if (i >= data_len && !silc_hash_table_count(mime->fields))
       i = 0;
     SILC_LOG_DEBUG(("Data len %d", data_len - i));
-    if (i != data_len)
+    if (data_len - i)
       silc_mime_add_data(mime, tmp + i, data_len - i);
   }
 
index 65567cdb9ff25f514b1f43f37b7274d47cd25d22..6ebcecd617c4b5769ee7fa5dcc276204f533af78 100644 (file)
@@ -22,7 +22,9 @@ int main(int argc, char **argv)
     silc_log_set_debug_string("*mime*");
   }
 
-  /* Simple MIME test */
+  /* 
+   * Simple MIME test
+   */
   SILC_LOG_DEBUG(("Allocating MIME message context"));
   mime = silc_mime_alloc();
   if (!mime)
@@ -60,7 +62,38 @@ int main(int argc, char **argv)
   silc_free(enc);
   silc_mime_free(mime);
 
-  /* Multipart test, with nesting */
+  /*
+   * Empty data area, only headers present
+   */
+  SILC_LOG_DEBUG(("Allocating MIME message context"));
+  mime = silc_mime_alloc();
+  if (!mime)
+    goto err;
+  SILC_LOG_DEBUG(("Adding Content-Transfer-Encoding: binary"));
+  silc_mime_add_field(mime, "Content-Transfer-Encoding", "binary");
+  SILC_LOG_DEBUG(("No data area, only header present"));
+  SILC_LOG_DEBUG(("Encoding MIME context"));
+  enc = silc_mime_encode(mime, &enc_len);
+  if (!enc)
+    goto err;
+  SILC_LOG_DEBUG(("Encoded MIME message: \n%s", enc));
+  silc_mime_free(mime);
+  SILC_LOG_DEBUG(("Decoding MIME message"));
+  mime = silc_mime_decode(NULL, enc, enc_len);
+  if (!mime)
+    goto err;
+  SILC_LOG_DEBUG(("Re-encoding MIME context"));
+  silc_free(enc);
+  enc = silc_mime_encode(mime, &enc_len);
+  if (!enc)
+    goto err;
+  SILC_LOG_HEXDUMP(("Re-encoded MIME message:"), enc, enc_len);
+  silc_free(enc);
+  silc_mime_free(mime);
+
+  /*
+   * Multipart test, with nesting
+   */
   SILC_LOG_DEBUG(("Allocating MIME message context"));
   mime = silc_mime_alloc();
   if (!mime)
@@ -106,7 +139,7 @@ int main(int argc, char **argv)
   part = silc_mime_alloc();
   if (!part)
     goto err;
-  SILC_LOG_DEBUG(("Adding MIME data, 10 A's + 1 B"));
+  SILC_LOG_DEBUG(("Adding MIME data (NO HEADERS), 10 A's + 1 B"));
   for (i = 0; i < 10; i++)
     tmp[i] = 'A';
   tmp[10] = 'B';
@@ -116,6 +149,16 @@ int main(int argc, char **argv)
     goto err;
   SILC_LOG_DEBUG(("Allocating part"));
   part = silc_mime_alloc();
+  if (!part)
+    goto err;
+  SILC_LOG_DEBUG(("Adding Content-Type: image/foobar"));
+  SILC_LOG_DEBUG(("No data area, only header"));
+  silc_mime_add_field(part, "Content-Type", "image/foobar");
+  SILC_LOG_DEBUG(("Adding part to MIME message"));
+  if (!silc_mime_add_multipart(mime, part))
+    goto err;
+  SILC_LOG_DEBUG(("Allocating part"));
+  part = silc_mime_alloc();
   if (!part)
     goto err;
   SILC_LOG_DEBUG(("Adding part to MIME message"));
@@ -171,7 +214,9 @@ int main(int argc, char **argv)
   }
   silc_mime_free(mime);
 
-  /* Fragmentation test */
+  /*
+   * Fragmentation test
+   */
   SILC_LOG_DEBUG(("Allocating MIME assembler"));
   ass = silc_mime_assembler_alloc();
   if (!ass)