Contiki-NG
Loading...
Searching...
No Matches
cbor.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2018, SICS, RISE AB
3 * Copyright (c) 2023, Uppsala universitet
4 * Copyright (c) 2024, Siemens AG
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the Institute nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32#ifndef CBOR_H_
33#define CBOR_H_
34
35/**
36 * \addtogroup data
37 * @{
38 *
39 * \defgroup cbor CBOR
40 *
41 * Functions for reading and writing CBOR.
42 * @{
43 *
44 * \file
45 * CBOR API.
46 */
47
48#include <stdbool.h>
49#include <stddef.h>
50#include <stdint.h>
51
52/**
53 * Defines how many arrays and maps can be open simultaneously while writing.
54 */
55#ifdef CBOR_CONF_MAX_NESTING
56#define CBOR_MAX_NESTING CBOR_CONF_MAX_NESTING
57#else /* CBOR_CONF_MAX_NESTING */
58#define CBOR_MAX_NESTING (8)
59#endif /* CBOR_CONF_MAX_NESTING */
60
61#define CBOR_UNSIGNED_SIZE(uint) ((uint) < CBOR_SIZE_1 \
62 ? 1 \
63 : ((uint) <= UINT8_MAX \
64 ? 1 + 1 \
65 : ((uint) <= UINT16_MAX \
66 ? 1 + 2 \
67 : ((uint) <= UINT32_MAX \
68 ? 1 + 4 \
69 : 1 + 8))))
70#define CBOR_BYTE_STRING_SIZE(bytes) (CBOR_UNSIGNED_SIZE(bytes) + (bytes))
71
72/**
73 * Enumeration of major types.
74 */
75typedef enum cbor_major_type_t {
76 CBOR_MAJOR_TYPE_NONE = -1,
77 CBOR_MAJOR_TYPE_UNSIGNED = 0x00,
78 CBOR_MAJOR_TYPE_SIGNED = 0x20,
79 CBOR_MAJOR_TYPE_BYTE_STRING = 0x40,
80 CBOR_MAJOR_TYPE_TEXT_STRING = 0x60,
81 CBOR_MAJOR_TYPE_ARRAY = 0x80,
82 CBOR_MAJOR_TYPE_MAP = 0xA0,
83 CBOR_MAJOR_TYPE_SIMPLE = 0xE0,
85
86/**
87 * Enumeration of simple values.
88 */
89typedef enum cbor_simple_value_t {
90 CBOR_SIMPLE_VALUE_NONE = -1,
91 CBOR_SIMPLE_VALUE_FALSE = 0xF4,
92 CBOR_SIMPLE_VALUE_TRUE = 0xF5,
93 CBOR_SIMPLE_VALUE_NULL = 0xF6,
94 CBOR_SIMPLE_VALUE_UNDEFINED = 0xF7,
96
97/**
98 * Enumeration of size information in various major types.
99 */
100typedef enum cbor_size_t {
101 CBOR_SIZE_NONE = -1, /**< error condition */
102 CBOR_SIZE_1 = 0x18, /**< 1 byte */
103 CBOR_SIZE_2 = 0x19, /**< 2 bytes */
104 CBOR_SIZE_4 = 0x1A, /**< 4 bytes */
105 CBOR_SIZE_8 = 0x1B, /**< 8 bytes */
107
108/**
109 * Structure of a nesting record.
110 */
111typedef struct cbor_nesting_record_t {
112 uint8_t *start;
113 size_t objects;
115
116/**
117 * Structure of the internal state of a CBOR writer.
118 */
119typedef struct cbor_writer_state_t {
120 const uint8_t *buffer_head;
121 uint8_t *buffer;
122 size_t buffer_size;
123 size_t nesting_depth;
126
127/**
128 * Structure of the internal state of a CBOR reader.
129 */
130typedef struct cbor_reader_state_t {
131 const uint8_t *cbor;
132 size_t cbor_size;
134
135/**
136 * Prepares for writing CBOR output.
137 *
138 * \param state State of the CBOR writer.
139 * \param buffer Buffer for holding CBOR output.
140 * \param buffer_size Size of \p buffer in bytes.
141 */
143 uint8_t *buffer, size_t buffer_size);
144
145/**
146 * Finishes writing CBOR output.
147 *
148 * \param state State of the CBOR writer.
149 *
150 * \return Size of the CBOR output or \c 0 on error.
151 */
153
154/**
155 * Marks the CBOR output as erroneous.
156 *
157 * \param state State of the CBOR writer.
158 */
160
161/**
162 * Appends an arbitrary CBOR object.
163 *
164 * \param state State of the CBOR writer.
165 * \param object Location of the CBOR object.
166 * \param object_size Size of \p object in bytes.
167 */
169 const void *object, size_t object_size);
170
171/**
172 * Appends an unsigned integer.
173 *
174 * \param state State of the CBOR writer.
175 * \param value Unsigned integer.
176 */
177void cbor_write_unsigned(cbor_writer_state_t *state, uint64_t value);
178
179/**
180 * Appends a signed integer.
181 *
182 * \param state State of the CBOR writer.
183 * \param value Signed integer.
184 */
185void cbor_write_signed(cbor_writer_state_t *state, int64_t value);
186
187/**
188 * Appends a byte string.
189 *
190 * \param state State of the CBOR writer.
191 * \param data The byte string.
192 * \param data_size Size of \p data in bytes.
193 */
195 const uint8_t *data, size_t data_size);
196
197/**
198 * Appends a text string.
199 *
200 * \param state State of the CBOR writer.
201 * \param text The text string.
202 * \param text_size Number of characters in \p text.
203 */
205 const char *text, size_t text_size);
206
207/**
208 * Appends the simple value \c null.
209 *
210 * \param state State of the CBOR writer.
211 */
213
214/**
215 * Appends the simple value \c undefined.
216 *
217 * \param state State of the CBOR writer.
218 */
220
221/**
222 * Appends a boolean simple value.
223 *
224 * \param state State of the CBOR writer.
225 * \param boolean The boolean simple value to append.
226 */
227void cbor_write_bool(cbor_writer_state_t *state, bool boolean);
228
229/**
230 * Encloses subsequent CBOR objects in a byte string.
231 *
232 * \param state State of the CBOR writer.
233 */
235
236/**
237 * Stops enclosing subsequent CBOR objects in the innermost byte string.
238 *
239 * \param state State of the CBOR writer.
240 */
242
243/**
244 * Adds subsequent CBOR objects to an array.
245 *
246 * \param state State of the CBOR writer.
247 */
249
250/**
251 * Stops adding subsequent CBOR objects to the innermost array.
252 *
253 * \param state State of the CBOR writer.
254 */
256
257/**
258 * Adds subsequent entries to a map.
259 *
260 * \param state State of the CBOR writer.
261 */
263
264/**
265 * Stops adding subsequent entries to the innermost map.
266 *
267 * \param state State of the CBOR writer.
268 */
270
271/**
272 * Prepares for reading CBOR input.
273 *
274 * \param state State of the CBOR reader.
275 * \param cbor Buffer that holds the CBOR input.
276 * \param cbor_size Size of \p cbor in bytes.
277 */
279 const uint8_t *cbor, size_t cbor_size);
280
281/**
282 * Inspects the next major type.
283 *
284 * \param state State of the CBOR reader.
285 *
286 * \return The next major type (could be \c CBOR_MAJOR_TYPE_NONE).
287 */
289
290/**
291 * Ensures that no bytes remain unread.
292 *
293 * \param state State of the CBOR reader.
294 *
295 * \return \c true if no unread bytes remain, or \c false otherwise.
296 */
298
299/**
300 * Reads an unsigned integer.
301 *
302 * \param state State of the CBOR reader.
303 * \param value Buffer to store the unsigned integer.
304 *
305 * \return Size of the unsigned integer or \c CBOR_SIZE_NONE on error.
306 */
307cbor_size_t cbor_read_unsigned(cbor_reader_state_t *state, uint64_t *value);
308
309/**
310 * Reads a signed integer.
311 *
312 * \param state State of the CBOR reader.
313 * \param value Buffer to store the signed integer.
314 *
315 * \return Size of the signed integer or \c CBOR_SIZE_NONE on error.
316 */
317cbor_size_t cbor_read_signed(cbor_reader_state_t *state, int64_t *value);
318
319/**
320 * Reads a byte string.
321 *
322 * \param state State of the CBOR reader.
323 * \param data_size Size of the byte string in bytes.
324 *
325 * \return First byte of the byte string or \c NULL on error.
326 */
327const uint8_t *cbor_read_data(cbor_reader_state_t *state, size_t *data_size);
328
329/**
330 * Reads a text string.
331 *
332 * \param state State of the CBOR reader.
333 * \param text_size Number of characters in the text string.
334 *
335 * \return First character of the text string or \c NULL on error.
336 */
337const char *cbor_read_text(cbor_reader_state_t *state, size_t *text_size);
338
339/**
340 * Reads a simple value.
341 *
342 * \param state State of the CBOR reader.
343 *
344 * \return The simple value or \c CBOR_SIMPLE_VALUE_NONE on error.
345 */
347
348/**
349 * Reads the number of elements of an array.
350 *
351 * \param state State of the CBOR reader.
352 *
353 * \return Number of elements or \c SIZE_MAX on error.
354 */
356
357/**
358 * Reads the number of entries of a map.
359 *
360 * \param state State of the CBOR reader.
361 *
362 * \return Number of entries or \c SIZE_MAX on error.
363 */
365
366/** @} */
367/** @} */
368
369#endif /* CBOR_H_ */
size_t cbor_end_writer(cbor_writer_state_t *state)
Finishes writing CBOR output.
Definition cbor.c:54
struct cbor_writer_state_t cbor_writer_state_t
Structure of the internal state of a CBOR writer.
struct cbor_reader_state_t cbor_reader_state_t
Structure of the internal state of a CBOR reader.
void cbor_close_data(cbor_writer_state_t *state)
Stops enclosing subsequent CBOR objects in the innermost byte string.
Definition cbor.c:249
void cbor_open_map(cbor_writer_state_t *state)
Adds subsequent entries to a map.
Definition cbor.c:276
cbor_size_t cbor_read_signed(cbor_reader_state_t *state, int64_t *value)
Reads a signed integer.
Definition cbor.c:364
void cbor_break_writer(cbor_writer_state_t *state)
Marks the CBOR output as erroneous.
Definition cbor.c:62
size_t cbor_read_array(cbor_reader_state_t *state)
Reads the number of elements of an array.
Definition cbor.c:442
cbor_major_type_t
Enumeration of major types.
Definition cbor.h:75
bool cbor_end_reader(cbor_reader_state_t *state)
Ensures that no bytes remain unread.
Definition cbor.c:310
cbor_simple_value_t
Enumeration of simple values.
Definition cbor.h:89
cbor_size_t cbor_read_unsigned(cbor_reader_state_t *state, uint64_t *value)
Reads an unsigned integer.
Definition cbor.c:316
cbor_simple_value_t cbor_read_simple(cbor_reader_state_t *state)
Reads a simple value.
Definition cbor.c:420
void cbor_write_text(cbor_writer_state_t *state, const char *text, size_t text_size)
Appends a text string.
Definition cbor.c:196
struct cbor_nesting_record_t cbor_nesting_record_t
Structure of a nesting record.
void cbor_write_unsigned(cbor_writer_state_t *state, uint64_t value)
Appends an unsigned integer.
Definition cbor.c:169
cbor_major_type_t cbor_peek_next(cbor_reader_state_t *state)
Inspects the next major type.
Definition cbor.c:301
void cbor_write_null(cbor_writer_state_t *state)
Appends the simple value null.
Definition cbor.c:205
void cbor_open_data(cbor_writer_state_t *state)
Encloses subsequent CBOR objects in a byte string.
Definition cbor.c:243
void cbor_write_bool(cbor_writer_state_t *state, bool boolean)
Appends a boolean simple value.
Definition cbor.c:217
void cbor_open_array(cbor_writer_state_t *state)
Adds subsequent CBOR objects to an array.
Definition cbor.c:260
const uint8_t * cbor_read_data(cbor_reader_state_t *state, size_t *data_size)
Reads a byte string.
Definition cbor.c:402
void cbor_close_map(cbor_writer_state_t *state)
Stops adding subsequent entries to the innermost map.
Definition cbor.c:282
void cbor_write_object(cbor_writer_state_t *state, const void *object, size_t object_size)
Appends an arbitrary CBOR object.
Definition cbor.c:106
#define CBOR_MAX_NESTING
Defines how many arrays and maps can be open simultaneously while writing.
Definition cbor.h:58
void cbor_init_writer(cbor_writer_state_t *state, uint8_t *buffer, size_t buffer_size)
Prepares for writing CBOR output.
Definition cbor.c:44
cbor_size_t
Enumeration of size information in various major types.
Definition cbor.h:100
void cbor_close_array(cbor_writer_state_t *state)
Stops adding subsequent CBOR objects to the innermost array.
Definition cbor.c:266
void cbor_init_reader(cbor_reader_state_t *state, const uint8_t *cbor, size_t cbor_size)
Prepares for reading CBOR input.
Definition cbor.c:293
void cbor_write_data(cbor_writer_state_t *state, const uint8_t *data, size_t data_size)
Appends a byte string.
Definition cbor.c:187
void cbor_write_signed(cbor_writer_state_t *state, int64_t value)
Appends a signed integer.
Definition cbor.c:176
size_t cbor_read_map(cbor_reader_state_t *state)
Reads the number of entries of a map.
Definition cbor.c:451
void cbor_write_undefined(cbor_writer_state_t *state)
Appends the simple value undefined.
Definition cbor.c:211
const char * cbor_read_text(cbor_reader_state_t *state, size_t *text_size)
Reads a text string.
Definition cbor.c:411
@ CBOR_SIZE_NONE
error condition
Definition cbor.h:101
@ CBOR_SIZE_4
4 bytes
Definition cbor.h:104
@ CBOR_SIZE_2
2 bytes
Definition cbor.h:103
@ CBOR_SIZE_8
8 bytes
Definition cbor.h:105
@ CBOR_SIZE_1
1 byte
Definition cbor.h:102
Structure of a nesting record.
Definition cbor.h:111
Structure of the internal state of a CBOR reader.
Definition cbor.h:130
Structure of the internal state of a CBOR writer.
Definition cbor.h:119