Thiago Macieira | f1cadf0 | 2015-05-08 17:19:17 -0700 | [diff] [blame] | 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2015 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 | |
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 | 5752ce5 | 2015-06-16 12:10:03 -0700 | [diff] [blame] | 36 | 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] | 37 | { |
| 38 | encoder->ptr = buffer; |
| 39 | encoder->end = buffer + size; |
Thiago Macieira | 4e9626c | 2015-09-21 14:57:17 -0700 | [diff] [blame] | 40 | encoder->added = 0; |
Thiago Macieira | f1cadf0 | 2015-05-08 17:19:17 -0700 | [diff] [blame] | 41 | encoder->flags = flags; |
| 42 | } |
| 43 | |
Thiago Macieira | 5752ce5 | 2015-06-16 12:10:03 -0700 | [diff] [blame] | 44 | static inline void put16(void *where, uint16_t v) |
Thiago Macieira | f1cadf0 | 2015-05-08 17:19:17 -0700 | [diff] [blame] | 45 | { |
Thiago Macieira | 5934a9f | 2015-06-16 11:55:28 -0700 | [diff] [blame] | 46 | v = cbor_htons(v); |
Thiago Macieira | f1cadf0 | 2015-05-08 17:19:17 -0700 | [diff] [blame] | 47 | memcpy(where, &v, sizeof(v)); |
| 48 | } |
| 49 | |
Erich Keane | 47a7856 | 2015-08-10 15:19:50 -0700 | [diff] [blame] | 50 | // Note: Since this is currently only used in situations where OOM is the only |
| 51 | // valid error, we KNOW this to be true. Thus, this function now returns just 'true', |
| 52 | // but if in the future, any function starts returning a non-OOM error, this will need |
| 53 | // to be changed to the test. At the moment, this is done to prevent more branches |
| 54 | // being created in the tinycbor output |
| 55 | static inline bool isOomError(CborError err) |
| 56 | { |
| 57 | (void) err; |
| 58 | return true; |
| 59 | } |
| 60 | |
Thiago Macieira | 5752ce5 | 2015-06-16 12:10:03 -0700 | [diff] [blame] | 61 | static inline void put32(void *where, uint32_t v) |
Thiago Macieira | f1cadf0 | 2015-05-08 17:19:17 -0700 | [diff] [blame] | 62 | { |
Thiago Macieira | 5934a9f | 2015-06-16 11:55:28 -0700 | [diff] [blame] | 63 | v = cbor_htonl(v); |
Thiago Macieira | f1cadf0 | 2015-05-08 17:19:17 -0700 | [diff] [blame] | 64 | memcpy(where, &v, sizeof(v)); |
| 65 | } |
| 66 | |
Thiago Macieira | 5752ce5 | 2015-06-16 12:10:03 -0700 | [diff] [blame] | 67 | static inline void put64(void *where, uint64_t v) |
Thiago Macieira | f1cadf0 | 2015-05-08 17:19:17 -0700 | [diff] [blame] | 68 | { |
Thiago Macieira | 5934a9f | 2015-06-16 11:55:28 -0700 | [diff] [blame] | 69 | v = cbor_htonll(v); |
Thiago Macieira | f1cadf0 | 2015-05-08 17:19:17 -0700 | [diff] [blame] | 70 | memcpy(where, &v, sizeof(v)); |
| 71 | } |
| 72 | |
Thiago Macieira | 397f979 | 2015-09-18 19:36:26 -0700 | [diff] [blame] | 73 | static inline bool would_overflow(CborEncoder *encoder, size_t len) |
| 74 | { |
| 75 | ptrdiff_t remaining = (ptrdiff_t)encoder->end; |
| 76 | remaining -= remaining ? (ptrdiff_t)encoder->ptr : encoder->bytes_needed; |
| 77 | remaining -= (ptrdiff_t)len; |
| 78 | return unlikely(remaining < 0); |
| 79 | } |
| 80 | |
| 81 | static inline void advance_ptr(CborEncoder *encoder, size_t n) |
| 82 | { |
| 83 | if (encoder->end) |
| 84 | encoder->ptr += n; |
| 85 | else |
| 86 | encoder->bytes_needed += n; |
| 87 | } |
| 88 | |
Thiago Macieira | 5752ce5 | 2015-06-16 12:10:03 -0700 | [diff] [blame] | 89 | 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] | 90 | { |
Thiago Macieira | 397f979 | 2015-09-18 19:36:26 -0700 | [diff] [blame] | 91 | if (would_overflow(encoder, len)) { |
Erich Keane | 47a7856 | 2015-08-10 15:19:50 -0700 | [diff] [blame] | 92 | if (encoder->end != NULL) { |
| 93 | len -= encoder->end - encoder->ptr; |
Thiago Macieira | 397f979 | 2015-09-18 19:36:26 -0700 | [diff] [blame] | 94 | encoder->end = NULL; |
| 95 | encoder->bytes_needed = 0; |
Erich Keane | 47a7856 | 2015-08-10 15:19:50 -0700 | [diff] [blame] | 96 | } |
| 97 | |
Thiago Macieira | 397f979 | 2015-09-18 19:36:26 -0700 | [diff] [blame] | 98 | advance_ptr(encoder, len); |
Thiago Macieira | f1cadf0 | 2015-05-08 17:19:17 -0700 | [diff] [blame] | 99 | return CborErrorOutOfMemory; |
Erich Keane | 47a7856 | 2015-08-10 15:19:50 -0700 | [diff] [blame] | 100 | } |
| 101 | |
Thiago Macieira | f1cadf0 | 2015-05-08 17:19:17 -0700 | [diff] [blame] | 102 | memcpy(encoder->ptr, data, len); |
| 103 | encoder->ptr += len; |
| 104 | return CborNoError; |
| 105 | } |
| 106 | |
Thiago Macieira | 5752ce5 | 2015-06-16 12:10:03 -0700 | [diff] [blame] | 107 | static inline CborError append_byte_to_buffer(CborEncoder *encoder, uint8_t byte) |
Thiago Macieira | 8f7bd9e | 2015-05-12 13:37:46 +0900 | [diff] [blame] | 108 | { |
Thiago Macieira | 397f979 | 2015-09-18 19:36:26 -0700 | [diff] [blame] | 109 | if (would_overflow(encoder, 1)) { |
Erich Keane | 47a7856 | 2015-08-10 15:19:50 -0700 | [diff] [blame] | 110 | if (encoder->end != NULL) { |
Thiago Macieira | 397f979 | 2015-09-18 19:36:26 -0700 | [diff] [blame] | 111 | encoder->end = NULL; |
| 112 | encoder->bytes_needed = 0; |
Erich Keane | 47a7856 | 2015-08-10 15:19:50 -0700 | [diff] [blame] | 113 | } |
| 114 | |
Thiago Macieira | 397f979 | 2015-09-18 19:36:26 -0700 | [diff] [blame] | 115 | advance_ptr(encoder, 1); |
Thiago Macieira | 8f7bd9e | 2015-05-12 13:37:46 +0900 | [diff] [blame] | 116 | return CborErrorOutOfMemory; |
Erich Keane | 47a7856 | 2015-08-10 15:19:50 -0700 | [diff] [blame] | 117 | } |
| 118 | |
Thiago Macieira | 8f7bd9e | 2015-05-12 13:37:46 +0900 | [diff] [blame] | 119 | *encoder->ptr++ = byte; |
| 120 | return CborNoError; |
| 121 | } |
| 122 | |
Thiago Macieira | 4e9626c | 2015-09-21 14:57:17 -0700 | [diff] [blame] | 123 | 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] | 124 | { |
Thiago Macieira | afa4ff6 | 2015-05-09 10:09:55 -0700 | [diff] [blame] | 125 | /* Little-endian would have been so much more convenient here: |
| 126 | * We could just write at the beginning of buf but append_to_buffer |
| 127 | * only the necessary bytes. |
| 128 | * Since it has to be big endian, do it the other way around: |
| 129 | * write from the end. */ |
Thiago Macieira | 510e5b8 | 2015-09-21 16:05:02 -0700 | [diff] [blame] | 130 | uint64_t buf[2]; |
| 131 | uint8_t *const bufend = (uint8_t *)buf + sizeof(buf); |
Thiago Macieira | 5752ce5 | 2015-06-16 12:10:03 -0700 | [diff] [blame] | 132 | uint8_t *bufstart = bufend - 1; |
Thiago Macieira | afa4ff6 | 2015-05-09 10:09:55 -0700 | [diff] [blame] | 133 | 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] | 134 | |
| 135 | if (ui < Value8Bit) { |
Thiago Macieira | afa4ff6 | 2015-05-09 10:09:55 -0700 | [diff] [blame] | 136 | *bufstart += shiftedMajorType; |
Thiago Macieira | f1cadf0 | 2015-05-08 17:19:17 -0700 | [diff] [blame] | 137 | } else { |
Thiago Macieira | afa4ff6 | 2015-05-09 10:09:55 -0700 | [diff] [blame] | 138 | unsigned more = 0; |
Thiago Macieira | 13c8579 | 2015-07-07 16:15:41 -0700 | [diff] [blame] | 139 | if (ui > 0xffU) |
Thiago Macieira | afa4ff6 | 2015-05-09 10:09:55 -0700 | [diff] [blame] | 140 | ++more; |
Thiago Macieira | 13c8579 | 2015-07-07 16:15:41 -0700 | [diff] [blame] | 141 | if (ui > 0xffffU) |
Thiago Macieira | afa4ff6 | 2015-05-09 10:09:55 -0700 | [diff] [blame] | 142 | ++more; |
Thiago Macieira | 13c8579 | 2015-07-07 16:15:41 -0700 | [diff] [blame] | 143 | if (ui > 0xffffffffU) |
Thiago Macieira | afa4ff6 | 2015-05-09 10:09:55 -0700 | [diff] [blame] | 144 | ++more; |
| 145 | bufstart -= 1 << more; |
| 146 | *bufstart = shiftedMajorType + Value8Bit + more; |
Thiago Macieira | f1cadf0 | 2015-05-08 17:19:17 -0700 | [diff] [blame] | 147 | } |
| 148 | |
Thiago Macieira | afa4ff6 | 2015-05-09 10:09:55 -0700 | [diff] [blame] | 149 | return append_to_buffer(encoder, bufstart, bufend - bufstart); |
Thiago Macieira | f1cadf0 | 2015-05-08 17:19:17 -0700 | [diff] [blame] | 150 | } |
| 151 | |
Thiago Macieira | 4e9626c | 2015-09-21 14:57:17 -0700 | [diff] [blame] | 152 | static inline CborError encode_number(CborEncoder *encoder, uint64_t ui, uint8_t shiftedMajorType) |
| 153 | { |
| 154 | ++encoder->added; |
| 155 | return encode_number_no_update(encoder, ui, shiftedMajorType); |
| 156 | } |
| 157 | |
| 158 | |
Thiago Macieira | f1cadf0 | 2015-05-08 17:19:17 -0700 | [diff] [blame] | 159 | CborError cbor_encode_uint(CborEncoder *encoder, uint64_t value) |
| 160 | { |
| 161 | return encode_number(encoder, value, UnsignedIntegerType << MajorTypeShift); |
| 162 | } |
| 163 | |
| 164 | CborError cbor_encode_int(CborEncoder *encoder, int64_t value) |
| 165 | { |
| 166 | // adapted from code in RFC 7049 appendix C (pseudocode) |
| 167 | uint64_t ui = value >> 63; // extend sign to whole length |
| 168 | uint8_t majorType = ui & 0x20; // extract major type |
| 169 | ui ^= value; // complement negatives |
| 170 | return encode_number(encoder, ui, majorType); |
| 171 | } |
| 172 | |
| 173 | CborError cbor_encode_simple_value(CborEncoder *encoder, uint8_t value) |
| 174 | { |
| 175 | #ifndef CBOR_ENCODER_NO_CHECK_USER |
| 176 | // check if this is a valid simple type |
| 177 | if (value >= HalfPrecisionFloat && value <= Break) |
| 178 | return CborErrorIllegalSimpleType; |
| 179 | #endif |
| 180 | return encode_number(encoder, value, SimpleTypesType << MajorTypeShift); |
| 181 | } |
| 182 | |
Thiago Macieira | 81f3343 | 2015-07-02 16:16:28 -0700 | [diff] [blame] | 183 | CborError cbor_encode_floating_point(CborEncoder *encoder, CborType fpType, const void *value) |
Thiago Macieira | f1cadf0 | 2015-05-08 17:19:17 -0700 | [diff] [blame] | 184 | { |
Thiago Macieira | 81f3343 | 2015-07-02 16:16:28 -0700 | [diff] [blame] | 185 | uint8_t buf[1 + sizeof(uint64_t)]; |
| 186 | assert(fpType == CborHalfFloatType || fpType == CborFloatType || fpType == CborDoubleType); |
| 187 | buf[0] = fpType; |
Thiago Macieira | f1cadf0 | 2015-05-08 17:19:17 -0700 | [diff] [blame] | 188 | |
Thiago Macieira | 81f3343 | 2015-07-02 16:16:28 -0700 | [diff] [blame] | 189 | unsigned size = 2U << (fpType - CborHalfFloatType); |
| 190 | if (size == 8) |
| 191 | put64(buf + 1, *(const uint64_t*)value); |
| 192 | else if (size == 4) |
| 193 | put32(buf + 1, *(const uint32_t*)value); |
| 194 | else |
| 195 | put16(buf + 1, *(const uint16_t*)value); |
Thiago Macieira | 4e9626c | 2015-09-21 14:57:17 -0700 | [diff] [blame] | 196 | ++encoder->added; |
Thiago Macieira | 81f3343 | 2015-07-02 16:16:28 -0700 | [diff] [blame] | 197 | return append_to_buffer(encoder, buf, size + 1); |
Thiago Macieira | f1cadf0 | 2015-05-08 17:19:17 -0700 | [diff] [blame] | 198 | } |
Thiago Macieira | 8f98a11 | 2015-05-08 17:25:29 -0700 | [diff] [blame] | 199 | |
| 200 | CborError cbor_encode_tag(CborEncoder *encoder, CborTag tag) |
| 201 | { |
Thiago Macieira | 4e9626c | 2015-09-21 14:57:17 -0700 | [diff] [blame] | 202 | // tags don't count towards the number of elements in an array or map |
| 203 | return encode_number_no_update(encoder, tag, TagType << MajorTypeShift); |
Thiago Macieira | 8f98a11 | 2015-05-08 17:25:29 -0700 | [diff] [blame] | 204 | } |
Thiago Macieira | b54debe | 2015-05-08 17:42:39 -0700 | [diff] [blame] | 205 | |
Thiago Macieira | 5752ce5 | 2015-06-16 12:10:03 -0700 | [diff] [blame] | 206 | 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] | 207 | { |
| 208 | CborError err = encode_number(encoder, length, shiftedMajorType); |
Erich Keane | 47a7856 | 2015-08-10 15:19:50 -0700 | [diff] [blame] | 209 | if (err && !isOomError(err)) |
Thiago Macieira | b54debe | 2015-05-08 17:42:39 -0700 | [diff] [blame] | 210 | return err; |
| 211 | return append_to_buffer(encoder, string, length); |
| 212 | } |
| 213 | |
Thiago Macieira | 5752ce5 | 2015-06-16 12:10:03 -0700 | [diff] [blame] | 214 | 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] | 215 | { |
| 216 | return encode_string(encoder, length, ByteStringType << MajorTypeShift, string); |
| 217 | } |
| 218 | |
| 219 | CborError cbor_encode_text_string(CborEncoder *encoder, const char *string, size_t length) |
| 220 | { |
| 221 | return encode_string(encoder, length, TextStringType << MajorTypeShift, string); |
| 222 | } |
Thiago Macieira | 355817e | 2015-05-08 18:38:18 -0700 | [diff] [blame] | 223 | |
Thiago Macieira | 5663524 | 2015-09-20 17:44:51 -0700 | [diff] [blame] | 224 | #ifdef __GNUC__ |
| 225 | __attribute__((noinline)) |
| 226 | #endif |
| 227 | 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] | 228 | { |
Thiago Macieira | 8f7bd9e | 2015-05-12 13:37:46 +0900 | [diff] [blame] | 229 | CborError err; |
Thiago Macieira | 4e9626c | 2015-09-21 14:57:17 -0700 | [diff] [blame] | 230 | container->ptr = encoder->ptr; |
| 231 | container->end = encoder->end; |
| 232 | ++encoder->added; |
| 233 | container->added = 0; |
| 234 | |
| 235 | cbor_static_assert(((MapType << MajorTypeShift) & CborIteratorFlag_ContainerIsMap) == CborIteratorFlag_ContainerIsMap); |
| 236 | cbor_static_assert(((ArrayType << MajorTypeShift) & CborIteratorFlag_ContainerIsMap) == 0); |
| 237 | container->flags = shiftedMajorType & CborIteratorFlag_ContainerIsMap; |
| 238 | |
| 239 | if (length == CborIndefiniteLength) { |
| 240 | container->flags |= CborIteratorFlag_UnknownLength; |
| 241 | err = append_byte_to_buffer(container, shiftedMajorType + IndefiniteLength); |
| 242 | } else { |
| 243 | err = encode_number_no_update(container, length, shiftedMajorType); |
| 244 | } |
Erich Keane | 47a7856 | 2015-08-10 15:19:50 -0700 | [diff] [blame] | 245 | if (err && !isOomError(err)) |
Thiago Macieira | 355817e | 2015-05-08 18:38:18 -0700 | [diff] [blame] | 246 | return err; |
Thiago Macieira | 8f7bd9e | 2015-05-12 13:37:46 +0900 | [diff] [blame] | 247 | |
Thiago Macieira | 355817e | 2015-05-08 18:38:18 -0700 | [diff] [blame] | 248 | return CborNoError; |
| 249 | } |
| 250 | |
| 251 | CborError cbor_encoder_create_array(CborEncoder *encoder, CborEncoder *arrayEncoder, size_t length) |
| 252 | { |
Thiago Macieira | 5663524 | 2015-09-20 17:44:51 -0700 | [diff] [blame] | 253 | return create_container(encoder, arrayEncoder, length, ArrayType << MajorTypeShift); |
Thiago Macieira | 355817e | 2015-05-08 18:38:18 -0700 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | CborError cbor_encoder_create_map(CborEncoder *encoder, CborEncoder *mapEncoder, size_t length) |
| 257 | { |
Thiago Macieira | 4e9626c | 2015-09-21 14:57:17 -0700 | [diff] [blame] | 258 | if (length != CborIndefiniteLength && length > SIZE_MAX / 2) |
| 259 | return CborErrorDataTooLarge; |
Thiago Macieira | 5663524 | 2015-09-20 17:44:51 -0700 | [diff] [blame] | 260 | return create_container(encoder, mapEncoder, length, MapType << MajorTypeShift); |
Thiago Macieira | 355817e | 2015-05-08 18:38:18 -0700 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | CborError cbor_encoder_close_container(CborEncoder *encoder, const CborEncoder *containerEncoder) |
| 264 | { |
Thiago Macieira | 397f979 | 2015-09-18 19:36:26 -0700 | [diff] [blame] | 265 | if (encoder->end) |
| 266 | encoder->ptr = containerEncoder->ptr; |
| 267 | else |
| 268 | encoder->bytes_needed = containerEncoder->bytes_needed; |
Erich Keane | 47a7856 | 2015-08-10 15:19:50 -0700 | [diff] [blame] | 269 | encoder->end = containerEncoder->end; |
Thiago Macieira | 8f7bd9e | 2015-05-12 13:37:46 +0900 | [diff] [blame] | 270 | if (containerEncoder->flags & CborIteratorFlag_UnknownLength) |
| 271 | return append_byte_to_buffer(encoder, BreakByte); |
Thiago Macieira | 355817e | 2015-05-08 18:38:18 -0700 | [diff] [blame] | 272 | return CborNoError; |
| 273 | } |