commit d327bb2: [Minor] Lua_text: Add randombytes constructor

Vsevolod Stakhov vsevolod at highsecure.ru
Mon May 4 19:42:06 UTC 2020


Author: Vsevolod Stakhov
Date: 2020-05-04 20:27:56 +0100
URL: https://github.com/rspamd/rspamd/commit/d327bb26a373615b55faf6fe94aeb7154bba05ab

[Minor] Lua_text: Add randombytes constructor

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

diff --git a/src/lua/lua_text.c b/src/lua/lua_text.c
index 8ca30bbc0..1550fb239 100644
--- a/src/lua/lua_text.c
+++ b/src/lua/lua_text.c
@@ -42,6 +42,13 @@ LUA_FUNCTION_DEF (text, fromstring);
  * @return {rspamd_text} resulting text
  */
 LUA_FUNCTION_DEF (text, null);
+/***
+ * @function rspamd_text.randombytes(nbytes)
+ * Creates rspamd_text with random bytes inside (raw bytes)
+ * @param {number} nbytes number of random bytes generated
+ * @return {rspamd_text} random bytes text
+ */
+LUA_FUNCTION_DEF (text, randombytes);
 
 /***
  * @function rspamd_text.fromtable(tbl[, delim])
@@ -176,6 +183,7 @@ static const struct luaL_reg textlib_f[] = {
 		LUA_INTERFACE_DEF (text, fromtable),
 		{"from_table", lua_text_fromtable},
 		LUA_INTERFACE_DEF (text, null),
+		LUA_INTERFACE_DEF (text, randombytes),
 		{NULL, NULL}
 };
 
@@ -282,6 +290,20 @@ lua_text_null (lua_State *L)
 	return 1;
 }
 
+static gint
+lua_text_randombytes (lua_State *L)
+{
+	LUA_TRACE_POINT;
+	guint nbytes = luaL_checkinteger (L, 1);
+	struct rspamd_lua_text *out;
+
+	out = lua_new_text (L, NULL, nbytes, TRUE);
+	randombytes_buf ((char *)out->start, nbytes);
+	out->len = nbytes;
+
+	return 1;
+}
+
 #define MAX_REC 10
 
 static void


More information about the Commits mailing list