Skip to content

Commit

Permalink
lib-mail: message_header_hash_more() - add v3 that strips spaces
Browse files Browse the repository at this point in the history
This helps with Zimbra, which strips away trailing whitespaces in
BODY[HEADER].
  • Loading branch information
sirainen committed Jun 23, 2017
1 parent 14458ea commit ea03e17
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 3 deletions.
21 changes: 18 additions & 3 deletions src/lib-mail/message-header-hash.c
Expand Up @@ -11,7 +11,7 @@ void message_header_hash_more(struct message_header_hash_context *ctx,
{
size_t i, start;

i_assert(version == 1 || version == 2);
i_assert(version >= 1 && version <= MESSAGE_HEADER_HASH_MAX_VERSION);

if (version == 1) {
method->loop(context, data, size);
Expand All @@ -28,24 +28,39 @@ void message_header_hash_more(struct message_header_hash_context *ctx,
So we'll just replace all control and 8bit chars with '?' and
remove any repeated '?', which hopefully will satisfy everybody.
Also:
- Zimbra removes trailing spaces from IMAP BODY[HEADER], but not
IMAP BODY[] or POP3 TOP. Just strip away all spaces with version 3.
*/
for (i = start = 0; i < size; i++) {
bool cur_is_questionmark = FALSE;

switch (data[i]) {
case ' ':
if (version == 3) {
/* strip away spaces */
method->loop(context, data + start, i-start);
start = i+1;
}
break;
case '\t':
case '\n':
break;
default:
if (data[i] < 0x20 || data[i] >= 0x7f || data[i] == '?') {
/* remove repeated '?' */
if (start < i || (i == 0 && !ctx->prev_was_questionmark)) {
if (start < i || !ctx->prev_was_questionmark) {
method->loop(context, data + start, i-start);
method->loop(context, "?", 1);
}
start = i+1;
cur_is_questionmark = TRUE;
}
break;
}
ctx->prev_was_questionmark = cur_is_questionmark;
}
ctx->prev_was_questionmark = start == i;
method->loop(context, data + start, i-start);
}
2 changes: 2 additions & 0 deletions src/lib-mail/message-header-hash.h
@@ -1,6 +1,8 @@
#ifndef MESSAGE_HEADER_HASH_H
#define MESSAGE_HEADER_HASH_H

#define MESSAGE_HEADER_HASH_MAX_VERSION 3

struct hash_method;

struct message_header_hash_context {
Expand Down
12 changes: 12 additions & 0 deletions src/lib-mail/test-message-header-hash.c
Expand Up @@ -25,6 +25,18 @@ static const struct {
{ "\x01?hi??\x01", 2, "?hi?" },
{ "?\t?hi?\t?", 2, "?\t?hi?\t?" },
{ "\n\nhi\n\n", 2, "\n\nhi\n\n" },
{ "", 2, "" },
{ " ", 2, " " },
{ " ", 2, " " },
{ "? ? ? hi \x01\x02 \x03 ", 2, "? ? ? hi ? ? " },

{ test_input_with_nuls, 3, "?\t\n?!?x?yz?-plop?" },
{ "\n\nhi\n\n", 2, "\n\nhi\n\n" },
{ "", 3, "" },
{ " ", 3, "" },
{ " ", 3, "" },
{ " ? ", 3, "?" },
{ "? ? ? hi \x01\x02 \x03 ", 3, "???hi??" },
};

static void test_message_header_hash_more(void)
Expand Down

0 comments on commit ea03e17

Please sign in to comment.