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