]> code.citadel.org Git - citadel.git/blobdiff - citadel/mime_parser.c
* tiny tool for message retrieval, first draft.
[citadel.git] / citadel / mime_parser.c
index 869327ecbc0a475f754753ea2f1129fc06915d58..a1a4965e945b7c7f5e6c011972dd5af5157ebd62 100644 (file)
@@ -20,7 +20,6 @@
 
 #include "citadel.h"
 #include "server.h"
-#include "serv_extensions.h"
 #include "sysdep_decls.h"
 #include "tools.h"
 
 
 void extract_key(char *target, char *source, char *key)
 {
-       int a, b;
-
-       strcpy(target, source);
-       for (a = 0; a < strlen(target); ++a) {
-               if ((!strncasecmp(&target[a], key, strlen(key)))
-                   && (target[a + strlen(key)] == '=')) {
-                       strcpy(target, &target[a + strlen(key) + 1]);
-                       if (target[0] == 34)
-                               strcpy(target, &target[1]);
-                       for (b = 0; b < strlen(target); ++b)
-                               if (target[b] == 34)
-                                       target[b] = 0;
-                       return;
+       char *ptr;
+       char looking_for[256];
+       int double_quotes = 0;
+
+       snprintf(looking_for, sizeof looking_for, "%s=", key);
+
+       ptr = bmstrcasestr(source, looking_for);
+       if (ptr == NULL) {
+               strcpy(target, "");
+               return;
+       }
+       strcpy(target, (ptr + strlen(looking_for)));
+
+       for (ptr=target; (*ptr != 0); ++ptr) {
+
+               /* A semicolon means we've hit the end of the key, unless we're inside double quotes */
+               if ( (double_quotes != 1) && (*ptr == ';')) {
+                       *ptr = 0;
+               }
+
+               /* if we find double quotes, we've got a great set of string boundaries */
+               if (*ptr == '\"') {
+                       ++double_quotes;
+                       if (double_quotes == 1) {
+                               strcpy(ptr, ptr+1);
+                       }
+                       else {
+                               *ptr = 0;
+                       }
                }
        }
-       strcpy(target, "");
 }
 
 
@@ -60,43 +74,6 @@ char *fixed_partnum(char *supplied_partnum) {
 
 
 
-/*
- * Convert "quoted-printable" to binary.  Returns number of bytes decoded.
- * according to RFC2045 section 6.7
- */
-int CtdlDecodeQuotedPrintable(char *decoded, char *encoded, int sourcelen) {
-       unsigned int ch;
-       int decoded_length = 0;
-       int pos = 0;
-
-       while (pos < sourcelen)
-       {
-               if (!strncmp(&encoded[pos], "=\r\n", 3))
-               {
-                       pos += 3;
-               }
-               else if (!strncmp(&encoded[pos], "=\n", 2))
-               {
-                       pos += 2;
-               }
-               else if (encoded[pos] == '=')
-               {
-                       ch = 0;
-                       sscanf(&encoded[pos+1], "%02x", &ch);
-                       pos += 3;
-                       decoded[decoded_length++] = ch;
-               }
-               else
-               {
-                       decoded[decoded_length++] = encoded[pos];
-                       pos += 1;
-               }
-       }
-       decoded[decoded_length] = 0;
-       return(decoded_length);
-}
-
-
 /*
  * Given a message or message-part body and a length, handle any necessary
  * decoding and pass the request up the stack.
@@ -270,6 +247,8 @@ void the_mime_parser(char *partnum,
        char nested_partnum[256];
        int crlf_in_use = 0;
        char *evaluate_crlf_ptr = NULL;
+       int buflen = 0;
+       int headerlen = 0;
 
        ptr = content_start;
        content_length = 0;
@@ -314,13 +293,14 @@ void the_mime_parser(char *partnum,
 
        /* Learn interesting things from the headers */
        strcpy(header, "");
+       headerlen = 0;
        do {
-               ptr = memreadline(ptr, buf, SIZ);
+               ptr = memreadlinelen(ptr, buf, SIZ, &buflen);
                if (ptr >= content_end) {
                        goto end_parser;
                }
 
-               for (i = 0; i < strlen(buf); ++i) {
+               for (i = 0; i < buflen; ++i) {
                        if (isspace(buf[i])) {
                                buf[i] = ' ';
                        }
@@ -332,6 +312,7 @@ void the_mime_parser(char *partnum,
                                striplt(content_type);
                                extract_key(content_type_name, content_type, "name");
                                extract_key(charset, content_type, "charset");
+                               extract_key(boundary, header, "boundary");
                                /* Deal with weird headers */
                                if (strchr(content_type, ' '))
                                        *(strchr(content_type, ' ')) = '\0';
@@ -354,14 +335,15 @@ void the_mime_parser(char *partnum,
                                strcpy(encoding, &header[26]);
                                striplt(encoding);
                        }
-                       if (strlen(boundary) == 0)
-                               extract_key(boundary, header, "boundary");
                        strcpy(header, "");
+                       headerlen = 0;
                }
-               if ((strlen(header) + strlen(buf) + 2) < SIZ) {
-                       strcat(header, buf);
+               if ((headerlen + buflen + 2) < SIZ) {
+                       memcpy(&header[headerlen], buf, buflen);
+                       headerlen += buflen;
+                       header[headerlen] = '\0';
                }
-       } while ((strlen(buf) > 0) && (*ptr != 0));
+       } while ((!IsEmptyStr(buf)) && (*ptr != 0));
 
        if (strchr(disposition, ';'))
                *(strchr(disposition, ';')) = '\0';
@@ -370,7 +352,7 @@ void the_mime_parser(char *partnum,
                *(strchr(content_type, ';')) = '\0';
        striplt(content_type);
 
-       if (strlen(boundary) > 0) {
+       if (!IsEmptyStr(boundary)) {
                is_multipart = 1;
        } else {
                is_multipart = 0;
@@ -409,7 +391,7 @@ void the_mime_parser(char *partnum,
                                        --part_end;     /* omit the trailing CR */
                                }
 
-                               if (strlen(partnum) > 0) {
+                               if (!IsEmptyStr(partnum)) {
                                        snprintf(nested_partnum,
                                                 sizeof nested_partnum,
                                                 "%s.%d", partnum,