name change
[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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
23  */
24
25 #ifndef __STRUTIL_H__
26 #define __STRUTIL_H__
27
28 /* ... thus, config.h needs to be #included */
29
30 /** @file
31  * String handling and conversion utilities.
32  */
33
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
36  *  buffer.
37  *
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".
42  */
43 const guchar *find_line_end(const guchar *data, const guchar *dataend,
44     const guchar **eol);
45
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.
52  */
53 int        get_token_len(const guchar *linep, const guchar *lineend,
54     const guchar **next_token);
55
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.
58  *
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
62  *
63  * @see tvb_format_text()
64  */
65 gchar*     format_text(const guchar *line, int len);
66
67 /** Turn an array of bytes into a string showing the bytes in hex.
68  *
69  * @param bd A pointer to the byte array
70  * @param bd_len The length of the byte array
71  * @return A pointer to the formatted string
72  *
73  * @see bytes_to_str_punct()
74  */
75 gchar*     bytes_to_str(const guint8 *bd, int bd_len);
76
77 /** Turn an array of bytes into a string showing the bytes in hex,
78  *  separated by a punctuation character.
79  *
80  * @param bd A pointer to the byte array
81  * @param bd_len The length of the byte array
82  * @param punct The punctuation character
83  * @return A pointer to the formatted string
84  *
85  * @see bytes_to_str()
86  */
87 gchar*     bytes_to_str_punct(const guint8 *bd, int bd_len, gchar punct);
88
89 /** Turn a string of hex digits with optional separators (defined by
90  *  is_byte_sep() into a byte array.
91  *
92  * @param hex_str The string of hex digits.
93  * @param bytes The GByteArray that will receive the bytes.  This
94  *        must be initialized by the caller.
95  * @param force_separators If set to TRUE, separators MUST exist between 
96  *        bytes.
97  * @return True if the string was converted successfully
98  */
99 gboolean   hex_str_to_bytes(const char *hex_str, GByteArray *bytes,
100     gboolean force_separators);
101
102 /** Turn a OID string representation (dot notaion) into a byte array.
103  *
104  * @param oid_str The OID string (dot notaion).
105  * @param bytes The GByteArray that will receive the bytes.  This
106  *        must be initialized by the caller.
107  * @return True if the string was converted successfully
108  */
109 gboolean   oid_str_to_bytes(const char *oid_str, GByteArray *bytes);
110
111 /** Return a XML escaped representation of the unescaped string.
112  *  The returned string must be freed when no longer in use. 
113  *
114  * @param unescaped The unescaped string
115  * @return An XML-escaped representation of the input string
116  */
117 gchar*     xml_escape(const gchar *unescaped);
118
119 /* Return the first occurrence of needle in haystack.
120  * Algorithm copied from GNU's glibc 2.3.2 memcmp()
121  *
122  * @param haystack The data to search
123  * @param haystack_len The length of the search data
124  * @param needle The string to look for
125  * @param needle_len The length of the search string
126  * @return A pointer to the first occurrence of "needle" in
127  *         "haystack".  If "needle" isn't found or is NULL, or if
128  *         "needle_len" is 0, NULL is returned.
129  */
130 const guint8 * epan_memmem(const guint8 *haystack, guint haystack_len,
131                 const guint8 *needle, guint needle_len);
132
133 /* Surround a string or a macro, resolved to a string, with double quotes */
134 #define _STRINGIFY(a)           # a
135 #define STRINGIFY(a)            _STRINGIFY(a)
136
137 /** Scan a string to make sure it's valid hex.
138  *
139  * @param string The string to validate
140  * @param nbytes The length of the return buffer
141  * @return A pointer to a buffer containing the converted raw bytes.  This
142  *         buffer must be g_free()d by the caller.
143  */
144 guint8 * convert_string_to_hex(const char *string, size_t *nbytes);
145
146 /** Prep a string for case-sensitive vs case-insensitive searching.
147  *
148  * @param string The search string
149  * @param case_insensitive TRUE if case-insensitive, FALSE if not
150  * @return A direct copy of the string if it's a case-sensitive search and
151  * an uppercased version if not.  In either case the string must be g_free()d
152  * by the caller.
153  */
154 char * convert_string_case(const char *string, gboolean case_insensitive);
155
156 /* g_strlcat() does not exist in GLib 1.2[.x] */
157 #if GLIB_MAJOR_VERSION < 2
158 gsize g_strlcat(gchar *dst, gchar *src, gsize size);
159 #endif
160
161 /* g_ascii_isprint() does not exist in GLib 1.2[.x].
162  * assume all codes >=0x20 and <0x80 are ASCII printables.
163  */
164 #if GLIB_MAJOR_VERSION < 2
165 #define g_ascii_isprint(c)                                              \
166         (((c<0x20)||(c>=0x80))?FALSE:TRUE)
167 #endif
168
169 #ifdef _WIN32
170
171 /** Given a UTF-8 string, convert it to UTF-16.  This is meant to be used
172  * to convert between GTK+ 2.x (UTF-8) to Windows (UTF-16).
173  *
174  * @param utf8str The string to convert.  May be NULL.
175  * @return The string converted to UTF-16.  If utf8str is NULL, returns
176  * NULL.  The return value should NOT be freed by the caller.
177  */
178 wchar_t * utf_8to16(const char *utf8str);
179
180 /** Given a UTF-16 string, convert it to UTF-8.  This is meant to be used
181  * to convert between GTK+ 2.x (UTF-8) to Windows (UTF-16).
182  *
183  * @param utf16str The string to convert.  May be NULL.
184  * @return The string converted to UTF-8.  If utf16str is NULL, returns
185  * NULL.  The return value should NOT be freed by the caller.
186  */
187 gchar * utf_16to8(const wchar_t *utf16str);
188
189 #endif /* _WIN32 */
190
191 #endif /* __STRUTIL_H__ */