110ff414cSEd Maste /* 210ff414cSEd Maste * Copyright (c) 2014-2020 Pavel Kalvoda <me@pavelkalvoda.com> 310ff414cSEd Maste * 410ff414cSEd Maste * libcbor is free software; you can redistribute it and/or modify 510ff414cSEd Maste * it under the terms of the MIT license. See LICENSE for details. 610ff414cSEd Maste */ 710ff414cSEd Maste 810ff414cSEd Maste #ifndef LIBCBOR_SERIALIZATION_H 910ff414cSEd Maste #define LIBCBOR_SERIALIZATION_H 1010ff414cSEd Maste 1110ff414cSEd Maste #include "cbor/cbor_export.h" 1210ff414cSEd Maste #include "cbor/common.h" 1310ff414cSEd Maste 1410ff414cSEd Maste #ifdef __cplusplus 1510ff414cSEd Maste extern "C" { 1610ff414cSEd Maste #endif 1710ff414cSEd Maste 1810ff414cSEd Maste /* 1910ff414cSEd Maste * ============================================================================ 2010ff414cSEd Maste * High level encoding 2110ff414cSEd Maste * ============================================================================ 2210ff414cSEd Maste */ 2310ff414cSEd Maste 2410ff414cSEd Maste /** Serialize the given item 2510ff414cSEd Maste * 26*5d3e7166SEd Maste * @param item A data item 2710ff414cSEd Maste * @param buffer Buffer to serialize to 2810ff414cSEd Maste * @param buffer_size Size of the \p buffer 2910ff414cSEd Maste * @return Length of the result. 0 on failure. 3010ff414cSEd Maste */ 31*5d3e7166SEd Maste _CBOR_NODISCARD CBOR_EXPORT size_t cbor_serialize(const cbor_item_t *item, 32*5d3e7166SEd Maste cbor_mutable_data buffer, 33*5d3e7166SEd Maste size_t buffer_size); 34*5d3e7166SEd Maste 35*5d3e7166SEd Maste /** Compute the length (in bytes) of the item when serialized using 36*5d3e7166SEd Maste * `cbor_serialize`. 37*5d3e7166SEd Maste * 38*5d3e7166SEd Maste * Time complexity is proportional to the number of nested items. 39*5d3e7166SEd Maste * 40*5d3e7166SEd Maste * @param item A data item 41*5d3e7166SEd Maste * @return Length (>= 1) of the item when serialized. 0 if the length overflows 42*5d3e7166SEd Maste * `size_t`. 43*5d3e7166SEd Maste */ 44*5d3e7166SEd Maste _CBOR_NODISCARD CBOR_EXPORT size_t 45*5d3e7166SEd Maste cbor_serialized_size(const cbor_item_t *item); 4610ff414cSEd Maste 4710ff414cSEd Maste /** Serialize the given item, allocating buffers as needed 4810ff414cSEd Maste * 49*5d3e7166SEd Maste * Since libcbor v0.10, the return value is always the same as `buffer_size` (if 50*5d3e7166SEd Maste * provided, see https://github.com/PJK/libcbor/pull/251/). New clients should 51*5d3e7166SEd Maste * ignore the return value. 52*5d3e7166SEd Maste * 5310ff414cSEd Maste * \rst 54*5d3e7166SEd Maste * .. warning:: It is the caller's responsibility to free the buffer using an 5510ff414cSEd Maste * appropriate ``free`` implementation. 5610ff414cSEd Maste * \endrst 5710ff414cSEd Maste * 58*5d3e7166SEd Maste * @param item A data item 59*5d3e7166SEd Maste * @param[out] buffer Buffer containing the result 60*5d3e7166SEd Maste * @param[out] buffer_size Size of the \p buffer, or 0 on memory allocation 61*5d3e7166SEd Maste * failure. 62*5d3e7166SEd Maste * @return Length of the result in bytes 63*5d3e7166SEd Maste * @return 0 on memory allocation failure, in which case \p buffer is `NULL`. 6410ff414cSEd Maste */ 6510ff414cSEd Maste CBOR_EXPORT size_t cbor_serialize_alloc(const cbor_item_t *item, 66*5d3e7166SEd Maste unsigned char **buffer, 6710ff414cSEd Maste size_t *buffer_size); 6810ff414cSEd Maste 6910ff414cSEd Maste /** Serialize an uint 7010ff414cSEd Maste * 71*5d3e7166SEd Maste * @param item A uint 72*5d3e7166SEd Maste * @param[out] buffer Buffer to serialize to 7310ff414cSEd Maste * @param buffer_size Size of the \p buffer 74*5d3e7166SEd Maste * @return Length of the result 75*5d3e7166SEd Maste * @return 0 if the \p buffer_size doesn't fit the result 7610ff414cSEd Maste */ 77*5d3e7166SEd Maste _CBOR_NODISCARD CBOR_EXPORT size_t cbor_serialize_uint(const cbor_item_t *item, 78*5d3e7166SEd Maste cbor_mutable_data buffer, 79*5d3e7166SEd Maste size_t buffer_size); 8010ff414cSEd Maste 8110ff414cSEd Maste /** Serialize a negint 8210ff414cSEd Maste * 83*5d3e7166SEd Maste * @param item A negint 84*5d3e7166SEd Maste * @param[out] buffer Buffer to serialize to 8510ff414cSEd Maste * @param buffer_size Size of the \p buffer 86*5d3e7166SEd Maste * @return Length of the result 87*5d3e7166SEd Maste * @return 0 if the \p buffer_size doesn't fit the result 8810ff414cSEd Maste */ 89*5d3e7166SEd Maste _CBOR_NODISCARD CBOR_EXPORT size_t cbor_serialize_negint( 90*5d3e7166SEd Maste const cbor_item_t *item, cbor_mutable_data buffer, size_t buffer_size); 9110ff414cSEd Maste 9210ff414cSEd Maste /** Serialize a bytestring 9310ff414cSEd Maste * 94*5d3e7166SEd Maste * @param item A bytestring 95*5d3e7166SEd Maste * @param[out] buffer Buffer to serialize to 9610ff414cSEd Maste * @param buffer_size Size of the \p buffer 97*5d3e7166SEd Maste * @return Length of the result 98*5d3e7166SEd Maste * @return 0 if the \p buffer_size doesn't fit the result. The \p buffer may 99*5d3e7166SEd Maste * still be modified 10010ff414cSEd Maste */ 101*5d3e7166SEd Maste _CBOR_NODISCARD CBOR_EXPORT size_t cbor_serialize_bytestring( 102*5d3e7166SEd Maste const cbor_item_t *item, cbor_mutable_data buffer, size_t buffer_size); 10310ff414cSEd Maste 10410ff414cSEd Maste /** Serialize a string 10510ff414cSEd Maste * 106*5d3e7166SEd Maste * @param item A string 107*5d3e7166SEd Maste * @param[out] buffer Buffer to serialize to 10810ff414cSEd Maste * @param buffer_size Size of the \p buffer 109*5d3e7166SEd Maste * @return Length of the result 110*5d3e7166SEd Maste * @return 0 if the \p buffer_size doesn't fit the result. The \p buffer may 111*5d3e7166SEd Maste * still be modified 11210ff414cSEd Maste */ 113*5d3e7166SEd Maste _CBOR_NODISCARD CBOR_EXPORT size_t cbor_serialize_string( 114*5d3e7166SEd Maste const cbor_item_t *item, cbor_mutable_data buffer, size_t buffer_size); 11510ff414cSEd Maste /** Serialize an array 11610ff414cSEd Maste * 117*5d3e7166SEd Maste * @param item An array 118*5d3e7166SEd Maste * @param[out] buffer Buffer to serialize to 11910ff414cSEd Maste * @param buffer_size Size of the \p buffer 120*5d3e7166SEd Maste * @return Length of the result 121*5d3e7166SEd Maste * @return 0 if the \p buffer_size doesn't fit the result. The \p buffer may 122*5d3e7166SEd Maste * still be modified 12310ff414cSEd Maste */ 124*5d3e7166SEd Maste _CBOR_NODISCARD CBOR_EXPORT size_t cbor_serialize_array( 125*5d3e7166SEd Maste const cbor_item_t *item, cbor_mutable_data buffer, size_t buffer_size); 12610ff414cSEd Maste 12710ff414cSEd Maste /** Serialize a map 12810ff414cSEd Maste * 129*5d3e7166SEd Maste * @param item A map 130*5d3e7166SEd Maste * @param[out] buffer Buffer to serialize to 13110ff414cSEd Maste * @param buffer_size Size of the \p buffer 132*5d3e7166SEd Maste * @return Length of the result 133*5d3e7166SEd Maste * @return 0 if the \p buffer_size doesn't fit the result. The \p buffer may 134*5d3e7166SEd Maste * still be modified 13510ff414cSEd Maste */ 136*5d3e7166SEd Maste _CBOR_NODISCARD CBOR_EXPORT size_t cbor_serialize_map(const cbor_item_t *item, 137*5d3e7166SEd Maste cbor_mutable_data buffer, 138*5d3e7166SEd Maste size_t buffer_size); 13910ff414cSEd Maste 14010ff414cSEd Maste /** Serialize a tag 14110ff414cSEd Maste * 142*5d3e7166SEd Maste * @param item A tag 143*5d3e7166SEd Maste * @param[out] buffer Buffer to serialize to 14410ff414cSEd Maste * @param buffer_size Size of the \p buffer 145*5d3e7166SEd Maste * @return Length of the result 146*5d3e7166SEd Maste * @return 0 if the \p buffer_size doesn't fit the result. The \p buffer may 147*5d3e7166SEd Maste * still be modified 14810ff414cSEd Maste */ 149*5d3e7166SEd Maste _CBOR_NODISCARD CBOR_EXPORT size_t cbor_serialize_tag(const cbor_item_t *item, 150*5d3e7166SEd Maste cbor_mutable_data buffer, 151*5d3e7166SEd Maste size_t buffer_size); 15210ff414cSEd Maste 15310ff414cSEd Maste /** Serialize a 15410ff414cSEd Maste * 155*5d3e7166SEd Maste * @param item A float or ctrl 156*5d3e7166SEd Maste * @param[out] buffer Buffer to serialize to 15710ff414cSEd Maste * @param buffer_size Size of the \p buffer 158*5d3e7166SEd Maste * @return Length of the result 159*5d3e7166SEd Maste * @return 0 if the \p buffer_size doesn't fit the result 16010ff414cSEd Maste */ 161*5d3e7166SEd Maste _CBOR_NODISCARD CBOR_EXPORT size_t cbor_serialize_float_ctrl( 162*5d3e7166SEd Maste const cbor_item_t *item, cbor_mutable_data buffer, size_t buffer_size); 16310ff414cSEd Maste 16410ff414cSEd Maste #ifdef __cplusplus 16510ff414cSEd Maste } 16610ff414cSEd Maste #endif 16710ff414cSEd Maste 16810ff414cSEd Maste #endif // LIBCBOR_SERIALIZATION_H 169