dcerpc-nt: add UNION_ALIGN_TO... helpers
[wireshark-sm.git] / wsutil / strtoi.c
blobb64763a537401652d0b4c58f8357973b33948147
1 /* strtoi.c
2 * Utilities to convert strings to integers
4 * Copyright 2016, Dario Lombardo
6 * Wireshark - Network traffic analyzer
7 * By Gerald Combs <gerald@wireshark.org>
8 * Copyright 1998 Gerald Combs
10 * SPDX-License-Identifier: GPL-2.0-or-later
13 #include "config.h"
15 #include <errno.h>
17 #include <glib.h>
19 #include "strtoi.h"
20 #include <wsutil/ws_assert.h>
22 bool ws_strtoi64(const char* str, const char** endptr, int64_t* cint)
24 char* end;
25 int64_t val;
27 ws_assert(cint);
29 if (!str) {
30 errno = EINVAL;
31 return false;
34 errno = 0;
35 val = g_ascii_strtoll(str, &end, 10);
36 if ((val == 0 && end == str) || (endptr == NULL && *end != '\0')) {
37 *cint = 0;
38 if (endptr != NULL)
39 *endptr = end;
40 errno = EINVAL;
41 return false;
43 if ((val == INT64_MAX || val == INT64_MIN) && errno == ERANGE) {
45 * Return the value, so our caller knows whether to
46 * report the value as "too small" or "too large".
48 *cint = val;
49 if (endptr != NULL)
50 *endptr = end;
51 /* errno is already set */
52 return false;
54 if (endptr != NULL)
55 *endptr = end;
56 *cint = val;
57 return true;
60 #define DEFINE_WS_STRTOI_BITS(bits) \
61 bool ws_strtoi##bits(const char* str, const char** endptr, int##bits##_t* cint) \
62 { \
63 int64_t val = 0; \
64 if (!ws_strtoi64(str, endptr, &val)) { \
65 /* \
66 * For ERANGE, return either INT##bits##_MIN or \
67 * INT##bits##_MAX so our caller knows whether \
68 * to report the value as "too small" or "too \
69 * large". \
70 * \
71 * For other errors, return 0, for parallelism \
72 * with ws_strtoi64(). \
73 */ \
74 if (errno == ERANGE) { \
75 if (val < 0) \
76 *cint = INT##bits##_MIN; \
77 else \
78 *cint = INT##bits##_MAX; \
79 } else \
80 *cint = 0; \
81 return false; \
82 } \
83 if (val < INT##bits##_MIN) { \
84 /* \
85 * Return INT##bits##_MIN so our caller knows whether to \
86 * report the value as "too small" or "too large". \
87 */ \
88 *cint = INT##bits##_MIN; \
89 errno = ERANGE; \
90 return false; \
91 } \
92 if (val > INT##bits##_MAX) { \
93 /* \
94 * Return INT##bits##_MAX so our caller knows whether to \
95 * report the value as "too small" or "too large". \
96 */ \
97 *cint = INT##bits##_MAX; \
98 errno = ERANGE; \
99 return false; \
101 *cint = (int##bits##_t)val; \
102 return true; \
105 DEFINE_WS_STRTOI_BITS(32)
106 DEFINE_WS_STRTOI_BITS(16)
107 DEFINE_WS_STRTOI_BITS(8)
109 bool ws_strtoi(const char* str, const char** endptr, int* cint)
111 int64_t val = 0;
112 if (!ws_strtoi64(str, endptr, &val)) {
114 * For ERANGE, return either INT_MIN or
115 * INT_MAX so our caller knows whether
116 * to report the value as "too small" or "too
117 * large".
119 * For other errors, return 0, for parallelism
120 * with ws_strtoi64().
122 if (errno == ERANGE) {
123 if (val < 0)
124 *cint = INT_MIN;
125 else
126 *cint = INT_MAX;
127 } else
128 *cint = 0;
129 return false;
131 if (val < INT_MIN) {
133 * Return INT_MIN so our caller knows whether to
134 * report the value as "too small" or "too large".
136 *cint = INT_MIN;
137 errno = ERANGE;
138 return false;
140 if (val > INT_MAX) {
142 * Return INT_MAX so our caller knows whether to
143 * report the value as "too small" or "too large".
145 *cint = INT_MAX;
146 errno = ERANGE;
147 return false;
149 *cint = (int)val;
150 return true;
153 bool ws_basestrtou64(const char* str, const char** endptr, uint64_t* cint, int base)
155 char* end;
156 uint64_t val;
158 ws_assert(cint);
160 if (!str) {
161 errno = EINVAL;
162 return false;
165 if (str[0] == '-' || str[0] == '+') {
167 * Unsigned numbers don't have a sign.
169 *cint = 0;
170 if (endptr != NULL)
171 *endptr = str;
172 errno = EINVAL;
173 return false;
175 errno = 0;
176 val = g_ascii_strtoull(str, &end, base);
177 if ((val == 0 && end == str) || (endptr == NULL && *end != '\0')) {
178 *cint = 0;
179 if (endptr != NULL)
180 *endptr = end;
181 errno = EINVAL;
182 return false;
184 if (val == UINT64_MAX && errno == ERANGE) {
186 * Return the value, because ws_strtoi64() does.
188 *cint = val;
189 if (endptr != NULL)
190 *endptr = end;
191 /* errno is already set */
192 return false;
194 if (endptr != NULL)
195 *endptr = end;
196 *cint = val;
197 return true;
200 bool ws_strtou64(const char* str, const char** endptr, uint64_t* cint)
202 return ws_basestrtou64(str, endptr, cint, 10);
205 bool ws_hexstrtou64(const char* str, const char** endptr, uint64_t* cint)
207 return ws_basestrtou64(str, endptr, cint, 16);
210 #define DEFINE_WS_STRTOU_BITS(bits) \
211 bool ws_basestrtou##bits(const char* str, const char** endptr, uint##bits##_t* cint, int base) \
213 uint64_t val; \
214 if (!ws_basestrtou64(str, endptr, &val, base)) { \
215 /* \
216 * For ERANGE, return UINT##bits##_MAX for parallelism \
217 * with ws_strtoi##bits(). \
219 * For other errors, return 0, for parallelism \
220 * with ws_basestrtou64(). \
221 */ \
222 if (errno == ERANGE) \
223 *cint = UINT##bits##_MAX; \
224 else \
225 *cint = 0; \
226 return false; \
228 if (val > UINT##bits##_MAX) { \
229 /* \
230 * Return UINT##bits##_MAX for parallelism with \
231 * ws_strtoi##bits(). \
232 */ \
233 *cint = UINT##bits##_MAX; \
234 errno = ERANGE; \
235 return false; \
237 *cint = (uint##bits##_t)val; \
238 return true; \
241 bool ws_strtou##bits(const char* str, const char** endptr, uint##bits##_t* cint) \
243 return ws_basestrtou##bits(str, endptr, cint, 10); \
246 bool ws_hexstrtou##bits(const char* str, const char** endptr, uint##bits##_t* cint) \
248 return ws_basestrtou##bits(str, endptr, cint, 16); \
251 DEFINE_WS_STRTOU_BITS(32)
252 DEFINE_WS_STRTOU_BITS(16)
253 DEFINE_WS_STRTOU_BITS(8)
255 bool ws_basestrtou(const char* str, const char** endptr, unsigned* cint, int base)
257 uint64_t val;
258 if (!ws_basestrtou64(str, endptr, &val, base)) {
260 * For ERANGE, return UINT_MAX for parallelism
261 * with ws_strtoi().
263 * For other errors, return 0, for parallelism
264 * with ws_basestrtou64().
266 if (errno == ERANGE)
267 *cint = UINT_MAX;
268 else
269 *cint = 0;
270 return false;
272 if (val > UINT_MAX) {
274 * Return UINT_MAX for parallelism with
275 * ws_strtoi().
277 *cint = UINT_MAX;
278 errno = ERANGE;
279 return false;
281 *cint = (unsigned)val;
282 return true;
285 bool ws_strtou(const char* str, const char** endptr, unsigned* cint)
287 return ws_basestrtou(str, endptr, cint, 10);
290 bool ws_hexstrtou(const char* str, const char** endptr, unsigned* cint)
292 return ws_basestrtou(str, endptr, cint, 16);
296 * Editor modelines - https://www.wireshark.org/tools/modelines.html
298 * Local variables:
299 * c-basic-offset: 4
300 * tab-width: 8
301 * indent-tabs-mode: t
302 * End:
304 * vi: set shiftwidth=4 tabstop=8 noexpandtab:
305 * :indentSize=4:tabSize=8:noTabs=false: