kee

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

commit 4c1d61c490a1c90eb577da135f5f22a6dec2fa58
parent 927132e4aa022fc30de410894493c10af3b6c54c
Author: lash <dev@holbrook.no>
Date:   Sat, 27 Apr 2024 22:54:41 +0100

WIP validate function for merged ledger item msg

Diffstat:
Msrc/tests/transport.c | 8+++++++-
Msrc/transport.c | 79++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-----------
Msrc/transport.h | 4++--
3 files changed, 77 insertions(+), 14 deletions(-)

diff --git a/src/tests/transport.c b/src/tests/transport.c @@ -77,6 +77,7 @@ int test_msg() { struct kee_test_t t; struct kee_transport_t ledger_transport; struct kee_transport_t item_transport; + struct kee_transport_t merged_transport; char out[1024]; size_t out_len; @@ -112,7 +113,12 @@ int test_msg() { } out_len = 1024; - r = kee_transport_encode_ledger(&ledger_transport, &item_transport, out, &out_len); + r = kee_transport_encode_ledger(&ledger_transport, &item_transport, &merged_transport, KEE_TRANSPORT_RAW); + if (r) { + return 1; + } + + r = kee_transport_validate(&merged_transport); if (r) { return 1; } diff --git a/src/transport.c b/src/transport.c @@ -252,31 +252,77 @@ int kee_transport_read(struct kee_transport_t *trans, char *out, size_t *out_len return ERR_OK; } -enum kee_cmd_e kee_transport_cmd(struct kee_transport_t *trans) { - return *trans->cmd & 0x1f; +static int validate_multi(char *data, size_t data_len) { + char *p; + size_t c; + unsigned short l; + + c = 0; + p = data; + while (c < data_len - 1) { + memcpy(&l, p, sizeof(unsigned short)); + if (l == 0) { + break; + } + if (is_le()) { + flip_endian(2, &l); + } + c += l + sizeof(unsigned short); + p += c; + } + if (p == data) { + return ERR_FAIL; + } + return c != data_len - 1; +} + +/// \todo check state +int kee_transport_validate(struct kee_transport_t *trans) { + int r; + char cmd; + + cmd = *trans->cmd & 0x1f; + + if (cmd == 0) { + r = validate_multi(trans->chunker.data + 1, trans->chunker.data_len); + if (r) { + return ERR_FAIL; + } + } + return ERR_OK; } -int kee_transport_encode_ledger(struct kee_transport_t *trans_ledger, struct kee_transport_t *trans_item, char *out, size_t *out_len) { + +int kee_transport_encode_ledger(struct kee_transport_t *trans_ledger, struct kee_transport_t *trans_item, struct kee_transport_t *trans_out, enum kee_transport_mode_e mode) { int r; char *p; unsigned short part_length; size_t l; + size_t c; + char *out; - *out_len = 0; - *out = 0; - p = out + 1; + l = sizeof(unsigned short); + out = malloc(trans_ledger->chunker.data_len + trans_item->chunker.data_len + (l * 2) + 1); + // only use raw mode for this, since we are joining data + if (trans_ledger->mode != KEE_TRANSPORT_RAW) { + return ERR_FAIL; + } + if (trans_item->mode != KEE_TRANSPORT_RAW) { + return ERR_FAIL; + } + c = 0; + p = out; part_length = (unsigned short)trans_ledger->chunker.data_len; r = to_endian(TO_ENDIAN_BIG, 2, &part_length); if (r) { return ERR_FAIL; } - l = sizeof(unsigned short); memcpy(p, &part_length, l); p += sizeof(unsigned short); - *out_len += l; + c += l; memcpy(p, trans_ledger->chunker.data, trans_ledger->chunker.data_len); p += trans_ledger->chunker.data_len; - *out_len += trans_ledger->chunker.data_len; + c += trans_ledger->chunker.data_len; part_length = (unsigned short)trans_item->chunker.data_len; r = to_endian(TO_ENDIAN_BIG, 2, &part_length); @@ -285,10 +331,21 @@ int kee_transport_encode_ledger(struct kee_transport_t *trans_ledger, struct kee } memcpy(p, &part_length, l); p += l; - *out_len += l; + c += l; memcpy(p, trans_item->chunker.data, trans_item->chunker.data_len); p += trans_item->chunker.data_len; - *out_len += trans_item->chunker.data_len; + c += trans_item->chunker.data_len; + + r = kee_transport_single(trans_out, mode, KEE_CMD_PACKED, c); + if (r) { + return ERR_FAIL; + } + r = kee_transport_write(trans_out, out, c); + if (r) { + return ERR_FAIL; + } + + free(out); return ERR_OK; } diff --git a/src/transport.h b/src/transport.h @@ -64,6 +64,6 @@ void kee_transport_set_response(struct kee_transport_t *trans); int kee_transport_import(struct kee_transport_t *trans, enum kee_transport_mode_e mode, const char *data, size_t data_len); int kee_transport_read(struct kee_transport_t *trans, char *out, size_t *out_len); -int kee_transport_encode_ledger(struct kee_transport_t *trans_ledger, struct kee_transport_t *trans_item, char *out, size_t *out_len); - +int kee_transport_encode_ledger(struct kee_transport_t *trans_ledger, struct kee_transport_t *trans_item, struct kee_transport_t *trans_out, enum kee_transport_mode_e mode); +int kee_transport_validate(struct kee_transport_t *trans); #endif // _KEE_TRANSPORT_H