kee

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

commit 52d73eea9a069cff8bf48f9877b98cfd41b2ce9a
parent 580ca5f81c934b9e252302a6162c08f7ca28adf0
Author: lash <dev@holbrook.no>
Date:   Fri, 31 May 2024 21:01:20 +0100

Move all small code objects aux to liblash

Diffstat:
M.gitignore | 3+++
Msrc/Makefile | 4++--
Msrc/aux/Makefile | 14+++++++++-----
Asrc/aux/liblash/Makefile | 28++++++++++++++++++++++++++++
Asrc/aux/liblash/src/Makefile | 46++++++++++++++++++++++++++++++++++++++++++++++
Asrc/aux/liblash/src/endian/Makefile | 23+++++++++++++++++++++++
Asrc/aux/liblash/src/endian/endian.c | 47+++++++++++++++++++++++++++++++++++++++++++++++
Asrc/aux/liblash/src/endian/endian.h | 47+++++++++++++++++++++++++++++++++++++++++++++++
Asrc/aux/liblash/src/endian/strip.c | 50++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/aux/liblash/src/endian/strip.h | 25+++++++++++++++++++++++++
Asrc/aux/liblash/src/hex/Makefile | 29+++++++++++++++++++++++++++++
Asrc/aux/liblash/src/hex/hex.c | 92+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/aux/liblash/src/hex/hex.h | 10++++++++++
Asrc/aux/liblash/src/llog/Makefile | 28++++++++++++++++++++++++++++
Asrc/aux/liblash/src/llog/hex.c | 21+++++++++++++++++++++
Rsrc/aux/llog/llog.c -> src/aux/liblash/src/llog/llog.c | 0
Rsrc/aux/llog/llog.h -> src/aux/liblash/src/llog/llog.h | 0
Asrc/aux/liblash/src/rerr/Makefile | 27+++++++++++++++++++++++++++
Asrc/aux/liblash/src/rerr/rerr.c | 112+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Rsrc/aux/rerr/rerr.h -> src/aux/liblash/src/rerr/rerr.h | 0
Dsrc/aux/llog/Makefile | 20--------------------
Dsrc/aux/rerr/Makefile | 19-------------------
Dsrc/aux/rerr/rerr.c | 108-------------------------------------------------------------------------------
Msrc/cadir.c | 10++++------
Msrc/gpg.c | 19++++---------------
Msrc/gtk/Makefile | 5++---
Msrc/gtk/kee-entry.c | 9++++-----
Msrc/gtk/kee-key.c | 5+----
Msrc/gtk/tests/Makefile | 5+++--
Dsrc/hex.c | 64----------------------------------------------------------------
Dsrc/hex.h | 23-----------------------
Msrc/tests/Makefile | 9+++++----
Msrc/tests/content.c | 2+-
Msrc/tests/ledger.c | 6+++---
34 files changed, 626 insertions(+), 284 deletions(-)

