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 */
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);
74 * Given a string, generate a string from it that shows non-printable
75 * characters as C-style escapes except a whitespace character
76 * (space, tab, carriage return, new line, vertical tab, or formfeed)
77 * which will be replaced by a space, and return a pointer to it.
79 * @param line A pointer to the input string
80 * @param len The length of the input string
81 * @return A pointer to the formatted string
85 gchar* format_text_wsp(const guchar *line, size_t len);
88 * Given a string, generate a string from it that shows non-printable
89 * characters as the chr parameter passed, except a whitespace character
90 * (space, tab, carriage return, new line, vertical tab, or formfeed)
91 * which will be replaced by a space, and return a pointer to it.
93 * @param string A pointer to the input string
94 * @param len The length of the input string
95 * @param chr The character to use to replace non-printable characters
96 * @return A pointer to the formatted string
100 gchar* format_text_chr(const guchar *string, const size_t len, const guchar chr);
103 /** Turn a string of hex digits with optional separators (defined by
104 * is_byte_sep() into a byte array.
106 * @param hex_str The string of hex digits.
107 * @param bytes The GByteArray that will receive the bytes. This
108 * must be initialized by the caller.
109 * @param force_separators If set to TRUE, separators MUST exist between
111 * @return True if the string was converted successfully
114 gboolean hex_str_to_bytes(const char *hex_str, GByteArray *bytes,
115 gboolean force_separators);
117 /** Turn an RFC 3986 percent-encoded string into a byte array.
119 * @param uri_str The string of hex digits.
120 * @param bytes The GByteArray that will receive the bytes. This
121 * must be initialized by the caller.
122 * @return True if the string was converted successfully
126 gboolean uri_str_to_bytes(const char *uri_str, GByteArray *bytes);
128 /** Turn a byte array into an RFC 3986 percent-encoded string.
130 * @param bytes The GByteArray that will receive the bytes. This
131 * must be initialized by the caller.
132 * @param reserved_chars Normally the "gen-delims" and "sub-delims"
133 * from RFC 3986 (":/?#[]@" and "!$&'()*+,;=" respectively)
134 * plus space (hex value 20) are treated as reserved characters.
135 * If this variable is non-NULL, its contents will be used
137 * @note Any non-printing character determined by isprint(), along
138 * with the % character itself are always reserved.
139 * @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 * @param is_absolute True if this is an absolute OID; false for relative OID.
150 * @return True if the string was converted successfully
153 gboolean rel_oid_str_to_bytes(const char *oid_str, GByteArray *bytes, gboolean is_absolute);
155 /** Turn a OID string representation (dot notation) into a byte array.
157 * @param oid_str The OID string (dot notaion).
158 * @param bytes The GByteArray that will receive the bytes. This
159 * must be initialized by the caller.
160 * @return True if the string was converted successfully
163 gboolean oid_str_to_bytes(const char *oid_str, GByteArray *bytes);
166 * Create a copy of a GByteArray
168 * @param ba The byte array to be copied.
169 * @return If ba exists, a freshly allocated copy. NULL otherwise.
171 * @todo - Should this be in strutil.c?
174 GByteArray *byte_array_dup(GByteArray *ba);
177 * Compare the contents of two GByteArrays
179 * @param ba1 A byte array
180 * @param ba2 A byte array
181 * @return If both arrays are non-NULL and their lengths are equal and
182 * their contents are equal, returns TRUE. Otherwise, returns
185 * @todo - Should this be in strutil.c?
188 gboolean byte_array_equal(GByteArray *ba1, GByteArray *ba2);
191 /** Return a XML escaped representation of the unescaped string.
192 * The returned string must be freed when no longer in use.
194 * @param unescaped The unescaped string
195 * @return An XML-escaped representation of the input string
198 gchar* xml_escape(const gchar *unescaped);
201 * Return the first occurrence of needle in haystack.
202 * Algorithm copied from GNU's glibc 2.3.2 memcmp()
204 * @param haystack The data to search
205 * @param haystack_len The length of the search data
206 * @param needle The string to look for
207 * @param needle_len The length of the search string
208 * @return A pointer to the first occurrence of "needle" in
209 * "haystack". If "needle" isn't found or is NULL, or if
210 * "needle_len" is 0, NULL is returned.
213 const guint8 * epan_memmem(const guint8 *haystack, guint haystack_len,
214 const guint8 *needle, guint needle_len);
216 /** Scan a string to make sure it's valid hex.
218 * @param string The string to validate
219 * @param nbytes The length of the return buffer
220 * @return A pointer to a buffer containing the converted raw bytes. This
221 * buffer must be g_free()d by the caller.
224 guint8 * convert_string_to_hex(const char *string, size_t *nbytes);
226 /** Prep a string for case-sensitive vs case-insensitive searching.
228 * @param string The search string
229 * @param case_insensitive TRUE if case-insensitive, FALSE if not
230 * @return A direct copy of the string if it's a case-sensitive search and
231 * an uppercased version if not. In either case the string must be g_free()d
235 char * convert_string_case(const char *string, gboolean case_insensitive);
237 /** Finds the first occurrence of string 'needle' in string 'haystack'.
238 * The matching is done in a case insensitive manner.
240 * @param haystack The string possibly containing the substring
241 * @param needle The substring to be searched
242 * @return A pointer into 'haystack' where 'needle' is first found.
243 * Otherwise it returns NULL.
246 char * epan_strcasestr(const char *haystack, const char *needle);
248 /** Guarantee a non-null string.
250 * @param string The string to check
251 * @return A pointer 'string' if it's non-null, otherwise "[NULL]".
254 const char * string_or_null(const char *string);
257 int escape_string_len(const char *string);
259 char * escape_string(char *dst, const char *string);
263 void IA5_7BIT_decode(unsigned char * dest, const unsigned char* src, int len);
265 /** Copy a string, escaping the 'chr' characters in it
267 * @param str The string to be copied
268 * @param chr The character to be escaped
269 * @return A copy of the string with every original 'chr' being
270 * transformed into double 'chr'.
273 gchar* ws_strdup_escape_char (const gchar *str, const gchar chr);
275 /** Copy a string, unescaping the 'chr' characters in it
277 * @param str The string to be copied
278 * @param chr The character to be escaped
279 * @return A copy of the string with every occurrence of double 'chr' in
280 * the original string being copied as a single 'chr'.
283 gchar* ws_strdup_unescape_char (const gchar *str, const gchar chr);
285 /** Replace values in a string
287 * @param str String containing 0 or more values to be replaced.
288 * @param old_val Old value.
289 * @param new_val New value. May be NULL, in which case occurences of
290 * old_value will be removed.
291 * @return A newly-allocated version of str with replacement values or
295 gchar *string_replace(const gchar* str, const gchar *old_val, const gchar *new_val);
299 #endif /* __cplusplus */
301 #endif /* __STRUTIL_H__ */