diff options
| author | ruki <[email protected]> | 2022-05-10 21:08:24 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2022-05-10 21:08:24 +0800 |
| commit | 5de35d097d1ee18b8cbb316100a1b6744115a4e0 (patch) | |
| tree | 37a99721dfeaa241891326e578350fe87041996c | |
| parent | e9e97ef1b39c8c9227d1b34abb36ee560c30e1d0 (diff) | |
read frame
| -rw-r--r-- | core/src/xmake/lz4/compress_file.c | 4 | ||||
| -rw-r--r-- | core/src/xmake/lz4/decompress_file.c | 4 | ||||
| -rw-r--r-- | core/src/xmake/lz4/prefix.h | 42 |
3 files changed, 39 insertions, 11 deletions
diff --git a/core/src/xmake/lz4/compress_file.c b/core/src/xmake/lz4/compress_file.c index ead65221d..385138866 100644 --- a/core/src/xmake/lz4/compress_file.c +++ b/core/src/xmake/lz4/compress_file.c @@ -58,11 +58,11 @@ tb_int_t xm_lz4_compress_file(lua_State* lua) while (!tb_stream_beof(istream)) { write_ok = tb_false; - tb_int_t ireal = (tb_int_t)tb_stream_read(istream, idata, sizeof(idata)); + tb_long_t ireal = (tb_long_t)tb_stream_read(istream, idata, sizeof(idata)); if (ireal > 0) { tb_byte_t* odata = tb_null; - tb_int_t oreal = xm_lz4_cstream_compress(stream_lz4, idata, ireal, &odata); + tb_long_t oreal = xm_lz4_cstream_compress(stream_lz4, idata, ireal, &odata); tb_assert_and_check_break(oreal >= 0 && odata); if (oreal > 0) { diff --git a/core/src/xmake/lz4/decompress_file.c b/core/src/xmake/lz4/decompress_file.c index 0b0d01553..7dc5a023f 100644 --- a/core/src/xmake/lz4/decompress_file.c +++ b/core/src/xmake/lz4/decompress_file.c @@ -58,11 +58,11 @@ tb_int_t xm_lz4_decompress_file(lua_State* lua) while (!tb_stream_beof(istream)) { write_ok = tb_false; - tb_int_t ireal = (tb_int_t)tb_stream_read(istream, idata, sizeof(idata)); + tb_long_t ireal = (tb_long_t)tb_stream_read(istream, idata, sizeof(idata)); if (ireal > 0) { tb_byte_t* odata = tb_null; - tb_int_t oreal = xm_lz4_dstream_decompress(stream_lz4, idata, ireal, &odata); + tb_long_t oreal = xm_lz4_dstream_decompress(stream_lz4, idata, ireal, &odata); tb_assert_and_check_break(oreal >= 0 && odata); if (oreal > 0) { diff --git a/core/src/xmake/lz4/prefix.h b/core/src/xmake/lz4/prefix.h index d67a7d560..b04eb3952 100644 --- a/core/src/xmake/lz4/prefix.h +++ b/core/src/xmake/lz4/prefix.h @@ -38,8 +38,8 @@ typedef struct __xm_lz4_cstream_t { LZ4F_cctx* cctx; LZ4_byte* buffer; - tb_int_t write_maxn; - tb_int_t buffer_maxn; + tb_size_t write_maxn; + tb_size_t buffer_maxn; tb_bool_t header_written; LZ4_byte header[LZ4F_HEADER_SIZE_MAX]; }xm_lz4_cstream_t; @@ -49,9 +49,11 @@ typedef struct __xm_lz4_dstream_t { LZ4F_dctx* dctx; LZ4_byte* srcBuf; - tb_int_t srcBufNext; - tb_int_t srcBufSize; - tb_int_t srcBufMaxSize; + tb_size_t srcBufNext; + tb_size_t srcBufSize; + tb_size_t srcBufMaxSize; + tb_size_t header_size; + LZ4_byte header[LZ4F_HEADER_SIZE_MAX]; }xm_lz4_dstream_t; /* ////////////////////////////////////////////////////////////////////////////////////// @@ -112,7 +114,7 @@ static __tb_inline__ xm_lz4_cstream_t* xm_lz4_cstream_init() return stream; } -static __tb_inline__ tb_int_t xm_lz4_cstream_compress(xm_lz4_cstream_t* stream, tb_byte_t const* idata, tb_int_t isize, tb_byte_t** podata) +static __tb_inline__ tb_long_t xm_lz4_cstream_compress(xm_lz4_cstream_t* stream, tb_byte_t const* idata, tb_size_t isize, tb_byte_t** podata) { // check tb_assert_and_check_return_val(stream && idata && isize && podata, -1); @@ -172,11 +174,37 @@ static __tb_inline__ xm_lz4_dstream_t* xm_lz4_dstream_init() return stream; } -static __tb_inline__ tb_int_t xm_lz4_dstream_decompress(xm_lz4_dstream_t* stream, tb_byte_t const* idata, tb_int_t isize, tb_byte_t** podata) +static __tb_inline__ tb_long_t xm_lz4_dstream_decompress(xm_lz4_dstream_t* stream, tb_byte_t const* idata, tb_size_t isize, tb_byte_t** podata) { // check tb_assert_and_check_return_val(stream && idata && isize && podata, -1); + // read header first + LZ4F_errorCode_t ret; + const tb_size_t header_size = sizeof(stream->header); + if (stream->header_size < header_size) + { + tb_size_t size = tb_min(header_size - stream->header_size, isize); + tb_memcpy(stream->header + stream->header_size, idata, size); + stream->header_size += size; + idata += size; + isize -= size; + + // get frame info if header is ok + if (stream->header_size == header_size) + { + size_t consumed_size; + LZ4F_frameInfo_t info; + ret = LZ4F_getFrameInfo(stream->dctx, &info, stream->header, &consumed_size); + if (LZ4F_isError(ret)) { + return -1; + } + } + + // TODO + } + tb_check_return_val(stream->header_size == header_size && isize, 0); + return 0; } |
