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.
28 #include "ws_symbol_export.h"
32 #endif /* __cplusplus */
34 /* ... thus, config.h needs to be #included */
37 * String handling and conversion utilities.
40 /** Given a pointer into a data buffer, and to the end of the buffer,
41 * find the end of the (putative) line at that position in the data
44 * @param data A pointer to the beginning of the data
45 * @param dataend A pointer to the end of the data
46 * @param eol A pointer that will receive the EOL location
47 * @return A pointer to the EOL character(s) in "*eol".
49 const guchar *find_line_end(const guchar *data, const guchar *dataend,
52 /** Get the length of the next token in a line, and the beginning of the
53 * next token after that (if any).
54 * @param linep A pointer to the beginning of the line
55 * @param lineend A pointer to the end of the line
56 * @param next_token Receives the location of the next token
57 * @return 0 if there is no next token.
60 int get_token_len(const guchar *linep, const guchar *lineend,
61 const guchar **next_token);
63 /** Given a string, generate a string from it that shows non-printable
64 * characters as C-style escapes, and return a pointer to it.
66 * @param line A pointer to the input string
67 * @param len The length of the input string
68 * @return A pointer to the formatted string
70 * @see tvb_format_text()
73 gchar* format_text(const guchar *line, size_t len);
76 * Given a string, generate a string from it that shows non-printable
77 * characters as C-style escapes except a whitespace character
78 * (space, tab, carriage return, new line, vertical tab, or formfeed)
79 * which will be replaced by a space, and return a pointer to it.
81 * @param line A pointer to the input string
82 * @param len The length of the input string
83 * @return A pointer to the formatted string
87 gchar* format_text_wsp(const guchar *line, size_t len);
89 /** Turn an array of bytes into a string showing the bytes in hex.
91 * @param bd A pointer to the byte array
92 * @param bd_len The length of the byte array
93 * @return A pointer to the formatted string
95 * @see bytes_to_str_punct()
98 gchar* bytes_to_str(const guint8 *bd, int bd_len);
100 /** Turn an array of bytes into a string showing the bytes in hex,
101 * separated by a punctuation character.
103 * @param bd A pointer to the byte array
104 * @param bd_len The length of the byte array
105 * @param punct The punctuation character
106 * @return A pointer to the formatted string
108 * @see bytes_to_str()
111 gchar* bytes_to_str_punct(const guint8 *bd, int bd_len, gchar punct);
113 /** Turn a string of hex digits with optional separators (defined by
114 * is_byte_sep() into a byte array.
116 * @param hex_str The string of hex digits.
117 * @param bytes The GByteArray that will receive the bytes. This
118 * must be initialized by the caller.
119 * @param force_separators If set to TRUE, separators MUST exist between
121 * @return True if the string was converted successfully
124 gboolean hex_str_to_bytes(const char *hex_str, GByteArray *bytes,
125 gboolean force_separators);
127 /** Turn an RFC 3986 percent-encoded string into a byte array.
129 * @param uri_str The string of hex digits.
130 * @param bytes The GByteArray that will receive the bytes. This
131 * must be initialized by the caller.
132 * @return True if the string was converted successfully
136 gboolean uri_str_to_bytes(const char *uri_str, GByteArray *bytes);
138 /** Turn a byte array into an RFC 3986 percent-encoded string.
140 * @param bytes The GByteArray that will receive the bytes. This
141 * must be initialized by the caller.
142 * @param reserved_chars Normally the "gen-delims" and "sub-delims"
143 * from RFC 3986 (":/?#[]@" and "!$&'()*+,;=" respectively)
144 * plus space (hex value 20) are treated as reserved characters.
145 * If this variable is non-NULL, its contents will be used
147 * @note Any non-printing character determined by isprint(), along
148 * with the % character itself are always reserved.
149 * @see uri_str_to_bytes(), format_text(), isprint()
152 const gchar* format_uri(const GByteArray *bytes, const gchar *reserved_chars);
154 /** Turn a OID string representation (dot notation) into a byte array.
156 * @param oid_str The OID string (dot notaion).
157 * @param bytes The GByteArray that will receive the bytes. This
158 * must be initialized by the caller.
159 * @return True if the string was converted successfully
162 gboolean oid_str_to_bytes(const char *oid_str, GByteArray *bytes);
165 * Create a copy of a GByteArray
167 * @param ba The byte array to be copied.
168 * @return If ba exists, a freshly allocated copy. NULL otherwise.
170 * @todo - Should this be in strutil.c?
173 GByteArray *byte_array_dup(GByteArray *ba);
176 * Compare the contents of two GByteArrays
178 * @param ba1 A byte array
179 * @param ba2 A byte array
180 * @return If both arrays are non-NULL and their lengths are equal and
181 * their contents are equal, returns TRUE. Otherwise, returns
184 * @todo - Should this be in strutil.c?
187 gboolean byte_array_equal(GByteArray *ba1, GByteArray *ba2);
190 /** Return a XML escaped representation of the unescaped string.
191 * The returned string must be freed when no longer in use.
193 * @param unescaped The unescaped string
194 * @return An XML-escaped representation of the input string
197 gchar* xml_escape(const gchar *unescaped);
200 * Return the first occurrence of needle in haystack.
201 * Algorithm copied from GNU's glibc 2.3.2 memcmp()
203 * @param haystack The data to search
204 * @param haystack_len The length of the search data
205 * @param needle The string to look for
206 * @param needle_len The length of the search string
207 * @return A pointer to the first occurrence of "needle" in
208 * "haystack". If "needle" isn't found or is NULL, or if
209 * "needle_len" is 0, NULL is returned.
212 const guint8 * epan_memmem(const guint8 *haystack, guint haystack_len,
213 const guint8 *needle, guint needle_len);
215 /** Surround a string or a macro, resolved to a string, with double quotes */
216 #define _STRINGIFY(a) # a
217 #define STRINGIFY(a) _STRINGIFY(a)
219 /** Scan a string to make sure it's valid hex.
221 * @param string The string to validate
222 * @param nbytes The length of the return buffer
223 * @return A pointer to a buffer containing the converted raw bytes. This
224 * buffer must be g_free()d by the caller.
227 guint8 * convert_string_to_hex(const char *string, size_t *nbytes);
229 /** Prep a string for case-sensitive vs case-insensitive searching.
231 * @param string The search string
232 * @param case_insensitive TRUE if case-insensitive, FALSE if not
233 * @return A direct copy of the string if it's a case-sensitive search and
234 * an uppercased version if not. In either case the string must be g_free()d
238 char * convert_string_case(const char *string, gboolean case_insensitive);
240 /** Finds the first occurrence of string 'needle' in string 'haystack'.
241 * The matching is done in a case insensitive manner.
243 * @param haystack The string possibly containing the substring
244 * @param needle The substring to be searched
245 * @return A pointer into 'haystack' where 'needle' is first found.
246 * Otherwise it returns NULL.
249 char * epan_strcasestr(const char *haystack, const char *needle);
251 /** Guarantee a non-null string.
253 * @param string The string to check
254 * @return A pointer 'string' if it's non-null, otherwise "[NULL]".
257 const char * string_or_null(const char *string);
260 int escape_string_len(const char *string);
262 char * escape_string(char *dst, const char *string);
266 void IA5_7BIT_decode(unsigned char * dest, const unsigned char* src, int len);
268 /** Copy a string, escaping the 'chr' characters in it
270 * @param str The string to be copied
271 * @param chr The character to be escaped
272 * @return A copy of the string with every original 'chr' being
273 * transformed into double 'chr'.
276 gchar* ws_strdup_escape_char (const gchar *str, const gchar chr);
278 /** Copy a string, unescaping the 'chr' characters in it
280 * @param str The string to be copied
281 * @param chr The character to be escaped
282 * @return A copy of the string with every occurrence of double 'chr' in
283 * the original string being copied as a single 'chr'.
286 gchar* ws_strdup_unescape_char (const gchar *str, const gchar chr);
288 /** Replace values in a string
290 * @param str String containing 0 or more values to be replaced.
291 * @param old_val Old value.
292 * @param new_val New value. May be NULL, in which case occurences of
293 * old_value will be removed.
294 * @return A newly-allocated version of str with replacement values or
298 gchar *string_replace(const gchar* str, const gchar *old_val, const gchar *new_val);
301 * g_strcmp0 appears first in GLIB 2.16, define it locally for earlier versions.
306 #endif /* __cplusplus */
308 #endif /* __STRUTIL_H__ */