zabbix-agent2/src/libs/zbxeval/misc.c
/******************************************************************************
 *                                                                            *
 * Function: zbx_eval_serialize                                               *
 *                                                                            *
 * Purpose: serialize evaluation context into buffer                          *
 *                                                                            *
 * Parameters: ctx         - [IN] the evaluation context                      *
 *             malloc_func - [IN] the buffer memory allocation function,      *
 *                                optional (by default the buffer is          *
 *                                allocated in heap)                          *
 *             data  - [OUT] the buffer with serialized evaluation context    *
 *                                                                            *
 * Comments: Location of the replaced tokens (with token.value set) are not   *
 *           serialized, making it impossible to reconstruct the expression   *
 *           text with replaced tokens.                                       *
 *           Context serialization/deserialization must be used for           *
 *           context caching.                                                 *
 *                                                                            *
 * Return value: The size of serialized data.                                 *
 *                                                                            *
 ******************************************************************************/
zbx_eval_serialize

Purpose: serialize evaluation context into buffer

Parameters: ctx         - [IN] the evaluation context
malloc_func - [IN] the buffer memory allocation function,
optional (by default the buffer is
allocated in heap)
data  - [OUT] the buffer with serialized evaluation context

Comments: Location of the replaced tokens (with token.value set) are not
serialized, making it impossible to reconstruct the expression
text with replaced tokens.
Context serialization/deserialization must be used for
context caching.

Return value: The size of serialized data.
zbx_eval_serialize Purpose : serialize evaluation context into buffer Parameters : ctx - [ IN ] the evaluation context malloc_func - [ IN ] the buffer memory allocation function optional ( by default the buffer is allocated in heap ) data - [ OUT ] the buffer with serialized evaluation context Comments : Location of the replaced tokens ( with token . value set ) are not serialized making it impossible to reconstruct the expression text with replaced tokens . Context serialization / deserialization must be used for context caching . Return value : The size of serialized data .
size_t	zbx_eval_serialize(const zbx_eval_context_t *ctx, zbx_mem_malloc_func_t malloc_func,
		unsigned char **data)
{
	int		i;
	unsigned char	buffer_static[ZBX_EVAL_STATIC_BUFFER_SIZE], *buffer = buffer_static, *ptr = buffer, len_buff[6];
	size_t		buffer_size = ZBX_EVAL_STATIC_BUFFER_SIZE;
	zbx_uint32_t	len, len_offset;
	if (NULL == malloc_func)
		malloc_func = ZBX_DEFAULT_MEM_MALLOC_FUNC;
	ptr += zbx_serialize_uint31_compact(ptr, ctx->stack.values_num);
	for (i = 0; i < ctx->stack.values_num; i++)
	{
		const zbx_eval_token_t	*token = &ctx->stack.values[i];
		reserve_buffer(&buffer, &buffer_size, 23, &ptr);
		ptr += zbx_serialize_value(ptr, token->type);
		ptr += zbx_serialize_uint31_compact(ptr, token->opt);
		ptr += zbx_serialize_uint31_compact(ptr, token->loc.l);
		ptr += zbx_serialize_uint31_compact(ptr, token->loc.r);
		serialize_variant(&buffer, &buffer_size, &token->value, &ptr);
	}
	len = ptr - buffer;
	len_offset = zbx_serialize_uint31_compact(len_buff, len);
	*data = malloc_func(NULL, len + len_offset);
	memcpy(*data, len_buff, len_offset);
	memcpy(*data + len_offset, buffer, len);
	if (buffer != buffer_static)
		zbx_free(buffer);
	return len + len_offset;
}
size_t zbx_eval_serialize ( const zbx_eval_context_t * ctx , zbx_mem_malloc_func_t malloc_func , unsigned char * * data ) { int i ; unsigned char buffer_static [ ZBX_EVAL_STATIC_BUFFER_SIZE ] , * buffer = buffer_static , * ptr = buffer , len_buff [ 6 ] ; size_t buffer_size = ZBX_EVAL_STATIC_BUFFER_SIZE ; zbx_uint32_t len , len_offset ; if ( NULL == malloc_func ) malloc_func = ZBX_DEFAULT_MEM_MALLOC_FUNC ; ptr += zbx_serialize_uint31_compact ( ptr , ctx -> stack . values_num ) ; for ( i = 0 ; i < ctx -> stack . values_num ; i ++ ) { const zbx_eval_token_t * token = & ctx -> stack . values [ i ] ; reserve_buffer ( & buffer , & buffer_size , 23 , & ptr ) ; ptr += zbx_serialize_value ( ptr , token -> type ) ; ptr += zbx_serialize_uint31_compact ( ptr , token -> opt ) ; ptr += zbx_serialize_uint31_compact ( ptr , token -> loc . l ) ; ptr += zbx_serialize_uint31_compact ( ptr , token -> loc . r ) ; serialize_variant ( & buffer , & buffer_size , & token -> value , & ptr ) ; } len = ptr - buffer ; len_offset = zbx_serialize_uint31_compact ( len_buff , len ) ; * data = malloc_func ( NULL , len + len_offset ) ; memcpy ( * data , len_buff , len_offset ) ; memcpy ( * data + len_offset , buffer , len ) ; if ( buffer != buffer_static ) zbx_free ( buffer ) ; return len + len_offset ; }
Function: zbx_eval_serialize                                               
                                                                            
 Purpose: serialize evaluation context into buffer                          
                                                                            
 Parameters: ctx         - [IN] the evaluation context                      
             malloc_func - [IN] the buffer memory allocation function,      
                                optional (by default the buffer is          
                                allocated in heap)                          
             data  - [OUT] the buffer with serialized evaluation context    
                                                                            
 Comments: Location of the replaced tokens (with token.value set) are not   
           serialized, making it impossible to reconstruct the expression   
           text with replaced tokens.
Function : zbx_eval_serialize Purpose : serialize evaluation context into buffer Parameters : ctx - [ IN ] the evaluation context malloc_func - [ IN ] the buffer memory allocation function optional ( by default the buffer is allocated in heap ) data - [ OUT ] the buffer with serialized evaluation context Comments : Location of the replaced tokens ( with token . value set ) are not serialized making it impossible to reconstruct the expression text with replaced tokens .
/* reserve space for maximum possible worst case scenario with empty variant:         */ /*  4 bytes token type, 6 bytes per compact uint31 and 1 byte empty variant (4+3*6+1) */