ignore : separator characters in chunk_from_hex()
This commit is contained in:
@@ -307,24 +307,46 @@ static char hex2bin(char hex)
|
|||||||
chunk_t chunk_from_hex(chunk_t hex, char *buf)
|
chunk_t chunk_from_hex(chunk_t hex, char *buf)
|
||||||
{
|
{
|
||||||
int i, len;
|
int i, len;
|
||||||
|
u_char *ptr;
|
||||||
bool odd = FALSE;
|
bool odd = FALSE;
|
||||||
|
|
||||||
len = (hex.len / 2);
|
/* subtract the number of optional ':' separation characters */
|
||||||
if (hex.len % 2)
|
len = hex.len;
|
||||||
|
ptr = hex.ptr;
|
||||||
|
for (i = 0; i < hex.len; i++)
|
||||||
|
{
|
||||||
|
if (*ptr++ == ':')
|
||||||
|
{
|
||||||
|
len--;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* compute the number of binary bytes */
|
||||||
|
if (len % 2)
|
||||||
{
|
{
|
||||||
odd = TRUE;
|
odd = TRUE;
|
||||||
len++;
|
len++;
|
||||||
}
|
}
|
||||||
|
len /= 2;
|
||||||
|
|
||||||
|
/* allocate buffer memory unless provided by caller */
|
||||||
if (!buf)
|
if (!buf)
|
||||||
{
|
{
|
||||||
buf = malloc(len);
|
buf = malloc(len);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* buffer is filled from the right */
|
/* buffer is filled from the right */
|
||||||
memset(buf, 0, len);
|
memset(buf, 0, len);
|
||||||
hex.ptr += hex.len;
|
hex.ptr += hex.len;
|
||||||
|
|
||||||
for (i = len - 1; i >= 0; i--)
|
for (i = len - 1; i >= 0; i--)
|
||||||
{
|
{
|
||||||
buf[i] = hex2bin(*(--hex.ptr));
|
/* skip separation characters */
|
||||||
|
if (*(--hex.ptr) == ':')
|
||||||
|
{
|
||||||
|
--hex.ptr;
|
||||||
|
}
|
||||||
|
buf[i] = hex2bin(*hex.ptr);
|
||||||
if (i > 0 || !odd)
|
if (i > 0 || !odd)
|
||||||
{
|
{
|
||||||
buf[i] |= hex2bin(*(--hex.ptr)) << 4;
|
buf[i] |= hex2bin(*(--hex.ptr)) << 4;
|
||||||
|
|||||||
Reference in New Issue
Block a user