commit 639befd4555da2ba63a05c28b14c3e212c3c1b85
parent a0191f3719f96690ba1617054b6ce733f5a57293
Author: lash <dev@holbrook.no>
Date: Thu, 2 May 2024 11:31:25 +0100
Add import command abstraction in transport (badly)
Diffstat:
7 files changed, 119 insertions(+), 46 deletions(-)
diff --git a/src/gtk/kee-import.c b/src/gtk/kee-import.c
@@ -9,6 +9,7 @@
#include "scan.h"
#include "err.h"
#include "transport.h"
+#include "ledger.h"
typedef struct {
@@ -20,6 +21,8 @@ struct _KeeImportClass {
struct _KeeImport {
GtkWidget parent;
+ char cmd_accept;
+ char cmd_count;
KeeMenu *win;
GListModel *camera_list;
struct kee_camera_devices camera_device;
@@ -94,6 +97,8 @@ static void kee_import_init(KeeImport *o) {
memset(&o->scan, 0, sizeof(struct kee_scanner));
o->stack = GTK_STACK(gtk_stack_new());
o->viewbox = GTK_BOX(gtk_box_new(GTK_ORIENTATION_VERTICAL, 0));
+ /// \todo remember to reset this
+ o->cmd_accept = 0xff;
}
static void kee_import_handle_camera_change(GtkDropDown *chooser, GParamSpec *spec, KeeImport *import) {
@@ -128,6 +133,27 @@ static void kee_import_handle_import_data_text(KeeImport *o, GString *v, GtkText
g_action_activate(act, NULL);
}
+static void accept_cmd(KeeImport *o, char cmd) {
+ o->cmd_accept = cmd;
+}
+
+static int check_cmd(KeeImport *o, char cmd) {
+ return 1;
+}
+
+
+static int import_ledger(KeeImport *o, const char *in, size_t in_len) {
+ int r;
+ struct kee_ledger_t ledger;
+
+ r = kee_ledger_parse_open(&ledger, in, in_len);
+ if (r) {
+ return r;
+ }
+
+ return ERR_OK;
+}
+
static void kee_import_handle_import_data_accept(KeeImport *o, GString *v, GtkStack *stack) {
int r;
char *s;
@@ -138,7 +164,14 @@ static void kee_import_handle_import_data_accept(KeeImport *o, GString *v, GtkSt
s = (char*)v->str;
c = strlen(s) + 1;
- r = kee_transport_import(&trans, KEE_TRANSPORT_BASE64, s, c);
+ //r = kee_transport_import(&trans, KEE_TRANSPORT_BASE64, s, c);
+ r = kee_transport_single(&trans, KEE_TRANSPORT_BASE64, KEE_CMD_IMPORT, 0);
+ if (r) {
+ return;
+ }
+
+ //r = kee_transport_read(&trans, b, &c);
+ r = kee_transport_write(&trans, s, c);
if (r) {
return;
}
@@ -148,7 +181,20 @@ static void kee_import_handle_import_data_accept(KeeImport *o, GString *v, GtkSt
if (r) {
return;
}
- g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "import accept");
+
+ switch(*trans.cmd) {
+ case KEE_CMD_LEDGER:
+ r = import_ledger(o, b, c);
+ break;
+ default:
+ r = ERR_FAIL;
+ }
+
+ if (r) {
+ g_log(G_LOG_DOMAIN, G_LOG_LEVEL_INFO, "failed import");
+ } else {
+ g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "accepted import");
+ }
}
//static void kee_import_handle_import_data_check(KeeImport *o, const char *data, GtkActionable *act) {
diff --git a/src/gtk/ui.c b/src/gtk/ui.c
@@ -33,26 +33,26 @@ static void ui_handle_unlock(KeeKey *o, KeeMenu *menu) {
kee_menu_prev(menu);
}
-static void ui_handle_import(KeeImport *import, GString *v, KeeMenu *menu) {
- //GtkWidget *widget;
- int r;
- struct kee_transport_t trans;
- char *s;
-
- s = (char*)v->str;
- r = kee_transport_import(&trans, KEE_TRANSPORT_BASE64, s, strlen(s) + 1);
- if (r) {
- debug_log(DEBUG_INFO, "invalid input for transport");
- return;
- }
- //kee_transport_read(&trans);
-
- //switch(kee_transport_cmd(&trans)) {
- // case KEE_CMD_DELTA:
- // widget = kee_menu_next("item");
- //}
-
-}
+//static void ui_handle_import(KeeImport *import, GString *v, KeeMenu *menu) {
+// //GtkWidget *widget;
+// int r;
+// struct kee_transport_t trans;
+// char *s;
+//
+// s = (char*)v->str;
+// r = kee_transport_import(&trans, KEE_TRANSPORT_BASE64, s, strlen(s) + 1);
+// if (r) {
+// debug_log(DEBUG_INFO, "invalid input for transport");
+// return;
+// }
+// //kee_transport_read(&trans);
+//
+// //switch(kee_transport_cmd(&trans)) {
+// // case KEE_CMD_DELTA:
+// // widget = kee_menu_next("item");
+// //}
+//
+//}
//static GtkWidget* ui_build_view(KeeMenu *menu) {
// GtkListItemFactory *factory;
@@ -93,7 +93,7 @@ void ui_build(GtkApplication *gapp, struct kee_context *ctx) {
import = kee_import_new(win);
kee_menu_add(win, "import", GTK_WIDGET(import));
- g_signal_connect(import, "data_available", G_CALLBACK(ui_handle_import), win);
+ //g_signal_connect(import, "data_available", G_CALLBACK(ui_handle_import), win);
widget = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
kee_menu_add(win, "entry", widget);
diff --git a/src/ledger.c b/src/ledger.c
@@ -827,7 +827,7 @@ int kee_ledger_serialize_open(struct kee_ledger_t *ledger, char *out, size_t *ou
return ERR_OK;
}
-int kee_ledger_parse_open(struct kee_ledger_t *ledger, char *in, size_t in_len) {
+int kee_ledger_parse_open(struct kee_ledger_t *ledger, const char *in, size_t in_len) {
int r;
char err[1024];
char b[1024];
diff --git a/src/ledger.h b/src/ledger.h
@@ -66,7 +66,7 @@ int kee_ledger_sign(struct kee_ledger_t *ledger, struct kee_ledger_item_t *item,
void kee_ledger_item_init(struct kee_ledger_item_t *item);
int kee_ledger_item_serialize(struct kee_ledger_item_t *item, char *out, size_t *out_len, enum kee_item_serialize_mode_e mode);
int kee_ledger_serialize_open(struct kee_ledger_t *ledger, char *out, size_t *out_len);
-int kee_ledger_parse_open(struct kee_ledger_t *ledger, char *in, size_t in_len);
+int kee_ledger_parse_open(struct kee_ledger_t *ledger, const char *in, size_t in_len);
int kee_ledger_put(struct kee_ledger_t *ledger, struct db_ctx *db);
int kee_ledger_item_put(struct kee_ledger_t *ledger, struct db_ctx *db, int idx);
int kee_ledger_verify(struct kee_ledger_t *ledger, int *err_idx);
diff --git a/src/tests/transport.c b/src/tests/transport.c
@@ -57,11 +57,18 @@ int test_pack() {
return 1;
}
- r = kee_transport_import(&hi, KEE_TRANSPORT_BASE64, out, c);
+ //r = kee_transport_import(&hi, KEE_TRANSPORT_BASE64, c);
+ r = kee_transport_single(&hi, KEE_TRANSPORT_BASE64, KEE_CMD_IMPORT, 0);
if (r) {
return 1;
}
+ r = kee_transport_write(&hi, out, c);
+ if (r) {
+ return 1;
+ }
+
+ c = 1024;
r = kee_transport_read(&hi, out, &c);
if (r) {
return 1;
diff --git a/src/transport.c b/src/transport.c
@@ -155,9 +155,13 @@ int kee_transport_single(struct kee_transport_t *trans, enum kee_transport_mode_
if (cmd >= KEE_N_CMD) {
return ERR_INVALID_CMD;
}
-
- trans->chunker.data_len = data_len + 1;
- trans->cmd = (char*)trans->chunker.data;
+ if (data_len == 0) {
+ trans->chunker.data_len = KEE_TRANSPORT_CHUNK_MAX_SIZE;
+ } else {
+ trans->chunker.data_len = data_len + 1;
+ }
+ trans->data = trans->chunker.data;
+ trans->cmd = (char*)trans->data;
trans->mode = mode;
*(trans->cmd) = cmd;
trans->chunker.crsr = 1;
@@ -165,6 +169,21 @@ int kee_transport_single(struct kee_transport_t *trans, enum kee_transport_mode_
return ERR_OK;
}
+//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_import(struct kee_transport_t *trans, enum kee_transport_mode_e mode) {
+// memset(trans, 0, sizeof(struct kee_transport_t));
+// //memcpy(trans->chunker.data, data, data_len);
+// //if (*data & KEE_CMD_CHUNKED) {
+// // return ERR_UNSUPPORTED;
+// //}
+// trans->chunker.crsr = 1;
+// trans->chunker.data_len = KEE_TRANSPORT_CHUNK_MAX_SIZE;
+// trans->mode = mode;
+// *trans->cmd = KEE_CMD_IMPORT;
+//
+// return ERR_OK;
+//}
+
void kee_transport_set_response(struct kee_transport_t *trans) {
if (trans->state == 0) {
*trans->cmd |= KEE_CMD_SIGN_RESPONSE;
@@ -175,7 +194,7 @@ int kee_transport_write(struct kee_transport_t *trans, const char *in, size_t in
if (trans->state) {
return ERR_FAIL;
}
- memcpy(trans->chunker.data + trans->chunker.crsr, in, in_len);
+ memcpy(trans->data + trans->chunker.crsr, in, in_len);
trans->chunker.crsr += in_len;
return ERR_OK;
}
@@ -222,36 +241,35 @@ int kee_transport_next(struct kee_transport_t *trans, char *out, size_t *out_len
return 0;
}
-int kee_transport_import(struct kee_transport_t *trans, enum kee_transport_mode_e mode, const char *data, size_t data_len) {
- memset(trans, 0, sizeof(struct kee_transport_t));
- memcpy(trans->chunker.data, data, data_len);
- if (*data & KEE_CMD_CHUNKED) {
- return ERR_UNSUPPORTED;
- }
- trans->chunker.crsr = 1;
- trans->chunker.data_len = data_len - 1;
- trans->mode = mode;
-
- return ERR_OK;
-}
int kee_transport_read(struct kee_transport_t *trans, char *out, size_t *out_len) {
int r;
+ if (*trans->cmd == KEE_CMD_IMPORT) {
+ trans->data++;
+ }
+
switch (trans->mode) {
case KEE_TRANSPORT_BASE64:
- r = unpack(trans->chunker.data, trans->chunker.data_len, out, out_len);
+ r = unpack(trans->data, trans->chunker.data_len, out, out_len);
if (r) {
return ERR_FAIL;
}
break;
case KEE_TRANSPORT_RAW:
- memcpy(out, trans->chunker.data, trans->chunker.data_len);
+ memcpy(out, trans->data, trans->chunker.data_len);
break;
default:
return ERR_FAIL;
}
+ if (*trans->cmd == KEE_CMD_IMPORT) {
+ /// \todo handle this crop better, should not require any of these copies
+ *trans->cmd = *out;
+ memcpy(trans->data, out+1, *out_len);
+ memcpy(out, trans->data, (*out_len)-1);
+ }
+
return ERR_OK;
}
diff --git a/src/transport.h b/src/transport.h
@@ -11,11 +11,12 @@ enum kee_transport_mode_e {
};
enum kee_cmd_e { // max number 31
- KEE_CMD_PACKED = 0,
+ KEE_CMD_IMPORT = 0,
KEE_CMD_ID,
KEE_CMD_LEDGER,
KEE_CMD_DELTA,
KEE_CMD_CLEAR,
+ KEE_CMD_PACKED,
KEE_N_CMD,
KEE_INVALID_CMD,
};
@@ -29,6 +30,7 @@ struct kee_transport_t {
struct kee_chunk_t chunker;
short checksum;
char state;
+ char *data;
};
/**
@@ -61,7 +63,7 @@ int kee_transport_single(struct kee_transport_t *trans, enum kee_transport_mode_
int kee_transport_write(struct kee_transport_t *trans, const char *in, size_t in_len);
int kee_transport_next(struct kee_transport_t *trans, char *out, size_t *out_len);
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_import(struct kee_transport_t *trans, enum kee_transport_mode_e mode, size_t decoded_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, struct kee_transport_t *trans_out, enum kee_transport_mode_e mode);