commit b34cca62da54cf2b08f97f5554a628b88163d149
parent f3f59213a202161c1ac4e10cddc4c404a03ce542
Author: lash <dev@holbrook.no>
Date: Sun, 23 Mar 2025 15:22:17 +0000
Add vararg debug
Diffstat:
9 files changed, 203 insertions(+), 17 deletions(-)
diff --git a/src/Makefile b/src/Makefile
@@ -1,6 +1,12 @@
-all: asn1 aux core dummy
+INCLUDES := -I. -I./aux/include
+CFLAGS += $(INCLUDES) -Wall
+
+all: all-gpg
+all-dummy: asn1 aux core dummy
+all-gpg: asn1 aux core gpg
core:
+ $(CC) $(CFLAGS) -c debug.c
make -C lq
aux:
@@ -9,6 +15,12 @@ aux:
asn1:
make -C asn1 all
+gpg:
+ make -C crypto gpg
+ make -C mem std
+ make -C io std
+ make -C store dummy
+
dummy:
make -C crypto dummy
make -C mem std
diff --git a/src/aux/liblash/src/llog/llog.h b/src/aux/liblash/src/llog/llog.h
@@ -1,7 +1,9 @@
#ifndef LLOG_H_
#define LLOG_H_
+#ifndef LLOG_LENGTH
#define LLOG_LENGTH 1024
+#endif
#ifndef LLOG_DEFAULT_NS
#define LLOG_DEFAULT_NS "llog"
diff --git a/src/crypto/Makefile b/src/crypto/Makefile
@@ -1,8 +1,11 @@
-INCLUDES := -I..
+INCLUDES := -I.. -I../aux/include
CFLAGS += $(INCLUDES) -Wall
dummy:
- $(CC) $(CFLAGS) -g3 -c dummy.c
+ $(CC) $(CFLAGS) -c dummy.c
+
+gpg:
+ $(CC) $(CFLAGS) -DLQ_GPG -c gcrypt.c
clean:
rm -vf *.o
diff --git a/src/crypto/gcrypt.c b/src/crypto/gcrypt.c
@@ -1,5 +1,4 @@
#ifdef LQ_GPG
-#define LQ_GPG
#define GPG_MIN_VERSION "1.10.2"
#define GPG_KEY_TYP 1
@@ -9,6 +8,9 @@
#include "lq/crypto.h"
#include "lq/io.h"
+#include "lq/mem.h"
+#include "lq/config.h"
+#include "lq/err.h"
#include "debug.h"
#ifdef RERR
@@ -23,17 +25,17 @@ char *_rerr[7] = {
};
#endif
-const char *gpgVersion = NULL;
-
struct gpg_store {
gcry_sexp_t k;
char fingerprint[LQ_FP_LEN];
char public_key[LQ_PUBKEY_LEN];
- char last_signature[LQ_SIG_LEN];
+ char last_signature[LQ_SIGN_LEN];
char last_data[LQ_DIGEST_LEN];
};
+static const char *gpg_version = NULL;
static int gpg_cfg_idx_dir;
+static int gpg_passphrase_digest_len;
int lq_crypto_init() {
#ifdef RERR
@@ -42,13 +44,25 @@ int lq_crypto_init() {
//char *p;
//size_t c;
int r;
-
- gpg = lq_zero(sizeof(struct gpg_store));
- if (gpg == NULL) {
- return ERR_MEM;
+ char *v;
+
+ if (gpg_version == NULL) {
+ v = gcry_check_version(GPG_MIN_VERSION);
+ //if (v == nullptr) {
+ if (v == NULL) {
+ return ERR_NOCRYPTO;
+ }
}
- gpg->passphrase_digest_len = gcry_md_get_algo_dlen(GCRY_MD_SHA256);
+ gpg_version = v;
+ //sprintf(d, "Using gpg version: %s", gpgVersion);
+ debug_dbg("gpg", "using gpg");
+// gpg = lq_zero(sizeof(struct gpg_store));
+// if (gpg == NULL) {
+// return ERR_MEM;
+// }
+// gpg->passphrase_digest_len = gcry_md_get_algo_dlen(GCRY_MD_SHA256);
+ gpg_passphrase_digest_len = gcry_md_get_algo_dlen(GCRY_MD_SHA256);
gpg_cfg_idx_dir = lq_config_register(LQ_TYP_STR, "CRYPTODIR");
// strcpy(gpg->path, path);
@@ -62,21 +76,30 @@ int lq_crypto_init() {
return ERR_OK;
}
-LQPrivKey* lq_privatekey_new(const char *seed, size_t seed_len, const char *passphrase, size_t passphrase_len) {
+LQPrivKey* lq_privatekey_new(const char *seed, size_t seed_len, const char *passphrase, size_t passphrase_len) {
}
LQPrivKey* lq_privatekey_load(const char *passphrase, size_t passphrase_len) {
LQPrivKey *o;
+ struct gpg_store *gpg;
+ // allocate private key memory
o = lq_alloc(sizeof(LQPrivKey));
if (o == NULL) {
return NULL;
}
-
+
+ // allocate gpg internal private key memory
+ o->impl = lq_alloc(sizeof(struct gpg_store));
+ if (o->impl == NULL) {
+ lq_free(o);
+ return NULL;
+ }
+
+ //
o->key_typ = GPG_KEY_TYP;
o->key_state = LQ_KEY_INIT;
- o->impl = (void*)&gpg;
return o;
}
@@ -105,6 +128,8 @@ size_t lq_signature_bytes(LQSig *sig, char **out) {
}
void lq_privatekey_free(LQPrivKey *pk) {
+ lq_free(pk->impl);
+ lq_free(pk);
}
void lq_publickey_free(LQPubKey *pubk) {
diff --git a/src/debug.c b/src/debug.c
@@ -0,0 +1,86 @@
+#include <string.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <stdarg.h>
+
+#include <llog.h>
+
+#include "lq/mem.h"
+#include "debug.h"
+
+static int default_fd = 2;
+static char nl = 0x0a;
+
+
+int debug_fd(int fd) {
+ if (fcntl(fd, F_GETFD) < 0) {
+ return 1;
+ };
+ default_fd = fd;
+ return 0;
+}
+
+
+static void debug_write(int fd, const char *s) {
+ size_t r;
+ size_t l;
+ size_t c;
+ char *p;
+
+ l = strlen(s);
+ c = 0;
+ p = (char*)s;
+ while (c < l) {
+ r = write(fd, p, l - c);
+ p += r;
+ c += r;
+ }
+ write(fd, &nl, 1);
+}
+
+void llog_out(const char *s) {
+ debug_write(default_fd, s);
+}
+
+void debug_dbg(const char *ns, const char *msg) {
+ char *p;
+
+ p = llog_new_ns(LLOG_DEBUG, (char*)msg, (char*)ns);
+ llog_out(p);
+}
+
+void debug_dbg_x(const char *ns, const char *msg, int argc, ...) {
+ int i;
+ long long l;
+ char *k;
+ char *p;
+ enum debug_typ_e typ;
+ void *v;
+ va_list vv;
+
+ va_start(vv, argc);
+
+ p = llog_new_ns(LLOG_DEBUG, (char*)msg, (char*)ns);
+
+ for (i = 0; i < argc; i++) {
+ typ = va_arg(vv, enum debug_typ_e);
+ l = va_arg(vv, int);
+ k = va_arg(vv, char*);
+ switch (typ) {
+ case MORGEL_TYP_BIN:
+ v = va_arg(vv, char*);
+ llog_add_b(k, v, l);
+ break;
+ case MORGEL_TYP_STR:
+ v = va_arg(vv, char*);
+ llog_add_s(k, v);
+ break;
+ case MORGEL_TYP_NUM:
+ l = va_arg(vv, long long);
+ llog_add_n(k, l);
+ break;
+ }
+ }
+ llog_out(p);
+ va_end(vv);
+}
diff --git a/src/debug.h b/src/debug.h
@@ -0,0 +1,13 @@
+#ifndef MORGEL_H_
+#define MORGEL_H_
+
+enum debug_typ_e {
+ MORGEL_TYP_BIN,
+ MORGEL_TYP_NUM,
+ MORGEL_TYP_STR,
+};
+
+void debug_dbg(const char *ns, const char *msg);
+void debug_dbg_x(const char *ns, const char *msg, int argc, ...);
+
+#endif // MOREGELLONS_H_
diff --git a/src/lq/err.h b/src/lq/err.h
@@ -1,10 +1,12 @@
#ifndef LIBQAEDA_ERR_H_
#define LIBQAEDA_ERR_H_
+// provides ERR_OK = 0, ERR_FAIL = 1, ERR_UNIMPLEMENTED = 2
+#include <rerr.h>
+
/// Error values used across all error contexts.
enum err_e {
- ERR_OK, ///< No error
- ERR_NOOP, ///< No action taken.
+ ERR_NOOP = 3, ///< No action taken.
ERR_BYTEORDER, ///< Errors related to endianness
ERR_OVERFLOW, ///< Not enough space to write
ERR_INIT, ///< Failure instantiating object or data
diff --git a/src/test/Makefile b/src/test/Makefile
@@ -6,6 +6,7 @@ LIBS := ../asn1/defs_asn1_tab.o `pkg-config --libs libtasn1` -L../aux/lib -llash
LDFLAGS := -lcheck $(LIBS)
all: build
+ CK_FORK=no LD_LIBRARY_PATH=`realpath ../aux/lib` ./test_debug_bin
CK_FORK=no LD_LIBRARY_PATH=`realpath ../aux/lib` ./test_config_bin
CK_FORK=no LD_LIBRARY_PATH=`realpath ../aux/lib` ./test_crypto_bin
CK_FORK=no LD_LIBRARY_PATH=`realpath ../aux/lib` ./test_msg_bin
@@ -15,8 +16,10 @@ all: build
test: all
build:
+ $(CC) $(CFLAGS) test_debug.c -o test_debug_bin ../debug.o $(LDFLAGS)
$(CC) $(CFLAGS) test_config.c -o test_config_bin ../lq/config.o ../mem/std.o $(LDFLAGS)
$(CC) $(CFLAGS) test_crypto.c -o test_crypto_bin ../crypto/dummy.o ../mem/std.o $(LDFLAGS)
+ #$(CC) $(CFLAGS) test_crypto.c -o test_crypto_bin ../crypto/gcrypt.o ../mem/std.o $(LDFLAGS) -lgcrypt
$(CC) $(CFLAGS) test_msg.c -o test_msg_bin ../crypto/dummy.o ../mem/std.o ../store/dummy.o ../store/file.o ../io/std.o ../lq/msg.o $(LDFLAGS)
$(CC) $(CFLAGS) test_cert.c -o test_cert_bin ../crypto/dummy.o ../mem/std.o ../store/dummy.o ../store/file.o ../io/std.o ../lq/msg.o ../lq/cert.o $(LDFLAGS)
$(CC) $(CFLAGS) test_trust.c -o test_trust_bin ../crypto/dummy.o ../mem/std.o ../store/mem.o ../lq/trust.o -lhashmap $(LDFLAGS)
diff --git a/src/test/test_debug.c b/src/test/test_debug.c
@@ -0,0 +1,40 @@
+#include <check.h>
+#include <stdlib.h>
+
+#include "debug.h"
+
+START_TEST(check_debug_novar) {
+ debug_dbg("test", "foo");
+ debug_dbg_x("test", "foo", 1, MORGEL_TYP_STR, 0, "bar", "baz");
+ debug_dbg_x("test", "foo", 1, MORGEL_TYP_BIN, 3, "inky", "pinky");
+ debug_dbg_x("test", "foo", 1, MORGEL_TYP_NUM, 0, "xyzzy", 42);
+}
+END_TEST
+
+Suite * common_suite(void) {
+ Suite *s;
+ TCase *tc;
+
+ s = suite_create("debug");
+ tc = tcase_create("touch");
+ tcase_add_test(tc, check_debug_novar);
+ suite_add_tcase(s, tc);
+
+ return s;
+}
+
+int main(void) {
+ int n_fail;
+
+ Suite *s;
+ SRunner *sr;
+
+ s = common_suite();
+ sr = srunner_create(s);
+
+ srunner_run_all(sr, CK_VERBOSE);
+ n_fail = srunner_ntests_failed(sr);
+ srunner_free(sr);
+
+ return (n_fail == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
+}