Modified hex_string_to_bin

This commit is contained in:
Maxim Biro 2014-02-09 00:58:15 -05:00
parent 7f977f9ad2
commit 5fad72b910

View File

@ -33,17 +33,22 @@
#include <assert.h> #include <assert.h>
#endif // DEBUG #endif // DEBUG
/* TODO: rewrite */ // You are responsible for freeing the return value!
unsigned char *hex_string_to_bin(char hex_string[]) uint8_t *hex_string_to_bin(char *hex_string)
{ {
size_t i, len = strlen(hex_string); // byte is represented by exactly 2 hex digits, so lenth of binary string
unsigned char *val = malloc(len); // is half of that of the hex one. only hex string with even length
// valid. the more proper implementation would be to check if strlen(hex_string)
// is odd and return error code if it is. we assume strlen is even. if it's not
// then the last byte just won't be written in 'ret'.
size_t i, len = strlen(hex_string)/2;
uint8_t *ret = malloc(len);
char *pos = hex_string; char *pos = hex_string;
for (i = 0; i < len; ++i, pos += 2) for (i = 0; i < len; ++i, pos += 2)
sscanf(pos, "%2hhx", &val[i]); sscanf(pos, "%2hhx", &ret[i]);
return val; return ret;
} }
int cmdline_parsefor_ipv46(int argc, char **argv, uint8_t *ipv6enabled) int cmdline_parsefor_ipv46(int argc, char **argv, uint8_t *ipv6enabled)