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