2000-09-28 03:16:29 +00:00
|
|
|
/* strutil.h
|
|
|
|
* String utility definitions
|
2000-09-11 16:16:13 +00:00
|
|
|
*
|
2006-05-21 05:12:17 +00:00
|
|
|
* Wireshark - Network traffic analyzer
|
|
|
|
* By Gerald Combs <gerald@wireshark.org>
|
2000-09-11 16:16:13 +00:00
|
|
|
* Copyright 1998 Gerald Combs
|
2002-08-28 20:41:00 +00:00
|
|
|
*
|
2018-02-08 16:59:17 +00:00
|
|
|
* SPDX-License-Identifier: GPL-2.0-or-later
|
2000-09-11 16:16:13 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef __STRUTIL_H__
|
|
|
|
#define __STRUTIL_H__
|
|
|
|
|
2013-03-01 23:53:11 +00:00
|
|
|
#include "ws_symbol_export.h"
|
|
|
|
|
2017-01-29 19:19:25 +00:00
|
|
|
#include <epan/wmem/wmem.h>
|
|
|
|
|
2011-12-29 00:08:47 +00:00
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif /* __cplusplus */
|
|
|
|
|
2004-06-06 14:29:07 +00:00
|
|
|
/** @file
|
|
|
|
* String handling and conversion utilities.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/** Given a pointer into a data buffer, and to the end of the buffer,
|
|
|
|
* find the end of the (putative) line at that position in the data
|
|
|
|
* buffer.
|
|
|
|
*
|
|
|
|
* @param data A pointer to the beginning of the data
|
|
|
|
* @param dataend A pointer to the end of the data
|
|
|
|
* @param eol A pointer that will receive the EOL location
|
|
|
|
* @return A pointer to the EOL character(s) in "*eol".
|
|
|
|
*/
|
2002-08-02 21:29:45 +00:00
|
|
|
const guchar *find_line_end(const guchar *data, const guchar *dataend,
|
|
|
|
const guchar **eol);
|
2004-06-06 14:29:07 +00:00
|
|
|
|
|
|
|
/** Get the length of the next token in a line, and the beginning of the
|
|
|
|
* next token after that (if any).
|
|
|
|
* @param linep A pointer to the beginning of the line
|
|
|
|
* @param lineend A pointer to the end of the line
|
|
|
|
* @param next_token Receives the location of the next token
|
|
|
|
* @return 0 if there is no next token.
|
|
|
|
*/
|
2013-05-03 23:37:06 +00:00
|
|
|
WS_DLL_PUBLIC
|
2002-08-02 21:29:45 +00:00
|
|
|
int get_token_len(const guchar *linep, const guchar *lineend,
|
|
|
|
const guchar **next_token);
|
2004-06-06 14:29:07 +00:00
|
|
|
|
2017-01-30 02:53:49 +00:00
|
|
|
/** Given a string, generate a string from it that shows non-printable
|
|
|
|
* characters as C-style escapes, and return a pointer to it.
|
|
|
|
*
|
|
|
|
* @param allocator The wmem scope
|
|
|
|
* @param line A pointer to the input string
|
|
|
|
* @param len The length of the input string
|
|
|
|
* @return A pointer to the formatted string
|
|
|
|
*
|
|
|
|
* @see tvb_format_text()
|
|
|
|
*/
|
|
|
|
WS_DLL_PUBLIC
|
2017-01-31 12:51:19 +00:00
|
|
|
gchar* format_text(wmem_allocator_t* allocator, const guchar *line, size_t len);
|
2017-01-30 02:53:49 +00:00
|
|
|
|
2010-08-29 10:47:38 +00:00
|
|
|
/**
|
|
|
|
* Given a string, generate a string from it that shows non-printable
|
|
|
|
* characters as C-style escapes except a whitespace character
|
|
|
|
* (space, tab, carriage return, new line, vertical tab, or formfeed)
|
|
|
|
* which will be replaced by a space, and return a pointer to it.
|
|
|
|
*
|
2017-01-29 23:51:00 +00:00
|
|
|
* @param allocator The wmem scope
|
2010-08-29 10:47:38 +00:00
|
|
|
* @param line A pointer to the input string
|
|
|
|
* @param len The length of the input string
|
|
|
|
* @return A pointer to the formatted string
|
|
|
|
*
|
|
|
|
*/
|
2013-05-03 23:37:06 +00:00
|
|
|
WS_DLL_PUBLIC
|
2017-01-29 23:51:00 +00:00
|
|
|
gchar* format_text_wsp(wmem_allocator_t* allocator, const guchar *line, size_t len);
|
2006-06-19 15:53:03 +00:00
|
|
|
|
2013-11-12 09:23:13 +00:00
|
|
|
/**
|
2013-10-12 12:38:56 +00:00
|
|
|
* Given a string, generate a string from it that shows non-printable
|
|
|
|
* characters as the chr parameter passed, except a whitespace character
|
|
|
|
* (space, tab, carriage return, new line, vertical tab, or formfeed)
|
|
|
|
* which will be replaced by a space, and return a pointer to it.
|
|
|
|
*
|
2017-01-29 19:19:25 +00:00
|
|
|
* @param allocator The wmem scope
|
2013-12-02 13:46:20 +00:00
|
|
|
* @param string A pointer to the input string
|
2013-10-12 12:38:56 +00:00
|
|
|
* @param len The length of the input string
|
|
|
|
* @param chr The character to use to replace non-printable characters
|
|
|
|
* @return A pointer to the formatted string
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
WS_DLL_PUBLIC
|
2017-01-29 19:19:25 +00:00
|
|
|
gchar* format_text_chr(wmem_allocator_t* allocator, const guchar *string, const size_t len, const guchar chr);
|
2013-10-12 12:38:56 +00:00
|
|
|
|
|
|
|
|
2004-06-06 14:29:07 +00:00
|
|
|
/** Turn a string of hex digits with optional separators (defined by
|
|
|
|
* is_byte_sep() into a byte array.
|
|
|
|
*
|
|
|
|
* @param hex_str The string of hex digits.
|
|
|
|
* @param bytes The GByteArray that will receive the bytes. This
|
|
|
|
* must be initialized by the caller.
|
2006-09-22 21:14:54 +00:00
|
|
|
* @param force_separators If set to TRUE, separators MUST exist between
|
2004-06-06 14:29:07 +00:00
|
|
|
* bytes.
|
|
|
|
* @return True if the string was converted successfully
|
|
|
|
*/
|
2013-03-01 23:53:11 +00:00
|
|
|
WS_DLL_PUBLIC
|
2004-06-06 14:29:07 +00:00
|
|
|
gboolean hex_str_to_bytes(const char *hex_str, GByteArray *bytes,
|
|
|
|
gboolean force_separators);
|
|
|
|
|
2014-04-13 03:20:15 +00:00
|
|
|
/* Turn a string of hex digits with optional separators (defined by encoding)
|
|
|
|
* into a byte array. Unlike hex_str_to_bytes(), this will read as many hex-char
|
|
|
|
* pairs as possible and not error if it hits a non-hex-char; instead it just ends
|
|
|
|
* there. (i.e., like strtol()/atoi()/etc.) But it must see two hex chars at the
|
|
|
|
* beginning or it will return FALSE.
|
|
|
|
*
|
|
|
|
* @param hex_str The string of hex digits.
|
|
|
|
* @param bytes The GByteArray that will receive the bytes. This
|
|
|
|
* must be initialized by the caller.
|
|
|
|
* @param endptr if not NULL, is set to the char after the last hex character consumed.
|
|
|
|
* @param encoding set to one or more bitwise-or'ed ENC_SEP_* (see proto.h)
|
|
|
|
* @param fail_if_partial If set to TRUE, then the conversion fails if the whole
|
|
|
|
* hex_str is not consumed.
|
|
|
|
* @return FALSE only if no bytes were generated; or if fail_if_partial is TRUE
|
|
|
|
* and the entire hex_str was not consumed.
|
|
|
|
*
|
|
|
|
* If no ENC_SEP_* is set, then no separators are allowed. If multiple ENC_SEP_* are
|
|
|
|
* bit-or'ed, any of them can be a separator, but once the separator is seen then
|
|
|
|
* only its same type is accepted for the rest of the string. (i.e., it won't convert
|
|
|
|
* a "01:23-4567" even if ENC_SEP_COLON|ENC_SEP_DASH|ENC_SEP_NONE is passed in)
|
|
|
|
*
|
|
|
|
* This is done this way because it's likely a malformed scenario if they're mixed,
|
|
|
|
* and this routine is used by dissectors via tvb_get_string_XXX routines.
|
|
|
|
*/
|
|
|
|
WS_DLL_PUBLIC
|
|
|
|
gboolean hex_str_to_bytes_encoding(const char *hex_str, GByteArray *bytes, const char **endptr,
|
|
|
|
const guint encoding, const gboolean fail_if_partial);
|
|
|
|
|
2007-01-11 02:42:34 +00:00
|
|
|
/** Turn an RFC 3986 percent-encoded string into a byte array.
|
|
|
|
*
|
|
|
|
* @param uri_str The string of hex digits.
|
|
|
|
* @param bytes The GByteArray that will receive the bytes. This
|
|
|
|
* must be initialized by the caller.
|
|
|
|
* @return True if the string was converted successfully
|
2007-01-11 22:12:33 +00:00
|
|
|
* @see format_uri()
|
2007-01-11 02:42:34 +00:00
|
|
|
*/
|
2013-03-01 23:53:11 +00:00
|
|
|
WS_DLL_PUBLIC
|
2007-01-11 02:42:34 +00:00
|
|
|
gboolean uri_str_to_bytes(const char *uri_str, GByteArray *bytes);
|
|
|
|
|
2007-01-11 22:12:33 +00:00
|
|
|
/** Turn a byte array into an RFC 3986 percent-encoded string.
|
|
|
|
*
|
2017-01-31 13:29:53 +00:00
|
|
|
* @param allocator The wmem scope
|
2007-01-11 22:12:33 +00:00
|
|
|
* @param bytes The GByteArray that will receive the bytes. This
|
|
|
|
* must be initialized by the caller.
|
|
|
|
* @param reserved_chars Normally the "gen-delims" and "sub-delims"
|
|
|
|
* from RFC 3986 (":/?#[]@" and "!$&'()*+,;=" respectively)
|
|
|
|
* plus space (hex value 20) are treated as reserved characters.
|
|
|
|
* If this variable is non-NULL, its contents will be used
|
|
|
|
* instead.
|
|
|
|
* @note Any non-printing character determined by isprint(), along
|
|
|
|
* with the % character itself are always reserved.
|
|
|
|
* @see uri_str_to_bytes(), format_text(), isprint()
|
|
|
|
*/
|
2013-03-01 23:53:11 +00:00
|
|
|
WS_DLL_PUBLIC
|
2017-01-31 13:29:53 +00:00
|
|
|
gchar* format_uri(wmem_allocator_t* allocator, const GByteArray *bytes, const gchar *reserved_chars);
|
2007-01-11 22:12:33 +00:00
|
|
|
|
2013-10-06 02:31:10 +00:00
|
|
|
/** Turn a OID string representation (dot notation) into a byte array.
|
|
|
|
*
|
|
|
|
* @param oid_str The OID string (dot notaion).
|
|
|
|
* @param bytes The GByteArray that will receive the bytes. This
|
|
|
|
* must be initialized by the caller.
|
|
|
|
* @param is_absolute True if this is an absolute OID; false for relative OID.
|
|
|
|
* @return True if the string was converted successfully
|
|
|
|
*/
|
|
|
|
WS_DLL_PUBLIC
|
|
|
|
gboolean rel_oid_str_to_bytes(const char *oid_str, GByteArray *bytes, gboolean is_absolute);
|
|
|
|
|
2007-01-11 22:12:33 +00:00
|
|
|
/** Turn a OID string representation (dot notation) into a byte array.
|
2005-12-02 13:16:58 +00:00
|
|
|
*
|
|
|
|
* @param oid_str The OID string (dot notaion).
|
|
|
|
* @param bytes The GByteArray that will receive the bytes. This
|
|
|
|
* must be initialized by the caller.
|
|
|
|
* @return True if the string was converted successfully
|
|
|
|
*/
|
2013-05-03 23:37:06 +00:00
|
|
|
WS_DLL_PUBLIC
|
2005-12-02 13:16:58 +00:00
|
|
|
gboolean oid_str_to_bytes(const char *oid_str, GByteArray *bytes);
|
|
|
|
|
2007-01-11 02:42:34 +00:00
|
|
|
/**
|
|
|
|
* Create a copy of a GByteArray
|
|
|
|
*
|
|
|
|
* @param ba The byte array to be copied.
|
|
|
|
* @return If ba exists, a freshly allocated copy. NULL otherwise.
|
|
|
|
*
|
2010-08-29 10:47:38 +00:00
|
|
|
* @todo - Should this be in strutil.c?
|
2007-01-11 02:42:34 +00:00
|
|
|
*/
|
2013-03-01 23:53:11 +00:00
|
|
|
WS_DLL_PUBLIC
|
2014-04-13 03:20:15 +00:00
|
|
|
GByteArray *byte_array_dup(const GByteArray *ba);
|
2007-01-11 02:42:34 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Compare the contents of two GByteArrays
|
|
|
|
*
|
|
|
|
* @param ba1 A byte array
|
|
|
|
* @param ba2 A byte array
|
|
|
|
* @return If both arrays are non-NULL and their lengths are equal and
|
|
|
|
* their contents are equal, returns TRUE. Otherwise, returns
|
|
|
|
* FALSE.
|
|
|
|
*
|
2010-08-29 10:47:38 +00:00
|
|
|
* @todo - Should this be in strutil.c?
|
2007-01-11 02:42:34 +00:00
|
|
|
*/
|
2013-03-01 23:53:11 +00:00
|
|
|
WS_DLL_PUBLIC
|
2007-01-11 02:42:34 +00:00
|
|
|
gboolean byte_array_equal(GByteArray *ba1, GByteArray *ba2);
|
|
|
|
|
|
|
|
|
2004-06-06 14:29:07 +00:00
|
|
|
/** Return a XML escaped representation of the unescaped string.
|
2006-09-22 21:14:54 +00:00
|
|
|
* The returned string must be freed when no longer in use.
|
2004-06-06 14:29:07 +00:00
|
|
|
*
|
|
|
|
* @param unescaped The unescaped string
|
|
|
|
* @return An XML-escaped representation of the input string
|
|
|
|
*/
|
2013-03-01 23:53:11 +00:00
|
|
|
WS_DLL_PUBLIC
|
2004-05-01 20:46:24 +00:00
|
|
|
gchar* xml_escape(const gchar *unescaped);
|
2003-07-04 03:41:00 +00:00
|
|
|
|
2007-01-11 02:42:34 +00:00
|
|
|
/**
|
|
|
|
* Return the first occurrence of needle in haystack.
|
2004-06-06 14:29:07 +00:00
|
|
|
* Algorithm copied from GNU's glibc 2.3.2 memcmp()
|
|
|
|
*
|
|
|
|
* @param haystack The data to search
|
|
|
|
* @param haystack_len The length of the search data
|
|
|
|
* @param needle The string to look for
|
|
|
|
* @param needle_len The length of the search string
|
|
|
|
* @return A pointer to the first occurrence of "needle" in
|
|
|
|
* "haystack". If "needle" isn't found or is NULL, or if
|
|
|
|
* "needle_len" is 0, NULL is returned.
|
|
|
|
*/
|
2013-05-03 23:37:06 +00:00
|
|
|
WS_DLL_PUBLIC
|
2003-08-27 15:23:11 +00:00
|
|
|
const guint8 * epan_memmem(const guint8 *haystack, guint haystack_len,
|
|
|
|
const guint8 *needle, guint needle_len);
|
|
|
|
|
2004-08-13 02:39:49 +00:00
|
|
|
/** Scan a string to make sure it's valid hex.
|
|
|
|
*
|
|
|
|
* @param string The string to validate
|
|
|
|
* @param nbytes The length of the return buffer
|
|
|
|
* @return A pointer to a buffer containing the converted raw bytes. This
|
|
|
|
* buffer must be g_free()d by the caller.
|
|
|
|
*/
|
2013-03-01 23:53:11 +00:00
|
|
|
WS_DLL_PUBLIC
|
2004-08-13 02:39:49 +00:00
|
|
|
guint8 * convert_string_to_hex(const char *string, size_t *nbytes);
|
|
|
|
|
|
|
|
/** Prep a string for case-sensitive vs case-insensitive searching.
|
|
|
|
*
|
|
|
|
* @param string The search string
|
2004-10-27 04:45:00 +00:00
|
|
|
* @param case_insensitive TRUE if case-insensitive, FALSE if not
|
2004-08-13 02:39:49 +00:00
|
|
|
* @return A direct copy of the string if it's a case-sensitive search and
|
|
|
|
* an uppercased version if not. In either case the string must be g_free()d
|
|
|
|
* by the caller.
|
|
|
|
*/
|
2013-03-01 23:53:11 +00:00
|
|
|
WS_DLL_PUBLIC
|
2004-08-13 02:39:49 +00:00
|
|
|
char * convert_string_case(const char *string, gboolean case_insensitive);
|
|
|
|
|
2013-02-26 04:42:26 +00:00
|
|
|
/** Finds the first occurrence of string 'needle' in string 'haystack'.
|
2007-02-07 13:45:28 +00:00
|
|
|
* The matching is done in a case insensitive manner.
|
|
|
|
*
|
|
|
|
* @param haystack The string possibly containing the substring
|
|
|
|
* @param needle The substring to be searched
|
|
|
|
* @return A pointer into 'haystack' where 'needle' is first found.
|
|
|
|
* Otherwise it returns NULL.
|
|
|
|
*/
|
2013-03-01 23:53:11 +00:00
|
|
|
WS_DLL_PUBLIC
|
2016-01-20 01:52:17 +00:00
|
|
|
const char * epan_strcasestr(const char *haystack, const char *needle);
|
2007-02-07 13:45:28 +00:00
|
|
|
|
2009-02-17 23:15:35 +00:00
|
|
|
/** Guarantee a non-null string.
|
|
|
|
*
|
|
|
|
* @param string The string to check
|
|
|
|
* @return A pointer 'string' if it's non-null, otherwise "[NULL]".
|
|
|
|
*/
|
2013-03-01 23:53:11 +00:00
|
|
|
WS_DLL_PUBLIC
|
2009-02-17 23:15:35 +00:00
|
|
|
const char * string_or_null(const char *string);
|
|
|
|
|
2013-03-01 23:53:11 +00:00
|
|
|
WS_DLL_PUBLIC
|
2009-08-19 18:37:13 +00:00
|
|
|
int escape_string_len(const char *string);
|
2013-03-01 23:53:11 +00:00
|
|
|
WS_DLL_PUBLIC
|
2009-08-19 18:37:13 +00:00
|
|
|
char * escape_string(char *dst, const char *string);
|
|
|
|
|
2009-10-23 01:56:09 +00:00
|
|
|
|
2013-05-03 23:37:06 +00:00
|
|
|
WS_DLL_PUBLIC
|
2009-10-23 01:56:09 +00:00
|
|
|
void IA5_7BIT_decode(unsigned char * dest, const unsigned char* src, int len);
|
|
|
|
|
2011-04-13 16:56:24 +00:00
|
|
|
/** Copy a string, escaping the 'chr' characters in it
|
2010-01-26 18:21:17 +00:00
|
|
|
*
|
|
|
|
* @param str The string to be copied
|
2013-04-26 18:28:21 +00:00
|
|
|
* @param chr The character to be escaped
|
2011-04-13 16:56:24 +00:00
|
|
|
* @return A copy of the string with every original 'chr' being
|
|
|
|
* transformed into double 'chr'.
|
2010-01-26 18:21:17 +00:00
|
|
|
*/
|
2013-03-01 23:53:11 +00:00
|
|
|
WS_DLL_PUBLIC
|
2011-04-13 16:56:24 +00:00
|
|
|
gchar* ws_strdup_escape_char (const gchar *str, const gchar chr);
|
2010-01-26 18:21:17 +00:00
|
|
|
|
2011-04-13 16:56:24 +00:00
|
|
|
/** Copy a string, unescaping the 'chr' characters in it
|
2010-01-26 18:21:17 +00:00
|
|
|
*
|
|
|
|
* @param str The string to be copied
|
2013-04-26 18:28:21 +00:00
|
|
|
* @param chr The character to be escaped
|
2013-02-26 04:42:26 +00:00
|
|
|
* @return A copy of the string with every occurrence of double 'chr' in
|
2011-04-13 16:56:24 +00:00
|
|
|
* the original string being copied as a single 'chr'.
|
2010-01-26 18:21:17 +00:00
|
|
|
*/
|
2013-03-01 23:53:11 +00:00
|
|
|
WS_DLL_PUBLIC
|
2011-04-13 16:56:24 +00:00
|
|
|
gchar* ws_strdup_unescape_char (const gchar *str, const gchar chr);
|
2010-01-26 18:21:17 +00:00
|
|
|
|
2010-05-27 15:51:25 +00:00
|
|
|
/** Replace values in a string
|
|
|
|
*
|
|
|
|
* @param str String containing 0 or more values to be replaced.
|
|
|
|
* @param old_val Old value.
|
|
|
|
* @param new_val New value. May be NULL, in which case occurences of
|
|
|
|
* old_value will be removed.
|
|
|
|
* @return A newly-allocated version of str with replacement values or
|
|
|
|
* NULL on failure.
|
|
|
|
*/
|
2013-03-01 23:53:11 +00:00
|
|
|
WS_DLL_PUBLIC
|
2010-05-27 15:51:25 +00:00
|
|
|
gchar *string_replace(const gchar* str, const gchar *old_val, const gchar *new_val);
|
|
|
|
|
2011-12-29 00:08:47 +00:00
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif /* __cplusplus */
|
|
|
|
|
2000-09-11 16:16:13 +00:00
|
|
|
#endif /* __STRUTIL_H__ */
|