[PATCH] Mime multipart parsing

Paul Bagyenda <[email protected]>
Newsgroups gmane.comp.mobile.kannel.devel
Message-ID <[email protected]>
Hi,
   There is a slight problem with the multipart parsing in Kannel's lib:
- when parsing an enclosed multipart element fails, the module crashes
- body elements that end in CRLF cause some problems.

Attached is a fix for this.

Cheers and happy new year.
mime.diff (application/octet-stream, 2.4 KB)
Index: gwlib/mime.c
===================================================================
RCS file: /home/cvs/gateway/gwlib/mime.c,v
retrieving revision 1.17
diff -u -r1.17 mime.c
--- gwlib/mime.c	18 Nov 2006 22:36:09 -0000	1.17
+++ gwlib/mime.c	3 Jan 2007 05:36:46 -0000
@@ -388,19 +388,19 @@
         octstr_append(seperator, boundary);
         while ((entity = parse_get_seperated_block(context, seperator)) != NULL) {
             MIMEEntity *m;
-
-            /* we have still two linefeeds at the beginning and end that we 
+	    int del2 = 0;
+	    
+            /* we have still linefeeds at the beginning and end that we 
              * need to remove, these are from the separator. 
-             * We check if it is \n or \r\n?! */
-            if (octstr_get_char(entity, 0) == '\r')
+             * We check if it is LF only or CRLF! */
+	    del2 = (octstr_get_char(entity, 0) == '\r');
+            if (del2) 
                 octstr_delete(entity, 0, 2);	      
             else
                 octstr_delete(entity, 0, 1);
 
-            if (octstr_get_char(entity, octstr_len(entity) - 2) == '\r' && 
-                octstr_get_char(entity, octstr_len(entity) - 4) == '\r')
-                octstr_delete(entity, octstr_len(entity) - 4, 4);
-            else if (octstr_get_char(entity, octstr_len(entity) - 2) == '\r')
+	    /* we assume the same mechanism applies to beginning and end -- seems reasonable! */
+            if (del2)
                 octstr_delete(entity, octstr_len(entity) - 2, 2);
             else
                 octstr_delete(entity, octstr_len(entity) - 1, 1);
@@ -411,15 +411,16 @@
 
             /* call ourself for this MIME entity and inject to list */
             m = mime_octstr_to_entity(entity);
-            gwlist_append(e->multiparts, m);
-
-            /* check if this entity is our start entity (in terms of related)
-             * and set our start pointer to it */
-            if (cid_matches(m->headers, start)) {
-                /* set only if none has been set before */
-                e->start = (e->start == NULL) ? m : e->start;
-            }
-
+	    if (m) {
+		 gwlist_append(e->multiparts, m);
+		 
+		 /* check if this entity is our start entity (in terms of related)
+		  * and set our start pointer to it */
+		 if (cid_matches(m->headers, start)) {
+		      /* set only if none has been set before */
+		      e->start = (e->start == NULL) ? m : e->start;
+		 }
+	    }
 
             octstr_destroy(entity);
         }
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.