2 * String utility definitions
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
28 /* ... thus, config.h needs to be #included */
31 * String handling and conversion utilities.
34 /** Given a pointer into a data buffer, and to the end of the buffer,
35 * find the end of the (putative) line at that position in the data
38 * @param data A pointer to the beginning of the data
39 * @param dataend A pointer to the end of the data
40 * @param eol A pointer that will receive the EOL location
41 * @return A pointer to the EOL character(s) in "*eol".
43 const guchar *find_line_end(const guchar *data, const guchar *dataend,
46 /** Get the length of the next token in a line, and the beginning of the
47 * next token after that (if any).
48 * @param linep A pointer to the beginning of the line
49 * @param lineend A pointer to the end of the line
50 * @param next_token Receives the location of the next token
51 * @return 0 if there is no next token.
53 int get_token_len(const guchar *linep, const guchar *lineend,
54 const guchar **next_token);
56 /** Given a string, generate a string from it that shows non-printable
57 * characters as C-style escapes, and return a pointer to it.
59 * @param line A pointer to the input string
60 * @param len The length of the input string
61 * @return A pointer to the formatted string
63 * @see tvb_format_text()
65 gchar* format_text(const guchar *line, int len);
67 gchar* format_text_wsp(const guchar *line, int len);
69 /** Turn an array of bytes into a string showing the bytes in hex.
71 * @param bd A pointer to the byte array
72 * @param bd_len The length of the byte array
73 * @return A pointer to the formatted string
75 * @see bytes_to_str_punct()
77 gchar* bytes_to_str(const guint8 *bd, int bd_len);
79 /** Turn an array of bytes into a string showing the bytes in hex,
80 * separated by a punctuation character.
82 * @param bd A pointer to the byte array
83 * @param bd_len The length of the byte array
84 * @param punct The punctuation character
85 * @return A pointer to the formatted string
89 gchar* bytes_to_str_punct(const guint8 *bd, int bd_len, gchar punct);
91 /** Turn a string of hex digits with optional separators (defined by
92 * is_byte_sep() into a byte array.
94 * @param hex_str The string of hex digits.
95 * @param bytes The GByteArray that will receive the bytes. This
96 * must be initialized by the caller.
97 * @param force_separators If set to TRUE, separators MUST exist between
99 * @return True if the string was converted successfully
101 gboolean hex_str_to_bytes(const char *hex_str, GByteArray *bytes,
102 gboolean force_separators);
104 /** Turn an RFC 3986 percent-encoded string into a byte array.
106 * @param uri_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 * @return True if the string was converted successfully
112 gboolean uri_str_to_bytes(const char *uri_str, GByteArray *bytes);
114 /** Turn a byte array into an RFC 3986 percent-encoded string.
116 * @param bytes The GByteArray that will receive the bytes. This
117 * must be initialized by the caller.
118 * @param reserved_chars Normally the "gen-delims" and "sub-delims"
119 * from RFC 3986 (":/?#[]@" and "!$&'()*+,;=" respectively)
120 * plus space (hex value 20) are treated as reserved characters.
121 * If this variable is non-NULL, its contents will be used
123 * @note Any non-printing character determined by isprint(), along
124 * with the % character itself are always reserved.
125 * @see uri_str_to_bytes(), format_text(), isprint()
127 gchar* format_uri(const GByteArray *bytes, const gchar *reserved_chars);
129 /** Turn a OID string representation (dot notation) into a byte array.
131 * @param oid_str The OID string (dot notaion).
132 * @param bytes The GByteArray that will receive the bytes. This
133 * must be initialized by the caller.
134 * @return True if the string was converted successfully
136 gboolean oid_str_to_bytes(const char *oid_str, GByteArray *bytes);
139 * Create a copy of a GByteArray
141 * @param ba The byte array to be copied.
142 * @return If ba exists, a freshly allocated copy. NULL otherwise.
144 * XXX - Should this be in strutil.c?
146 GByteArray *byte_array_dup(GByteArray *ba);
149 * Compare the contents of two GByteArrays
151 * @param ba1 A byte array
152 * @param ba2 A byte array
153 * @return If both arrays are non-NULL and their lengths are equal and
154 * their contents are equal, returns TRUE. Otherwise, returns
157 * XXX - Should this be in strutil.c?
159 gboolean byte_array_equal(GByteArray *ba1, GByteArray *ba2);
162 /** Return a XML escaped representation of the unescaped string.
163 * The returned string must be freed when no longer in use.
165 * @param unescaped The unescaped string
166 * @return An XML-escaped representation of the input string
168 gchar* xml_escape(const gchar *unescaped);
171 * Return the first occurrence of needle in haystack.
172 * Algorithm copied from GNU's glibc 2.3.2 memcmp()
174 * @param haystack The data to search
175 * @param haystack_len The length of the search data
176 * @param needle The string to look for
177 * @param needle_len The length of the search string
178 * @return A pointer to the first occurrence of "needle" in
179 * "haystack". If "needle" isn't found or is NULL, or if
180 * "needle_len" is 0, NULL is returned.
182 const guint8 * epan_memmem(const guint8 *haystack, guint haystack_len,
183 const guint8 *needle, guint needle_len);
185 /* Surround a string or a macro, resolved to a string, with double quotes */
186 #define _STRINGIFY(a) # a
187 #define STRINGIFY(a) _STRINGIFY(a)
189 /** Scan a string to make sure it's valid hex.
191 * @param string The string to validate
192 * @param nbytes The length of the return buffer
193 * @return A pointer to a buffer containing the converted raw bytes. This
194 * buffer must be g_free()d by the caller.
196 guint8 * convert_string_to_hex(const char *string, size_t *nbytes);
198 /** Prep a string for case-sensitive vs case-insensitive searching.
200 * @param string The search string
201 * @param case_insensitive TRUE if case-insensitive, FALSE if not
202 * @return A direct copy of the string if it's a case-sensitive search and
203 * an uppercased version if not. In either case the string must be g_free()d
206 char * convert_string_case(const char *string, gboolean case_insensitive);
208 /* g_strlcat() does not exist in GLib 1.2[.x] */
209 #if GLIB_MAJOR_VERSION < 2
210 gsize g_strlcat(gchar *dst, gchar *src, gsize size);
213 #if GLIB_MAJOR_VERSION < 2
214 /* g_ascii_isprint() does not exist in GLib 1.2[.x].
215 * assume all codes >=0x20 and <0x80 are ASCII printables.
217 #define g_ascii_isprint(c) \
218 (((c<0x20)||(c>=0x80))?FALSE:TRUE)
219 /* g_ascii_isxdigit() does not exist in Glib 1.2 */
220 #define g_ascii_isxdigit(c) \
221 ( ((c>='0')&&(c<='9'))?TRUE: \
222 ( ((c>='a')&&(c<='f'))?TRUE: \
223 (((c>='A')&&(c<='F'))?TRUE:FALSE) ) )
227 #endif /* __STRUTIL_H__ */