diff options
author | red-001 <red-001@openmailbox.org> | 2016-05-28 04:37:28 +0100 |
---|---|---|
committer | kwolekr <kwolekr@minetest.net> | 2016-05-27 23:37:28 -0400 |
commit | 62d15ac7c1fcd7214a9e45d46bbc560f998edb95 (patch) | |
tree | fb2a14588d526ea24d45f5e774cddd327af4a6e8 /src/script/lua_api | |
parent | c4e083f7e142d8e25e906bb19f9b1f03bb7b430d (diff) | |
download | minetest-62d15ac7c1fcd7214a9e45d46bbc560f998edb95.tar.gz minetest-62d15ac7c1fcd7214a9e45d46bbc560f998edb95.tar.bz2 minetest-62d15ac7c1fcd7214a9e45d46bbc560f998edb95.zip |
Add base64 encoding and decoding to the lua api. (#3919)
Diffstat (limited to 'src/script/lua_api')
-rw-r--r-- | src/script/lua_api/l_util.cpp | 35 | ||||
-rw-r--r-- | src/script/lua_api/l_util.h | 6 |
2 files changed, 41 insertions, 0 deletions
diff --git a/src/script/lua_api/l_util.cpp b/src/script/lua_api/l_util.cpp index c3e6c8964..e90b7fbcf 100644 --- a/src/script/lua_api/l_util.cpp +++ b/src/script/lua_api/l_util.cpp @@ -32,6 +32,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "filesys.h" #include "settings.h" #include "util/auth.h" +#include "util/base64.h" #include <algorithm> // log([level,] text) @@ -320,6 +321,34 @@ int ModApiUtil::l_decompress(lua_State *L) return 1; } +// encode_base64(string) +int ModApiUtil::l_encode_base64(lua_State *L) +{ + NO_MAP_LOCK_REQUIRED; + + size_t size; + const char *data = luaL_checklstring(L, 1, &size); + + std::string out = base64_encode((const unsigned char *)(data), size); + + lua_pushlstring(L, out.data(), out.size()); + return 1; +} + +// decode_base64(string) +int ModApiUtil::l_decode_base64(lua_State *L) +{ + NO_MAP_LOCK_REQUIRED; + + size_t size; + const char *data = luaL_checklstring(L, 1, &size); + + std::string out = base64_decode(std::string(data, size)); + + lua_pushlstring(L, out.data(), out.size()); + return 1; +} + // mkdir(path) int ModApiUtil::l_mkdir(lua_State *L) { @@ -433,6 +462,9 @@ void ModApiUtil::Initialize(lua_State *L, int top) API_FCT(get_dir_list); API_FCT(request_insecure_environment); + + API_FCT(encode_base64); + API_FCT(decode_base64); } void ModApiUtil::InitializeAsync(AsyncEngine& engine) @@ -459,5 +491,8 @@ void ModApiUtil::InitializeAsync(AsyncEngine& engine) ASYNC_API_FCT(mkdir); ASYNC_API_FCT(get_dir_list); + + ASYNC_API_FCT(encode_base64); + ASYNC_API_FCT(decode_base64); } diff --git a/src/script/lua_api/l_util.h b/src/script/lua_api/l_util.h index 6fac7e7eb..779dbe281 100644 --- a/src/script/lua_api/l_util.h +++ b/src/script/lua_api/l_util.h @@ -95,6 +95,12 @@ private: // request_insecure_environment() static int l_request_insecure_environment(lua_State *L); + // encode_base64(string) + static int l_encode_base64(lua_State *L); + + // decode_base64(string) + static int l_decode_base64(lua_State *L); + public: static void Initialize(lua_State *L, int top); |