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