Add EncodeHex() and DecodeHex() to Redbean

This commit is contained in:
Justine Tunney 2023-07-06 15:38:08 -07:00
parent 00acd81b2f
commit a186143f62
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
9 changed files with 97 additions and 13 deletions

View file

@ -721,14 +721,21 @@ FUNCTIONS
A granular. It can tell you if traffic originated from private
networks, ARIN, APNIC, DOD, etc.
DecodeLatin1(iso-8859-1:str) → utf-8:str
Turns ISO-8859-1 string into UTF-8.
EncodeHex(binary:str) → ascii:str
Turns binary into ASCII base-16 hexadecimal lowercase string.
DecodeHex(ascii:str) → binary:str
Turns ASCII base-16 hexadecimal byte string into binary string,
case-insensitively. Non-hex characters may not appear in string.
DecodeBase64(ascii:str) → binary:str
Turns ASCII into binary, in a permissive way that ignores
characters outside the base64 alphabet, such as whitespace. See
decodebase64.c.
DecodeLatin1(iso-8859-1:str) → utf-8:str
Turns ISO-8859-1 string into UTF-8.
EncodeBase64(binary:str) → ascii:str
Turns binary into ASCII. This can be used to create HTML data:
URIs that do things like embed a PNG file in a web page. See

View file

@ -42,8 +42,10 @@
#include "libc/runtime/runtime.h"
#include "libc/sock/sock.h"
#include "libc/stdio/rand.h"
#include "libc/str/highwayhash64.h"
#include "libc/str/str.h"
#include "libc/str/strwidth.h"
#include "libc/str/tab.internal.h"
#include "libc/sysv/consts/af.h"
#include "libc/sysv/consts/ipproto.h"
#include "libc/sysv/consts/o.h"
@ -355,6 +357,20 @@ int LuaBsf(lua_State *L) {
}
}
int LuaHighwayHash64(lua_State *L) {
long i;
size_t n;
uint64_t k[4];
const char *p;
p = luaL_checklstring(L, 1, &n);
k[0] = luaL_optinteger(L, 2, 0);
k[1] = luaL_optinteger(L, 3, 0);
k[2] = luaL_optinteger(L, 4, 0);
k[3] = luaL_optinteger(L, 5, 0);
lua_pushinteger(L, HighwayHash64(p, n, k));
return 1;
}
static int LuaHash(lua_State *L, uint32_t H(uint32_t, const void *, size_t)) {
long i;
size_t n;
@ -586,6 +602,42 @@ int LuaEncodeLatin1(lua_State *L) {
}
}
int LuaEncodeHex(lua_State *L) {
char *p;
size_t n;
const char *s;
luaL_Buffer buf;
s = luaL_checklstring(L, 1, &n);
p = luaL_buffinitsize(L, &buf, n * 2 + 1);
hexpcpy(p, s, n);
luaL_pushresultsize(&buf, n * 2);
return 1;
}
int LuaDecodeHex(lua_State *L) {
char *p;
int x, y;
size_t i, n;
const char *s;
luaL_Buffer buf;
s = luaL_checklstring(L, 1, &n);
if (n & 1) {
luaL_argerror(L, 1, "hex string length uneven");
__builtin_unreachable();
}
p = luaL_buffinitsize(L, &buf, n >> 1);
for (i = 0; i < n; i += 2) {
if ((x = kHexToInt[s[i + 0] & 255]) == -1 ||
(y = kHexToInt[s[i + 1] & 255]) == -1) {
luaL_argerror(L, 1, "hex string has non-hex character");
__builtin_unreachable();
}
p[i >> 1] = x << 4 | y;
}
luaL_pushresultsize(&buf, n >> 1);
return 1;
}
int LuaGetRandomBytes(lua_State *L) {
size_t n;
luaL_Buffer buf;

View file

@ -21,9 +21,11 @@ int LuaCrc32(lua_State *);
int LuaCrc32c(lua_State *);
int LuaDecimate(lua_State *);
int LuaDecodeBase64(lua_State *);
int LuaDecodeHex(lua_State *);
int LuaDecodeLatin1(lua_State *);
int LuaDeflate(lua_State *);
int LuaEncodeBase64(lua_State *);
int LuaEncodeHex(lua_State *);
int LuaEncodeLatin1(lua_State *);
int LuaEscapeFragment(lua_State *);
int LuaEscapeHost(lua_State *);
@ -50,6 +52,7 @@ int LuaGetRandomBytes(lua_State *);
int LuaGetTime(lua_State *);
int LuaHasControlCodes(lua_State *);
int LuaHex(lua_State *);
int LuaHighwayHash64(lua_State *);
int LuaIndentLines(lua_State *);
int LuaInflate(lua_State *);
int LuaIsAcceptableHost(lua_State *);

View file

@ -2556,8 +2556,7 @@ static char *ServeErrorImpl(unsigned code, const char *reason,
static char *ServeErrorWithPath(unsigned code, const char *reason,
const char *path, size_t pathlen) {
ERRORF("(srvr) server error: %d %s %`'.*s", code, reason,
pathlen, path);
ERRORF("(srvr) server error: %d %s %`'.*s", code, reason, pathlen, path);
return ServeErrorImpl(code, reason, NULL);
}
@ -5128,10 +5127,12 @@ static const luaL_Reg kLuaFuncs[] = {
{"Crc32c", LuaCrc32c}, //
{"Decimate", LuaDecimate}, //
{"DecodeBase64", LuaDecodeBase64}, //
{"DecodeHex", LuaDecodeHex}, //
{"DecodeJson", LuaDecodeJson}, //
{"DecodeLatin1", LuaDecodeLatin1}, //
{"Deflate", LuaDeflate}, //
{"EncodeBase64", LuaEncodeBase64}, //
{"EncodeHex", LuaEncodeHex}, //
{"EncodeJson", LuaEncodeJson}, //
{"EncodeLatin1", LuaEncodeLatin1}, //
{"EncodeLua", LuaEncodeLua}, //
@ -5193,6 +5194,7 @@ static const luaL_Reg kLuaFuncs[] = {
{"HasControlCodes", LuaHasControlCodes}, //
{"HasParam", LuaHasParam}, //
{"HidePath", LuaHidePath}, //
{"HighwayHash64", LuaHighwayHash64}, //
{"IndentLines", LuaIndentLines}, //
{"Inflate", LuaInflate}, //
{"IsAcceptableHost", LuaIsAcceptableHost}, //