diff options
| author | ruki <[email protected]> | 2022-05-09 22:32:38 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2022-05-09 22:32:38 +0800 |
| commit | 1c2853f1a2587586257c64bf3ec08a61035c7d6a (patch) | |
| tree | bbc10bb569e1981b3855ba013c827e05727d5804 | |
| parent | 1c4b7d59a2048adde4108209524cfd515da95564 (diff) | |
add lz4 stream type
| -rw-r--r-- | core/src/xmake/lz4/compress_file.c | 57 | ||||
| -rw-r--r-- | core/src/xmake/lz4/prefix.h | 62 |
2 files changed, 118 insertions, 1 deletions
diff --git a/core/src/xmake/lz4/compress_file.c b/core/src/xmake/lz4/compress_file.c index 640072d07..e2403a31f 100644 --- a/core/src/xmake/lz4/compress_file.c +++ b/core/src/xmake/lz4/compress_file.c @@ -38,5 +38,60 @@ tb_int_t xm_lz4_compress_file(lua_State* lua) // check tb_assert_and_check_return_val(lua, 0); - return 0; + // get the file paths + tb_char_t const* srcpath = luaL_checkstring(lua, 1); + tb_char_t const* dstpath = luaL_checkstring(lua, 2); + tb_check_return_val(srcpath && dstpath, 0); + + // init lz4 stream + xm_lz4_stream_t stream_lz4; + xm_lz4_stream_init(&stream_lz4); + + // do compress + tb_bool_t ok = tb_false; + tb_stream_ref_t istream = tb_stream_init_from_file(srcpath, TB_FILE_MODE_RO); + tb_stream_ref_t ostream = tb_stream_init_from_file(dstpath, TB_FILE_MODE_RW | TB_FILE_MODE_CREAT | TB_FILE_MODE_TRUNC); + if (istream && ostream && tb_stream_open(istream) && tb_stream_open(ostream)) + { + tb_bool_t write_ok = tb_false; + tb_byte_t idata[TB_STREAM_BLOCK_MAXN]; + while (!tb_stream_beof(istream)) + { + write_ok = tb_false; + tb_int_t ireal = (tb_int_t)tb_stream_read(istream, idata, sizeof(idata)); + if (ireal > 0) + { + tb_byte_t* odata = tb_null; + tb_int_t oreal = xm_lz4_stream_compress(&stream_lz4, idata, ireal, &odata); + tb_assert_and_check_break(oreal >= 0 && odata); + if (oreal > 0) + { + if (!tb_stream_bwrit(ostream, odata, oreal)) + break; + } + } + else break; + write_ok = tb_true; + } + + if (tb_stream_beof(istream) && write_ok) + ok = tb_true; + } + + // exit stream + if (istream) + { + tb_stream_exit(istream); + istream = tb_null; + } + if (ostream) + { + tb_stream_exit(ostream); + ostream = tb_null; + } + xm_lz4_stream_exit(&stream_lz4); + + // ok? + lua_pushboolean(lua, ok); + return 1; } diff --git a/core/src/xmake/lz4/prefix.h b/core/src/xmake/lz4/prefix.h index aeb40da38..a9e201436 100644 --- a/core/src/xmake/lz4/prefix.h +++ b/core/src/xmake/lz4/prefix.h @@ -29,6 +29,68 @@ #include "lz4.h" #include "lz4hc.h" +/* ////////////////////////////////////////////////////////////////////////////////////// + * macros + */ +#define LZ4_STREAM_DICTSIZE (65536) + +#define LZ4_STREAM_BUFFER_POLICY_APPEND (0) +#define LZ4_STREAM_BUFFER_POLICY_RESET (1) +#define LZ4_STREAM_BUFFER_POLICY_EXTERNAL (2) + +/* ////////////////////////////////////////////////////////////////////////////////////// + * types + */ + +// the lz4 stream type +typedef struct __xm_lz4_stream_t +{ + tb_byte_t buffer[TB_STREAM_BLOCK_MAXN]; + tb_int_t buffer_size; + tb_int_t buffer_position; + tb_byte_t* output; + tb_int_t output_maxn; + LZ4_stream_t handle; +}xm_lz4_stream_t; + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ +static __tb_inline__ tb_int_t xm_lz4_stream_buffer_policy(tb_int_t buffer_size, tb_int_t buffer_position, tb_int_t data_size) +{ + if (data_size > buffer_size || data_size > LZ4_STREAM_DICTSIZE) + return LZ4_STREAM_BUFFER_POLICY_EXTERNAL; + if (buffer_position + data_size <= buffer_size) + return LZ4_STREAM_BUFFER_POLICY_APPEND; + if (data_size + LZ4_STREAM_DICTSIZE > buffer_position) + return LZ4_STREAM_BUFFER_POLICY_EXTERNAL; + return LZ4_STREAM_BUFFER_POLICY_RESET; +} + +static __tb_inline__ tb_void_t xm_lz4_stream_init(xm_lz4_stream_t* stream) +{ + stream->buffer_position = 0; + stream->buffer_size = sizeof(stream->buffer); + stream->output = tb_null; + stream->output_maxn = 0; + LZ4_resetStream(&stream->handle); +} + +static __tb_inline__ tb_void_t xm_lz4_stream_exit(xm_lz4_stream_t* stream) +{ + if (stream->output) + { + tb_free(stream->output); + stream->output = tb_null; + } +} + +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; +} + #endif |
