From 60f55f34eeec70842b21accdd7fc0dc5d89493a2 Mon Sep 17 00:00:00 2001 From: Dirk Engling Date: Sat, 24 Oct 2026 17:21:47 +0200 Subject: Add an ip address parser and formatter for all formats opentracker needs --- ot_hex.c | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) (limited to 'ot_hex.c') diff --git a/ot_hex.c b/ot_hex.c index 17dd1ef..9581951 100644 --- a/ot_hex.c +++ b/ot_hex.c @@ -10,16 +10,18 @@ /* Opentracker */ #include "ot_hex.h" -static int fromhex(unsigned char c) { - if (c >= '0' && c <= '9') - return c - '0'; - c |= 0x20; /* fold upper case onto lower, digits are already handled */ - if (c >= 'a' && c <= 'f') - return c - 'a' + 10; +int hex_value(int c) { + unsigned char u = (unsigned char)c; + + if (u >= '0' && u <= '9') + return u - '0'; + u |= 0x20; /* fold upper case onto lower, digits are already handled */ + if (u >= 'a' && u <= 'f') + return u - 'a' + 10; return -1; } -int hex_isdigit(int c) { return fromhex((unsigned char)c) >= 0; } +int hex_isdigit(int c) { return hex_value(c) >= 0; } char *hex_fromhash(char *dest, const uint8_t *hash) { static const char digits[] = "0123456789ABCDEF"; @@ -43,9 +45,9 @@ int hex_tohash(ot_hash hash, const char *src) { /* Tested one character at a time so that a string ending early stops us at its terminator rather than one byte past it */ - if ((high = fromhex((unsigned char)src[2 * i])) < 0) + if ((high = hex_value((unsigned char)src[2 * i])) < 0) return 0; - if ((low = fromhex((unsigned char)src[2 * i + 1])) < 0) + if ((low = hex_value((unsigned char)src[2 * i + 1])) < 0) return 0; decoded[i] = (uint8_t)((high << 4) | low); -- cgit v1.3