2019-05-28 19:20:35 +08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2019 Nordic Semiconductor ASA
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <zephyr/types.h>
|
|
|
|
#include <errno.h>
|
2022-05-06 17:23:05 +08:00
|
|
|
#include <zephyr/sys/util.h>
|
2019-05-28 19:20:35 +08:00
|
|
|
|
2020-05-28 00:26:57 +08:00
|
|
|
int char2hex(char c, uint8_t *x)
|
2019-05-28 19:20:35 +08:00
|
|
|
{
|
|
|
|
if (c >= '0' && c <= '9') {
|
|
|
|
*x = c - '0';
|
|
|
|
} else if (c >= 'a' && c <= 'f') {
|
|
|
|
*x = c - 'a' + 10;
|
|
|
|
} else if (c >= 'A' && c <= 'F') {
|
|
|
|
*x = c - 'A' + 10;
|
|
|
|
} else {
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-05-28 00:26:57 +08:00
|
|
|
int hex2char(uint8_t x, char *c)
|
2019-05-28 19:20:35 +08:00
|
|
|
{
|
|
|
|
if (x <= 9) {
|
|
|
|
*c = x + '0';
|
2019-08-10 18:33:00 +08:00
|
|
|
} else if (x <= 15) {
|
2019-05-28 19:20:35 +08:00
|
|
|
*c = x - 10 + 'a';
|
|
|
|
} else {
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-05-28 00:26:57 +08:00
|
|
|
size_t bin2hex(const uint8_t *buf, size_t buflen, char *hex, size_t hexlen)
|
2019-05-28 19:20:35 +08:00
|
|
|
{
|
2022-03-28 19:07:48 +08:00
|
|
|
if (hexlen < (buflen * 2 + 1)) {
|
2019-05-28 19:20:35 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (size_t i = 0; i < buflen; i++) {
|
|
|
|
if (hex2char(buf[i] >> 4, &hex[2 * i]) < 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (hex2char(buf[i] & 0xf, &hex[2 * i + 1]) < 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
hex[2 * buflen] = '\0';
|
|
|
|
return 2 * buflen;
|
|
|
|
}
|
|
|
|
|
2020-05-28 00:26:57 +08:00
|
|
|
size_t hex2bin(const char *hex, size_t hexlen, uint8_t *buf, size_t buflen)
|
2019-05-28 19:20:35 +08:00
|
|
|
{
|
2020-05-28 00:26:57 +08:00
|
|
|
uint8_t dec;
|
2019-05-28 19:20:35 +08:00
|
|
|
|
|
|
|
if (buflen < hexlen / 2 + hexlen % 2) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* if hexlen is uneven, insert leading zero nibble */
|
|
|
|
if (hexlen % 2) {
|
|
|
|
if (char2hex(hex[0], &dec) < 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
buf[0] = dec;
|
|
|
|
hex++;
|
|
|
|
buf++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* regular hex conversion */
|
|
|
|
for (size_t i = 0; i < hexlen / 2; i++) {
|
|
|
|
if (char2hex(hex[2 * i], &dec) < 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
buf[i] = dec << 4;
|
|
|
|
|
|
|
|
if (char2hex(hex[2 * i + 1], &dec) < 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
buf[i] += dec;
|
|
|
|
}
|
|
|
|
|
|
|
|
return hexlen / 2 + hexlen % 2;
|
|
|
|
}
|