From ad541c67761b8aa6da1566802c347cac4018d0c2 Mon Sep 17 00:00:00 2001 From: Dirk Engling Date: Mon, 2 Nov 2026 20:50:24 +0100 Subject: Move the build sources into src/ --- src/ot_hex.c | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 src/ot_hex.c (limited to 'src/ot_hex.c') diff --git a/src/ot_hex.c b/src/ot_hex.c new file mode 100644 index 0000000..b537094 --- /dev/null +++ b/src/ot_hex.c @@ -0,0 +1,56 @@ +/* This software was written by Dirk Engling + It is considered beerware. Prost. Skol. Cheers or whatever. */ + +/* System */ +#include +#include + +/* Opentracker */ +#include "ot_hex.h" + +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 hex_value(c) >= 0; } + +char *hex_from_hash(char *dest, const uint8_t *hash) { + static const char digits[] = "0123456789ABCDEF"; + char *d = dest; + size_t i; + + for (i = 0; i < sizeof(ot_hash); ++i) { + *d++ = digits[hash[i] >> 4]; + *d++ = digits[hash[i] & 15]; + } + *d = 0; + return dest; +} + +int hex_to_hash(ot_hash hash, const char *src) { + uint8_t decoded[sizeof(ot_hash)]; + size_t i; + + for (i = 0; i < sizeof(ot_hash); ++i) { + int high, low; + + /* 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 = hex_value((unsigned char)src[2 * i])) < 0) + return 0; + if ((low = hex_value((unsigned char)src[2 * i + 1])) < 0) + return 0; + + decoded[i] = (uint8_t)((high << 4) | low); + } + + memcpy(hash, decoded, sizeof(decoded)); + return 1; +} -- cgit v1.3