summaryrefslogtreecommitdiffstats
path: root/src/lua
diff options
context:
space:
mode:
authorVsevolod Stakhov <vsevolod@highsecure.ru>2019-07-22 15:45:26 +0100
committerVsevolod Stakhov <vsevolod@highsecure.ru>2019-07-22 15:45:26 +0100
commitb916a6f9bf99157cc7eb0224e17ce2e9dc3d1632 (patch)
treed9792ed07cc636e3b8e77af0862dfeeb065e323e /src/lua
parent94472423c0877566ced5f996412376db0601abfd (diff)
downloadrspamd-b916a6f9bf99157cc7eb0224e17ce2e9dc3d1632.tar.gz
rspamd-b916a6f9bf99157cc7eb0224e17ce2e9dc3d1632.zip
[Minor] Lua_util: Add qp decode binding
Diffstat (limited to 'src/lua')
-rw-r--r--src/lua/lua_util.c56
1 files changed, 56 insertions, 0 deletions
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),
@@ -1032,6 +1042,52 @@ lua_util_encode_qp (lua_State *L)
}
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)
{
LUA_TRACE_POINT;