From d95163270e022fe87ece0c151c2987fd62274ba8 Mon Sep 17 00:00:00 2001 From: Dirk Engling Date: Sat, 24 Oct 2026 05:25:05 +0200 Subject: Add a dedicated ranged unsigned int scanner and a bencoded variant --- ot_scan.c | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 ot_scan.c (limited to 'ot_scan.c') diff --git a/ot_scan.c b/ot_scan.c new file mode 100644 index 0000000..a96f82b --- /dev/null +++ b/ot_scan.c @@ -0,0 +1,44 @@ +/* This software was written by Dirk Engling + It is considered beerware. Prost. Skol. Cheers or whatever. + + $id$ */ + +/* System */ +#include +#include +#include + +/* Opentracker */ +#include "ot_scan.h" + +size_t scan_udec(const char *src, uint64_t *dest, uint64_t max) { + unsigned long long value; + char *end; + + if (*src < '0' || *src > '9') + return 0; + + errno = 0; + value = strtoull(src, &end, 10); + + if (errno == ERANGE || value > (unsigned long long)max) + return 0; + + *dest = (uint64_t)value; + return (size_t)(end - src); +} + +size_t scan_udec_bencode(const char *src, uint64_t *dest, uint64_t max) { + uint64_t value; + size_t consumed = scan_udec(src, &value, max); + + /* bencode writes no leading zeros, so anything longer than a single digit + must not begin with one. */ + if (consumed > 1 && *src == '0') + return 0; + + if (consumed) + *dest = value; + + return consumed; +} -- cgit v1.3