diff options
Diffstat (limited to 'ot_hex.c')
| -rw-r--r-- | ot_hex.c | 20 |
1 files changed, 11 insertions, 9 deletions
| @@ -10,16 +10,18 @@ | |||
| 10 | /* Opentracker */ | 10 | /* Opentracker */ |
| 11 | #include "ot_hex.h" | 11 | #include "ot_hex.h" |
| 12 | 12 | ||
| 13 | static int fromhex(unsigned char c) { | 13 | int hex_value(int c) { |
| 14 | if (c >= '0' && c <= '9') | 14 | unsigned char u = (unsigned char)c; |
| 15 | return c - '0'; | 15 | |
| 16 | c |= 0x20; /* fold upper case onto lower, digits are already handled */ | 16 | if (u >= '0' && u <= '9') |
| 17 | if (c >= 'a' && c <= 'f') | 17 | return u - '0'; |
| 18 | return c - 'a' + 10; | 18 | u |= 0x20; /* fold upper case onto lower, digits are already handled */ |
| 19 | if (u >= 'a' && u <= 'f') | ||
| 20 | return u - 'a' + 10; | ||
| 19 | return -1; | 21 | return -1; |
| 20 | } | 22 | } |
| 21 | 23 | ||
| 22 | int hex_isdigit(int c) { return fromhex((unsigned char)c) >= 0; } | 24 | int hex_isdigit(int c) { return hex_value(c) >= 0; } |
| 23 | 25 | ||
| 24 | char *hex_fromhash(char *dest, const uint8_t *hash) { | 26 | char *hex_fromhash(char *dest, const uint8_t *hash) { |
| 25 | static const char digits[] = "0123456789ABCDEF"; | 27 | static const char digits[] = "0123456789ABCDEF"; |
| @@ -43,9 +45,9 @@ int hex_tohash(ot_hash hash, const char *src) { | |||
| 43 | 45 | ||
| 44 | /* Tested one character at a time so that a string ending early stops us | 46 | /* Tested one character at a time so that a string ending early stops us |
| 45 | at its terminator rather than one byte past it */ | 47 | at its terminator rather than one byte past it */ |
| 46 | if ((high = fromhex((unsigned char)src[2 * i])) < 0) | 48 | if ((high = hex_value((unsigned char)src[2 * i])) < 0) |
| 47 | return 0; | 49 | return 0; |
| 48 | if ((low = fromhex((unsigned char)src[2 * i + 1])) < 0) | 50 | if ((low = hex_value((unsigned char)src[2 * i + 1])) < 0) |
| 49 | return 0; | 51 | return 0; |
| 50 | 52 | ||
| 51 | decoded[i] = (uint8_t)((high << 4) | low); | 53 | decoded[i] = (uint8_t)((high << 4) | low); |
