chiark / gitweb /
merge extra MIME parsing
[disorder] / lib / mime.c
index c8ffe3192e922f634fb4a9ad41e55e84b9f99175..5562c7e5b8976d805cc76c5265335b2e42cf1d6a 100644 (file)
@@ -27,6 +27,8 @@
 #include <string.h>
 #include <ctype.h>
 
+#include <stdio.h>
+
 #include "mem.h"
 #include "mime.h"
 #include "vector.h"
@@ -295,7 +297,7 @@ static int isboundary(const char *ptr, const char *boundary, size_t bl) {
          && (iscrlf(ptr + bl + 2)
              || (ptr[bl + 2] == '-'
                  && ptr[bl + 3] == '-'
-                 && iscrlf(ptr + bl + 4))));
+                 && (iscrlf(ptr + bl + 4) || *(ptr + bl + 4) == 0))));
 }
 
 static int isfinal(const char *ptr, const char *boundary, size_t bl) {
@@ -304,7 +306,7 @@ static int isfinal(const char *ptr, const char *boundary, size_t bl) {
          && !strncmp(ptr + 2, boundary, bl)
          && ptr[bl + 2] == '-'
          && ptr[bl + 3] == '-'
-         && iscrlf(ptr + bl + 4));
+         && (iscrlf(ptr + bl + 4) || *(ptr + bl + 4) == 0));
 }
 
 /** @brief Parse a multipart MIME body
@@ -322,12 +324,16 @@ int mime_multipart(const char *s,
   const char *start, *e;
   int ret;
 
-  if(!isboundary(s, boundary, bl)) return -1;
+  /* We must start with a boundary string */
+  if(!isboundary(s, boundary, bl))
+    return -1;
+  /* Keep going until we hit a final boundary */
   while(!isfinal(s, boundary, bl)) {
     s = strstr(s, "\r\n") + 2;
     start = s;
     while(!isboundary(s, boundary, bl)) {
-      if(!(e = strstr(s, "\r\n"))) return -1;
+      if(!(e = strstr(s, "\r\n")))
+       return -1;
       s = e + 2;
     }
     if((ret = callback(xstrndup(start,