Thiago Macieira | 54a0e10 | 2015-05-05 21:25:06 -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 | 54a0e10 | 2015-05-05 21:25:06 -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 |
Otavio Pontes | e2d5dd5 | 2016-07-08 09:49:38 -0300 | [diff] [blame] | 26 | #define _DEFAULT_SOURCE 1 |
Thiago Macieira | 86c8186 | 2016-08-04 13:56:48 -0700 | [diff] [blame] | 27 | #ifndef __STDC_LIMIT_MACROS |
| 28 | # define __STDC_LIMIT_MACROS 1 |
| 29 | #endif |
| 30 | |
Thiago Macieira | 54a0e10 | 2015-05-05 21:25:06 -0700 | [diff] [blame] | 31 | #include "cbor.h" |
Thiago Macieira | cf3116e | 2017-03-04 23:36:23 -0600 | [diff] [blame] | 32 | #include "cborinternal_p.h" |
Thiago Macieira | 54a0e10 | 2015-05-05 21:25:06 -0700 | [diff] [blame] | 33 | #include "compilersupport_p.h" |
| 34 | |
| 35 | #include <assert.h> |
Thiago Macieira | 54a0e10 | 2015-05-05 21:25:06 -0700 | [diff] [blame] | 36 | #include <string.h> |
| 37 | |
Thiago Macieira | 8f3fb78 | 2015-06-16 16:27:01 -0700 | [diff] [blame] | 38 | #include "assert_p.h" /* Always include last */ |
| 39 | |
Thiago Macieira | 4a99af9 | 2015-05-12 10:41:45 +0900 | [diff] [blame] | 40 | #ifndef CBOR_PARSER_MAX_RECURSIONS |
| 41 | # define CBOR_PARSER_MAX_RECURSIONS 1024 |
| 42 | #endif |
| 43 | |
Thiago Macieira | 54a0e10 | 2015-05-05 21:25:06 -0700 | [diff] [blame] | 44 | /** |
Thiago Macieira | 46a818e | 2015-10-08 15:13:05 +0200 | [diff] [blame] | 45 | * \defgroup CborParsing Parsing CBOR streams |
| 46 | * \brief Group of functions used to parse CBOR streams. |
Thiago Macieira | 54a0e10 | 2015-05-05 21:25:06 -0700 | [diff] [blame] | 47 | * |
Thiago Macieira | 46a818e | 2015-10-08 15:13:05 +0200 | [diff] [blame] | 48 | * TinyCBOR provides functions for pull-based stream parsing of a CBOR-encoded |
| 49 | * payload. The main data type for the parsing is a CborValue, which behaves |
| 50 | * like an iterator and can be used to extract the encoded data. It is first |
| 51 | * initialized with a call to cbor_parser_init() and is usually used to extract |
| 52 | * exactly one item, most often an array or map. |
Thiago Macieira | 54a0e10 | 2015-05-05 21:25:06 -0700 | [diff] [blame] | 53 | * |
Thiago Macieira | 46a818e | 2015-10-08 15:13:05 +0200 | [diff] [blame] | 54 | * Nested CborValue objects can be parsed using cbor_value_enter_container(). |
| 55 | * Each call to cbor_value_enter_container() must be matched by a call to |
| 56 | * cbor_value_leave_container(), with the exact same parameters. |
Thiago Macieira | 54a0e10 | 2015-05-05 21:25:06 -0700 | [diff] [blame] | 57 | * |
Thiago Macieira | 46a818e | 2015-10-08 15:13:05 +0200 | [diff] [blame] | 58 | * The example below initializes a CborParser object, begins the parsing with a |
| 59 | * CborValue and decodes a single integer: |
| 60 | * |
| 61 | * \code |
| 62 | * int extract_int(const uint8_t *buffer, size_t len) |
| 63 | * { |
| 64 | * CborParser parser; |
| 65 | * CborValue value; |
| 66 | * int result; |
| 67 | * cbor_parser_init(buffer, len, 0, &buffer, &value); |
| 68 | * cbor_value_get_int(&value, &result); |
| 69 | * return result; |
| 70 | * } |
| 71 | * \endcode |
| 72 | * |
| 73 | * The code above does no error checking, which means it assumes the data comes |
| 74 | * from a source trusted to send one properly-encoded integer. The following |
| 75 | * example does the exact same operation, but includes error parsing and |
| 76 | * returns 0 on parsing failure: |
| 77 | * |
| 78 | * \code |
| 79 | * int extract_int(const uint8_t *buffer, size_t len) |
| 80 | * { |
| 81 | * CborParser parser; |
| 82 | * CborValue value; |
| 83 | * int result; |
| 84 | * if (cbor_parser_init(buffer, len, 0, &buffer, &value) != CborNoError) |
| 85 | * return 0; |
| 86 | * if (!cbor_value_is_integer(&value) || |
| 87 | * cbor_value_get_int(&value, &result) != CborNoError) |
| 88 | * return 0; |
| 89 | * return result; |
| 90 | * } |
| 91 | * \endcode |
| 92 | * |
| 93 | * Note, in the example above, that one can't distinguish a parsing failure |
| 94 | * from an encoded value of zero. Reporting a parsing error is left as an |
| 95 | * exercise to the reader. |
| 96 | * |
| 97 | * The code above does not execute a range-check either: it is possible that |
| 98 | * the value decoded from the CBOR stream encodes a number larger than what can |
| 99 | * be represented in a variable of type \c{int}. If detecting that case is |
| 100 | * important, the code should call cbor_value_get_int_checked() instead. |
| 101 | * |
| 102 | * <h3 class="groupheader">Memory and parsing constraints</h3> |
| 103 | * |
| 104 | * TinyCBOR is designed to run with little memory and with minimal overhead. |
| 105 | * Except where otherwise noted, the parser functions always run on constant |
| 106 | * time (O(1)), do not recurse and never allocate memory (thus, stack usage is |
| 107 | * bounded and is O(1)). |
| 108 | * |
| 109 | * <h3 class="groupheader">Error handling and preconditions</h3> |
| 110 | * |
| 111 | * All functions operating on a CborValue return a CborError condition, with |
| 112 | * CborNoError standing for the normal situation in which no parsing error |
| 113 | * occurred. All functions may return parsing errors in case the stream cannot |
| 114 | * be decoded properly, be it due to corrupted data or due to reaching the end |
| 115 | * of the input buffer. |
| 116 | * |
| 117 | * Error conditions must not be ignored. All decoder functions have undefined |
| 118 | * behavior if called after an error has been reported, and may crash. |
| 119 | * |
| 120 | * Some functions are also documented to have preconditions, like |
| 121 | * cbor_value_get_int() requiring that the input be an integral value. |
| 122 | * Violation of preconditions also results in undefined behavior and the |
| 123 | * program may crash. |
| 124 | */ |
| 125 | |
| 126 | /** |
| 127 | * \addtogroup CborParsing |
| 128 | * @{ |
| 129 | */ |
| 130 | |
| 131 | /** |
| 132 | * \struct CborValue |
| 133 | * |
| 134 | * This type contains one value parsed from the CBOR stream. Each CborValue |
| 135 | * behaves as an iterator in a StAX-style parser. |
| 136 | * |
| 137 | * \if privatedocs |
Thiago Macieira | 54a0e10 | 2015-05-05 21:25:06 -0700 | [diff] [blame] | 138 | * Implementation details: the CborValue contains these fields: |
| 139 | * \list |
| 140 | * \li ptr: pointer to the actual data |
| 141 | * \li flags: flags from the decoder |
Thiago Macieira | 2312efd | 2015-05-06 16:07:48 -0700 | [diff] [blame] | 142 | * \li extra: partially decoded integer value (0, 1 or 2 bytes) |
Thiago Macieira | 54a0e10 | 2015-05-05 21:25:06 -0700 | [diff] [blame] | 143 | * \li remaining: remaining items in this collection after this item or UINT32_MAX if length is unknown |
| 144 | * \endlist |
Thiago Macieira | 46a818e | 2015-10-08 15:13:05 +0200 | [diff] [blame] | 145 | * \endif |
Thiago Macieira | 54a0e10 | 2015-05-05 21:25:06 -0700 | [diff] [blame] | 146 | */ |
| 147 | |
Thiago Macieira | 2c22d71 | 2017-03-05 00:17:35 -0600 | [diff] [blame^] | 148 | static inline uint16_t get16(const uint8_t *ptr) |
| 149 | { |
| 150 | uint16_t result; |
| 151 | memcpy(&result, ptr, sizeof(result)); |
| 152 | return cbor_ntohs(result); |
| 153 | } |
| 154 | |
| 155 | static inline uint32_t get32(const uint8_t *ptr) |
| 156 | { |
| 157 | uint32_t result; |
| 158 | memcpy(&result, ptr, sizeof(result)); |
| 159 | return cbor_ntohl(result); |
| 160 | } |
| 161 | |
| 162 | static inline uint64_t get64(const uint8_t *ptr) |
| 163 | { |
| 164 | uint64_t result; |
| 165 | memcpy(&result, ptr, sizeof(result)); |
| 166 | return cbor_ntohll(result); |
| 167 | } |
| 168 | |
| 169 | CborError _cbor_value_extract_number(const uint8_t **ptr, const uint8_t *end, uint64_t *len) |
| 170 | { |
| 171 | uint8_t additional_information = **ptr & SmallValueMask; |
| 172 | ++*ptr; |
| 173 | if (additional_information < Value8Bit) { |
| 174 | *len = additional_information; |
| 175 | return CborNoError; |
| 176 | } |
| 177 | if (unlikely(additional_information > Value64Bit)) |
| 178 | return CborErrorIllegalNumber; |
| 179 | |
| 180 | size_t bytesNeeded = (size_t)(1 << (additional_information - Value8Bit)); |
| 181 | if (unlikely(bytesNeeded > (size_t)(end - *ptr))) { |
| 182 | return CborErrorUnexpectedEOF; |
| 183 | } else if (bytesNeeded == 1) { |
| 184 | *len = (uint8_t)(*ptr)[0]; |
| 185 | } else if (bytesNeeded == 2) { |
| 186 | *len = get16(*ptr); |
| 187 | } else if (bytesNeeded == 4) { |
| 188 | *len = get32(*ptr); |
| 189 | } else { |
| 190 | *len = get64(*ptr); |
| 191 | } |
| 192 | *ptr += bytesNeeded; |
| 193 | return CborNoError; |
| 194 | } |
| 195 | |
Thiago Macieira | f5cb94b | 2015-06-16 16:10:49 -0700 | [diff] [blame] | 196 | static CborError extract_length(const CborParser *parser, const uint8_t **ptr, size_t *len) |
Thiago Macieira | 54a0e10 | 2015-05-05 21:25:06 -0700 | [diff] [blame] | 197 | { |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 198 | uint64_t v; |
Thiago Macieira | 2c22d71 | 2017-03-05 00:17:35 -0600 | [diff] [blame^] | 199 | CborError err = _cbor_value_extract_number(ptr, parser->end, &v); |
Mike Colagrosso | 629d5b7 | 2016-02-24 15:12:34 -0700 | [diff] [blame] | 200 | if (err) { |
| 201 | *len = 0; |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 202 | return err; |
Mike Colagrosso | 629d5b7 | 2016-02-24 15:12:34 -0700 | [diff] [blame] | 203 | } |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 204 | |
alradmsft | 9ba4791 | 2016-10-11 17:56:15 -0700 | [diff] [blame] | 205 | *len = (size_t)v; |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 206 | if (v != *len) |
| 207 | return CborErrorDataTooLarge; |
| 208 | return CborNoError; |
| 209 | } |
| 210 | |
| 211 | static bool is_fixed_type(uint8_t type) |
| 212 | { |
| 213 | return type != CborTextStringType && type != CborByteStringType && type != CborArrayType && |
| 214 | type != CborMapType; |
| 215 | } |
| 216 | |
| 217 | static CborError preparse_value(CborValue *it) |
| 218 | { |
| 219 | const CborParser *parser = it->parser; |
Thiago Macieira | 11e913f | 2015-05-07 13:01:18 -0700 | [diff] [blame] | 220 | it->type = CborInvalidType; |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 221 | |
Thiago Macieira | dbc0129 | 2016-06-06 17:02:25 -0700 | [diff] [blame] | 222 | /* are we at the end? */ |
Thiago Macieira | 54a0e10 | 2015-05-05 21:25:06 -0700 | [diff] [blame] | 223 | if (it->ptr == parser->end) |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 224 | return CborErrorUnexpectedEOF; |
Thiago Macieira | 54a0e10 | 2015-05-05 21:25:06 -0700 | [diff] [blame] | 225 | |
| 226 | uint8_t descriptor = *it->ptr; |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 227 | uint8_t type = descriptor & MajorTypeMask; |
Thiago Macieira | 851c481 | 2015-05-08 15:23:20 -0700 | [diff] [blame] | 228 | it->type = type; |
Thiago Macieira | 54a0e10 | 2015-05-05 21:25:06 -0700 | [diff] [blame] | 229 | it->flags = 0; |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 230 | it->extra = (descriptor &= SmallValueMask); |
| 231 | |
Thiago Macieira | 56d9983 | 2015-05-07 14:34:27 -0700 | [diff] [blame] | 232 | if (descriptor > Value64Bit) { |
| 233 | if (unlikely(descriptor != IndefiniteLength)) |
Thiago Macieira | 3f76f63 | 2015-05-12 10:10:09 +0900 | [diff] [blame] | 234 | return type == CborSimpleType ? CborErrorUnknownType : CborErrorIllegalNumber; |
Thiago Macieira | 56d9983 | 2015-05-07 14:34:27 -0700 | [diff] [blame] | 235 | if (likely(!is_fixed_type(type))) { |
Thiago Macieira | dbc0129 | 2016-06-06 17:02:25 -0700 | [diff] [blame] | 236 | /* special case */ |
Thiago Macieira | 56d9983 | 2015-05-07 14:34:27 -0700 | [diff] [blame] | 237 | it->flags |= CborIteratorFlag_UnknownLength; |
| 238 | it->type = type; |
| 239 | return CborNoError; |
| 240 | } |
| 241 | return type == CborSimpleType ? CborErrorUnexpectedBreak : CborErrorIllegalNumber; |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 242 | } |
Thiago Macieira | 54a0e10 | 2015-05-05 21:25:06 -0700 | [diff] [blame] | 243 | |
Thiago Macieira | c70169f | 2015-05-06 07:49:44 -0700 | [diff] [blame] | 244 | size_t bytesNeeded = descriptor < Value8Bit ? 0 : (1 << (descriptor - Value8Bit)); |
Thiago Macieira | 63abed9 | 2015-10-28 17:01:14 -0700 | [diff] [blame] | 245 | if (bytesNeeded + 1 > (size_t)(parser->end - it->ptr)) |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 246 | return CborErrorUnexpectedEOF; |
Thiago Macieira | c70169f | 2015-05-06 07:49:44 -0700 | [diff] [blame] | 247 | |
Thiago Macieira | 851c481 | 2015-05-08 15:23:20 -0700 | [diff] [blame] | 248 | uint8_t majortype = type >> MajorTypeShift; |
| 249 | if (majortype == NegativeIntegerType) { |
Thiago Macieira | 54a0e10 | 2015-05-05 21:25:06 -0700 | [diff] [blame] | 250 | it->flags |= CborIteratorFlag_NegativeInteger; |
Thiago Macieira | 851c481 | 2015-05-08 15:23:20 -0700 | [diff] [blame] | 251 | it->type = CborIntegerType; |
| 252 | } else if (majortype == SimpleTypesType) { |
Thiago Macieira | 54a0e10 | 2015-05-05 21:25:06 -0700 | [diff] [blame] | 253 | switch (descriptor) { |
| 254 | case FalseValue: |
| 255 | it->extra = false; |
Thiago Macieira | 851c481 | 2015-05-08 15:23:20 -0700 | [diff] [blame] | 256 | it->type = CborBooleanType; |
Thiago Macieira | 991dd92 | 2015-05-07 11:57:59 -0700 | [diff] [blame] | 257 | break; |
| 258 | |
Thiago Macieira | 851c481 | 2015-05-08 15:23:20 -0700 | [diff] [blame] | 259 | case SinglePrecisionFloat: |
| 260 | case DoublePrecisionFloat: |
| 261 | it->flags |= CborIteratorFlag_IntegerValueTooLarge; |
Thiago Macieira | dbc0129 | 2016-06-06 17:02:25 -0700 | [diff] [blame] | 262 | /* fall through */ |
Thiago Macieira | 54a0e10 | 2015-05-05 21:25:06 -0700 | [diff] [blame] | 263 | case TrueValue: |
| 264 | case NullValue: |
| 265 | case UndefinedValue: |
| 266 | case HalfPrecisionFloat: |
Thiago Macieira | 851c481 | 2015-05-08 15:23:20 -0700 | [diff] [blame] | 267 | it->type = *it->ptr; |
Thiago Macieira | 54a0e10 | 2015-05-05 21:25:06 -0700 | [diff] [blame] | 268 | break; |
| 269 | |
| 270 | case SimpleTypeInNextByte: |
Thiago Macieira | 851c481 | 2015-05-08 15:23:20 -0700 | [diff] [blame] | 271 | it->extra = (uint8_t)it->ptr[1]; |
Thiago Macieira | 54a0e10 | 2015-05-05 21:25:06 -0700 | [diff] [blame] | 272 | #ifndef CBOR_PARSER_NO_STRICT_CHECKS |
Thiago Macieira | 851c481 | 2015-05-08 15:23:20 -0700 | [diff] [blame] | 273 | if (unlikely(it->extra < 32)) { |
| 274 | it->type = CborInvalidType; |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 275 | return CborErrorIllegalSimpleType; |
Thiago Macieira | 851c481 | 2015-05-08 15:23:20 -0700 | [diff] [blame] | 276 | } |
Thiago Macieira | 54a0e10 | 2015-05-05 21:25:06 -0700 | [diff] [blame] | 277 | #endif |
Thiago Macieira | 991dd92 | 2015-05-07 11:57:59 -0700 | [diff] [blame] | 278 | break; |
| 279 | |
Thiago Macieira | 54a0e10 | 2015-05-05 21:25:06 -0700 | [diff] [blame] | 280 | case 28: |
| 281 | case 29: |
| 282 | case 30: |
Thiago Macieira | 54a0e10 | 2015-05-05 21:25:06 -0700 | [diff] [blame] | 283 | case Break: |
Thiago Macieira | dbc0129 | 2016-06-06 17:02:25 -0700 | [diff] [blame] | 284 | assert(false); /* these conditions can't be reached */ |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 285 | return CborErrorUnexpectedBreak; |
Thiago Macieira | 54a0e10 | 2015-05-05 21:25:06 -0700 | [diff] [blame] | 286 | } |
Thiago Macieira | 851c481 | 2015-05-08 15:23:20 -0700 | [diff] [blame] | 287 | return CborNoError; |
Thiago Macieira | 54a0e10 | 2015-05-05 21:25:06 -0700 | [diff] [blame] | 288 | } |
| 289 | |
Thiago Macieira | dbc0129 | 2016-06-06 17:02:25 -0700 | [diff] [blame] | 290 | /* try to decode up to 16 bits */ |
Thiago Macieira | 54a0e10 | 2015-05-05 21:25:06 -0700 | [diff] [blame] | 291 | if (descriptor < Value8Bit) |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 292 | return CborNoError; |
Thiago Macieira | 54a0e10 | 2015-05-05 21:25:06 -0700 | [diff] [blame] | 293 | |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 294 | if (descriptor == Value8Bit) |
| 295 | it->extra = (uint8_t)it->ptr[1]; |
| 296 | else if (descriptor == Value16Bit) |
Thiago Macieira | 54a0e10 | 2015-05-05 21:25:06 -0700 | [diff] [blame] | 297 | it->extra = get16(it->ptr + 1); |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 298 | else |
Thiago Macieira | dbc0129 | 2016-06-06 17:02:25 -0700 | [diff] [blame] | 299 | it->flags |= CborIteratorFlag_IntegerValueTooLarge; /* Value32Bit or Value64Bit */ |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 300 | return CborNoError; |
| 301 | } |
Thiago Macieira | 54a0e10 | 2015-05-05 21:25:06 -0700 | [diff] [blame] | 302 | |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 303 | static CborError preparse_next_value(CborValue *it) |
| 304 | { |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 305 | if (it->remaining != UINT32_MAX) { |
Thiago Macieira | dbc0129 | 2016-06-06 17:02:25 -0700 | [diff] [blame] | 306 | /* don't decrement the item count if the current item is tag: they don't count */ |
Thiago Macieira | 11e913f | 2015-05-07 13:01:18 -0700 | [diff] [blame] | 307 | if (it->type != CborTagType && !--it->remaining) { |
| 308 | it->type = CborInvalidType; |
Thiago Macieira | 56d9983 | 2015-05-07 14:34:27 -0700 | [diff] [blame] | 309 | return CborNoError; |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 310 | } |
Thiago Macieira | 5752ce5 | 2015-06-16 12:10:03 -0700 | [diff] [blame] | 311 | } else if (it->remaining == UINT32_MAX && it->ptr != it->parser->end && *it->ptr == (uint8_t)BreakByte) { |
Thiago Macieira | dbc0129 | 2016-06-06 17:02:25 -0700 | [diff] [blame] | 312 | /* end of map or array */ |
Thiago Macieira | 56d9983 | 2015-05-07 14:34:27 -0700 | [diff] [blame] | 313 | ++it->ptr; |
| 314 | it->type = CborInvalidType; |
| 315 | it->remaining = 0; |
| 316 | return CborNoError; |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 317 | } |
Thiago Macieira | 56d9983 | 2015-05-07 14:34:27 -0700 | [diff] [blame] | 318 | |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 319 | return preparse_value(it); |
| 320 | } |
| 321 | |
| 322 | static CborError advance_internal(CborValue *it) |
| 323 | { |
| 324 | uint64_t length; |
Thiago Macieira | 2c22d71 | 2017-03-05 00:17:35 -0600 | [diff] [blame^] | 325 | CborError err = _cbor_value_extract_number(&it->ptr, it->parser->end, &length); |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 326 | assert(err == CborNoError); |
| 327 | |
Thiago Macieira | 56d9983 | 2015-05-07 14:34:27 -0700 | [diff] [blame] | 328 | if (it->type == CborByteStringType || it->type == CborTextStringType) { |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 329 | assert(length == (size_t)length); |
Thiago Macieira | 56d9983 | 2015-05-07 14:34:27 -0700 | [diff] [blame] | 330 | assert((it->flags & CborIteratorFlag_UnknownLength) == 0); |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 331 | it->ptr += length; |
| 332 | } |
| 333 | |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 334 | return preparse_next_value(it); |
Thiago Macieira | 54a0e10 | 2015-05-05 21:25:06 -0700 | [diff] [blame] | 335 | } |
| 336 | |
Thiago Macieira | 2312efd | 2015-05-06 16:07:48 -0700 | [diff] [blame] | 337 | /** \internal |
| 338 | * |
| 339 | * Decodes the CBOR integer value when it is larger than the 16 bits available |
| 340 | * in value->extra. This function requires that value->flags have the |
| 341 | * CborIteratorFlag_IntegerValueTooLarge flag set. |
| 342 | * |
| 343 | * This function is also used to extract single- and double-precision floating |
| 344 | * point values (SinglePrecisionFloat == Value32Bit and DoublePrecisionFloat == |
| 345 | * Value64Bit). |
| 346 | */ |
Thiago Macieira | 54a0e10 | 2015-05-05 21:25:06 -0700 | [diff] [blame] | 347 | uint64_t _cbor_value_decode_int64_internal(const CborValue *value) |
| 348 | { |
Thiago Macieira | 2312efd | 2015-05-06 16:07:48 -0700 | [diff] [blame] | 349 | assert(value->flags & CborIteratorFlag_IntegerValueTooLarge || |
| 350 | value->type == CborFloatType || value->type == CborDoubleType); |
Thiago Macieira | 851c481 | 2015-05-08 15:23:20 -0700 | [diff] [blame] | 351 | |
Thiago Macieira | dbc0129 | 2016-06-06 17:02:25 -0700 | [diff] [blame] | 352 | /* since the additional information can only be Value32Bit or Value64Bit, |
| 353 | * we just need to test for the one bit those two options differ */ |
Thiago Macieira | 851c481 | 2015-05-08 15:23:20 -0700 | [diff] [blame] | 354 | assert((*value->ptr & SmallValueMask) == Value32Bit || (*value->ptr & SmallValueMask) == Value64Bit); |
| 355 | if ((*value->ptr & 1) == (Value32Bit & 1)) |
Thiago Macieira | 54a0e10 | 2015-05-05 21:25:06 -0700 | [diff] [blame] | 356 | return get32(value->ptr + 1); |
| 357 | |
| 358 | assert((*value->ptr & SmallValueMask) == Value64Bit); |
| 359 | return get64(value->ptr + 1); |
| 360 | } |
| 361 | |
| 362 | /** |
| 363 | * Initializes the CBOR parser for parsing \a size bytes beginning at \a |
| 364 | * buffer. Parsing will use flags set in \a flags. The iterator to the first |
| 365 | * element is returned in \a it. |
Thiago Macieira | 2312efd | 2015-05-06 16:07:48 -0700 | [diff] [blame] | 366 | * |
| 367 | * The \a parser structure needs to remain valid throughout the decoding |
| 368 | * process. It is not thread-safe to share one CborParser among multiple |
| 369 | * threads iterating at the same time, but the object can be copied so multiple |
| 370 | * threads can iterate. |
Thiago Macieira | 54a0e10 | 2015-05-05 21:25:06 -0700 | [diff] [blame] | 371 | */ |
Thiago Macieira | 5752ce5 | 2015-06-16 12:10:03 -0700 | [diff] [blame] | 372 | CborError cbor_parser_init(const uint8_t *buffer, size_t size, int flags, CborParser *parser, CborValue *it) |
Thiago Macieira | 54a0e10 | 2015-05-05 21:25:06 -0700 | [diff] [blame] | 373 | { |
| 374 | memset(parser, 0, sizeof(*parser)); |
| 375 | parser->end = buffer + size; |
Thiago Macieira | 54a0e10 | 2015-05-05 21:25:06 -0700 | [diff] [blame] | 376 | parser->flags = flags; |
| 377 | it->parser = parser; |
| 378 | it->ptr = buffer; |
Thiago Macieira | dbc0129 | 2016-06-06 17:02:25 -0700 | [diff] [blame] | 379 | 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] | 380 | return preparse_value(it); |
Thiago Macieira | 2312efd | 2015-05-06 16:07:48 -0700 | [diff] [blame] | 381 | } |
| 382 | |
| 383 | /** |
Thiago Macieira | 46a818e | 2015-10-08 15:13:05 +0200 | [diff] [blame] | 384 | * \fn bool cbor_value_at_end(const CborValue *it) |
| 385 | * |
| 386 | * Returns true if \a it has reached the end of the iteration, usually when |
Thiago Macieira | 740e29d | 2016-07-07 13:32:47 -0700 | [diff] [blame] | 387 | * advancing after the last item in an array or map. |
Thiago Macieira | 46a818e | 2015-10-08 15:13:05 +0200 | [diff] [blame] | 388 | * |
Thiago Macieira | 740e29d | 2016-07-07 13:32:47 -0700 | [diff] [blame] | 389 | * In the case of the outermost CborValue object, this function returns true |
| 390 | * after decoding a single element. A pointer to the first byte of the |
| 391 | * remaining data (if any) can be obtained with cbor_value_get_next_byte(). |
| 392 | * |
| 393 | * \sa cbor_value_advance(), cbor_value_is_valid(), cbor_value_get_next_byte() |
| 394 | */ |
| 395 | |
| 396 | /** |
| 397 | * \fn const uint8_t *cbor_value_get_next_byte(const CborValue *it) |
| 398 | * |
| 399 | * Returns a pointer to the next byte that would be decoded if this CborValue |
| 400 | * object were advanced. |
| 401 | * |
| 402 | * This function is useful if cbor_value_at_end() returns true for the |
| 403 | * outermost CborValue: the pointer returned is the first byte of the data |
| 404 | * remaining in the buffer, if any. Code can decide whether to begin decoding a |
| 405 | * new CBOR data stream from this point, or parse some other data appended to |
| 406 | * the same buffer. |
| 407 | * |
| 408 | * This function may be used even after a parsing error. If that occurred, |
| 409 | * then this function returns a pointer to where the parsing error occurred. |
| 410 | * Note that the error recovery is not precise and the pointer may not indicate |
| 411 | * the exact byte containing bad data. |
| 412 | * |
| 413 | * \sa cbor_value_at_end() |
Thiago Macieira | 46a818e | 2015-10-08 15:13:05 +0200 | [diff] [blame] | 414 | */ |
| 415 | |
| 416 | /** |
| 417 | * \fn bool cbor_value_is_valid(const CborValue *it) |
| 418 | * |
| 419 | * Returns true if the iterator \a it contains a valid value. Invalid iterators |
| 420 | * happen when iteration reaches the end of a container (see \ref |
| 421 | * cbor_value_at_end()) or when a search function resulted in no matches. |
| 422 | * |
| 423 | * \sa cbor_value_advance(), cbor_valie_at_end(), cbor_value_get_type() |
| 424 | */ |
| 425 | |
| 426 | /** |
Thiago Macieira | 2312efd | 2015-05-06 16:07:48 -0700 | [diff] [blame] | 427 | * Advances the CBOR value \a it by one fixed-size position. Fixed-size types |
| 428 | * are: integers, tags, simple types (including boolean, null and undefined |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 429 | * values) and floating point types. |
Thiago Macieira | 2312efd | 2015-05-06 16:07:48 -0700 | [diff] [blame] | 430 | * |
Thiago Macieira | 46a818e | 2015-10-08 15:13:05 +0200 | [diff] [blame] | 431 | * If the type is not of fixed size, this function has undefined behavior. Code |
| 432 | * must be sure that the current type is one of the fixed-size types before |
| 433 | * calling this function. This function is provided because it can guarantee |
| 434 | * that runs in constant time (O(1)). |
| 435 | * |
| 436 | * If the caller is not able to determine whether the type is fixed or not, code |
| 437 | * can use the cbor_value_advance() function instead. |
| 438 | * |
| 439 | * \sa cbor_value_at_end(), cbor_value_advance(), cbor_value_enter_container(), cbor_value_leave_container() |
Thiago Macieira | 2312efd | 2015-05-06 16:07:48 -0700 | [diff] [blame] | 440 | */ |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 441 | CborError cbor_value_advance_fixed(CborValue *it) |
Thiago Macieira | 54a0e10 | 2015-05-05 21:25:06 -0700 | [diff] [blame] | 442 | { |
Thiago Macieira | 2312efd | 2015-05-06 16:07:48 -0700 | [diff] [blame] | 443 | assert(it->type != CborInvalidType); |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 444 | assert(is_fixed_type(it->type)); |
| 445 | if (!it->remaining) |
| 446 | return CborErrorAdvancePastEOF; |
| 447 | return advance_internal(it); |
Thiago Macieira | 54a0e10 | 2015-05-05 21:25:06 -0700 | [diff] [blame] | 448 | } |
| 449 | |
Thiago Macieira | 4a99af9 | 2015-05-12 10:41:45 +0900 | [diff] [blame] | 450 | static CborError advance_recursive(CborValue *it, int nestingLevel) |
| 451 | { |
| 452 | if (is_fixed_type(it->type)) |
| 453 | return advance_internal(it); |
| 454 | |
| 455 | if (!cbor_value_is_container(it)) { |
| 456 | size_t len = SIZE_MAX; |
Thiago Macieira | ff130bc | 2015-06-19 15:15:33 -0700 | [diff] [blame] | 457 | return _cbor_value_copy_string(it, NULL, &len, it); |
Thiago Macieira | 4a99af9 | 2015-05-12 10:41:45 +0900 | [diff] [blame] | 458 | } |
| 459 | |
Thiago Macieira | dbc0129 | 2016-06-06 17:02:25 -0700 | [diff] [blame] | 460 | /* map or array */ |
Thiago Macieira | 4a99af9 | 2015-05-12 10:41:45 +0900 | [diff] [blame] | 461 | if (nestingLevel == CBOR_PARSER_MAX_RECURSIONS) |
| 462 | return CborErrorNestingTooDeep; |
| 463 | |
| 464 | CborError err; |
| 465 | CborValue recursed; |
| 466 | err = cbor_value_enter_container(it, &recursed); |
| 467 | if (err) |
| 468 | return err; |
| 469 | while (!cbor_value_at_end(&recursed)) { |
| 470 | err = advance_recursive(&recursed, nestingLevel + 1); |
| 471 | if (err) |
| 472 | return err; |
| 473 | } |
| 474 | return cbor_value_leave_container(it, &recursed); |
| 475 | } |
| 476 | |
| 477 | |
Thiago Macieira | 2312efd | 2015-05-06 16:07:48 -0700 | [diff] [blame] | 478 | /** |
| 479 | * Advances the CBOR value \a it by one element, skipping over containers. |
| 480 | * Unlike cbor_value_advance_fixed(), this function can be called on a CBOR |
| 481 | * value of any type. However, if the type is a container (map or array) or a |
| 482 | * string with a chunked payload, this function will not run in constant time |
| 483 | * and will recurse into itself (it will run on O(n) time for the number of |
| 484 | * elements or chunks and will use O(n) memory for the number of nested |
| 485 | * containers). |
| 486 | * |
Thiago Macieira | 46a818e | 2015-10-08 15:13:05 +0200 | [diff] [blame] | 487 | * \sa cbor_value_at_end(), cbor_value_advance_fixed(), cbor_value_enter_container(), cbor_value_leave_container() |
Thiago Macieira | 2312efd | 2015-05-06 16:07:48 -0700 | [diff] [blame] | 488 | */ |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 489 | CborError cbor_value_advance(CborValue *it) |
Thiago Macieira | 2312efd | 2015-05-06 16:07:48 -0700 | [diff] [blame] | 490 | { |
| 491 | assert(it->type != CborInvalidType); |
| 492 | if (!it->remaining) |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 493 | return CborErrorAdvancePastEOF; |
Thiago Macieira | 4a99af9 | 2015-05-12 10:41:45 +0900 | [diff] [blame] | 494 | return advance_recursive(it, 0); |
Thiago Macieira | 2312efd | 2015-05-06 16:07:48 -0700 | [diff] [blame] | 495 | } |
| 496 | |
| 497 | /** |
Thiago Macieira | 46a818e | 2015-10-08 15:13:05 +0200 | [diff] [blame] | 498 | * \fn bool cbor_value_is_tag(const CborValue *value) |
| 499 | * |
| 500 | * Returns true if the iterator \a value is valid and points to a CBOR tag. |
| 501 | * |
| 502 | * \sa cbor_value_get_tag(), cbor_value_skip_tag() |
| 503 | */ |
| 504 | |
| 505 | /** |
| 506 | * \fn CborError cbor_value_get_tag(const CborValue *value, CborTag *result) |
| 507 | * |
| 508 | * Retrieves the CBOR tag value that \a value points to and stores it in \a |
| 509 | * result. If the iterator \a value does not point to a CBOR tag value, the |
| 510 | * behavior is undefined, so checking with \ref cbor_value_get_type or with |
| 511 | * \ref cbor_value_is_tag is recommended. |
| 512 | * |
| 513 | * \sa cbor_value_get_type(), cbor_value_is_valid(), cbor_value_is_tag() |
| 514 | */ |
| 515 | |
| 516 | /** |
Thiago Macieira | c4a73c6 | 2015-05-09 18:14:11 -0700 | [diff] [blame] | 517 | * Advances the CBOR value \a it until it no longer points to a tag. If \a it is |
| 518 | * already not pointing to a tag, then this function returns it unchanged. |
| 519 | * |
Thiago Macieira | 46a818e | 2015-10-08 15:13:05 +0200 | [diff] [blame] | 520 | * This function does not run in constant time: it will run on O(n) for n being |
| 521 | * the number of tags. It does use constant memory (O(1) memory requirements). |
| 522 | * |
Thiago Macieira | c4a73c6 | 2015-05-09 18:14:11 -0700 | [diff] [blame] | 523 | * \sa cbor_value_advance_fixed(), cbor_value_advance() |
| 524 | */ |
| 525 | CborError cbor_value_skip_tag(CborValue *it) |
| 526 | { |
| 527 | while (cbor_value_is_tag(it)) { |
| 528 | CborError err = cbor_value_advance_fixed(it); |
| 529 | if (err) |
| 530 | return err; |
| 531 | } |
| 532 | return CborNoError; |
| 533 | } |
| 534 | |
Thiago Macieira | c4a73c6 | 2015-05-09 18:14:11 -0700 | [diff] [blame] | 535 | /** |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 536 | * \fn bool cbor_value_is_container(const CborValue *it) |
| 537 | * |
Thiago Macieira | 2312efd | 2015-05-06 16:07:48 -0700 | [diff] [blame] | 538 | * Returns true if the \a it value is a container and requires recursion in |
| 539 | * order to decode (maps and arrays), false otherwise. |
| 540 | */ |
Thiago Macieira | 54a0e10 | 2015-05-05 21:25:06 -0700 | [diff] [blame] | 541 | |
Thiago Macieira | 2312efd | 2015-05-06 16:07:48 -0700 | [diff] [blame] | 542 | /** |
| 543 | * Creates a CborValue iterator pointing to the first element of the container |
| 544 | * represented by \a it and saves it in \a recursed. The \a it container object |
| 545 | * needs to be kept and passed again to cbor_value_leave_container() in order |
| 546 | * to continue iterating past this container. |
| 547 | * |
Thiago Macieira | 46a818e | 2015-10-08 15:13:05 +0200 | [diff] [blame] | 548 | * The \a it CborValue iterator must point to a container. |
| 549 | * |
Thiago Macieira | 2312efd | 2015-05-06 16:07:48 -0700 | [diff] [blame] | 550 | * \sa cbor_value_is_container(), cbor_value_leave_container(), cbor_value_advance() |
| 551 | */ |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 552 | CborError cbor_value_enter_container(const CborValue *it, CborValue *recursed) |
Thiago Macieira | 54a0e10 | 2015-05-05 21:25:06 -0700 | [diff] [blame] | 553 | { |
Thiago Macieira | 56d9983 | 2015-05-07 14:34:27 -0700 | [diff] [blame] | 554 | CborError err; |
Thiago Macieira | 2312efd | 2015-05-06 16:07:48 -0700 | [diff] [blame] | 555 | assert(cbor_value_is_container(it)); |
Thiago Macieira | 54a0e10 | 2015-05-05 21:25:06 -0700 | [diff] [blame] | 556 | *recursed = *it; |
Thiago Macieira | 56d9983 | 2015-05-07 14:34:27 -0700 | [diff] [blame] | 557 | |
Thiago Macieira | 54a0e10 | 2015-05-05 21:25:06 -0700 | [diff] [blame] | 558 | if (it->flags & CborIteratorFlag_UnknownLength) { |
| 559 | recursed->remaining = UINT32_MAX; |
Thiago Macieira | 56d9983 | 2015-05-07 14:34:27 -0700 | [diff] [blame] | 560 | ++recursed->ptr; |
| 561 | err = preparse_value(recursed); |
| 562 | if (err != CborErrorUnexpectedBreak) |
| 563 | return err; |
Thiago Macieira | dbc0129 | 2016-06-06 17:02:25 -0700 | [diff] [blame] | 564 | /* actually, break was expected here |
| 565 | * it's just an empty container */ |
Thiago Macieira | 56d9983 | 2015-05-07 14:34:27 -0700 | [diff] [blame] | 566 | ++recursed->ptr; |
Thiago Macieira | 54a0e10 | 2015-05-05 21:25:06 -0700 | [diff] [blame] | 567 | } else { |
Thiago Macieira | 56d9983 | 2015-05-07 14:34:27 -0700 | [diff] [blame] | 568 | uint64_t len; |
Thiago Macieira | 2c22d71 | 2017-03-05 00:17:35 -0600 | [diff] [blame^] | 569 | err = _cbor_value_extract_number(&recursed->ptr, recursed->parser->end, &len); |
Thiago Macieira | 56d9983 | 2015-05-07 14:34:27 -0700 | [diff] [blame] | 570 | assert(err == CborNoError); |
Thiago Macieira | 56d9983 | 2015-05-07 14:34:27 -0700 | [diff] [blame] | 571 | |
Thiago Macieira | e12dfd0 | 2016-06-07 16:29:25 -0700 | [diff] [blame] | 572 | recursed->remaining = (uint32_t)len; |
Thiago Macieira | 3f76f63 | 2015-05-12 10:10:09 +0900 | [diff] [blame] | 573 | if (recursed->remaining != len || len == UINT32_MAX) { |
Thiago Macieira | dbc0129 | 2016-06-06 17:02:25 -0700 | [diff] [blame] | 574 | /* back track the pointer to indicate where the error occurred */ |
Thiago Macieira | 3f76f63 | 2015-05-12 10:10:09 +0900 | [diff] [blame] | 575 | recursed->ptr = it->ptr; |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 576 | return CborErrorDataTooLarge; |
Thiago Macieira | 3f76f63 | 2015-05-12 10:10:09 +0900 | [diff] [blame] | 577 | } |
Thiago Macieira | ce16f05 | 2015-05-07 23:14:25 -0700 | [diff] [blame] | 578 | if (recursed->type == CborMapType) { |
Thiago Macieira | dbc0129 | 2016-06-06 17:02:25 -0700 | [diff] [blame] | 579 | /* maps have keys and values, so we need to multiply by 2 */ |
Thiago Macieira | 3f76f63 | 2015-05-12 10:10:09 +0900 | [diff] [blame] | 580 | if (recursed->remaining > UINT32_MAX / 2) { |
Thiago Macieira | dbc0129 | 2016-06-06 17:02:25 -0700 | [diff] [blame] | 581 | /* back track the pointer to indicate where the error occurred */ |
Thiago Macieira | 3f76f63 | 2015-05-12 10:10:09 +0900 | [diff] [blame] | 582 | recursed->ptr = it->ptr; |
Thiago Macieira | ce16f05 | 2015-05-07 23:14:25 -0700 | [diff] [blame] | 583 | return CborErrorDataTooLarge; |
Thiago Macieira | 3f76f63 | 2015-05-12 10:10:09 +0900 | [diff] [blame] | 584 | } |
Thiago Macieira | ce16f05 | 2015-05-07 23:14:25 -0700 | [diff] [blame] | 585 | recursed->remaining *= 2; |
| 586 | } |
Thiago Macieira | 56d9983 | 2015-05-07 14:34:27 -0700 | [diff] [blame] | 587 | if (len != 0) |
| 588 | return preparse_value(recursed); |
Thiago Macieira | 54a0e10 | 2015-05-05 21:25:06 -0700 | [diff] [blame] | 589 | } |
Thiago Macieira | 56d9983 | 2015-05-07 14:34:27 -0700 | [diff] [blame] | 590 | |
Thiago Macieira | dbc0129 | 2016-06-06 17:02:25 -0700 | [diff] [blame] | 591 | /* the case of the empty container */ |
Thiago Macieira | 56d9983 | 2015-05-07 14:34:27 -0700 | [diff] [blame] | 592 | recursed->type = CborInvalidType; |
| 593 | recursed->remaining = 0; |
| 594 | return CborNoError; |
Thiago Macieira | c70169f | 2015-05-06 07:49:44 -0700 | [diff] [blame] | 595 | } |
| 596 | |
Thiago Macieira | 2312efd | 2015-05-06 16:07:48 -0700 | [diff] [blame] | 597 | /** |
| 598 | * Updates \a it to point to the next element after the container. The \a |
Thiago Macieira | 56d9983 | 2015-05-07 14:34:27 -0700 | [diff] [blame] | 599 | * recursed object needs to point to the element obtained either by advancing |
| 600 | * the last element of the container (via cbor_value_advance(), |
| 601 | * cbor_value_advance_fixed(), a nested cbor_value_leave_container(), or the \c |
| 602 | * next pointer from cbor_value_copy_string() or cbor_value_dup_string()). |
Thiago Macieira | 2312efd | 2015-05-06 16:07:48 -0700 | [diff] [blame] | 603 | * |
Thiago Macieira | 46a818e | 2015-10-08 15:13:05 +0200 | [diff] [blame] | 604 | * The \a it and \a recursed parameters must be the exact same as passed to |
| 605 | * cbor_value_enter_container(). |
| 606 | * |
Thiago Macieira | 2312efd | 2015-05-06 16:07:48 -0700 | [diff] [blame] | 607 | * \sa cbor_value_enter_container(), cbor_value_at_end() |
| 608 | */ |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 609 | CborError cbor_value_leave_container(CborValue *it, const CborValue *recursed) |
Thiago Macieira | c70169f | 2015-05-06 07:49:44 -0700 | [diff] [blame] | 610 | { |
Thiago Macieira | 2312efd | 2015-05-06 16:07:48 -0700 | [diff] [blame] | 611 | assert(cbor_value_is_container(it)); |
Thiago Macieira | 56d9983 | 2015-05-07 14:34:27 -0700 | [diff] [blame] | 612 | assert(recursed->type == CborInvalidType); |
Thiago Macieira | c70169f | 2015-05-06 07:49:44 -0700 | [diff] [blame] | 613 | it->ptr = recursed->ptr; |
Thiago Macieira | 56d9983 | 2015-05-07 14:34:27 -0700 | [diff] [blame] | 614 | return preparse_next_value(it); |
Thiago Macieira | c70169f | 2015-05-06 07:49:44 -0700 | [diff] [blame] | 615 | } |
| 616 | |
Thiago Macieira | 46a818e | 2015-10-08 15:13:05 +0200 | [diff] [blame] | 617 | |
Thiago Macieira | 2312efd | 2015-05-06 16:07:48 -0700 | [diff] [blame] | 618 | /** |
Thiago Macieira | 46a818e | 2015-10-08 15:13:05 +0200 | [diff] [blame] | 619 | * \fn CborType cbor_value_get_type(const CborValue *value) |
| 620 | * |
| 621 | * Returns the type of the CBOR value that the iterator \a value points to. If |
| 622 | * \a value does not point to a valid value, this function returns \ref |
| 623 | * CborInvalidType. |
| 624 | * |
| 625 | * TinyCBOR also provides functions to test directly if a given CborValue object |
| 626 | * is of a given type, like cbor_value_is_text_string() and cbor_value_is_null(). |
| 627 | * |
| 628 | * \sa cbor_value_is_valid() |
| 629 | */ |
| 630 | |
| 631 | /** |
| 632 | * \fn bool cbor_value_is_null(const CborValue *value) |
| 633 | * |
| 634 | * Returns true if the iterator \a value is valid and points to a CBOR null type. |
| 635 | * |
| 636 | * \sa cbor_value_is_valid(), cbor_value_is_undefined() |
| 637 | */ |
| 638 | |
| 639 | /** |
| 640 | * \fn bool cbor_value_is_undefined(const CborValue *value) |
| 641 | * |
| 642 | * Returns true if the iterator \a value is valid and points to a CBOR undefined type. |
| 643 | * |
| 644 | * \sa cbor_value_is_valid(), cbor_value_is_null() |
| 645 | */ |
| 646 | |
| 647 | /** |
| 648 | * \fn bool cbor_value_is_boolean(const CborValue *value) |
| 649 | * |
| 650 | * Returns true if the iterator \a value is valid and points to a CBOR boolean |
| 651 | * type (true or false). |
| 652 | * |
| 653 | * \sa cbor_value_is_valid(), cbor_value_get_boolean() |
| 654 | */ |
| 655 | |
| 656 | /** |
| 657 | * \fn CborError cbor_value_get_boolean(const CborValue *value, bool *result) |
| 658 | * |
| 659 | * Retrieves the boolean value that \a value points to and stores it in \a |
| 660 | * result. If the iterator \a value does not point to a boolean value, the |
| 661 | * behavior is undefined, so checking with \ref cbor_value_get_type or with |
| 662 | * \ref cbor_value_is_boolean is recommended. |
| 663 | * |
| 664 | * \sa cbor_value_get_type(), cbor_value_is_valid(), cbor_value_is_boolean() |
| 665 | */ |
| 666 | |
| 667 | /** |
| 668 | * \fn bool cbor_value_is_simple_type(const CborValue *value) |
| 669 | * |
| 670 | * Returns true if the iterator \a value is valid and points to a CBOR Simple Type |
| 671 | * type (other than true, false, null and undefined). |
| 672 | * |
| 673 | * \sa cbor_value_is_valid(), cbor_value_get_simple_type() |
| 674 | */ |
| 675 | |
| 676 | /** |
| 677 | * \fn CborError cbor_value_get_simple_type(const CborValue *value, uint8_t *result) |
| 678 | * |
| 679 | * Retrieves the CBOR Simple Type value that \a value points to and stores it |
| 680 | * in \a result. If the iterator \a value does not point to a simple_type |
| 681 | * value, the behavior is undefined, so checking with \ref cbor_value_get_type |
| 682 | * or with \ref cbor_value_is_simple_type is recommended. |
| 683 | * |
| 684 | * \sa cbor_value_get_type(), cbor_value_is_valid(), cbor_value_is_simple_type() |
| 685 | */ |
| 686 | |
| 687 | /** |
| 688 | * \fn bool cbor_value_is_integer(const CborValue *value) |
| 689 | * |
| 690 | * Returns true if the iterator \a value is valid and points to a CBOR integer |
| 691 | * type. |
| 692 | * |
| 693 | * \sa cbor_value_is_valid(), cbor_value_get_int, cbor_value_get_int64, cbor_value_get_uint64, cbor_value_get_raw_integer |
| 694 | */ |
| 695 | |
| 696 | /** |
| 697 | * \fn bool cbor_value_is_unsigned_integer(const CborValue *value) |
| 698 | * |
| 699 | * Returns true if the iterator \a value is valid and points to a CBOR unsigned |
| 700 | * integer type (positive values or zero). |
| 701 | * |
| 702 | * \sa cbor_value_is_valid(), cbor_value_get_uint64() |
| 703 | */ |
| 704 | |
| 705 | /** |
| 706 | * \fn bool cbor_value_is_negative_integer(const CborValue *value) |
| 707 | * |
| 708 | * Returns true if the iterator \a value is valid and points to a CBOR negative |
| 709 | * integer type. |
| 710 | * |
| 711 | * \sa cbor_value_is_valid(), cbor_value_get_int, cbor_value_get_int64, cbor_value_get_raw_integer |
| 712 | */ |
| 713 | |
| 714 | /** |
| 715 | * \fn CborError cbor_value_get_int(const CborValue *value, int *result) |
| 716 | * |
| 717 | * Retrieves the CBOR integer value that \a value points to and stores it in \a |
| 718 | * result. If the iterator \a value does not point to an integer value, the |
| 719 | * behavior is undefined, so checking with \ref cbor_value_get_type or with |
| 720 | * \ref cbor_value_is_integer is recommended. |
| 721 | * |
| 722 | * Note that this function does not do range-checking: integral values that do |
| 723 | * not fit in a variable of type \c{int} are silently truncated to fit. Use |
| 724 | * cbor_value_get_int_checked() that is not acceptable. |
| 725 | * |
| 726 | * \sa cbor_value_get_type(), cbor_value_is_valid(), cbor_value_is_integer() |
| 727 | */ |
| 728 | |
| 729 | /** |
| 730 | * \fn CborError cbor_value_get_int64(const CborValue *value, int64_t *result) |
| 731 | * |
| 732 | * Retrieves the CBOR integer value that \a value points to and stores it in \a |
| 733 | * result. If the iterator \a value does not point to an integer value, the |
| 734 | * behavior is undefined, so checking with \ref cbor_value_get_type or with |
| 735 | * \ref cbor_value_is_integer is recommended. |
| 736 | * |
| 737 | * Note that this function does not do range-checking: integral values that do |
| 738 | * not fit in a variable of type \c{int64_t} are silently truncated to fit. Use |
| 739 | * cbor_value_get_int64_checked() that is not acceptable. |
| 740 | * |
| 741 | * \sa cbor_value_get_type(), cbor_value_is_valid(), cbor_value_is_integer() |
| 742 | */ |
| 743 | |
| 744 | /** |
| 745 | * \fn CborError cbor_value_get_uint64(const CborValue *value, uint64_t *result) |
| 746 | * |
| 747 | * Retrieves the CBOR integer value that \a value points to and stores it in \a |
| 748 | * result. If the iterator \a value does not point to an unsigned integer |
| 749 | * value, the behavior is undefined, so checking with \ref cbor_value_get_type |
| 750 | * or with \ref cbor_value_is_unsigned_integer is recommended. |
| 751 | * |
| 752 | * \sa cbor_value_get_type(), cbor_value_is_valid(), cbor_value_is_unsigned_integer() |
| 753 | */ |
| 754 | |
| 755 | /** |
| 756 | * \fn CborError cbor_value_get_raw_integer(const CborValue *value, uint64_t *result) |
| 757 | * |
| 758 | * Retrieves the CBOR integer value that \a value points to and stores it in \a |
| 759 | * result. If the iterator \a value does not point to an integer value, the |
| 760 | * behavior is undefined, so checking with \ref cbor_value_get_type or with |
| 761 | * \ref cbor_value_is_integer is recommended. |
| 762 | * |
| 763 | * This function is provided because CBOR negative integers can assume values |
| 764 | * that cannot be represented with normal 64-bit integer variables. |
| 765 | * |
| 766 | * If the integer is unsigned (that is, if cbor_value_is_unsigned_integer() |
| 767 | * returns true), then \a result will contain the actual value. If the integer |
| 768 | * is negative, then \a result will contain the absolute value of that integer, |
| 769 | * minus one. That is, \c {actual = -result - 1}. On architectures using two's |
| 770 | * complement for representation of negative integers, it is equivalent to say |
| 771 | * that \a result will contain the bitwise negation of the actual value. |
| 772 | * |
| 773 | * \sa cbor_value_get_type(), cbor_value_is_valid(), cbor_value_is_integer() |
| 774 | */ |
| 775 | |
| 776 | /** |
Thiago Macieira | 0f02e79 | 2016-07-07 19:55:08 -0700 | [diff] [blame] | 777 | * Retrieves the CBOR integer value that \a value points to and stores it in \a |
| 778 | * result. If the iterator \a value does not point to an integer value, the |
| 779 | * behavior is undefined, so checking with \ref cbor_value_get_type or with |
| 780 | * \ref cbor_value_is_integer is recommended. |
| 781 | * |
| 782 | * Unlike cbor_value_get_int64(), this function performs a check to see if the |
| 783 | * stored integer fits in \a result without data loss. If the number is outside |
| 784 | * the valid range for the data type, this function returns the recoverable |
| 785 | * error CborErrorDataTooLarge. In that case, use either |
| 786 | * cbor_value_get_uint64() (if the number is positive) or |
| 787 | * cbor_value_get_raw_integer(). |
| 788 | * |
| 789 | * \sa cbor_value_get_type(), cbor_value_is_valid(), cbor_value_is_integer(), cbor_value_get_int64() |
| 790 | */ |
| 791 | CborError cbor_value_get_int64_checked(const CborValue *value, int64_t *result) |
| 792 | { |
| 793 | assert(cbor_value_is_integer(value)); |
| 794 | uint64_t v = _cbor_value_extract_int64_helper(value); |
| 795 | |
| 796 | /* Check before converting, as the standard says (C11 6.3.1.3 paragraph 3): |
| 797 | * "[if] the new type is signed and the value cannot be represented in it; either the |
| 798 | * result is implementation-defined or an implementation-defined signal is raised." |
| 799 | * |
| 800 | * The range for int64_t is -2^63 to 2^63-1 (int64_t is required to be |
| 801 | * two's complement, C11 7.20.1.1 paragraph 3), which in CBOR is |
| 802 | * represented the same way, differing only on the "sign bit" (the major |
| 803 | * type). |
| 804 | */ |
| 805 | |
| 806 | if (unlikely(v > (uint64_t)INT64_MAX)) |
| 807 | return CborErrorDataTooLarge; |
| 808 | |
| 809 | *result = v; |
| 810 | if (value->flags & CborIteratorFlag_NegativeInteger) |
| 811 | *result = -*result - 1; |
| 812 | return CborNoError; |
| 813 | } |
| 814 | |
| 815 | /** |
| 816 | * Retrieves the CBOR integer value that \a value points to and stores it in \a |
| 817 | * result. If the iterator \a value does not point to an integer value, the |
| 818 | * behavior is undefined, so checking with \ref cbor_value_get_type or with |
| 819 | * \ref cbor_value_is_integer is recommended. |
| 820 | * |
| 821 | * Unlike cbor_value_get_int(), this function performs a check to see if the |
| 822 | * stored integer fits in \a result without data loss. If the number is outside |
| 823 | * the valid range for the data type, this function returns the recoverable |
| 824 | * error CborErrorDataTooLarge. In that case, use one of the other integer |
| 825 | * functions to obtain the value. |
| 826 | * |
| 827 | * \sa cbor_value_get_type(), cbor_value_is_valid(), cbor_value_is_integer(), cbor_value_get_int64(), |
| 828 | * cbor_value_get_uint64(), cbor_value_get_int64_checked(), cbor_value_get_raw_integer() |
| 829 | */ |
| 830 | CborError cbor_value_get_int_checked(const CborValue *value, int *result) |
| 831 | { |
| 832 | assert(cbor_value_is_integer(value)); |
| 833 | uint64_t v = _cbor_value_extract_int64_helper(value); |
| 834 | |
| 835 | /* Check before converting, as the standard says (C11 6.3.1.3 paragraph 3): |
| 836 | * "[if] the new type is signed and the value cannot be represented in it; either the |
| 837 | * result is implementation-defined or an implementation-defined signal is raised." |
| 838 | * |
| 839 | * But we can convert from signed to unsigned without fault (paragraph 2). |
| 840 | * |
| 841 | * The range for int is implementation-defined and int is not guaranteed use |
| 842 | * two's complement representation (int32_t is). |
| 843 | */ |
| 844 | |
| 845 | if (value->flags & CborIteratorFlag_NegativeInteger) { |
| 846 | if (unlikely(v > (unsigned) -(INT_MIN + 1))) |
| 847 | return CborErrorDataTooLarge; |
| 848 | |
alradmsft | 9ba4791 | 2016-10-11 17:56:15 -0700 | [diff] [blame] | 849 | *result = (int)v; |
Thiago Macieira | 0f02e79 | 2016-07-07 19:55:08 -0700 | [diff] [blame] | 850 | *result = -*result - 1; |
| 851 | } else { |
| 852 | if (unlikely(v > (uint64_t)INT_MAX)) |
| 853 | return CborErrorDataTooLarge; |
| 854 | |
alradmsft | 9ba4791 | 2016-10-11 17:56:15 -0700 | [diff] [blame] | 855 | *result = (int)v; |
Thiago Macieira | 0f02e79 | 2016-07-07 19:55:08 -0700 | [diff] [blame] | 856 | } |
| 857 | return CborNoError; |
| 858 | |
| 859 | } |
| 860 | |
| 861 | /** |
Thiago Macieira | 46a818e | 2015-10-08 15:13:05 +0200 | [diff] [blame] | 862 | * \fn bool cbor_value_is_length_known(const CborValue *value) |
| 863 | * |
| 864 | * Returns true if the length of this type is known without calculation. That |
| 865 | * is, if the length of this CBOR string, map or array is encoded in the data |
| 866 | * stream, this function returns true. If the length is not encoded, it returns |
| 867 | * false. |
| 868 | * |
| 869 | * If the length is known, code can call cbor_value_get_string_length(), |
| 870 | * cbor_value_get_array_length() or cbor_value_get_map_length() to obtain the |
| 871 | * length. If the length is not known but is necessary, code can use the |
| 872 | * cbor_value_calculate_string_length() function (no equivalent function is |
| 873 | * provided for maps and arrays). |
| 874 | */ |
| 875 | |
| 876 | /** |
| 877 | * \fn bool cbor_value_is_text_string(const CborValue *value) |
| 878 | * |
| 879 | * Returns true if the iterator \a value is valid and points to a CBOR text |
| 880 | * string. CBOR text strings are UTF-8 encoded and usually contain |
| 881 | * human-readable text. |
| 882 | * |
| 883 | * \sa cbor_value_is_valid(), cbor_value_get_string_length(), cbor_value_calculate_string_length(), |
| 884 | * cbor_value_copy_text_string(), cbor_value_dup_text_string() |
| 885 | */ |
| 886 | |
| 887 | /** |
| 888 | * \fn bool cbor_value_is_byte_string(const CborValue *value) |
| 889 | * |
| 890 | * Returns true if the iterator \a value is valid and points to a CBOR text |
| 891 | * string. CBOR byte strings are binary data with no specified encoding or |
| 892 | * format. |
| 893 | * |
| 894 | * \sa cbor_value_is_valid(), cbor_value_get_string_length(), cbor_value_calculate_string_length(), |
| 895 | * cbor_value_copy_byte_string(), cbor_value_dup_byte_string() |
| 896 | */ |
| 897 | |
| 898 | /** |
| 899 | * \fn CborError cbor_value_get_string_length(const CborValue *value, size_t *length) |
| 900 | * |
| 901 | * Extracts the length of the byte or text string that \a value points to and |
| 902 | * stores it in \a result. If the iterator \a value does not point to a text |
| 903 | * string or a byte string, the behaviour is undefined, so checking with \ref |
| 904 | * cbor_value_get_type, with \ref cbor_value_is_text_string or \ref |
| 905 | * cbor_value_is_byte_string is recommended. |
| 906 | * |
| 907 | * If the length of this string is not encoded in the CBOR data stream, this |
| 908 | * function will return the recoverable error CborErrorUnknownLength. You may |
| 909 | * also check whether that is the case by using cbor_value_is_length_known(). |
| 910 | * |
| 911 | * If the length of the string is required but the length was not encoded, use |
| 912 | * cbor_value_calculate_string_length(), but note that that function does not |
| 913 | * run in constant time. |
| 914 | * |
| 915 | * \note On 32-bit platforms, this function will return error condition of \ref |
| 916 | * CborErrorDataTooLarge if the stream indicates a length that is too big to |
| 917 | * fit in 32-bit. |
| 918 | * |
| 919 | * \sa cbor_value_is_valid(), cbor_value_is_length_known(), cbor_value_calculate_string_length() |
| 920 | */ |
| 921 | |
| 922 | /** |
| 923 | * Calculates the length of the byte or text string that \a value points to and |
| 924 | * stores it in \a len. If the iterator \a value does not point to a text |
| 925 | * string or a byte string, the behaviour is undefined, so checking with \ref |
| 926 | * cbor_value_get_type, with \ref cbor_value_is_text_string or \ref |
| 927 | * cbor_value_is_byte_string is recommended. |
| 928 | * |
| 929 | * This function is different from cbor_value_get_string_length() in that it |
| 930 | * calculates the length even for strings sent in chunks. For that reason, this |
| 931 | * function may not run in constant time (it will run in O(n) time on the |
| 932 | * number of chunks). It does use constant memory (O(1)). |
Thiago Macieira | 2312efd | 2015-05-06 16:07:48 -0700 | [diff] [blame] | 933 | * |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 934 | * \note On 32-bit platforms, this function will return error condition of \ref |
| 935 | * CborErrorDataTooLarge if the stream indicates a length that is too big to |
| 936 | * fit in 32-bit. |
Thiago Macieira | 2312efd | 2015-05-06 16:07:48 -0700 | [diff] [blame] | 937 | * |
| 938 | * \sa cbor_value_get_string_length(), cbor_value_copy_string(), cbor_value_is_length_known() |
| 939 | */ |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 940 | CborError cbor_value_calculate_string_length(const CborValue *value, size_t *len) |
Thiago Macieira | 2312efd | 2015-05-06 16:07:48 -0700 | [diff] [blame] | 941 | { |
Thiago Macieira | 9ae0581 | 2015-05-11 15:09:09 +0900 | [diff] [blame] | 942 | *len = SIZE_MAX; |
Thiago Macieira | ff130bc | 2015-06-19 15:15:33 -0700 | [diff] [blame] | 943 | return _cbor_value_copy_string(value, NULL, len, NULL); |
Thiago Macieira | c70169f | 2015-05-06 07:49:44 -0700 | [diff] [blame] | 944 | } |
| 945 | |
Thiago Macieira | dbc0129 | 2016-06-06 17:02:25 -0700 | [diff] [blame] | 946 | /* We return uintptr_t so that we can pass memcpy directly as the iteration |
| 947 | * function. The choice is to optimize for memcpy, which is used in the base |
| 948 | * parser API (cbor_value_copy_string), while memcmp is used in convenience API |
| 949 | * only. */ |
Thiago Macieira | 5752ce5 | 2015-06-16 12:10:03 -0700 | [diff] [blame] | 950 | typedef uintptr_t (*IterateFunction)(char *, const uint8_t *, size_t); |
Thiago Macieira | 9ae0581 | 2015-05-11 15:09:09 +0900 | [diff] [blame] | 951 | |
Thiago Macieira | 5752ce5 | 2015-06-16 12:10:03 -0700 | [diff] [blame] | 952 | static uintptr_t iterate_noop(char *dest, const uint8_t *src, size_t len) |
Thiago Macieira | 9ae0581 | 2015-05-11 15:09:09 +0900 | [diff] [blame] | 953 | { |
| 954 | (void)dest; |
| 955 | (void)src; |
| 956 | (void)len; |
| 957 | return true; |
| 958 | } |
| 959 | |
Thiago Macieira | 5752ce5 | 2015-06-16 12:10:03 -0700 | [diff] [blame] | 960 | static uintptr_t iterate_memcmp(char *s1, const uint8_t *s2, size_t len) |
Thiago Macieira | c4a73c6 | 2015-05-09 18:14:11 -0700 | [diff] [blame] | 961 | { |
Thiago Macieira | 5752ce5 | 2015-06-16 12:10:03 -0700 | [diff] [blame] | 962 | return memcmp(s1, (const char *)s2, len) == 0; |
Thiago Macieira | c4a73c6 | 2015-05-09 18:14:11 -0700 | [diff] [blame] | 963 | } |
| 964 | |
Thiago Macieira | 9ae0581 | 2015-05-11 15:09:09 +0900 | [diff] [blame] | 965 | static CborError iterate_string_chunks(const CborValue *value, char *buffer, size_t *buflen, |
| 966 | bool *result, CborValue *next, IterateFunction func) |
| 967 | { |
| 968 | assert(cbor_value_is_byte_string(value) || cbor_value_is_text_string(value)); |
| 969 | |
| 970 | size_t total; |
| 971 | CborError err; |
Thiago Macieira | 5752ce5 | 2015-06-16 12:10:03 -0700 | [diff] [blame] | 972 | const uint8_t *ptr = value->ptr; |
Thiago Macieira | 9ae0581 | 2015-05-11 15:09:09 +0900 | [diff] [blame] | 973 | if (cbor_value_is_length_known(value)) { |
Thiago Macieira | dbc0129 | 2016-06-06 17:02:25 -0700 | [diff] [blame] | 974 | /* easy case: fixed length */ |
Thiago Macieira | 9ae0581 | 2015-05-11 15:09:09 +0900 | [diff] [blame] | 975 | err = extract_length(value->parser, &ptr, &total); |
| 976 | if (err) |
| 977 | return err; |
Thiago Macieira | 63abed9 | 2015-10-28 17:01:14 -0700 | [diff] [blame] | 978 | if (total > (size_t)(value->parser->end - ptr)) |
Thiago Macieira | 9ae0581 | 2015-05-11 15:09:09 +0900 | [diff] [blame] | 979 | return CborErrorUnexpectedEOF; |
| 980 | if (total <= *buflen) |
Thiago Macieira | e12dfd0 | 2016-06-07 16:29:25 -0700 | [diff] [blame] | 981 | *result = !!func(buffer, ptr, total); |
Thiago Macieira | 9ae0581 | 2015-05-11 15:09:09 +0900 | [diff] [blame] | 982 | else |
| 983 | *result = false; |
| 984 | ptr += total; |
| 985 | } else { |
Thiago Macieira | dbc0129 | 2016-06-06 17:02:25 -0700 | [diff] [blame] | 986 | /* chunked */ |
Thiago Macieira | 9ae0581 | 2015-05-11 15:09:09 +0900 | [diff] [blame] | 987 | ++ptr; |
| 988 | total = 0; |
| 989 | *result = true; |
| 990 | while (true) { |
| 991 | size_t chunkLen; |
| 992 | size_t newTotal; |
| 993 | |
| 994 | if (ptr == value->parser->end) |
| 995 | return CborErrorUnexpectedEOF; |
| 996 | |
Thiago Macieira | 5752ce5 | 2015-06-16 12:10:03 -0700 | [diff] [blame] | 997 | if (*ptr == (uint8_t)BreakByte) { |
Thiago Macieira | 9ae0581 | 2015-05-11 15:09:09 +0900 | [diff] [blame] | 998 | ++ptr; |
| 999 | break; |
| 1000 | } |
| 1001 | |
Thiago Macieira | dbc0129 | 2016-06-06 17:02:25 -0700 | [diff] [blame] | 1002 | /* is this the right type? */ |
Thiago Macieira | 9ae0581 | 2015-05-11 15:09:09 +0900 | [diff] [blame] | 1003 | if ((*ptr & MajorTypeMask) != value->type) |
| 1004 | return CborErrorIllegalType; |
| 1005 | |
| 1006 | err = extract_length(value->parser, &ptr, &chunkLen); |
| 1007 | if (err) |
| 1008 | return err; |
| 1009 | |
Thiago Macieira | 1de31a4 | 2015-06-16 16:01:16 -0700 | [diff] [blame] | 1010 | if (unlikely(add_check_overflow(total, chunkLen, &newTotal))) |
Thiago Macieira | 9ae0581 | 2015-05-11 15:09:09 +0900 | [diff] [blame] | 1011 | return CborErrorDataTooLarge; |
| 1012 | |
Thiago Macieira | 63abed9 | 2015-10-28 17:01:14 -0700 | [diff] [blame] | 1013 | if (chunkLen > (size_t)(value->parser->end - ptr)) |
Thiago Macieira | 9ae0581 | 2015-05-11 15:09:09 +0900 | [diff] [blame] | 1014 | return CborErrorUnexpectedEOF; |
| 1015 | |
| 1016 | if (*result && *buflen >= newTotal) |
Thiago Macieira | e12dfd0 | 2016-06-07 16:29:25 -0700 | [diff] [blame] | 1017 | *result = !!func(buffer + total, ptr, chunkLen); |
Thiago Macieira | 9ae0581 | 2015-05-11 15:09:09 +0900 | [diff] [blame] | 1018 | else |
| 1019 | *result = false; |
| 1020 | |
| 1021 | ptr += chunkLen; |
| 1022 | total = newTotal; |
| 1023 | } |
| 1024 | } |
| 1025 | |
Thiago Macieira | dbc0129 | 2016-06-06 17:02:25 -0700 | [diff] [blame] | 1026 | /* is there enough room for the ending NUL byte? */ |
Thiago Macieira | e136feb | 2017-02-24 21:21:04 -0800 | [diff] [blame] | 1027 | if (*result && *buflen > total) { |
| 1028 | uint8_t nul[] = { 0 }; |
| 1029 | *result = !!func(buffer + total, nul, 1); |
| 1030 | } |
Thiago Macieira | 9ae0581 | 2015-05-11 15:09:09 +0900 | [diff] [blame] | 1031 | *buflen = total; |
| 1032 | |
| 1033 | if (next) { |
| 1034 | *next = *value; |
| 1035 | next->ptr = ptr; |
| 1036 | return preparse_next_value(next); |
| 1037 | } |
| 1038 | return CborNoError; |
| 1039 | } |
| 1040 | |
Thiago Macieira | 2312efd | 2015-05-06 16:07:48 -0700 | [diff] [blame] | 1041 | /** |
Thiago Macieira | ff130bc | 2015-06-19 15:15:33 -0700 | [diff] [blame] | 1042 | * \fn CborError cbor_value_copy_text_string(const CborValue *value, char *buffer, size_t *buflen, CborValue *next) |
| 1043 | * |
Thiago Macieira | 2312efd | 2015-05-06 16:07:48 -0700 | [diff] [blame] | 1044 | * Copies the string pointed by \a value into the buffer provided at \a buffer |
| 1045 | * of \a buflen bytes. If \a buffer is a NULL pointer, this function will not |
| 1046 | * copy anything and will only update the \a next value. |
| 1047 | * |
Thiago Macieira | 46a818e | 2015-10-08 15:13:05 +0200 | [diff] [blame] | 1048 | * If the iterator \a value does not point to a text string, the behaviour is |
| 1049 | * undefined, so checking with \ref cbor_value_get_type or \ref |
| 1050 | * cbor_value_is_text_string is recommended. |
| 1051 | * |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 1052 | * If the provided buffer length was too small, this function returns an error |
| 1053 | * condition of \ref CborErrorOutOfMemory. If you need to calculate the length |
| 1054 | * of the string in order to preallocate a buffer, use |
Thiago Macieira | 2312efd | 2015-05-06 16:07:48 -0700 | [diff] [blame] | 1055 | * cbor_value_calculate_string_length(). |
| 1056 | * |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 1057 | * On success, this function sets the number of bytes copied to \c{*buflen}. If |
| 1058 | * the buffer is large enough, this function will insert a null byte after the |
| 1059 | * last copied byte, to facilitate manipulation of text strings. That byte is |
| 1060 | * not included in the returned value of \c{*buflen}. |
Thiago Macieira | 2312efd | 2015-05-06 16:07:48 -0700 | [diff] [blame] | 1061 | * |
| 1062 | * The \a next pointer, if not null, will be updated to point to the next item |
| 1063 | * after this string. If \a value points to the last item, then \a next will be |
| 1064 | * invalid. |
| 1065 | * |
Thiago Macieira | 46a818e | 2015-10-08 15:13:05 +0200 | [diff] [blame] | 1066 | * This function may not run in constant time (it will run in O(n) time on the |
| 1067 | * number of chunks). It requires constant memory (O(1)). |
| 1068 | * |
Thiago Macieira | 2312efd | 2015-05-06 16:07:48 -0700 | [diff] [blame] | 1069 | * \note This function does not perform UTF-8 validation on the incoming text |
| 1070 | * string. |
| 1071 | * |
Thiago Macieira | ff130bc | 2015-06-19 15:15:33 -0700 | [diff] [blame] | 1072 | * \sa cbor_value_dup_text_string(), cbor_value_copy_byte_string(), cbor_value_get_string_length(), cbor_value_calculate_string_length() |
Thiago Macieira | 2312efd | 2015-05-06 16:07:48 -0700 | [diff] [blame] | 1073 | */ |
Thiago Macieira | ff130bc | 2015-06-19 15:15:33 -0700 | [diff] [blame] | 1074 | |
| 1075 | /** |
| 1076 | * \fn CborError cbor_value_copy_byte_string(const CborValue *value, uint8_t *buffer, size_t *buflen, CborValue *next) |
| 1077 | * |
| 1078 | * Copies the string pointed by \a value into the buffer provided at \a buffer |
| 1079 | * of \a buflen bytes. If \a buffer is a NULL pointer, this function will not |
| 1080 | * copy anything and will only update the \a next value. |
| 1081 | * |
Thiago Macieira | 46a818e | 2015-10-08 15:13:05 +0200 | [diff] [blame] | 1082 | * If the iterator \a value does not point to a byte string, the behaviour is |
| 1083 | * undefined, so checking with \ref cbor_value_get_type or \ref |
| 1084 | * cbor_value_is_byte_string is recommended. |
| 1085 | * |
Thiago Macieira | ff130bc | 2015-06-19 15:15:33 -0700 | [diff] [blame] | 1086 | * If the provided buffer length was too small, this function returns an error |
| 1087 | * condition of \ref CborErrorOutOfMemory. If you need to calculate the length |
| 1088 | * of the string in order to preallocate a buffer, use |
| 1089 | * cbor_value_calculate_string_length(). |
| 1090 | * |
| 1091 | * On success, this function sets the number of bytes copied to \c{*buflen}. If |
| 1092 | * the buffer is large enough, this function will insert a null byte after the |
| 1093 | * last copied byte, to facilitate manipulation of null-terminated strings. |
| 1094 | * That byte is not included in the returned value of \c{*buflen}. |
| 1095 | * |
| 1096 | * The \a next pointer, if not null, will be updated to point to the next item |
| 1097 | * after this string. If \a value points to the last item, then \a next will be |
| 1098 | * invalid. |
| 1099 | * |
Thiago Macieira | 46a818e | 2015-10-08 15:13:05 +0200 | [diff] [blame] | 1100 | * This function may not run in constant time (it will run in O(n) time on the |
| 1101 | * number of chunks). It requires constant memory (O(1)). |
| 1102 | * |
Thiago Macieira | ff130bc | 2015-06-19 15:15:33 -0700 | [diff] [blame] | 1103 | * \sa cbor_value_dup_text_string(), cbor_value_copy_text_string(), cbor_value_get_string_length(), cbor_value_calculate_string_length() |
| 1104 | */ |
| 1105 | |
| 1106 | CborError _cbor_value_copy_string(const CborValue *value, void *buffer, |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 1107 | size_t *buflen, CborValue *next) |
Thiago Macieira | 2312efd | 2015-05-06 16:07:48 -0700 | [diff] [blame] | 1108 | { |
Thiago Macieira | 9ae0581 | 2015-05-11 15:09:09 +0900 | [diff] [blame] | 1109 | bool copied_all; |
Thiago Macieira | ed5b57c | 2015-07-07 16:38:27 -0700 | [diff] [blame] | 1110 | CborError err = iterate_string_chunks(value, (char*)buffer, buflen, &copied_all, next, |
Thiago Macieira | 9ae0581 | 2015-05-11 15:09:09 +0900 | [diff] [blame] | 1111 | buffer ? (IterateFunction)memcpy : iterate_noop); |
| 1112 | return err ? err : |
| 1113 | copied_all ? CborNoError : CborErrorOutOfMemory; |
Thiago Macieira | c70169f | 2015-05-06 07:49:44 -0700 | [diff] [blame] | 1114 | } |
| 1115 | |
Thiago Macieira | c4a73c6 | 2015-05-09 18:14:11 -0700 | [diff] [blame] | 1116 | /** |
| 1117 | * Compares the entry \a value with the string \a string and store the result |
Thiago Macieira | 46a818e | 2015-10-08 15:13:05 +0200 | [diff] [blame] | 1118 | * in \a result. If the value is different from \a string \a result will |
| 1119 | * contain \c false. |
Thiago Macieira | c4a73c6 | 2015-05-09 18:14:11 -0700 | [diff] [blame] | 1120 | * |
| 1121 | * The entry at \a value may be a tagged string. If \a is not a string or a |
| 1122 | * tagged string, the comparison result will be false. |
Thiago Macieira | 46a818e | 2015-10-08 15:13:05 +0200 | [diff] [blame] | 1123 | * |
| 1124 | * CBOR requires text strings to be encoded in UTF-8, but this function does |
| 1125 | * not validate either the strings in the stream or the string \a string to be |
| 1126 | * matched. Moreover, comparison is done on strict codepoint comparison, |
| 1127 | * without any Unicode normalization. |
| 1128 | * |
| 1129 | * This function may not run in constant time (it will run in O(n) time on the |
| 1130 | * number of chunks). It requires constant memory (O(1)). |
| 1131 | * |
| 1132 | * \sa cbor_value_skip_tag(), cbor_value_copy_text_string() |
Thiago Macieira | c4a73c6 | 2015-05-09 18:14:11 -0700 | [diff] [blame] | 1133 | */ |
| 1134 | CborError cbor_value_text_string_equals(const CborValue *value, const char *string, bool *result) |
| 1135 | { |
| 1136 | CborValue copy = *value; |
| 1137 | CborError err = cbor_value_skip_tag(©); |
| 1138 | if (err) |
| 1139 | return err; |
| 1140 | if (!cbor_value_is_text_string(©)) { |
| 1141 | *result = false; |
| 1142 | return CborNoError; |
| 1143 | } |
| 1144 | |
| 1145 | size_t len = strlen(string); |
| 1146 | return iterate_string_chunks(©, CONST_CAST(char *, string), &len, result, NULL, iterate_memcmp); |
| 1147 | } |
| 1148 | |
| 1149 | /** |
Thiago Macieira | 46a818e | 2015-10-08 15:13:05 +0200 | [diff] [blame] | 1150 | * \fn bool cbor_value_is_array(const CborValue *value) |
Thiago Macieira | 7b623c2 | 2015-05-11 15:52:14 +0900 | [diff] [blame] | 1151 | * |
Thiago Macieira | 46a818e | 2015-10-08 15:13:05 +0200 | [diff] [blame] | 1152 | * Returns true if the iterator \a value is valid and points to a CBOR array. |
| 1153 | * |
| 1154 | * \sa cbor_value_is_valid(), cbor_value_is_map() |
| 1155 | */ |
| 1156 | |
| 1157 | /** |
| 1158 | * \fn CborError cbor_value_get_array_length(const CborValue *value, size_t *length) |
| 1159 | * |
| 1160 | * Extracts the length of the CBOR array that \a value points to and stores it |
| 1161 | * in \a result. If the iterator \a value does not point to a CBOR array, the |
| 1162 | * behaviour is undefined, so checking with \ref cbor_value_get_type or \ref |
| 1163 | * cbor_value_is_array is recommended. |
| 1164 | * |
| 1165 | * If the length of this array is not encoded in the CBOR data stream, this |
| 1166 | * function will return the recoverable error CborErrorUnknownLength. You may |
| 1167 | * also check whether that is the case by using cbor_value_is_length_known(). |
| 1168 | * |
| 1169 | * \note On 32-bit platforms, this function will return error condition of \ref |
| 1170 | * CborErrorDataTooLarge if the stream indicates a length that is too big to |
| 1171 | * fit in 32-bit. |
| 1172 | * |
| 1173 | * \sa cbor_value_is_valid(), cbor_value_is_length_known() |
| 1174 | */ |
| 1175 | |
| 1176 | /** |
| 1177 | * \fn bool cbor_value_is_map(const CborValue *value) |
| 1178 | * |
| 1179 | * Returns true if the iterator \a value is valid and points to a CBOR map. |
| 1180 | * |
| 1181 | * \sa cbor_value_is_valid(), cbor_value_is_array() |
| 1182 | */ |
| 1183 | |
| 1184 | /** |
| 1185 | * \fn CborError cbor_value_get_map_length(const CborValue *value, size_t *length) |
| 1186 | * |
| 1187 | * Extracts the length of the CBOR map that \a value points to and stores it in |
| 1188 | * \a result. If the iterator \a value does not point to a CBOR map, the |
| 1189 | * behaviour is undefined, so checking with \ref cbor_value_get_type or \ref |
| 1190 | * cbor_value_is_map is recommended. |
| 1191 | * |
| 1192 | * If the length of this map is not encoded in the CBOR data stream, this |
| 1193 | * function will return the recoverable error CborErrorUnknownLength. You may |
| 1194 | * also check whether that is the case by using cbor_value_is_length_known(). |
| 1195 | * |
| 1196 | * \note On 32-bit platforms, this function will return error condition of \ref |
| 1197 | * CborErrorDataTooLarge if the stream indicates a length that is too big to |
| 1198 | * fit in 32-bit. |
| 1199 | * |
| 1200 | * \sa cbor_value_is_valid(), cbor_value_is_length_known() |
| 1201 | */ |
| 1202 | |
| 1203 | /** |
| 1204 | * Attempts to find the value in map \a map that corresponds to the text string |
| 1205 | * entry \a string. If the iterator \a value does not point to a CBOR map, the |
| 1206 | * behaviour is undefined, so checking with \ref cbor_value_get_type or \ref |
| 1207 | * cbor_value_is_map is recommended. |
| 1208 | * |
| 1209 | * If the item is found, it is stored in \a result. If no item is found |
| 1210 | * matching the key, then \a result will contain an element of type \ref |
| 1211 | * CborInvalidType. Matching is performed using |
| 1212 | * cbor_value_text_string_equals(), so tagged strings will also match. |
| 1213 | * |
| 1214 | * This function has a time complexity of O(n) where n is the number of |
| 1215 | * elements in the map to be searched. In addition, this function is has O(n) |
| 1216 | * memory requirement based on the number of nested containers (maps or arrays) |
| 1217 | * found as elements of this map. |
| 1218 | * |
| 1219 | * \sa cbor_value_is_valid(), cbor_value_text_string_equals(), cbor_value_advance() |
Thiago Macieira | 7b623c2 | 2015-05-11 15:52:14 +0900 | [diff] [blame] | 1220 | */ |
| 1221 | CborError cbor_value_map_find_value(const CborValue *map, const char *string, CborValue *element) |
| 1222 | { |
| 1223 | assert(cbor_value_is_map(map)); |
| 1224 | size_t len = strlen(string); |
| 1225 | CborError err = cbor_value_enter_container(map, element); |
| 1226 | if (err) |
| 1227 | goto error; |
| 1228 | |
| 1229 | while (!cbor_value_at_end(element)) { |
Thiago Macieira | dbc0129 | 2016-06-06 17:02:25 -0700 | [diff] [blame] | 1230 | /* find the non-tag so we can compare */ |
Thiago Macieira | 7b623c2 | 2015-05-11 15:52:14 +0900 | [diff] [blame] | 1231 | err = cbor_value_skip_tag(element); |
| 1232 | if (err) |
| 1233 | goto error; |
| 1234 | if (cbor_value_is_text_string(element)) { |
| 1235 | bool equals; |
| 1236 | size_t dummyLen = len; |
| 1237 | err = iterate_string_chunks(element, CONST_CAST(char *, string), &dummyLen, |
| 1238 | &equals, element, iterate_memcmp); |
| 1239 | if (err) |
| 1240 | goto error; |
| 1241 | if (equals) |
| 1242 | return preparse_value(element); |
| 1243 | } else { |
Thiago Macieira | dbc0129 | 2016-06-06 17:02:25 -0700 | [diff] [blame] | 1244 | /* skip this key */ |
Thiago Macieira | 7b623c2 | 2015-05-11 15:52:14 +0900 | [diff] [blame] | 1245 | err = cbor_value_advance(element); |
| 1246 | if (err) |
| 1247 | goto error; |
| 1248 | } |
| 1249 | |
Thiago Macieira | dbc0129 | 2016-06-06 17:02:25 -0700 | [diff] [blame] | 1250 | /* skip this value */ |
Thiago Macieira | 7b623c2 | 2015-05-11 15:52:14 +0900 | [diff] [blame] | 1251 | err = cbor_value_skip_tag(element); |
| 1252 | if (err) |
| 1253 | goto error; |
| 1254 | err = cbor_value_advance(element); |
| 1255 | if (err) |
| 1256 | goto error; |
| 1257 | } |
| 1258 | |
Thiago Macieira | dbc0129 | 2016-06-06 17:02:25 -0700 | [diff] [blame] | 1259 | /* not found */ |
Thiago Macieira | 7b623c2 | 2015-05-11 15:52:14 +0900 | [diff] [blame] | 1260 | element->type = CborInvalidType; |
| 1261 | return CborNoError; |
| 1262 | |
| 1263 | error: |
| 1264 | element->type = CborInvalidType; |
| 1265 | return err; |
| 1266 | } |
| 1267 | |
| 1268 | /** |
Thiago Macieira | 46a818e | 2015-10-08 15:13:05 +0200 | [diff] [blame] | 1269 | * \fn bool cbor_value_is_float(const CborValue *value) |
| 1270 | * |
| 1271 | * Returns true if the iterator \a value is valid and points to a CBOR |
| 1272 | * single-precision floating point (32-bit). |
| 1273 | * |
| 1274 | * \sa cbor_value_is_valid(), cbor_value_is_double(), cbor_value_is_half_float() |
| 1275 | */ |
| 1276 | |
| 1277 | /** |
| 1278 | * \fn CborError cbor_value_get_float(const CborValue *value, float *result) |
| 1279 | * |
| 1280 | * Retrieves the CBOR single-precision floating point (32-bit) value that \a |
| 1281 | * value points to and stores it in \a result. If the iterator \a value does |
| 1282 | * not point to a single-precision floating point value, the behavior is |
| 1283 | * undefined, so checking with \ref cbor_value_get_type or with \ref |
| 1284 | * cbor_value_is_float is recommended. |
| 1285 | * |
| 1286 | * \sa cbor_value_get_type(), cbor_value_is_valid(), cbor_value_is_float(), cbor_value_get_double() |
| 1287 | */ |
| 1288 | |
| 1289 | /** |
| 1290 | * \fn bool cbor_value_is_double(const CborValue *value) |
| 1291 | * |
| 1292 | * Returns true if the iterator \a value is valid and points to a CBOR |
| 1293 | * double-precision floating point (64-bit). |
| 1294 | * |
| 1295 | * \sa cbor_value_is_valid(), cbor_value_is_float(), cbor_value_is_half_float() |
| 1296 | */ |
| 1297 | |
| 1298 | /** |
| 1299 | * \fn CborError cbor_value_get_double(const CborValue *value, float *result) |
| 1300 | * |
| 1301 | * Retrieves the CBOR double-precision floating point (64-bit) value that \a |
| 1302 | * value points to and stores it in \a result. If the iterator \a value does |
| 1303 | * not point to a double-precision floating point value, the behavior is |
| 1304 | * undefined, so checking with \ref cbor_value_get_type or with \ref |
| 1305 | * cbor_value_is_double is recommended. |
| 1306 | * |
| 1307 | * \sa cbor_value_get_type(), cbor_value_is_valid(), cbor_value_is_double(), cbor_value_get_float() |
| 1308 | */ |
| 1309 | |
| 1310 | /** |
| 1311 | * \fn bool cbor_value_is_half_float(const CborValue *value) |
| 1312 | * |
| 1313 | * Returns true if the iterator \a value is valid and points to a CBOR |
| 1314 | * single-precision floating point (16-bit). |
| 1315 | * |
| 1316 | * \sa cbor_value_is_valid(), cbor_value_is_double(), cbor_value_is_float() |
| 1317 | */ |
| 1318 | |
| 1319 | /** |
| 1320 | * Retrieves the CBOR half-precision floating point (16-bit) value that \a |
| 1321 | * value points to and stores it in \a result. If the iterator \a value does |
| 1322 | * not point to a half-precision floating point value, the behavior is |
| 1323 | * undefined, so checking with \ref cbor_value_get_type or with \ref |
| 1324 | * cbor_value_is_half_float is recommended. |
| 1325 | * |
| 1326 | * Note: since the C language does not have a standard type for half-precision |
| 1327 | * floating point, this function takes a \c{void *} as a parameter for the |
| 1328 | * storage area, which must be at least 16 bits wide. |
| 1329 | * |
| 1330 | * \sa cbor_value_get_type(), cbor_value_is_valid(), cbor_value_is_half_float(), cbor_value_get_float() |
Thiago Macieira | c4a73c6 | 2015-05-09 18:14:11 -0700 | [diff] [blame] | 1331 | */ |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 1332 | CborError cbor_value_get_half_float(const CborValue *value, void *result) |
Thiago Macieira | c70169f | 2015-05-06 07:49:44 -0700 | [diff] [blame] | 1333 | { |
Thiago Macieira | cac5db5 | 2016-07-07 15:50:59 -0700 | [diff] [blame] | 1334 | assert(cbor_value_is_half_float(value)); |
Thiago Macieira | c70169f | 2015-05-06 07:49:44 -0700 | [diff] [blame] | 1335 | |
Thiago Macieira | dbc0129 | 2016-06-06 17:02:25 -0700 | [diff] [blame] | 1336 | /* size has been computed already */ |
Thiago Macieira | c70169f | 2015-05-06 07:49:44 -0700 | [diff] [blame] | 1337 | uint16_t v = get16(value->ptr + 1); |
| 1338 | memcpy(result, &v, sizeof(v)); |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 1339 | return CborNoError; |
Thiago Macieira | 54a0e10 | 2015-05-05 21:25:06 -0700 | [diff] [blame] | 1340 | } |
Thiago Macieira | 46a818e | 2015-10-08 15:13:05 +0200 | [diff] [blame] | 1341 | |
| 1342 | /** @} */ |