hex_utils: Cleaned up and fixed the type confusion that was going on

This commit is contained in:
dragonmux 2022-07-20 02:12:07 +01:00
parent 8db1d30852
commit eb9d9893f8
No known key found for this signature in database
GPG Key ID: 64861EA89B35507A

View File

@ -26,16 +26,16 @@
static const char hexdigits[] = "0123456789abcdef"; static const char hexdigits[] = "0123456789abcdef";
char * hexify(char *hex, const void *buf, size_t size) char *hexify(char *hex, const void *buf, const size_t size)
{ {
char *tmp = hex; char *dst = hex;
const uint8_t *b = buf; const uint8_t *const src = buf;
while (size--) { for (size_t idx = 0; idx < size; ++idx) {
*tmp++ = hexdigits[*b >> 4]; *dst++ = hexdigits[src[idx] >> 4];
*tmp++ = hexdigits[*b++ & 0xF]; *dst++ = hexdigits[src[idx] & 0xF];
} }
*tmp++ = 0; *dst++ = 0;
return hex; return hex;
} }
@ -50,13 +50,11 @@ static uint8_t unhex_digit(char hex)
return tmp; return tmp;
} }
char * unhexify(void *buf, const char *hex, size_t size) char *unhexify(void *buf, const char *hex, const size_t size)
{ {
uint8_t *b = buf; uint8_t *const dst = buf;
while (size--) { for (size_t idx = 0; idx < size; ++idx, hex += 2) {
*b = unhex_digit(*hex++) << 4; dst[idx] = (unhex_digit(hex[0]) << 4) | unhex_digit(hex[1]);
*b++ |= unhex_digit(*hex++);
} }
return buf; return buf;
} }