diff options
author | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2020-04-10 11:38:39 +0100 |
---|---|---|
committer | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2020-04-10 11:44:46 +0100 |
commit | f8072344f9fb3fbc1ac93122f6cad11204c8cbf2 (patch) | |
tree | 29a3c5b33cdaf77138d7f753a97b04d6102c84c0 /src/lua/lua_util.c | |
parent | baecba4ce93e91b553e2d5d0d8e779dcd05e1d43 (diff) | |
download | rspamd-f8072344f9fb3fbc1ac93122f6cad11204c8cbf2.tar.gz rspamd-f8072344f9fb3fbc1ac93122f6cad11204c8cbf2.zip |
[Feature] Allow multiple base32 encodings in Lua API
Diffstat (limited to 'src/lua/lua_util.c')
-rw-r--r-- | src/lua/lua_util.c | 30 |
1 files changed, 25 insertions, 5 deletions
diff --git a/src/lua/lua_util.c b/src/lua/lua_util.c index 053cce864..a378c1ace 100644 --- a/src/lua/lua_util.c +++ b/src/lua/lua_util.c @@ -94,16 +94,18 @@ LUA_FUNCTION_DEF (util, decode_qp); LUA_FUNCTION_DEF (util, decode_base64); /*** - * @function util.encode_base32(input) + * @function util.encode_base32(input, [b32type = 'default']) * Encodes data in base32 breaking lines if needed * @param {text or string} input input data + * @param {string} b32type base32 type (default, bleach, rfc) * @return {rspamd_text} encoded data chunk */ LUA_FUNCTION_DEF (util, encode_base32); /*** - * @function util.decode_base32(input) + * @function util.decode_base32(input, [b32type = 'default']) * Decodes data from base32 ignoring whitespace characters * @param {text or string} input data to decode + * @param {string} b32type base32 type (default, bleach, rfc) * @return {rspamd_text} decoded data chunk */ LUA_FUNCTION_DEF (util, decode_base32); @@ -1157,6 +1159,7 @@ lua_util_encode_base32 (lua_State *L) struct rspamd_lua_text *t; const gchar *s = NULL; gchar *out; + enum rspamd_base32_type btype = RSPAMD_BASE32_DEFAULT; gsize inlen, outlen; if (lua_type (L, 1) == LUA_TSTRING) { @@ -1171,11 +1174,19 @@ lua_util_encode_base32 (lua_State *L) } } + if (lua_type (L, 2) == LUA_TSTRING) { + btype = rspamd_base32_decode_type_from_str (lua_tostring (L, 2)); + + if (btype == RSPAMD_BASE32_INVALID) { + return luaL_error (L, "invalid b32 type: %s", lua_tostring (L, 2)); + } + } + if (s == NULL) { - lua_pushnil (L); + return luaL_error (L, "invalid arguments"); } else { - out = rspamd_encode_base32 (s, inlen, RSPAMD_BASE32_DEFAULT); + out = rspamd_encode_base32 (s, inlen, btype); if (out != NULL) { t = lua_newuserdata (L, sizeof (*t)); @@ -1201,6 +1212,7 @@ lua_util_decode_base32 (lua_State *L) struct rspamd_lua_text *t; const gchar *s = NULL; gsize inlen, outlen; + enum rspamd_base32_type btype = RSPAMD_BASE32_DEFAULT; if (lua_type (L, 1) == LUA_TSTRING) { s = luaL_checklstring (L, 1, &inlen); @@ -1214,10 +1226,18 @@ lua_util_decode_base32 (lua_State *L) } } + if (lua_type (L, 2) == LUA_TSTRING) { + btype = rspamd_base32_decode_type_from_str (lua_tostring (L, 2)); + + if (btype == RSPAMD_BASE32_INVALID) { + return luaL_error (L, "invalid b32 type: %s", lua_tostring (L, 2)); + } + } + if (s != NULL) { t = lua_newuserdata (L, sizeof (*t)); rspamd_lua_setclass (L, "rspamd{text}", -1); - t->start = rspamd_decode_base32 (s, inlen, &outlen, RSPAMD_BASE32_DEFAULT); + t->start = rspamd_decode_base32 (s, inlen, &outlen, btype); t->len = outlen; t->flags = RSPAMD_TEXT_FLAG_OWN; } |