add crc32 function
This commit is contained in:
parent
104cc11645
commit
13c309667b
@ -26,6 +26,38 @@ 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)
|
||||||
|
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
|
-- Check if given table is an array
|
||||||
function is_array(x)
|
function is_array(x)
|
||||||
if #x > 0 then
|
if #x > 0 then
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user