blob: 64861386a5f5fd18a28b34253a54a6db0ce17d0d [file] [log] [blame]
Thiago Macieiraf1cadf02015-05-08 17:19:17 -07001/****************************************************************************
2**
Thiago Macieira46a818e2015-10-08 15:13:05 +02003** Copyright (C) 2016 Intel Corporation
Thiago Macieiraf1cadf02015-05-08 17:19:17 -07004**
5** Permission is hereby granted, free of charge, to any person obtaining a copy
6** of this software and associated documentation files (the "Software"), to deal
7** in the Software without restriction, including without limitation the rights
8** to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9** copies of the Software, and to permit persons to whom the Software is
10** furnished to do so, subject to the following conditions:
11**
12** The above copyright notice and this permission notice shall be included in
13** all copies or substantial portions of the Software.
14**
15** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16** IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17** FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18** AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19** LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20** OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21** THE SOFTWARE.
22**
23****************************************************************************/
24
Thiago Macieiraed5b57c2015-07-07 16:38:27 -070025#define _BSD_SOURCE 1
Thiago Macieiraf1cadf02015-05-08 17:19:17 -070026#include "cbor.h"
27#include "cborconstants_p.h"
28#include "compilersupport_p.h"
29
30#include <assert.h>
Thiago Macieiraf1cadf02015-05-08 17:19:17 -070031#include <stdlib.h>
32#include <string.h>
33
Thiago Macieira8f3fb782015-06-16 16:27:01 -070034#include "assert_p.h" /* Always include last */
35
Thiago Macieira46a818e2015-10-08 15:13:05 +020036/**
37 * \defgroup CborEncoding Encoding to CBOR
38 * \brief Group of functions used to encode data to CBOR.
39 *
40 * CborEncoder is used to encode data into a CBOR stream. The outermost
41 * CborEncoder is initialized by calling cbor_encoder_init(), with the buffer
42 * where the CBOR stream will be stored. The outermost CborEncoder is usually
43 * used to encode exactly one item, most often an array or map. It is possible
44 * to encode more than one item, but care must then be taken on the decoder
45 * side to ensure the state is reset after each item was decoded.
46 *
47 * Nested CborEncoder objects are created using cbor_encoder_create_array() and
48 * cbor_encoder_create_map(), later closed with cbor_encoder_close_container()
49 * or cbor_encoder_close_container_checked(). The pairs of creation and closing
50 * must be exactly matched and their parameters are always the same.
51 *
52 * CborEncoder writes directly to the user-supplied buffer, without extra
53 * buffering. CborEncoder does not allocate memory and CborEncoder objects are
54 * usually created on the stack of the encoding functions.
55 *
56 * The example below initializes a CborEncoder object with a buffer and encodes
57 * a single integer.
58 *
59 * \code
60 * uint8_t buf[16];
61 * CborEncoder encoder;
62 * cbor_encoder_init(&encoder, &buf, sizeof(buf), 0);
63 * cbor_encode_int(&encoder, some_value);
64 * \endcode
65 *
66 * As explained before, usually the outermost CborEncoder object is used to add
67 * one array or map, which in turn contains multiple elements. The example
68 * below creates a CBOR map with one element: a key "foo" and a boolean value.
69 *
70 * \code
71 * uint8_t buf[16];
72 * CborEncoder encoder, mapEncoder;
73 * cbor_encoder_init(&encoder, &buf, sizeof(buf), 0);
74 * cbor_encoder_create_map(&encoder, &mapEncoder, 1);
75 * cbor_encode_text_stringz(&mapEncoder, "foo");
76 * cbor_encode_boolean(&mapEncoder, some_value);
77 * cbor_encoder_close_container(&encoder, &mapEncoder);
78 * \endcode
79 *
80 * <h3 class="groupheader">Error checking and buffer size</h2>
81 *
82 * All functions operating on CborEncoder return a condition of type CborError.
83 * If the encoding was successful, they return CborNoError. Some functions do
84 * extra checking on the input provided and may return some other error
85 * conditions (for example, cbor_encode_simple_value() checks that the type is
86 * of the correct type).
87 *
88 * In addition, all functions check whether the buffer has enough bytes to
89 * encode the item being appended. If that is not possible, they return
90 * CborErrorOutOfMemory.
91 *
92 * It is possible to continue with the encoding of data past the first function
93 * that returns CborErrorOutOfMemory. CborEncoder functions will not overrun
94 * the buffer, but will instead count how many more bytes are needed to
95 * complete the encoding. The CborEncoder::bytes_needed member will contain
96 * that count.
97 *
98 * \section1 Finalizing the encoding
99 *
100 * Once all items have been appended and the containers have all been properly
101 * closed, the user-supplied buffer will contain the CBOR stream and may be
102 * immediately used. The CborEncoder::ptr member will contain the pointer to
103 * one-past-the-last byte of the data and can be used to calculate the size of
104 * the payload.
105 *
106 * The example below illustrates how one can encode an item with error checking
107 * and then pass on the buffer for network sending.
108 *
109 * \code
110 * uint8_t buf[16];
111 * CborError err;
112 * CborEncoder encoder, mapEncoder;
113 * cbor_encoder_init(&encoder, &buf, sizeof(buf), 0);
114 * err = cbor_encoder_create_map(&encoder, &mapEncoder, 1);
115 * if (!err)
116 * return err;
117 * err = cbor_encode_text_stringz(&mapEncoder, "foo");
118 * if (!err)
119 * return err;
120 * err = cbor_encode_boolean(&mapEncoder, some_value);
121 * if (!err)
122 * return err;
123 * err = cbor_encoder_close_container_checked(&encoder, &mapEncoder);
124 * if (!err)
125 * return err;
126 *
127 * ptrdiff_t len = encoder.ptr - buf;
128 * send_payload(buf, len);
129 * return CborNoError;
130 * \endcode
131 */
132
133/**
134 * \addtogroup CborEncoding
135 * @{
136 */
137
138/**
139 * \struct CborEncoder
140 * Structure used to encode to CBOR.
141 */
142
143/**
144 * Initializes a CborEncoder structure \a encoder by pointing it to buffer \a
145 * buffer of size \a size. The \a flags field is currently unused and must be
146 * zero.
147 */
Thiago Macieira5752ce52015-06-16 12:10:03 -0700148void cbor_encoder_init(CborEncoder *encoder, uint8_t *buffer, size_t size, int flags)
Thiago Macieiraf1cadf02015-05-08 17:19:17 -0700149{
150 encoder->ptr = buffer;
151 encoder->end = buffer + size;
Thiago Macieira4e9626c2015-09-21 14:57:17 -0700152 encoder->added = 0;
Thiago Macieiraf1cadf02015-05-08 17:19:17 -0700153 encoder->flags = flags;
154}
155
Thiago Macieira5752ce52015-06-16 12:10:03 -0700156static inline void put16(void *where, uint16_t v)
Thiago Macieiraf1cadf02015-05-08 17:19:17 -0700157{
Thiago Macieira5934a9f2015-06-16 11:55:28 -0700158 v = cbor_htons(v);
Thiago Macieiraf1cadf02015-05-08 17:19:17 -0700159 memcpy(where, &v, sizeof(v));
160}
161
Thiago Macieiradbc01292016-06-06 17:02:25 -0700162/* Note: Since this is currently only used in situations where OOM is the only
163 * valid error, we KNOW this to be true. Thus, this function now returns just 'true',
164 * but if in the future, any function starts returning a non-OOM error, this will need
165 * to be changed to the test. At the moment, this is done to prevent more branches
166 * being created in the tinycbor output */
Erich Keane47a78562015-08-10 15:19:50 -0700167static inline bool isOomError(CborError err)
168{
169 (void) err;
170 return true;
171}
172
Thiago Macieira5752ce52015-06-16 12:10:03 -0700173static inline void put32(void *where, uint32_t v)
Thiago Macieiraf1cadf02015-05-08 17:19:17 -0700174{
Thiago Macieira5934a9f2015-06-16 11:55:28 -0700175 v = cbor_htonl(v);
Thiago Macieiraf1cadf02015-05-08 17:19:17 -0700176 memcpy(where, &v, sizeof(v));
177}
178
Thiago Macieira5752ce52015-06-16 12:10:03 -0700179static inline void put64(void *where, uint64_t v)
Thiago Macieiraf1cadf02015-05-08 17:19:17 -0700180{
Thiago Macieira5934a9f2015-06-16 11:55:28 -0700181 v = cbor_htonll(v);
Thiago Macieiraf1cadf02015-05-08 17:19:17 -0700182 memcpy(where, &v, sizeof(v));
183}
184
Thiago Macieira397f9792015-09-18 19:36:26 -0700185static inline bool would_overflow(CborEncoder *encoder, size_t len)
186{
187 ptrdiff_t remaining = (ptrdiff_t)encoder->end;
188 remaining -= remaining ? (ptrdiff_t)encoder->ptr : encoder->bytes_needed;
189 remaining -= (ptrdiff_t)len;
190 return unlikely(remaining < 0);
191}
192
193static inline void advance_ptr(CborEncoder *encoder, size_t n)
194{
195 if (encoder->end)
196 encoder->ptr += n;
197 else
198 encoder->bytes_needed += n;
199}
200
Thiago Macieira5752ce52015-06-16 12:10:03 -0700201static inline CborError append_to_buffer(CborEncoder *encoder, const void *data, size_t len)
Thiago Macieiraf1cadf02015-05-08 17:19:17 -0700202{
Thiago Macieira397f9792015-09-18 19:36:26 -0700203 if (would_overflow(encoder, len)) {
Erich Keane47a78562015-08-10 15:19:50 -0700204 if (encoder->end != NULL) {
205 len -= encoder->end - encoder->ptr;
Thiago Macieira397f9792015-09-18 19:36:26 -0700206 encoder->end = NULL;
207 encoder->bytes_needed = 0;
Erich Keane47a78562015-08-10 15:19:50 -0700208 }
209
Thiago Macieira397f9792015-09-18 19:36:26 -0700210 advance_ptr(encoder, len);
Thiago Macieiraf1cadf02015-05-08 17:19:17 -0700211 return CborErrorOutOfMemory;
Erich Keane47a78562015-08-10 15:19:50 -0700212 }
213
Thiago Macieiraf1cadf02015-05-08 17:19:17 -0700214 memcpy(encoder->ptr, data, len);
215 encoder->ptr += len;
216 return CborNoError;
217}
218
Thiago Macieira5752ce52015-06-16 12:10:03 -0700219static inline CborError append_byte_to_buffer(CborEncoder *encoder, uint8_t byte)
Thiago Macieira8f7bd9e2015-05-12 13:37:46 +0900220{
Thiago Macieira618d2f12015-09-22 09:14:29 -0700221 return append_to_buffer(encoder, &byte, 1);
Thiago Macieira8f7bd9e2015-05-12 13:37:46 +0900222}
223
Thiago Macieira4e9626c2015-09-21 14:57:17 -0700224static inline CborError encode_number_no_update(CborEncoder *encoder, uint64_t ui, uint8_t shiftedMajorType)
Thiago Macieiraf1cadf02015-05-08 17:19:17 -0700225{
Thiago Macieiraafa4ff62015-05-09 10:09:55 -0700226 /* Little-endian would have been so much more convenient here:
227 * We could just write at the beginning of buf but append_to_buffer
228 * only the necessary bytes.
229 * Since it has to be big endian, do it the other way around:
230 * write from the end. */
Thiago Macieira510e5b82015-09-21 16:05:02 -0700231 uint64_t buf[2];
232 uint8_t *const bufend = (uint8_t *)buf + sizeof(buf);
Thiago Macieira5752ce52015-06-16 12:10:03 -0700233 uint8_t *bufstart = bufend - 1;
Thiago Macieiradbc01292016-06-06 17:02:25 -0700234 put64(buf + 1, ui); /* we probably have a bunch of zeros in the beginning */
Thiago Macieiraf1cadf02015-05-08 17:19:17 -0700235
236 if (ui < Value8Bit) {
Thiago Macieiraafa4ff62015-05-09 10:09:55 -0700237 *bufstart += shiftedMajorType;
Thiago Macieiraf1cadf02015-05-08 17:19:17 -0700238 } else {
Thiago Macieiraafa4ff62015-05-09 10:09:55 -0700239 unsigned more = 0;
Thiago Macieira13c85792015-07-07 16:15:41 -0700240 if (ui > 0xffU)
Thiago Macieiraafa4ff62015-05-09 10:09:55 -0700241 ++more;
Thiago Macieira13c85792015-07-07 16:15:41 -0700242 if (ui > 0xffffU)
Thiago Macieiraafa4ff62015-05-09 10:09:55 -0700243 ++more;
Thiago Macieira13c85792015-07-07 16:15:41 -0700244 if (ui > 0xffffffffU)
Thiago Macieiraafa4ff62015-05-09 10:09:55 -0700245 ++more;
Thiago Macieirae12dfd02016-06-07 16:29:25 -0700246 bufstart -= (size_t)1 << more;
Thiago Macieiraafa4ff62015-05-09 10:09:55 -0700247 *bufstart = shiftedMajorType + Value8Bit + more;
Thiago Macieiraf1cadf02015-05-08 17:19:17 -0700248 }
249
Thiago Macieiraafa4ff62015-05-09 10:09:55 -0700250 return append_to_buffer(encoder, bufstart, bufend - bufstart);
Thiago Macieiraf1cadf02015-05-08 17:19:17 -0700251}
252
Thiago Macieira4e9626c2015-09-21 14:57:17 -0700253static inline CborError encode_number(CborEncoder *encoder, uint64_t ui, uint8_t shiftedMajorType)
254{
255 ++encoder->added;
256 return encode_number_no_update(encoder, ui, shiftedMajorType);
257}
258
Thiago Macieira46a818e2015-10-08 15:13:05 +0200259/**
260 * Appends the unsigned 64-bit integer \a value to the CBOR stream provided by
261 * \a encoder.
262 *
263 * \sa cbor_encode_negative_int, cbor_encode_int
264 */
Thiago Macieiraf1cadf02015-05-08 17:19:17 -0700265CborError cbor_encode_uint(CborEncoder *encoder, uint64_t value)
266{
267 return encode_number(encoder, value, UnsignedIntegerType << MajorTypeShift);
268}
269
Thiago Macieira46a818e2015-10-08 15:13:05 +0200270/**
271 * Appends the negative 64-bit integer whose absolute value is \a
272 * absolute_value to the CBOR stream provided by \a encoder.
273 *
274 * \sa cbor_encode_uint, cbor_encode_int
275 */
Thiago Macieira78632b32015-09-26 14:18:48 -0700276CborError cbor_encode_negative_int(CborEncoder *encoder, uint64_t absolute_value)
277{
278 return encode_number(encoder, absolute_value, NegativeIntegerType << MajorTypeShift);
279}
280
Thiago Macieira46a818e2015-10-08 15:13:05 +0200281/**
282 * Appends the signed 64-bit integer \a value to the CBOR stream provided by
283 * \a encoder.
284 *
285 * \sa cbor_encode_negative_int, cbor_encode_uint
286 */
Thiago Macieiraf1cadf02015-05-08 17:19:17 -0700287CborError cbor_encode_int(CborEncoder *encoder, int64_t value)
288{
Thiago Macieiradbc01292016-06-06 17:02:25 -0700289 /* adapted from code in RFC 7049 appendix C (pseudocode) */
290 uint64_t ui = value >> 63; /* extend sign to whole length */
291 uint8_t majorType = ui & 0x20; /* extract major type */
292 ui ^= value; /* complement negatives */
Thiago Macieiraf1cadf02015-05-08 17:19:17 -0700293 return encode_number(encoder, ui, majorType);
294}
295
Thiago Macieira46a818e2015-10-08 15:13:05 +0200296/**
297 * Appends the CBOR Simple Type of value \a value to the CBOR stream provided by
298 * \a encoder.
299 *
300 * This function may return error CborErrorIllegalSimpleType if the \a value
301 * variable contains a number that is not a valid simple type.
302 */
Thiago Macieiraf1cadf02015-05-08 17:19:17 -0700303CborError cbor_encode_simple_value(CborEncoder *encoder, uint8_t value)
304{
305#ifndef CBOR_ENCODER_NO_CHECK_USER
Thiago Macieiradbc01292016-06-06 17:02:25 -0700306 /* check if this is a valid simple type */
Thiago Macieiraf1cadf02015-05-08 17:19:17 -0700307 if (value >= HalfPrecisionFloat && value <= Break)
308 return CborErrorIllegalSimpleType;
309#endif
310 return encode_number(encoder, value, SimpleTypesType << MajorTypeShift);
311}
312
Thiago Macieira46a818e2015-10-08 15:13:05 +0200313/**
314 * Appends the floating-point value of type \a fpType and pointed to by \a
315 * value to the CBOR stream provided by \a encoder. The value of \a fpType must
316 * be one of CborHalfFloatType, CborFloatType or CborDoubleType, otherwise the
317 * behavior of this function is undefined.
318 *
319 * This function is useful for code that needs to pass through floating point
320 * values but does not wish to have the actual floating-point code.
321 *
322 * \sa cbor_encode_half_float, cbor_encode_float, cbor_encode_double
323 */
Thiago Macieira81f33432015-07-02 16:16:28 -0700324CborError cbor_encode_floating_point(CborEncoder *encoder, CborType fpType, const void *value)
Thiago Macieiraf1cadf02015-05-08 17:19:17 -0700325{
Thiago Macieira81f33432015-07-02 16:16:28 -0700326 uint8_t buf[1 + sizeof(uint64_t)];
327 assert(fpType == CborHalfFloatType || fpType == CborFloatType || fpType == CborDoubleType);
328 buf[0] = fpType;
Thiago Macieiraf1cadf02015-05-08 17:19:17 -0700329
Thiago Macieira81f33432015-07-02 16:16:28 -0700330 unsigned size = 2U << (fpType - CborHalfFloatType);
331 if (size == 8)
332 put64(buf + 1, *(const uint64_t*)value);
333 else if (size == 4)
334 put32(buf + 1, *(const uint32_t*)value);
335 else
336 put16(buf + 1, *(const uint16_t*)value);
Thiago Macieira4e9626c2015-09-21 14:57:17 -0700337 ++encoder->added;
Thiago Macieira81f33432015-07-02 16:16:28 -0700338 return append_to_buffer(encoder, buf, size + 1);
Thiago Macieiraf1cadf02015-05-08 17:19:17 -0700339}
Thiago Macieira8f98a112015-05-08 17:25:29 -0700340
Thiago Macieira46a818e2015-10-08 15:13:05 +0200341/**
342 * Appends the CBOR tag \a tag to the CBOR stream provided by \a encoder.
343 *
344 * \sa CborTag
345 */
Thiago Macieira8f98a112015-05-08 17:25:29 -0700346CborError cbor_encode_tag(CborEncoder *encoder, CborTag tag)
347{
Thiago Macieiradbc01292016-06-06 17:02:25 -0700348 /* tags don't count towards the number of elements in an array or map */
Thiago Macieira4e9626c2015-09-21 14:57:17 -0700349 return encode_number_no_update(encoder, tag, TagType << MajorTypeShift);
Thiago Macieira8f98a112015-05-08 17:25:29 -0700350}
Thiago Macieirab54debe2015-05-08 17:42:39 -0700351
Thiago Macieira5752ce52015-06-16 12:10:03 -0700352static CborError encode_string(CborEncoder *encoder, size_t length, uint8_t shiftedMajorType, const void *string)
Thiago Macieirab54debe2015-05-08 17:42:39 -0700353{
354 CborError err = encode_number(encoder, length, shiftedMajorType);
Erich Keane47a78562015-08-10 15:19:50 -0700355 if (err && !isOomError(err))
Thiago Macieirab54debe2015-05-08 17:42:39 -0700356 return err;
357 return append_to_buffer(encoder, string, length);
358}
359
Thiago Macieira46a818e2015-10-08 15:13:05 +0200360/**
361 * \fn CborError cbor_encode_text_stringz(CborEncoder *encoder, const char *string)
362 *
363 * Appends the null-terminated text string \a string to the CBOR stream
364 * provided by \a encoder. CBOR requires that \a string be valid UTF-8, but
365 * TinyCBOR makes no verification of correctness. The terminating null is not
366 * included in the stream.
367 *
368 * \sa cbor_encode_text_string, cbor_encode_byte_string
369 */
370
371/**
372 * Appends the text string \a string of length \a length to the CBOR stream
373 * provided by \a encoder. CBOR requires that \a string be valid UTF-8, but
374 * TinyCBOR makes no verification of correctness.
375 *
376 * \sa CborError cbor_encode_text_stringz, cbor_encode_byte_string
377 */
Thiago Macieira5752ce52015-06-16 12:10:03 -0700378CborError cbor_encode_byte_string(CborEncoder *encoder, const uint8_t *string, size_t length)
Thiago Macieirab54debe2015-05-08 17:42:39 -0700379{
380 return encode_string(encoder, length, ByteStringType << MajorTypeShift, string);
381}
382
Thiago Macieira46a818e2015-10-08 15:13:05 +0200383/**
384 * Appends the byte string \a string of length \a length to the CBOR stream
385 * provided by \a encoder. CBOR byte strings are arbitrary raw data.
386 *
387 * \sa cbor_encode_text_stringz, cbor_encode_text_string
388 */
Thiago Macieirab54debe2015-05-08 17:42:39 -0700389CborError cbor_encode_text_string(CborEncoder *encoder, const char *string, size_t length)
390{
391 return encode_string(encoder, length, TextStringType << MajorTypeShift, string);
392}
Thiago Macieira355817e2015-05-08 18:38:18 -0700393
Thiago Macieira56635242015-09-20 17:44:51 -0700394#ifdef __GNUC__
395__attribute__((noinline))
396#endif
397static CborError create_container(CborEncoder *encoder, CborEncoder *container, size_t length, uint8_t shiftedMajorType)
Thiago Macieira355817e2015-05-08 18:38:18 -0700398{
Thiago Macieira8f7bd9e2015-05-12 13:37:46 +0900399 CborError err;
Thiago Macieira4e9626c2015-09-21 14:57:17 -0700400 container->ptr = encoder->ptr;
401 container->end = encoder->end;
402 ++encoder->added;
403 container->added = 0;
404
405 cbor_static_assert(((MapType << MajorTypeShift) & CborIteratorFlag_ContainerIsMap) == CborIteratorFlag_ContainerIsMap);
406 cbor_static_assert(((ArrayType << MajorTypeShift) & CborIteratorFlag_ContainerIsMap) == 0);
407 container->flags = shiftedMajorType & CborIteratorFlag_ContainerIsMap;
408
409 if (length == CborIndefiniteLength) {
410 container->flags |= CborIteratorFlag_UnknownLength;
411 err = append_byte_to_buffer(container, shiftedMajorType + IndefiniteLength);
412 } else {
413 err = encode_number_no_update(container, length, shiftedMajorType);
414 }
Erich Keane47a78562015-08-10 15:19:50 -0700415 if (err && !isOomError(err))
Thiago Macieira355817e2015-05-08 18:38:18 -0700416 return err;
Thiago Macieira8f7bd9e2015-05-12 13:37:46 +0900417
Thiago Macieira355817e2015-05-08 18:38:18 -0700418 return CborNoError;
419}
420
Thiago Macieira46a818e2015-10-08 15:13:05 +0200421/**
422 * Creates a CBOR array in the CBOR stream provided by \a encoder and
423 * initializes \a arrayEncoder so that items can be added to the array using
424 * the CborEncoder functions. The array must be terminated by calling either
425 * cbor_encoder_close_container() or cbor_encoder_close_container_checked()
426 * with the same \a encoder and \a arrayEncoder parameters.
427 *
428 * The number of items inserted into the array must be exactly \a length items,
429 * otherwise the stream is invalid. If the number of items is not known when
430 * creating the array, the constant \ref CborIndefiniteLength may be passed as
431 * length instead.
432 *
433 * \sa cbor_encoder_create_map
434 */
Thiago Macieira355817e2015-05-08 18:38:18 -0700435CborError cbor_encoder_create_array(CborEncoder *encoder, CborEncoder *arrayEncoder, size_t length)
436{
Thiago Macieira56635242015-09-20 17:44:51 -0700437 return create_container(encoder, arrayEncoder, length, ArrayType << MajorTypeShift);
Thiago Macieira355817e2015-05-08 18:38:18 -0700438}
439
Thiago Macieira46a818e2015-10-08 15:13:05 +0200440/**
441 * Creates a CBOR map in the CBOR stream provided by \a encoder and
442 * initializes \a mapEncoder so that items can be added to the map using
443 * the CborEncoder functions. The map must be terminated by calling either
444 * cbor_encoder_close_container() or cbor_encoder_close_container_checked()
445 * with the same \a encoder and \a mapEncoder parameters.
446 *
447 * The number of pair of items inserted into the map must be exactly \a length
448 * items, otherwise the stream is invalid. If the number of items is not known
449 * when creating the map, the constant \ref CborIndefiniteLength may be passed as
450 * length instead.
451 *
452 * \b{Implementation limitation:} TinyCBOR cannot encode more than SIZE_MAX/2
453 * key-value pairs in the stream. If the length \a length is larger than this
454 * value, this function returns error CborErrorDataTooLarge.
455 *
456 * \sa cbor_encoder_create_array
457 */
Thiago Macieira355817e2015-05-08 18:38:18 -0700458CborError cbor_encoder_create_map(CborEncoder *encoder, CborEncoder *mapEncoder, size_t length)
459{
Thiago Macieira4e9626c2015-09-21 14:57:17 -0700460 if (length != CborIndefiniteLength && length > SIZE_MAX / 2)
461 return CborErrorDataTooLarge;
Thiago Macieira56635242015-09-20 17:44:51 -0700462 return create_container(encoder, mapEncoder, length, MapType << MajorTypeShift);
Thiago Macieira355817e2015-05-08 18:38:18 -0700463}
464
Thiago Macieira46a818e2015-10-08 15:13:05 +0200465/**
466 * Closes the CBOR container (array or map) provided by \a containerEncoder and
467 * updates the CBOR stream provided by \a encoder. Both parameters must be the
468 * same as were passed to cbor_encoder_create_array() or
469 * cbor_encoder_create_map().
470 *
471 * This function does not verify that the number of items (or pair of items, in
472 * the case of a map) was correct. To execute that verification, call
473 * cbor_encoder_close_container_checked() instead.
474 *
475 * \sa cbor_encoder_create_array(), cbor_encoder_create_map()
476 */
Thiago Macieira355817e2015-05-08 18:38:18 -0700477CborError cbor_encoder_close_container(CborEncoder *encoder, const CborEncoder *containerEncoder)
478{
Thiago Macieira397f9792015-09-18 19:36:26 -0700479 if (encoder->end)
480 encoder->ptr = containerEncoder->ptr;
481 else
482 encoder->bytes_needed = containerEncoder->bytes_needed;
Erich Keane47a78562015-08-10 15:19:50 -0700483 encoder->end = containerEncoder->end;
Thiago Macieira8f7bd9e2015-05-12 13:37:46 +0900484 if (containerEncoder->flags & CborIteratorFlag_UnknownLength)
485 return append_byte_to_buffer(encoder, BreakByte);
Thiago Macieira355817e2015-05-08 18:38:18 -0700486 return CborNoError;
487}
Thiago Macieira46a818e2015-10-08 15:13:05 +0200488
489/**
490 * \fn CborError cbor_encode_boolean(CborEncoder *encoder, bool value)
491 *
492 * Appends the boolean value \a value to the CBOR stream provided by \a encoder.
493 */
494
495/**
496 * \fn CborError cbor_encode_null(CborEncoder *encoder)
497 *
498 * Appends the CBOR type representing a null value to the CBOR stream provided
499 * by \a encoder.
500 *
501 * \sa cbor_encode_undefined()
502 */
503
504/**
505 * \fn CborError cbor_encode_undefined(CborEncoder *encoder)
506 *
507 * Appends the CBOR type representing an undefined value to the CBOR stream
508 * provided by \a encoder.
509 *
510 * \sa cbor_encode_null()
511 */
512
513/**
514 * \fn CborError cbor_encode_half_float(CborEncoder *encoder, const void *value)
515 *
516 * Appends the IEEE 754 half-precision (16-bit) floating point value pointed to
517 * by \a value to the CBOR stream provided by \a encoder.
518 *
519 * \sa cbor_encode_floating_point(), cbor_encode_float(), cbor_encode_double()
520 */
521
522/**
523 * \fn CborError cbor_encode_float(CborEncoder *encoder, float value)
524 *
525 * Appends the IEEE 754 single-precision (32-bit) floating point value \a value
526 * to the CBOR stream provided by \a encoder.
527 *
528 * \sa cbor_encode_floating_point(), cbor_encode_half_float(), cbor_encode_double()
529 */
530
531/**
532 * \fn CborError cbor_encode_double(CborEncoder *encoder, double value)
533 *
534 * Appends the IEEE 754 double-precision (64-bit) floating point value \a value
535 * to the CBOR stream provided by \a encoder.
536 *
537 * \sa cbor_encode_floating_point(), cbor_encode_half_float(), cbor_encode_float()
538 */
539
540/** @} */