Wireshark 4.5.0
The Wireshark network protocol analyzer
Loading...
Searching...
No Matches
str_util.h
Go to the documentation of this file.
1
11#ifndef __STR_UTIL_H__
12#define __STR_UTIL_H__
13
14#include <wireshark.h>
15#include <wsutil/wmem/wmem.h>
16
17#ifdef __cplusplus
18extern "C" {
19#endif /* __cplusplus */
20
21WS_DLL_PUBLIC
22char *
23wmem_strconcat(wmem_allocator_t *allocator, const char *first, ...)
24G_GNUC_MALLOC G_GNUC_NULL_TERMINATED;
25
26WS_DLL_PUBLIC
27char *
28wmem_strjoin(wmem_allocator_t *allocator,
29 const char *separator, const char *first, ...)
30G_GNUC_MALLOC G_GNUC_NULL_TERMINATED;
31
45WS_DLL_PUBLIC
46char *
48 const char *separator, char **str_array)
49G_GNUC_MALLOC;
50
62WS_DLL_PUBLIC
63char **
64wmem_strsplit(wmem_allocator_t *allocator, const char *src,
65 const char *delimiter, int max_tokens);
66
82WS_DLL_PUBLIC
83char*
84wmem_ascii_strdown(wmem_allocator_t *allocator, const char *str, ssize_t len);
85
101WS_DLL_PUBLIC
102char *ascii_strdown_inplace(char *str);
103
119WS_DLL_PUBLIC
120char *ascii_strup_inplace(char *str);
121
127WS_DLL_PUBLIC
128bool isprint_string(const char *str);
129
156WS_DLL_PUBLIC
157bool isprint_utf8_string(const char *str, const unsigned length);
158
164WS_DLL_PUBLIC
165bool isdigit_string(const unsigned char *str);
166
182WS_DLL_PUBLIC
183const char *ws_ascii_strcasestr(const char *haystack, const char *needle);
184
193WS_DLL_PUBLIC
194const uint8_t *ws_memrchr(const void *haystack, int ch, size_t n);
195
196WS_DLL_PUBLIC
197char *ws_escape_string(wmem_allocator_t *alloc, const char *string, bool add_quotes);
198
199WS_DLL_PUBLIC
200char *ws_escape_string_len(wmem_allocator_t *alloc, const char *string, ssize_t len, bool add_quotes);
201
202/* Replace null bytes with "\0". */
203WS_DLL_PUBLIC
204char *ws_escape_null(wmem_allocator_t *alloc, const char *string, size_t len, bool add_quotes);
205
206/* Escape as in a number of CSV dialects.
207 *
208 * @param allocator The wmem scope to use to allocate the returned string
209 * @param string The input string to escape
210 * @param add_quotes Whether to surround the string with quote_char
211 * @param quote_char The quote character, always escaped in some way.
212 * @param double_quote Whether to escape the quote character by doubling it
213 * @param escape_whitespace Whether to escape whitespace with a backslash
214 * @return The escaped string
215 *
216 * @note If double_quote is false, then quote_or_delim is escaped with a
217 * backslash ('\'). The quote character can be '\0', in which case it is
218 * ignored. If any character is being escaped with a backslash (i.e.,
219 * quote_char is not '\0' and double_quote is false, or escape_whitespace
220 * is true), then backslash is also escaped. If add_quotes is false, then
221 * quote_char can either be a quote character (if the string will be quoted
222 * later after further manipulation) or the delimiter (to escape it, since
223 * the string is not being quoted.).
224 */
225WS_DLL_PUBLIC
226char *ws_escape_csv(wmem_allocator_t *alloc, const char *string, bool add_quotes, char quote_char, bool double_quote, bool escape_whitespace);
227
228WS_DLL_PUBLIC
229int ws_xton(char ch);
230
231typedef enum {
233 /* XXX - This does not append a trailing space if there is no prefix.
234 * That's good if you intend to list the unit somewhere else, e.g. in a
235 * legend, header, or other column, but doesn't work well if intending
236 * to append your own unit. You can test whether there's a prefix or
237 * not with g_ascii_isdigit() (plus special handling for inf and NaN).
238 */
248 /* These next two aren't really for format_size (which takes an int) */
252
253#define FORMAT_SIZE_PREFIX_SI (1 << 0)
254#define FORMAT_SIZE_PREFIX_IEC (1 << 1)
270WS_DLL_PUBLIC
271char *format_units(wmem_allocator_t *allocator, double size,
272 format_size_units_e unit, uint16_t flags,
273 int precision);
274
284WS_DLL_PUBLIC
285char *format_size_wmem(wmem_allocator_t *allocator, int64_t size,
286 format_size_units_e unit, uint16_t flags);
287
288#define format_size(size, unit, flags) \
289 format_size_wmem(NULL, size, unit, flags)
290
291WS_DLL_PUBLIC
292char printable_char_or_period(char c);
293
294WS_DLL_PUBLIC WS_RETNONNULL
295const char *ws_strerrorname_r(int errnum, char *buf, size_t buf_size);
296
297WS_DLL_PUBLIC
298char *ws_strdup_underline(wmem_allocator_t *allocator, long offset, size_t len);
299
325WS_DLL_PUBLIC
326char *format_text(wmem_allocator_t* allocator, const char *string, size_t len);
327
336WS_DLL_PUBLIC
337char *format_text_string(wmem_allocator_t* allocator, const char *string);
338
350WS_DLL_PUBLIC
351char *format_text_wsp(wmem_allocator_t* allocator, const char *line, size_t len);
352
371WS_DLL_PUBLIC
372char *format_text_chr(wmem_allocator_t *allocator,
373 const char *string, size_t len, char chr);
374
390WS_DLL_PUBLIC
391char *format_char(wmem_allocator_t *allocator, char c);
392
413WS_DLL_PUBLIC
414char* ws_utf8_truncate(char *string, size_t len);
415
416WS_DLL_PUBLIC
417void EBCDIC_to_ASCII(uint8_t *buf, unsigned bytes);
418
419WS_DLL_PUBLIC
420uint8_t EBCDIC_to_ASCII1(uint8_t c);
421
422/* Types of character encodings */
423typedef enum {
424 HEXDUMP_ENC_ASCII = 0, /* ASCII */
425 HEXDUMP_ENC_EBCDIC = 1 /* EBCDIC */
426} hex_dump_enc;
427
428/*
429 * Hexdump options for ASCII:
430 */
431
432#define HEXDUMP_ASCII_MASK (0x0003U)
433#define HEXDUMP_ASCII_OPTION(option) ((option) & HEXDUMP_ASCII_MASK)
434
435#define HEXDUMP_ASCII_INCLUDE (0x0000U) /* include ASCII section no delimiters (legacy tshark behavior) */
436#define HEXDUMP_ASCII_DELIMIT (0x0001U) /* include ASCII section with delimiters, useful for reliable detection of last hexdata */
437#define HEXDUMP_ASCII_EXCLUDE (0x0002U) /* exclude ASCII section from hexdump reports, if we really don't want or need it */
438
439WS_DLL_PUBLIC
440bool hex_dump_buffer(bool (*print_line)(void *, const char *), void *fp,
441 const unsigned char *cp, unsigned length,
442 hex_dump_enc encoding,
443 unsigned ascii_option);
444
445/* To pass one of two strings, singular or plural */
446#define plurality(d,s,p) ((d) == 1 ? (s) : (p))
447
448#define true_or_false(val) ((val) ? "TRUE" : "FALSE")
449
450#define string_or_null(val) ((val) ? (val) : "[NULL]")
451
452#ifdef __cplusplus
453}
454#endif /* __cplusplus */
455
456#endif /* __STR_UTIL_H__ */
WS_DLL_PUBLIC const uint8_t * ws_memrchr(const void *haystack, int ch, size_t n)
Definition str_util.c:347
WS_DLL_PUBLIC char * format_text_string(wmem_allocator_t *allocator, const char *string)
Definition str_util.c:1246
WS_DLL_PUBLIC bool isprint_string(const char *str)
Definition str_util.c:267
WS_DLL_PUBLIC char * ws_utf8_truncate(char *string, size_t len)
Definition str_util.c:1323
WS_DLL_PUBLIC bool isprint_utf8_string(const char *str, const unsigned length)
Definition str_util.c:285
WS_DLL_PUBLIC char * ascii_strup_inplace(char *str)
Definition str_util.c:254
format_size_units_e
Definition str_util.h:231
@ FORMAT_SIZE_UNIT_BITS_S
Definition str_util.h:241
@ FORMAT_SIZE_UNIT_EVENTS
Definition str_util.h:245
@ FORMAT_SIZE_UNIT_BYTES
Definition str_util.h:239
@ FORMAT_SIZE_UNIT_PACKETS
Definition str_util.h:243
@ FORMAT_SIZE_UNIT_BYTES_S
Definition str_util.h:242
@ FORMAT_SIZE_UNIT_SECONDS
Definition str_util.h:249
@ FORMAT_SIZE_UNIT_NONE
Definition str_util.h:232
@ FORMAT_SIZE_UNIT_FIELDS
Definition str_util.h:247
@ FORMAT_SIZE_UNIT_EVENTS_S
Definition str_util.h:246
@ FORMAT_SIZE_UNIT_ERLANGS
Definition str_util.h:250
@ FORMAT_SIZE_UNIT_PACKETS_S
Definition str_util.h:244
@ FORMAT_SIZE_UNIT_BITS
Definition str_util.h:240
WS_DLL_PUBLIC char * ascii_strdown_inplace(char *str)
Definition str_util.c:241
WS_DLL_PUBLIC char * format_text_chr(wmem_allocator_t *allocator, const char *string, size_t len, char chr)
Definition str_util.c:1275
WS_DLL_PUBLIC char * format_text_wsp(wmem_allocator_t *allocator, const char *line, size_t len)
Definition str_util.c:1258
WS_DLL_PUBLIC const char * ws_ascii_strcasestr(const char *haystack, const char *needle)
Definition str_util.c:327
WS_DLL_PUBLIC char ** wmem_strsplit(wmem_allocator_t *allocator, const char *src, const char *delimiter, int max_tokens)
Definition str_util.c:141
WS_DLL_PUBLIC char * format_text(wmem_allocator_t *allocator, const char *string, size_t len)
Definition str_util.c:1221
WS_DLL_PUBLIC char * format_char(wmem_allocator_t *allocator, char c)
Definition str_util.c:1295
WS_DLL_PUBLIC char * format_size_wmem(wmem_allocator_t *allocator, int64_t size, format_size_units_e unit, uint16_t flags)
Definition str_util.c:595
WS_DLL_PUBLIC char * wmem_strjoinv(wmem_allocator_t *allocator, const char *separator, char **str_array) G_GNUC_MALLOC
Definition str_util.c:99
WS_DLL_PUBLIC char * wmem_ascii_strdown(wmem_allocator_t *allocator, const char *str, ssize_t len)
Definition str_util.c:199
WS_DLL_PUBLIC bool isdigit_string(const unsigned char *str)
Definition str_util.c:310
Definition wmem_allocator.h:27