xref: /openbsd/lib/libcbor/src/cbor/serialization.h (revision 771fbea0)
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/common.h"
12 
13 #ifdef __cplusplus
14 extern "C" {
15 #endif
16 
17 /*
18  * ============================================================================
19  * High level encoding
20  * ============================================================================
21  */
22 
23 /** Serialize the given item
24  *
25  * @param item[borrow] A data item
26  * @param buffer Buffer to serialize to
27  * @param buffer_size Size of the \p buffer
28  * @return Length of the result. 0 on failure.
29  */
30 size_t cbor_serialize(const cbor_item_t *item, cbor_mutable_data buffer,
31                       size_t buffer_size);
32 
33 /** Serialize the given item, allocating buffers as needed
34  *
35  * \rst
36  * .. warning:: It is your responsibility to free the buffer using an
37  * appropriate ``free`` implementation. \endrst
38  *
39  * @param item[borrow] A data item
40  * @param buffer[out] Buffer containing the result
41  * @param buffer_size[out] Size of the \p buffer
42  * @return Length of the result. 0 on failure, in which case \p buffer is
43  * ``NULL``.
44  */
45 size_t cbor_serialize_alloc(const cbor_item_t *item, cbor_mutable_data *buffer,
46                             size_t *buffer_size);
47 
48 /** Serialize an uint
49  *
50  * @param item[borrow] A uint
51  * @param buffer Buffer to serialize to
52  * @param buffer_size Size of the \p buffer
53  * @return Length of the result. 0 on failure.
54  */
55 size_t cbor_serialize_uint(const cbor_item_t *, cbor_mutable_data, size_t);
56 
57 /** Serialize a negint
58  *
59  * @param item[borrow] A neging
60  * @param buffer Buffer to serialize to
61  * @param buffer_size Size of the \p buffer
62  * @return Length of the result. 0 on failure.
63  */
64 size_t cbor_serialize_negint(const cbor_item_t *, cbor_mutable_data, size_t);
65 
66 /** Serialize a bytestring
67  *
68  * @param item[borrow] A bytestring
69  * @param buffer Buffer to serialize to
70  * @param buffer_size Size of the \p buffer
71  * @return Length of the result. 0 on failure.
72  */
73 size_t cbor_serialize_bytestring(const cbor_item_t *, cbor_mutable_data,
74                                  size_t);
75 
76 /** Serialize a string
77  *
78  * @param item[borrow] A string
79  * @param buffer Buffer to serialize to
80  * @param buffer_size Size of the \p buffer
81  * @return Length of the result. 0 on failure.
82  */
83 size_t cbor_serialize_string(const cbor_item_t *, cbor_mutable_data, size_t);
84 
85 /** Serialize an array
86  *
87  * @param item[borrow] An array
88  * @param buffer Buffer to serialize to
89  * @param buffer_size Size of the \p buffer
90  * @return Length of the result. 0 on failure.
91  */
92 size_t cbor_serialize_array(const cbor_item_t *, cbor_mutable_data, size_t);
93 
94 /** Serialize a map
95  *
96  * @param item[borrow] A map
97  * @param buffer Buffer to serialize to
98  * @param buffer_size Size of the \p buffer
99  * @return Length of the result. 0 on failure.
100  */
101 size_t cbor_serialize_map(const cbor_item_t *, cbor_mutable_data, size_t);
102 
103 /** Serialize a tag
104  *
105  * @param item[borrow] A tag
106  * @param buffer Buffer to serialize to
107  * @param buffer_size Size of the \p buffer
108  * @return Length of the result. 0 on failure.
109  */
110 size_t cbor_serialize_tag(const cbor_item_t *, cbor_mutable_data, size_t);
111 
112 /** Serialize a
113  *
114  * @param item[borrow] A float or ctrl
115  * @param buffer Buffer to serialize to
116  * @param buffer_size Size of the \p buffer
117  * @return Length of the result. 0 on failure.
118  */
119 size_t cbor_serialize_float_ctrl(const cbor_item_t *, cbor_mutable_data,
120                                  size_t);
121 
122 #ifdef __cplusplus
123 }
124 #endif
125 
126 #endif  // LIBCBOR_SERIALIZATION_H
127