diff --git a/.gitignore b/.gitignore @@ -9,5 +9,8 @@ src/aux/**/.gitignore src/gtk/resources.c src/gtk/beamenu_defs.* src/tests/test_* +src/gtk/tests/test_* src/asn1/schema_entry_asn1_tab.c src/asn1/generate_asn1 +src/aux/include +src/aux/lib diff --git a/src/Makefile b/src/Makefile @@ -1,7 +1,7 @@ OBJS := $(patsubst %.c,%.o,$(filter-out main.c,$(wildcard *.c))) -INCLUDES := `pkg-config --cflags libgcrypt lmdb libxdg-basedir libqrencode zbar` +INCLUDES := `pkg-config --cflags libgcrypt lmdb libxdg-basedir libqrencode zbar` -Iaux/include CFLAGS += $(INCLUDES) -Wall -LIBS := `pkg-config --libs libgcrypt zlib lmdb libxdg-basedir libqrencode zbar` -lb64 -llash +LIBS := `pkg-config --libs libgcrypt zlib lmdb libxdg-basedir libqrencode zbar` -lb64 -llash -Lsrc/aux/lib LDFLAGS += $(LIBS) #all: aux resource $(OBJS) diff --git a/src/aux/Makefile b/src/aux/Makefile @@ -1,6 +1,7 @@ -all: - make -C llog - make -C rerr +CFLAGS += -I./include + +all: prep + make -C liblash install DESTDIR=`realpath .` make -C beamenu make -C beamenu gen @@ -8,8 +9,11 @@ local: -make -f Makefile.local clean: - make -C llog clean - make -C rerr clean + make -C liblash clean make -C beamenu clean .PHONY: clean + +prep: + mkdir -vp lib + mkdir -vp include diff --git a/src/aux/liblash/Makefile b/src/aux/liblash/Makefile @@ -0,0 +1,28 @@ +VERSION = 0.0.1 +DESTDIR := /usr/local +export DESTDIR +export VERSION + +all: + $(MAKE) -C src all + +shared: + $(MAKE) -C src shared +src: + $(MAKE) -C src + +test: src + $(MAKE) -C src test + +clean: + $(MAKE) -C src clean + +archive: + git archive --format=tar.gz HEAD > liblash-$(VERSION).tar.gz + +install: + mkdir -vp $(DESTDIR)/lib + mkdir -vp $(DESTDIR)/include + $(MAKE) -e -C src install + +.PHONY: clean diff --git a/src/aux/liblash/src/Makefile b/src/aux/liblash/src/Makefile @@ -0,0 +1,46 @@ +OBJS := $(patsubst %.c,%.o,$(wildcard *.c)) +CFLAGS += -Wall -Werror + +all: + make -C endian all + make -C hex all + make -C llog all + make -C rerr all + #make -C case all + +clean: + make -C endian clean + make -C hex clean + make -C llog clean + make -C rerr clean + #make -C case clean + +test: all + make -C endian test + make -C hex test + make -C llog test + make -C rerr test + #make -C case test + +shared: all + make -C endian shared + make -C hex shared + make -C llog shared + make -C rerr shared + $(CC) $(CFLAGS) -shared -o liblash.so endian/strip.so.o endian/endian.so.o hex/hex.so.o llog/llog.so.o rerr/rerr.so.o + +install: shared + cat -v endian/*.h >> $(DESTDIR)/include/lash.h + install -m0644 -v endian/*.h -t $(DESTDIR)/include + cat -v hex/*.h >> $(DESTDIR)/include/lash.h + install -m0644 -v hex/*.h -t $(DESTDIR)/include + cat -v llog/*.h >> $(DESTDIR)/include/lash.h + install -m0644 -v llog/*.h -t $(DESTDIR)/include + cat -v rerr/*.h >> $(DESTDIR)/include/lash.h + install -m0644 -v rerr/*.h -t $(DESTDIR)/include + #cp -v liblash.so $(DESTDIR)/lib/ + install -m0644 -v liblash.so -t $(DESTDIR)/lib + cd $(DESTDIR)/lib && ln -svf liblash.so liblash.so.$(VERSION) + + +.PHONY: clean diff --git a/src/aux/liblash/src/endian/Makefile b/src/aux/liblash/src/endian/Makefile @@ -0,0 +1,23 @@ +OBJS := $(patsubst %.c,%.o,$(wildcard *.c)) +SOBJS := $(patsubst %.c,%.so.o,$(wildcard *.c)) +CFLAGS += -Wall -Werror +VERSION = 0.0.1 + + +all: $(OBJS) + +%.o: %.c + $(CC) $(CFLAGS) -c $< -o $@ + +clean: + rm -vf *.o + +%.so.o: %.c + $(CC) $(CFLAGS) -c $< -o $@ -fpic + +shared: $(SOBJS) + +test: all + +.PHONY: clean + diff --git a/src/aux/liblash/src/endian/endian.c b/src/aux/liblash/src/endian/endian.c @@ -0,0 +1,47 @@ +#include "endian.h" + +int is_le() { + unsigned short s = 42; + return *((unsigned char*)&s) == 42; +} + + +void flip_endian(int l, void *v) { + int i; + char t; + char *ne; + char *p; + + p = (char*)v; + ne = p+(l-1); + for (i = 0; i < l/2; i++) { + t = *(p+i); + *(p+i) = *(ne-i); + *(ne-i) = t; + } +} + +int to_endian(char direction, int l, void *n) { + union le un; + + if (l == 1 || is_le() == direction) { + return 0; + } + switch(l) { + case sizeof(long long): + un.ll = (long long*)n; + break; + case sizeof(int): + un.i = (int*)n; + break; + case sizeof(short): + un.s = (short*)n; + break; + default: + return 1; + } + flip_endian(l, un.c); + + return 0; +} + diff --git a/src/aux/liblash/src/endian/endian.h b/src/aux/liblash/src/endian/endian.h @@ -0,0 +1,47 @@ +#ifndef LASH_ENDIAN_H_ +#define LASH_ENDIAN_H_ + +#define TO_ENDIAN_BIG 0 +#define TO_ENDIAN_LITTLE 1 + +/** + * + * \brief Encapsulats all suppoerted integer lengths for endian conversion. + * + */ +union le { + short *s; + int *i; + long long *ll; + unsigned char *c; +}; + +/* + * Return true (1) if system is little-endian. + */ +int is_le(); +/** + * Convert to specified endian order. + * + * The integer data in \c n is changed in-place. + * + * If \c direction is same as system endianness, or \c l==1, no action is taken. + * + * \param direction 0 for big-endian, 1 for little-endian. + * \param l Length of integer \c n in bytes. + * \param n Integer data. + * \return 1 if \c l is invalid byte size, 0 (success) otherwise. + * + */ +int to_endian(char direction, int l, void *n); +/** + * Change endian order of given number. + * + * The integer data in \c n is changed in-place. + * + * \param l Length of integer \c in bytes. + * \param v Integer data + */ +void flip_endian(int l, void *v); + +#endif // LASH_ENDIAN_H_ diff --git a/src/aux/liblash/src/endian/strip.c b/src/aux/liblash/src/endian/strip.c @@ -0,0 +1,50 @@ +#include <stddef.h> + +char* strip_be(char *value, size_t *len) { + int i; + char *p; + + p = value; + for (i = 0; i < *len; i++) { + if (*p & 0xff) { + break; + } + p++; + } + *len -= i; + if (!*len) { + *len = 1; + p--; + } + return p; +} + +int strap_be(const char *in, size_t in_len, char *out, size_t out_len) { + int i; + int c; + char *p; + char mask; + + if (in_len > out_len) { + return 1; + } + if (in_len == 0) { + return 1; + } + + mask = 0; + if (*in & 0x80) { + mask = 0xff; + } + for (i = 0; i < 4; i++) { + *(out+i) = mask; + } + + c = out_len - in_len; + p = out + c; + for (i = 0; i < in_len; i++) { + *(p+i) = *(in+i); + } + + return 0; +} diff --git a/src/aux/liblash/src/endian/strip.h b/src/aux/liblash/src/endian/strip.h @@ -0,0 +1,25 @@ +#ifndef LASH_BYTES_H_ +#define LASH_BYTES_H_ + +/** + * strip zero value zeros from a big-endian integer array + * + * \param value integer data to strip zeros from. + * \parmm len pointer to length of input integer data. Length of stripped integer will be written here. + * \return pointer to position in buffer containing the stripped integer data. + * + */ +char* strip_be(char *value, size_t *len); +/** + * expand a truncated signed big-endian integer to full bitsize + * + * \param in integer data to expand. + * \param in_len length of input integer. + * \param out output buffer where expanded integer will be written. + * \param out_len output buffer capacity. + * \return 0 if successfully written, 1 on any failure. + * + */ +int strap_be(const char *in, size_t in_len, char *out, size_t out_len); + +#endif diff --git a/src/aux/liblash/src/hex/Makefile b/src/aux/liblash/src/hex/Makefile @@ -0,0 +1,29 @@ +OBJS := $(patsubst %.c,%.o,$(filter-out test.c,$(wildcard *.c))) +SOBJS := $(patsubst %.c,%.so.o,$(filter-out test.c,$(wildcard *.c))) +INCLUDES := -I. +CFLAGS += $(INCLUDES) +VERSION = 0.0.1 +CFLAGS += -Wall -Werror + +all: $(OBJS) + +test: all + $(CC) $(CFLAGS) test.c hex.o -o test.out $(LDFLAGS) + +%.o: %.c + $(CC) $(CFLAGS) -c $< -o $@ $(LDFLAGS) + +clean: + rm -vf *.o + rm -vf *.out + rm -vf *.tar.gz + +archive: + git archive --format=tar.gz HEAD -o hex-$(VERSION).tar.gz + +%.so.o: %.c + $(CC) $(CFLAGS) -c $< -o $@ -fpic + +shared: $(SOBJS) + + diff --git a/src/aux/liblash/src/hex/hex.c b/src/aux/liblash/src/hex/hex.c @@ -0,0 +1,92 @@ +char *_x = "0123456789abcdef"; + +void b2c(char in, char *out) { + int v; + + v = (in & 0xf0) >> 4; + *out = *(_x+v); + v = in & 0x0f; + *(out+1) = *(_x+v); +} + +void b2h(const unsigned char *in, int l, unsigned char *out) { + int i; + char *p; + + p = (char*)out; + for (i = 0; i < l; i++) { + b2c(*(in+i), p); + p += 2; + } + *p = 0; +} + +char* c2h(char in, char *out) { + char i; + i = in & 0x0f; + *(out+1) = *(_x+i); + in >>= 4; + i = in & 0x0f; + *out = *(_x+i); + return out; +} + +int n2b(const char in, char *out) { + if (out == 0x0) { + return 1; + } + + if (in >= '0' && in <= '9') { + *out = in - '0'; + } else if (in >= 'A' && in <= 'F') { + *out = in - 'A' + 10; + } else if (in >= 'a' && in <= 'f') { + *out = in - 'a' + 10; + } else { + return 1; + } + + return 0; +} + +int h2b(const char *in, unsigned char *out) { + int r; + int i; + char b1; + char b2; + char *po; + char *pi; + + if (in == 0x0 || *in == '\0' || out == 0x0) { + return 0; + } + + i = 0; + po = (char*)out; + pi = (char*)in; + while (1) { + if (*pi == 0) { + break; + } + r = n2b(*pi, &b1); + if (r) { + return 0; + } + pi++; + if (*pi == 0) { // we dont allow cut strings + return 0; + } + r = n2b(*pi, &b2); + if (r) { + return 0; + } + pi++; + + //(*out)[i] = (b1 << 4) | b2; + *po = (b1 << 4) | b2; + po++; + i++; + } + return i; + +} diff --git a/src/aux/liblash/src/hex/hex.h b/src/aux/liblash/src/hex/hex.h @@ -0,0 +1,10 @@ +#ifndef LASH_HEX_H_ +#define LASH_HEX_H_ + +void b2c(char in, char *out); +int c2b(const char in, char *out); +int h2b(const char *in, unsigned char *out); +void b2h(const unsigned char *in, int l, unsigned char *out); +char* c2h(char in, char *out); + +#endif diff --git a/src/aux/liblash/src/llog/Makefile b/src/aux/liblash/src/llog/Makefile @@ -0,0 +1,28 @@ +OBJS := $(patsubst %.c,%.o,$(filter-out test.c,$(wildcard *.c))) +SOBJS := $(patsubst %.c,%.so.o,$(filter-out test.c,$(wildcard *.c))) +INCLUDES := -I. +CFLAGS += $(INCLUDES) +VERSION = 0.0.1 + +all: $(OBJS) + +test: all + $(CC) $(CFLAGS) test.c llog.o hex.o -o test.out $(LDFLAGS) + +%.o: %.c + $(CC) $(CFLAGS) -c $< -o $@ $(LDFLAGS) + +clean: + rm -vf *.o + rm -vf *.out + rm -vf *.tar.gz + +archive: + git archive --format=tar.gz HEAD -o llog-$(VERSION).tar.gz + +%.so.o: %.c + $(CC) $(CFLAGS) -c $< -o $@ -fpic + +shared: $(SOBJS) + + diff --git a/src/aux/liblash/src/llog/hex.c b/src/aux/liblash/src/llog/hex.c @@ -0,0 +1,21 @@ +#include <stdio.h> + +char *_x = "0123456789abcdef"; + +void b2h(const unsigned char *data, int l, unsigned char *zHex) { + unsigned int i; + + for (i = 0; i < l; i++) { + sprintf((char*)zHex+(i*2), "%02x", *(data+i)); + } +} + +char* c2h(char in, char *out) { + char i; + i = in & 0x0f; + *(out+1) = *(_x+i); + in >>= 4; + i = in & 0x0f; + *out = *(_x+i); + return out; +} diff --git a/src/aux/llog/llog.c b/src/aux/liblash/src/llog/llog.c diff --git a/src/aux/llog/llog.h b/src/aux/liblash/src/llog/llog.h diff --git a/src/aux/liblash/src/rerr/Makefile b/src/aux/liblash/src/rerr/Makefile @@ -0,0 +1,27 @@ +OBJS := $(patsubst %.c,%.o,$(filter-out test.c,$(wildcard *.c))) +SOBJS := $(patsubst %.c,%.so.o,$(filter-out test.c,$(wildcard *.c))) +INCLUDES := -I. +CFLAGS += $(INCLUDES) -DRERR -DRERR_N_PFX=2 +VERSION = 0.0.1 + +all: $(OBJS) + +test: all + $(CC) $(CFLAGS) test.c rerr.o -o test.out $(LDFLAGS) + +%.o: %.c + $(CC) $(CFLAGS) -c $< -o $@ $(LDFLAGS) + +clean: + rm -vf *.o + rm -vf *.out + +archive: + git archive --format=tar.gz HEAD -o rerr-$(VERSION).tar.gz + +%.so.o: %.c + $(CC) $(CFLAGS) -c $< -o $@ -fpic + +shared: $(SOBJS) + + diff --git a/src/aux/liblash/src/rerr/rerr.c b/src/aux/liblash/src/rerr/rerr.c @@ -0,0 +1,112 @@ +#include "rerr.h" + +#ifdef RERR +static char** rerr[RERR_N_PFX + 1]; +static char* rerr_pfx[RERR_N_PFX + 1]; +char *rerr_base[3] = { + "OK", + "FAIL", + "UNSUPPORTED", +}; +#endif + +void rerr_init() { +#ifdef RERR + int i; + + for (i = 1; i < RERR_N_PFX + 1; i++) { + rerr[i] = 0x0; + rerr_pfx[i] = 0x0; + } + rerr[0] = rerr_base; + rerr_pfx[0] = 0x0; +#endif +} + +void rerr_register(int pfx, char *label, void *start) { +#ifdef RERR + pfx >>= 8; + rerr_pfx[pfx] = label; + rerr[pfx] = start; +#endif +} + +#ifdef RERR +static void splitcode(int code, short *k, char *v) { + *v = code & 0xff; + *k = 0; + if (code > 0xff) { + *k = ((code >> 8) & 0xffff); + } +} + +static char *strv(short k, char v) { + return (char*)(*(rerr[k]+v)); +} +#endif + +char *rerrpfx(int code) { +#ifdef RERR + short k; + char v; + splitcode(code, &k, &v); + return rerr_pfx[k]; +#else + return ""; +#endif +} + +char *rerrstrv(int code) { +#ifdef RERR + short k; + char v; + splitcode(code, &k, &v); + return strv(k, v); +#endif +} + +char* rerrstr(int code, char *buf) { +#ifdef RERR + short k; + char v; + char *src; + char *dst; + + splitcode(code, &k, &v); + + dst = buf; + src = (char*)rerr_pfx[k]; + if (src) { + while (1) { + if (*src == 0) { + break; + } + *dst = *src; + src++; + dst++; + } + *dst = ':'; + dst++; + *dst = ' '; + dst++; + } + + src = strv(k, v); + //src = (char*)(*(rerr[k]+v)); + while (1) { + if (*src == 0) { + break; + } + *dst = *src; + src++; + dst++; + } + + *dst = 0; + + return buf; +#else + return 0; +#endif +} + diff --git a/src/aux/rerr/rerr.h b/src/aux/liblash/src/rerr/rerr.h diff --git a/src/aux/llog/Makefile b/src/aux/llog/Makefile @@ -1,20 +0,0 @@ -OBJS := $(patsubst %.c,%.o,$(filter-out test.c,$(wildcard *.c))) -INCLUDES := -I. -CFLAGS += $(INCLUDES) -VERSION = 0.0.1 - -all: $(OBJS) - -test: all - $(CC) $(CFLAGS) test.c llog.o hex.o -o test.out $(LDFLAGS) - -%.o: %.c - $(CC) $(CFLAGS) -c $< -o $@ $(LDFLAGS) - -clean: - rm -vf *.o - rm -vf *.out - rm -vf *.tar.gz - -archive: - git archive --format=tar.gz HEAD -o llog-$(VERSION).tar.gz diff --git a/src/aux/rerr/Makefile b/src/aux/rerr/Makefile @@ -1,19 +0,0 @@ -OBJS := $(patsubst %.c,%.o,$(filter-out test.c,$(wildcard *.c))) -INCLUDES := -I. -CFLAGS += $(INCLUDES) -DRERR -DRERR_N_PFX=2 -VERSION = 0.0.1 - -all: $(OBJS) - -test: all - $(CC) $(CFLAGS) test.c rerr.o -o test.out $(LDFLAGS) - -%.o: %.c - $(CC) $(CFLAGS) -c $< -o $@ $(LDFLAGS) - -clean: - rm -vf *.o - rm -vf *.out - -archive: - git archive --format=tar.gz HEAD -o rerr-$(VERSION).tar.gz diff --git a/src/aux/rerr/rerr.c b/src/aux/rerr/rerr.c @@ -1,108 +0,0 @@ -#include "rerr.h" - -#ifdef RERR -static char** rerr[RERR_N_PFX + 1]; -static char* rerr_pfx[RERR_N_PFX + 1]; -char *rerr_base[3] = { - "OK", - "FAIL", - "UNSUPPORTED", -}; -#endif - -void rerr_init() { -#ifdef RERR - int i; - - for (i = 1; i < RERR_N_PFX + 1; i++) { - rerr[i] = 0x0; - rerr_pfx[i] = 0x0; - } - rerr[0] = rerr_base; - rerr_pfx[0] = 0x0; -#endif -} - -void rerr_register(int pfx, char *label, void *start) { -#ifdef RERR - pfx >>= 8; - rerr_pfx[pfx] = label; - rerr[pfx] = start; -#endif -} - -#ifdef RERR -static void splitcode(int code, short *k, char *v) { - *v = code & 0xff; - *k = 0; - if (code > 0xff) { - *k = ((code >> 8) & 0xffff); - } -} - -static char *strv(short k, char v) { - return (char*)(*(rerr[k]+v)); -} -#endif - -char *rerrpfx(int code) { - short k; - char v; - splitcode(code, &k, &v); - return rerr_pfx[k]; -} - -char *rerrstrv(int code) { -#ifdef RERR - short k; - char v; - splitcode(code, &k, &v); - return strv(k, v); -#endif -} - -char* rerrstr(int code, char *buf) { -#ifdef RERR - short k; - char v; - char *src; - char *dst; - - splitcode(code, &k, &v); - - dst = buf; - src = (char*)rerr_pfx[k]; - if (src) { - while (1) { - if (*src == 0) { - break; - } - *dst = *src; - src++; - dst++; - } - *dst = ':'; - dst++; - *dst = ' '; - dst++; - } - - src = strv(k, v); - //src = (char*)(*(rerr[k]+v)); - while (1) { - if (*src == 0) { - break; - } - *dst = *src; - src++; - dst++; - } - - *dst = 0; - - return buf; -#else - return 0; -#endif -} - diff --git a/src/cadir.c b/src/cadir.c @@ -1,9 +1,9 @@ #include <string.h> #include <fcntl.h> #include <unistd.h> +#include <hex.h> #include "cadiz.h" -#include "hex.h" /// \todo replace with fadfada @@ -29,11 +29,9 @@ int cadiz_resolve(Cadiz *cadiz, const char *key, char *out, size_t *out_len) { p = path + c + 1; l = 129; - r = bin_to_hex((unsigned char*)key, 64, (unsigned char*)p, &l); - if (r) { - return 1; - } - p += l; + //r = bin_to_hex((unsigned char*)key, 64, (unsigned char*)p, &l); + b2h((unsigned char*)key, 64, (unsigned char*)p); + p += 128; *p = 0; fd = open(path, O_RDONLY); diff --git a/src/gpg.c b/src/gpg.c @@ -198,18 +198,12 @@ static int key_apply_public(struct gpg_store *gpg, gcry_sexp_t key) { } static char *key_filename(struct gpg_store *gpg, char *path) { - int r; char *p; - size_t c; strcpy((char*)path, gpg->path); p = (char*)path + strlen((char*)path); - c = 41; - r = bin_to_hex((unsigned char*)gpg->fingerprint, 20, (unsigned char*)p, &c); - if (r) { - return NULL; - } + b2h((unsigned char*)gpg->fingerprint, 20, (unsigned char*)p); return path; } @@ -413,7 +407,6 @@ int gpg_key_create(struct gpg_store *gpg, const char *passphrase) { /// \todo add key unload to destroy memory int gpg_key_load(struct gpg_store *gpg, const char *passphrase, enum gpg_find_mode_e mode, const void *criteria) { int r; - size_t c; char *p; char path[1024]; @@ -430,11 +423,7 @@ int gpg_key_load(struct gpg_store *gpg, const char *passphrase, enum gpg_find_mo case KEE_GPG_FIND_FINGERPRINT: strcpy(path, gpg->path); p = path + strlen(path); - c = 41; - r = bin_to_hex((const unsigned char*)criteria, FINGERPRINT_LENGTH, (unsigned char*)p, &c); - if (r) { - return debug_logerr(LLOG_ERROR, ERR_KEYFAIL, NULL); - } + b2h((const unsigned char*)criteria, FINGERPRINT_LENGTH, (unsigned char*)p); r = key_from_file(&gpg->k, path, passphrase); if (r) { return debug_logerr(LLOG_WARNING, ERR_KEYFAIL, NULL); @@ -560,7 +549,7 @@ int gpg_store_check(struct gpg_store *gpg, const char *passphrase) { //gcry_pk_get_keygrip(k, fingerprint); gcry_pk_get_keygrip(k, (unsigned char*)gpg->fingerprint); //bin_to_hex(fingerprint, 20, (unsigned char*)gpg->fingerprint, &fingerprint_len); - bin_to_hex((unsigned char*)gpg->fingerprint, 20, (unsigned char*)fingerprint, &fingerprint_len); + b2h((unsigned char*)gpg->fingerprint, 20, (unsigned char*)fingerprint); char ppp[4096]; //sprintf(ppp, "created key %s from %s", m_fingerprint, pp); sprintf(ppp, "created key %s from %s", fingerprint, pp); @@ -569,7 +558,7 @@ int gpg_store_check(struct gpg_store *gpg, const char *passphrase) { //gcry_pk_get_keygrip(k, fingerprint); gcry_pk_get_keygrip(k, (unsigned char*)gpg->fingerprint); //bin_to_hex(fingerprint, 20, (unsigned char*)gpg->fingerprint, &fingerprint_len); - bin_to_hex((unsigned char*)gpg->fingerprint, 20, (unsigned char*)fingerprint, &fingerprint_len); + b2h((unsigned char*)gpg->fingerprint, 20, (unsigned char*)fingerprint); char pp[4096]; //sprintf(pp, "found key %s in %s", (unsigned char*)m_fingerprint, p.c_str()); sprintf(pp, "found key %s in path: %s", fingerprint, p); diff --git a/src/gtk/Makefile b/src/gtk/Makefile @@ -2,12 +2,11 @@ OBJS := $(patsubst %.c,%.o,$(filter-out main.c beamenu_defs.c,$(wildcard *.c))) LINKOBJS := $(wildcard ../*.o) $(OBJS) INCLUDES := -I.. -I../aux/include CFLAGS += `pkg-config --cflags gtk4 gstreamer-1.0 libtasn1 libqrencode zbar` $(INCLUDES) -g3 -Wall -LIBS := `pkg-config --libs gtk4 zlib lmdb libgcrypt libxdg-basedir gstreamer-1.0 libtasn1 libqrencode zbar` -lb64 -lcmime -llash -lldap +LIBS := `pkg-config --libs gtk4 zlib lmdb libgcrypt libxdg-basedir gstreamer-1.0 libtasn1 libqrencode zbar` -lb64 -lcmime -llash -lldap -L../aux/lib LDFLAGS += $(LIBS) -AUXLIBS := `pkg-config --libs kee` all: menu resource $(OBJS) - $(CC) $(CFLAGS) main.c -o a.out $(LINKOBJS) $(LDFLAGS) $(AUXLIBS) beamenu_defs.o + $(CC) $(CFLAGS) main.c -o a.out $(LINKOBJS) $(LDFLAGS) ../aux/beamenu/beamenu.o ../aux/beamenu/import.o beamenu_defs.o %.o: %.c $(CC) $(CFLAGS) -c $< -o $@ $(LDFLAGS) diff --git a/src/gtk/kee-entry.c b/src/gtk/kee-entry.c @@ -185,7 +185,8 @@ static void kee_entry_handle_add(GtkButton *butt, KeeEntry *o) { buf = gtk_entry_get_buffer(o->form->bob_pubkey); b = (char*)gtk_entry_buffer_get_text(buf); - c = hex2bin(b, (unsigned char*)o->ledger.pubkey_bob); + //c = hex2bin(b, (unsigned char*)o->ledger.pubkey_bob); + c = h2b(b, (unsigned char*)o->ledger.pubkey_bob); if (c == 0) { // g_log(G_LOG_DOMAIN, G_LOG_LEVEL_INFO, "invalid counterparty public key data"); // return; @@ -484,10 +485,8 @@ static int process_entry_ledger(KeeEntry *o) { last_value_length = 129; strcpy(last_value, "uid="); if (o->bob_dn.uid == NULL) { - r = bin_to_hex((unsigned char*)o->ledger.pubkey_bob, 32, (unsigned char*)last_value+4, &last_value_length); - if (r) { - return ERR_FAIL; - } + b2h((unsigned char*)o->ledger.pubkey_bob, 32, (unsigned char*)last_value+4); + last_value_length = 65; r = kee_dn_from_str(&o->bob_dn, last_value, last_value_length+4); if (r) { return ERR_FAIL; diff --git a/src/gtk/kee-key.c b/src/gtk/kee-key.c @@ -90,10 +90,7 @@ KeeKey* kee_key_new(struct gpg_store *gpg) { } const char *kee_key_get_fingerprint(KeeKey *o, char *fingerprint) { - size_t fingerprint_len; - - fingerprint_len = 41; - bin_to_hex((unsigned char*)o->gpg->fingerprint, 20, (unsigned char*)fingerprint, &fingerprint_len); + b2h((unsigned char*)o->gpg->fingerprint, 20, (unsigned char*)fingerprint); //strcpy(fingerprint, o->gpg->fingerprint); return fingerprint; } diff --git a/src/gtk/tests/Makefile b/src/gtk/tests/Makefile @@ -7,12 +7,13 @@ CFLAGS += `pkg-config --cflags gtk4 gstreamer-1.0 libqrencode` $(INCLUDES) -Wa #LIBS := `pkg-config --libs gtk4 zlib lmdb libgcrypt libxdg-basedir gstreamer-1.0` -lb64 -lvarint -lcmime -llash LIBS := `pkg-config --libs gtk4 zlib lmdb libgcrypt libxdg-basedir gstreamer-1.0 libqrencode` -lb64 -lcmime -llash -ltasn1 -lldap LDFLAGS += $(LIBS) -AUXLIBS := `pkg-config --libs kee` +#AUXLIBS := `pkg-config --libs kee` all: $(OBJS) %.o: %.c - $(CC) $(CFLAGS) $< -o test_$* $(LINKOBJS) $(LDFLAGS) $(AUXLIBS) +# $(CC) $(CFLAGS) $< -o test_$* $(LINKOBJS) $(LDFLAGS) $(AUXLIBS) + $(CC) $(CFLAGS) $< -o test_$* $(LINKOBJS) $(LDFLAGS) ../../aux/beamenu/beamenu.o ../../aux/beamenu/import.o test_run: $(wildcard test_*) ./$< diff --git a/src/hex.c b/src/hex.c @@ -1,64 +0,0 @@ -#include <string.h> -#include <stddef.h> -#include <stdio.h> - -#include "hex.h" - -// cheekily stolen from https://nachtimwald.com/2017/09/24/hex-encode-and-decode-in-c/ -int hexchr2bin(const char hex, char *out) { - if (out == NULL) - return 0; - - if (hex >= '0' && hex <= '9') { - *out = hex - '0'; - } else if (hex >= 'A' && hex <= 'F') { - *out = hex - 'A' + 10; - } else if (hex >= 'a' && hex <= 'f') { - *out = hex - 'a' + 10; - } else { - return 0; - } - - return 1; -} - -size_t hex2bin(const char *hex, unsigned char *out) { - size_t len; - char b1; - char b2; - size_t i; - - if (hex == NULL || *hex == '\0' || out == NULL) - return 0; - - len = strlen(hex); - if (len % 2 != 0) - return 0; - len /= 2; - - memset(out, 'A', len); - for (i=0; i<len; i++) { - if (!hexchr2bin(hex[i*2], &b1) || !hexchr2bin(hex[i*2+1], &b2)) { - return 0; - } - //(*out)[i] = (b1 << 4) | b2; - *(out+i) = (b1 << 4) | b2; - } - return len; -} - -int bin_to_hex(const unsigned char *data, size_t l, unsigned char *zHex, size_t *z) { - unsigned int i; - - if (*z < (l*2)+1) { - return 1; - } - - for (i = 0; i < l; i++) { - sprintf((char*)zHex+(i*2), "%02x", *(data+i)); - } - *z = (i*2); - *(zHex+(*z)) = 0x0; - *z = *z + 1; - return 0; -} diff --git a/src/hex.h b/src/hex.h @@ -1,23 +0,0 @@ -#include <stddef.h> - -#ifdef __cplusplus -extern "C" { -#endif - -/** - * - * \brief Convert data to hex string - * - * \param data Input data. - * \param l Input data length. - * \param zHex If successful, contains hex string output. - * \param z Pointer to available length of \c zHex. Will contain the string length after succesful conversion. - * \todo \c z output is superfluous as zHex will (should) be zero-terminated. - * \return 1 if Output buffer is insufficient to write the result string, otherwise 0 (success). - */ -int bin_to_hex(const unsigned char *data, size_t l, unsigned char *zHex, size_t *z); -size_t hex2bin(const char *hex, unsigned char *out); - -#ifdef __cplusplus -} -#endif diff --git a/src/tests/Makefile b/src/tests/Makefile @@ -2,11 +2,11 @@ # OBJS := $(patsubst %.c,%.o,$(filter-out util.c,$(wildcard *.c))) LINKOBJS := $(wildcard ../*.o) -INCLUDES := -I.. +INCLUDES := -I.. -I../aux/include CFLAGS += `pkg-config --cflags gtk4 gstreamer-1.0 zbar` $(INCLUDES) -Wall -DRERR -DRERR_N_PREFIX=2 -LIBS := `pkg-config --libs gtk4 zlib lmdb libgcrypt libxdg-basedir gstreamer-1.0 libqrencode zbar` -lb64 -llash -ltasn1 -lcmime -lldap +LIBS := `pkg-config --libs gtk4 zlib lmdb libgcrypt libxdg-basedir gstreamer-1.0 libqrencode zbar` -lb64 -llash -ltasn1 -lcmime -lldap -L../aux/lib LDFLAGS += $(LIBS) -AUXLIBS := `pkg-config --libs kee` +#AUXLIBS := `pkg-config --libs kee` all: obj_debug $(OBJS) @@ -18,7 +18,8 @@ obj_debug: util %.o: %.c # $(CC) $(CFLAGS) $< -o test_$* debug.o testutil.o $(LINKOBJS) $(LDFLAGS) - $(CC) $(CFLAGS) $< -o test_$* testutil.o $(LINKOBJS) $(LDFLAGS) $(AUXLIBS) +# $(CC) $(CFLAGS) $< -o test_$* testutil.o $(LINKOBJS) $(LDFLAGS) $(AUXLIBS) + $(CC) $(CFLAGS) $< -o test_$* testutil.o $(LINKOBJS) $(LDFLAGS) #test_run: $(wildcard test_*) # ./$< diff --git a/src/tests/content.c b/src/tests/content.c @@ -16,7 +16,7 @@ int main() { char digest[64]; size_t l; - hex2bin(hash_of_foo, (unsigned char*)digest); + h2b(hash_of_foo, (unsigned char*)digest); cadiz.locator = "./testdata/resource"; diff --git a/src/tests/ledger.c b/src/tests/ledger.c @@ -39,19 +39,19 @@ int test_parse() { kee_ledger_init(&ledger); kee_ledger_reset_cache(&ledger); - c = hex2bin(test_ledger_data, (unsigned char*)data); + c = h2b(test_ledger_data, (unsigned char*)data); r = kee_ledger_parse(&ledger, data, c); if (r) { return 1; } - c = hex2bin(test_item_data_a, (unsigned char*)data); + c = h2b(test_item_data_a, (unsigned char*)data); ledger_item_a = kee_ledger_parse_item_db(&ledger, data, c); if (ledger_item_a == NULL) { return 1; } - c = hex2bin(test_item_data_b, (unsigned char*)data); + c = h2b(test_item_data_b, (unsigned char*)data); ledger_item_b = kee_ledger_parse_item_db(&ledger, data, c); if (ledger_item_b == NULL) { return 1;