summaryrefslogtreecommitdiff
path: root/core/src/xmake
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 /core/src/xmake
parentabc5735b2659094a2e6ce89437f0cacdf18bedbf (diff)
add base64
Diffstat (limited to 'core/src/xmake')
-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
5 files changed, 161 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 \