]> diplodocus.org Git - nmh/blobdiff - uip/mhparse.c
Alter HasSuffixC()'s char * to be const.
[nmh] / uip / mhparse.c
index 7e539c3a8eabc276a0cd1720b2c6dcec4cac8172..7e01b050807fe93b0538b73ca42c23c9cb367e3e 100644 (file)
@@ -294,7 +294,7 @@ get_content (FILE *in, char *file, int toplevel)
     m_getfld_state_t gstate = 0;
 
     /* allocate the content structure */
-    ct = mh_xcalloc(1, sizeof *ct);
+    NEW0(ct);
     ct->c_fp = in;
     ct->c_file = add (file, NULL);
     ct->c_begin = ftell (ct->c_fp) + 1;
@@ -330,7 +330,15 @@ get_content (FILE *in, char *file, int toplevel)
            continue;
 
        case BODY:
-           ct->c_begin = ftell (in) - strlen (buf);
+           if (name[0] == ':') {
+               /* Special case:  no blank line between header and body.  The
+                  file position indicator is on the newline at the end of the
+                  line, but it needs to be one prior to the beginning of the
+                  line.  So subtract the length of the line, bufsz, plus 1. */
+               ct->c_begin = ftell (in) - (bufsz + 1);
+           } else {
+               ct->c_begin = ftell (in) - (bufsz - 1);
+           }
            break;
 
        case FILEEOF:
@@ -581,7 +589,7 @@ add_header (CT ct, char *name, char *value)
     HF hp;
 
     /* allocate header field structure */
-    hp = mh_xmalloc (sizeof(*hp));
+    NEW(hp);
 
     /* link data into header structure */
     hp->name = name;
@@ -649,11 +657,7 @@ get_ctinfo (char *cp, CT ct, int magic)
                TYPE_FIELD, ct->c_file);
        return NOTOK;
     }
-
-    /* down case the content type string */
-    for (dp = ci->ci_type; *dp; dp++)
-       if (isalpha((unsigned char) *dp) && isupper ((unsigned char) *dp))
-           *dp = tolower ((unsigned char) *dp);
+    ToLower(ci->ci_type);
 
     while (isspace ((unsigned char) *cp))
        cp++;
@@ -688,11 +692,7 @@ get_ctinfo (char *cp, CT ct, int magic)
                TYPE_FIELD, ct->c_file, ci->ci_type);
        return NOTOK;
     }
-
-    /* down case the content subtype string */
-    for (dp = ci->ci_subtype; *dp; dp++)
-       if (isalpha((unsigned char) *dp) && isupper ((unsigned char) *dp))
-           *dp = tolower ((unsigned char) *dp);
+    ToLower(ci->ci_subtype);
 
 magic_skip:
     while (isspace ((unsigned char) *cp))
@@ -712,10 +712,8 @@ magic_skip:
      * Get any <Content-Id> given in buffer
      */
     if (magic && *cp == '<') {
-       if (ct->c_id) {
-           free (ct->c_id);
-           ct->c_id = NULL;
-       }
+        mh_xfree(ct->c_id);
+        ct->c_id = NULL;
        if (!(dp = strchr(ct->c_id = ++cp, '>'))) {
            advise (NULL, "invalid ID in message %s", ct->c_file);
            return NOTOK;
@@ -1027,7 +1025,7 @@ InitText (CT ct)
     ct->c_subtype = ct_str_subtype (CT_TEXT, ci->ci_subtype);
 
     /* allocate text character set structure */
-    t = mh_xcalloc(1, sizeof *t);
+    NEW0(t);
     ct->c_ctparams = (void *) t;
 
     /* scan for charset parameter */
@@ -1053,7 +1051,7 @@ InitText (CT ct)
     if (chset != NULL && !check_charset (chset, strlen (chset))) {
        snprintf (buffer, sizeof(buffer), "%s-charset-%s", invo_name, chset);
        if ((cp = context_find (buffer)))
-           ct->c_termproc = getcpy (cp);
+           ct->c_termproc = mh_xstrdup(cp);
     }
 
     return OK;
@@ -1131,7 +1129,7 @@ InitMultiPart (CT ct)
     }
 
     /* allocate primary structure for multipart info */
-    m = mh_xcalloc(1, sizeof *m);
+    NEW0(m);
     ct->c_ctparams = (void *) m;
 
     /* check if boundary parameter contains only whitespace characters */
@@ -1175,7 +1173,7 @@ InitMultiPart (CT ct)
            if (strcmp (bufp + 2, m->mp_start))
                continue;
 next_part:
-           part = mh_xcalloc(1, sizeof *part);
+           NEW0(part);
            *next = part;
            next = &part->mp_next;
 
@@ -1201,10 +1199,9 @@ end_part:
                if (inout)
                    goto next_part;
                goto last_part;
-           } else {
-               if (strcmp (bufp + 2, m->mp_stop) == 0)
-                   goto end_part;
            }
+            if (strcmp (bufp + 2, m->mp_stop) == 0)
+                goto end_part;
        }
     }
 
