TODO epan/dissectors/asn1/kerberos/packet-kerberos-template.c new GSS flags
[wireshark-sm.git] / wsutil / wmem / wmem_strbuf.c
blobff49f6b8bab8f8a0e9684de9212a7d2a7d135e06
1 /* wmem_strbuf.c
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
12 #include "config.h"
13 #include "wmem_strbuf.h"
15 #include <stdio.h>
16 #include <errno.h>
18 #include "wmem-int.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)
30 wmem_strbuf_t *
31 wmem_strbuf_new_sized(wmem_allocator_t *allocator,
32 size_t alloc_size)
34 wmem_strbuf_t *strbuf;
36 strbuf = wmem_new(allocator, wmem_strbuf_t);
38 strbuf->allocator = allocator;
39 strbuf->len = 0;
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';
45 return strbuf;
48 wmem_strbuf_t *
49 wmem_strbuf_new_len(wmem_allocator_t *allocator, const char *str, size_t len)
51 wmem_strbuf_t *strbuf;
52 size_t alloc_size;
54 alloc_size = DEFAULT_MINIMUM_SIZE;
56 /* +1 for the null-terminator */
57 while (alloc_size < (len + 1)) {
58 alloc_size *= 2;
61 strbuf = wmem_strbuf_new_sized(allocator, alloc_size);
63 if (str && len > 0) {
64 ws_assert(strbuf->alloc_size >= len + 1);
65 memcpy(strbuf->str, str, len);
66 strbuf->str[len] = '\0';
67 strbuf->len = len;
70 return strbuf;
73 wmem_strbuf_t *
74 wmem_strbuf_new(wmem_allocator_t *allocator, const char *str)
76 return wmem_strbuf_new_len(allocator, str, str ? strlen(str) : 0);
79 wmem_strbuf_t *
80 wmem_strbuf_dup(wmem_allocator_t *allocator, const wmem_strbuf_t *src)
82 wmem_strbuf_t *new;
84 new = wmem_strbuf_new_sized(allocator, src->alloc_size);
85 new->len = src->len;
86 memcpy(new->str, src->str, new->len);
87 new->str[new->len] = '\0';
88 return new;
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 */
93 static inline void
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
100 * at a time.
102 if (WMEM_STRBUF_ROOM(strbuf) >= to_add) {
103 return;
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)) {
111 new_alloc_len *= 2;
114 if (new_alloc_len == strbuf->alloc_size) {
115 return;
118 strbuf->str = (char *)wmem_realloc(strbuf->allocator, strbuf->str, new_alloc_len);
120 strbuf->alloc_size = new_alloc_len;
123 void
124 wmem_strbuf_append(wmem_strbuf_t *strbuf, const char *str)
126 size_t append_len;
128 if (!str || str[0] == '\0') {
129 return;
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';
141 void
142 wmem_strbuf_append_len(wmem_strbuf_t *strbuf, const char *str, size_t append_len)
145 if (!append_len || !str) {
146 return;
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';
156 static inline
157 int _strbuf_vsnprintf(wmem_strbuf_t *strbuf, const char *format, va_list ap)
159 int want_len;
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);
164 if (want_len < 0) {
165 /* Error. */
166 g_warning("%s: vsnprintf: (%d) %s", G_STRFUNC, want_len, g_strerror(errno));
167 return -1;
169 if ((size_t)want_len < buffer_size) {
170 /* Success. */
171 strbuf->len += want_len;
172 return 0;
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. */
182 void
183 wmem_strbuf_append_vprintf(wmem_strbuf_t *strbuf, const char *fmt, va_list ap)
185 int want_len;
186 va_list ap2;
188 va_copy(ap2, ap);
189 /* Try to write buffer, check if output fits. */
190 want_len = _strbuf_vsnprintf(strbuf, fmt, ap2);
191 va_end(ap2);
192 if (want_len <= 0)
193 return;
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);
202 void
203 wmem_strbuf_append_printf(wmem_strbuf_t *strbuf, const char *format, ...)
205 va_list ap;
207 va_start(ap, format);
208 wmem_strbuf_append_vprintf(strbuf, format, ap);
209 va_end(ap);
212 void
213 wmem_strbuf_append_c(wmem_strbuf_t *strbuf, const char c)
215 wmem_strbuf_grow(strbuf, 1);
217 strbuf->str[strbuf->len] = c;
218 strbuf->len++;
219 strbuf->str[strbuf->len] = '\0';
222 void
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';
233 void
234 wmem_strbuf_append_unichar(wmem_strbuf_t *strbuf, const gunichar c)
236 char buf[6];
237 size_t charlen;
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';
248 void
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);
253 } else {
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
263 void
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
277 static inline
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
293 static inline
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';
311 size_t
312 wmem_strbuf_append_hex_unichar(wmem_strbuf_t *strbuf, gunichar ch)
314 if (ch <= 0x7f) {
315 wmem_strbuf_append_hex(strbuf, (uint8_t)ch);
316 return HEX_CODELEN;
318 if (ch <= 0xffff) {
319 append_hex_bmp(strbuf, ch);
320 return BMP_CODELEN;
322 append_hex_any(strbuf, ch);
323 return ANY_CODELEN;
326 void
327 wmem_strbuf_truncate(wmem_strbuf_t *strbuf, const size_t len)
329 if (len >= strbuf->len) {
330 return;
333 strbuf->str[len] = '\0';
334 strbuf->len = len;
337 const char *
338 wmem_strbuf_get_str(const wmem_strbuf_t *strbuf)
340 return strbuf->str;
343 size_t
344 wmem_strbuf_get_len(const wmem_strbuf_t *strbuf)
346 return strbuf->len;
349 static inline int
350 _memcmp_len(const void *s1, size_t s1_len, const void *s2, size_t s2_len)
352 size_t len;
353 int cmp;
355 len = MIN(s1_len, s2_len);
356 if ((cmp = memcmp(s1, s2, len)) != 0)
357 return cmp;
358 if (s1_len < s2_len)
359 return -1;
360 if (s1_len > s2_len)
361 return 1;
362 return 0;
365 WS_DLL_PUBLIC
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);
372 const char *
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.
382 char *
383 wmem_strbuf_finalize(wmem_strbuf_t *strbuf)
385 if (strbuf == NULL)
386 return NULL;
388 char *ret = (char *)wmem_realloc(strbuf->allocator, strbuf->str, strbuf->len+1);
390 wmem_free(strbuf->allocator, strbuf);
392 return ret;
395 void
396 wmem_strbuf_destroy(wmem_strbuf_t *strbuf)
398 if (strbuf == NULL)
399 return;
401 wmem_free(strbuf->allocator, strbuf->str);
402 wmem_free(strbuf->allocator, strbuf);
405 static bool
406 string_utf8_validate(const char *str, ssize_t max_len, const char **endpptr)
408 bool valid;
409 const char *endp;
411 if (max_len <= 0) {
412 if (endpptr) {
413 *endpptr = str;
415 return true;
418 valid = g_utf8_validate(str, max_len, &endp);
420 if (valid || *endp != '\0') {
421 if (endpptr) {
422 *endpptr = endp;
424 return valid;
427 /* Invalid because of a nul byte. Skip nuls and continue. */
428 max_len -= endp - str;
429 str = endp;
430 while (max_len > 0 && *str == '\0') {
431 str++;
432 max_len--;
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. */
440 bool
441 wmem_strbuf_utf8_validate(wmem_strbuf_t *strbuf, const char **endpptr)
443 return string_utf8_validate(strbuf->str, strbuf->len, endpptr);
446 void
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
462 * Local variables:
463 * c-basic-offset: 4
464 * tab-width: 8
465 * indent-tabs-mode: nil
466 * End:
468 * vi: set shiftwidth=4 tabstop=8 expandtab:
469 * :indentSize=4:tabSize=8:noTabs=true: