1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 Intel Corporation
4 **
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 
25 #define _BSD_SOURCE 1
26 #define _DEFAULT_SOURCE 1
27 #ifndef __STDC_LIMIT_MACROS
28 #  define __STDC_LIMIT_MACROS 1
29 #endif
30 
31 #include "tinycbor/cbor.h"
32 #include "tinycbor/cborconstants_p.h"
33 #include "tinycbor/compilersupport_p.h"
34 
35 #include <assert.h>
36 #include <stdlib.h>
37 #include <string.h>
38 
39 
40 #include "tinycbor/assert_p.h"       /* Always include last */
41 
42 /**
43  * \defgroup CborEncoding Encoding to CBOR
44  * \brief Group of functions used to encode data to CBOR.
45  *
46  * CborEncoder is used to encode data into a CBOR stream. The outermost
47  * CborEncoder is initialized by calling cbor_encoder_init(), with the buffer
48  * where the CBOR stream will be stored. The outermost CborEncoder is usually
49  * used to encode exactly one item, most often an array or map. It is possible
50  * to encode more than one item, but care must then be taken on the decoder
51  * side to ensure the state is reset after each item was decoded.
52  *
53  * Nested CborEncoder objects are created using cbor_encoder_create_array() and
54  * cbor_encoder_create_map(), later closed with cbor_encoder_close_container()
55  * or cbor_encoder_close_container_checked(). The pairs of creation and closing
56  * must be exactly matched and their parameters are always the same.
57  *
58  * CborEncoder writes directly to the user-supplied buffer, without extra
59  * buffering. CborEncoder does not allocate memory and CborEncoder objects are
60  * usually created on the stack of the encoding functions.
61  *
62  * The example below initializes a CborEncoder object with a buffer and encodes
63  * a single integer.
64  *
65  * \code
66  *      uint8_t buf[16];
67  *      CborEncoder encoder;
68  *      cbor_encoder_init(&encoder, &buf, sizeof(buf), 0);
69  *      cbor_encode_int(&encoder, some_value);
70  * \endcode
71  *
72  * As explained before, usually the outermost CborEncoder object is used to add
73  * one array or map, which in turn contains multiple elements. The example
74  * below creates a CBOR map with one element: a key "foo" and a boolean value.
75  *
76  * \code
77  *      uint8_t buf[16];
78  *      CborEncoder encoder, mapEncoder;
79  *      cbor_encoder_init(&encoder, &buf, sizeof(buf), 0);
80  *      cbor_encoder_create_map(&encoder, &mapEncoder, 1);
81  *      cbor_encode_text_stringz(&mapEncoder, "foo");
82  *      cbor_encode_boolean(&mapEncoder, some_value);
83  *      cbor_encoder_close_container(&encoder, &mapEncoder);
84  * \endcode
85  *
86  * <h3 class="groupheader">Error checking and buffer size</h2>
87  *
88  * All functions operating on CborEncoder return a condition of type CborError.
89  * If the encoding was successful, they return CborNoError. Some functions do
90  * extra checking on the input provided and may return some other error
91  * conditions (for example, cbor_encode_simple_value() checks that the type is
92  * of the correct type).
93  *
94  * In addition, all functions check whether the buffer has enough bytes to
95  * encode the item being appended. If that is not possible, they return
96  * CborErrorOutOfMemory.
97  *
98  * It is possible to continue with the encoding of data past the first function
99  * that returns CborErrorOutOfMemory. CborEncoder functions will not overrun
100  * the buffer, but will instead count how many more bytes are needed to
101  * complete the encoding. At the end, you can obtain that count by calling
102  * cbor_encoder_get_extra_bytes_needed().
103  *
104  * \section1 Finalizing the encoding
105  *
106  * Once all items have been appended and the containers have all been properly
107  * closed, the user-supplied buffer will contain the CBOR stream and may be
108  * immediately used. To obtain the size of the buffer, call
109  * cbor_encoder_get_buffer_size() with the original buffer pointer.
110  *
111  * The example below illustrates how one can encode an item with error checking
112  * and then pass on the buffer for network sending.
113  *
114  * \code
115  *      uint8_t buf[16];
116  *      CborError err;
117  *      CborEncoder encoder, mapEncoder;
118  *      cbor_encoder_init(&encoder, &buf, sizeof(buf), 0);
119  *      err = cbor_encoder_create_map(&encoder, &mapEncoder, 1);
120  *      if (!err)
121  *          return err;
122  *      err = cbor_encode_text_stringz(&mapEncoder, "foo");
123  *      if (!err)
124  *          return err;
125  *      err = cbor_encode_boolean(&mapEncoder, some_value);
126  *      if (!err)
127  *          return err;
128  *      err = cbor_encoder_close_container_checked(&encoder, &mapEncoder);
129  *      if (!err)
130  *          return err;
131  *
132  *      size_t len = cbor_encoder_get_buffer_size(&encoder, buf);
133  *      send_payload(buf, len);
134  *      return CborNoError;
135  * \endcode
136  *
137  * Finally, the example below illustrates expands on the one above and also
138  * deals with dynamically growing the buffer if the initial allocation wasn't
139  * big enough. Note the two places where the error checking was replaced with
140  * an assertion, showing where the author assumes no error can occur.
141  *
142  * \code
143  * uint8_t *encode_string_array(const char **strings, int n, size_t *bufsize)
144  * {
145  *     CborError err;
146  *     CborEncoder encoder, arrayEncoder;
147  *     size_t size = 256;
148  *     uint8_t *buf = NULL;
149  *
150  *     while (1) {
151  *         int i;
152  *         size_t more_bytes;
153  *         uint8_t *nbuf = realloc(buf, size);
154  *         if (nbuf == NULL)
155  *             goto error;
156  *         buf = nbuf;
157  *
158  *         cbor_encoder_init(&encoder, &buf, size, 0);
159  *         err = cbor_encoder_create_array(&encoder, &arrayEncoder, n);
160  *         assert(err);         // can't fail, the buffer is always big enough
161  *
162  *         for (i = 0; i < n; ++i) {
163  *             err = cbor_encode_text_stringz(&arrayEncoder, strings[i]);
164  *             if (err && err != CborErrorOutOfMemory)
165  *                 goto error;
166  *         }
167  *
168  *         err = cbor_encoder_close_container_checked(&encoder, &arrayEncoder);
169  *         assert(err);         // shouldn't fail!
170  *
171  *         more_bytes = cbor_encoder_get_extra_bytes_needed(encoder);
172  *         if (more_size) {
173  *             // buffer wasn't big enough, try again
174  *             size += more_bytes;
175  *             continue;
176  *         }
177  *
178  *         *bufsize = cbor_encoder_get_buffer_size(encoder, buf);
179  *         return buf;
180  *     }
181  *  error:
182  *     free(buf);
183  *     return NULL;
184  *  }
185  * \endcode
186  */
187 
188 /**
189  * \addtogroup CborEncoding
190  * @{
191  */
192 
193 /**
194  * \struct CborEncoder
195  * Structure used to encode to CBOR.
196  */
197 
198 /**
199  * Initializes a CborEncoder structure \a encoder by pointing it to buffer \a
200  * buffer of size \a size. The \a flags field is currently unused and must be
201  * zero.
202  */
cbor_encoder_init(CborEncoder * encoder,cbor_encoder_writer * writer,int flags)203 void cbor_encoder_init(CborEncoder *encoder, cbor_encoder_writer *writer, int flags)
204 {
205     encoder->writer = writer;
206     encoder->added = 0;
207     encoder->flags = flags;
208 }
209 
210 #ifndef CBOR_NO_FLOATING_POINT
put16(void * where,uint16_t v)211 static inline void put16(void *where, uint16_t v)
212 {
213     v = cbor_htons(v);
214     memcpy(where, &v, sizeof(v));
215 }
216 #endif
217 
218 /* Note: Since this is currently only used in situations where OOM is the only
219  * valid error, we KNOW this to be true.  Thus, this function now returns just 'true',
220  * but if in the future, any function starts returning a non-OOM error, this will need
221  * to be changed to the test.  At the moment, this is done to prevent more branches
222  * being created in the tinycbor output */
isOomError(CborError err)223 static inline bool isOomError(CborError err)
224 {
225     (void) err;
226     return true;
227 }
228 
229 #ifndef CBOR_NO_FLOATING_POINT
put32(void * where,uint32_t v)230 static inline void put32(void *where, uint32_t v)
231 {
232     v = cbor_htonl(v);
233     memcpy(where, &v, sizeof(v));
234 }
235 #endif
236 
put64(void * where,uint64_t v)237 static inline void put64(void *where, uint64_t v)
238 {
239     v = cbor_htonll(v);
240     memcpy(where, &v, sizeof(v));
241 }
242 
append_to_buffer(CborEncoder * encoder,const void * data,size_t len)243 static inline CborError append_to_buffer(CborEncoder *encoder, const void *data, size_t len)
244 {
245     return encoder->writer->write(encoder->writer, data, len);
246 }
247 
append_byte_to_buffer(CborEncoder * encoder,uint8_t byte)248 static inline CborError append_byte_to_buffer(CborEncoder *encoder, uint8_t byte)
249 {
250     return append_to_buffer(encoder, &byte, 1);
251 }
252 
encode_number_no_update(CborEncoder * encoder,uint64_t ui,uint8_t shiftedMajorType)253 static inline CborError encode_number_no_update(CborEncoder *encoder, uint64_t ui, uint8_t shiftedMajorType)
254 {
255     /* Little-endian would have been so much more convenient here:
256      * We could just write at the beginning of buf but append_to_buffer
257      * only the necessary bytes.
258      * Since it has to be big endian, do it the other way around:
259      * write from the end. */
260     uint64_t buf[2];
261     uint8_t *const bufend = (uint8_t *)buf + sizeof(buf);
262     uint8_t *bufstart = bufend - 1;
263     put64(buf + 1, ui);     /* we probably have a bunch of zeros in the beginning */
264 
265     if (ui < Value8Bit) {
266         *bufstart += shiftedMajorType;
267     } else {
268         uint8_t more = 0;
269         if (ui > 0xffU)
270             ++more;
271         if (ui > 0xffffU)
272             ++more;
273         if (ui > 0xffffffffU)
274             ++more;
275         bufstart -= (size_t)1 << more;
276         *bufstart = shiftedMajorType + Value8Bit + more;
277     }
278 
279     return append_to_buffer(encoder, bufstart, bufend - bufstart);
280 }
281 
encode_number(CborEncoder * encoder,uint64_t ui,uint8_t shiftedMajorType)282 static inline CborError encode_number(CborEncoder *encoder, uint64_t ui, uint8_t shiftedMajorType)
283 {
284     ++encoder->added;
285     return encode_number_no_update(encoder, ui, shiftedMajorType);
286 }
287 
288 /**
289  * Appends the unsigned 64-bit integer \a value to the CBOR stream provided by
290  * \a encoder.
291  *
292  * \sa cbor_encode_negative_int, cbor_encode_int
293  */
cbor_encode_uint(CborEncoder * encoder,uint64_t value)294 CborError cbor_encode_uint(CborEncoder *encoder, uint64_t value)
295 {
296     return encode_number(encoder, value, UnsignedIntegerType << MajorTypeShift);
297 }
298 
299 /**
300  * Appends the negative 64-bit integer whose absolute value is \a
301  * absolute_value to the CBOR stream provided by \a encoder.
302  *
303  * \sa cbor_encode_uint, cbor_encode_int
304  */
cbor_encode_negative_int(CborEncoder * encoder,uint64_t absolute_value)305 CborError cbor_encode_negative_int(CborEncoder *encoder, uint64_t absolute_value)
306 {
307     return encode_number(encoder, absolute_value, NegativeIntegerType << MajorTypeShift);
308 }
309 
310 /**
311  * Appends the signed 64-bit integer \a value to the CBOR stream provided by
312  * \a encoder.
313  *
314  * \sa cbor_encode_negative_int, cbor_encode_uint
315  */
cbor_encode_int(CborEncoder * encoder,int64_t value)316 CborError cbor_encode_int(CborEncoder *encoder, int64_t value)
317 {
318     /* adapted from code in RFC 7049 appendix C (pseudocode) */
319     uint64_t ui = value >> 63;              /* extend sign to whole length */
320     uint8_t majorType = ui & 0x20;          /* extract major type */
321     ui ^= value;                            /* complement negatives */
322     return encode_number(encoder, ui, majorType);
323 }
324 
325 /**
326  * Appends the CBOR Simple Type of value \a value to the CBOR stream provided by
327  * \a encoder.
328  *
329  * This function may return error CborErrorIllegalSimpleType if the \a value
330  * variable contains a number that is not a valid simple type.
331  */
cbor_encode_simple_value(CborEncoder * encoder,uint8_t value)332 CborError cbor_encode_simple_value(CborEncoder *encoder, uint8_t value)
333 {
334 #ifndef CBOR_ENCODER_NO_CHECK_USER
335     /* check if this is a valid simple type */
336     if (value >= HalfPrecisionFloat && value <= Break)
337         return CborErrorIllegalSimpleType;
338 #endif
339     return encode_number(encoder, value, SimpleTypesType << MajorTypeShift);
340 }
341 
342 #ifndef CBOR_NO_FLOATING_POINT
343 /**
344  * Appends the floating-point value of type \a fpType and pointed to by \a
345  * value to the CBOR stream provided by \a encoder. The value of \a fpType must
346  * be one of CborHalfFloatType, CborFloatType or CborDoubleType, otherwise the
347  * behavior of this function is undefined.
348  *
349  * This function is useful for code that needs to pass through floating point
350  * values but does not wish to have the actual floating-point code.
351  *
352  * \sa cbor_encode_half_float, cbor_encode_float, cbor_encode_double
353  */
cbor_encode_floating_point(CborEncoder * encoder,CborType fpType,const void * value)354 CborError cbor_encode_floating_point(CborEncoder *encoder, CborType fpType, const void *value)
355 {
356     uint8_t buf[1 + sizeof(uint64_t)];
357     assert(fpType == CborHalfFloatType || fpType == CborFloatType || fpType == CborDoubleType);
358     buf[0] = fpType;
359 
360     unsigned size = 2U << (fpType - CborHalfFloatType);
361     if (size == 8)
362         put64(buf + 1, *(const uint64_t*)value);
363     else if (size == 4)
364         put32(buf + 1, *(const uint32_t*)value);
365     else
366         put16(buf + 1, *(const uint16_t*)value);
367     ++encoder->added;
368     return append_to_buffer(encoder, buf, size + 1);
369 }
370 #endif
371 
372 /**
373  * Appends the CBOR tag \a tag to the CBOR stream provided by \a encoder.
374  *
375  * \sa CborTag
376  */
cbor_encode_tag(CborEncoder * encoder,CborTag tag)377 CborError cbor_encode_tag(CborEncoder *encoder, CborTag tag)
378 {
379     /* tags don't count towards the number of elements in an array or map */
380     return encode_number_no_update(encoder, tag, TagType << MajorTypeShift);
381 }
382 
encode_string(CborEncoder * encoder,size_t length,uint8_t shiftedMajorType,const void * string)383 static CborError encode_string(CborEncoder *encoder, size_t length, uint8_t shiftedMajorType, const void *string)
384 {
385     CborError err = encode_number(encoder, length, shiftedMajorType);
386     if (err && !isOomError(err))
387         return err;
388     return append_to_buffer(encoder, string, length);
389 }
390 
391 /**
392  * \fn CborError cbor_encode_text_stringz(CborEncoder *encoder, const char *string)
393  *
394  * Appends the null-terminated text string \a string to the CBOR stream
395  * provided by \a encoder. CBOR requires that \a string be valid UTF-8, but
396  * TinyCBOR makes no verification of correctness. The terminating null is not
397  * included in the stream.
398  *
399  * \sa cbor_encode_text_string, cbor_encode_byte_string
400  */
401 
402 /**
403  * Appends the text string \a string of length \a length to the CBOR stream
404  * provided by \a encoder. CBOR requires that \a string be valid UTF-8, but
405  * TinyCBOR makes no verification of correctness.
406  *
407  * \sa CborError cbor_encode_text_stringz, cbor_encode_byte_string
408  */
cbor_encode_byte_string(CborEncoder * encoder,const uint8_t * string,size_t length)409 CborError cbor_encode_byte_string(CborEncoder *encoder, const uint8_t *string, size_t length)
410 {
411     return encode_string(encoder, length, ByteStringType << MajorTypeShift, string);
412 }
413 
414 /**
415  * Appends the byte string passed as \a iov and \a iov_len to the CBOR
416  * stream provided by \a encoder. CBOR byte strings are arbitrary raw data.
417  *
418  * \sa CborError cbor_encode_text_stringz, cbor_encode_byte_string
419  */
cbor_encode_byte_iovec(CborEncoder * encoder,const struct cbor_iovec iov[],int iov_len)420 CborError cbor_encode_byte_iovec(CborEncoder *encoder,
421                                  const struct cbor_iovec iov[], int iov_len)
422 {
423     CborError err;
424     size_t length;
425     int i;
426 
427     length = 0;
428     for (i = 0; i < iov_len; i++) {
429         length += iov[i].iov_len;
430     }
431     err = encode_number(encoder, length, ByteStringType << MajorTypeShift);
432     if (err && !isOomError(err)) {
433         return err;
434     }
435     for (i = 0; i < iov_len; i++) {
436         err = append_to_buffer(encoder, iov[i].iov_base, iov[i].iov_len);
437         if (err && !isOomError(err)) {
438             return err;
439         }
440     }
441     return 0;
442 }
443 
444 /**
445  * Appends the byte string \a string of length \a length to the CBOR stream
446  * provided by \a encoder. CBOR byte strings are arbitrary raw data.
447  *
448  * \sa cbor_encode_text_stringz, cbor_encode_text_string
449  */
cbor_encode_text_string(CborEncoder * encoder,const char * string,size_t length)450 CborError cbor_encode_text_string(CborEncoder *encoder, const char *string, size_t length)
451 {
452     return encode_string(encoder, length, TextStringType << MajorTypeShift, string);
453 }
454 
455 #ifdef __GNUC__
456 __attribute__((noinline))
457 #endif
create_container(CborEncoder * encoder,CborEncoder * container,size_t length,uint8_t shiftedMajorType)458 static CborError create_container(CborEncoder *encoder, CborEncoder *container, size_t length, uint8_t shiftedMajorType)
459 {
460     CborError err;
461     container->writer = encoder->writer;
462     ++encoder->added;
463     container->added = 0;
464 
465     cbor_static_assert(((MapType << MajorTypeShift) & CborIteratorFlag_ContainerIsMap) == CborIteratorFlag_ContainerIsMap);
466     cbor_static_assert(((ArrayType << MajorTypeShift) & CborIteratorFlag_ContainerIsMap) == 0);
467     container->flags = shiftedMajorType & CborIteratorFlag_ContainerIsMap;
468 
469     if (length == CborIndefiniteLength) {
470         container->flags |= CborIteratorFlag_UnknownLength;
471         err = append_byte_to_buffer(container, shiftedMajorType + IndefiniteLength);
472     } else {
473         err = encode_number_no_update(container, length, shiftedMajorType);
474     }
475     if (err && !isOomError(err))
476         return err;
477 
478     return CborNoError;
479 }
480 
481 /**
482  * Creates a CBOR array in the CBOR stream provided by \a encoder and
483  * initializes \a arrayEncoder so that items can be added to the array using
484  * the CborEncoder functions. The array must be terminated by calling either
485  * cbor_encoder_close_container() or cbor_encoder_close_container_checked()
486  * with the same \a encoder and \a arrayEncoder parameters.
487  *
488  * The number of items inserted into the array must be exactly \a length items,
489  * otherwise the stream is invalid. If the number of items is not known when
490  * creating the array, the constant \ref CborIndefiniteLength may be passed as
491  * length instead.
492  *
493  * \sa cbor_encoder_create_map
494  */
cbor_encoder_create_array(CborEncoder * encoder,CborEncoder * arrayEncoder,size_t length)495 CborError cbor_encoder_create_array(CborEncoder *encoder, CborEncoder *arrayEncoder, size_t length)
496 {
497     return create_container(encoder, arrayEncoder, length, ArrayType << MajorTypeShift);
498 }
499 
500 /**
501  * Creates a CBOR map in the CBOR stream provided by \a encoder and
502  * initializes \a mapEncoder so that items can be added to the map using
503  * the CborEncoder functions. The map must be terminated by calling either
504  * cbor_encoder_close_container() or cbor_encoder_close_container_checked()
505  * with the same \a encoder and \a mapEncoder parameters.
506  *
507  * The number of pair of items inserted into the map must be exactly \a length
508  * items, otherwise the stream is invalid. If the number of items is not known
509  * when creating the map, the constant \ref CborIndefiniteLength may be passed as
510  * length instead.
511  *
512  * \b{Implementation limitation:} TinyCBOR cannot encode more than SIZE_MAX/2
513  * key-value pairs in the stream. If the length \a length is larger than this
514  * value, this function returns error CborErrorDataTooLarge.
515  *
516  * \sa cbor_encoder_create_array
517  */
cbor_encoder_create_map(CborEncoder * encoder,CborEncoder * mapEncoder,size_t length)518 CborError cbor_encoder_create_map(CborEncoder *encoder, CborEncoder *mapEncoder, size_t length)
519 {
520     if (length != CborIndefiniteLength && length > SIZE_MAX / 2)
521         return CborErrorDataTooLarge;
522     return create_container(encoder, mapEncoder, length, MapType << MajorTypeShift);
523 }
524 
525 /**
526  * Creates a indefinite-length text string in the CBOR stream provided by
527  * \a encoder and initializes \a stringEncoder so that chunks of original string
528  * can be added using the CborEncoder functions. The string must be terminated by
529  * calling cbor_encoder_close_container() with the same \a encoder and
530  * \a stringEncoder parameters.
531  *
532  * \sa cbor_encoder_create_array
533  */
cbor_encoder_create_indef_text_string(CborEncoder * encoder,CborEncoder * stringEncoder)534 CborError cbor_encoder_create_indef_text_string(CborEncoder *encoder, CborEncoder *stringEncoder)
535 {
536     return create_container(encoder, stringEncoder, CborIndefiniteLength, TextStringType << MajorTypeShift);
537 }
538 
539 /**
540  * Creates a indefinite-length byte string in the CBOR stream provided by
541  * \a encoder and initializes \a stringEncoder so that chunks of original string
542  * can be added using the CborEncoder functions. The string must be terminated by
543  * calling cbor_encoder_close_container() with the same \a encoder and
544  * \a stringEncoder parameters.
545  *
546  * \sa cbor_encoder_create_array
547  */
cbor_encoder_create_indef_byte_string(CborEncoder * encoder,CborEncoder * stringEncoder)548 CborError cbor_encoder_create_indef_byte_string(CborEncoder *encoder, CborEncoder *stringEncoder)
549 {
550     return create_container(encoder, stringEncoder, CborIndefiniteLength, ByteStringType << MajorTypeShift);
551 }
552 
553 /**
554  * Closes the CBOR container (array, map or indefinite-length string) provided
555  * by \a containerEncoder and updates the CBOR stream provided by \a encoder.
556  * Both parameters must be the same as were passed to cbor_encoder_create_array() or
557  * cbor_encoder_create_map() or cbor_encoder_create_indef_byte_string().
558  *
559  * This function does not verify that the number of items (or pair of items, in
560  * the case of a map) was correct. To execute that verification, call
561  * cbor_encoder_close_container_checked() instead.
562  *
563  * \sa cbor_encoder_create_array(), cbor_encoder_create_map()
564  */
cbor_encoder_close_container(CborEncoder * encoder,const CborEncoder * containerEncoder)565 CborError cbor_encoder_close_container(CborEncoder *encoder, const CborEncoder *containerEncoder)
566 {
567     encoder->writer = containerEncoder->writer;
568 
569     if (containerEncoder->flags & CborIteratorFlag_UnknownLength)
570         return append_byte_to_buffer(encoder, BreakByte);
571     return CborNoError;
572 }
573 
574 /**
575  * \fn CborError cbor_encode_boolean(CborEncoder *encoder, bool value)
576  *
577  * Appends the boolean value \a value to the CBOR stream provided by \a encoder.
578  */
579 
580 /**
581  * \fn CborError cbor_encode_null(CborEncoder *encoder)
582  *
583  * Appends the CBOR type representing a null value to the CBOR stream provided
584  * by \a encoder.
585  *
586  * \sa cbor_encode_undefined()
587  */
588 
589 /**
590  * \fn CborError cbor_encode_undefined(CborEncoder *encoder)
591  *
592  * Appends the CBOR type representing an undefined value to the CBOR stream
593  * provided by \a encoder.
594  *
595  * \sa cbor_encode_null()
596  */
597 
598 /**
599  * \fn CborError cbor_encode_half_float(CborEncoder *encoder, const void *value)
600  *
601  * Appends the IEEE 754 half-precision (16-bit) floating point value pointed to
602  * by \a value to the CBOR stream provided by \a encoder.
603  *
604  * \sa cbor_encode_floating_point(), cbor_encode_float(), cbor_encode_double()
605  */
606 
607 /**
608  * \fn CborError cbor_encode_float(CborEncoder *encoder, float value)
609  *
610  * Appends the IEEE 754 single-precision (32-bit) floating point value \a value
611  * to the CBOR stream provided by \a encoder.
612  *
613  * \sa cbor_encode_floating_point(), cbor_encode_half_float(), cbor_encode_double()
614  */
615 
616 /**
617  * \fn CborError cbor_encode_double(CborEncoder *encoder, double value)
618  *
619  * Appends the IEEE 754 double-precision (64-bit) floating point value \a value
620  * to the CBOR stream provided by \a encoder.
621  *
622  * \sa cbor_encode_floating_point(), cbor_encode_half_float(), cbor_encode_float()
623  */
624 
625 /**
626  * \fn size_t cbor_encoder_get_buffer_size(const CborEncoder *encoder, const uint8_t *buffer)
627  *
628  * Returns the total size of the buffer starting at \a buffer after the
629  * encoding finished without errors. The \a encoder and \a buffer arguments
630  * must be the same as supplied to cbor_encoder_init().
631  *
632  * If the encoding process had errors, the return value of this function is
633  * meaningless. If the only errors were CborErrorOutOfMemory, instead use
634  * cbor_encoder_get_extra_bytes_needed() to find out by how much to grow the
635  * buffer before encoding again.
636  *
637  * See \ref CborEncoding for an example of using this function.
638  *
639  * \sa cbor_encoder_init(), cbor_encoder_get_extra_bytes_needed(), CborEncoding
640  */
641 
642 /**
643  * \fn size_t cbor_encoder_get_extra_bytes_needed(const CborEncoder *encoder)
644  *
645  * Returns how many more bytes the original buffer supplied to
646  * cbor_encoder_init() needs to be extended by so that no CborErrorOutOfMemory
647  * condition will happen for the encoding. If the buffer was big enough, this
648  * function returns 0. The \a encoder must be the original argument as passed
649  * to cbor_encoder_init().
650  *
651  * This function is usually called after an encoding sequence ended with one or
652  * more CborErrorOutOfMemory errors, but no other error. If any other error
653  * happened, the return value of this function is meaningless.
654  *
655  * See \ref CborEncoding for an example of using this function.
656  *
657  * \sa cbor_encoder_init(), cbor_encoder_get_buffer_size(), CborEncoding
658  */
659 
660 /** @} */
661