diff options
| author | Dirk Engling <erdgeist@erdgeist.org> | 2026-10-24 05:25:05 +0200 |
|---|---|---|
| committer | Dirk Engling <erdgeist@erdgeist.org> | 2026-10-24 05:25:05 +0200 |
| commit | d95163270e022fe87ece0c151c2987fd62274ba8 (patch) | |
| tree | 3cd0a9803462829dd84430bbf8d56f4ef81723ed | |
| parent | a494c5a170bae9838df2f4ff511bc5f203221fb2 (diff) | |
Add a dedicated ranged unsigned int scanner and a bencoded variant
| -rw-r--r-- | ot_scan.c | 44 | ||||
| -rw-r--r-- | ot_scan.h | 21 |
2 files changed, 65 insertions, 0 deletions
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 @@ | |||
| 1 | /* This software was written by Dirk Engling <erdgeist@erdgeist.org> | ||
| 2 | It is considered beerware. Prost. Skol. Cheers or whatever. | ||
| 3 | |||
| 4 | $id$ */ | ||
| 5 | |||
| 6 | /* System */ | ||
| 7 | #include <errno.h> | ||
| 8 | #include <stdint.h> | ||
| 9 | #include <stdlib.h> | ||
| 10 | |||
| 11 | /* Opentracker */ | ||
| 12 | #include "ot_scan.h" | ||
| 13 | |||
| 14 | size_t scan_udec(const char *src, uint64_t *dest, uint64_t max) { | ||
| 15 | unsigned long long value; | ||
| 16 | char *end; | ||
| 17 | |||
| 18 | if (*src < '0' || *src > '9') | ||
| 19 | return 0; | ||
| 20 | |||
| 21 | errno = 0; | ||
| 22 | value = strtoull(src, &end, 10); | ||
| 23 | |||
| 24 | if (errno == ERANGE || value > (unsigned long long)max) | ||
| 25 | return 0; | ||
| 26 | |||
| 27 | *dest = (uint64_t)value; | ||
| 28 | return (size_t)(end - src); | ||
| 29 | } | ||
| 30 | |||
| 31 | size_t scan_udec_bencode(const char *src, uint64_t *dest, uint64_t max) { | ||
| 32 | uint64_t value; | ||
| 33 | size_t consumed = scan_udec(src, &value, max); | ||
| 34 | |||
| 35 | /* bencode writes no leading zeros, so anything longer than a single digit | ||
| 36 | must not begin with one. */ | ||
| 37 | if (consumed > 1 && *src == '0') | ||
| 38 | return 0; | ||
| 39 | |||
| 40 | if (consumed) | ||
| 41 | *dest = value; | ||
| 42 | |||
| 43 | return consumed; | ||
| 44 | } | ||
diff --git a/ot_scan.h b/ot_scan.h new file mode 100644 index 0000000..860a088 --- /dev/null +++ b/ot_scan.h | |||
| @@ -0,0 +1,21 @@ | |||
| 1 | /* This software was written by Dirk Engling <erdgeist@erdgeist.org> | ||
| 2 | It is considered beerware. Prost. Skol. Cheers or whatever. | ||
| 3 | |||
| 4 | $id$ */ | ||
| 5 | |||
| 6 | #ifndef OT_SCAN_H__ | ||
| 7 | #define OT_SCAN_H__ | ||
| 8 | |||
| 9 | #include <stddef.h> | ||
| 10 | #include <stdint.h> | ||
| 11 | |||
| 12 | /* Read an unsigned decimal number from src into dest and return how many | ||
| 13 | characters were consumed. Returns zero and leaves dest untouched when src | ||
| 14 | does not begin with a digit, or when the value would exceed max. */ | ||
| 15 | size_t scan_udec(const char *src, uint64_t *dest, uint64_t max); | ||
| 16 | |||
| 17 | /* As scan_udec, but following the bencode integer grammar, which permits a | ||
| 18 | leading zero on zero alone. */ | ||
| 19 | size_t scan_udec_bencode(const char *src, uint64_t *dest, uint64_t max); | ||
| 20 | |||
| 21 | #endif | ||
