blob: 60ce57485e26980b1442f565a6c3d741c1866ef3 [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{
Thiago Macieirad2603492018-07-08 10:57:22 -0700168 size_t bytesNeeded;
Thiago Macieira2c22d712017-03-05 00:17:35 -0600169 uint8_t additional_information = **ptr & SmallValueMask;
170 ++*ptr;
171 if (additional_information < Value8Bit) {
172 *len = additional_information;
173 return CborNoError;
174 }
175 if (unlikely(additional_information > Value64Bit))
176 return CborErrorIllegalNumber;
177
Thiago Macieirad2603492018-07-08 10:57:22 -0700178 bytesNeeded = (size_t)(1 << (additional_information - Value8Bit));
Thiago Macieira2c22d712017-03-05 00:17:35 -0600179 if (unlikely(bytesNeeded > (size_t)(end - *ptr))) {
180 return CborErrorUnexpectedEOF;
181 } else if (bytesNeeded == 1) {
182 *len = (uint8_t)(*ptr)[0];
183 } else if (bytesNeeded == 2) {
184 *len = get16(*ptr);
185 } else if (bytesNeeded == 4) {
186 *len = get32(*ptr);
187 } else {
188 *len = get64(*ptr);
189 }
190 *ptr += bytesNeeded;
191 return CborNoError;
192}
193
Thiago Macieiraf5cb94b2015-06-16 16:10:49 -0700194static CborError extract_length(const CborParser *parser, const uint8_t **ptr, size_t *len)
Thiago Macieira54a0e102015-05-05 21:25:06 -0700195{
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700196 uint64_t v;
Thiago Macieira2c22d712017-03-05 00:17:35 -0600197 CborError err = _cbor_value_extract_number(ptr, parser->end, &v);
Mike Colagrosso629d5b72016-02-24 15:12:34 -0700198 if (err) {
199 *len = 0;
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700200 return err;
Mike Colagrosso629d5b72016-02-24 15:12:34 -0700201 }
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700202
alradmsft9ba47912016-10-11 17:56:15 -0700203 *len = (size_t)v;
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700204 if (v != *len)
205 return CborErrorDataTooLarge;
206 return CborNoError;
207}
208
209static bool is_fixed_type(uint8_t type)
210{
211 return type != CborTextStringType && type != CborByteStringType && type != CborArrayType &&
212 type != CborMapType;
213}
214
215static CborError preparse_value(CborValue *it)
216{
Thiago Macieira755f9ef2018-01-15 09:51:28 -0800217 enum {
218 /* flags to keep */
219 FlagsToKeep = CborIteratorFlag_ContainerIsMap | CborIteratorFlag_NextIsMapKey
220 };
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700221 const CborParser *parser = it->parser;
Thiago Macieira11e913f2015-05-07 13:01:18 -0700222 it->type = CborInvalidType;
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700223
Thiago Macieiradbc01292016-06-06 17:02:25 -0700224 /* are we at the end? */
Thiago Macieira54a0e102015-05-05 21:25:06 -0700225 if (it->ptr == parser->end)
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700226 return CborErrorUnexpectedEOF;
Thiago Macieira54a0e102015-05-05 21:25:06 -0700227
228 uint8_t descriptor = *it->ptr;
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700229 uint8_t type = descriptor & MajorTypeMask;
Thiago Macieira851c4812015-05-08 15:23:20 -0700230 it->type = type;
Thiago Macieira755f9ef2018-01-15 09:51:28 -0800231 it->flags &= FlagsToKeep;
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700232 it->extra = (descriptor &= SmallValueMask);
233
Thiago Macieira56d99832015-05-07 14:34:27 -0700234 if (descriptor > Value64Bit) {
235 if (unlikely(descriptor != IndefiniteLength))
Thiago Macieira3f76f632015-05-12 10:10:09 +0900236 return type == CborSimpleType ? CborErrorUnknownType : CborErrorIllegalNumber;
Thiago Macieira56d99832015-05-07 14:34:27 -0700237 if (likely(!is_fixed_type(type))) {
Thiago Macieiradbc01292016-06-06 17:02:25 -0700238 /* special case */
Thiago Macieira56d99832015-05-07 14:34:27 -0700239 it->flags |= CborIteratorFlag_UnknownLength;
240 it->type = type;
241 return CborNoError;
242 }
243 return type == CborSimpleType ? CborErrorUnexpectedBreak : CborErrorIllegalNumber;
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700244 }
Thiago Macieira54a0e102015-05-05 21:25:06 -0700245
Thiago Macieirac70169f2015-05-06 07:49:44 -0700246 size_t bytesNeeded = descriptor < Value8Bit ? 0 : (1 << (descriptor - Value8Bit));
Thiago Macieira63abed92015-10-28 17:01:14 -0700247 if (bytesNeeded + 1 > (size_t)(parser->end - it->ptr))
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700248 return CborErrorUnexpectedEOF;
Thiago Macieirac70169f2015-05-06 07:49:44 -0700249
Thiago Macieira851c4812015-05-08 15:23:20 -0700250 uint8_t majortype = type >> MajorTypeShift;
251 if (majortype == NegativeIntegerType) {
Thiago Macieira54a0e102015-05-05 21:25:06 -0700252 it->flags |= CborIteratorFlag_NegativeInteger;
Thiago Macieira851c4812015-05-08 15:23:20 -0700253 it->type = CborIntegerType;
254 } else if (majortype == SimpleTypesType) {
Thiago Macieira54a0e102015-05-05 21:25:06 -0700255 switch (descriptor) {
256 case FalseValue:
257 it->extra = false;
Thiago Macieira851c4812015-05-08 15:23:20 -0700258 it->type = CborBooleanType;
Thiago Macieira991dd922015-05-07 11:57:59 -0700259 break;
260
Thiago Macieira851c4812015-05-08 15:23:20 -0700261 case SinglePrecisionFloat:
262 case DoublePrecisionFloat:
263 it->flags |= CborIteratorFlag_IntegerValueTooLarge;
Thiago Macieiradbc01292016-06-06 17:02:25 -0700264 /* fall through */
Thiago Macieira54a0e102015-05-05 21:25:06 -0700265 case TrueValue:
266 case NullValue:
267 case UndefinedValue:
268 case HalfPrecisionFloat:
Thiago Macieira851c4812015-05-08 15:23:20 -0700269 it->type = *it->ptr;
Thiago Macieira54a0e102015-05-05 21:25:06 -0700270 break;
271
272 case SimpleTypeInNextByte:
Thiago Macieira851c4812015-05-08 15:23:20 -0700273 it->extra = (uint8_t)it->ptr[1];
Thiago Macieira54a0e102015-05-05 21:25:06 -0700274#ifndef CBOR_PARSER_NO_STRICT_CHECKS
Thiago Macieira851c4812015-05-08 15:23:20 -0700275 if (unlikely(it->extra < 32)) {
276 it->type = CborInvalidType;
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700277 return CborErrorIllegalSimpleType;
Thiago Macieira851c4812015-05-08 15:23:20 -0700278 }
Thiago Macieira54a0e102015-05-05 21:25:06 -0700279#endif
Thiago Macieira991dd922015-05-07 11:57:59 -0700280 break;
281
Thiago Macieira54a0e102015-05-05 21:25:06 -0700282 case 28:
283 case 29:
284 case 30:
Thiago Macieira54a0e102015-05-05 21:25:06 -0700285 case Break:
Thiago Macieirada8e35e2017-03-05 09:58:01 +0100286 cbor_assert(false); /* these conditions can't be reached */
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700287 return CborErrorUnexpectedBreak;
Thiago Macieira54a0e102015-05-05 21:25:06 -0700288 }
Thiago Macieira851c4812015-05-08 15:23:20 -0700289 return CborNoError;
Thiago Macieira54a0e102015-05-05 21:25:06 -0700290 }
291
Thiago Macieiradbc01292016-06-06 17:02:25 -0700292 /* try to decode up to 16 bits */
Thiago Macieira54a0e102015-05-05 21:25:06 -0700293 if (descriptor < Value8Bit)
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700294 return CborNoError;
Thiago Macieira54a0e102015-05-05 21:25:06 -0700295
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700296 if (descriptor == Value8Bit)
297 it->extra = (uint8_t)it->ptr[1];
298 else if (descriptor == Value16Bit)
Thiago Macieira54a0e102015-05-05 21:25:06 -0700299 it->extra = get16(it->ptr + 1);
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700300 else
Thiago Macieiradbc01292016-06-06 17:02:25 -0700301 it->flags |= CborIteratorFlag_IntegerValueTooLarge; /* Value32Bit or Value64Bit */
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700302 return CborNoError;
303}
Thiago Macieira54a0e102015-05-05 21:25:06 -0700304
Thiago Macieira2a38a952017-12-26 17:47:54 -0200305static CborError preparse_next_value_nodecrement(CborValue *it)
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700306{
Thiago Macieira2a38a952017-12-26 17:47:54 -0200307 if (it->remaining == UINT32_MAX && it->ptr != it->parser->end && *it->ptr == (uint8_t)BreakByte) {
Thiago Macieiradbc01292016-06-06 17:02:25 -0700308 /* end of map or array */
Thiago Macieira755f9ef2018-01-15 09:51:28 -0800309 if ((it->flags & CborIteratorFlag_ContainerIsMap && it->flags & CborIteratorFlag_NextIsMapKey)
310 || it->type == CborTagType) {
311 /* but we weren't expecting it! */
312 return CborErrorUnexpectedBreak;
313 }
Thiago Macieira56d99832015-05-07 14:34:27 -0700314 ++it->ptr;
315 it->type = CborInvalidType;
316 it->remaining = 0;
317 return CborNoError;
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700318 }
Thiago Macieira56d99832015-05-07 14:34:27 -0700319
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700320 return preparse_value(it);
321}
322
Thiago Macieira2a38a952017-12-26 17:47:54 -0200323static CborError preparse_next_value(CborValue *it)
324{
Thiago Macieira755f9ef2018-01-15 09:51:28 -0800325 /* tags don't count towards item totals or whether we've successfully
326 * read a map's key or value */
327 bool itemCounts = it->type != CborTagType;
328
Thiago Macieira2a38a952017-12-26 17:47:54 -0200329 if (it->remaining != UINT32_MAX) {
Thiago Macieira755f9ef2018-01-15 09:51:28 -0800330 if (itemCounts && --it->remaining == 0) {
Thiago Macieira2a38a952017-12-26 17:47:54 -0200331 it->type = CborInvalidType;
332 return CborNoError;
333 }
334 }
Thiago Macieira755f9ef2018-01-15 09:51:28 -0800335 if (itemCounts) {
336 /* toggle the flag indicating whether this was a map key */
337 it->flags ^= CborIteratorFlag_NextIsMapKey;
338 }
Thiago Macieira2a38a952017-12-26 17:47:54 -0200339 return preparse_next_value_nodecrement(it);
340}
341
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700342static CborError advance_internal(CborValue *it)
343{
344 uint64_t length;
Thiago Macieira2c22d712017-03-05 00:17:35 -0600345 CborError err = _cbor_value_extract_number(&it->ptr, it->parser->end, &length);
Thiago Macieirada8e35e2017-03-05 09:58:01 +0100346 cbor_assert(err == CborNoError);
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700347
Thiago Macieira56d99832015-05-07 14:34:27 -0700348 if (it->type == CborByteStringType || it->type == CborTextStringType) {
Thiago Macieirada8e35e2017-03-05 09:58:01 +0100349 cbor_assert(length == (size_t)length);
350 cbor_assert((it->flags & CborIteratorFlag_UnknownLength) == 0);
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700351 it->ptr += length;
352 }
353
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700354 return preparse_next_value(it);
Thiago Macieira54a0e102015-05-05 21:25:06 -0700355}
356
Thiago Macieira2312efd2015-05-06 16:07:48 -0700357/** \internal
358 *
359 * Decodes the CBOR integer value when it is larger than the 16 bits available
360 * in value->extra. This function requires that value->flags have the
361 * CborIteratorFlag_IntegerValueTooLarge flag set.
362 *
363 * This function is also used to extract single- and double-precision floating
364 * point values (SinglePrecisionFloat == Value32Bit and DoublePrecisionFloat ==
365 * Value64Bit).
366 */
Thiago Macieira54a0e102015-05-05 21:25:06 -0700367uint64_t _cbor_value_decode_int64_internal(const CborValue *value)
368{
Thiago Macieirada8e35e2017-03-05 09:58:01 +0100369 cbor_assert(value->flags & CborIteratorFlag_IntegerValueTooLarge ||
370 value->type == CborFloatType || value->type == CborDoubleType);
Thiago Macieira851c4812015-05-08 15:23:20 -0700371
Thiago Macieiradbc01292016-06-06 17:02:25 -0700372 /* since the additional information can only be Value32Bit or Value64Bit,
373 * we just need to test for the one bit those two options differ */
Thiago Macieirada8e35e2017-03-05 09:58:01 +0100374 cbor_assert((*value->ptr & SmallValueMask) == Value32Bit || (*value->ptr & SmallValueMask) == Value64Bit);
Thiago Macieira851c4812015-05-08 15:23:20 -0700375 if ((*value->ptr & 1) == (Value32Bit & 1))
Thiago Macieira54a0e102015-05-05 21:25:06 -0700376 return get32(value->ptr + 1);
377
Thiago Macieirada8e35e2017-03-05 09:58:01 +0100378 cbor_assert((*value->ptr & SmallValueMask) == Value64Bit);
Thiago Macieira54a0e102015-05-05 21:25:06 -0700379 return get64(value->ptr + 1);
380}
381
382/**
383 * Initializes the CBOR parser for parsing \a size bytes beginning at \a
384 * buffer. Parsing will use flags set in \a flags. The iterator to the first
385 * element is returned in \a it.
Thiago Macieira2312efd2015-05-06 16:07:48 -0700386 *
387 * The \a parser structure needs to remain valid throughout the decoding
388 * process. It is not thread-safe to share one CborParser among multiple
389 * threads iterating at the same time, but the object can be copied so multiple
390 * threads can iterate.
Thiago Macieira54a0e102015-05-05 21:25:06 -0700391 */
Koen Zandberg81fbe2e2018-05-24 11:48:24 +0200392CborError cbor_parser_init(const uint8_t *buffer, size_t size, uint32_t flags, CborParser *parser, CborValue *it)
Thiago Macieira54a0e102015-05-05 21:25:06 -0700393{
394 memset(parser, 0, sizeof(*parser));
395 parser->end = buffer + size;
Thiago Macieira54a0e102015-05-05 21:25:06 -0700396 parser->flags = flags;
397 it->parser = parser;
398 it->ptr = buffer;
Thiago Macieiradbc01292016-06-06 17:02:25 -0700399 it->remaining = 1; /* there's one type altogether, usually an array or map */
Thiago Macieira755f9ef2018-01-15 09:51:28 -0800400 it->flags = 0;
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700401 return preparse_value(it);
Thiago Macieira2312efd2015-05-06 16:07:48 -0700402}
403
404/**
Thiago Macieira46a818e2015-10-08 15:13:05 +0200405 * \fn bool cbor_value_at_end(const CborValue *it)
406 *
407 * Returns true if \a it has reached the end of the iteration, usually when
Thiago Macieira740e29d2016-07-07 13:32:47 -0700408 * advancing after the last item in an array or map.
Thiago Macieira46a818e2015-10-08 15:13:05 +0200409 *
Thiago Macieira740e29d2016-07-07 13:32:47 -0700410 * In the case of the outermost CborValue object, this function returns true
411 * after decoding a single element. A pointer to the first byte of the
412 * remaining data (if any) can be obtained with cbor_value_get_next_byte().
413 *
414 * \sa cbor_value_advance(), cbor_value_is_valid(), cbor_value_get_next_byte()
415 */
416
417/**
418 * \fn const uint8_t *cbor_value_get_next_byte(const CborValue *it)
419 *
420 * Returns a pointer to the next byte that would be decoded if this CborValue
421 * object were advanced.
422 *
423 * This function is useful if cbor_value_at_end() returns true for the
424 * outermost CborValue: the pointer returned is the first byte of the data
425 * remaining in the buffer, if any. Code can decide whether to begin decoding a
426 * new CBOR data stream from this point, or parse some other data appended to
427 * the same buffer.
428 *
429 * This function may be used even after a parsing error. If that occurred,
430 * then this function returns a pointer to where the parsing error occurred.
431 * Note that the error recovery is not precise and the pointer may not indicate
432 * the exact byte containing bad data.
433 *
434 * \sa cbor_value_at_end()
Thiago Macieira46a818e2015-10-08 15:13:05 +0200435 */
436
437/**
438 * \fn bool cbor_value_is_valid(const CborValue *it)
439 *
440 * Returns true if the iterator \a it contains a valid value. Invalid iterators
441 * happen when iteration reaches the end of a container (see \ref
442 * cbor_value_at_end()) or when a search function resulted in no matches.
443 *
MÃ¥rten Nordheimc1ae5212018-02-06 09:42:16 +0100444 * \sa cbor_value_advance(), cbor_value_at_end(), cbor_value_get_type()
Thiago Macieira46a818e2015-10-08 15:13:05 +0200445 */
446
447/**
Thiago Macieira73881122017-02-26 00:20:47 -0800448 * Performs a basic validation of the CBOR stream pointed by \a it and returns
449 * the error it found. If no error was found, it returns CborNoError and the
450 * application can iterate over the items with certainty that no other errors
451 * will appear during parsing.
452 *
453 * A basic validation checks for:
454 * \list
455 * \li absence of undefined additional information bytes;
456 * \li well-formedness of all numbers, lengths, and simple values;
457 * \li string contents match reported sizes;
458 * \li arrays and maps contain the number of elements they are reported to have;
459 * \endlist
460 *
461 * For further checks, see cbor_value_validate().
462 *
463 * This function has the same timing and memory requirements as
464 * cbor_value_advance().
465 *
466 * \sa cbor_value_validate(), cbor_value_advance()
467 */
468CborError cbor_value_validate_basic(const CborValue *it)
469{
470 CborValue value = *it;
471 return cbor_value_advance(&value);
472}
473
474/**
Thiago Macieira2312efd2015-05-06 16:07:48 -0700475 * Advances the CBOR value \a it by one fixed-size position. Fixed-size types
476 * are: integers, tags, simple types (including boolean, null and undefined
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700477 * values) and floating point types.
Thiago Macieira2312efd2015-05-06 16:07:48 -0700478 *
Thiago Macieira46a818e2015-10-08 15:13:05 +0200479 * If the type is not of fixed size, this function has undefined behavior. Code
480 * must be sure that the current type is one of the fixed-size types before
481 * calling this function. This function is provided because it can guarantee
Thiago Macieiraf5a172b2018-02-05 14:53:14 -0800482 * that it runs in constant time (O(1)).
Thiago Macieira46a818e2015-10-08 15:13:05 +0200483 *
484 * If the caller is not able to determine whether the type is fixed or not, code
485 * can use the cbor_value_advance() function instead.
486 *
487 * \sa cbor_value_at_end(), cbor_value_advance(), cbor_value_enter_container(), cbor_value_leave_container()
Thiago Macieira2312efd2015-05-06 16:07:48 -0700488 */
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700489CborError cbor_value_advance_fixed(CborValue *it)
Thiago Macieira54a0e102015-05-05 21:25:06 -0700490{
Thiago Macieirada8e35e2017-03-05 09:58:01 +0100491 cbor_assert(it->type != CborInvalidType);
492 cbor_assert(is_fixed_type(it->type));
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700493 if (!it->remaining)
494 return CborErrorAdvancePastEOF;
495 return advance_internal(it);
Thiago Macieira54a0e102015-05-05 21:25:06 -0700496}
497
Thiago Macieira4a99af92015-05-12 10:41:45 +0900498static CborError advance_recursive(CborValue *it, int nestingLevel)
499{
Thiago Macieirad2603492018-07-08 10:57:22 -0700500 CborError err;
501 CborValue recursed;
502
Thiago Macieira4a99af92015-05-12 10:41:45 +0900503 if (is_fixed_type(it->type))
504 return advance_internal(it);
505
506 if (!cbor_value_is_container(it)) {
507 size_t len = SIZE_MAX;
Thiago Macieiraff130bc2015-06-19 15:15:33 -0700508 return _cbor_value_copy_string(it, NULL, &len, it);
Thiago Macieira4a99af92015-05-12 10:41:45 +0900509 }
510
Thiago Macieiradbc01292016-06-06 17:02:25 -0700511 /* map or array */
Thiago Macieira4df83642017-12-17 14:01:23 -0800512 if (nestingLevel == 0)
Thiago Macieira4a99af92015-05-12 10:41:45 +0900513 return CborErrorNestingTooDeep;
514
Thiago Macieira4a99af92015-05-12 10:41:45 +0900515 err = cbor_value_enter_container(it, &recursed);
516 if (err)
517 return err;
518 while (!cbor_value_at_end(&recursed)) {
Thiago Macieira4df83642017-12-17 14:01:23 -0800519 err = advance_recursive(&recursed, nestingLevel - 1);
Thiago Macieira4a99af92015-05-12 10:41:45 +0900520 if (err)
521 return err;
522 }
523 return cbor_value_leave_container(it, &recursed);
524}
525
526
Thiago Macieira2312efd2015-05-06 16:07:48 -0700527/**
528 * Advances the CBOR value \a it by one element, skipping over containers.
529 * Unlike cbor_value_advance_fixed(), this function can be called on a CBOR
530 * value of any type. However, if the type is a container (map or array) or a
531 * string with a chunked payload, this function will not run in constant time
532 * and will recurse into itself (it will run on O(n) time for the number of
533 * elements or chunks and will use O(n) memory for the number of nested
534 * containers).
535 *
Thiago Macieira73881122017-02-26 00:20:47 -0800536 * The number of recursions can be limited at compile time to avoid stack
537 * exhaustion in constrained systems.
538 *
Thiago Macieira46a818e2015-10-08 15:13:05 +0200539 * \sa cbor_value_at_end(), cbor_value_advance_fixed(), cbor_value_enter_container(), cbor_value_leave_container()
Thiago Macieira2312efd2015-05-06 16:07:48 -0700540 */
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700541CborError cbor_value_advance(CborValue *it)
Thiago Macieira2312efd2015-05-06 16:07:48 -0700542{
Thiago Macieirada8e35e2017-03-05 09:58:01 +0100543 cbor_assert(it->type != CborInvalidType);
Thiago Macieira2312efd2015-05-06 16:07:48 -0700544 if (!it->remaining)
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700545 return CborErrorAdvancePastEOF;
Thiago Macieira4df83642017-12-17 14:01:23 -0800546 return advance_recursive(it, CBOR_PARSER_MAX_RECURSIONS);
Thiago Macieira2312efd2015-05-06 16:07:48 -0700547}
548
549/**
Thiago Macieira46a818e2015-10-08 15:13:05 +0200550 * \fn bool cbor_value_is_tag(const CborValue *value)
551 *
552 * Returns true if the iterator \a value is valid and points to a CBOR tag.
553 *
554 * \sa cbor_value_get_tag(), cbor_value_skip_tag()
555 */
556
557/**
558 * \fn CborError cbor_value_get_tag(const CborValue *value, CborTag *result)
559 *
560 * Retrieves the CBOR tag value that \a value points to and stores it in \a
561 * result. If the iterator \a value does not point to a CBOR tag value, the
562 * behavior is undefined, so checking with \ref cbor_value_get_type or with
563 * \ref cbor_value_is_tag is recommended.
564 *
565 * \sa cbor_value_get_type(), cbor_value_is_valid(), cbor_value_is_tag()
566 */
567
568/**
Thiago Macieirac4a73c62015-05-09 18:14:11 -0700569 * Advances the CBOR value \a it until it no longer points to a tag. If \a it is
570 * already not pointing to a tag, then this function returns it unchanged.
571 *
Thiago Macieira46a818e2015-10-08 15:13:05 +0200572 * This function does not run in constant time: it will run on O(n) for n being
573 * the number of tags. It does use constant memory (O(1) memory requirements).
574 *
Thiago Macieirac4a73c62015-05-09 18:14:11 -0700575 * \sa cbor_value_advance_fixed(), cbor_value_advance()
576 */
577CborError cbor_value_skip_tag(CborValue *it)
578{
579 while (cbor_value_is_tag(it)) {
580 CborError err = cbor_value_advance_fixed(it);
581 if (err)
582 return err;
583 }
584 return CborNoError;
585}
586
Thiago Macieirac4a73c62015-05-09 18:14:11 -0700587/**
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700588 * \fn bool cbor_value_is_container(const CborValue *it)
589 *
Thiago Macieira2312efd2015-05-06 16:07:48 -0700590 * Returns true if the \a it value is a container and requires recursion in
591 * order to decode (maps and arrays), false otherwise.
592 */
Thiago Macieira54a0e102015-05-05 21:25:06 -0700593
Thiago Macieira2312efd2015-05-06 16:07:48 -0700594/**
595 * Creates a CborValue iterator pointing to the first element of the container
596 * represented by \a it and saves it in \a recursed. The \a it container object
597 * needs to be kept and passed again to cbor_value_leave_container() in order
598 * to continue iterating past this container.
599 *
Thiago Macieira46a818e2015-10-08 15:13:05 +0200600 * The \a it CborValue iterator must point to a container.
601 *
Thiago Macieira2312efd2015-05-06 16:07:48 -0700602 * \sa cbor_value_is_container(), cbor_value_leave_container(), cbor_value_advance()
603 */
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700604CborError cbor_value_enter_container(const CborValue *it, CborValue *recursed)
Thiago Macieira54a0e102015-05-05 21:25:06 -0700605{
Thiago Macieira755f9ef2018-01-15 09:51:28 -0800606 cbor_static_assert(CborIteratorFlag_ContainerIsMap == (CborMapType & ~CborArrayType));
Thiago Macieirada8e35e2017-03-05 09:58:01 +0100607 cbor_assert(cbor_value_is_container(it));
Thiago Macieira54a0e102015-05-05 21:25:06 -0700608 *recursed = *it;
Thiago Macieira56d99832015-05-07 14:34:27 -0700609
Thiago Macieira54a0e102015-05-05 21:25:06 -0700610 if (it->flags & CborIteratorFlag_UnknownLength) {
611 recursed->remaining = UINT32_MAX;
Thiago Macieira56d99832015-05-07 14:34:27 -0700612 ++recursed->ptr;
Thiago Macieira54a0e102015-05-05 21:25:06 -0700613 } else {
Thiago Macieira56d99832015-05-07 14:34:27 -0700614 uint64_t len;
Thiago Macieira2a38a952017-12-26 17:47:54 -0200615 CborError err = _cbor_value_extract_number(&recursed->ptr, recursed->parser->end, &len);
Thiago Macieirada8e35e2017-03-05 09:58:01 +0100616 cbor_assert(err == CborNoError);
Thiago Macieira56d99832015-05-07 14:34:27 -0700617
Thiago Macieirae12dfd02016-06-07 16:29:25 -0700618 recursed->remaining = (uint32_t)len;
Thiago Macieira3f76f632015-05-12 10:10:09 +0900619 if (recursed->remaining != len || len == UINT32_MAX) {
Thiago Macieiradbc01292016-06-06 17:02:25 -0700620 /* back track the pointer to indicate where the error occurred */
Thiago Macieira3f76f632015-05-12 10:10:09 +0900621 recursed->ptr = it->ptr;
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700622 return CborErrorDataTooLarge;
Thiago Macieira3f76f632015-05-12 10:10:09 +0900623 }
Thiago Macieirace16f052015-05-07 23:14:25 -0700624 if (recursed->type == CborMapType) {
Thiago Macieiradbc01292016-06-06 17:02:25 -0700625 /* maps have keys and values, so we need to multiply by 2 */
Thiago Macieira3f76f632015-05-12 10:10:09 +0900626 if (recursed->remaining > UINT32_MAX / 2) {
Thiago Macieiradbc01292016-06-06 17:02:25 -0700627 /* back track the pointer to indicate where the error occurred */
Thiago Macieira3f76f632015-05-12 10:10:09 +0900628 recursed->ptr = it->ptr;
Thiago Macieirace16f052015-05-07 23:14:25 -0700629 return CborErrorDataTooLarge;
Thiago Macieira3f76f632015-05-12 10:10:09 +0900630 }
Thiago Macieirace16f052015-05-07 23:14:25 -0700631 recursed->remaining *= 2;
632 }
Thiago Macieira2a38a952017-12-26 17:47:54 -0200633 if (len == 0) {
634 /* the case of the empty container */
635 recursed->type = CborInvalidType;
636 return CborNoError;
637 }
Thiago Macieira54a0e102015-05-05 21:25:06 -0700638 }
Thiago Macieira755f9ef2018-01-15 09:51:28 -0800639 recursed->flags = (recursed->type & CborIteratorFlag_ContainerIsMap);
Thiago Macieira2a38a952017-12-26 17:47:54 -0200640 return preparse_next_value_nodecrement(recursed);
Thiago Macieirac70169f2015-05-06 07:49:44 -0700641}
642
Thiago Macieira2312efd2015-05-06 16:07:48 -0700643/**
644 * Updates \a it to point to the next element after the container. The \a
Thiago Macieira56d99832015-05-07 14:34:27 -0700645 * recursed object needs to point to the element obtained either by advancing
646 * the last element of the container (via cbor_value_advance(),
647 * cbor_value_advance_fixed(), a nested cbor_value_leave_container(), or the \c
648 * next pointer from cbor_value_copy_string() or cbor_value_dup_string()).
Thiago Macieira2312efd2015-05-06 16:07:48 -0700649 *
Thiago Macieira46a818e2015-10-08 15:13:05 +0200650 * The \a it and \a recursed parameters must be the exact same as passed to
651 * cbor_value_enter_container().
652 *
Thiago Macieira2312efd2015-05-06 16:07:48 -0700653 * \sa cbor_value_enter_container(), cbor_value_at_end()
654 */
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700655CborError cbor_value_leave_container(CborValue *it, const CborValue *recursed)
Thiago Macieirac70169f2015-05-06 07:49:44 -0700656{
Thiago Macieirada8e35e2017-03-05 09:58:01 +0100657 cbor_assert(cbor_value_is_container(it));
658 cbor_assert(recursed->type == CborInvalidType);
Thiago Macieirac70169f2015-05-06 07:49:44 -0700659 it->ptr = recursed->ptr;
Thiago Macieira56d99832015-05-07 14:34:27 -0700660 return preparse_next_value(it);
Thiago Macieirac70169f2015-05-06 07:49:44 -0700661}
662
Thiago Macieira46a818e2015-10-08 15:13:05 +0200663
Thiago Macieira2312efd2015-05-06 16:07:48 -0700664/**
Thiago Macieira46a818e2015-10-08 15:13:05 +0200665 * \fn CborType cbor_value_get_type(const CborValue *value)
666 *
667 * Returns the type of the CBOR value that the iterator \a value points to. If
668 * \a value does not point to a valid value, this function returns \ref
669 * CborInvalidType.
670 *
671 * TinyCBOR also provides functions to test directly if a given CborValue object
672 * is of a given type, like cbor_value_is_text_string() and cbor_value_is_null().
673 *
674 * \sa cbor_value_is_valid()
675 */
676
677/**
678 * \fn bool cbor_value_is_null(const CborValue *value)
679 *
680 * Returns true if the iterator \a value is valid and points to a CBOR null type.
681 *
682 * \sa cbor_value_is_valid(), cbor_value_is_undefined()
683 */
684
685/**
686 * \fn bool cbor_value_is_undefined(const CborValue *value)
687 *
688 * Returns true if the iterator \a value is valid and points to a CBOR undefined type.
689 *
690 * \sa cbor_value_is_valid(), cbor_value_is_null()
691 */
692
693/**
694 * \fn bool cbor_value_is_boolean(const CborValue *value)
695 *
696 * Returns true if the iterator \a value is valid and points to a CBOR boolean
697 * type (true or false).
698 *
699 * \sa cbor_value_is_valid(), cbor_value_get_boolean()
700 */
701
702/**
703 * \fn CborError cbor_value_get_boolean(const CborValue *value, bool *result)
704 *
705 * Retrieves the boolean value that \a value points to and stores it in \a
706 * result. If the iterator \a value does not point to a boolean value, the
707 * behavior is undefined, so checking with \ref cbor_value_get_type or with
708 * \ref cbor_value_is_boolean is recommended.
709 *
710 * \sa cbor_value_get_type(), cbor_value_is_valid(), cbor_value_is_boolean()
711 */
712
713/**
714 * \fn bool cbor_value_is_simple_type(const CborValue *value)
715 *
716 * Returns true if the iterator \a value is valid and points to a CBOR Simple Type
717 * type (other than true, false, null and undefined).
718 *
719 * \sa cbor_value_is_valid(), cbor_value_get_simple_type()
720 */
721
722/**
723 * \fn CborError cbor_value_get_simple_type(const CborValue *value, uint8_t *result)
724 *
725 * Retrieves the CBOR Simple Type value that \a value points to and stores it
726 * in \a result. If the iterator \a value does not point to a simple_type
727 * value, the behavior is undefined, so checking with \ref cbor_value_get_type
728 * or with \ref cbor_value_is_simple_type is recommended.
729 *
730 * \sa cbor_value_get_type(), cbor_value_is_valid(), cbor_value_is_simple_type()
731 */
732
733/**
734 * \fn bool cbor_value_is_integer(const CborValue *value)
735 *
736 * Returns true if the iterator \a value is valid and points to a CBOR integer
737 * type.
738 *
739 * \sa cbor_value_is_valid(), cbor_value_get_int, cbor_value_get_int64, cbor_value_get_uint64, cbor_value_get_raw_integer
740 */
741
742/**
743 * \fn bool cbor_value_is_unsigned_integer(const CborValue *value)
744 *
745 * Returns true if the iterator \a value is valid and points to a CBOR unsigned
746 * integer type (positive values or zero).
747 *
748 * \sa cbor_value_is_valid(), cbor_value_get_uint64()
749 */
750
751/**
752 * \fn bool cbor_value_is_negative_integer(const CborValue *value)
753 *
754 * Returns true if the iterator \a value is valid and points to a CBOR negative
755 * integer type.
756 *
757 * \sa cbor_value_is_valid(), cbor_value_get_int, cbor_value_get_int64, cbor_value_get_raw_integer
758 */
759
760/**
761 * \fn CborError cbor_value_get_int(const CborValue *value, int *result)
762 *
763 * Retrieves the CBOR integer value that \a value points to and stores it in \a
764 * result. If the iterator \a value does not point to an integer value, the
765 * behavior is undefined, so checking with \ref cbor_value_get_type or with
766 * \ref cbor_value_is_integer is recommended.
767 *
768 * Note that this function does not do range-checking: integral values that do
769 * not fit in a variable of type \c{int} are silently truncated to fit. Use
Thiago Macieiraf5a172b2018-02-05 14:53:14 -0800770 * cbor_value_get_int_checked() if that is not acceptable.
Thiago Macieira46a818e2015-10-08 15:13:05 +0200771 *
772 * \sa cbor_value_get_type(), cbor_value_is_valid(), cbor_value_is_integer()
773 */
774
775/**
776 * \fn CborError cbor_value_get_int64(const CborValue *value, int64_t *result)
777 *
778 * Retrieves the CBOR integer value that \a value points to and stores it in \a
779 * result. If the iterator \a value does not point to an integer value, the
780 * behavior is undefined, so checking with \ref cbor_value_get_type or with
781 * \ref cbor_value_is_integer is recommended.
782 *
783 * Note that this function does not do range-checking: integral values that do
784 * not fit in a variable of type \c{int64_t} are silently truncated to fit. Use
785 * cbor_value_get_int64_checked() that is not acceptable.
786 *
787 * \sa cbor_value_get_type(), cbor_value_is_valid(), cbor_value_is_integer()
788 */
789
790/**
791 * \fn CborError cbor_value_get_uint64(const CborValue *value, uint64_t *result)
792 *
793 * Retrieves the CBOR integer value that \a value points to and stores it in \a
794 * result. If the iterator \a value does not point to an unsigned integer
795 * value, the behavior is undefined, so checking with \ref cbor_value_get_type
796 * or with \ref cbor_value_is_unsigned_integer is recommended.
797 *
798 * \sa cbor_value_get_type(), cbor_value_is_valid(), cbor_value_is_unsigned_integer()
799 */
800
801/**
802 * \fn CborError cbor_value_get_raw_integer(const CborValue *value, uint64_t *result)
803 *
804 * Retrieves the CBOR integer value that \a value points to and stores it in \a
805 * result. If the iterator \a value does not point to an integer value, the
806 * behavior is undefined, so checking with \ref cbor_value_get_type or with
807 * \ref cbor_value_is_integer is recommended.
808 *
809 * This function is provided because CBOR negative integers can assume values
810 * that cannot be represented with normal 64-bit integer variables.
811 *
812 * If the integer is unsigned (that is, if cbor_value_is_unsigned_integer()
813 * returns true), then \a result will contain the actual value. If the integer
814 * is negative, then \a result will contain the absolute value of that integer,
815 * minus one. That is, \c {actual = -result - 1}. On architectures using two's
816 * complement for representation of negative integers, it is equivalent to say
817 * that \a result will contain the bitwise negation of the actual value.
818 *
819 * \sa cbor_value_get_type(), cbor_value_is_valid(), cbor_value_is_integer()
820 */
821
822/**
Thiago Macieira0f02e792016-07-07 19:55:08 -0700823 * Retrieves the CBOR integer value that \a value points to and stores it in \a
824 * result. If the iterator \a value does not point to an integer value, the
825 * behavior is undefined, so checking with \ref cbor_value_get_type or with
826 * \ref cbor_value_is_integer is recommended.
827 *
Thiago Macieiraf5a172b2018-02-05 14:53:14 -0800828 * Unlike \ref cbor_value_get_int64(), this function performs a check to see if the
Thiago Macieira0f02e792016-07-07 19:55:08 -0700829 * stored integer fits in \a result without data loss. If the number is outside
830 * the valid range for the data type, this function returns the recoverable
831 * error CborErrorDataTooLarge. In that case, use either
832 * cbor_value_get_uint64() (if the number is positive) or
833 * cbor_value_get_raw_integer().
834 *
835 * \sa cbor_value_get_type(), cbor_value_is_valid(), cbor_value_is_integer(), cbor_value_get_int64()
836 */
837CborError cbor_value_get_int64_checked(const CborValue *value, int64_t *result)
838{
Thiago Macieirad2603492018-07-08 10:57:22 -0700839 uint64_t v;
Thiago Macieirada8e35e2017-03-05 09:58:01 +0100840 cbor_assert(cbor_value_is_integer(value));
Thiago Macieirad2603492018-07-08 10:57:22 -0700841 v = _cbor_value_extract_int64_helper(value);
Thiago Macieira0f02e792016-07-07 19:55:08 -0700842
843 /* Check before converting, as the standard says (C11 6.3.1.3 paragraph 3):
844 * "[if] the new type is signed and the value cannot be represented in it; either the
845 * result is implementation-defined or an implementation-defined signal is raised."
846 *
847 * The range for int64_t is -2^63 to 2^63-1 (int64_t is required to be
848 * two's complement, C11 7.20.1.1 paragraph 3), which in CBOR is
849 * represented the same way, differing only on the "sign bit" (the major
850 * type).
851 */
852
853 if (unlikely(v > (uint64_t)INT64_MAX))
854 return CborErrorDataTooLarge;
855
856 *result = v;
857 if (value->flags & CborIteratorFlag_NegativeInteger)
858 *result = -*result - 1;
859 return CborNoError;
860}
861
862/**
863 * Retrieves the CBOR integer value that \a value points to and stores it in \a
864 * result. If the iterator \a value does not point to an integer value, the
865 * behavior is undefined, so checking with \ref cbor_value_get_type or with
866 * \ref cbor_value_is_integer is recommended.
867 *
Thiago Macieiraf5a172b2018-02-05 14:53:14 -0800868 * Unlike \ref cbor_value_get_int(), this function performs a check to see if the
Thiago Macieira0f02e792016-07-07 19:55:08 -0700869 * stored integer fits in \a result without data loss. If the number is outside
870 * the valid range for the data type, this function returns the recoverable
871 * error CborErrorDataTooLarge. In that case, use one of the other integer
872 * functions to obtain the value.
873 *
874 * \sa cbor_value_get_type(), cbor_value_is_valid(), cbor_value_is_integer(), cbor_value_get_int64(),
875 * cbor_value_get_uint64(), cbor_value_get_int64_checked(), cbor_value_get_raw_integer()
876 */
877CborError cbor_value_get_int_checked(const CborValue *value, int *result)
878{
Thiago Macieirad2603492018-07-08 10:57:22 -0700879 uint64_t v;
Thiago Macieirada8e35e2017-03-05 09:58:01 +0100880 cbor_assert(cbor_value_is_integer(value));
Thiago Macieirad2603492018-07-08 10:57:22 -0700881 v = _cbor_value_extract_int64_helper(value);
Thiago Macieira0f02e792016-07-07 19:55:08 -0700882
883 /* Check before converting, as the standard says (C11 6.3.1.3 paragraph 3):
884 * "[if] the new type is signed and the value cannot be represented in it; either the
885 * result is implementation-defined or an implementation-defined signal is raised."
886 *
887 * But we can convert from signed to unsigned without fault (paragraph 2).
888 *
Thiago Macieiraf5a172b2018-02-05 14:53:14 -0800889 * The range for int is implementation-defined and int is not guaranteed to use
890 * two's complement representation (although int32_t is).
Thiago Macieira0f02e792016-07-07 19:55:08 -0700891 */
892
893 if (value->flags & CborIteratorFlag_NegativeInteger) {
894 if (unlikely(v > (unsigned) -(INT_MIN + 1)))
895 return CborErrorDataTooLarge;
896
alradmsft9ba47912016-10-11 17:56:15 -0700897 *result = (int)v;
Thiago Macieira0f02e792016-07-07 19:55:08 -0700898 *result = -*result - 1;
899 } else {
900 if (unlikely(v > (uint64_t)INT_MAX))
901 return CborErrorDataTooLarge;
902
alradmsft9ba47912016-10-11 17:56:15 -0700903 *result = (int)v;
Thiago Macieira0f02e792016-07-07 19:55:08 -0700904 }
905 return CborNoError;
906
907}
908
909/**
Thiago Macieira46a818e2015-10-08 15:13:05 +0200910 * \fn bool cbor_value_is_length_known(const CborValue *value)
911 *
912 * Returns true if the length of this type is known without calculation. That
913 * is, if the length of this CBOR string, map or array is encoded in the data
914 * stream, this function returns true. If the length is not encoded, it returns
915 * false.
916 *
917 * If the length is known, code can call cbor_value_get_string_length(),
918 * cbor_value_get_array_length() or cbor_value_get_map_length() to obtain the
919 * length. If the length is not known but is necessary, code can use the
920 * cbor_value_calculate_string_length() function (no equivalent function is
921 * provided for maps and arrays).
922 */
923
924/**
925 * \fn bool cbor_value_is_text_string(const CborValue *value)
926 *
927 * Returns true if the iterator \a value is valid and points to a CBOR text
928 * string. CBOR text strings are UTF-8 encoded and usually contain
929 * human-readable text.
930 *
931 * \sa cbor_value_is_valid(), cbor_value_get_string_length(), cbor_value_calculate_string_length(),
932 * cbor_value_copy_text_string(), cbor_value_dup_text_string()
933 */
934
935/**
936 * \fn bool cbor_value_is_byte_string(const CborValue *value)
937 *
Maciej Jurczake6084652020-02-16 11:10:36 +0100938 * Returns true if the iterator \a value is valid and points to a CBOR byte
Thiago Macieira46a818e2015-10-08 15:13:05 +0200939 * string. CBOR byte strings are binary data with no specified encoding or
940 * format.
941 *
942 * \sa cbor_value_is_valid(), cbor_value_get_string_length(), cbor_value_calculate_string_length(),
943 * cbor_value_copy_byte_string(), cbor_value_dup_byte_string()
944 */
945
946/**
947 * \fn CborError cbor_value_get_string_length(const CborValue *value, size_t *length)
948 *
949 * Extracts the length of the byte or text string that \a value points to and
950 * stores it in \a result. If the iterator \a value does not point to a text
951 * string or a byte string, the behaviour is undefined, so checking with \ref
952 * cbor_value_get_type, with \ref cbor_value_is_text_string or \ref
953 * cbor_value_is_byte_string is recommended.
954 *
955 * If the length of this string is not encoded in the CBOR data stream, this
956 * function will return the recoverable error CborErrorUnknownLength. You may
957 * also check whether that is the case by using cbor_value_is_length_known().
958 *
959 * If the length of the string is required but the length was not encoded, use
960 * cbor_value_calculate_string_length(), but note that that function does not
961 * run in constant time.
962 *
963 * \note On 32-bit platforms, this function will return error condition of \ref
964 * CborErrorDataTooLarge if the stream indicates a length that is too big to
965 * fit in 32-bit.
966 *
967 * \sa cbor_value_is_valid(), cbor_value_is_length_known(), cbor_value_calculate_string_length()
968 */
969
970/**
971 * Calculates the length of the byte or text string that \a value points to and
972 * stores it in \a len. If the iterator \a value does not point to a text
973 * string or a byte string, the behaviour is undefined, so checking with \ref
974 * cbor_value_get_type, with \ref cbor_value_is_text_string or \ref
975 * cbor_value_is_byte_string is recommended.
976 *
977 * This function is different from cbor_value_get_string_length() in that it
978 * calculates the length even for strings sent in chunks. For that reason, this
979 * function may not run in constant time (it will run in O(n) time on the
980 * number of chunks). It does use constant memory (O(1)).
Thiago Macieira2312efd2015-05-06 16:07:48 -0700981 *
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700982 * \note On 32-bit platforms, this function will return error condition of \ref
983 * CborErrorDataTooLarge if the stream indicates a length that is too big to
984 * fit in 32-bit.
Thiago Macieira2312efd2015-05-06 16:07:48 -0700985 *
Thiago Macieira51b56062017-02-23 16:03:13 -0800986 * \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 -0700987 */
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700988CborError cbor_value_calculate_string_length(const CborValue *value, size_t *len)
Thiago Macieira2312efd2015-05-06 16:07:48 -0700989{
Thiago Macieira9ae05812015-05-11 15:09:09 +0900990 *len = SIZE_MAX;
Thiago Macieiraff130bc2015-06-19 15:15:33 -0700991 return _cbor_value_copy_string(value, NULL, len, NULL);
Thiago Macieirac70169f2015-05-06 07:49:44 -0700992}
993
Thiago Macieira57ba7c92017-02-25 22:32:20 -0800994static inline void prepare_string_iteration(CborValue *it)
995{
996 if (!cbor_value_is_length_known(it)) {
997 /* chunked string: we're before the first chunk;
998 * advance to the first chunk */
999 ++it->ptr;
1000 it->flags |= CborIteratorFlag_IteratingStringChunks;
1001 }
1002}
1003
Andreas Zisowsky70aba6b2018-03-19 11:51:35 +01001004CborError CBOR_INTERNAL_API_CC _cbor_value_prepare_string_iteration(CborValue *it)
Thiago Macieira57ba7c92017-02-25 22:32:20 -08001005{
1006 cbor_assert((it->flags & CborIteratorFlag_IteratingStringChunks) == 0);
1007 prepare_string_iteration(it);
1008
1009 /* are we at the end? */
1010 if (it->ptr == it->parser->end)
1011 return CborErrorUnexpectedEOF;
1012 return CborNoError;
1013}
1014
Thiago Macieira51b56062017-02-23 16:03:13 -08001015static CborError get_string_chunk(CborValue *it, const void **bufferptr, size_t *len)
1016{
1017 CborError err;
1018
1019 /* Possible states:
1020 * length known | iterating | meaning
1021 * no | no | before the first chunk of a chunked string
1022 * yes | no | at a non-chunked string
1023 * no | yes | second or later chunk
1024 * yes | yes | after a non-chunked string
1025 */
1026 if (it->flags & CborIteratorFlag_IteratingStringChunks) {
1027 /* already iterating */
1028 if (cbor_value_is_length_known(it)) {
1029 /* if the length was known, it wasn't chunked, so finish iteration */
1030 goto last_chunk;
1031 }
Thiago Macieira57ba7c92017-02-25 22:32:20 -08001032 } else {
1033 prepare_string_iteration(it);
Thiago Macieira51b56062017-02-23 16:03:13 -08001034 }
1035
1036 /* are we at the end? */
1037 if (it->ptr == it->parser->end)
1038 return CborErrorUnexpectedEOF;
1039
1040 if (*it->ptr == BreakByte) {
1041 /* last chunk */
1042 ++it->ptr;
1043last_chunk:
1044 *bufferptr = NULL;
Thiago Macieirab0879392017-12-22 10:47:00 -02001045 *len = 0;
Thiago Macieira51b56062017-02-23 16:03:13 -08001046 return preparse_next_value(it);
1047 } else if ((uint8_t)(*it->ptr & MajorTypeMask) == it->type) {
1048 err = extract_length(it->parser, &it->ptr, len);
1049 if (err)
1050 return err;
1051 if (*len > (size_t)(it->parser->end - it->ptr))
1052 return CborErrorUnexpectedEOF;
1053
1054 *bufferptr = it->ptr;
1055 it->ptr += *len;
1056 } else {
1057 return CborErrorIllegalType;
1058 }
1059
1060 it->flags |= CborIteratorFlag_IteratingStringChunks;
1061 return CborNoError;
1062}
1063
Andreas Zisowsky70aba6b2018-03-19 11:51:35 +01001064CborError CBOR_INTERNAL_API_CC
1065_cbor_value_get_string_chunk(const CborValue *value, const void **bufferptr,
1066 size_t *len, CborValue *next)
Thiago Macieira51b56062017-02-23 16:03:13 -08001067{
1068 CborValue tmp;
1069 if (!next)
1070 next = &tmp;
1071 *next = *value;
1072 return get_string_chunk(next, bufferptr, len);
1073}
1074
Thiago Macieiradbc01292016-06-06 17:02:25 -07001075/* We return uintptr_t so that we can pass memcpy directly as the iteration
1076 * function. The choice is to optimize for memcpy, which is used in the base
1077 * parser API (cbor_value_copy_string), while memcmp is used in convenience API
1078 * only. */
Thiago Macieira5752ce52015-06-16 12:10:03 -07001079typedef uintptr_t (*IterateFunction)(char *, const uint8_t *, size_t);
Thiago Macieira9ae05812015-05-11 15:09:09 +09001080
Thiago Macieira5752ce52015-06-16 12:10:03 -07001081static uintptr_t iterate_noop(char *dest, const uint8_t *src, size_t len)
Thiago Macieira9ae05812015-05-11 15:09:09 +09001082{
1083 (void)dest;
1084 (void)src;
1085 (void)len;
1086 return true;
1087}
1088
Thiago Macieira5752ce52015-06-16 12:10:03 -07001089static uintptr_t iterate_memcmp(char *s1, const uint8_t *s2, size_t len)
Thiago Macieirac4a73c62015-05-09 18:14:11 -07001090{
Thiago Macieira5752ce52015-06-16 12:10:03 -07001091 return memcmp(s1, (const char *)s2, len) == 0;
Thiago Macieirac4a73c62015-05-09 18:14:11 -07001092}
1093
alradmsftacf202a2017-03-03 18:23:17 -08001094static uintptr_t iterate_memcpy(char *dest, const uint8_t *src, size_t len)
1095{
1096 return (uintptr_t)memcpy(dest, src, len);
1097}
1098
Thiago Macieira9ae05812015-05-11 15:09:09 +09001099static CborError iterate_string_chunks(const CborValue *value, char *buffer, size_t *buflen,
1100 bool *result, CborValue *next, IterateFunction func)
1101{
Thiago Macieira9ae05812015-05-11 15:09:09 +09001102 CborError err;
Thiago Macieira2dcf42f2017-02-24 21:17:51 -08001103 CborValue tmp;
1104 size_t total = 0;
1105 const void *ptr;
1106
Thiago Macieirad2603492018-07-08 10:57:22 -07001107 cbor_assert(cbor_value_is_byte_string(value) || cbor_value_is_text_string(value));
Thiago Macieira2dcf42f2017-02-24 21:17:51 -08001108 if (!next)
1109 next = &tmp;
1110 *next = *value;
1111 *result = true;
1112
1113 while (1) {
1114 size_t newTotal;
1115 size_t chunkLen;
1116 err = get_string_chunk(next, &ptr, &chunkLen);
Thiago Macieira9ae05812015-05-11 15:09:09 +09001117 if (err)
1118 return err;
Thiago Macieira2dcf42f2017-02-24 21:17:51 -08001119 if (!ptr)
1120 break;
1121
1122 if (unlikely(add_check_overflow(total, chunkLen, &newTotal)))
1123 return CborErrorDataTooLarge;
1124
1125 if (*result && *buflen >= newTotal)
1126 *result = !!func(buffer + total, (const uint8_t *)ptr, chunkLen);
Thiago Macieira9ae05812015-05-11 15:09:09 +09001127 else
1128 *result = false;
Thiago Macieira9ae05812015-05-11 15:09:09 +09001129
Thiago Macieira2dcf42f2017-02-24 21:17:51 -08001130 total = newTotal;
Thiago Macieira9ae05812015-05-11 15:09:09 +09001131 }
1132
Thiago Macieiradbc01292016-06-06 17:02:25 -07001133 /* is there enough room for the ending NUL byte? */
Thiago Macieirae136feb2017-02-24 21:21:04 -08001134 if (*result && *buflen > total) {
1135 uint8_t nul[] = { 0 };
1136 *result = !!func(buffer + total, nul, 1);
1137 }
Thiago Macieira9ae05812015-05-11 15:09:09 +09001138 *buflen = total;
Thiago Macieira9ae05812015-05-11 15:09:09 +09001139 return CborNoError;
1140}
1141
Thiago Macieira2312efd2015-05-06 16:07:48 -07001142/**
Thiago Macieiraff130bc2015-06-19 15:15:33 -07001143 * \fn CborError cbor_value_copy_text_string(const CborValue *value, char *buffer, size_t *buflen, CborValue *next)
1144 *
Thiago Macieiraf5a172b2018-02-05 14:53:14 -08001145 * Copies the string pointed to by \a value into the buffer provided at \a buffer
Thiago Macieira2312efd2015-05-06 16:07:48 -07001146 * of \a buflen bytes. If \a buffer is a NULL pointer, this function will not
1147 * copy anything and will only update the \a next value.
1148 *
Thiago Macieira46a818e2015-10-08 15:13:05 +02001149 * If the iterator \a value does not point to a text string, the behaviour is
1150 * undefined, so checking with \ref cbor_value_get_type or \ref
1151 * cbor_value_is_text_string is recommended.
1152 *
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -07001153 * If the provided buffer length was too small, this function returns an error
1154 * condition of \ref CborErrorOutOfMemory. If you need to calculate the length
1155 * of the string in order to preallocate a buffer, use
Thiago Macieira2312efd2015-05-06 16:07:48 -07001156 * cbor_value_calculate_string_length().
1157 *
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -07001158 * On success, this function sets the number of bytes copied to \c{*buflen}. If
1159 * the buffer is large enough, this function will insert a null byte after the
1160 * last copied byte, to facilitate manipulation of text strings. That byte is
Thiago Macieiraf5a172b2018-02-05 14:53:14 -08001161 * not included in the returned value of \c{*buflen}. If there was no space for
1162 * the terminating null, no error is returned, so callers must check the value
1163 * of *buflen after the call, before relying on the '\0'; if it has not been
1164 * changed by the call, there is no '\0'-termination on the buffer's contents.
Thiago Macieira2312efd2015-05-06 16:07:48 -07001165 *
1166 * The \a next pointer, if not null, will be updated to point to the next item
1167 * after this string. If \a value points to the last item, then \a next will be
1168 * invalid.
1169 *
Thiago Macieira46a818e2015-10-08 15:13:05 +02001170 * This function may not run in constant time (it will run in O(n) time on the
1171 * number of chunks). It requires constant memory (O(1)).
1172 *
Thiago Macieira2312efd2015-05-06 16:07:48 -07001173 * \note This function does not perform UTF-8 validation on the incoming text
1174 * string.
1175 *
Thiago Macieira51b56062017-02-23 16:03:13 -08001176 * \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 -07001177 */
Thiago Macieiraff130bc2015-06-19 15:15:33 -07001178
1179/**
1180 * \fn CborError cbor_value_copy_byte_string(const CborValue *value, uint8_t *buffer, size_t *buflen, CborValue *next)
1181 *
1182 * Copies the string pointed by \a value into the buffer provided at \a buffer
1183 * of \a buflen bytes. If \a buffer is a NULL pointer, this function will not
1184 * copy anything and will only update the \a next value.
1185 *
Thiago Macieira46a818e2015-10-08 15:13:05 +02001186 * If the iterator \a value does not point to a byte string, the behaviour is
1187 * undefined, so checking with \ref cbor_value_get_type or \ref
1188 * cbor_value_is_byte_string is recommended.
1189 *
Thiago Macieiraff130bc2015-06-19 15:15:33 -07001190 * If the provided buffer length was too small, this function returns an error
1191 * condition of \ref CborErrorOutOfMemory. If you need to calculate the length
1192 * of the string in order to preallocate a buffer, use
1193 * cbor_value_calculate_string_length().
1194 *
1195 * On success, this function sets the number of bytes copied to \c{*buflen}. If
1196 * the buffer is large enough, this function will insert a null byte after the
1197 * last copied byte, to facilitate manipulation of null-terminated strings.
1198 * That byte is not included in the returned value of \c{*buflen}.
1199 *
1200 * The \a next pointer, if not null, will be updated to point to the next item
1201 * after this string. If \a value points to the last item, then \a next will be
1202 * invalid.
1203 *
Thiago Macieira46a818e2015-10-08 15:13:05 +02001204 * This function may not run in constant time (it will run in O(n) time on the
1205 * number of chunks). It requires constant memory (O(1)).
1206 *
Thiago Macieira51b56062017-02-23 16:03:13 -08001207 * \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 -07001208 */
1209
1210CborError _cbor_value_copy_string(const CborValue *value, void *buffer,
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -07001211 size_t *buflen, CborValue *next)
Thiago Macieira2312efd2015-05-06 16:07:48 -07001212{
Thiago Macieira9ae05812015-05-11 15:09:09 +09001213 bool copied_all;
Thiago Macieiraed5b57c2015-07-07 16:38:27 -07001214 CborError err = iterate_string_chunks(value, (char*)buffer, buflen, &copied_all, next,
alradmsftacf202a2017-03-03 18:23:17 -08001215 buffer ? iterate_memcpy : iterate_noop);
Thiago Macieira9ae05812015-05-11 15:09:09 +09001216 return err ? err :
1217 copied_all ? CborNoError : CborErrorOutOfMemory;
Thiago Macieirac70169f2015-05-06 07:49:44 -07001218}
1219
Thiago Macieirac4a73c62015-05-09 18:14:11 -07001220/**
Thiago Macieiraf5a172b2018-02-05 14:53:14 -08001221 * Compares the entry \a value with the string \a string and stores the result
Thiago Macieira46a818e2015-10-08 15:13:05 +02001222 * in \a result. If the value is different from \a string \a result will
1223 * contain \c false.
Thiago Macieirac4a73c62015-05-09 18:14:11 -07001224 *
Thiago Macieiraf5a172b2018-02-05 14:53:14 -08001225 * The entry at \a value may be a tagged string. If \a value is not a string or
1226 * a tagged string, the comparison result will be false.
Thiago Macieira46a818e2015-10-08 15:13:05 +02001227 *
1228 * CBOR requires text strings to be encoded in UTF-8, but this function does
1229 * not validate either the strings in the stream or the string \a string to be
1230 * matched. Moreover, comparison is done on strict codepoint comparison,
1231 * without any Unicode normalization.
1232 *
1233 * This function may not run in constant time (it will run in O(n) time on the
1234 * number of chunks). It requires constant memory (O(1)).
1235 *
1236 * \sa cbor_value_skip_tag(), cbor_value_copy_text_string()
Thiago Macieirac4a73c62015-05-09 18:14:11 -07001237 */
1238CborError cbor_value_text_string_equals(const CborValue *value, const char *string, bool *result)
1239{
Thiago Macieirad2603492018-07-08 10:57:22 -07001240 size_t len;
Thiago Macieirac4a73c62015-05-09 18:14:11 -07001241 CborValue copy = *value;
1242 CborError err = cbor_value_skip_tag(&copy);
1243 if (err)
1244 return err;
1245 if (!cbor_value_is_text_string(&copy)) {
1246 *result = false;
1247 return CborNoError;
1248 }
1249
Thiago Macieirad2603492018-07-08 10:57:22 -07001250 len = strlen(string);
Thiago Macieirac4a73c62015-05-09 18:14:11 -07001251 return iterate_string_chunks(&copy, CONST_CAST(char *, string), &len, result, NULL, iterate_memcmp);
1252}
1253
1254/**
Thiago Macieira46a818e2015-10-08 15:13:05 +02001255 * \fn bool cbor_value_is_array(const CborValue *value)
Thiago Macieira7b623c22015-05-11 15:52:14 +09001256 *
Thiago Macieira46a818e2015-10-08 15:13:05 +02001257 * Returns true if the iterator \a value is valid and points to a CBOR array.
1258 *
1259 * \sa cbor_value_is_valid(), cbor_value_is_map()
1260 */
1261
1262/**
1263 * \fn CborError cbor_value_get_array_length(const CborValue *value, size_t *length)
1264 *
1265 * Extracts the length of the CBOR array that \a value points to and stores it
1266 * in \a result. If the iterator \a value does not point to a CBOR array, the
1267 * behaviour is undefined, so checking with \ref cbor_value_get_type or \ref
1268 * cbor_value_is_array is recommended.
1269 *
1270 * If the length of this array is not encoded in the CBOR data stream, this
1271 * function will return the recoverable error CborErrorUnknownLength. You may
1272 * also check whether that is the case by using cbor_value_is_length_known().
1273 *
1274 * \note On 32-bit platforms, this function will return error condition of \ref
1275 * CborErrorDataTooLarge if the stream indicates a length that is too big to
1276 * fit in 32-bit.
1277 *
1278 * \sa cbor_value_is_valid(), cbor_value_is_length_known()
1279 */
1280
1281/**
1282 * \fn bool cbor_value_is_map(const CborValue *value)
1283 *
1284 * Returns true if the iterator \a value is valid and points to a CBOR map.
1285 *
1286 * \sa cbor_value_is_valid(), cbor_value_is_array()
1287 */
1288
1289/**
1290 * \fn CborError cbor_value_get_map_length(const CborValue *value, size_t *length)
1291 *
1292 * Extracts the length of the CBOR map that \a value points to and stores it in
1293 * \a result. If the iterator \a value does not point to a CBOR map, the
1294 * behaviour is undefined, so checking with \ref cbor_value_get_type or \ref
1295 * cbor_value_is_map is recommended.
1296 *
1297 * If the length of this map is not encoded in the CBOR data stream, this
1298 * function will return the recoverable error CborErrorUnknownLength. You may
1299 * also check whether that is the case by using cbor_value_is_length_known().
1300 *
1301 * \note On 32-bit platforms, this function will return error condition of \ref
1302 * CborErrorDataTooLarge if the stream indicates a length that is too big to
1303 * fit in 32-bit.
1304 *
1305 * \sa cbor_value_is_valid(), cbor_value_is_length_known()
1306 */
1307
1308/**
1309 * Attempts to find the value in map \a map that corresponds to the text string
1310 * entry \a string. If the iterator \a value does not point to a CBOR map, the
1311 * behaviour is undefined, so checking with \ref cbor_value_get_type or \ref
1312 * cbor_value_is_map is recommended.
1313 *
1314 * If the item is found, it is stored in \a result. If no item is found
1315 * matching the key, then \a result will contain an element of type \ref
1316 * CborInvalidType. Matching is performed using
1317 * cbor_value_text_string_equals(), so tagged strings will also match.
1318 *
1319 * This function has a time complexity of O(n) where n is the number of
1320 * elements in the map to be searched. In addition, this function is has O(n)
1321 * memory requirement based on the number of nested containers (maps or arrays)
1322 * found as elements of this map.
1323 *
1324 * \sa cbor_value_is_valid(), cbor_value_text_string_equals(), cbor_value_advance()
Thiago Macieira7b623c22015-05-11 15:52:14 +09001325 */
1326CborError cbor_value_map_find_value(const CborValue *map, const char *string, CborValue *element)
1327{
Thiago Macieirad2603492018-07-08 10:57:22 -07001328 CborError err;
Thiago Macieira7b623c22015-05-11 15:52:14 +09001329 size_t len = strlen(string);
Thiago Macieirad2603492018-07-08 10:57:22 -07001330 cbor_assert(cbor_value_is_map(map));
1331 err = cbor_value_enter_container(map, element);
Thiago Macieira7b623c22015-05-11 15:52:14 +09001332 if (err)
1333 goto error;
1334
1335 while (!cbor_value_at_end(element)) {
Thiago Macieiradbc01292016-06-06 17:02:25 -07001336 /* find the non-tag so we can compare */
Thiago Macieira7b623c22015-05-11 15:52:14 +09001337 err = cbor_value_skip_tag(element);
1338 if (err)
1339 goto error;
1340 if (cbor_value_is_text_string(element)) {
1341 bool equals;
1342 size_t dummyLen = len;
1343 err = iterate_string_chunks(element, CONST_CAST(char *, string), &dummyLen,
1344 &equals, element, iterate_memcmp);
1345 if (err)
1346 goto error;
1347 if (equals)
1348 return preparse_value(element);
1349 } else {
Thiago Macieiradbc01292016-06-06 17:02:25 -07001350 /* skip this key */
Thiago Macieira7b623c22015-05-11 15:52:14 +09001351 err = cbor_value_advance(element);
1352 if (err)
1353 goto error;
1354 }
1355
Thiago Macieiradbc01292016-06-06 17:02:25 -07001356 /* skip this value */
Thiago Macieira7b623c22015-05-11 15:52:14 +09001357 err = cbor_value_skip_tag(element);
1358 if (err)
1359 goto error;
1360 err = cbor_value_advance(element);
1361 if (err)
1362 goto error;
1363 }
1364
Thiago Macieiradbc01292016-06-06 17:02:25 -07001365 /* not found */
Thiago Macieira7b623c22015-05-11 15:52:14 +09001366 element->type = CborInvalidType;
1367 return CborNoError;
1368
1369error:
1370 element->type = CborInvalidType;
1371 return err;
1372}
1373
1374/**
Thiago Macieira46a818e2015-10-08 15:13:05 +02001375 * \fn bool cbor_value_is_float(const CborValue *value)
1376 *
1377 * Returns true if the iterator \a value is valid and points to a CBOR
1378 * single-precision floating point (32-bit).
1379 *
1380 * \sa cbor_value_is_valid(), cbor_value_is_double(), cbor_value_is_half_float()
1381 */
1382
1383/**
1384 * \fn CborError cbor_value_get_float(const CborValue *value, float *result)
1385 *
1386 * Retrieves the CBOR single-precision floating point (32-bit) value that \a
1387 * value points to and stores it in \a result. If the iterator \a value does
1388 * not point to a single-precision floating point value, the behavior is
1389 * undefined, so checking with \ref cbor_value_get_type or with \ref
1390 * cbor_value_is_float is recommended.
1391 *
1392 * \sa cbor_value_get_type(), cbor_value_is_valid(), cbor_value_is_float(), cbor_value_get_double()
1393 */
1394
1395/**
1396 * \fn bool cbor_value_is_double(const CborValue *value)
1397 *
1398 * Returns true if the iterator \a value is valid and points to a CBOR
1399 * double-precision floating point (64-bit).
1400 *
1401 * \sa cbor_value_is_valid(), cbor_value_is_float(), cbor_value_is_half_float()
1402 */
1403
1404/**
1405 * \fn CborError cbor_value_get_double(const CborValue *value, float *result)
1406 *
1407 * Retrieves the CBOR double-precision floating point (64-bit) value that \a
1408 * value points to and stores it in \a result. If the iterator \a value does
1409 * not point to a double-precision floating point value, the behavior is
1410 * undefined, so checking with \ref cbor_value_get_type or with \ref
1411 * cbor_value_is_double is recommended.
1412 *
1413 * \sa cbor_value_get_type(), cbor_value_is_valid(), cbor_value_is_double(), cbor_value_get_float()
1414 */
1415
1416/**
1417 * \fn bool cbor_value_is_half_float(const CborValue *value)
1418 *
1419 * Returns true if the iterator \a value is valid and points to a CBOR
1420 * single-precision floating point (16-bit).
1421 *
1422 * \sa cbor_value_is_valid(), cbor_value_is_double(), cbor_value_is_float()
1423 */
1424
1425/**
1426 * Retrieves the CBOR half-precision floating point (16-bit) value that \a
1427 * value points to and stores it in \a result. If the iterator \a value does
1428 * not point to a half-precision floating point value, the behavior is
1429 * undefined, so checking with \ref cbor_value_get_type or with \ref
1430 * cbor_value_is_half_float is recommended.
1431 *
1432 * Note: since the C language does not have a standard type for half-precision
1433 * floating point, this function takes a \c{void *} as a parameter for the
1434 * storage area, which must be at least 16 bits wide.
1435 *
1436 * \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 -07001437 */
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -07001438CborError cbor_value_get_half_float(const CborValue *value, void *result)
Thiago Macieirac70169f2015-05-06 07:49:44 -07001439{
Thiago Macieirad2603492018-07-08 10:57:22 -07001440 uint16_t v;
Thiago Macieirada8e35e2017-03-05 09:58:01 +01001441 cbor_assert(cbor_value_is_half_float(value));
Thiago Macieirac70169f2015-05-06 07:49:44 -07001442
Thiago Macieiradbc01292016-06-06 17:02:25 -07001443 /* size has been computed already */
Thiago Macieirad2603492018-07-08 10:57:22 -07001444 v = get16(value->ptr + 1);
Thiago Macieirac70169f2015-05-06 07:49:44 -07001445 memcpy(result, &v, sizeof(v));
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -07001446 return CborNoError;
Thiago Macieira54a0e102015-05-05 21:25:06 -07001447}
Thiago Macieira46a818e2015-10-08 15:13:05 +02001448
1449/** @} */