add base64 library
This commit is contained in:
@@ -15,6 +15,7 @@
|
|||||||
|
|
||||||
// Libraries
|
// Libraries
|
||||||
extern const luaL_Reg audiolib[];
|
extern const luaL_Reg audiolib[];
|
||||||
|
extern const luaL_Reg base64lib[];
|
||||||
extern const luaL_Reg bjsonlib[];
|
extern const luaL_Reg bjsonlib[];
|
||||||
extern const luaL_Reg blocklib[];
|
extern const luaL_Reg blocklib[];
|
||||||
extern const luaL_Reg cameralib[];
|
extern const luaL_Reg cameralib[];
|
||||||
|
|||||||
@@ -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}
|
||||||
|
};
|
||||||
@@ -39,6 +39,7 @@ static void remove_lib_funcs(
|
|||||||
}
|
}
|
||||||
|
|
||||||
static void create_libs(State* L, StateType stateType) {
|
static void create_libs(State* L, StateType stateType) {
|
||||||
|
openlib(L, "base64", base64lib);
|
||||||
openlib(L, "bjson", bjsonlib);
|
openlib(L, "bjson", bjsonlib);
|
||||||
openlib(L, "block", blocklib);
|
openlib(L, "block", blocklib);
|
||||||
openlib(L, "core", corelib);
|
openlib(L, "core", corelib);
|
||||||
|
|||||||
@@ -240,6 +240,11 @@ namespace lua {
|
|||||||
inline const char* tostring(lua::State* L, int idx) {
|
inline const char* tostring(lua::State* L, int idx) {
|
||||||
return lua_tostring(L, 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) {
|
inline const void* topointer(lua::State* L, int idx) {
|
||||||
return lua_topointer(L, idx);
|
return lua_topointer(L, idx);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -363,8 +363,8 @@ util::Buffer<ubyte> util::base64_decode(const char* str, size_t size) {
|
|||||||
return bytes;
|
return bytes;
|
||||||
}
|
}
|
||||||
|
|
||||||
util::Buffer<ubyte> util::base64_decode(const std::string& str) {
|
util::Buffer<ubyte> util::base64_decode(std::string_view str) {
|
||||||
return base64_decode(str.c_str(), str.size());
|
return base64_decode(str.data(), str.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
int util::replaceAll(
|
int util::replaceAll(
|
||||||
|
|||||||
@@ -63,7 +63,7 @@ namespace util {
|
|||||||
|
|
||||||
std::string base64_encode(const ubyte* data, size_t size);
|
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 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);
|
std::string tohex(uint64_t value);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user