summaryrefslogtreecommitdiff
path: root/core/src/xmake/base64/encode.c
diff options
context:
space:
mode:
authorruki <[email protected]>2022-04-28 00:59:36 +0800
committerruki <[email protected]>2022-04-28 00:59:36 +0800
commit9f24c274381f9636c058a950080edab8b934e9e7 (patch)
treeff2c387cd2b7e64616673d32eb82d2f804e983e9 /core/src/xmake/base64/encode.c
parent98a97e2faf67a1cc913ddd93ba8feea4368712ea (diff)
improve base64
Diffstat (limited to 'core/src/xmake/base64/encode.c')
-rw-r--r--core/src/xmake/base64/encode.c29
1 files changed, 20 insertions, 9 deletions
diff --git a/core/src/xmake/base64/encode.c b/core/src/xmake/base64/encode.c
index b87927e82..ce9fc8a2a 100644
--- a/core/src/xmake/base64/encode.c
+++ b/core/src/xmake/base64/encode.c
@@ -38,19 +38,30 @@ 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);
+ // 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)
+ {
+ lua_pushnil(lua);
+ lua_pushfstring(lua, "invalid data(%p) and size(%d)!", data, (tb_int_t)size);
+ return 2;
+ }
// 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);
+ tb_size_t real = tb_base64_encode(data, size, buff, sizeof(buff));
+ if (real > 0)
+ {
+ lua_pushlstring(lua, buff, (tb_int_t)real);
+ return 1;
+ }
}
- else lua_pushnil(lua);
- return 1;
+ lua_pushnil(lua);
+ lua_pushstring(lua, "buffer is too small");
+ return 2;
}