-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathffi-zlib-stream.mooc
161 lines (129 loc) · 4.17 KB
/
ffi-zlib-stream.mooc
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
--
-- Copyright (c) 2024 lalawue
--
-- This library is free software; you can redistribute it and/or modify it
-- under the terms of the MIT license. See LICENSE for details.
--
import FFI from "ffi"
import Z from "ffi-http1-session.zlib"
zlib = Z.zlib
zlib_err = Z.zlib_err
math_min = math.min
ffi_copy = FFI.copy
ffi_str = FFI.string
str_char = string.char
_str_suffix = str_char(0, 0, 0xff, 0xff)
class ZlibStream {
_in = {}
_out = {}
_buf_size = 16384
_window_bits = -15
fn init(buf_size, window_bits) {
self._buf_size = buf_size or self._buf_size
self._window_bits = window_bits or self._window_bits
self._in, self._out = {}, {}
self:resetStream()
}
-- end stream
fn destroy() {
self:resetStream(true)
}
-- reset then create new after stream finished
fn resetStream(no_create) {
_in = self._in
_out = self._out
if _in.stream {
zlib.inflateEnd(_in.stream)
_in.stream = nil
}
if _out.stream {
zlib.deflateEnd(_out.stream)
_out.stream = nil
}
if no_create {
return
}
_in.stream, _in.inbuf, _in.outbuf =
Z.createStream(self._buf_size)
_out.stream, _out.inbuf, _out.outbuf =
Z.createStream(self._buf_size)
if zlib.Z_OK ~= Z.initInflate(_in.stream, self._window_bits) {
zlib.inflateEnd(_in.stream)
return nil
}
if zlib.Z_OK ~= Z.initDeflate(_out.stream, { windowBits = self._window_bits }) {
zlib.inflateEnd(_in.stream)
zlib.deflateEnd(_out.stream)
return nil
}
}
-- inflate stream
fn inflate(in_data) {
guard type(in_data) == "string" and in_data:len() > 0 else {
return false, "ZLibStream: Invalid params"
}
_inflate = zlib.inflate
_in = self._in
in_data ..= _str_suffix
out_data = ""
repeat {
_in.stream.avail_in = math_min(self._buf_size, #in_data)
_in.stream.next_in = _in.inbuf
ffi_copy(_in.inbuf, in_data, _in.stream.avail_in)
if _in.stream.avail_in >= #in_data {
in_data = ""
} else {
in_data = in_data:sub(1 + _in.stream.avail_in)
}
_in.stream.avail_out = self._buf_size
_in.stream.next_out = _in.outbuf
err = _inflate(_in.stream, 2)
switch err {
case 0, 1, -5:
out_data ..= ffi_str(_in.outbuf, (self._buf_size - _in.stream.avail_out))
default:
return false, "ZLibStream: \(zlib_err(err))"
}
} until in_data:len() <= 0
return true, out_data
}
-- deflate stream
fn deflate(in_data) {
guard type(in_data) == "string" and in_data:len() > 0 else {
return false, "ZLibStream: Invalid params"
}
_in_size = self._buf_size / 2
_deflate = zlib.deflate
_out = self._out
out_data = ""
out_len = 0
repeat {
_out.stream.avail_in = math_min(_in_size, #in_data)
_out.stream.next_in = _out.inbuf
ffi_copy(_out.inbuf, in_data, _out.stream.avail_in)
if _out.stream.avail_in >= #in_data {
in_data = ""
} else {
in_data = in_data:sub(1 + _out.stream.avail_in)
}
_out.stream.avail_out = self._buf_size
_out.stream.next_out = _out.outbuf
err = _deflate(_out.stream, 2)
switch err {
case 0, 1, -5:
out_data ..= ffi_str(_out.outbuf, (self._buf_size - _out.stream.avail_out))
default:
return false, "ZlibStream: \(zlib_err(err))"
}
out_len += _out.stream.avail_out
} until in_data:len() <= 0
if out_len < 5 or out_data:sub(-4) ~= _str_suffix {
out_data ..= str_char(0x0)
} else {
out_data = out_data:sub(1, -5)
}
--print("deflate message length \(out_data:len())")
return true, out_data
}
}
return ZlibStream