pytest.ini: declare minimum version requirement
[metze/wireshark/wip.git] / epan / strutil.h
index f11972e19a7d886ea0e6166f697fd697f2f72c97..2046cb0506715f522cd930bd0c113edba0e2bd67 100644 (file)
@@ -1,31 +1,23 @@
 /* strutil.h
  * String utility definitions
  *
- * $Id$
- *
  * Wireshark - Network traffic analyzer
  * By Gerald Combs <gerald@wireshark.org>
  * Copyright 1998 Gerald Combs
  *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License
- * as published by the Free Software Foundation; either version 2
- * of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+ * SPDX-License-Identifier: GPL-2.0-or-later
  */
 
 #ifndef __STRUTIL_H__
 #define __STRUTIL_H__
 
-/* ... thus, config.h needs to be #included */
+#include "ws_symbol_export.h"
+
+#include <epan/wmem/wmem.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif /* __cplusplus */
 
 /** @file
  * String handling and conversion utilities.
@@ -50,43 +42,54 @@ const guchar *find_line_end(const guchar *data, const guchar *dataend,
  * @param next_token Receives the location of the next token
  * @return 0 if there is no next token.
  */
+WS_DLL_PUBLIC
 int        get_token_len(const guchar *linep, const guchar *lineend,
     const guchar **next_token);
 
 /** 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()
  */
-gchar*     format_text(const guchar *line, int len);
-
-gchar*     format_text_wsp(const guchar *line, int len);
+WS_DLL_PUBLIC
+gchar*     format_text(wmem_allocator_t* allocator, const guchar *line, size_t len);
 
-/** Turn an array of bytes into a string showing the bytes in hex.
+/**
+ * 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.
  *
- * @param bd A pointer to the byte array
- * @param bd_len The length of the byte array
+ * @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 bytes_to_str_punct()
  */
-gchar*     bytes_to_str(const guint8 *bd, int bd_len);
+WS_DLL_PUBLIC
+gchar*     format_text_wsp(wmem_allocator_t* allocator, const guchar *line, size_t len);
 
-/** Turn an array of bytes into a string showing the bytes in hex,
- *  separated by a punctuation character.
+/**
+ * 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.
  *
- * @param bd A pointer to the byte array
- * @param bd_len The length of the byte array
- * @param punct The punctuation character
+ * @param allocator The wmem scope
+ * @param string A pointer to the input string
+ * @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
  *
- * @see bytes_to_str()
  */
-gchar*     bytes_to_str_punct(const guint8 *bd, int bd_len, gchar punct);
+WS_DLL_PUBLIC
+gchar*     format_text_chr(wmem_allocator_t* allocator, const guchar *string, const size_t len, const guchar chr);
+
 
 /** Turn a string of hex digits with optional separators (defined by
  *  is_byte_sep() into a byte array.
@@ -98,9 +101,38 @@ gchar*     bytes_to_str_punct(const guint8 *bd, int bd_len, gchar punct);
  *        bytes.
  * @return True if the string was converted successfully
  */
+WS_DLL_PUBLIC
 gboolean   hex_str_to_bytes(const char *hex_str, GByteArray *bytes,
     gboolean force_separators);
 
+/* 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);
+
 /** Turn an RFC 3986 percent-encoded string into a byte array.
  *
  * @param uri_str The string of hex digits.
@@ -109,10 +141,12 @@ gboolean   hex_str_to_bytes(const char *hex_str, GByteArray *bytes,
  * @return True if the string was converted successfully
  * @see format_uri()
  */
+WS_DLL_PUBLIC
 gboolean   uri_str_to_bytes(const char *uri_str, GByteArray *bytes);
 
 /** Turn a byte array into an RFC 3986 percent-encoded string.
  *
+ * @param allocator The wmem scope
  * @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"
@@ -124,15 +158,28 @@ gboolean   uri_str_to_bytes(const char *uri_str, GByteArray *bytes);
  *       with the % character itself are always reserved.
  * @see uri_str_to_bytes(),  format_text(), isprint()
  */
-gchar*     format_uri(const GByteArray *bytes, const gchar *reserved_chars);
+WS_DLL_PUBLIC
+gchar* format_uri(wmem_allocator_t* allocator, const GByteArray *bytes, const gchar *reserved_chars);
 
 /** 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);
+
+/** 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.
+ * @return True if the string was converted successfully
+ */
+WS_DLL_PUBLIC
 gboolean   oid_str_to_bytes(const char *oid_str, GByteArray *bytes);
 
 /**
@@ -141,9 +188,10 @@ gboolean   oid_str_to_bytes(const char *oid_str, GByteArray *bytes);
  * @param ba The byte array to be copied.
  * @return If ba exists, a freshly allocated copy.  NULL otherwise.
  *
- * XXX - Should this be in strutil.c?
+ * @todo - Should this be in strutil.c?
  */
-GByteArray *byte_array_dup(GByteArray *ba);
+WS_DLL_PUBLIC
+GByteArray *byte_array_dup(const GByteArray *ba);
 
 /**
  * Compare the contents of two GByteArrays
@@ -154,8 +202,9 @@ GByteArray *byte_array_dup(GByteArray *ba);
  *         their contents are equal, returns TRUE.  Otherwise, returns
  *         FALSE.
  *
- * XXX - Should this be in strutil.c?
+ * @todo - Should this be in strutil.c?
  */
