DR-Tarantool
view release on metacpan or search on metacpan
*/
MP_PROTO __attribute__((pure)) enum mp_type
mp_typeof(const char c);
/**
* \brief Calculate exact buffer size needed to store an array header of
* \a size elements. Maximum return value is 5. For performance reasons you
* can preallocate buffer for maximum size without calling the function.
* \param size - a number of elements
* \return buffer size in bytes (max is 5)
*/
MP_PROTO __attribute__((const)) uint32_t
mp_sizeof_array(uint32_t size);
/**
* \brief Encode an array header of \a size elements.
*
* All array members must be encoded after the header.
*
* Example usage:
* \code
* // 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);
* \endcode
* It is your responsibility to ensure that \a data has enough space.
* \param data - a buffer
* \param size - a number of elements
* \return \a data + \link mp_sizeof_array() mp_sizeof_array(size) \endlink
* \sa mp_sizeof_array
*/
MP_PROTO char *
mp_encode_array(char *data, uint32_t size);
/**
* \brief Check that \a cur buffer has enough bytes to decode an array header
* \param cur buffer
* \param end end of the buffer
* \retval 0 - buffer has enough bytes
* \retval > 0 - the number of remaining bytes to read
* \pre cur < end
* \pre mp_typeof(*cur) == MP_ARRAY
*/
MP_PROTO __attribute__((pure)) ptrdiff_t
mp_check_array(const char *cur, const char *end);
/**
* \brief Decode an array header from MsgPack \a data.
*
* All array members must be decoded after the header.
* \param data - the pointer to a buffer
* \return the number of elements in an array
* \post *data = *data + mp_sizeof_array(retval)
* \sa \link mp_encode_array() An usage example \endlink
*/
MP_PROTO uint32_t
mp_decode_array(const char **data);
/**
* \brief Calculate exact buffer size needed to store a map header of
* \a size elements. Maximum return value is 5. For performance reasons you
* can preallocate buffer for maximum size without calling the function.
* \param size - a number of elements
* \return buffer size in bytes (max is 5)
*/
MP_PROTO __attribute__((const)) uint32_t
mp_sizeof_map(uint32_t size);
/**
* \brief Encode a map header of \a size elements.
*
* All map key-value pairs must be encoded after the header.
*
* Example usage:
* \code
* 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);
* \endcode
* It is your responsibility to ensure that \a data has enough space.
* \param data - a buffer
* \param size - a number of key/value pairs
* \return \a data + \link mp_sizeof_map() mp_sizeof_map(size)\endlink
* \sa mp_sizeof_map
*/
MP_PROTO char *
mp_encode_map(char *data, uint32_t size);
/**
* \brief Check that \a cur buffer has enough bytes to decode a map header
* \param cur buffer
* \param end end of the buffer
* \retval 0 - buffer has enough bytes
* \retval > 0 - the number of remaining bytes to read
* \pre cur < end
* \pre mp_typeof(*cur) == MP_MAP
*/
MP_PROTO __attribute__((pure)) ptrdiff_t
mp_check_map(const char *cur, const char *end);
/**
* \brief Decode a map header from MsgPack \a data.
*
* All map key-value pairs must be decoded after the header.
* \param data - the pointer to a buffer
* \return the number of key/value pairs in a map
* \post *data = *data + mp_sizeof_array(retval)
* \sa \link mp_encode_map() An usage example \endlink
*/
MP_PROTO uint32_t
mp_decode_map(const char **data);
/**
* \brief Calculate exact buffer size needed to store an integer \a num.
* Maximum return value is 9. For performance reasons you can preallocate
* buffer for maximum size without calling the function.
* Example usage:
* \code
* char **data = ...;
* char *end = *data;
* my_buffer_ensure(mp_sizeof_uint(x), &end);
* // my_buffer_ensure(9, &end);
* mp_encode_uint(buffer, x);
* \endcode
* \param num - a number
* \return buffer size in bytes (max is 9)
*/
MP_PROTO __attribute__((const)) uint32_t
mp_sizeof_uint(uint64_t num);
/**
* \brief Calculate exact buffer size needed to store an integer \a num.
* Maximum return value is 9. For performance reasons you can preallocate
* buffer for maximum size without calling the function.
* \param num - a number
* \return buffer size in bytes (max is 9)
* \pre \a num < 0
*/
MP_PROTO __attribute__((const)) uint32_t
mp_sizeof_int(int64_t num);
/**
* \brief Encode an unsigned integer \a num.
* It is your responsibility to ensure that \a data has enough space.
* \param data - a buffer
* \param num - a number
* \return \a data + mp_sizeof_uint(\a num)
* \sa \link mp_encode_array() An usage example \endlink
* \sa mp_sizeof_uint()
*/
MP_PROTO char *
mp_encode_uint(char *data, uint64_t num);
/**
* \brief Encode a signed integer \a num.
* It is your responsibility to ensure that \a data has enough space.
* \param data - a buffer
* \param num - a number
* \return \a data + mp_sizeof_int(\a num)
* \sa \link mp_encode_array() An usage example \endlink
* \sa mp_sizeof_int()
* \pre \a num < 0
*/
MP_PROTO char *
* See mp_encode_strl() for more details.
* \param data - a bufer
* \param len - a string length
* \return data + mp_sizeof_binl(\a len)
* \sa mp_encode_strl
*/
MP_PROTO char *
mp_encode_binl(char *data, uint32_t len);
/**
* \brief Encode a binstring of length \a len.
* The function is equivalent to mp_encode_binl() + memcpy.
* \param data - a buffer
* \param str - a pointer to binstring data
* \param len - a binstring length
* \return \a data + mp_sizeof_bin(\a len) ==
* data + mp_sizeof_binl(\a len) + \a len
* \sa mp_encode_strl
*/
MP_PROTO char *
mp_encode_bin(char *data, const char *str, uint32_t len);
/**
* \brief Check that \a cur buffer has enough bytes to decode a string header
* \param cur buffer
* \param end end of the buffer
* \retval 0 - buffer has enough bytes
* \retval > 0 - the number of remaining bytes to read
* \pre cur < end
* \pre mp_typeof(*cur) == MP_STR
*/
MP_PROTO __attribute__((pure)) ptrdiff_t
mp_check_strl(const char *cur, const char *end);
/**
* \brief Check that \a cur buffer has enough bytes to decode a binstring header
* \param cur buffer
* \param end end of the buffer
* \retval 0 - buffer has enough bytes
* \retval > 0 - the number of remaining bytes to read
* \pre cur < end
* \pre mp_typeof(*cur) == MP_BIN
*/
MP_PROTO __attribute__((pure)) ptrdiff_t
mp_check_binl(const char *cur, const char *end);
/**
* \brief Decode a length of a string from MsgPack \a data
* \param data - the pointer to a buffer
* \return a length of astring
* \post *data = *data + mp_sizeof_strl(retval)
* \sa mp_encode_strl
*/
MP_PROTO uint32_t
mp_decode_strl(const char **data);
/**
* \brief Decode a string from MsgPack \a data
* \param data - the pointer to a buffer
* \param len - the pointer to save a string length
* \return a pointer to a decoded string
* \post *data = *data + mp_sizeof_str(*len)
* \sa mp_encode_binl
*/
MP_PROTO const char *
mp_decode_str(const char **data, uint32_t *len);
/**
* \brief Decode a length of a binstring from MsgPack \a data
* \param data - the pointer to a buffer
* \return a length of a binstring
* \post *data = *data + mp_sizeof_binl(retval)
* \sa mp_encode_binl
*/
MP_PROTO uint32_t
mp_decode_binl(const char **data);
/**
* \brief Decode a binstring from MsgPack \a data
* \param data - the pointer to a buffer
* \param len - the pointer to save a binstring length
* \return a pointer to a decoded binstring
* \post *data = *data + mp_sizeof_str(*len)
* \sa mp_encode_binl
*/
MP_PROTO const char *
mp_decode_bin(const char **data, uint32_t *len);
/**
* \brief 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.
* \return buffer size in bytes (always 1)
*/
MP_PROTO __attribute__((const)) uint32_t
mp_sizeof_nil(void);
/**
* \brief Encode the nil value.
* It is your responsibility to ensure that \a data has enough space.
* \param data - a buffer
* \return \a data + mp_sizeof_nil()
* \sa \link mp_encode_array() An usage example \endlink
* \sa mp_sizeof_nil()
*/
MP_PROTO char *
mp_encode_nil(char *data);
/**
* \brief Check that \a cur buffer has enough bytes to decode nil
* \param cur buffer
* \param end end of the buffer
* \retval 0 - buffer has enough bytes
* \retval > 0 - the number of remaining bytes to read
* \pre cur < end
* \pre mp_typeof(*cur) == MP_NIL
*/
MP_PROTO __attribute__((pure)) ptrdiff_t
mp_check_nil(const char *cur, const char *end);
/**
* \brief Decode the nil value from MsgPack \a data
* \param data - the pointer to a buffer
* \post *data = *data + mp_sizeof_nil()
*/
MP_PROTO void
mp_decode_nil(const char **data);
/**
* \brief 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.
* \return buffer size in bytes (always 1)
*/
MP_PROTO __attribute__((const)) uint32_t
mp_sizeof_bool(bool val);
/**
* \brief Encode a bool value \a val.
* It is your responsibility to ensure that \a data has enough space.
* \param data - a buffer
* \param val - a bool
* \return \a data + mp_sizeof_bool(val)
* \sa \link mp_encode_array() An usage example \endlink
* \sa mp_sizeof_bool()
*/
MP_PROTO char *
mp_encode_bool(char *data, bool val);
/**
* \brief Check that \a cur buffer has enough bytes to decode a bool value
* \param cur buffer
* \param end end of the buffer
* \retval 0 - buffer has enough bytes
* \retval > 0 - the number of remaining bytes to read
* \pre cur < end
* \pre mp_typeof(*cur) == MP_BOOL
*/
MP_PROTO __attribute__((pure)) ptrdiff_t
mp_check_bool(const char *cur, const char *end);
/**
* \brief Decode a bool value from MsgPack \a data
* \param data - the pointer to a buffer
* \return a decoded bool value
* \post *data = *data + mp_sizeof_bool(retval)
*/
MP_PROTO bool
mp_decode_bool(const char **data);
/**
* \brief Skip one element in a packed \a 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:
* \code
* 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
*
* \endcode
( run in 0.977 second using v1.01-cache-2.11-cpan-39bf76dae61 )