commit eb28cc8: [Minor] Implement binary string checks

Vsevolod Stakhov vsevolod at rspamd.com
Fri Jul 1 20:35:04 UTC 2022


Author: Vsevolod Stakhov
Date: 2022-07-01 21:14:52 +0100
URL: https://github.com/rspamd/rspamd/commit/eb28cc845c2e1063e6b1111519164e12ce2bed4e

[Minor] Implement binary string checks
Issue: #4204

---
 src/lua/lua_common.h |  7 +++++++
 src/lua/lua_text.c   | 17 +++++++++++++++++
 2 files changed, 24 insertions(+)

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)


More information about the Commits mailing list