diff options
author | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2021-06-18 13:35:05 +0100 |
---|---|---|
committer | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2021-06-18 13:35:05 +0100 |
commit | de7ac4e37284fcd241060213619297df41a71dce (patch) | |
tree | eebb222e637b021c78888a84f6022a3231aba0d6 /src/libmime | |
parent | f6321fbd323034d9763e53a56af014872a8a625a (diff) | |
download | rspamd-de7ac4e37284fcd241060213619297df41a71dce.tar.gz rspamd-de7ac4e37284fcd241060213619297df41a71dce.zip |
[Minor] Add a simple routine to remove smtp comments in place
Diffstat (limited to 'src/libmime')
-rw-r--r-- | src/libmime/mime_headers.c | 87 | ||||
-rw-r--r-- | src/libmime/mime_headers.h | 8 |
2 files changed, 95 insertions, 0 deletions
diff --git a/src/libmime/mime_headers.c b/src/libmime/mime_headers.c index 9dc336cf7..0bca94024 100644 --- a/src/libmime/mime_headers.c +++ b/src/libmime/mime_headers.c @@ -1967,4 +1967,91 @@ rspamd_message_set_modified_header (struct rspamd_task *task, } } } +} + +gsize +rspamd_strip_smtp_comments_inplace (gchar *input, gsize len) +{ + enum parser_state { + parse_normal, + parse_obrace, + parse_comment, + parse_quoted_copy, + parse_quoted_ignore, + } state = parse_normal, next_state = parse_normal; + gchar *d = input, *end = input + len, *start = input; + gchar t; + int obraces = 0, ebraces = 0; + + while (input < end) { + t = *input; + switch (state) { + case parse_normal: + if (t == '(') { + state = parse_obrace; + } + else if (t == '\\') { + state = parse_quoted_copy; + next_state = parse_normal; + } + else { + *d++ = t; + } + input ++; + break; + case parse_obrace: + obraces ++; + if (t == '(') { + obraces ++; + } + else if (t == ')') { + ebraces ++; + + if (obraces == ebraces) { + obraces = 0; + ebraces = 0; + state = parse_normal; + } + } + else if (t == '\\') { + state = parse_quoted_ignore; + next_state = parse_comment; + } + else { + state = parse_comment; + } + input ++; + break; + case parse_comment: + if (t == '(') { + state = parse_obrace; + } + else if (t == ')') { + ebraces ++; + + if (obraces == ebraces) { + obraces = 0; + ebraces = 0; + state = parse_normal; + } + } + else if (t == '\\') { + state = parse_quoted_ignore; + next_state = parse_comment; + } + input ++; + break; + case parse_quoted_copy: + *d++ = t; + state = next_state; + input ++; + break; + case parse_quoted_ignore: + state = next_state; + input ++; + break; + } + } + + return (d - start); }
\ No newline at end of file diff --git a/src/libmime/mime_headers.h b/src/libmime/mime_headers.h index ad8f1b68f..f24b0d6c6 100644 --- a/src/libmime/mime_headers.h +++ b/src/libmime/mime_headers.h @@ -206,6 +206,14 @@ struct rspamd_mime_headers_table * rspamd_message_headers_ref (struct rspamd_mim */ struct rspamd_mime_headers_table* rspamd_message_headers_new (void); +/** + * Strip rfc822 CFWS sequences from a string in place + * @param input input + * @param len length of the input + * @return new length of the input + */ +gsize rspamd_strip_smtp_comments_inplace (gchar *input, gsize len); + #ifdef __cplusplus } #endif |