2012-12-15 00:46:36 +00:00
|
|
|
/* wmem_strbuf.c
|
|
|
|
* Wireshark Memory Manager String Buffer
|
|
|
|
* Copyright 2012, Evan Huus <eapache@gmail.com>
|
|
|
|
*
|
|
|
|
* Wireshark - Network traffic analyzer
|
|
|
|
* By Gerald Combs <gerald@wireshark.org>
|
|
|
|
* Copyright 1998 Gerald Combs
|
|
|
|
*
|
2018-02-08 16:31:35 +00:00
|
|
|
* SPDX-License-Identifier: GPL-2.0-or-later
|
2012-12-15 00:46:36 +00:00
|
|
|
*/
|
|
|
|
|
2013-08-14 00:31:14 +00:00
|
|
|
#include "config.h"
|
|
|
|
|
2012-12-15 00:46:36 +00:00
|
|
|
#include <string.h>
|
2021-03-19 18:18:51 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <errno.h>
|
2012-12-15 00:46:36 +00:00
|
|
|
#include <glib.h>
|
|
|
|
|
|
|
|
#include "wmem_core.h"
|
|
|
|
#include "wmem_strbuf.h"
|
|
|
|
|
2021-03-20 13:13:27 +00:00
|
|
|
#include <wsutil/ws_assert.h>
|
|
|
|
|
2012-12-15 00:46:36 +00:00
|
|
|
#define DEFAULT_MINIMUM_LEN 16
|
|
|
|
|
|
|
|
/* Holds a wmem-allocated string-buffer.
|
2013-04-21 23:19:37 +00:00
|
|
|
* len is the length of the string (not counting the null-terminator) and
|
|
|
|
* should be the same as strlen(str) unless the string contains embedded
|
|
|
|
* nulls.
|
2012-12-15 00:46:36 +00:00
|
|
|
* alloc_len is the length of the raw buffer pointed to by str, regardless of
|
|
|
|
* what string is actually being stored (i.e. the buffer contents)
|
|
|
|
* max_len is the maximum permitted alloc_len (NOT the maximum permitted len,
|
|
|
|
* which must be one shorter than alloc_len to permit null-termination).
|
|
|
|
* When max_len is 0 (the default), no maximum is enforced.
|
|
|
|
*/
|
|
|
|
struct _wmem_strbuf_t {
|
|
|
|
wmem_allocator_t *allocator;
|
|
|
|
|
|
|
|
gchar *str;
|
|
|
|
|
|
|
|
gsize len;
|
|
|
|
gsize alloc_len;
|
|
|
|
gsize max_len;
|
|
|
|
};
|
|
|
|
|
2013-04-26 21:30:24 +00:00
|
|
|
/* _ROOM accounts for the null-terminator, _RAW_ROOM does not.
|
|
|
|
* Some functions need one, some functions need the other. */
|
|
|
|
#define WMEM_STRBUF_ROOM(S) ((S)->alloc_len - (S)->len - 1)
|
|
|
|
#define WMEM_STRBUF_RAW_ROOM(S) ((S)->alloc_len - (S)->len)
|
|
|
|
|
2012-12-15 00:46:36 +00:00
|
|
|
wmem_strbuf_t *
|
|
|
|
wmem_strbuf_sized_new(wmem_allocator_t *allocator,
|
|
|
|
gsize alloc_len, gsize max_len)
|
|
|
|
{
|
|
|
|
wmem_strbuf_t *strbuf;
|
|
|
|
|
2021-03-20 13:13:27 +00:00
|
|
|
ws_assert((max_len == 0) || (alloc_len <= max_len));
|
2012-12-15 00:46:36 +00:00
|
|
|
|
2013-07-06 04:08:07 +00:00
|
|
|
strbuf = wmem_new(allocator, wmem_strbuf_t);
|
2012-12-15 00:46:36 +00:00
|
|
|
|
|
|
|
strbuf->allocator = allocator;
|
|
|
|
strbuf->len = 0;
|
|
|
|
strbuf->alloc_len = alloc_len ? alloc_len : DEFAULT_MINIMUM_LEN;
|
|
|
|
strbuf->max_len = max_len;
|
|
|
|
|
2013-03-20 01:18:10 +00:00
|
|
|
strbuf->str = (gchar *)wmem_alloc(strbuf->allocator, strbuf->alloc_len);
|
2012-12-15 00:46:36 +00:00
|
|
|
strbuf->str[0] = '\0';
|
|
|
|
|
|
|
|
return strbuf;
|
|
|
|
}
|
|
|
|
|
|
|
|
wmem_strbuf_t *
|
|
|
|
wmem_strbuf_new(wmem_allocator_t *allocator, const gchar *str)
|
|
|
|
{
|
|
|
|
wmem_strbuf_t *strbuf;
|
|
|
|
gsize len, alloc_len;
|
|
|
|
|
|
|
|
len = str ? strlen(str) : 0;
|
|
|
|
alloc_len = DEFAULT_MINIMUM_LEN;
|
|
|
|
|
|
|
|
/* +1 for the null-terminator */
|
|
|
|
while (alloc_len < (len + 1)) {
|
|
|
|
alloc_len *= 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
strbuf = wmem_strbuf_sized_new(allocator, alloc_len, 0);
|
|
|
|
|
|
|
|
if (str && len > 0) {
|
2021-04-30 10:18:25 +00:00
|
|
|
(void) g_strlcpy(strbuf->str, str, alloc_len);
|
2012-12-15 00:46:36 +00:00
|
|
|
strbuf->len = len;
|
|
|
|
}
|
|
|
|
|
|
|
|
return strbuf;
|
|
|
|
}
|
|
|
|
|
2020-09-17 19:27:26 +00:00
|
|
|
/* grows the allocated size of the wmem_strbuf_t. If max_len is set, then
|
|
|
|
* not guaranteed to grow by the full amount to_add */
|
2014-03-31 11:44:05 +00:00
|
|
|
static inline void
|
2012-12-29 20:37:53 +00:00
|
|
|
wmem_strbuf_grow(wmem_strbuf_t *strbuf, const gsize to_add)
|
2012-12-15 00:46:36 +00:00
|
|
|
{
|
2012-12-29 20:37:53 +00:00
|
|
|
gsize new_alloc_len, new_len;
|
2013-07-06 04:08:07 +00:00
|
|
|
|
2014-05-02 13:53:33 +00:00
|
|
|
/* short-circuit for efficiency if we have room already; greatly speeds up
|
|
|
|
* repeated calls to wmem_strbuf_append_c and others which grow a little bit
|
|
|
|
* at a time.
|
|
|
|
*/
|
|
|
|
if (WMEM_STRBUF_ROOM(strbuf) >= to_add) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-12-15 00:46:36 +00:00
|
|
|
new_alloc_len = strbuf->alloc_len;
|
2012-12-29 20:37:53 +00:00
|
|
|
new_len = strbuf->len + to_add;
|
2012-12-15 00:46:36 +00:00
|
|
|
|
|
|
|
/* +1 for the null-terminator */
|
|
|
|
while (new_alloc_len < (new_len + 1)) {
|
|
|
|
new_alloc_len *= 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* max length only enforced if not 0 */
|
|
|
|
if (strbuf->max_len && new_alloc_len > strbuf->max_len) {
|
|
|
|
new_alloc_len = strbuf->max_len;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (new_alloc_len == strbuf->alloc_len) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-03-20 01:18:10 +00:00
|
|
|
strbuf->str = (gchar *)wmem_realloc(strbuf->allocator, strbuf->str, new_alloc_len);
|
2012-12-15 00:46:36 +00:00
|
|
|
|
|
|
|
strbuf->alloc_len = new_alloc_len;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
wmem_strbuf_append(wmem_strbuf_t *strbuf, const gchar *str)
|
|
|
|
{
|
2013-04-21 23:19:37 +00:00
|
|
|
gsize append_len;
|
|
|
|
|
2013-04-24 00:11:57 +00:00
|
|
|
if (!str || str[0] == '\0') {
|
2012-12-15 00:46:36 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-04-21 23:19:37 +00:00
|
|
|
append_len = strlen(str);
|
|
|
|
|
|
|
|
wmem_strbuf_grow(strbuf, append_len);
|
2012-12-15 00:46:36 +00:00
|
|
|
|
2021-04-30 10:18:25 +00:00
|
|
|
(void) g_strlcpy(&strbuf->str[strbuf->len], str, strbuf->max_len ? WMEM_STRBUF_RAW_ROOM(strbuf) : append_len+1);
|
2013-04-21 23:19:37 +00:00
|
|
|
|
|
|
|
strbuf->len = MIN(strbuf->len + append_len, strbuf->alloc_len - 1);
|
2012-12-15 00:46:36 +00:00
|
|
|
}
|
|
|
|
|
2020-09-17 19:27:26 +00:00
|
|
|
void
|
|
|
|
wmem_strbuf_append_len(wmem_strbuf_t *strbuf, const gchar *str, gsize append_len)
|
|
|
|
{
|
|
|
|
|
2020-10-17 19:28:19 +00:00
|
|
|
if (!append_len || !str) {
|
2020-09-17 19:27:26 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
wmem_strbuf_grow(strbuf, append_len);
|
|
|
|
|
|
|
|
if (strbuf->max_len) {
|
|
|
|
append_len = MIN(append_len, WMEM_STRBUF_ROOM(strbuf));
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(&strbuf->str[strbuf->len], str, append_len);
|
|
|
|
strbuf->len += append_len;
|
|
|
|
strbuf->str[strbuf->len] = '\0';
|
|
|
|
}
|
|
|
|
|
2021-03-19 18:18:51 +00:00
|
|
|
static inline
|
|
|
|
int _strbuf_vsnprintf(wmem_strbuf_t *strbuf, const char *format, va_list ap, gboolean reset)
|
2012-12-29 20:37:53 +00:00
|
|
|
{
|
2021-03-19 18:18:51 +00:00
|
|
|
int want_len;
|
|
|
|
char *buffer = &strbuf->str[strbuf->len];
|
|
|
|
size_t buffer_size = WMEM_STRBUF_RAW_ROOM(strbuf);
|
|
|
|
|
|
|
|
want_len = vsnprintf(buffer, buffer_size, format, ap);
|
|
|
|
if (want_len < 0) {
|
|
|
|
/* Error. */
|
|
|
|
g_warning("%s: vsnprintf (%d): %s", G_STRFUNC, want_len, g_strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if ((size_t)want_len < buffer_size) {
|
|
|
|
/* Success. */
|
|
|
|
strbuf->len += want_len;
|
|
|
|
return 0;
|
|
|
|
}
|
2012-12-29 20:37:53 +00:00
|
|
|
|
2021-03-19 18:18:51 +00:00
|
|
|
/* No space in buffer, output was truncated. */
|
|
|
|
if (reset) {
|
|
|
|
strbuf->str[strbuf->len] = '\0'; /* Reset. */
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
strbuf->len += buffer_size - 1; /* Append. */
|
2021-03-20 13:13:27 +00:00
|
|
|
ws_assert(strbuf->len == strbuf->alloc_len - 1);
|
2021-03-19 18:18:51 +00:00
|
|
|
}
|
2013-04-21 23:19:37 +00:00
|
|
|
|
2021-03-19 18:18:51 +00:00
|
|
|
return want_len; /* Length (not including terminating null) that would be written
|
|
|
|
if there was enough space in buffer. */
|
2012-12-29 20:37:53 +00:00
|
|
|
}
|
2021-03-19 18:18:51 +00:00
|
|
|
|
2019-10-08 15:35:34 +00:00
|
|
|
void
|
2016-04-08 16:38:54 +00:00
|
|
|
wmem_strbuf_append_vprintf(wmem_strbuf_t *strbuf, const gchar *fmt, va_list ap)
|
|
|
|
{
|
2021-03-19 18:18:51 +00:00
|
|
|
int want_len;
|
2016-04-08 16:38:54 +00:00
|
|
|
va_list ap2;
|
|
|
|
|
|
|
|
G_VA_COPY(ap2, ap);
|
2021-03-19 18:18:51 +00:00
|
|
|
/* Try to write buffer, check if output fits. */
|
|
|
|
want_len = _strbuf_vsnprintf(strbuf, fmt, ap2, TRUE); /* Remove output if truncated. */
|
2016-04-08 16:38:54 +00:00
|
|
|
va_end(ap2);
|
2021-03-19 18:18:51 +00:00
|
|
|
if (want_len <= 0)
|
|
|
|
return;
|
2016-04-08 16:38:54 +00:00
|
|
|
|
2021-03-19 18:18:51 +00:00
|
|
|
/* Resize buffer and try again. This could hit the 'max_len' ceiling. */
|
|
|
|
wmem_strbuf_grow(strbuf, want_len);
|
|
|
|
_strbuf_vsnprintf(strbuf, fmt, ap, FALSE); /* Keep output if truncated. */
|
2016-04-08 16:38:54 +00:00
|
|
|
}
|
2012-12-29 20:37:53 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
wmem_strbuf_append_printf(wmem_strbuf_t *strbuf, const gchar *format, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, format);
|
|
|
|
wmem_strbuf_append_vprintf(strbuf, format, ap);
|
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
|
2013-04-26 21:30:24 +00:00
|
|
|
void
|
|
|
|
wmem_strbuf_append_c(wmem_strbuf_t *strbuf, const gchar c)
|
|
|
|
{
|
|
|
|
wmem_strbuf_grow(strbuf, 1);
|
|
|
|
|
2020-09-17 19:27:26 +00:00
|
|
|
if (!strbuf->max_len || WMEM_STRBUF_ROOM(strbuf) >= 1) {
|
2013-04-26 21:30:24 +00:00
|
|
|
strbuf->str[strbuf->len] = c;
|
|
|
|
strbuf->len++;
|
|
|
|
strbuf->str[strbuf->len] = '\0';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
wmem_strbuf_append_unichar(wmem_strbuf_t *strbuf, const gunichar c)
|
|
|
|
{
|
|
|
|
gchar buf[6];
|
|
|
|
gsize charlen;
|
|
|
|
|
|
|
|
charlen = g_unichar_to_utf8(c, buf);
|
|
|
|
|
|
|
|
wmem_strbuf_grow(strbuf, charlen);
|
|
|
|
|
2020-09-17 19:27:26 +00:00
|
|
|
if (!strbuf->max_len || WMEM_STRBUF_ROOM(strbuf) >= charlen) {
|
2013-04-26 21:30:24 +00:00
|
|
|
memcpy(&strbuf->str[strbuf->len], buf, charlen);
|
|
|
|
strbuf->len += charlen;
|
|
|
|
strbuf->str[strbuf->len] = '\0';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-24 00:11:57 +00:00
|
|
|
void
|
|
|
|
wmem_strbuf_truncate(wmem_strbuf_t *strbuf, const gsize len)
|
|
|
|
{
|
|
|
|
if (len >= strbuf->len) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
strbuf->str[len] = '\0';
|
|
|
|
strbuf->len = len;
|
|
|
|
}
|
|
|
|
|
2012-12-19 00:51:48 +00:00
|
|
|
const gchar *
|
|
|
|
wmem_strbuf_get_str(wmem_strbuf_t *strbuf)
|
|
|
|
{
|
|
|
|
return strbuf->str;
|
|
|
|
}
|
|
|
|
|
|
|
|
gsize
|
|
|
|
wmem_strbuf_get_len(wmem_strbuf_t *strbuf)
|
|
|
|
{
|
|
|
|
return strbuf->len;
|
|
|
|
}
|
|
|
|
|
2014-03-28 20:28:53 +00:00
|
|
|
/* Truncates the allocated memory down to the minimal amount, frees the header
|
|
|
|
* structure, and returns a non-const pointer to the raw string. The
|
|
|
|
* wmem_strbuf_t structure cannot be used after this is called.
|
|
|
|
*/
|
|
|
|
char *
|
|
|
|
wmem_strbuf_finalize(wmem_strbuf_t *strbuf)
|
|
|
|
{
|
|
|
|
char *ret;
|
|
|
|
|
|
|
|
ret = (char *)wmem_realloc(strbuf->allocator, strbuf->str, strbuf->len+1);
|
|
|
|
|
|
|
|
wmem_free(strbuf->allocator, strbuf);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-12-15 00:46:36 +00:00
|
|
|
/*
|
2019-07-26 18:43:17 +00:00
|
|
|
* Editor modelines - https://www.wireshark.org/tools/modelines.html
|
2012-12-15 00:46:36 +00:00
|
|
|
*
|
|
|
|
* Local variables:
|
|
|
|
* c-basic-offset: 4
|
|
|
|
* tab-width: 8
|
|
|
|
* indent-tabs-mode: nil
|
|
|
|
* End:
|
|
|
|
*
|
|
|
|
* vi: set shiftwidth=4 tabstop=8 expandtab:
|
|
|
|
* :indentSize=4:tabSize=8:noTabs=true:
|
|
|
|
*/
|