commit b916a6f: [Minor] Lua_util: Add qp decode binding

Vsevolod Stakhov vsevolod at highsecure.ru
Mon Jul 22 14:49:08 UTC 2019


Author: Vsevolod Stakhov
Date: 2019-07-22 15:45:26 +0100
URL: https://github.com/rspamd/rspamd/commit/b916a6f9bf99157cc7eb0224e17ce2e9dc3d1632

[Minor] Lua_util: Add qp decode binding

---
 src/lua/lua_util.c | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 56 insertions(+)

diff --git a/src/lua/lua_util.c b/src/lua/lua_util.c
index 927ada2d4..be795bc55 100644
--- a/src/lua/lua_util.c
+++ b/src/lua/lua_util.c
@@ -71,6 +71,15 @@ LUA_FUNCTION_DEF (util, encode_base64);
  * @return {rspamd_text} encoded data chunk
  */
 LUA_FUNCTION_DEF (util, encode_qp);
+
+/***
+ * @function util.decode_qp(input)
+ * Decodes data from quouted printable
+ * @param {text or string} input input data
+ * @return {rspamd_text} decoded data chunk
+ */
+LUA_FUNCTION_DEF (util, decode_qp);
+
 /***
  * @function util.decode_base64(input)
  * Decodes data from base64 ignoring whitespace characters
@@ -606,6 +615,7 @@ static const struct luaL_reg utillib_f[] = {
 	LUA_INTERFACE_DEF (util, process_message),
 	LUA_INTERFACE_DEF (util, encode_base64),
 	LUA_INTERFACE_DEF (util, encode_qp),
+	LUA_INTERFACE_DEF (util, decode_qp),
 	LUA_INTERFACE_DEF (util, decode_base64),
 	LUA_INTERFACE_DEF (util, encode_base32),
 	LUA_INTERFACE_DEF (util, decode_base32),
@@ -1031,6 +1041,52 @@ lua_util_encode_qp (lua_State *L)
 	return 1;
 }
 
+static gint
+lua_util_decode_qp (lua_State *L)
+{
+	LUA_TRACE_POINT;
+	struct rspamd_lua_text *t, *out;
+	const gchar *s = NULL;
+	gsize inlen = 0;
+	gssize outlen;
+
+	if (lua_type (L, 1) == LUA_TSTRING) {
+		s = luaL_checklstring (L, 1, &inlen);
+	}
+	else if (lua_type (L, 1) == LUA_TUSERDATA) {
+		t = lua_check_text (L, 1);
+
+		if (t != NULL) {
+			s = t->start;
+			inlen = t->len;
+		}
+	}
+
+	if (s == NULL) {
+		lua_pushnil (L);
+	}
+	else {
+		out = lua_newuserdata (L, sizeof (*t));
+		rspamd_lua_setclass (L, "rspamd{text}", -1);
+		out->start = g_malloc (inlen + 1);
+		out->flags = RSPAMD_TEXT_FLAG_OWN;
+		outlen = rspamd_decode_qp_buf (s, inlen, (gchar *)out->start, inlen + 1);
+
+		if (outlen > 0) {
+			out->len = outlen;
+		}
+		else {
+			/*
+			 * It removes out and frees memory on gc due to RSPAMD_TEXT_FLAG_OWN
+			 */
+			lua_pop (L, 1);
+			lua_pushnil (L);
+		}
+	}
+
+	return 1;
+}
+
 static gint
 lua_util_decode_base64 (lua_State *L)
 {


More information about the Commits mailing list