Promote ASL ID parsing helpers

This commit is contained in:
Carl Niklas Rydberg 2025-12-20 11:16:16 +01:00
parent 4bc40995b4
commit f48b73b75f
4 changed files with 149 additions and 116 deletions

View file

@ -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

View file

@ -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 <stdbool.h>
#include <stdint.h>
#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 */

114
src/near_core/asl/parse.c Normal file
View file

@ -0,0 +1,114 @@
#include "amduat/asl/parse.h"
#include "amduat/enc/asl1_core.h"
#include "amduat/hash/asl1.h"
#include <errno.h>
#include <stdlib.h>
#include <string.h>
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;
}

View file

@ -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 <errno.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
@ -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;