Merge pull request #206 from EchterAgo/hex2bin_speedup
Speed up `hex2bin` function
This commit is contained in:
commit
b5071a8cc6
1 changed files with 33 additions and 20 deletions
43
util.c
43
util.c
|
@ -508,29 +508,42 @@ char *abin2hex(const unsigned char *p, size_t len)
|
||||||
|
|
||||||
bool hex2bin(unsigned char *p, const char *hexstr, size_t len)
|
bool hex2bin(unsigned char *p, const char *hexstr, size_t len)
|
||||||
{
|
{
|
||||||
char hex_byte[3];
|
if(hexstr == NULL)
|
||||||
char *ep;
|
return false;
|
||||||
|
|
||||||
hex_byte[2] = '\0';
|
size_t hexstr_len = strlen(hexstr);
|
||||||
|
if((hexstr_len % 2) != 0) {
|
||||||
while (*hexstr && len) {
|
|
||||||
if (!hexstr[1]) {
|
|
||||||
applog(LOG_ERR, "hex2bin str truncated");
|
applog(LOG_ERR, "hex2bin str truncated");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
hex_byte[0] = hexstr[0];
|
|
||||||
hex_byte[1] = hexstr[1];
|
size_t bin_len = hexstr_len / 2;
|
||||||
*p = (unsigned char) strtol(hex_byte, &ep, 16);
|
if (bin_len > len) {
|
||||||
if (*ep) {
|
applog(LOG_ERR, "hex2bin buffer too small");
|
||||||
applog(LOG_ERR, "hex2bin failed on '%s'", hex_byte);
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
p++;
|
|
||||||
hexstr += 2;
|
memset(p, 0, len);
|
||||||
len--;
|
|
||||||
|
size_t i = 0;
|
||||||
|
while (i < hexstr_len) {
|
||||||
|
char c = hexstr[i];
|
||||||
|
unsigned char nibble;
|
||||||
|
if(c >= '0' && c <= '9') {
|
||||||
|
nibble = (c - '0');
|
||||||
|
} else if (c >= 'A' && c <= 'F') {
|
||||||
|
nibble = (10 + (c - 'A'));
|
||||||
|
} else if (c >= 'a' && c <= 'f') {
|
||||||
|
nibble = (10 + (c - 'a'));
|
||||||
|
} else {
|
||||||
|
applog(LOG_ERR, "hex2bin invalid hex");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
p[(i / 2)] |= (nibble << ((1 - (i % 2)) * 4));
|
||||||
|
i++;
|
||||||
}
|
}
|
||||||
|
|
||||||
return (len == 0 && *hexstr == 0) ? true : false;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
int varint_encode(unsigned char *p, uint64_t n)
|
int varint_encode(unsigned char *p, uint64_t n)
|
||||||
|
|
Loading…
Reference in a new issue