summaryrefslogtreecommitdiff
path: root/ot_scan.c
diff options
context:
space:
mode:
Diffstat (limited to 'ot_scan.c')
-rw-r--r--ot_scan.c44
1 files changed, 44 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
14size_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
31size_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}