Wireshark  4.3.0
The Wireshark network protocol analyzer
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
18 extern "C" {
19 #endif /* __cplusplus */
20 
21 WS_DLL_PUBLIC
22 char *
23 wmem_strconcat(wmem_allocator_t *allocator, const char *first, ...)
24 G_GNUC_MALLOC G_GNUC_NULL_TERMINATED;
25 
26 WS_DLL_PUBLIC
27 char *
28 wmem_strjoin(wmem_allocator_t *allocator,
29  const char *separator, const char *first, ...)
30 G_GNUC_MALLOC G_GNUC_NULL_TERMINATED;
31 
45 WS_DLL_PUBLIC
46 char *
48  const char *separator, char **str_array)
49 G_GNUC_MALLOC;
50 
62 WS_DLL_PUBLIC
63 char **
64 wmem_strsplit(wmem_allocator_t *allocator, const char *src,
65  const char *delimiter, int max_tokens);
66 
82 WS_DLL_PUBLIC
83 char*
84 wmem_ascii_strdown(wmem_allocator_t *allocator, const char *str, ssize_t len);
85 
101 WS_DLL_PUBLIC
102 char *ascii_strdown_inplace(char *str);
103 
119 WS_DLL_PUBLIC
120 char *ascii_strup_inplace(char *str);
121 
127 WS_DLL_PUBLIC
128 bool isprint_string(const char *str);
129 
156 WS_DLL_PUBLIC
157 bool isprint_utf8_string(const char *str, const unsigned length);
158 
164 WS_DLL_PUBLIC
165 bool isdigit_string(const unsigned char *str);
166 
182 WS_DLL_PUBLIC
183 const char *ws_ascii_strcasestr(const char *haystack, const char *needle);
184 
193 WS_DLL_PUBLIC
194 const uint8_t *ws_memrchr(const void *haystack, int ch, size_t n);
195 
196 WS_DLL_PUBLIC
197 char *ws_escape_string(wmem_allocator_t *alloc, const char *string, bool add_quotes);
198 
199 WS_DLL_PUBLIC
200 char *ws_escape_string_len(wmem_allocator_t *alloc, const char *string, ssize_t len, bool add_quotes);
201 
202 /* Replace null bytes with "\0". */
203 WS_DLL_PUBLIC
204 char *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  */
225 WS_DLL_PUBLIC
226 char *ws_escape_csv(wmem_allocator_t *alloc, const char *string, bool add_quotes, char quote_char, bool double_quote, bool escape_whitespace);
227 
228 WS_DLL_PUBLIC
229 int ws_xton(char ch);
230 
231 typedef 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  */
246  /* These next two aren't really for format_size (which takes an int) */
250 
251 #define FORMAT_SIZE_PREFIX_SI (1 << 0)
252 #define FORMAT_SIZE_PREFIX_IEC (1 << 1)
268 WS_DLL_PUBLIC
269 char *format_units(wmem_allocator_t *allocator, double size,
270  format_size_units_e unit, uint16_t flags,
271  int precision);
272 
282 WS_DLL_PUBLIC
283 char *format_size_wmem(wmem_allocator_t *allocator, int64_t size,
284  format_size_units_e unit, uint16_t flags);
285 
286 #define format_size(size, unit, flags) \
287  format_size_wmem(NULL, size, unit, flags)
288 
289 WS_DLL_PUBLIC
290 char printable_char_or_period(char c);
291 
292 WS_DLL_PUBLIC WS_RETNONNULL
293 const char *ws_strerrorname_r(int errnum, char *buf, size_t buf_size);
294 
295 WS_DLL_PUBLIC
296 char *ws_strdup_underline(wmem_allocator_t *allocator, long offset, size_t len);
297 
323 WS_DLL_PUBLIC
324 char *format_text(wmem_allocator_t* allocator, const char *string, size_t len);
325 
334 WS_DLL_PUBLIC
335 char *format_text_string(wmem_allocator_t* allocator, const char *string);
336 
348 WS_DLL_PUBLIC
349 char *format_text_wsp(wmem_allocator_t* allocator, const char *line, size_t len);
350 
369 WS_DLL_PUBLIC
370 char *format_text_chr(wmem_allocator_t *allocator,
371  const char *string, size_t len, char chr);
372 
388 WS_DLL_PUBLIC
389 char *format_char(wmem_allocator_t *allocator, char c);
390 
411 WS_DLL_PUBLIC
412 char* ws_utf8_truncate(char *string, size_t len);
413 
414 WS_DLL_PUBLIC
415 void EBCDIC_to_ASCII(uint8_t *buf, unsigned bytes);
416 
417 WS_DLL_PUBLIC
418 uint8_t EBCDIC_to_ASCII1(uint8_t c);
419 
420 /* Types of character encodings */
421 typedef enum {
422  HEXDUMP_ENC_ASCII = 0, /* ASCII */
423  HEXDUMP_ENC_EBCDIC = 1 /* EBCDIC */
424 } hex_dump_enc;
425 
426 /*
427  * Hexdump options for ASCII:
428  */
429 
430 #define HEXDUMP_ASCII_MASK (0x0003U)
431 #define HEXDUMP_ASCII_OPTION(option) ((option) & HEXDUMP_ASCII_MASK)
432 
433 #define HEXDUMP_ASCII_INCLUDE (0x0000U) /* include ASCII section no delimiters (legacy tshark behavior) */
434 #define HEXDUMP_ASCII_DELIMIT (0x0001U) /* include ASCII section with delimiters, useful for reliable detection of last hexdata */
435 #define HEXDUMP_ASCII_EXCLUDE (0x0002U) /* exclude ASCII section from hexdump reports, if we really don't want or need it */
436 
437 WS_DLL_PUBLIC
438 bool hex_dump_buffer(bool (*print_line)(void *, const char *), void *fp,
439  const unsigned char *cp, unsigned length,
440  hex_dump_enc encoding,
441  unsigned ascii_option);
442 
443 /* To pass one of two strings, singular or plural */
444 #define plurality(d,s,p) ((d) == 1 ? (s) : (p))
445 
446 #define true_or_false(val) ((val) ? "TRUE" : "FALSE")
447 
448 #define string_or_null(val) ((val) ? (val) : "[NULL]")
449 
450 #ifdef __cplusplus
451 }
452 #endif /* __cplusplus */
453 
454 #endif /* __STR_UTIL_H__ */
WS_DLL_PUBLIC char * ascii_strup_inplace(char *str)
Definition: str_util.c:254
WS_DLL_PUBLIC const char * ws_ascii_strcasestr(const char *haystack, const char *needle)
Definition: str_util.c:327
WS_DLL_PUBLIC bool isprint_string(const char *str)
Definition: str_util.c:267
WS_DLL_PUBLIC char * format_text_chr(wmem_allocator_t *allocator, const char *string, size_t len, char chr)
Definition: str_util.c:1269
WS_DLL_PUBLIC bool isprint_utf8_string(const char *str, const unsigned length)
Definition: str_util.c:285
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 char * format_char(wmem_allocator_t *allocator, char c)
Definition: str_util.c:1289
WS_DLL_PUBLIC char * format_units(wmem_allocator_t *allocator, double size, format_size_units_e unit, uint16_t flags, int precision)
Definition: str_util.c:436
format_size_units_e
Definition: str_util.h:231
@ FORMAT_SIZE_UNIT_BITS_S
Definition: str_util.h:241
@ 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:247
@ FORMAT_SIZE_UNIT_NONE
Definition: str_util.h:232
@ FORMAT_SIZE_UNIT_FIELDS
Definition: str_util.h:245
@ FORMAT_SIZE_UNIT_ERLANGS
Definition: str_util.h:248
@ FORMAT_SIZE_UNIT_PACKETS_S
Definition: str_util.h:244
@ FORMAT_SIZE_UNIT_BITS
Definition: str_util.h:240
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 * format_text_string(wmem_allocator_t *allocator, const char *string)
Definition: str_util.c:1240
WS_DLL_PUBLIC char * format_text_wsp(wmem_allocator_t *allocator, const char *line, size_t len)
Definition: str_util.c:1252
WS_DLL_PUBLIC char * ascii_strdown_inplace(char *str)
Definition: str_util.c:241
WS_DLL_PUBLIC char * format_text(wmem_allocator_t *allocator, const char *string, size_t len)
Definition: str_util.c:1215
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 * ws_utf8_truncate(char *string, size_t len)
Definition: str_util.c:1317
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 bool isdigit_string(const unsigned char *str)
Definition: str_util.c:310
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:589
Definition: wmem_allocator.h:27