| 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
22 extern "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);
62
73WS_DLL_PUBLIC bool ws_strtoi8 (const char* str, const char** endptr, int8_t* cint);
74
85WS_DLL_PUBLIC bool ws_strtoi (const char* str, const char** endptr, int* cint);
86
94WS_DLL_PUBLIC bool ws_strtou64(const char* str, const char** endptr, uint64_t* cint);
95
103WS_DLL_PUBLIC bool ws_strtou32(const char* str, const char** endptr, uint32_t* cint);
104
111WS_DLL_PUBLIC bool ws_strtou16(const char* str, const char** endptr, uint16_t* cint);
112
122WS_DLL_PUBLIC bool ws_strtou8 (const char* str, const char** endptr, uint8_t* cint);
123
132WS_DLL_PUBLIC bool ws_strtou (const char* str, const char** endptr, unsigned* cint);
133
146WS_DLL_PUBLIC bool ws_hexstrtou64(const char* str, const char** endptr, uint64_t* cint);
147
148
149WS_DLL_PUBLIC bool ws_hexstrtou32(const char* str, const char** endptr, uint32_t* cint);
150
159WS_DLL_PUBLIC bool ws_hexstrtou16(const char* str, const char** endptr, uint16_t* cint);
160
169WS_DLL_PUBLIC bool ws_hexstrtou8 (const char* str, const char** endptr, uint8_t* cint);
170
179WS_DLL_PUBLIC bool ws_hexstrtou (const char* str, const char** endptr, unsigned* cint);
180
197WS_DLL_PUBLIC bool ws_basestrtou64(const char* str, const char** endptr, uint64_t* cint, int base);
198
215WS_DLL_PUBLIC bool ws_basestrtou32(const char* str, const char** endptr, uint32_t* cint, int base);
216
233WS_DLL_PUBLIC bool ws_basestrtou16(const char* str, const char** endptr, uint16_t* cint, int base);
234
251WS_DLL_PUBLIC bool ws_basestrtou8 (const char* str, const char** endptr, uint8_t* cint, int base);
252
269WS_DLL_PUBLIC bool ws_basestrtou (const char* str, const char** endptr, unsigned* cint, int base);
270
271/*
272 * @brief Convert a counted string (not necessarily null terminated, of the
273 * given length) in the specified base to an unsigned 64-bit integer, with
274 * error checks.
275 *
276 * @param buf The string buffer to convert
277 * @param len The length of the string
278 * @param endptr A pointer that will store a pointer to the first invalid
279 * character in str, allowing a number to be parsed even if there is trailing
280 * whitespace. If NULL, then the string is assumed to contain only valid
281 * characters (or it will error out).
282 * @param cint The converted integer
283 * @param base The base for the integer; 0 means "if it begins with 0x,
284 * it's hex, otherwise if it begins with 0, it's octal, otherwise it's
285 * decimal".
286 * @return true if the conversion succeeds, false otherwise.
287 * On error, errno is set to EINVAL for unrecognized input and ERANGE
288 * if the resulting number does not fit in the type.
289 *
290 * @note This is useful when a string representation of an integer is not
291 * null-terminated and also cannot be modified to insert a NULL (e.g.,
292 * a const uint8_t* from packet data), avoiding having to copy the string.
293 * This does not allow a sign, neither '+' nor '-', prefixing the string,
294 * unlike strtoull and g_ascii_strtoull. (The latter allow a negative sign
295 * and cast to unsigned in the normal way.)
296 */
297WS_DLL_PUBLIC bool ws_basebuftou64(const uint8_t* buf, size_t len, const uint8_t** endptr, uint64_t* cint, int base);
298
299/*
300 * @brief Convert a counted decimal string (not necessarily null terminated,
301 * of the given length) to an unsigned 64-bit integer, with error checks.
302 *
303 * @param buf The string buffer to convert
304 * @param len The length of the string
305 * @param endptr A pointer that will store a pointer to the first invalid
306 * character in str, allowing a number to be parsed even if there is trailing
307 * whitespace. If NULL, then the string is assumed to contain only valid
308 * characters (or it will error out).
309 * @param cint The converted integer
310 * @return true if the conversion succeeds, false otherwise.
311 * On error, errno is set to EINVAL for unrecognized input and ERANGE
312 * if the resulting number does not fit in the type.
313 *
314 * @note This does not allow a sign, neither '+' nor '-', prefixing the string,
315 * unlike strtoull and g_ascii_strtoull. (The latter allow a negative sign
316 * and cast to unsigned in the normal way.)
317 */
318WS_DLL_PUBLIC bool ws_buftou64(const uint8_t* buf, size_t len, const uint8_t** endptr, uint64_t* cint);
319
320/*
321 * @brief Convert a counted hexadecimal string (not necessarily null terminated,
322 * of the given length) to an unsigned 64-bit integer, with error checks.
323 *
324 * @param buf The string buffer to convert
325 * @param len The length of the string
326 * @param endptr A pointer that will store a pointer to the first invalid
327 * character in str, allowing a number to be parsed even if there is trailing
328 * whitespace. If NULL, then the string is assumed to contain only valid
329 * characters (or it will error out).
330 * @param cint The converted integer
331 * @return true if the conversion succeeds, false otherwise.
332 * On error, errno is set to EINVAL for unrecognized input and ERANGE
333 * if the resulting number does not fit in the type.
334 *
335 * @note This does not allow a sign, neither '+' nor '-', prefixing the string,
336 * unlike strtoull and g_ascii_strtoull. (The latter allow a negative sign
337 * and cast to unsigned in the normal way.)
338 */
339WS_DLL_PUBLIC bool ws_hexbuftou64(const uint8_t* buf, size_t len, const uint8_t** endptr, uint64_t* cint);
340
341WS_DLL_PUBLIC bool ws_basebuftou32(const uint8_t* buf, size_t len, const uint8_t** endptr, uint32_t* cint, int base);
342
343WS_DLL_PUBLIC bool ws_buftou32(const uint8_t* buf, size_t len, const uint8_t** endptr, uint32_t* cint);
344
345WS_DLL_PUBLIC bool ws_hexbuftou32(const uint8_t* buf, size_t len, const uint8_t** endptr, uint32_t* cint);
346
347WS_DLL_PUBLIC bool ws_basebuftou16(const uint8_t* buf, size_t len, const uint8_t** endptr, uint16_t* cint, int base);
348
349WS_DLL_PUBLIC bool ws_buftou16(const uint8_t* buf, size_t len, const uint8_t** endptr, uint16_t* cint);
350
351WS_DLL_PUBLIC bool ws_hexbuftou16(const uint8_t* buf, size_t len, const uint8_t** endptr, uint16_t* cint);
352
353WS_DLL_PUBLIC bool ws_basebuftou8(const uint8_t* buf, size_t len, const uint8_t** endptr, uint8_t* cint, int base);
354
355WS_DLL_PUBLIC bool ws_buftou8(const uint8_t* buf, size_t len, const uint8_t** endptr, uint8_t* cint);
356
357WS_DLL_PUBLIC bool ws_hexbuftou8(const uint8_t* buf, size_t len, const uint8_t** endptr, uint8_t* cint);
358
359#ifdef __cplusplus
360}
361#endif /* __cplusplus */
362
363#endif
364
365/*
366 * Editor modelines - https://www.wireshark.org/tools/modelines.html
367 *
368 * Local variables:
369 * c-basic-offset: 4
370 * tab-width: 8
371 * indent-tabs-mode: t
372 * End:
373 *
374 * vi: set shiftwidth=4 tabstop=8 noexpandtab:
375 * :indentSize=4:tabSize=8:noTabs=false:
376 */
ws_strtou32
WS_DLL_PUBLIC bool ws_strtou32(const char *str, const char **endptr, uint32_t *cint)
Convert a decimal string to a 32-bit unsigned int, with error checks.
Definition strtoi.c:228
ws_basestrtou16
WS_DLL_PUBLIC bool ws_basestrtou16(const char *str, const char **endptr, uint16_t *cint, int base)
Convert a string in the specified base to a 16-bit unsigned int, with error checks.
Definition strtoi.c:229
ws_hexstrtou16
WS_DLL_PUBLIC bool ws_hexstrtou16(const char *str, const char **endptr, uint16_t *cint)
Convert a hexadecimal string to an 16-bit unsigned int, with error checks.
Definition strtoi.c:229
ws_strtoi16
WS_DLL_PUBLIC bool ws_strtoi16(const char *str, const char **endptr, int16_t *cint)
Convert a string to a 16-bit integer.
Definition strtoi.c:92
ws_basestrtou8
WS_DLL_PUBLIC bool ws_basestrtou8(const char *str, const char **endptr, uint8_t *cint, int base)
Convert a string in the specified base to an 8-bit unsigned int, with error checks.
Definition strtoi.c:230
ws_strtou8
WS_DLL_PUBLIC bool ws_strtou8(const char *str, const char **endptr, uint8_t *cint)
Convert a decimal string to an unsigned int, with error checks.
Definition strtoi.c:230
ws_basestrtou
WS_DLL_PUBLIC bool ws_basestrtou(const char *str, const char **endptr, unsigned *cint, int base)
Convert a string in the specified base to an unsigned int, with error checks.
Definition strtoi.c:232
ws_strtou
WS_DLL_PUBLIC bool ws_strtou(const char *str, const char **endptr, unsigned *cint)
Convert a decimal string to an unsigned int, with error checks.
Definition strtoi.c:262
ws_strtou16
WS_DLL_PUBLIC bool ws_strtou16(const char *str, const char **endptr, uint16_t *cint)
Convert a decimal string to an unsigned int, with error checks.
Definition strtoi.c:229
ws_strtoi
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_hexstrtou8
WS_DLL_PUBLIC bool ws_hexstrtou8(const char *str, const char **endptr, uint8_t *cint)
Convert a hexadecimal string in the specified base to an 8-bit unsigned int, with error checks.
Definition strtoi.c:230
ws_basestrtou32
WS_DLL_PUBLIC bool ws_basestrtou32(const char *str, const char **endptr, uint32_t *cint, int base)
Convert a string in the specified base to a 32-bit unsigned int, with error checks.
Definition strtoi.c:228
ws_hexstrtou
WS_DLL_PUBLIC bool ws_hexstrtou(const char *str, const char **endptr, unsigned *cint)
Convert a hexadecimal string to an unsigned int, with error checks.
Definition strtoi.c:267
ws_strtoi32
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_hexstrtou64
WS_DLL_PUBLIC bool ws_hexstrtou64(const char *str, const char **endptr, uint64_t *cint)
Convert a hexdecimal string to an unsigned int, with error checks.
Definition strtoi.c:191
ws_basestrtou64
WS_DLL_PUBLIC bool ws_basestrtou64(const char *str, const char **endptr, uint64_t *cint, int base)
Convert a string in the specified base to a 64-bit unsigned int, with error checks.
Definition strtoi.c:139
ws_strtou64
WS_DLL_PUBLIC bool ws_strtou64(const char *str, const char **endptr, uint64_t *cint)
Convert a decimal string to a 64-bit unsigned int, with error checks.
Definition strtoi.c:186
ws_strtoi8
WS_DLL_PUBLIC bool ws_strtoi8(const char *str, const char **endptr, int8_t *cint)
Convert a string to an 8-bit integer.
Definition strtoi.c:93