xref: /openbsd-src/lib/libcbor/src/cbor/serialization.h (revision 4dcc46c4d04180142eda526ce521dfb137776d05)
1da0d961cSdjm /*
2d3425be1Sdjm  * Copyright (c) 2014-2020 Pavel Kalvoda <me@pavelkalvoda.com>
3da0d961cSdjm  *
4da0d961cSdjm  * libcbor is free software; you can redistribute it and/or modify
5da0d961cSdjm  * it under the terms of the MIT license. See LICENSE for details.
6da0d961cSdjm  */
7da0d961cSdjm 
8da0d961cSdjm #ifndef LIBCBOR_SERIALIZATION_H
9da0d961cSdjm #define LIBCBOR_SERIALIZATION_H
10da0d961cSdjm 
11*4dcc46c4Sdjm #include "cbor/cbor_export.h"
12da0d961cSdjm #include "cbor/common.h"
13da0d961cSdjm 
14da0d961cSdjm #ifdef __cplusplus
15da0d961cSdjm extern "C" {
16da0d961cSdjm #endif
17da0d961cSdjm 
18da0d961cSdjm /*
19da0d961cSdjm  * ============================================================================
20da0d961cSdjm  * High level encoding
21da0d961cSdjm  * ============================================================================
22da0d961cSdjm  */
23da0d961cSdjm 
24da0d961cSdjm /** Serialize the given item
25da0d961cSdjm  *
26da0d961cSdjm  * @param item[borrow] A data item
27da0d961cSdjm  * @param buffer Buffer to serialize to
28da0d961cSdjm  * @param buffer_size Size of the \p buffer
29da0d961cSdjm  * @return Length of the result. 0 on failure.
30da0d961cSdjm  */
31*4dcc46c4Sdjm _CBOR_NODISCARD CBOR_EXPORT size_t cbor_serialize(const cbor_item_t *item,
32*4dcc46c4Sdjm                                                   cbor_mutable_data buffer,
339e5c2ddcSdjm                                                   size_t buffer_size);
34da0d961cSdjm 
35*4dcc46c4Sdjm /** Compute the length (in bytes) of the item when serialized using
36*4dcc46c4Sdjm  * `cbor_serialize`.
37*4dcc46c4Sdjm  *
38*4dcc46c4Sdjm  * Time complexity is proportional to the number of nested items.
39*4dcc46c4Sdjm  *
40*4dcc46c4Sdjm  * @param item[borrow] A data item
41*4dcc46c4Sdjm  * @return Length (>= 1) of the item when serialized. 0 if the length overflows
42*4dcc46c4Sdjm  * `size_t`.
43*4dcc46c4Sdjm  */
44*4dcc46c4Sdjm _CBOR_NODISCARD CBOR_EXPORT size_t
45*4dcc46c4Sdjm cbor_serialized_size(const cbor_item_t *item);
46*4dcc46c4Sdjm 
47da0d961cSdjm /** Serialize the given item, allocating buffers as needed
48da0d961cSdjm  *
49*4dcc46c4Sdjm  * Since libcbor v0.10, the return value is always the same as `buffer_size` (if
50*4dcc46c4Sdjm  * provided, see https://github.com/PJK/libcbor/pull/251/). New clients should
51*4dcc46c4Sdjm  * ignore the return value.
52*4dcc46c4Sdjm  *
53da0d961cSdjm  * \rst
54*4dcc46c4Sdjm  * .. warning:: It is the caller's responsibility to free the buffer using an
55*4dcc46c4Sdjm  *  appropriate ``free`` implementation.
56*4dcc46c4Sdjm  * \endrst
57da0d961cSdjm  *
58da0d961cSdjm  * @param item[borrow] A data item
59da0d961cSdjm  * @param buffer[out] Buffer containing the result
60*4dcc46c4Sdjm  * @param buffer_size[out] Size of the \p buffer, or ``NULL``
619e5c2ddcSdjm  * @return Length of the result. 0 on failure, in which case \p buffer is
629e5c2ddcSdjm  * ``NULL``.
63da0d961cSdjm  */
64*4dcc46c4Sdjm CBOR_EXPORT size_t cbor_serialize_alloc(const cbor_item_t *item,
65*4dcc46c4Sdjm                                         cbor_mutable_data *buffer,
669e5c2ddcSdjm                                         size_t *buffer_size);
67da0d961cSdjm 
68da0d961cSdjm /** Serialize an uint
69da0d961cSdjm  *
70da0d961cSdjm  * @param item[borrow] A uint
71da0d961cSdjm  * @param buffer Buffer to serialize to
72da0d961cSdjm  * @param buffer_size Size of the \p buffer
73da0d961cSdjm  * @return Length of the result. 0 on failure.
74da0d961cSdjm  */
75*4dcc46c4Sdjm _CBOR_NODISCARD CBOR_EXPORT size_t cbor_serialize_uint(const cbor_item_t *,
76*4dcc46c4Sdjm                                                        cbor_mutable_data,
77*4dcc46c4Sdjm                                                        size_t);
78da0d961cSdjm 
79da0d961cSdjm /** Serialize a negint
80da0d961cSdjm  *
81*4dcc46c4Sdjm  * @param item[borrow] A negint
82da0d961cSdjm  * @param buffer Buffer to serialize to
83da0d961cSdjm  * @param buffer_size Size of the \p buffer
84da0d961cSdjm  * @return Length of the result. 0 on failure.
85da0d961cSdjm  */
86*4dcc46c4Sdjm _CBOR_NODISCARD CBOR_EXPORT size_t cbor_serialize_negint(const cbor_item_t *,
87*4dcc46c4Sdjm                                                          cbor_mutable_data,
88*4dcc46c4Sdjm                                                          size_t);
89da0d961cSdjm 
90da0d961cSdjm /** Serialize a bytestring
91da0d961cSdjm  *
92da0d961cSdjm  * @param item[borrow] A bytestring
93da0d961cSdjm  * @param buffer Buffer to serialize to
94da0d961cSdjm  * @param buffer_size Size of the \p buffer
95da0d961cSdjm  * @return Length of the result. 0 on failure.
96da0d961cSdjm  */
97*4dcc46c4Sdjm _CBOR_NODISCARD CBOR_EXPORT size_t
98*4dcc46c4Sdjm cbor_serialize_bytestring(const cbor_item_t *, cbor_mutable_data, size_t);
99da0d961cSdjm 
100da0d961cSdjm /** Serialize a string
101da0d961cSdjm  *
102da0d961cSdjm  * @param item[borrow] A string
103da0d961cSdjm  * @param buffer Buffer to serialize to
104da0d961cSdjm  * @param buffer_size Size of the \p buffer
105da0d961cSdjm  * @return Length of the result. 0 on failure.
106da0d961cSdjm  */
107*4dcc46c4Sdjm _CBOR_NODISCARD CBOR_EXPORT size_t cbor_serialize_string(const cbor_item_t *,
108*4dcc46c4Sdjm                                                          cbor_mutable_data,
109*4dcc46c4Sdjm                                                          size_t);
110da0d961cSdjm 
111da0d961cSdjm /** Serialize an array
112da0d961cSdjm  *
113da0d961cSdjm  * @param item[borrow] An array
114da0d961cSdjm  * @param buffer Buffer to serialize to
115da0d961cSdjm  * @param buffer_size Size of the \p buffer
116da0d961cSdjm  * @return Length of the result. 0 on failure.
117da0d961cSdjm  */
118*4dcc46c4Sdjm _CBOR_NODISCARD CBOR_EXPORT size_t cbor_serialize_array(const cbor_item_t *,
119*4dcc46c4Sdjm                                                         cbor_mutable_data,
120*4dcc46c4Sdjm                                                         size_t);
121da0d961cSdjm 
122da0d961cSdjm /** Serialize a map
123da0d961cSdjm  *
124da0d961cSdjm  * @param item[borrow] A map
125da0d961cSdjm  * @param buffer Buffer to serialize to
126da0d961cSdjm  * @param buffer_size Size of the \p buffer
127da0d961cSdjm  * @return Length of the result. 0 on failure.
128da0d961cSdjm  */
129*4dcc46c4Sdjm _CBOR_NODISCARD CBOR_EXPORT size_t cbor_serialize_map(const cbor_item_t *,
130*4dcc46c4Sdjm                                                       cbor_mutable_data,
131*4dcc46c4Sdjm                                                       size_t);
132da0d961cSdjm 
133da0d961cSdjm /** Serialize a tag
134da0d961cSdjm  *
135da0d961cSdjm  * @param item[borrow] A tag
136da0d961cSdjm  * @param buffer Buffer to serialize to
137da0d961cSdjm  * @param buffer_size Size of the \p buffer
138da0d961cSdjm  * @return Length of the result. 0 on failure.
139da0d961cSdjm  */
140*4dcc46c4Sdjm _CBOR_NODISCARD CBOR_EXPORT size_t cbor_serialize_tag(const cbor_item_t *,
141*4dcc46c4Sdjm                                                       cbor_mutable_data,
142*4dcc46c4Sdjm                                                       size_t);
143da0d961cSdjm 
144da0d961cSdjm /** Serialize a
145da0d961cSdjm  *
146da0d961cSdjm  * @param item[borrow] A float or ctrl
147da0d961cSdjm  * @param buffer Buffer to serialize to
148da0d961cSdjm  * @param buffer_size Size of the \p buffer
149da0d961cSdjm  * @return Length of the result. 0 on failure.
150da0d961cSdjm  */
151*4dcc46c4Sdjm _CBOR_NODISCARD CBOR_EXPORT size_t
152*4dcc46c4Sdjm cbor_serialize_float_ctrl(const cbor_item_t *, cbor_mutable_data, size_t);
153da0d961cSdjm 
154da0d961cSdjm #ifdef __cplusplus
155da0d961cSdjm }
156da0d961cSdjm #endif
157da0d961cSdjm 
158da0d961cSdjm #endif  // LIBCBOR_SERIALIZATION_H
159