mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-06-28 07:18:30 +00:00
Add EncodeBase32() to Redbean (#856)
This commit is contained in:
parent
3b086af91b
commit
6e4b9b6515
7 changed files with 221 additions and 1 deletions
|
@ -605,6 +605,30 @@ int LuaEncodeLatin1(lua_State *L) {
|
|||
}
|
||||
}
|
||||
|
||||
dontinline int LuaBase32Impl(lua_State *L,
|
||||
char *B32(const char *, size_t, const char *, size_t, size_t *)) {
|
||||
char *p;
|
||||
size_t sl, al; // source/output and alphabet lengths
|
||||
const char *s = luaL_checklstring(L, 1, &sl);
|
||||
// use an empty string, as EncodeBase32 provides a default value
|
||||
const char *a = luaL_optlstring(L, 2, "", &al);
|
||||
if (!IS2POW(al) || al > 128 || al == 1)
|
||||
return luaL_error(L, "alphabet length is not a power of 2 in range 2..128");
|
||||
if (!(p = B32(s, sl, a, al, &sl)))
|
||||
return luaL_error(L, "out of memory");
|
||||
lua_pushlstring(L, p, sl);
|
||||
free(p);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int LuaEncodeBase32(lua_State *L) {
|
||||
return LuaBase32Impl(L, EncodeBase32);
|
||||
}
|
||||
|
||||
int LuaDecodeBase32(lua_State *L) {
|
||||
return LuaBase32Impl(L, DecodeBase32);
|
||||
}
|
||||
|
||||
int LuaEncodeHex(lua_State *L) {
|
||||
char *p;
|
||||
size_t n;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue