2 * Definitions for the 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 #ifndef __WMEM_STRBUF_H__
13 #define __WMEM_STRBUF_H__
15 #include <ws_codepoints.h>
17 #include "wmem_core.h"
21 #endif /* __cplusplus */
25 * @defgroup wmem-strbuf String Buffer
27 * A string object implementation on top of wmem.
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
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.
42 struct _wmem_strbuf_t
{
43 /* read-only fields */
44 wmem_allocator_t
*allocator
;
52 typedef struct _wmem_strbuf_t wmem_strbuf_t
;
56 wmem_strbuf_new_sized(wmem_allocator_t
*allocator
, size_t alloc_size
)
61 wmem_strbuf_new(wmem_allocator_t
*allocator
, const char *str
)
64 #define wmem_strbuf_create(allocator) \
65 wmem_strbuf_new(allocator, "")
69 wmem_strbuf_new_len(wmem_allocator_t
*allocator
, const char *str
, size_t len
)
74 wmem_strbuf_dup(wmem_allocator_t
*allocator
, const wmem_strbuf_t
*strbuf
)
79 wmem_strbuf_append(wmem_strbuf_t
*strbuf
, const char *str
);
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
86 wmem_strbuf_append_len(wmem_strbuf_t
*strbuf
, const char *str
, size_t append_len
);
90 wmem_strbuf_append_printf(wmem_strbuf_t
*strbuf
, const char *format
, ...)
95 wmem_strbuf_append_vprintf(wmem_strbuf_t
*strbuf
, const char *fmt
, va_list ap
);
99 wmem_strbuf_append_c(wmem_strbuf_t
*strbuf
, const char c
);
103 wmem_strbuf_append_c_count(wmem_strbuf_t
*strbuf
, const char c
, size_t count
);
107 wmem_strbuf_append_unichar(wmem_strbuf_t
*strbuf
, const gunichar c
);
109 #define wmem_strbuf_append_unichar_repl(buf) \
110 wmem_strbuf_append_unichar(buf, UNICODE_REPLACEMENT_CHARACTER)
112 /* As wmem_strbuf_append_unichar but appends a REPLACEMENT CHARACTER
113 * instead for any invalid Unicode codepoints.
117 wmem_strbuf_append_unichar_validated(wmem_strbuf_t
*strbuf
, const gunichar c
);
121 wmem_strbuf_append_hex(wmem_strbuf_t
*strbuf
, uint8_t);
123 /* Returns the number of characters written (4, 6 or 10). */
126 wmem_strbuf_append_hex_unichar(wmem_strbuf_t
*strbuf
, gunichar
);
130 wmem_strbuf_truncate(wmem_strbuf_t
*strbuf
, const size_t len
);
134 wmem_strbuf_get_str(const wmem_strbuf_t
*strbuf
);
138 wmem_strbuf_get_len(const wmem_strbuf_t
*strbuf
);
142 wmem_strbuf_strcmp(const wmem_strbuf_t
*sb1
, const wmem_strbuf_t
*sb2
);
146 wmem_strbuf_strstr(const wmem_strbuf_t
*haystack
, const wmem_strbuf_t
*needle
);
148 /** Truncates the allocated memory down to the minimal amount, frees the header
149 * structure, and returns a non-const pointer to the raw string. The
150 * wmem_strbuf_t structure cannot be used after this is called. Basically a
151 * destructor for when you still need the underlying C-string.
155 wmem_strbuf_finalize(wmem_strbuf_t
*strbuf
);
159 wmem_strbuf_destroy(wmem_strbuf_t
*strbuf
);
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).
168 wmem_strbuf_utf8_validate(wmem_strbuf_t
*strbuf
, const char **endptr
);
172 wmem_strbuf_utf8_make_valid(wmem_strbuf_t
*strbuf
);
179 #endif /* __cplusplus */
181 #endif /* __WMEM_STRBUF_H__ */
184 * Editor modelines - https://www.wireshark.org/tools/modelines.html
189 * indent-tabs-mode: nil
192 * vi: set shiftwidth=4 tabstop=8 expandtab:
193 * :indentSize=4:tabSize=8:noTabs=true: