Thiago Macieira | 54a0e10 | 2015-05-05 21:25:06 -0700 | [diff] [blame] | 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2015 Intel Corporation |
| 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 | |
| 25 | #define _BSD_SOURCE |
| 26 | #include "cbor.h" |
| 27 | #include "cborconstants_p.h" |
| 28 | #include "compilersupport_p.h" |
| 29 | |
| 30 | #include <assert.h> |
Thiago Macieira | 2312efd | 2015-05-06 16:07:48 -0700 | [diff] [blame] | 31 | #include <stdlib.h> |
Thiago Macieira | 54a0e10 | 2015-05-05 21:25:06 -0700 | [diff] [blame] | 32 | #include <string.h> |
| 33 | |
Thiago Macieira | 8f3fb78 | 2015-06-16 16:27:01 -0700 | [diff] [blame^] | 34 | #include "assert_p.h" /* Always include last */ |
| 35 | |
Thiago Macieira | 4a99af9 | 2015-05-12 10:41:45 +0900 | [diff] [blame] | 36 | #ifndef CBOR_PARSER_MAX_RECURSIONS |
| 37 | # define CBOR_PARSER_MAX_RECURSIONS 1024 |
| 38 | #endif |
| 39 | |
Thiago Macieira | 54a0e10 | 2015-05-05 21:25:06 -0700 | [diff] [blame] | 40 | /** |
| 41 | * \typedef CborValue |
| 42 | * This type contains one value parsed from the CBOR stream. |
| 43 | * |
| 44 | * To get the actual type, use cbor_value_get_type(). Then extract the value |
| 45 | * using one of the corresponding functions: cbor_value_get_boolean(), cbor_value_get_int64(), |
| 46 | * cbor_value_get_int(), cbor_value_copy_string(), cbor_value_get_array(), cbor_value_get_map(), |
| 47 | * cbor_value_get_double(), cbor_value_get_float(). |
| 48 | * |
| 49 | * In C++ and C11 modes, you can additionally use the cbor_value_get_integer() |
| 50 | * and cbor_value_get_floating_point() generic functions. |
| 51 | * |
| 52 | * \omit |
| 53 | * Implementation details: the CborValue contains these fields: |
| 54 | * \list |
| 55 | * \li ptr: pointer to the actual data |
| 56 | * \li flags: flags from the decoder |
Thiago Macieira | 2312efd | 2015-05-06 16:07:48 -0700 | [diff] [blame] | 57 | * \li extra: partially decoded integer value (0, 1 or 2 bytes) |
Thiago Macieira | 54a0e10 | 2015-05-05 21:25:06 -0700 | [diff] [blame] | 58 | * \li remaining: remaining items in this collection after this item or UINT32_MAX if length is unknown |
| 59 | * \endlist |
| 60 | * \endomit |
| 61 | */ |
| 62 | |
Thiago Macieira | 5752ce5 | 2015-06-16 12:10:03 -0700 | [diff] [blame] | 63 | static inline uint16_t get16(const uint8_t *ptr) |
Thiago Macieira | 54a0e10 | 2015-05-05 21:25:06 -0700 | [diff] [blame] | 64 | { |
| 65 | uint16_t result; |
| 66 | memcpy(&result, ptr, sizeof(result)); |
Thiago Macieira | 5934a9f | 2015-06-16 11:55:28 -0700 | [diff] [blame] | 67 | return cbor_ntohs(result); |
Thiago Macieira | 54a0e10 | 2015-05-05 21:25:06 -0700 | [diff] [blame] | 68 | } |
| 69 | |
Thiago Macieira | 5752ce5 | 2015-06-16 12:10:03 -0700 | [diff] [blame] | 70 | static inline uint32_t get32(const uint8_t *ptr) |
Thiago Macieira | 54a0e10 | 2015-05-05 21:25:06 -0700 | [diff] [blame] | 71 | { |
| 72 | uint32_t result; |
| 73 | memcpy(&result, ptr, sizeof(result)); |
Thiago Macieira | 5934a9f | 2015-06-16 11:55:28 -0700 | [diff] [blame] | 74 | return cbor_ntohl(result); |
Thiago Macieira | 54a0e10 | 2015-05-05 21:25:06 -0700 | [diff] [blame] | 75 | } |
| 76 | |
Thiago Macieira | 5752ce5 | 2015-06-16 12:10:03 -0700 | [diff] [blame] | 77 | static inline uint64_t get64(const uint8_t *ptr) |
Thiago Macieira | 54a0e10 | 2015-05-05 21:25:06 -0700 | [diff] [blame] | 78 | { |
| 79 | uint64_t result; |
| 80 | memcpy(&result, ptr, sizeof(result)); |
Thiago Macieira | 5934a9f | 2015-06-16 11:55:28 -0700 | [diff] [blame] | 81 | return cbor_ntohll(result); |
Thiago Macieira | 54a0e10 | 2015-05-05 21:25:06 -0700 | [diff] [blame] | 82 | } |
| 83 | |
Thiago Macieira | f5cb94b | 2015-06-16 16:10:49 -0700 | [diff] [blame] | 84 | static CborError extract_number(const CborParser *parser, const uint8_t **ptr, uint64_t *len) |
Thiago Macieira | 2312efd | 2015-05-06 16:07:48 -0700 | [diff] [blame] | 85 | { |
| 86 | uint8_t additional_information = **ptr & SmallValueMask; |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 87 | ++*ptr; |
Thiago Macieira | 2312efd | 2015-05-06 16:07:48 -0700 | [diff] [blame] | 88 | if (additional_information < Value8Bit) { |
| 89 | *len = additional_information; |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 90 | return CborNoError; |
Thiago Macieira | 2312efd | 2015-05-06 16:07:48 -0700 | [diff] [blame] | 91 | } |
| 92 | if (unlikely(additional_information > Value64Bit)) |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 93 | return CborErrorIllegalNumber; |
Thiago Macieira | 2312efd | 2015-05-06 16:07:48 -0700 | [diff] [blame] | 94 | |
| 95 | size_t bytesNeeded = 1 << (additional_information - Value8Bit); |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 96 | if (unlikely(*ptr + bytesNeeded > parser->end)) { |
| 97 | return CborErrorUnexpectedEOF; |
Thiago Macieira | 2312efd | 2015-05-06 16:07:48 -0700 | [diff] [blame] | 98 | } else if (bytesNeeded == 1) { |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 99 | *len = (uint8_t)(*ptr)[0]; |
Thiago Macieira | 2312efd | 2015-05-06 16:07:48 -0700 | [diff] [blame] | 100 | } else if (bytesNeeded == 2) { |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 101 | *len = get16(*ptr); |
Thiago Macieira | 2312efd | 2015-05-06 16:07:48 -0700 | [diff] [blame] | 102 | } else if (bytesNeeded == 4) { |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 103 | *len = get32(*ptr); |
Thiago Macieira | 2312efd | 2015-05-06 16:07:48 -0700 | [diff] [blame] | 104 | } else { |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 105 | *len = get64(*ptr); |
Thiago Macieira | 2312efd | 2015-05-06 16:07:48 -0700 | [diff] [blame] | 106 | } |
| 107 | *ptr += bytesNeeded; |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 108 | return CborNoError; |
Thiago Macieira | 2312efd | 2015-05-06 16:07:48 -0700 | [diff] [blame] | 109 | } |
| 110 | |
Thiago Macieira | f5cb94b | 2015-06-16 16:10:49 -0700 | [diff] [blame] | 111 | 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] | 112 | { |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 113 | uint64_t v; |
| 114 | CborError err = extract_number(parser, ptr, &v); |
| 115 | if (err) |
| 116 | return err; |
| 117 | |
| 118 | *len = v; |
| 119 | if (v != *len) |
| 120 | return CborErrorDataTooLarge; |
| 121 | return CborNoError; |
| 122 | } |
| 123 | |
| 124 | static bool is_fixed_type(uint8_t type) |
| 125 | { |
| 126 | return type != CborTextStringType && type != CborByteStringType && type != CborArrayType && |
| 127 | type != CborMapType; |
| 128 | } |
| 129 | |
| 130 | static CborError preparse_value(CborValue *it) |
| 131 | { |
| 132 | const CborParser *parser = it->parser; |
Thiago Macieira | 11e913f | 2015-05-07 13:01:18 -0700 | [diff] [blame] | 133 | it->type = CborInvalidType; |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 134 | |
Thiago Macieira | 54a0e10 | 2015-05-05 21:25:06 -0700 | [diff] [blame] | 135 | // are we at the end? |
Thiago Macieira | 54a0e10 | 2015-05-05 21:25:06 -0700 | [diff] [blame] | 136 | if (it->ptr == parser->end) |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 137 | return CborErrorUnexpectedEOF; |
Thiago Macieira | 54a0e10 | 2015-05-05 21:25:06 -0700 | [diff] [blame] | 138 | |
| 139 | uint8_t descriptor = *it->ptr; |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 140 | uint8_t type = descriptor & MajorTypeMask; |
Thiago Macieira | 851c481 | 2015-05-08 15:23:20 -0700 | [diff] [blame] | 141 | it->type = type; |
Thiago Macieira | 54a0e10 | 2015-05-05 21:25:06 -0700 | [diff] [blame] | 142 | it->flags = 0; |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 143 | it->extra = (descriptor &= SmallValueMask); |
| 144 | |
Thiago Macieira | 56d9983 | 2015-05-07 14:34:27 -0700 | [diff] [blame] | 145 | if (descriptor > Value64Bit) { |
| 146 | if (unlikely(descriptor != IndefiniteLength)) |
Thiago Macieira | 3f76f63 | 2015-05-12 10:10:09 +0900 | [diff] [blame] | 147 | return type == CborSimpleType ? CborErrorUnknownType : CborErrorIllegalNumber; |
Thiago Macieira | 56d9983 | 2015-05-07 14:34:27 -0700 | [diff] [blame] | 148 | if (likely(!is_fixed_type(type))) { |
| 149 | // special case |
| 150 | it->flags |= CborIteratorFlag_UnknownLength; |
| 151 | it->type = type; |
| 152 | return CborNoError; |
| 153 | } |
| 154 | return type == CborSimpleType ? CborErrorUnexpectedBreak : CborErrorIllegalNumber; |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 155 | } |
Thiago Macieira | 54a0e10 | 2015-05-05 21:25:06 -0700 | [diff] [blame] | 156 | |
Thiago Macieira | c70169f | 2015-05-06 07:49:44 -0700 | [diff] [blame] | 157 | size_t bytesNeeded = descriptor < Value8Bit ? 0 : (1 << (descriptor - Value8Bit)); |
| 158 | if (it->ptr + 1 + bytesNeeded > parser->end) |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 159 | return CborErrorUnexpectedEOF; |
Thiago Macieira | c70169f | 2015-05-06 07:49:44 -0700 | [diff] [blame] | 160 | |
Thiago Macieira | 851c481 | 2015-05-08 15:23:20 -0700 | [diff] [blame] | 161 | uint8_t majortype = type >> MajorTypeShift; |
| 162 | if (majortype == NegativeIntegerType) { |
Thiago Macieira | 54a0e10 | 2015-05-05 21:25:06 -0700 | [diff] [blame] | 163 | it->flags |= CborIteratorFlag_NegativeInteger; |
Thiago Macieira | 851c481 | 2015-05-08 15:23:20 -0700 | [diff] [blame] | 164 | it->type = CborIntegerType; |
| 165 | } else if (majortype == SimpleTypesType) { |
Thiago Macieira | 54a0e10 | 2015-05-05 21:25:06 -0700 | [diff] [blame] | 166 | switch (descriptor) { |
| 167 | case FalseValue: |
| 168 | it->extra = false; |
Thiago Macieira | 851c481 | 2015-05-08 15:23:20 -0700 | [diff] [blame] | 169 | it->type = CborBooleanType; |
Thiago Macieira | 991dd92 | 2015-05-07 11:57:59 -0700 | [diff] [blame] | 170 | break; |
| 171 | |
Thiago Macieira | 851c481 | 2015-05-08 15:23:20 -0700 | [diff] [blame] | 172 | case SinglePrecisionFloat: |
| 173 | case DoublePrecisionFloat: |
| 174 | it->flags |= CborIteratorFlag_IntegerValueTooLarge; |
| 175 | // fall through |
Thiago Macieira | 54a0e10 | 2015-05-05 21:25:06 -0700 | [diff] [blame] | 176 | case TrueValue: |
| 177 | case NullValue: |
| 178 | case UndefinedValue: |
| 179 | case HalfPrecisionFloat: |
Thiago Macieira | 851c481 | 2015-05-08 15:23:20 -0700 | [diff] [blame] | 180 | it->type = *it->ptr; |
Thiago Macieira | 54a0e10 | 2015-05-05 21:25:06 -0700 | [diff] [blame] | 181 | break; |
| 182 | |
| 183 | case SimpleTypeInNextByte: |
Thiago Macieira | 851c481 | 2015-05-08 15:23:20 -0700 | [diff] [blame] | 184 | it->extra = (uint8_t)it->ptr[1]; |
Thiago Macieira | 54a0e10 | 2015-05-05 21:25:06 -0700 | [diff] [blame] | 185 | #ifndef CBOR_PARSER_NO_STRICT_CHECKS |
Thiago Macieira | 851c481 | 2015-05-08 15:23:20 -0700 | [diff] [blame] | 186 | if (unlikely(it->extra < 32)) { |
| 187 | it->type = CborInvalidType; |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 188 | return CborErrorIllegalSimpleType; |
Thiago Macieira | 851c481 | 2015-05-08 15:23:20 -0700 | [diff] [blame] | 189 | } |
Thiago Macieira | 54a0e10 | 2015-05-05 21:25:06 -0700 | [diff] [blame] | 190 | #endif |
Thiago Macieira | 991dd92 | 2015-05-07 11:57:59 -0700 | [diff] [blame] | 191 | break; |
| 192 | |
Thiago Macieira | 54a0e10 | 2015-05-05 21:25:06 -0700 | [diff] [blame] | 193 | case 28: |
| 194 | case 29: |
| 195 | case 30: |
Thiago Macieira | 54a0e10 | 2015-05-05 21:25:06 -0700 | [diff] [blame] | 196 | case Break: |
Thiago Macieira | 851c481 | 2015-05-08 15:23:20 -0700 | [diff] [blame] | 197 | assert(false); // these conditions can't be reached |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 198 | return CborErrorUnexpectedBreak; |
Thiago Macieira | 54a0e10 | 2015-05-05 21:25:06 -0700 | [diff] [blame] | 199 | } |
Thiago Macieira | 851c481 | 2015-05-08 15:23:20 -0700 | [diff] [blame] | 200 | return CborNoError; |
Thiago Macieira | 54a0e10 | 2015-05-05 21:25:06 -0700 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | // try to decode up to 16 bits |
| 204 | if (descriptor < Value8Bit) |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 205 | return CborNoError; |
Thiago Macieira | 54a0e10 | 2015-05-05 21:25:06 -0700 | [diff] [blame] | 206 | |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 207 | if (descriptor == Value8Bit) |
| 208 | it->extra = (uint8_t)it->ptr[1]; |
| 209 | else if (descriptor == Value16Bit) |
Thiago Macieira | 54a0e10 | 2015-05-05 21:25:06 -0700 | [diff] [blame] | 210 | it->extra = get16(it->ptr + 1); |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 211 | else |
| 212 | it->flags |= CborIteratorFlag_IntegerValueTooLarge; // Value32Bit or Value64Bit |
| 213 | return CborNoError; |
| 214 | } |
Thiago Macieira | 54a0e10 | 2015-05-05 21:25:06 -0700 | [diff] [blame] | 215 | |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 216 | static CborError preparse_next_value(CborValue *it) |
| 217 | { |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 218 | if (it->remaining != UINT32_MAX) { |
Thiago Macieira | 11e913f | 2015-05-07 13:01:18 -0700 | [diff] [blame] | 219 | // don't decrement the item count if the current item is tag: they don't count |
| 220 | if (it->type != CborTagType && !--it->remaining) { |
| 221 | it->type = CborInvalidType; |
Thiago Macieira | 56d9983 | 2015-05-07 14:34:27 -0700 | [diff] [blame] | 222 | return CborNoError; |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 223 | } |
Thiago Macieira | 5752ce5 | 2015-06-16 12:10:03 -0700 | [diff] [blame] | 224 | } else if (it->remaining == UINT32_MAX && it->ptr != it->parser->end && *it->ptr == (uint8_t)BreakByte) { |
Thiago Macieira | 56d9983 | 2015-05-07 14:34:27 -0700 | [diff] [blame] | 225 | // end of map or array |
| 226 | ++it->ptr; |
| 227 | it->type = CborInvalidType; |
| 228 | it->remaining = 0; |
| 229 | return CborNoError; |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 230 | } |
Thiago Macieira | 56d9983 | 2015-05-07 14:34:27 -0700 | [diff] [blame] | 231 | |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 232 | return preparse_value(it); |
| 233 | } |
| 234 | |
| 235 | static CborError advance_internal(CborValue *it) |
| 236 | { |
| 237 | uint64_t length; |
| 238 | CborError err = extract_number(it->parser, &it->ptr, &length); |
| 239 | assert(err == CborNoError); |
| 240 | |
Thiago Macieira | 56d9983 | 2015-05-07 14:34:27 -0700 | [diff] [blame] | 241 | if (it->type == CborByteStringType || it->type == CborTextStringType) { |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 242 | assert(length == (size_t)length); |
Thiago Macieira | 56d9983 | 2015-05-07 14:34:27 -0700 | [diff] [blame] | 243 | assert((it->flags & CborIteratorFlag_UnknownLength) == 0); |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 244 | it->ptr += length; |
| 245 | } |
| 246 | |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 247 | return preparse_next_value(it); |
Thiago Macieira | 54a0e10 | 2015-05-05 21:25:06 -0700 | [diff] [blame] | 248 | } |
| 249 | |
Thiago Macieira | 2312efd | 2015-05-06 16:07:48 -0700 | [diff] [blame] | 250 | /** \internal |
| 251 | * |
| 252 | * Decodes the CBOR integer value when it is larger than the 16 bits available |
| 253 | * in value->extra. This function requires that value->flags have the |
| 254 | * CborIteratorFlag_IntegerValueTooLarge flag set. |
| 255 | * |
| 256 | * This function is also used to extract single- and double-precision floating |
| 257 | * point values (SinglePrecisionFloat == Value32Bit and DoublePrecisionFloat == |
| 258 | * Value64Bit). |
| 259 | */ |
Thiago Macieira | 54a0e10 | 2015-05-05 21:25:06 -0700 | [diff] [blame] | 260 | uint64_t _cbor_value_decode_int64_internal(const CborValue *value) |
| 261 | { |
Thiago Macieira | 2312efd | 2015-05-06 16:07:48 -0700 | [diff] [blame] | 262 | assert(value->flags & CborIteratorFlag_IntegerValueTooLarge || |
| 263 | value->type == CborFloatType || value->type == CborDoubleType); |
Thiago Macieira | 851c481 | 2015-05-08 15:23:20 -0700 | [diff] [blame] | 264 | |
| 265 | // since the additional information can only be Value32Bit or Value64Bit, |
| 266 | // we just need to test for the one bit those two options differ |
| 267 | assert((*value->ptr & SmallValueMask) == Value32Bit || (*value->ptr & SmallValueMask) == Value64Bit); |
| 268 | if ((*value->ptr & 1) == (Value32Bit & 1)) |
Thiago Macieira | 54a0e10 | 2015-05-05 21:25:06 -0700 | [diff] [blame] | 269 | return get32(value->ptr + 1); |
| 270 | |
| 271 | assert((*value->ptr & SmallValueMask) == Value64Bit); |
| 272 | return get64(value->ptr + 1); |
| 273 | } |
| 274 | |
| 275 | /** |
| 276 | * Initializes the CBOR parser for parsing \a size bytes beginning at \a |
| 277 | * buffer. Parsing will use flags set in \a flags. The iterator to the first |
| 278 | * element is returned in \a it. |
Thiago Macieira | 2312efd | 2015-05-06 16:07:48 -0700 | [diff] [blame] | 279 | * |
| 280 | * The \a parser structure needs to remain valid throughout the decoding |
| 281 | * process. It is not thread-safe to share one CborParser among multiple |
| 282 | * threads iterating at the same time, but the object can be copied so multiple |
| 283 | * threads can iterate. |
| 284 | * |
| 285 | * ### Write how to determine the end pointer |
| 286 | * ### Write how to do limited-buffer windowed decoding |
Thiago Macieira | 54a0e10 | 2015-05-05 21:25:06 -0700 | [diff] [blame] | 287 | */ |
Thiago Macieira | 5752ce5 | 2015-06-16 12:10:03 -0700 | [diff] [blame] | 288 | 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] | 289 | { |
| 290 | memset(parser, 0, sizeof(*parser)); |
| 291 | parser->end = buffer + size; |
Thiago Macieira | 54a0e10 | 2015-05-05 21:25:06 -0700 | [diff] [blame] | 292 | parser->flags = flags; |
| 293 | it->parser = parser; |
| 294 | it->ptr = buffer; |
| 295 | 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] | 296 | return preparse_value(it); |
Thiago Macieira | 2312efd | 2015-05-06 16:07:48 -0700 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | /** |
| 300 | * Advances the CBOR value \a it by one fixed-size position. Fixed-size types |
| 301 | * are: integers, tags, simple types (including boolean, null and undefined |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 302 | * values) and floating point types. |
Thiago Macieira | 2312efd | 2015-05-06 16:07:48 -0700 | [diff] [blame] | 303 | * |
| 304 | * \sa cbor_value_at_end(), cbor_value_advance(), cbor_value_begin_recurse(), cbor_value_end_recurse() |
| 305 | */ |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 306 | CborError cbor_value_advance_fixed(CborValue *it) |
Thiago Macieira | 54a0e10 | 2015-05-05 21:25:06 -0700 | [diff] [blame] | 307 | { |
Thiago Macieira | 2312efd | 2015-05-06 16:07:48 -0700 | [diff] [blame] | 308 | assert(it->type != CborInvalidType); |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 309 | assert(is_fixed_type(it->type)); |
| 310 | if (!it->remaining) |
| 311 | return CborErrorAdvancePastEOF; |
| 312 | return advance_internal(it); |
Thiago Macieira | 54a0e10 | 2015-05-05 21:25:06 -0700 | [diff] [blame] | 313 | } |
| 314 | |
Thiago Macieira | 4a99af9 | 2015-05-12 10:41:45 +0900 | [diff] [blame] | 315 | static CborError advance_recursive(CborValue *it, int nestingLevel) |
| 316 | { |
| 317 | if (is_fixed_type(it->type)) |
| 318 | return advance_internal(it); |
| 319 | |
| 320 | if (!cbor_value_is_container(it)) { |
| 321 | size_t len = SIZE_MAX; |
| 322 | return cbor_value_copy_string(it, NULL, &len, it); |
| 323 | } |
| 324 | |
| 325 | // map or array |
| 326 | if (nestingLevel == CBOR_PARSER_MAX_RECURSIONS) |
| 327 | return CborErrorNestingTooDeep; |
| 328 | |
| 329 | CborError err; |
| 330 | CborValue recursed; |
| 331 | err = cbor_value_enter_container(it, &recursed); |
| 332 | if (err) |
| 333 | return err; |
| 334 | while (!cbor_value_at_end(&recursed)) { |
| 335 | err = advance_recursive(&recursed, nestingLevel + 1); |
| 336 | if (err) |
| 337 | return err; |
| 338 | } |
| 339 | return cbor_value_leave_container(it, &recursed); |
| 340 | } |
| 341 | |
| 342 | |
Thiago Macieira | 2312efd | 2015-05-06 16:07:48 -0700 | [diff] [blame] | 343 | /** |
| 344 | * Advances the CBOR value \a it by one element, skipping over containers. |
| 345 | * Unlike cbor_value_advance_fixed(), this function can be called on a CBOR |
| 346 | * value of any type. However, if the type is a container (map or array) or a |
| 347 | * string with a chunked payload, this function will not run in constant time |
| 348 | * and will recurse into itself (it will run on O(n) time for the number of |
| 349 | * elements or chunks and will use O(n) memory for the number of nested |
| 350 | * containers). |
| 351 | * |
Thiago Macieira | 2312efd | 2015-05-06 16:07:48 -0700 | [diff] [blame] | 352 | * \sa cbor_value_at_end(), cbor_value_advance_fixed(), cbor_value_begin_recurse(), cbor_value_end_recurse() |
| 353 | */ |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 354 | CborError cbor_value_advance(CborValue *it) |
Thiago Macieira | 2312efd | 2015-05-06 16:07:48 -0700 | [diff] [blame] | 355 | { |
| 356 | assert(it->type != CborInvalidType); |
| 357 | if (!it->remaining) |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 358 | return CborErrorAdvancePastEOF; |
Thiago Macieira | 4a99af9 | 2015-05-12 10:41:45 +0900 | [diff] [blame] | 359 | return advance_recursive(it, 0); |
Thiago Macieira | 2312efd | 2015-05-06 16:07:48 -0700 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | /** |
Thiago Macieira | c4a73c6 | 2015-05-09 18:14:11 -0700 | [diff] [blame] | 363 | * Advances the CBOR value \a it until it no longer points to a tag. If \a it is |
| 364 | * already not pointing to a tag, then this function returns it unchanged. |
| 365 | * |
| 366 | * \sa cbor_value_advance_fixed(), cbor_value_advance() |
| 367 | */ |
| 368 | CborError cbor_value_skip_tag(CborValue *it) |
| 369 | { |
| 370 | while (cbor_value_is_tag(it)) { |
| 371 | CborError err = cbor_value_advance_fixed(it); |
| 372 | if (err) |
| 373 | return err; |
| 374 | } |
| 375 | return CborNoError; |
| 376 | } |
| 377 | |
Thiago Macieira | c4a73c6 | 2015-05-09 18:14:11 -0700 | [diff] [blame] | 378 | /** |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 379 | * \fn bool cbor_value_is_container(const CborValue *it) |
| 380 | * |
Thiago Macieira | 2312efd | 2015-05-06 16:07:48 -0700 | [diff] [blame] | 381 | * Returns true if the \a it value is a container and requires recursion in |
| 382 | * order to decode (maps and arrays), false otherwise. |
| 383 | */ |
Thiago Macieira | 54a0e10 | 2015-05-05 21:25:06 -0700 | [diff] [blame] | 384 | |
Thiago Macieira | 2312efd | 2015-05-06 16:07:48 -0700 | [diff] [blame] | 385 | /** |
| 386 | * Creates a CborValue iterator pointing to the first element of the container |
| 387 | * represented by \a it and saves it in \a recursed. The \a it container object |
| 388 | * needs to be kept and passed again to cbor_value_leave_container() in order |
| 389 | * to continue iterating past this container. |
| 390 | * |
| 391 | * \sa cbor_value_is_container(), cbor_value_leave_container(), cbor_value_advance() |
| 392 | */ |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 393 | CborError cbor_value_enter_container(const CborValue *it, CborValue *recursed) |
Thiago Macieira | 54a0e10 | 2015-05-05 21:25:06 -0700 | [diff] [blame] | 394 | { |
Thiago Macieira | 56d9983 | 2015-05-07 14:34:27 -0700 | [diff] [blame] | 395 | CborError err; |
Thiago Macieira | 2312efd | 2015-05-06 16:07:48 -0700 | [diff] [blame] | 396 | assert(cbor_value_is_container(it)); |
Thiago Macieira | 54a0e10 | 2015-05-05 21:25:06 -0700 | [diff] [blame] | 397 | *recursed = *it; |
Thiago Macieira | 56d9983 | 2015-05-07 14:34:27 -0700 | [diff] [blame] | 398 | |
Thiago Macieira | 54a0e10 | 2015-05-05 21:25:06 -0700 | [diff] [blame] | 399 | if (it->flags & CborIteratorFlag_UnknownLength) { |
| 400 | recursed->remaining = UINT32_MAX; |
Thiago Macieira | 56d9983 | 2015-05-07 14:34:27 -0700 | [diff] [blame] | 401 | ++recursed->ptr; |
| 402 | err = preparse_value(recursed); |
| 403 | if (err != CborErrorUnexpectedBreak) |
| 404 | return err; |
| 405 | // actually, break was expected here |
| 406 | // it's just an empty container |
| 407 | ++recursed->ptr; |
Thiago Macieira | 54a0e10 | 2015-05-05 21:25:06 -0700 | [diff] [blame] | 408 | } else { |
Thiago Macieira | 56d9983 | 2015-05-07 14:34:27 -0700 | [diff] [blame] | 409 | uint64_t len; |
| 410 | err = extract_number(recursed->parser, &recursed->ptr, &len); |
| 411 | assert(err == CborNoError); |
Thiago Macieira | 56d9983 | 2015-05-07 14:34:27 -0700 | [diff] [blame] | 412 | |
Thiago Macieira | 54a0e10 | 2015-05-05 21:25:06 -0700 | [diff] [blame] | 413 | recursed->remaining = len; |
Thiago Macieira | 3f76f63 | 2015-05-12 10:10:09 +0900 | [diff] [blame] | 414 | if (recursed->remaining != len || len == UINT32_MAX) { |
| 415 | // back track the pointer to indicate where the error occurred |
| 416 | recursed->ptr = it->ptr; |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 417 | return CborErrorDataTooLarge; |
Thiago Macieira | 3f76f63 | 2015-05-12 10:10:09 +0900 | [diff] [blame] | 418 | } |
Thiago Macieira | ce16f05 | 2015-05-07 23:14:25 -0700 | [diff] [blame] | 419 | if (recursed->type == CborMapType) { |
| 420 | // maps have keys and values, so we need to multiply by 2 |
Thiago Macieira | 3f76f63 | 2015-05-12 10:10:09 +0900 | [diff] [blame] | 421 | if (recursed->remaining > UINT32_MAX / 2) { |
| 422 | // back track the pointer to indicate where the error occurred |
| 423 | recursed->ptr = it->ptr; |
Thiago Macieira | ce16f05 | 2015-05-07 23:14:25 -0700 | [diff] [blame] | 424 | return CborErrorDataTooLarge; |
Thiago Macieira | 3f76f63 | 2015-05-12 10:10:09 +0900 | [diff] [blame] | 425 | } |
Thiago Macieira | ce16f05 | 2015-05-07 23:14:25 -0700 | [diff] [blame] | 426 | recursed->remaining *= 2; |
| 427 | } |
Thiago Macieira | 56d9983 | 2015-05-07 14:34:27 -0700 | [diff] [blame] | 428 | if (len != 0) |
| 429 | return preparse_value(recursed); |
Thiago Macieira | 54a0e10 | 2015-05-05 21:25:06 -0700 | [diff] [blame] | 430 | } |
Thiago Macieira | 56d9983 | 2015-05-07 14:34:27 -0700 | [diff] [blame] | 431 | |
| 432 | // the case of the empty container |
| 433 | recursed->type = CborInvalidType; |
| 434 | recursed->remaining = 0; |
| 435 | return CborNoError; |
Thiago Macieira | c70169f | 2015-05-06 07:49:44 -0700 | [diff] [blame] | 436 | } |
| 437 | |
Thiago Macieira | 2312efd | 2015-05-06 16:07:48 -0700 | [diff] [blame] | 438 | /** |
| 439 | * 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] | 440 | * recursed object needs to point to the element obtained either by advancing |
| 441 | * the last element of the container (via cbor_value_advance(), |
| 442 | * cbor_value_advance_fixed(), a nested cbor_value_leave_container(), or the \c |
| 443 | * next pointer from cbor_value_copy_string() or cbor_value_dup_string()). |
Thiago Macieira | 2312efd | 2015-05-06 16:07:48 -0700 | [diff] [blame] | 444 | * |
| 445 | * \sa cbor_value_enter_container(), cbor_value_at_end() |
| 446 | */ |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 447 | CborError cbor_value_leave_container(CborValue *it, const CborValue *recursed) |
Thiago Macieira | c70169f | 2015-05-06 07:49:44 -0700 | [diff] [blame] | 448 | { |
Thiago Macieira | 2312efd | 2015-05-06 16:07:48 -0700 | [diff] [blame] | 449 | assert(cbor_value_is_container(it)); |
Thiago Macieira | 56d9983 | 2015-05-07 14:34:27 -0700 | [diff] [blame] | 450 | assert(recursed->type == CborInvalidType); |
Thiago Macieira | c70169f | 2015-05-06 07:49:44 -0700 | [diff] [blame] | 451 | it->ptr = recursed->ptr; |
Thiago Macieira | 56d9983 | 2015-05-07 14:34:27 -0700 | [diff] [blame] | 452 | return preparse_next_value(it); |
Thiago Macieira | c70169f | 2015-05-06 07:49:44 -0700 | [diff] [blame] | 453 | } |
| 454 | |
Thiago Macieira | 2312efd | 2015-05-06 16:07:48 -0700 | [diff] [blame] | 455 | /** |
| 456 | * Calculates the length of the string in \a value and stores the result in \a |
| 457 | * len. This function is different from cbor_value_get_string_length() in that |
| 458 | * it calculates the length even for strings sent in chunks. For that reason, |
| 459 | * this function may not run in constant time (it will run in O(n) time on the |
| 460 | * number of chunks). |
| 461 | * |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 462 | * \note On 32-bit platforms, this function will return error condition of \ref |
| 463 | * CborErrorDataTooLarge if the stream indicates a length that is too big to |
| 464 | * fit in 32-bit. |
Thiago Macieira | 2312efd | 2015-05-06 16:07:48 -0700 | [diff] [blame] | 465 | * |
| 466 | * \sa cbor_value_get_string_length(), cbor_value_copy_string(), cbor_value_is_length_known() |
| 467 | */ |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 468 | CborError cbor_value_calculate_string_length(const CborValue *value, size_t *len) |
Thiago Macieira | 2312efd | 2015-05-06 16:07:48 -0700 | [diff] [blame] | 469 | { |
Thiago Macieira | 9ae0581 | 2015-05-11 15:09:09 +0900 | [diff] [blame] | 470 | *len = SIZE_MAX; |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 471 | return cbor_value_copy_string(value, NULL, len, NULL); |
Thiago Macieira | c70169f | 2015-05-06 07:49:44 -0700 | [diff] [blame] | 472 | } |
| 473 | |
Thiago Macieira | 2312efd | 2015-05-06 16:07:48 -0700 | [diff] [blame] | 474 | /** |
| 475 | * Allocates memory for the string pointed by \a value and copies it into this |
| 476 | * buffer. The pointer to the buffer is stored in \a buffer and the number of |
| 477 | * bytes copied is stored in \a len (those variables must not be NULL). |
| 478 | * |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 479 | * If \c malloc returns a NULL pointer, this function will return error |
| 480 | * condition \ref CborErrorOutOfMemory. |
Thiago Macieira | 2312efd | 2015-05-06 16:07:48 -0700 | [diff] [blame] | 481 | * |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 482 | * On success, \c{*buffer} will contain a valid pointer that must be freed by |
| 483 | * calling \c{free()}. This is the case even for zero-length strings. |
Thiago Macieira | 2312efd | 2015-05-06 16:07:48 -0700 | [diff] [blame] | 484 | * |
| 485 | * 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] | 486 | * after this string. If \a value points to the last item, then \a next will be |
| 487 | * invalid. |
Thiago Macieira | 2312efd | 2015-05-06 16:07:48 -0700 | [diff] [blame] | 488 | * |
| 489 | * \note This function does not perform UTF-8 validation on the incoming text |
| 490 | * string. |
| 491 | * |
| 492 | * \sa cbor_value_copy_string() |
| 493 | */ |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 494 | CborError cbor_value_dup_string(const CborValue *value, char **buffer, size_t *buflen, CborValue *next) |
Thiago Macieira | c70169f | 2015-05-06 07:49:44 -0700 | [diff] [blame] | 495 | { |
Thiago Macieira | 2312efd | 2015-05-06 16:07:48 -0700 | [diff] [blame] | 496 | assert(buffer); |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 497 | assert(buflen); |
Thiago Macieira | 851c481 | 2015-05-08 15:23:20 -0700 | [diff] [blame] | 498 | CborError err = cbor_value_copy_string(value, NULL, buflen, NULL); |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 499 | if (err) |
| 500 | return err; |
Thiago Macieira | c70169f | 2015-05-06 07:49:44 -0700 | [diff] [blame] | 501 | |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 502 | ++*buflen; |
| 503 | *buffer = malloc(*buflen); |
Thiago Macieira | 2312efd | 2015-05-06 16:07:48 -0700 | [diff] [blame] | 504 | if (!*buffer) { |
Thiago Macieira | c70169f | 2015-05-06 07:49:44 -0700 | [diff] [blame] | 505 | // out of memory |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 506 | return CborErrorOutOfMemory; |
Thiago Macieira | c70169f | 2015-05-06 07:49:44 -0700 | [diff] [blame] | 507 | } |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 508 | err = cbor_value_copy_string(value, *buffer, buflen, next); |
| 509 | if (err) { |
Thiago Macieira | 2312efd | 2015-05-06 16:07:48 -0700 | [diff] [blame] | 510 | free(*buffer); |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 511 | return err; |
Thiago Macieira | c70169f | 2015-05-06 07:49:44 -0700 | [diff] [blame] | 512 | } |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 513 | return CborNoError; |
Thiago Macieira | 2312efd | 2015-05-06 16:07:48 -0700 | [diff] [blame] | 514 | } |
| 515 | |
Thiago Macieira | 9ae0581 | 2015-05-11 15:09:09 +0900 | [diff] [blame] | 516 | // We return uintptr_t so that we can pass memcpy directly as the iteration |
| 517 | // function. The choice is to optimize for memcpy, which is used in the base |
| 518 | // parser API (cbor_value_copy_string), while memcmp is used in convenience API |
| 519 | // only. |
Thiago Macieira | 5752ce5 | 2015-06-16 12:10:03 -0700 | [diff] [blame] | 520 | typedef uintptr_t (*IterateFunction)(char *, const uint8_t *, size_t); |
Thiago Macieira | 9ae0581 | 2015-05-11 15:09:09 +0900 | [diff] [blame] | 521 | |
Thiago Macieira | 5752ce5 | 2015-06-16 12:10:03 -0700 | [diff] [blame] | 522 | 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] | 523 | { |
| 524 | (void)dest; |
| 525 | (void)src; |
| 526 | (void)len; |
| 527 | return true; |
| 528 | } |
| 529 | |
Thiago Macieira | 5752ce5 | 2015-06-16 12:10:03 -0700 | [diff] [blame] | 530 | 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] | 531 | { |
Thiago Macieira | 5752ce5 | 2015-06-16 12:10:03 -0700 | [diff] [blame] | 532 | return memcmp(s1, (const char *)s2, len) == 0; |
Thiago Macieira | c4a73c6 | 2015-05-09 18:14:11 -0700 | [diff] [blame] | 533 | } |
| 534 | |
Thiago Macieira | 9ae0581 | 2015-05-11 15:09:09 +0900 | [diff] [blame] | 535 | static CborError iterate_string_chunks(const CborValue *value, char *buffer, size_t *buflen, |
| 536 | bool *result, CborValue *next, IterateFunction func) |
| 537 | { |
| 538 | assert(cbor_value_is_byte_string(value) || cbor_value_is_text_string(value)); |
| 539 | |
| 540 | size_t total; |
| 541 | CborError err; |
Thiago Macieira | 5752ce5 | 2015-06-16 12:10:03 -0700 | [diff] [blame] | 542 | const uint8_t *ptr = value->ptr; |
Thiago Macieira | 9ae0581 | 2015-05-11 15:09:09 +0900 | [diff] [blame] | 543 | if (cbor_value_is_length_known(value)) { |
| 544 | // easy case: fixed length |
| 545 | err = extract_length(value->parser, &ptr, &total); |
| 546 | if (err) |
| 547 | return err; |
| 548 | if (ptr + total > value->parser->end) |
| 549 | return CborErrorUnexpectedEOF; |
| 550 | if (total <= *buflen) |
| 551 | *result = func(buffer, ptr, total); |
| 552 | else |
| 553 | *result = false; |
| 554 | ptr += total; |
| 555 | } else { |
| 556 | // chunked |
| 557 | ++ptr; |
| 558 | total = 0; |
| 559 | *result = true; |
| 560 | while (true) { |
| 561 | size_t chunkLen; |
| 562 | size_t newTotal; |
| 563 | |
| 564 | if (ptr == value->parser->end) |
| 565 | return CborErrorUnexpectedEOF; |
| 566 | |
Thiago Macieira | 5752ce5 | 2015-06-16 12:10:03 -0700 | [diff] [blame] | 567 | if (*ptr == (uint8_t)BreakByte) { |
Thiago Macieira | 9ae0581 | 2015-05-11 15:09:09 +0900 | [diff] [blame] | 568 | ++ptr; |
| 569 | break; |
| 570 | } |
| 571 | |
| 572 | // is this the right type? |
| 573 | if ((*ptr & MajorTypeMask) != value->type) |
| 574 | return CborErrorIllegalType; |
| 575 | |
| 576 | err = extract_length(value->parser, &ptr, &chunkLen); |
| 577 | if (err) |
| 578 | return err; |
| 579 | |
Thiago Macieira | 1de31a4 | 2015-06-16 16:01:16 -0700 | [diff] [blame] | 580 | if (unlikely(add_check_overflow(total, chunkLen, &newTotal))) |
Thiago Macieira | 9ae0581 | 2015-05-11 15:09:09 +0900 | [diff] [blame] | 581 | return CborErrorDataTooLarge; |
| 582 | |
| 583 | if (ptr + chunkLen > value->parser->end) |
| 584 | return CborErrorUnexpectedEOF; |
| 585 | |
| 586 | if (*result && *buflen >= newTotal) |
| 587 | *result = func(buffer + total, ptr, chunkLen); |
| 588 | else |
| 589 | *result = false; |
| 590 | |
| 591 | ptr += chunkLen; |
| 592 | total = newTotal; |
| 593 | } |
| 594 | } |
| 595 | |
| 596 | // is there enough room for the ending NUL byte? |
| 597 | if (*result && *buflen > total) |
Thiago Macieira | 5752ce5 | 2015-06-16 12:10:03 -0700 | [diff] [blame] | 598 | *result = func(buffer + total, (const uint8_t *)"", 1); |
Thiago Macieira | 9ae0581 | 2015-05-11 15:09:09 +0900 | [diff] [blame] | 599 | *buflen = total; |
| 600 | |
| 601 | if (next) { |
| 602 | *next = *value; |
| 603 | next->ptr = ptr; |
| 604 | return preparse_next_value(next); |
| 605 | } |
| 606 | return CborNoError; |
| 607 | } |
| 608 | |
Thiago Macieira | 2312efd | 2015-05-06 16:07:48 -0700 | [diff] [blame] | 609 | /** |
| 610 | * Copies the string pointed by \a value into the buffer provided at \a buffer |
| 611 | * of \a buflen bytes. If \a buffer is a NULL pointer, this function will not |
| 612 | * copy anything and will only update the \a next value. |
| 613 | * |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 614 | * If the provided buffer length was too small, this function returns an error |
| 615 | * condition of \ref CborErrorOutOfMemory. If you need to calculate the length |
| 616 | * of the string in order to preallocate a buffer, use |
Thiago Macieira | 2312efd | 2015-05-06 16:07:48 -0700 | [diff] [blame] | 617 | * cbor_value_calculate_string_length(). |
| 618 | * |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 619 | * On success, this function sets the number of bytes copied to \c{*buflen}. If |
| 620 | * the buffer is large enough, this function will insert a null byte after the |
| 621 | * last copied byte, to facilitate manipulation of text strings. That byte is |
| 622 | * not included in the returned value of \c{*buflen}. |
Thiago Macieira | 2312efd | 2015-05-06 16:07:48 -0700 | [diff] [blame] | 623 | * |
| 624 | * The \a next pointer, if not null, will be updated to point to the next item |
| 625 | * after this string. If \a value points to the last item, then \a next will be |
| 626 | * invalid. |
| 627 | * |
| 628 | * \note This function does not perform UTF-8 validation on the incoming text |
| 629 | * string. |
| 630 | * |
| 631 | * \sa cbor_value_dup_string(), cbor_value_get_string_length(), cbor_value_calculate_string_length() |
| 632 | */ |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 633 | CborError cbor_value_copy_string(const CborValue *value, char *buffer, |
| 634 | size_t *buflen, CborValue *next) |
Thiago Macieira | 2312efd | 2015-05-06 16:07:48 -0700 | [diff] [blame] | 635 | { |
Thiago Macieira | 9ae0581 | 2015-05-11 15:09:09 +0900 | [diff] [blame] | 636 | bool copied_all; |
| 637 | CborError err = iterate_string_chunks(value, buffer, buflen, &copied_all, next, |
| 638 | buffer ? (IterateFunction)memcpy : iterate_noop); |
| 639 | return err ? err : |
| 640 | copied_all ? CborNoError : CborErrorOutOfMemory; |
Thiago Macieira | c70169f | 2015-05-06 07:49:44 -0700 | [diff] [blame] | 641 | } |
| 642 | |
Thiago Macieira | c4a73c6 | 2015-05-09 18:14:11 -0700 | [diff] [blame] | 643 | /** |
| 644 | * Compares the entry \a value with the string \a string and store the result |
| 645 | * in \a result. If the value is different from \a string or if it is not a |
| 646 | * text string, \a result will contain \c false. |
| 647 | * |
| 648 | * The entry at \a value may be a tagged string. If \a is not a string or a |
| 649 | * tagged string, the comparison result will be false. |
| 650 | */ |
| 651 | CborError cbor_value_text_string_equals(const CborValue *value, const char *string, bool *result) |
| 652 | { |
| 653 | CborValue copy = *value; |
| 654 | CborError err = cbor_value_skip_tag(©); |
| 655 | if (err) |
| 656 | return err; |
| 657 | if (!cbor_value_is_text_string(©)) { |
| 658 | *result = false; |
| 659 | return CborNoError; |
| 660 | } |
| 661 | |
| 662 | size_t len = strlen(string); |
| 663 | return iterate_string_chunks(©, CONST_CAST(char *, string), &len, result, NULL, iterate_memcmp); |
| 664 | } |
| 665 | |
| 666 | /** |
Thiago Macieira | 7b623c2 | 2015-05-11 15:52:14 +0900 | [diff] [blame] | 667 | * Attempts to find the value in map \a map that corresponds to the text string |
| 668 | * entry \a string. If the item is found, it is stored in \a result. If no item |
| 669 | * is found matching the key, then \a result will contain an element of type |
| 670 | * \ref CborInvalidType. |
| 671 | * |
| 672 | * \note This function may be expensive to execute. |
| 673 | */ |
| 674 | CborError cbor_value_map_find_value(const CborValue *map, const char *string, CborValue *element) |
| 675 | { |
| 676 | assert(cbor_value_is_map(map)); |
| 677 | size_t len = strlen(string); |
| 678 | CborError err = cbor_value_enter_container(map, element); |
| 679 | if (err) |
| 680 | goto error; |
| 681 | |
| 682 | while (!cbor_value_at_end(element)) { |
| 683 | // find the non-tag so we can compare |
| 684 | err = cbor_value_skip_tag(element); |
| 685 | if (err) |
| 686 | goto error; |
| 687 | if (cbor_value_is_text_string(element)) { |
| 688 | bool equals; |
| 689 | size_t dummyLen = len; |
| 690 | err = iterate_string_chunks(element, CONST_CAST(char *, string), &dummyLen, |
| 691 | &equals, element, iterate_memcmp); |
| 692 | if (err) |
| 693 | goto error; |
| 694 | if (equals) |
| 695 | return preparse_value(element); |
| 696 | } else { |
| 697 | // skip this key |
| 698 | err = cbor_value_advance(element); |
| 699 | if (err) |
| 700 | goto error; |
| 701 | } |
| 702 | |
| 703 | // skip this value |
| 704 | err = cbor_value_skip_tag(element); |
| 705 | if (err) |
| 706 | goto error; |
| 707 | err = cbor_value_advance(element); |
| 708 | if (err) |
| 709 | goto error; |
| 710 | } |
| 711 | |
| 712 | // not found |
| 713 | element->type = CborInvalidType; |
| 714 | return CborNoError; |
| 715 | |
| 716 | error: |
| 717 | element->type = CborInvalidType; |
| 718 | return err; |
| 719 | } |
| 720 | |
| 721 | /** |
Thiago Macieira | c4a73c6 | 2015-05-09 18:14:11 -0700 | [diff] [blame] | 722 | * Extracts a half-precision floating point from \a value and stores it in \a |
| 723 | * result. |
| 724 | */ |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 725 | CborError cbor_value_get_half_float(const CborValue *value, void *result) |
Thiago Macieira | c70169f | 2015-05-06 07:49:44 -0700 | [diff] [blame] | 726 | { |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 727 | assert(value->type == CborHalfFloatType); |
Thiago Macieira | c70169f | 2015-05-06 07:49:44 -0700 | [diff] [blame] | 728 | |
| 729 | // size has been computed already |
| 730 | uint16_t v = get16(value->ptr + 1); |
| 731 | memcpy(result, &v, sizeof(v)); |
Thiago Macieira | a43a4ef | 2015-05-06 20:25:18 -0700 | [diff] [blame] | 732 | return CborNoError; |
Thiago Macieira | 54a0e10 | 2015-05-05 21:25:06 -0700 | [diff] [blame] | 733 | } |