@@ -1223,7 +1220,7 @@ end_part:
                continue;
            *next = NULL;
            free_content (p);
-           free ((char *) part);
+           free(part);
        }
     }
 
@@ -1414,7 +1411,7 @@ InitMessage (CT ct)
                PM pm;
                struct partial *p;
 
-               p = mh_xcalloc(1, sizeof *p);
+               NEW0(p);
                ct->c_ctparams = (void *) p;
 
                /* scan for parameters "id", "number", and "total" */
@@ -1462,7 +1459,7 @@ invalid_param:
                CT p;
                FILE *fp;
 
-               e = mh_xcalloc(1, sizeof *e);
+               NEW0(e);
                ct->c_ctparams = (void *) e;
 
                if (!ct->c_fp
@@ -1702,8 +1699,7 @@ size_encoding (CT ct)
     if (ce->ce_file) {
        if (stat (ce->ce_file, &st) != NOTOK)
            return (long) st.st_size;
-       else
-           return 0L;
+        return 0L;
     }
 
     if (ct->c_encoding == CE_EXTERNAL)
@@ -1836,7 +1832,7 @@ openBase64 (CT ct, char **file)
         for (i = 0; i < decoded_len; ++i) {
             putc (*decoded_p++, ce->ce_fp);
         }
-        free ((char *) decoded);
+        free(decoded);
         if (ferror (ce->ce_fp)) {
             content_error (ce->ce_file, ct, "error writing to");
             goto clean_up;
@@ -2322,12 +2318,11 @@ openExternal (CT ct, CT cb, CE ce, char **file, int *fd)
     if (find_cache (ct, rcachesw, (int *) 0, cb->c_id,
                cachefile, sizeof(cachefile)) != NOTOK) {
        if ((ce->ce_fp = fopen (cachefile, "r"))) {
-           ce->ce_file = getcpy (cachefile);
+           ce->ce_file = mh_xstrdup(cachefile);
            ce->ce_unlink = 0;
            goto ready_already;
-       } else {
-           admonish (cachefile, "unable to fopen for reading");
        }
+        admonish (cachefile, "unable to fopen for reading");
     }
 
     *fd = fileno (ce->ce_fp);
@@ -2374,7 +2369,7 @@ openFile (CT ct, char **file)
        return NOTOK;
     }
 
-    ce->ce_file = getcpy (e->eb_name);
+    ce->ce_file = mh_xstrdup(e->eb_name);
     ce->ce_unlink = 0;
 
     if ((ce->ce_fp = fopen (ce->ce_file, "r")) == NULL) {
@@ -2515,7 +2510,7 @@ openFTP (CT ct, char **file)
                  LocalName (1));
        pass = buffer;
     } else {
-       ruserpass (e->eb_site, &username, &password);
+       ruserpass (e->eb_site, &username, &password, 0);
        user = username;
        pass = password;
     }
@@ -2745,8 +2740,7 @@ openMail (CT ct, char **file)
 
     /* showproc is for mhshow and mhstore, though mhlist -debug
      * prints it, too. */
-    if (ct->c_showproc)
-       free (ct->c_showproc);
+    mh_xfree(ct->c_showproc);
     ct->c_showproc = add ("true", NULL);
 
     fseek (ce->ce_fp, 0L, SEEK_SET);
@@ -2919,17 +2913,16 @@ readDigest (CT ct, char *cp)
             }
 
             return OK;
-        } else {
-            if (debugsw) {
-                fprintf (stderr, "invalid MD5 digest (got %d octets)\n",
-                         (int) strlen ((char *) digest));
-            }
-
-            return NOTOK;
         }
-    } else {
+        if (debugsw) {
+            fprintf (stderr, "invalid MD5 digest (got %d octets)\n",
+                     (int) strlen ((char *) digest));
+        }
+
         return NOTOK;
     }
+
+    return NOTOK;
 }
 
 
@@ -3295,8 +3288,7 @@ parse_header_attrs (const char *filename, const char *fieldname,
 
        /* down case the attribute name */
        for (dp = cp; istoken ((unsigned char) *dp); dp++)
-           if (isalpha((unsigned char) *dp) && isupper ((unsigned char) *dp))
-               *dp = tolower ((unsigned char) *dp);
+            *dp = tolower ((unsigned char) *dp);
 
        for (up = dp; isspace ((unsigned char) *dp);)
            dp++;
@@ -3405,8 +3397,7 @@ parse_header_attrs (const char *filename, const char *fieldname,
                           "field\n%*s(parameter %s)", filename, fieldname,
                           strlen(invo_name) + 2, "", nameptr);
                    free(nameptr);
-                   if (charset)
-                       free(charset);
+                    mh_xfree(charset);
                    return NOTOK;
                }
 
@@ -3431,10 +3422,8 @@ parse_header_attrs (const char *filename, const char *fieldname,
                               filename, fieldname, strlen(invo_name) + 2,
                               "", nameptr);
                        free(nameptr);
-                       if (charset)
-                           free(charset);
-                       if (lang)
-                           free(lang);
+                        mh_xfree(charset);
+                        mh_xfree(lang);
                        return NOTOK;
                    }
                    vp += 2;
