diff options
| -rw-r--r-- | xmake/core/compress/lz4.lua | 160 | ||||
| -rw-r--r-- | xmake/core/sandbox/modules/import/core/compress/lz4.lua | 8 |
2 files changed, 164 insertions, 4 deletions
diff --git a/xmake/core/compress/lz4.lua b/xmake/core/compress/lz4.lua index 32a215af0..b109b1804 100644 --- a/xmake/core/compress/lz4.lua +++ b/xmake/core/compress/lz4.lua @@ -37,8 +37,12 @@ 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 @@ -54,6 +58,84 @@ 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 the socket is opened function _cstream:_ensure_opened() if not self:cdata() then @@ -87,6 +169,84 @@ 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._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 _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._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 the socket is opened function _dstream:_ensure_opened() if not self:cdata() then diff --git a/xmake/core/sandbox/modules/import/core/compress/lz4.lua b/xmake/core/sandbox/modules/import/core/compress/lz4.lua index a93c9aa65..eaf97192b 100644 --- a/xmake/core/sandbox/modules/import/core/compress/lz4.lua +++ b/xmake/core/sandbox/modules/import/core/compress/lz4.lua @@ -67,8 +67,8 @@ function sandbox_core_compress_lz4_cstream.read(stream, buff, size, opt) end -- write data to stream -function sandbox_core_compress_lz4_cstream.write(stream, data, beof, opt) - local real, errors = stream:_write(data, beof, opt) +function sandbox_core_compress_lz4_cstream.write(stream, data, opt) + local real, errors = stream:_write(data, opt) if real < 0 and errors then raise(errors) end @@ -85,8 +85,8 @@ function sandbox_core_compress_lz4_dstream.read(stream, buff, size, opt) end -- write data to stream -function sandbox_core_compress_lz4_dstream.write(stream, data, beof, opt) - local real, errors = stream:_write(data, beof, opt) +function sandbox_core_compress_lz4_dstream.write(stream, data, opt) + local real, errors = stream:_write(data, opt) if real < 0 and errors then raise(errors) end |
