From d490fa23fae552641a257159ae7a77a5f8b9ba78 Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Wed, 20 Sep 2023 16:17:09 +0100 Subject: [PATCH] [Minor] Modernize lua_util_encode_base64 --- src/lua/lua_util.c | 37 ++++++++++--------------------------- 1 file changed, 10 insertions(+), 27 deletions(-) diff --git a/src/lua/lua_util.c b/src/lua/lua_util.c index 40f6f6c48..152c02d1e 100644 --- a/src/lua/lua_util.c +++ b/src/lua/lua_util.c @@ -984,37 +984,25 @@ lua_util_encode_base64(lua_State *L) { LUA_TRACE_POINT; struct rspamd_lua_text *t; - const gchar *s = NULL; gchar *out; - gsize inlen, outlen; - guint str_lim = 0; + gsize outlen; + long str_lim = 0; gboolean fold = FALSE; - 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; - } - } + t = lua_check_text_or_string(L, 1); if (lua_gettop(L) > 1) { - str_lim = luaL_checknumber(L, 2); - - fold = !!(str_lim > 0); + str_lim = luaL_checkinteger(L, 2); + fold = str_lim > 0; } - if (s == NULL) { - lua_pushnil(L); + if (t == NULL) { + return luaL_error(L, "invalid arguments"); } else { if (fold) { - out = rspamd_encode_base64(s, inlen, str_lim, &outlen); + out = rspamd_encode_base64(t->start, t->len, str_lim, &outlen); } else { enum rspamd_newlines_type how = RSPAMD_TASK_NEWLINES_CRLF; @@ -1033,16 +1021,11 @@ lua_util_encode_base64(lua_State *L) } } - out = rspamd_encode_base64_fold(s, inlen, str_lim, &outlen, how); + out = rspamd_encode_base64_fold(t->start, t->len, str_lim, &outlen, how); } if (out != NULL) { - t = lua_newuserdata(L, sizeof(*t)); - rspamd_lua_setclass(L, "rspamd{text}", -1); - t->start = out; - t->len = outlen; - /* Need destruction */ - t->flags = RSPAMD_TEXT_FLAG_OWN; + lua_new_text(L, out, outlen, TRUE); } else { lua_pushnil(L); -- 2.39.5