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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
|
--!A cross-platform build utility based on Lua
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
--
-- Copyright (C) 2015-present, Xmake Open Source Community.
--
-- @author ruki
-- @file lz4.lua
--
-- define module: lz4
local lz4 = lz4 or {}
local _cstream = _cstream or {}
local _dstream = _dstream or {}
-- load modules
local io = require("base/io")
local utils = require("base/utils")
local bytes = require("base/bytes")
local table = require("base/table")
-- save metatable and builtin functions
lz4._compress = lz4._compress or lz4.compress
lz4._decompress = lz4._decompress or lz4.decompress
lz4._block_compress = lz4._block_compress or lz4.block_compress
lz4._block_decompress = lz4._block_decompress or lz4.block_decompress
lz4._compress_file = lz4._compress_file or lz4.compress_file
lz4._decompress_file = lz4._decompress_file or lz4.decompress_file
lz4._compress_stream_open = lz4._compress_stream_open or lz4.compress_stream_open
lz4._compress_stream_read = lz4._compress_stream_read or lz4.compress_stream_read
lz4._compress_stream_write = lz4._compress_stream_write or lz4.compress_stream_write
lz4._compress_stream_close = lz4._compress_stream_close or lz4.compress_stream_close
lz4._decompress_stream_open = lz4._decompress_stream_open or lz4.decompress_stream_open
lz4._decompress_stream_read = lz4._decompress_stream_read or lz4.decompress_stream_read
lz4._decompress_stream_write = lz4._decompress_stream_write or lz4.decompress_stream_write
lz4._decompress_stream_close = lz4._decompress_stream_close or lz4.decompress_stream_close
-- new a compress stream
function _cstream.new(handle)
local instance = table.inherit(_cstream)
instance._HANDLE = handle
setmetatable(instance, _cstream)
return instance
end
-- get cdata of stream
function _cstream:cdata()
return self._HANDLE
end
-- read data from stream
function _cstream:read(buff, size, opt)
assert(buff)
-- ensure opened
local ok, errors = self:_ensure_opened()
if not ok then
return -1, errors
end
-- check buffer
size = size or buff:size()
if buff:size() < size then
return -1, string.format("%s: too small buffer!", self)
end
-- check size
if size == 0 then
return 0
elseif size == nil or size < 0 then
return -1, string.format("%s: invalid size(%d)!", self, size)
end
-- init start in buffer
opt = opt or {}
local start = opt.start or 1
local pos = start - 1
if start >= buff:size() or start < 1 then
return -1, string.format("%s: invalid start(%d)!", self, start)
end
-- read it
local read, data_or_errors = lz4._compress_stream_read(self:cdata(), buff:caddr() + pos, math.min(buff:size() - pos, size))
if read > 0 then
data_or_errors = buff:slice(start, read)
end
if read < 0 and data_or_errors then
data_or_errors = string.format("%s: %s", self, data_or_errors)
end
return read, data_or_errors
end
-- write data to stream
function _cstream:write(data, opt)
-- ensure opened
local ok, errors = self:_ensure_opened()
if not ok then
return -1, errors
end
-- get data address and size for bytes and string
if type(data) == "string" then
data = bytes(data)
end
local datasize = data:size()
local dataaddr = data:caddr()
-- init start and last
opt = opt or {}
local start = opt.start or 1
local last = opt.last or datasize
if start < 1 or start > datasize then
return -1, string.format("%s: invalid start(%d)!", self, start)
end
if last < start - 1 or last > datasize + start - 1 then
return -1, string.format("%s: invalid last(%d)!", self, last)
end
-- write it
local errors = nil
local write, errors = lz4._compress_stream_write(self:cdata(), dataaddr + start - 1, last + 1 - start, opt.beof)
if write < 0 and errors then
errors = string.format("%s: %s", self, errors)
end
return write, errors
end
-- ensure it is opened
function _cstream:_ensure_opened()
if not self:cdata() then
return false, string.format("%s: has been closed!", self)
end
return true
end
-- tostring(stream)
function _cstream:__tostring()
return string.format("<lz4/cstream: %s>", self:cdata())
end
-- gc(stream)
function _cstream:__gc()
if self:cdata() and lz4._compress_stream_close(self:cdata()) then
self._HANDLE = nil
end
end
-- new a decompress stream
function _dstream.new(handle)
local instance = table.inherit(_dstream)
instance._HANDLE = handle
setmetatable(instance, _dstream)
return instance
end
-- get cdata of stream
function _dstream:cdata()
return self._HANDLE
end
-- read data from stream
function _dstream:read(buff, size, opt)
assert(buff)
-- ensure opened
local ok, errors = self:_ensure_opened()
if not ok then
return -1, errors
end
-- check buffer
size = size or buff:size()
if buff:size() < size then
return -1, string.format("%s: too small buffer!", self)
end
-- check size
if size == 0 then
return 0
elseif size == nil or size < 0 then
return -1, string.format("%s: invalid size(%d)!", self, size)
end
-- init start in buffer
opt = opt or {}
local start = opt.start or 1
local pos = start - 1
if start >= buff:size() or start < 1 then
return -1, string.format("%s: invalid start(%d)!", self, start)
end
-- read it
local read, data_or_errors = lz4._decompress_stream_read(self:cdata(), buff:caddr() + pos, math.min(buff:size() - pos, size))
if read > 0 then
data_or_errors = buff:slice(start, read)
end
if read < 0 and data_or_errors then
data_or_errors = string.format("%s: %s", self, data_or_errors)
end
return read, data_or_errors
end
-- write data to stream
function _dstream:write(data, opt)
-- ensure opened
local ok, errors = self:_ensure_opened()
if not ok then
return -1, errors
end
-- get data address and size for bytes and string
if type(data) == "string" then
data = bytes(data)
end
local datasize = data:size()
local dataaddr = data:caddr()
-- init start and last
opt = opt or {}
local start = opt.start or 1
local last = opt.last or datasize
if start < 1 or start > datasize then
return -1, string.format("%s: invalid start(%d)!", self, start)
end
if last < start - 1 or last > datasize + start - 1 then
return -1, string.format("%s: invalid last(%d)!", self, last)
end
-- write it
local errors = nil
local write, errors = lz4._decompress_stream_write(self:cdata(), dataaddr + start - 1, last + 1 - start, opt.beof)
if write < 0 and errors then
errors = string.format("%s: %s", self, errors)
end
return write, errors
end
-- ensure it is opened
function _dstream:_ensure_opened()
if not self:cdata() then
return false, string.format("%s: has been closed!", self)
end
return true
end
-- tostring(stream)
function _dstream:__tostring()
return string.format("<lz4/dstream: %s>", self:cdata())
end
-- gc(stream)
function _dstream:__gc()
if self:cdata() and lz4._decompress_stream_close(self:cdata()) then
self._HANDLE = nil
end
end
-- open a compress stream
function lz4.compress_stream(opt)
local handle, errors = lz4._compress_stream_open()
if handle then
return _cstream.new(handle)
else
return nil, errors or "failed to open compress stream!"
end
end
-- open a decompress stream
function lz4.decompress_stream(opt)
local handle, errors = lz4._decompress_stream_open()
if handle then
return _dstream.new(handle)
else
return nil, errors or "failed to open decompress stream!"
end
end
-- compress frame data
--
-- @param data the data
-- @param opt the options
--
-- @return the result data
--
function lz4.compress(data, opt)
if type(data) == "string" then
data = bytes(data)
end
local datasize = data:size()
local dataaddr = data:caddr()
local result, errors = lz4._compress(dataaddr, datasize)
if not result then
return nil, errors or string.format("compress frame data failed, %s", errors or "unknown")
end
return bytes(result)
end
-- decompres frame data
--
-- @param data the data
-- @param opt the options
--
-- @return the result data
--
function lz4.decompress(data, opt)
if type(data) == "string" then
data = bytes(data)
end
local datasize = data:size()
local dataaddr = data:caddr()
local result, errors = lz4._decompress(dataaddr, datasize)
if not result then
return nil, string.format("decompress frame data failed, %s", errors or "unknown")
end
return bytes(result)
end
-- compress file data
function lz4.compress_file(srcpath, dstpath, opt)
local ok, errors = lz4._compress_file(tostring(srcpath), tostring(dstpath))
if not ok then
errors = string.format("compress file %s failed!", srcpath, errors or os.strerror() or "unknown")
end
return ok, errors
end
-- decompress file data
function lz4.decompress_file(srcpath, dstpath, opt)
local ok, errors = lz4._decompress_file(tostring(srcpath), tostring(dstpath))
if not ok then
errors = string.format("decompress file %s failed!", srcpath, errors or os.strerror() or "unknown")
end
return ok, errors
end
-- compress block data
--
-- @param data the data
-- @param opt the options
--
-- @return the result data
--
function lz4.block_compress(data, opt)
if type(data) == "string" then
data = bytes(data)
end
local datasize = data:size()
local dataaddr = data:caddr()
local result, errors = lz4._block_compress(dataaddr, datasize)
if not result then
return nil, errors or string.format("compress block data failed, %s", errors or "unknown")
end
return bytes(result)
end
-- decompres block data
--
-- @param data the data
-- @param realsize the decompressed real size
-- @param opt the options
--
-- @return the result data
--
function lz4.block_decompress(data, realsize, opt)
local datasize = data:size()
local dataaddr = data:caddr()
local result, errors = lz4._block_decompress(dataaddr, datasize, realsize)
if not result then
return nil, string.format("decompress block data failed, %s", errors or "unknown")
end
return bytes(result)
end
-- return module: lz4
return lz4
|