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