commit 09ca913e0591522a128c1fff200046f7da9587be
parent d389cad0a803536e3b1e9c62cadb58ee8ad27430
Author: lash <dev@holbrook.no>
Date: Sat, 27 Apr 2024 15:33:30 +0100
Add transport decoder
Diffstat:
7 files changed, 79 insertions(+), 3 deletions(-)
diff --git a/src/err.h b/src/err.h
@@ -32,6 +32,7 @@ enum keeError {
ERR_QR_MISSING,
ERR_QR_INVALID,
ERR_SPACE,
+ ERR_UNSUPPORTED,
};
#endif // _KEE_ERR_H
diff --git a/src/gtk/kee-entry-item.c b/src/gtk/kee-entry-item.c
@@ -71,11 +71,35 @@ void kee_entry_item_apply_list_item_widget(KeeEntryItem *o) {
kee_content_resolve(&o->item->content, o->resolver);
sprintf(o->header, "%s\nalice: %i\nbob: %i\n", o->item->content.subject, o->item->alice_credit_delta, o->item->bob_credit_delta);
+
+ widget = gtk_widget_get_first_child(GTK_WIDGET(o));
+ if (widget) {
+ gtk_box_remove(GTK_BOX(o), widget);
+ }
widget = gtk_label_new(o->header);
gtk_box_append(GTK_BOX(o), widget);
- return;
}
+void kee_entry_item_apply_sign_widget(KeeEntryItem *o) {
+ GtkBox *box;
+ GtkWidget *widget;
+
+ widget = gtk_widget_get_first_child(GTK_WIDGET(o));
+ if (widget) {
+ gtk_box_remove(GTK_BOX(o), widget);
+ }
+
+ widget = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
+ gtk_box_append(GTK_BOX(o), widget);
+ box = GTK_BOX(widget);
+
+ widget = gtk_label_new(o->header);
+ gtk_box_append(box, widget);
+
+ widget = gtk_entry_new();
+ gtk_entry_set_placeholder_text(GTK_ENTRY(widget), "passphrase");
+ gtk_box_append(box, widget);
+}
void kee_entry_item_apply_edit_widget(GtkBox *box, struct kee_entry_item_form_t *form, int first) {
GtkWidget *widget;
@@ -103,3 +127,4 @@ void kee_entry_item_apply_edit_widget(GtkBox *box, struct kee_entry_item_form_t
gtk_box_append(box, widget);
}
+
diff --git a/src/gtk/kee-transport.c b/src/gtk/kee-transport.c
@@ -36,6 +36,7 @@ static void kee_transport_class_init(KeeTransportClass *kls) {
object_class->finalize = kee_transport_finalize;
}
+/// \todo >6MB at module size 7, later copied to gbytes. improve if possible.
static void kee_transport_init(KeeTransport *o) {
o->image_data = malloc(QR_IMAGE_SIZE);
}
@@ -93,6 +94,7 @@ void kee_transport_handle_qr(GAction *Act, GVariant *v, KeeTransport *o) {
return;
}
+ // make rectangles in module size from qr data
p = o->image_data;
width_pixels = o->image_width * QR_IMAGE_COMPONENTS * QR_IMAGE_MODULE_SIZE;
for (i = 0; i < (int)(o->image_width * o->image_width); i++) {
diff --git a/src/gtk/ui.c b/src/gtk/ui.c
@@ -70,6 +70,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(kee_transport_handle_read));
widget = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
kee_menu_add(win, "entry", widget);
@@ -80,7 +81,7 @@ void ui_build(GtkApplication *gapp, struct kee_context *ctx) {
/// \todo make kee-entry action map/group
act = g_simple_action_new("qr", G_VARIANT_TYPE_STRING);
g_action_map_add_action(G_ACTION_MAP(gapp), G_ACTION(act));
- g_signal_connect(act, "activate", G_CALLBACK(kee_transport_handle_qr), trans);
+ g_signal_connect(act, "activate", G_CALLBACK(kee_transport_handle_in), trans);
gtk_window_present(GTK_WINDOW (win));
}
diff --git a/src/tests/transport.c b/src/tests/transport.c
@@ -36,6 +36,7 @@ int test_pack() {
int r;
size_t c;
struct kee_transport_t h;
+ struct kee_transport_t hi;
char out[1024];
r = kee_transport_single(&h, KEE_TRANSPORT_BASE64, 1, 1024);
@@ -55,9 +56,20 @@ int test_pack() {
return 1;
}
+ r = kee_transport_import(&hi, KEE_TRANSPORT_BASE64, out, c);
+ if (r) {
+ return 1;
+ }
+
+ r = kee_transport_read(&hi, out, &c);
+ if (r) {
+ return 1;
+ }
+
return 0;
}
+
int main() {
int r;
diff --git a/src/transport.c b/src/transport.c
@@ -148,7 +148,6 @@ int unpack(char *in, size_t in_len, char *out, size_t *out_len) {
/// \todo implement checksum
int kee_transport_single(struct kee_transport_t *trans, enum kee_transport_mode_e mode, char cmd, size_t data_len) {
-
memset(trans, 0, sizeof(struct kee_transport_t));
if (cmd >= KEE_N_CMD) {
return ERR_INVALID_CMD;
@@ -203,6 +202,7 @@ int kee_transport_next(struct kee_transport_t *trans, char *out, size_t *out_len
//l = KEE_TRANSPORT_CHUNK_SIZE;
}
+ /// \todo when chunking, dont forget this as a separate step before start to chunk
switch (trans->mode) {
case KEE_TRANSPORT_BASE64:
r = pack(trans->chunker.data, l, out, out_len);
@@ -218,3 +218,36 @@ 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;
+
+ switch (trans->mode) {
+ case KEE_TRANSPORT_BASE64:
+ r = unpack(trans->chunker.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);
+ break;
+ default:
+ return ERR_FAIL;
+ }
+
+ return ERR_OK;
+}
diff --git a/src/transport.h b/src/transport.h
@@ -59,5 +59,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_read(struct kee_transport_t *trans, char *out, size_t *out_len);
#endif // _KEE_TRANSPORT_H