Include config.h first; it defines _FILE_OFFSET_BITS, and if some system
[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 /* ... thus, config.h needs to be #included */
35
36 /** @file
37  * String handling and conversion utilities.
38  */
39
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
42  *  buffer.
43  *
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".
48  */
49 const guchar *find_line_end(const guchar *data, const guchar *dataend,
50     const guchar **eol);
51
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.
58  */
59 WS_DLL_PUBLIC
60 int        get_token_len(const guchar *linep, const guchar *lineend,
61     const guchar **next_token);
62
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.
65  *
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
69  *
70  * @see tvb_format_text()
71  */
72 WS_DLL_PUBLIC
73 gchar*     format_text(const guchar *line, size_t len);
74
75 /**
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.
80  *
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
84  *
85  */
86 WS_DLL_PUBLIC
87 gchar*     format_text_wsp(const guchar *line, size_t len);
88
89 /** Turn an array of bytes into a string showing the bytes in hex.
90  *
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
94  *
95  * @see bytes_to_str_punct()
96  */
97 WS_DLL_PUBLIC
98 gchar*     bytes_to_str(const guint8 *bd, int bd_len);
99
100 /** Turn an array of bytes into a string showing the bytes in hex,
101  *  separated by a punctuation character.
102  *
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
107  *
108  * @see bytes_to_str()
109  */
110 WS_DLL_PUBLIC
111 gchar*     bytes_to_str_punct(const guint8 *bd, int bd_len, gchar punct);
112
113 /** Turn a string of hex digits with optional separators (defined by
114  *  is_byte_sep() into a byte array.
115  *
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
120  *        bytes.
121  * @return True if the string was converted successfully
122  */
123 WS_DLL_PUBLIC
124 gboolean   hex_str_to_bytes(const char *hex_str, GByteArray *bytes,
125     gboolean force_separators);
126
127 /** Turn an RFC 3986 percent-encoded string into a byte array.
128  *
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
133  * @see format_uri()
134  */
135 WS_DLL_PUBLIC
136 gboolean   uri_str_to_bytes(const char *uri_str, GByteArray *bytes);
137
138 /** Turn a byte array into an RFC 3986 percent-encoded string.
139  *
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
146  *        instead.
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()
150  */
151 WS_DLL_PUBLIC
152 const gchar* format_uri(const GByteArray *bytes, const gchar *reserved_chars);
153
154 /** Turn a OID string representation (dot notation) into a byte array.
155  *
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
160  */
161 WS_DLL_PUBLIC
162 gboolean   oid_str_to_bytes(const char *oid_str, GByteArray *bytes);
163
164 /**
165  * Create a copy of a GByteArray
166  *
167  * @param ba The byte array to be copied.
168  * @return If ba exists, a freshly allocated copy.  NULL otherwise.
169  *
170  * @todo - Should this be in strutil.c?
171  */
172 WS_DLL_PUBLIC
173 GByteArray *byte_array_dup(GByteArray *ba);
174
175 /**
176  * Compare the contents of two GByteArrays
177  *
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
182  *         FALSE.
183  *
184  * @todo - Should this be in strutil.c?
185  */
186 WS_DLL_PUBLIC
187 gboolean byte_array_equal(GByteArray *ba1, GByteArray *ba2);
188
189
190 /** Return a XML escaped representation of the unescaped string.
191  *  The returned string must be freed when no longer in use.
192  *
193  * @param unescaped The unescaped string
194  * @return An XML-escaped representation of the input string
195  */
196 WS_DLL_PUBLIC
197 gchar*     xml_escape(const gchar *unescaped);
198
199 /**
200  * Return the first occurrence of needle in haystack.
201  * Algorithm copied from GNU's glibc 2.3.2 memcmp()
202  *
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.
210  */
211 WS_DLL_PUBLIC
212 const guint8 * epan_memmem(const guint8 *haystack, guint haystack_len,
213                 const guint8 *needle, guint needle_len);
214
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)
218
219 /** Scan a string to make sure it's valid hex.
220  *
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.
225  */
226 WS_DLL_PUBLIC
227 guint8 * convert_string_to_hex(const char *string, size_t *nbytes);
228
229 /** Prep a string for case-sensitive vs case-insensitive searching.
230  *
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
235  * by the caller.
236  */
237 WS_DLL_PUBLIC
238 char * convert_string_case(const char *string, gboolean case_insensitive);
239
240 /** Finds the first occurrence of string 'needle' in string 'haystack'.
241  *  The matching is done in a case insensitive manner.
242  *
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.
247  */
248 WS_DLL_PUBLIC
249 char * epan_strcasestr(const char *haystack, const char *needle);
250
251 /** Guarantee a non-null string.
252  *
253  * @param string The string to check
254  * @return A pointer 'string' if it's non-null, otherwise "[NULL]".
255  */
256 WS_DLL_PUBLIC
257 const char * string_or_null(const char *string);
258
259 WS_DLL_PUBLIC
260 int escape_string_len(const char *string);
261 WS_DLL_PUBLIC
262 char * escape_string(char *dst, const char *string);
263
264
265 WS_DLL_PUBLIC
266 void IA5_7BIT_decode(unsigned char * dest, const unsigned char* src, int len);
267
268 /** Copy a string, escaping the 'chr' characters in it
269  *
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'.
274  */
275 WS_DLL_PUBLIC
276 gchar* ws_strdup_escape_char (const gchar *str, const gchar chr);
277
278 /** Copy a string, unescaping the 'chr' characters in it
279  *
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'.
284  */
285 WS_DLL_PUBLIC
286 gchar* ws_strdup_unescape_char (const gchar *str, const gchar chr);
287
288 /** Replace values in a string
289  *
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
295  * NULL on failure.
296  */
297 WS_DLL_PUBLIC
298 gchar *string_replace(const gchar* str, const gchar *old_val, const gchar *new_val);
299
300 /**
301  * g_strcmp0 appears first in GLIB 2.16, define it locally for earlier versions.
302  */
303
304 #ifdef __cplusplus
305 }
306 #endif /* __cplusplus */
307
308 #endif /* __STRUTIL_H__ */