parent
0fefab2cdd
commit
513bac81b5
@ -26,36 +26,21 @@ function __vc_Canvas_set_data(self, data)
|
|||||||
self:_set_data(tostring(_ffi.cast("uintptr_t", canvas_ffi_buffer)))
|
self:_set_data(tostring(_ffi.cast("uintptr_t", canvas_ffi_buffer)))
|
||||||
end
|
end
|
||||||
|
|
||||||
_ffi.cdef([[
|
|
||||||
unsigned long crc32(unsigned long crc, const char *buf, unsigned len);
|
|
||||||
]])
|
|
||||||
|
|
||||||
local zlib
|
|
||||||
if _ffi.os == "Windows" then
|
|
||||||
zlib = _ffi.load("zlib1")
|
|
||||||
elseif _ffi.os == "OSX" then
|
|
||||||
zlib = _ffi.load("z")
|
|
||||||
elseif _ffi.os == "Linux" then
|
|
||||||
zlib = _ffi.load("libz.so.1")
|
|
||||||
else
|
|
||||||
error("platform does not support zlib " .. _ffi.os)
|
|
||||||
end
|
|
||||||
|
|
||||||
function crc32(bytes, chksum)
|
function crc32(bytes, chksum)
|
||||||
local chksum = chksum or 0
|
local chksum = chksum or 0
|
||||||
|
|
||||||
local length = #bytes
|
local length = #bytes
|
||||||
if type(bytes) == "string" then
|
if type(bytes) == "table" then
|
||||||
return zlib.crc32(chksum, bytes, length)
|
local buffer_len = _ffi.new('int[1]', length)
|
||||||
elseif type(bytes) == "table" then
|
|
||||||
local buffer = _ffi.new(
|
local buffer = _ffi.new(
|
||||||
string.format("char[%s]", length)
|
string.format("char[%s]", length)
|
||||||
)
|
)
|
||||||
for i=1, length do
|
for i=1, length do
|
||||||
buffer[i - 1] = bytes[i]
|
buffer[i - 1] = bytes[i]
|
||||||
end
|
end
|
||||||
return zlib.crc32(chksum, buffer, length)
|
bytes = _ffi.string(buffer, buffer_len[0])
|
||||||
end
|
end
|
||||||
|
return _crc32(bytes, chksum)
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Check if given table is an array
|
-- Check if given table is an array
|
||||||
|
|||||||
@ -56,6 +56,7 @@ extern const luaL_Reg transformlib[];
|
|||||||
|
|
||||||
// Lua Overrides
|
// Lua Overrides
|
||||||
extern int l_print(lua::State* L);
|
extern int l_print(lua::State* L);
|
||||||
|
extern int l_crc32(lua::State* L);
|
||||||
|
|
||||||
namespace lua {
|
namespace lua {
|
||||||
inline uint check_argc(lua::State* L, int a) {
|
inline uint check_argc(lua::State* L, int a) {
|
||||||
|
|||||||
@ -84,6 +84,7 @@ static void create_libs(State* L, StateType stateType) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
addfunc(L, "print", lua::wrap<l_print>);
|
addfunc(L, "print", lua::wrap<l_print>);
|
||||||
|
addfunc(L, "_crc32", lua::wrap<l_crc32>);
|
||||||
}
|
}
|
||||||
|
|
||||||
void lua::init_state(State* L, StateType stateType) {
|
void lua::init_state(State* L, StateType stateType) {
|
||||||
|
|||||||
@ -1,4 +1,5 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <zlib.h>
|
||||||
|
|
||||||
#include "libs/api_lua.hpp"
|
#include "libs/api_lua.hpp"
|
||||||
|
|
||||||
@ -25,3 +26,27 @@ int l_print(lua::State* L) {
|
|||||||
*output_stream << std::endl;
|
*output_stream << std::endl;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int l_crc32(lua::State* L) {
|
||||||
|
auto value = lua::tointeger(L, 2);
|
||||||
|
if (lua::isstring(L, 1)) {
|
||||||
|
auto string = lua::tolstring(L, 1);
|
||||||
|
return lua::pushinteger(
|
||||||
|
L,
|
||||||
|
crc32(
|
||||||
|
value,
|
||||||
|
reinterpret_cast<const ubyte*>(string.data()),
|
||||||
|
string.length()
|
||||||
|
)
|
||||||
|
);
|
||||||
|
} else if (auto bytearray = lua::touserdata<lua::LuaBytearray>(L, 1)) {
|
||||||
|
auto& bytes = bytearray->data();
|
||||||
|
return lua::pushinteger(L, crc32(value, bytes.data(), bytes.size()));
|
||||||
|
} else if (lua::istable(L, 1)) {
|
||||||
|
std::vector<ubyte> bytes;
|
||||||
|
lua::read_bytes_from_table(L, 1, bytes);
|
||||||
|
return lua::pushinteger(L, crc32(value, bytes.data(), bytes.size()));
|
||||||
|
} else {
|
||||||
|
throw std::runtime_error("invalid argument 1");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user