From f48b73b75f6e35754a0ecf7b22a99a01052663d0 Mon Sep 17 00:00:00 2001 From: Carl Niklas Rydberg Date: Sat, 20 Dec 2025 11:16:16 +0100 Subject: [PATCH] Promote ASL ID parsing helpers --- CMakeLists.txt | 1 + include/amduat/asl/parse.h | 29 +++++++++ src/near_core/asl/parse.c | 114 ++++++++++++++++++++++++++++++++++ src/tools/amduat_asl_cli.c | 121 ++----------------------------------- 4 files changed, 149 insertions(+), 116 deletions(-) create mode 100644 include/amduat/asl/parse.h create mode 100644 src/near_core/asl/parse.c diff --git a/CMakeLists.txt b/CMakeLists.txt index a1d6719..fa6af33 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -61,6 +61,7 @@ set(AMDUAT_UTIL_SRCS set(AMDUAT_ASL_SRCS src/kernel/asl/core.c + src/near_core/asl/parse.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/parse.h b/include/amduat/asl/parse.h new file mode 100644 index 0000000..113b7df --- /dev/null +++ b/include/amduat/asl/parse.h @@ -0,0 +1,29 @@ +#ifndef AMDUAT_ASL_PARSE_H +#define AMDUAT_ASL_PARSE_H + +#include "amduat/asl/core.h" +#include "amduat/asl/store.h" + +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +bool amduat_asl_parse_u16(const char *text, uint16_t *out); +bool amduat_asl_parse_u32(const char *text, uint32_t *out); + +bool amduat_asl_parse_profile_id( + const char *text, + amduat_asl_encoding_profile_id_t *out_id); + +bool amduat_asl_parse_hash_id(const char *text, amduat_hash_id_t *out_id); + +bool amduat_asl_parse_type_tag(const char *text, amduat_type_tag_t *out_tag); + +#ifdef __cplusplus +} /* extern "C" */ +#endif + +#endif /* AMDUAT_ASL_PARSE_H */ diff --git a/src/near_core/asl/parse.c b/src/near_core/asl/parse.c new file mode 100644 index 0000000..4c1b9ef --- /dev/null +++ b/src/near_core/asl/parse.c @@ -0,0 +1,114 @@ +#include "amduat/asl/parse.h" + +#include "amduat/enc/asl1_core.h" +#include "amduat/hash/asl1.h" + +#include +#include +#include + +bool amduat_asl_parse_u16(const char *text, uint16_t *out) { + char *end; + unsigned long value; + + if (text == NULL || text[0] == '\0' || out == NULL) { + return false; + } + + errno = 0; + value = strtoul(text, &end, 0); + if (errno != 0 || end == text || *end != '\0' || value > UINT16_MAX) { + return false; + } + *out = (uint16_t)value; + return true; +} + +bool amduat_asl_parse_u32(const char *text, uint32_t *out) { + char *end; + unsigned long value; + + if (text == NULL || text[0] == '\0' || out == NULL) { + return false; + } + + errno = 0; + value = strtoul(text, &end, 0); + if (errno != 0 || end == text || *end != '\0' || value > UINT32_MAX) { + return false; + } + *out = (uint32_t)value; + return true; +} + +bool amduat_asl_parse_profile_id(const char *text, + amduat_asl_encoding_profile_id_t *out_id) { + uint16_t id; + size_t i; + size_t count; + const amduat_enc_asl1_core_profile_desc_t *descs; + + if (text == NULL || out_id == NULL) { + return false; + } + + if (amduat_asl_parse_u16(text, &id)) { + if (amduat_enc_asl1_core_desc_lookup(id) == NULL) { + return false; + } + *out_id = id; + return true; + } + + descs = amduat_enc_asl1_core_descs(&count); + for (i = 0; i < count; ++i) { + if (descs[i].name != NULL && strcmp(descs[i].name, text) == 0) { + *out_id = descs[i].profile_id; + return true; + } + } + + return false; +} + +bool amduat_asl_parse_hash_id(const char *text, amduat_hash_id_t *out_id) { + uint16_t id; + size_t i; + size_t count; + const amduat_hash_asl1_desc_t *descs; + + if (text == NULL || out_id == NULL) { + return false; + } + + if (amduat_asl_parse_u16(text, &id)) { + if (amduat_hash_asl1_desc_lookup(id) == NULL) { + return false; + } + *out_id = id; + return true; + } + + descs = amduat_hash_asl1_descs(&count); + for (i = 0; i < count; ++i) { + if (descs[i].name != NULL && strcmp(descs[i].name, text) == 0) { + *out_id = descs[i].hash_id; + return true; + } + } + + return false; +} + +bool amduat_asl_parse_type_tag(const char *text, amduat_type_tag_t *out_tag) { + uint32_t tag_id; + + if (text == NULL || out_tag == NULL) { + return false; + } + if (!amduat_asl_parse_u32(text, &tag_id)) { + return false; + } + *out_tag = amduat_type_tag(tag_id); + return true; +} diff --git a/src/tools/amduat_asl_cli.c b/src/tools/amduat_asl_cli.c index 6ef8f7f..64575f2 100644 --- a/src/tools/amduat_asl_cli.c +++ b/src/tools/amduat_asl_cli.c @@ -1,11 +1,9 @@ #include "amduat/asl/asl_store_fs.h" #include "amduat/asl/asl_store_fs_meta.h" +#include "amduat/asl/parse.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 #include #include #include @@ -151,115 +149,6 @@ static bool amduat_asl_cli_copy_store_id( return true; } -static bool amduat_asl_cli_parse_u16(const char *text, uint16_t *out) { - char *end; - unsigned long value; - - if (text == NULL || text[0] == '\0' || out == NULL) { - return false; - } - - errno = 0; - value = strtoul(text, &end, 0); - if (errno != 0 || end == text || *end != '\0' || value > UINT16_MAX) { - return false; - } - *out = (uint16_t)value; - return true; -} - -static bool amduat_asl_cli_parse_u32(const char *text, uint32_t *out) { - char *end; - unsigned long value; - - if (text == NULL || text[0] == '\0' || out == NULL) { - return false; - } - - errno = 0; - value = strtoul(text, &end, 0); - if (errno != 0 || end == text || *end != '\0' || value > UINT32_MAX) { - return false; - } - *out = (uint32_t)value; - return true; -} - -static bool amduat_asl_cli_parse_profile_id( - const char *text, - amduat_asl_encoding_profile_id_t *out_id) { - uint16_t id; - size_t i; - size_t count; - const amduat_enc_asl1_core_profile_desc_t *descs; - - if (text == NULL || out_id == NULL) { - return false; - } - - if (amduat_asl_cli_parse_u16(text, &id)) { - if (amduat_enc_asl1_core_desc_lookup(id) == NULL) { - return false; - } - *out_id = id; - return true; - } - - descs = amduat_enc_asl1_core_descs(&count); - for (i = 0; i < count; ++i) { - if (descs[i].name != NULL && strcmp(descs[i].name, text) == 0) { - *out_id = descs[i].profile_id; - return true; - } - } - - return false; -} - -static bool amduat_asl_cli_parse_hash_id(const char *text, - amduat_hash_id_t *out_id) { - uint16_t id; - size_t i; - size_t count; - const amduat_hash_asl1_desc_t *descs; - - if (text == NULL || out_id == NULL) { - return false; - } - - if (amduat_asl_cli_parse_u16(text, &id)) { - if (amduat_hash_asl1_desc_lookup(id) == NULL) { - return false; - } - *out_id = id; - return true; - } - - descs = amduat_hash_asl1_descs(&count); - for (i = 0; i < count; ++i) { - if (descs[i].name != NULL && strcmp(descs[i].name, text) == 0) { - *out_id = descs[i].hash_id; - return true; - } - } - - return false; -} - -static bool amduat_asl_cli_parse_type_tag(const char *text, - amduat_type_tag_t *out_tag) { - uint32_t tag_id; - - if (text == NULL || out_tag == NULL) { - return false; - } - if (!amduat_asl_cli_parse_u32(text, &tag_id)) { - return false; - } - *out_tag = amduat_type_tag(tag_id); - return true; -} - static bool amduat_asl_cli_parse_ref_format( const char *text, amduat_asl_cli_ref_format_t *out_fmt) { @@ -557,7 +446,7 @@ static int amduat_asl_cli_cmd_init(int argc, char **argv) { } } if (opts.profile != NULL) { - if (!amduat_asl_cli_parse_profile_id( + if (!amduat_asl_parse_profile_id( opts.profile, &cfg_in.config.encoding_profile_id)) { fprintf(stderr, "error: unknown profile: %s\n", opts.profile); @@ -565,7 +454,7 @@ static int amduat_asl_cli_cmd_init(int argc, char **argv) { } } if (opts.hash != NULL) { - if (!amduat_asl_cli_parse_hash_id(opts.hash, &cfg_in.config.hash_id)) { + if (!amduat_asl_parse_hash_id(opts.hash, &cfg_in.config.hash_id)) { fprintf(stderr, "error: unknown hash: %s\n", opts.hash); return AMDUAT_ASL_CLI_EXIT_USAGE; } @@ -625,7 +514,7 @@ static int amduat_asl_cli_cmd_put(int argc, char **argv) { fprintf(stderr, "error: --type-tag requires a value\n"); return AMDUAT_ASL_CLI_EXIT_USAGE; } - if (!amduat_asl_cli_parse_type_tag(argv[++i], &opts.type_tag)) { + if (!amduat_asl_parse_type_tag(argv[++i], &opts.type_tag)) { fprintf(stderr, "error: invalid type-tag\n"); return AMDUAT_ASL_CLI_EXIT_USAGE; } @@ -838,7 +727,7 @@ static int amduat_asl_cli_cmd_get(int argc, char **argv) { fprintf(stderr, "error: --expect-type-tag requires a value\n"); return AMDUAT_ASL_CLI_EXIT_USAGE; } - if (!amduat_asl_cli_parse_type_tag(argv[++i], + if (!amduat_asl_parse_type_tag(argv[++i], &opts.expect_type_tag)) { fprintf(stderr, "error: invalid expect-type-tag\n"); return AMDUAT_ASL_CLI_EXIT_USAGE;