2 * Wireshark Memory Manager String Buffer
3 * Copyright 2012, Evan Huus <eapache@gmail.com>
5 * Wireshark - Network traffic analyzer
6 * By Gerald Combs <gerald@wireshark.org>
7 * Copyright 1998 Gerald Combs
9 * SPDX-License-Identifier: GPL-2.0-or-later
13 #include "wmem_strbuf.h"
19 #include "wmem_strutl.h"
21 #include <wsutil/unicode-utils.h>
23 #define DEFAULT_MINIMUM_SIZE 16
25 /* _ROOM accounts for the null-terminator, _RAW_ROOM does not.
26 * Some functions need one, some functions need the other. */
27 #define WMEM_STRBUF_ROOM(S) ((S)->alloc_size - (S)->len - 1)
28 #define WMEM_STRBUF_RAW_ROOM(S) ((S)->alloc_size - (S)->len)
31 wmem_strbuf_new_sized(wmem_allocator_t
*allocator
,
34 wmem_strbuf_t
*strbuf
;
36 strbuf
= wmem_new(allocator
, wmem_strbuf_t
);
38 strbuf
->allocator
= allocator
;
40 strbuf
->alloc_size
= alloc_size
? alloc_size
: DEFAULT_MINIMUM_SIZE
;
42 strbuf
->str
= (char *)wmem_alloc(strbuf
->allocator
, strbuf
->alloc_size
);
43 strbuf
->str
[0] = '\0';
49 wmem_strbuf_new_len(wmem_allocator_t
*allocator
, const char *str
, size_t len
)
51 wmem_strbuf_t
*strbuf
;
54 alloc_size
= DEFAULT_MINIMUM_SIZE
;
56 /* +1 for the null-terminator */
57 while (alloc_size
< (len
+ 1)) {
61 strbuf
= wmem_strbuf_new_sized(allocator
, alloc_size
);
64 ws_assert(strbuf
->alloc_size
>= len
+ 1);
65 memcpy(strbuf
->str
, str
, len
);
66 strbuf
->str
[len
] = '\0';
74 wmem_strbuf_new(wmem_allocator_t
*allocator
, const char *str
)
76 return wmem_strbuf_new_len(allocator
, str
, str
? strlen(str
) : 0);
80 wmem_strbuf_dup(wmem_allocator_t
*allocator
, const wmem_strbuf_t
*src
)
84 new = wmem_strbuf_new_sized(allocator
, src
->alloc_size
);
86 memcpy(new->str
, src
->str
, new->len
);
87 new->str
[new->len
] = '\0';
91 /* grows the allocated size of the wmem_strbuf_t. If max_size is set, then
92 * not guaranteed to grow by the full amount to_add */
94 wmem_strbuf_grow(wmem_strbuf_t
*strbuf
, const size_t to_add
)
96 size_t new_alloc_len
, new_len
;
98 /* short-circuit for efficiency if we have room already; greatly speeds up
99 * repeated calls to wmem_strbuf_append_c and others which grow a little bit
102 if (WMEM_STRBUF_ROOM(strbuf
) >= to_add
) {
106 new_alloc_len
= strbuf
->alloc_size
;
107 new_len
= strbuf
->len
+ to_add
;
109 /* +1 for the null-terminator */
110 while (new_alloc_len
< (new_len
+ 1)) {
114 if (new_alloc_len
== strbuf
->alloc_size
) {
118 strbuf
->str
= (char *)wmem_realloc(strbuf
->allocator
, strbuf
->str
, new_alloc_len
);
120 strbuf
->alloc_size
= new_alloc_len
;
124 wmem_strbuf_append(wmem_strbuf_t
*strbuf
, const char *str
)
128 if (!str
|| str
[0] == '\0') {
132 append_len
= strlen(str
);
133 wmem_strbuf_grow(strbuf
, append_len
);
135 ws_assert(WMEM_STRBUF_RAW_ROOM(strbuf
) >= append_len
+ 1);
136 memcpy(&strbuf
->str
[strbuf
->len
], str
, append_len
);
137 strbuf
->len
+= append_len
;
138 strbuf
->str
[strbuf
->len
] = '\0';
142 wmem_strbuf_append_len(wmem_strbuf_t
*strbuf
, const char *str
, size_t append_len
)
145 if (!append_len
|| !str
) {
149 wmem_strbuf_grow(strbuf
, append_len
);
151 memcpy(&strbuf
->str
[strbuf
->len
], str
, append_len
);
152 strbuf
->len
+= append_len
;
153 strbuf
->str
[strbuf
->len
] = '\0';
157 int _strbuf_vsnprintf(wmem_strbuf_t
*strbuf
, const char *format
, va_list ap
)
160 char *buffer
= &strbuf
->str
[strbuf
->len
];
161 size_t buffer_size
= WMEM_STRBUF_RAW_ROOM(strbuf
);
163 want_len
= vsnprintf(buffer
, buffer_size
, format
, ap
);
166 g_warning("%s: vsnprintf: (%d) %s", G_STRFUNC
, want_len
, g_strerror(errno
));
169 if ((size_t)want_len
< buffer_size
) {
171 strbuf
->len
+= want_len
;
175 /* Not enough space in buffer, output was truncated. */
176 strbuf
->str
[strbuf
->len
] = '\0'; /* Reset. */
178 return want_len
; /* Length (not including terminating null) that would be written
179 if there was enough space in buffer. */
183 wmem_strbuf_append_vprintf(wmem_strbuf_t
*strbuf
, const char *fmt
, va_list ap
)
189 /* Try to write buffer, check if output fits. */
190 want_len
= _strbuf_vsnprintf(strbuf
, fmt
, ap2
);
195 /* Resize buffer and try again. */
196 wmem_strbuf_grow(strbuf
, want_len
);
197 want_len
= _strbuf_vsnprintf(strbuf
, fmt
, ap
);
198 /* Second time must succeed or error out. */
199 ws_assert(want_len
<= 0);
203 wmem_strbuf_append_printf(wmem_strbuf_t
*strbuf
, const char *format
, ...)
207 va_start(ap
, format
);
208 wmem_strbuf_append_vprintf(strbuf
, format
, ap
);
213 wmem_strbuf_append_c(wmem_strbuf_t
*strbuf
, const char c
)
215 wmem_strbuf_grow(strbuf
, 1);
217 strbuf
->str
[strbuf
->len
] = c
;
219 strbuf
->str
[strbuf
->len
] = '\0';
223 wmem_strbuf_append_c_count(wmem_strbuf_t
*strbuf
, const char c
, size_t count
)
225 wmem_strbuf_grow(strbuf
, count
);
227 while (count
-- > 0) {
228 strbuf
->str
[strbuf
->len
++] = c
;
230 strbuf
->str
[strbuf
->len
] = '\0';
234 wmem_strbuf_append_unichar(wmem_strbuf_t
*strbuf
, const gunichar c
)
239 charlen
= g_unichar_to_utf8(c
, buf
);
241 wmem_strbuf_grow(strbuf
, charlen
);
243 memcpy(&strbuf
->str
[strbuf
->len
], buf
, charlen
);
244 strbuf
->len
+= charlen
;
245 strbuf
->str
[strbuf
->len
] = '\0';
249 wmem_strbuf_append_unichar_validated(wmem_strbuf_t
*strbuf
, const gunichar c
)
251 if (g_unichar_validate(c
)) {
252 wmem_strbuf_append_unichar(strbuf
, c
);
254 wmem_strbuf_append_unichar(strbuf
, UNICODE_REPLACEMENT_CHARACTER
);
258 static const char hex
[16] = { '0', '1', '2', '3', '4', '5', '6', '7',
259 '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
261 #define HEX_CODELEN 4
264 wmem_strbuf_append_hex(wmem_strbuf_t
*strbuf
, uint8_t ch
)
266 wmem_strbuf_grow(strbuf
, HEX_CODELEN
* 1);
268 strbuf
->str
[strbuf
->len
++] = '\\';
269 strbuf
->str
[strbuf
->len
++] = 'x';
270 strbuf
->str
[strbuf
->len
++] = hex
[(ch
>> 4) & 0xF];
271 strbuf
->str
[strbuf
->len
++] = hex
[(ch
>> 0) & 0xF];
272 strbuf
->str
[strbuf
->len
] = '\0';
275 #define BMP_CODELEN 6
278 void append_hex_bmp(wmem_strbuf_t
*strbuf
, gunichar ch
)
280 wmem_strbuf_grow(strbuf
, BMP_CODELEN
* 1);
282 strbuf
->str
[strbuf
->len
++] = '\\';
283 strbuf
->str
[strbuf
->len
++] = 'u';
284 strbuf
->str
[strbuf
->len
++] = hex
[(ch
>> 12) & 0xF];
285 strbuf
->str
[strbuf
->len
++] = hex
[(ch
>> 8) & 0xF];
286 strbuf
->str
[strbuf
->len
++] = hex
[(ch
>> 4) & 0xF];
287 strbuf
->str
[strbuf
->len
++] = hex
[(ch
>> 0) & 0xF];
288 strbuf
->str
[strbuf
->len
] = '\0';
291 #define ANY_CODELEN 10
294 void append_hex_any(wmem_strbuf_t
*strbuf
, gunichar ch
)
296 wmem_strbuf_grow(strbuf
, ANY_CODELEN
* 1);
298 strbuf
->str
[strbuf
->len
++] = '\\';
299 strbuf
->str
[strbuf
->len
++] = 'U';
300 strbuf
->str
[strbuf
->len
++] = hex
[(ch
>> 28) & 0xF];
301 strbuf
->str
[strbuf
->len
++] = hex
[(ch
>> 24) & 0xF];
302 strbuf
->str
[strbuf
->len
++] = hex
[(ch
>> 20) & 0xF];
303 strbuf
->str
[strbuf
->len
++] = hex
[(ch
>> 16) & 0xF];
304 strbuf
->str
[strbuf
->len
++] = hex
[(ch
>> 12) & 0xF];
305 strbuf
->str
[strbuf
->len
++] = hex
[(ch
>> 8) & 0xF];
306 strbuf
->str
[strbuf
->len
++] = hex
[(ch
>> 4) & 0xF];
307 strbuf
->str
[strbuf
->len
++] = hex
[(ch
>> 0) & 0xF];
308 strbuf
->str
[strbuf
->len
] = '\0';
312 wmem_strbuf_append_hex_unichar(wmem_strbuf_t
*strbuf
, gunichar ch
)
315 wmem_strbuf_append_hex(strbuf
, (uint8_t)ch
);
319 append_hex_bmp(strbuf
, ch
);
322 append_hex_any(strbuf
, ch
);
327 wmem_strbuf_truncate(wmem_strbuf_t
*strbuf
, const size_t len
)
329 if (len
>= strbuf
->len
) {
333 strbuf
->str
[len
] = '\0';
338 wmem_strbuf_get_str(const wmem_strbuf_t
*strbuf
)
344 wmem_strbuf_get_len(const wmem_strbuf_t
*strbuf
)
350 _memcmp_len(const void *s1
, size_t s1_len
, const void *s2
, size_t s2_len
)
355 len
= MIN(s1_len
, s2_len
);
356 if ((cmp
= memcmp(s1
, s2
, len
)) != 0)
367 wmem_strbuf_strcmp(const wmem_strbuf_t
*sb1
, const wmem_strbuf_t
*sb2
)
369 return _memcmp_len(sb1
->str
, sb1
->len
, sb2
->str
, sb2
->len
);
373 wmem_strbuf_strstr(const wmem_strbuf_t
*haystack
, const wmem_strbuf_t
*needle
)
375 return ws_memmem(haystack
->str
, haystack
->len
, needle
->str
, needle
->len
);
378 /* Truncates the allocated memory down to the minimal amount, frees the header
379 * structure, and returns a non-const pointer to the raw string. The
380 * wmem_strbuf_t structure cannot be used after this is called.
383 wmem_strbuf_finalize(wmem_strbuf_t
*strbuf
)
388 char *ret
= (char *)wmem_realloc(strbuf
->allocator
, strbuf
->str
, strbuf
->len
+1);
390 wmem_free(strbuf
->allocator
, strbuf
);
396 wmem_strbuf_destroy(wmem_strbuf_t
*strbuf
)
401 wmem_free(strbuf
->allocator
, strbuf
->str
);
402 wmem_free(strbuf
->allocator
, strbuf
);
406 string_utf8_validate(const char *str
, ssize_t max_len
, const char **endpptr
)
418 valid
= g_utf8_validate(str
, max_len
, &endp
);
420 if (valid
|| *endp
!= '\0') {
427 /* Invalid because of a nul byte. Skip nuls and continue. */
428 max_len
-= endp
- str
;
430 while (max_len
> 0 && *str
== '\0') {
434 return string_utf8_validate(str
, max_len
, endpptr
);
437 /* g_utf8_validate() returns false in the string contains embedded NUL
438 * bytes. We accept \x00 as valid and work around that to validate the
439 * entire len bytes. */
441 wmem_strbuf_utf8_validate(wmem_strbuf_t
*strbuf
, const char **endpptr
)
443 return string_utf8_validate(strbuf
->str
, strbuf
->len
, endpptr
);
447 wmem_strbuf_utf8_make_valid(wmem_strbuf_t
*strbuf
)
449 wmem_strbuf_t
*tmp
= ws_utf8_make_valid_strbuf(strbuf
->allocator
, strbuf
->str
, strbuf
->len
);
451 wmem_free(strbuf
->allocator
, strbuf
->str
);
452 strbuf
->str
= tmp
->str
;
453 strbuf
->len
= tmp
->len
;
454 strbuf
->alloc_size
= tmp
->alloc_size
;
456 wmem_free(strbuf
->allocator
, tmp
);
460 * Editor modelines - https://www.wireshark.org/tools/modelines.html
465 * indent-tabs-mode: nil
468 * vi: set shiftwidth=4 tabstop=8 expandtab:
469 * :indentSize=4:tabSize=8:noTabs=true: