Fix a bunch of warnings.
[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 #ifdef __cplusplus
29 extern "C" {
30 #endif /* __cplusplus */
31
32 /* ... thus, config.h needs to be #included */
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 int        get_token_len(const guchar *linep, const guchar *lineend,
58     const guchar **next_token);
59
60 /** Given a string, generate a string from it that shows non-printable
61  *  characters as C-style escapes, and return a pointer to it.
62  *
63  * @param line A pointer to the input string
64  * @param len The length of the input string
65  * @return A pointer to the formatted string
66  *
67  * @see tvb_format_text()
68  */
69 gchar*     format_text(const guchar *line, size_t len);
70
71 /**
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.
76  *
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
80  *
81  */
82 gchar*     format_text_wsp(const guchar *line, size_t len);
83
84 /** Turn an array of bytes into a string showing the bytes in hex.
85  *
86  * @param bd A pointer to the byte array
87  * @param bd_len The length of the byte array
88  * @return A pointer to the formatted string
89  *
90  * @see bytes_to_str_punct()
91  */
92 gchar*     bytes_to_str(const guint8 *bd, int bd_len);
93
94 /** Turn an array of bytes into a string showing the bytes in hex,
95  *  separated by a punctuation character.
96  *
97  * @param bd A pointer to the byte array
98  * @param bd_len The length of the byte array
99  * @param punct The punctuation character
100  * @return A pointer to the formatted string
101  *
102  * @see bytes_to_str()
103  */
104 gchar*     bytes_to_str_punct(const guint8 *bd, int bd_len, gchar punct);
105
106 /** Turn a string of hex digits with optional separators (defined by
107  *  is_byte_sep() into a byte array.
108  *
109  * @param hex_str The string of hex digits.
110  * @param bytes The GByteArray that will receive the bytes.  This
111  *        must be initialized by the caller.
112  * @param force_separators If set to TRUE, separators MUST exist between
113  *        bytes.
114  * @return True if the string was converted successfully
115  */
116 gboolean   hex_str_to_bytes(const char *hex_str, GByteArray *bytes,
117     gboolean force_separators);
118
119 /** Turn an RFC 3986 percent-encoded string into a byte array.
120  *
121  * @param uri_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  * @return True if the string was converted successfully
125  * @see format_uri()
126  */
127 gboolean   uri_str_to_bytes(const char *uri_str, GByteArray *bytes);
128
129 /** Turn a byte array into an RFC 3986 percent-encoded string.
130  *
131  * @param bytes The GByteArray that will receive the bytes.  This
132  *        must be initialized by the caller.
133  * @param reserved_chars Normally the "gen-delims" and "sub-delims"
134  *        from RFC 3986 (":/?#[]@" and "!$&'()*+,;=" respectively)
135  *        plus space (hex value 20) are treated as reserved characters.
136  *        If this variable is non-NULL, its contents will be used
137  *        instead.
138  * @note Any non-printing character determined by isprint(), along
139  *       with the % character itself are always reserved.
140  * @see uri_str_to_bytes(),  format_text(), isprint()
141  */
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  * @return True if the string was converted successfully
150  */
151 gboolean   oid_str_to_bytes(const char *oid_str, GByteArray *bytes);
152
153 /**
154  * Create a copy of a GByteArray
155  *
156  * @param ba The byte array to be copied.
157  * @return If ba exists, a freshly allocated copy.  NULL otherwise.
158  *
159  * @todo - Should this be in strutil.c?
160  */
161 GByteArray *byte_array_dup(GByteArray *ba);
162
163 /**
164  * Compare the contents of two GByteArrays
165  *
166  * @param ba1 A byte array
167  * @param ba2 A byte array
168  * @return If both arrays are non-NULL and their lengths are equal and
169  *         their contents are equal, returns TRUE.  Otherwise, returns
170  *         FALSE.
171  *
172  * @todo - Should this be in strutil.c?
173  */
174 gboolean byte_array_equal(GByteArray *ba1, GByteArray *ba2);
175
176
177 /** Return a XML escaped representation of the unescaped string.
178  *  The returned string must be freed when no longer in use.
179  *
180  * @param unescaped The unescaped string
181  * @return An XML-escaped representation of the input string
182  */
183 gchar*     xml_escape(const gchar *unescaped);
184
185 /**
186  * Return the first occurrence of needle in haystack.
187  * Algorithm copied from GNU's glibc 2.3.2 memcmp()
188  *
189  * @param haystack The data to search
190  * @param haystack_len The length of the search data
191  * @param needle The string to look for
192  * @param needle_len The length of the search string
193  * @return A pointer to the first occurrence of "needle" in
194  *         "haystack".  If "needle" isn't found or is NULL, or if
195  *         "needle_len" is 0, NULL is returned.
196  */
197 const guint8 * epan_memmem(const guint8 *haystack, guint haystack_len,
198                 const guint8 *needle, guint needle_len);
199
200 /** Surround a string or a macro, resolved to a string, with double quotes */
201 #define _STRINGIFY(a)           # a
202 #define STRINGIFY(a)            _STRINGIFY(a)
203
204 /** Scan a string to make sure it's valid hex.
205  *
206  * @param string The string to validate
207  * @param nbytes The length of the return buffer
208  * @return A pointer to a buffer containing the converted raw bytes.  This
209  *         buffer must be g_free()d by the caller.
210  */
211 guint8 * convert_string_to_hex(const char *string, size_t *nbytes);
212
213 /** Prep a string for case-sensitive vs case-insensitive searching.
214  *
215  * @param string The search string
216  * @param case_insensitive TRUE if case-insensitive, FALSE if not
217  * @return A direct copy of the string if it's a case-sensitive search and
218  * an uppercased version if not.  In either case the string must be g_free()d
219  * by the caller.
220  */
221 char * convert_string_case(const char *string, gboolean case_insensitive);
222
223 /** Finds the first occurence of string 'needle' in string 'haystack'.
224  *  The matching is done in a case insensitive manner.
225  *
226  * @param haystack The string possibly containing the substring
227  * @param needle The substring to be searched
228  * @return A pointer into 'haystack' where 'needle' is first found.
229  *   Otherwise it returns NULL.
230  */
231 char * epan_strcasestr(const char *haystack, const char *needle);
232
233 /** Guarantee a non-null string.
234  *
235  * @param string The string to check
236  * @return A pointer 'string' if it's non-null, otherwise "[NULL]".
237  */
238 const char * string_or_null(const char *string);
239
240 int escape_string_len(const char *string);
241 char * escape_string(char *dst, const char *string);
242
243
244 void IA5_7BIT_decode(unsigned char * dest, const unsigned char* src, int len);
245
246 /** Copy a string, escaping the 'chr' characters in it
247  *
248  * @param str The string to be copied
249  * @param char The character to be escaped
250  * @return A copy of the string with every original 'chr' being
251  * transformed into double 'chr'.
252  */
253 gchar* ws_strdup_escape_char (const gchar *str, const gchar chr);
254
255 /** Copy a string, unescaping the 'chr' characters in it
256  *
257  * @param str The string to be copied
258  * @param char The character to be escaped
259  * @return A copy of the string with every occurence of double 'chr' in
260  * the original string being copied as a single 'chr'.
261  */
262 gchar* ws_strdup_unescape_char (const gchar *str, const gchar chr);
263
264 /** Replace values in a string
265  *
266  * @param str String containing 0 or more values to be replaced.
267  * @param old_val Old value.
268  * @param new_val New value. May be NULL, in which case occurences of
269  *                           old_value will be removed.
270  * @return A newly-allocated version of str with replacement values or
271  * NULL on failure.
272  */
273 gchar *string_replace(const gchar* str, const gchar *old_val, const gchar *new_val);
274
275 /**
276  * g_strcmp0 appears first in GLIB 2.16, define it locally for earlier versions. 
277  */
278
279 #if !GLIB_CHECK_VERSION(2,16,0)
280 int     g_strcmp0                       (const char     *str1,
281                                          const char     *str2);
282 #endif /* GLIB_CHECK_VERSION(2,16,0) */
283 #ifdef __cplusplus
284 }
285 #endif /* __cplusplus */
286
287 #endif /* __STRUTIL_H__ */