CPMConnect: unify version handling
[wireshark-wip.git] / epan / strutil.h
blobf44351c0a51d559cfaeee764b8094845a3f02e4d
1 /* strutil.h
2 * String utility definitions
4 * $Id$
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.
25 #ifndef __STRUTIL_H__
26 #define __STRUTIL_H__
28 #include "ws_symbol_export.h"
30 #ifdef __cplusplus
31 extern "C" {
32 #endif /* __cplusplus */
34 /* ... thus, config.h needs to be #included */
36 /** @file
37 * String handling and conversion utilities.
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.
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".
49 const guchar *find_line_end(const guchar *data, const guchar *dataend,
50 const guchar **eol);
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.
59 WS_DLL_PUBLIC
60 int get_token_len(const guchar *linep, const guchar *lineend,
61 const guchar **next_token);
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.
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
70 * @see tvb_format_text()
72 WS_DLL_PUBLIC
73 gchar* format_text(const guchar *line, size_t len);
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.
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
86 WS_DLL_PUBLIC
87 gchar* format_text_wsp(const guchar *line, size_t len);
89 /**
90 * Given a string, generate a string from it that shows non-printable
91 * characters as the chr parameter passed, except a whitespace character
92 * (space, tab, carriage return, new line, vertical tab, or formfeed)
93 * which will be replaced by a space, and return a pointer to it.
95 * @param line A pointer to the input string
96 * @param len The length of the input string
97 * @param chr The character to use to replace non-printable characters
98 * @return A pointer to the formatted string
101 WS_DLL_PUBLIC
102 gchar* format_text_chr(const guchar *string, const size_t len, const guchar chr);
105 /** Turn a string of hex digits with optional separators (defined by
106 * is_byte_sep() into a byte array.
108 * @param hex_str The string of hex digits.
109 * @param bytes The GByteArray that will receive the bytes. This
110 * must be initialized by the caller.
111 * @param force_separators If set to TRUE, separators MUST exist between
112 * bytes.
113 * @return True if the string was converted successfully
115 WS_DLL_PUBLIC
116 gboolean hex_str_to_bytes(const char *hex_str, GByteArray *bytes,
117 gboolean force_separators);
119 /** Turn an RFC 3986 percent-encoded string into a byte array.
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()
127 WS_DLL_PUBLIC
128 gboolean uri_str_to_bytes(const char *uri_str, GByteArray *bytes);
130 /** Turn a byte array into an RFC 3986 percent-encoded string.
132 * @param bytes The GByteArray that will receive the bytes. This
133 * must be initialized by the caller.
134 * @param reserved_chars Normally the "gen-delims" and "sub-delims"
135 * from RFC 3986 (":/?#[]@" and "!$&'()*+,;=" respectively)
136 * plus space (hex value 20) are treated as reserved characters.
137 * If this variable is non-NULL, its contents will be used
138 * instead.
139 * @note Any non-printing character determined by isprint(), along
140 * with the % character itself are always reserved.
141 * @see uri_str_to_bytes(), format_text(), isprint()
143 WS_DLL_PUBLIC
144 const gchar* format_uri(const GByteArray *bytes, const gchar *reserved_chars);
146 /** Turn a OID string representation (dot notation) into a byte array.
148 * @param oid_str The OID string (dot notaion).
149 * @param bytes The GByteArray that will receive the bytes. This
150 * must be initialized by the caller.
151 * @param is_absolute True if this is an absolute OID; false for relative OID.
152 * @return True if the string was converted successfully
154 WS_DLL_PUBLIC
155 gboolean rel_oid_str_to_bytes(const char *oid_str, GByteArray *bytes, gboolean is_absolute);
157 /** Turn a OID string representation (dot notation) into a byte array.
159 * @param oid_str The OID string (dot notaion).
160 * @param bytes The GByteArray that will receive the bytes. This
161 * must be initialized by the caller.
162 * @return True if the string was converted successfully
164 WS_DLL_PUBLIC
165 gboolean oid_str_to_bytes(const char *oid_str, GByteArray *bytes);
168 * Create a copy of a GByteArray
170 * @param ba The byte array to be copied.
171 * @return If ba exists, a freshly allocated copy. NULL otherwise.
173 * @todo - Should this be in strutil.c?
175 WS_DLL_PUBLIC
176 GByteArray *byte_array_dup(GByteArray *ba);
179 * Compare the contents of two GByteArrays
181 * @param ba1 A byte array
182 * @param ba2 A byte array
183 * @return If both arrays are non-NULL and their lengths are equal and
184 * their contents are equal, returns TRUE. Otherwise, returns
185 * FALSE.
187 * @todo - Should this be in strutil.c?
189 WS_DLL_PUBLIC
190 gboolean byte_array_equal(GByteArray *ba1, GByteArray *ba2);
193 /** Return a XML escaped representation of the unescaped string.
194 * The returned string must be freed when no longer in use.
196 * @param unescaped The unescaped string
197 * @return An XML-escaped representation of the input string
199 WS_DLL_PUBLIC
200 gchar* xml_escape(const gchar *unescaped);
203 * Return the first occurrence of needle in haystack.
204 * Algorithm copied from GNU's glibc 2.3.2 memcmp()
206 * @param haystack The data to search
207 * @param haystack_len The length of the search data
208 * @param needle The string to look for
209 * @param needle_len The length of the search string
210 * @return A pointer to the first occurrence of "needle" in
211 * "haystack". If "needle" isn't found or is NULL, or if
212 * "needle_len" is 0, NULL is returned.
214 WS_DLL_PUBLIC
215 const guint8 * epan_memmem(const guint8 *haystack, guint haystack_len,
216 const guint8 *needle, guint needle_len);
218 /** Scan a string to make sure it's valid hex.
220 * @param string The string to validate
221 * @param nbytes The length of the return buffer
222 * @return A pointer to a buffer containing the converted raw bytes. This
223 * buffer must be g_free()d by the caller.
225 WS_DLL_PUBLIC
226 guint8 * convert_string_to_hex(const char *string, size_t *nbytes);
228 /** Prep a string for case-sensitive vs case-insensitive searching.
230 * @param string The search string
231 * @param case_insensitive TRUE if case-insensitive, FALSE if not
232 * @return A direct copy of the string if it's a case-sensitive search and
233 * an uppercased version if not. In either case the string must be g_free()d
234 * by the caller.
236 WS_DLL_PUBLIC
237 char * convert_string_case(const char *string, gboolean case_insensitive);
239 /** Finds the first occurrence of string 'needle' in string 'haystack'.
240 * The matching is done in a case insensitive manner.
242 * @param haystack The string possibly containing the substring
243 * @param needle The substring to be searched
244 * @return A pointer into 'haystack' where 'needle' is first found.
245 * Otherwise it returns NULL.
247 WS_DLL_PUBLIC
248 char * epan_strcasestr(const char *haystack, const char *needle);
250 /** Guarantee a non-null string.
252 * @param string The string to check
253 * @return A pointer 'string' if it's non-null, otherwise "[NULL]".
255 WS_DLL_PUBLIC
256 const char * string_or_null(const char *string);
258 WS_DLL_PUBLIC
259 int escape_string_len(const char *string);
260 WS_DLL_PUBLIC
261 char * escape_string(char *dst, const char *string);
264 WS_DLL_PUBLIC
265 void IA5_7BIT_decode(unsigned char * dest, const unsigned char* src, int len);
267 /** Copy a string, escaping the 'chr' characters in it
269 * @param str The string to be copied
270 * @param chr The character to be escaped
271 * @return A copy of the string with every original 'chr' being
272 * transformed into double 'chr'.
274 WS_DLL_PUBLIC
275 gchar* ws_strdup_escape_char (const gchar *str, const gchar chr);
277 /** Copy a string, unescaping the 'chr' characters in it
279 * @param str The string to be copied
280 * @param chr The character to be escaped
281 * @return A copy of the string with every occurrence of double 'chr' in
282 * the original string being copied as a single 'chr'.
284 WS_DLL_PUBLIC
285 gchar* ws_strdup_unescape_char (const gchar *str, const gchar chr);
287 /** Replace values in a string
289 * @param str String containing 0 or more values to be replaced.
290 * @param old_val Old value.
291 * @param new_val New value. May be NULL, in which case occurences of
292 * old_value will be removed.
293 * @return A newly-allocated version of str with replacement values or
294 * NULL on failure.
296 WS_DLL_PUBLIC
297 gchar *string_replace(const gchar* str, const gchar *old_val, const gchar *new_val);
299 #ifdef __cplusplus
301 #endif /* __cplusplus */
303 #endif /* __STRUTIL_H__ */