blob: aeb9a415fb7871904723eb007efd9824be5ac5bf [file] [log] [blame]
Thiago Macieira54a0e102015-05-05 21:25:06 -07001/****************************************************************************
2**
Thiago Macieira51b56062017-02-23 16:03:13 -08003** Copyright (C) 2017 Intel Corporation
Thiago Macieira54a0e102015-05-05 21:25:06 -07004**
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
Vipul Rahane650c31f2017-12-15 12:48:40 -080025#ifndef _BSD_SOURCE
Thiago Macieiraed5b57c2015-07-07 16:38:27 -070026#define _BSD_SOURCE 1
Vipul Rahane650c31f2017-12-15 12:48:40 -080027#endif
28#ifndef _DEFAULT_SOURCE
Otavio Pontese2d5dd52016-07-08 09:49:38 -030029#define _DEFAULT_SOURCE 1
Vipul Rahane650c31f2017-12-15 12:48:40 -080030#endif
Thiago Macieira86c81862016-08-04 13:56:48 -070031#ifndef __STDC_LIMIT_MACROS
32# define __STDC_LIMIT_MACROS 1
33#endif
34
Thiago Macieira54a0e102015-05-05 21:25:06 -070035#include "cbor.h"
Thiago Macieiracf3116e2017-03-04 23:36:23 -060036#include "cborinternal_p.h"
Thiago Macieira54a0e102015-05-05 21:25:06 -070037#include "compilersupport_p.h"
38
Thiago Macieira54a0e102015-05-05 21:25:06 -070039#include <string.h>
40
41/**
Thiago Macieira46a818e2015-10-08 15:13:05 +020042 * \defgroup CborParsing Parsing CBOR streams
43 * \brief Group of functions used to parse CBOR streams.
Thiago Macieira54a0e102015-05-05 21:25:06 -070044 *
Thiago Macieira46a818e2015-10-08 15:13:05 +020045 * TinyCBOR provides functions for pull-based stream parsing of a CBOR-encoded
46 * payload. The main data type for the parsing is a CborValue, which behaves
47 * like an iterator and can be used to extract the encoded data. It is first
48 * initialized with a call to cbor_parser_init() and is usually used to extract
49 * exactly one item, most often an array or map.
Thiago Macieira54a0e102015-05-05 21:25:06 -070050 *
Thiago Macieira46a818e2015-10-08 15:13:05 +020051 * Nested CborValue objects can be parsed using cbor_value_enter_container().
52 * Each call to cbor_value_enter_container() must be matched by a call to
53 * cbor_value_leave_container(), with the exact same parameters.
Thiago Macieira54a0e102015-05-05 21:25:06 -070054 *
Thiago Macieira46a818e2015-10-08 15:13:05 +020055 * The example below initializes a CborParser object, begins the parsing with a
56 * CborValue and decodes a single integer:
57 *
58 * \code
59 * int extract_int(const uint8_t *buffer, size_t len)
60 * {
61 * CborParser parser;
62 * CborValue value;
63 * int result;
Thiago Macieiraf5a172b2018-02-05 14:53:14 -080064 * cbor_parser_init(buffer, len, 0, &parser, &value);
Thiago Macieira46a818e2015-10-08 15:13:05 +020065 * cbor_value_get_int(&value, &result);
66 * return result;
67 * }
68 * \endcode
69 *
70 * The code above does no error checking, which means it assumes the data comes
71 * from a source trusted to send one properly-encoded integer. The following
Thiago Macieiraf5a172b2018-02-05 14:53:14 -080072 * example does the exact same operation, but includes error checking and
Thiago Macieira46a818e2015-10-08 15:13:05 +020073 * returns 0 on parsing failure:
74 *
75 * \code
76 * int extract_int(const uint8_t *buffer, size_t len)
77 * {
78 * CborParser parser;
79 * CborValue value;
80 * int result;
Thiago Macieiraf5a172b2018-02-05 14:53:14 -080081 * if (cbor_parser_init(buffer, len, 0, &parser, &value) != CborNoError)
Thiago Macieira46a818e2015-10-08 15:13:05 +020082 * return 0;
83 * if (!cbor_value_is_integer(&value) ||
84 * cbor_value_get_int(&value, &result) != CborNoError)
85 * return 0;
86 * return result;
87 * }
88 * \endcode
89 *
90 * Note, in the example above, that one can't distinguish a parsing failure
91 * from an encoded value of zero. Reporting a parsing error is left as an
92 * exercise to the reader.
93 *
94 * The code above does not execute a range-check either: it is possible that
95 * the value decoded from the CBOR stream encodes a number larger than what can
96 * be represented in a variable of type \c{int}. If detecting that case is
97 * important, the code should call cbor_value_get_int_checked() instead.
98 *
99 * <h3 class="groupheader">Memory and parsing constraints</h3>
100 *
101 * TinyCBOR is designed to run with little memory and with minimal overhead.
102 * Except where otherwise noted, the parser functions always run on constant
103 * time (O(1)), do not recurse and never allocate memory (thus, stack usage is
104 * bounded and is O(1)).
105 *
106 * <h3 class="groupheader">Error handling and preconditions</h3>
107 *
108 * All functions operating on a CborValue return a CborError condition, with
109 * CborNoError standing for the normal situation in which no parsing error
110 * occurred. All functions may return parsing errors in case the stream cannot
111 * be decoded properly, be it due to corrupted data or due to reaching the end
112 * of the input buffer.
113 *
114 * Error conditions must not be ignored. All decoder functions have undefined
115 * behavior if called after an error has been reported, and may crash.
116 *
117 * Some functions are also documented to have preconditions, like
118 * cbor_value_get_int() requiring that the input be an integral value.
119 * Violation of preconditions also results in undefined behavior and the
120 * program may crash.
121 */
122
123/**
124 * \addtogroup CborParsing
125 * @{
126 */
127
128/**
129 * \struct CborValue
130 *
131 * This type contains one value parsed from the CBOR stream. Each CborValue
132 * behaves as an iterator in a StAX-style parser.
133 *
134 * \if privatedocs
Thiago Macieira54a0e102015-05-05 21:25:06 -0700135 * Implementation details: the CborValue contains these fields:
136 * \list
137 * \li ptr: pointer to the actual data
138 * \li flags: flags from the decoder
Thiago Macieira2312efd2015-05-06 16:07:48 -0700139 * \li extra: partially decoded integer value (0, 1 or 2 bytes)
Thiago Macieira54a0e102015-05-05 21:25:06 -0700140 * \li remaining: remaining items in this collection after this item or UINT32_MAX if length is unknown
141 * \endlist
Thiago Macieira46a818e2015-10-08 15:13:05 +0200142 * \endif
Thiago Macieira54a0e102015-05-05 21:25:06 -0700143 */
144
Thiago Macieira2c22d712017-03-05 00:17:35 -0600145static inline uint16_t get16(const uint8_t *ptr)
146{
147 uint16_t result;
148 memcpy(&result, ptr, sizeof(result));
149 return cbor_ntohs(result);
150}
151
152static inline uint32_t get32(const uint8_t *ptr)
153{
154 uint32_t result;
155 memcpy(&result, ptr, sizeof(result));
156 return cbor_ntohl(result);
157}
158
159static inline uint64_t get64(const uint8_t *ptr)
160{
161 uint64_t result;
162 memcpy(&result, ptr, sizeof(result));
163 return cbor_ntohll(result);
164}
165
Andreas Zisowsky70aba6b2018-03-19 11:51:35 +0100166CborError CBOR_INTERNAL_API_CC _cbor_value_extract_number(const uint8_t **ptr, const uint8_t *end, uint64_t *len)
Thiago Macieira2c22d712017-03-05 00:17:35 -0600167{
168 uint8_t additional_information = **ptr & SmallValueMask;
169 ++*ptr;
170 if (additional_information < Value8Bit) {
171 *len = additional_information;
172 return CborNoError;
173 }
174 if (unlikely(additional_information > Value64Bit))
175 return CborErrorIllegalNumber;
176
177 size_t bytesNeeded = (size_t)(1 << (additional_information - Value8Bit));
178 if (unlikely(bytesNeeded > (size_t)(end - *ptr))) {
179 return CborErrorUnexpectedEOF;
180 } else if (bytesNeeded == 1) {
181 *len = (uint8_t)(*ptr)[0];
182 } else if (bytesNeeded == 2) {
183 *len = get16(*ptr);
184 } else if (bytesNeeded == 4) {
185 *len = get32(*ptr);
186 } else {
187 *len = get64(*ptr);
188 }
189 *ptr += bytesNeeded;
190 return CborNoError;
191}
192
Thiago Macieiraf5cb94b2015-06-16 16:10:49 -0700193static CborError extract_length(const CborParser *parser, const uint8_t **ptr, size_t *len)
Thiago Macieira54a0e102015-05-05 21:25:06 -0700194{
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700195 uint64_t v;
Thiago Macieira2c22d712017-03-05 00:17:35 -0600196 CborError err = _cbor_value_extract_number(ptr, parser->end, &v);
Mike Colagrosso629d5b72016-02-24 15:12:34 -0700197 if (err) {
198 *len = 0;
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700199 return err;
Mike Colagrosso629d5b72016-02-24 15:12:34 -0700200 }
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700201
alradmsft9ba47912016-10-11 17:56:15 -0700202 *len = (size_t)v;
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700203 if (v != *len)
204 return CborErrorDataTooLarge;
205 return CborNoError;
206}
207
208static bool is_fixed_type(uint8_t type)
209{
210 return type != CborTextStringType && type != CborByteStringType && type != CborArrayType &&
211 type != CborMapType;
212}
213
214static CborError preparse_value(CborValue *it)
215{
216 const CborParser *parser = it->parser;
Thiago Macieira11e913f2015-05-07 13:01:18 -0700217 it->type = CborInvalidType;
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700218
Thiago Macieiradbc01292016-06-06 17:02:25 -0700219 /* are we at the end? */
Thiago Macieira54a0e102015-05-05 21:25:06 -0700220 if (it->ptr == parser->end)
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700221 return CborErrorUnexpectedEOF;
Thiago Macieira54a0e102015-05-05 21:25:06 -0700222
223 uint8_t descriptor = *it->ptr;
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700224 uint8_t type = descriptor & MajorTypeMask;
Thiago Macieira851c4812015-05-08 15:23:20 -0700225 it->type = type;
Thiago Macieira54a0e102015-05-05 21:25:06 -0700226 it->flags = 0;
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700227 it->extra = (descriptor &= SmallValueMask);
228
Thiago Macieira56d99832015-05-07 14:34:27 -0700229 if (descriptor > Value64Bit) {
230 if (unlikely(descriptor != IndefiniteLength))
Thiago Macieira3f76f632015-05-12 10:10:09 +0900231 return type == CborSimpleType ? CborErrorUnknownType : CborErrorIllegalNumber;
Thiago Macieira56d99832015-05-07 14:34:27 -0700232 if (likely(!is_fixed_type(type))) {
Thiago Macieiradbc01292016-06-06 17:02:25 -0700233 /* special case */
Thiago Macieira56d99832015-05-07 14:34:27 -0700234 it->flags |= CborIteratorFlag_UnknownLength;
235 it->type = type;
236 return CborNoError;
237 }
238 return type == CborSimpleType ? CborErrorUnexpectedBreak : CborErrorIllegalNumber;
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700239 }
Thiago Macieira54a0e102015-05-05 21:25:06 -0700240
Thiago Macieirac70169f2015-05-06 07:49:44 -0700241 size_t bytesNeeded = descriptor < Value8Bit ? 0 : (1 << (descriptor - Value8Bit));
Thiago Macieira63abed92015-10-28 17:01:14 -0700242 if (bytesNeeded + 1 > (size_t)(parser->end - it->ptr))
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700243 return CborErrorUnexpectedEOF;
Thiago Macieirac70169f2015-05-06 07:49:44 -0700244
Thiago Macieira851c4812015-05-08 15:23:20 -0700245 uint8_t majortype = type >> MajorTypeShift;
246 if (majortype == NegativeIntegerType) {
Thiago Macieira54a0e102015-05-05 21:25:06 -0700247 it->flags |= CborIteratorFlag_NegativeInteger;
Thiago Macieira851c4812015-05-08 15:23:20 -0700248 it->type = CborIntegerType;
249 } else if (majortype == SimpleTypesType) {
Thiago Macieira54a0e102015-05-05 21:25:06 -0700250 switch (descriptor) {
251 case FalseValue:
252 it->extra = false;
Thiago Macieira851c4812015-05-08 15:23:20 -0700253 it->type = CborBooleanType;
Thiago Macieira991dd922015-05-07 11:57:59 -0700254 break;
255
Thiago Macieira851c4812015-05-08 15:23:20 -0700256 case SinglePrecisionFloat:
257 case DoublePrecisionFloat:
258 it->flags |= CborIteratorFlag_IntegerValueTooLarge;
Thiago Macieiradbc01292016-06-06 17:02:25 -0700259 /* fall through */
Thiago Macieira54a0e102015-05-05 21:25:06 -0700260 case TrueValue:
261 case NullValue:
262 case UndefinedValue:
263 case HalfPrecisionFloat:
Thiago Macieira851c4812015-05-08 15:23:20 -0700264 it->type = *it->ptr;
Thiago Macieira54a0e102015-05-05 21:25:06 -0700265 break;
266
267 case SimpleTypeInNextByte:
Thiago Macieira851c4812015-05-08 15:23:20 -0700268 it->extra = (uint8_t)it->ptr[1];
Thiago Macieira54a0e102015-05-05 21:25:06 -0700269#ifndef CBOR_PARSER_NO_STRICT_CHECKS
Thiago Macieira851c4812015-05-08 15:23:20 -0700270 if (unlikely(it->extra < 32)) {
271 it->type = CborInvalidType;
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700272 return CborErrorIllegalSimpleType;
Thiago Macieira851c4812015-05-08 15:23:20 -0700273 }
Thiago Macieira54a0e102015-05-05 21:25:06 -0700274#endif
Thiago Macieira991dd922015-05-07 11:57:59 -0700275 break;
276
Thiago Macieira54a0e102015-05-05 21:25:06 -0700277 case 28:
278 case 29:
279 case 30:
Thiago Macieira54a0e102015-05-05 21:25:06 -0700280 case Break:
Thiago Macieirada8e35e2017-03-05 09:58:01 +0100281 cbor_assert(false); /* these conditions can't be reached */
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700282 return CborErrorUnexpectedBreak;
Thiago Macieira54a0e102015-05-05 21:25:06 -0700283 }
Thiago Macieira851c4812015-05-08 15:23:20 -0700284 return CborNoError;
Thiago Macieira54a0e102015-05-05 21:25:06 -0700285 }
286
Thiago Macieiradbc01292016-06-06 17:02:25 -0700287 /* try to decode up to 16 bits */
Thiago Macieira54a0e102015-05-05 21:25:06 -0700288 if (descriptor < Value8Bit)
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700289 return CborNoError;
Thiago Macieira54a0e102015-05-05 21:25:06 -0700290
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700291 if (descriptor == Value8Bit)
292 it->extra = (uint8_t)it->ptr[1];
293 else if (descriptor == Value16Bit)
Thiago Macieira54a0e102015-05-05 21:25:06 -0700294 it->extra = get16(it->ptr + 1);
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700295 else
Thiago Macieiradbc01292016-06-06 17:02:25 -0700296 it->flags |= CborIteratorFlag_IntegerValueTooLarge; /* Value32Bit or Value64Bit */
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700297 return CborNoError;
298}
Thiago Macieira54a0e102015-05-05 21:25:06 -0700299
Thiago Macieira2a38a952017-12-26 17:47:54 -0200300static CborError preparse_next_value_nodecrement(CborValue *it)
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700301{
Thiago Macieira2a38a952017-12-26 17:47:54 -0200302 if (it->remaining == UINT32_MAX && it->ptr != it->parser->end && *it->ptr == (uint8_t)BreakByte) {
Thiago Macieiradbc01292016-06-06 17:02:25 -0700303 /* end of map or array */
Thiago Macieira56d99832015-05-07 14:34:27 -0700304 ++it->ptr;
305 it->type = CborInvalidType;
306 it->remaining = 0;
307 return CborNoError;
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700308 }
Thiago Macieira56d99832015-05-07 14:34:27 -0700309
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700310 return preparse_value(it);
311}
312
Thiago Macieira2a38a952017-12-26 17:47:54 -0200313static CborError preparse_next_value(CborValue *it)
314{
315 if (it->remaining != UINT32_MAX) {
316 /* don't decrement the item count if the current item is tag: they don't count */
317 if (it->type != CborTagType && --it->remaining == 0) {
318 it->type = CborInvalidType;
319 return CborNoError;
320 }
321 }
322 return preparse_next_value_nodecrement(it);
323}
324
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700325static CborError advance_internal(CborValue *it)
326{
327 uint64_t length;
Thiago Macieira2c22d712017-03-05 00:17:35 -0600328 CborError err = _cbor_value_extract_number(&it->ptr, it->parser->end, &length);
Thiago Macieirada8e35e2017-03-05 09:58:01 +0100329 cbor_assert(err == CborNoError);
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700330
Thiago Macieira56d99832015-05-07 14:34:27 -0700331 if (it->type == CborByteStringType || it->type == CborTextStringType) {
Thiago Macieirada8e35e2017-03-05 09:58:01 +0100332 cbor_assert(length == (size_t)length);
333 cbor_assert((it->flags & CborIteratorFlag_UnknownLength) == 0);
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700334 it->ptr += length;
335 }
336
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700337 return preparse_next_value(it);
Thiago Macieira54a0e102015-05-05 21:25:06 -0700338}
339
Thiago Macieira2312efd2015-05-06 16:07:48 -0700340/** \internal
341 *
342 * Decodes the CBOR integer value when it is larger than the 16 bits available
343 * in value->extra. This function requires that value->flags have the
344 * CborIteratorFlag_IntegerValueTooLarge flag set.
345 *
346 * This function is also used to extract single- and double-precision floating
347 * point values (SinglePrecisionFloat == Value32Bit and DoublePrecisionFloat ==
348 * Value64Bit).
349 */
Thiago Macieira54a0e102015-05-05 21:25:06 -0700350uint64_t _cbor_value_decode_int64_internal(const CborValue *value)
351{
Thiago Macieirada8e35e2017-03-05 09:58:01 +0100352 cbor_assert(value->flags & CborIteratorFlag_IntegerValueTooLarge ||
353 value->type == CborFloatType || value->type == CborDoubleType);
Thiago Macieira851c4812015-05-08 15:23:20 -0700354
Thiago Macieiradbc01292016-06-06 17:02:25 -0700355 /* since the additional information can only be Value32Bit or Value64Bit,
356 * we just need to test for the one bit those two options differ */
Thiago Macieirada8e35e2017-03-05 09:58:01 +0100357 cbor_assert((*value->ptr & SmallValueMask) == Value32Bit || (*value->ptr & SmallValueMask) == Value64Bit);
Thiago Macieira851c4812015-05-08 15:23:20 -0700358 if ((*value->ptr & 1) == (Value32Bit & 1))
Thiago Macieira54a0e102015-05-05 21:25:06 -0700359 return get32(value->ptr + 1);
360
Thiago Macieirada8e35e2017-03-05 09:58:01 +0100361 cbor_assert((*value->ptr & SmallValueMask) == Value64Bit);
Thiago Macieira54a0e102015-05-05 21:25:06 -0700362 return get64(value->ptr + 1);
363}
364
365/**
366 * Initializes the CBOR parser for parsing \a size bytes beginning at \a
367 * buffer. Parsing will use flags set in \a flags. The iterator to the first
368 * element is returned in \a it.
Thiago Macieira2312efd2015-05-06 16:07:48 -0700369 *
370 * The \a parser structure needs to remain valid throughout the decoding
371 * process. It is not thread-safe to share one CborParser among multiple
372 * threads iterating at the same time, but the object can be copied so multiple
373 * threads can iterate.
Thiago Macieira54a0e102015-05-05 21:25:06 -0700374 */
Thiago Macieira5752ce52015-06-16 12:10:03 -0700375CborError cbor_parser_init(const uint8_t *buffer, size_t size, int flags, CborParser *parser, CborValue *it)
Thiago Macieira54a0e102015-05-05 21:25:06 -0700376{
377 memset(parser, 0, sizeof(*parser));
378 parser->end = buffer + size;
Thiago Macieira54a0e102015-05-05 21:25:06 -0700379 parser->flags = flags;
380 it->parser = parser;
381 it->ptr = buffer;
Thiago Macieiradbc01292016-06-06 17:02:25 -0700382 it->remaining = 1; /* there's one type altogether, usually an array or map */
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700383 return preparse_value(it);
Thiago Macieira2312efd2015-05-06 16:07:48 -0700384}
385
386/**
Thiago Macieira46a818e2015-10-08 15:13:05 +0200387 * \fn bool cbor_value_at_end(const CborValue *it)
388 *
389 * Returns true if \a it has reached the end of the iteration, usually when
Thiago Macieira740e29d2016-07-07 13:32:47 -0700390 * advancing after the last item in an array or map.
Thiago Macieira46a818e2015-10-08 15:13:05 +0200391 *
Thiago Macieira740e29d2016-07-07 13:32:47 -0700392 * In the case of the outermost CborValue object, this function returns true
393 * after decoding a single element. A pointer to the first byte of the
394 * remaining data (if any) can be obtained with cbor_value_get_next_byte().
395 *
396 * \sa cbor_value_advance(), cbor_value_is_valid(), cbor_value_get_next_byte()
397 */
398
399/**
400 * \fn const uint8_t *cbor_value_get_next_byte(const CborValue *it)
401 *
402 * Returns a pointer to the next byte that would be decoded if this CborValue
403 * object were advanced.
404 *
405 * This function is useful if cbor_value_at_end() returns true for the
406 * outermost CborValue: the pointer returned is the first byte of the data
407 * remaining in the buffer, if any. Code can decide whether to begin decoding a
408 * new CBOR data stream from this point, or parse some other data appended to
409 * the same buffer.
410 *
411 * This function may be used even after a parsing error. If that occurred,
412 * then this function returns a pointer to where the parsing error occurred.
413 * Note that the error recovery is not precise and the pointer may not indicate
414 * the exact byte containing bad data.
415 *
416 * \sa cbor_value_at_end()
Thiago Macieira46a818e2015-10-08 15:13:05 +0200417 */
418
419/**
420 * \fn bool cbor_value_is_valid(const CborValue *it)
421 *
422 * Returns true if the iterator \a it contains a valid value. Invalid iterators
423 * happen when iteration reaches the end of a container (see \ref
424 * cbor_value_at_end()) or when a search function resulted in no matches.
425 *
MÃ¥rten Nordheimc1ae5212018-02-06 09:42:16 +0100426 * \sa cbor_value_advance(), cbor_value_at_end(), cbor_value_get_type()
Thiago Macieira46a818e2015-10-08 15:13:05 +0200427 */
428
429/**
Thiago Macieira73881122017-02-26 00:20:47 -0800430 * Performs a basic validation of the CBOR stream pointed by \a it and returns
431 * the error it found. If no error was found, it returns CborNoError and the
432 * application can iterate over the items with certainty that no other errors
433 * will appear during parsing.
434 *
435 * A basic validation checks for:
436 * \list
437 * \li absence of undefined additional information bytes;
438 * \li well-formedness of all numbers, lengths, and simple values;
439 * \li string contents match reported sizes;
440 * \li arrays and maps contain the number of elements they are reported to have;
441 * \endlist
442 *
443 * For further checks, see cbor_value_validate().
444 *
445 * This function has the same timing and memory requirements as
446 * cbor_value_advance().
447 *
448 * \sa cbor_value_validate(), cbor_value_advance()
449 */
450CborError cbor_value_validate_basic(const CborValue *it)
451{
452 CborValue value = *it;
453 return cbor_value_advance(&value);
454}
455
456/**
Thiago Macieira2312efd2015-05-06 16:07:48 -0700457 * Advances the CBOR value \a it by one fixed-size position. Fixed-size types
458 * are: integers, tags, simple types (including boolean, null and undefined
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700459 * values) and floating point types.
Thiago Macieira2312efd2015-05-06 16:07:48 -0700460 *
Thiago Macieira46a818e2015-10-08 15:13:05 +0200461 * If the type is not of fixed size, this function has undefined behavior. Code
462 * must be sure that the current type is one of the fixed-size types before
463 * calling this function. This function is provided because it can guarantee
Thiago Macieiraf5a172b2018-02-05 14:53:14 -0800464 * that it runs in constant time (O(1)).
Thiago Macieira46a818e2015-10-08 15:13:05 +0200465 *
466 * If the caller is not able to determine whether the type is fixed or not, code
467 * can use the cbor_value_advance() function instead.
468 *
469 * \sa cbor_value_at_end(), cbor_value_advance(), cbor_value_enter_container(), cbor_value_leave_container()
Thiago Macieira2312efd2015-05-06 16:07:48 -0700470 */
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700471CborError cbor_value_advance_fixed(CborValue *it)
Thiago Macieira54a0e102015-05-05 21:25:06 -0700472{
Thiago Macieirada8e35e2017-03-05 09:58:01 +0100473 cbor_assert(it->type != CborInvalidType);
474 cbor_assert(is_fixed_type(it->type));
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700475 if (!it->remaining)
476 return CborErrorAdvancePastEOF;
477 return advance_internal(it);
Thiago Macieira54a0e102015-05-05 21:25:06 -0700478}
479
Thiago Macieira4a99af92015-05-12 10:41:45 +0900480static CborError advance_recursive(CborValue *it, int nestingLevel)
481{
482 if (is_fixed_type(it->type))
483 return advance_internal(it);
484
485 if (!cbor_value_is_container(it)) {
486 size_t len = SIZE_MAX;
Thiago Macieiraff130bc2015-06-19 15:15:33 -0700487 return _cbor_value_copy_string(it, NULL, &len, it);
Thiago Macieira4a99af92015-05-12 10:41:45 +0900488 }
489
Thiago Macieiradbc01292016-06-06 17:02:25 -0700490 /* map or array */
Thiago Macieira4df83642017-12-17 14:01:23 -0800491 if (nestingLevel == 0)
Thiago Macieira4a99af92015-05-12 10:41:45 +0900492 return CborErrorNestingTooDeep;
493
494 CborError err;
495 CborValue recursed;
496 err = cbor_value_enter_container(it, &recursed);
497 if (err)
498 return err;
499 while (!cbor_value_at_end(&recursed)) {
Thiago Macieira4df83642017-12-17 14:01:23 -0800500 err = advance_recursive(&recursed, nestingLevel - 1);
Thiago Macieira4a99af92015-05-12 10:41:45 +0900501 if (err)
502 return err;
503 }
504 return cbor_value_leave_container(it, &recursed);
505}
506
507
Thiago Macieira2312efd2015-05-06 16:07:48 -0700508/**
509 * Advances the CBOR value \a it by one element, skipping over containers.
510 * Unlike cbor_value_advance_fixed(), this function can be called on a CBOR
511 * value of any type. However, if the type is a container (map or array) or a
512 * string with a chunked payload, this function will not run in constant time
513 * and will recurse into itself (it will run on O(n) time for the number of
514 * elements or chunks and will use O(n) memory for the number of nested
515 * containers).
516 *
Thiago Macieira73881122017-02-26 00:20:47 -0800517 * The number of recursions can be limited at compile time to avoid stack
518 * exhaustion in constrained systems.
519 *
Thiago Macieira46a818e2015-10-08 15:13:05 +0200520 * \sa cbor_value_at_end(), cbor_value_advance_fixed(), cbor_value_enter_container(), cbor_value_leave_container()
Thiago Macieira2312efd2015-05-06 16:07:48 -0700521 */
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700522CborError cbor_value_advance(CborValue *it)
Thiago Macieira2312efd2015-05-06 16:07:48 -0700523{
Thiago Macieirada8e35e2017-03-05 09:58:01 +0100524 cbor_assert(it->type != CborInvalidType);
Thiago Macieira2312efd2015-05-06 16:07:48 -0700525 if (!it->remaining)
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700526 return CborErrorAdvancePastEOF;
Thiago Macieira4df83642017-12-17 14:01:23 -0800527 return advance_recursive(it, CBOR_PARSER_MAX_RECURSIONS);
Thiago Macieira2312efd2015-05-06 16:07:48 -0700528}
529
530/**
Thiago Macieira46a818e2015-10-08 15:13:05 +0200531 * \fn bool cbor_value_is_tag(const CborValue *value)
532 *
533 * Returns true if the iterator \a value is valid and points to a CBOR tag.
534 *
535 * \sa cbor_value_get_tag(), cbor_value_skip_tag()
536 */
537
538/**
539 * \fn CborError cbor_value_get_tag(const CborValue *value, CborTag *result)
540 *
541 * Retrieves the CBOR tag value that \a value points to and stores it in \a
542 * result. If the iterator \a value does not point to a CBOR tag value, the
543 * behavior is undefined, so checking with \ref cbor_value_get_type or with
544 * \ref cbor_value_is_tag is recommended.
545 *
546 * \sa cbor_value_get_type(), cbor_value_is_valid(), cbor_value_is_tag()
547 */
548
549/**
Thiago Macieirac4a73c62015-05-09 18:14:11 -0700550 * Advances the CBOR value \a it until it no longer points to a tag. If \a it is
551 * already not pointing to a tag, then this function returns it unchanged.
552 *
Thiago Macieira46a818e2015-10-08 15:13:05 +0200553 * This function does not run in constant time: it will run on O(n) for n being
554 * the number of tags. It does use constant memory (O(1) memory requirements).
555 *
Thiago Macieirac4a73c62015-05-09 18:14:11 -0700556 * \sa cbor_value_advance_fixed(), cbor_value_advance()
557 */
558CborError cbor_value_skip_tag(CborValue *it)
559{
560 while (cbor_value_is_tag(it)) {
561 CborError err = cbor_value_advance_fixed(it);
562 if (err)
563 return err;
564 }
565 return CborNoError;
566}
567
Thiago Macieirac4a73c62015-05-09 18:14:11 -0700568/**
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700569 * \fn bool cbor_value_is_container(const CborValue *it)
570 *
Thiago Macieira2312efd2015-05-06 16:07:48 -0700571 * Returns true if the \a it value is a container and requires recursion in
572 * order to decode (maps and arrays), false otherwise.
573 */
Thiago Macieira54a0e102015-05-05 21:25:06 -0700574
Thiago Macieira2312efd2015-05-06 16:07:48 -0700575/**
576 * Creates a CborValue iterator pointing to the first element of the container
577 * represented by \a it and saves it in \a recursed. The \a it container object
578 * needs to be kept and passed again to cbor_value_leave_container() in order
579 * to continue iterating past this container.
580 *
Thiago Macieira46a818e2015-10-08 15:13:05 +0200581 * The \a it CborValue iterator must point to a container.
582 *
Thiago Macieira2312efd2015-05-06 16:07:48 -0700583 * \sa cbor_value_is_container(), cbor_value_leave_container(), cbor_value_advance()
584 */
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700585CborError cbor_value_enter_container(const CborValue *it, CborValue *recursed)
Thiago Macieira54a0e102015-05-05 21:25:06 -0700586{
Thiago Macieirada8e35e2017-03-05 09:58:01 +0100587 cbor_assert(cbor_value_is_container(it));
Thiago Macieira54a0e102015-05-05 21:25:06 -0700588 *recursed = *it;
Thiago Macieira56d99832015-05-07 14:34:27 -0700589
Thiago Macieira54a0e102015-05-05 21:25:06 -0700590 if (it->flags & CborIteratorFlag_UnknownLength) {
591 recursed->remaining = UINT32_MAX;
Thiago Macieira56d99832015-05-07 14:34:27 -0700592 ++recursed->ptr;
Thiago Macieira54a0e102015-05-05 21:25:06 -0700593 } else {
Thiago Macieira56d99832015-05-07 14:34:27 -0700594 uint64_t len;
Thiago Macieira2a38a952017-12-26 17:47:54 -0200595 CborError err = _cbor_value_extract_number(&recursed->ptr, recursed->parser->end, &len);
Thiago Macieirada8e35e2017-03-05 09:58:01 +0100596 cbor_assert(err == CborNoError);
Thiago Macieira56d99832015-05-07 14:34:27 -0700597
Thiago Macieirae12dfd02016-06-07 16:29:25 -0700598 recursed->remaining = (uint32_t)len;
Thiago Macieira3f76f632015-05-12 10:10:09 +0900599 if (recursed->remaining != len || len == UINT32_MAX) {
Thiago Macieiradbc01292016-06-06 17:02:25 -0700600 /* back track the pointer to indicate where the error occurred */
Thiago Macieira3f76f632015-05-12 10:10:09 +0900601 recursed->ptr = it->ptr;
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700602 return CborErrorDataTooLarge;
Thiago Macieira3f76f632015-05-12 10:10:09 +0900603 }
Thiago Macieirace16f052015-05-07 23:14:25 -0700604 if (recursed->type == CborMapType) {
Thiago Macieiradbc01292016-06-06 17:02:25 -0700605 /* maps have keys and values, so we need to multiply by 2 */
Thiago Macieira3f76f632015-05-12 10:10:09 +0900606 if (recursed->remaining > UINT32_MAX / 2) {
Thiago Macieiradbc01292016-06-06 17:02:25 -0700607 /* back track the pointer to indicate where the error occurred */
Thiago Macieira3f76f632015-05-12 10:10:09 +0900608 recursed->ptr = it->ptr;
Thiago Macieirace16f052015-05-07 23:14:25 -0700609 return CborErrorDataTooLarge;
Thiago Macieira3f76f632015-05-12 10:10:09 +0900610 }
Thiago Macieirace16f052015-05-07 23:14:25 -0700611 recursed->remaining *= 2;
612 }
Thiago Macieira2a38a952017-12-26 17:47:54 -0200613 if (len == 0) {
614 /* the case of the empty container */
615 recursed->type = CborInvalidType;
616 return CborNoError;
617 }
Thiago Macieira54a0e102015-05-05 21:25:06 -0700618 }
Thiago Macieira2a38a952017-12-26 17:47:54 -0200619 return preparse_next_value_nodecrement(recursed);
Thiago Macieirac70169f2015-05-06 07:49:44 -0700620}
621
Thiago Macieira2312efd2015-05-06 16:07:48 -0700622/**
623 * Updates \a it to point to the next element after the container. The \a
Thiago Macieira56d99832015-05-07 14:34:27 -0700624 * recursed object needs to point to the element obtained either by advancing
625 * the last element of the container (via cbor_value_advance(),
626 * cbor_value_advance_fixed(), a nested cbor_value_leave_container(), or the \c
627 * next pointer from cbor_value_copy_string() or cbor_value_dup_string()).
Thiago Macieira2312efd2015-05-06 16:07:48 -0700628 *
Thiago Macieira46a818e2015-10-08 15:13:05 +0200629 * The \a it and \a recursed parameters must be the exact same as passed to
630 * cbor_value_enter_container().
631 *
Thiago Macieira2312efd2015-05-06 16:07:48 -0700632 * \sa cbor_value_enter_container(), cbor_value_at_end()
633 */
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700634CborError cbor_value_leave_container(CborValue *it, const CborValue *recursed)
Thiago Macieirac70169f2015-05-06 07:49:44 -0700635{
Thiago Macieirada8e35e2017-03-05 09:58:01 +0100636 cbor_assert(cbor_value_is_container(it));
637 cbor_assert(recursed->type == CborInvalidType);
Thiago Macieirac70169f2015-05-06 07:49:44 -0700638 it->ptr = recursed->ptr;
Thiago Macieira56d99832015-05-07 14:34:27 -0700639 return preparse_next_value(it);
Thiago Macieirac70169f2015-05-06 07:49:44 -0700640}
641
Thiago Macieira46a818e2015-10-08 15:13:05 +0200642
Thiago Macieira2312efd2015-05-06 16:07:48 -0700643/**
Thiago Macieira46a818e2015-10-08 15:13:05 +0200644 * \fn CborType cbor_value_get_type(const CborValue *value)
645 *
646 * Returns the type of the CBOR value that the iterator \a value points to. If
647 * \a value does not point to a valid value, this function returns \ref
648 * CborInvalidType.
649 *
650 * TinyCBOR also provides functions to test directly if a given CborValue object
651 * is of a given type, like cbor_value_is_text_string() and cbor_value_is_null().
652 *
653 * \sa cbor_value_is_valid()
654 */
655
656/**
657 * \fn bool cbor_value_is_null(const CborValue *value)
658 *
659 * Returns true if the iterator \a value is valid and points to a CBOR null type.
660 *
661 * \sa cbor_value_is_valid(), cbor_value_is_undefined()
662 */
663
664/**
665 * \fn bool cbor_value_is_undefined(const CborValue *value)
666 *
667 * Returns true if the iterator \a value is valid and points to a CBOR undefined type.
668 *
669 * \sa cbor_value_is_valid(), cbor_value_is_null()
670 */
671
672/**
673 * \fn bool cbor_value_is_boolean(const CborValue *value)
674 *
675 * Returns true if the iterator \a value is valid and points to a CBOR boolean
676 * type (true or false).
677 *
678 * \sa cbor_value_is_valid(), cbor_value_get_boolean()
679 */
680
681/**
682 * \fn CborError cbor_value_get_boolean(const CborValue *value, bool *result)
683 *
684 * Retrieves the boolean value that \a value points to and stores it in \a
685 * result. If the iterator \a value does not point to a boolean value, the
686 * behavior is undefined, so checking with \ref cbor_value_get_type or with
687 * \ref cbor_value_is_boolean is recommended.
688 *
689 * \sa cbor_value_get_type(), cbor_value_is_valid(), cbor_value_is_boolean()
690 */
691
692/**
693 * \fn bool cbor_value_is_simple_type(const CborValue *value)
694 *
695 * Returns true if the iterator \a value is valid and points to a CBOR Simple Type
696 * type (other than true, false, null and undefined).
697 *
698 * \sa cbor_value_is_valid(), cbor_value_get_simple_type()
699 */
700
701/**
702 * \fn CborError cbor_value_get_simple_type(const CborValue *value, uint8_t *result)
703 *
704 * Retrieves the CBOR Simple Type value that \a value points to and stores it
705 * in \a result. If the iterator \a value does not point to a simple_type
706 * value, the behavior is undefined, so checking with \ref cbor_value_get_type
707 * or with \ref cbor_value_is_simple_type is recommended.
708 *
709 * \sa cbor_value_get_type(), cbor_value_is_valid(), cbor_value_is_simple_type()
710 */
711
712/**
713 * \fn bool cbor_value_is_integer(const CborValue *value)
714 *
715 * Returns true if the iterator \a value is valid and points to a CBOR integer
716 * type.
717 *
718 * \sa cbor_value_is_valid(), cbor_value_get_int, cbor_value_get_int64, cbor_value_get_uint64, cbor_value_get_raw_integer
719 */
720
721/**
722 * \fn bool cbor_value_is_unsigned_integer(const CborValue *value)
723 *
724 * Returns true if the iterator \a value is valid and points to a CBOR unsigned
725 * integer type (positive values or zero).
726 *
727 * \sa cbor_value_is_valid(), cbor_value_get_uint64()
728 */
729
730/**
731 * \fn bool cbor_value_is_negative_integer(const CborValue *value)
732 *
733 * Returns true if the iterator \a value is valid and points to a CBOR negative
734 * integer type.
735 *
736 * \sa cbor_value_is_valid(), cbor_value_get_int, cbor_value_get_int64, cbor_value_get_raw_integer
737 */
738
739/**
740 * \fn CborError cbor_value_get_int(const CborValue *value, int *result)
741 *
742 * Retrieves the CBOR integer value that \a value points to and stores it in \a
743 * result. If the iterator \a value does not point to an integer value, the
744 * behavior is undefined, so checking with \ref cbor_value_get_type or with
745 * \ref cbor_value_is_integer is recommended.
746 *
747 * Note that this function does not do range-checking: integral values that do
748 * not fit in a variable of type \c{int} are silently truncated to fit. Use
Thiago Macieiraf5a172b2018-02-05 14:53:14 -0800749 * cbor_value_get_int_checked() if that is not acceptable.
Thiago Macieira46a818e2015-10-08 15:13:05 +0200750 *
751 * \sa cbor_value_get_type(), cbor_value_is_valid(), cbor_value_is_integer()
752 */
753
754/**
755 * \fn CborError cbor_value_get_int64(const CborValue *value, int64_t *result)
756 *
757 * Retrieves the CBOR integer value that \a value points to and stores it in \a
758 * result. If the iterator \a value does not point to an integer value, the
759 * behavior is undefined, so checking with \ref cbor_value_get_type or with
760 * \ref cbor_value_is_integer is recommended.
761 *
762 * Note that this function does not do range-checking: integral values that do
763 * not fit in a variable of type \c{int64_t} are silently truncated to fit. Use
764 * cbor_value_get_int64_checked() that is not acceptable.
765 *
766 * \sa cbor_value_get_type(), cbor_value_is_valid(), cbor_value_is_integer()
767 */
768
769/**
770 * \fn CborError cbor_value_get_uint64(const CborValue *value, uint64_t *result)
771 *
772 * Retrieves the CBOR integer value that \a value points to and stores it in \a
773 * result. If the iterator \a value does not point to an unsigned integer
774 * value, the behavior is undefined, so checking with \ref cbor_value_get_type
775 * or with \ref cbor_value_is_unsigned_integer is recommended.
776 *
777 * \sa cbor_value_get_type(), cbor_value_is_valid(), cbor_value_is_unsigned_integer()
778 */
779
780/**
781 * \fn CborError cbor_value_get_raw_integer(const CborValue *value, uint64_t *result)
782 *
783 * Retrieves the CBOR integer value that \a value points to and stores it in \a
784 * result. If the iterator \a value does not point to an integer value, the
785 * behavior is undefined, so checking with \ref cbor_value_get_type or with
786 * \ref cbor_value_is_integer is recommended.
787 *
788 * This function is provided because CBOR negative integers can assume values
789 * that cannot be represented with normal 64-bit integer variables.
790 *
791 * If the integer is unsigned (that is, if cbor_value_is_unsigned_integer()
792 * returns true), then \a result will contain the actual value. If the integer
793 * is negative, then \a result will contain the absolute value of that integer,
794 * minus one. That is, \c {actual = -result - 1}. On architectures using two's
795 * complement for representation of negative integers, it is equivalent to say
796 * that \a result will contain the bitwise negation of the actual value.
797 *
798 * \sa cbor_value_get_type(), cbor_value_is_valid(), cbor_value_is_integer()
799 */
800
801/**
Thiago Macieira0f02e792016-07-07 19:55:08 -0700802 * Retrieves the CBOR integer value that \a value points to and stores it in \a
803 * result. If the iterator \a value does not point to an integer value, the
804 * behavior is undefined, so checking with \ref cbor_value_get_type or with
805 * \ref cbor_value_is_integer is recommended.
806 *
Thiago Macieiraf5a172b2018-02-05 14:53:14 -0800807 * Unlike \ref cbor_value_get_int64(), this function performs a check to see if the
Thiago Macieira0f02e792016-07-07 19:55:08 -0700808 * stored integer fits in \a result without data loss. If the number is outside
809 * the valid range for the data type, this function returns the recoverable
810 * error CborErrorDataTooLarge. In that case, use either
811 * cbor_value_get_uint64() (if the number is positive) or
812 * cbor_value_get_raw_integer().
813 *
814 * \sa cbor_value_get_type(), cbor_value_is_valid(), cbor_value_is_integer(), cbor_value_get_int64()
815 */
816CborError cbor_value_get_int64_checked(const CborValue *value, int64_t *result)
817{
Thiago Macieirada8e35e2017-03-05 09:58:01 +0100818 cbor_assert(cbor_value_is_integer(value));
Thiago Macieira0f02e792016-07-07 19:55:08 -0700819 uint64_t v = _cbor_value_extract_int64_helper(value);
820
821 /* Check before converting, as the standard says (C11 6.3.1.3 paragraph 3):
822 * "[if] the new type is signed and the value cannot be represented in it; either the
823 * result is implementation-defined or an implementation-defined signal is raised."
824 *
825 * The range for int64_t is -2^63 to 2^63-1 (int64_t is required to be
826 * two's complement, C11 7.20.1.1 paragraph 3), which in CBOR is
827 * represented the same way, differing only on the "sign bit" (the major
828 * type).
829 */
830
831 if (unlikely(v > (uint64_t)INT64_MAX))
832 return CborErrorDataTooLarge;
833
834 *result = v;
835 if (value->flags & CborIteratorFlag_NegativeInteger)
836 *result = -*result - 1;
837 return CborNoError;
838}
839
840/**
841 * Retrieves the CBOR integer value that \a value points to and stores it in \a
842 * result. If the iterator \a value does not point to an integer value, the
843 * behavior is undefined, so checking with \ref cbor_value_get_type or with
844 * \ref cbor_value_is_integer is recommended.
845 *
Thiago Macieiraf5a172b2018-02-05 14:53:14 -0800846 * Unlike \ref cbor_value_get_int(), this function performs a check to see if the
Thiago Macieira0f02e792016-07-07 19:55:08 -0700847 * stored integer fits in \a result without data loss. If the number is outside
848 * the valid range for the data type, this function returns the recoverable
849 * error CborErrorDataTooLarge. In that case, use one of the other integer
850 * functions to obtain the value.
851 *
852 * \sa cbor_value_get_type(), cbor_value_is_valid(), cbor_value_is_integer(), cbor_value_get_int64(),
853 * cbor_value_get_uint64(), cbor_value_get_int64_checked(), cbor_value_get_raw_integer()
854 */
855CborError cbor_value_get_int_checked(const CborValue *value, int *result)
856{
Thiago Macieirada8e35e2017-03-05 09:58:01 +0100857 cbor_assert(cbor_value_is_integer(value));
Thiago Macieira0f02e792016-07-07 19:55:08 -0700858 uint64_t v = _cbor_value_extract_int64_helper(value);
859
860 /* Check before converting, as the standard says (C11 6.3.1.3 paragraph 3):
861 * "[if] the new type is signed and the value cannot be represented in it; either the
862 * result is implementation-defined or an implementation-defined signal is raised."
863 *
864 * But we can convert from signed to unsigned without fault (paragraph 2).
865 *
Thiago Macieiraf5a172b2018-02-05 14:53:14 -0800866 * The range for int is implementation-defined and int is not guaranteed to use
867 * two's complement representation (although int32_t is).
Thiago Macieira0f02e792016-07-07 19:55:08 -0700868 */
869
870 if (value->flags & CborIteratorFlag_NegativeInteger) {
871 if (unlikely(v > (unsigned) -(INT_MIN + 1)))
872 return CborErrorDataTooLarge;
873
alradmsft9ba47912016-10-11 17:56:15 -0700874 *result = (int)v;
Thiago Macieira0f02e792016-07-07 19:55:08 -0700875 *result = -*result - 1;
876 } else {
877 if (unlikely(v > (uint64_t)INT_MAX))
878 return CborErrorDataTooLarge;
879
alradmsft9ba47912016-10-11 17:56:15 -0700880 *result = (int)v;
Thiago Macieira0f02e792016-07-07 19:55:08 -0700881 }
882 return CborNoError;
883
884}
885
886/**
Thiago Macieira46a818e2015-10-08 15:13:05 +0200887 * \fn bool cbor_value_is_length_known(const CborValue *value)
888 *
889 * Returns true if the length of this type is known without calculation. That
890 * is, if the length of this CBOR string, map or array is encoded in the data
891 * stream, this function returns true. If the length is not encoded, it returns
892 * false.
893 *
894 * If the length is known, code can call cbor_value_get_string_length(),
895 * cbor_value_get_array_length() or cbor_value_get_map_length() to obtain the
896 * length. If the length is not known but is necessary, code can use the
897 * cbor_value_calculate_string_length() function (no equivalent function is
898 * provided for maps and arrays).
899 */
900
901/**
902 * \fn bool cbor_value_is_text_string(const CborValue *value)
903 *
904 * Returns true if the iterator \a value is valid and points to a CBOR text
905 * string. CBOR text strings are UTF-8 encoded and usually contain
906 * human-readable text.
907 *
908 * \sa cbor_value_is_valid(), cbor_value_get_string_length(), cbor_value_calculate_string_length(),
909 * cbor_value_copy_text_string(), cbor_value_dup_text_string()
910 */
911
912/**
913 * \fn bool cbor_value_is_byte_string(const CborValue *value)
914 *
915 * Returns true if the iterator \a value is valid and points to a CBOR text
916 * string. CBOR byte strings are binary data with no specified encoding or
917 * format.
918 *
919 * \sa cbor_value_is_valid(), cbor_value_get_string_length(), cbor_value_calculate_string_length(),
920 * cbor_value_copy_byte_string(), cbor_value_dup_byte_string()
921 */
922
923/**
924 * \fn CborError cbor_value_get_string_length(const CborValue *value, size_t *length)
925 *
926 * Extracts the length of the byte or text string that \a value points to and
927 * stores it in \a result. If the iterator \a value does not point to a text
928 * string or a byte string, the behaviour is undefined, so checking with \ref
929 * cbor_value_get_type, with \ref cbor_value_is_text_string or \ref
930 * cbor_value_is_byte_string is recommended.
931 *
932 * If the length of this string is not encoded in the CBOR data stream, this
933 * function will return the recoverable error CborErrorUnknownLength. You may
934 * also check whether that is the case by using cbor_value_is_length_known().
935 *
936 * If the length of the string is required but the length was not encoded, use
937 * cbor_value_calculate_string_length(), but note that that function does not
938 * run in constant time.
939 *
940 * \note On 32-bit platforms, this function will return error condition of \ref
941 * CborErrorDataTooLarge if the stream indicates a length that is too big to
942 * fit in 32-bit.
943 *
944 * \sa cbor_value_is_valid(), cbor_value_is_length_known(), cbor_value_calculate_string_length()
945 */
946
947/**
948 * Calculates the length of the byte or text string that \a value points to and
949 * stores it in \a len. If the iterator \a value does not point to a text
950 * string or a byte string, the behaviour is undefined, so checking with \ref
951 * cbor_value_get_type, with \ref cbor_value_is_text_string or \ref
952 * cbor_value_is_byte_string is recommended.
953 *
954 * This function is different from cbor_value_get_string_length() in that it
955 * calculates the length even for strings sent in chunks. For that reason, this
956 * function may not run in constant time (it will run in O(n) time on the
957 * number of chunks). It does use constant memory (O(1)).
Thiago Macieira2312efd2015-05-06 16:07:48 -0700958 *
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700959 * \note On 32-bit platforms, this function will return error condition of \ref
960 * CborErrorDataTooLarge if the stream indicates a length that is too big to
961 * fit in 32-bit.
Thiago Macieira2312efd2015-05-06 16:07:48 -0700962 *
Thiago Macieira51b56062017-02-23 16:03:13 -0800963 * \sa cbor_value_get_string_length(), cbor_value_copy_text_string(), cbor_value_copy_byte_string(), cbor_value_is_length_known()
Thiago Macieira2312efd2015-05-06 16:07:48 -0700964 */
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700965CborError cbor_value_calculate_string_length(const CborValue *value, size_t *len)
Thiago Macieira2312efd2015-05-06 16:07:48 -0700966{
Thiago Macieira9ae05812015-05-11 15:09:09 +0900967 *len = SIZE_MAX;
Thiago Macieiraff130bc2015-06-19 15:15:33 -0700968 return _cbor_value_copy_string(value, NULL, len, NULL);
Thiago Macieirac70169f2015-05-06 07:49:44 -0700969}
970
Thiago Macieira57ba7c92017-02-25 22:32:20 -0800971static inline void prepare_string_iteration(CborValue *it)
972{
973 if (!cbor_value_is_length_known(it)) {
974 /* chunked string: we're before the first chunk;
975 * advance to the first chunk */
976 ++it->ptr;
977 it->flags |= CborIteratorFlag_IteratingStringChunks;
978 }
979}
980
Andreas Zisowsky70aba6b2018-03-19 11:51:35 +0100981CborError CBOR_INTERNAL_API_CC _cbor_value_prepare_string_iteration(CborValue *it)
Thiago Macieira57ba7c92017-02-25 22:32:20 -0800982{
983 cbor_assert((it->flags & CborIteratorFlag_IteratingStringChunks) == 0);
984 prepare_string_iteration(it);
985
986 /* are we at the end? */
987 if (it->ptr == it->parser->end)
988 return CborErrorUnexpectedEOF;
989 return CborNoError;
990}
991
Thiago Macieira51b56062017-02-23 16:03:13 -0800992static CborError get_string_chunk(CborValue *it, const void **bufferptr, size_t *len)
993{
994 CborError err;
995
996 /* Possible states:
997 * length known | iterating | meaning
998 * no | no | before the first chunk of a chunked string
999 * yes | no | at a non-chunked string
1000 * no | yes | second or later chunk
1001 * yes | yes | after a non-chunked string
1002 */
1003 if (it->flags & CborIteratorFlag_IteratingStringChunks) {
1004 /* already iterating */
1005 if (cbor_value_is_length_known(it)) {
1006 /* if the length was known, it wasn't chunked, so finish iteration */
1007 goto last_chunk;
1008 }
Thiago Macieira57ba7c92017-02-25 22:32:20 -08001009 } else {
1010 prepare_string_iteration(it);
Thiago Macieira51b56062017-02-23 16:03:13 -08001011 }
1012
1013 /* are we at the end? */
1014 if (it->ptr == it->parser->end)
1015 return CborErrorUnexpectedEOF;
1016
1017 if (*it->ptr == BreakByte) {
1018 /* last chunk */
1019 ++it->ptr;
1020last_chunk:
1021 *bufferptr = NULL;
Thiago Macieirab0879392017-12-22 10:47:00 -02001022 *len = 0;
Thiago Macieira51b56062017-02-23 16:03:13 -08001023 return preparse_next_value(it);
1024 } else if ((uint8_t)(*it->ptr & MajorTypeMask) == it->type) {
1025 err = extract_length(it->parser, &it->ptr, len);
1026 if (err)
1027 return err;
1028 if (*len > (size_t)(it->parser->end - it->ptr))
1029 return CborErrorUnexpectedEOF;
1030
1031 *bufferptr = it->ptr;
1032 it->ptr += *len;
1033 } else {
1034 return CborErrorIllegalType;
1035 }
1036
1037 it->flags |= CborIteratorFlag_IteratingStringChunks;
1038 return CborNoError;
1039}
1040
Andreas Zisowsky70aba6b2018-03-19 11:51:35 +01001041CborError CBOR_INTERNAL_API_CC
1042_cbor_value_get_string_chunk(const CborValue *value, const void **bufferptr,
1043 size_t *len, CborValue *next)
Thiago Macieira51b56062017-02-23 16:03:13 -08001044{
1045 CborValue tmp;
1046 if (!next)
1047 next = &tmp;
1048 *next = *value;
1049 return get_string_chunk(next, bufferptr, len);
1050}
1051
Thiago Macieiradbc01292016-06-06 17:02:25 -07001052/* We return uintptr_t so that we can pass memcpy directly as the iteration
1053 * function. The choice is to optimize for memcpy, which is used in the base
1054 * parser API (cbor_value_copy_string), while memcmp is used in convenience API
1055 * only. */
Thiago Macieira5752ce52015-06-16 12:10:03 -07001056typedef uintptr_t (*IterateFunction)(char *, const uint8_t *, size_t);
Thiago Macieira9ae05812015-05-11 15:09:09 +09001057
Thiago Macieira5752ce52015-06-16 12:10:03 -07001058static uintptr_t iterate_noop(char *dest, const uint8_t *src, size_t len)
Thiago Macieira9ae05812015-05-11 15:09:09 +09001059{
1060 (void)dest;
1061 (void)src;
1062 (void)len;
1063 return true;
1064}
1065
Thiago Macieira5752ce52015-06-16 12:10:03 -07001066static uintptr_t iterate_memcmp(char *s1, const uint8_t *s2, size_t len)
Thiago Macieirac4a73c62015-05-09 18:14:11 -07001067{
Thiago Macieira5752ce52015-06-16 12:10:03 -07001068 return memcmp(s1, (const char *)s2, len) == 0;
Thiago Macieirac4a73c62015-05-09 18:14:11 -07001069}
1070
alradmsftacf202a2017-03-03 18:23:17 -08001071static uintptr_t iterate_memcpy(char *dest, const uint8_t *src, size_t len)
1072{
1073 return (uintptr_t)memcpy(dest, src, len);
1074}
1075
Thiago Macieira9ae05812015-05-11 15:09:09 +09001076static CborError iterate_string_chunks(const CborValue *value, char *buffer, size_t *buflen,
1077 bool *result, CborValue *next, IterateFunction func)
1078{
Thiago Macieirada8e35e2017-03-05 09:58:01 +01001079 cbor_assert(cbor_value_is_byte_string(value) || cbor_value_is_text_string(value));
Thiago Macieira9ae05812015-05-11 15:09:09 +09001080
Thiago Macieira9ae05812015-05-11 15:09:09 +09001081 CborError err;
Thiago Macieira2dcf42f2017-02-24 21:17:51 -08001082 CborValue tmp;
1083 size_t total = 0;
1084 const void *ptr;
1085
1086 if (!next)
1087 next = &tmp;
1088 *next = *value;
1089 *result = true;
1090
1091 while (1) {
1092 size_t newTotal;
1093 size_t chunkLen;
1094 err = get_string_chunk(next, &ptr, &chunkLen);
Thiago Macieira9ae05812015-05-11 15:09:09 +09001095 if (err)
1096 return err;
Thiago Macieira2dcf42f2017-02-24 21:17:51 -08001097 if (!ptr)
1098 break;
1099
1100 if (unlikely(add_check_overflow(total, chunkLen, &newTotal)))
1101 return CborErrorDataTooLarge;
1102
1103 if (*result && *buflen >= newTotal)
1104 *result = !!func(buffer + total, (const uint8_t *)ptr, chunkLen);
Thiago Macieira9ae05812015-05-11 15:09:09 +09001105 else
1106 *result = false;
Thiago Macieira9ae05812015-05-11 15:09:09 +09001107
Thiago Macieira2dcf42f2017-02-24 21:17:51 -08001108 total = newTotal;
Thiago Macieira9ae05812015-05-11 15:09:09 +09001109 }
1110
Thiago Macieiradbc01292016-06-06 17:02:25 -07001111 /* is there enough room for the ending NUL byte? */
Thiago Macieirae136feb2017-02-24 21:21:04 -08001112 if (*result && *buflen > total) {
1113 uint8_t nul[] = { 0 };
1114 *result = !!func(buffer + total, nul, 1);
1115 }
Thiago Macieira9ae05812015-05-11 15:09:09 +09001116 *buflen = total;
Thiago Macieira9ae05812015-05-11 15:09:09 +09001117 return CborNoError;
1118}
1119
Thiago Macieira2312efd2015-05-06 16:07:48 -07001120/**
Thiago Macieiraff130bc2015-06-19 15:15:33 -07001121 * \fn CborError cbor_value_copy_text_string(const CborValue *value, char *buffer, size_t *buflen, CborValue *next)
1122 *
Thiago Macieiraf5a172b2018-02-05 14:53:14 -08001123 * Copies the string pointed to by \a value into the buffer provided at \a buffer
Thiago Macieira2312efd2015-05-06 16:07:48 -07001124 * of \a buflen bytes. If \a buffer is a NULL pointer, this function will not
1125 * copy anything and will only update the \a next value.
1126 *
Thiago Macieira46a818e2015-10-08 15:13:05 +02001127 * If the iterator \a value does not point to a text string, the behaviour is
1128 * undefined, so checking with \ref cbor_value_get_type or \ref
1129 * cbor_value_is_text_string is recommended.
1130 *
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -07001131 * If the provided buffer length was too small, this function returns an error
1132 * condition of \ref CborErrorOutOfMemory. If you need to calculate the length
1133 * of the string in order to preallocate a buffer, use
Thiago Macieira2312efd2015-05-06 16:07:48 -07001134 * cbor_value_calculate_string_length().
1135 *
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -07001136 * On success, this function sets the number of bytes copied to \c{*buflen}. If
1137 * the buffer is large enough, this function will insert a null byte after the
1138 * last copied byte, to facilitate manipulation of text strings. That byte is
Thiago Macieiraf5a172b2018-02-05 14:53:14 -08001139 * not included in the returned value of \c{*buflen}. If there was no space for
1140 * the terminating null, no error is returned, so callers must check the value
1141 * of *buflen after the call, before relying on the '\0'; if it has not been
1142 * changed by the call, there is no '\0'-termination on the buffer's contents.
Thiago Macieira2312efd2015-05-06 16:07:48 -07001143 *
1144 * The \a next pointer, if not null, will be updated to point to the next item
1145 * after this string. If \a value points to the last item, then \a next will be
1146 * invalid.
1147 *
Thiago Macieira46a818e2015-10-08 15:13:05 +02001148 * This function may not run in constant time (it will run in O(n) time on the
1149 * number of chunks). It requires constant memory (O(1)).
1150 *
Thiago Macieira2312efd2015-05-06 16:07:48 -07001151 * \note This function does not perform UTF-8 validation on the incoming text
1152 * string.
1153 *
Thiago Macieira51b56062017-02-23 16:03:13 -08001154 * \sa cbor_value_get_text_string_chunk() cbor_value_dup_text_string(), cbor_value_copy_byte_string(), cbor_value_get_string_length(), cbor_value_calculate_string_length()
Thiago Macieira2312efd2015-05-06 16:07:48 -07001155 */
Thiago Macieiraff130bc2015-06-19 15:15:33 -07001156
1157/**
1158 * \fn CborError cbor_value_copy_byte_string(const CborValue *value, uint8_t *buffer, size_t *buflen, CborValue *next)
1159 *
1160 * Copies the string pointed by \a value into the buffer provided at \a buffer
1161 * of \a buflen bytes. If \a buffer is a NULL pointer, this function will not
1162 * copy anything and will only update the \a next value.
1163 *
Thiago Macieira46a818e2015-10-08 15:13:05 +02001164 * If the iterator \a value does not point to a byte string, the behaviour is
1165 * undefined, so checking with \ref cbor_value_get_type or \ref
1166 * cbor_value_is_byte_string is recommended.
1167 *
Thiago Macieiraff130bc2015-06-19 15:15:33 -07001168 * If the provided buffer length was too small, this function returns an error
1169 * condition of \ref CborErrorOutOfMemory. If you need to calculate the length
1170 * of the string in order to preallocate a buffer, use
1171 * cbor_value_calculate_string_length().
1172 *
1173 * On success, this function sets the number of bytes copied to \c{*buflen}. If
1174 * the buffer is large enough, this function will insert a null byte after the
1175 * last copied byte, to facilitate manipulation of null-terminated strings.
1176 * That byte is not included in the returned value of \c{*buflen}.
1177 *
1178 * The \a next pointer, if not null, will be updated to point to the next item
1179 * after this string. If \a value points to the last item, then \a next will be
1180 * invalid.
1181 *
Thiago Macieira46a818e2015-10-08 15:13:05 +02001182 * This function may not run in constant time (it will run in O(n) time on the
1183 * number of chunks). It requires constant memory (O(1)).
1184 *
Thiago Macieira51b56062017-02-23 16:03:13 -08001185 * \sa cbor_value_get_byte_string_chunk(), cbor_value_dup_text_string(), cbor_value_copy_text_string(), cbor_value_get_string_length(), cbor_value_calculate_string_length()
Thiago Macieiraff130bc2015-06-19 15:15:33 -07001186 */
1187
1188CborError _cbor_value_copy_string(const CborValue *value, void *buffer,
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -07001189 size_t *buflen, CborValue *next)
Thiago Macieira2312efd2015-05-06 16:07:48 -07001190{
Thiago Macieira9ae05812015-05-11 15:09:09 +09001191 bool copied_all;
Thiago Macieiraed5b57c2015-07-07 16:38:27 -07001192 CborError err = iterate_string_chunks(value, (char*)buffer, buflen, &copied_all, next,
alradmsftacf202a2017-03-03 18:23:17 -08001193 buffer ? iterate_memcpy : iterate_noop);
Thiago Macieira9ae05812015-05-11 15:09:09 +09001194 return err ? err :
1195 copied_all ? CborNoError : CborErrorOutOfMemory;
Thiago Macieirac70169f2015-05-06 07:49:44 -07001196}
1197
Thiago Macieirac4a73c62015-05-09 18:14:11 -07001198/**
Thiago Macieiraf5a172b2018-02-05 14:53:14 -08001199 * Compares the entry \a value with the string \a string and stores the result
Thiago Macieira46a818e2015-10-08 15:13:05 +02001200 * in \a result. If the value is different from \a string \a result will
1201 * contain \c false.
Thiago Macieirac4a73c62015-05-09 18:14:11 -07001202 *
Thiago Macieiraf5a172b2018-02-05 14:53:14 -08001203 * The entry at \a value may be a tagged string. If \a value is not a string or
1204 * a tagged string, the comparison result will be false.
Thiago Macieira46a818e2015-10-08 15:13:05 +02001205 *
1206 * CBOR requires text strings to be encoded in UTF-8, but this function does
1207 * not validate either the strings in the stream or the string \a string to be
1208 * matched. Moreover, comparison is done on strict codepoint comparison,
1209 * without any Unicode normalization.
1210 *
1211 * This function may not run in constant time (it will run in O(n) time on the
1212 * number of chunks). It requires constant memory (O(1)).
1213 *
1214 * \sa cbor_value_skip_tag(), cbor_value_copy_text_string()
Thiago Macieirac4a73c62015-05-09 18:14:11 -07001215 */
1216CborError cbor_value_text_string_equals(const CborValue *value, const char *string, bool *result)
1217{
1218 CborValue copy = *value;
1219 CborError err = cbor_value_skip_tag(&copy);
1220 if (err)
1221 return err;
1222 if (!cbor_value_is_text_string(&copy)) {
1223 *result = false;
1224 return CborNoError;
1225 }
1226
1227 size_t len = strlen(string);
1228 return iterate_string_chunks(&copy, CONST_CAST(char *, string), &len, result, NULL, iterate_memcmp);
1229}
1230
1231/**
Thiago Macieira46a818e2015-10-08 15:13:05 +02001232 * \fn bool cbor_value_is_array(const CborValue *value)
Thiago Macieira7b623c22015-05-11 15:52:14 +09001233 *
Thiago Macieira46a818e2015-10-08 15:13:05 +02001234 * Returns true if the iterator \a value is valid and points to a CBOR array.
1235 *
1236 * \sa cbor_value_is_valid(), cbor_value_is_map()
1237 */
1238
1239/**
1240 * \fn CborError cbor_value_get_array_length(const CborValue *value, size_t *length)
1241 *
1242 * Extracts the length of the CBOR array that \a value points to and stores it
1243 * in \a result. If the iterator \a value does not point to a CBOR array, the
1244 * behaviour is undefined, so checking with \ref cbor_value_get_type or \ref
1245 * cbor_value_is_array is recommended.
1246 *
1247 * If the length of this array is not encoded in the CBOR data stream, this
1248 * function will return the recoverable error CborErrorUnknownLength. You may
1249 * also check whether that is the case by using cbor_value_is_length_known().
1250 *
1251 * \note On 32-bit platforms, this function will return error condition of \ref
1252 * CborErrorDataTooLarge if the stream indicates a length that is too big to
1253 * fit in 32-bit.
1254 *
1255 * \sa cbor_value_is_valid(), cbor_value_is_length_known()
1256 */
1257
1258/**
1259 * \fn bool cbor_value_is_map(const CborValue *value)
1260 *
1261 * Returns true if the iterator \a value is valid and points to a CBOR map.
1262 *
1263 * \sa cbor_value_is_valid(), cbor_value_is_array()
1264 */
1265
1266/**
1267 * \fn CborError cbor_value_get_map_length(const CborValue *value, size_t *length)
1268 *
1269 * Extracts the length of the CBOR map that \a value points to and stores it in
1270 * \a result. If the iterator \a value does not point to a CBOR map, the
1271 * behaviour is undefined, so checking with \ref cbor_value_get_type or \ref
1272 * cbor_value_is_map is recommended.
1273 *
1274 * If the length of this map is not encoded in the CBOR data stream, this
1275 * function will return the recoverable error CborErrorUnknownLength. You may
1276 * also check whether that is the case by using cbor_value_is_length_known().
1277 *
1278 * \note On 32-bit platforms, this function will return error condition of \ref
1279 * CborErrorDataTooLarge if the stream indicates a length that is too big to
1280 * fit in 32-bit.
1281 *
1282 * \sa cbor_value_is_valid(), cbor_value_is_length_known()
1283 */
1284
1285/**
1286 * Attempts to find the value in map \a map that corresponds to the text string
1287 * entry \a string. If the iterator \a value does not point to a CBOR map, the
1288 * behaviour is undefined, so checking with \ref cbor_value_get_type or \ref
1289 * cbor_value_is_map is recommended.
1290 *
1291 * If the item is found, it is stored in \a result. If no item is found
1292 * matching the key, then \a result will contain an element of type \ref
1293 * CborInvalidType. Matching is performed using
1294 * cbor_value_text_string_equals(), so tagged strings will also match.
1295 *
1296 * This function has a time complexity of O(n) where n is the number of
1297 * elements in the map to be searched. In addition, this function is has O(n)
1298 * memory requirement based on the number of nested containers (maps or arrays)
1299 * found as elements of this map.
1300 *
1301 * \sa cbor_value_is_valid(), cbor_value_text_string_equals(), cbor_value_advance()
Thiago Macieira7b623c22015-05-11 15:52:14 +09001302 */
1303CborError cbor_value_map_find_value(const CborValue *map, const char *string, CborValue *element)
1304{
Thiago Macieirada8e35e2017-03-05 09:58:01 +01001305 cbor_assert(cbor_value_is_map(map));
Thiago Macieira7b623c22015-05-11 15:52:14 +09001306 size_t len = strlen(string);
1307 CborError err = cbor_value_enter_container(map, element);
1308 if (err)
1309 goto error;
1310
1311 while (!cbor_value_at_end(element)) {
Thiago Macieiradbc01292016-06-06 17:02:25 -07001312 /* find the non-tag so we can compare */
Thiago Macieira7b623c22015-05-11 15:52:14 +09001313 err = cbor_value_skip_tag(element);
1314 if (err)
1315 goto error;
1316 if (cbor_value_is_text_string(element)) {
1317 bool equals;
1318 size_t dummyLen = len;
1319 err = iterate_string_chunks(element, CONST_CAST(char *, string), &dummyLen,
1320 &equals, element, iterate_memcmp);
1321 if (err)
1322 goto error;
1323 if (equals)
1324 return preparse_value(element);
1325 } else {
Thiago Macieiradbc01292016-06-06 17:02:25 -07001326 /* skip this key */
Thiago Macieira7b623c22015-05-11 15:52:14 +09001327 err = cbor_value_advance(element);
1328 if (err)
1329 goto error;
1330 }
1331
Thiago Macieiradbc01292016-06-06 17:02:25 -07001332 /* skip this value */
Thiago Macieira7b623c22015-05-11 15:52:14 +09001333 err = cbor_value_skip_tag(element);
1334 if (err)
1335 goto error;
1336 err = cbor_value_advance(element);
1337 if (err)
1338 goto error;
1339 }
1340
Thiago Macieiradbc01292016-06-06 17:02:25 -07001341 /* not found */
Thiago Macieira7b623c22015-05-11 15:52:14 +09001342 element->type = CborInvalidType;
1343 return CborNoError;
1344
1345error:
1346 element->type = CborInvalidType;
1347 return err;
1348}
1349
1350/**
Thiago Macieira46a818e2015-10-08 15:13:05 +02001351 * \fn bool cbor_value_is_float(const CborValue *value)
1352 *
1353 * Returns true if the iterator \a value is valid and points to a CBOR
1354 * single-precision floating point (32-bit).
1355 *
1356 * \sa cbor_value_is_valid(), cbor_value_is_double(), cbor_value_is_half_float()
1357 */
1358
1359/**
1360 * \fn CborError cbor_value_get_float(const CborValue *value, float *result)
1361 *
1362 * Retrieves the CBOR single-precision floating point (32-bit) value that \a
1363 * value points to and stores it in \a result. If the iterator \a value does
1364 * not point to a single-precision floating point value, the behavior is
1365 * undefined, so checking with \ref cbor_value_get_type or with \ref
1366 * cbor_value_is_float is recommended.
1367 *
1368 * \sa cbor_value_get_type(), cbor_value_is_valid(), cbor_value_is_float(), cbor_value_get_double()
1369 */
1370
1371/**
1372 * \fn bool cbor_value_is_double(const CborValue *value)
1373 *
1374 * Returns true if the iterator \a value is valid and points to a CBOR
1375 * double-precision floating point (64-bit).
1376 *
1377 * \sa cbor_value_is_valid(), cbor_value_is_float(), cbor_value_is_half_float()
1378 */
1379
1380/**
1381 * \fn CborError cbor_value_get_double(const CborValue *value, float *result)
1382 *
1383 * Retrieves the CBOR double-precision floating point (64-bit) value that \a
1384 * value points to and stores it in \a result. If the iterator \a value does
1385 * not point to a double-precision floating point value, the behavior is
1386 * undefined, so checking with \ref cbor_value_get_type or with \ref
1387 * cbor_value_is_double is recommended.
1388 *
1389 * \sa cbor_value_get_type(), cbor_value_is_valid(), cbor_value_is_double(), cbor_value_get_float()
1390 */
1391
1392/**
1393 * \fn bool cbor_value_is_half_float(const CborValue *value)
1394 *
1395 * Returns true if the iterator \a value is valid and points to a CBOR
1396 * single-precision floating point (16-bit).
1397 *
1398 * \sa cbor_value_is_valid(), cbor_value_is_double(), cbor_value_is_float()
1399 */
1400
1401/**
1402 * Retrieves the CBOR half-precision floating point (16-bit) value that \a
1403 * value points to and stores it in \a result. If the iterator \a value does
1404 * not point to a half-precision floating point value, the behavior is
1405 * undefined, so checking with \ref cbor_value_get_type or with \ref
1406 * cbor_value_is_half_float is recommended.
1407 *
1408 * Note: since the C language does not have a standard type for half-precision
1409 * floating point, this function takes a \c{void *} as a parameter for the
1410 * storage area, which must be at least 16 bits wide.
1411 *
1412 * \sa cbor_value_get_type(), cbor_value_is_valid(), cbor_value_is_half_float(), cbor_value_get_float()
Thiago Macieirac4a73c62015-05-09 18:14:11 -07001413 */
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -07001414CborError cbor_value_get_half_float(const CborValue *value, void *result)
Thiago Macieirac70169f2015-05-06 07:49:44 -07001415{
Thiago Macieirada8e35e2017-03-05 09:58:01 +01001416 cbor_assert(cbor_value_is_half_float(value));
Thiago Macieirac70169f2015-05-06 07:49:44 -07001417
Thiago Macieiradbc01292016-06-06 17:02:25 -07001418 /* size has been computed already */
Thiago Macieirac70169f2015-05-06 07:49:44 -07001419 uint16_t v = get16(value->ptr + 1);
1420 memcpy(result, &v, sizeof(v));
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -07001421 return CborNoError;
Thiago Macieira54a0e102015-05-05 21:25:06 -07001422}
Thiago Macieira46a818e2015-10-08 15:13:05 +02001423
1424/** @} */