From 4bc40995b4d1a55f50aa4ab00c493b483636f287 Mon Sep 17 00:00:00 2001 From: Carl Niklas Rydberg Date: Sat, 20 Dec 2025 11:14:06 +0100 Subject: [PATCH] Promote ASL reference hex helpers --- CMakeLists.txt | 1 + include/amduat/asl/ref_text.h | 27 +++++++++++ src/near_core/asl/ref_text.c | 79 ++++++++++++++++++++++++++++++++ src/tools/amduat_asl_cli.c | 85 +++-------------------------------- 4 files changed, 112 insertions(+), 80 deletions(-) create mode 100644 include/amduat/asl/ref_text.h create mode 100644 src/near_core/asl/ref_text.c diff --git a/CMakeLists.txt b/CMakeLists.txt index e9eca1d..a1d6719 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -62,6 +62,7 @@ set(AMDUAT_UTIL_SRCS set(AMDUAT_ASL_SRCS src/kernel/asl/core.c src/near_core/asl/store.c + src/near_core/asl/ref_text.c src/near_core/asl/registry.c ) diff --git a/include/amduat/asl/ref_text.h b/include/amduat/asl/ref_text.h new file mode 100644 index 0000000..2c9c3bb --- /dev/null +++ b/include/amduat/asl/ref_text.h @@ -0,0 +1,27 @@ +#ifndef AMDUAT_ASL_REF_TEXT_H +#define AMDUAT_ASL_REF_TEXT_H + +#include "amduat/asl/core.h" + +#include +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +/* Caller owns any heap allocations returned in out_hex or out_ref->digest. */ +bool amduat_asl_ref_encode_hex(amduat_reference_t ref, char **out_hex); + +bool amduat_asl_ref_decode_hex(const char *hex, amduat_reference_t *out_ref); + +bool amduat_asl_ref_decode_bytes(const uint8_t *bytes, + size_t len, + amduat_reference_t *out_ref); + +#ifdef __cplusplus +} /* extern "C" */ +#endif + +#endif /* AMDUAT_ASL_REF_TEXT_H */ diff --git a/src/near_core/asl/ref_text.c b/src/near_core/asl/ref_text.c new file mode 100644 index 0000000..1bf9a40 --- /dev/null +++ b/src/near_core/asl/ref_text.c @@ -0,0 +1,79 @@ +#include "amduat/asl/ref_text.h" + +#include "amduat/enc/asl1_core_codec.h" +#include "amduat/util/hex.h" + +#include +#include + +bool amduat_asl_ref_encode_hex(amduat_reference_t ref, char **out_hex) { + amduat_octets_t bytes; + char *hex; + size_t hex_size; + + if (out_hex == NULL) { + return false; + } + *out_hex = NULL; + + if (!amduat_enc_asl1_core_encode_reference_v1(ref, &bytes)) { + return false; + } + hex_size = amduat_hex_encoded_size(bytes.len); + if (hex_size == 0u) { + free((void *)bytes.data); + return false; + } + hex = (char *)malloc(hex_size); + if (hex == NULL) { + free((void *)bytes.data); + return false; + } + if (!amduat_hex_encode_lower(bytes.data, bytes.len, hex, hex_size)) { + free(hex); + free((void *)bytes.data); + return false; + } + free((void *)bytes.data); + *out_hex = hex; + return true; +} + +bool amduat_asl_ref_decode_hex(const char *hex, amduat_reference_t *out_ref) { + uint8_t *bytes; + size_t len; + const char *hex_text; + bool ok; + + if (hex == NULL || out_ref == NULL) { + return false; + } + + hex_text = hex; + if (hex_text[0] == '0' && (hex_text[1] == 'x' || hex_text[1] == 'X')) { + hex_text += 2; + } + + bytes = NULL; + len = 0; + if (!amduat_hex_decode_alloc(hex_text, &bytes, &len)) { + return false; + } + + ok = amduat_enc_asl1_core_decode_reference_v1( + amduat_octets(bytes, len), + out_ref); + free(bytes); + return ok; +} + +bool amduat_asl_ref_decode_bytes(const uint8_t *bytes, + size_t len, + amduat_reference_t *out_ref) { + if (out_ref == NULL || (len != 0u && bytes == NULL)) { + return false; + } + return amduat_enc_asl1_core_decode_reference_v1( + amduat_octets(bytes, len), + out_ref); +} diff --git a/src/tools/amduat_asl_cli.c b/src/tools/amduat_asl_cli.c index e4cb48c..6ef8f7f 100644 --- a/src/tools/amduat_asl_cli.c +++ b/src/tools/amduat_asl_cli.c @@ -1,11 +1,10 @@ #include "amduat/asl/asl_store_fs.h" #include "amduat/asl/asl_store_fs_meta.h" +#include "amduat/asl/ref_text.h" #include "amduat/asl/store.h" #include "amduat/enc/asl1_core.h" #include "amduat/enc/asl1_core_codec.h" #include "amduat/hash/asl1.h" -#include "amduat/util/hex.h" - #include #include #include @@ -443,80 +442,6 @@ static bool amduat_asl_cli_write_text_line(const char *path, return ok; } -static bool amduat_asl_cli_encode_reference_hex(amduat_reference_t ref, - char **out_hex) { - amduat_octets_t bytes; - char *hex; - size_t hex_size; - - if (out_hex == NULL) { - return false; - } - *out_hex = NULL; - - if (!amduat_enc_asl1_core_encode_reference_v1(ref, &bytes)) { - return false; - } - hex_size = amduat_hex_encoded_size(bytes.len); - if (hex_size == 0u) { - free((void *)bytes.data); - return false; - } - hex = (char *)malloc(hex_size); - if (hex == NULL) { - free((void *)bytes.data); - return false; - } - if (!amduat_hex_encode_lower(bytes.data, bytes.len, hex, hex_size)) { - free(hex); - free((void *)bytes.data); - return false; - } - free((void *)bytes.data); - *out_hex = hex; - return true; -} - -static bool amduat_asl_cli_decode_reference_hex(const char *hex, - amduat_reference_t *out_ref) { - uint8_t *bytes; - size_t len; - const char *hex_text; - bool ok; - - if (hex == NULL || out_ref == NULL) { - return false; - } - - hex_text = hex; - if (hex_text[0] == '0' && (hex_text[1] == 'x' || hex_text[1] == 'X')) { - hex_text += 2; - } - - bytes = NULL; - len = 0; - if (!amduat_hex_decode_alloc(hex_text, &bytes, &len)) { - return false; - } - - ok = amduat_enc_asl1_core_decode_reference_v1( - amduat_octets(bytes, len), - out_ref); - free(bytes); - return ok; -} - -static bool amduat_asl_cli_decode_reference_bytes(const uint8_t *bytes, - size_t len, - amduat_reference_t *out_ref) { - if (out_ref == NULL || (len != 0u && bytes == NULL)) { - return false; - } - return amduat_enc_asl1_core_decode_reference_v1( - amduat_octets(bytes, len), - out_ref); -} - static void amduat_asl_cli_free_reference(amduat_reference_t *ref) { if (ref == NULL) { return; @@ -798,7 +723,7 @@ static int amduat_asl_cli_cmd_put(int argc, char **argv) { } else { if (opts.ref_format == AMDUAT_ASL_CLI_REF_HEX) { char *hex_ref = NULL; - if (!amduat_asl_cli_encode_reference_hex(ref, &hex_ref)) { + if (!amduat_asl_ref_encode_hex(ref, &hex_ref)) { fprintf(stderr, "error: failed to encode reference\n"); exit_code = AMDUAT_ASL_CLI_EXIT_CODEC; } else { @@ -834,7 +759,7 @@ static int amduat_asl_cli_cmd_put(int argc, char **argv) { fprintf(stderr, "type_tag=0x%08x\n", (unsigned int)artifact.type_tag.tag_id); } - if (amduat_asl_cli_encode_reference_hex(ref, &hex_ref)) { + if (amduat_asl_ref_encode_hex(ref, &hex_ref)) { fprintf(stderr, "ref=%s\n", hex_ref); free(hex_ref); } @@ -949,7 +874,7 @@ static int amduat_asl_cli_cmd_get(int argc, char **argv) { memset(&ref, 0, sizeof(ref)); if (opts.ref_format == AMDUAT_ASL_CLI_REF_HEX) { - if (!amduat_asl_cli_decode_reference_hex(opts.ref, &ref)) { + if (!amduat_asl_ref_decode_hex(opts.ref, &ref)) { fprintf(stderr, "error: invalid hex reference\n"); return AMDUAT_ASL_CLI_EXIT_CODEC; } @@ -960,7 +885,7 @@ static int amduat_asl_cli_cmd_get(int argc, char **argv) { fprintf(stderr, "error: failed to read reference: %s\n", opts.ref); return AMDUAT_ASL_CLI_EXIT_IO; } - ok = amduat_asl_cli_decode_reference_bytes(ref_bytes, ref_len, &ref); + ok = amduat_asl_ref_decode_bytes(ref_bytes, ref_len, &ref); free(ref_bytes); if (!ok) { fprintf(stderr, "error: invalid reference bytes\n");