From 73083841b92505091b7d1070554930651b91b701 Mon Sep 17 00:00:00 2001 From: Aki Tuomi Date: Mon, 15 Aug 2016 19:22:31 +0300 Subject: [PATCH] lib-mail: Fix snippet generation Now we won't add leading whitespace for no reason, and also strip BOM when found. --- src/lib-mail/message-snippet.c | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/lib-mail/message-snippet.c b/src/lib-mail/message-snippet.c index bfd20ce2af..20e5d0a813 100644 --- a/src/lib-mail/message-snippet.c +++ b/src/lib-mail/message-snippet.c @@ -30,7 +30,7 @@ struct snippet_context { static bool snippet_generate(struct snippet_context *ctx, const unsigned char *data, size_t size) { - unsigned int i, count; + size_t i, count; if (ctx->html2text != NULL) { buffer_set_used_size(ctx->plain_output, 0); @@ -42,6 +42,7 @@ static bool snippet_generate(struct snippet_context *ctx, /* message-decoder should feed us only valid and complete UTF-8 input */ + for (i = 0; i < size; i += count) { count = 1; switch (ctx->state) { @@ -53,9 +54,17 @@ static bool snippet_generate(struct snippet_context *ctx, ctx->state = SNIPPET_STATE_NORMAL; /* fallthrough */ case SNIPPET_STATE_NORMAL: + if (size-i >= 3 && + ((data[i] == U'\xEF' && data[i+1] == U'\xBB' && data[i+2] == U'\xBF') || + (data[i] == U'\xBF' && data[i+1] == U'\xBB' && data[i+2] == U'\xEF'))) { + count += 2; /* because we skip +1 next */ + break; + } if (data[i] == '\r' || data[i] == '\n' || data[i] == '\t' || data[i] == ' ') { - ctx->add_whitespace = TRUE; + /* skip any leading whitespace */ + if (str_len(ctx->snippet) > 1) + ctx->add_whitespace = TRUE; if (data[i] == '\n') ctx->state = SNIPPET_STATE_NEWLINE; break;