summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2022-04-26 22:38:45 +0800
committerruki <[email protected]>2022-04-26 22:38:45 +0800
commit7de522635bf6651b2e39140aa707bb90fae9e90f (patch)
tree2d944e15e5132ca3fdcf2afd71dd0820c1195c35
parentabc5735b2659094a2e6ce89437f0cacdf18bedbf (diff)
add base64
-rw-r--r--core/src/xmake/base64/decode.c56
-rw-r--r--core/src/xmake/base64/encode.c56
-rw-r--r--core/src/xmake/base64/prefix.h32
-rw-r--r--core/src/xmake/engine.c15
-rw-r--r--core/src/xmake/makefile2
-rw-r--r--xmake/core/base/base64.lua61
-rw-r--r--xmake/core/sandbox/modules/import/core/base/base64.lua47
7 files changed, 269 insertions, 0 deletions
diff --git a/core/src/xmake/base64/decode.c b/core/src/xmake/base64/decode.c
new file mode 100644
index 000000000..dc1614fc0
--- /dev/null
+++ b/core/src/xmake/base64/decode.c
@@ -0,0 +1,56 @@
+/*!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 base64.c
+ *
+ */
+
+/* //////////////////////////////////////////////////////////////////////////////////////
+ * trace
+ */
+#define TB_TRACE_MODULE_NAME "base64"
+#define TB_TRACE_MODULE_DEBUG (0)
+
+/* //////////////////////////////////////////////////////////////////////////////////////
+ * includes
+ */
+#include "prefix.h"
+
+/* //////////////////////////////////////////////////////////////////////////////////////
+ * implementation
+ */
+tb_int_t xm_base64_decode(lua_State* lua)
+{
+ // check
+ tb_assert_and_check_return_val(lua, 0);
+
+ // get the string
+ size_t size = 0;
+ tb_char_t const* cstr = luaL_checklstring(lua, 1, &size);
+ tb_check_return_val(cstr && size, 0);
+
+ // decode it
+ tb_byte_t buff[8192];
+ if (size < sizeof(buff))
+ {
+ tb_size_t real = tb_base64_decode(cstr, size, buff, sizeof(buff));
+ if (real > 0) lua_pushlstring(lua, (tb_char_t const*)buff, (tb_int_t)real);
+ else lua_pushnil(lua);
+ }
+ else lua_pushnil(lua);
+ return 1;
+}
diff --git a/core/src/xmake/base64/encode.c b/core/src/xmake/base64/encode.c
new file mode 100644
index 000000000..b87927e82
--- /dev/null
+++ b/core/src/xmake/base64/encode.c
@@ -0,0 +1,56 @@
+/*!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 base64.c
+ *
+ */
+
+/* //////////////////////////////////////////////////////////////////////////////////////
+ * trace
+ */
+#define TB_TRACE_MODULE_NAME "base64"
+#define TB_TRACE_MODULE_DEBUG (0)
+
+/* //////////////////////////////////////////////////////////////////////////////////////
+ * includes
+ */
+#include "prefix.h"
+
+/* //////////////////////////////////////////////////////////////////////////////////////
+ * implementation
+ */
+tb_int_t xm_base64_encode(lua_State* lua)
+{
+ // check
+ tb_assert_and_check_return_val(lua, 0);
+
+ // get the data
+ size_t size = 0;
+ tb_char_t const* data = luaL_checklstring(lua, 1, &size);
+ tb_check_return_val(data && size, 0);
+
+ // encode it
+ tb_char_t buff[8192];
+ if (size * 3 / 2 < sizeof(buff))
+ {
+ tb_size_t real = tb_base64_encode((tb_byte_t const*)data, size, buff, sizeof(buff));
+ if (real > 0) lua_pushlstring(lua, buff, (tb_int_t)real);
+ else lua_pushnil(lua);
+ }
+ else lua_pushnil(lua);
+ return 1;
+}
diff --git a/core/src/xmake/base64/prefix.h b/core/src/xmake/base64/prefix.h
new file mode 100644
index 000000000..512f5e22f
--- /dev/null
+++ b/core/src/xmake/base64/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_BASE64_PREFIX_H
+#define XM_BASE64_PREFIX_H
+
+/* //////////////////////////////////////////////////////////////////////////////////////
+ * includes
+ */
+#include "../prefix.h"
+
+
+#endif
+
+
diff --git a/core/src/xmake/engine.c b/core/src/xmake/engine.c
index 7040a75d9..6672a70bf 100644
--- a/core/src/xmake/engine.c
+++ b/core/src/xmake/engine.c
@@ -183,6 +183,10 @@ tb_int_t xm_path_is_absolute(lua_State* lua);
tb_int_t xm_hash_uuid4(lua_State* lua);
tb_int_t xm_hash_sha256(lua_State* lua);
+// the base64 functions
+tb_int_t xm_base64_encode(lua_State* lua);
+tb_int_t xm_base64_decode(lua_State* lua);
+
// the windows functions
#ifdef TB_CONFIG_OS_WINDOWS
tb_int_t xm_winos_cp_info(lua_State* lua);
@@ -387,6 +391,14 @@ static luaL_Reg const g_hash_functions[] =
, { tb_null, tb_null }
};
+// the base64 functions
+static luaL_Reg const g_base64_functions[] =
+{
+ { "encode", xm_base64_encode }
+, { "decode", xm_base64_decode }
+, { tb_null, tb_null }
+};
+
// the string functions
static luaL_Reg const g_string_functions[] =
{
@@ -932,6 +944,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 base64 functions
+ xm_lua_register(engine->lua, "base64", g_base64_functions);
+
// bind string functions
xm_lua_register(engine->lua, "string", g_string_functions);
diff --git a/core/src/xmake/makefile b/core/src/xmake/makefile
index e6225b52e..2f29b549c 100644
--- a/core/src/xmake/makefile
+++ b/core/src/xmake/makefile
@@ -99,6 +99,8 @@ xmake_C_FILES += \
path/is_absolute \
hash/uuid4 \
hash/sha256 \
+ base64/encode \
+ base64/decode \
string/trim \
string/split \
string/lastof \
diff --git a/xmake/core/base/base64.lua b/xmake/core/base/base64.lua
new file mode 100644
index 000000000..f663fbb0e
--- /dev/null
+++ b/xmake/core/base/base64.lua
@@ -0,0 +1,61 @@
+--!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 base64.lua
+--
+
+-- define module: base64
+local base64 = base64 or {}
+
+-- load modules
+local io = require("base/io")
+local utils = require("base/utils")
+
+-- save metatable and builtin functions
+base64._encode = base64._encode or base64.encode
+base64._decode = base64._decode or base64.decode
+
+-- decode base64 string to the data
+--
+-- @param base64str the base64 string
+--
+-- @return the data
+--
+function base64.decode(base64str)
+ local data = base64._decode(base64str)
+ if not data then
+ return nil, string.format("decode base64 failed")
+ end
+ return data
+end
+
+-- encode data to the base64 string
+--
+-- @param data the data
+--
+-- @return the base64 string
+--
+function base64.encode(data)
+ local base64str = base64._encode(data)
+ if not base64str then
+ return nil, string.format("encode base64 failed")
+ end
+ return base64str
+end
+
+-- return module: base64
+return base64
diff --git a/xmake/core/sandbox/modules/import/core/base/base64.lua b/xmake/core/sandbox/modules/import/core/base/base64.lua
new file mode 100644
index 000000000..2779ced78
--- /dev/null
+++ b/xmake/core/sandbox/modules/import/core/base/base64.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 base64.lua
+--
+
+-- define module
+local sandbox_core_base_base64 = sandbox_core_base_base64 or {}
+
+-- load modules
+local base64 = require("base/base64")
+local raise = require("sandbox/modules/raise")
+
+-- decode the base64 string to the data
+function sandbox_core_base_base64.decode(base64str, opt)
+ local data, errors = base64.decode(base64str, opt)
+ if not data and errors then
+ raise(errors)
+ end
+ return data
+end
+
+-- encode the data to the base64 string
+function sandbox_core_base_base64.encode(data, opt)
+ local base64str, errors = base64.encode(data, opt)
+ if not base64str and errors then
+ raise(errors)
+ end
+ return base64str
+end
+
+-- return module
+return sandbox_core_base_base64