commit 72c396ea7af595204725790b1bf377930193ad70
parent 6b42cd0876cf42c91c89270818dfa0f89518ba4a
Author: lash <dev@holbrook.no>
Date: Sat, 15 Jun 2024 01:11:57 +0100
Move transport cli code to cli.c
Diffstat:
3 files changed, 57 insertions(+), 34 deletions(-)
diff --git a/src/cmd/cli.c b/src/cmd/cli.c
@@ -1,10 +1,12 @@
#include <string.h>
#include <stdlib.h>
+
#include <unistd.h>
#include "gpg.h"
#include "err.h"
#include "debug.h"
+#include "transport.h"
#include "cli.h"
@@ -52,6 +54,53 @@ int cli_init(struct kee_cli_t *cli, const char *passphrase) {
return ERR_OK;
}
+int cli_decode(struct kee_cli_t *cli, char *in, long unsigned int *in_size) {
+ int r;
+ r = kee_transport_single(&cli->trans, KEE_TRANSPORT_BASE64, KEE_CMD_IMPORT, 0);
+ if (r) {
+ debug_logerr(LLOG_CRITICAL, ERR_FAIL, "transport init fail");
+ return ERR_FAIL;
+ }
+
+ r = kee_transport_write(&cli->trans, in, *in_size);
+ if (r) {
+ debug_logerr(LLOG_CRITICAL, ERR_FAIL, "parse transport fail");
+ return ERR_FAIL;
+ }
+
+ *in_size = KEE_CLI_BUFMAX;
+ r = kee_transport_read(&cli->trans, in, in_size);
+ if (r) {
+ debug_logerr(LLOG_CRITICAL, ERR_FAIL, "unwrap transport fail");
+ return ERR_FAIL;
+ }
+ return ERR_OK;
+}
+
+int cli_encode(struct kee_cli_t *cli, char *out, long unsigned int *out_size) {
+ int r;
+
+ r = kee_transport_single(&cli->trans, KEE_TRANSPORT_BASE64, KEE_CMD_LEDGER, KEE_CLI_BUFMAX);
+ if (r) {
+ debug_logerr(LLOG_CRITICAL, ERR_FAIL, "transport output init fail");
+ return ERR_FAIL;
+ }
+
+ r = kee_transport_write(&cli->trans, out, *out_size);
+ if (r) {
+ debug_logerr(LLOG_CRITICAL, ERR_FAIL, "transport output process fail");
+ return ERR_FAIL;
+ }
+
+ *out_size = KEE_CLI_BUFMAX;
+ r = kee_transport_next(&cli->trans, out, out_size);
+ if (r) {
+ debug_logerr(LLOG_CRITICAL, ERR_FAIL, "transport output writeout fail");
+ return ERR_FAIL;
+ }
+ return ERR_OK;
+}
+
int cli_exit(struct kee_cli_t *cli, int err) {
int f;
int c;
diff --git a/src/cmd/cli.h b/src/cmd/cli.h
@@ -9,6 +9,7 @@
struct kee_cli_t {
struct kee_settings settings;
struct gpg_store gpg;
+ struct kee_transport_t trans;
char *passphrase;
char *result;
size_t result_len;
@@ -16,5 +17,7 @@ struct kee_cli_t {
int cli_init(struct kee_cli_t *cli, const char *passphrase);
int cli_exit(struct kee_cli_t *cli, int err);
+int cli_decode(struct kee_cli_t *cli, char *in, long unsigned int *in_size);
+int cli_encode(struct kee_cli_t *cli, char *out, long unsigned int *out_size);
#endif // KEE_CLI_H_
diff --git a/src/cmd/sign.c b/src/cmd/sign.c
@@ -20,7 +20,6 @@ void debug_log(int lvl, const char *s) {
int main(int argc, char **argv) {
- struct kee_transport_t trans;
struct kee_ledger_t ledger;
struct kee_cli_t cli;
char dbg[4096];
@@ -67,25 +66,11 @@ int main(int argc, char **argv) {
sprintf(dbg, "Read %lu bytes from %s", c, *(argv+1));
debug_log(DEBUG_INFO, dbg);
- r = kee_transport_single(&trans, KEE_TRANSPORT_BASE64, KEE_CMD_IMPORT, 0);
+ r = cli_decode(&cli, b, &c);
if (r) {
- debug_logerr(LLOG_CRITICAL, ERR_FAIL, "transport init fail");
- return cli_exit(&cli, ERR_FAIL);
- }
-
- r = kee_transport_write(&trans, b, c);
- if (r) {
- debug_logerr(LLOG_CRITICAL, ERR_FAIL, "parse transport fail");
- return cli_exit(&cli, ERR_FAIL);
+ return cli_exit(&cli, r);
}
-
- c = KEE_CLI_BUFMAX;
- r = kee_transport_read(&trans, b, &c);
- if (r) {
- debug_logerr(LLOG_CRITICAL, ERR_FAIL, "unwrap transport fail");
- return cli_exit(&cli, ERR_FAIL);
- }
-
+
r = kee_ledger_parse_open(&ledger, &cli.gpg, b, c);
if (r) {
debug_logerr(LLOG_CRITICAL, ERR_FAIL, "not valid ledger data");
@@ -108,23 +93,9 @@ int main(int argc, char **argv) {
return cli_exit(&cli, ERR_FAIL);
}
- r = kee_transport_single(&trans, KEE_TRANSPORT_BASE64, KEE_CMD_LEDGER, KEE_CLI_BUFMAX);
+ r = cli_encode(&cli, b, &c);
if (r) {
- debug_logerr(LLOG_CRITICAL, ERR_FAIL, "transport output init fail");
- return cli_exit(&cli, ERR_FAIL);
- }
-
- r = kee_transport_write(&trans, b, c);
- if (r) {
- debug_logerr(LLOG_CRITICAL, ERR_FAIL, "transport output process fail");
- return cli_exit(&cli, ERR_FAIL);
- }
-
- c = KEE_CLI_BUFMAX;
- r = kee_transport_next(&trans, b, &c);
- if (r) {
- debug_logerr(LLOG_CRITICAL, ERR_FAIL, "transport output writeout fail");
- return cli_exit(&cli, ERR_FAIL);
+ return cli_exit(&cli, r);
}
cli.result = b;