Skip to content

Commit 62d15ac

Browse files
red-001kwolekr
authored andcommittedMay 28, 2016
Add base64 encoding and decoding to the lua api. (#3919)
1 parent c4e083f commit 62d15ac

File tree

4 files changed

+49
-0
lines changed

4 files changed

+49
-0
lines changed
 

‎doc/lua_api.txt

+4
Original file line numberDiff line numberDiff line change
@@ -2438,6 +2438,10 @@ These functions return the leftover itemstack.
24382438
* See documentation on `minetest.compress()` for supported compression methods.
24392439
* currently supported.
24402440
* `...` indicates method-specific arguments. Currently, no methods use this.
2441+
* `minetest.encode_base64(string)`: returns string encoded in base64
2442+
* Encodes a string in base64.
2443+
* `minetest.decode_base64(string)`: returns string
2444+
* Decodes a string encoded in base64.
24412445
* `minetest.is_protected(pos, name)`: returns boolean
24422446
* Returns true, if player `name` shouldn't be abled to dig at `pos` or do other
24432447
actions, defineable by mods, due to some mod-defined ownership-like concept.

‎doc/menu_lua_api.txt

+4
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,10 @@ string:trim()
210210
^ eg. string.trim("\n \t\tfoo bar\t ") == "foo bar"
211211
core.is_yes(arg) (possible in async calls)
212212
^ returns whether arg can be interpreted as yes
213+
minetest.encode_base64(string) (possible in async calls)
214+
^ Encodes a string in base64.
215+
minetest.decode_base64(string) (possible in async calls)
216+
^ Decodes a string encoded in base64.
213217

214218
Version compat:
215219
core.get_min_supp_proto()

‎src/script/lua_api/l_util.cpp

+35
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
3232
#include "filesys.h"
3333
#include "settings.h"
3434
#include "util/auth.h"
35+
#include "util/base64.h"
3536
#include <algorithm>
3637

3738
// log([level,] text)
@@ -320,6 +321,34 @@ int ModApiUtil::l_decompress(lua_State *L)
320321
return 1;
321322
}
322323

324+
// encode_base64(string)
325+
int ModApiUtil::l_encode_base64(lua_State *L)
326+
{
327+
NO_MAP_LOCK_REQUIRED;
328+
329+
size_t size;
330+
const char *data = luaL_checklstring(L, 1, &size);
331+
332+
std::string out = base64_encode((const unsigned char *)(data), size);
333+
334+
lua_pushlstring(L, out.data(), out.size());
335+
return 1;
336+
}
337+
338+
// decode_base64(string)
339+
int ModApiUtil::l_decode_base64(lua_State *L)
340+
{
341+
NO_MAP_LOCK_REQUIRED;
342+
343+
size_t size;
344+
const char *data = luaL_checklstring(L, 1, &size);
345+
346+
std::string out = base64_decode(std::string(data, size));
347+
348+
lua_pushlstring(L, out.data(), out.size());
349+
return 1;
350+
}
351+
323352
// mkdir(path)
324353
int ModApiUtil::l_mkdir(lua_State *L)
325354
{
@@ -433,6 +462,9 @@ void ModApiUtil::Initialize(lua_State *L, int top)
433462
API_FCT(get_dir_list);
434463

435464
API_FCT(request_insecure_environment);
465+
466+
API_FCT(encode_base64);
467+
API_FCT(decode_base64);
436468
}
437469

438470
void ModApiUtil::InitializeAsync(AsyncEngine& engine)
@@ -459,5 +491,8 @@ void ModApiUtil::InitializeAsync(AsyncEngine& engine)
459491

460492
ASYNC_API_FCT(mkdir);
461493
ASYNC_API_FCT(get_dir_list);
494+
495+
ASYNC_API_FCT(encode_base64);
496+
ASYNC_API_FCT(decode_base64);
462497
}
463498

‎src/script/lua_api/l_util.h

+6
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,12 @@ class ModApiUtil : public ModApiBase {
9595
// request_insecure_environment()
9696
static int l_request_insecure_environment(lua_State *L);
9797

98+
// encode_base64(string)
99+
static int l_encode_base64(lua_State *L);
100+
101+
// decode_base64(string)
102+
static int l_decode_base64(lua_State *L);
103+
98104
public:
99105
static void Initialize(lua_State *L, int top);
100106

1 commit comments

Comments
 (1)

HybridDog commented on Jul 18, 2016

@HybridDog
Contributor

@red-001 Since complex Unicode text can't always be put into clipboard, can this be used to send custom player skins to the server (/send_skin <ctrl+v>)?

Please sign in to comment.