Thiago Macieira | 54a0e10 | 2015-05-05 21:25:06 -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 | |
| 25 | #define _BSD_SOURCE |
| 26 | #include "cbor.h" |
| 27 | #include "cborconstants_p.h" |
| 28 | #include "compilersupport_p.h" |
| 29 | |
| 30 | #include <assert.h> |
| 31 | #include <endian.h> |
Thiago Macieira | 2312efd | 2015-05-06 16:07:48 -0700 | [diff] [blame] | 32 | #include <stdlib.h> |
Thiago Macieira | 54a0e10 | 2015-05-05 21:25:06 -0700 | [diff] [blame] | 33 | #include <string.h> |
| 34 | |
Thiago Macieira | c70169f | 2015-05-06 07:49:44 -0700 | [diff] [blame] | 35 | #if defined(__GNUC__) && !defined(__INTEL_COMPILER) && !defined(__clang__) && \ |
| 36 | (__GNUC__ * 100 + __GNUC_MINOR__ >= 404) |
| 37 | # pragma GCC optimize("-ffunction-sections") |
| 38 | #endif |
| 39 | |
Thiago Macieira | 54a0e10 | 2015-05-05 21:25:06 -0700 | [diff] [blame] | 40 | /** |
| 41 | * \typedef CborValue |
| 42 | * This type contains one value parsed from the CBOR stream. |
| 43 | * |
| 44 | * To get the actual type, use cbor_value_get_type(). Then extract the value |
| 45 | * using one of the corresponding functions: cbor_value_get_boolean(), cbor_value_get_int64(), |
| 46 | * cbor_value_get_int(), cbor_value_copy_string(), cbor_value_get_array(), cbor_value_get_map(), |
| 47 | * cbor_value_get_double(), cbor_value_get_float(). |
| 48 | * |
| 49 | * In C++ and C11 modes, you can additionally use the cbor_value_get_integer() |
| 50 | * and cbor_value_get_floating_point() generic functions. |
| 51 | * |
| 52 | * \omit |
| 53 | * Implementation details: the CborValue contains these fields: |
| 54 | * \list |
| 55 | * \li ptr: pointer to the actual data |
| 56 | * \li flags: flags from the decoder |
Thiago Macieira | 2312efd | 2015-05-06 16:07:48 -0700 | [diff] [blame] | 57 | * \li extra: partially decoded integer value (0, 1 or 2 bytes) |
Thiago Macieira | 54a0e10 | 2015-05-05 21:25:06 -0700 | [diff] [blame] | 58 | * \li remaining: remaining items in this collection after this item or UINT32_MAX if length is unknown |
| 59 | * \endlist |
| 60 | * \endomit |
| 61 | */ |
| 62 | |
Thiago Macieira | 54a0e10 | 2015-05-05 21:25:06 -0700 | [diff] [blame] | 63 | static inline uint16_t get16(const char *ptr) |
| 64 | { |
| 65 | uint16_t result; |
| 66 | memcpy(&result, ptr, sizeof(result)); |
| 67 | return be16toh(result); |
| 68 | } |
| 69 | |
| 70 | static inline uint32_t get32(const char *ptr) |
| 71 | { |
| 72 | uint32_t result; |
| 73 | memcpy(&result, ptr, sizeof(result)); |
| 74 | return be32toh(result); |
| 75 | } |
| 76 | |
| 77 | static inline uint64_t get64(const char *ptr) |
| 78 | { |
| 79 | uint64_t result; |
| 80 | memcpy(&result, ptr, sizeof(result)); |
| 81 | return be64toh(result); |
| 82 | } |
| 83 | |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 84 | static inline CborError extract_number(const CborParser *parser, const char **ptr, uint64_t *len) |
Thiago Macieira | 2312efd | 2015-05-06 16:07:48 -0700 | [diff] [blame] | 85 | { |
| 86 | uint8_t additional_information = **ptr & SmallValueMask; |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 87 | ++*ptr; |
Thiago Macieira | 2312efd | 2015-05-06 16:07:48 -0700 | [diff] [blame] | 88 | if (additional_information < Value8Bit) { |
| 89 | *len = additional_information; |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 90 | return CborNoError; |
Thiago Macieira | 2312efd | 2015-05-06 16:07:48 -0700 | [diff] [blame] | 91 | } |
| 92 | if (unlikely(additional_information > Value64Bit)) |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 93 | return CborErrorIllegalNumber; |
Thiago Macieira | 2312efd | 2015-05-06 16:07:48 -0700 | [diff] [blame] | 94 | |
| 95 | size_t bytesNeeded = 1 << (additional_information - Value8Bit); |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 96 | if (unlikely(*ptr + bytesNeeded > parser->end)) { |
| 97 | return CborErrorUnexpectedEOF; |
Thiago Macieira | 2312efd | 2015-05-06 16:07:48 -0700 | [diff] [blame] | 98 | } else if (bytesNeeded == 1) { |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 99 | *len = (uint8_t)(*ptr)[0]; |
Thiago Macieira | 2312efd | 2015-05-06 16:07:48 -0700 | [diff] [blame] | 100 | } else if (bytesNeeded == 2) { |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 101 | *len = get16(*ptr); |
Thiago Macieira | 2312efd | 2015-05-06 16:07:48 -0700 | [diff] [blame] | 102 | } else if (bytesNeeded == 4) { |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 103 | *len = get32(*ptr); |
Thiago Macieira | 2312efd | 2015-05-06 16:07:48 -0700 | [diff] [blame] | 104 | } else { |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 105 | *len = get64(*ptr); |
Thiago Macieira | 2312efd | 2015-05-06 16:07:48 -0700 | [diff] [blame] | 106 | } |
| 107 | *ptr += bytesNeeded; |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 108 | return CborNoError; |
Thiago Macieira | 2312efd | 2015-05-06 16:07:48 -0700 | [diff] [blame] | 109 | } |
| 110 | |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 111 | static inline CborError extract_length(const CborParser *parser, const char **ptr, size_t *len) |
Thiago Macieira | 54a0e10 | 2015-05-05 21:25:06 -0700 | [diff] [blame] | 112 | { |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 113 | uint64_t v; |
| 114 | CborError err = extract_number(parser, ptr, &v); |
| 115 | if (err) |
| 116 | return err; |
| 117 | |
| 118 | *len = v; |
| 119 | if (v != *len) |
| 120 | return CborErrorDataTooLarge; |
| 121 | return CborNoError; |
| 122 | } |
| 123 | |
| 124 | static bool is_fixed_type(uint8_t type) |
| 125 | { |
| 126 | return type != CborTextStringType && type != CborByteStringType && type != CborArrayType && |
| 127 | type != CborMapType; |
| 128 | } |
| 129 | |
| 130 | static CborError preparse_value(CborValue *it) |
| 131 | { |
| 132 | const CborParser *parser = it->parser; |
Thiago Macieira | 11e913f | 2015-05-07 13:01:18 -0700 | [diff] [blame^] | 133 | it->type = CborInvalidType; |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 134 | |
Thiago Macieira | 54a0e10 | 2015-05-05 21:25:06 -0700 | [diff] [blame] | 135 | // are we at the end? |
Thiago Macieira | 54a0e10 | 2015-05-05 21:25:06 -0700 | [diff] [blame] | 136 | if (it->ptr == parser->end) |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 137 | return CborErrorUnexpectedEOF; |
Thiago Macieira | 54a0e10 | 2015-05-05 21:25:06 -0700 | [diff] [blame] | 138 | |
| 139 | uint8_t descriptor = *it->ptr; |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 140 | uint8_t type = descriptor & MajorTypeMask; |
Thiago Macieira | 54a0e10 | 2015-05-05 21:25:06 -0700 | [diff] [blame] | 141 | it->flags = 0; |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 142 | it->extra = (descriptor &= SmallValueMask); |
| 143 | |
| 144 | if (descriptor == IndefiniteLength && !is_fixed_type(type)) { |
| 145 | // special case |
| 146 | it->flags |= CborIteratorFlag_UnknownLength; |
| 147 | it->type = type; |
| 148 | return CborNoError; |
| 149 | } |
Thiago Macieira | 54a0e10 | 2015-05-05 21:25:06 -0700 | [diff] [blame] | 150 | |
Thiago Macieira | c70169f | 2015-05-06 07:49:44 -0700 | [diff] [blame] | 151 | size_t bytesNeeded = descriptor < Value8Bit ? 0 : (1 << (descriptor - Value8Bit)); |
| 152 | if (it->ptr + 1 + bytesNeeded > parser->end) |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 153 | return CborErrorUnexpectedEOF; |
Thiago Macieira | c70169f | 2015-05-06 07:49:44 -0700 | [diff] [blame] | 154 | |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 155 | switch ((CborMajorTypes)(type >> MajorTypeShift)) { |
Thiago Macieira | 54a0e10 | 2015-05-05 21:25:06 -0700 | [diff] [blame] | 156 | case NegativeIntegerType: |
| 157 | it->flags |= CborIteratorFlag_NegativeInteger; |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 158 | type = CborIntegerType; |
Thiago Macieira | 54a0e10 | 2015-05-05 21:25:06 -0700 | [diff] [blame] | 159 | // fall through |
| 160 | case UnsignedIntegerType: |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 161 | case ByteStringType: |
| 162 | case TextStringType: |
| 163 | case ArrayType: |
| 164 | case MapType: |
Thiago Macieira | 54a0e10 | 2015-05-05 21:25:06 -0700 | [diff] [blame] | 165 | case TagType: |
| 166 | break; |
| 167 | |
| 168 | case SimpleTypesType: |
| 169 | switch (descriptor) { |
| 170 | case FalseValue: |
| 171 | it->extra = false; |
Thiago Macieira | 991dd92 | 2015-05-07 11:57:59 -0700 | [diff] [blame] | 172 | type = CborBooleanType; |
| 173 | break; |
| 174 | |
Thiago Macieira | 54a0e10 | 2015-05-05 21:25:06 -0700 | [diff] [blame] | 175 | case TrueValue: |
| 176 | case NullValue: |
| 177 | case UndefinedValue: |
| 178 | case HalfPrecisionFloat: |
| 179 | case SinglePrecisionFloat: |
| 180 | case DoublePrecisionFloat: |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 181 | type = *it->ptr; |
Thiago Macieira | 54a0e10 | 2015-05-05 21:25:06 -0700 | [diff] [blame] | 182 | break; |
| 183 | |
| 184 | case SimpleTypeInNextByte: |
Thiago Macieira | 54a0e10 | 2015-05-05 21:25:06 -0700 | [diff] [blame] | 185 | #ifndef CBOR_PARSER_NO_STRICT_CHECKS |
Thiago Macieira | 991dd92 | 2015-05-07 11:57:59 -0700 | [diff] [blame] | 186 | if ((unsigned char)it->ptr[1] < 32) |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 187 | return CborErrorIllegalSimpleType; |
Thiago Macieira | 54a0e10 | 2015-05-05 21:25:06 -0700 | [diff] [blame] | 188 | #endif |
Thiago Macieira | 991dd92 | 2015-05-07 11:57:59 -0700 | [diff] [blame] | 189 | break; |
| 190 | |
Thiago Macieira | 54a0e10 | 2015-05-05 21:25:06 -0700 | [diff] [blame] | 191 | case 28: |
| 192 | case 29: |
| 193 | case 30: |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 194 | return CborErrorUnknownType; |
Thiago Macieira | 54a0e10 | 2015-05-05 21:25:06 -0700 | [diff] [blame] | 195 | |
| 196 | case Break: |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 197 | return CborErrorUnexpectedBreak; |
Thiago Macieira | 54a0e10 | 2015-05-05 21:25:06 -0700 | [diff] [blame] | 198 | } |
| 199 | break; |
Thiago Macieira | 54a0e10 | 2015-05-05 21:25:06 -0700 | [diff] [blame] | 200 | } |
| 201 | |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 202 | if (unlikely(descriptor > Value64Bit)) |
| 203 | return CborErrorIllegalNumber; |
| 204 | |
| 205 | // no further errors possible |
| 206 | it->type = type; |
| 207 | |
Thiago Macieira | 54a0e10 | 2015-05-05 21:25:06 -0700 | [diff] [blame] | 208 | // try to decode up to 16 bits |
| 209 | if (descriptor < Value8Bit) |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 210 | return CborNoError; |
Thiago Macieira | 54a0e10 | 2015-05-05 21:25:06 -0700 | [diff] [blame] | 211 | |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 212 | if (descriptor == Value8Bit) |
| 213 | it->extra = (uint8_t)it->ptr[1]; |
| 214 | else if (descriptor == Value16Bit) |
Thiago Macieira | 54a0e10 | 2015-05-05 21:25:06 -0700 | [diff] [blame] | 215 | it->extra = get16(it->ptr + 1); |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 216 | else |
| 217 | it->flags |= CborIteratorFlag_IntegerValueTooLarge; // Value32Bit or Value64Bit |
| 218 | return CborNoError; |
| 219 | } |
Thiago Macieira | 54a0e10 | 2015-05-05 21:25:06 -0700 | [diff] [blame] | 220 | |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 221 | static CborError preparse_next_value(CborValue *it) |
| 222 | { |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 223 | if (it->remaining != UINT32_MAX) { |
Thiago Macieira | 11e913f | 2015-05-07 13:01:18 -0700 | [diff] [blame^] | 224 | // don't decrement the item count if the current item is tag: they don't count |
| 225 | if (it->type != CborTagType && !--it->remaining) { |
| 226 | it->type = CborInvalidType; |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 227 | return it->ptr == it->parser->end ? CborNoError : CborErrorGarbageAtEnd; |
| 228 | } |
| 229 | } |
| 230 | return preparse_value(it); |
| 231 | } |
| 232 | |
| 233 | static CborError advance_internal(CborValue *it) |
| 234 | { |
| 235 | uint64_t length; |
| 236 | CborError err = extract_number(it->parser, &it->ptr, &length); |
| 237 | assert(err == CborNoError); |
| 238 | |
| 239 | if (!is_fixed_type(it->type)) { |
| 240 | assert(length == (size_t)length); |
| 241 | it->ptr += length; |
| 242 | } |
| 243 | |
| 244 | if (it->remaining == UINT32_MAX && *it->ptr == (char)BreakByte) { |
| 245 | // end of map or array |
| 246 | it->remaining = 0; |
| 247 | return CborNoError; |
| 248 | } |
| 249 | |
| 250 | return preparse_next_value(it); |
Thiago Macieira | 54a0e10 | 2015-05-05 21:25:06 -0700 | [diff] [blame] | 251 | } |
| 252 | |
Thiago Macieira | 2312efd | 2015-05-06 16:07:48 -0700 | [diff] [blame] | 253 | /** \internal |
| 254 | * |
| 255 | * Decodes the CBOR integer value when it is larger than the 16 bits available |
| 256 | * in value->extra. This function requires that value->flags have the |
| 257 | * CborIteratorFlag_IntegerValueTooLarge flag set. |
| 258 | * |
| 259 | * This function is also used to extract single- and double-precision floating |
| 260 | * point values (SinglePrecisionFloat == Value32Bit and DoublePrecisionFloat == |
| 261 | * Value64Bit). |
| 262 | */ |
Thiago Macieira | 54a0e10 | 2015-05-05 21:25:06 -0700 | [diff] [blame] | 263 | uint64_t _cbor_value_decode_int64_internal(const CborValue *value) |
| 264 | { |
Thiago Macieira | 2312efd | 2015-05-06 16:07:48 -0700 | [diff] [blame] | 265 | assert(value->flags & CborIteratorFlag_IntegerValueTooLarge || |
| 266 | value->type == CborFloatType || value->type == CborDoubleType); |
Thiago Macieira | 54a0e10 | 2015-05-05 21:25:06 -0700 | [diff] [blame] | 267 | if ((*value->ptr & SmallValueMask) == Value32Bit) |
| 268 | return get32(value->ptr + 1); |
| 269 | |
| 270 | assert((*value->ptr & SmallValueMask) == Value64Bit); |
| 271 | return get64(value->ptr + 1); |
| 272 | } |
| 273 | |
| 274 | /** |
| 275 | * Initializes the CBOR parser for parsing \a size bytes beginning at \a |
| 276 | * buffer. Parsing will use flags set in \a flags. The iterator to the first |
| 277 | * element is returned in \a it. |
Thiago Macieira | 2312efd | 2015-05-06 16:07:48 -0700 | [diff] [blame] | 278 | * |
| 279 | * The \a parser structure needs to remain valid throughout the decoding |
| 280 | * process. It is not thread-safe to share one CborParser among multiple |
| 281 | * threads iterating at the same time, but the object can be copied so multiple |
| 282 | * threads can iterate. |
| 283 | * |
| 284 | * ### Write how to determine the end pointer |
| 285 | * ### Write how to do limited-buffer windowed decoding |
Thiago Macieira | 54a0e10 | 2015-05-05 21:25:06 -0700 | [diff] [blame] | 286 | */ |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 287 | CborError cbor_parser_init(const char *buffer, size_t size, int flags, CborParser *parser, CborValue *it) |
Thiago Macieira | 54a0e10 | 2015-05-05 21:25:06 -0700 | [diff] [blame] | 288 | { |
| 289 | memset(parser, 0, sizeof(*parser)); |
| 290 | parser->end = buffer + size; |
Thiago Macieira | 54a0e10 | 2015-05-05 21:25:06 -0700 | [diff] [blame] | 291 | parser->flags = flags; |
| 292 | it->parser = parser; |
| 293 | it->ptr = buffer; |
| 294 | it->remaining = 1; // there's one type altogether, usually an array or map |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 295 | return preparse_value(it); |
Thiago Macieira | 2312efd | 2015-05-06 16:07:48 -0700 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | /** |
| 299 | * Advances the CBOR value \a it by one fixed-size position. Fixed-size types |
| 300 | * are: integers, tags, simple types (including boolean, null and undefined |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 301 | * values) and floating point types. |
Thiago Macieira | 2312efd | 2015-05-06 16:07:48 -0700 | [diff] [blame] | 302 | * |
| 303 | * \sa cbor_value_at_end(), cbor_value_advance(), cbor_value_begin_recurse(), cbor_value_end_recurse() |
| 304 | */ |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 305 | CborError cbor_value_advance_fixed(CborValue *it) |
Thiago Macieira | 54a0e10 | 2015-05-05 21:25:06 -0700 | [diff] [blame] | 306 | { |
Thiago Macieira | 2312efd | 2015-05-06 16:07:48 -0700 | [diff] [blame] | 307 | assert(it->type != CborInvalidType); |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 308 | assert(is_fixed_type(it->type)); |
| 309 | if (!it->remaining) |
| 310 | return CborErrorAdvancePastEOF; |
| 311 | return advance_internal(it); |
Thiago Macieira | 54a0e10 | 2015-05-05 21:25:06 -0700 | [diff] [blame] | 312 | } |
| 313 | |
Thiago Macieira | 2312efd | 2015-05-06 16:07:48 -0700 | [diff] [blame] | 314 | /** |
| 315 | * Advances the CBOR value \a it by one element, skipping over containers. |
| 316 | * Unlike cbor_value_advance_fixed(), this function can be called on a CBOR |
| 317 | * value of any type. However, if the type is a container (map or array) or a |
| 318 | * string with a chunked payload, this function will not run in constant time |
| 319 | * and will recurse into itself (it will run on O(n) time for the number of |
| 320 | * elements or chunks and will use O(n) memory for the number of nested |
| 321 | * containers). |
| 322 | * |
Thiago Macieira | 2312efd | 2015-05-06 16:07:48 -0700 | [diff] [blame] | 323 | * \sa cbor_value_at_end(), cbor_value_advance_fixed(), cbor_value_begin_recurse(), cbor_value_end_recurse() |
| 324 | */ |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 325 | CborError cbor_value_advance(CborValue *it) |
Thiago Macieira | 2312efd | 2015-05-06 16:07:48 -0700 | [diff] [blame] | 326 | { |
| 327 | assert(it->type != CborInvalidType); |
| 328 | if (!it->remaining) |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 329 | return CborErrorAdvancePastEOF; |
Thiago Macieira | 2312efd | 2015-05-06 16:07:48 -0700 | [diff] [blame] | 330 | if (is_fixed_type(it->type)) |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 331 | return advance_internal(it); |
Thiago Macieira | 2312efd | 2015-05-06 16:07:48 -0700 | [diff] [blame] | 332 | |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 333 | if (!cbor_value_is_container(it)) |
| 334 | return cbor_value_copy_string(it, NULL, 0, it); |
| 335 | |
| 336 | // map or array |
| 337 | CborError err; |
| 338 | CborValue recursed; |
| 339 | err = cbor_value_enter_container(it, &recursed); |
| 340 | if (err) |
| 341 | return err; |
| 342 | while (!cbor_value_at_end(&recursed)) { |
| 343 | err = cbor_value_advance(&recursed); |
| 344 | if (err) |
| 345 | return err; |
Thiago Macieira | 2312efd | 2015-05-06 16:07:48 -0700 | [diff] [blame] | 346 | } |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 347 | return cbor_value_leave_container(it, &recursed); |
Thiago Macieira | 2312efd | 2015-05-06 16:07:48 -0700 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | /** |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 351 | * \fn bool cbor_value_is_container(const CborValue *it) |
| 352 | * |
Thiago Macieira | 2312efd | 2015-05-06 16:07:48 -0700 | [diff] [blame] | 353 | * Returns true if the \a it value is a container and requires recursion in |
| 354 | * order to decode (maps and arrays), false otherwise. |
| 355 | */ |
Thiago Macieira | 54a0e10 | 2015-05-05 21:25:06 -0700 | [diff] [blame] | 356 | |
Thiago Macieira | 2312efd | 2015-05-06 16:07:48 -0700 | [diff] [blame] | 357 | /** |
| 358 | * Creates a CborValue iterator pointing to the first element of the container |
| 359 | * represented by \a it and saves it in \a recursed. The \a it container object |
| 360 | * needs to be kept and passed again to cbor_value_leave_container() in order |
| 361 | * to continue iterating past this container. |
| 362 | * |
| 363 | * \sa cbor_value_is_container(), cbor_value_leave_container(), cbor_value_advance() |
| 364 | */ |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 365 | CborError cbor_value_enter_container(const CborValue *it, CborValue *recursed) |
Thiago Macieira | 54a0e10 | 2015-05-05 21:25:06 -0700 | [diff] [blame] | 366 | { |
Thiago Macieira | 2312efd | 2015-05-06 16:07:48 -0700 | [diff] [blame] | 367 | assert(cbor_value_is_container(it)); |
Thiago Macieira | 54a0e10 | 2015-05-05 21:25:06 -0700 | [diff] [blame] | 368 | *recursed = *it; |
| 369 | if (it->flags & CborIteratorFlag_UnknownLength) { |
| 370 | recursed->remaining = UINT32_MAX; |
| 371 | } else { |
| 372 | uint64_t len = _cbor_value_extract_int64_helper(it); |
| 373 | recursed->remaining = len; |
| 374 | if (recursed->remaining != len || len == UINT32_MAX) |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 375 | return CborErrorDataTooLarge; |
Thiago Macieira | 54a0e10 | 2015-05-05 21:25:06 -0700 | [diff] [blame] | 376 | } |
Thiago Macieira | c70169f | 2015-05-06 07:49:44 -0700 | [diff] [blame] | 377 | return advance_internal(recursed); |
| 378 | } |
| 379 | |
Thiago Macieira | 2312efd | 2015-05-06 16:07:48 -0700 | [diff] [blame] | 380 | /** |
| 381 | * Updates \a it to point to the next element after the container. The \a |
| 382 | * recursed object needs to point to the last element of the container. |
| 383 | * |
| 384 | * \sa cbor_value_enter_container(), cbor_value_at_end() |
| 385 | */ |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 386 | CborError cbor_value_leave_container(CborValue *it, const CborValue *recursed) |
Thiago Macieira | c70169f | 2015-05-06 07:49:44 -0700 | [diff] [blame] | 387 | { |
Thiago Macieira | 2312efd | 2015-05-06 16:07:48 -0700 | [diff] [blame] | 388 | assert(cbor_value_is_container(it)); |
Thiago Macieira | c70169f | 2015-05-06 07:49:44 -0700 | [diff] [blame] | 389 | assert(cbor_value_at_end(recursed)); |
| 390 | it->ptr = recursed->ptr; |
| 391 | return advance_internal(it); |
| 392 | } |
| 393 | |
Thiago Macieira | 2312efd | 2015-05-06 16:07:48 -0700 | [diff] [blame] | 394 | /** |
| 395 | * Calculates the length of the string in \a value and stores the result in \a |
| 396 | * len. This function is different from cbor_value_get_string_length() in that |
| 397 | * it calculates the length even for strings sent in chunks. For that reason, |
| 398 | * this function may not run in constant time (it will run in O(n) time on the |
| 399 | * number of chunks). |
| 400 | * |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 401 | * \note On 32-bit platforms, this function will return error condition of \ref |
| 402 | * CborErrorDataTooLarge if the stream indicates a length that is too big to |
| 403 | * fit in 32-bit. |
Thiago Macieira | 2312efd | 2015-05-06 16:07:48 -0700 | [diff] [blame] | 404 | * |
| 405 | * \sa cbor_value_get_string_length(), cbor_value_copy_string(), cbor_value_is_length_known() |
| 406 | */ |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 407 | CborError cbor_value_calculate_string_length(const CborValue *value, size_t *len) |
Thiago Macieira | 2312efd | 2015-05-06 16:07:48 -0700 | [diff] [blame] | 408 | { |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 409 | return cbor_value_copy_string(value, NULL, len, NULL); |
Thiago Macieira | c70169f | 2015-05-06 07:49:44 -0700 | [diff] [blame] | 410 | } |
| 411 | |
Thiago Macieira | 2312efd | 2015-05-06 16:07:48 -0700 | [diff] [blame] | 412 | /** |
| 413 | * Allocates memory for the string pointed by \a value and copies it into this |
| 414 | * buffer. The pointer to the buffer is stored in \a buffer and the number of |
| 415 | * bytes copied is stored in \a len (those variables must not be NULL). |
| 416 | * |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 417 | * If \c malloc returns a NULL pointer, this function will return error |
| 418 | * condition \ref CborErrorOutOfMemory. |
Thiago Macieira | 2312efd | 2015-05-06 16:07:48 -0700 | [diff] [blame] | 419 | * |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 420 | * On success, \c{*buffer} will contain a valid pointer that must be freed by |
| 421 | * calling \c{free()}. This is the case even for zero-length strings. |
Thiago Macieira | 2312efd | 2015-05-06 16:07:48 -0700 | [diff] [blame] | 422 | * |
| 423 | * The \a next pointer, if not null, will be updated to point to the next item |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 424 | * after this string. If \a value points to the last item, then \a next will be |
| 425 | * invalid. |
Thiago Macieira | 2312efd | 2015-05-06 16:07:48 -0700 | [diff] [blame] | 426 | * |
| 427 | * \note This function does not perform UTF-8 validation on the incoming text |
| 428 | * string. |
| 429 | * |
| 430 | * \sa cbor_value_copy_string() |
| 431 | */ |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 432 | CborError cbor_value_dup_string(const CborValue *value, char **buffer, size_t *buflen, CborValue *next) |
Thiago Macieira | c70169f | 2015-05-06 07:49:44 -0700 | [diff] [blame] | 433 | { |
Thiago Macieira | 2312efd | 2015-05-06 16:07:48 -0700 | [diff] [blame] | 434 | assert(buffer); |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 435 | assert(buflen); |
| 436 | CborError err = cbor_value_calculate_string_length(value, buflen); |
| 437 | if (err) |
| 438 | return err; |
Thiago Macieira | c70169f | 2015-05-06 07:49:44 -0700 | [diff] [blame] | 439 | |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 440 | ++*buflen; |
| 441 | *buffer = malloc(*buflen); |
Thiago Macieira | 2312efd | 2015-05-06 16:07:48 -0700 | [diff] [blame] | 442 | if (!*buffer) { |
Thiago Macieira | c70169f | 2015-05-06 07:49:44 -0700 | [diff] [blame] | 443 | // out of memory |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 444 | return CborErrorOutOfMemory; |
Thiago Macieira | c70169f | 2015-05-06 07:49:44 -0700 | [diff] [blame] | 445 | } |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 446 | err = cbor_value_copy_string(value, *buffer, buflen, next); |
| 447 | if (err) { |
Thiago Macieira | 2312efd | 2015-05-06 16:07:48 -0700 | [diff] [blame] | 448 | free(*buffer); |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 449 | return err; |
Thiago Macieira | c70169f | 2015-05-06 07:49:44 -0700 | [diff] [blame] | 450 | } |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 451 | return CborNoError; |
Thiago Macieira | 2312efd | 2015-05-06 16:07:48 -0700 | [diff] [blame] | 452 | } |
| 453 | |
| 454 | /** |
| 455 | * Copies the string pointed by \a value into the buffer provided at \a buffer |
| 456 | * of \a buflen bytes. If \a buffer is a NULL pointer, this function will not |
| 457 | * copy anything and will only update the \a next value. |
| 458 | * |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 459 | * If the provided buffer length was too small, this function returns an error |
| 460 | * condition of \ref CborErrorOutOfMemory. If you need to calculate the length |
| 461 | * of the string in order to preallocate a buffer, use |
Thiago Macieira | 2312efd | 2015-05-06 16:07:48 -0700 | [diff] [blame] | 462 | * cbor_value_calculate_string_length(). |
| 463 | * |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 464 | * On success, this function sets the number of bytes copied to \c{*buflen}. If |
| 465 | * the buffer is large enough, this function will insert a null byte after the |
| 466 | * last copied byte, to facilitate manipulation of text strings. That byte is |
| 467 | * not included in the returned value of \c{*buflen}. |
Thiago Macieira | 2312efd | 2015-05-06 16:07:48 -0700 | [diff] [blame] | 468 | * |
| 469 | * The \a next pointer, if not null, will be updated to point to the next item |
| 470 | * after this string. If \a value points to the last item, then \a next will be |
| 471 | * invalid. |
| 472 | * |
| 473 | * \note This function does not perform UTF-8 validation on the incoming text |
| 474 | * string. |
| 475 | * |
| 476 | * \sa cbor_value_dup_string(), cbor_value_get_string_length(), cbor_value_calculate_string_length() |
| 477 | */ |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 478 | CborError cbor_value_copy_string(const CborValue *value, char *buffer, |
| 479 | size_t *buflen, CborValue *next) |
Thiago Macieira | 2312efd | 2015-05-06 16:07:48 -0700 | [diff] [blame] | 480 | { |
| 481 | assert(cbor_value_is_byte_string(value) || cbor_value_is_text_string(value)); |
| 482 | |
| 483 | size_t total; |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 484 | CborError err; |
Thiago Macieira | 2312efd | 2015-05-06 16:07:48 -0700 | [diff] [blame] | 485 | const char *ptr = value->ptr; |
| 486 | if (cbor_value_is_length_known(value)) { |
| 487 | // easy case: fixed length |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 488 | err = extract_length(value->parser, &ptr, &total); |
| 489 | if (err) |
| 490 | return err; |
Thiago Macieira | 2312efd | 2015-05-06 16:07:48 -0700 | [diff] [blame] | 491 | if (buffer) { |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 492 | if (*buflen < total) |
| 493 | return CborErrorOutOfMemory; |
Thiago Macieira | 2312efd | 2015-05-06 16:07:48 -0700 | [diff] [blame] | 494 | memcpy(buffer, ptr, total); |
| 495 | ptr += total; |
| 496 | } |
| 497 | } else { |
| 498 | // chunked |
| 499 | ++ptr; |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 500 | total = 0; |
Thiago Macieira | 2312efd | 2015-05-06 16:07:48 -0700 | [diff] [blame] | 501 | while (true) { |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 502 | size_t chunkLen; |
| 503 | size_t newTotal; |
| 504 | |
Thiago Macieira | 2312efd | 2015-05-06 16:07:48 -0700 | [diff] [blame] | 505 | if (ptr == value->parser->end) |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 506 | return CborErrorUnexpectedEOF; |
Thiago Macieira | 2312efd | 2015-05-06 16:07:48 -0700 | [diff] [blame] | 507 | |
| 508 | if (*ptr == (char)BreakByte) { |
| 509 | ++ptr; |
| 510 | break; |
| 511 | } |
| 512 | |
| 513 | // is this the right type? |
| 514 | if ((*ptr & MajorTypeMask) != value->type) |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 515 | return CborErrorIllegalType; |
Thiago Macieira | 2312efd | 2015-05-06 16:07:48 -0700 | [diff] [blame] | 516 | |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 517 | err = extract_length(value->parser, &ptr, &chunkLen); |
| 518 | if (err) |
| 519 | return err; |
Thiago Macieira | 2312efd | 2015-05-06 16:07:48 -0700 | [diff] [blame] | 520 | |
Thiago Macieira | 2312efd | 2015-05-06 16:07:48 -0700 | [diff] [blame] | 521 | if (unlikely(!add_check_overflow(total, chunkLen, &newTotal))) |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 522 | return CborErrorDataTooLarge; |
Thiago Macieira | 2312efd | 2015-05-06 16:07:48 -0700 | [diff] [blame] | 523 | |
| 524 | if (buffer) { |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 525 | if (*buflen < newTotal) |
| 526 | return CborErrorOutOfMemory; |
Thiago Macieira | 2312efd | 2015-05-06 16:07:48 -0700 | [diff] [blame] | 527 | memcpy(buffer + total, ptr, chunkLen); |
| 528 | } |
| 529 | ptr += chunkLen; |
| 530 | total = newTotal; |
| 531 | } |
| 532 | } |
| 533 | |
| 534 | // is there enough room for the ending NUL byte? |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 535 | if (buffer && *buflen > total) |
Thiago Macieira | 2312efd | 2015-05-06 16:07:48 -0700 | [diff] [blame] | 536 | buffer[total] = '\0'; |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 537 | *buflen = total; |
Thiago Macieira | 2312efd | 2015-05-06 16:07:48 -0700 | [diff] [blame] | 538 | |
| 539 | if (next) { |
| 540 | *next = *value; |
| 541 | next->ptr = ptr; |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 542 | err = preparse_next_value(next); |
| 543 | if (err) |
| 544 | return err; |
Thiago Macieira | 2312efd | 2015-05-06 16:07:48 -0700 | [diff] [blame] | 545 | } |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 546 | return CborNoError; |
Thiago Macieira | c70169f | 2015-05-06 07:49:44 -0700 | [diff] [blame] | 547 | } |
| 548 | |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 549 | CborError cbor_value_get_half_float(const CborValue *value, void *result) |
Thiago Macieira | c70169f | 2015-05-06 07:49:44 -0700 | [diff] [blame] | 550 | { |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 551 | assert(value->type == CborHalfFloatType); |
Thiago Macieira | c70169f | 2015-05-06 07:49:44 -0700 | [diff] [blame] | 552 | |
| 553 | // size has been computed already |
| 554 | uint16_t v = get16(value->ptr + 1); |
| 555 | memcpy(result, &v, sizeof(v)); |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 556 | return CborNoError; |
Thiago Macieira | 54a0e10 | 2015-05-05 21:25:06 -0700 | [diff] [blame] | 557 | } |