]> code.citadel.org Git - citadel.git/commitdiff
* Brought over the updated mime_parser.c from citadel
authorArt Cancro <ajc@citadel.org>
Sat, 20 Jan 2007 19:20:39 +0000 (19:20 +0000)
committerArt Cancro <ajc@citadel.org>
Sat, 20 Jan 2007 19:20:39 +0000 (19:20 +0000)
webcit/mime_parser.c

index bf850a288d4edf41a81aa75b10f218cff2d91b02..33b249b5062ff3a050dd5532b073e270b550ef11 100644 (file)
 #include "webserver.h"
 #include "mime_parser.h"
 
-/**
- * \brief get mime key
- * \param target where to put the mime buffer at???
- * \param source where to extract the mimetype from
- * \param key what???
- */
+
 void extract_key(char *target, char *source, char *key)
 {
        int a, b;
@@ -40,11 +35,9 @@ void extract_key(char *target, char *source, char *key)
 }
 
 
-/**
- * \brief For non-multipart messages, we need to generate a quickie partnum of "1"
+/*
+ * For non-multipart messages, we need to generate a quickie partnum of "1"
  * to return to callback functions.  Some callbacks demand it.
- * \param supplied_partnum partnum to convert
- * \return the converted num
  */
 char *fixed_partnum(char *supplied_partnum) {
        if (supplied_partnum == NULL) return "1";
@@ -54,11 +47,8 @@ char *fixed_partnum(char *supplied_partnum) {
 
 
 
-/**
- * \brief Convert "quoted-printable" to binary.  Returns number of bytes decoded.
- * \param decoded the buffer with the decoded output
- * \param encoded the encoded string to decode
- * \param sourcelen length of the decoded buffer
+/*
+ * Convert "quoted-printable" to binary.  Returns number of bytes decoded.
  */
 int CtdlDecodeQuotedPrintable(char *decoded, char *encoded, int sourcelen) {
        char buf[SIZ];
@@ -70,11 +60,11 @@ int CtdlDecodeQuotedPrintable(char *decoded, char *encoded, int sourcelen) {
 
        decoded[0] = 0;
        decoded_length = 0;
-       buf[0] = 0;
+       memset(buf, '\0', SIZ);
        buf_length = 0;
 
        for (i = 0; i < sourcelen; ++i) {
-
+               size_t buflen = strlen(buf);
                buf[buf_length++] = encoded[i];
 
                if ( (encoded[i] == '\n')
@@ -84,28 +74,34 @@ int CtdlDecodeQuotedPrintable(char *decoded, char *encoded, int sourcelen) {
 
                        /*** begin -- process one line ***/
 
-                       if (buf[strlen(buf)-1] == '\n') {
-                               buf[strlen(buf)-1] = 0;
+                       if (buf[buflen-1] == '\n') {
+                               buf[buflen-1] = 0;
+                               buflen --;
                        }
-                       if (buf[strlen(buf)-1] == '\r') {
-                               buf[strlen(buf)-1] = 0;
+                       if (buf[buflen-1] == '\r') {
+                               buf[buflen-1] = 0;
+                               buflen --;
                        }
-                       while (isspace(buf[strlen(buf)-1])) {
-                               buf[strlen(buf)-1] = 0;
+                       while (isspace(buf[buflen-1])) {
+                               buf[buflen-1] = 0;
+                               buflen --;
                        }
                        soft_line_break = 0;
 
-                       while (strlen(buf) > 0) {
-                               if (!strcmp(buf, "=")) {
+                       while (buflen > 0) {
+                               if ((buf[0] ==  '=') && (buf[1] == '\0')) {
                                        soft_line_break = 1;
-                                       strcpy(buf, "");
-                               } else if ((strlen(buf)>=3) && (buf[0]=='=')) {
+                                       buf[0] = '\0';
+                                       buflen = 0;
+                               } else if ((buflen>=3) && (buf[0]=='=')) {
                                        sscanf(&buf[1], "%02x", &ch);
                                        decoded[decoded_length++] = ch;
                                        strcpy(buf, &buf[3]);
+                                       buflen -=3;
                                } else {
                                        decoded[decoded_length++] = buf[0];
                                        strcpy(buf, &buf[1]);
+                                       buflen --;
                                }
                        }
                        if (soft_line_break == 0) {
@@ -121,24 +117,9 @@ int CtdlDecodeQuotedPrintable(char *decoded, char *encoded, int sourcelen) {
        return(decoded_length);
 }
 
-/**
- * \brief fully decode a message
+/*
  * Given a message or message-part body and a length, handle any necessary
  * decoding and pass the request up the stack.
- * \param partnum todo ?????
- * \param part_start todo
- * \param length todo
- * \param content_type todo
- * \param charset todo
- * \param encoding todo
- * \param disposition todo
- * \param name todo
- * \param filename todo
- * \param CallBack todo
- * \param PreMultiPartCallBack todo
- * \param PostMultiPartCallBack todo
- * \param userdata todo
- * \param dont_decode todo
  */
 void mime_decode(char *partnum,
                 char *part_start, size_t length,
@@ -204,11 +185,13 @@ void mime_decode(char *partnum,
                return;
        }
        
+       /* Fail silently if we hit an unknown encoding. */
        if ((strcasecmp(encoding, "base64"))
            && (strcasecmp(encoding, "quoted-printable"))) {
                return;
        }
-       /**
+
+       /*
         * Allocate a buffer for the decoded data.  The output buffer is slightly
         * larger than the input buffer; this assumes that the decoded data
         * will never be significantly larger than the encoded data.  This is a
@@ -223,8 +206,7 @@ void mime_decode(char *partnum,
                bytes_decoded = CtdlDecodeBase64(decoded, part_start, length);
        }
        else if (!strcasecmp(encoding, "quoted-printable")) {
-               bytes_decoded = CtdlDecodeQuotedPrintable(decoded,
-                                                       part_start, length);
+               bytes_decoded = CtdlDecodeQuotedPrintable(decoded, part_start, length);
        }
 
        if (bytes_decoded > 0) if (CallBack != NULL) {
@@ -236,19 +218,11 @@ void mime_decode(char *partnum,
        free(decoded);
 }
 
-/**
- * \brief Break out the components of a multipart message
+/*
+ * Break out the components of a multipart message
  * (This function expects to be fed HEADERS + CONTENT)
  * Note: NULL can be supplied as content_end; in this case, the message is
  * considered to have ended when the parser encounters a 0x00 byte.
- * \param partnum todo
- * \param content_start todo ?????
- * \param content_end todo
- * \param CallBack todo
- * \param PreMultiPartCallBack
- * \param PostMultiPartCallBack
- * \param userdata todo
- * \param dont_decode todo
  */
 void the_mime_parser(char *partnum,
                     char *content_start, char *content_end,
@@ -351,12 +325,12 @@ void the_mime_parser(char *partnum,
        disposition = malloc(SIZ);
        memset(disposition, 0, SIZ);
 
-       /** If the caller didn't supply an endpointer, generate one by measure */
+       /* If the caller didn't supply an endpointer, generate one by measure */
        if (content_end == NULL) {
                content_end = &content_start[strlen(content_start)];
        }
 
-       /** Learn interesting things from the headers */
+       /* Learn interesting things from the headers */
        strcpy(header, "");
        do {
                ptr = memreadline(ptr, buf, SIZ);
@@ -375,7 +349,7 @@ void the_mime_parser(char *partnum,
                                strcpy(content_type, &header[14]);
                                extract_key(content_type_name, content_type, "name");
                                extract_key(charset, content_type, "charset");
-                               /** Deal with weird headers */
+                               /* Deal with weird headers */
                                if (strchr(content_type, ' '))
                                        *(strchr(content_type, ' ')) = '\0';
                                if (strchr(content_type, ';'))
@@ -413,18 +387,18 @@ void the_mime_parser(char *partnum,
                is_multipart = 0;
        }
 
-       /** If this is a multipart message, then recursively process it */
+       /* If this is a multipart message, then recursively process it */
        part_start = NULL;
        if (is_multipart) {
 
-               /** Tell the client about this message's multipartedness */
+               /* Tell the client about this message's multipartedness */
                if (PreMultiPartCallBack != NULL) {
                        PreMultiPartCallBack("", "", partnum, "",
                                NULL, content_type, charset,
                                0, encoding, userdata);
                }
 
-               /** Figure out where the boundaries are */
+               /* Figure out where the boundaries are */
                snprintf(startary, SIZ, "--%s", boundary);
                snprintf(endary, SIZ, "--%s--", boundary);
                startary_len = strlen(startary);
@@ -464,21 +438,20 @@ void the_mime_parser(char *partnum,
                        }
 
                        if (next_boundary != NULL) {
-                               /**
-                                * If we pass out of scope, don't attempt to
+                               /* If we pass out of scope, don't attempt to
                                 * read past the end boundary. */
                                if (!strcmp(next_boundary, endary)) {
                                        ptr = content_end;
                                }
                                else {
-                                       /** Set up for the next part. */
+                                       /* Set up for the next part. */
                                        part_start = strstr(next_boundary, "\n");
                                        ++part_start;
                                        ptr = part_start;
                                }
                        }
                        else {
-                               /** Invalid end of multipart.  Bail out! */
+                               /* Invalid end of multipart.  Bail out! */
                                ptr = content_end;
                        }
                } while ( (ptr < content_end) && (next_boundary != NULL) );
@@ -490,7 +463,7 @@ void the_mime_parser(char *partnum,
                goto end_parser;
        }
 
-       /** If it's not a multipart message, then do something with it */
+       /* If it's not a multipart message, then do something with it */
        if (!is_multipart) {
                part_start = ptr;
                length = 0;
@@ -499,17 +472,16 @@ void the_mime_parser(char *partnum,
                        ++length;
                }
                part_end = content_end;
-               /** fix an off-by-one error */
+               /* fix an off-by-one error */
                --part_end;
                --length;
                
-               /** Truncate if the header told us to */
+               /* Truncate if the header told us to */
                if ( (content_length > 0) && (length > content_length) ) {
                        length = content_length;
                }
 
-               /**
-                * Sometimes the "name" field is tacked on to Content-type,
+               /* Sometimes the "name" field is tacked on to Content-type,
                 * and sometimes it's tacked on to Content-disposition.  Use
                 * whichever one we have.
                 */
@@ -520,13 +492,10 @@ void the_mime_parser(char *partnum,
                        name = content_type_name;
                }
        
-               /*
-               lprintf(9, "mime_decode part=%s, len=%d, type=%s, charset=%s, encoding=%s\n",
-                       partnum, length, content_type, charset, encoding);
-               */
+               /* lprintf(CTDL_DEBUG, "mime_decode part=%s, len=%d, type=%s, charset=%s, encoding=%s\n",
+                       partnum, length, content_type, charset, encoding); */
 
-               /**
-                * Ok, we've got a non-multipart part here, so do something with it.
+               /* Ok, we've got a non-multipart part here, so do something with it.
                 */
                mime_decode(partnum,
                        part_start, length,
@@ -536,7 +505,7 @@ void the_mime_parser(char *partnum,
                        userdata, dont_decode
                );
 
-               /**
+               /*
                 * Now if it's an encapsulated message/rfc822 then we have to recurse into it
                 */
                if (!strcasecmp(content_type, "message/rfc822")) {
@@ -577,7 +546,7 @@ void the_mime_parser(char *partnum,
 
        }
 
-end_parser:    /** free the buffers!  end the oppression!! */
+end_parser:    /* free the buffers!  end the oppression!! */
        free(boundary);
        free(startary);
        free(endary);   
@@ -593,18 +562,11 @@ end_parser:       /** free the buffers!  end the oppression!! */
 
 
 
-/**
- * \brief Entry point for the MIME parser.
+/*
+ * Entry point for the MIME parser.
  * (This function expects to be fed HEADERS + CONTENT)
  * Note: NULL can be supplied as content_end; in this case, the message is
  * considered to have ended when the parser encounters a 0x00 byte.
- * \param content_start todo ?????????
- * \param content_end todo 
- * \param CallBack todo
- * \param PreMultiPartCallBack todo
- * \param PostMultiPartCallBack todo
- * \param userdata todo
- * \param dont_decode todo
  */
 void mime_parser(char *content_start,
                char *content_end,
@@ -658,5 +620,4 @@ void mime_parser(char *content_start,
 }
 
 
-
 /*@}*/