t: add basic tests for our SHA-1 implementation
[gitweb.git] / mailinfo.c
index 36ec927b9ef9e41c08ba5934b450980002b98aca..b395adbdf2a405567bc40c254125265f30b12bbd 100644 (file)
@@ -1,4 +1,5 @@
 #include "cache.h"
+#include "config.h"
 #include "utf8.h"
 #include "strbuf.h"
 #include "mailinfo.h"
@@ -57,17 +58,17 @@ static void parse_bogus_from(struct mailinfo *mi, const struct strbuf *line)
 static const char *unquote_comment(struct strbuf *outbuf, const char *in)
 {
        int c;
-       int take_next_litterally = 0;
+       int take_next_literally = 0;
 
        strbuf_addch(outbuf, '(');
 
        while ((c = *in++) != 0) {
-               if (take_next_litterally == 1) {
-                       take_next_litterally = 0;
+               if (take_next_literally == 1) {
+                       take_next_literally = 0;
                } else {
                        switch (c) {
                        case '\\':
-                               take_next_litterally = 1;
+                               take_next_literally = 1;
                                continue;
                        case '(':
                                in = unquote_comment(outbuf, in);
@@ -87,15 +88,15 @@ static const char *unquote_comment(struct strbuf *outbuf, const char *in)
 static const char *unquote_quoted_string(struct strbuf *outbuf, const char *in)
 {
        int c;
-       int take_next_litterally = 0;
+       int take_next_literally = 0;
 
        while ((c = *in++) != 0) {
-               if (take_next_litterally == 1) {
-                       take_next_litterally = 0;
+               if (take_next_literally == 1) {
+                       take_next_literally = 0;
                } else {
                        switch (c) {
                        case '\\':
-                               take_next_litterally = 1;
+                               take_next_literally = 1;
                                continue;
                        case '"':
                                return in;
@@ -148,16 +149,14 @@ static void handle_from(struct mailinfo *mi, const struct strbuf *from)
        at = strchr(f.buf, '@');
        if (!at) {
                parse_bogus_from(mi, from);
-               return;
+               goto out;
        }
 
        /*
         * If we already have one email, don't take any confusing lines
         */
-       if (mi->email.len && strchr(at + 1, '@')) {
-               strbuf_release(&f);
-               return;
-       }
+       if (mi->email.len && strchr(at + 1, '@'))
+               goto out;
 
        /* Pick up the string around '@', possibly delimited with <>
         * pair; that is the email part.
@@ -197,6 +196,7 @@ static void handle_from(struct mailinfo *mi, const struct strbuf *from)
        }
 
        get_sane_name(&mi->name, &f, &mi->email);
+out:
        strbuf_release(&f);
 }
 
@@ -237,11 +237,22 @@ static int slurp_attr(const char *line, const char *name, struct strbuf *attr)
        return 1;
 }
 
+static int has_attr_value(const char *line, const char *name, const char *value)
+{
+       struct strbuf sb = STRBUF_INIT;
+       int rc = slurp_attr(line, name, &sb) && !strcasecmp(sb.buf, value);
+       strbuf_release(&sb);
+       return rc;
+}
+
 static void handle_content_type(struct mailinfo *mi, struct strbuf *line)
 {
        struct strbuf *boundary = xmalloc(sizeof(struct strbuf));
        strbuf_init(boundary, line->len);
 
+       mi->format_flowed = has_attr_value(line->buf, "format=", "flowed");
+       mi->delsp = has_attr_value(line->buf, "delsp=", "yes");
+
        if (slurp_attr(line->buf, "boundary=", boundary)) {
                strbuf_insert(boundary, 0, "--", 2);
                if (++mi->content_top >= &mi->content[MAX_BOUNDARIES]) {
@@ -367,11 +378,16 @@ static struct strbuf *decode_q_segment(const struct strbuf *q_seg, int rfc2047)
 
        while ((c = *in++) != 0) {
                if (c == '=') {
-                       int d = *in++;
+                       int ch, d = *in;
                        if (d == '\n' || !d)
                                break; /* drop trailing newline */
-                       strbuf_addch(out, (hexval(d) << 4) | hexval(*in++));
-                       continue;
+                       ch = hex2chr(in);
+                       if (ch >= 0) {
+                               strbuf_addch(out, ch);
+                               in += 2;
+                               continue;
+                       }
+                       /* garbage -- fall through */
                }
                if (rfc2047 && c == '_') /* rfc2047 4.2 (2) */
                        c = 0x20;
@@ -711,7 +727,7 @@ static void flush_inbody_header_accum(struct mailinfo *mi)
        if (!mi->inbody_header_accum.len)
                return;
        if (!check_header(mi, &mi->inbody_header_accum, mi->s_hdr_data, 0))
-               die("BUG: inbody_header_accum, if not empty, must always contain a valid in-body header");
+               BUG("inbody_header_accum, if not empty, must always contain a valid in-body header");
        strbuf_reset(&mi->inbody_header_accum);
 }
 
@@ -757,8 +773,13 @@ static int handle_commit_msg(struct mailinfo *mi, struct strbuf *line)
        assert(!mi->filter_stage);
 
        if (mi->header_stage) {
-               if (!line->len || (line->len == 1 && line->buf[0] == '\n'))
+               if (!line->len || (line->len == 1 && line->buf[0] == '\n')) {
+                       if (mi->inbody_header_accum.len) {
+                               flush_inbody_header_accum(mi);
+                               mi->header_stage = 0;
+                       }
                        return 0;
+               }
        }
 
        if (mi->use_inbody_headers && mi->header_stage) {
@@ -817,6 +838,7 @@ static void handle_filter(struct mailinfo *mi, struct strbuf *line)
                if (!handle_commit_msg(mi, line))
                        break;
                mi->filter_stage++;
+               /* fallthrough */
        case 1:
                handle_patch(mi, line);
                break;
@@ -877,7 +899,10 @@ static int read_one_header_line(struct strbuf *line, FILE *in)
        for (;;) {
                int peek;
 
-               peek = fgetc(in); ungetc(peek, in);
+               peek = fgetc(in);
+               if (peek == EOF)
+                       break;
+               ungetc(peek, in);
                if (peek != ' ' && peek != '\t')
                        break;
                if (strbuf_getline_lf(&continuation, in))
@@ -911,8 +936,7 @@ static int handle_boundary(struct mailinfo *mi, struct strbuf *line)
                /* we hit an end boundary */
                /* pop the current boundary off the stack */
                strbuf_release(*(mi->content_top));
-               free(*(mi->content_top));
-               *(mi->content_top) = NULL;
+               FREE_AND_NULL(*(mi->content_top));
 
                /* technically won't happen as is_multipart_boundary()
                   will fail first.  But just in case..
@@ -921,6 +945,7 @@ static int handle_boundary(struct mailinfo *mi, struct strbuf *line)
                        error("Detected mismatched boundaries, can't recover");
                        mi->input_error = -1;
                        mi->content_top = mi->content;
+                       strbuf_release(&newline);
                        return 0;
                }
                handle_filter(mi, &newline);
@@ -950,6 +975,52 @@ static int handle_boundary(struct mailinfo *mi, struct strbuf *line)
        return 1;
 }
 
+static void handle_filter_flowed(struct mailinfo *mi, struct strbuf *line,
+                                struct strbuf *prev)
+{
+       size_t len = line->len;
+       const char *rest;
+
+       if (!mi->format_flowed) {
+               handle_filter(mi, line);
+               return;
+       }
+
+       if (line->buf[len - 1] == '\n') {
+               len--;
+               if (len && line->buf[len - 1] == '\r')
+                       len--;
+       }
+
+       /* Keep signature separator as-is. */
+       if (skip_prefix(line->buf, "-- ", &rest) && rest - line->buf == len) {
+               if (prev->len) {
+                       handle_filter(mi, prev);
+                       strbuf_reset(prev);
+               }
+               handle_filter(mi, line);
+               return;
+       }
+
+       /* Unstuff space-stuffed line. */
+       if (len && line->buf[0] == ' ') {
+               strbuf_remove(line, 0, 1);
+               len--;
+       }
+
+       /* Save flowed line for later, but without the soft line break. */
+       if (len && line->buf[len - 1] == ' ') {
+               strbuf_add(prev, line->buf, len - !!mi->delsp);
+               return;
+       }
+
+       /* Prepend any previous partial lines */
+       strbuf_insert(line, 0, prev->buf, prev->len);
+       strbuf_reset(prev);
+
+       handle_filter(mi, line);
+}
+
 static void handle_body(struct mailinfo *mi, struct strbuf *line)
 {
        struct strbuf prev = STRBUF_INIT;
@@ -998,7 +1069,7 @@ static void handle_body(struct mailinfo *mi, struct strbuf *line)
                                                strbuf_addbuf(&prev, sb);
                                                break;
                                        }
-                               handle_filter(mi, sb);
+                               handle_filter_flowed(mi, sb, &prev);
                        }
                        /*
                         * The partial chunk is saved in "prev" and will be
@@ -1008,13 +1079,16 @@ static void handle_body(struct mailinfo *mi, struct strbuf *line)
                        break;
                }
                default:
-                       handle_filter(mi, line);
+                       handle_filter_flowed(mi, line, &prev);
                }
 
                if (mi->input_error)
                        break;
        } while (!strbuf_getwholeline(line, mi->input, '\n'));
 
+       if (prev.len)
+               handle_filter(mi, &prev);
+
        flush_inbody_header_accum(mi);
 
 handle_body_out:
@@ -1094,6 +1168,10 @@ int mailinfo(struct mailinfo *mi, const char *msg, const char *patch)
 
        do {
                peek = fgetc(mi->input);
+               if (peek == EOF) {
+                       fclose(cmitmsg);
+                       return error("empty patch: '%s'", patch);
+               }
        } while (isspace(peek));
        ungetc(peek, mi->input);