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