Move B and Q decoding into check header.
[git.git] / mailinfo.c
index 3c56f8c..bee7b20 100644 (file)
@@ -324,6 +324,7 @@ static void cleanup_space(char *buf)
        }
 }
 
+static void decode_header_bq(char *it);
 typedef int (*header_fn_t)(char *);
 struct header_def {
        const char *name;
@@ -331,7 +332,7 @@ struct header_def {
        int namelen;
 };
 
-static void check_header(char *line, int len, struct header_def *header)
+static void check_header(char *line, struct header_def *header)
 {
        int i;
 
@@ -343,13 +344,17 @@ static void check_header(char *line, int len, struct header_def *header)
                int len = header[i].namelen;
                if (!strncasecmp(line, header[i].name, len) &&
                    line[len] == ':' && isspace(line[len + 1])) {
+                       /* Unwrap inline B and Q encoding, and optionally
+                        * normalize the meta information to utf8.
+                        */
+                       decode_header_bq(line + len + 2);
                        header[i].func(line + len + 2);
                        break;
                }
        }
 }
 
-static void check_subheader_line(char *line, int len)
+static void check_subheader_line(char *line)
 {
        static struct header_def header[] = {
                { "Content-Type", handle_subcontent_type },
@@ -357,9 +362,9 @@ static void check_subheader_line(char *line, int len)
                  handle_content_transfer_encoding },
                { NULL },
        };
-       check_header(line, len, header);
+       check_header(line, header);
 }
-static void check_header_line(char *line, int len)
+static void check_header_line(char *line)
 {
        static struct header_def header[] = {
                { "From", handle_from },
@@ -370,7 +375,7 @@ static void check_header_line(char *line, int len)
                  handle_content_transfer_encoding },
                { NULL },
        };
-       check_header(line, len, header);
+       check_header(line, header);
 }
 
 static int read_one_header_line(char *line, int sz, FILE *in)
@@ -405,7 +410,7 @@ static unsigned hexval(int c)
        return ~0;
 }
 
-static int decode_q_segment(char *in, char *ot, char *ep)
+static int decode_q_segment(char *in, char *ot, char *ep, int rfc2047)
 {
        int c;
        while ((c = *in++) != 0 && (in <= ep)) {
@@ -414,9 +419,11 @@ static int decode_q_segment(char *in, char *ot, char *ep)
                        if (d == '\n' || !d)
                                break; /* drop trailing newline */
                        *ot++ = ((hexval(d) << 4) | hexval(*in++));
+                       continue;
                }
-               else
-                       *ot++ = c;
+               if (rfc2047 && c == '_') /* rfc2047 4.2 (2) */
+                       c = 0x20;
+               *ot++ = c;
        }
        *ot = 0;
        return 0;
@@ -547,7 +554,7 @@ static void decode_header_bq(char *it)
                        sz = decode_b_segment(cp + 3, piecebuf, ep);
                        break;
                case 'q':
-                       sz = decode_q_segment(cp + 3, piecebuf, ep);
+                       sz = decode_q_segment(cp + 3, piecebuf, ep, 1);
                        break;
                }
                if (sz < 0)
@@ -569,7 +576,7 @@ static void decode_transfer_encoding(char *line)
        switch (transfer_encoding) {
        case TE_QP:
                ep = line + strlen(line);
-               decode_q_segment(line, line, ep);
+               decode_q_segment(line, line, ep, 0);
                break;
        case TE_BASE64:
                ep = line + strlen(line);
@@ -595,13 +602,6 @@ static void handle_info(void)
        cleanup_space(email);
        cleanup_space(sub);
 
-       /* Unwrap inline B and Q encoding, and optionally
-        * normalize the meta information to utf8.
-        */
-       decode_header_bq(name);
-       decode_header_bq(date);
-       decode_header_bq(email);
-       decode_header_bq(sub);
        printf("Author: %s\nEmail: %s\nSubject: %s\nDate: %s\n\n",
               name, email, sub, date);
 }
@@ -707,8 +707,8 @@ static void handle_multipart_body(void)
                return;
        /* We are on boundary line.  Start slurping the subhead. */
        while (1) {
-               int len = read_one_header_line(line, sizeof(line), stdin);
-               if (!len) {
+               int hdr = read_one_header_line(line, sizeof(line), stdin);
+               if (!hdr) {
                        if (handle_multipart_one_part() < 0)
                                return;
                        /* Reset per part headers */
@@ -716,7 +716,7 @@ static void handle_multipart_body(void)
                        charset[0] = 0;
                }
                else
-                       check_subheader_line(line, len);
+                       check_subheader_line(line);
        }
        fclose(patchfile);
        if (!patch_lines) {
@@ -785,15 +785,15 @@ int main(int argc, char **argv)
                exit(1);
        }
        while (1) {
-               int len = read_one_header_line(line, sizeof(line), stdin);
-               if (!len) {
+               int hdr = read_one_header_line(line, sizeof(line), stdin);
+               if (!hdr) {
                        if (multipart_boundary[0])
                                handle_multipart_body();
                        else
                                handle_body();
                        break;
                }
-               check_header_line(line, len);
+               check_header_line(line);
        }
        return 0;
 }