Merge pull request #485 from MihailRis/crc32

crc32
This commit is contained in:
MihailRis 2025-03-17 02:42:21 +03:00 committed by GitHub
commit e1ee1d8a31
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 38 additions and 1 deletions

View File

@ -26,6 +26,38 @@ function __vc_Canvas_set_data(self, data)
self:_set_data(tostring(_ffi.cast("uintptr_t", canvas_ffi_buffer)))
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)
local chksum = chksum or 0
local length = #bytes
if type(bytes) == "string" then
return zlib.crc32(chksum, bytes, length)
elseif type(bytes) == "table" then
local buffer = _ffi.new(
string.format("char[%s]", length)
)
for i=1, length do
buffer[i - 1] = bytes[i]
end
return zlib.crc32(chksum, buffer, length)
end
end
-- Check if given table is an array
function is_array(x)
if #x > 0 then

View File

@ -30,7 +30,7 @@ static int l_resolve(lua::State* L) {
static int l_read(lua::State* L) {
io::path path = lua::require_string(L, 1);
if (io::is_regular_file(path)) {
return lua::pushstring(L, io::read_string(path));
return lua::pushlstring(L, io::read_string(path));
}
throw std::runtime_error(
"file does not exists " + util::quote(path.string())

View File

@ -184,6 +184,11 @@ namespace lua {
return 1;
}
inline int pushlstring(lua::State* L, std::string_view view) {
lua_pushlstring(L, reinterpret_cast<const char*>(view.data()), view.size());
return 1;
}
template <typename... Args>
inline int pushfstring(lua_State* L, const char* fmt, Args... args) {
lua_pushfstring(L, fmt, args...);