summaryrefslogtreecommitdiff
path: root/core/src/xmake
diff options
context:
space:
mode:
authorruki <[email protected]>2022-05-08 19:43:19 +0800
committerruki <[email protected]>2022-05-08 19:43:19 +0800
commit131e16c8a0a3534320e4712aadebeef9764cc4fe (patch)
tree508dcad1f1f192ec0b6c3b0b0cfc5eabb0cb8775 /core/src/xmake
parent941676f28ac40dc8a3801ce2a8cc33a5afe00e0f (diff)
impl lz4.block_compress
Diffstat (limited to 'core/src/xmake')
-rw-r--r--core/src/xmake/lz4/block_compress.c40
1 files changed, 39 insertions, 1 deletions
diff --git a/core/src/xmake/lz4/block_compress.c b/core/src/xmake/lz4/block_compress.c
index 4f1e5d4c2..199467110 100644
--- a/core/src/xmake/lz4/block_compress.c
+++ b/core/src/xmake/lz4/block_compress.c
@@ -38,5 +38,43 @@ tb_int_t xm_lz4_block_compress(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 || size > LZ4_MAX_INPUT_SIZE)
+ {
+ lua_pushnil(lua);
+ lua_pushfstring(lua, "invalid data(%p) and size(%d)!", data, (tb_int_t)size);
+ return 2;
+ }
+
+ // do compress
+ tb_bool_t ok = tb_false;
+ tb_byte_t* output_data = tb_null;
+ tb_byte_t buffer[8192];
+ do
+ {
+ tb_size_t output_size = LZ4_compressBound(size);
+ tb_assert_and_check_break(output_size);
+
+ output_data = output_size <= sizeof(buffer)? buffer : tb_malloc(output_size);
+ tb_assert_and_check_break(output_data);
+
+ tb_int_t real = LZ4_compress_default((tb_char_t const*)data, (tb_char_t*)output_data, size, output_size);
+ tb_assert_and_check_break(real > 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;
}