aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorVsevolod Stakhov <vsevolod@highsecure.ru>2017-04-09 10:46:38 +0100
committerVsevolod Stakhov <vsevolod@highsecure.ru>2017-04-09 10:46:38 +0100
commit61f3e49741fc9fe1e2fed075a704bb0ce5f6b4ba (patch)
tree1f1cac19963b4741438c1eecb6735a06cd2c47d7
parent3aa4586c6e23687b263194bf76e151891c3cf24d (diff)
downloadrspamd-61f3e49741fc9fe1e2fed075a704bb0ce5f6b4ba.tar.gz
rspamd-61f3e49741fc9fe1e2fed075a704bb0ce5f6b4ba.zip
[Minor] Add lua routine to validate utf8
-rw-r--r--src/lua/lua_util.c26
1 files changed, 26 insertions, 0 deletions
diff --git a/src/lua/lua_util.c b/src/lua/lua_util.c
index af24558e4..93e668251 100644
--- a/src/lua/lua_util.c
+++ b/src/lua/lua_util.c
@@ -368,6 +368,13 @@ LUA_FUNCTION_DEF (util, normalize_prob);
LUA_FUNCTION_DEF (util, is_utf_spoofed);
/***
+ * @function util.is_valid_utf8(str)
+ * Returns true if a string is valid UTF8 string
+ * @return {boolean} true if a string is spoofed
+ */
+LUA_FUNCTION_DEF (util, is_valid_utf8);
+
+/***
* @function util.pack(fmt, ...)
*
* Backport of Lua 5.3 `string.pack` function:
@@ -507,6 +514,7 @@ static const struct luaL_reg utillib_f[] = {
LUA_INTERFACE_DEF (util, caseless_hash),
LUA_INTERFACE_DEF (util, caseless_hash_fast),
LUA_INTERFACE_DEF (util, is_utf_spoofed),
+ LUA_INTERFACE_DEF (util, is_valid_utf8),
LUA_INTERFACE_DEF (util, get_hostname),
LUA_INTERFACE_DEF (util, pack),
LUA_INTERFACE_DEF (util, unpack),
@@ -1973,6 +1981,24 @@ lua_util_get_hostname (lua_State *L)
return 1;
}
+static gint
+lua_util_is_valid_utf8 (lua_State *L)
+{
+ const gchar *str;
+ gsize len;
+
+ str = lua_tolstring (L, 1, &len);
+
+ if (str) {
+ lua_pushboolean (L, g_utf8_validate (str, len, NULL));
+ }
+ else {
+ return luaL_error (L, "invalid arguments");
+ }
+
+ return 1;
+}
+
/* Backport from Lua 5.3 */
/******************************************************************************