]> Pileus Git - ~andy/fetchmail/blobdiff - unmime.c
Henrik's fix.
[~andy/fetchmail] / unmime.c
index 5a9841c8caccfd71d44fc751817db5fe0467fea9..0f0dcf42a81a86dbbdedc0e3759548428781645d 100644 (file)
--- a/unmime.c
+++ b/unmime.c
@@ -259,8 +259,8 @@ static unsigned char MultipartDelimiter[MAX_DELIM_LEN+3];
 
 
 /* This string replaces the "Content-Transfer-Encoding: quoted-printable"
- * string in all headers, including those in body-parts. It must be
- * no longer than the original string.
+ * string in all headers, including those in body-parts. The replacement
+ * must be no longer than the original string.
  */
 static const char ENC8BIT[] = "Content-Transfer-Encoding: 8bit";
 static void SetEncoding8bit(unsigned char *XferEncOfs)
@@ -275,6 +275,53 @@ static void SetEncoding8bit(unsigned char *XferEncOfs)
   }
 }
 
+static char *GetBoundary(char *CntType)
+{
+  char *p1, *p2;
+  int flag;
+
+  /* Find the "boundary" delimiter. It must be preceded with a ';'
+   * and optionally some whitespace.
+   */
+  p1 = CntType;
+  do {
+    p2 = strchr(p1, ';'); 
+    if (p2)
+      for (p2++; isspace(*p2); p2++);
+
+    p1 = p2;
+  } while ((p1) && (strncasecmp(p1, "boundary", 8) != 0));
+
+  if (p1 == NULL)
+    /* No boundary delimiter */
+    return NULL;
+
+  /* Skip "boundary", whitespace and '='; check that we do have a '=' */
+  for (p1+=8, flag=0; (isspace(*p1) || (*p1 == '=')); p1++)
+    flag |= (*p1 == '=');
+  if (!flag)
+    return NULL;
+
+  /* Find end of boundary delimiter string */
+  if (*p1 == '\"') {
+    /* The delimiter is inside quotes */
+    p1++;
+    p2 = strchr(p1, '\"');
+    if (p2 == NULL)
+      return NULL;  /* No closing '"' !?! */
+  }
+  else {
+    /* There might be more text after the "boundary" string. */
+    p2 = strchr(p1, ';');  /* Safe - delimiter with ';' must be in quotes */
+  }
+
+  /* Zero-terminate the boundary string */
+  if (p2 != NULL)
+    *p2 = '\0';
+
+  return (p1 && strlen(p1)) ? p1 : NULL;
+}
+
 
 /*
  * This routine does three things:
@@ -292,7 +339,7 @@ static void SetEncoding8bit(unsigned char *XferEncOfs)
  *
  * The return value is a bitmask.
  */
-int MimeBodyType(unsigned char *hdrs)
+int MimeBodyType(unsigned char *hdrs, int WantDecode)
 {
   unsigned char *NxtHdr = hdrs;
   unsigned char *XferEnc, *XferEncOfs, *CntType, *MimeVer, *p;
@@ -370,34 +417,18 @@ int MimeBodyType(unsigned char *hdrs)
   if ((MimeVer != NULL) && (strcmp(MimeVer, "1.0") == 0)) {
 
     /* Check Content-Type to see if this is a multipart message */
-    if (CntType != NULL) {
-      if ((strncasecmp(CntType, "multipart/", 10) == 0) ||
-         (strncasecmp(CntType, "message/", 8) == 0)) {
-
-       char *p1, *p2;
+    if ( (CntType != NULL) &&
+         ((strncasecmp(CntType, "multipart/", 10) == 0) ||
+         (strncasecmp(CntType, "message/", 8) == 0)) ) {
 
-       /* Search for "boundary=" */
-       p1 = strchr(CntType, '=');
-       if (p1 != NULL) {
-         /* Skip the '=' and any whitespace after it */
-         for (p1++; (isspace(*p1)); p1++); 
-              
-         /* The delimiter might be inside quotes */
-         if (*p1 == '\"') {
-           p1++;
-           p2 = strchr(p1, '\"');
-           if (p2 != NULL)
-             *p2 = '\0';
-         }
+      char *p1 = GetBoundary(CntType);
 
-         if (strlen(p1) > 0) {
-           /* The actual delimiter is "--" followed by 
-              the boundary string */
-           strcpy(MultipartDelimiter, "--");
-           strncat(MultipartDelimiter, p1, MAX_DELIM_LEN);
-           BodyType = (MSG_IS_8BIT | MSG_NEEDS_DECODE);
-         }
-       }
+      if (p1 != NULL) {
+       /* The actual delimiter is "--" followed by 
+          the boundary string */
+       strcpy(MultipartDelimiter, "--");
+       strncat(MultipartDelimiter, p1, MAX_DELIM_LEN);
+       BodyType = (MSG_IS_8BIT | MSG_NEEDS_DECODE);
       }
     }
 
@@ -409,7 +440,9 @@ int MimeBodyType(unsigned char *hdrs)
       if (strcasecmp(XferEnc, "quoted-printable") == 0) {
        CurrEncodingIsQP = 1;
        BodyType = (MSG_IS_8BIT | MSG_NEEDS_DECODE);
-        SetEncoding8bit(XferEncOfs);
+       if (WantDecode) {
+           SetEncoding8bit(XferEncOfs);
+        }
       }
       else if (strcasecmp(XferEnc, "7bit") == 0) {
        CurrEncodingIsQP = 0;
@@ -612,7 +645,7 @@ int main(int argc, char *argv[])
   DBG_FWRITE(buffer, strlen(buffer), 1, fd_orig);
 
   UnMimeHeader(buffer);
-  bodytype = MimeBodyType(buffer);
+  bodytype = MimeBodyType(buffer, 1);
 
   i = strlen(buffer);
   fwrite(buffer, i, 1, stdout);