Skip to content

Commit

Permalink
Optimize base64.decode & base64.urldecode
Browse files Browse the repository at this point in the history
  • Loading branch information
Sainan authored and well-in-that-case committed Dec 3, 2024
1 parent 361ff80 commit 821729b
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/lbase64.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,13 @@ static int encode(lua_State* L) {
}

static int decode(lua_State* L) {
pluto_pushstring(L, soup::base64::decode(pluto_checkstring(L, 1)));
size_t len;
const char* str = luaL_checklstring(L, 1, &len);
size_t out_len = soup::base64::getDecodedSize(str, len);
char shrtbuf[LUAI_MAXSHORTLEN];
char* dec = plutoS_prealloc(L, shrtbuf, out_len);
soup::base64::decode(dec, str, len);
plutoS_commit(L, dec, out_len);
return 1;
}

Expand All @@ -37,7 +43,13 @@ static int urlEncode(lua_State* L) {
}

static int urlDecode(lua_State* L) {
pluto_pushstring(L, soup::base64::urlDecode(pluto_checkstring(L, 1)));
size_t len;
const char* str = luaL_checklstring(L, 1, &len);
size_t out_len = soup::base64::getDecodedSize(str, len);
char shrtbuf[LUAI_MAXSHORTLEN];
char* dec = plutoS_prealloc(L, shrtbuf, out_len);
soup::base64::urlDecode(dec, str, len);
plutoS_commit(L, dec, out_len);
return 1;
}

Expand Down

0 comments on commit 821729b

Please sign in to comment.