ISO14443: Fix Dead Store (Dead assignement/Dead increment) Warning found by Clang
[metze/wireshark/wip.git] / epan / strutil.h
1 /* strutil.h
2  * String utility definitions
3  *
4  * Wireshark - Network traffic analyzer
5  * By Gerald Combs <gerald@wireshark.org>
6  * Copyright 1998 Gerald Combs
7  *
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.
12  *
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.
17  *
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.
21  */
22
23 #ifndef __STRUTIL_H__
24 #define __STRUTIL_H__
25
26 #include "ws_symbol_export.h"
27
28 #ifdef __cplusplus
29 extern "C" {
30 #endif /* __cplusplus */
31
32 /** @file
33  * String handling and conversion utilities.
34  */
35
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
38  *  buffer.
39  *
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".
44  */
45 const guchar *find_line_end(const guchar *data, const guchar *dataend,
46     const guchar **eol);
47
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.
54  */
55 WS_DLL_PUBLIC
56 int        get_token_len(const guchar *linep, const guchar *lineend,
57     const guchar **next_token);
58
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.
61  *
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
65  *
66  * @see tvb_format_text()
67  */
68 WS_DLL_PUBLIC
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 WS_DLL_PUBLIC
83 gchar*     format_text_wsp(const guchar *line, size_t len);
84
85 /**
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.
90  *
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
95  *
96  */
97 WS_DLL_PUBLIC
98 gchar*     format_text_chr(const guchar *string, const size_t len, const guchar chr);
99
100
101 /** Turn a string of hex digits with optional separators (defined by
102  *  is_byte_sep() into a byte array.
103  *
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
108  *        bytes.
109  * @return True if the string was converted successfully
110  */
111 WS_DLL_PUBLIC
112 gboolean   hex_str_to_bytes(const char *hex_str, GByteArray *bytes,
113     gboolean force_separators);
114
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.
120  *
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.
130  *
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)
135  *
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.
138  */
139 WS_DLL_PUBLIC
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);
142
143 /** Turn an RFC 3986 percent-encoded string into a byte array.
144  *
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
149  * @see format_uri()
150  */
151 WS_DLL_PUBLIC
152 gboolean   uri_str_to_bytes(const char *uri_str, GByteArray *bytes);
153
154 /** Turn a byte array into an RFC 3986 percent-encoded string.
155  *
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
162  *        instead.
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()
166  */
167 WS_DLL_PUBLIC
168 const gchar* format_uri(const GByteArray *bytes, const gchar *reserved_chars);
169
170 /** Turn a OID string representation (dot notation) into a byte array.
171  *
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
177  */
178 WS_DLL_PUBLIC
179 gboolean   rel_oid_str_to_bytes(const char *oid_str, GByteArray *bytes, gboolean is_absolute);
180
181 /** Turn a OID string representation (dot notation) into a byte array.
182  *
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
187  */
188 WS_DLL_PUBLIC
189 gboolean   oid_str_to_bytes(const char *oid_str, GByteArray *bytes);
190
191 /**
192  * Create a copy of a GByteArray
193  *
194  * @param ba The byte array to be copied.
195  * @return If ba exists, a freshly allocated copy.  NULL otherwise.
196  *
197  * @todo - Should this be in strutil.c?
198  */
199 WS_DLL_PUBLIC
200 GByteArray *byte_array_dup(const GByteArray *ba);
201
202 /**
203  * Compare the contents of two GByteArrays
204  *
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
209  *         FALSE.
210  *
211  * @todo - Should this be in strutil.c?
212  */
213 WS_DLL_PUBLIC
214 gboolean byte_array_equal(GByteArray *ba1, GByteArray *ba2);
215
216
217 /** Return a XML escaped representation of the unescaped string.
218  *  The returned string must be freed when no longer in use.
219  *
220  * @param unescaped The unescaped string
221  * @return An XML-escaped representation of the input string
222  */
223 WS_DLL_PUBLIC
224 gchar*     xml_escape(const gchar *unescaped);
225
226 /**
227  * Return the first occurrence of needle in haystack.
228  * Algorithm copied from GNU's glibc 2.3.2 memcmp()
229  *
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.
237  */
238 WS_DLL_PUBLIC
239 const guint8 * epan_memmem(const guint8 *haystack, guint haystack_len,
240                 const guint8 *needle, guint needle_len);
241
242 /** Scan a string to make sure it's valid hex.
243  *
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.
248  */
249 WS_DLL_PUBLIC
250 guint8 * convert_string_to_hex(const char *string, size_t *nbytes);
251
252 /** Prep a string for case-sensitive vs case-insensitive searching.
253  *
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
258  * by the caller.
259  */
260 WS_DLL_PUBLIC
261 char * convert_string_case(const char *string, gboolean case_insensitive);
262
263 /** Finds the first occurrence of string 'needle' in string 'haystack'.
264  *  The matching is done in a case insensitive manner.
265  *
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.
270  */
271 WS_DLL_PUBLIC
272 char * epan_strcasestr(const char *haystack, const char *needle);
273
274 /** Guarantee a non-null string.
275  *
276  * @param string The string to check
277  * @return A pointer 'string' if it's non-null, otherwise "[NULL]".
278  */
279 WS_DLL_PUBLIC
280 const char * string_or_null(const char *string);
281
282 WS_DLL_PUBLIC
283 int escape_string_len(const char *string);
284 WS_DLL_PUBLIC
285 char * escape_string(char *dst, const char *string);
286
287
288 WS_DLL_PUBLIC
289 void IA5_7BIT_decode(unsigned char * dest, const unsigned char* src, int len);
290
291 /** Copy a string, escaping the 'chr' characters in it
292  *
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'.
297  */
298 WS_DLL_PUBLIC
299 gchar* ws_strdup_escape_char (const gchar *str, const gchar chr);
300
301 /** Copy a string, unescaping the 'chr' characters in it
302  *
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'.
307  */
308 WS_DLL_PUBLIC
309 gchar* ws_strdup_unescape_char (const gchar *str, const gchar chr);
310
311 /** Replace values in a string
312  *
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
318  * NULL on failure.
319  */
320 WS_DLL_PUBLIC
321 gchar *string_replace(const gchar* str, const gchar *old_val, const gchar *new_val);
322
323 #ifdef __cplusplus
324 }
325 #endif /* __cplusplus */
326
327 #endif /* __STRUTIL_H__ */