From f0cbfec0eed63d3ad36868835e93973efd383012 Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Mon, 29 Mar 2021 14:16:06 +0100 Subject: [Minor] Lowercase CSS --- src/libserver/css/css_parser.cxx | 25 +++++++++++++++++++++---- src/libserver/css/css_util.cxx | 16 ++++++++-------- 2 files changed, 29 insertions(+), 12 deletions(-) (limited to 'src/libserver/css') diff --git a/src/libserver/css/css_parser.cxx b/src/libserver/css/css_parser.cxx index f80386fc2..d828ffa46 100644 --- a/src/libserver/css/css_parser.cxx +++ b/src/libserver/css/css_parser.cxx @@ -18,6 +18,7 @@ #include "css_tokeniser.hxx" #include "css_selector.hxx" #include "css_rule.hxx" +#include "css_util.hxx" #include "css.hxx" #include "fmt/core.h" @@ -159,6 +160,9 @@ public: return tl::make_unexpected(error); } + /* Helper parser methods */ + bool need_unescape(const std::string_view &sv); + private: std::unique_ptr style_object; std::unique_ptr tokeniser; @@ -170,9 +174,6 @@ private: const int max_rec = 20; bool eof = false; - /* Helper parser methods */ - bool need_unescape(const std::string_view &sv); - /* Consumers */ auto component_value_consumer(std::unique_ptr &top) -> bool; auto function_consumer(std::unique_ptr &top) -> bool; @@ -684,8 +685,24 @@ auto parse_css(rspamd_mempool_t *pool, const std::string_view &st) -> tl::expected, css_parse_error> { css_parser parser(pool); + std::string_view processed_input; + + if (parser.need_unescape(st)) { + processed_input = rspamd::css::unescape_css(pool, st); + } + else { + /* Lowercase inplace */ + auto *nspace = reinterpret_cast(rspamd_mempool_alloc(pool, st.length())); + auto *p = nspace; + + for (const auto c : st) { + *p++ = g_ascii_tolower(c); + } + + processed_input = std::string_view{nspace, (std::size_t)(p - nspace)}; + } - if (parser.consume_input(st)) { + if (parser.consume_input(processed_input)) { return parser.get_object_maybe(); } diff --git a/src/libserver/css/css_util.cxx b/src/libserver/css/css_util.cxx index 7388e49fd..7add8043c 100644 --- a/src/libserver/css/css_util.cxx +++ b/src/libserver/css/css_util.cxx @@ -23,7 +23,7 @@ namespace rspamd::css { std::string_view unescape_css(rspamd_mempool_t *pool, const std::string_view &sv) { - auto *nspace = reinterpret_cast(rspamd_mempool_alloc(pool, sv.length ())); + auto *nspace = reinterpret_cast(rspamd_mempool_alloc(pool, sv.length())); auto *d = nspace; auto nleft = sv.length (); @@ -38,20 +38,20 @@ std::string_view unescape_css(rspamd_mempool_t *pool, auto escape_offset = 0, i = 0; #define MAYBE_CONSUME_CHAR(c) do { \ - if (c == '"' || c == '\'') { \ + if ((c) == '"' || (c) == '\'') { \ state = quoted; \ - quote_char = c; \ + quote_char = (c); \ nleft--; \ - *d++ = c; \ + *d++ = (c); \ } \ - else if (c == '\\') { \ + else if ((c) == '\\') { \ escape_offset = i; \ state = escape; \ } \ else { \ state = normal; \ nleft--; \ - *d++ = c; \ + *d++ = g_ascii_tolower(c); \ } \ } while (0) @@ -89,14 +89,14 @@ std::string_view unescape_css(rspamd_mempool_t *pool, else { if (val < 0x80) { /* Trivial case: ascii character */ - *d++ = (unsigned char)val; + *d++ = (unsigned char)g_ascii_tolower(val); nleft --; } else { UChar32 uc = val; auto off = 0; UTF8_APPEND_CHAR_SAFE((uint8_t *) d, off, - sv.length (), uc); + sv.length (), u_tolower(uc)); d += off; nleft -= off; } -- cgit v1.2.3