+int
+convert_charset (CT ct, char *dest_charset, int *message_mods) {
+ char *src_charset = content_charset (ct);
+ int status = OK;
+
+ if (strcasecmp (src_charset, dest_charset)) {
+#ifdef HAVE_ICONV
+ iconv_t conv_desc = NULL;
+ char *dest;
+ int fd = -1;
+ char **file = NULL;
+ FILE **fp = NULL;
+ size_t begin;
+ size_t end;
+ int opened_input_file = 0;
+ char src_buffer[BUFSIZ];
+ size_t dest_buffer_size = BUFSIZ;
+ char *dest_buffer = mh_xmalloc(dest_buffer_size);
+ HF hf;
+ char *tempfile;
+ int fromutf8 = !strcasecmp(src_charset, "UTF-8");
+
+ if ((conv_desc = iconv_open (dest_charset, src_charset)) ==
+ (iconv_t) -1) {
+ advise (NULL, "Can't convert %s to %s", src_charset, dest_charset);
+ free (src_charset);
+ return NOTOK;
+ }
+
+ if ((tempfile = m_mktemp2 (NULL, invo_name, &fd, NULL)) == NULL) {
+ adios (NULL, "unable to create temporary file in %s",
+ get_temp_dir());
+ }
+ dest = add (tempfile, NULL);
+
+ if (ct->c_cefile.ce_file) {
+ file = &ct->c_cefile.ce_file;
+ fp = &ct->c_cefile.ce_fp;
+ begin = end = 0;
+ } else if (ct->c_file) {
+ file = &ct->c_file;
+ fp = &ct->c_fp;
+ begin = (size_t) ct->c_begin;
+ end = (size_t) ct->c_end;
+ } /* else no input file: shouldn't happen */
+
+ if (file && *file && fp) {
+ if (! *fp) {
+ if ((*fp = fopen (*file, "r")) == NULL) {
+ advise (*file, "unable to open for reading");
+ status = NOTOK;
+ } else {
+ opened_input_file = 1;
+ }
+ }
+ }
+
+ if (fp && *fp) {
+ size_t inbytes;
+ size_t bytes_to_read =
+ end > 0 && end > begin ? end - begin : sizeof src_buffer;
+
+ fseeko (*fp, begin, SEEK_SET);
+ while ((inbytes = fread (src_buffer, 1,
+ min (bytes_to_read, sizeof src_buffer),
+ *fp)) > 0) {
+ ICONV_CONST char *ib = src_buffer;
+ char *ob = dest_buffer;
+ size_t outbytes = dest_buffer_size;
+ size_t outbytes_before = outbytes;
+
+ if (end > 0) bytes_to_read -= inbytes;
+
+iconv_start:
+ if (iconv (conv_desc, &ib, &inbytes, &ob, &outbytes) ==
+ (size_t) -1) {
+ if (errno == E2BIG) {
+ /*
+ * Bump up the buffer by at least a factor of 2
+ * over what we need.
+ */
+ size_t bumpup = inbytes * 2, ob_off = ob - dest_buffer;
+ dest_buffer_size += bumpup;
+ dest_buffer = mh_xrealloc(dest_buffer,
+ dest_buffer_size);
+ ob = dest_buffer + ob_off;
+ outbytes += bumpup;
+ outbytes_before += bumpup;
+ goto iconv_start;
+ }
+ if (errno == EINVAL) {
+ /* middle of multi-byte sequence */
+ if (write (fd, dest_buffer, outbytes_before - outbytes) < 0) {
+ advise (dest, "write");
+ }
+ fseeko (*fp, -inbytes, SEEK_CUR);
+ if (end > 0) bytes_to_read += inbytes;
+ /* advise(NULL, "convert_charset: EINVAL"); */
+ continue;
+ }
+ if (errno == EILSEQ) {
+ /* invalid multi-byte sequence */
+ if (fromutf8) {
+ for (++ib, --inbytes;
+ inbytes > 0 &&
+ (((unsigned char) *ib) & 0xc0) == 0x80;
+ ++ib, --inbytes)
+ continue;
+ } else {
+ ib++; inbytes--; /* skip it */
+ }
+ (*ob++) = '?'; outbytes --;
+ /* advise(NULL, "convert_charset: EILSEQ"); */
+ goto iconv_start;
+ }
+ advise (NULL, "convert_charset: errno = %d", errno);
+ status = NOTOK;
+ break;
+ } else {
+ if (write (fd, dest_buffer, outbytes_before - outbytes)
+ < 0) {
+ advise (dest, "write");
+ }
+ }
+ }
+
+ if (opened_input_file) {
+ fclose (*fp);
+ *fp = NULL;
+ }
+ }
+
+ iconv_close (conv_desc);
+ close (fd);
+
+ if (status == OK) {
+ /* Replace the decoded file with the converted one. */
+ if (ct->c_cefile.ce_file) {
+ if (ct->c_cefile.ce_unlink) {
+ (void) m_unlink (ct->c_cefile.ce_file);
+ }
+ free (ct->c_cefile.ce_file);
+ }
+ ct->c_cefile.ce_file = dest;
+ ct->c_cefile.ce_unlink = 1;
+
+ ++*message_mods;
+
+ /* Update ct->c_ctline. */
+ if (ct->c_ctline) {
+ char *ctline = concat(" ", ct->c_ctinfo.ci_type, "/",
+ ct->c_ctinfo.ci_subtype, NULL);
+ char *outline;
+
+ replace_param(&ct->c_ctinfo.ci_first_pm,
+ &ct->c_ctinfo.ci_last_pm, "charset",
+ dest_charset, 0);
+ outline = output_params(strlen(TYPE_FIELD) + 1 + strlen(ctline),
+ ct->c_ctinfo.ci_first_pm, NULL, 0);
+ if (outline) {
+ ctline = add(outline, ctline);
+ free(outline);
+ }
+
+ free (ct->c_ctline);
+ ct->c_ctline = ctline;
+ } /* else no CT line, which is odd */
+
+ /* Update Content-Type header field. */
+ for (hf = ct->c_first_hf; hf; hf = hf->next) {
+ if (! strcasecmp (TYPE_FIELD, hf->name)) {
+ char *ctline = concat (ct->c_ctline, "\n", NULL);
+
+ free (hf->value);
+ hf->value = ctline;
+ break;
+ }
+ }
+ } else {
+ (void) m_unlink (dest);
+ }
+ free(dest_buffer);
+#else /* ! HAVE_ICONV */
+ NMH_UNUSED (message_mods);
+
+ advise (NULL, "Can't convert %s to %s without iconv", src_charset,
+ dest_charset);
+ errno = ENOSYS;
+ status = NOTOK;
+#endif /* ! HAVE_ICONV */
+ }
+
+ free (src_charset);
+ return status;
+}
+