kee

Offline IOU signer with QR as transport
git clone https://holbrook.no/src/kee
Info | Log | Files | Refs | README | LICENSE

commit 894b67266b9fece28bfe3cac8424bf1bed7c02b3
parent c747b457c2a68516541c5f680225d28ea43ff719
Author: lash <dev@holbrook.no>
Date:   Fri, 29 Mar 2024 13:14:52 +0000

Remove aux sources

Diffstat:
M.gitignore | 1+
Msrc/Makefile | 17++++++++++-------
Msrc/aux/Makefile | 17++++++++++++++++-
Msrc/aux/README.md | 2++
Dsrc/aux/varint/.package | 2--
Dsrc/aux/varint/CMakeLists.txt | 4----
Dsrc/aux/varint/Makefile | 5-----
Dsrc/aux/varint/README.md | 59-----------------------------------------------------------
Dsrc/aux/varint/varint.c | 168-------------------------------------------------------------------------------
Dsrc/aux/varint/varint.h | 15---------------
Msrc/gtk/Makefile | 10++++++----
Msrc/gtk/kee-entry.c | 25++++++++++++++++++++++++-
12 files changed, 59 insertions(+), 266 deletions(-)

diff --git a/.gitignore b/.gitignore @@ -4,3 +4,4 @@ testdata_* *\~ **/test_* **/*.out +src/aux/include diff --git a/src/Makefile b/src/Makefile @@ -1,22 +1,25 @@ OBJS := $(patsubst %.c,%.o,$(filter-out main.c,$(wildcard *.c))) INCLUDES := `pkg-config --cflags libgcrypt lmdb libxdg-basedir` -CFLAGS += $(INCLUDES) -g3 -Wall -Iaux/varint -LIBS := `pkg-config --libs libgcrypt zlib lmdb libxdg-basedir` -lb64 +#CFLAGS += $(INCLUDES) -g3 -Wall -Iaux/varint +CFLAGS += $(INCLUDES) -g3 -Wall +LIBS := `pkg-config --libs libgcrypt zlib lmdb libxdg-basedir` -lb64 -lvarint LDFLAGS += $(LIBS) #all: aux resource $(OBJS) # $(CC) $(CFLAGS) main.c -o a.out $(OBJS) $(LDFLAGS) aux/varint/varint.o -all: aux $(OBJS) +#all: aux $(OBJS) +all: $(OBJS) %.o: %.c $(CC) $(CFLAGS) -c $< -o $@ $(LDFLAGS) -aux: - make -C aux +#aux: +# make -C aux clean: rm -vf *.o make -C gtk clean - make -C aux clean +# make -C aux clean -.PHONY: clean aux +#.PHONY: clean aux +.PHONY: clean diff --git a/src/aux/Makefile b/src/aux/Makefile @@ -1,5 +1,20 @@ -all: +# TODO: unify destinations + + +all: varint cmime + +varint: make -C varint +cmime: + mkdir -p libcmime/build + cd libcmime/build && cmake .. + make -C libcmime/build + mkdir -vp include + cd include && ln -vsf ../libcmime/src cmime + clean: make -C varint clean + make -C libcmime/build clean + +.PHOBY: clean diff --git a/src/aux/README.md b/src/aux/README.md @@ -3,3 +3,5 @@ add auxiliary source files here. e.g. implementation of varint TODO: varint header file + +TODO: mime interface def diff --git a/src/aux/varint/.package b/src/aux/varint/.package @@ -1,2 +0,0 @@ -file varint.h -file varint.c diff --git a/src/aux/varint/CMakeLists.txt b/src/aux/varint/CMakeLists.txt @@ -1,4 +0,0 @@ -add_library(varint varint.c) -set_property(TARGET varint PROPERTY C_VISIBILITY_PRESET default) -set_property(TARGET varint PROPERTY VISIBILITY_INLINES_HIDDEN OFF) -include_directories(.) diff --git a/src/aux/varint/Makefile b/src/aux/varint/Makefile @@ -1,5 +0,0 @@ -all: - $(CC) -c varint.c -o varint.o - -clean: - rm -vf *.o diff --git a/src/aux/varint/README.md b/src/aux/varint/README.md @@ -1,59 +0,0 @@ -# varint.c - -Read and write variable sized integers in C. -Compatible with Protobuf and Go varints. - -## API -```c -// read uint64 or int64 from data. returns the number of bytes read or returns -// the zero if there is not enough data or returns -1 if the data does not -// represent a valid varint. -int varint_read_u(const void *data, size_t len, uint64_t *x); -int varint_read_i(const void *data, size_t len, int64_t *x); - -// write a uint64 or int64 to data buffer. The data buffer must be at least -// 10 bytes in order to hold the maximum sized varint. Returns the number of -// bytes written. -int varint_write_u(void *data, uint64_t x); -int varint_write_i(void *data, int64_t x); -``` - -## Example - -```c -#include <stdio.h> -#include "varint.h" - -int main() { - char buf[100]; - int n; - int64_t i; - uint64_t u; - - // write and read a signed integer - i = -305; - n = varint_write_i(buf, i); - printf("wrote the value %lld (%d bytes)\n", (long long)i, n); - n = varint_read_i(buf, sizeof(buf), &i); - printf("read the value %lld (%d bytes)\n", (long long)i, n); - - - // write and read an unsigned integer - u = 102993; - n = varint_write_u(buf, u); - printf("wrote the value %llu (%d bytes)\n", (unsigned long long)u, n); - n = varint_read_u(buf, sizeof(buf), &u); - printf("read the value %lld (%d bytes)\n", (unsigned long long)u, n); -} -// output: -// wrote the value -305 (2 bytes) -// read the value -305 (2 bytes) -// wrote the value 102993 (3 bytes) -// read the value 102993 (3 bytes) -``` - -## Running tests - -```sh -$ cc -DVARINT_TEST varint.c && ./a.out -``` diff --git a/src/aux/varint/varint.c b/src/aux/varint/varint.c @@ -1,168 +0,0 @@ -// Copyright 2020 Joshua J Baker. All rights reserved. - -#include "varint.h" - -// varint_write_u writes a uint64 varint to data, which could be to 10 bytes. -// Make sure that you provide a data buffer that can take 10 bytes! -// Returns the number of bytes written. -int varint_write_u(void *data, uint64_t x) { - char *str = data; - uint64_t n = 0; - n+=x>=0x80; str[0]=x|0x80; x>>=7; - n+=x>=0x80; str[1]=x|0x80; x>>=7; - n+=x>=0x80; str[2]=x|0x80; x>>=7; - n+=x>=0x80; str[3]=x|0x80; x>>=7; - n+=x>=0x80; str[4]=x|0x80; x>>=7; - n+=x>=0x80; str[5]=x|0x80; x>>=7; - n+=x>=0x80; str[6]=x|0x80; x>>=7; - n+=x>=0x80; str[7]=x|0x80; x>>=7; - n+=x>=0x80; str[8]=x|0x80; x>>=7; - n+=x>=0x80; str[9]=x|0x80; x>>=7; - str[n] ^= 0x80; - return n+1; -} - -// varint_write_i writes a int64 varint to data, which could be to 10 bytes. -// Make sure that you provide a data buffer that can take 10 bytes! -// Returns the number of bytes written. -int varint_write_i(void *buf, int64_t x) { - uint64_t ux = (uint64_t)x << 1; - if (x < 0) { - ux = ~ux; - } - return varint_write_u(buf, ux); -} - -// varint_read_u reads a uint64 varint from data. -// Returns the number of bytes reads, or returns 0 if there's not enough data -// to complete the read, or returns -1 if the data buffer does not represent -// a valid uint64 varint. -int varint_read_u(const void *data, size_t len, uint64_t *x) { - const char *str = data; - uint64_t b; - *x = 0; - if (len==0) return 0; b=str[0]; *x|=(b&0x7f)<<(7*0); if (b<0x80) return 0+1; - if (len==1) return 0; b=str[1]; *x|=(b&0x7f)<<(7*1); if (b<0x80) return 1+1; - if (len==2) return 0; b=str[2]; *x|=(b&0x7f)<<(7*2); if (b<0x80) return 2+1; - if (len==3) return 0; b=str[3]; *x|=(b&0x7f)<<(7*3); if (b<0x80) return 3+1; - if (len==4) return 0; b=str[4]; *x|=(b&0x7f)<<(7*4); if (b<0x80) return 4+1; - if (len==5) return 0; b=str[5]; *x|=(b&0x7f)<<(7*5); if (b<0x80) return 5+1; - if (len==6) return 0; b=str[6]; *x|=(b&0x7f)<<(7*6); if (b<0x80) return 6+1; - if (len==7) return 0; b=str[7]; *x|=(b&0x7f)<<(7*7); if (b<0x80) return 7+1; - if (len==8) return 0; b=str[8]; *x|=(b&0x7f)<<(7*8); if (b<0x80) return 8+1; - if (len==9) return 0; b=str[9]; *x|=(b&0x7f)<<(7*9); if (b<0x80) return 9+1; - return -1; -} - -// varint_read_i reads a int64 varint from data. -// Returns the number of bytes reads, or returns 0 if there's not enough data -// to complete the read, or returns -1 if the data buffer does not represent -// a valid int64 varint. -int varint_read_i(const void *data, size_t len, int64_t *x) { - uint64_t ux; - int n = varint_read_u(data, len, &ux); - *x = (int64_t)(ux >> 1); - if ((ux&1) != 0) { - *x = ~*x; - } - return n; -} - -//============================================================================== -// TESTS -// $ cc -DVARINT_TEST buf.c && ./a.out # run tests -//============================================================================== -#ifdef VARINT_TEST -#include <math.h> -#include <time.h> -#include <assert.h> -#include <stdlib.h> -#include <stdio.h> -#include <string.h> - -static uint64_t rand_uint() { - return (((uint64_t)rand()<<33) | - ((uint64_t)rand()<<1) | - ((uint64_t)rand()<<0)) >> (rand()&63); -} - -static int64_t rand_int() { - return (int64_t)(rand_uint()) * ((rand()&1)?1:-1); -} - -static void rand_fill(char *data, int len) { - int i = 0; - for (; i < len-3; i += 3) { - uint32_t r = rand(); - data[i+0] = r; - data[i+1] = r >> 8; - data[i+2] = r >> 16; - } - for (; i < len; i++) { - data[i] = rand(); - } -} - -int main() { - printf("Running varint.c tests...\n"); - int seed = getenv("SEED")?atoi(getenv("SEED")):time(NULL); - printf("seed=%d\n", seed); - srand(seed); - int N = 500000; - uint64_t *uints = malloc(sizeof(uint64_t)*N); - char *data = malloc(10*N); - assert(uints && data); - printf("[RANDOMS] "); - for (int h = 0; h < 50; h++) { - for (int i = 0, n = 0; i < N; i++) { - uints[i] = rand_uint(); - int nn = varint_write_u(data+n, uints[i]); - assert(nn > 0); - n += nn; - } - for (int i = 0, n = 0; i < N; i++) { - uint64_t x; - int nn = varint_read_u(data+n, 10*N-n, &x); - assert(nn > 0); - assert(x == uints[i]); - n += nn; - } - int64_t *ints = (int64_t*)uints; - for (int i = 0, n = 0; i < N; i++) { - ints[i] = rand_int(); - int nn = varint_write_i(data+n, ints[i]); - assert(nn > 0); - n += nn; - } - for (int i = 0, n = 0; i < N; i++) { - int64_t x; - int nn = varint_read_i(data+n, 10*N-n, &x); - assert(nn > 0); - assert(x == ints[i]); - n += nn; - } - printf("."); - fflush(stdout); - } - printf("\n"); - printf("[FUZZING] "); - int sz = 10*N/2; - for (int h = 0; h < 50; h++) { - rand_fill(data, sz); - for (int i = 0; i < sz; i++) { - int64_t x; - varint_read_i(data+i, sz-i, &x); - } - for (int i = 0; i < sz; i++) { - uint64_t x; - varint_read_u(data+i, sz-i, &x); - } - printf("."); - fflush(stdout); - } - printf("\n"); - free(uints); - free(data); - printf("PASSED\n"); -} -#endif diff --git a/src/aux/varint/varint.h b/src/aux/varint/varint.h @@ -1,15 +0,0 @@ -// Copyright 2020 Joshua J Baker. All rights reserved. - -#ifndef VARINT_H -#define VARINT_H - -#include <stdint.h> -#include <stddef.h> - -int varint_read_u(const void *data, size_t len, uint64_t *x); -int varint_read_i(const void *data, size_t len, int64_t *x); - -int varint_write_u(void *data, uint64_t x); -int varint_write_i(void *data, int64_t x); - -#endif diff --git a/src/gtk/Makefile b/src/gtk/Makefile @@ -1,8 +1,10 @@ OBJS := $(patsubst %.c,%.o,$(filter-out main.c,$(wildcard *.c))) -LINKOBJS := $(wildcard ../*.o) $(OBJS) ../aux/varint/varint.o -INCLUDES := -I.. -CFLAGS += `pkg-config --cflags gtk4 gstreamer-1.0` $(INCLUDES) -g3 -Wall -LIBS := `pkg-config --libs gtk4 zlib lmdb libgcrypt libxdg-basedir gstreamer-1.0` -lb64 +LINKOBJS := $(wildcard ../*.o) $(OBJS) +INCLUDES := -I.. -I../aux/include +#CFLAGS += `pkg-config --cflags gtk4 gstreamer-1.0 libcmime` $(INCLUDES) -g3 -Wall +CFLAGS += `pkg-config --cflags gtk4 gstreamer-1.0 ` $(INCLUDES) -g3 -Wall +#LIBS := `pkg-config --libs gtk4 zlib lmdb libgcrypt libxdg-basedir gstreamer-1.0 libcmime` -lb64 +LIBS := `pkg-config --libs gtk4 zlib lmdb libgcrypt libxdg-basedir gstreamer-1.0` -lb64 -lcmime -lvarint LDFLAGS += $(LIBS) all: resource $(OBJS) diff --git a/src/gtk/kee-entry.c b/src/gtk/kee-entry.c @@ -1,6 +1,8 @@ #include <glib-object.h> #include <gtk/gtk.h> +#include "cmime.h" + #include "kee-entry.h" #include "db.h" #include "err.h" @@ -27,6 +29,7 @@ struct _KeeEntry { char *alice; char *bob; char *body; + char *subject; char decimals; struct Cadiz *resolver; }; @@ -54,6 +57,7 @@ static void kee_entry_init(KeeEntry *o) { o->unit_of_account = (char*)((o->mem)+64); o->state = 2; o->resolver = NULL; + o->subject = NULL; } KeeEntry* kee_entry_new(struct Cadiz *resolver) { @@ -68,12 +72,15 @@ int kee_entry_load(KeeEntry *o, struct db_ctx *db, const char *id) { } /// \todo enum state +/// \todo separate message rsolve and parse in separate function int kee_entry_deserialize(KeeEntry *o, const char *key, size_t key_len, const char *data, size_t data_len) { int r; struct kee_import im; size_t out_len; + size_t t; size_t remaining; char *p; + CMimeMessage_T *msg; o->state = 1; import_init(&im, data, data_len); @@ -104,7 +111,21 @@ int kee_entry_deserialize(KeeEntry *o, const char *key, size_t key_len, const ch r = import_read(&im, o->body, out_len); if (o->resolver) { + t = out_len; r = cadiz_resolve(o->resolver, o->body, o->body, &out_len); + if (!r) { + msg = cmime_message_new(); + o->subject = o->body + out_len; + r = cmime_message_from_string(&msg, o->body, 0); + if (!r) { + o->subject = cmime_message_get_subject(msg); + o->subject = cmime_string_strip(o->subject); + out_len += strlen(o->subject) + 1; + } + remaining -= out_len; + } else { + remaining -= t; + } } o->state = 0; @@ -131,7 +152,9 @@ void kee_entry_apply_list_item_widget(KeeEntry *o) { l = 129; bin_to_hex((unsigned char*)o->bob, 64, bob_hex, &l); sprintf(o->header, "[%s] %s -> %s", o->unit_of_account, alice_hex, bob_hex); - widget = gtk_label_new(o->header); + //widget = gtk_label_new(o->header); + //gtk_box_append(GTK_BOX(o), widget); + widget = gtk_label_new(o->subject); gtk_box_append(GTK_BOX(o), widget); return; }