add base64 library

This commit is contained in:
MihailRis 2024-11-18 11:52:06 +03:00
parent 712c0756b8
commit 0bfc5d2ad4
6 changed files with 62 additions and 3 deletions

View File

@ -15,6 +15,7 @@
// Libraries
extern const luaL_Reg audiolib[];
extern const luaL_Reg base64lib[];
extern const luaL_Reg bjsonlib[];
extern const luaL_Reg blocklib[];
extern const luaL_Reg cameralib[];

View File

@ -0,0 +1,52 @@
#include "api_lua.hpp"
#include "util/stringutil.hpp"
static int l_encode(lua::State* L) {
if (lua::istable(L, 1)) {
lua::pushvalue(L, 1);
size_t size = lua::objlen(L, 1);
util::Buffer<char> buffer(size);
for (size_t i = 0; i < size; i++) {
lua::rawgeti(L, i + 1);
buffer[i] = lua::tointeger(L, -1);
lua::pop(L);
}
lua::pop(L);
return lua::pushstring(L, util::base64_encode(
reinterpret_cast<const ubyte*>(buffer.data()), buffer.size()
));
} else if (auto bytes = lua::touserdata<lua::LuaBytearray>(L, 1)) {
return lua::pushstring(
L,
util::base64_encode(
bytes->data().data(),
bytes->data().size()
)
);
}
throw std::runtime_error("array or ByteArray expected");
}
static int l_decode(lua::State* L) {
auto buffer = util::base64_decode(lua::require_lstring(L, 1));
if (lua::toboolean(L, 2)) {
lua::createtable(L, buffer.size(), 0);
for (size_t i = 0; i < buffer.size(); i++) {
lua::pushinteger(L, buffer[i] & 0xFF);
lua::rawseti(L, i+1);
}
} else {
lua::newuserdata<lua::LuaBytearray>(L, buffer.size());
auto bytearray = lua::touserdata<lua::LuaBytearray>(L, -1);
bytearray->data().reserve(buffer.size());
std::memcpy(bytearray->data().data(), buffer.data(), buffer.size());
}
return 1;
}
const luaL_Reg base64lib[] = {
{"encode", lua::wrap<l_encode>},
{"decode", lua::wrap<l_decode>},
{NULL, NULL}
};

View File

@ -39,6 +39,7 @@ static void remove_lib_funcs(
}
static void create_libs(State* L, StateType stateType) {
openlib(L, "base64", base64lib);
openlib(L, "bjson", bjsonlib);
openlib(L, "block", blocklib);
openlib(L, "core", corelib);

View File

@ -240,6 +240,11 @@ namespace lua {
inline const char* tostring(lua::State* L, int idx) {
return lua_tostring(L, idx);
}
inline std::string_view tolstring(lua::State* L, int idx) {
size_t len = 0;
auto string = lua_tolstring(L, idx, &len);
return std::string_view(string, len);
}
inline const void* topointer(lua::State* L, int idx) {
return lua_topointer(L, idx);
}

View File

@ -363,8 +363,8 @@ util::Buffer<ubyte> util::base64_decode(const char* str, size_t size) {
return bytes;
}
util::Buffer<ubyte> util::base64_decode(const std::string& str) {
return base64_decode(str.c_str(), str.size());
util::Buffer<ubyte> util::base64_decode(std::string_view str) {
return base64_decode(str.data(), str.size());
}
int util::replaceAll(

View File

@ -63,7 +63,7 @@ namespace util {
std::string base64_encode(const ubyte* data, size_t size);
util::Buffer<ubyte> base64_decode(const char* str, size_t size);
util::Buffer<ubyte> base64_decode(const std::string& str);
util::Buffer<ubyte> base64_decode(std::string_view str);
std::string tohex(uint64_t value);