commit ccea1d0: [Minor] Lua_text: Allow to convert text to bytes

Vsevolod Stakhov vsevolod at highsecure.ru
Mon Sep 9 14:14:04 UTC 2019


Author: Vsevolod Stakhov
Date: 2019-09-09 15:01:12 +0100
URL: https://github.com/rspamd/rspamd/commit/ccea1d01a88980a47104b13371c2cec88f47fb8b

[Minor] Lua_text: Allow to convert text to bytes

---
 src/lua/lua_text.c | 28 ++++++++++++++++++++++++++++
 1 file changed, 28 insertions(+)

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},
@@ -371,6 +378,27 @@ lua_text_at (lua_State *L)
 	return 1;
 }
 
+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)
 {


More information about the Commits mailing list