Wireshark 4.5.0
The Wireshark network protocol analyzer
Loading...
Searching...
No Matches
wmem_strbuf.h
Go to the documentation of this file.
1
12#ifndef __WMEM_STRBUF_H__
13#define __WMEM_STRBUF_H__
14
15#include <ws_codepoints.h>
16
17#include "wmem_core.h"
18
19#ifdef __cplusplus
20extern "C" {
21#endif /* __cplusplus */
22
32/* Holds a wmem-allocated string-buffer.
33 * len is the length of the string (not counting the null-terminator) and
34 * should be the same as strlen(str) unless the string contains embedded
35 * nulls.
36 * alloc_size is the size of the raw buffer pointed to by str, regardless of
37 * what string is actually being stored (i.e. the buffer contents)
38 * max_size is the maximum permitted alloc_size (NOT the maximum permitted len,
39 * which must be one shorter than alloc_size to permit null-termination).
40 * When max_size is 0 (the default), no maximum is enforced.
41 */
43 /* read-only fields */
44 wmem_allocator_t *allocator;
45 char *str;
46 size_t len;
47
48 /* private fields */
49 size_t alloc_size;
50};
51
52typedef struct _wmem_strbuf_t wmem_strbuf_t;
53
54WS_DLL_PUBLIC
56wmem_strbuf_new_sized(wmem_allocator_t *allocator, size_t alloc_size)
57G_GNUC_MALLOC;
58
59WS_DLL_PUBLIC
61wmem_strbuf_new(wmem_allocator_t *allocator, const char *str)
62G_GNUC_MALLOC;
63
64#define wmem_strbuf_create(allocator) \
65 wmem_strbuf_new(allocator, "")
66
67WS_DLL_PUBLIC
69wmem_strbuf_new_len(wmem_allocator_t *allocator, const char *str, size_t len)
70G_GNUC_MALLOC;
71
72WS_DLL_PUBLIC
74wmem_strbuf_dup(wmem_allocator_t *allocator, const wmem_strbuf_t *strbuf)
75G_GNUC_MALLOC;
76
77WS_DLL_PUBLIC
78void
79wmem_strbuf_append(wmem_strbuf_t *strbuf, const char *str);
80
81/* Appends up to append_len bytes (as allowed by strbuf->max_size) from
82 * str. Ensures that strbuf is null terminated afterwards but will copy
83 * embedded nulls. */
84WS_DLL_PUBLIC
85void
86wmem_strbuf_append_len(wmem_strbuf_t *strbuf, const char *str, size_t append_len);
87
88WS_DLL_PUBLIC
89void
90wmem_strbuf_append_printf(wmem_strbuf_t *strbuf, const char *format, ...)
91G_GNUC_PRINTF(2, 3);
92
93WS_DLL_PUBLIC
94void
95wmem_strbuf_append_vprintf(wmem_strbuf_t *strbuf, const char *fmt, va_list ap);
96
97WS_DLL_PUBLIC
98void
99wmem_strbuf_append_c(wmem_strbuf_t *strbuf, const char c);
100
101WS_DLL_PUBLIC
102void
103wmem_strbuf_append_c_count(wmem_strbuf_t *strbuf, const char c, size_t count);
104
105WS_DLL_PUBLIC
106void
107wmem_strbuf_append_unichar(wmem_strbuf_t *strbuf, const gunichar c);
108
109#define wmem_strbuf_append_unichar_repl(buf) \
110 wmem_strbuf_append_unichar(buf, UNICODE_REPLACEMENT_CHARACTER)
111
112/* As wmem_strbuf_append_unichar but appends a REPLACEMENT CHARACTER
113 * instead for any invalid Unicode codepoints.
114 */
115WS_DLL_PUBLIC
116void
117wmem_strbuf_append_unichar_validated(wmem_strbuf_t *strbuf, const gunichar c);
118
119WS_DLL_PUBLIC
120void
121wmem_strbuf_append_hex(wmem_strbuf_t *strbuf, uint8_t);
122
123/* Returns the number of characters written (4, 6 or 10). */
124WS_DLL_PUBLIC
125size_t
126wmem_strbuf_append_hex_unichar(wmem_strbuf_t *strbuf, gunichar);
127
128WS_DLL_PUBLIC
129void
130wmem_strbuf_truncate(wmem_strbuf_t *strbuf, const size_t len);
131
132WS_DLL_PUBLIC
133const char *
134wmem_strbuf_get_str(const wmem_strbuf_t *strbuf);
135
136WS_DLL_PUBLIC
137size_t
138wmem_strbuf_get_len(const wmem_strbuf_t *strbuf);
139
140WS_DLL_PUBLIC
141int
142wmem_strbuf_strcmp(const wmem_strbuf_t *sb1, const wmem_strbuf_t *sb2);
143
144WS_DLL_PUBLIC
145const char *
146wmem_strbuf_strstr(const wmem_strbuf_t *haystack, const wmem_strbuf_t *needle);
147
153WS_DLL_PUBLIC
154char *
156
157WS_DLL_PUBLIC
158void
159wmem_strbuf_destroy(wmem_strbuf_t *strbuf);
160
161/* Validates the string buffer as UTF-8.
162 * Unlike g_utf8_validate(), accepts embedded NUL bytes as valid UTF-8.
163 * If endpptr is non-NULL, then the end of the valid range is stored there
164 * (i.e. the first invalid character, or the end of the buffer otherwise).
165 */
166WS_DLL_PUBLIC
167bool
168wmem_strbuf_utf8_validate(wmem_strbuf_t *strbuf, const char **endptr);
169
170WS_DLL_PUBLIC
171void
172wmem_strbuf_utf8_make_valid(wmem_strbuf_t *strbuf);
173
177#ifdef __cplusplus
178}
179#endif /* __cplusplus */
180
181#endif /* __WMEM_STRBUF_H__ */
182
183/*
184 * Editor modelines - https://www.wireshark.org/tools/modelines.html
185 *
186 * Local variables:
187 * c-basic-offset: 4
188 * tab-width: 8
189 * indent-tabs-mode: nil
190 * End:
191 *
192 * vi: set shiftwidth=4 tabstop=8 expandtab:
193 * :indentSize=4:tabSize=8:noTabs=true:
194 */
WS_DLL_PUBLIC char * wmem_strbuf_finalize(wmem_strbuf_t *strbuf)
Definition wmem_strbuf.c:383
Definition wmem_allocator.h:27
Definition wmem_strbuf.h:42