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 | |
Thiago Macieira | ed5b57c | 2015-07-07 16:38:27 -0700 | [diff] [blame] | 25 | #define _BSD_SOURCE 1 |
Thiago Macieira | f1cadf0 | 2015-05-08 17:19:17 -0700 | [diff] [blame] | 26 | #include "cbor.h" |
| 27 | #include "cborconstants_p.h" |
| 28 | #include "compilersupport_p.h" |
| 29 | |
| 30 | #include <assert.h> |
Thiago Macieira | f1cadf0 | 2015-05-08 17:19:17 -0700 | [diff] [blame] | 31 | #include <stdlib.h> |
| 32 | #include <string.h> |
| 33 | |
Thiago Macieira | 8f3fb78 | 2015-06-16 16:27:01 -0700 | [diff] [blame] | 34 | #include "assert_p.h" /* Always include last */ |
| 35 | |
Thiago Macieira | 46a818e | 2015-10-08 15:13:05 +0200 | [diff] [blame^] | 36 | /** |
| 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 Macieira | 5752ce5 | 2015-06-16 12:10:03 -0700 | [diff] [blame] | 148 | 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] | 149 | { |
| 150 | encoder->ptr = buffer; |
| 151 | encoder->end = buffer + size; |
Thiago Macieira | 4e9626c | 2015-09-21 14:57:17 -0700 | [diff] [blame] | 152 | encoder->added = 0; |
Thiago Macieira | f1cadf0 | 2015-05-08 17:19:17 -0700 | [diff] [blame] | 153 | encoder->flags = flags; |
| 154 | } |
| 155 | |
Thiago Macieira | 5752ce5 | 2015-06-16 12:10:03 -0700 | [diff] [blame] | 156 | static inline void put16(void *where, uint16_t v) |
Thiago Macieira | f1cadf0 | 2015-05-08 17:19:17 -0700 | [diff] [blame] | 157 | { |
Thiago Macieira | 5934a9f | 2015-06-16 11:55:28 -0700 | [diff] [blame] | 158 | v = cbor_htons(v); |
Thiago Macieira | f1cadf0 | 2015-05-08 17:19:17 -0700 | [diff] [blame] | 159 | memcpy(where, &v, sizeof(v)); |
| 160 | } |
| 161 | |
Thiago Macieira | dbc0129 | 2016-06-06 17:02:25 -0700 | [diff] [blame] | 162 | /* 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 Keane | 47a7856 | 2015-08-10 15:19:50 -0700 | [diff] [blame] | 167 | static inline bool isOomError(CborError err) |
| 168 | { |
| 169 | (void) err; |
| 170 | return true; |
| 171 | } |
| 172 | |
Thiago Macieira | 5752ce5 | 2015-06-16 12:10:03 -0700 | [diff] [blame] | 173 | static inline void put32(void *where, uint32_t v) |
Thiago Macieira | f1cadf0 | 2015-05-08 17:19:17 -0700 | [diff] [blame] | 174 | { |
Thiago Macieira | 5934a9f | 2015-06-16 11:55:28 -0700 | [diff] [blame] | 175 | v = cbor_htonl(v); |
Thiago Macieira | f1cadf0 | 2015-05-08 17:19:17 -0700 | [diff] [blame] | 176 | memcpy(where, &v, sizeof(v)); |
| 177 | } |
| 178 | |
Thiago Macieira | 5752ce5 | 2015-06-16 12:10:03 -0700 | [diff] [blame] | 179 | static inline void put64(void *where, uint64_t v) |
Thiago Macieira | f1cadf0 | 2015-05-08 17:19:17 -0700 | [diff] [blame] | 180 | { |
Thiago Macieira | 5934a9f | 2015-06-16 11:55:28 -0700 | [diff] [blame] | 181 | v = cbor_htonll(v); |
Thiago Macieira | f1cadf0 | 2015-05-08 17:19:17 -0700 | [diff] [blame] | 182 | memcpy(where, &v, sizeof(v)); |
| 183 | } |
| 184 | |
Thiago Macieira | 397f979 | 2015-09-18 19:36:26 -0700 | [diff] [blame] | 185 | static 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 | |
| 193 | static 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 Macieira | 5752ce5 | 2015-06-16 12:10:03 -0700 | [diff] [blame] | 201 | 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] | 202 | { |
Thiago Macieira | 397f979 | 2015-09-18 19:36:26 -0700 | [diff] [blame] | 203 | if (would_overflow(encoder, len)) { |
Erich Keane | 47a7856 | 2015-08-10 15:19:50 -0700 | [diff] [blame] | 204 | if (encoder->end != NULL) { |
| 205 | len -= encoder->end - encoder->ptr; |
Thiago Macieira | 397f979 | 2015-09-18 19:36:26 -0700 | [diff] [blame] | 206 | encoder->end = NULL; |
| 207 | encoder->bytes_needed = 0; |
Erich Keane | 47a7856 | 2015-08-10 15:19:50 -0700 | [diff] [blame] | 208 | } |
| 209 | |
Thiago Macieira | 397f979 | 2015-09-18 19:36:26 -0700 | [diff] [blame] | 210 | advance_ptr(encoder, len); |
Thiago Macieira | f1cadf0 | 2015-05-08 17:19:17 -0700 | [diff] [blame] | 211 | return CborErrorOutOfMemory; |
Erich Keane | 47a7856 | 2015-08-10 15:19:50 -0700 | [diff] [blame] | 212 | } |
| 213 | |
Thiago Macieira | f1cadf0 | 2015-05-08 17:19:17 -0700 | [diff] [blame] | 214 | memcpy(encoder->ptr, data, len); |
| 215 | encoder->ptr += len; |
| 216 | return CborNoError; |
| 217 | } |
| 218 | |
Thiago Macieira | 5752ce5 | 2015-06-16 12:10:03 -0700 | [diff] [blame] | 219 | static inline CborError append_byte_to_buffer(CborEncoder *encoder, uint8_t byte) |
Thiago Macieira | 8f7bd9e | 2015-05-12 13:37:46 +0900 | [diff] [blame] | 220 | { |
Thiago Macieira | 618d2f1 | 2015-09-22 09:14:29 -0700 | [diff] [blame] | 221 | return append_to_buffer(encoder, &byte, 1); |
Thiago Macieira | 8f7bd9e | 2015-05-12 13:37:46 +0900 | [diff] [blame] | 222 | } |
| 223 | |
Thiago Macieira | 4e9626c | 2015-09-21 14:57:17 -0700 | [diff] [blame] | 224 | 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] | 225 | { |
Thiago Macieira | afa4ff6 | 2015-05-09 10:09:55 -0700 | [diff] [blame] | 226 | /* 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 Macieira | 510e5b8 | 2015-09-21 16:05:02 -0700 | [diff] [blame] | 231 | uint64_t buf[2]; |
| 232 | uint8_t *const bufend = (uint8_t *)buf + sizeof(buf); |
Thiago Macieira | 5752ce5 | 2015-06-16 12:10:03 -0700 | [diff] [blame] | 233 | uint8_t *bufstart = bufend - 1; |
Thiago Macieira | dbc0129 | 2016-06-06 17:02:25 -0700 | [diff] [blame] | 234 | 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] | 235 | |
| 236 | if (ui < Value8Bit) { |
Thiago Macieira | afa4ff6 | 2015-05-09 10:09:55 -0700 | [diff] [blame] | 237 | *bufstart += shiftedMajorType; |
Thiago Macieira | f1cadf0 | 2015-05-08 17:19:17 -0700 | [diff] [blame] | 238 | } else { |
Thiago Macieira | afa4ff6 | 2015-05-09 10:09:55 -0700 | [diff] [blame] | 239 | unsigned more = 0; |
Thiago Macieira | 13c8579 | 2015-07-07 16:15:41 -0700 | [diff] [blame] | 240 | if (ui > 0xffU) |
Thiago Macieira | afa4ff6 | 2015-05-09 10:09:55 -0700 | [diff] [blame] | 241 | ++more; |
Thiago Macieira | 13c8579 | 2015-07-07 16:15:41 -0700 | [diff] [blame] | 242 | if (ui > 0xffffU) |
Thiago Macieira | afa4ff6 | 2015-05-09 10:09:55 -0700 | [diff] [blame] | 243 | ++more; |
Thiago Macieira | 13c8579 | 2015-07-07 16:15:41 -0700 | [diff] [blame] | 244 | if (ui > 0xffffffffU) |
Thiago Macieira | afa4ff6 | 2015-05-09 10:09:55 -0700 | [diff] [blame] | 245 | ++more; |
Thiago Macieira | e12dfd0 | 2016-06-07 16:29:25 -0700 | [diff] [blame] | 246 | bufstart -= (size_t)1 << more; |
Thiago Macieira | afa4ff6 | 2015-05-09 10:09:55 -0700 | [diff] [blame] | 247 | *bufstart = shiftedMajorType + Value8Bit + more; |
Thiago Macieira | f1cadf0 | 2015-05-08 17:19:17 -0700 | [diff] [blame] | 248 | } |
| 249 | |
Thiago Macieira | afa4ff6 | 2015-05-09 10:09:55 -0700 | [diff] [blame] | 250 | return append_to_buffer(encoder, bufstart, bufend - bufstart); |
Thiago Macieira | f1cadf0 | 2015-05-08 17:19:17 -0700 | [diff] [blame] | 251 | } |
| 252 | |
Thiago Macieira | 4e9626c | 2015-09-21 14:57:17 -0700 | [diff] [blame] | 253 | static 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 Macieira | 46a818e | 2015-10-08 15:13:05 +0200 | [diff] [blame^] | 259 | /** |
| 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 Macieira | f1cadf0 | 2015-05-08 17:19:17 -0700 | [diff] [blame] | 265 | CborError cbor_encode_uint(CborEncoder *encoder, uint64_t value) |
| 266 | { |
| 267 | return encode_number(encoder, value, UnsignedIntegerType << MajorTypeShift); |
| 268 | } |
| 269 | |
Thiago Macieira | 46a818e | 2015-10-08 15:13:05 +0200 | [diff] [blame^] | 270 | /** |
| 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 Macieira | 78632b3 | 2015-09-26 14:18:48 -0700 | [diff] [blame] | 276 | CborError cbor_encode_negative_int(CborEncoder *encoder, uint64_t absolute_value) |
| 277 | { |
| 278 | return encode_number(encoder, absolute_value, NegativeIntegerType << MajorTypeShift); |
| 279 | } |
| 280 | |
Thiago Macieira | 46a818e | 2015-10-08 15:13:05 +0200 | [diff] [blame^] | 281 | /** |
| 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 Macieira | f1cadf0 | 2015-05-08 17:19:17 -0700 | [diff] [blame] | 287 | CborError cbor_encode_int(CborEncoder *encoder, int64_t value) |
| 288 | { |
Thiago Macieira | dbc0129 | 2016-06-06 17:02:25 -0700 | [diff] [blame] | 289 | /* 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 Macieira | f1cadf0 | 2015-05-08 17:19:17 -0700 | [diff] [blame] | 293 | return encode_number(encoder, ui, majorType); |
| 294 | } |
| 295 | |
Thiago Macieira | 46a818e | 2015-10-08 15:13:05 +0200 | [diff] [blame^] | 296 | /** |
| 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 Macieira | f1cadf0 | 2015-05-08 17:19:17 -0700 | [diff] [blame] | 303 | CborError cbor_encode_simple_value(CborEncoder *encoder, uint8_t value) |
| 304 | { |
| 305 | #ifndef CBOR_ENCODER_NO_CHECK_USER |
Thiago Macieira | dbc0129 | 2016-06-06 17:02:25 -0700 | [diff] [blame] | 306 | /* check if this is a valid simple type */ |
Thiago Macieira | f1cadf0 | 2015-05-08 17:19:17 -0700 | [diff] [blame] | 307 | if (value >= HalfPrecisionFloat && value <= Break) |
| 308 | return CborErrorIllegalSimpleType; |
| 309 | #endif |
| 310 | return encode_number(encoder, value, SimpleTypesType << MajorTypeShift); |
| 311 | } |
| 312 | |
Thiago Macieira | 46a818e | 2015-10-08 15:13:05 +0200 | [diff] [blame^] | 313 | /** |
| 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 Macieira | 81f3343 | 2015-07-02 16:16:28 -0700 | [diff] [blame] | 324 | CborError cbor_encode_floating_point(CborEncoder *encoder, CborType fpType, const void *value) |
Thiago Macieira | f1cadf0 | 2015-05-08 17:19:17 -0700 | [diff] [blame] | 325 | { |
Thiago Macieira | 81f3343 | 2015-07-02 16:16:28 -0700 | [diff] [blame] | 326 | uint8_t buf[1 + sizeof(uint64_t)]; |
| 327 | assert(fpType == CborHalfFloatType || fpType == CborFloatType || fpType == CborDoubleType); |
| 328 | buf[0] = fpType; |
Thiago Macieira | f1cadf0 | 2015-05-08 17:19:17 -0700 | [diff] [blame] | 329 | |
Thiago Macieira | 81f3343 | 2015-07-02 16:16:28 -0700 | [diff] [blame] | 330 | 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 Macieira | 4e9626c | 2015-09-21 14:57:17 -0700 | [diff] [blame] | 337 | ++encoder->added; |
Thiago Macieira | 81f3343 | 2015-07-02 16:16:28 -0700 | [diff] [blame] | 338 | return append_to_buffer(encoder, buf, size + 1); |
Thiago Macieira | f1cadf0 | 2015-05-08 17:19:17 -0700 | [diff] [blame] | 339 | } |
Thiago Macieira | 8f98a11 | 2015-05-08 17:25:29 -0700 | [diff] [blame] | 340 | |
Thiago Macieira | 46a818e | 2015-10-08 15:13:05 +0200 | [diff] [blame^] | 341 | /** |
| 342 | * Appends the CBOR tag \a tag to the CBOR stream provided by \a encoder. |
| 343 | * |
| 344 | * \sa CborTag |
| 345 | */ |
Thiago Macieira | 8f98a11 | 2015-05-08 17:25:29 -0700 | [diff] [blame] | 346 | CborError cbor_encode_tag(CborEncoder *encoder, CborTag tag) |
| 347 | { |
Thiago Macieira | dbc0129 | 2016-06-06 17:02:25 -0700 | [diff] [blame] | 348 | /* 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] | 349 | return encode_number_no_update(encoder, tag, TagType << MajorTypeShift); |
Thiago Macieira | 8f98a11 | 2015-05-08 17:25:29 -0700 | [diff] [blame] | 350 | } |
Thiago Macieira | b54debe | 2015-05-08 17:42:39 -0700 | [diff] [blame] | 351 | |
Thiago Macieira | 5752ce5 | 2015-06-16 12:10:03 -0700 | [diff] [blame] | 352 | 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] | 353 | { |
| 354 | CborError err = encode_number(encoder, length, shiftedMajorType); |
Erich Keane | 47a7856 | 2015-08-10 15:19:50 -0700 | [diff] [blame] | 355 | if (err && !isOomError(err)) |
Thiago Macieira | b54debe | 2015-05-08 17:42:39 -0700 | [diff] [blame] | 356 | return err; |
| 357 | return append_to_buffer(encoder, string, length); |
| 358 | } |
| 359 | |
Thiago Macieira | 46a818e | 2015-10-08 15:13:05 +0200 | [diff] [blame^] | 360 | /** |
| 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 Macieira | 5752ce5 | 2015-06-16 12:10:03 -0700 | [diff] [blame] | 378 | 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] | 379 | { |
| 380 | return encode_string(encoder, length, ByteStringType << MajorTypeShift, string); |
| 381 | } |
| 382 | |
Thiago Macieira | 46a818e | 2015-10-08 15:13:05 +0200 | [diff] [blame^] | 383 | /** |
| 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 Macieira | b54debe | 2015-05-08 17:42:39 -0700 | [diff] [blame] | 389 | CborError cbor_encode_text_string(CborEncoder *encoder, const char *string, size_t length) |
| 390 | { |
| 391 | return encode_string(encoder, length, TextStringType << MajorTypeShift, string); |
| 392 | } |
Thiago Macieira | 355817e | 2015-05-08 18:38:18 -0700 | [diff] [blame] | 393 | |
Thiago Macieira | 5663524 | 2015-09-20 17:44:51 -0700 | [diff] [blame] | 394 | #ifdef __GNUC__ |
| 395 | __attribute__((noinline)) |
| 396 | #endif |
| 397 | 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] | 398 | { |
Thiago Macieira | 8f7bd9e | 2015-05-12 13:37:46 +0900 | [diff] [blame] | 399 | CborError err; |
Thiago Macieira | 4e9626c | 2015-09-21 14:57:17 -0700 | [diff] [blame] | 400 | 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 Keane | 47a7856 | 2015-08-10 15:19:50 -0700 | [diff] [blame] | 415 | if (err && !isOomError(err)) |
Thiago Macieira | 355817e | 2015-05-08 18:38:18 -0700 | [diff] [blame] | 416 | return err; |
Thiago Macieira | 8f7bd9e | 2015-05-12 13:37:46 +0900 | [diff] [blame] | 417 | |
Thiago Macieira | 355817e | 2015-05-08 18:38:18 -0700 | [diff] [blame] | 418 | return CborNoError; |
| 419 | } |
| 420 | |
Thiago Macieira | 46a818e | 2015-10-08 15:13:05 +0200 | [diff] [blame^] | 421 | /** |
| 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 Macieira | 355817e | 2015-05-08 18:38:18 -0700 | [diff] [blame] | 435 | CborError cbor_encoder_create_array(CborEncoder *encoder, CborEncoder *arrayEncoder, size_t length) |
| 436 | { |
Thiago Macieira | 5663524 | 2015-09-20 17:44:51 -0700 | [diff] [blame] | 437 | return create_container(encoder, arrayEncoder, length, ArrayType << MajorTypeShift); |
Thiago Macieira | 355817e | 2015-05-08 18:38:18 -0700 | [diff] [blame] | 438 | } |
| 439 | |
Thiago Macieira | 46a818e | 2015-10-08 15:13:05 +0200 | [diff] [blame^] | 440 | /** |
| 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 Macieira | 355817e | 2015-05-08 18:38:18 -0700 | [diff] [blame] | 458 | CborError cbor_encoder_create_map(CborEncoder *encoder, CborEncoder *mapEncoder, size_t length) |
| 459 | { |
Thiago Macieira | 4e9626c | 2015-09-21 14:57:17 -0700 | [diff] [blame] | 460 | if (length != CborIndefiniteLength && length > SIZE_MAX / 2) |
| 461 | return CborErrorDataTooLarge; |
Thiago Macieira | 5663524 | 2015-09-20 17:44:51 -0700 | [diff] [blame] | 462 | return create_container(encoder, mapEncoder, length, MapType << MajorTypeShift); |
Thiago Macieira | 355817e | 2015-05-08 18:38:18 -0700 | [diff] [blame] | 463 | } |
| 464 | |
Thiago Macieira | 46a818e | 2015-10-08 15:13:05 +0200 | [diff] [blame^] | 465 | /** |
| 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 Macieira | 355817e | 2015-05-08 18:38:18 -0700 | [diff] [blame] | 477 | CborError cbor_encoder_close_container(CborEncoder *encoder, const CborEncoder *containerEncoder) |
| 478 | { |
Thiago Macieira | 397f979 | 2015-09-18 19:36:26 -0700 | [diff] [blame] | 479 | if (encoder->end) |
| 480 | encoder->ptr = containerEncoder->ptr; |
| 481 | else |
| 482 | encoder->bytes_needed = containerEncoder->bytes_needed; |
Erich Keane | 47a7856 | 2015-08-10 15:19:50 -0700 | [diff] [blame] | 483 | encoder->end = containerEncoder->end; |
Thiago Macieira | 8f7bd9e | 2015-05-12 13:37:46 +0900 | [diff] [blame] | 484 | if (containerEncoder->flags & CborIteratorFlag_UnknownLength) |
| 485 | return append_byte_to_buffer(encoder, BreakByte); |
Thiago Macieira | 355817e | 2015-05-08 18:38:18 -0700 | [diff] [blame] | 486 | return CborNoError; |
| 487 | } |
Thiago Macieira | 46a818e | 2015-10-08 15:13:05 +0200 | [diff] [blame^] | 488 | |
| 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 | /** @} */ |