summaryrefslogtreecommitdiff
path: root/core/src/xmake/base64/encode.c
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/base64/encode.c
parentabc5735b2659094a2e6ce89437f0cacdf18bedbf (diff)
add base64
Diffstat (limited to 'core/src/xmake/base64/encode.c')
-rw-r--r--core/src/xmake/base64/encode.c56
1 files changed, 56 insertions, 0 deletions
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;
+}