diff options
| author | ruki <[email protected]> | 2022-05-10 00:14:57 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2022-05-10 00:14:57 +0800 |
| commit | 6f789c9db16eadf5c81b63221cddff59988da465 (patch) | |
| tree | d071747134b4c65070b65f96f8f4708b91edf6b0 | |
| parent | e78095da9381e3b45922086e9b292b3b7ab49009 (diff) | |
compress file
| -rw-r--r-- | core/src/xmake/lz4/prefix.h | 46 | ||||
| -rw-r--r-- | xmake/core/compress/lz4.lua | 22 |
2 files changed, 48 insertions, 20 deletions
diff --git a/core/src/xmake/lz4/prefix.h b/core/src/xmake/lz4/prefix.h index a9e201436..b88dfbbfe 100644 --- a/core/src/xmake/lz4/prefix.h +++ b/core/src/xmake/lz4/prefix.h @@ -50,6 +50,7 @@ typedef struct __xm_lz4_stream_t tb_int_t buffer_position; tb_byte_t* output; tb_int_t output_maxn; + tb_int_t accelerate; LZ4_stream_t handle; }xm_lz4_stream_t; @@ -69,6 +70,7 @@ static __tb_inline__ tb_int_t xm_lz4_stream_buffer_policy(tb_int_t buffer_size, static __tb_inline__ tb_void_t xm_lz4_stream_init(xm_lz4_stream_t* stream) { + stream->accelerate = 1; stream->buffer_position = 0; stream->buffer_size = sizeof(stream->buffer); stream->output = tb_null; @@ -87,8 +89,48 @@ static __tb_inline__ tb_void_t xm_lz4_stream_exit(xm_lz4_stream_t* stream) static __tb_inline__ tb_int_t xm_lz4_stream_compress(xm_lz4_stream_t* stream, tb_byte_t const* idata, tb_int_t isize, tb_byte_t** podata) { - // TODO - return -1; + // check + tb_assert_and_check_return_val(stream && idata && isize && podata, -1); + + // ensure the output buffer + tb_int_t bound = LZ4_compressBound(isize); + if (bound > 0 && bound > stream->output_maxn) + { + if (!stream->output) stream->output = tb_malloc_bytes(bound); + else stream->output = (tb_byte_t*)tb_ralloc(stream->output, bound); + stream->output_maxn = bound; + } + tb_assert_and_check_return_val(stream->output && stream->output_maxn > 0, -1); + + // do compress + tb_int_t real = 0; + tb_int_t policy = xm_lz4_stream_buffer_policy(stream->buffer_size, stream->buffer_position, isize); + if (policy == LZ4_STREAM_BUFFER_POLICY_APPEND || policy == LZ4_STREAM_BUFFER_POLICY_RESET) + { + tb_byte_t* buffer = tb_null; + if (policy == LZ4_STREAM_BUFFER_POLICY_APPEND) + { + buffer = stream->buffer + stream->buffer_position; + stream->buffer_position += isize; + } + else + { + buffer = stream->buffer; + stream->buffer_position = isize; + } + tb_memcpy(buffer, idata, isize); + real = LZ4_compress_fast_continue(&stream->handle, (tb_char_t*)buffer, (tb_char_t*)stream->output, isize, bound, stream->accelerate); + tb_assert_and_check_return_val(real > 0, -1); + } + else + { + real = LZ4_compress_fast_continue(&stream->handle, (tb_char_t*)idata, (tb_char_t*)stream->output, isize, bound, stream->accelerate); + tb_assert_and_check_return_val(real > 0, -1); + stream->buffer_position = LZ4_saveDict(&stream->handle, (tb_char_t*)stream->buffer, stream->buffer_size); + } + + *podata = stream->output; + return real; } #endif diff --git a/xmake/core/compress/lz4.lua b/xmake/core/compress/lz4.lua index 4c495dc26..ecc7a742a 100644 --- a/xmake/core/compress/lz4.lua +++ b/xmake/core/compress/lz4.lua @@ -31,6 +31,8 @@ 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 -- compress frame data -- @@ -72,30 +74,14 @@ function lz4.decompress(data, opt) return bytes(result) end --- TODO use stream in core -- compress file data function lz4.compress_file(srcpath, dstpath, opt) - local data, errors = io.readfile(srcpath, {encoding = "binary"}) - if data then - data, errors = lz4.compress(data, opt) - if data then - return io.writefile(dstpath, data, {encoding = "binary"}) - end - end - return false, errors or "compress failed" + return lz4._compress_file(srcpath, dstpath) end --- TODO use stream in core -- decompress file data function lz4.decompress_file(srcpath, dstpath, opt) - local data, errors = io.readfile(srcpath, {encoding = "binary"}) - if data then - data, errors = lz4.decompress(data, opt) - if data then - return io.writefile(dstpath, data, {encoding = "binary"}) - end - end - return false, errors or "decompress failed" + return lz4._decompress_file(srcpath, dstpath) end -- compress block data |
