mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-07-07 11:48: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
|
@ -737,8 +737,20 @@ FUNCTIONS
|
|||
Turns ASCII base-16 hexadecimal byte string into binary string,
|
||||
case-insensitively. Non-hex characters may not appear in string.
|
||||
|
||||
DecodeBase32(ascii:str[, alphabet:str]) → binary:str
|
||||
Turns ASCII into binary using provided alphabet. The default
|
||||
decoding uses Crockford's base32 alphabet in a permissive way
|
||||
that ignores whitespaces and dash ('-') and stops at the first
|
||||
character outside of the alphabet.
|
||||
|
||||
EncodeBase32(binary:str[, alphabet:str]) → ascii:str
|
||||
Turns binary into ASCII using provided alphabet (using Crockford's
|
||||
base32 encoding by default). Any alphabet that has a power of 2
|
||||
length (up to 128) may be supplied for encoding and decoding,
|
||||
which allows to provide alternative base32 encodings.
|
||||
|
||||
DecodeBase64(ascii:str) → binary:str
|
||||
Turns ASCII into binary, in a permissive way that ignores
|
||||
Turns ASCII into binary in a permissive way that ignores
|
||||
characters outside the base64 alphabet, such as whitespace. See
|
||||
decodebase64.c.
|
||||
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -20,10 +20,12 @@ int LuaCompress(lua_State *);
|
|||
int LuaCrc32(lua_State *);
|
||||
int LuaCrc32c(lua_State *);
|
||||
int LuaDecimate(lua_State *);
|
||||
int LuaDecodeBase32(lua_State *);
|
||||
int LuaDecodeBase64(lua_State *);
|
||||
int LuaDecodeHex(lua_State *);
|
||||
int LuaDecodeLatin1(lua_State *);
|
||||
int LuaDeflate(lua_State *);
|
||||
int LuaEncodeBase32(lua_State *);
|
||||
int LuaEncodeBase64(lua_State *);
|
||||
int LuaEncodeHex(lua_State *);
|
||||
int LuaEncodeLatin1(lua_State *);
|
||||
|
|
|
@ -5126,11 +5126,13 @@ static const luaL_Reg kLuaFuncs[] = {
|
|||
{"Crc32", LuaCrc32}, //
|
||||
{"Crc32c", LuaCrc32c}, //
|
||||
{"Decimate", LuaDecimate}, //
|
||||
{"DecodeBase32", LuaDecodeBase32}, //
|
||||
{"DecodeBase64", LuaDecodeBase64}, //
|
||||
{"DecodeHex", LuaDecodeHex}, //
|
||||
{"DecodeJson", LuaDecodeJson}, //
|
||||
{"DecodeLatin1", LuaDecodeLatin1}, //
|
||||
{"Deflate", LuaDeflate}, //
|
||||
{"EncodeBase32", LuaEncodeBase32}, //
|
||||
{"EncodeBase64", LuaEncodeBase64}, //
|
||||
{"EncodeHex", LuaEncodeHex}, //
|
||||
{"EncodeJson", LuaEncodeJson}, //
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue