Debian Codegraph @

src/utils-numbers.h

raw
1 /*
2 * Copyright © 2025 Pierre Le Marre <[email protected]>
3 * SPDX-License-Identifier: MIT
4 */
5 #pragma once
6
7 #include "config.h"
8
9 #include <stdbool.h>
10 #include <stdint.h>
11
12 #include "utils.h"
13
14 /*
15 * Define various parsers to avoid the use of strto* -- it’s slower and accepts
16 * a bunch of stuff we don’t want to allow, like signs, spaces, even locale stuff.
17 *
18 * But the real feature is that it does not require a NULL-terminated string, so
19 * it works safely *also* on any buffer, assuming the correct corresponding size
20 * is provided. For NULL-terminated strings, just pass `SIZE_MAX` as the length:
21 * the parsers will *always* stop on a NULL character.
22 */
23
24 #define MAKE_PARSE_DEC_TO(type, max) \
25 static inline int \
26 parse_dec_to_##type(const char *s, size_t len, type (*out)) \
27 { \
28 type result = 0; \
29 size_t i; \
30 for (i = 0; \
31 i < len && (unsigned char)(s[i] - '0') < 10U && \
32 result <= (max) / 10 && \
33 result * 10 <= (max) - (unsigned char) (s[i] - '0');\
34 i++) { \
35 result = result * 10 + (s[i] - '0'); \
36 } \
37 *out = result; \
38 /* Check if there is more to parse */ \
39 /* We can safely convert the length to int on success */ \
40 return (i >= len || (unsigned char)(s[i] - '0') >= 10U) \
41 ? (int) i \
42 : -1; \
43 }
44
45 /**
46 * Parse a `uint32_t` in decimal format.
47 *
48 * @returns -1 on error (overflow) or the count of characters parsed.
49 */
50 MAKE_PARSE_DEC_TO(uint32_t, UINT32_MAX)
51
52 /**
53 * Parse a `uint64_t` in decimal format.
54 *
55 * @returns -1 on error (overflow) or the count of characters parsed.
56 */
57 MAKE_PARSE_DEC_TO(uint64_t, UINT64_MAX)
58
59 #undef MAKE_PARSE_DEC_TO
60
61 static const unsigned char digits__[] = {
62 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
63 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
64 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
65 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
66 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0xff, 0xff,
67 0xff, 0xff, 0xff, 0xff, 0xff, 10, 11, 12, 13, 14, 15, 0xff,
68 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
69 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
70 0xff, 10, 11, 12, 13, 14, 15, 0xff, 0xff, 0xff, 0xff, 0xff,
71 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
72 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
73 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
74 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
75 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
76 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
77 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
78 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
79 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
80 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
81 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
82 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
83 0xff, 0xff, 0xff, 0xff
84 };
85
86 #define MAKE_PARSE_HEX_TO(type, max) \
87 static inline int \
88 parse_hex_to_##type(const char *s, size_t len, type (*out)) \
89 { \
90 type result = 0; \
91 size_t i = 0; \
92 for (; i < len && digits__[(unsigned char) s[i]] < 16u && \
93 result <= (max) >> 4; \
94 i++) { \
95 result = result * 16 + digits__[(unsigned char) s[i]]; \
96 } \
97 *out = result; \
98 /* Check if there is more to parse */ \
99 /* We can safely convert the length to int on success */ \
100 return (i >= len || !is_xdigit(s[i])) ? (int) i : -1; \
101 }
102
103 /**
104 * Parse a `uint32_t` in hexdecimal format.
105 *
106 * @returns -1 on error (overflow) or the count of characters parsed.
107 */
108 MAKE_PARSE_HEX_TO(uint32_t, UINT32_MAX)
109
110 /**
111 * Parse a `uint64_t` in hexdecimal format.
112 *
113 * @returns -1 on error (overflow) or the count of characters parsed.
114 */
115 MAKE_PARSE_HEX_TO(uint64_t, UINT64_MAX)
116
117 #undef MAKE_PARSE_HEX_TO