MP_PROTOintmp_check(constchar**data,constchar*end)
Equivalent to mp_next() but also validates MsgPack in data.
Parametersdata - the pointer to a buffer
end - the end of a buffer
Returnvalues0 when MsgPack in data is valid.
!= 0 when MsgPack in data is not valid.
Postcondition
*data = *data + mp_sizeof_TYPE() where TYPE is mp_typeof(**data)
*data is not defined if MsgPack is not valid
Seealsomp_next()MP_PROTOptrdiff_tmp_check_array(constchar*cur,constchar*end)
Check that cur buffer has enough bytes to decode an array header.
Parameterscur buffer
end end of the buffer
Returnvalues0 - buffer has enough bytes
> 0 - the number of remaining bytes to read
Precondition
cur < end
mp_typeof(*cur) == MP_ARRAY
MP_PROTOptrdiff_tmp_check_binl(constchar*cur,constchar*end)
Check that cur buffer has enough bytes to decode a binstring header.
Parameterscur buffer
end end of the buffer
Returnvalues0 - buffer has enough bytes
> 0 - the number of remaining bytes to read
Precondition
cur < end
mp_typeof(*cur) == MP_BIN
MP_PROTOptrdiff_tmp_check_bool(constchar*cur,constchar*end)
Check that cur buffer has enough bytes to decode a bool value.
Parameterscur buffer
end end of the buffer
Returnvalues0 - buffer has enough bytes
> 0 - the number of remaining bytes to read
Precondition
cur < end
mp_typeof(*cur) == MP_BOOL
MP_PROTOptrdiff_tmp_check_double(constchar*cur,constchar*end)
Check that cur buffer has enough bytes to decode a double.
Parameterscur buffer
end end of the buffer
Returnvalues0 - buffer has enough bytes
> 0 - the number of remaining bytes to read
Precondition
cur < end
mp_typeof(*cur) == MP_DOUBLE
MP_PROTOptrdiff_tmp_check_float(constchar*cur,constchar*end)
Check that cur buffer has enough bytes to decode a float.
Parameterscur buffer
end end of the buffer
Returnvalues0 - buffer has enough bytes
> 0 - the number of remaining bytes to read
Precondition
cur < end
mp_typeof(*cur) == MP_FLOAT
MP_PROTOptrdiff_tmp_check_int(constchar*cur,constchar*end)
Check that cur buffer has enough bytes to decode an int.
Parameterscur buffer
end end of the buffer
Returnvalues0 - buffer has enough bytes
> 0 - the number of remaining bytes to read
Precondition
cur < end
mp_typeof(*cur) == MP_INT
MP_PROTOptrdiff_tmp_check_map(constchar*cur,constchar*end)
Check that cur buffer has enough bytes to decode a map header.
Parameterscur buffer
end end of the buffer
Returnvalues0 - buffer has enough bytes
> 0 - the number of remaining bytes to read
Precondition
cur < end
mp_typeof(*cur) == MP_MAP
MP_PROTOptrdiff_tmp_check_nil(constchar*cur,constchar*end)
Check that cur buffer has enough bytes to decode nil.
Parameterscur buffer
end end of the buffer
Returnvalues0 - buffer has enough bytes
> 0 - the number of remaining bytes to read
Precondition
cur < end
mp_typeof(*cur) == MP_NIL
MP_PROTOptrdiff_tmp_check_strl(constchar*cur,constchar*end)
Check that cur buffer has enough bytes to decode a string header.
Parameterscur buffer
end end of the buffer
Returnvalues0 - buffer has enough bytes
> 0 - the number of remaining bytes to read
Precondition
cur < end
mp_typeof(*cur) == MP_STR
MP_PROTOptrdiff_tmp_check_uint(constchar*cur,constchar*end)
Check that cur buffer has enough bytes to decode an uint.
Parameterscur buffer
end end of the buffer
Returnvalues0 - buffer has enough bytes
> 0 - the number of remaining bytes to read
Precondition
cur < end
mp_typeof(*cur) == MP_UINT
MP_PROTOintmp_compare_uint(constchar*data_a,constchar*data_b)
Compare two packed unsigned integers. The function is faster than two mp_decode_uint() calls.
Parametersdata_a unsigned int a
data_b unsigned int b
Returnvalues< 0 when a < b0 when a == b> 0 when a > bMP_PROTOuint32_tmp_decode_array(constchar**data)
Decode an array header from MsgPack data. All array members must be decoded after the header.
Parametersdata - the pointer to a buffer
Returns
the number of elements in an array
Postcondition
*data = *data + mp_sizeof_array(retval)
SeealsoAnusageexampleMP_PROTOconstchar*mp_decode_bin(constchar**data,uint32_t*len)
Decode a binstring from MsgPack data.
Parametersdata - the pointer to a buffer
len - the pointer to save a binstring length
Returns
a pointer to a decoded binstring
Postcondition
*data = *data + mp_sizeof_str(*len)
Seealsomp_encode_binlMP_PROTOuint32_tmp_decode_binl(constchar**data)
Decode a length of a binstring from MsgPack data.
Parametersdata - the pointer to a buffer
Returns
a length of a binstring
Postcondition
*data = *data + mp_sizeof_binl(retval)
Seealsomp_encode_binlMP_PROTOboolmp_decode_bool(constchar**data)
Decode a bool value from MsgPack data.
Parametersdata - the pointer to a buffer
Returns
a decoded bool value
Postcondition
*data = *data + mp_sizeof_bool(retval)
MP_PROTOdoublemp_decode_double(constchar**data)
Decode a double from MsgPack data.
Parametersdata - the pointer to a buffer
Returns
a double
Postcondition
*data = *data + mp_sizeof_double(retval)
MP_PROTOfloatmp_decode_float(constchar**data)
Decode a float from MsgPack data.
Parametersdata - the pointer to a buffer
Returns
a float
Postcondition
*data = *data + mp_sizeof_float(retval)
MP_PROTOint64_tmp_decode_int(constchar**data)
Decode a signed integer from MsgPack data.
Parametersdata - the pointer to a buffer
Returns
an unsigned number
Postcondition
*data = *data + mp_sizeof_int(retval)
MP_PROTOuint32_tmp_decode_map(constchar**data)
Decode a map header from MsgPack data. All map key-value pairs must be decoded after the header.
Parametersdata - the pointer to a buffer
Returns
the number of key/value pairs in a map
Postcondition
*data = *data + mp_sizeof_array(retval)
SeealsoAnusageexampleMP_PROTOvoidmp_decode_nil(constchar**data)
Decode the nil value from MsgPack data.
Parametersdata - the pointer to a buffer
Postcondition
*data = *data + mp_sizeof_nil()MP_PROTOconstchar*mp_decode_str(constchar**data,uint32_t*len)
Decode a string from MsgPack data.
Parametersdata - the pointer to a buffer
len - the pointer to save a string length
Returns
a pointer to a decoded string
Postcondition
*data = *data + mp_sizeof_str(*len)
Seealsomp_encode_binlMP_PROTOconstchar*mp_decode_strbin(constchar**data,uint32_t*len)
Decode a string or binstring from MsgPack data.
Parametersdata - the pointer to a buffer
len - the pointer to save a binstring length
Returns
a pointer to a decoded binstring
Postcondition
*data = *data + mp_sizeof_strbinl(*len)
Seealsomp_encode_binlMP_PROTOuint32_tmp_decode_strbinl(constchar**data)
Decode a length of a string or binstring from MsgPack data.
Parametersdata - the pointer to a buffer
Returns
a length of a string
Postcondition
*data = *data + mp_sizeof_strbinl(retval)
Seealsomp_encode_binlMP_PROTOuint32_tmp_decode_strl(constchar**data)
Decode a length of a string from MsgPack data.
Parametersdata - the pointer to a buffer
Returns
a length of astring
Postcondition
*data = *data + mp_sizeof_strl(retval)
Seealsomp_encode_strlMP_PROTOuint64_tmp_decode_uint(constchar**data)
Decode an unsigned integer from MsgPack data.
Parametersdata - the pointer to a buffer
Returns
an unsigned number
Postcondition
*data = *data + mp_sizeof_uint(retval)
MP_PROTOchar*mp_encode_array(char*data,uint32_tsize)
Encode an array header of size elements. All array members must be encoded after the header.
Example usage:
// Encode
char buf[1024];
char *w = buf;
w = mp_encode_array(w, 2)
w = mp_encode_uint(w, 10);
w = mp_encode_uint(w, 15);
// Decode
const char *r = buf;
uint32_t size = mp_decode_array(&r);
for (uint32_t i = 0; i < size; i++) {
uint64_t val = mp_decode_uint(&r);
}
assert (r == w);
It is your responsibility to ensure that data has enough space.
Parametersdata - a buffer
size - a number of elements
Returnsdata + mp_sizeof_array(size)Seealsomp_sizeof_arrayMP_PROTOchar*mp_encode_bin(char*data,constchar*str,uint32_tlen)
Encode a binstring of length len. The function is equivalent to mp_encode_binl() + memcpy.
Parametersdata - a buffer
str - a pointer to binstring data
len - a binstring length
Returnsdata + mp_sizeof_bin(len) == data + mp_sizeof_binl(len) + lenSeealsomp_encode_strlMP_PROTOchar*mp_encode_binl(char*data,uint32_tlen)
Encode a binstring header of length len. See mp_encode_strl() for more details.
Parametersdata - a bufer
len - a string length
Returns
data + mp_sizeof_binl(len)
Seealsomp_encode_strlMP_PROTOchar*mp_encode_bool(char*data,boolval)
Encode a bool value val. It is your responsibility to ensure that data has enough space.
Parametersdata - a buffer
val - a bool
Returnsdata + mp_sizeof_bool(val)
SeealsoAnusageexamplemp_sizeof_bool()MP_PROTOchar*mp_encode_double(char*data,doublenum)
Encode a double num. It is your responsibility to ensure that data has enough space.
Parametersdata - a buffer
num - a float
Returnsdata + mp_sizeof_double(num)
SeealsoAnusageexamplemp_sizeof_double()MP_PROTOchar*mp_encode_float(char*data,floatnum)
Encode a float num. It is your responsibility to ensure that data has enough space.
Parametersdata - a buffer
num - a float
Returnsdata + mp_sizeof_float(num)
Seealsomp_sizeof_float()AnusageexampleMP_PROTOchar*mp_encode_int(char*data,int64_tnum)
Encode a signed integer num. It is your responsibility to ensure that data has enough space.
Parametersdata - a buffer
num - a number
Returnsdata + mp_sizeof_int(num)
SeealsoAnusageexamplemp_sizeof_int()Preconditionnum < 0
MP_PROTOchar*mp_encode_map(char*data,uint32_tsize)
Encode a map header of size elements. All map key-value pairs must be encoded after the header.
Example usage:
char buf[1024];
// Encode
char *w = buf;
w = mp_encode_map(b, 2);
w = mp_encode_str(b, "key1", 4);
w = mp_encode_str(b, "value1", 6);
w = mp_encode_str(b, "key2", 4);
w = mp_encode_str(b, "value2", 6);
// Decode
const char *r = buf;
uint32_t size = mp_decode_map(&r);
for (uint32_t i = 0; i < size; i++) {
// Use switch(mp_typeof(**r)) to support more types
uint32_t key_len, val_len;
const char *key = mp_decode_str(&r, key_len);
const char *val = mp_decode_str(&r, val_len);
}
assert (r == w);
It is your responsibility to ensure that data has enough space.
Parametersdata - a buffer
size - a number of key/value pairs
Returnsdata + mp_sizeof_map(size)Seealsomp_sizeof_mapMP_PROTOchar*mp_encode_nil(char*data)
Encode the nil value. It is your responsibility to ensure that data has enough space.
Parametersdata - a buffer
Returnsdata + mp_sizeof_nil()SeealsoAnusageexamplemp_sizeof_nil()MP_PROTOchar*mp_encode_str(char*data,constchar*str,uint32_tlen)
Encode a string of length len. The function is equivalent to mp_encode_strl() + memcpy.
Parametersdata - a buffer
str - a pointer to string data
len - a string length
Returnsdata + mp_sizeof_str(len) == data + mp_sizeof_strl(len) + len
Seealsomp_encode_strlMP_PROTOchar*mp_encode_strl(char*data,uint32_tlen)
Encode a string header of length len. The function encodes MsgPack header (only header) for a string of
length len. You should append actual string data to the buffer manually after encoding the header
(exactly len bytes without trailing '\0').
This approach is very useful for cases when the total length of the string is known in advance, but the
string data is not stored in a single continuous buffer (e.g. network packets).
It is your responsibility to ensure that data has enough space. Usage example:
char buffer[1024];
char *b = buffer;
b = mp_encode_strl(b, hdr.total_len);
char *s = b;
memcpy(b, pkt1.data, pkt1.len)
b += pkt1.len;
// get next packet
memcpy(b, pkt2.data, pkt2.len)
b += pkt2.len;
// get next packet
memcpy(b, pkt1.data, pkt3.len)
b += pkt3.len;
// Check that all data was received
assert(hdr.total_len == (uint32_t) (b - s))
Hint: you can dynamically reallocate the buffer during the process.
Parametersdata - a buffer
len - a string length
Returnsdata + mp_sizeof_strl(len)
Seealsomp_sizeof_strl()MP_PROTOchar*mp_encode_uint(char*data,uint64_tnum)
Encode an unsigned integer num. It is your responsibility to ensure that data has enough space.
Parametersdata - a buffer
num - a number
Returnsdata + mp_sizeof_uint(num)
SeealsoAnusageexamplemp_sizeof_uint()MP_PROTOsize_tmp_format(char*data,size_tdata_size,constchar*format,...)
Encode a sequence of values according to format string. Example: mp_format(buf, sz, '[%d {%d%s%d%s}]',
42, 0, 'false', 1, 'true'); to get a msgpack array of two items: number 42 and map (0->'false, 2->'true")
Does not write items that don't fit to data_size argument.
Parametersdata - a buffer
data_size - a buffer size
format - zero-end string, containing structure of resulting msgpack and types of next arguments.
Format can contain '[' and ']' pairs, defining arrays, '{' and '}' pairs, defining maps, and format
specifiers, described below: d, i - int u - unsigned int ld, li - long lu - unsigned long lld, lli -
long long llu - unsigned long long hd, hi - short hu - unsigned short hhd, hhi - char (as number) hhu
- unsigned char (as number) f - float lf - double b - bool s - zero-end string %.*s - string with
specified length %% is ignored smthelse assert and undefined behaviour NIL - a nil value all other
symbols are ignored.
Returns
the number of requred bytes.
Returnvalues> data_size means that is not enough space and whole msgpack was not encoded.
MP_PROTOintmp_fprint(FILE*file,constchar*data)
print to file msgpacked data in JSON format. MP_EXT is printed as 'EXT' only
Parametersfile - pointer to file (or NULL for stdout)
data - pointer to buffer containing msgpack object
Returnvalues0 - success
-1 - wrong msgpack
MP_PROTOvoidmp_next(constchar**data)
Skip one element in a packed data. The function is faster than mp_typeof + mp_decode_XXX() combination.
For arrays and maps the function also skips all members. For strings and binstrings the function also
skips the string data.
Usage example:
char buf[1024];
char *w = buf;
// First MsgPack object
w = mp_encode_uint(w, 10);
// Second MsgPack object
w = mp_encode_array(w, 4);
w = mp_encode_array(w, 2);
// Begin of an inner array
w = mp_encode_str(w, "second inner 1", 14);
w = mp_encode_str(w, "second inner 2", 14);
// End of an inner array
w = mp_encode_str(w, "second", 6);
w = mp_encode_uint(w, 20);
w = mp_encode_bool(w, true);
// Third MsgPack object
w = mp_encode_str(w, "third", 5);
// EOF
const char *r = buf;
// First MsgPack object
assert(mp_typeof(**r) == MP_UINT);
mp_next(&r); // skip the first object
// Second MsgPack object
assert(mp_typeof(**r) == MP_ARRAY);
mp_decode_array(&r);
assert(mp_typeof(**r) == MP_ARRAY); // inner array
mp_next(&r); // -->> skip the entire inner array (with all members)
assert(mp_typeof(**r) == MP_STR); // second
mp_next(&r);
assert(mp_typeof(**r) == MP_UINT); // 20
mp_next(&r);
assert(mp_typeof(**r) == MP_BOOL); // true
mp_next(&r);
// Third MsgPack object
assert(mp_typeof(**r) == MP_STR); // third
mp_next(&r);
assert(r == w); // EOF
Parametersdata - the pointer to a buffer
Postcondition
*data = *data + mp_sizeof_TYPE() where TYPE is mp_typeof(**data)
MP_PROTOuint32_tmp_sizeof_array(uint32_tsize)
Calculate exact buffer size needed to store an array header of size elements. Maximum return value is 5.
For performance reasons you can preallocate buffer for maximum size without calling the function.
Parameterssize - a number of elements
Returns
buffer size in bytes (max is 5)
MP_PROTOuint32_tmp_sizeof_bin(uint32_tlen)
Equivalent to mp_sizeof_binl(len) + len.
Parameterslen - a string length
Returns
size in chars (max is 5 + len)
MP_PROTOuint32_tmp_sizeof_binl(uint32_tlen)
Calculate exact buffer size needed to store a binstring header of length num. Maximum return value is 5.
For performance reasons you can preallocate buffer for maximum size without calling the function.
Parameterslen - a string length
Returns
size in chars (max is 5)
MP_PROTOuint32_tmp_sizeof_bool(boolval)
Calculate exact buffer size needed to store a boolean value. The return value is always 1. The function
was added to provide integrity of the library.
Returns
buffer size in bytes (always 1)
MP_PROTOuint32_tmp_sizeof_double(doublenum)
Calculate exact buffer size needed to store a double num. The return value is either 5 or 9. The function
was added to provide integrity of the library. For performance reasons you can preallocate buffer for
maximum size without calling the function.
Parametersnum - a double
Returns
buffer size in bytes (5 or 9)
MP_PROTOuint32_tmp_sizeof_float(floatnum)
Calculate exact buffer size needed to store a float num. The return value is always 5. The function was
added to provide integrity of the library.
Parametersnum - a float
Returns
buffer size in bytes (always 5)
MP_PROTOuint32_tmp_sizeof_int(int64_tnum)
Calculate exact buffer size needed to store an integer num. Maximum return value is 9. For performance
reasons you can preallocate buffer for maximum size without calling the function.
Parametersnum - a number
Returns
buffer size in bytes (max is 9)
Preconditionnum < 0
MP_PROTOuint32_tmp_sizeof_map(uint32_tsize)
Calculate exact buffer size needed to store a map header of size elements. Maximum return value is 5. For
performance reasons you can preallocate buffer for maximum size without calling the function.
Parameterssize - a number of elements
Returns
buffer size in bytes (max is 5)
MP_PROTOuint32_tmp_sizeof_nil(void)
Calculate exact buffer size needed to store the nil value. The return value is always 1. The function was
added to provide integrity of the library.
Returns
buffer size in bytes (always 1)
MP_PROTOuint32_tmp_sizeof_str(uint32_tlen)
Equivalent to mp_sizeof_strl(len) + len.
Parameterslen - a string length
Returns
size in chars (max is 5 + len)
MP_PROTOuint32_tmp_sizeof_strl(uint32_tlen)
Calculate exact buffer size needed to store a string header of length num. Maximum return value is 5. For
performance reasons you can preallocate buffer for maximum size without calling the function.
Parameterslen - a string length
Returns
size in chars (max is 5)
MP_PROTOuint32_tmp_sizeof_uint(uint64_tnum)
Calculate exact buffer size needed to store an integer num. Maximum return value is 9. For performance
reasons you can preallocate buffer for maximum size without calling the function. Example usage:
char **data = ...;
char *end = *data;
my_buffer_ensure(mp_sizeof_uint(x), &end);
// my_buffer_ensure(9, &end);
mp_encode_uint(buffer, x);
Parametersnum - a number
Returns
buffer size in bytes (max is 9)
MP_PROTOenummp_typemp_typeof(constcharc)
Determine MsgPack type by a first byte c of encoded data. Example usage:
assert(MP_ARRAY == mp_typeof(0x90));
Parametersc - a first byte of encoded data
Returns
MsgPack type
MP_PROTOsize_tmp_vformat(char*data,size_tdata_size,constchar*format,va_listargs)
mp_format variation, taking variable argument list Example: va_list args; va_start(args, fmt);
mp_vformat(data, data_size, fmt, args); va_end(args);
Seealsomp_format()