]> diplodocus.org Git - nmh/blobdiff - uip/mhparse.c
Update manpages to use .TP for tagged paragraphs (part I).
[nmh] / uip / mhparse.c
index e9a4ceebeb11cc4fa6b9ddc75dbdefe7e45fc163..0dcd1333d499e027070a754034b9b41ac21aef53 100644 (file)
@@ -22,8 +22,6 @@
 
 extern int debugsw;
 
-extern int endian;     /* mhmisc.c     */
-
 extern pid_t xpid;     /* mhshowsbr.c  */
 
 /* cache policies */
@@ -48,11 +46,7 @@ struct k2v SubText[] = {
     { NULL,       TEXT_UNKNOWN }    /* this one must be last! */
 };
 
-struct k2v Charset[] = {
-    { "us-ascii",   CHARSET_USASCII },
-    { "iso-8859-1", CHARSET_LATIN },
-    { NULL,         CHARSET_UNKNOWN }  /* this one must be last! */
-};
+/* Charset[] removed -- yozo.  Mon Oct  8 01:03:41 JST 2012 */
 
 /*
  * Structures for MULTIPART messages
@@ -268,6 +262,7 @@ get_content (FILE *in, char *file, int toplevel)
     char *np, *vp;
     CT ct;
     HF hp;
+    m_getfld_state_t gstate = 0;
 
     /* allocate the content structure */
     if (!(ct = (CT) calloc (1, sizeof(*ct))))
@@ -281,11 +276,12 @@ get_content (FILE *in, char *file, int toplevel)
      * Parse the header fields for this
      * content into a linked list.
      */
-    for (compnum = 1, state = FLD;;) {
-       switch (state = m_getfld (state, name, buf, sizeof(buf), in)) {
+    m_getfld_track_filepos (&gstate, in);
+    for (compnum = 1;;) {
+       int bufsz = sizeof buf;
+       switch (state = m_getfld (&gstate, name, buf, &bufsz, in)) {
        case FLD:
        case FLDPLUS:
-       case FLDEOF:
            compnum++;
 
            /* get copies of the buffers */
@@ -294,22 +290,19 @@ get_content (FILE *in, char *file, int toplevel)
 
            /* if necessary, get rest of field */
            while (state == FLDPLUS) {
-               state = m_getfld (state, name, buf, sizeof(buf), in);
+               bufsz = sizeof buf;
+               state = m_getfld (&gstate, name, buf, &bufsz, in);
                vp = add (buf, vp);     /* add to previous value */
            }
 
            /* Now add the header data to the list */
            add_header (ct, np, vp);
 
-           /* continue, if this isn't the last header field */
-           if (state != FLDEOF) {
-               ct->c_begin = ftell (in) + 1;
-               continue;
-           }
-           /* else fall... */
+           /* continue, to see if this isn't the last header field */
+           ct->c_begin = ftell (in) + 1;
+           continue;
 
        case BODY:
-       case BODYEOF:
            ct->c_begin = ftell (in) - strlen (buf);
            break;
 
@@ -328,6 +321,7 @@ get_content (FILE *in, char *file, int toplevel)
        /* break out of the loop */
        break;
     }
+    m_getfld_state_destroy (&gstate);
 
     /*
      * Read the content headers.  We will parse the
@@ -1079,14 +1073,8 @@ InitText (CT ct)
 
     /* check if content specified a character set */
     if (*ap) {
-       /* match character set or set to CHARSET_UNKNOWN */
-       for (kv = Charset; kv->kv_key; kv++) {
-           if (!mh_strcasecmp (*ep, kv->kv_key)) {
-               chset = *ep;
-               break;
-           }
-       }
-       t->tx_charset = kv->kv_value;
+       chset = *ep;
+       t->tx_charset = CHARSET_SPECIFIED;
     } else {
        t->tx_charset = CHARSET_UNSPECIFIED;
     }
@@ -1133,9 +1121,22 @@ InitMultiPart (CT ct)
      */
     if (ct->c_encoding != CE_7BIT && ct->c_encoding != CE_8BIT
        && ct->c_encoding != CE_BINARY) {
+       /* Copy the Content-Transfer-Encoding header field body so we can
+          remove any trailing whitespace and leading blanks from it. */
+       char *cte = add (ct->c_celine ? ct->c_celine : "(null)", NULL);
+
+       bp = cte + strlen (cte) - 1;
+       while (bp >= cte && isspace (*bp)) *bp-- = '\0';
+       for (bp = cte; *bp && isblank (*bp); ++bp) continue;
+
        admonish (NULL,
-                 "\"%s/%s\" type in message %s must be encoded in 7bit, 8bit, or binary",
-                 ci->ci_type, ci->ci_subtype, ct->c_file);
+                 "\"%s/%s\" type in message %s must be encoded in\n"
+                 "7bit, 8bit, or binary, per RFC 2045 (6.4).  One workaround "
+                 "is to\nmanually edit the file and change the \"%s\"\n"
+                 "Content-Transfer-Encoding to one of those.  For now",
+                 ci->ci_type, ci->ci_subtype, ct->c_file, bp);
+       free (cte);
+
        return NOTOK;
     }
 
@@ -1727,9 +1728,9 @@ static int
 openBase64 (CT ct, char **file)
 {
     int        bitno, cc, digested;
-    int fd, len, skip;
-    unsigned long bits;
-    unsigned char value, *b, *b1, *b2, *b3;
+    int fd, len, skip, own_ct_fp = 0;
+    uint32_t bits;
+    unsigned char value, b;
     unsigned char *cp, *ep;
     char buffer[BUFSIZ];
     /* sbeck -- handle suffixes */
@@ -1737,11 +1738,6 @@ openBase64 (CT ct, char **file)
     CE ce;
     MD5_CTX mdContext;
 
-    b  = (unsigned char *) &bits;
-    b1 = &b[endian > 0 ? 1 : 2];
-    b2 = &b[endian > 0 ? 2 : 1];
-    b3 = &b[endian > 0 ? 3 : 0];
-
     ce = ct->c_cefile;
     if (ce->ce_fp) {
        fseek (ce->ce_fp, 0L, SEEK_SET);
@@ -1776,8 +1772,8 @@ openBase64 (CT ct, char **file)
     }
     if (cp != NULL && *cp != '\0') {
         if (ce->ce_unlink) {
-            // Temporary file already exists, so we rename to
-            // version with extension.
+            /* Temporary file already exists, so we rename to
+               version with extension. */
             char *file_org = strdup(ce->ce_file);
             ce->ce_file = add (cp, ce->ce_file);
             if (rename(file_org, ce->ce_file)) {
@@ -1798,9 +1794,12 @@ openBase64 (CT ct, char **file)
     if ((len = ct->c_end - ct->c_begin) < 0)
        adios (NULL, "internal error(1)");
 
-    if (!ct->c_fp && (ct->c_fp = fopen (ct->c_file, "r")) == NULL) {
-       content_error (ct->c_file, ct, "unable to open for reading");
-       return NOTOK;
+    if (! ct->c_fp) {
+       if ((ct->c_fp = fopen (ct->c_file, "r")) == NULL) {
+           content_error (ct->c_file, ct, "unable to open for reading");
+           return NOTOK;
+       }
+       own_ct_fp = 1;
     }
     
     if ((digested = ct->c_digested))
@@ -1847,17 +1846,20 @@ openBase64 (CT ct, char **file)
                    bits |= value << bitno;
 test_end:
                    if ((bitno -= 6) < 0) {
-                       putc ((char) *b1, ce->ce_fp);
+                       b = (bits >> 16) & 0xff;
+                       putc ((char) b, ce->ce_fp);
                        if (digested)
-                           MD5Update (&mdContext, b1, 1);
+                           MD5Update (&mdContext, &b, 1);
                        if (skip < 2) {
-                           putc ((char) *b2, ce->ce_fp);
+                           b = (bits >> 8) & 0xff;
+                           putc ((char) b, ce->ce_fp);
                            if (digested)
-                               MD5Update (&mdContext, b2, 1);
+                               MD5Update (&mdContext, &b, 1);
                            if (skip < 1) {
-                               putc ((char) *b3, ce->ce_fp);
+                               b = bits & 0xff;
+                               putc ((char) b, ce->ce_fp);
                                if (digested)
-                                   MD5Update (&mdContext, b3, 1);
+                                   MD5Update (&mdContext, &b, 1);
                            }
                        }
 
@@ -1912,9 +1914,17 @@ self_delimiting:
 
 ready_to_go:
     *file = ce->ce_file;
+    if (own_ct_fp) {
+      fclose (ct->c_fp);
+      ct->c_fp = NULL;
+    }
     return fileno (ce->ce_fp);
 
 clean_up:
+    if (own_ct_fp) {
+      fclose (ct->c_fp);
+      ct->c_fp = NULL;
+    }
     free_encoding (ct, 0);
     return NOTOK;
 }
@@ -1954,7 +1964,7 @@ InitQuoted (CT ct)
 static int
 openQuoted (CT ct, char **file)
 {
-    int        cc, digested, len, quoted;
+    int        cc, digested, len, quoted, own_ct_fp = 0;
     unsigned char *cp, *ep;
     char buffer[BUFSIZ];
     unsigned char mask;
@@ -1997,8 +2007,8 @@ openQuoted (CT ct, char **file)
     }
     if (cp != NULL && *cp != '\0') {
         if (ce->ce_unlink) {
-            // Temporary file already exists, so we rename to
-            // version with extension.
+            /* Temporary file already exists, so we rename to
+               version with extension. */
             char *file_org = strdup(ce->ce_file);
             ce->ce_file = add (cp, ce->ce_file);
             if (rename(file_org, ce->ce_file)) {
@@ -2016,17 +2026,15 @@ openQuoted (CT ct, char **file)
        return NOTOK;
     }
 
-    if ((ce->ce_fp = fopen (ce->ce_file, "w+")) == NULL) {
-       content_error (ce->ce_file, ct, "unable to fopen for reading/writing");
-       return NOTOK;
-    }
-
     if ((len = ct->c_end - ct->c_begin) < 0)
        adios (NULL, "internal error(2)");
 
-    if (!ct->c_fp && (ct->c_fp = fopen (ct->c_file, "r")) == NULL) {
-       content_error (ct->c_file, ct, "unable to open for reading");
-       return NOTOK;
+    if (! ct->c_fp) {
+       if ((ct->c_fp = fopen (ct->c_file, "r")) == NULL) {
+           content_error (ct->c_file, ct, "unable to open for reading");
+           return NOTOK;
+       }
+       own_ct_fp = 1;
     }
 
     if ((digested = ct->c_digested))
@@ -2148,10 +2156,18 @@ openQuoted (CT ct, char **file)
 
 ready_to_go:
     *file = ce->ce_file;
+    if (own_ct_fp) {
+      fclose (ct->c_fp);
+      ct->c_fp = NULL;
+    }
     return fileno (ce->ce_fp);
 
 clean_up:
     free_encoding (ct, 0);
+    if (own_ct_fp) {
+      fclose (ct->c_fp);
+      ct->c_fp = NULL;
+    }
     return NOTOK;
 }
 
@@ -2174,7 +2190,7 @@ Init7Bit (CT ct)
 int
 open7Bit (CT ct, char **file)
 {
-    int        cc, fd, len;
+    int        cc, fd, len, own_ct_fp = 0;
     char buffer[BUFSIZ];
     /* sbeck -- handle suffixes */
     char *cp;
@@ -2215,8 +2231,8 @@ open7Bit (CT ct, char **file)
     }
     if (cp != NULL && *cp != '\0') {
         if (ce->ce_unlink) {
-            // Temporary file already exists, so we rename to
-            // version with extension.
+            /* Temporary file already exists, so we rename to
+               version with extension. */
             char *file_org = strdup(ce->ce_file);
             ce->ce_file = add (cp, ce->ce_file);
             if (rename(file_org, ce->ce_file)) {
@@ -2284,9 +2300,12 @@ open7Bit (CT ct, char **file)
     if ((len = ct->c_end - ct->c_begin) < 0)
        adios (NULL, "internal error(3)");
 
-    if (!ct->c_fp && (ct->c_fp = fopen (ct->c_file, "r")) == NULL) {
-       content_error (ct->c_file, ct, "unable to open for reading");
-       return NOTOK;
+    if (! ct->c_fp) {
+       if ((ct->c_fp = fopen (ct->c_file, "r")) == NULL) {
+           content_error (ct->c_file, ct, "unable to open for reading");
+           return NOTOK;
+       }
+       own_ct_fp = 1;
     }
 
     lseek (fd = fileno (ct->c_fp), (off_t) ct->c_begin, SEEK_SET);
@@ -2323,10 +2342,18 @@ open7Bit (CT ct, char **file)
 
 ready_to_go:
     *file = ce->ce_file;
+    if (own_ct_fp) {
+      fclose (ct->c_fp);
+      ct->c_fp = NULL;
+    }
     return fileno (ce->ce_fp);
 
 clean_up:
     free_encoding (ct, 0);
+    if (own_ct_fp) {
+      fclose (ct->c_fp);
+      ct->c_fp = NULL;
+    }
     return NOTOK;
 }
 
@@ -2792,15 +2819,10 @@ static int
 readDigest (CT ct, char *cp)
 {
     int        bitno, skip;
-    unsigned long bits;
+    uint32_t bits;
     char *bp = cp;
     unsigned char *dp, value, *ep;
-    unsigned char *b, *b1, *b2, *b3;
 
-    b  = (unsigned char *) &bits,
-    b1 = &b[endian > 0 ? 1 : 2],
-    b2 = &b[endian > 0 ? 2 : 1],
-    b3 = &b[endian > 0 ? 3 : 0];
     bitno = 18;
     bits = 0L;
     skip = 0;
@@ -2822,11 +2844,11 @@ test_end:
                if ((bitno -= 6) < 0) {
                    if (dp + (3 - skip) > ep)
                        goto invalid_digest;
-                   *dp++ = *b1;
+                   *dp++ = (bits >> 16) & 0xff;
                    if (skip < 2) {
-                       *dp++ = *b2;
+                       *dp++ = (bits >> 8) & 0xff;
                        if (skip < 1)
-                           *dp++ = *b3;
+                           *dp++ = bits & 0xff;
                    }
                    bitno = 18;
                    bits = 0L;