Add EncodeBase32() to Redbean (#856)

This commit is contained in:
Paul Kulchenko 2023-10-11 20:06:20 -07:00 committed by GitHub
parent 3b086af91b
commit 6e4b9b6515
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 221 additions and 1 deletions

View file

@ -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;