+/*
+ * Fix various problems that aren't handled elsewhere. These
+ * are fixed unconditionally: there are no switches to disable
+ * them. Currently, "problems" are these:
+ * 1) remove extraneous semicolon at the end of a header parameter list
+ * 2) replace RFC 2047 encoding with RFC 2231 encoding of name and
+ * filename parameters in Content-Type and Content-Disposition
+ * headers, respectively.
+ */
+static int
+fix_always (CT *ctp, const fix_transformations *fx, int *message_mods)
+{
+ int status = OK;
+
+ switch ((*ctp)->c_type) {
+ case CT_MULTIPART: {
+ struct multipart *m = (struct multipart *) (*ctp)->c_ctparams;
+ struct part *part;
+
+ for (part = m->mp_parts; status == OK && part; part = part->mp_next) {
+ status = fix_always (&part->mp_part, fx, message_mods);
+ }
+ break;
+ }
+
+ case CT_MESSAGE:
+ if ((*ctp)->c_subtype == MESSAGE_EXTERNAL) {
+ struct exbody *e = (struct exbody *) (*ctp)->c_ctparams;
+
+ status = fix_always (&e->eb_content, fx, message_mods);
+ }
+ break;
+
+ default: {
+ HF hf;
+
+ if ((*ctp)->c_first_hf) {
+ fix_filename_encoding (*ctp);
+ }
+
+ for (hf = (*ctp)->c_first_hf; hf; hf = hf->next) {
+ size_t len = strlen (hf->value);
+
+ if (strcasecmp (hf->name, TYPE_FIELD) != 0 &&
+ strcasecmp (hf->name, DISPO_FIELD) != 0) {
+ /* Only do this for Content-Type and
+ Content-Disposition fields because those are the
+ only headers that parse_mime() warns about. */
+ continue;
+ }
+
+ /* whitespace following a trailing ';' will be nuked as well */
+ if (hf->value[len - 1] == '\n') {
+ while (isspace((unsigned char)(hf->value[len - 2]))) {
+ if (len-- == 0) { break; }
+ }
+ }
+
+ if (hf->value[len - 2] == ';') {
+ /* Remove trailing ';' from parameter value. */
+ hf->value[len - 2] = '\n';
+ hf->value[len - 1] = '\0';
+
+ /* Also, if Content-Type parameter, remove trailing ';'
+ from (*ctp)->c_ctline. This probably isn't necessary
+ but can't hurt. */
+ if (strcasecmp(hf->name, TYPE_FIELD) == 0 && (*ctp)->c_ctline) {
+ size_t l = strlen((*ctp)->c_ctline) - 1;
+ while (isspace((unsigned char)((*ctp)->c_ctline[l])) ||
+ (*ctp)->c_ctline[l] == ';') {
+ (*ctp)->c_ctline[l--] = '\0';
+ if (l == 0) { break; }
+ }
+ }
+
+ ++*message_mods;
+ if (verbosw) {
+ report (NULL, (*ctp)->c_partno, (*ctp)->c_file,
+ "remove trailing ; from %s parameter value",
+ hf->name);
+ }
+ }
+ }
+
+ if (fx->checkbase64 && (*ctp)->c_encoding == CE_BASE64) {
+ status = check_base64_encoding (ctp);
+ }
+ }}
+
+ return status;
+}
+
+
+/*
+ * Decodes UTF-8 encoded header values. Similar to fix_filename_param(), but
+ * does not modify any MIME parameter values.
+ */
+static int
+decode_header_field_bodies (CT ct, int *message_mods)
+{
+ int status = OK;
+
+ switch (ct->c_type) {
+ case CT_MULTIPART: {
+ struct multipart *m = (struct multipart *) ct->c_ctparams;
+ struct part *part;
+
+ for (part = m->mp_parts; status == OK && part; part = part->mp_next) {
+ status = decode_header_field_bodies (part->mp_part, message_mods);
+ }
+ break;
+ }
+
+ case CT_MESSAGE:
+ if (ct->c_subtype == MESSAGE_EXTERNAL) {
+ struct exbody *e = (struct exbody *) ct->c_ctparams;
+
+ status = decode_header_field_bodies (e->eb_content, message_mods);
+ }
+ break;
+ }
+
+ HF hf;
+
+ for (hf = ct->c_first_hf; hf; hf = hf->next) {
+ /* Only decode UTF-8 values. */
+ if (hf->value && has_suffix(hf->value, "?=\n") &&
+ (! strncasecmp (hf->value, " =?utf8?", 8) ||
+ ! strncasecmp (hf->value, " =?utf-8?", 9))) {
+ /* Looks like an RFC 2047 encoded parameter. */
+ char decoded[PATH_MAX + 1];
+
+ if (decode_rfc2047 (hf->value, decoded, sizeof decoded)) {
+ const size_t len = strlen(decoded);
+
+ /* decode_rfc2047() could truncate if the buffer fills up.
+ Detect and discard if that happened. */
+ if (len < sizeof(decoded) - 1 && strcmp(hf->value, decoded)) {
+ hf->value = mh_xrealloc (hf->value, len + 1);
+ strncpy (hf->value, decoded, len + 1);
+ ++*message_mods;
+ }
+ } else {
+ inform("failed to decode %s parameter %s", hf->name, hf->value);
+ status = NOTOK;
+ }
+ }
+ }
+
+ return status;
+}
+
+
+/*
+ * Factor out common code for loops in fix_filename_encoding().
+ */
+static int
+fix_filename_param (char *name, char *value, PM *first_pm, PM *last_pm)
+{
+ bool fixed = false;
+
+ if (has_prefix(value, "=?") && has_suffix(value, "?=")) {
+ /* Looks like an RFC 2047 encoded parameter. */
+ char decoded[PATH_MAX + 1];
+
+ if (decode_rfc2047 (value, decoded, sizeof decoded)) {
+ /* Encode using RFC 2231. */
+ replace_param (first_pm, last_pm, name, decoded, 0);
+ fixed = true;
+ } else {
+ inform("failed to decode %s parameter %s", name, value);
+ }
+ }
+
+ return fixed;
+}
+
+
+/*
+ * Replace RFC 2047 encoding with RFC 2231 encoding of name and
+ * filename parameters in Content-Type and Content-Disposition
+ * headers, respectively.
+ */
+static int
+fix_filename_encoding (CT ct)
+{
+ PM pm;
+ HF hf;
+ int fixed = 0;
+
+ for (pm = ct->c_ctinfo.ci_first_pm; pm; pm = pm->pm_next) {
+ if (pm->pm_name && pm->pm_value &&
+ strcasecmp (pm->pm_name, "name") == 0) {
+ fixed = fix_filename_param (pm->pm_name, pm->pm_value,
+ &ct->c_ctinfo.ci_first_pm,
+ &ct->c_ctinfo.ci_last_pm);
+ }
+ }
+
+ for (pm = ct->c_dispo_first; pm; pm = pm->pm_next) {
+ if (pm->pm_name && pm->pm_value &&
+ strcasecmp (pm->pm_name, "filename") == 0) {
+ fixed = fix_filename_param (pm->pm_name, pm->pm_value,
+ &ct->c_dispo_first,
+ &ct->c_dispo_last);
+ }
+ }
+
+ /* Fix hf values to correspond. */
+ for (hf = ct->c_first_hf; fixed && hf; hf = hf->next) {
+ enum { OTHER, TYPE_HEADER, DISPO_HEADER } field = OTHER;
+
+ if (strcasecmp (hf->name, TYPE_FIELD) == 0) {
+ field = TYPE_HEADER;
+ } else if (strcasecmp (hf->name, DISPO_FIELD) == 0) {
+ field = DISPO_HEADER;
+ }
+
+ if (field != OTHER) {
+ const char *const semicolon_loc = strchr (hf->value, ';');
+
+ if (semicolon_loc) {
+ const size_t len =
+ strlen (hf->name) + 1 + semicolon_loc - hf->value;
+ const char *const params =
+ output_params (len,
+ field == TYPE_HEADER
+ ? ct->c_ctinfo.ci_first_pm
+ : ct->c_dispo_first,
+ NULL, 0);
+ const char *const new_params = concat (params, "\n", NULL);
+
+ replace_substring (&hf->value, semicolon_loc, new_params);
+ free((void *)new_params); /* Cast away const. Sigh. */
+ free((void *)params);
+ } else {
+ inform("did not find semicolon in %s:%s\n",
+ hf->name, hf->value);
+ }
+ }
+ }
+
+ return OK;
+}
+
+
+/*
+ * Output content in input file to output file.
+ */