Source
static void eval_token_print_alloc(const zbx_eval_context_t *ctx, char **str, size_t *str_alloc, size_t *str_offset,
/*
** Copyright (C) 2001-2025 Zabbix SIA
**
** This program is free software: you can redistribute it and/or modify it under the terms of
** the GNU Affero General Public License as published by the Free Software Foundation, version 3.
**
** 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 Affero General Public License for more details.
**
** You should have received a copy of the GNU Affero General Public License along with this program.
** If not, see <https://www.gnu.org/licenses/>.
**/
/******************************************************************************
* *
* Purpose: Reserves number of bytes in the specified buffer, reallocating if *
* necessary. *
* *
* Parameters: buffer - [IN/OUT] *
* buffer_size - [IN/OUT] *
* reserve - [IN] number of bytes to reserve *
* ptr - [IN/OUT] pointer to 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;
while (offset + reserve >= new_size)
new_size = new_size + (new_size / 2);
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;
break;
case ZBX_VARIANT_NONE:
break;