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 /** Turn an array of bytes into a string showing the bytes in hex.
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
73 * @see bytes_to_str_punct()
75 gchar* bytes_to_str(const guint8 *bd, int bd_len);
77 /** Turn an array of bytes into a string showing the bytes in hex,
78 * separated by a punctuation character.
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
87 gchar* bytes_to_str_punct(const guint8 *bd, int bd_len, gchar punct);
89 /** Turn a string of hex digits with optional separators (defined by
90 * is_byte_sep() into a byte array.
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
97 * @return True if the string was converted successfully
99 gboolean hex_str_to_bytes(const char *hex_str, GByteArray *bytes,
100 gboolean force_separators);
102 /** Turn a OID string representation (dot notaion) into a byte array.
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
109 gboolean oid_str_to_bytes(const char *oid_str, GByteArray *bytes);
111 /** Return a XML escaped representation of the unescaped string.
112 * The returned string must be freed when no longer in use.
114 * @param unescaped The unescaped string
115 * @return An XML-escaped representation of the input string
117 gchar* xml_escape(const gchar *unescaped);
119 /* Return the first occurrence of needle in haystack.
120 * Algorithm copied from GNU's glibc 2.3.2 memcmp()
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.
130 const guint8 * epan_memmem(const guint8 *haystack, guint haystack_len,
131 const guint8 *needle, guint needle_len);
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)
137 /** Scan a string to make sure it's valid hex.
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.
144 guint8 * convert_string_to_hex(const char *string, size_t *nbytes);
146 /** Prep a string for case-sensitive vs case-insensitive searching.
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
154 char * convert_string_case(const char *string, gboolean case_insensitive);
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);
161 /* g_ascii_isprint() does not exist in GLib 1.2[.x].
162 * assume all codes >=0x20 and <0x80 are ASCII printables.
164 #if GLIB_MAJOR_VERSION < 2
165 #define g_ascii_isprint(c) \
166 (((c<0x20)||(c>=0x80))?FALSE:TRUE)
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).
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.
178 wchar_t * utf_8to16(const char *utf8str);
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).
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.
187 gchar * utf_16to8(const wchar_t *utf16str);
191 #endif /* __STRUTIL_H__ */