+typedef int (*header_fn_t)(char *);
+struct header_def {
+ const char *name;
+ header_fn_t func;
+ int namelen;
+};
+
+static void check_header(char *line, int len, struct header_def *header)
+{
+ int i;
+
+ if (header[0].namelen <= 0) {
+ for (i = 0; header[i].name; i++)
+ header[i].namelen = strlen(header[i].name);
+ }
+ for (i = 0; header[i].name; i++) {
+ int len = header[i].namelen;
+ if (!strncasecmp(line, header[i].name, len) &&
+ line[len] == ':' && isspace(line[len + 1])) {
+ header[i].func(line + len + 2);
+ break;
+ }
+ }
+}
+
+static void check_subheader_line(char *line, int len)
+{
+ static struct header_def header[] = {
+ { "Content-Type", handle_subcontent_type },
+ { "Content-Transfer-Encoding",
+ handle_content_transfer_encoding },
+ { NULL },
+ };
+ check_header(line, len, header);
+}
+static void check_header_line(char *line, int len)
+{
+ static struct header_def header[] = {
+ { "From", handle_from },
+ { "Date", handle_date },
+ { "Subject", handle_subject },
+ { "Content-Type", handle_content_type },
+ { "Content-Transfer-Encoding",
+ handle_content_transfer_encoding },
+ { NULL },
+ };
+ check_header(line, len, header);
+}
+
+static int read_one_header_line(char *line, int sz, FILE *in)
+{
+ int ofs = 0;
+ while (ofs < sz) {
+ int peek, len;
+ if (fgets(line + ofs, sz - ofs, in) == NULL)
+ return ofs;
+ len = eatspace(line + ofs);
+ if (len == 0)
+ return ofs;
+ peek = fgetc(in); ungetc(peek, in);
+ if (peek == ' ' || peek == '\t') {
+ /* Yuck, 2822 header "folding" */
+ ofs += len;
+ continue;
+ }
+ return ofs + len;
+ }
+ return ofs;
+}
+
+static unsigned hexval(int c)
+{
+ if (c >= '0' && c <= '9')
+ return c - '0';
+ if (c >= 'a' && c <= 'f')
+ return c - 'a' + 10;
+ if (c >= 'A' && c <= 'F')
+ return c - 'A' + 10;
+ return ~0;
+}
+
+static int decode_q_segment(char *in, char *ot, char *ep)
+{
+ int c;
+ while ((c = *in++) != 0 && (in <= ep)) {
+ if (c == '=') {
+ int d = *in++;
+ if (d == '\n' || !d)
+ break; /* drop trailing newline */
+ *ot++ = ((hexval(d) << 4) | hexval(*in++));
+ }
+ else
+ *ot++ = c;
+ }
+ *ot = 0;
+ return 0;
+}
+
+static int decode_b_segment(char *in, char *ot, char *ep)
+{
+ /* Decode in..ep, possibly in-place to ot */
+ int c, pos = 0, acc = 0;
+
+ while ((c = *in++) != 0 && (in <= ep)) {
+ if (c == '+')
+ c = 62;
+ else if (c == '/')
+ c = 63;
+ else if ('A' <= c && c <= 'Z')
+ c -= 'A';
+ else if ('a' <= c && c <= 'z')
+ c -= 'a' - 26;
+ else if ('0' <= c && c <= '9')
+ c -= '0' - 52;
+ else if (c == '=') {
+ /* padding is almost like (c == 0), except we do
+ * not output NUL resulting only from it;
+ * for now we just trust the data.
+ */
+ c = 0;
+ }
+ else
+ continue; /* garbage */
+ switch (pos++) {
+ case 0:
+ acc = (c << 2);
+ break;
+ case 1:
+ *ot++ = (acc | (c >> 4));
+ acc = (c & 15) << 4;
+ break;
+ case 2:
+ *ot++ = (acc | (c >> 2));
+ acc = (c & 3) << 6;
+ break;
+ case 3:
+ *ot++ = (acc | c);
+ acc = pos = 0;
+ break;
+ }
+ }
+ *ot = 0;
+ return 0;
+}
+
+static void convert_to_utf8(char *line, char *charset)
+{
+#ifndef NO_ICONV
+ char *in, *out;
+ size_t insize, outsize, nrc;
+ char outbuf[4096]; /* cheat */
+ static char latin_one[] = "latin1";
+ char *input_charset = *charset ? charset : latin_one;
+ iconv_t conv = iconv_open(metainfo_charset, input_charset);
+
+ if (conv == (iconv_t) -1) {
+ static int warned_latin1_once = 0;
+ if (input_charset != latin_one) {
+ fprintf(stderr, "cannot convert from %s to %s\n",
+ input_charset, metainfo_charset);
+ *charset = 0;
+ }
+ else if (!warned_latin1_once) {
+ warned_latin1_once = 1;
+ fprintf(stderr, "tried to convert from %s to %s, "
+ "but your iconv does not work with it.\n",
+ input_charset, metainfo_charset);
+ }
+ return;
+ }
+ in = line;
+ insize = strlen(in);
+ out = outbuf;
+ outsize = sizeof(outbuf);
+ nrc = iconv(conv, &in, &insize, &out, &outsize);
+ iconv_close(conv);
+ if (nrc == (size_t) -1)
+ return;
+ *out = 0;
+ strcpy(line, outbuf);
+#endif
+}
+
+static void decode_header_bq(char *it)
+{
+ char *in, *out, *ep, *cp, *sp;
+ char outbuf[1000];
+
+ in = it;
+ out = outbuf;
+ while ((ep = strstr(in, "=?")) != NULL) {
+ int sz, encoding;
+ char charset_q[256], piecebuf[256];
+ if (in != ep) {
+ sz = ep - in;
+ memcpy(out, in, sz);
+ out += sz;
+ in += sz;
+ }
+ /* E.g.
+ * ep : "=?iso-2022-jp?B?GyR...?= foo"
+ * ep : "=?ISO-8859-1?Q?Foo=FCbar?= baz"
+ */
+ ep += 2;
+ cp = strchr(ep, '?');
+ if (!cp)
+ return; /* no munging */
+ for (sp = ep; sp < cp; sp++)
+ charset_q[sp - ep] = tolower(*sp);
+ charset_q[cp - ep] = 0;
+ encoding = cp[1];
+ if (!encoding || cp[2] != '?')
+ return; /* no munging */
+ ep = strstr(cp + 3, "?=");
+ if (!ep)
+ return; /* no munging */
+ switch (tolower(encoding)) {
+ default:
+ return; /* no munging */
+ case 'b':
+ sz = decode_b_segment(cp + 3, piecebuf, ep);
+ break;
+ case 'q':
+ sz = decode_q_segment(cp + 3, piecebuf, ep);
+ break;
+ }
+ if (sz < 0)
+ return;
+ if (metainfo_charset)
+ convert_to_utf8(piecebuf, charset_q);
+ strcpy(out, piecebuf);
+ out += strlen(out);
+ in = ep + 2;
+ }
+ strcpy(out, in);
+ strcpy(it, outbuf);
+}
+
+static void decode_transfer_encoding(char *line)
+{
+ char *ep;
+
+ switch (transfer_encoding) {
+ case TE_QP:
+ ep = line + strlen(line);
+ decode_q_segment(line, line, ep);
+ break;
+ case TE_BASE64:
+ ep = line + strlen(line);
+ decode_b_segment(line, line, ep);
+ break;
+ case TE_DONTCARE:
+ break;
+ }
+}
+
+static void handle_info(void)