+WS_DLL_PUBLIC
 gboolean byte_array_equal(GByteArray *ba1, GByteArray *ba2);
 
 
@@ -165,6 +214,7 @@ gboolean byte_array_equal(GByteArray *ba1, GByteArray *ba2);
  * @param unescaped The unescaped string
  * @return An XML-escaped representation of the input string
  */
+WS_DLL_PUBLIC
 gchar*     xml_escape(const gchar *unescaped);
 
 /**
@@ -179,13 +229,10 @@ gchar*     xml_escape(const gchar *unescaped);
  *         "haystack".  If "needle" isn't found or is NULL, or if
  *         "needle_len" is 0, NULL is returned.
  */
+WS_DLL_PUBLIC
 const guint8 * epan_memmem(const guint8 *haystack, guint haystack_len,
                const guint8 *needle, guint needle_len);
 
-/* Surround a string or a macro, resolved to a string, with double quotes */
-#define _STRINGIFY(a)           # a
-#define STRINGIFY(a)            _STRINGIFY(a)
-
 /** Scan a string to make sure it's valid hex.
  *
  * @param string The string to validate
@@ -193,6 +240,7 @@ const guint8 * epan_memmem(const guint8 *haystack, guint haystack_len,
  * @return A pointer to a buffer containing the converted raw bytes.  This
  *         buffer must be g_free()d by the caller.
  */
+WS_DLL_PUBLIC
 guint8 * convert_string_to_hex(const char *string, size_t *nbytes);
 
 /** Prep a string for case-sensitive vs case-insensitive searching.
@@ -203,9 +251,10 @@ guint8 * convert_string_to_hex(const char *string, size_t *nbytes);
  * an uppercased version if not.  In either case the string must be g_free()d
  * by the caller.
  */
+WS_DLL_PUBLIC
 char * convert_string_case(const char *string, gboolean case_insensitive);
 
-/** Finds the first occurence of string 'needle' in string 'haystack'.
+/** Finds the first occurrence of string 'needle' in string 'haystack'.
  *  The matching is done in a case insensitive manner.
  *
  * @param haystack The string possibly containing the substring
@@ -213,26 +262,60 @@ char * convert_string_case(const char *string, gboolean case_insensitive);
  * @return A pointer into 'haystack' where 'needle' is first found.
  *   Otherwise it returns NULL.
  */
-char * epan_strcasestr(const char *haystack, const char *needle);
+WS_DLL_PUBLIC
+const char * epan_strcasestr(const char *haystack, const char *needle);
+
+/** Guarantee a non-null string.
+ *
+ * @param string The string to check
+ * @return A pointer 'string' if it's non-null, otherwise "[NULL]".
+ */
+WS_DLL_PUBLIC
+const char * string_or_null(const char *string);
+
+WS_DLL_PUBLIC
+int escape_string_len(const char *string);
+WS_DLL_PUBLIC
+char * escape_string(char *dst, const char *string);
+
+
+WS_DLL_PUBLIC
+void IA5_7BIT_decode(unsigned char * dest, const unsigned char* src, int len);
+
+/** Copy a string, escaping the 'chr' characters in it
+ *
+ * @param str The string to be copied
+ * @param chr The character to be escaped
+ * @return A copy of the string with every original 'chr' being
+ * transformed into double 'chr'.
+ */
+WS_DLL_PUBLIC
+gchar* ws_strdup_escape_char (const gchar *str, const gchar chr);
 
-/* g_strlcat() does not exist in GLib 1.2[.x] */
-#if GLIB_MAJOR_VERSION < 2
-gsize g_strlcat(gchar *dst, const gchar *src, gsize size);
-gsize g_strlcpy(gchar *dest, const gchar *src, gsize dest_size);
-#endif
+/** Copy a string, unescaping the 'chr' characters in it
+ *
+ * @param str The string to be copied
+ * @param chr The character to be escaped
+ * @return A copy of the string with every occurrence of double 'chr' in
+ * the original string being copied as a single 'chr'.
+ */
+WS_DLL_PUBLIC
+gchar* ws_strdup_unescape_char (const gchar *str, const gchar chr);
 
-#if GLIB_MAJOR_VERSION < 2
-/* g_ascii_isprint() does not exist in GLib 1.2[.x].
- * assume all codes >=0x20 and <0x80 are ASCII printables.
+/** 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.
  */
-#define g_ascii_isprint(c)                                             \
-       (((c<0x20)||(c>=0x80))?FALSE:TRUE)
-/* g_ascii_isxdigit() does not exist in Glib 1.2 */
-#define g_ascii_isxdigit(c)                                            \
-       ( ((c>='0')&&(c<='9'))?TRUE:                                    \
-               ( ((c>='a')&&(c<='f'))?TRUE:                            \
-                       (((c>='A')&&(c<='F'))?TRUE:FALSE) ) )
+WS_DLL_PUBLIC
+gchar *string_replace(const gchar* str, const gchar *old_val, const gchar *new_val);
 
-#endif
+#ifdef __cplusplus
+}
+#endif /* __cplusplus */
 
 #endif /* __STRUTIL_H__ */