commit b64fb1cd9e9d23f3622ba121f4aec795782ad0c2
parent 3a55ef78d1ec7a69a869ff23d86dceb3099b43b0
Author: lash <dev@holbrook.no>
Date: Fri, 26 Apr 2024 15:04:06 +0100
WIP add transport payload generator
Diffstat:
4 files changed, 116 insertions(+), 9 deletions(-)
diff --git a/src/chunk.h b/src/chunk.h
@@ -0,0 +1,18 @@
+#ifndef KEE_CHUNK_H_
+#define KEE_CHUNK_H_
+
+#include <stddef.h>
+
+#ifndef KEE_TRANSPORT_CHUNK_MAX_SIZE
+#define KEE_TRANSPORT_CHUNK_MAX_SIZE 768
+#endif
+
+struct kee_chunk_t {
+ char *data;
+ size_t data_len;
+ size_t crsr;
+ short cardinality;
+ short number;
+};
+
+#endif // KEE_CHUNK_H_
diff --git a/src/err.h b/src/err.h
@@ -29,6 +29,8 @@ enum keeError {
ERR_KEYFAIL,
///
ERR_ALREADY_SIGNED,
+ //
+ ERR_INVALID_CMD,
};
#endif // _KEE_ERR_H
diff --git a/src/transport.c b/src/transport.c
@@ -141,3 +141,72 @@ int unpack(char *in, size_t in_len, char *out, size_t *out_len) {
free(buf);
return ERR_OK;
}
+
+/// \todo implement checksum
+int kee_transport_single(struct kee_transport_t *trans, char cmd, size_t data_len) {
+
+ memset(trans, 0, sizeof(struct kee_transport_header_t));
+ if (cmd >= KEE_N_CMD) {
+ return ERR_INVALID_CMD;
+ }
+
+ trans->chunk.data_len(in_len + 1);
+ trans->chunk.data = malloc(trans->chunk.data_len);
+ trans->cmd = trans->chunk.data;
+
+ return ERR_OK;
+}
+
+int kee_transport_write(struct kee_transport_t *trans, const char *in, size_t in_len) {
+ if (trans.state) {
+ return ERR_FAIL;
+ }
+ memcpy(trans->chunk.data + trans->chunk.crsr, in, in_len);
+ trans->chunk.crsr += in_len;
+ return ERR_OK;
+}
+
+/// \todo consider pass validation function
+/// \todo implement chunking
+int kee_transport_next(struct kee_transport_t *trans, enum kee_transport_mode_e mode, char *out, size_t *out_len) {
+ int r;
+ size_t l;
+
+ if (trans.state) {
+ if (trans.state > 1) {
+ return ERR_FAIL;
+ }
+ else {
+ trans->chunk.data_len = trans->chunk.crsr;
+ trans->chunk.crsr = 0;
+ trans->state = 1;
+ }
+
+ if (trans->remaining < KEE_TRANSPORT_CHUNK_SIZE - 1) {
+ l = trans_remaining;
+ } else {
+ return 1; // unimplemented
+ //l = KEE_TRANSPORT_CHUNK_SIZE;
+ }
+
+ *(trans->buf) = trans->cmd;
+ memcpy(trans->buf + 1, trans->data, l - 1);
+ switch (mode) {
+ case KEE_TRANSPORT_BASE64:
+ r = pack_encode(trans->data, trans->data_len + 1, out, out_len);
+ if (r) {
+ retrun ERR_FAIL;
+ }
+ break;
+ case KEE_TRANSPORT_RAW:
+ memcpy(out, trans->data, trans->data_len + 1);
+ break;
+ default:
+ retrun ERR_FAIL;
+ }
+ return 0;
+}
+
+void kee_transport_header_free() {
+ free(trans->buf);
+}
diff --git a/src/transport.h b/src/transport.h
@@ -1,11 +1,31 @@
-#ifndef _TRANSPORT_H
-#define _TRANSPORT_H
+#ifndef _KEE_TRANSPORT_H
+#define _KEE_TRANSPORT_H
#include <stddef.h>
-#ifdef __cplusplus
-extern "C" {
-#endif
+enum kee_transport_mode_e {
+ KEE_TRANSPORT_RAW,
+ KEE_TRANSPORT_BASE64,
+}
+
+enum kee_cmd_e { // max number 31
+ KEE_CMD_ID = 0,
+ KEE_CMD_LEDGER,
+ KEE_CMD_DELTA,
+ KEE_CMD_CLEAR,
+ KEE_N_CMD,
+};
+
+#define KEE_CMD_SIGN_RESPONSE 32
+#define KEE_CMD_SIGN_REQUEST 64
+#define KEE_CMD_CHUNKED 128
+
+struct kee_transport_header_t {
+ char *cmd;
+ struct kee_chunk_t chunker;
+ short checksum;
+ char state;
+};
/**
*
@@ -33,8 +53,6 @@ int pack(char *in, size_t in_len, char *out, size_t *out_len);
*/
int unpack(char *in, size_t in_len, char *out, size_t *out_len);
-#ifdef __cplusplus
-}
-#endif
+int kee_transport_single(enum kee_cmd_e cmd, int total);
-#endif // _TRANSPORT_H
+#endif // _KEE_TRANSPORT_H