Wireshark 4.7.0
The Wireshark network protocol analyzer
Loading...
Searching...
No Matches
strtoi.h
Go to the documentation of this file.
1
13#ifndef _WS_STRTOI_H
14#define _WS_STRTOI_H
15
16#include <stdbool.h>
17#include <inttypes.h>
18
19#include "ws_symbol_export.h"
20
21#ifdef __cplusplus
22extern "C" {
23#endif /* __cplusplus */
24
25/*
26 * \brief Convert a decimal string to a signed/unsigned int, with error checks.
27 * \param str The string to convert
28 * \param endptr A pointer that will store a pointer to the first invalid
29 * character in str, allowing a number to be parsed even if there is trailing
30 * whitespace. If NULL, then the string is assumed to contain only valid
31 * characters (or it will error out).
32 * \param cint The converted integer
33 * \return true if the conversion succeeds, false otherwise.
34 * On error, errno is set to EINVAL for unrecognized input and ERANGE
35 * if the resulting number does not fit in the type.
36 */
37WS_DLL_PUBLIC bool ws_strtoi64(const char* str, const char** endptr, int64_t* cint);
38
49WS_DLL_PUBLIC bool ws_strtoi32(const char* str, const char** endptr, int32_t* cint);
50
61WS_DLL_PUBLIC bool ws_strtoi16(const char* str, const char** endptr, int16_t* cint);
62WS_DLL_PUBLIC bool ws_strtoi8 (const char* str, const char** endptr, int8_t* cint);
63
74WS_DLL_PUBLIC bool ws_strtoi (const char* str, const char** endptr, int* cint);
75
81WS_DLL_PUBLIC bool ws_strtou64(const char* str, const char** endptr, uint64_t* cint);
82WS_DLL_PUBLIC bool ws_strtou32(const char* str, const char** endptr, uint32_t* cint);
83
92WS_DLL_PUBLIC bool ws_strtou16(const char* str, const char** endptr, uint16_t* cint);
93
103WS_DLL_PUBLIC bool ws_strtou8 (const char* str, const char** endptr, uint8_t* cint);
104
113WS_DLL_PUBLIC bool ws_strtou (const char* str, const char** endptr, unsigned* cint);
114
115/*
116 * \brief Convert a hexadecimal string to an unsigned int, with error checks.
117 * \param str The string to convert
118 * \param endptr A pointer that will store a pointer to the first invalid
119 * character in str, allowing a number to be parsed even if there is trailing
120 * whitespace. If NULL, then the string is assumed to contain only valid
121 * characters (or it will error out).
122 * \param cint The converted integer
123 * \return true if the conversion succeeds, false otherwise.
124 * On error, errno is set to EINVAL for unrecognized input and ERANGE
125 * if the resulting number does not fit in the type.
126 */
127
128WS_DLL_PUBLIC bool ws_hexstrtou64(const char* str, const char** endptr, uint64_t* cint);
129
139WS_DLL_PUBLIC bool ws_hexstrtou32(const char* str, const char** endptr, uint32_t* cint);
140
149WS_DLL_PUBLIC bool ws_hexstrtou16(const char* str, const char** endptr, uint16_t* cint);
150WS_DLL_PUBLIC bool ws_hexstrtou8 (const char* str, const char** endptr, uint8_t* cint);
151
160WS_DLL_PUBLIC bool ws_hexstrtou (const char* str, const char** endptr, unsigned* cint);
161
162/*
163 * \brief Convert a string in the specified base to an unsigned int, with
164 * error checks.
165 * \param str The string to convert
166 * \param endptr A pointer that will store a pointer to the first invalid
167 * character in str, allowing a number to be parsed even if there is trailing
168 * whitespace. If NULL, then the string is assumed to contain only valid
169 * characters (or it will error out).
170 * \param cint The converted integer
171 * \param base The base for the integer; 0 means "if it begins with 0x,
172 * it's hex, otherwise if it begins with 0, it's octal, otherwise it's
173 * decimal".
174 * \return true if the conversion succeeds, false otherwise.
175 * On error, errno is set to EINVAL for unrecognized input and ERANGE
176 * if the resulting number does not fit in the type.
177 */
178
179WS_DLL_PUBLIC bool ws_basestrtou64(const char* str, const char** endptr, uint64_t* cint, int base);
180
192WS_DLL_PUBLIC bool ws_basestrtou32(const char* str, const char** endptr, uint32_t* cint, int base);
193
205WS_DLL_PUBLIC bool ws_basestrtou16(const char* str, const char** endptr, uint16_t* cint, int base);
206
218WS_DLL_PUBLIC bool ws_basestrtou8 (const char* str, const char** endptr, uint8_t* cint, int base);
219
235WS_DLL_PUBLIC bool ws_basestrtou (const char* str, const char** endptr, unsigned* cint, int base);
236
237/*
238 * @brief Convert a counted string (not necessarily null terminated, of the
239 * given length) in the specified base to an unsigned 64-bit integer, with
240 * error checks.
241 *
242 * @param buf The string buffer to convert
243 * @param len The length of the string
244 * @param endptr A pointer that will store a pointer to the first invalid
245 * character in str, allowing a number to be parsed even if there is trailing
246 * whitespace. If NULL, then the string is assumed to contain only valid
247 * characters (or it will error out).
248 * @param cint The converted integer
249 * @param base The base for the integer; 0 means "if it begins with 0x,
250 * it's hex, otherwise if it begins with 0, it's octal, otherwise it's
251 * decimal".
252 * @return true if the conversion succeeds, false otherwise.
253 * On error, errno is set to EINVAL for unrecognized input and ERANGE
254 * if the resulting number does not fit in the type.
255 *
256 * @note This is useful when a string representation of an integer is not
257 * null-terminated and also cannot be modified to insert a NULL (e.g.,
258 * a const uint8_t* from packet data), avoiding having to copy the string.
259 * This does not allow a sign, neither '+' nor '-', prefixing the string,
260 * unlike strtoull and g_ascii_strtoull. (The latter allow a negative sign
261 * and cast to unsigned in the normal way.)
262 */
263WS_DLL_PUBLIC bool ws_basebuftou64(const uint8_t* buf, size_t len, const uint8_t** endptr, uint64_t* cint, int base);
264
265/*
266 * @brief Convert a counted decimal string (not necessarily null terminated,
267 * of the given length) to an unsigned 64-bit integer, with error checks.
268 *
269 * @param buf The string buffer to convert
270 * @param len The length of the string
271 * @param endptr A pointer that will store a pointer to the first invalid
272 * character in str, allowing a number to be parsed even if there is trailing
273 * whitespace. If NULL, then the string is assumed to contain only valid
274 * characters (or it will error out).
275 * @param cint The converted integer
276 * @return true if the conversion succeeds, false otherwise.
277 * On error, errno is set to EINVAL for unrecognized input and ERANGE
278 * if the resulting number does not fit in the type.
279 *
280 * @note This does not allow a sign, neither '+' nor '-', prefixing the string,
281 * unlike strtoull and g_ascii_strtoull. (The latter allow a negative sign
282 * and cast to unsigned in the normal way.)
283 */
284WS_DLL_PUBLIC bool ws_buftou64(const uint8_t* buf, size_t len, const uint8_t** endptr, uint64_t* cint);
285
286/*
287 * @brief Convert a counted hexadecimal string (not necessarily null terminated,
288 * of the given length) to an unsigned 64-bit integer, with error checks.
289 *
290 * @param buf The string buffer to convert
291 * @param len The length of the string
292 * @param endptr A pointer that will store a pointer to the first invalid
293 * character in str, allowing a number to be parsed even if there is trailing
294 * whitespace. If NULL, then the string is assumed to contain only valid
295 * characters (or it will error out).
296 * @param cint The converted integer
297 * @return true if the conversion succeeds, false otherwise.
298 * On error, errno is set to EINVAL for unrecognized input and ERANGE
299 * if the resulting number does not fit in the type.
300 *
301 * @note This does not allow a sign, neither '+' nor '-', prefixing the string,
302 * unlike strtoull and g_ascii_strtoull. (The latter allow a negative sign
303 * and cast to unsigned in the normal way.)
304 */
305WS_DLL_PUBLIC bool ws_hexbuftou64(const uint8_t* buf, size_t len, const uint8_t** endptr, uint64_t* cint);
306
307WS_DLL_PUBLIC bool ws_basebuftou32(const uint8_t* buf, size_t len, const uint8_t** endptr, uint32_t* cint, int base);
308
309WS_DLL_PUBLIC bool ws_buftou32(const uint8_t* buf, size_t len, const uint8_t** endptr, uint32_t* cint);
310
311WS_DLL_PUBLIC bool ws_hexbuftou32(const uint8_t* buf, size_t len, const uint8_t** endptr, uint32_t* cint);
312
313WS_DLL_PUBLIC bool ws_basebuftou16(const uint8_t* buf, size_t len, const uint8_t** endptr, uint16_t* cint, int base);
314
315WS_DLL_PUBLIC bool ws_buftou16(const uint8_t* buf, size_t len, const uint8_t** endptr, uint16_t* cint);
316
317WS_DLL_PUBLIC bool ws_hexbuftou16(const uint8_t* buf, size_t len, const uint8_t** endptr, uint16_t* cint);
318
319WS_DLL_PUBLIC bool ws_basebuftou8(const uint8_t* buf, size_t len, const uint8_t** endptr, uint8_t* cint, int base);
320
321WS_DLL_PUBLIC bool ws_buftou8(const uint8_t* buf, size_t len, const uint8_t** endptr, uint8_t* cint);
322
323WS_DLL_PUBLIC bool ws_hexbuftou8(const uint8_t* buf, size_t len, const uint8_t** endptr, uint8_t* cint);
324
325#ifdef __cplusplus
326}
327#endif /* __cplusplus */
328
329#endif
330
331/*
332 * Editor modelines - https://www.wireshark.org/tools/modelines.html
333 *
334 * Local variables:
335 * c-basic-offset: 4
336 * tab-width: 8
337 * indent-tabs-mode: t
338 * End:
339 *
340 * vi: set shiftwidth=4 tabstop=8 noexpandtab:
341 * :indentSize=4:tabSize=8:noTabs=false:
342 */
WS_DLL_PUBLIC bool ws_basestrtou16(const char *str, const char **endptr, uint16_t *cint, int base)
Convert a string to an unsigned integer of specified bits.
Definition strtoi.c:229
WS_DLL_PUBLIC bool ws_hexstrtou16(const char *str, const char **endptr, uint16_t *cint)
Convert a string in the specified base to an unsigned int, with error checks.
Definition strtoi.c:229
WS_DLL_PUBLIC bool ws_strtoi16(const char *str, const char **endptr, int16_t *cint)
Convert a string to an integer of specified size.
Definition strtoi.c:92
WS_DLL_PUBLIC bool ws_basestrtou8(const char *str, const char **endptr, uint8_t *cint, int base)
Convert a string to an unsigned integer with specified base.
Definition strtoi.c:230
WS_DLL_PUBLIC bool ws_strtou8(const char *str, const char **endptr, uint8_t *cint)
Convert a hexadecimal string to an unsigned int, with error checks.
Definition strtoi.c:230
WS_DLL_PUBLIC bool ws_basestrtou(const char *str, const char **endptr, unsigned *cint, int base)
Converts a string to an unsigned integer with error handling.
Definition strtoi.c:232
WS_DLL_PUBLIC bool ws_strtou(const char *str, const char **endptr, unsigned *cint)
Convert a hexadecimal string to an unsigned int, with error checks.
Definition strtoi.c:262
WS_DLL_PUBLIC bool ws_strtou16(const char *str, const char **endptr, uint16_t *cint)
Convert a hexadecimal string to an unsigned int, with error checks.
Definition strtoi.c:229
WS_DLL_PUBLIC bool ws_strtoi(const char *str, const char **endptr, int *cint)
Convert a string to an integer.
Definition strtoi.c:95
WS_DLL_PUBLIC bool ws_basestrtou32(const char *str, const char **endptr, uint32_t *cint, int base)
Converts a string to an unsigned integer with specified base.
Definition strtoi.c:228
WS_DLL_PUBLIC bool ws_hexstrtou(const char *str, const char **endptr, unsigned *cint)
Convert a string in the specified base to an unsigned int, with error checks.
Definition strtoi.c:267
WS_DLL_PUBLIC bool ws_strtoi32(const char *str, const char **endptr, int32_t *cint)
Convert a string to an integer of specified size.
Definition strtoi.c:91
WS_DLL_PUBLIC bool ws_hexstrtou32(const char *str, const char **endptr, uint32_t *cint)
Convert a string in the specified base to an unsigned int, with error checks.
Definition strtoi.c:228
WS_DLL_PUBLIC bool ws_strtou64(const char *str, const char **endptr, uint64_t *cint)
Convert a hexadecimal string to an unsigned int, with error checks.
Definition strtoi.c:186