diff options
| author | ruki <[email protected]> | 2022-05-08 20:00:38 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2022-05-08 20:00:38 +0800 |
| commit | 023ecb9d8f518b4e8b9740cabf9cc1b45d22e6f9 (patch) | |
| tree | 31e02c90696454b52fad65854fb424f842dcf1ef /core/src | |
| parent | 131e16c8a0a3534320e4712aadebeef9764cc4fe (diff) | |
improve test
Diffstat (limited to 'core/src')
| -rw-r--r-- | core/src/xmake/lz4/block_decompress.c | 46 |
1 files changed, 45 insertions, 1 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; } |
