bliss

KISS in Lua
git clone git://bvnf.space/bliss.git
Log | Files | Refs | README | LICENSE

commit ba752629e0421c9eba09c83823f3fcff0b542d31
parent be1c16f22b3e8d7a3beecff1955dd52c2d418a61
Author: aabacchus <ben@bvnf.space>
Date:   Tue, 13 Jun 2023 16:21:55 +0100

add BLAKE3 wrapper

Diffstat:
A.gitignore | 1+
MMakefile | 21++++++++++++++++++---
MREADME | 1+
Abliss/b3sum.c | 71+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mbliss/init.lua | 10++++++++--
Mtest.lua | 8++++++++
6 files changed, 107 insertions(+), 5 deletions(-)

diff --git a/.gitignore b/.gitignore @@ -0,0 +1 @@ +*.so diff --git a/Makefile b/Makefile @@ -2,9 +2,24 @@ PREFIX = /usr LUA_LMOD = $$(pkgconf --variable=INSTALL_LMOD lua) +LUA_CMOD = $$(pkgconf --variable=INSTALL_CMOD lua) -install: +CFLAGS = -Wall -Wextra -pedantic +LDFLAGS = -shared -fPIC +LIBS = $$(pkgconf --libs lua) + +all: bliss/b3sum.so + +install: all mkdir -p "$(DESTDIR)$(LUA_LMOD)/bliss" + mkdir -p "$(DESTDIR)$(LUA_CMOD)/bliss" + mkdir -p "$(DESTDIR)$(PREFIX)/bin" cp bliss/*.lua "$(DESTDIR)$(LUA_LMOD)/bliss/" - mkdir -p "$(DESTDIR)$(PREFIX)/bin" - cp main.lua "$(DESTDIR)$(PREFIX)/bin/bliss" + cp bliss/*.so "$(DESTDIR)$(LUA_CMOD)/bliss/" + cp main.lua "$(DESTDIR)$(PREFIX)/bin/bliss" + +bliss/b3sum.so: bliss/b3sum.c + $(CC) $(CFLAGS) -o $@ bliss/b3sum.c $(LDFLAGS) $(LIBS) -lblake3 + +clean: + rm -f bliss/b3sum.so diff --git a/README b/README @@ -30,3 +30,4 @@ Dependencies ------------ lua-posix library (https://github.com/luaposix/luaposix) +BLAKE3 C library (https://git.sr.ht/~mcf/b3sum) diff --git a/bliss/b3sum.c b/bliss/b3sum.c @@ -0,0 +1,71 @@ +/* Copyright 2023 phoebos + * Lua wrapper for the C blake3 library. + */ +#include <blake3.h> +#include <errno.h> +#include <lauxlib.h> +#include <lua.h> +#include <stdlib.h> +#include <string.h> + +int +l_init(lua_State *L) { + blake3_hasher *ctx = lua_newuserdata(L, sizeof(blake3_hasher)); + blake3_hasher_init(ctx); + return 1; +} + +int +l_update(lua_State *L) { + blake3_hasher *ctx = lua_touserdata(L, 1); + luaL_argcheck(L, ctx != NULL, 1, "hasher context expected"); + + size_t n; + const char *s = luaL_checklstring(L, 2, &n); + blake3_hasher_update(ctx, s, n); + return 0; +} + +int +l_finalize(lua_State *L) { + blake3_hasher *ctx = lua_touserdata(L, 1); + luaL_argcheck(L, ctx != NULL, 1, "hasher context expected"); + + /* default size is 32 if no second argument */ + int n = luaL_optinteger(L, 2, 32); + luaL_Buffer b; + luaL_buffinit(L, &b); + + unsigned char *out = malloc(n); + if (out == NULL) + return luaL_error(L, "malloc failed: %s", strerror(errno)); + + blake3_hasher_finalize(ctx, out, n); + + /* format raw data to a hex string */ + unsigned char *hexes = (unsigned char *)"0123456789abcdef"; + for (int i = 0; i < n; i++) { + unsigned char high = (out[i] & 0xf0) >> 0x04; + unsigned char low = (out[i] & 0x0f); + luaL_addchar(&b, hexes[high]); + luaL_addchar(&b, hexes[low]); + } + free(out); + + luaL_pushresult(&b); + + return 1; +} + +const struct luaL_Reg fns[] = { + {"init", l_init}, + {"update", l_update}, + {"finalize", l_finalize}, + {NULL, NULL}, +}; + +int +luaopen_bliss_b3sum(lua_State *L) { + luaL_newlib(L, fns); + return 1; +} diff --git a/bliss/init.lua b/bliss/init.lua @@ -2,13 +2,19 @@ local cwd = (...):gsub('%.init$', '') local M = {} +-- merge these into the toplevel bliss module local names = {'utils', 'search', 'list'} -for i = 1, #names do - local name = names[i] +for _, name in ipairs(names) do local t = require(cwd .. '.' .. name) for k, v in pairs(t) do M[k] = v end end +-- add these into submodules +names = {'b3sum'} +for _, name in ipairs(names) do + M[name] = require(cwd .. '.' .. name) +end + return M diff --git a/test.lua b/test.lua @@ -10,3 +10,11 @@ for k,v in pairs(env) do print(k,v) end end + +local ctx = bliss.b3sum.init() +bliss.b3sum.update(ctx, 'test\n') +local obs = bliss.b3sum.finalize(ctx) + +local exp = bliss.capture("echo test | b3sum") +local exp_ = bliss.split(exp[1], ' ')[1] +assert(exp_ == obs)