f975ea49e32e5b2fa6b7515df14ddceefbc8f870
[metze/wireshark/wip.git] / epan / strutil.h
1 /* strutil.h
2  * String utility definitions
3  *
4  * $Id$
5  *
6  * Wireshark - Network traffic analyzer
7  * By Gerald Combs <gerald@wireshark.org>
8  * Copyright 1998 Gerald Combs
9  *
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.
14  *
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.
19  *
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.
23  */
24
25 #ifndef __STRUTIL_H__
26 #define __STRUTIL_H__
27
28 #include "ws_symbol_export.h"
29
30 #ifdef __cplusplus
31 extern "C" {
32 #endif /* __cplusplus */
33
34 /** @file
35  * String handling and conversion utilities.
36  */
37
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
40  *  buffer.
41  *
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".
46  */
47 const guchar *find_line_end(const guchar *data, const guchar *dataend,
48     const guchar **eol);
49
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.
56  */
57 WS_DLL_PUBLIC
58 int        get_token_len(const guchar *linep, const guchar *lineend,
59     const guchar **next_token);
60
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.
63  *
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
67  *
68  * @see tvb_format_text()
69  */
70 WS_DLL_PUBLIC
71 gchar*     format_text(const guchar *line, size_t len);
72
73 /**
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.
78  *
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
82  *
83  */
84 WS_DLL_PUBLIC
85 gchar*     format_text_wsp(const guchar *line, size_t len);
86
87 /**
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.
92  *
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
97  *
98  */
99 WS_DLL_PUBLIC
100 gchar*     format_text_chr(const guchar *string, const size_t len, const guchar chr);
101
102
103 /** Turn a string of hex digits with optional separators (defined by
104  *  is_byte_sep() into a byte array.
105  *
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
110  *        bytes.
111  * @return True if the string was converted successfully
112  */
113 WS_DLL_PUBLIC
114 gboolean   hex_str_to_bytes(const char *hex_str, GByteArray *bytes,
115     gboolean force_separators);
116
117 /** Turn an RFC 3986 percent-encoded string into a byte array.
118  *
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
123  * @see format_uri()
124  */
125 WS_DLL_PUBLIC
126 gboolean   uri_str_to_bytes(const char *uri_str, GByteArray *bytes);
127
128 /** Turn a byte array into an RFC 3986 percent-encoded string.
129  *
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
136  *        instead.
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()
140  */
141 WS_DLL_PUBLIC
142 const gchar* format_uri(const GByteArray *bytes, const gchar *reserved_chars);
143
144 /** Turn a OID string representation (dot notation) into a byte array.
145  *
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
151  */
152 WS_DLL_PUBLIC
153 gboolean   rel_oid_str_to_bytes(const char *oid_str, GByteArray *bytes, gboolean is_absolute);
154
155 /** Turn a OID string representation (dot notation) into a byte array.
156  *
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
161  */
162 WS_DLL_PUBLIC
163 gboolean   oid_str_to_bytes(const char *oid_str, GByteArray *bytes);
164
165 /**
166  * Create a copy of a GByteArray
167  *
168  * @param ba The byte array to be copied.
169  * @return If ba exists, a freshly allocated copy.  NULL otherwise.
170  *
171  * @todo - Should this be in strutil.c?
172  */
173 WS_DLL_PUBLIC
174 GByteArray *byte_array_dup(GByteArray *ba);
175
176 /**
177  * Compare the contents of two GByteArrays
178  *
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
183  *         FALSE.
184  *
185  * @todo - Should this be in strutil.c?
186  */
187 WS_DLL_PUBLIC
188 gboolean byte_array_equal(GByteArray *ba1, GByteArray *ba2);
189
190
191 /** Return a XML escaped representation of the unescaped string.
192  *  The returned string must be freed when no longer in use.
193  *
194  * @param unescaped The unescaped string
195  * @return An XML-escaped representation of the input string
196  */
197 WS_DLL_PUBLIC
198 gchar*     xml_escape(const gchar *unescaped);
199
200 /**
201  * Return the first occurrence of needle in haystack.
202  * Algorithm copied from GNU's glibc 2.3.2 memcmp()
203  *
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.
211  */
212 WS_DLL_PUBLIC
213 const guint8 * epan_memmem(const guint8 *haystack, guint haystack_len,
214                 const guint8 *needle, guint needle_len);
215
216 /** Scan a string to make sure it's valid hex.
217  *
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.
222  */
223 WS_DLL_PUBLIC
224 guint8 * convert_string_to_hex(const char *string, size_t *nbytes);
225
226 /** Prep a string for case-sensitive vs case-insensitive searching.
227  *
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
232  * by the caller.
233  */
234 WS_DLL_PUBLIC
235 char * convert_string_case(const char *string, gboolean case_insensitive);
236
237 /** Finds the first occurrence of string 'needle' in string 'haystack'.
238  *  The matching is done in a case insensitive manner.
239  *
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.
244  */
245 WS_DLL_PUBLIC
246 char * epan_strcasestr(const char *haystack, const char *needle);
247
248 /** Guarantee a non-null string.
249  *
250  * @param string The string to check
251  * @return A pointer 'string' if it's non-null, otherwise "[NULL]".
252  */
253 WS_DLL_PUBLIC
254 const char * string_or_null(const char *string);
255
256 WS_DLL_PUBLIC
257 int escape_string_len(const char *string);
258 WS_DLL_PUBLIC
259 char * escape_string(char *dst, const char *string);
260
261
262 WS_DLL_PUBLIC
263 void IA5_7BIT_decode(unsigned char * dest, const unsigned char* src, int len);
264
265 /** Copy a string, escaping the 'chr' characters in it
266  *
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'.
271  */
272 WS_DLL_PUBLIC
273 gchar* ws_strdup_escape_char (const gchar *str, const gchar chr);
274
275 /** Copy a string, unescaping the 'chr' characters in it
276  *
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'.
281  */
282 WS_DLL_PUBLIC
283 gchar* ws_strdup_unescape_char (const gchar *str, const gchar chr);
284
285 /** Replace values in a string
286  *
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
292  * NULL on failure.
293  */
294 WS_DLL_PUBLIC
295 gchar *string_replace(const gchar* str, const gchar *old_val, const gchar *new_val);
296
297 #ifdef __cplusplus
298 }
299 #endif /* __cplusplus */
300
301 #endif /* __STRUTIL_H__ */