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_scan.c | 65 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 src/ot_scan.c (limited to 'src/ot_scan.c') diff --git a/src/ot_scan.c b/src/ot_scan.c new file mode 100644 index 0000000..3b8e87c --- /dev/null +++ b/src/ot_scan.c @@ -0,0 +1,65 @@ +/* This software was written by Dirk Engling + It is considered beerware. Prost. Skol. Cheers or whatever. */ + +/* System */ +#include +#include +#include + +/* Opentracker */ +#include "ot_scan.h" + +size_t scan_uint(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_uint_bencode(const char *src, uint64_t *dest, uint64_t max) { + uint64_t value; + size_t consumed = scan_uint(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; +} + +int scan_int(const char *src, size_t len, int64_t *dest) { + int64_t value = 0; + int negate = 0; + + if (len && *src == '-') + --len, ++src, negate = 1; + if (!len) + return -1; + + while (len--) { + if (*src < '0' || *src > '9') + return -1; + /* A twenty character field of nines does not fit, and silently wrapping + would magically heal a nonsense announce */ + if (value > (INT64_MAX - (*src - '0')) / 10) + return -1; + value = 10 * value + (*src++ - '0'); + } + + *dest = negate ? -value : value; + return 0; +} -- cgit v1.3