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