Promote ASL reference hex helpers

This commit is contained in:
Carl Niklas Rydberg 2025-12-20 11:14:06 +01:00
parent 93d9f87783
commit 4bc40995b4
4 changed files with 112 additions and 80 deletions

View file

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

View file

@ -0,0 +1,27 @@
#ifndef AMDUAT_ASL_REF_TEXT_H
#define AMDUAT_ASL_REF_TEXT_H
#include "amduat/asl/core.h"
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#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 */

View file

@ -0,0 +1,79 @@
#include "amduat/asl/ref_text.h"
#include "amduat/enc/asl1_core_codec.h"
#include "amduat/util/hex.h"
#include <stdlib.h>
#include <string.h>
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);
}

View file

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