commit 10e2a18fb7cadfd5791f65cbe988bb60cf8ed0e9
parent ebe97dca356c3070f0d4d5310aa7db979af07625
Author: lash <dev@holbrook.no>
Date: Sun, 2 Mar 2025 02:35:57 +0000
Add aux liblash, WIP serialize msg
Diffstat:
35 files changed, 1456 insertions(+), 23 deletions(-)
diff --git a/.gitignore b/.gitignore
@@ -3,3 +3,5 @@
src/asn1/*_tab.c
src/asn1/*_tab.o
src/asn1/generate_*
+src/aux/lib
+src/aux/include
diff --git a/src/asn1/defs.txt b/src/asn1/defs.txt
@@ -1,6 +1,5 @@
Qaeda DEFINITIONS EXPLICIT TAGS ::= BEGIN
Msg ::= SEQUENCE {
- domain OCTET STRING,
data OCTET STRING,
--- timestamp GeneralizedTime,
timestamp OCTET STRING,
@@ -9,6 +8,7 @@ Qaeda DEFINITIONS EXPLICIT TAGS ::= BEGIN
Cert ::= SEQUENCE {
parent Cert,
+ domain OCTET STRING,
request Msg,
request_sig OCTET STRING,
response Msg,
diff --git a/src/aux/Makefile b/src/aux/Makefile
@@ -0,0 +1,2 @@
+install:
+ make DESTDIR=`realpath .` -C liblash install
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,49 @@
+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
+ make -C case 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 case/case.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
+ cat -v case/*.h >> $(DESTDIR)/include/lash.h
+ install -m0644 -v case/*.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/case/Makefile b/src/aux/liblash/src/case/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)
+VERSION = 0.0.1
+CFLAGS += -Wall -Werror
+
+all: $(OBJS)
+
+test: all
+ $(CC) $(CFLAGS) test.c case.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 case-$(VERSION).tar.gz
+
+%.so.o: %.c
+ $(CC) $(CFLAGS) -c $< -o $@ -fpic
+
+shared: $(SOBJS)
diff --git a/src/aux/liblash/src/case/case.c b/src/aux/liblash/src/case/case.c
@@ -0,0 +1,29 @@
+void uc(char *b) {
+ int i;
+ char v;
+
+ i = 0;
+ v = 1;
+ while(v > 0) {
+ v = *(b+i);
+ if (v > 0x60 && v < 0x7b) {
+ *(b+i) -= 0x20;
+ }
+ i++;
+ }
+}
+
+void lc(char *b) {
+ int i;
+ char v;
+
+ i = 0;
+ v = 1;
+ while(v > 0) {
+ v = *(b+i);
+ if (v > 0x40 && v < 0x5b) {
+ *(b+i) += 0x20;
+ }
+ i++;
+ }
+}
diff --git a/src/aux/liblash/src/case/case.h b/src/aux/liblash/src/case/case.h
@@ -0,0 +1,7 @@
+#ifndef LASH_CASE_H_
+#define LASH_CASE_H_
+
+void uc(char *b);
+void lc(char *b);
+
+#endif
diff --git a/src/aux/liblash/src/case/test.c b/src/aux/liblash/src/case/test.c
@@ -0,0 +1,40 @@
+#include <string.h>
+
+#include "case.h"
+
+
+int test_uc() {
+ char data[] = "fO13_oBar";
+
+ uc(data);
+ if (strcmp(data, "FO13_OBAR")) {
+ return 1;
+ }
+ return 0;
+}
+
+int test_lc() {
+ char data[] = "FooB12_aR";
+
+ lc(data);
+ if (strcmp(data, "foob12_ar")) {
+ return 1;
+ }
+ return 0;
+}
+
+int main() {
+ int r;
+
+ r = test_uc();
+ if (r) {
+ return 1;
+ }
+
+ r = test_lc();
+ if (r) {
+ return 2;
+ }
+
+ return 0;
+}
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/hex/test.c b/src/aux/liblash/src/hex/test.c
@@ -0,0 +1,53 @@
+#include <string.h>
+
+#include "hex.h"
+
+char test_bin[] = {0xde, 0xad, 0xbe, 0xef};
+char test_hex[] = "deadbeef";
+char *test_empty = "";
+
+int test_from_string() {
+ int i;
+ char v[4];
+ int r;
+
+ r = h2b(test_hex, v);
+ if (r != 4) {
+ return 1;
+ }
+ for (i = 0; i < 4; i++) {
+ if (test_bin[i] != v[i]) {
+ return 1;
+ }
+ }
+ return 0;
+}
+
+int test_from_bin() {
+ int r;
+ char v[9];
+
+ b2h(test_bin, 4, v);
+ if (strcmp(v, test_hex)) {
+ return 1;
+ }
+ return 0;
+}
+
+
+int main() {
+ int r;
+
+ r = test_from_string();
+ if (r) {
+ return 1;
+ }
+
+ r = test_from_bin();
+ if (r) {
+ return 1;
+ }
+
+
+ return 0;
+}
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/liblash/src/llog/llog.c b/src/aux/liblash/src/llog/llog.c
@@ -0,0 +1,236 @@
+#include "llog.h"
+
+extern void b2h(const unsigned char *b, int l, char *hx);
+extern char* c2h(char in, char *out);
+
+
+char lloglvl_str[][4] = {
+ "???",
+ "crt",
+ "err",
+ "wrn",
+ "inf",
+ "dbg",
+ "gru",
+ "usr",
+};
+
+char _llogbuf_v[LLOG_LENGTH];
+char* _llogbuf = (char*)_llogbuf_v;
+int _llogbuf_crsr;
+
+static int cpy(const char *s) {
+ char *p;
+ int c;
+
+ p = _llogbuf + _llogbuf_crsr;
+ c = 0;
+ while (1) {
+ if (*s == 0) {
+ break;
+ }
+ *p = *s;
+ s++;
+ p++;
+ c++;
+ }
+ return c;
+}
+
+static int pfxfmt(char *v) {
+ int c;
+ char *p;
+
+ p = _llogbuf + _llogbuf_crsr;
+
+ *p = '[';
+ p++;
+ _llogbuf_crsr++;
+
+ c = cpy(v);
+ p += c;
+
+ *p = ']';
+ c++;
+ _llogbuf_crsr += c;
+
+ return c+1;
+}
+
+static int lvlfmt(enum lloglvl_e lvl) {
+ char *v;
+
+ v = lloglvl_str[(int)lvl];
+
+ return pfxfmt(v);
+}
+
+static char* kvstart(char *k) {
+ char *p;
+ int c;
+
+ p = _llogbuf + _llogbuf_crsr;
+ *p = 0x09;
+ p++;
+
+ _llogbuf_crsr++;
+ c = cpy(k);
+ p += c;
+ *p = '=';
+ c++;
+
+ _llogbuf_crsr += c;
+
+ return _llogbuf + _llogbuf_crsr;
+}
+
+char *llog_new(enum lloglvl_e lvl, char *msg) {
+ char *p;
+ int c;
+
+ _llogbuf_crsr = 0;
+ c = lvlfmt(lvl);
+ p = _llogbuf + c;
+ *p = ' ';
+ p++;
+ _llogbuf_crsr++;
+
+ c = cpy(msg);
+ _llogbuf_crsr += c;
+ p += c;
+ *p = 0;
+
+ return _llogbuf;
+}
+
+char *llog_new_ns(enum lloglvl_e lvl, char *msg, char *ns) {
+ char *p;
+ int c;
+
+ _llogbuf_crsr = 0;
+ c = pfxfmt(ns);
+ p = _llogbuf + c;
+ c = lvlfmt(lvl);
+ p += c;
+
+ *p = ' ';
+ p++;
+ _llogbuf_crsr++;
+ c = cpy(msg);
+
+ _llogbuf_crsr += c;
+ p += c;
+ *p = 0;
+
+ return _llogbuf;
+}
+
+char* llog_add_s(const char *k, char *v) {
+ char *p;
+ int c;
+
+ p = kvstart((char*)k);
+ c = 0;
+ while (1) {
+ if (*v == 0) {
+ break;
+ }
+ *p = *v;
+ p++;
+ c++;
+ v++;
+ }
+
+ _llogbuf_crsr += c;
+ *p = 0;
+
+ return _llogbuf;
+}
+
+
+char* llog_add_n(const char *k, long long v) {
+ char *p;
+ int c;
+ char i;
+ long long r;
+ char *b;
+
+ r = 0;
+ p = kvstart((char*)k);
+ c = 0;
+ b = (char*)&r;
+ b += (sizeof(r) - 1);
+ while (v != 0) {
+ i = v % 10;
+ i += 0x30;
+ *b = i;
+ b--;
+ c++;
+ v /= 10;
+ }
+ for (i = 0; i < c; i++) {
+ b++;
+ *p = *b;
+ p++;
+ }
+
+ _llogbuf_crsr += c;
+ *p = 0;
+
+ return _llogbuf;
+}
+
+char* llog_add_x(const char *k, long long v) {
+ char *p;
+ int c;
+ char i;
+ char *b;
+
+ p = kvstart((char*)k);
+ c = 0;
+ b = (char*)&v;
+ b += (sizeof(v) - 1);
+
+ i = 0;
+ while (*b == 0 && i < 8) {
+ b--;
+ i++;
+ }
+
+ if (i == 8) {
+ b++;
+ i--;
+ }
+ *p = '0';
+ p++;
+ *p = 'x';
+ p++;
+ c = 2;
+ for (; i < 8; i++) {
+ c2h(*b, p);
+ p += 2;
+ c += 2;
+ b--;
+ }
+
+ _llogbuf_crsr += c;
+ *p = 0;
+
+ return _llogbuf;
+}
+
+
+char* llog_add_b(const char *k, void *v, int l) {
+ char *p;
+ int c;
+
+ c = 0;
+ p = kvstart((char*)k);
+ b2h(v, l, p);
+ c += (l * 2);
+ _llogbuf_crsr += c;
+ p += c;
+ *p = 0;
+
+ return _llogbuf;
+}
diff --git a/src/aux/liblash/src/llog/llog.h b/src/aux/liblash/src/llog/llog.h
@@ -0,0 +1,30 @@
+#ifndef LLOG_H_
+#define LLOG_H_
+
+#define LLOG_LENGTH 1024
+
+#ifndef LLOG_DEFAULT_NS
+#define LLOG_DEFAULT_NS "llog"
+#endif
+
+enum lloglvl_e {
+ LLOG_NONE = 0,
+ LLOG_CRITICAL,
+ LLOG_ERROR,
+ LLOG_WARNING,
+ LLOG_INFO,
+ LLOG_DEBUG,
+ LLOG_GURU,
+ LLOG_USR,
+ LLOG_N_LEVELS,
+};
+
+char* llog_new(enum lloglvl_e lvl, char *msg);
+char* llog_new_ns(enum lloglvl_e lvl, char *msg, char *ns);
+char* llog_add_s(const char *k, char *v);
+char* llog_add_n(const char *k, long long v);
+char* llog_add_x(const char *k, long long v);
+char* llog_add_b(const char *k, void *v, int l);
+extern void llog_out(const char *v);
+
+#endif
diff --git a/src/aux/liblash/src/llog/test.c b/src/aux/liblash/src/llog/test.c
@@ -0,0 +1,53 @@
+#include <stdio.h>
+#include <string.h>
+
+#include "llog.h"
+
+void llog_out(const char *v) {
+ fprintf(stderr, "%s\n", v);
+}
+
+int main() {
+ char *p;
+ char beef[] = { 0xbe, 0xef };
+
+ p = llog_new(LLOG_CRITICAL, "foo");
+ if (strcmp("[crt] foo", p)) {
+ return 1;
+ }
+ llog_out(p);
+
+ p = llog_add_s("bar", "baz");
+ if (strcmp("[crt] foo\tbar=baz", p)) {
+ return 1;
+ }
+
+ p = llog_add_n("xyzzy", 42);
+ if (strcmp("[crt] foo\tbar=baz\txyzzy=42", p)) {
+ return 1;
+ }
+
+ p = llog_add_b("dead", (void*)beef, 2);
+ if (strcmp("[crt] foo\tbar=baz\txyzzy=42\tdead=beef", p)) {
+ return 1;
+ }
+
+ p = llog_new_ns(LLOG_CRITICAL, "pinky", "inky");
+ if (strcmp("[inky][crt] pinky", p)) {
+ return 1;
+ }
+
+ p = llog_add_s("blinky", "clyde");
+ if (strcmp("[inky][crt] pinky\tblinky=clyde", p)) {
+ return 1;
+ }
+
+ p = llog_new(LLOG_INFO, "foo");
+ p = llog_add_x("bar", 666);
+ if (strcmp("[inf] foo\tbar=0x029a", p)) {
+ return 1;
+ }
+
+
+ return 0;
+}
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,114 @@
+#include "rerr.h"
+
+#ifdef RERR
+static char** rerr[RERR_N_PFX + 1];
+static const char* rerr_pfx[RERR_N_PFX + 1];
+char *rerr_base[3] = {
+ "OK",
+ "FAIL",
+ "UNSUPPORTED",
+};
+#endif
+
+void rerr_init(const char *coreprefix) {
+#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] = coreprefix;
+#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
+
+const 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);
+#else
+ return "";
+#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/liblash/src/rerr/rerr.h b/src/aux/liblash/src/rerr/rerr.h
@@ -0,0 +1,18 @@
+#ifndef RERR_H_
+#define RERR_H_
+
+#define ERR_OK 0x0
+#define ERR_FAIL 0x1
+#define ERR_UNSUPPORTED 0x2
+
+#ifndef RERR_N_PFX
+#define RERR_N_PFX 0
+#endif
+
+void rerr_init(const char *coreprefix);
+void rerr_register(int pfx, char *label, void *start);
+char* rerrstr(int code, char *buf);
+char* rerrstrv(int code);
+const char* rerrpfx(int code);
+
+#endif // RERR_H
diff --git a/src/aux/liblash/src/rerr/test.c b/src/aux/liblash/src/rerr/test.c
@@ -0,0 +1,44 @@
+#include <string.h>
+
+#include "rerr.h"
+
+char *bars[3] = {
+ "inky",
+ "pinky",
+ "blinky",
+};
+
+int main() {
+ const char *s;
+ char v[1024];
+ rerr_init("core");
+
+ s = rerrpfx(0);
+ if (strcmp(s, "core")) {
+ return 1;
+ }
+
+ s = rerrstr(0, v);
+ if (strcmp(s, "core: OK")) {
+ return 1;
+ }
+
+ rerr_register(0x200, "bar", bars);
+
+ s = rerrstr(0x202, v);
+ if (strcmp(s, "bar: blinky")) {
+ return 1;
+ }
+
+ s = rerrstrv(0x202);
+ if (strcmp(s, "blinky")) {
+ return 1;
+ }
+
+ s = rerrpfx(0x202);
+ if (strcmp(s, "bar")) {
+ return 1;
+ }
+
+ return 0;
+}
diff --git a/src/aux/liblash/src/tests/Makefile b/src/aux/liblash/src/tests/Makefile
@@ -0,0 +1,19 @@
+TESTS := $(patsubst %.c,%.testbin,$(wildcard *.c))
+LINKOBJS := $(wildcard ../*.o)
+CFLAGS += -I.. -Wall
+
+all: $(TESTS)
+
+%.testbin: %.c
+ $(CC) $(CFLAGS) $< -o $@ $(LINKOBJS)
+ ./$@
+
+#test_run: $(wildcard out_*)
+# for f in $^; do ./$$f; done
+
+test: all
+
+clean:
+ rm -vf *.testbin
+
+.PHONY: clean
diff --git a/src/aux/liblash/src/tests/test_endian.c b/src/aux/liblash/src/tests/test_endian.c
@@ -0,0 +1,63 @@
+#include <string.h>
+#include <stdio.h>
+
+#include "endian.h"
+#include "strip.h"
+
+
+int main() {
+ int r;
+ int i;
+ char left[] = { 0x02, 0x13, 0x24, 0x35};
+ char right[] = { 0x35, 0x24, 0x13, 0x02};
+ unsigned char tmp[4];
+
+ memcpy(tmp, left, 4);
+
+ // four byte flip
+ flip_endian(4, tmp);
+ for (i = 0; i < 4; i++) {
+ if (tmp[i] != *(right+i)) {
+ return 1;
+ }
+ }
+
+ // two byte flip
+ memcpy(tmp, left, 2);
+ flip_endian(2, tmp);
+ for (i = 0; i < 4; i++) {
+ if (tmp[i] != *(right+2+i)) {
+ return 1;
+ }
+ }
+
+ // single byte flip
+ tmp[0] = 0x2a;
+ flip_endian(1, tmp);
+ tmp[0] = 0x2a;
+
+
+ // check explicit endian convert
+ if (is_le()) {
+ memcpy(tmp, left, 4);
+ } else {
+ memcpy(tmp, right, 4);
+ }
+ r = to_endian(0, 4, (void*)tmp);
+ if (r) {
+ return 1;
+ }
+ for (i = 0; i < 4; i++) {
+ if (tmp[i] != *(right+i)) {
+ return 1;
+ }
+ }
+
+ // check invalid length
+ r = to_endian(0, 3, (void*)tmp);
+ if (!r) {
+ return 1;
+ }
+
+ return 0;
+}
diff --git a/src/aux/liblash/src/tests/test_strip.c b/src/aux/liblash/src/tests/test_strip.c
@@ -0,0 +1,168 @@
+#include <string.h>
+#include <stddef.h>
+#include <stdio.h>
+
+#include "strip.h"
+#include "endian.h"
+
+static int test_strip() {
+ size_t l = 4;
+ char be_int_semi[4] = {0x00, 0x00, 0x02, 0x9f};
+ char be_int_full[4] = {0x40, 0x00, 0x02, 0x9f};
+ char be_int_zero[4] = {0x00, 0x00, 0x00, 0x00};
+ char be_int_mini[1] = {0x2a};
+ char be_int_mini_zero[1] = {0x00};
+ char *r;
+
+ r = strip_be((char*)be_int_semi, &l);
+ if (l != 2) {
+ return 1;
+ }
+ if (*r != 0x02) {
+ return 1;
+ }
+
+ l = 4;
+ r = strip_be((char*)be_int_full, &l);
+ if (l != 4) {
+ return 1;
+ }
+ if (*r != 0x40) {
+ return 1;
+ }
+
+ l = 4;
+ r = strip_be((char*)be_int_zero, &l);
+ if (l != 1) {
+ return 1;
+ }
+ if (*r != 0x00) {
+ return 1;
+ }
+
+ l = 1;
+ r = strip_be((char*)be_int_mini, &l);
+ if (l != 1) {
+ return 1;
+ }
+ if (*r != 0x2a) {
+ return 1;
+ }
+
+ l = 1;
+ r = strip_be((char*)be_int_mini_zero, &l);
+ if (l != 1) {
+ return 1;
+ }
+ if (*r != 0x00) {
+ return 1;
+ }
+
+ return 0;
+}
+
+static int test_strap() {
+ int r;
+ char v_neg[] = {0xfe, 0xf2, 0x00, 0x00};
+ char v_pos[] = {0x02, 0x9a, 0x00, 0x00};
+ char v_full[] = {0x2a, 0x13, 0x24, 0x35};
+ char v_full_neg[] = {0xfa, 0x13, 0x24, 0x35};
+ char out[4];
+ int *p;
+
+ memset(out, 0, 4);
+ r = strap_be(v_neg, 2, out, 4);
+ if (r) {
+ return r;
+ }
+ if (is_le()) {
+ flip_endian(4, out);
+ }
+ p = (int*)out;
+ if (*p != -270) {
+ return 1;
+ }
+
+ memset(out, 0, 4);
+ r = strap_be(v_pos, 2, out, 4);
+ if (r) {
+ return r;
+ }
+ if (is_le()) {
+ flip_endian(4, out);
+ }
+ p = (int*)out;
+ if (*p != 666) {
+ return 1;
+ }
+
+ memset(out, 0, 4);
+ r = strap_be(v_full, 1, out, 4);
+ if (r) {
+ return r;
+ }
+ if (is_le()) {
+ flip_endian(4, out);
+ }
+ p = (int*)out;
+ if (*p != 42) {
+ return 1;
+ }
+
+ memset(out, 0, 4);
+ r = strap_be(v_full, 3, out, 4);
+ if (r) {
+ return r;
+ }
+ if (is_le()) {
+ flip_endian(4, out);
+ }
+ p = (int*)out;
+ if (*p != 2757412) {
+ return 1;
+ }
+
+ memset(out, 0, 4);
+ r = strap_be(v_full, 4, out, 4);
+ if (r) {
+ return r;
+ }
+ if (is_le()) {
+ flip_endian(4, out);
+ }
+ p = (int*)out;
+ if (*p != 705897525) {
+ return 1;
+ }
+
+ memset(out, 0, 4);
+ r = strap_be(v_full_neg, 4, out, 4);
+ if (r) {
+ return r;
+ }
+ if (is_le()) {
+ flip_endian(4, out);
+ }
+ p = (int*)out;
+ if (*p != -99408843) {
+ return 1;
+ }
+
+ return 0;
+}
+
+int main() {
+ int r;
+
+ r = test_strip();
+ if (r) {
+ return r;
+ }
+
+ r = test_strap();
+ if (r) {
+ return r;
+ }
+
+ return 0;
+}
diff --git a/src/lq/Makefile b/src/lq/Makefile
@@ -1,5 +1,5 @@
OBJS := $(patsubst %.c,%.o,$(filter-out main.c,$(wildcard *.c)))
-INCLUDES := -I..
+INCLUDES := -I.. -I../aux/include
CFLAGS += $(INCLUDES) -Wall
%.o: %.c
diff --git a/src/lq/crypto.h b/src/lq/crypto.h
@@ -7,6 +7,14 @@
#define LQ_DIGEST_LEN 32
#endif
+#ifndef LQ_PUBKEY_LEN
+#define LQ_PUBKEY_LEN 65
+#endif
+
+#ifndef LQ_PRIVKEY_LEN
+#define LQ_PRIVKEY_LEN 32
+#endif
+
struct lq_privatekey_t {
int key_typ;
void *lokey;
diff --git a/src/lq/msg.c b/src/lq/msg.c
@@ -1,8 +1,12 @@
+#include <stddef.h>
#include <time.h>
+#include <libtasn1.h>
#include "lq/msg.h"
#include "lq/mem.h"
#include "lq/crypto.h"
+#include "lq/wire.h"
+#include "endian.h"
LQMsg* lq_msg_new(const char *msg_data, size_t msg_len) {
@@ -10,33 +14,25 @@ LQMsg* lq_msg_new(const char *msg_data, size_t msg_len) {
msg = lq_alloc(sizeof(LQMsg));
lq_set(msg, 0, sizeof(LQMsg));
- msg->timestamp = (int)time(NULL);
+ clock_gettime(CLOCK_REALTIME, &msg->time);
return msg;
}
-void lq_msg_set_domain(LQMsg *msg, const char *domain) {
- lq_cpy(msg->domain, (void*)domain, LQ_MSG_DOMAIN_LEN);
-}
-
int lq_msg_sign(LQMsg *msg, LQPrivKey *pk) {
return lq_msg_sign_salted(msg, pk, 0, 0);
}
int lq_msg_sign_salted(LQMsg *msg, LQPrivKey *pk, const char *salt, size_t salt_len) {
- size_t l;
int r;
char *data;
char digest[LQ_DIGEST_LEN];
- l = LQ_MSG_DOMAIN_LEN + msg->len;
- data = lq_alloc(l);
- lq_cpy(data, msg->domain, LQ_MSG_DOMAIN_LEN);
- lq_cpy(data + LQ_MSG_DOMAIN_LEN, msg->data, msg->len);
+ data = lq_alloc(msg->len);
+ lq_cpy(data, msg->data, msg->len);
msg->pubkey = lq_publickey_from_privatekey(pk);
- r = lq_digest(data, l, (char*)digest);
- //msg->signature = lq_privatekey_sign(pk, msg->data, msg->len, salt, salt_len);
+ r = lq_digest(data, msg->len, (char*)digest);
return r;
}
@@ -45,12 +41,54 @@ void lq_msg_free(LQMsg *msg) {
if (msg->pubkey != 0) {
lq_free(msg->pubkey);
}
- //if (msg->signature != 0) {
- // lq_free(msg->signature);
- //}
lq_free(msg);
}
-char* lq_msg_serialize(LQMsg *msg, char *out, size_t *out_len) {
- return NULL;
+int lq_msg_serialize(LQMsg *msg, char *out, size_t *out_len) {
+ int c;
+ int r;
+ char timedata[8];
+ char err[1024];
+ asn1_node node;
+
+ r = asn1_array2tree(defs_asn1_tab, &node, err);
+ if (r != ASN1_SUCCESS) {
+ return 1;
+ }
+
+ c = (int)msg->len;
+ r = asn1_write_value(node, "Qaeda.Msg.data", msg->data, c);
+ if (r != ASN1_SUCCESS) {
+ return 1;
+ }
+
+ lq_cpy(timedata, &msg->time.tv_sec, 4);
+ lq_cpy(((char*)timedata)+4, &msg->time.tv_nsec, 4);
+ r = to_endian(TO_ENDIAN_BIG, 4, timedata);
+ if (r) {
+ return 1;
+ }
+ r = to_endian(TO_ENDIAN_BIG, 4, ((char*)timedata)+4);
+ if (r) {
+ return 1;
+ }
+
+ c = sizeof(int);
+ r = asn1_write_value(node, "Qaeda.Msg.timestamp", &timedata, c);
+ if (r != ASN1_SUCCESS) {
+ return 1;
+ }
+
+ c = msg->pubkey->lolen;
+ r = asn1_write_value(node, "Qaeda.Msg.pubkey", &msg->pubkey->lokey, c);
+ if (r != ASN1_SUCCESS) {
+ return 1;
+ }
+
+ r = asn1_der_coding(node, "Qaeda.Msg", out, (int*)out_len, err);
+ if (r != ASN1_SUCCESS) {
+ return 1;
+ }
+
+ return 0;
}
diff --git a/src/lq/msg.h b/src/lq/msg.h
@@ -2,6 +2,7 @@
#define LIBQAEDA_MSG_H_
#include <stddef.h>
+#include <time.h>
#include "lq/crypto.h"
@@ -12,17 +13,15 @@
struct lq_msg_t {
const char *data;
size_t len;
- char domain[LQ_MSG_DOMAIN_LEN];
- int timestamp;
+ struct timespec time;
LQPubKey *pubkey;
};
typedef struct lq_msg_t LQMsg;
LQMsg* lq_msg_new(const char *msg_data, size_t msg_len);
-void lq_msg_set_domain(LQMsg *msg, const char *domain);
int lq_msg_sign(LQMsg *msg, LQPrivKey *pk);
int lq_msg_sign_salted(LQMsg *msg, LQPrivKey *pk, const char *salt, size_t salt_len);
-char *lq_msg_serialize(LQMsg *msg, char *out, size_t *out_len);
+int lq_msg_serialize(LQMsg *msg, char *out, size_t *out_len);
int lq_msg_deserialize(LQMsg *msg, const char *in, size_t in_len);
void lq_msg_free(LQMsg *msg);
#endif // LIBQAEDA_MSG_H_
diff --git a/src/lq/wire.h b/src/lq/wire.h
@@ -0,0 +1,7 @@
+#ifndef LIBQAEDA_WIRE_H_
+#define LIBQAEDA_WIRE_H_
+
+extern const asn1_static_node defs_asn1_tab[];
+
+#endif // LIBQAEDA_WIRE_H_
+