From: Vsevolod Stakhov Date: Fri, 1 Jul 2022 20:14:52 +0000 (+0100) Subject: [Minor] Implement binary string checks X-Git-Tag: 3.3~156 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=eb28cc845c2e1063e6b1111519164e12ce2bed4e;p=rspamd.git [Minor] Implement binary string checks Issue: #4204 --- diff --git a/src/lua/lua_common.h b/src/lua/lua_common.h index a1f62cdb6..c961d37dd 100644 --- a/src/lua/lua_common.h +++ b/src/lua/lua_common.h @@ -112,6 +112,7 @@ struct rspamd_lua_ip { #define RSPAMD_TEXT_FLAG_WIPE (1u << 2u) #define RSPAMD_TEXT_FLAG_SYSMALLOC (1u << 3u) #define RSPAMD_TEXT_FLAG_FAKE (1u << 4u) +#define RSPAMD_TEXT_FLAG_BINARY (1u << 5u) struct rspamd_lua_text { const gchar *start; guint len; @@ -276,6 +277,12 @@ struct rspamd_lua_text *lua_check_text_or_string (lua_State *L, gint pos); /* Creates and *pushes* new rspamd text, data is copied if RSPAMD_TEXT_FLAG_OWN is in flags*/ struct rspamd_lua_text *lua_new_text (lua_State *L, const gchar *start, gsize len, gboolean own); +/** + * Checks if a text has binary characters (non ascii and non-utf8 characters) + * @param t + * @return + */ +bool lua_is_text_binary(struct rspamd_lua_text *t); struct rspamd_lua_regexp *lua_check_regexp (lua_State *L, gint pos); diff --git a/src/lua/lua_text.c b/src/lua/lua_text.c index 19317bf59..04a416942 100644 --- a/src/lua/lua_text.c +++ b/src/lua/lua_text.c @@ -348,6 +348,23 @@ lua_new_text (lua_State *L, const gchar *start, gsize len, gboolean own) return t; } +bool +lua_is_text_binary(struct rspamd_lua_text *t) +{ + if (t == NULL || t->len == 0) { + return false; + } + + if (rspamd_str_has_8bit(t->start, t->len)) { + if (rspamd_fast_utf8_validate(t->start, t->len) == 0) { + return true; + } + return false; + } + + return true; +} + static gint lua_text_fromstring (lua_State *L)