zabbix-agent2/src/libs/zbxalgo/serialize.c
/******************************************************************************
 *                                                                            *
 * Function: zbx_serialize_uint31_compact                                     *
 *                                                                            *
 * Purpose: serialize 31 bit unsigned integer into utf-8 like byte stream     *
 *                                                                            *
 * Parameters: ptr   - [OUT] the output buffer                                *
 *             value - [IN] the value to serialize                            *
 *                                                                            *
 * Return value: The number of bytes written to the buffer.                   *
 *                                                                            *
 * Comments: This serialization method should be used with variables usually  *
 *           having small value while still supporting larger values.         *
 *                                                                            *
 ******************************************************************************/
zbx_serialize_uint31_compact

Purpose: serialize 31 bit unsigned integer into utf-8 like byte stream

Parameters: ptr   - [OUT] the output buffer
value - [IN] the value to serialize

Return value: The number of bytes written to the buffer.

Comments: This serialization method should be used with variables usually
having small value while still supporting larger values.
zbx_serialize_uint31_compact Purpose : serialize 31 bit unsigned integer into utf - 8 like byte stream Parameters : ptr - [ OUT ] the output buffer value - [ IN ] the value to serialize Return value : The number of bytes written to the buffer . Comments : This serialization method should be used with variables usually having small value while still supporting larger values .
zbx_uint32_t	zbx_serialize_uint31_compact(unsigned char *ptr, zbx_uint32_t value)
{
	if (0x7f >= value)
	{
		ptr[0] = (unsigned char)value;
		return 1;
	}
	else
	{
		unsigned char	buf[6];
		zbx_uint32_t	len, pos = (zbx_uint32_t)(sizeof(buf) - 1);
		while (value > (zbx_uint32_t)(0x7f >> (sizeof(buf) - pos)))
		{
			buf[pos] = (unsigned char)(0x80 | (value & 0x3f));
			value >>= 6;
			pos--;
		}
		buf[pos] = (unsigned char)(value | (0xfe << (pos + 1)));
		len = (zbx_uint32_t)(sizeof(buf) - pos);
		memcpy(ptr, buf + pos, len);
		return len;
	}
}
zbx_uint32_t zbx_serialize_uint31_compact ( unsigned char * ptr , zbx_uint32_t value ) { if ( 0x7f >= value ) { ptr [ 0 ] = ( unsigned char ) value ; return 1 ; } else { unsigned char buf [ 6 ] ; zbx_uint32_t len , pos = ( zbx_uint32_t ) ( sizeof ( buf ) - 1 ) ; while ( value > ( zbx_uint32_t ) ( 0x7f >> ( sizeof ( buf ) - pos ) ) ) { buf [ pos ] = ( unsigned char ) ( 0x80 | ( value & 0x3f ) ) ; value >>= 6 ; pos -- ; } buf [ pos ] = ( unsigned char ) ( value | ( 0xfe << ( pos + 1 ) ) ) ; len = ( zbx_uint32_t ) ( sizeof ( buf ) - pos ) ; memcpy ( ptr , buf + pos , len ) ; return len ; } }
Function: zbx_serialize_uint31_compact                                     
                                                                            
 Purpose: serialize 31 bit unsigned integer into utf-8 like byte stream     
                                                                            
 Parameters: ptr   - [OUT] the output buffer                                
             value - [IN] the value to serialize                            
                                                                            
 Return value: The number of bytes written to the buffer.
Function : zbx_serialize_uint31_compact Purpose : serialize 31 bit unsigned integer into utf - 8 like byte stream Parameters : ptr - [ OUT ] the output buffer value - [ IN ] the value to serialize Return value : The number of bytes written to the buffer .