diff options
| author | Dirk Engling <erdgeist@erdgeist.org> | 2026-10-26 18:13:27 +0100 |
|---|---|---|
| committer | Dirk Engling <erdgeist@erdgeist.org> | 2026-10-26 18:13:27 +0100 |
| commit | 78c3cebc389a2b0d426d74900a65b8656edb426d (patch) | |
| tree | 122003fecc6b53c2dde6393896a852e3689900ff /ot_scan.c | |
| parent | 6837c5ac80e3624c6dc3a82d03e50d68b2f5a989 (diff) | |
Third run of making variable and function names more readable, integer scans and ip parsers
Diffstat (limited to 'ot_scan.c')
| -rw-r--r-- | ot_scan.c | 29 |
1 files changed, 26 insertions, 3 deletions
| @@ -11,7 +11,7 @@ | |||
| 11 | /* Opentracker */ | 11 | /* Opentracker */ |
| 12 | #include "ot_scan.h" | 12 | #include "ot_scan.h" |
| 13 | 13 | ||
| 14 | size_t scan_udec(const char *src, uint64_t *dest, uint64_t max) { | 14 | size_t scan_uint(const char *src, uint64_t *dest, uint64_t max) { |
| 15 | unsigned long long value; | 15 | unsigned long long value; |
| 16 | char *end; | 16 | char *end; |
| 17 | 17 | ||
| @@ -28,9 +28,9 @@ size_t scan_udec(const char *src, uint64_t *dest, uint64_t max) { | |||
| 28 | return (size_t)(end - src); | 28 | return (size_t)(end - src); |
| 29 | } | 29 | } |
| 30 | 30 | ||
| 31 | size_t scan_udec_bencode(const char *src, uint64_t *dest, uint64_t max) { | 31 | size_t scan_uint_bencode(const char *src, uint64_t *dest, uint64_t max) { |
| 32 | uint64_t value; | 32 | uint64_t value; |
| 33 | size_t consumed = scan_udec(src, &value, max); | 33 | size_t consumed = scan_uint(src, &value, max); |
| 34 | 34 | ||
| 35 | /* bencode writes no leading zeros, so anything longer than a single digit | 35 | /* bencode writes no leading zeros, so anything longer than a single digit |
| 36 | must not begin with one. */ | 36 | must not begin with one. */ |
| @@ -42,3 +42,26 @@ size_t scan_udec_bencode(const char *src, uint64_t *dest, uint64_t max) { | |||
| 42 | 42 | ||
| 43 | return consumed; | 43 | return consumed; |
| 44 | } | 44 | } |
| 45 | |||
| 46 | int scan_int(const char *src, size_t len, int64_t *dest) { | ||
| 47 | int64_t value = 0; | ||
| 48 | int negate = 0; | ||
| 49 | |||
| 50 | if (len && *src == '-') | ||
| 51 | --len, ++src, negate = 1; | ||
| 52 | if (!len) | ||
| 53 | return -1; | ||
| 54 | |||
| 55 | while (len--) { | ||
| 56 | if (*src < '0' || *src > '9') | ||
| 57 | return -1; | ||
| 58 | /* A twenty character field of nines does not fit, and silently wrapping | ||
| 59 | would magically heal a nonsense announce */ | ||
| 60 | if (value > (INT64_MAX - (*src - '0')) / 10) | ||
| 61 | return -1; | ||
| 62 | value = 10 * value + (*src++ - '0'); | ||
| 63 | } | ||
| 64 | |||
| 65 | *dest = negate ? -value : value; | ||
| 66 | return 0; | ||
| 67 | } | ||
