From 61f3e49741fc9fe1e2fed075a704bb0ce5f6b4ba Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Sun, 9 Apr 2017 10:46:38 +0100 Subject: [PATCH] [Minor] Add lua routine to validate utf8 --- src/lua/lua_util.c | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) 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 @@ -367,6 +367,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, ...) * @@ -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 */ /****************************************************************************** -- 2.39.5