Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions deps/nbytes/src/nbytes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,17 +157,20 @@ const int8_t unhex_table[256] = {
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1};

inline char nibble(uint8_t x) {
return x + '0' + ((x > 9) * 39);
}

size_t HexEncode(const char *src, size_t slen, char *dst, size_t dlen) {
// We know how much we'll write, just make sure that there's space.
NBYTES_ASSERT_TRUE(dlen >= MultiplyWithOverflowCheck<size_t>(slen, 2u) &&
"not enough space provided for hex encode");

dlen = slen * 2;
for (size_t i = 0, k = 0; k < dlen; i += 1, k += 2) {
static const char hex[] = "0123456789abcdef";
uint8_t val = static_cast<uint8_t>(src[i]);
dst[k + 0] = hex[val >> 4];
dst[k + 1] = hex[val & 15];
dst[k + 0] = nibble(val >> 4);
dst[k + 1] = nibble(val & 15);
}

return dlen;
Expand Down