From 6a7ba4d620a1915b74a9a7ac26b07f7f621a5db6 Mon Sep 17 00:00:00 2001 From: Niklas Dusenlund Date: Fri, 9 Feb 2024 12:58:08 +0100 Subject: [PATCH] Add a function to help debugging --- src/util.c | 10 ++++++++++ src/util.h | 4 ++++ 2 files changed, 14 insertions(+) diff --git a/src/util.c b/src/util.c index 4318214f0..8df53f74f 100644 --- a/src/util.c +++ b/src/util.c @@ -39,6 +39,16 @@ void util_uint8_to_hex(const uint8_t* in_bin, const size_t in_len, char* out) rust_util_bytes(in_bin, in_len), rust_util_bytes_mut((uint8_t*)out, in_len * 2 + 1)); } +#if defined(SEMIHOSTING) +char* util_uint8_to_hex_alloc(const uint8_t* in_bin, const size_t in_len) +{ + void* out = malloc(in_len * 2 + 1); + rust_util_uint8_to_hex( + rust_util_bytes(in_bin, in_len), rust_util_bytes_mut((uint8_t*)out, in_len * 2 + 1)); + return (char*)out; +} +#endif + void util_cleanup_str(char** str) { util_zero(*str, strlens(*str)); diff --git a/src/util.h b/src/util.h index e84ec1c2d..fa5cf3890 100644 --- a/src/util.h +++ b/src/util.h @@ -61,6 +61,10 @@ void util_zero(volatile void* dst, size_t len); // `out` must be of size in_len*2+1. Use BB_HEX_SIZE() to compute the size. void util_uint8_to_hex(const uint8_t* in_bin, size_t in_len, char* out); +// This function is only compiled when we compile with SEMIHOSTING, it is convenient when debugging +// to print byte arrays as hex. +char* util_uint8_to_hex_alloc(const uint8_t* in_bin, size_t in_len); + #define BB_HEX_SIZE(in_bin) (sizeof((in_bin)) * 2 + 1) void util_cleanup_str(char** str);