]> code.citadel.org Git - citadel.git/blobdiff - webcit/mime_parser.c
* serv_crypto.c: made changes to OpenSSL calls ... removed unnecessary
[citadel.git] / webcit / mime_parser.c
index 2fa52d9a1e517304b0677d15c075ea35f5e698b8..613b9258e51cccd2fa2ab5fb72fceffb2515855f 100644 (file)
@@ -3,11 +3,12 @@
  *
  * This is the MIME parser for Citadel.  Sometimes it actually works.
  *
- * Copyright (c) 1998-2002 by Art Cancro
+ * Copyright (c) 1998-2003 by Art Cancro
  * This code is distributed under the terms of the GNU General Public License.
  *
  */
 
+
 #include <stdlib.h>
 #include <unistd.h>
 #include <stdio.h>
@@ -17,7 +18,9 @@
 #include <string.h>
 #include <sys/stat.h>
 #include <errno.h>
+
 #include "webcit.h"
+#include "mime_parser.h"
 
 
 void extract_key(char *target, char *source, char *key)
@@ -56,7 +59,7 @@ char *fixed_partnum(char *supplied_partnum) {
 /*
  * Convert "quoted-printable" to binary.  Returns number of bytes decoded.
  */
-int decode_quoted_printable(char *decoded, char *encoded, int sourcelen) {
+int CtdlDecodeQuotedPrintable(char *decoded, char *encoded, int sourcelen) {
        char buf[SIZ];
        int buf_length = 0;
        int soft_line_break = 0;
@@ -164,8 +167,6 @@ void mime_decode(char *partnum,
        char *decoded;
        size_t bytes_decoded = 0;
 
-       fprintf(stderr, "mime_decode() called\n");
-
        /* Some encodings aren't really encodings */
        if (!strcasecmp(encoding, "7bit"))
                strcpy(encoding, "");
@@ -186,7 +187,6 @@ void mime_decode(char *partnum,
        
        if ((strcasecmp(encoding, "base64"))
            && (strcasecmp(encoding, "quoted-printable"))) {
-               fprintf(stderr, "ERROR: unknown MIME encoding '%s'\n", encoding);
                return;
        }
        /*
@@ -195,24 +195,18 @@ void mime_decode(char *partnum,
         * will never be larger than the encoded data.  This is a safe
         * assumption with base64, uuencode, and quoted-printable.
         */
-       fprintf(stderr, "About to allocate %d bytes for decoded part\n",
-               length+2048);
-       decoded = malloc(length+2048);
+       decoded = mallok(length+2048);
        if (decoded == NULL) {
-               fprintf(stderr, "ERROR: cannot allocate memory.\n");
                return;
        }
-       fprintf(stderr, "Got it!\n");
 
-       fprintf(stderr, "Decoding %s\n", encoding);
        if (!strcasecmp(encoding, "base64")) {
-               bytes_decoded = decode_base64(decoded, part_start);
+               bytes_decoded = CtdlDecodeBase64(decoded, part_start, length);
        }
        else if (!strcasecmp(encoding, "quoted-printable")) {
-               bytes_decoded = decode_quoted_printable(decoded,
+               bytes_decoded = CtdlDecodeQuotedPrintable(decoded,
                                                        part_start, length);
        }
-       fprintf(stderr, "Bytes decoded: %d\n", bytes_decoded);
 
        if (bytes_decoded > 0) if (CallBack != NULL) {
                CallBack(name, filename, fixed_partnum(partnum),
@@ -220,7 +214,7 @@ void mime_decode(char *partnum,
                        content_type, bytes_decoded, "binary", userdata);
        }
 
-       free(decoded);
+       phree(decoded);
 }
 
 /*
@@ -277,7 +271,9 @@ void the_mime_parser(char *partnum,
        size_t content_length;
        char *encoding;
        char *disposition;
-       char *name;
+       char *name = NULL;
+       char *content_type_name;
+       char *content_disposition_name;
        char *filename;
        int is_multipart;
        int part_seq = 0;
@@ -285,35 +281,37 @@ void the_mime_parser(char *partnum,
        size_t length;
        char nested_partnum[SIZ];
 
-       fprintf(stderr, "the_mime_parser() called\n");
        ptr = content_start;
        content_length = 0;
 
-       boundary = malloc(SIZ);
+       boundary = mallok(SIZ);
        memset(boundary, 0, SIZ);
 
-       startary = malloc(SIZ);
+       startary = mallok(SIZ);
        memset(startary, 0, SIZ);
 
-       endary = malloc(SIZ);
+       endary = mallok(SIZ);
        memset(endary, 0, SIZ);
 
-       header = malloc(SIZ);
+       header = mallok(SIZ);
        memset(header, 0, SIZ);
 
-       content_type = malloc(SIZ);
+       content_type = mallok(SIZ);
        memset(content_type, 0, SIZ);
 
-       encoding = malloc(SIZ);
+       encoding = mallok(SIZ);
        memset(encoding, 0, SIZ);
 
-       name = malloc(SIZ);
-       memset(name, 0, SIZ);
+       content_type_name = mallok(SIZ);
+       memset(content_type_name, 0, SIZ);
 
-       filename = malloc(SIZ);
+       content_disposition_name = mallok(SIZ);
+       memset(content_disposition_name, 0, SIZ);
+
+       filename = mallok(SIZ);
        memset(filename, 0, SIZ);
 
-       disposition = malloc(SIZ);
+       disposition = mallok(SIZ);
        memset(disposition, 0, SIZ);
 
        /* If the caller didn't supply an endpointer, generate one by measure */
@@ -335,12 +333,16 @@ void the_mime_parser(char *partnum,
                if (!isspace(buf[0])) {
                        if (!strncasecmp(header, "Content-type: ", 14)) {
                                strcpy(content_type, &header[14]);
-                               extract_key(name, content_type, "name");
-                               fprintf(stderr, "Extracted content-type <%s>\n",
-                                       content_type);
+                               extract_key(content_type_name, content_type, "name");
+                               /* Deal with weird headers */
+                               if (strchr(content_type, ' '))
+                                       *(strchr(content_type, ' ')) = '\0';
+                               if (strchr(content_type, ';'))
+                                       *(strchr(content_type, ';')) = '\0';
                        }
                        if (!strncasecmp(header, "Content-Disposition: ", 21)) {
                                strcpy(disposition, &header[21]);
+                               extract_key(content_disposition_name, disposition, "name");
                                extract_key(filename, disposition, "filename");
                        }
                        if (!strncasecmp(header, "Content-length: ", 16)) {
@@ -357,16 +359,12 @@ void the_mime_parser(char *partnum,
                        strcat(header, buf);
        } while ((strlen(buf) > 0) && (*ptr != 0));
 
-       for (i = 0; i < strlen(disposition); ++i)
-               if (disposition[i] == ';')
-                       disposition[i] = 0;
-       while (isspace(disposition[0]))
-               strcpy(disposition, &disposition[1]);
-       for (i = 0; i < strlen(content_type); ++i)
-               if (content_type[i] == ';')
-                       content_type[i] = 0;
-       while (isspace(content_type[0]))
-               strcpy(content_type, &content_type[1]);
+       if (strchr(disposition, ';'))
+               *(strchr(disposition, ';')) = '\0';
+       striplt(disposition);
+       if (strchr(content_type, ';'))
+               *(strchr(content_type, ';')) = '\0';
+       striplt(content_type);
 
        if (strlen(boundary) > 0) {
                is_multipart = 1;
@@ -374,9 +372,6 @@ void the_mime_parser(char *partnum,
                is_multipart = 0;
        }
 
-       fprintf(stderr, "is_multipart=%d, boundary=<%s>\n",
-               is_multipart, boundary);
-
        /* If this is a multipart message, then recursively process it */
        part_start = NULL;
        if (is_multipart) {
@@ -389,20 +384,22 @@ void the_mime_parser(char *partnum,
                }
 
                /* Figure out where the boundaries are */
-               sprintf(startary, "--%s", boundary);
-               sprintf(endary, "--%s--", boundary);
+               snprintf(startary, SIZ, "--%s", boundary);
+               snprintf(endary, SIZ, "--%s--", boundary);
                do {
                        if ( (!strncasecmp(ptr, startary, strlen(startary)))
                           || (!strncasecmp(ptr, endary, strlen(endary))) ) {
-                               fprintf(stderr, "hit boundary!\n");
                                if (part_start != NULL) {
                                        if (strlen(partnum) > 0) {
-                                               sprintf(nested_partnum, "%s.%d",
-                                                       partnum, ++part_seq);
+                                               snprintf(nested_partnum,
+                                                        sizeof nested_partnum,
+                                                        "%s.%d", partnum,
+                                                        ++part_seq);
                                        }
                                        else {
-                                               sprintf(nested_partnum, "%d",
-                                                       ++part_seq);
+                                               snprintf(nested_partnum,
+                                                        sizeof nested_partnum,
+                                                        "%d", ++part_seq);
                                        }
                                        the_mime_parser(nested_partnum,
                                                    part_start, part_end,
@@ -419,7 +416,16 @@ void the_mime_parser(char *partnum,
                                part_end = ptr;
                                ++ptr;
                        }
-               } while ( (strcasecmp(ptr, endary)) && (ptr <= content_end) );
+                       /* If we pass out of scope in the MIME multipart (by
+                        * hitting the end boundary), force the pointer out
+                        * of scope so this loop ends.
+                        */
+                       if (ptr < content_end) {
+                               if (!strcasecmp(ptr, endary)) {
+                                       ptr = content_end++;
+                               }
+                       }
+               } while (ptr <= content_end);
                if (PostMultiPartCallBack != NULL) {
                        PostMultiPartCallBack("", "", partnum, "", NULL,
                                content_type, 0, encoding, userdata);
@@ -429,7 +435,6 @@ void the_mime_parser(char *partnum,
 
        /* If it's not a multipart message, then do something with it */
        if (!is_multipart) {
-               fprintf(stderr, "doing non-multipart thing\n");
                part_start = ptr;
                length = 0;
                while (ptr < content_end) {
@@ -437,11 +442,24 @@ void the_mime_parser(char *partnum,
                        ++length;
                }
                part_end = content_end;
+               /* fix an off-by-one error */
+               --part_end;
+               --length;
                
                /* Truncate if the header told us to */
                if ( (content_length > 0) && (length > content_length) ) {
                        length = content_length;
-                       fprintf(stderr, "truncated to %ld\n", (long)content_length);
+               }
+
+               /* Sometimes the "name" field is tacked on to Content-type,
+                * and sometimes it's tacked on to Content-disposition.  Use
+                * whichever one we have.
+                */
+               if (strlen(content_disposition_name) > strlen(content_type_name)) {
+                       name = content_disposition_name;
+               }
+               else {
+                       name = content_type_name;
                }
                
                mime_decode(partnum,
@@ -453,15 +471,16 @@ void the_mime_parser(char *partnum,
        }
 
 end_parser:    /* free the buffers!  end the oppression!! */
-       free(boundary);
-       free(startary);
-       free(endary);   
-       free(header);
-       free(content_type);
-       free(encoding);
-       free(name);
-       free(filename);
-       free(disposition);
+       phree(boundary);
+       phree(startary);
+       phree(endary);  
+       phree(header);
+       phree(content_type);
+       phree(encoding);
+       phree(content_type_name);
+       phree(content_disposition_name);
+       phree(filename);
+       phree(disposition);
 }
 
 
@@ -513,10 +532,10 @@ void mime_parser(char *content_start,
 )
 {
 
-       fprintf(stderr, "mime_parser() called\n");
        the_mime_parser("", content_start, content_end,
                        CallBack,
                        PreMultiPartCallBack,
                        PostMultiPartCallBack,
                        userdata, dont_decode);
 }
+