summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2022-05-08 20:00:38 +0800
committerruki <[email protected]>2022-05-08 20:00:38 +0800
commit023ecb9d8f518b4e8b9740cabf9cc1b45d22e6f9 (patch)
tree31e02c90696454b52fad65854fb424f842dcf1ef
parent131e16c8a0a3534320e4712aadebeef9764cc4fe (diff)
improve test
-rw-r--r--core/src/xmake/lz4/block_decompress.c46
-rw-r--r--tests/modules/compress/test.lua3
-rw-r--r--xmake/core/compress/lz4.lua5
-rw-r--r--xmake/core/sandbox/modules/import/core/compress/lz4.lua4
4 files changed, 52 insertions, 6 deletions
diff --git a/core/src/xmake/lz4/block_decompress.c b/core/src/xmake/lz4/block_decompress.c
index 0bbad10ea..6262566d2 100644
--- a/core/src/xmake/lz4/block_decompress.c
+++ b/core/src/xmake/lz4/block_decompress.c
@@ -38,6 +38,50 @@ tb_int_t xm_lz4_block_decompress(lua_State* lua)
// check
tb_assert_and_check_return_val(lua, 0);
- return 0;
+ // get data and size
+ tb_size_t size = 0;
+ tb_byte_t const* data = tb_null;
+ if (lua_isnumber(lua, 1)) data = (tb_byte_t const*)(tb_size_t)(tb_long_t)lua_tonumber(lua, 1);
+ if (lua_isnumber(lua, 2)) size = (tb_size_t)lua_tonumber(lua, 2);
+ if (!data || !size)
+ {
+ lua_pushnil(lua);
+ lua_pushfstring(lua, "invalid data(%p) and size(%d)!", data, (tb_int_t)size);
+ return 2;
+ }
+
+ // get real size
+ tb_size_t real = (tb_size_t)lua_tonumber(lua, 3);
+ if (!real)
+ {
+ lua_pushnil(lua);
+ lua_pushfstring(lua, "invalid output size(%d)!", (tb_int_t)real);
+ return 2;
+ }
+
+ // do decompress
+ tb_bool_t ok = tb_false;
+ tb_byte_t* output_data = tb_null;
+ tb_byte_t buffer[8192];
+ do
+ {
+ output_data = real <= sizeof(buffer)? buffer : tb_malloc(real);
+ tb_assert_and_check_break(output_data);
+
+ tb_int_t r = LZ4_decompress_fast((tb_char_t const*)data, (tb_char_t*)output_data, real);
+ tb_assert_and_check_break(r > 0);
+
+ lua_pushlstring(lua, (tb_char_t const*)output_data, real);
+ ok = tb_true;
+ } while (0);
+
+ if (output_data && output_data != buffer)
+ {
+ tb_free(output_data);
+ output_data = tb_null;
+ }
+
+ if (!ok) lua_pushnil(lua);
+ return 1;
}
diff --git a/tests/modules/compress/test.lua b/tests/modules/compress/test.lua
index 38a65adf9..b12e2e877 100644
--- a/tests/modules/compress/test.lua
+++ b/tests/modules/compress/test.lua
@@ -1,6 +1,7 @@
import("core.compress.lz4")
-function test_lz4_frame_compress(t)
+function test_lz4(t)
t:are_equal(lz4.decompress(lz4.compress("hello world")):str(), "hello world")
+ t:are_equal(lz4.block_decompress(lz4.block_compress("hello world"), 11):str(), "hello world")
end
diff --git a/xmake/core/compress/lz4.lua b/xmake/core/compress/lz4.lua
index 2ac5de7b3..a153d434a 100644
--- a/xmake/core/compress/lz4.lua
+++ b/xmake/core/compress/lz4.lua
@@ -92,14 +92,15 @@ end
-- decompres block data
--
-- @param data the data
+-- @param realsize the decompressed real size
-- @param opt the options
--
-- @return the result data
--
-function lz4.block_decompress(data, opt)
+function lz4.block_decompress(data, realsize, opt)
local datasize = data:size()
local dataaddr = data:caddr()
- local result, errors = lz4._block_decompress(dataaddr, datasize)
+ local result, errors = lz4._block_decompress(dataaddr, datasize, realsize)
if not result then
return nil, string.format("decompress block data failed, %s", errors or "unknown")
end
diff --git a/xmake/core/sandbox/modules/import/core/compress/lz4.lua b/xmake/core/sandbox/modules/import/core/compress/lz4.lua
index 8f45d7a3a..ec959aa71 100644
--- a/xmake/core/sandbox/modules/import/core/compress/lz4.lua
+++ b/xmake/core/sandbox/modules/import/core/compress/lz4.lua
@@ -53,8 +53,8 @@ function sandbox_core_compress_lz4.block_compress(data, opt)
end
-- decompress block data
-function sandbox_core_compress_lz4.block_decompress(data, opt)
- local result, errors = lz4.block_decompress(data, opt)
+function sandbox_core_compress_lz4.block_decompress(data, realsize, opt)
+ local result, errors = lz4.block_decompress(data, realsize, opt)
if not result and errors then
raise(errors)
end