aboutsummaryrefslogtreecommitdiffstats
path: root/src/lua/lua_util.c
diff options
context:
space:
mode:
authorVsevolod Stakhov <vsevolod@highsecure.ru>2016-01-27 17:37:30 +0000
committerVsevolod Stakhov <vsevolod@highsecure.ru>2016-01-27 17:37:30 +0000
commit01710012952e4839393c5ed1e6b4a04692d0af02 (patch)
treef24bec7cc2e5e1fc993c4c382c7805073870f5a3 /src/lua/lua_util.c
parent5e182370ff95d82a74bbc4c610a0a8f6f145e385 (diff)
downloadrspamd-01710012952e4839393c5ed1e6b4a04692d0af02.tar.gz
rspamd-01710012952e4839393c5ed1e6b4a04692d0af02.zip
Add base32 decode/encode routines to lua util
Diffstat (limited to 'src/lua/lua_util.c')
-rw-r--r--src/lua/lua_util.c94
1 files changed, 94 insertions, 0 deletions
diff --git a/src/lua/lua_util.c b/src/lua/lua_util.c
index cbd54e6cf..15bb90180 100644
--- a/src/lua/lua_util.c
+++ b/src/lua/lua_util.c
@@ -70,6 +70,22 @@ LUA_FUNCTION_DEF (util, encode_base64);
* @return {rspamd_text} decoded data chunk
*/
LUA_FUNCTION_DEF (util, decode_base64);
+
+/***
+ * @function util.encode_base32(input)
+ * Encodes data in base32 breaking lines if needed
+ * @param {text or string} input input data
+ * @return {rspamd_text} encoded data chunk
+ */
+LUA_FUNCTION_DEF (util, encode_base32);
+/***
+ * @function util.decode_base32(input)
+ * Decodes data from base32 ignoring whitespace characters
+ * @param {text or string} input data to decode
+ * @return {rspamd_text} decoded data chunk
+ */
+LUA_FUNCTION_DEF (util, decode_base32);
+
/***
* @function util.tokenize_text(input[, exceptions])
* Create tokens from a text using optional exceptions list
@@ -183,6 +199,8 @@ static const struct luaL_reg utillib_f[] = {
LUA_INTERFACE_DEF (util, process_message),
LUA_INTERFACE_DEF (util, encode_base64),
LUA_INTERFACE_DEF (util, decode_base64),
+ LUA_INTERFACE_DEF (util, encode_base32),
+ LUA_INTERFACE_DEF (util, decode_base32),
LUA_INTERFACE_DEF (util, tokenize_text),
LUA_INTERFACE_DEF (util, tanh),
LUA_INTERFACE_DEF (util, parse_html),
@@ -442,6 +460,82 @@ lua_util_decode_base64 (lua_State *L)
}
static gint
+lua_util_encode_base32 (lua_State *L)
+{
+ struct rspamd_lua_text *t;
+ const gchar *s = NULL;
+ gchar *out;
+ gsize inlen, 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 = rspamd_encode_base32 (s, inlen);
+
+ if (out != NULL) {
+ t = lua_newuserdata (L, sizeof (*t));
+ outlen = strlen (out);
+ rspamd_lua_setclass (L, "rspamd{text}", -1);
+ t->start = out;
+ t->len = outlen;
+ /* Need destruction */
+ t->own = TRUE;
+ }
+ else {
+ lua_pushnil (L);
+ }
+ }
+
+ return 1;
+}
+
+static gint
+lua_util_decode_base32 (lua_State *L)
+{
+ struct rspamd_lua_text *t;
+ const gchar *s = NULL;
+ gsize inlen, 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) {
+ t = lua_newuserdata (L, sizeof (*t));
+ rspamd_lua_setclass (L, "rspamd{text}", -1);
+ t->start = rspamd_decode_base32 (s, inlen, &outlen);
+ t->len = outlen;
+ t->own = TRUE;
+ }
+ else {
+ lua_pushnil (L);
+ }
+
+ return 1;
+}
+
+static gint
lua_util_tokenize_text (lua_State *L)
{
const gchar *in = NULL;