.TH "msgpuck.h" 3 "Sun Jun 4 2017" "MsgPuck" \" -*- nroff -*- .ad l .nh .SH NAME msgpuck.h \- MsgPuck is a simple and efficient MsgPack encoder/decoder library in a single self-contained file\&. .SH SYNOPSIS .br .PP \fC#include \fP .br \fC#include \fP .br \fC#include \fP .br \fC#include \fP .br \fC#include \fP .br \fC#include \fP .br \fC#include \fP .br \fC#include \fP .br .SS "Enumerations" .in +1c .ti -1c .RI "enum \fBmp_type\fP { \fBMP_NIL\fP = 0, \fBMP_UINT\fP, \fBMP_INT\fP, \fBMP_STR\fP, \fBMP_BIN\fP, \fBMP_ARRAY\fP, \fBMP_MAP\fP, \fBMP_BOOL\fP, \fBMP_FLOAT\fP, \fBMP_DOUBLE\fP, \fBMP_EXT\fP } .RI "MsgPack data types\&. "" .br .in -1c .SS "Functions" .in +1c .ti -1c .RI "MP_PROTO enum \fBmp_type\fP \fBmp_typeof\fP (const char c)" .br .RI "Determine MsgPack type by a first byte \fIc\fP of encoded data\&. " .ti -1c .RI "MP_PROTO uint32_t \fBmp_sizeof_array\fP (uint32_t size)" .br .RI "Calculate exact buffer size needed to store an array header of \fIsize\fP elements\&. Maximum return value is 5\&. For performance reasons you can preallocate buffer for maximum size without calling the function\&. " .ti -1c .RI "MP_PROTO char * \fBmp_encode_array\fP (char *data, uint32_t size)" .br .RI "Encode an array header of \fIsize\fP elements\&. " .ti -1c .RI "MP_PROTO ptrdiff_t \fBmp_check_array\fP (const char *cur, const char *end)" .br .RI "Check that \fIcur\fP buffer has enough bytes to decode an array header\&. " .ti -1c .RI "MP_PROTO uint32_t \fBmp_decode_array\fP (const char **data)" .br .RI "Decode an array header from MsgPack \fIdata\fP\&. " .ti -1c .RI "MP_PROTO uint32_t \fBmp_sizeof_map\fP (uint32_t size)" .br .RI "Calculate exact buffer size needed to store a map header of \fIsize\fP elements\&. Maximum return value is 5\&. For performance reasons you can preallocate buffer for maximum size without calling the function\&. " .ti -1c .RI "MP_PROTO char * \fBmp_encode_map\fP (char *data, uint32_t size)" .br .RI "Encode a map header of \fIsize\fP elements\&. " .ti -1c .RI "MP_PROTO ptrdiff_t \fBmp_check_map\fP (const char *cur, const char *end)" .br .RI "Check that \fIcur\fP buffer has enough bytes to decode a map header\&. " .ti -1c .RI "MP_PROTO uint32_t \fBmp_decode_map\fP (const char **data)" .br .RI "Decode a map header from MsgPack \fIdata\fP\&. " .ti -1c .RI "MP_PROTO uint32_t \fBmp_sizeof_uint\fP (uint64_t num)" .br .RI "Calculate exact buffer size needed to store an integer \fInum\fP\&. Maximum return value is 9\&. For performance reasons you can preallocate buffer for maximum size without calling the function\&. Example usage: " .ti -1c .RI "MP_PROTO uint32_t \fBmp_sizeof_int\fP (int64_t num)" .br .RI "Calculate exact buffer size needed to store an integer \fInum\fP\&. Maximum return value is 9\&. For performance reasons you can preallocate buffer for maximum size without calling the function\&. " .ti -1c .RI "MP_PROTO char * \fBmp_encode_uint\fP (char *data, uint64_t num)" .br .RI "Encode an unsigned integer \fInum\fP\&. It is your responsibility to ensure that \fIdata\fP has enough space\&. " .ti -1c .RI "MP_PROTO char * \fBmp_encode_int\fP (char *data, int64_t num)" .br .RI "Encode a signed integer \fInum\fP\&. It is your responsibility to ensure that \fIdata\fP has enough space\&. " .ti -1c .RI "MP_PROTO ptrdiff_t \fBmp_check_uint\fP (const char *cur, const char *end)" .br .RI "Check that \fIcur\fP buffer has enough bytes to decode an uint\&. " .ti -1c .RI "MP_PROTO ptrdiff_t \fBmp_check_int\fP (const char *cur, const char *end)" .br .RI "Check that \fIcur\fP buffer has enough bytes to decode an int\&. " .ti -1c .RI "MP_PROTO uint64_t \fBmp_decode_uint\fP (const char **data)" .br .RI "Decode an unsigned integer from MsgPack \fIdata\fP\&. " .ti -1c .RI "MP_PROTO int64_t \fBmp_decode_int\fP (const char **data)" .br .RI "Decode a signed integer from MsgPack \fIdata\fP\&. " .ti -1c .RI "MP_PROTO int \fBmp_compare_uint\fP (const char *data_a, const char *data_b)" .br .RI "Compare two packed unsigned integers\&. " .ti -1c .RI "MP_PROTO uint32_t \fBmp_sizeof_float\fP (float num)" .br .RI "Calculate exact buffer size needed to store a float \fInum\fP\&. The return value is always 5\&. The function was added to provide integrity of the library\&. " .ti -1c .RI "MP_PROTO uint32_t \fBmp_sizeof_double\fP (double num)" .br .RI "Calculate exact buffer size needed to store a double \fInum\fP\&. 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\&. " .ti -1c .RI "MP_PROTO char * \fBmp_encode_float\fP (char *data, float num)" .br .RI "Encode a float \fInum\fP\&. It is your responsibility to ensure that \fIdata\fP has enough space\&. " .ti -1c .RI "MP_PROTO char * \fBmp_encode_double\fP (char *data, double num)" .br .RI "Encode a double \fInum\fP\&. It is your responsibility to ensure that \fIdata\fP has enough space\&. " .ti -1c .RI "MP_PROTO ptrdiff_t \fBmp_check_float\fP (const char *cur, const char *end)" .br .RI "Check that \fIcur\fP buffer has enough bytes to decode a float\&. " .ti -1c .RI "MP_PROTO ptrdiff_t \fBmp_check_double\fP (const char *cur, const char *end)" .br .RI "Check that \fIcur\fP buffer has enough bytes to decode a double\&. " .ti -1c .RI "MP_PROTO float \fBmp_decode_float\fP (const char **data)" .br .RI "Decode a float from MsgPack \fIdata\fP\&. " .ti -1c .RI "MP_PROTO double \fBmp_decode_double\fP (const char **data)" .br .RI "Decode a double from MsgPack \fIdata\fP\&. " .ti -1c .RI "MP_PROTO uint32_t \fBmp_sizeof_strl\fP (uint32_t len)" .br .RI "Calculate exact buffer size needed to store a string header of length \fInum\fP\&. Maximum return value is 5\&. For performance reasons you can preallocate buffer for maximum size without calling the function\&. " .ti -1c .RI "MP_PROTO uint32_t \fBmp_sizeof_str\fP (uint32_t len)" .br .RI "Equivalent to mp_sizeof_strl(\fIlen\fP) + \fIlen\fP\&. " .ti -1c .RI "MP_PROTO uint32_t \fBmp_sizeof_binl\fP (uint32_t len)" .br .RI "Calculate exact buffer size needed to store a binstring header of length \fInum\fP\&. Maximum return value is 5\&. For performance reasons you can preallocate buffer for maximum size without calling the function\&. " .ti -1c .RI "MP_PROTO uint32_t \fBmp_sizeof_bin\fP (uint32_t len)" .br .RI "Equivalent to mp_sizeof_binl(\fIlen\fP) + \fIlen\fP\&. " .ti -1c .RI "MP_PROTO char * \fBmp_encode_strl\fP (char *data, uint32_t len)" .br .RI "Encode a string header of length \fIlen\fP\&. " .ti -1c .RI "MP_PROTO char * \fBmp_encode_str\fP (char *data, const char *str, uint32_t len)" .br .RI "Encode a string of length \fIlen\fP\&. The function is equivalent to \fBmp_encode_strl()\fP + memcpy\&. " .ti -1c .RI "MP_PROTO char * \fBmp_encode_binl\fP (char *data, uint32_t len)" .br .RI "Encode a binstring header of length \fIlen\fP\&. See \fBmp_encode_strl()\fP for more details\&. " .ti -1c .RI "MP_PROTO char * \fBmp_encode_bin\fP (char *data, const char *str, uint32_t len)" .br .RI "Encode a binstring of length \fIlen\fP\&. The function is equivalent to \fBmp_encode_binl()\fP + memcpy\&. " .ti -1c .RI "MP_PROTO size_t \fBmp_format\fP (char *data, size_t data_size, const char *format,\&.\&.\&.)" .br .RI "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\&. " .ti -1c .RI "MP_PROTO size_t \fBmp_vformat\fP (char *data, size_t data_size, const char *format, va_list args)" .br .RI "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); " .ti -1c .RI "MP_PROTO int \fBmp_fprint\fP (FILE *file, const char *data)" .br .RI "print to \fIfile\fP msgpacked data in JSON format\&. MP_EXT is printed as 'EXT' only " .ti -1c .RI "MP_PROTO ptrdiff_t \fBmp_check_strl\fP (const char *cur, const char *end)" .br .RI "Check that \fIcur\fP buffer has enough bytes to decode a string header\&. " .ti -1c .RI "MP_PROTO ptrdiff_t \fBmp_check_binl\fP (const char *cur, const char *end)" .br .RI "Check that \fIcur\fP buffer has enough bytes to decode a binstring header\&. " .ti -1c .RI "MP_PROTO uint32_t \fBmp_decode_strl\fP (const char **data)" .br .RI "Decode a length of a string from MsgPack \fIdata\fP\&. " .ti -1c .RI "MP_PROTO const char * \fBmp_decode_str\fP (const char **data, uint32_t *len)" .br .RI "Decode a string from MsgPack \fIdata\fP\&. " .ti -1c .RI "MP_PROTO uint32_t \fBmp_decode_binl\fP (const char **data)" .br .RI "Decode a length of a binstring from MsgPack \fIdata\fP\&. " .ti -1c .RI "MP_PROTO const char * \fBmp_decode_bin\fP (const char **data, uint32_t *len)" .br .RI "Decode a binstring from MsgPack \fIdata\fP\&. " .ti -1c .RI "MP_PROTO uint32_t \fBmp_decode_strbinl\fP (const char **data)" .br .RI "Decode a length of a string or binstring from MsgPack \fIdata\fP\&. " .ti -1c .RI "MP_PROTO const char * \fBmp_decode_strbin\fP (const char **data, uint32_t *len)" .br .RI "Decode a string or binstring from MsgPack \fIdata\fP\&. " .ti -1c .RI "MP_PROTO uint32_t \fBmp_sizeof_nil\fP (void)" .br .RI "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\&. " .ti -1c .RI "MP_PROTO char * \fBmp_encode_nil\fP (char *data)" .br .RI "Encode the nil value\&. It is your responsibility to ensure that \fIdata\fP has enough space\&. " .ti -1c .RI "MP_PROTO ptrdiff_t \fBmp_check_nil\fP (const char *cur, const char *end)" .br .RI "Check that \fIcur\fP buffer has enough bytes to decode nil\&. " .ti -1c .RI "MP_PROTO void \fBmp_decode_nil\fP (const char **data)" .br .RI "Decode the nil value from MsgPack \fIdata\fP\&. " .ti -1c .RI "MP_PROTO uint32_t \fBmp_sizeof_bool\fP (bool val)" .br .RI "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\&. " .ti -1c .RI "MP_PROTO char * \fBmp_encode_bool\fP (char *data, bool val)" .br .RI "Encode a bool value \fIval\fP\&. It is your responsibility to ensure that \fIdata\fP has enough space\&. " .ti -1c .RI "MP_PROTO ptrdiff_t \fBmp_check_bool\fP (const char *cur, const char *end)" .br .RI "Check that \fIcur\fP buffer has enough bytes to decode a bool value\&. " .ti -1c .RI "MP_PROTO bool \fBmp_decode_bool\fP (const char **data)" .br .RI "Decode a bool value from MsgPack \fIdata\fP\&. " .ti -1c .RI "MP_PROTO void \fBmp_next\fP (const char **data)" .br .RI "Skip one element in a packed \fIdata\fP\&. " .ti -1c .RI "MP_PROTO int \fBmp_check\fP (const char **data, const char *end)" .br .RI "Equivalent to \fBmp_next()\fP but also validates MsgPack in \fIdata\fP\&. " .in -1c .SH "Detailed Description" .PP MsgPuck is a simple and efficient MsgPack encoder/decoder library in a single self-contained file\&. MsgPuck Usage example: .PP .nf // Encode char buf[1024]; char *w = buf; w = mp_encode_array(w, 4) w = mp_encode_uint(w, 10); w = mp_encode_str(w, "hello world", strlen("hello world")); w = mp_encode_bool(w, true); w = mp_encode_double(w, 3\&.1415); // Validate const char *b = buf; int r = mp_check(&b, w); assert(!r) assert(b == w); // Decode uint32_t size; uint64_t ival; const char *sval; uint32_t sval_len; bool bval; double dval; const char *r = buf; size = mp_decode_array(&r); // size is 4 ival = mp_decode_uint(&r); // ival is 10; sval = mp_decode_str(&r, &sval_len); // sval is "hello world", sval_len is strlen("hello world") bval = mp_decode_bool(&r); // bval is true dval = mp_decode_double(&r); // dval is 3\&.1415 assert(r == w); .fi .PP .PP \fBNote:\fP .RS 4 Supported compilers\&. The implementation requires a C99+ or C++03+ compatible compiler\&. .PP Inline functions\&. The implementation is compatible with both C99 and GNU inline functions\&. Please define MP_SOURCE 1 before #include <\fBmsgpuck\&.h\fP> in a single compilation unit\&. This module will be used to store non-inlined versions of functions and global tables\&. .RE .PP .SH "Function Documentation" .PP .SS "MP_PROTO int mp_check (const char ** data, const char * end)" .PP Equivalent to \fBmp_next()\fP but also validates MsgPack in \fIdata\fP\&. .PP \fBParameters:\fP .RS 4 \fIdata\fP - the pointer to a buffer .br \fIend\fP - the end of a buffer .RE .PP \fBReturn values:\fP .RS 4 \fI0\fP when MsgPack in \fIdata\fP is valid\&. .br \fI!=\fP 0 when MsgPack in \fIdata\fP is not valid\&. .RE .PP \fBPostcondition:\fP .RS 4 *data = *data + mp_sizeof_TYPE() where TYPE is mp_typeof(**data) .PP *data is not defined if MsgPack is not valid .RE .PP \fBSee also:\fP .RS 4 \fBmp_next()\fP .RE .PP .SS "MP_PROTO ptrdiff_t mp_check_array (const char * cur, const char * end)" .PP Check that \fIcur\fP buffer has enough bytes to decode an array header\&. .PP \fBParameters:\fP .RS 4 \fIcur\fP buffer .br \fIend\fP end of the buffer .RE .PP \fBReturn values:\fP .RS 4 \fI0\fP - buffer has enough bytes .br \fI>\fP 0 - the number of remaining bytes to read .RE .PP \fBPrecondition:\fP .RS 4 cur < end .PP mp_typeof(*cur) == MP_ARRAY .RE .PP .SS "MP_PROTO ptrdiff_t mp_check_binl (const char * cur, const char * end)" .PP Check that \fIcur\fP buffer has enough bytes to decode a binstring header\&. .PP \fBParameters:\fP .RS 4 \fIcur\fP buffer .br \fIend\fP end of the buffer .RE .PP \fBReturn values:\fP .RS 4 \fI0\fP - buffer has enough bytes .br \fI>\fP 0 - the number of remaining bytes to read .RE .PP \fBPrecondition:\fP .RS 4 cur < end .PP mp_typeof(*cur) == MP_BIN .RE .PP .SS "MP_PROTO ptrdiff_t mp_check_bool (const char * cur, const char * end)" .PP Check that \fIcur\fP buffer has enough bytes to decode a bool value\&. .PP \fBParameters:\fP .RS 4 \fIcur\fP buffer .br \fIend\fP end of the buffer .RE .PP \fBReturn values:\fP .RS 4 \fI0\fP - buffer has enough bytes .br \fI>\fP 0 - the number of remaining bytes to read .RE .PP \fBPrecondition:\fP .RS 4 cur < end .PP mp_typeof(*cur) == MP_BOOL .RE .PP .SS "MP_PROTO ptrdiff_t mp_check_double (const char * cur, const char * end)" .PP Check that \fIcur\fP buffer has enough bytes to decode a double\&. .PP \fBParameters:\fP .RS 4 \fIcur\fP buffer .br \fIend\fP end of the buffer .RE .PP \fBReturn values:\fP .RS 4 \fI0\fP - buffer has enough bytes .br \fI>\fP 0 - the number of remaining bytes to read .RE .PP \fBPrecondition:\fP .RS 4 cur < end .PP mp_typeof(*cur) == MP_DOUBLE .RE .PP .SS "MP_PROTO ptrdiff_t mp_check_float (const char * cur, const char * end)" .PP Check that \fIcur\fP buffer has enough bytes to decode a float\&. .PP \fBParameters:\fP .RS 4 \fIcur\fP buffer .br \fIend\fP end of the buffer .RE .PP \fBReturn values:\fP .RS 4 \fI0\fP - buffer has enough bytes .br \fI>\fP 0 - the number of remaining bytes to read .RE .PP \fBPrecondition:\fP .RS 4 cur < end .PP mp_typeof(*cur) == MP_FLOAT .RE .PP .SS "MP_PROTO ptrdiff_t mp_check_int (const char * cur, const char * end)" .PP Check that \fIcur\fP buffer has enough bytes to decode an int\&. .PP \fBParameters:\fP .RS 4 \fIcur\fP buffer .br \fIend\fP end of the buffer .RE .PP \fBReturn values:\fP .RS 4 \fI0\fP - buffer has enough bytes .br \fI>\fP 0 - the number of remaining bytes to read .RE .PP \fBPrecondition:\fP .RS 4 cur < end .PP mp_typeof(*cur) == MP_INT .RE .PP .SS "MP_PROTO ptrdiff_t mp_check_map (const char * cur, const char * end)" .PP Check that \fIcur\fP buffer has enough bytes to decode a map header\&. .PP \fBParameters:\fP .RS 4 \fIcur\fP buffer .br \fIend\fP end of the buffer .RE .PP \fBReturn values:\fP .RS 4 \fI0\fP - buffer has enough bytes .br \fI>\fP 0 - the number of remaining bytes to read .RE .PP \fBPrecondition:\fP .RS 4 cur < end .PP mp_typeof(*cur) == MP_MAP .RE .PP .SS "MP_PROTO ptrdiff_t mp_check_nil (const char * cur, const char * end)" .PP Check that \fIcur\fP buffer has enough bytes to decode nil\&. .PP \fBParameters:\fP .RS 4 \fIcur\fP buffer .br \fIend\fP end of the buffer .RE .PP \fBReturn values:\fP .RS 4 \fI0\fP - buffer has enough bytes .br \fI>\fP 0 - the number of remaining bytes to read .RE .PP \fBPrecondition:\fP .RS 4 cur < end .PP mp_typeof(*cur) == MP_NIL .RE .PP .SS "MP_PROTO ptrdiff_t mp_check_strl (const char * cur, const char * end)" .PP Check that \fIcur\fP buffer has enough bytes to decode a string header\&. .PP \fBParameters:\fP .RS 4 \fIcur\fP buffer .br \fIend\fP end of the buffer .RE .PP \fBReturn values:\fP .RS 4 \fI0\fP - buffer has enough bytes .br \fI>\fP 0 - the number of remaining bytes to read .RE .PP \fBPrecondition:\fP .RS 4 cur < end .PP mp_typeof(*cur) == MP_STR .RE .PP .SS "MP_PROTO ptrdiff_t mp_check_uint (const char * cur, const char * end)" .PP Check that \fIcur\fP buffer has enough bytes to decode an uint\&. .PP \fBParameters:\fP .RS 4 \fIcur\fP buffer .br \fIend\fP end of the buffer .RE .PP \fBReturn values:\fP .RS 4 \fI0\fP - buffer has enough bytes .br \fI>\fP 0 - the number of remaining bytes to read .RE .PP \fBPrecondition:\fP .RS 4 cur < end .PP mp_typeof(*cur) == MP_UINT .RE .PP .SS "MP_PROTO int mp_compare_uint (const char * data_a, const char * data_b)" .PP Compare two packed unsigned integers\&. The function is faster than two \fBmp_decode_uint()\fP calls\&. .PP \fBParameters:\fP .RS 4 \fIdata_a\fP unsigned int a .br \fIdata_b\fP unsigned int b .RE .PP \fBReturn values:\fP .RS 4 \fI<\fP 0 when \fIa\fP < \fIb\fP .br \fI0\fP when \fIa\fP == \fIb\fP .br \fI>\fP 0 when \fIa\fP > \fIb\fP .RE .PP .SS "MP_PROTO uint32_t mp_decode_array (const char ** data)" .PP Decode an array header from MsgPack \fIdata\fP\&. All array members must be decoded after the header\&. .PP \fBParameters:\fP .RS 4 \fIdata\fP - the pointer to a buffer .RE .PP \fBReturns:\fP .RS 4 the number of elements in an array .RE .PP \fBPostcondition:\fP .RS 4 *data = *data + mp_sizeof_array(retval) .RE .PP \fBSee also:\fP .RS 4 \fBAn usage example \fP .RE .PP .SS "MP_PROTO const char* mp_decode_bin (const char ** data, uint32_t * len)" .PP Decode a binstring from MsgPack \fIdata\fP\&. .PP \fBParameters:\fP .RS 4 \fIdata\fP - the pointer to a buffer .br \fIlen\fP - the pointer to save a binstring length .RE .PP \fBReturns:\fP .RS 4 a pointer to a decoded binstring .RE .PP \fBPostcondition:\fP .RS 4 *data = *data + mp_sizeof_str(*len) .RE .PP \fBSee also:\fP .RS 4 \fBmp_encode_binl\fP .RE .PP .SS "MP_PROTO uint32_t mp_decode_binl (const char ** data)" .PP Decode a length of a binstring from MsgPack \fIdata\fP\&. .PP \fBParameters:\fP .RS 4 \fIdata\fP - the pointer to a buffer .RE .PP \fBReturns:\fP .RS 4 a length of a binstring .RE .PP \fBPostcondition:\fP .RS 4 *data = *data + mp_sizeof_binl(retval) .RE .PP \fBSee also:\fP .RS 4 \fBmp_encode_binl\fP .RE .PP .SS "MP_PROTO bool mp_decode_bool (const char ** data)" .PP Decode a bool value from MsgPack \fIdata\fP\&. .PP \fBParameters:\fP .RS 4 \fIdata\fP - the pointer to a buffer .RE .PP \fBReturns:\fP .RS 4 a decoded bool value .RE .PP \fBPostcondition:\fP .RS 4 *data = *data + mp_sizeof_bool(retval) .RE .PP .SS "MP_PROTO double mp_decode_double (const char ** data)" .PP Decode a double from MsgPack \fIdata\fP\&. .PP \fBParameters:\fP .RS 4 \fIdata\fP - the pointer to a buffer .RE .PP \fBReturns:\fP .RS 4 a double .RE .PP \fBPostcondition:\fP .RS 4 *data = *data + mp_sizeof_double(retval) .RE .PP .SS "MP_PROTO float mp_decode_float (const char ** data)" .PP Decode a float from MsgPack \fIdata\fP\&. .PP \fBParameters:\fP .RS 4 \fIdata\fP - the pointer to a buffer .RE .PP \fBReturns:\fP .RS 4 a float .RE .PP \fBPostcondition:\fP .RS 4 *data = *data + mp_sizeof_float(retval) .RE .PP .SS "MP_PROTO int64_t mp_decode_int (const char ** data)" .PP Decode a signed integer from MsgPack \fIdata\fP\&. .PP \fBParameters:\fP .RS 4 \fIdata\fP - the pointer to a buffer .RE .PP \fBReturns:\fP .RS 4 an unsigned number .RE .PP \fBPostcondition:\fP .RS 4 *data = *data + mp_sizeof_int(retval) .RE .PP .SS "MP_PROTO uint32_t mp_decode_map (const char ** data)" .PP Decode a map header from MsgPack \fIdata\fP\&. All map key-value pairs must be decoded after the header\&. .PP \fBParameters:\fP .RS 4 \fIdata\fP - the pointer to a buffer .RE .PP \fBReturns:\fP .RS 4 the number of key/value pairs in a map .RE .PP \fBPostcondition:\fP .RS 4 *data = *data + mp_sizeof_array(retval) .RE .PP \fBSee also:\fP .RS 4 \fBAn usage example \fP .RE .PP .SS "MP_PROTO void mp_decode_nil (const char ** data)" .PP Decode the nil value from MsgPack \fIdata\fP\&. .PP \fBParameters:\fP .RS 4 \fIdata\fP - the pointer to a buffer .RE .PP \fBPostcondition:\fP .RS 4 *data = *data + \fBmp_sizeof_nil()\fP .RE .PP .SS "MP_PROTO const char* mp_decode_str (const char ** data, uint32_t * len)" .PP Decode a string from MsgPack \fIdata\fP\&. .PP \fBParameters:\fP .RS 4 \fIdata\fP - the pointer to a buffer .br \fIlen\fP - the pointer to save a string length .RE .PP \fBReturns:\fP .RS 4 a pointer to a decoded string .RE .PP \fBPostcondition:\fP .RS 4 *data = *data + mp_sizeof_str(*len) .RE .PP \fBSee also:\fP .RS 4 \fBmp_encode_binl\fP .RE .PP .SS "MP_PROTO const char* mp_decode_strbin (const char ** data, uint32_t * len)" .PP Decode a string or binstring from MsgPack \fIdata\fP\&. .PP \fBParameters:\fP .RS 4 \fIdata\fP - the pointer to a buffer .br \fIlen\fP - the pointer to save a binstring length .RE .PP \fBReturns:\fP .RS 4 a pointer to a decoded binstring .RE .PP \fBPostcondition:\fP .RS 4 *data = *data + mp_sizeof_strbinl(*len) .RE .PP \fBSee also:\fP .RS 4 \fBmp_encode_binl\fP .RE .PP .SS "MP_PROTO uint32_t mp_decode_strbinl (const char ** data)" .PP Decode a length of a string or binstring from MsgPack \fIdata\fP\&. .PP \fBParameters:\fP .RS 4 \fIdata\fP - the pointer to a buffer .RE .PP \fBReturns:\fP .RS 4 a length of a string .RE .PP \fBPostcondition:\fP .RS 4 *data = *data + mp_sizeof_strbinl(retval) .RE .PP \fBSee also:\fP .RS 4 \fBmp_encode_binl\fP .RE .PP .SS "MP_PROTO uint32_t mp_decode_strl (const char ** data)" .PP Decode a length of a string from MsgPack \fIdata\fP\&. .PP \fBParameters:\fP .RS 4 \fIdata\fP - the pointer to a buffer .RE .PP \fBReturns:\fP .RS 4 a length of astring .RE .PP \fBPostcondition:\fP .RS 4 *data = *data + mp_sizeof_strl(retval) .RE .PP \fBSee also:\fP .RS 4 \fBmp_encode_strl\fP .RE .PP .SS "MP_PROTO uint64_t mp_decode_uint (const char ** data)" .PP Decode an unsigned integer from MsgPack \fIdata\fP\&. .PP \fBParameters:\fP .RS 4 \fIdata\fP - the pointer to a buffer .RE .PP \fBReturns:\fP .RS 4 an unsigned number .RE .PP \fBPostcondition:\fP .RS 4 *data = *data + mp_sizeof_uint(retval) .RE .PP .SS "MP_PROTO char* mp_encode_array (char * data, uint32_t size)" .PP Encode an array header of \fIsize\fP elements\&. All array members must be encoded after the header\&. .PP Example usage: .PP .nf // 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); .fi .PP It is your responsibility to ensure that \fIdata\fP has enough space\&. .PP \fBParameters:\fP .RS 4 \fIdata\fP - a buffer .br \fIsize\fP - a number of elements .RE .PP \fBReturns:\fP .RS 4 \fIdata\fP + \fBmp_sizeof_array(size) \fP .RE .PP \fBSee also:\fP .RS 4 \fBmp_sizeof_array\fP .RE .PP .SS "MP_PROTO char* mp_encode_bin (char * data, const char * str, uint32_t len)" .PP Encode a binstring of length \fIlen\fP\&. The function is equivalent to \fBmp_encode_binl()\fP + memcpy\&. .PP \fBParameters:\fP .RS 4 \fIdata\fP - a buffer .br \fIstr\fP - a pointer to binstring data .br \fIlen\fP - a binstring length .RE .PP \fBReturns:\fP .RS 4 \fIdata\fP + mp_sizeof_bin(\fIlen\fP) == data + mp_sizeof_binl(\fIlen\fP) + \fIlen\fP .RE .PP \fBSee also:\fP .RS 4 \fBmp_encode_strl\fP .RE .PP .SS "MP_PROTO char* mp_encode_binl (char * data, uint32_t len)" .PP Encode a binstring header of length \fIlen\fP\&. See \fBmp_encode_strl()\fP for more details\&. .PP \fBParameters:\fP .RS 4 \fIdata\fP - a bufer .br \fIlen\fP - a string length .RE .PP \fBReturns:\fP .RS 4 data + mp_sizeof_binl(\fIlen\fP) .RE .PP \fBSee also:\fP .RS 4 \fBmp_encode_strl\fP .RE .PP .SS "MP_PROTO char* mp_encode_bool (char * data, bool val)" .PP Encode a bool value \fIval\fP\&. It is your responsibility to ensure that \fIdata\fP has enough space\&. .PP \fBParameters:\fP .RS 4 \fIdata\fP - a buffer .br \fIval\fP - a bool .RE .PP \fBReturns:\fP .RS 4 \fIdata\fP + mp_sizeof_bool(val) .RE .PP \fBSee also:\fP .RS 4 \fBAn usage example \fP .PP \fBmp_sizeof_bool()\fP .RE .PP .SS "MP_PROTO char* mp_encode_double (char * data, double num)" .PP Encode a double \fInum\fP\&. It is your responsibility to ensure that \fIdata\fP has enough space\&. .PP \fBParameters:\fP .RS 4 \fIdata\fP - a buffer .br \fInum\fP - a float .RE .PP \fBReturns:\fP .RS 4 \fIdata\fP + mp_sizeof_double(\fInum\fP) .RE .PP \fBSee also:\fP .RS 4 \fBAn usage example \fP .PP \fBmp_sizeof_double()\fP .RE .PP .SS "MP_PROTO char* mp_encode_float (char * data, float num)" .PP Encode a float \fInum\fP\&. It is your responsibility to ensure that \fIdata\fP has enough space\&. .PP \fBParameters:\fP .RS 4 \fIdata\fP - a buffer .br \fInum\fP - a float .RE .PP \fBReturns:\fP .RS 4 \fIdata\fP + mp_sizeof_float(\fInum\fP) .RE .PP \fBSee also:\fP .RS 4 \fBmp_sizeof_float()\fP .PP \fBAn usage example \fP .RE .PP .SS "MP_PROTO char* mp_encode_int (char * data, int64_t num)" .PP Encode a signed integer \fInum\fP\&. It is your responsibility to ensure that \fIdata\fP has enough space\&. .PP \fBParameters:\fP .RS 4 \fIdata\fP - a buffer .br \fInum\fP - a number .RE .PP \fBReturns:\fP .RS 4 \fIdata\fP + mp_sizeof_int(\fInum\fP) .RE .PP \fBSee also:\fP .RS 4 \fBAn usage example \fP .PP \fBmp_sizeof_int()\fP .RE .PP \fBPrecondition:\fP .RS 4 \fInum\fP < 0 .RE .PP .SS "MP_PROTO char* mp_encode_map (char * data, uint32_t size)" .PP Encode a map header of \fIsize\fP elements\&. All map key-value pairs must be encoded after the header\&. .PP Example usage: .PP .nf 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); .fi .PP It is your responsibility to ensure that \fIdata\fP has enough space\&. .PP \fBParameters:\fP .RS 4 \fIdata\fP - a buffer .br \fIsize\fP - a number of key/value pairs .RE .PP \fBReturns:\fP .RS 4 \fIdata\fP + \fBmp_sizeof_map(size)\fP .RE .PP \fBSee also:\fP .RS 4 \fBmp_sizeof_map\fP .RE .PP .SS "MP_PROTO char* mp_encode_nil (char * data)" .PP Encode the nil value\&. It is your responsibility to ensure that \fIdata\fP has enough space\&. .PP \fBParameters:\fP .RS 4 \fIdata\fP - a buffer .RE .PP \fBReturns:\fP .RS 4 \fIdata\fP + \fBmp_sizeof_nil()\fP .RE .PP \fBSee also:\fP .RS 4 \fBAn usage example \fP .PP \fBmp_sizeof_nil()\fP .RE .PP .SS "MP_PROTO char* mp_encode_str (char * data, const char * str, uint32_t len)" .PP Encode a string of length \fIlen\fP\&. The function is equivalent to \fBmp_encode_strl()\fP + memcpy\&. .PP \fBParameters:\fP .RS 4 \fIdata\fP - a buffer .br \fIstr\fP - a pointer to string data .br \fIlen\fP - a string length .RE .PP \fBReturns:\fP .RS 4 \fIdata\fP + mp_sizeof_str(len) == data + mp_sizeof_strl(len) + len .RE .PP \fBSee also:\fP .RS 4 \fBmp_encode_strl\fP .RE .PP .SS "MP_PROTO char* mp_encode_strl (char * data, uint32_t len)" .PP Encode a string header of length \fIlen\fP\&. The function encodes MsgPack header (\fIonly\fP header) for a string of length \fIlen\fP\&. You should append actual string data to the buffer manually after encoding the header (exactly \fIlen\fP bytes without trailing '\\0')\&. .PP 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)\&. .PP It is your responsibility to ensure that \fIdata\fP has enough space\&. Usage example: .PP .nf 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)) .fi .PP Hint: you can dynamically reallocate the buffer during the process\&. .PP \fBParameters:\fP .RS 4 \fIdata\fP - a buffer .br \fIlen\fP - a string length .RE .PP \fBReturns:\fP .RS 4 \fIdata\fP + mp_sizeof_strl(len) .RE .PP \fBSee also:\fP .RS 4 \fBmp_sizeof_strl()\fP .RE .PP .SS "MP_PROTO char* mp_encode_uint (char * data, uint64_t num)" .PP Encode an unsigned integer \fInum\fP\&. It is your responsibility to ensure that \fIdata\fP has enough space\&. .PP \fBParameters:\fP .RS 4 \fIdata\fP - a buffer .br \fInum\fP - a number .RE .PP \fBReturns:\fP .RS 4 \fIdata\fP + mp_sizeof_uint(\fInum\fP) .RE .PP \fBSee also:\fP .RS 4 \fBAn usage example \fP .PP \fBmp_sizeof_uint()\fP .RE .PP .SS "MP_PROTO size_t mp_format (char * data, size_t data_size, const char * format, \&.\&.\&.)" .PP 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\&. .PP \fBParameters:\fP .RS 4 \fIdata\fP - a buffer .br \fIdata_size\fP - a buffer size .br \fIformat\fP - 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\&. .RE .PP \fBReturns:\fP .RS 4 the number of requred bytes\&. .RE .PP \fBReturn values:\fP .RS 4 \fI>\fP data_size means that is not enough space and whole msgpack was not encoded\&. .RE .PP .SS "MP_PROTO int mp_fprint (FILE * file, const char * data)" .PP print to \fIfile\fP msgpacked data in JSON format\&. MP_EXT is printed as 'EXT' only .PP \fBParameters:\fP .RS 4 \fIfile\fP - pointer to file (or NULL for stdout) .br \fIdata\fP - pointer to buffer containing msgpack object .RE .PP \fBReturn values:\fP .RS 4 \fI0\fP - success .br \fI-1\fP - wrong msgpack .RE .PP .SS "MP_PROTO void mp_next (const char ** data)" .PP Skip one element in a packed \fIdata\fP\&. 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\&. .PP Usage example: .PP .nf 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 .fi .PP .PP \fBParameters:\fP .RS 4 \fIdata\fP - the pointer to a buffer .RE .PP \fBPostcondition:\fP .RS 4 *data = *data + mp_sizeof_TYPE() where TYPE is mp_typeof(**data) .RE .PP .SS "MP_PROTO uint32_t mp_sizeof_array (uint32_t size)" .PP Calculate exact buffer size needed to store an array header of \fIsize\fP elements\&. Maximum return value is 5\&. For performance reasons you can preallocate buffer for maximum size without calling the function\&. .PP \fBParameters:\fP .RS 4 \fIsize\fP - a number of elements .RE .PP \fBReturns:\fP .RS 4 buffer size in bytes (max is 5) .RE .PP .SS "MP_PROTO uint32_t mp_sizeof_bin (uint32_t len)" .PP Equivalent to mp_sizeof_binl(\fIlen\fP) + \fIlen\fP\&. .PP \fBParameters:\fP .RS 4 \fIlen\fP - a string length .RE .PP \fBReturns:\fP .RS 4 size in chars (max is 5 + \fIlen\fP) .RE .PP .SS "MP_PROTO uint32_t mp_sizeof_binl (uint32_t len)" .PP Calculate exact buffer size needed to store a binstring header of length \fInum\fP\&. Maximum return value is 5\&. For performance reasons you can preallocate buffer for maximum size without calling the function\&. .PP \fBParameters:\fP .RS 4 \fIlen\fP - a string length .RE .PP \fBReturns:\fP .RS 4 size in chars (max is 5) .RE .PP .SS "MP_PROTO uint32_t mp_sizeof_bool (bool val)" .PP 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\&. .PP \fBReturns:\fP .RS 4 buffer size in bytes (always 1) .RE .PP .SS "MP_PROTO uint32_t mp_sizeof_double (double num)" .PP Calculate exact buffer size needed to store a double \fInum\fP\&. 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\&. .PP \fBParameters:\fP .RS 4 \fInum\fP - a double .RE .PP \fBReturns:\fP .RS 4 buffer size in bytes (5 or 9) .RE .PP .SS "MP_PROTO uint32_t mp_sizeof_float (float num)" .PP Calculate exact buffer size needed to store a float \fInum\fP\&. The return value is always 5\&. The function was added to provide integrity of the library\&. .PP \fBParameters:\fP .RS 4 \fInum\fP - a float .RE .PP \fBReturns:\fP .RS 4 buffer size in bytes (always 5) .RE .PP .SS "MP_PROTO uint32_t mp_sizeof_int (int64_t num)" .PP Calculate exact buffer size needed to store an integer \fInum\fP\&. Maximum return value is 9\&. For performance reasons you can preallocate buffer for maximum size without calling the function\&. .PP \fBParameters:\fP .RS 4 \fInum\fP - a number .RE .PP \fBReturns:\fP .RS 4 buffer size in bytes (max is 9) .RE .PP \fBPrecondition:\fP .RS 4 \fInum\fP < 0 .RE .PP .SS "MP_PROTO uint32_t mp_sizeof_map (uint32_t size)" .PP Calculate exact buffer size needed to store a map header of \fIsize\fP elements\&. Maximum return value is 5\&. For performance reasons you can preallocate buffer for maximum size without calling the function\&. .PP \fBParameters:\fP .RS 4 \fIsize\fP - a number of elements .RE .PP \fBReturns:\fP .RS 4 buffer size in bytes (max is 5) .RE .PP .SS "MP_PROTO uint32_t mp_sizeof_nil (void)" .PP 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\&. .PP \fBReturns:\fP .RS 4 buffer size in bytes (always 1) .RE .PP .SS "MP_PROTO uint32_t mp_sizeof_str (uint32_t len)" .PP Equivalent to mp_sizeof_strl(\fIlen\fP) + \fIlen\fP\&. .PP \fBParameters:\fP .RS 4 \fIlen\fP - a string length .RE .PP \fBReturns:\fP .RS 4 size in chars (max is 5 + \fIlen\fP) .RE .PP .SS "MP_PROTO uint32_t mp_sizeof_strl (uint32_t len)" .PP Calculate exact buffer size needed to store a string header of length \fInum\fP\&. Maximum return value is 5\&. For performance reasons you can preallocate buffer for maximum size without calling the function\&. .PP \fBParameters:\fP .RS 4 \fIlen\fP - a string length .RE .PP \fBReturns:\fP .RS 4 size in chars (max is 5) .RE .PP .SS "MP_PROTO uint32_t mp_sizeof_uint (uint64_t num)" .PP Calculate exact buffer size needed to store an integer \fInum\fP\&. Maximum return value is 9\&. For performance reasons you can preallocate buffer for maximum size without calling the function\&. Example usage: .PP .nf char **data = \&.\&.\&.; char *end = *data; my_buffer_ensure(mp_sizeof_uint(x), &end); // my_buffer_ensure(9, &end); mp_encode_uint(buffer, x); .fi .PP .PP \fBParameters:\fP .RS 4 \fInum\fP - a number .RE .PP \fBReturns:\fP .RS 4 buffer size in bytes (max is 9) .RE .PP .SS "MP_PROTO enum \fBmp_type\fP mp_typeof (const char c)" .PP Determine MsgPack type by a first byte \fIc\fP of encoded data\&. Example usage: .PP .nf assert(MP_ARRAY == mp_typeof(0x90)); .fi .PP .PP \fBParameters:\fP .RS 4 \fIc\fP - a first byte of encoded data .RE .PP \fBReturns:\fP .RS 4 MsgPack type .RE .PP .SS "MP_PROTO size_t mp_vformat (char * data, size_t data_size, const char * format, va_list args)" .PP 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); .PP \fBSee also:\fP .RS 4 \fBmp_format() \fP .RE .PP .SH "Author" .PP Generated automatically by Doxygen for MsgPuck from the source code\&.