summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2022-05-10 22:38:37 +0800
committerruki <[email protected]>2022-05-10 22:38:37 +0800
commitbee9295b88b17adb7b7bec2c7d26592377892ceb (patch)
tree7d50c8b5278d6ad74ce9382f1066e4fbe498d5a4
parentd121dbc188c5011d9ce416e653ec01a345bff41a (diff)
decompress data
-rw-r--r--core/src/xmake/lz4/compress_file.c3
-rw-r--r--core/src/xmake/lz4/decompress_file.c3
-rw-r--r--core/src/xmake/lz4/prefix.h43
-rw-r--r--makefile2
4 files changed, 37 insertions, 14 deletions
diff --git a/core/src/xmake/lz4/compress_file.c b/core/src/xmake/lz4/compress_file.c
index 385138866..a4c1a7076 100644
--- a/core/src/xmake/lz4/compress_file.c
+++ b/core/src/xmake/lz4/compress_file.c
@@ -63,9 +63,10 @@ tb_int_t xm_lz4_compress_file(lua_State* lua)
{
tb_byte_t* odata = tb_null;
tb_long_t oreal = xm_lz4_cstream_compress(stream_lz4, idata, ireal, &odata);
- tb_assert_and_check_break(oreal >= 0 && odata);
+ tb_assert_and_check_break(oreal >= 0);
if (oreal > 0)
{
+ tb_assert_and_check_break(odata);
if (!tb_stream_bwrit(ostream, odata, oreal))
break;
}
diff --git a/core/src/xmake/lz4/decompress_file.c b/core/src/xmake/lz4/decompress_file.c
index 7dc5a023f..d0f9b27b7 100644
--- a/core/src/xmake/lz4/decompress_file.c
+++ b/core/src/xmake/lz4/decompress_file.c
@@ -63,9 +63,10 @@ tb_int_t xm_lz4_decompress_file(lua_State* lua)
{
tb_byte_t* odata = tb_null;
tb_long_t oreal = xm_lz4_dstream_decompress(stream_lz4, idata, ireal, &odata);
- tb_assert_and_check_break(oreal >= 0 && odata);
+ tb_assert_and_check_break(oreal >= 0);
if (oreal > 0)
{
+ tb_assert_and_check_break(odata);
if (!tb_stream_bwrit(ostream, odata, oreal))
break;
}
diff --git a/core/src/xmake/lz4/prefix.h b/core/src/xmake/lz4/prefix.h
index fabfc6286..342269a4b 100644
--- a/core/src/xmake/lz4/prefix.h
+++ b/core/src/xmake/lz4/prefix.h
@@ -49,11 +49,11 @@ typedef struct __xm_lz4_dstream_t
{
LZ4F_dctx* dctx;
LZ4_byte* buffer;
- tb_size_t buffer_next;
tb_size_t buffer_size;
tb_size_t buffer_maxn;
tb_size_t header_size;
LZ4_byte header[LZ4F_HEADER_SIZE_MAX];
+ LZ4_byte output[TB_STREAM_BLOCK_MAXN];
}xm_lz4_dstream_t;
/* //////////////////////////////////////////////////////////////////////////////////////
@@ -117,7 +117,7 @@ static __tb_inline__ xm_lz4_cstream_t* xm_lz4_cstream_init()
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);
+ tb_assert_and_check_return_val(stream && stream->cctx && idata && isize && podata, -1);
tb_assert_and_check_return_val(isize <= stream->write_maxn, -1);
if (!stream->header_written)
@@ -144,6 +144,11 @@ static __tb_inline__ tb_void_t xm_lz4_dstream_exit(xm_lz4_dstream_t* stream)
LZ4F_freeDecompressionContext(stream->dctx);
stream->dctx = tb_null;
}
+ if (stream->buffer)
+ {
+ tb_free(stream->buffer);
+ stream->buffer = tb_null;
+ }
tb_free(stream);
}
}
@@ -177,10 +182,9 @@ static __tb_inline__ xm_lz4_dstream_t* xm_lz4_dstream_init()
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);
+ tb_assert_and_check_return_val(stream && stream->dctx && 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)
{
@@ -193,12 +197,11 @@ static __tb_inline__ tb_long_t xm_lz4_dstream_decompress(xm_lz4_dstream_t* strea
// 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)) {
+ tb_size_t consumed_size = header_size;
+ LZ4F_errorCode_t ret = LZ4F_getFrameInfo(stream->dctx, &info, stream->header, &consumed_size);
+ if (LZ4F_isError(ret))
return -1;
- }
switch (info.blockSizeID)
{
@@ -222,13 +225,31 @@ static __tb_inline__ tb_long_t xm_lz4_dstream_decompress(xm_lz4_dstream_t* strea
stream->buffer = (LZ4_byte*)tb_malloc(stream->buffer_maxn);
tb_assert_and_check_return_val(stream->buffer, -1);
- stream->buffer_size = header_size - consumed_size;
- tb_memcpy(stream->buffer, stream->header + consumed_size, stream->buffer_size);
+ // stream->buffer_size = header_size - consumed_size;
+ // tb_memcpy(stream->buffer, stream->header + consumed_size, stream->buffer_size);
}
}
tb_check_return_val(stream->header_size == header_size && isize, 0);
+ tb_assert_and_check_return_val(stream->buffer && stream->buffer_size + isize <= stream->buffer_maxn, -1);
+
+ // append the input data
+ tb_memcpy(stream->buffer + stream->buffer_size, idata, isize);
+ stream->buffer_size += isize;
+
+ // do decompress
+ size_t srcsize = stream->buffer_size;
+ size_t dstsize = sizeof(stream->output);
+ tb_size_t ret = LZ4F_decompress(stream->dctx, stream->output, &dstsize, stream->buffer, &srcsize, tb_null);
+ if (LZ4F_isError(ret))
+ return -1;
+
+ // move the left input data
+ if (srcsize < stream->buffer_size)
+ tb_memmov(stream->buffer, stream->buffer + srcsize, stream->buffer_size - srcsize);
+ stream->buffer_size -= srcsize;
- return 0;
+ *podata = stream->output;
+ return dstsize;
}
#endif
diff --git a/makefile b/makefile
index 63d019542..5fc73bfe5 100644
--- a/makefile
+++ b/makefile
@@ -1,5 +1,5 @@
# is debug?
-debug :=n
+debug :=y
# verbose
ifneq ($(VERBOSE),y)