@@ -3477,10 +3466,8 @@ bad_quote:
                                filename, fieldname, strlen(invo_name) + 2, "",
                                nameptr);
                        free(nameptr);
-                       if (charset)
-                           free(charset);
-                       if (lang)
-                           free(lang);
+                        mh_xfree(charset);
+                        mh_xfree(lang);
                        return NOTOK;
                    case '"':
                        break;
@@ -3537,8 +3524,7 @@ bad_quote:
            }
 
            if (pp == NULL) {
-               pp = mh_xmalloc(sizeof(*pp));
-               memset(pp, 0, sizeof(*pp));
+               NEW0(pp);
                pp->name = nameptr;
                pp->next = phead;
                phead = pp;
@@ -3548,8 +3534,7 @@ bad_quote:
             * Insert this into the section linked list
             */
 
-           sp = mh_xmalloc(sizeof(*sp));
-           memset(sp, 0, sizeof(*sp));
+           NEW0(sp);
            sp->value = valptr;
            sp->index = index;
            sp->len = len;
@@ -3590,11 +3575,9 @@ bad_quote:
             */
 
            if (index == 0 && encoded) {
-               if (pp->charset)
-                   free(pp->charset);
+                mh_xfree(pp->charset);
                pp->charset = charset;
-               if (pp->lang)
-                   free(pp->lang);
+                mh_xfree(pp->lang);
                pp->lang = lang;
            }
        } else {
@@ -3665,7 +3648,7 @@ content_charset (CT ct) {
 
     ret_charset = get_param(ct->c_ctinfo.ci_first_pm, "charset", '?', 0);
 
-    return ret_charset ? ret_charset : getcpy ("US-ASCII");
+    return ret_charset ? ret_charset : mh_xstrdup("US-ASCII");
 }
 
 
@@ -3694,8 +3677,7 @@ output_params(size_t initialwidth, PM params, int *offsetout, int external)
 
        if (strlen(params->pm_name) > CPERLIN) {
            advise(NULL, "Parameter name \"%s\" is too long", params->pm_name);
-           if (paramout)
-               free(paramout);
+            mh_xfree(paramout);
            return NULL;
        }
 
@@ -3732,8 +3714,7 @@ output_params(size_t initialwidth, PM params, int *offsetout, int external)
                                 numchars, valoff);
 
            if (i == 0) {
-               if (paramout)
-                   free(paramout);
+                mh_xfree(paramout);
                return NULL;
            }
 
@@ -3796,8 +3777,7 @@ output_params(size_t initialwidth, PM params, int *offsetout, int external)
                             strlen(params->pm_value + valoff), valoff);
 
        if (i == 0) {
-           if (paramout)
-               free(paramout);
+            mh_xfree(paramout);
            return NULL;
        }
 
@@ -3896,13 +3876,13 @@ param_len(PM pm, int index, size_t valueoff, int *encode, int *cont,
         */
 
        if (! pm->pm_charset) {
-           pm->pm_charset = getcpy(write_charset_8bit());
+           pm->pm_charset = mh_xstrdup(write_charset_8bit());
            if (strcasecmp(pm->pm_charset, "US-ASCII") == 0)
                adios(NULL, "8-bit characters in parameter \"%s\", but "
                      "local character set is US-ASCII", pm->pm_name);
        }
        if (! pm->pm_lang)
-           pm->pm_lang = getcpy(NULL); /* Default to a blank lang tag */
+           pm->pm_lang = mh_xstrdup("");       /* Default to a blank lang tag */
 
        len++;          /* For the encoding marker */
        maxfit--;
@@ -4082,10 +4062,9 @@ normal_param(PM pm, char *output, size_t len, size_t valuelen,
 PM
 add_param(PM *first, PM *last, char *name, char *value, int nocopy)
 {
-    PM pm = mh_xmalloc(sizeof(*pm));
-
-    memset(pm, 0, sizeof(*pm));
+    PM pm;
 
+    NEW0(pm);
     pm->pm_name = nocopy ? name : getcpy(name);
     pm->pm_value = nocopy ? value : getcpy(value);
 
@@ -4139,8 +4118,7 @@ get_param(PM first, const char *name, char replace, int fetchonly)
        if (strcasecmp(name, first->pm_name) == 0) {
            if (fetchonly)
                return first->pm_value;
-           else
-               return getcpy(get_param_value(first, replace));
+            return getcpy(get_param_value(first, replace));
        }
        first = first->pm_next;
     }
@@ -4251,6 +4229,10 @@ noiconv:
     q = buffer;
     bufsize = sizeof(buffer);
     for (p = pm->pm_value; *p != '\0' && bufsize > 1; p++, q++, bufsize--) {
+        /* FIXME: !iscntrl should perhaps be isprint as that allows all
+         * classes bar cntrl, whereas the cntrl class can include those
+         * in space and blank.
+         * http://pubs.opengroup.org/onlinepubs/009695399/basedefs/xbd_chap07.html */
        if (isascii((unsigned char) *p) && !iscntrl((unsigned char) *p))
            *q = *p;
        else