xref: /freebsd/contrib/libcbor/src/cbor/serialization.h (revision ec0ea6efa1ad229d75c394c1a9b9cac33af2b1d3)
1 /*
2  * Copyright (c) 2014-2020 Pavel Kalvoda <me@pavelkalvoda.com>
3  *
4  * libcbor is free software; you can redistribute it and/or modify
5  * it under the terms of the MIT license. See LICENSE for details.
6  */
7 
8 #ifndef LIBCBOR_SERIALIZATION_H
9 #define LIBCBOR_SERIALIZATION_H
10 
11 #include "cbor/cbor_export.h"
12 #include "cbor/common.h"
13 
14 #ifdef __cplusplus
15 extern "C" {
16 #endif
17 
18 /*
19  * ============================================================================
20  * High level encoding
21  * ============================================================================
22  */
23 
24 /** Serialize the given item
25  *
26  * @param item[borrow] A data item
27  * @param buffer Buffer to serialize to
28  * @param buffer_size Size of the \p buffer
29  * @return Length of the result. 0 on failure.
30  */
31 CBOR_EXPORT size_t cbor_serialize(const cbor_item_t *item,
32                                   cbor_mutable_data buffer, size_t buffer_size);
33 
34 /** Serialize the given item, allocating buffers as needed
35  *
36  * \rst
37  * .. warning:: It is your responsibility to free the buffer using an
38  *  appropriate ``free`` implementation.
39  * \endrst
40  *
41  * @param item[borrow] A data item
42  * @param buffer[out] Buffer containing the result
43  * @param buffer_size[out] Size of the \p buffer
44  * @return Length of the result. 0 on failure, in which case \p buffer is
45  * ``NULL``.
46  */
47 CBOR_EXPORT size_t cbor_serialize_alloc(const cbor_item_t *item,
48                                         cbor_mutable_data *buffer,
49                                         size_t *buffer_size);
50 
51 /** Serialize an uint
52  *
53  * @param item[borrow] A uint
54  * @param buffer Buffer to serialize to
55  * @param buffer_size Size of the \p buffer
56  * @return Length of the result. 0 on failure.
57  */
58 CBOR_EXPORT size_t cbor_serialize_uint(const cbor_item_t *, cbor_mutable_data,
59                                        size_t);
60 
61 /** Serialize a negint
62  *
63  * @param item[borrow] A neging
64  * @param buffer Buffer to serialize to
65  * @param buffer_size Size of the \p buffer
66  * @return Length of the result. 0 on failure.
67  */
68 CBOR_EXPORT size_t cbor_serialize_negint(const cbor_item_t *, cbor_mutable_data,
69                                          size_t);
70 
71 /** Serialize a bytestring
72  *
73  * @param item[borrow] A bytestring
74  * @param buffer Buffer to serialize to
75  * @param buffer_size Size of the \p buffer
76  * @return Length of the result. 0 on failure.
77  */
78 CBOR_EXPORT size_t cbor_serialize_bytestring(const cbor_item_t *,
79                                              cbor_mutable_data, size_t);
80 
81 /** Serialize a string
82  *
83  * @param item[borrow] A string
84  * @param buffer Buffer to serialize to
85  * @param buffer_size Size of the \p buffer
86  * @return Length of the result. 0 on failure.
87  */
88 CBOR_EXPORT size_t cbor_serialize_string(const cbor_item_t *, cbor_mutable_data,
89                                          size_t);
90 
91 /** Serialize an array
92  *
93  * @param item[borrow] An array
94  * @param buffer Buffer to serialize to
95  * @param buffer_size Size of the \p buffer
96  * @return Length of the result. 0 on failure.
97  */
98 CBOR_EXPORT size_t cbor_serialize_array(const cbor_item_t *, cbor_mutable_data,
99                                         size_t);
100 
101 /** Serialize a map
102  *
103  * @param item[borrow] A map
104  * @param buffer Buffer to serialize to
105  * @param buffer_size Size of the \p buffer
106  * @return Length of the result. 0 on failure.
107  */
108 CBOR_EXPORT size_t cbor_serialize_map(const cbor_item_t *, cbor_mutable_data,
109                                       size_t);
110 
111 /** Serialize a tag
112  *
113  * @param item[borrow] A tag
114  * @param buffer Buffer to serialize to
115  * @param buffer_size Size of the \p buffer
116  * @return Length of the result. 0 on failure.
117  */
118 CBOR_EXPORT size_t cbor_serialize_tag(const cbor_item_t *, cbor_mutable_data,
119                                       size_t);
120 
121 /** Serialize a
122  *
123  * @param item[borrow] A float or ctrl
124  * @param buffer Buffer to serialize to
125  * @param buffer_size Size of the \p buffer
126  * @return Length of the result. 0 on failure.
127  */
128 CBOR_EXPORT size_t cbor_serialize_float_ctrl(const cbor_item_t *,
129                                              cbor_mutable_data, size_t);
130 
131 #ifdef __cplusplus
132 }
133 #endif
134 
135 #endif  // LIBCBOR_SERIALIZATION_H
136