Skip to content

Commit fd42f24

Browse files
bytes2hexstring hook (#1213)
This PR adds a new hook implementation for converting byte arrays into hex strings. This is needed by the kontrol-node to encode memory as JSON strings efficiently. --------- Co-authored-by: Tamás Tóth <[email protected]>
1 parent bb5318b commit fd42f24

File tree

3 files changed

+34
-0
lines changed

3 files changed

+34
-0
lines changed

bin/llvm-krun

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ expanded_input_file="$(mktemp tmp.in.XXXXXXXXXX)"
99
parser_file="$(mktemp tmp.parse.XXXXXXXXXX)"
1010
temp_inputs=()
1111

12+
# shellcheck disable=SC2329
1213
cleanup () {
1314
# shellcheck disable=SC2317
1415
rm -rf "$input_file" "$expanded_input_file" "$output_file" "$parser_file" "${temp_inputs[@]}"

runtime/strings/bytes.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include <algorithm>
2+
#include <array>
23
#include <cstdint>
34
#include <cstdlib>
45
#include <cstring>
@@ -162,6 +163,24 @@ SortBytes hook_BYTES_string2bytes(SortString s) {
162163
return hook_BYTES_bytes2string(s);
163164
}
164165

166+
// Convert a bytes array to its hexadecimal string representation
167+
// For example, the bytes array b'\x01\xef' becomes the string "01ef"
168+
// syntax String ::= Bytes2Hex( Bytes )
169+
SortString hook_BYTES_bytes2hex(SortBytes b) {
170+
static const std::array<char, 16> hexchars
171+
= {'0', '1', '2', '3', '4', '5', '6', '7',
172+
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
173+
auto len_b = len(b);
174+
auto *result
175+
= static_cast<string *>(kore_alloc_token(sizeof(string) + len_b * 2));
176+
for (size_t i = 0; i < len_b; i++) {
177+
result->data[i * 2] = hexchars.at((b->data[i] >> 4) & 0xf);
178+
result->data[i * 2 + 1] = hexchars.at(b->data[i] & 0xf);
179+
}
180+
init_with_len(result, len_b * 2);
181+
return result;
182+
}
183+
165184
SortBytes hook_BYTES_substr(SortBytes input, SortInt start, SortInt end) {
166185
uint64_t ustart = GET_UI(start);
167186
uint64_t uend = GET_UI(end);

unittests/runtime-strings/bytestest.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ mpz_ptr
2020
hook_BYTES_bytes2int(string *b, uint64_t endianness, uint64_t signedness);
2121
string *hook_BYTES_int2bytes(mpz_t len, mpz_t i, uint64_t endianness);
2222
string *hook_BYTES_bytes2string(string *b);
23+
string *hook_BYTES_bytes2hex(string *b);
2324
string *hook_BYTES_string2bytes(string *s);
2425
string *hook_BYTES_substr(string *b, mpz_t start, mpz_t end);
2526
string *hook_BYTES_replaceAt(string *b, mpz_t start, string *b2);
@@ -171,6 +172,19 @@ BOOST_AUTO_TEST_CASE(bytes2string) {
171172
BOOST_CHECK_EQUAL(0, memcmp(_1234->data, "1234", 4));
172173
}
173174

175+
BOOST_AUTO_TEST_CASE(bytes2hex) {
176+
auto empty = make_string("");
177+
auto res = hook_BYTES_bytes2hex(empty);
178+
BOOST_CHECK(res != empty);
179+
BOOST_CHECK_EQUAL(len(res), 0);
180+
181+
auto _01ef = make_string("\x01\xef", 2);
182+
res = hook_BYTES_bytes2hex(_01ef);
183+
BOOST_CHECK(res != _01ef);
184+
BOOST_CHECK_EQUAL(len(res), 4);
185+
BOOST_CHECK_EQUAL(0, memcmp(res->data, "01ef", 4));
186+
}
187+
174188
BOOST_AUTO_TEST_CASE(string2bytes) {
175189
auto empty = make_string("");
176190
auto res = hook_BYTES_string2bytes(empty);

0 commit comments

Comments
 (0)