21 const char cc = (char)std::tolower(c);
23 throw std::invalid_argument(
"invalid hex character");
29 throw std::invalid_argument(
"invalid hex character");
34 throw std::invalid_argument(
"invalid hex character");
37 static uint8_t chars_to_byte(
const char *cs)
39 const uint8_t *data = (
const uint8_t *)cs;
43 static char nibble_hex(
const uint8_t nibble)
45 assert((nibble & 0xf0) == 0);
47 return (
char)(
'a' + nibble - 10);
50 return (
char)(
'0' + nibble);
55 static const char *find_hex_string_of_length(
56 const std::string &hex,
const size_t bytes)
58 if (
'0' == hex[0] &&
'x' == hex[1]) {
59 if (hex.size() != 2 + bytes * 2) {
60 throw std::invalid_argument(
"invalid hex length");
62 return hex.c_str() + 2;
65 if (hex.size() != bytes * 2) {
66 throw std::invalid_argument(
"invalid hex length");
77 const char *cur = find_hex_string_of_length(hex, bytes);
78 uint8_t *
const dest_bytes_end = (uint8_t *)dest;
79 uint8_t *dest_bytes = dest_bytes_end + bytes;
82 *dest_bytes = chars_to_byte(cur);
84 }
while (dest_bytes > dest_bytes_end);
88 const void *bytes,
size_t num_bytes,
bool prefix)
96 out.reserve(num_bytes * 2 + 2);
100 out.reserve(num_bytes * 2);
103 const uint8_t *
const src_bytes_end = (
const uint8_t *)bytes;
104 const uint8_t *src_bytes = src_bytes_end + num_bytes;
107 const uint8_t
byte = *src_bytes;
108 out.push_back(nibble_hex(
byte >> 4));
109 out.push_back(nibble_hex(
byte & 0x0f));
110 }
while (src_bytes > src_bytes_end);