diff options
| author | ruki <[email protected]> | 2022-04-28 00:59:36 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2022-04-28 00:59:36 +0800 |
| commit | 9f24c274381f9636c058a950080edab8b934e9e7 (patch) | |
| tree | ff2c387cd2b7e64616673d32eb82d2f804e983e9 | |
| parent | 98a97e2faf67a1cc913ddd93ba8feea4368712ea (diff) | |
improve base64
| -rw-r--r-- | core/src/xmake/base64/decode.c | 12 | ||||
| -rw-r--r-- | core/src/xmake/base64/encode.c | 29 | ||||
| -rw-r--r-- | xmake/core/base/base64.lua | 12 |
3 files changed, 37 insertions, 16 deletions
diff --git a/core/src/xmake/base64/decode.c b/core/src/xmake/base64/decode.c index dc1614fc0..11aeefd1c 100644 --- a/core/src/xmake/base64/decode.c +++ b/core/src/xmake/base64/decode.c @@ -48,9 +48,13 @@ tb_int_t xm_base64_decode(lua_State* lua) 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); + if (real > 0) + { + lua_pushlstring(lua, (tb_char_t const*)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; } 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; } diff --git a/xmake/core/base/base64.lua b/xmake/core/base/base64.lua index f663fbb0e..35752547d 100644 --- a/xmake/core/base/base64.lua +++ b/xmake/core/base/base64.lua @@ -24,6 +24,7 @@ local base64 = base64 or {} -- load modules local io = require("base/io") local utils = require("base/utils") +local bytes = require("base/bytes") -- save metatable and builtin functions base64._encode = base64._encode or base64.encode @@ -40,7 +41,7 @@ function base64.decode(base64str) if not data then return nil, string.format("decode base64 failed") end - return data + return bytes(data) end -- encode data to the base64 string @@ -50,9 +51,14 @@ end -- @return the base64 string -- function base64.encode(data) - local base64str = base64._encode(data) + if type(data) == "string" then + data = bytes(data) + end + local datasize = data:size() + local dataaddr = data:caddr() + local base64str, errors = base64._encode(dataaddr, datasize) if not base64str then - return nil, string.format("encode base64 failed") + return nil, errors or string.format("encode base64 failed") end return base64str end |
