forked from koreader/koreader-base
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzlib.lua
33 lines (26 loc) · 835 Bytes
/
zlib.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
--[[--
LuaJIT FFI wrapper for zlib.
@module ffi.zlib
]]
local ffi = require("ffi")
require("ffi/zlib_h")
local libz = ffi.loadlib("z", 1)
local zlib = {}
-- Data compression/decompression of strings via zlib
-- from http://luajit.org/ext_ffi_tutorial.html
function zlib.zlib_compress(data)
local n = libz.compressBound(#data)
local buf = ffi.new("uint8_t[?]", n)
local buflen = ffi.new("unsigned long[1]", n)
local res = libz.compress2(buf, buflen, data, #data, 9)
assert(res == 0)
return ffi.string(buf, buflen[0])
end
function zlib.zlib_uncompress(zdata, datalen)
local buf = ffi.new("uint8_t[?]", datalen)
local buflen = ffi.new("unsigned long[1]", datalen)
local res = libz.uncompress(buf, buflen, zdata, #zdata)
assert(res == 0)
return ffi.string(buf, buflen[0])
end
return zlib