Path: blob/main/contrib/libcbor/src/cbor/serialization.h
39536 views
/*1* Copyright (c) 2014-2020 Pavel Kalvoda <[email protected]>2*3* libcbor is free software; you can redistribute it and/or modify4* it under the terms of the MIT license. See LICENSE for details.5*/67#ifndef LIBCBOR_SERIALIZATION_H8#define LIBCBOR_SERIALIZATION_H910#include "cbor/cbor_export.h"11#include "cbor/common.h"1213#ifdef __cplusplus14extern "C" {15#endif1617/*18* ============================================================================19* High level encoding20* ============================================================================21*/2223/** Serialize the given item24*25* @param item A data item26* @param buffer Buffer to serialize to27* @param buffer_size Size of the \p buffer28* @return Length of the result. 0 on failure.29*/30_CBOR_NODISCARD CBOR_EXPORT size_t cbor_serialize(const cbor_item_t *item,31cbor_mutable_data buffer,32size_t buffer_size);3334/** Compute the length (in bytes) of the item when serialized using35* `cbor_serialize`.36*37* Time complexity is proportional to the number of nested items.38*39* @param item A data item40* @return Length (>= 1) of the item when serialized. 0 if the length overflows41* `size_t`.42*/43_CBOR_NODISCARD CBOR_EXPORT size_t44cbor_serialized_size(const cbor_item_t *item);4546/** Serialize the given item, allocating buffers as needed47*48* Since libcbor v0.10, the return value is always the same as `buffer_size` (if49* provided, see https://github.com/PJK/libcbor/pull/251/). New clients should50* ignore the return value.51*52* \rst53* .. warning:: It is the caller's responsibility to free the buffer using an54* appropriate ``free`` implementation.55* \endrst56*57* @param item A data item58* @param[out] buffer Buffer containing the result59* @param[out] buffer_size Size of the \p buffer, or 0 on memory allocation60* failure.61* @return Length of the result in bytes62* @return 0 on memory allocation failure, in which case \p buffer is `NULL`.63*/64CBOR_EXPORT size_t cbor_serialize_alloc(const cbor_item_t *item,65unsigned char **buffer,66size_t *buffer_size);6768/** Serialize an uint69*70* @param item A uint71* @param[out] buffer Buffer to serialize to72* @param buffer_size Size of the \p buffer73* @return Length of the result74* @return 0 if the \p buffer_size doesn't fit the result75*/76_CBOR_NODISCARD CBOR_EXPORT size_t cbor_serialize_uint(const cbor_item_t *item,77cbor_mutable_data buffer,78size_t buffer_size);7980/** Serialize a negint81*82* @param item A negint83* @param[out] buffer Buffer to serialize to84* @param buffer_size Size of the \p buffer85* @return Length of the result86* @return 0 if the \p buffer_size doesn't fit the result87*/88_CBOR_NODISCARD CBOR_EXPORT size_t cbor_serialize_negint(89const cbor_item_t *item, cbor_mutable_data buffer, size_t buffer_size);9091/** Serialize a bytestring92*93* @param item A bytestring94* @param[out] buffer Buffer to serialize to95* @param buffer_size Size of the \p buffer96* @return Length of the result97* @return 0 if the \p buffer_size doesn't fit the result. The \p buffer may98* still be modified99*/100_CBOR_NODISCARD CBOR_EXPORT size_t cbor_serialize_bytestring(101const cbor_item_t *item, cbor_mutable_data buffer, size_t buffer_size);102103/** Serialize a string104*105* @param item A string106* @param[out] buffer Buffer to serialize to107* @param buffer_size Size of the \p buffer108* @return Length of the result109* @return 0 if the \p buffer_size doesn't fit the result. The \p buffer may110* still be modified111*/112_CBOR_NODISCARD CBOR_EXPORT size_t cbor_serialize_string(113const cbor_item_t *item, cbor_mutable_data buffer, size_t buffer_size);114/** Serialize an array115*116* @param item An array117* @param[out] buffer Buffer to serialize to118* @param buffer_size Size of the \p buffer119* @return Length of the result120* @return 0 if the \p buffer_size doesn't fit the result. The \p buffer may121* still be modified122*/123_CBOR_NODISCARD CBOR_EXPORT size_t cbor_serialize_array(124const cbor_item_t *item, cbor_mutable_data buffer, size_t buffer_size);125126/** Serialize a map127*128* @param item A map129* @param[out] buffer Buffer to serialize to130* @param buffer_size Size of the \p buffer131* @return Length of the result132* @return 0 if the \p buffer_size doesn't fit the result. The \p buffer may133* still be modified134*/135_CBOR_NODISCARD CBOR_EXPORT size_t cbor_serialize_map(const cbor_item_t *item,136cbor_mutable_data buffer,137size_t buffer_size);138139/** Serialize a tag140*141* @param item A tag142* @param[out] buffer Buffer to serialize to143* @param buffer_size Size of the \p buffer144* @return Length of the result145* @return 0 if the \p buffer_size doesn't fit the result. The \p buffer may146* still be modified147*/148_CBOR_NODISCARD CBOR_EXPORT size_t cbor_serialize_tag(const cbor_item_t *item,149cbor_mutable_data buffer,150size_t buffer_size);151152/** Serialize a153*154* @param item A float or ctrl155* @param[out] buffer Buffer to serialize to156* @param buffer_size Size of the \p buffer157* @return Length of the result158* @return 0 if the \p buffer_size doesn't fit the result159*/160_CBOR_NODISCARD CBOR_EXPORT size_t cbor_serialize_float_ctrl(161const cbor_item_t *item, cbor_mutable_data buffer, size_t buffer_size);162163#ifdef __cplusplus164}165#endif166167#endif // LIBCBOR_SERIALIZATION_H168169170