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