diff options
author | Maxim Biro <nurupo.contributions@gmail.com> | 2014-02-09 00:58:15 -0500 |
---|---|---|
committer | Maxim Biro <nurupo.contributions@gmail.com> | 2014-02-09 00:58:15 -0500 |
commit | 5fad72b910fde1bf66fecf7728ac73354ab23a3b (patch) | |
tree | 2958a04d8d72aac9ba78407785badb86a05faadd /testing/misc_tools.c | |
parent | 7f977f9ad2fd4fdbfc1aec2081909da38cf601e9 (diff) |
Modified hex_string_to_bin
Diffstat (limited to 'testing/misc_tools.c')
-rw-r--r-- | testing/misc_tools.c | 17 |
1 files changed, 11 insertions, 6 deletions
diff --git a/testing/misc_tools.c b/testing/misc_tools.c index efe1a555..d0d80038 100644 --- a/testing/misc_tools.c +++ b/testing/misc_tools.c | |||
@@ -33,17 +33,22 @@ | |||
33 | #include <assert.h> | 33 | #include <assert.h> |
34 | #endif // DEBUG | 34 | #endif // DEBUG |
35 | 35 | ||
36 | /* TODO: rewrite */ | 36 | // You are responsible for freeing the return value! |
37 | unsigned char *hex_string_to_bin(char hex_string[]) | 37 | uint8_t *hex_string_to_bin(char *hex_string) |
38 | { | 38 | { |
39 | size_t i, len = strlen(hex_string); | 39 | // byte is represented by exactly 2 hex digits, so lenth of binary string |
40 | unsigned char *val = malloc(len); | 40 | // is half of that of the hex one. only hex string with even length |
41 | // valid. the more proper implementation would be to check if strlen(hex_string) | ||
42 | // is odd and return error code if it is. we assume strlen is even. if it's not | ||
43 | // then the last byte just won't be written in 'ret'. | ||
44 | size_t i, len = strlen(hex_string)/2; | ||
45 | uint8_t *ret = malloc(len); | ||
41 | char *pos = hex_string; | 46 | char *pos = hex_string; |
42 | 47 | ||
43 | for (i = 0; i < len; ++i, pos += 2) | 48 | for (i = 0; i < len; ++i, pos += 2) |
44 | sscanf(pos, "%2hhx", &val[i]); | 49 | sscanf(pos, "%2hhx", &ret[i]); |
45 | 50 | ||
46 | return val; | 51 | return ret; |
47 | } | 52 | } |
48 | 53 | ||
49 | int cmdline_parsefor_ipv46(int argc, char **argv, uint8_t *ipv6enabled) | 54 | int cmdline_parsefor_ipv46(int argc, char **argv, uint8_t *ipv6enabled) |