kee

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

commit 7118562dab25726fada98981dc39347e44ecd8aa
parent 08c5d584f2b276a06b0f2a9eb2ecccda55490299
Author: lash <dev@holbrook.no>
Date:   Sat, 20 Apr 2024 09:31:51 +0100

Separate out content resolver component for ledger

Diffstat:
Msrc/cadir.c | 3++-
Asrc/content.c | 69+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/content.h | 28++++++++++++++++++++++++++++
Msrc/ledger.c | 26+++++++++++++-------------
Msrc/ledger.h | 6++++--
Msrc/tests/Makefile | 3++-
Asrc/tests/content.c | 56++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/tests/testdata_resource/1382ea21c44e0352916e17267351fcc2d890816a254caba354b2da23c2b33a9c50c5b4151ddab876d199e3c0bca26fcf75302427db5ca05e5f049f2a9f8dc9d4 | 7+++++++
8 files changed, 181 insertions(+), 17 deletions(-)

diff --git a/src/cadir.c b/src/cadir.c @@ -7,6 +7,7 @@ /// \todo replace with fadfada +/// \todo fail on file size too big for buffer //int cadiz_resolve(const char *locator, enum CadizKeyType key_type, const char *key, char *out, size_t *out_len) { int cadiz_resolve(Cadiz *cadiz, const char *key, char *out, size_t *out_len) { int r; @@ -42,6 +43,6 @@ int cadiz_resolve(Cadiz *cadiz, const char *key, char *out, size_t *out_len) { l = read(fd, out, *out_len); close(fd); *out_len = l; - + return 0; } diff --git a/src/content.c b/src/content.c @@ -0,0 +1,69 @@ +#include <stdlib.h> +#include <string.h> + +#include "cmime.h" + +#include "content.h" +#include "err.h" + + +struct kee_content_t* kee_content_new(const char *key, size_t size_hint) { + struct kee_content_t *content = malloc(sizeof(struct kee_content_t)); + if (content == NULL) { + return NULL; + } + + if (size_hint) { + content->mem_size = size_hint; + } else { + content->mem_size = KEE_CONTENT_DEFAULT_SIZE; + } + + content->mem = malloc(content->mem_size); + if (content->mem == NULL) { + free(content); + return NULL; + } + content->body = content->mem; + content->flags = 0; + + memcpy(content->key, key, KEE_CONTENT_KEY_SIZE); + return content; +} + +int kee_content_resolve(struct kee_content_t *content, Cadiz *cadiz) { + int r; + CMimeMessage_T *msg; + char *subject; + size_t subject_len; + size_t c; + + content->flags = 0; + c = content->mem_size; + r = cadiz_resolve(cadiz, content->key, content->body, &c); + if (!r) { + content->mem_size -= c; + content->flags |= KEE_CONTENT_RESOLVED_BODY; + msg = cmime_message_new(); + r = cmime_message_from_string(&msg, content->body, 0); + if (!r) { + subject = cmime_message_get_subject(msg); + subject = cmime_string_strip(subject); + subject_len = strlen(subject) + 1; + if (subject_len <= content->mem_size) { + content->subject = content->body + c; + strcpy(content->subject, subject); + content->flags |= KEE_CONTENT_RESOLVED_SUBJECT; + content->mem_size -= subject_len; + } + } + cmime_message_free(msg); + } + content->flags |= KEE_CONTENT_RESOLVED; + return ERR_OK; +} + +void kee_content_free(struct kee_content_t *content) { + free(content->mem); + free(content); +} diff --git a/src/content.h b/src/content.h @@ -0,0 +1,28 @@ +#ifndef KEE_CONTENT_H_ +#define KEE_CONTENT_H_ + +#include <stddef.h> + +#include "cadiz.h" + +#define KEE_CONTENT_DEFAULT_SIZE 4096 +#define KEE_CONTENT_KEY_SIZE 64 + +#define KEE_CONTENT_RESOLVED 1 +#define KEE_CONTENT_RESOLVED_BODY 2 +#define KEE_CONTENT_RESOLVED_SUBJECT 4 + +struct kee_content_t { + char key[KEE_CONTENT_KEY_SIZE]; + size_t mem_size; + char *mem; + char *subject; + char *body; + char flags; +}; + +struct kee_content_t* kee_content_new(const char *key, size_t size_hint); +int kee_content_resolve(struct kee_content_t *content, Cadiz *cadiz); +void kee_content_free(struct kee_content_t *content); + +#endif diff --git a/src/ledger.c b/src/ledger.c @@ -296,7 +296,7 @@ struct kee_ledger_item_t *kee_ledger_parse_item(struct kee_ledger_t *ledger, con if (r != ASN1_SUCCESS) { return NULL; } - if (tmp[0] == "T") { // "TRUE" + if (tmp[0] == 'T') { // "TRUE" cur->response = 1; } @@ -305,12 +305,12 @@ struct kee_ledger_item_t *kee_ledger_parse_item(struct kee_ledger_t *ledger, con if (r != ASN1_SUCCESS) { return NULL; } - - c = 4096; - r = asn1_read_value(item, "body", cur->body, &c); - if (r != ASN1_SUCCESS) { - return NULL; - } +// +// c = 4096; +// r = asn1_read_value(item, "body", cur->body, &c); +// if (r != ASN1_SUCCESS) { +// return NULL; +// } return cur; } @@ -381,12 +381,12 @@ int kee_ledger_parse(struct kee_ledger_t *ledger, const char *data, size_t data_ if (r != ASN1_SUCCESS) { return r; } - - c = 4096 - 64; - r = asn1_read_value(item, "body", ledger->body, &c); - if (r != ASN1_SUCCESS) { - return r; - } +// +// c = 4096 - 64; +// r = asn1_read_value(item, "body", ledger->body, &c); +// if (r != ASN1_SUCCESS) { +// return r; +// } return ERR_OK; } diff --git a/src/ledger.h b/src/ledger.h @@ -3,6 +3,8 @@ #include <time.h> +#include "content.h" + enum kee_initiator { ALICE, BOB, @@ -17,7 +19,7 @@ struct kee_ledger_item_t { time_t time; enum kee_initiator initiator; char response; - char body[4096]; + struct kee_content_t *content; }; struct kee_ledger_t { @@ -26,7 +28,7 @@ struct kee_ledger_t { char pubkey_bob[32]; char uoa_decimals; char uoa[64]; - char body[4032]; + struct kee_content_t *content; }; struct kee_ledger_item_t *kee_ledger_parse_item(struct kee_ledger_t *ledger, const char *data, size_t data_len); diff --git a/src/tests/Makefile b/src/tests/Makefile @@ -5,7 +5,7 @@ LINKOBJS := $(wildcard ../*.o) INCLUDES := -I.. CFLAGS += `pkg-config --cflags gtk4 gstreamer-1.0` $(INCLUDES) -Wall #LIBS := `pkg-config --libs gtk4 zlib lmdb libgcrypt libxdg-basedir gstreamer-1.0` -lb64 -lvarint -llash -LIBS := `pkg-config --libs gtk4 zlib lmdb libgcrypt libxdg-basedir gstreamer-1.0` -lb64 -llash -ltasn1 +LIBS := `pkg-config --libs gtk4 zlib lmdb libgcrypt libxdg-basedir gstreamer-1.0` -lb64 -llash -ltasn1 -lcmime LDFLAGS += $(LIBS) all: obj_debug $(OBJS) @@ -20,6 +20,7 @@ obj_debug: # ./$< test_run: ./test_cadir + ./test_content ./test_ledger test: all test_run diff --git a/src/tests/content.c b/src/tests/content.c @@ -0,0 +1,56 @@ +#include <string.h> + +#include "content.h" + +#include "hex.h" +#include "cadiz.h" + +const char hash_of_foo[] = "1382ea21c44e0352916e17267351fcc2d890816a254caba354b2da23c2b33a9c50c5b4151ddab876d199e3c0bca26fcf75302427db5ca05e5f049f2a9f8dc9d4"; + + +int main() { + int r; + Cadiz cadiz; + struct kee_content_t *content; + char digest[64]; + size_t l; + + hex2bin(hash_of_foo, (unsigned char*)digest); + + cadiz.locator = "./testdata_resource"; + +/// \todo this causes a character to be emitted to console after main +// l = 256; +// content = kee_content_new(digest, l); +// if (content == NULL) { +// return 1; +// } +// r = kee_content_resolve(content, &cadiz); +// if (r) { +// return 1; +// } +// kee_content_free(content); + + l = 4096; + content = kee_content_new(digest, l); + if (content == NULL) { + return 1; + } + r = kee_content_resolve(content, &cadiz); + if (r) { + return 1; + } + + if (content->flags != (KEE_CONTENT_RESOLVED_SUBJECT | KEE_CONTENT_RESOLVED_BODY | KEE_CONTENT_RESOLVED)) { + return 1; + } + + if (strcmp(content->subject, "Federal back single democratic growth fly image.")) { + kee_content_free(content); + return 1; + } + + kee_content_free(content); + + return 0; +} diff --git a/src/tests/testdata_resource/1382ea21c44e0352916e17267351fcc2d890816a254caba354b2da23c2b33a9c50c5b4151ddab876d199e3c0bca26fcf75302427db5ca05e5f049f2a9f8dc9d4 b/src/tests/testdata_resource/1382ea21c44e0352916e17267351fcc2d890816a254caba354b2da23c2b33a9c50c5b4151ddab876d199e3c0bca26fcf75302427db5ca05e5f049f2a9f8dc9d4 @@ -0,0 +1,7 @@ +Subject: Federal back single democratic growth fly image. +Content-Type: text/plain; charset="utf-8" +Content-Transfer-Encoding: quoted-printable +MIME-Version: 1.0 + +Relate single collection month difference. Spring perhaps maintain purpose tr= +aditional.