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"
|
2022-06-18 09:43:24 +00:00
|
|
|
#include "wmem_strbuf.h"
|
2013-08-14 00:31:14 +00:00
|
|
|
|
2021-03-19 18:18:51 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <errno.h>
|
2012-12-15 00:46:36 +00:00
|
|
|
|
2021-07-09 18:46:39 +00:00
|
|
|
#include "wmem-int.h"
|
2022-06-18 09:43:24 +00:00
|
|
|
#include "wmem_strutl.h"
|
2012-12-15 00:46:36 +00:00
|
|
|
|
2023-02-06 17:46:35 +00:00
|
|
|
#include <wsutil/unicode-utils.h>
|
|
|
|
|
2022-09-27 14:03:17 +00:00
|
|
|
#define DEFAULT_MINIMUM_SIZE 16
|
2012-12-15 00:46:36 +00:00
|
|
|
|
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. */
|
2022-09-27 14:03:17 +00:00
|
|
|
#define WMEM_STRBUF_ROOM(S) ((S)->alloc_size - (S)->len - 1)
|
|
|
|
#define WMEM_STRBUF_RAW_ROOM(S) ((S)->alloc_size - (S)->len)
|
2013-04-26 21:30:24 +00:00
|
|
|
|
2012-12-15 00:46:36 +00:00
|
|
|
wmem_strbuf_t *
|
2022-11-25 19:04:09 +00:00
|
|
|
wmem_strbuf_new_sized(wmem_allocator_t *allocator,
|
|
|
|
size_t alloc_size)
|
2012-12-15 00:46:36 +00:00
|
|
|
{
|
|
|
|
wmem_strbuf_t *strbuf;
|
|
|
|
|
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;
|
2022-09-27 14:03:17 +00:00
|
|
|
strbuf->alloc_size = alloc_size ? alloc_size : DEFAULT_MINIMUM_SIZE;
|
2012-12-15 00:46:36 +00:00
|
|
|
|
2022-09-27 14:03:17 +00:00
|
|
|
strbuf->str = (gchar *)wmem_alloc(strbuf->allocator, strbuf->alloc_size);
|
2012-12-15 00:46:36 +00:00
|
|
|
strbuf->str[0] = '\0';
|
|
|
|
|
|
|
|
return strbuf;
|
|
|
|
}
|
|
|
|
|
|
|
|
wmem_strbuf_t *
|
2022-06-18 09:43:24 +00:00
|
|
|
wmem_strbuf_new_len(wmem_allocator_t *allocator, const gchar *str, size_t len)
|
2012-12-15 00:46:36 +00:00
|
|
|
{
|
|
|
|
wmem_strbuf_t *strbuf;
|
2022-09-27 14:03:17 +00:00
|
|
|
size_t alloc_size;
|
2012-12-15 00:46:36 +00:00
|
|
|
|
2022-09-27 14:03:17 +00:00
|
|
|
alloc_size = DEFAULT_MINIMUM_SIZE;
|
2012-12-15 00:46:36 +00:00
|
|
|
|
|
|
|
/* +1 for the null-terminator */
|
2022-09-27 14:03:17 +00:00
|
|
|
while (alloc_size < (len + 1)) {
|
|
|
|
alloc_size *= 2;
|
2012-12-15 00:46:36 +00:00
|
|
|
}
|
|
|
|
|
2022-11-25 19:04:09 +00:00
|
|
|
strbuf = wmem_strbuf_new_sized(allocator, alloc_size);
|
2012-12-15 00:46:36 +00:00
|
|
|
|
|
|
|
if (str && len > 0) {
|
2023-02-06 17:27:44 +00:00
|
|
|
ws_assert(strbuf->alloc_size >= len + 1);
|
2022-06-18 09:43:24 +00:00
|
|
|
memcpy(strbuf->str, str, len);
|
|
|
|
strbuf->str[len] = '\0';
|
2012-12-15 00:46:36 +00:00
|
|
|
strbuf->len = len;
|
|
|
|
}
|
|
|
|
|
|
|
|
return strbuf;
|
|
|
|
}
|
|
|
|
|
2022-06-18 09:43:24 +00:00
|
|
|
wmem_strbuf_t *
|
|
|
|
wmem_strbuf_new(wmem_allocator_t *allocator, const gchar *str)
|
|
|
|
{
|
|
|
|
return wmem_strbuf_new_len(allocator, str, str ? strlen(str) : 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
wmem_strbuf_t *
|
|
|
|
wmem_strbuf_dup(wmem_allocator_t *allocator, const wmem_strbuf_t *src)
|
|
|
|
{
|
|
|
|
wmem_strbuf_t *new;
|
|
|
|
|
2022-11-25 19:04:09 +00:00
|
|
|
new = wmem_strbuf_new_sized(allocator, src->alloc_size);
|
2022-06-18 09:43:24 +00:00
|
|
|
new->len = src->len;
|
|
|
|
memcpy(new->str, src->str, new->len);
|
|
|
|
new->str[new->len] = '\0';
|
|
|
|
return new;
|
|
|
|
}
|
|
|
|
|
2022-09-27 14:03:17 +00:00
|
|
|
/* grows the allocated size of the wmem_strbuf_t. If max_size is set, then
|
2020-09-17 19:27:26 +00:00
|
|
|
* not guaranteed to grow by the full amount to_add */
|
2014-03-31 11:44:05 +00:00
|
|
|
static inline void
|
2022-09-27 14:03:17 +00:00
|
|
|
wmem_strbuf_grow(wmem_strbuf_t *strbuf, const size_t to_add)
|
2012-12-15 00:46:36 +00:00
|
|
|
{
|
2022-09-27 14:03:17 +00:00
|
|
|
size_t 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;
|
|
|
|
}
|
|
|
|
|
2022-09-27 14:03:17 +00:00
|
|
|
new_alloc_len = strbuf->alloc_size;
|
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;
|
|
|
|
}
|
|
|
|
|
2022-09-27 14:03:17 +00:00
|
|
|
if (new_alloc_len == strbuf->alloc_size) {
|
2012-12-15 00:46:36 +00:00
|
|
|
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
|
|
|
|
2022-09-27 14:03:17 +00:00
|
|
|
strbuf->alloc_size = new_alloc_len;
|
2012-12-15 00:46:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
wmem_strbuf_append(wmem_strbuf_t *strbuf, const gchar *str)
|
|
|
|
{
|
2022-09-27 14:03:17 +00:00
|
|
|
size_t append_len;
|
2013-04-21 23:19:37 +00:00
|
|
|
|
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
|
|
|
|
2023-02-06 17:27:44 +00:00
|
|
|
ws_assert(WMEM_STRBUF_RAW_ROOM(strbuf) >= append_len + 1);
|
2022-12-14 19:38:45 +00:00
|
|
|
memcpy(&strbuf->str[strbuf->len], str, append_len);
|
|
|
|
strbuf->len += append_len;
|
|
|
|
strbuf->str[strbuf->len] = '\0';
|
2012-12-15 00:46:36 +00:00
|
|
|
}
|
|
|
|
|
2020-09-17 19:27:26 +00:00
|
|
|
void
|
2022-09-27 14:03:17 +00:00
|
|
|
wmem_strbuf_append_len(wmem_strbuf_t *strbuf, const gchar *str, size_t append_len)
|
2020-09-17 19:27:26 +00:00
|
|
|
{
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
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
|
2022-11-25 19:04:09 +00:00
|
|
|
int _strbuf_vsnprintf(wmem_strbuf_t *strbuf, const char *format, va_list ap)
|
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. */
|
2021-06-26 01:25:16 +00:00
|
|
|
g_warning("%s: vsnprintf: (%d) %s", G_STRFUNC, want_len, g_strerror(errno));
|
2021-03-19 18:18:51 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if ((size_t)want_len < buffer_size) {
|
|
|
|
/* Success. */
|
|
|
|
strbuf->len += want_len;
|
|
|
|
return 0;
|
|
|
|
}
|
2012-12-29 20:37:53 +00:00
|
|
|
|
2022-11-25 19:04:09 +00:00
|
|
|
/* Not enough space in buffer, output was truncated. */
|
|
|
|
strbuf->str[strbuf->len] = '\0'; /* Reset. */
|
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;
|
|
|
|
|
2021-12-12 11:34:48 +00:00
|
|
|
va_copy(ap2, ap);
|
2021-03-19 18:18:51 +00:00
|
|
|
/* Try to write buffer, check if output fits. */
|
2022-11-25 19:04:09 +00:00
|
|
|
want_len = _strbuf_vsnprintf(strbuf, fmt, ap2);
|
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
|
|
|
|
2022-11-25 19:04:09 +00:00
|
|
|
/* Resize buffer and try again. */
|
2021-03-19 18:18:51 +00:00
|
|
|
wmem_strbuf_grow(strbuf, want_len);
|
2022-11-25 19:04:09 +00:00
|
|
|
want_len = _strbuf_vsnprintf(strbuf, fmt, ap);
|
|
|
|
/* Second time must succeed or error out. */
|
2023-02-06 17:27:44 +00:00
|
|
|
ws_assert(want_len <= 0);
|
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);
|
|
|
|
|
2022-12-14 19:38:45 +00:00
|
|
|
strbuf->str[strbuf->len] = c;
|
|
|
|
strbuf->len++;
|
|
|
|
strbuf->str[strbuf->len] = '\0';
|
2013-04-26 21:30:24 +00:00
|
|
|
}
|
|
|
|
|
2022-12-14 19:50:30 +00:00
|
|
|
void
|
|
|
|
wmem_strbuf_append_c_count(wmem_strbuf_t *strbuf, const gchar c, size_t count)
|
|
|
|
{
|
|
|
|
wmem_strbuf_grow(strbuf, count);
|
|
|
|
|
|
|
|
while (count-- > 0) {
|
|
|
|
strbuf->str[strbuf->len++] = c;
|
|
|
|
}
|
|
|
|
strbuf->str[strbuf->len] = '\0';
|
|
|
|
}
|
|
|
|
|
2013-04-26 21:30:24 +00:00
|
|
|
void
|
|
|
|
wmem_strbuf_append_unichar(wmem_strbuf_t *strbuf, const gunichar c)
|
|
|
|
{
|
|
|
|
gchar buf[6];
|
2022-09-27 14:03:17 +00:00
|
|
|
size_t charlen;
|
2013-04-26 21:30:24 +00:00
|
|
|
|
|
|
|
charlen = g_unichar_to_utf8(c, buf);
|
|
|
|
|
|
|
|
wmem_strbuf_grow(strbuf, charlen);
|
|
|
|
|
2022-12-14 19:38:45 +00:00
|
|
|
memcpy(&strbuf->str[strbuf->len], buf, charlen);
|
|
|
|
strbuf->len += charlen;
|
|
|
|
strbuf->str[strbuf->len] = '\0';
|
2013-04-26 21:30:24 +00:00
|
|
|
}
|
|
|
|
|
2022-10-19 03:18:37 +00:00
|
|
|
void
|
|
|
|
wmem_strbuf_append_unichar_validated(wmem_strbuf_t *strbuf, const gunichar c)
|
|
|
|
{
|
|
|
|
if (g_unichar_validate(c)) {
|
|
|
|
wmem_strbuf_append_unichar(strbuf, c);
|
|
|
|
} else {
|
|
|
|
wmem_strbuf_append_unichar(strbuf, UNICODE_REPLACEMENT_CHARACTER);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-03 09:49:07 +00:00
|
|
|
static const char hex[16] = { '0', '1', '2', '3', '4', '5', '6', '7',
|
|
|
|
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
|
|
|
|
|
2022-10-15 09:11:00 +00:00
|
|
|
#define HEX_CODELEN 4
|
|
|
|
|
2022-10-03 09:49:07 +00:00
|
|
|
void
|
|
|
|
wmem_strbuf_append_hex(wmem_strbuf_t *strbuf, uint8_t ch)
|
|
|
|
{
|
2022-10-15 09:11:00 +00:00
|
|
|
wmem_strbuf_grow(strbuf, HEX_CODELEN * 1);
|
2022-10-03 09:49:07 +00:00
|
|
|
|
2022-12-14 19:38:45 +00:00
|
|
|
strbuf->str[strbuf->len++] = '\\';
|
|
|
|
strbuf->str[strbuf->len++] = 'x';
|
|
|
|
strbuf->str[strbuf->len++] = hex[(ch >> 4) & 0xF];
|
|
|
|
strbuf->str[strbuf->len++] = hex[(ch >> 0) & 0xF];
|
|
|
|
strbuf->str[strbuf->len] = '\0';
|
2022-10-03 09:49:07 +00:00
|
|
|
}
|
|
|
|
|
2022-10-15 09:11:00 +00:00
|
|
|
#define BMP_CODELEN 6
|
|
|
|
|
|
|
|
static inline
|
|
|
|
void append_hex_bmp(wmem_strbuf_t *strbuf, gunichar ch)
|
|
|
|
{
|
|
|
|
wmem_strbuf_grow(strbuf, BMP_CODELEN * 1);
|
|
|
|
|
2022-12-14 19:38:45 +00:00
|
|
|
strbuf->str[strbuf->len++] = '\\';
|
|
|
|
strbuf->str[strbuf->len++] = 'u';
|
|
|
|
strbuf->str[strbuf->len++] = hex[(ch >> 12) & 0xF];
|
|
|
|
strbuf->str[strbuf->len++] = hex[(ch >> 8) & 0xF];
|
|
|
|
strbuf->str[strbuf->len++] = hex[(ch >> 4) & 0xF];
|
|
|
|
strbuf->str[strbuf->len++] = hex[(ch >> 0) & 0xF];
|
|
|
|
strbuf->str[strbuf->len] = '\0';
|
2022-10-15 09:11:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#define ANY_CODELEN 10
|
|
|
|
|
|
|
|
static inline
|
|
|
|
void append_hex_any(wmem_strbuf_t *strbuf, gunichar ch)
|
|
|
|
{
|
|
|
|
wmem_strbuf_grow(strbuf, ANY_CODELEN * 1);
|
|
|
|
|
2022-12-14 19:38:45 +00:00
|
|
|
strbuf->str[strbuf->len++] = '\\';
|
|
|
|
strbuf->str[strbuf->len++] = 'U';
|
|
|
|
strbuf->str[strbuf->len++] = hex[(ch >> 28) & 0xF];
|
|
|
|
strbuf->str[strbuf->len++] = hex[(ch >> 24) & 0xF];
|
|
|
|
strbuf->str[strbuf->len++] = hex[(ch >> 20) & 0xF];
|
|
|
|
strbuf->str[strbuf->len++] = hex[(ch >> 16) & 0xF];
|
|
|
|
strbuf->str[strbuf->len++] = hex[(ch >> 12) & 0xF];
|
|
|
|
strbuf->str[strbuf->len++] = hex[(ch >> 8) & 0xF];
|
|
|
|
strbuf->str[strbuf->len++] = hex[(ch >> 4) & 0xF];
|
|
|
|
strbuf->str[strbuf->len++] = hex[(ch >> 0) & 0xF];
|
|
|
|
strbuf->str[strbuf->len] = '\0';
|
2022-10-15 09:11:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
wmem_strbuf_append_hex_unichar(wmem_strbuf_t *strbuf, gunichar ch)
|
|
|
|
{
|
|
|
|
if (ch <= 0x7f) {
|
|
|
|
wmem_strbuf_append_hex(strbuf, (uint8_t)ch);
|
|
|
|
return HEX_CODELEN;
|
|
|
|
}
|
|
|
|
if (ch <= 0xffff) {
|
|
|
|
append_hex_bmp(strbuf, ch);
|
|
|
|
return BMP_CODELEN;
|
|
|
|
}
|
|
|
|
append_hex_any(strbuf, ch);
|
|
|
|
return ANY_CODELEN;
|
|
|
|
}
|
|
|
|
|
2013-04-24 00:11:57 +00:00
|
|
|
void
|
2022-09-27 14:03:17 +00:00
|
|
|
wmem_strbuf_truncate(wmem_strbuf_t *strbuf, const size_t len)
|
2013-04-24 00:11:57 +00:00
|
|
|
{
|
|
|
|
if (len >= strbuf->len) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
strbuf->str[len] = '\0';
|
|
|
|
strbuf->len = len;
|
|
|
|
}
|
|
|
|
|
2012-12-19 00:51:48 +00:00
|
|
|
const gchar *
|
2022-06-18 09:43:24 +00:00
|
|
|
wmem_strbuf_get_str(const wmem_strbuf_t *strbuf)
|
2012-12-19 00:51:48 +00:00
|
|
|
{
|
|
|
|
return strbuf->str;
|
|
|
|
}
|
|
|
|
|
2022-09-27 14:03:17 +00:00
|
|
|
size_t
|
2022-06-18 09:43:24 +00:00
|
|
|
wmem_strbuf_get_len(const wmem_strbuf_t *strbuf)
|
2012-12-19 00:51:48 +00:00
|
|
|
{
|
|
|
|
return strbuf->len;
|
|
|
|
}
|
|
|
|
|
2022-06-18 09:43:24 +00:00
|
|
|
static inline int
|
|
|
|
_memcmp_len(const void *s1, size_t s1_len, const void *s2, size_t s2_len)
|
|
|
|
{
|
|
|
|
size_t len;
|
|
|
|
int cmp;
|
|
|
|
|
|
|
|
len = MIN(s1_len, s2_len);
|
|
|
|
if ((cmp = memcmp(s1, s2, len)) != 0)
|
|
|
|
return cmp;
|
|
|
|
if (s1_len < s2_len)
|
|
|
|
return -1;
|
|
|
|
if (s1_len > s2_len)
|
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
WS_DLL_PUBLIC
|
|
|
|
int
|
|
|
|
wmem_strbuf_strcmp(const wmem_strbuf_t *sb1, const wmem_strbuf_t *sb2)
|
|
|
|
{
|
|
|
|
return _memcmp_len(sb1->str, sb1->len, sb2->str, sb2->len);
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *
|
|
|
|
wmem_strbuf_strstr(const wmem_strbuf_t *haystack, const wmem_strbuf_t *needle)
|
|
|
|
{
|
|
|
|
return ws_memmem(haystack->str, haystack->len, needle->str, needle->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)
|
|
|
|
{
|
2022-06-18 09:43:24 +00:00
|
|
|
if (strbuf == NULL)
|
|
|
|
return NULL;
|
2014-03-28 20:28:53 +00:00
|
|
|
|
2022-06-18 09:43:24 +00:00
|
|
|
char *ret = (char *)wmem_realloc(strbuf->allocator, strbuf->str, strbuf->len+1);
|
2014-03-28 20:28:53 +00:00
|
|
|
|
|
|
|
wmem_free(strbuf->allocator, strbuf);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-07-10 15:12:03 +00:00
|
|
|
void
|
|
|
|
wmem_strbuf_destroy(wmem_strbuf_t *strbuf)
|
|
|
|
{
|
2022-06-18 09:43:24 +00:00
|
|
|
if (strbuf == NULL)
|
|
|
|
return;
|
2021-07-10 15:12:03 +00:00
|
|
|
|
2022-06-18 09:43:24 +00:00
|
|
|
wmem_free(strbuf->allocator, strbuf->str);
|
|
|
|
wmem_free(strbuf->allocator, strbuf);
|
2021-07-10 15:12:03 +00:00
|
|
|
}
|
|
|
|
|
2022-10-19 12:38:06 +00:00
|
|
|
static bool
|
|
|
|
string_utf8_validate(const char *str, ssize_t max_len, const char **endpptr)
|
|
|
|
{
|
|
|
|
bool valid;
|
2022-11-06 20:01:39 +00:00
|
|
|
const char *endp;
|
2022-10-19 12:38:06 +00:00
|
|
|
|
2022-11-06 20:01:39 +00:00
|
|
|
if (max_len <= 0) {
|
|
|
|
if (endpptr) {
|
|
|
|
*endpptr = str;
|
|
|
|
}
|
2022-10-19 12:38:06 +00:00
|
|
|
return true;
|
2022-11-06 20:01:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
valid = g_utf8_validate(str, max_len, &endp);
|
2022-10-19 12:38:06 +00:00
|
|
|
|
2022-11-06 20:01:39 +00:00
|
|
|
if (valid || *endp != '\0') {
|
|
|
|
if (endpptr) {
|
|
|
|
*endpptr = endp;
|
|
|
|
}
|
2022-10-19 12:38:06 +00:00
|
|
|
return valid;
|
2022-11-06 20:01:39 +00:00
|
|
|
}
|
2022-10-19 12:38:06 +00:00
|
|
|
|
|
|
|
/* Invalid because of a nul byte. Skip nuls and continue. */
|
2022-11-06 20:01:39 +00:00
|
|
|
max_len -= endp - str;
|
|
|
|
str = endp;
|
2022-10-19 12:38:06 +00:00
|
|
|
while (max_len > 0 && *str == '\0') {
|
|
|
|
str++;
|
|
|
|
max_len--;
|
|
|
|
}
|
|
|
|
return string_utf8_validate(str, max_len, endpptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* g_utf8_validate() returns FALSE in the string contains embedded NUL
|
|
|
|
* bytes. We accept \x00 as valid and work around that to validate the
|
|
|
|
* entire len bytes. */
|
2022-09-26 21:39:01 +00:00
|
|
|
bool
|
2022-10-19 12:38:06 +00:00
|
|
|
wmem_strbuf_utf8_validate(wmem_strbuf_t *strbuf, const char **endpptr)
|
2022-09-26 21:39:01 +00:00
|
|
|
{
|
2022-10-19 12:38:06 +00:00
|
|
|
return string_utf8_validate(strbuf->str, strbuf->len, endpptr);
|
2022-10-03 09:49:07 +00:00
|
|
|
}
|
2022-09-26 21:39:01 +00:00
|
|
|
|
2022-10-03 09:49:07 +00:00
|
|
|
void
|
|
|
|
wmem_strbuf_utf8_make_valid(wmem_strbuf_t *strbuf)
|
|
|
|
{
|
2023-02-06 17:46:35 +00:00
|
|
|
wmem_strbuf_t *tmp = ws_utf8_make_valid_strbuf(strbuf->allocator, strbuf->str, strbuf->len);
|
2022-09-26 21:39:01 +00:00
|
|
|
|
2023-02-06 17:46:35 +00:00
|
|
|
wmem_free(strbuf->allocator, strbuf->str);
|
|
|
|
strbuf->str = tmp->str;
|
|
|
|
strbuf->len = tmp->len;
|
|
|
|
strbuf->alloc_size = tmp->alloc_size;
|
2022-09-26 21:39:01 +00:00
|
|
|
|
2023-02-06 17:46:35 +00:00
|
|
|
wmem_free(strbuf->allocator, tmp);
|
2022-09-26 21:39:01 +00:00
|
|
|
}
|
|
|
|
|
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:
|
|
|
|
*/
|