commit afb77164ac5170cd713ff5b94db1d76855ada1e4
parent 7118562dab25726fada98981dc39347e44ecd8aa
Author: lash <dev@holbrook.no>
Date: Sat, 20 Apr 2024 09:42:33 +0100
Implement content resolver to receive content key from ledger
Diffstat:
5 files changed, 40 insertions(+), 52 deletions(-)
diff --git a/src/content.c b/src/content.c
@@ -7,12 +7,7 @@
#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;
- }
-
+int kee_content_init(struct kee_content_t *content, const char *key, size_t size_hint) {
if (size_hint) {
content->mem_size = size_hint;
} else {
@@ -21,14 +16,13 @@ struct kee_content_t* kee_content_new(const char *key, size_t size_hint) {
content->mem = malloc(content->mem_size);
if (content->mem == NULL) {
- free(content);
- return NULL;
+ return 1;
}
content->body = content->mem;
content->flags = 0;
memcpy(content->key, key, KEE_CONTENT_KEY_SIZE);
- return content;
+ return ERR_OK;
}
int kee_content_resolve(struct kee_content_t *content, Cadiz *cadiz) {
@@ -65,5 +59,4 @@ int kee_content_resolve(struct kee_content_t *content, Cadiz *cadiz) {
void kee_content_free(struct kee_content_t *content) {
free(content->mem);
- free(content);
}
diff --git a/src/content.h b/src/content.h
@@ -21,7 +21,7 @@ struct kee_content_t {
char flags;
};
-struct kee_content_t* kee_content_new(const char *key, size_t size_hint);
+int kee_content_init(struct kee_content_t *content, 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);
diff --git a/src/ledger.c b/src/ledger.c
@@ -10,6 +10,7 @@
#include "debug.h"
#include "digest.h"
#include "strip.h"
+#include "content.h"
extern const asn1_static_node schema_entry_asn1_tab[];
@@ -17,23 +18,6 @@ extern const asn1_static_node schema_entry_asn1_tab[];
int kee_ledger_resolve(struct kee_ledger_t *ledger, struct Cadiz *resolver) {
return ERR_OK;
}
-//
-// if (o->resolver) {
-// 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 -= c;
-// }
-// }
static char *get_message(asn1_node item, char *out_digest, char *out_data, size_t *out_len) {
@@ -224,7 +208,7 @@ struct kee_ledger_item_t *kee_ledger_parse_item(struct kee_ledger_t *ledger, con
int *collateral_delta;
const char *pubkey_first;
const char *pubkey_last;
- char tmp[8];
+ char tmp[64];
int v;
@@ -305,12 +289,16 @@ 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 = 64;
+ r = asn1_read_value(item, "body", tmp, &c);
+ if (r != ASN1_SUCCESS) {
+ return NULL;
+ }
+ r = kee_content_init(&(ledger->content), tmp, 0);
+ if (r) {
+ return NULL;
+ }
return cur;
}
@@ -332,9 +320,10 @@ int kee_ledger_parse(struct kee_ledger_t *ledger, const char *data, size_t data_
asn1_node root;
asn1_node item;
int c;
- //CMimeMessage_T *msg;
+ char content_key[64];
memset(ledger, 0, sizeof(struct kee_ledger_t));
+
memset(&root, 0, sizeof(root));
memset(&item, 0, sizeof(item));
r = asn1_array2tree(schema_entry_asn1_tab, &root, err);
@@ -381,12 +370,17 @@ 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 = 64;
+ r = asn1_read_value(item, "body", content_key, &c);
+ if (r != ASN1_SUCCESS) {
+ return r;
+ }
+ r = kee_content_init(&(ledger->content), content_key, 0);
+ if (r) {
+ return 1;
+ }
return ERR_OK;
}
diff --git a/src/ledger.h b/src/ledger.h
@@ -19,7 +19,7 @@ struct kee_ledger_item_t {
time_t time;
enum kee_initiator initiator;
char response;
- struct kee_content_t *content;
+ struct kee_content_t content;
};
struct kee_ledger_t {
@@ -28,7 +28,7 @@ struct kee_ledger_t {
char pubkey_bob[32];
char uoa_decimals;
char uoa[64];
- struct kee_content_t *content;
+ 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/content.c b/src/tests/content.c
@@ -11,7 +11,8 @@ const char hash_of_foo[] = "1382ea21c44e0352916e17267351fcc2d890816a254caba354b2
int main() {
int r;
Cadiz cadiz;
- struct kee_content_t *content;
+ //struct kee_content_t *content;
+ struct kee_content_t content;
char digest[64];
size_t l;
@@ -32,25 +33,25 @@ int main() {
// kee_content_free(content);
l = 4096;
- content = kee_content_new(digest, l);
- if (content == NULL) {
+ r = kee_content_init(&content, digest, l);
+ if (r) {
return 1;
}
- r = kee_content_resolve(content, &cadiz);
+ r = kee_content_resolve(&content, &cadiz);
if (r) {
return 1;
}
- if (content->flags != (KEE_CONTENT_RESOLVED_SUBJECT | KEE_CONTENT_RESOLVED_BODY | KEE_CONTENT_RESOLVED)) {
+ 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);
+ if (strcmp(content.subject, "Federal back single democratic growth fly image.")) {
+ kee_content_free(&content);
return 1;
}
- kee_content_free(content);
+ kee_content_free(&content);
return 0;
}