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"
30 #endif /* __cplusplus */
33 * String handling and conversion utilities.
36 /** Given a pointer into a data buffer, and to the end of the buffer,
37 * find the end of the (putative) line at that position in the data
40 * @param data A pointer to the beginning of the data
41 * @param dataend A pointer to the end of the data
42 * @param eol A pointer that will receive the EOL location
43 * @return A pointer to the EOL character(s) in "*eol".
45 const guchar *find_line_end(const guchar *data, const guchar *dataend,
48 /** Get the length of the next token in a line, and the beginning of the
49 * next token after that (if any).
50 * @param linep A pointer to the beginning of the line
51 * @param lineend A pointer to the end of the line
52 * @param next_token Receives the location of the next token
53 * @return 0 if there is no next token.
56 int get_token_len(const guchar *linep, const guchar *lineend,
57 const guchar **next_token);
59 /** Given a string, generate a string from it that shows non-printable
60 * characters as C-style escapes, and return a pointer to it.
62 * @param line A pointer to the input string
63 * @param len The length of the input string
64 * @return A pointer to the formatted string
66 * @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
83 gchar* format_text_wsp(const guchar *line, size_t len);
86 * Given a string, generate a string from it that shows non-printable
87 * characters as the chr parameter passed, except a whitespace character
88 * (space, tab, carriage return, new line, vertical tab, or formfeed)
89 * which will be replaced by a space, and return a pointer to it.
91 * @param string A pointer to the input string
92 * @param len The length of the input string
93 * @param chr The character to use to replace non-printable characters
94 * @return A pointer to the formatted string
98 gchar* format_text_chr(const guchar *string, const size_t len, const guchar chr);
101 /** Turn a string of hex digits with optional separators (defined by
102 * is_byte_sep() into a byte array.
104 * @param hex_str The string of hex digits.
105 * @param bytes The GByteArray that will receive the bytes. This
106 * must be initialized by the caller.
107 * @param force_separators If set to TRUE, separators MUST exist between
109 * @return True if the string was converted successfully
112 gboolean hex_str_to_bytes(const char *hex_str, GByteArray *bytes,
113 gboolean force_separators);
115 /* Turn a string of hex digits with optional separators (defined by encoding)
116 * into a byte array. Unlike hex_str_to_bytes(), this will read as many hex-char
117 * pairs as possible and not error if it hits a non-hex-char; instead it just ends
118 * there. (i.e., like strtol()/atoi()/etc.) But it must see two hex chars at the
119 * beginning or it will return FALSE.
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 endptr if not NULL, is set to the char after the last hex character consumed.
125 * @param encoding set to one or more bitwise-or'ed ENC_SEP_* (see proto.h)
126 * @param fail_if_partial If set to TRUE, then the conversion fails if the whole
127 * hex_str is not consumed.
128 * @return FALSE only if no bytes were generated; or if fail_if_partial is TRUE
129 * and the entire hex_str was not consumed.
131 * If no ENC_SEP_* is set, then no separators are allowed. If multiple ENC_SEP_* are
132 * bit-or'ed, any of them can be a separator, but once the separator is seen then
133 * only its same type is accepted for the rest of the string. (i.e., it won't convert
134 * a "01:23-4567" even if ENC_SEP_COLON|ENC_SEP_DASH|ENC_SEP_NONE is passed in)
136 * This is done this way because it's likely a malformed scenario if they're mixed,
137 * and this routine is used by dissectors via tvb_get_string_XXX routines.
140 gboolean hex_str_to_bytes_encoding(const char *hex_str, GByteArray *bytes, const char **endptr,
141 const guint encoding, const gboolean fail_if_partial);
143 /** Turn an RFC 3986 percent-encoded string into a byte array.
145 * @param uri_str The string of hex digits.
146 * @param bytes The GByteArray that will receive the bytes. This
147 * must be initialized by the caller.
148 * @return True if the string was converted successfully
152 gboolean uri_str_to_bytes(const char *uri_str, GByteArray *bytes);
154 /** Turn a byte array into an RFC 3986 percent-encoded string.
156 * @param bytes The GByteArray that will receive the bytes. This
157 * must be initialized by the caller.
158 * @param reserved_chars Normally the "gen-delims" and "sub-delims"
159 * from RFC 3986 (":/?#[]@" and "!$&'()*+,;=" respectively)
160 * plus space (hex value 20) are treated as reserved characters.
161 * If this variable is non-NULL, its contents will be used
163 * @note Any non-printing character determined by isprint(), along
164 * with the % character itself are always reserved.
165 * @see uri_str_to_bytes(), format_text(), isprint()
168 const gchar* format_uri(const GByteArray *bytes, const gchar *reserved_chars);
170 /** Turn a OID string representation (dot notation) into a byte array.
172 * @param oid_str The OID string (dot notaion).
173 * @param bytes The GByteArray that will receive the bytes. This
174 * must be initialized by the caller.
175 * @param is_absolute True if this is an absolute OID; false for relative OID.
176 * @return True if the string was converted successfully
179 gboolean rel_oid_str_to_bytes(const char *oid_str, GByteArray *bytes, gboolean is_absolute);
181 /** Turn a OID string representation (dot notation) into a byte array.
183 * @param oid_str The OID string (dot notaion).
184 * @param bytes The GByteArray that will receive the bytes. This
185 * must be initialized by the caller.
186 * @return True if the string was converted successfully
189 gboolean oid_str_to_bytes(const char *oid_str, GByteArray *bytes);
192 * Create a copy of a GByteArray
194 * @param ba The byte array to be copied.
195 * @return If ba exists, a freshly allocated copy. NULL otherwise.
197 * @todo - Should this be in strutil.c?
200 GByteArray *byte_array_dup(const GByteArray *ba);
203 * Compare the contents of two GByteArrays
205 * @param ba1 A byte array
206 * @param ba2 A byte array
207 * @return If both arrays are non-NULL and their lengths are equal and
208 * their contents are equal, returns TRUE. Otherwise, returns
211 * @todo - Should this be in strutil.c?
214 gboolean byte_array_equal(GByteArray *ba1, GByteArray *ba2);
217 /** Return a XML escaped representation of the unescaped string.
218 * The returned string must be freed when no longer in use.
220 * @param unescaped The unescaped string
221 * @return An XML-escaped representation of the input string
224 gchar* xml_escape(const gchar *unescaped);
227 * Return the first occurrence of needle in haystack.
228 * Algorithm copied from GNU's glibc 2.3.2 memcmp()
230 * @param haystack The data to search
231 * @param haystack_len The length of the search data
232 * @param needle The string to look for
233 * @param needle_len The length of the search string
234 * @return A pointer to the first occurrence of "needle" in
235 * "haystack". If "needle" isn't found or is NULL, or if
236 * "needle_len" is 0, NULL is returned.
239 const guint8 * epan_memmem(const guint8 *haystack, guint haystack_len,
240 const guint8 *needle, guint needle_len);
242 /** Scan a string to make sure it's valid hex.
244 * @param string The string to validate
245 * @param nbytes The length of the return buffer
246 * @return A pointer to a buffer containing the converted raw bytes. This
247 * buffer must be g_free()d by the caller.
250 guint8 * convert_string_to_hex(const char *string, size_t *nbytes);
252 /** Prep a string for case-sensitive vs case-insensitive searching.
254 * @param string The search string
255 * @param case_insensitive TRUE if case-insensitive, FALSE if not
256 * @return A direct copy of the string if it's a case-sensitive search and
257 * an uppercased version if not. In either case the string must be g_free()d
261 char * convert_string_case(const char *string, gboolean case_insensitive);
263 /** Finds the first occurrence of string 'needle' in string 'haystack'.
264 * The matching is done in a case insensitive manner.
266 * @param haystack The string possibly containing the substring
267 * @param needle The substring to be searched
268 * @return A pointer into 'haystack' where 'needle' is first found.
269 * Otherwise it returns NULL.
272 char * epan_strcasestr(const char *haystack, const char *needle);
274 /** Guarantee a non-null string.
276 * @param string The string to check
277 * @return A pointer 'string' if it's non-null, otherwise "[NULL]".
280 const char * string_or_null(const char *string);
283 int escape_string_len(const char *string);
285 char * escape_string(char *dst, const char *string);
289 void IA5_7BIT_decode(unsigned char * dest, const unsigned char* src, int len);
291 /** Copy a string, escaping the 'chr' characters in it
293 * @param str The string to be copied
294 * @param chr The character to be escaped
295 * @return A copy of the string with every original 'chr' being
296 * transformed into double 'chr'.
299 gchar* ws_strdup_escape_char (const gchar *str, const gchar chr);
301 /** Copy a string, unescaping the 'chr' characters in it
303 * @param str The string to be copied
304 * @param chr The character to be escaped
305 * @return A copy of the string with every occurrence of double 'chr' in
306 * the original string being copied as a single 'chr'.
309 gchar* ws_strdup_unescape_char (const gchar *str, const gchar chr);
311 /** Replace values in a string
313 * @param str String containing 0 or more values to be replaced.
314 * @param old_val Old value.
315 * @param new_val New value. May be NULL, in which case occurences of
316 * old_value will be removed.
317 * @return A newly-allocated version of str with replacement values or
321 gchar *string_replace(const gchar* str, const gchar *old_val, const gchar *new_val);
325 #endif /* __cplusplus */
327 #endif /* __STRUTIL_H__ */