diff options
author | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2019-09-09 15:01:12 +0100 |
---|---|---|
committer | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2019-09-09 15:01:12 +0100 |
commit | ccea1d01a88980a47104b13371c2cec88f47fb8b (patch) | |
tree | 28ae2948041271ec0b0685bc02a90f97f6d78928 /src/lua | |
parent | 6d97c8f9bfe5ddf16b53c9b8f8d11258c0af8f36 (diff) | |
download | rspamd-ccea1d01a88980a47104b13371c2cec88f47fb8b.tar.gz rspamd-ccea1d01a88980a47104b13371c2cec88f47fb8b.zip |
[Minor] Lua_text: Allow to convert text to bytes
Diffstat (limited to 'src/lua')
-rw-r--r-- | src/lua/lua_text.c | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/src/lua/lua_text.c b/src/lua/lua_text.c index 68897019d..63b1b22a8 100644 --- a/src/lua/lua_text.c +++ b/src/lua/lua_text.c @@ -81,6 +81,12 @@ LUA_FUNCTION_DEF (text, span); * @return {integer} byte at the position `pos` or nil if pos out of bound */ LUA_FUNCTION_DEF (text, at); +/*** + * @method rspamd_text:bytes() + * Converts text to an array of bytes + * @return {table|integer} bytes in the array (as unsigned char) + */ +LUA_FUNCTION_DEF (text, bytes); LUA_FUNCTION_DEF (text, take_ownership); LUA_FUNCTION_DEF (text, gc); LUA_FUNCTION_DEF (text, eq); @@ -99,6 +105,7 @@ static const struct luaL_reg textlib_m[] = { LUA_INTERFACE_DEF (text, save_in_file), LUA_INTERFACE_DEF (text, span), LUA_INTERFACE_DEF (text, at), + LUA_INTERFACE_DEF (text, bytes), {"write", lua_text_save_in_file}, {"__len", lua_text_len}, {"__tostring", lua_text_str}, @@ -372,6 +379,27 @@ lua_text_at (lua_State *L) } static gint +lua_text_bytes (lua_State *L) +{ + LUA_TRACE_POINT; + struct rspamd_lua_text *t = lua_check_text (L, 1); + + if (t) { + lua_createtable (L, t->len, 0); + + for (gsize i = 0; i < t->len; i ++) { + lua_pushinteger (L, (guchar)t->start[i]); + lua_rawseti (L, -2, i + 1); + } + } + else { + return luaL_error (L, "invalid arguments"); + } + + return 1; +} + +static gint lua_text_save_in_file (lua_State *L) { LUA_TRACE_POINT; |