Thiago Macieira | f1cadf0 | 2015-05-08 17:19:17 -0700 | [diff] [blame] | 1 | /**************************************************************************** |
| 2 | ** |
Thiago Macieira | 46a818e | 2015-10-08 15:13:05 +0200 | [diff] [blame] | 3 | ** Copyright (C) 2016 Intel Corporation |
Thiago Macieira | f1cadf0 | 2015-05-08 17:19:17 -0700 | [diff] [blame] | 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 | |
Vipul Rahane | 650c31f | 2017-12-15 12:48:40 -0800 | [diff] [blame] | 25 | #ifndef _BSD_SOURCE |
Thiago Macieira | ed5b57c | 2015-07-07 16:38:27 -0700 | [diff] [blame] | 26 | #define _BSD_SOURCE 1 |
Vipul Rahane | 650c31f | 2017-12-15 12:48:40 -0800 | [diff] [blame] | 27 | #endif |
| 28 | #ifndef _DEFAULT_SOURCE |
Otavio Pontes | e2d5dd5 | 2016-07-08 09:49:38 -0300 | [diff] [blame] | 29 | #define _DEFAULT_SOURCE 1 |
Vipul Rahane | 650c31f | 2017-12-15 12:48:40 -0800 | [diff] [blame] | 30 | #endif |
Thiago Macieira | 86c8186 | 2016-08-04 13:56:48 -0700 | [diff] [blame] | 31 | #ifndef __STDC_LIMIT_MACROS |
| 32 | # define __STDC_LIMIT_MACROS 1 |
| 33 | #endif |
| 34 | |
Thiago Macieira | f1cadf0 | 2015-05-08 17:19:17 -0700 | [diff] [blame] | 35 | #include "cbor.h" |
Thiago Macieira | cf3116e | 2017-03-04 23:36:23 -0600 | [diff] [blame] | 36 | #include "cborinternal_p.h" |
Thiago Macieira | f1cadf0 | 2015-05-08 17:19:17 -0700 | [diff] [blame] | 37 | #include "compilersupport_p.h" |
| 38 | |
Thiago Macieira | f1cadf0 | 2015-05-08 17:19:17 -0700 | [diff] [blame] | 39 | #include <stdlib.h> |
| 40 | #include <string.h> |
| 41 | |
Thiago Macieira | 46a818e | 2015-10-08 15:13:05 +0200 | [diff] [blame] | 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; |
Sergio Martins | d2dd95c | 2019-06-05 21:55:45 +0100 | [diff] [blame] | 68 | * cbor_encoder_init(&encoder, buf, sizeof(buf), 0); |
Thiago Macieira | 46a818e | 2015-10-08 15:13:05 +0200 | [diff] [blame] | 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; |
Pedro Oliveira | d75f2eb | 2019-01-11 00:35:29 +0000 | [diff] [blame] | 79 | * cbor_encoder_init(&encoder, buf, sizeof(buf), 0); |
Thiago Macieira | 46a818e | 2015-10-08 15:13:05 +0200 | [diff] [blame] | 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 | * |
Thiago Macieira | f5a172b | 2018-02-05 14:53:14 -0800 | [diff] [blame] | 86 | * <h3 class="groupheader">Error checking and buffer size</h3> |
Thiago Macieira | 46a818e | 2015-10-08 15:13:05 +0200 | [diff] [blame] | 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 |
Thiago Macieira | 7ae36a7 | 2015-10-28 16:19:23 -0700 | [diff] [blame] | 101 | * complete the encoding. At the end, you can obtain that count by calling |
| 102 | * cbor_encoder_get_extra_bytes_needed(). |
Thiago Macieira | 46a818e | 2015-10-08 15:13:05 +0200 | [diff] [blame] | 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 |
Thiago Macieira | 7ae36a7 | 2015-10-28 16:19:23 -0700 | [diff] [blame] | 108 | * immediately used. To obtain the size of the buffer, call |
| 109 | * cbor_encoder_get_buffer_size() with the original buffer pointer. |
Thiago Macieira | 46a818e | 2015-10-08 15:13:05 +0200 | [diff] [blame] | 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; |
Pedro Oliveira | d75f2eb | 2019-01-11 00:35:29 +0000 | [diff] [blame] | 118 | * cbor_encoder_init(&encoder, buf, sizeof(buf), 0); |
Thiago Macieira | 46a818e | 2015-10-08 15:13:05 +0200 | [diff] [blame] | 119 | * err = cbor_encoder_create_map(&encoder, &mapEncoder, 1); |
elie-elkhoury | 1cc264a | 2019-10-09 11:31:45 +0100 | [diff] [blame] | 120 | * if (err) |
Thiago Macieira | 46a818e | 2015-10-08 15:13:05 +0200 | [diff] [blame] | 121 | * return err; |
| 122 | * err = cbor_encode_text_stringz(&mapEncoder, "foo"); |
elie-elkhoury | 1cc264a | 2019-10-09 11:31:45 +0100 | [diff] [blame] | 123 | * if (err) |
Thiago Macieira | 46a818e | 2015-10-08 15:13:05 +0200 | [diff] [blame] | 124 | * return err; |
| 125 | * err = cbor_encode_boolean(&mapEncoder, some_value); |
elie-elkhoury | 1cc264a | 2019-10-09 11:31:45 +0100 | [diff] [blame] | 126 | * if (err) |
Thiago Macieira | 46a818e | 2015-10-08 15:13:05 +0200 | [diff] [blame] | 127 | * return err; |
| 128 | * err = cbor_encoder_close_container_checked(&encoder, &mapEncoder); |
elie-elkhoury | 1cc264a | 2019-10-09 11:31:45 +0100 | [diff] [blame] | 129 | * if (err) |
Thiago Macieira | 46a818e | 2015-10-08 15:13:05 +0200 | [diff] [blame] | 130 | * return err; |
| 131 | * |
Thiago Macieira | 7ae36a7 | 2015-10-28 16:19:23 -0700 | [diff] [blame] | 132 | * size_t len = cbor_encoder_get_buffer_size(&encoder, buf); |
Thiago Macieira | 46a818e | 2015-10-08 15:13:05 +0200 | [diff] [blame] | 133 | * send_payload(buf, len); |
| 134 | * return CborNoError; |
| 135 | * \endcode |
Thiago Macieira | 7ae36a7 | 2015-10-28 16:19:23 -0700 | [diff] [blame] | 136 | * |
Thiago Macieira | f5a172b | 2018-02-05 14:53:14 -0800 | [diff] [blame] | 137 | * Finally, the example below expands on the one above and also |
Thiago Macieira | 7ae36a7 | 2015-10-28 16:19:23 -0700 | [diff] [blame] | 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 |
Thiago Macieira | da8e35e | 2017-03-05 09:58:01 +0100 | [diff] [blame] | 140 | * an cbor_assertion, showing where the author assumes no error can occur. |
Thiago Macieira | 7ae36a7 | 2015-10-28 16:19:23 -0700 | [diff] [blame] | 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 | * |
Pedro Oliveira | d75f2eb | 2019-01-11 00:35:29 +0000 | [diff] [blame] | 158 | * cbor_encoder_init(&encoder, buf, size, 0); |
Thiago Macieira | 7ae36a7 | 2015-10-28 16:19:23 -0700 | [diff] [blame] | 159 | * err = cbor_encoder_create_array(&encoder, &arrayEncoder, n); |
elie-elkhoury | 1cc264a | 2019-10-09 11:31:45 +0100 | [diff] [blame] | 160 | * cbor_assert(!err); // can't fail, the buffer is always big enough |
Thiago Macieira | 7ae36a7 | 2015-10-28 16:19:23 -0700 | [diff] [blame] | 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); |
elie-elkhoury | 1cc264a | 2019-10-09 11:31:45 +0100 | [diff] [blame] | 169 | * cbor_assert(!err); // shouldn't fail! |
Thiago Macieira | 7ae36a7 | 2015-10-28 16:19:23 -0700 | [diff] [blame] | 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 |
Thiago Macieira | 46a818e | 2015-10-08 15:13:05 +0200 | [diff] [blame] | 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 | */ |
Thiago Macieira | 5752ce5 | 2015-06-16 12:10:03 -0700 | [diff] [blame] | 203 | void cbor_encoder_init(CborEncoder *encoder, uint8_t *buffer, size_t size, int flags) |
Thiago Macieira | f1cadf0 | 2015-05-08 17:19:17 -0700 | [diff] [blame] | 204 | { |
Thiago Macieira | ede7f14 | 2016-07-30 23:08:39 -0700 | [diff] [blame] | 205 | encoder->data.ptr = buffer; |
Thiago Macieira | f1cadf0 | 2015-05-08 17:19:17 -0700 | [diff] [blame] | 206 | encoder->end = buffer + size; |
Thiago Macieira | cc2bfbb | 2017-12-17 23:38:37 -0800 | [diff] [blame] | 207 | encoder->remaining = 2; |
Thiago Macieira | f1cadf0 | 2015-05-08 17:19:17 -0700 | [diff] [blame] | 208 | encoder->flags = flags; |
| 209 | } |
| 210 | |
Thiago Macieira | 5752ce5 | 2015-06-16 12:10:03 -0700 | [diff] [blame] | 211 | static inline void put16(void *where, uint16_t v) |
Thiago Macieira | f1cadf0 | 2015-05-08 17:19:17 -0700 | [diff] [blame] | 212 | { |
Alexander Richardson | 71ce609 | 2020-07-27 16:40:33 +0100 | [diff] [blame^] | 213 | uint16_t v_be = cbor_htons(v); |
| 214 | memcpy(where, &v_be, sizeof(v_be)); |
Thiago Macieira | f1cadf0 | 2015-05-08 17:19:17 -0700 | [diff] [blame] | 215 | } |
| 216 | |
Thiago Macieira | dbc0129 | 2016-06-06 17:02:25 -0700 | [diff] [blame] | 217 | /* Note: Since this is currently only used in situations where OOM is the only |
| 218 | * valid error, we KNOW this to be true. Thus, this function now returns just 'true', |
| 219 | * but if in the future, any function starts returning a non-OOM error, this will need |
| 220 | * to be changed to the test. At the moment, this is done to prevent more branches |
| 221 | * being created in the tinycbor output */ |
Erich Keane | 47a7856 | 2015-08-10 15:19:50 -0700 | [diff] [blame] | 222 | static inline bool isOomError(CborError err) |
| 223 | { |
| 224 | (void) err; |
| 225 | return true; |
| 226 | } |
| 227 | |
Thiago Macieira | 5752ce5 | 2015-06-16 12:10:03 -0700 | [diff] [blame] | 228 | static inline void put32(void *where, uint32_t v) |
Thiago Macieira | f1cadf0 | 2015-05-08 17:19:17 -0700 | [diff] [blame] | 229 | { |
Alexander Richardson | 71ce609 | 2020-07-27 16:40:33 +0100 | [diff] [blame^] | 230 | uint32_t v_be = cbor_htonl(v); |
| 231 | memcpy(where, &v_be, sizeof(v_be)); |
Thiago Macieira | f1cadf0 | 2015-05-08 17:19:17 -0700 | [diff] [blame] | 232 | } |
| 233 | |
Thiago Macieira | 5752ce5 | 2015-06-16 12:10:03 -0700 | [diff] [blame] | 234 | static inline void put64(void *where, uint64_t v) |
Thiago Macieira | f1cadf0 | 2015-05-08 17:19:17 -0700 | [diff] [blame] | 235 | { |
Alexander Richardson | 71ce609 | 2020-07-27 16:40:33 +0100 | [diff] [blame^] | 236 | uint64_t v_be = cbor_htonll(v); |
| 237 | memcpy(where, &v_be, sizeof(v_be)); |
Thiago Macieira | f1cadf0 | 2015-05-08 17:19:17 -0700 | [diff] [blame] | 238 | } |
| 239 | |
Thiago Macieira | 397f979 | 2015-09-18 19:36:26 -0700 | [diff] [blame] | 240 | static inline bool would_overflow(CborEncoder *encoder, size_t len) |
| 241 | { |
| 242 | ptrdiff_t remaining = (ptrdiff_t)encoder->end; |
Thiago Macieira | ede7f14 | 2016-07-30 23:08:39 -0700 | [diff] [blame] | 243 | remaining -= remaining ? (ptrdiff_t)encoder->data.ptr : encoder->data.bytes_needed; |
Thiago Macieira | 397f979 | 2015-09-18 19:36:26 -0700 | [diff] [blame] | 244 | remaining -= (ptrdiff_t)len; |
| 245 | return unlikely(remaining < 0); |
| 246 | } |
| 247 | |
| 248 | static inline void advance_ptr(CborEncoder *encoder, size_t n) |
| 249 | { |
| 250 | if (encoder->end) |
Thiago Macieira | ede7f14 | 2016-07-30 23:08:39 -0700 | [diff] [blame] | 251 | encoder->data.ptr += n; |
Thiago Macieira | 397f979 | 2015-09-18 19:36:26 -0700 | [diff] [blame] | 252 | else |
Thiago Macieira | ede7f14 | 2016-07-30 23:08:39 -0700 | [diff] [blame] | 253 | encoder->data.bytes_needed += n; |
Thiago Macieira | 397f979 | 2015-09-18 19:36:26 -0700 | [diff] [blame] | 254 | } |
| 255 | |
Thiago Macieira | 5752ce5 | 2015-06-16 12:10:03 -0700 | [diff] [blame] | 256 | static inline CborError append_to_buffer(CborEncoder *encoder, const void *data, size_t len) |
Thiago Macieira | f1cadf0 | 2015-05-08 17:19:17 -0700 | [diff] [blame] | 257 | { |
Thiago Macieira | 397f979 | 2015-09-18 19:36:26 -0700 | [diff] [blame] | 258 | if (would_overflow(encoder, len)) { |
Erich Keane | 47a7856 | 2015-08-10 15:19:50 -0700 | [diff] [blame] | 259 | if (encoder->end != NULL) { |
Thiago Macieira | ede7f14 | 2016-07-30 23:08:39 -0700 | [diff] [blame] | 260 | len -= encoder->end - encoder->data.ptr; |
Thiago Macieira | 397f979 | 2015-09-18 19:36:26 -0700 | [diff] [blame] | 261 | encoder->end = NULL; |
Thiago Macieira | ede7f14 | 2016-07-30 23:08:39 -0700 | [diff] [blame] | 262 | encoder->data.bytes_needed = 0; |
Erich Keane | 47a7856 | 2015-08-10 15:19:50 -0700 | [diff] [blame] | 263 | } |
| 264 | |
Thiago Macieira | 397f979 | 2015-09-18 19:36:26 -0700 | [diff] [blame] | 265 | advance_ptr(encoder, len); |
Thiago Macieira | f1cadf0 | 2015-05-08 17:19:17 -0700 | [diff] [blame] | 266 | return CborErrorOutOfMemory; |
Erich Keane | 47a7856 | 2015-08-10 15:19:50 -0700 | [diff] [blame] | 267 | } |
| 268 | |
Thiago Macieira | ede7f14 | 2016-07-30 23:08:39 -0700 | [diff] [blame] | 269 | memcpy(encoder->data.ptr, data, len); |
| 270 | encoder->data.ptr += len; |
Thiago Macieira | f1cadf0 | 2015-05-08 17:19:17 -0700 | [diff] [blame] | 271 | return CborNoError; |
| 272 | } |
| 273 | |
Thiago Macieira | 5752ce5 | 2015-06-16 12:10:03 -0700 | [diff] [blame] | 274 | static inline CborError append_byte_to_buffer(CborEncoder *encoder, uint8_t byte) |
Thiago Macieira | 8f7bd9e | 2015-05-12 13:37:46 +0900 | [diff] [blame] | 275 | { |
Thiago Macieira | 618d2f1 | 2015-09-22 09:14:29 -0700 | [diff] [blame] | 276 | return append_to_buffer(encoder, &byte, 1); |
Thiago Macieira | 8f7bd9e | 2015-05-12 13:37:46 +0900 | [diff] [blame] | 277 | } |
| 278 | |
Thiago Macieira | 4e9626c | 2015-09-21 14:57:17 -0700 | [diff] [blame] | 279 | static inline CborError encode_number_no_update(CborEncoder *encoder, uint64_t ui, uint8_t shiftedMajorType) |
Thiago Macieira | f1cadf0 | 2015-05-08 17:19:17 -0700 | [diff] [blame] | 280 | { |
Thiago Macieira | afa4ff6 | 2015-05-09 10:09:55 -0700 | [diff] [blame] | 281 | /* Little-endian would have been so much more convenient here: |
| 282 | * We could just write at the beginning of buf but append_to_buffer |
| 283 | * only the necessary bytes. |
| 284 | * Since it has to be big endian, do it the other way around: |
| 285 | * write from the end. */ |
Thiago Macieira | 510e5b8 | 2015-09-21 16:05:02 -0700 | [diff] [blame] | 286 | uint64_t buf[2]; |
| 287 | uint8_t *const bufend = (uint8_t *)buf + sizeof(buf); |
Thiago Macieira | 5752ce5 | 2015-06-16 12:10:03 -0700 | [diff] [blame] | 288 | uint8_t *bufstart = bufend - 1; |
Thiago Macieira | dbc0129 | 2016-06-06 17:02:25 -0700 | [diff] [blame] | 289 | put64(buf + 1, ui); /* we probably have a bunch of zeros in the beginning */ |
Thiago Macieira | f1cadf0 | 2015-05-08 17:19:17 -0700 | [diff] [blame] | 290 | |
| 291 | if (ui < Value8Bit) { |
Thiago Macieira | afa4ff6 | 2015-05-09 10:09:55 -0700 | [diff] [blame] | 292 | *bufstart += shiftedMajorType; |
Thiago Macieira | f1cadf0 | 2015-05-08 17:19:17 -0700 | [diff] [blame] | 293 | } else { |
alradmsft | 9ba4791 | 2016-10-11 17:56:15 -0700 | [diff] [blame] | 294 | uint8_t more = 0; |
Thiago Macieira | 13c8579 | 2015-07-07 16:15:41 -0700 | [diff] [blame] | 295 | if (ui > 0xffU) |
Thiago Macieira | afa4ff6 | 2015-05-09 10:09:55 -0700 | [diff] [blame] | 296 | ++more; |
Thiago Macieira | 13c8579 | 2015-07-07 16:15:41 -0700 | [diff] [blame] | 297 | if (ui > 0xffffU) |
Thiago Macieira | afa4ff6 | 2015-05-09 10:09:55 -0700 | [diff] [blame] | 298 | ++more; |
Thiago Macieira | 13c8579 | 2015-07-07 16:15:41 -0700 | [diff] [blame] | 299 | if (ui > 0xffffffffU) |
Thiago Macieira | afa4ff6 | 2015-05-09 10:09:55 -0700 | [diff] [blame] | 300 | ++more; |
Thiago Macieira | e12dfd0 | 2016-06-07 16:29:25 -0700 | [diff] [blame] | 301 | bufstart -= (size_t)1 << more; |
Thiago Macieira | afa4ff6 | 2015-05-09 10:09:55 -0700 | [diff] [blame] | 302 | *bufstart = shiftedMajorType + Value8Bit + more; |
Thiago Macieira | f1cadf0 | 2015-05-08 17:19:17 -0700 | [diff] [blame] | 303 | } |
| 304 | |
Thiago Macieira | afa4ff6 | 2015-05-09 10:09:55 -0700 | [diff] [blame] | 305 | return append_to_buffer(encoder, bufstart, bufend - bufstart); |
Thiago Macieira | f1cadf0 | 2015-05-08 17:19:17 -0700 | [diff] [blame] | 306 | } |
| 307 | |
Thiago Macieira | cc2bfbb | 2017-12-17 23:38:37 -0800 | [diff] [blame] | 308 | static inline void saturated_decrement(CborEncoder *encoder) |
| 309 | { |
| 310 | if (encoder->remaining) |
| 311 | --encoder->remaining; |
| 312 | } |
| 313 | |
Thiago Macieira | 4e9626c | 2015-09-21 14:57:17 -0700 | [diff] [blame] | 314 | static inline CborError encode_number(CborEncoder *encoder, uint64_t ui, uint8_t shiftedMajorType) |
| 315 | { |
Thiago Macieira | cc2bfbb | 2017-12-17 23:38:37 -0800 | [diff] [blame] | 316 | saturated_decrement(encoder); |
Thiago Macieira | 4e9626c | 2015-09-21 14:57:17 -0700 | [diff] [blame] | 317 | return encode_number_no_update(encoder, ui, shiftedMajorType); |
| 318 | } |
| 319 | |
Thiago Macieira | 46a818e | 2015-10-08 15:13:05 +0200 | [diff] [blame] | 320 | /** |
| 321 | * Appends the unsigned 64-bit integer \a value to the CBOR stream provided by |
| 322 | * \a encoder. |
| 323 | * |
| 324 | * \sa cbor_encode_negative_int, cbor_encode_int |
| 325 | */ |
Thiago Macieira | f1cadf0 | 2015-05-08 17:19:17 -0700 | [diff] [blame] | 326 | CborError cbor_encode_uint(CborEncoder *encoder, uint64_t value) |
| 327 | { |
| 328 | return encode_number(encoder, value, UnsignedIntegerType << MajorTypeShift); |
| 329 | } |
| 330 | |
Thiago Macieira | 46a818e | 2015-10-08 15:13:05 +0200 | [diff] [blame] | 331 | /** |
| 332 | * Appends the negative 64-bit integer whose absolute value is \a |
| 333 | * absolute_value to the CBOR stream provided by \a encoder. |
| 334 | * |
Thiago Macieira | f5a172b | 2018-02-05 14:53:14 -0800 | [diff] [blame] | 335 | * If the value \a absolute_value is zero, this function encodes -2^64. |
Thiago Macieira | 497066e | 2018-01-11 19:37:09 -0800 | [diff] [blame] | 336 | * |
Thiago Macieira | 46a818e | 2015-10-08 15:13:05 +0200 | [diff] [blame] | 337 | * \sa cbor_encode_uint, cbor_encode_int |
| 338 | */ |
Thiago Macieira | 78632b3 | 2015-09-26 14:18:48 -0700 | [diff] [blame] | 339 | CborError cbor_encode_negative_int(CborEncoder *encoder, uint64_t absolute_value) |
| 340 | { |
Thiago Macieira | 497066e | 2018-01-11 19:37:09 -0800 | [diff] [blame] | 341 | return encode_number(encoder, absolute_value - 1, NegativeIntegerType << MajorTypeShift); |
Thiago Macieira | 78632b3 | 2015-09-26 14:18:48 -0700 | [diff] [blame] | 342 | } |
| 343 | |
Thiago Macieira | 46a818e | 2015-10-08 15:13:05 +0200 | [diff] [blame] | 344 | /** |
| 345 | * Appends the signed 64-bit integer \a value to the CBOR stream provided by |
| 346 | * \a encoder. |
| 347 | * |
| 348 | * \sa cbor_encode_negative_int, cbor_encode_uint |
| 349 | */ |
Thiago Macieira | f1cadf0 | 2015-05-08 17:19:17 -0700 | [diff] [blame] | 350 | CborError cbor_encode_int(CborEncoder *encoder, int64_t value) |
| 351 | { |
Thiago Macieira | dbc0129 | 2016-06-06 17:02:25 -0700 | [diff] [blame] | 352 | /* adapted from code in RFC 7049 appendix C (pseudocode) */ |
| 353 | uint64_t ui = value >> 63; /* extend sign to whole length */ |
| 354 | uint8_t majorType = ui & 0x20; /* extract major type */ |
| 355 | ui ^= value; /* complement negatives */ |
Thiago Macieira | f1cadf0 | 2015-05-08 17:19:17 -0700 | [diff] [blame] | 356 | return encode_number(encoder, ui, majorType); |
| 357 | } |
| 358 | |
Thiago Macieira | 46a818e | 2015-10-08 15:13:05 +0200 | [diff] [blame] | 359 | /** |
| 360 | * Appends the CBOR Simple Type of value \a value to the CBOR stream provided by |
| 361 | * \a encoder. |
| 362 | * |
| 363 | * This function may return error CborErrorIllegalSimpleType if the \a value |
| 364 | * variable contains a number that is not a valid simple type. |
| 365 | */ |
Thiago Macieira | f1cadf0 | 2015-05-08 17:19:17 -0700 | [diff] [blame] | 366 | CborError cbor_encode_simple_value(CborEncoder *encoder, uint8_t value) |
| 367 | { |
| 368 | #ifndef CBOR_ENCODER_NO_CHECK_USER |
Thiago Macieira | dbc0129 | 2016-06-06 17:02:25 -0700 | [diff] [blame] | 369 | /* check if this is a valid simple type */ |
Thiago Macieira | f1cadf0 | 2015-05-08 17:19:17 -0700 | [diff] [blame] | 370 | if (value >= HalfPrecisionFloat && value <= Break) |
| 371 | return CborErrorIllegalSimpleType; |
| 372 | #endif |
| 373 | return encode_number(encoder, value, SimpleTypesType << MajorTypeShift); |
| 374 | } |
| 375 | |
Thiago Macieira | 46a818e | 2015-10-08 15:13:05 +0200 | [diff] [blame] | 376 | /** |
| 377 | * Appends the floating-point value of type \a fpType and pointed to by \a |
| 378 | * value to the CBOR stream provided by \a encoder. The value of \a fpType must |
| 379 | * be one of CborHalfFloatType, CborFloatType or CborDoubleType, otherwise the |
| 380 | * behavior of this function is undefined. |
| 381 | * |
| 382 | * This function is useful for code that needs to pass through floating point |
| 383 | * values but does not wish to have the actual floating-point code. |
| 384 | * |
| 385 | * \sa cbor_encode_half_float, cbor_encode_float, cbor_encode_double |
| 386 | */ |
Thiago Macieira | 81f3343 | 2015-07-02 16:16:28 -0700 | [diff] [blame] | 387 | CborError cbor_encode_floating_point(CborEncoder *encoder, CborType fpType, const void *value) |
Thiago Macieira | f1cadf0 | 2015-05-08 17:19:17 -0700 | [diff] [blame] | 388 | { |
Thiago Macieira | d260349 | 2018-07-08 10:57:22 -0700 | [diff] [blame] | 389 | unsigned size; |
Thiago Macieira | 81f3343 | 2015-07-02 16:16:28 -0700 | [diff] [blame] | 390 | uint8_t buf[1 + sizeof(uint64_t)]; |
Thiago Macieira | da8e35e | 2017-03-05 09:58:01 +0100 | [diff] [blame] | 391 | cbor_assert(fpType == CborHalfFloatType || fpType == CborFloatType || fpType == CborDoubleType); |
Thiago Macieira | 81f3343 | 2015-07-02 16:16:28 -0700 | [diff] [blame] | 392 | buf[0] = fpType; |
Thiago Macieira | f1cadf0 | 2015-05-08 17:19:17 -0700 | [diff] [blame] | 393 | |
Thiago Macieira | d260349 | 2018-07-08 10:57:22 -0700 | [diff] [blame] | 394 | size = 2U << (fpType - CborHalfFloatType); |
Thiago Macieira | 81f3343 | 2015-07-02 16:16:28 -0700 | [diff] [blame] | 395 | if (size == 8) |
| 396 | put64(buf + 1, *(const uint64_t*)value); |
| 397 | else if (size == 4) |
| 398 | put32(buf + 1, *(const uint32_t*)value); |
| 399 | else |
| 400 | put16(buf + 1, *(const uint16_t*)value); |
Thiago Macieira | cc2bfbb | 2017-12-17 23:38:37 -0800 | [diff] [blame] | 401 | saturated_decrement(encoder); |
Thiago Macieira | 81f3343 | 2015-07-02 16:16:28 -0700 | [diff] [blame] | 402 | return append_to_buffer(encoder, buf, size + 1); |
Thiago Macieira | f1cadf0 | 2015-05-08 17:19:17 -0700 | [diff] [blame] | 403 | } |
Thiago Macieira | 8f98a11 | 2015-05-08 17:25:29 -0700 | [diff] [blame] | 404 | |
Thiago Macieira | 46a818e | 2015-10-08 15:13:05 +0200 | [diff] [blame] | 405 | /** |
| 406 | * Appends the CBOR tag \a tag to the CBOR stream provided by \a encoder. |
| 407 | * |
| 408 | * \sa CborTag |
| 409 | */ |
Thiago Macieira | 8f98a11 | 2015-05-08 17:25:29 -0700 | [diff] [blame] | 410 | CborError cbor_encode_tag(CborEncoder *encoder, CborTag tag) |
| 411 | { |
Thiago Macieira | dbc0129 | 2016-06-06 17:02:25 -0700 | [diff] [blame] | 412 | /* tags don't count towards the number of elements in an array or map */ |
Thiago Macieira | 4e9626c | 2015-09-21 14:57:17 -0700 | [diff] [blame] | 413 | return encode_number_no_update(encoder, tag, TagType << MajorTypeShift); |
Thiago Macieira | 8f98a11 | 2015-05-08 17:25:29 -0700 | [diff] [blame] | 414 | } |
Thiago Macieira | b54debe | 2015-05-08 17:42:39 -0700 | [diff] [blame] | 415 | |
Thiago Macieira | 5752ce5 | 2015-06-16 12:10:03 -0700 | [diff] [blame] | 416 | static CborError encode_string(CborEncoder *encoder, size_t length, uint8_t shiftedMajorType, const void *string) |
Thiago Macieira | b54debe | 2015-05-08 17:42:39 -0700 | [diff] [blame] | 417 | { |
| 418 | CborError err = encode_number(encoder, length, shiftedMajorType); |
Erich Keane | 47a7856 | 2015-08-10 15:19:50 -0700 | [diff] [blame] | 419 | if (err && !isOomError(err)) |
Thiago Macieira | b54debe | 2015-05-08 17:42:39 -0700 | [diff] [blame] | 420 | return err; |
| 421 | return append_to_buffer(encoder, string, length); |
| 422 | } |
| 423 | |
Thiago Macieira | 46a818e | 2015-10-08 15:13:05 +0200 | [diff] [blame] | 424 | /** |
| 425 | * \fn CborError cbor_encode_text_stringz(CborEncoder *encoder, const char *string) |
| 426 | * |
| 427 | * Appends the null-terminated text string \a string to the CBOR stream |
| 428 | * provided by \a encoder. CBOR requires that \a string be valid UTF-8, but |
| 429 | * TinyCBOR makes no verification of correctness. The terminating null is not |
| 430 | * included in the stream. |
| 431 | * |
| 432 | * \sa cbor_encode_text_string, cbor_encode_byte_string |
| 433 | */ |
| 434 | |
| 435 | /** |
| 436 | * Appends the text string \a string of length \a length to the CBOR stream |
| 437 | * provided by \a encoder. CBOR requires that \a string be valid UTF-8, but |
| 438 | * TinyCBOR makes no verification of correctness. |
| 439 | * |
| 440 | * \sa CborError cbor_encode_text_stringz, cbor_encode_byte_string |
| 441 | */ |
Thiago Macieira | 5752ce5 | 2015-06-16 12:10:03 -0700 | [diff] [blame] | 442 | CborError cbor_encode_byte_string(CborEncoder *encoder, const uint8_t *string, size_t length) |
Thiago Macieira | b54debe | 2015-05-08 17:42:39 -0700 | [diff] [blame] | 443 | { |
| 444 | return encode_string(encoder, length, ByteStringType << MajorTypeShift, string); |
| 445 | } |
| 446 | |
Thiago Macieira | 46a818e | 2015-10-08 15:13:05 +0200 | [diff] [blame] | 447 | /** |
| 448 | * Appends the byte string \a string of length \a length to the CBOR stream |
| 449 | * provided by \a encoder. CBOR byte strings are arbitrary raw data. |
| 450 | * |
| 451 | * \sa cbor_encode_text_stringz, cbor_encode_text_string |
| 452 | */ |
Thiago Macieira | b54debe | 2015-05-08 17:42:39 -0700 | [diff] [blame] | 453 | CborError cbor_encode_text_string(CborEncoder *encoder, const char *string, size_t length) |
| 454 | { |
| 455 | return encode_string(encoder, length, TextStringType << MajorTypeShift, string); |
| 456 | } |
Thiago Macieira | 355817e | 2015-05-08 18:38:18 -0700 | [diff] [blame] | 457 | |
Thiago Macieira | 5663524 | 2015-09-20 17:44:51 -0700 | [diff] [blame] | 458 | #ifdef __GNUC__ |
| 459 | __attribute__((noinline)) |
| 460 | #endif |
| 461 | static CborError create_container(CborEncoder *encoder, CborEncoder *container, size_t length, uint8_t shiftedMajorType) |
Thiago Macieira | 355817e | 2015-05-08 18:38:18 -0700 | [diff] [blame] | 462 | { |
Thiago Macieira | 8f7bd9e | 2015-05-12 13:37:46 +0900 | [diff] [blame] | 463 | CborError err; |
Thiago Macieira | ede7f14 | 2016-07-30 23:08:39 -0700 | [diff] [blame] | 464 | container->data.ptr = encoder->data.ptr; |
Thiago Macieira | 4e9626c | 2015-09-21 14:57:17 -0700 | [diff] [blame] | 465 | container->end = encoder->end; |
Thiago Macieira | cc2bfbb | 2017-12-17 23:38:37 -0800 | [diff] [blame] | 466 | saturated_decrement(encoder); |
| 467 | container->remaining = length + 1; /* overflow ok on CborIndefiniteLength */ |
Thiago Macieira | 4e9626c | 2015-09-21 14:57:17 -0700 | [diff] [blame] | 468 | |
| 469 | cbor_static_assert(((MapType << MajorTypeShift) & CborIteratorFlag_ContainerIsMap) == CborIteratorFlag_ContainerIsMap); |
| 470 | cbor_static_assert(((ArrayType << MajorTypeShift) & CborIteratorFlag_ContainerIsMap) == 0); |
| 471 | container->flags = shiftedMajorType & CborIteratorFlag_ContainerIsMap; |
| 472 | |
| 473 | if (length == CborIndefiniteLength) { |
| 474 | container->flags |= CborIteratorFlag_UnknownLength; |
| 475 | err = append_byte_to_buffer(container, shiftedMajorType + IndefiniteLength); |
| 476 | } else { |
Thiago Macieira | cc2bfbb | 2017-12-17 23:38:37 -0800 | [diff] [blame] | 477 | if (shiftedMajorType & CborIteratorFlag_ContainerIsMap) |
| 478 | container->remaining += length; |
Thiago Macieira | 4e9626c | 2015-09-21 14:57:17 -0700 | [diff] [blame] | 479 | err = encode_number_no_update(container, length, shiftedMajorType); |
| 480 | } |
Thiago Macieira | 863a480 | 2016-10-26 20:48:54 -0700 | [diff] [blame] | 481 | return err; |
Thiago Macieira | 355817e | 2015-05-08 18:38:18 -0700 | [diff] [blame] | 482 | } |
| 483 | |
Thiago Macieira | 46a818e | 2015-10-08 15:13:05 +0200 | [diff] [blame] | 484 | /** |
| 485 | * Creates a CBOR array in the CBOR stream provided by \a encoder and |
| 486 | * initializes \a arrayEncoder so that items can be added to the array using |
| 487 | * the CborEncoder functions. The array must be terminated by calling either |
| 488 | * cbor_encoder_close_container() or cbor_encoder_close_container_checked() |
| 489 | * with the same \a encoder and \a arrayEncoder parameters. |
| 490 | * |
| 491 | * The number of items inserted into the array must be exactly \a length items, |
| 492 | * otherwise the stream is invalid. If the number of items is not known when |
| 493 | * creating the array, the constant \ref CborIndefiniteLength may be passed as |
| 494 | * length instead. |
| 495 | * |
| 496 | * \sa cbor_encoder_create_map |
| 497 | */ |
Thiago Macieira | 355817e | 2015-05-08 18:38:18 -0700 | [diff] [blame] | 498 | CborError cbor_encoder_create_array(CborEncoder *encoder, CborEncoder *arrayEncoder, size_t length) |
| 499 | { |
Thiago Macieira | 5663524 | 2015-09-20 17:44:51 -0700 | [diff] [blame] | 500 | return create_container(encoder, arrayEncoder, length, ArrayType << MajorTypeShift); |
Thiago Macieira | 355817e | 2015-05-08 18:38:18 -0700 | [diff] [blame] | 501 | } |
| 502 | |
Thiago Macieira | 46a818e | 2015-10-08 15:13:05 +0200 | [diff] [blame] | 503 | /** |
| 504 | * Creates a CBOR map in the CBOR stream provided by \a encoder and |
| 505 | * initializes \a mapEncoder so that items can be added to the map using |
| 506 | * the CborEncoder functions. The map must be terminated by calling either |
| 507 | * cbor_encoder_close_container() or cbor_encoder_close_container_checked() |
| 508 | * with the same \a encoder and \a mapEncoder parameters. |
| 509 | * |
| 510 | * The number of pair of items inserted into the map must be exactly \a length |
Thiago Macieira | f5a172b | 2018-02-05 14:53:14 -0800 | [diff] [blame] | 511 | * items, otherwise the stream is invalid. If the number is not known |
Thiago Macieira | 46a818e | 2015-10-08 15:13:05 +0200 | [diff] [blame] | 512 | * when creating the map, the constant \ref CborIndefiniteLength may be passed as |
| 513 | * length instead. |
| 514 | * |
| 515 | * \b{Implementation limitation:} TinyCBOR cannot encode more than SIZE_MAX/2 |
| 516 | * key-value pairs in the stream. If the length \a length is larger than this |
Thiago Macieira | f5a172b | 2018-02-05 14:53:14 -0800 | [diff] [blame] | 517 | * value (and is not \ref CborIndefiniteLength), this function returns error |
| 518 | * CborErrorDataTooLarge. |
Thiago Macieira | 46a818e | 2015-10-08 15:13:05 +0200 | [diff] [blame] | 519 | * |
| 520 | * \sa cbor_encoder_create_array |
| 521 | */ |
Thiago Macieira | 355817e | 2015-05-08 18:38:18 -0700 | [diff] [blame] | 522 | CborError cbor_encoder_create_map(CborEncoder *encoder, CborEncoder *mapEncoder, size_t length) |
| 523 | { |
Thiago Macieira | 4e9626c | 2015-09-21 14:57:17 -0700 | [diff] [blame] | 524 | if (length != CborIndefiniteLength && length > SIZE_MAX / 2) |
| 525 | return CborErrorDataTooLarge; |
Thiago Macieira | 5663524 | 2015-09-20 17:44:51 -0700 | [diff] [blame] | 526 | return create_container(encoder, mapEncoder, length, MapType << MajorTypeShift); |
Thiago Macieira | 355817e | 2015-05-08 18:38:18 -0700 | [diff] [blame] | 527 | } |
| 528 | |
Thiago Macieira | 46a818e | 2015-10-08 15:13:05 +0200 | [diff] [blame] | 529 | /** |
| 530 | * Closes the CBOR container (array or map) provided by \a containerEncoder and |
| 531 | * updates the CBOR stream provided by \a encoder. Both parameters must be the |
| 532 | * same as were passed to cbor_encoder_create_array() or |
| 533 | * cbor_encoder_create_map(). |
| 534 | * |
Thiago Macieira | f5a172b | 2018-02-05 14:53:14 -0800 | [diff] [blame] | 535 | * Since version 0.5, this function verifies that the number of items (or pairs |
| 536 | * of items, in the case of a map) was correct. It is no longer necessary to call |
Thiago Macieira | 46a818e | 2015-10-08 15:13:05 +0200 | [diff] [blame] | 537 | * cbor_encoder_close_container_checked() instead. |
| 538 | * |
| 539 | * \sa cbor_encoder_create_array(), cbor_encoder_create_map() |
| 540 | */ |
Thiago Macieira | 355817e | 2015-05-08 18:38:18 -0700 | [diff] [blame] | 541 | CborError cbor_encoder_close_container(CborEncoder *encoder, const CborEncoder *containerEncoder) |
| 542 | { |
Thiago Macieira | 397f979 | 2015-09-18 19:36:26 -0700 | [diff] [blame] | 543 | if (encoder->end) |
Thiago Macieira | ede7f14 | 2016-07-30 23:08:39 -0700 | [diff] [blame] | 544 | encoder->data.ptr = containerEncoder->data.ptr; |
Thiago Macieira | 397f979 | 2015-09-18 19:36:26 -0700 | [diff] [blame] | 545 | else |
Thiago Macieira | ede7f14 | 2016-07-30 23:08:39 -0700 | [diff] [blame] | 546 | encoder->data.bytes_needed = containerEncoder->data.bytes_needed; |
Erich Keane | 47a7856 | 2015-08-10 15:19:50 -0700 | [diff] [blame] | 547 | encoder->end = containerEncoder->end; |
Thiago Macieira | 8f7bd9e | 2015-05-12 13:37:46 +0900 | [diff] [blame] | 548 | if (containerEncoder->flags & CborIteratorFlag_UnknownLength) |
| 549 | return append_byte_to_buffer(encoder, BreakByte); |
Thiago Macieira | cc2bfbb | 2017-12-17 23:38:37 -0800 | [diff] [blame] | 550 | |
| 551 | if (containerEncoder->remaining != 1) |
| 552 | return containerEncoder->remaining == 0 ? CborErrorTooManyItems : CborErrorTooFewItems; |
| 553 | |
Thiago Macieira | 5433fc3 | 2017-12-17 15:15:50 -0800 | [diff] [blame] | 554 | if (!encoder->end) |
Thiago Macieira | 3369dd1 | 2017-11-13 11:14:27 -0800 | [diff] [blame] | 555 | return CborErrorOutOfMemory; /* keep the state */ |
Thiago Macieira | 355817e | 2015-05-08 18:38:18 -0700 | [diff] [blame] | 556 | return CborNoError; |
| 557 | } |
Thiago Macieira | 46a818e | 2015-10-08 15:13:05 +0200 | [diff] [blame] | 558 | |
| 559 | /** |
| 560 | * \fn CborError cbor_encode_boolean(CborEncoder *encoder, bool value) |
| 561 | * |
| 562 | * Appends the boolean value \a value to the CBOR stream provided by \a encoder. |
| 563 | */ |
| 564 | |
| 565 | /** |
| 566 | * \fn CborError cbor_encode_null(CborEncoder *encoder) |
| 567 | * |
| 568 | * Appends the CBOR type representing a null value to the CBOR stream provided |
| 569 | * by \a encoder. |
| 570 | * |
| 571 | * \sa cbor_encode_undefined() |
| 572 | */ |
| 573 | |
| 574 | /** |
| 575 | * \fn CborError cbor_encode_undefined(CborEncoder *encoder) |
| 576 | * |
| 577 | * Appends the CBOR type representing an undefined value to the CBOR stream |
| 578 | * provided by \a encoder. |
| 579 | * |
| 580 | * \sa cbor_encode_null() |
| 581 | */ |
| 582 | |
| 583 | /** |
| 584 | * \fn CborError cbor_encode_half_float(CborEncoder *encoder, const void *value) |
| 585 | * |
| 586 | * Appends the IEEE 754 half-precision (16-bit) floating point value pointed to |
| 587 | * by \a value to the CBOR stream provided by \a encoder. |
| 588 | * |
| 589 | * \sa cbor_encode_floating_point(), cbor_encode_float(), cbor_encode_double() |
| 590 | */ |
| 591 | |
| 592 | /** |
| 593 | * \fn CborError cbor_encode_float(CborEncoder *encoder, float value) |
| 594 | * |
| 595 | * Appends the IEEE 754 single-precision (32-bit) floating point value \a value |
| 596 | * to the CBOR stream provided by \a encoder. |
| 597 | * |
| 598 | * \sa cbor_encode_floating_point(), cbor_encode_half_float(), cbor_encode_double() |
| 599 | */ |
| 600 | |
| 601 | /** |
| 602 | * \fn CborError cbor_encode_double(CborEncoder *encoder, double value) |
| 603 | * |
| 604 | * Appends the IEEE 754 double-precision (64-bit) floating point value \a value |
| 605 | * to the CBOR stream provided by \a encoder. |
| 606 | * |
| 607 | * \sa cbor_encode_floating_point(), cbor_encode_half_float(), cbor_encode_float() |
| 608 | */ |
| 609 | |
Thiago Macieira | 7ae36a7 | 2015-10-28 16:19:23 -0700 | [diff] [blame] | 610 | /** |
| 611 | * \fn size_t cbor_encoder_get_buffer_size(const CborEncoder *encoder, const uint8_t *buffer) |
| 612 | * |
| 613 | * Returns the total size of the buffer starting at \a buffer after the |
| 614 | * encoding finished without errors. The \a encoder and \a buffer arguments |
| 615 | * must be the same as supplied to cbor_encoder_init(). |
| 616 | * |
| 617 | * If the encoding process had errors, the return value of this function is |
| 618 | * meaningless. If the only errors were CborErrorOutOfMemory, instead use |
| 619 | * cbor_encoder_get_extra_bytes_needed() to find out by how much to grow the |
| 620 | * buffer before encoding again. |
| 621 | * |
| 622 | * See \ref CborEncoding for an example of using this function. |
| 623 | * |
| 624 | * \sa cbor_encoder_init(), cbor_encoder_get_extra_bytes_needed(), CborEncoding |
| 625 | */ |
| 626 | |
| 627 | /** |
| 628 | * \fn size_t cbor_encoder_get_extra_bytes_needed(const CborEncoder *encoder) |
| 629 | * |
| 630 | * Returns how many more bytes the original buffer supplied to |
| 631 | * cbor_encoder_init() needs to be extended by so that no CborErrorOutOfMemory |
| 632 | * condition will happen for the encoding. If the buffer was big enough, this |
| 633 | * function returns 0. The \a encoder must be the original argument as passed |
| 634 | * to cbor_encoder_init(). |
| 635 | * |
| 636 | * This function is usually called after an encoding sequence ended with one or |
| 637 | * more CborErrorOutOfMemory errors, but no other error. If any other error |
| 638 | * happened, the return value of this function is meaningless. |
| 639 | * |
| 640 | * See \ref CborEncoding for an example of using this function. |
| 641 | * |
| 642 | * \sa cbor_encoder_init(), cbor_encoder_get_buffer_size(), CborEncoding |
| 643 | */ |
| 644 | |
Thiago Macieira | 46a818e | 2015-10-08 15:13:05 +0200 | [diff] [blame] | 645 | /** @} */ |