2 * String utility definitions
6 * Wireshark - Network traffic analyzer
7 * By Gerald Combs <gerald@wireshark.org>
8 * Copyright 1998 Gerald Combs
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
30 #endif /* __cplusplus */
32 /* ... thus, config.h needs to be #included */
35 * String handling and conversion utilities.
38 /** Given a pointer into a data buffer, and to the end of the buffer,
39 * find the end of the (putative) line at that position in the data
42 * @param data A pointer to the beginning of the data
43 * @param dataend A pointer to the end of the data
44 * @param eol A pointer that will receive the EOL location
45 * @return A pointer to the EOL character(s) in "*eol".
47 const guchar *find_line_end(const guchar *data, const guchar *dataend,
50 /** Get the length of the next token in a line, and the beginning of the
51 * next token after that (if any).
52 * @param linep A pointer to the beginning of the line
53 * @param lineend A pointer to the end of the line
54 * @param next_token Receives the location of the next token
55 * @return 0 if there is no next token.
57 int get_token_len(const guchar *linep, const guchar *lineend,
58 const guchar **next_token);
60 /** Given a string, generate a string from it that shows non-printable
61 * characters as C-style escapes, and return a pointer to it.
63 * @param line A pointer to the input string
64 * @param len The length of the input string
65 * @return A pointer to the formatted string
67 * @see tvb_format_text()
69 gchar* format_text(const guchar *line, size_t len);
72 * Given a string, generate a string from it that shows non-printable
73 * characters as C-style escapes except a whitespace character
74 * (space, tab, carriage return, new line, vertical tab, or formfeed)
75 * which will be replaced by a space, and return a pointer to it.
77 * @param line A pointer to the input string
78 * @param len The length of the input string
79 * @return A pointer to the formatted string
82 gchar* format_text_wsp(const guchar *line, size_t len);
84 /** Turn an array of bytes into a string showing the bytes in hex.
86 * @param bd A pointer to the byte array
87 * @param bd_len The length of the byte array
88 * @return A pointer to the formatted string
90 * @see bytes_to_str_punct()
92 gchar* bytes_to_str(const guint8 *bd, int bd_len);
94 /** Turn an array of bytes into a string showing the bytes in hex,
95 * separated by a punctuation character.
97 * @param bd A pointer to the byte array
98 * @param bd_len The length of the byte array
99 * @param punct The punctuation character
100 * @return A pointer to the formatted string
102 * @see bytes_to_str()
104 gchar* bytes_to_str_punct(const guint8 *bd, int bd_len, gchar punct);
106 /** Turn a string of hex digits with optional separators (defined by
107 * is_byte_sep() into a byte array.
109 * @param hex_str The string of hex digits.
110 * @param bytes The GByteArray that will receive the bytes. This
111 * must be initialized by the caller.
112 * @param force_separators If set to TRUE, separators MUST exist between
114 * @return True if the string was converted successfully
116 gboolean hex_str_to_bytes(const char *hex_str, GByteArray *bytes,
117 gboolean force_separators);
119 /** Turn an RFC 3986 percent-encoded string into a byte array.
121 * @param uri_str The string of hex digits.
122 * @param bytes The GByteArray that will receive the bytes. This
123 * must be initialized by the caller.
124 * @return True if the string was converted successfully
127 gboolean uri_str_to_bytes(const char *uri_str, GByteArray *bytes);
129 /** Turn a byte array into an RFC 3986 percent-encoded string.
131 * @param bytes The GByteArray that will receive the bytes. This
132 * must be initialized by the caller.
133 * @param reserved_chars Normally the "gen-delims" and "sub-delims"
134 * from RFC 3986 (":/?#[]@" and "!$&'()*+,;=" respectively)
135 * plus space (hex value 20) are treated as reserved characters.
136 * If this variable is non-NULL, its contents will be used
138 * @note Any non-printing character determined by isprint(), along
139 * with the % character itself are always reserved.
140 * @see uri_str_to_bytes(), format_text(), isprint()
142 const gchar* format_uri(const GByteArray *bytes, const gchar *reserved_chars);
144 /** Turn a OID string representation (dot notation) into a byte array.
146 * @param oid_str The OID string (dot notaion).
147 * @param bytes The GByteArray that will receive the bytes. This
148 * must be initialized by the caller.
149 * @return True if the string was converted successfully
151 gboolean oid_str_to_bytes(const char *oid_str, GByteArray *bytes);
154 * Create a copy of a GByteArray
156 * @param ba The byte array to be copied.
157 * @return If ba exists, a freshly allocated copy. NULL otherwise.
159 * @todo - Should this be in strutil.c?
161 GByteArray *byte_array_dup(GByteArray *ba);
164 * Compare the contents of two GByteArrays
166 * @param ba1 A byte array
167 * @param ba2 A byte array
168 * @return If both arrays are non-NULL and their lengths are equal and
169 * their contents are equal, returns TRUE. Otherwise, returns
172 * @todo - Should this be in strutil.c?
174 gboolean byte_array_equal(GByteArray *ba1, GByteArray *ba2);
177 /** Return a XML escaped representation of the unescaped string.
178 * The returned string must be freed when no longer in use.
180 * @param unescaped The unescaped string
181 * @return An XML-escaped representation of the input string
183 gchar* xml_escape(const gchar *unescaped);
186 * Return the first occurrence of needle in haystack.
187 * Algorithm copied from GNU's glibc 2.3.2 memcmp()
189 * @param haystack The data to search
190 * @param haystack_len The length of the search data
191 * @param needle The string to look for
192 * @param needle_len The length of the search string
193 * @return A pointer to the first occurrence of "needle" in
194 * "haystack". If "needle" isn't found or is NULL, or if
195 * "needle_len" is 0, NULL is returned.
197 const guint8 * epan_memmem(const guint8 *haystack, guint haystack_len,
198 const guint8 *needle, guint needle_len);
200 /** Surround a string or a macro, resolved to a string, with double quotes */
201 #define _STRINGIFY(a) # a
202 #define STRINGIFY(a) _STRINGIFY(a)
204 /** Scan a string to make sure it's valid hex.
206 * @param string The string to validate
207 * @param nbytes The length of the return buffer
208 * @return A pointer to a buffer containing the converted raw bytes. This
209 * buffer must be g_free()d by the caller.
211 guint8 * convert_string_to_hex(const char *string, size_t *nbytes);
213 /** Prep a string for case-sensitive vs case-insensitive searching.
215 * @param string The search string
216 * @param case_insensitive TRUE if case-insensitive, FALSE if not
217 * @return A direct copy of the string if it's a case-sensitive search and
218 * an uppercased version if not. In either case the string must be g_free()d
221 char * convert_string_case(const char *string, gboolean case_insensitive);
223 /** Finds the first occurrence of string 'needle' in string 'haystack'.
224 * The matching is done in a case insensitive manner.
226 * @param haystack The string possibly containing the substring
227 * @param needle The substring to be searched
228 * @return A pointer into 'haystack' where 'needle' is first found.
229 * Otherwise it returns NULL.
231 char * epan_strcasestr(const char *haystack, const char *needle);
233 /** Guarantee a non-null string.
235 * @param string The string to check
236 * @return A pointer 'string' if it's non-null, otherwise "[NULL]".
238 const char * string_or_null(const char *string);
240 int escape_string_len(const char *string);
241 char * escape_string(char *dst, const char *string);
244 void IA5_7BIT_decode(unsigned char * dest, const unsigned char* src, int len);
246 /** Copy a string, escaping the 'chr' characters in it
248 * @param str The string to be copied
249 * @param char The character to be escaped
250 * @return A copy of the string with every original 'chr' being
251 * transformed into double 'chr'.
253 gchar* ws_strdup_escape_char (const gchar *str, const gchar chr);
255 /** Copy a string, unescaping the 'chr' characters in it
257 * @param str The string to be copied
258 * @param char The character to be escaped
259 * @return A copy of the string with every occurrence of double 'chr' in
260 * the original string being copied as a single 'chr'.
262 gchar* ws_strdup_unescape_char (const gchar *str, const gchar chr);
264 /** Replace values in a string
266 * @param str String containing 0 or more values to be replaced.
267 * @param old_val Old value.
268 * @param new_val New value. May be NULL, in which case occurences of
269 * old_value will be removed.
270 * @return A newly-allocated version of str with replacement values or
273 gchar *string_replace(const gchar* str, const gchar *old_val, const gchar *new_val);
276 * g_strcmp0 appears first in GLIB 2.16, define it locally for earlier versions.
279 #if !GLIB_CHECK_VERSION(2,16,0)
280 int g_strcmp0 (const char *str1,
282 #endif /* GLIB_CHECK_VERSION(2,16,0) */
285 #endif /* __cplusplus */
287 #endif /* __STRUTIL_H__ */