diff options
| author | ruki <[email protected]> | 2022-05-08 17:29:38 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2022-05-08 17:29:38 +0800 |
| commit | 4045ac9d0c0e49c04794e2f8a253e81d6d29287c (patch) | |
| tree | 44c41fbf94263e8faa63c7cb40f7c0002f19bbdc | |
| parent | 27cc34ff877909993cc5de57b7ac7cff6a13b771 (diff) | |
add lz4 modules
| -rw-r--r-- | core/src/xmake/engine.c | 19 | ||||
| -rw-r--r-- | core/src/xmake/lz4/block_compress.c | 42 | ||||
| -rw-r--r-- | core/src/xmake/lz4/block_decompress.c | 43 | ||||
| -rw-r--r-- | core/src/xmake/lz4/compress.c | 42 | ||||
| -rw-r--r-- | core/src/xmake/lz4/decompress.c | 43 | ||||
| -rw-r--r-- | core/src/xmake/lz4/prefix.h | 32 | ||||
| -rw-r--r-- | core/src/xmake/makefile | 9 | ||||
| -rw-r--r-- | xmake/core/compress/lz4.lua | 73 | ||||
| -rw-r--r-- | xmake/core/sandbox/modules/import/core/compress/lz4.lua | 47 |
9 files changed, 348 insertions, 2 deletions
diff --git a/core/src/xmake/engine.c b/core/src/xmake/engine.c index 1a74387d2..397fa1dcc 100644 --- a/core/src/xmake/engine.c +++ b/core/src/xmake/engine.c @@ -189,6 +189,12 @@ tb_int_t xm_hash_md5(lua_State* lua); tb_int_t xm_base64_encode(lua_State* lua); tb_int_t xm_base64_decode(lua_State* lua); +// the lz4 functions +tb_int_t xm_lz4_compress(lua_State* lua); +tb_int_t xm_lz4_decompress(lua_State* lua); +tb_int_t xm_lz4_block_compress(lua_State* lua); +tb_int_t xm_lz4_block_decompress(lua_State* lua); + // the windows functions #ifdef TB_CONFIG_OS_WINDOWS tb_int_t xm_winos_cp_info(lua_State* lua); @@ -403,6 +409,16 @@ static luaL_Reg const g_base64_functions[] = , { tb_null, tb_null } }; +// the lz4 functions +static luaL_Reg const g_lz4_functions[] = +{ + { "compress", xm_lz4_compress } +, { "decompress", xm_lz4_decompress } +, { "block_compress", xm_lz4_block_compress } +, { "block_decompress", xm_lz4_block_decompress } +, { tb_null, tb_null } +}; + // the string functions static luaL_Reg const g_string_functions[] = { @@ -948,6 +964,9 @@ xm_engine_ref_t xm_engine_init(tb_char_t const* name, xm_engine_lni_initalizer_c // bind hash functions xm_lua_register(engine->lua, "hash", g_hash_functions); + // bind lz4 functions + xm_lua_register(engine->lua, "lz4", g_lz4_functions); + // bind base64 functions xm_lua_register(engine->lua, "base64", g_base64_functions); diff --git a/core/src/xmake/lz4/block_compress.c b/core/src/xmake/lz4/block_compress.c new file mode 100644 index 000000000..4f1e5d4c2 --- /dev/null +++ b/core/src/xmake/lz4/block_compress.c @@ -0,0 +1,42 @@ +/*!A cross-platform build utility based on Lua + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2015-present, TBOOX Open Source Group. + * + * @author ruki + * @file block_compress.c + * + */ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * trace + */ +#define TB_TRACE_MODULE_NAME "block_compress" +#define TB_TRACE_MODULE_DEBUG (0) + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * implementation + */ +tb_int_t xm_lz4_block_compress(lua_State* lua) +{ + // check + tb_assert_and_check_return_val(lua, 0); + + return 0; +} diff --git a/core/src/xmake/lz4/block_decompress.c b/core/src/xmake/lz4/block_decompress.c new file mode 100644 index 000000000..0bbad10ea --- /dev/null +++ b/core/src/xmake/lz4/block_decompress.c @@ -0,0 +1,43 @@ +/*!A cross-platform build utility based on Lua + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2015-present, TBOOX Open Source Group. + * + * @author ruki + * @file block_decompress.c + * + */ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * trace + */ +#define TB_TRACE_MODULE_NAME "block_decompress" +#define TB_TRACE_MODULE_DEBUG (0) + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * implementation + */ +tb_int_t xm_lz4_block_decompress(lua_State* lua) +{ + // check + tb_assert_and_check_return_val(lua, 0); + + return 0; +} + diff --git a/core/src/xmake/lz4/compress.c b/core/src/xmake/lz4/compress.c new file mode 100644 index 000000000..0d3f42e86 --- /dev/null +++ b/core/src/xmake/lz4/compress.c @@ -0,0 +1,42 @@ +/*!A cross-platform build utility based on Lua + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2015-present, TBOOX Open Source Group. + * + * @author ruki + * @file compress.c + * + */ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * trace + */ +#define TB_TRACE_MODULE_NAME "compress" +#define TB_TRACE_MODULE_DEBUG (0) + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * implementation + */ +tb_int_t xm_lz4_compress(lua_State* lua) +{ + // check + tb_assert_and_check_return_val(lua, 0); + + return 0; +} diff --git a/core/src/xmake/lz4/decompress.c b/core/src/xmake/lz4/decompress.c new file mode 100644 index 000000000..d18d02f7e --- /dev/null +++ b/core/src/xmake/lz4/decompress.c @@ -0,0 +1,43 @@ +/*!A cross-platform build utility based on Lua + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2015-present, TBOOX Open Source Group. + * + * @author ruki + * @file decompress.c + * + */ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * trace + */ +#define TB_TRACE_MODULE_NAME "decompress" +#define TB_TRACE_MODULE_DEBUG (0) + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * implementation + */ +tb_int_t xm_lz4_decompress(lua_State* lua) +{ + // check + tb_assert_and_check_return_val(lua, 0); + + return 0; +} + diff --git a/core/src/xmake/lz4/prefix.h b/core/src/xmake/lz4/prefix.h new file mode 100644 index 000000000..b0a8d06d0 --- /dev/null +++ b/core/src/xmake/lz4/prefix.h @@ -0,0 +1,32 @@ +/*!A cross-platform build utility based on Lua + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2015-present, TBOOX Open Source Group. + * + * @author ruki + * @file prefix.h + * + */ +#ifndef XM_LZ4_PREFIX_H +#define XM_LZ4_PREFIX_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "../prefix.h" + + +#endif + + diff --git a/core/src/xmake/makefile b/core/src/xmake/makefile index d9e8025fb..9de3fe01b 100644 --- a/core/src/xmake/makefile +++ b/core/src/xmake/makefile @@ -133,7 +133,11 @@ xmake_C_FILES += \ libc/byteof \ libc/setbyte \ libc/strndup \ - tty/term_mode + tty/term_mode \ + lz4/compress \ + lz4/decompress \ + lz4/block_compress \ + lz4/block_decompress iswin = ifeq ($(PLAT),windows) @@ -168,7 +172,8 @@ xmake_CXFLAGS += $(if $(findstring curses,$(base_LIBNAMES)),-DXM_CONFIG xmake_INC_DIRS += \ ../tbox/tbox/src \ ../tbox/inc/$(PLAT) \ - ../sv/sv/include + ../sv/sv/include \ + ../lz4/lz4/lib ifeq ($(RUNTIME),luajit) xmake_INC_DIRS += ../luajit/luajit/src xmake_CXFLAGS += -DUSE_LUAJIT diff --git a/xmake/core/compress/lz4.lua b/xmake/core/compress/lz4.lua new file mode 100644 index 000000000..803e9cf0b --- /dev/null +++ b/xmake/core/compress/lz4.lua @@ -0,0 +1,73 @@ +--!A cross-platform build utility based on Lua +-- +-- Licensed under the Apache License, Version 2.0 (the "License"); +-- you may not use this file except in compliance with the License. +-- You may obtain a copy of the License at +-- +-- http://www.apache.org/licenses/LICENSE-2.0 +-- +-- Unless required by applicable law or agreed to in writing, software +-- distributed under the License is distributed on an "AS IS" BASIS, +-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +-- See the License for the specific language governing permissions and +-- limitations under the License. +-- +-- Copyright (C) 2015-present, TBOOX Open Source Group. +-- +-- @author ruki +-- @file lz4.lua +-- + +-- define module: lz4 +local lz4 = lz4 or {} + +-- load modules +local io = require("base/io") +local utils = require("base/utils") +local bytes = require("base/bytes") + +-- save metatable and builtin functions +lz4._compress = lz4._compress or lz4.compress +lz4._decompress = lz4._decompress or lz4.decompress +lz4._block_compress = lz4._block_compress or lz4.block_compress +lz4._block_decompress = lz4._block_decompress or lz4.block_decompress + +-- compress data +-- +-- @param data the data +-- @param opt the options +-- +-- @return the result data +-- +function lz4.compress(data, opt) + if type(data) == "string" then + data = bytes(data) + end + local datasize = data:size() + local dataaddr = data:caddr() + local result, errors = lz4._compress(dataaddr, datasize) + if not result then + return nil, errors or string.format("compress lz4 data failed") + end + return bytes(result) +end + +-- decompres data +-- +-- @param data the data +-- @param opt the options +-- +-- @return the result data +-- +function lz4.decompress(data, opt) + local datasize = data:size() + local dataaddr = data:caddr() + local result, errors = lz4._decompress(dataaddr, datasize) + if not result then + return nil, string.format("decompress lz4 data failed") + end + return bytes(result) +end + +-- return module: lz4 +return lz4 diff --git a/xmake/core/sandbox/modules/import/core/compress/lz4.lua b/xmake/core/sandbox/modules/import/core/compress/lz4.lua new file mode 100644 index 000000000..ba49e35bb --- /dev/null +++ b/xmake/core/sandbox/modules/import/core/compress/lz4.lua @@ -0,0 +1,47 @@ +--!A cross-platform build utility based on Lua +-- +-- Licensed under the Apache License, Version 2.0 (the "License"); +-- you may not use this file except in compliance with the License. +-- You may obtain a copy of the License at +-- +-- http://www.apache.org/licenses/LICENSE-2.0 +-- +-- Unless required by applicable law or agreed to in writing, software +-- distributed under the License is distributed on an "AS IS" BASIS, +-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +-- See the License for the specific language governing permissions and +-- limitations under the License. +-- +-- Copyright (C) 2015-present, TBOOX Open Source Group. +-- +-- @author ruki +-- @file lz4.lua +-- + +-- define module +local sandbox_core_base_lz4 = sandbox_core_base_lz4 or {} + +-- load modules +local lz4 = require("base/lz4") +local raise = require("sandbox/modules/raise") + +-- compress data +function sandbox_core_base_lz4.compress(data, opt) + local result, errors = lz4.compress(data, opt) + if not result and errors then + raise(errors) + end + return result +end + +-- decompress data +function sandbox_core_base_lz4.decompress(data, opt) + local result, errors = lz4.decompress(data, opt) + if not result and errors then + raise(errors) + end + return result +end + +-- return module +return sandbox_core_base_lz4 |
