From 168285b4569f862443419a2bffe221e1a8ab8150 Mon Sep 17 00:00:00 2001 From: Dirk Engling Date: Sat, 24 Oct 2026 02:04:33 +0200 Subject: Replace all manual hex conversion with the new helper --- opentracker.c | 11 +++-------- ot_accesslist.c | 26 ++++---------------------- ot_fullscrape.c | 19 ++++--------------- ot_stats.c | 23 ++++++----------------- 4 files changed, 17 insertions(+), 62 deletions(-) diff --git a/opentracker.c b/opentracker.c index 4d44950..f218589 100644 --- a/opentracker.c +++ b/opentracker.c @@ -28,6 +28,7 @@ /* Opentracker */ #include "ot_accesslist.h" +#include "ot_hex.h" #include "ot_http.h" #include "ot_livesync.h" #include "ot_mutex.h" @@ -646,16 +647,10 @@ void load_state(const char *const state_filename) { /* We do ignore anything that is not of the form "^[:xdigit:]:\d+:\d+" */ while (fgets(inbuf, sizeof(inbuf), state_filehandle)) { size_t i; - for (i = 0; i < sizeof(ot_hash); ++i) { - int eger = 16 * scan_fromhex(inbuf[2 * i]) + scan_fromhex(inbuf[1 + 2 * i]); - if (eger < 0) - continue; - infohash[i] = eger; - } - if (i != sizeof(ot_hash)) + if (!hex_tohash(infohash, inbuf)) continue; - i *= 2; + i = 2 * sizeof(ot_hash); if (inbuf[i++] != ':' || !(consumed = scan_ulonglong(inbuf + i, &base))) continue; diff --git a/ot_accesslist.c b/ot_accesslist.c index 2827494..ee6adb2 100644 --- a/ot_accesslist.c +++ b/ot_accesslist.c @@ -20,10 +20,10 @@ #include "fmt.h" #include "ip6.h" #include "mmap.h" -#include "scan.h" /* Opentracker */ #include "ot_accesslist.h" +#include "ot_hex.h" #include "ot_vector.h" #include "trackerlogic.h" @@ -128,20 +128,11 @@ static void accesslist_readfile(void) { /* We do ignore anything that is not of the form "^[:xdigit:]{40}[^:xdigit:].*" */ while (read_offs <= map_end) { - size_t i; - for (i = 0; i < sizeof(ot_hash); ++i) { - int eger1 = scan_fromhex((unsigned char)read_offs[2 * i]); - int eger2 = scan_fromhex((unsigned char)read_offs[1 + 2 * i]); - if (eger1 < 0 || eger2 < 0) - break; - (*info_hash)[i] = (uint8_t)(eger1 * 16 + eger2); - } - - if (i == sizeof(ot_hash)) { + if (hex_tohash(*info_hash, read_offs)) { read_offs += 40; /* Append accesslist to accesslist vector */ - if (read_offs == map_end || scan_fromhex((unsigned char)*read_offs) < 0) + if (read_offs == map_end || !hex_isdigit((unsigned char)*read_offs)) ++info_hash; } @@ -261,7 +252,6 @@ static void *accesslist_adddel_worker(char *fifoname, ot_accesslist *_Atomic while ((linelen = getline(&line, &linecap, fifo)) > 0) { ot_hash info_hash; - size_t i; printf("Got line %*s", (int)linelen, line); /* We do ignore anything that is not of the form "^[:xdigit:]{40}[^:xdigit:].*" @@ -269,15 +259,7 @@ static void *accesslist_adddel_worker(char *fifoname, ot_accesslist *_Atomic if (linelen < 41) continue; - for (i = 0; i < sizeof(ot_hash); ++i) { - int eger1 = scan_fromhex((unsigned char)line[2 * i]); - int eger2 = scan_fromhex((unsigned char)line[1 + 2 * i]); - if (eger1 < 0 || eger2 < 0) - break; - ((uint8_t *)info_hash)[i] = (uint8_t)(eger1 * 16 + eger2); - } - printf("parsed info_hash %20s\n", info_hash); - if (i != sizeof(ot_hash)) + if (!hex_tohash(info_hash, line)) continue; /* From now on we modify g_accesslist_add and g_accesslist_delete, so prevent the diff --git a/ot_fullscrape.c b/ot_fullscrape.c index 3e36e8e..08970a6 100644 --- a/ot_fullscrape.c +++ b/ot_fullscrape.c @@ -23,6 +23,7 @@ #include "textcode.h" /* Opentracker */ +#include "ot_hex.h" #include "ot_fullscrape.h" #include "ot_iovec.h" #include "ot_mutex.h" @@ -48,18 +49,6 @@ static void fullscrape_make_zstd(int taskid, ot_tasktype mode); /* Converter function from memory to human readable hex strings XXX - Duplicated from ot_stats. Needs fix. */ -static char *to_hex(char *d, uint8_t *s) { - char *m = "0123456789ABCDEF"; - char *t = d; - char *e = d + 40; - while (d < e) { - *d++ = m[*s >> 4]; - *d++ = m[*s++ & 15]; - } - *d = 0; - return t; -} - /* This is the entry point into this worker thread It grabs tasks from mutex_tasklist and delivers results back */ @@ -117,12 +106,12 @@ static char * fullscrape_write_one( ot_tasktype mode, char *r, ot_torrent *torre break; case TASK_FULLSCRAPE_TPB_ASCII: - to_hex(r, *hash); + hex_fromhash(r, *hash); r += 2 * sizeof(ot_hash); r += sprintf(r, ":%zd:%zd\n", seed_count, peer_count - seed_count); break; case TASK_FULLSCRAPE_TPB_ASCII_PLUS: - to_hex(r, *hash); + hex_fromhash(r, *hash); r += 2 * sizeof(ot_hash); r += sprintf(r, ":%zd:%zd:%zd\n", seed_count, peer_count - seed_count, down_count); break; @@ -138,7 +127,7 @@ static char * fullscrape_write_one( ot_tasktype mode, char *r, ot_torrent *torre r += sprintf(r, ":%zd:%zd\n", seed_count, peer_count - seed_count); break; case TASK_FULLSCRAPE_TRACKERSTATE: - to_hex(r, *hash); + hex_fromhash(r, *hash); r += 2 * sizeof(ot_hash); r += sprintf(r, ":%zd:%zd\n", torrent->peer_list6->base, down_count); break; diff --git a/ot_stats.c b/ot_stats.c index afc972a..d43f29c 100644 --- a/ot_stats.c +++ b/ot_stats.c @@ -23,6 +23,7 @@ /* Opentracker */ #include "ot_accesslist.h" +#include "ot_hex.h" #include "ot_iovec.h" #include "ot_mutex.h" #include "ot_stats.h" @@ -295,18 +296,6 @@ static int torrent_statter(ot_torrent *torrent, uintptr_t data) { } /* Converter function from memory to human readable hex strings */ -static char *to_hex(char *d, uint8_t *s) { - char *m = "0123456789ABCDEF"; - char *t = d; - char *e = d + 40; - while (d < e) { - *d++ = m[*s >> 4]; - *d++ = m[*s++ & 15]; - } - *d = 0; - return t; -} - typedef struct { size_t val; ot_hash hash; @@ -361,15 +350,15 @@ size_t stats_top_txt(char *reply, size_t amount) { r += sprintf(r, "Top %zd torrents by peers:\n", amount); for (idx = 0; idx < amount; ++idx) if (top100c[idx].val) - r += sprintf(r, "\t%zd\t%s\n", top100c[idx].val, to_hex(hex_out, top100c[idx].hash)); + r += sprintf(r, "\t%zd\t%s\n", top100c[idx].val, hex_fromhash(hex_out, top100c[idx].hash)); r += sprintf(r, "Top %zd torrents by seeds:\n", amount); for (idx = 0; idx < amount; ++idx) if (top100s[idx].val) - r += sprintf(r, "\t%zd\t%s\n", top100s[idx].val, to_hex(hex_out, top100s[idx].hash)); + r += sprintf(r, "\t%zd\t%s\n", top100s[idx].val, hex_fromhash(hex_out, top100s[idx].hash)); r += sprintf(r, "Top %zd torrents by leechers:\n", amount); for (idx = 0; idx < amount; ++idx) if (top100l[idx].val) - r += sprintf(r, "\t%zd\t%s\n", top100l[idx].val, to_hex(hex_out, top100l[idx].hash)); + r += sprintf(r, "\t%zd\t%s\n", top100l[idx].val, hex_fromhash(hex_out, top100l[idx].hash)); return r - reply; } @@ -667,9 +656,9 @@ void stats_issue_event(ot_status_event event, PROTO_FLAG proto, uintptr_t event_ localtime_r(&ttt, &time_now); strftime(timestring, sizeof(timestring), "%FT%T%z", &time_now); - to_hex(hash_hex, *ws->hash); + hex_fromhash(hash_hex, *ws->hash); if (ws->peer_id) - to_hex(peerid_hex, (uint8_t *)ws->peer_id); + hex_fromhash(peerid_hex, (uint8_t *)ws->peer_id); else { *peerid_hex = 0; } -- cgit v1.3