-
Notifications
You must be signed in to change notification settings - Fork 1
/
zlib.lua
318 lines (262 loc) · 7.5 KB
/
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
local ffi = require("ffi")
local _zlib = ffi.load("z")
-- http://zlib.net/zpipe.c
-- /usr/include/zlib.h
ffi.cdef[[
int uncompress(char *dest, unsigned long *destLen, const char *source, unsigned long sourceLen);
int compress2(char *dest, unsigned long *destLen, const char *source, unsigned long sourceLen, int level);
unsigned long compressBound(unsigned long sourceLen);
typedef void *(*alloc_func)(void *opaque, unsigned int items, unsigned int size);
typedef void (*free_func)(void *opaque, void *address);
const char *zlibVersion(void);
typedef struct z_stream_s {
const unsigned char *next_in;
unsigned int avail_in;
unsigned long total_in;
unsigned char *next_out;
unsigned int avail_out;
unsigned long total_out;
const char *msg;
struct internal_state *state;
alloc_func zalloc;
free_func zfree;
void *opaque;
int data_type;
unsigned long adler;
unsigned long reserved;
} z_stream;
typedef z_stream *z_streamp;
int deflateInit_(z_streamp strm, int level, const char *version, int stream_size);
int inflateInit_(z_streamp strm, const char *version, int stream_size);
int deflate(z_streamp strm, int flush);
int deflateEnd(z_streamp strm);
int inflate(z_streamp strm, int flush);
int inflateEnd(z_streamp strm);
]]
local flush_values = {
Z_NO_FLUSH = 0,
Z_PARTIAL_FLUSH = 1,
Z_SYNC_FLUSH = 2,
Z_FULL_FLUSH = 3,
Z_FINISH = 4,
Z_BLOCK = 5,
Z_TREES = 6,
}
local ret_codes = {
Z_OK = 0,
Z_STREAM_END = 1,
Z_NEED_DICT = 2,
Z_ERRNO = -1,
Z_STREAM_ERROR = -2,
Z_DATA_ERROR = -3,
Z_MEM_ERROR = -4,
Z_BUF_ERROR = -5,
Z_VERSION_ERROR = -6,
}
local function z_assert(ok, ret)
if ok then
return
end
local code = ""
for k, v in pairs(ret_codes) do
if v == ret then
code = k
break
end
end
error(code)
end
---@class zlib.z_stream
---@field avail_in integer
---@field avail_out integer
---@field next_in ffi.cdata*
---@field next_out ffi.cdata*
local z_stream = {}
---@alias zlib.level -1|0|1|2|3|4|5|6|7|8|9
local zlib = {}
---@return string
function zlib.version()
return ffi.string(_zlib.zlibVersion())
end
---@param size integer
---@return integer
function zlib.compress_bound(size)
---@type integer
return tonumber(_zlib.compressBound(size))
end
---@param dst_p ffi.cdata*
---@param dst_size integer
---@param src_p string|ffi.cdata*
---@param src_size integer
---@param level zlib.level?
---@return integer
function zlib.compress_ex(dst_p, dst_size, src_p, src_size, level)
level = level or -1
local out_size = ffi.new("unsigned long[1]", dst_size)
---@type integer
local ret = _zlib.compress2(dst_p, out_size, src_p, src_size, level)
z_assert(ret == ret_codes.Z_OK, ret)
---@type integer
return tonumber(out_size[0])
end
---@param dst_p ffi.cdata*
---@param dst_size integer
---@param src_p string|ffi.cdata*
---@param src_size integer
---@return integer
function zlib.uncompress_ex(dst_p, dst_size, src_p, src_size)
local out_size = ffi.new("unsigned long[1]", dst_size)
---@type integer
local ret = _zlib.uncompress(dst_p, out_size, src_p, src_size)
z_assert(ret == ret_codes.Z_OK, ret)
---@type integer
return tonumber(out_size[0])
end
---@param s string
---@param level zlib.level?
---@return string
function zlib.compress(s, level)
local size = zlib.compress_bound(#s)
local out = ffi.new("char[?]", size)
size = zlib.compress_ex(out, size, s, #s, level)
return ffi.string(out, size)
end
---@param s string
---@param size integer
---@return string
function zlib.uncompress(s, size)
local out = ffi.new("char[?]", size)
size = zlib.uncompress_ex(out, size, s, #s)
return ffi.string(out, size)
end
---@param stream_p ffi.cdata*
---@return true?
local function update_stream(stream_p)
---@type zlib.z_stream
local stream = stream_p[0]
if stream.avail_in == 0 then
local next_in, avail_in = coroutine.yield("read")
if not next_in then
return
end
stream.next_in = next_in
stream.avail_in = avail_in
end
if stream.avail_out == 0 then
local next_out, avail_out = coroutine.yield("write")
if not next_out then
return
end
stream.next_out = next_out
stream.avail_out = avail_out
end
assert(stream.avail_out > 0)
return true
end
---@param level zlib.level?
function zlib.deflate_async(level)
level = level or -1
local finish = false
local stream_p = ffi.new("z_stream[1]")
---@type integer
local ret = _zlib.deflateInit_(stream_p, level, _zlib.zlibVersion(), ffi.sizeof("z_stream"))
z_assert(ret == ret_codes.Z_OK, ret)
-- Z_OK, Z_STREAM_END, Z_STREAM_ERROR, Z_BUF_ERROR
while ret ~= ret_codes.Z_STREAM_END do
if not update_stream(stream_p) then
_zlib.deflateEnd(stream_p)
return
end
if not finish and stream_p[0].avail_in == 0 then
finish = true
end
---@type integer
ret = _zlib.deflate(stream_p, finish and flush_values.Z_FINISH or flush_values.Z_NO_FLUSH)
z_assert(ret ~= ret_codes.Z_STREAM_ERROR, ret)
-- Z_BUF_ERROR is ok
end
_zlib.deflateEnd(stream_p)
coroutine.yield("write", stream_p[0].avail_out)
end
function zlib.inflate_async()
local stream_p = ffi.new("z_stream[1]")
---@type integer
local ret = _zlib.inflateInit_(stream_p, _zlib.zlibVersion(), ffi.sizeof("z_stream"))
z_assert(ret == ret_codes.Z_OK, ret)
-- Z_OK, Z_STREAM_END, Z_NEED_DICT, Z_DATA_ERROR, Z_STREAM_ERROR, Z_MEM_ERROR, Z_BUF_ERROR
while ret ~= ret_codes.Z_STREAM_END do
if not update_stream(stream_p) then
_zlib.inflateEnd(stream_p)
return
end
---@type integer
ret = _zlib.inflate(stream_p, flush_values.Z_NO_FLUSH)
z_assert(ret ~= ret_codes.Z_STREAM_ERROR, ret)
-- Z_BUF_ERROR is ok
if
ret == ret_codes.Z_NEED_DICT or
ret == ret_codes.Z_DATA_ERROR or
ret == ret_codes.Z_MEM_ERROR
then
_zlib.inflateEnd(stream_p)
coroutine.yield("error", ret)
return
end
end
_zlib.inflateEnd(stream_p)
coroutine.yield("write", stream_p[0].avail_out)
end
---@param s string
---@param filter function
---@param chunk_size integer?
---@return string
function zlib.apply_filter(s, filter, chunk_size)
chunk_size = chunk_size or 8192
local src_p = ffi.cast("const unsigned char *", s)
local src_size = #s
local dst_p = ffi.new("unsigned char[?]", chunk_size)
---@type string[]
local out = {}
local has_data = false
local co = coroutine.create(filter)
---@type ffi.cdata*, integer
local buf, buf_size
while coroutine.status(co) ~= "dead" do
local _, action, avail_out = assert(coroutine.resume(co, buf, buf_size))
if action == "read" then
buf, buf_size = src_p, src_size
---@type ffi.cdata*, integer
src_p, src_size = src_p + src_size, 0
elseif action == "write" then
if has_data then
table.insert(out, ffi.string(dst_p, chunk_size - (avail_out or 0)))
end
buf, buf_size = dst_p, chunk_size
has_data = true
elseif action == "error" then
error(avail_out)
end
end
return table.concat(out)
end
---@param s string
---@param chunk_size integer?
---@param level zlib.level?
---@return string
function zlib.deflate(s, chunk_size, level)
local function filter() return zlib.deflate_async(level) end
return zlib.apply_filter(s, filter, chunk_size)
end
---@param s string
---@param chunk_size integer?
---@return string
function zlib.inflate(s, chunk_size)
return zlib.apply_filter(s, zlib.inflate_async, chunk_size)
end
local test_string = ("test"):rep(1000)
assert(zlib.uncompress(zlib.compress(test_string), #test_string) == test_string)
assert(zlib.inflate(zlib.deflate(test_string, 10), 10) == test_string)
assert(zlib.inflate(zlib.deflate(test_string, 10, 0), 10) == test_string)
assert(zlib.inflate(zlib.deflate(test_string, 10, 9), 10) == test_string)
assert(zlib.inflate(zlib.deflate(test_string)) == test_string)
return zlib