Skip to content

Commit 1f147f2

Browse files
committed
Add util_dbg_hex
1 parent 5bc5f08 commit 1f147f2

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

src/util.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,16 @@ void util_uint8_to_hex(const uint8_t* in_bin, const size_t in_len, char* out)
4747
rust_util_bytes(in_bin, in_len), rust_util_bytes_mut((uint8_t*)out, in_len * 2 + 1));
4848
}
4949

50+
const char* util_dbg_hex(const uint8_t* bin, const size_t len)
51+
{
52+
if (len > UTIL_DBG_HEX_MAX_LEN) {
53+
util_log("len too large, %zu > %d", len, UTIL_DBG_HEX_MAX_LEN);
54+
}
55+
static char buf[UTIL_DBG_HEX_MAX_LEN * 2 + 1] = {0};
56+
util_uint8_to_hex(bin, len, buf);
57+
return buf;
58+
}
59+
5060
void util_cleanup_str(char** str)
5161
{
5262
util_zero(*str, strlens(*str));

src/util.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,22 @@ void util_zero(volatile void* dst, size_t len);
6161
// `out` must be of size in_len*2+1. Use BB_HEX_SIZE() to compute the size.
6262
void util_uint8_to_hex(const uint8_t* in_bin, size_t in_len, char* out);
6363

64+
#define UTIL_DBG_HEX_MAX_LEN 64
65+
/// This function is for debug purposes only!
66+
///
67+
/// Don't use this in production, the returned pointer is only valid until this function is called
68+
/// again. This function is not thread safe.
69+
///
70+
/// Returns a null terminated string, suitable for printing with printf in C.
71+
///
72+
/// Max `len` is UTIL_DBG_HEX_MAX_LEN. This function panics if `len` is to large.
73+
///
74+
/// Usage:
75+
/// uint8_t arr[2] = {1,2};
76+
/// util_log("%s", util_dbg_hex(arr, sizeof(arr)));
77+
///
78+
const char* util_dbg_hex(const uint8_t* bin, size_t len);
79+
6480
#define BB_HEX_SIZE(in_bin) (sizeof((in_bin)) * 2 + 1)
6581

6682
void util_cleanup_str(char** str);

0 commit comments

Comments
 (0)