2 * String utility definitions
4 * Wireshark - Network traffic analyzer
5 * By Gerald Combs <gerald@wireshark.org>
6 * Copyright 1998 Gerald Combs
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 #include "ws_symbol_export.h"
28 #include <epan/wmem/wmem.h>
32 #endif /* __cplusplus */
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.
58 int get_token_len(const guchar *linep, const guchar *lineend,
59 const guchar **next_token);
61 /** Given a string, generate a string from it that shows non-printable
62 * characters as C-style escapes, and return a pointer to it.
64 * @param line A pointer to the input string
65 * @param len The length of the input string
66 * @return A pointer to the formatted string
68 * @see tvb_format_text()
71 gchar* format_text(const guchar *line, size_t len);
73 /** Given a string, generate a string from it that shows non-printable
74 * characters as C-style escapes, and return a pointer to it.
76 * @param allocator The wmem scope
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
81 * @see tvb_format_text()
84 gchar* format_text_wmem(wmem_allocator_t* allocator, const guchar *line, size_t len);
87 * Given a string, generate a string from it that shows non-printable
88 * characters as C-style escapes except a whitespace character
89 * (space, tab, carriage return, new line, vertical tab, or formfeed)
90 * which will be replaced by a space, and return a pointer to it.
92 * @param allocator The wmem scope
93 * @param line A pointer to the input string
94 * @param len The length of the input string
95 * @return A pointer to the formatted string
99 gchar* format_text_wsp(wmem_allocator_t* allocator, const guchar *line, size_t len);
102 * Given a string, generate a string from it that shows non-printable
103 * characters as the chr parameter passed, except a whitespace character
104 * (space, tab, carriage return, new line, vertical tab, or formfeed)
105 * which will be replaced by a space, and return a pointer to it.
107 * @param allocator The wmem scope
108 * @param string A pointer to the input string
109 * @param len The length of the input string
110 * @param chr The character to use to replace non-printable characters
111 * @return A pointer to the formatted string
115 gchar* format_text_chr(wmem_allocator_t* allocator, const guchar *string, const size_t len, const guchar chr);
118 /** Turn a string of hex digits with optional separators (defined by
119 * is_byte_sep() into a byte array.
121 * @param hex_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 * @param force_separators If set to TRUE, separators MUST exist between
126 * @return True if the string was converted successfully
129 gboolean hex_str_to_bytes(const char *hex_str, GByteArray *bytes,
130 gboolean force_separators);
132 /* Turn a string of hex digits with optional separators (defined by encoding)
133 * into a byte array. Unlike hex_str_to_bytes(), this will read as many hex-char
134 * pairs as possible and not error if it hits a non-hex-char; instead it just ends
135 * there. (i.e., like strtol()/atoi()/etc.) But it must see two hex chars at the
136 * beginning or it will return FALSE.
138 * @param hex_str The string of hex digits.
139 * @param bytes The GByteArray that will receive the bytes. This
140 * must be initialized by the caller.
141 * @param endptr if not NULL, is set to the char after the last hex character consumed.
142 * @param encoding set to one or more bitwise-or'ed ENC_SEP_* (see proto.h)
143 * @param fail_if_partial If set to TRUE, then the conversion fails if the whole
144 * hex_str is not consumed.
145 * @return FALSE only if no bytes were generated; or if fail_if_partial is TRUE
146 * and the entire hex_str was not consumed.
148 * If no ENC_SEP_* is set, then no separators are allowed. If multiple ENC_SEP_* are
149 * bit-or'ed, any of them can be a separator, but once the separator is seen then
150 * only its same type is accepted for the rest of the string. (i.e., it won't convert
151 * a "01:23-4567" even if ENC_SEP_COLON|ENC_SEP_DASH|ENC_SEP_NONE is passed in)
153 * This is done this way because it's likely a malformed scenario if they're mixed,
154 * and this routine is used by dissectors via tvb_get_string_XXX routines.
157 gboolean hex_str_to_bytes_encoding(const char *hex_str, GByteArray *bytes, const char **endptr,
158 const guint encoding, const gboolean fail_if_partial);
160 /** Turn an RFC 3986 percent-encoded string into a byte array.
162 * @param uri_str The string of hex digits.
163 * @param bytes The GByteArray that will receive the bytes. This
164 * must be initialized by the caller.
165 * @return True if the string was converted successfully
169 gboolean uri_str_to_bytes(const char *uri_str, GByteArray *bytes);
171 /** Turn a byte array into an RFC 3986 percent-encoded string.
173 * @param bytes The GByteArray that will receive the bytes. This
174 * must be initialized by the caller.
175 * @param reserved_chars Normally the "gen-delims" and "sub-delims"
176 * from RFC 3986 (":/?#[]@" and "!$&'()*+,;=" respectively)
177 * plus space (hex value 20) are treated as reserved characters.
178 * If this variable is non-NULL, its contents will be used
180 * @note Any non-printing character determined by isprint(), along
181 * with the % character itself are always reserved.
182 * @see uri_str_to_bytes(), format_text(), isprint()
185 const gchar* format_uri(const GByteArray *bytes, const gchar *reserved_chars);
187 /** Turn a OID string representation (dot notation) into a byte array.
189 * @param oid_str The OID string (dot notaion).
190 * @param bytes The GByteArray that will receive the bytes. This
191 * must be initialized by the caller.
192 * @param is_absolute True if this is an absolute OID; false for relative OID.
193 * @return True if the string was converted successfully
196 gboolean rel_oid_str_to_bytes(const char *oid_str, GByteArray *bytes, gboolean is_absolute);
198 /** Turn a OID string representation (dot notation) into a byte array.
200 * @param oid_str The OID string (dot notaion).
201 * @param bytes The GByteArray that will receive the bytes. This
202 * must be initialized by the caller.
203 * @return True if the string was converted successfully
206 gboolean oid_str_to_bytes(const char *oid_str, GByteArray *bytes);
209 * Create a copy of a GByteArray
211 * @param ba The byte array to be copied.
212 * @return If ba exists, a freshly allocated copy. NULL otherwise.
214 * @todo - Should this be in strutil.c?
217 GByteArray *byte_array_dup(const GByteArray *ba);
220 * Compare the contents of two GByteArrays
222 * @param ba1 A byte array
223 * @param ba2 A byte array
224 * @return If both arrays are non-NULL and their lengths are equal and
225 * their contents are equal, returns TRUE. Otherwise, returns
228 * @todo - Should this be in strutil.c?
231 gboolean byte_array_equal(GByteArray *ba1, GByteArray *ba2);
234 /** Return a XML escaped representation of the unescaped string.
235 * The returned string must be freed when no longer in use.
237 * @param unescaped The unescaped string
238 * @return An XML-escaped representation of the input string
241 gchar* xml_escape(const gchar *unescaped);
244 * Return the first occurrence of needle in haystack.
245 * Algorithm copied from GNU's glibc 2.3.2 memcmp()
247 * @param haystack The data to search
248 * @param haystack_len The length of the search data
249 * @param needle The string to look for
250 * @param needle_len The length of the search string
251 * @return A pointer to the first occurrence of "needle" in
252 * "haystack". If "needle" isn't found or is NULL, or if
253 * "needle_len" is 0, NULL is returned.
256 const guint8 * epan_memmem(const guint8 *haystack, guint haystack_len,
257 const guint8 *needle, guint needle_len);
259 /** Scan a string to make sure it's valid hex.
261 * @param string The string to validate
262 * @param nbytes The length of the return buffer
263 * @return A pointer to a buffer containing the converted raw bytes. This
264 * buffer must be g_free()d by the caller.
267 guint8 * convert_string_to_hex(const char *string, size_t *nbytes);
269 /** Prep a string for case-sensitive vs case-insensitive searching.
271 * @param string The search string
272 * @param case_insensitive TRUE if case-insensitive, FALSE if not
273 * @return A direct copy of the string if it's a case-sensitive search and
274 * an uppercased version if not. In either case the string must be g_free()d
278 char * convert_string_case(const char *string, gboolean case_insensitive);
280 /** Finds the first occurrence of string 'needle' in string 'haystack'.
281 * The matching is done in a case insensitive manner.
283 * @param haystack The string possibly containing the substring
284 * @param needle The substring to be searched
285 * @return A pointer into 'haystack' where 'needle' is first found.
286 * Otherwise it returns NULL.
289 const char * epan_strcasestr(const char *haystack, const char *needle);
291 /** Guarantee a non-null string.
293 * @param string The string to check
294 * @return A pointer 'string' if it's non-null, otherwise "[NULL]".
297 const char * string_or_null(const char *string);
300 int escape_string_len(const char *string);
302 char * escape_string(char *dst, const char *string);
306 void IA5_7BIT_decode(unsigned char * dest, const unsigned char* src, int len);
308 /** Copy a string, escaping the 'chr' characters in it
310 * @param str The string to be copied
311 * @param chr The character to be escaped
312 * @return A copy of the string with every original 'chr' being
313 * transformed into double 'chr'.
316 gchar* ws_strdup_escape_char (const gchar *str, const gchar chr);
318 /** Copy a string, unescaping the 'chr' characters in it
320 * @param str The string to be copied
321 * @param chr The character to be escaped
322 * @return A copy of the string with every occurrence of double 'chr' in
323 * the original string being copied as a single 'chr'.
326 gchar* ws_strdup_unescape_char (const gchar *str, const gchar chr);
328 /** Replace values in a string
330 * @param str String containing 0 or more values to be replaced.
331 * @param old_val Old value.
332 * @param new_val New value. May be NULL, in which case occurences of
333 * old_value will be removed.
334 * @return A newly-allocated version of str with replacement values or
338 gchar *string_replace(const gchar* str, const gchar *old_val, const gchar *new_val);
342 #endif /* __cplusplus */
344 #endif /* __STRUTIL_H__ */