+ if (strcasecmp (type, ct_type_subtype)) {
+ char *ct_type, *ct_subtype;
+ HF hf;
+
+ /* The Content-Type header does not match the
+ content, so update these struct Content
+ fields to match:
+ * c_type, c_subtype
+ * c_ctinfo.ci_type, c_ctinfo.ci_subtype
+ * c_ctline
+ */
+ /* Extract type and subtype from type/subtype. */
+ ct_type = mh_xstrdup(ct_type_subtype);
+ if ((cp = strchr (ct_type, '/'))) {
+ *cp = '\0';
+ ct_subtype = mh_xstrdup(++cp);
+ } else {
+ advise (NULL, "missing / in MIME type of %s %s",
+ ct->c_file, ct->c_partno);
+ free (ct_type);
+ return NOTOK;
+ }
+
+ ct->c_type = ct_str_type (ct_type);
+ ct->c_subtype = ct_str_subtype (ct->c_type, ct_subtype);
+
+ free (ct->c_ctinfo.ci_type);
+ ct->c_ctinfo.ci_type = ct_type;
+ free (ct->c_ctinfo.ci_subtype);
+ ct->c_ctinfo.ci_subtype = ct_subtype;
+ if (! replace_substring (&ct->c_ctline, type,
+ ct_type_subtype)) {
+ advise (NULL, "did not find %s in %s",
+ type, ct->c_ctline);
+ }
+
+ /* Update Content-Type header field. */
+ for (hf = ct->c_first_hf; hf; hf = hf->next) {
+ if (! strcasecmp (TYPE_FIELD, hf->name)) {
+ if (replace_substring (&hf->value, type,
+ ct_type_subtype)) {
+ ++*message_mods;
+ if (verbosw) {
+ report (NULL, ct->c_partno, ct->c_file,
+ "change Content-Type in header "
+ "from %s to %s",
+ type, ct_type_subtype);
+ }
+ break;
+ } else {
+ advise (NULL, "did not find %s in %s",
+ type, hf->value);
+ }
+ }
+ }
+ }
+ free (ct_type_subtype);
+ }
+ free (type_subtype);
+ }
+ }
+ }}
+
+ return status;
+}
+
+
+/*
+ * Replace a substring, allocating space to hold the new one.
+ */
+char *
+replace_substring (char **str, const char *old, const char *new) {
+ char *cp;
+
+ if ((cp = strstr (*str, old))) {
+ char *remainder = cp + strlen (old);
+ char *prefix, *new_str;
+
+ if (cp - *str) {
+ prefix = mh_xstrdup(*str);
+ *(prefix + (cp - *str)) = '\0';
+ new_str = concat (prefix, new, remainder, NULL);
+ free (prefix);
+ } else {
+ new_str = concat (new, remainder, NULL);
+ }
+
+ free (*str);
+
+ return *str = new_str;
+ }
+
+ return NULL;
+}
+
+
+/*
+ * Remove a name=value parameter, given just its name, from a header value.
+ */
+char *
+remove_parameter (char *str, const char *name) {
+ /* It looks to me, based on the BNF in RFC 2045, than there can't
+ be whitespace betwwen the parameter name and the "=", or
+ between the "=" and the parameter value. */
+ char *param_name = concat (name, "=", NULL);
+ char *cp;
+
+ if ((cp = strstr (str, param_name))) {
+ char *start, *end;
+ size_t count = 1;
+
+ /* Remove any leading spaces, before the parameter name. */
+ for (start = cp;
+ start > str && isspace ((unsigned char) *(start-1));
+ --start) {
+ continue;
+ }
+ /* Remove a leading semicolon. */
+ if (start > str && *(start-1) == ';') { --start; }
+
+ end = cp + strlen (name) + 1;
+ if (*end == '"') {
+ /* Skip past the quoted value, and then the final quote. */
+ for (++end ; *end && *end != '"'; ++end) { continue; }
+ ++end;
+ } else {
+ /* Skip past the value. */
+ for (++end ; *end && ! isspace ((unsigned char) *end); ++end) {}
+ }
+
+ /* Count how many characters need to be moved. Include
+ trailing null, which is accounted for by the
+ initialization of count to 1. */
+ for (cp = end; *cp; ++cp) { ++count; }
+ (void) memmove (start, end, count);