From: Timo Sirainen Date: Fri, 10 Aug 2012 04:31:28 +0000 (+0300) Subject: quoted-printable decode didn't ignore whitespace at the end of soft line break. X-Git-Tag: 2.2.alpha1~380 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=2f67f2fbb4b145ce7ba39ef9f15932aeadeb1747;p=thirdparty%2Fdovecot%2Fcore.git quoted-printable decode didn't ignore whitespace at the end of soft line break. --- diff --git a/src/lib-mail/quoted-printable.c b/src/lib-mail/quoted-printable.c index 6a6214762c..edab5e733d 100644 --- a/src/lib-mail/quoted-printable.c +++ b/src/lib-mail/quoted-printable.c @@ -8,11 +8,31 @@ #define QP_IS_TRAILING_SPACE(c) \ ((c) == ' ' || (c) == '\t') +static int +qp_is_end_of_line(const unsigned char *src, size_t *src_pos, size_t size) +{ + size_t i = *src_pos; + + i_assert(src[i] == '='); + for (i++; i < size; i++) { + if (QP_IS_TRAILING_SPACE(src[i]) || src[i] == '\r') + continue; + + if (src[i] != '\n') + return 0; + + *src_pos = i; + return 1; + } + return -1; +} + void quoted_printable_decode(const unsigned char *src, size_t src_size, size_t *src_pos_r, buffer_t *dest) { char hexbuf[3]; size_t src_pos, pos, next; + int ret; hexbuf[2] = '\0'; @@ -38,25 +58,17 @@ void quoted_printable_decode(const unsigned char *src, size_t src_size, buffer_append(dest, src + next, src_pos - next); next = src_pos; - if (src_pos+1 >= src_size) - break; - - if (src[src_pos+1] == '\n') { - /* =\n -> skip both */ - src_pos++; - next += 2; + if ((ret = qp_is_end_of_line(src, &src_pos, src_size)) > 0) { + /* =[whitespace][\r]\n */ + next = src_pos+1; continue; } - - if (src_pos+2 >= src_size) + if (ret < 0) { + /* unknown yet if this is end of line */ break; - - if (src[src_pos+1] == '\r' && src[src_pos+2] == '\n') { - /* =\r\n -> skip both */ - src_pos += 2; - next += 3; - continue; } + if (src_pos+2 >= src_size) + break; /* = */ hexbuf[0] = src[src_pos+1]; diff --git a/src/lib-mail/test-quoted-printable.c b/src/lib-mail/test-quoted-printable.c index 2270d738a0..4587fb0e5a 100644 --- a/src/lib-mail/test-quoted-printable.c +++ b/src/lib-mail/test-quoted-printable.c @@ -16,9 +16,9 @@ static void test_quoted_printable_decode(void) { static struct test_quoted_printable_decode_data data[] = { { "foo \r\nbar=", "foo\r\nbar", 1 }, - { "foo =\nbar", "foo bar", 0 }, - { "foo =\n=01", "foo \001", 0 }, - { "foo =\r\nbar", "foo bar", 0 }, + { "foo\t=\nbar", "foo\tbar", 0 }, + { "foo = \n=01", "foo \001", 0 }, + { "foo =\t\r\nbar", "foo bar", 0 }, { "foo =\r\n=01", "foo \001", 0 }, { "foo \nbar=", "foo\r\nbar", 1 }, { "=0A=0D ", "\n\r", 2 },