Source
xxxxxxxxxx
static void eval_token_print_alloc(const zbx_eval_context_t *ctx, char **str, size_t *str_alloc, size_t *str_offset,
/*
** Zabbix
** Copyright (C) 2001-2023 Zabbix SIA
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
**/
/******************************************************************************
* *
* Purpose: reserve number of bytes in the specified buffer, reallocating if *
* necessary *
* *
* Parameters: buffer - [IN/OUT] the buffer *
* buffer_size - [INT/OUT] the deserialized value *
* reserve - [IN] the number of bytes to reserve *
* ptr - [IN/OUT] a pointer to an offset in buffer *
* *
* Comments: Initially static buffer is used, allocating dynamic buffer when *
* static buffer is too small. *
* *
******************************************************************************/
static void reserve_buffer(unsigned char **buffer, size_t *buffer_size, size_t reserve, unsigned char **ptr)
{
size_t offset = *ptr - *buffer, new_size;
if (offset + reserve <= *buffer_size)
return;
new_size = *buffer_size * 1.5;
if (ZBX_EVAL_STATIC_BUFFER_SIZE == *buffer_size)
{
unsigned char *old = *buffer;
*buffer = zbx_malloc(NULL, new_size);
memcpy(*buffer, old, offset);
}
else
*buffer = zbx_realloc(*buffer, new_size);
*buffer_size = new_size;
*ptr = *buffer + offset;
}
static void serialize_variant(unsigned char **buffer, size_t *size, const zbx_variant_t *value,
unsigned char **ptr)
{
size_t len;
reserve_buffer(buffer, size, 1, ptr);
**ptr = value->type;
(*ptr)++;
switch (value->type)
{
case ZBX_VARIANT_UI64:
reserve_buffer(buffer, size, sizeof(value->data.ui64), ptr);
*ptr += zbx_serialize_uint64(*ptr, value->data.ui64);
break;
case ZBX_VARIANT_DBL:
reserve_buffer(buffer, size, sizeof(value->data.dbl), ptr);
*ptr += zbx_serialize_double(*ptr, value->data.dbl);
break;
case ZBX_VARIANT_STR:
len = strlen(value->data.str) + 1;
reserve_buffer(buffer, size, len, ptr);
memcpy(*ptr, value->data.str, len);
*ptr += len;