2 * String utility definitions
4 * Wireshark - Network traffic analyzer
5 * By Gerald Combs <gerald@wireshark.org>
6 * Copyright 1998 Gerald Combs
8 * SPDX-License-Identifier: GPL-2.0-or-later
14 #include "ws_symbol_export.h"
16 #include <epan/wmem_scopes.h>
17 #include <wsutil/str_util.h>
21 #endif /* __cplusplus */
24 * String handling and conversion utilities.
27 /** Given a pointer into a data buffer, and to the end of the buffer,
28 * find the end of the (putative) line at that position in the data
31 * @param data A pointer to the beginning of the data
32 * @param dataend A pointer to the end of the data
33 * @param eol A pointer that will receive the EOL location
34 * @return A pointer to the EOL character(s) in "*eol".
36 const unsigned char *find_line_end(const unsigned char *data
, const unsigned char *dataend
,
37 const unsigned char **eol
);
39 /** Get the length of the next token in a line, and the beginning of the
40 * next token after that (if any).
41 * @param linep A pointer to the beginning of the line
42 * @param lineend A pointer to the end of the line
43 * @param next_token Receives the location of the next token
44 * @return 0 if there is no next token.
47 int get_token_len(const unsigned char *linep
, const unsigned char *lineend
,
48 const unsigned char **next_token
);
50 /** Turn a string of hex digits with optional separators (defined by
51 * is_byte_sep() into a byte array.
53 * @param hex_str The string of hex digits.
54 * @param bytes The GByteArray that will receive the bytes. This
55 * must be initialized by the caller.
56 * @param force_separators If set to true, separators MUST exist between
58 * @return True if the string was converted successfully
61 bool hex_str_to_bytes(const char *hex_str
, GByteArray
*bytes
,
62 bool force_separators
);
64 /* Turn a string of hex digits with optional separators (defined by encoding)
65 * into a byte array. Unlike hex_str_to_bytes(), this will read as many hex-char
66 * pairs as possible and not error if it hits a non-hex-char; instead it just ends
67 * there. (i.e., like strtol()/atoi()/etc.) But it must see two hex chars at the
68 * beginning or it will return false.
70 * @param hex_str The string of hex digits.
71 * @param bytes The GByteArray that will receive the bytes. This
72 * must be initialized by the caller.
73 * @param endptr if not NULL, is set to the char after the last hex character consumed.
74 * @param encoding set to one or more bitwise-or'ed ENC_SEP_* (see proto.h)
75 * @param fail_if_partial If set to true, then the conversion fails if the whole
76 * hex_str is not consumed.
77 * @return false only if no bytes were generated; or if fail_if_partial is true
78 * and the entire hex_str was not consumed.
80 * If no ENC_SEP_* is set, then no separators are allowed. If multiple ENC_SEP_* are
81 * bit-or'ed, any of them can be a separator, but once the separator is seen then
82 * only its same type is accepted for the rest of the string. (i.e., it won't convert
83 * a "01:23-4567" even if ENC_SEP_COLON|ENC_SEP_DASH|ENC_SEP_NONE is passed in)
85 * This is done this way because it's likely a malformed scenario if they're mixed,
86 * and this routine is used by dissectors via tvb_get_string_XXX routines.
89 bool hex_str_to_bytes_encoding(const char *hex_str
, GByteArray
*bytes
, const char **endptr
,
90 const unsigned encoding
, const bool fail_if_partial
);
92 /** Turn an RFC 3986 percent-encoded array of characters, not necessarily
93 * null-terminated, into a byte array.
95 * @param uri_str The string of hex digits.
96 * @param bytes The GByteArray that will receive the bytes. This
97 * must be initialized by the caller.
98 * @return True if the string was converted successfully
101 bool uri_to_bytes(const char *uri_str
, GByteArray
*bytes
, size_t len
);
103 /** Turn an RFC 3986 percent-encoded string into a byte array.
105 * @param uri_str The string of hex digits.
106 * @param bytes The GByteArray that will receive the bytes. This
107 * must be initialized by the caller.
108 * @return True if the string was converted successfully
111 bool uri_str_to_bytes(const char *uri_str
, GByteArray
*bytes
);
113 /** Turn a OID string representation (dot notation) into a byte array.
115 * @param oid_str The OID string (dot notaion).
116 * @param bytes The GByteArray that will receive the bytes. This
117 * must be initialized by the caller.
118 * @param is_absolute True if this is an absolute OID; false for relative OID.
119 * @return True if the string was converted successfully
122 bool rel_oid_str_to_bytes(const char *oid_str
, GByteArray
*bytes
, bool is_absolute
);
124 /** Turn a OID string representation (dot notation) into a byte array.
126 * @param oid_str The OID string (dot notaion).
127 * @param bytes The GByteArray that will receive the bytes. This
128 * must be initialized by the caller.
129 * @return True if the string was converted successfully
132 bool oid_str_to_bytes(const char *oid_str
, GByteArray
*bytes
);
135 * Create a copy of a GByteArray
137 * @param ba The byte array to be copied.
138 * @return If ba exists, a freshly allocated copy. NULL otherwise.
140 * @todo - Should this be in strutil.c?
143 GByteArray
*byte_array_dup(const GByteArray
*ba
);
146 * Compare the contents of two GByteArrays
148 * @param ba1 A byte array
149 * @param ba2 A byte array
150 * @return If both arrays are non-NULL and their lengths are equal and
151 * their contents are equal, returns true. Otherwise, returns
154 * @todo - Should this be in strutil.c?
157 bool byte_array_equal(GByteArray
*ba1
, GByteArray
*ba2
);
160 /** Return a XML escaped representation of the unescaped string.
161 * The returned string must be freed when no longer in use.
163 * @param unescaped The unescaped string
164 * @return An XML-escaped representation of the input string
167 char* xml_escape(const char *unescaped
);
169 /** Scan a string to make sure it's valid hex.
171 * @param string The string to validate
172 * @param nbytes The length of the return buffer
173 * @return A pointer to a buffer containing the converted raw bytes. This
174 * buffer must be g_free()d by the caller.
177 uint8_t * convert_string_to_hex(const char *string
, size_t *nbytes
);
179 /** Prep a string for case-sensitive vs case-insensitive searching.
181 * @param string The search string
182 * @param case_insensitive true if case-insensitive, false if not
183 * @return A direct copy of the string if it's a case-sensitive search and
184 * an uppercased version if not. In either case the string must be g_free()d
188 char * convert_string_case(const char *string
, bool case_insensitive
);
191 void IA5_7BIT_decode(unsigned char * dest
, const unsigned char* src
, int len
);
193 #define FORMAT_LABEL_REPLACE_SPACE (0x1 << 0)
196 size_t ws_label_strcpy(char *label_str
, size_t bufsize
, size_t pos
, const uint8_t *str
, int flags
);
199 size_t ws_label_strcat(char *label_str
, size_t bufsize
, const uint8_t *str
, int flags
);
202 * Check name is valid. This covers names for display filter fields, dissector
203 * tables, preference modules, etc. Lower case is preferred.
205 WS_DLL_LOCAL
unsigned char
206 module_check_valid_name(const char *name
, bool lower_only
);
210 #endif /* __cplusplus */
212 #endif /* __STRUTIL_H__ */