blob: 45de2226a9ef2e6e9c08ff577e657fcf9504a04e [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{
217 const CborParser *parser = it->parser;
Thiago Macieira11e913f2015-05-07 13:01:18 -0700218 it->type = CborInvalidType;
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700219
Thiago Macieiradbc01292016-06-06 17:02:25 -0700220 /* are we at the end? */
Thiago Macieira54a0e102015-05-05 21:25:06 -0700221 if (it->ptr == parser->end)
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700222 return CborErrorUnexpectedEOF;
Thiago Macieira54a0e102015-05-05 21:25:06 -0700223
224 uint8_t descriptor = *it->ptr;
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700225 uint8_t type = descriptor & MajorTypeMask;
Thiago Macieira851c4812015-05-08 15:23:20 -0700226 it->type = type;
Thiago Macieira54a0e102015-05-05 21:25:06 -0700227 it->flags = 0;
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700228 it->extra = (descriptor &= SmallValueMask);
229
Thiago Macieira56d99832015-05-07 14:34:27 -0700230 if (descriptor > Value64Bit) {
231 if (unlikely(descriptor != IndefiniteLength))
Thiago Macieira3f76f632015-05-12 10:10:09 +0900232 return type == CborSimpleType ? CborErrorUnknownType : CborErrorIllegalNumber;
Thiago Macieira56d99832015-05-07 14:34:27 -0700233 if (likely(!is_fixed_type(type))) {
Thiago Macieiradbc01292016-06-06 17:02:25 -0700234 /* special case */
Thiago Macieira56d99832015-05-07 14:34:27 -0700235 it->flags |= CborIteratorFlag_UnknownLength;
236 it->type = type;
237 return CborNoError;
238 }
239 return type == CborSimpleType ? CborErrorUnexpectedBreak : CborErrorIllegalNumber;
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700240 }
Thiago Macieira54a0e102015-05-05 21:25:06 -0700241
Thiago Macieirac70169f2015-05-06 07:49:44 -0700242 size_t bytesNeeded = descriptor < Value8Bit ? 0 : (1 << (descriptor - Value8Bit));
Thiago Macieira63abed92015-10-28 17:01:14 -0700243 if (bytesNeeded + 1 > (size_t)(parser->end - it->ptr))
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700244 return CborErrorUnexpectedEOF;
Thiago Macieirac70169f2015-05-06 07:49:44 -0700245
Thiago Macieira851c4812015-05-08 15:23:20 -0700246 uint8_t majortype = type >> MajorTypeShift;
247 if (majortype == NegativeIntegerType) {
Thiago Macieira54a0e102015-05-05 21:25:06 -0700248 it->flags |= CborIteratorFlag_NegativeInteger;
Thiago Macieira851c4812015-05-08 15:23:20 -0700249 it->type = CborIntegerType;
250 } else if (majortype == SimpleTypesType) {
Thiago Macieira54a0e102015-05-05 21:25:06 -0700251 switch (descriptor) {
252 case FalseValue:
253 it->extra = false;
Thiago Macieira851c4812015-05-08 15:23:20 -0700254 it->type = CborBooleanType;
Thiago Macieira991dd922015-05-07 11:57:59 -0700255 break;
256
Thiago Macieira851c4812015-05-08 15:23:20 -0700257 case SinglePrecisionFloat:
258 case DoublePrecisionFloat:
259 it->flags |= CborIteratorFlag_IntegerValueTooLarge;
Thiago Macieiradbc01292016-06-06 17:02:25 -0700260 /* fall through */
Thiago Macieira54a0e102015-05-05 21:25:06 -0700261 case TrueValue:
262 case NullValue:
263 case UndefinedValue:
264 case HalfPrecisionFloat:
Thiago Macieira851c4812015-05-08 15:23:20 -0700265 it->type = *it->ptr;
Thiago Macieira54a0e102015-05-05 21:25:06 -0700266 break;
267
268 case SimpleTypeInNextByte:
Thiago Macieira851c4812015-05-08 15:23:20 -0700269 it->extra = (uint8_t)it->ptr[1];
Thiago Macieira54a0e102015-05-05 21:25:06 -0700270#ifndef CBOR_PARSER_NO_STRICT_CHECKS
Thiago Macieira851c4812015-05-08 15:23:20 -0700271 if (unlikely(it->extra < 32)) {
272 it->type = CborInvalidType;
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700273 return CborErrorIllegalSimpleType;
Thiago Macieira851c4812015-05-08 15:23:20 -0700274 }
Thiago Macieira54a0e102015-05-05 21:25:06 -0700275#endif
Thiago Macieira991dd922015-05-07 11:57:59 -0700276 break;
277
Thiago Macieira54a0e102015-05-05 21:25:06 -0700278 case 28:
279 case 29:
280 case 30:
Thiago Macieira54a0e102015-05-05 21:25:06 -0700281 case Break:
Thiago Macieirada8e35e2017-03-05 09:58:01 +0100282 cbor_assert(false); /* these conditions can't be reached */
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700283 return CborErrorUnexpectedBreak;
Thiago Macieira54a0e102015-05-05 21:25:06 -0700284 }
Thiago Macieira851c4812015-05-08 15:23:20 -0700285 return CborNoError;
Thiago Macieira54a0e102015-05-05 21:25:06 -0700286 }
287
Thiago Macieiradbc01292016-06-06 17:02:25 -0700288 /* try to decode up to 16 bits */
Thiago Macieira54a0e102015-05-05 21:25:06 -0700289 if (descriptor < Value8Bit)
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700290 return CborNoError;
Thiago Macieira54a0e102015-05-05 21:25:06 -0700291
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700292 if (descriptor == Value8Bit)
293 it->extra = (uint8_t)it->ptr[1];
294 else if (descriptor == Value16Bit)
Thiago Macieira54a0e102015-05-05 21:25:06 -0700295 it->extra = get16(it->ptr + 1);
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700296 else
Thiago Macieiradbc01292016-06-06 17:02:25 -0700297 it->flags |= CborIteratorFlag_IntegerValueTooLarge; /* Value32Bit or Value64Bit */
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700298 return CborNoError;
299}
Thiago Macieira54a0e102015-05-05 21:25:06 -0700300
Thiago Macieira2a38a952017-12-26 17:47:54 -0200301static CborError preparse_next_value_nodecrement(CborValue *it)
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700302{
Thiago Macieira2a38a952017-12-26 17:47:54 -0200303 if (it->remaining == UINT32_MAX && it->ptr != it->parser->end && *it->ptr == (uint8_t)BreakByte) {
Thiago Macieiradbc01292016-06-06 17:02:25 -0700304 /* end of map or array */
Thiago Macieira56d99832015-05-07 14:34:27 -0700305 ++it->ptr;
306 it->type = CborInvalidType;
307 it->remaining = 0;
308 return CborNoError;
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700309 }
Thiago Macieira56d99832015-05-07 14:34:27 -0700310
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700311 return preparse_value(it);
312}
313
Thiago Macieira2a38a952017-12-26 17:47:54 -0200314static CborError preparse_next_value(CborValue *it)
315{
316 if (it->remaining != UINT32_MAX) {
317 /* don't decrement the item count if the current item is tag: they don't count */
318 if (it->type != CborTagType && --it->remaining == 0) {
319 it->type = CborInvalidType;
320 return CborNoError;
321 }
322 }
323 return preparse_next_value_nodecrement(it);
324}
325
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700326static CborError advance_internal(CborValue *it)
327{
328 uint64_t length;
Thiago Macieira2c22d712017-03-05 00:17:35 -0600329 CborError err = _cbor_value_extract_number(&it->ptr, it->parser->end, &length);
Thiago Macieirada8e35e2017-03-05 09:58:01 +0100330 cbor_assert(err == CborNoError);
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700331
Thiago Macieira56d99832015-05-07 14:34:27 -0700332 if (it->type == CborByteStringType || it->type == CborTextStringType) {
Thiago Macieirada8e35e2017-03-05 09:58:01 +0100333 cbor_assert(length == (size_t)length);
334 cbor_assert((it->flags & CborIteratorFlag_UnknownLength) == 0);
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700335 it->ptr += length;
336 }
337
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700338 return preparse_next_value(it);
Thiago Macieira54a0e102015-05-05 21:25:06 -0700339}
340
Thiago Macieira2312efd2015-05-06 16:07:48 -0700341/** \internal
342 *
343 * Decodes the CBOR integer value when it is larger than the 16 bits available
344 * in value->extra. This function requires that value->flags have the
345 * CborIteratorFlag_IntegerValueTooLarge flag set.
346 *
347 * This function is also used to extract single- and double-precision floating
348 * point values (SinglePrecisionFloat == Value32Bit and DoublePrecisionFloat ==
349 * Value64Bit).
350 */
Thiago Macieira54a0e102015-05-05 21:25:06 -0700351uint64_t _cbor_value_decode_int64_internal(const CborValue *value)
352{
Thiago Macieirada8e35e2017-03-05 09:58:01 +0100353 cbor_assert(value->flags & CborIteratorFlag_IntegerValueTooLarge ||
354 value->type == CborFloatType || value->type == CborDoubleType);
Thiago Macieira851c4812015-05-08 15:23:20 -0700355
Thiago Macieiradbc01292016-06-06 17:02:25 -0700356 /* since the additional information can only be Value32Bit or Value64Bit,
357 * we just need to test for the one bit those two options differ */
Thiago Macieirada8e35e2017-03-05 09:58:01 +0100358 cbor_assert((*value->ptr & SmallValueMask) == Value32Bit || (*value->ptr & SmallValueMask) == Value64Bit);
Thiago Macieira851c4812015-05-08 15:23:20 -0700359 if ((*value->ptr & 1) == (Value32Bit & 1))
Thiago Macieira54a0e102015-05-05 21:25:06 -0700360 return get32(value->ptr + 1);
361
Thiago Macieirada8e35e2017-03-05 09:58:01 +0100362 cbor_assert((*value->ptr & SmallValueMask) == Value64Bit);
Thiago Macieira54a0e102015-05-05 21:25:06 -0700363 return get64(value->ptr + 1);
364}
365
366/**
367 * Initializes the CBOR parser for parsing \a size bytes beginning at \a
368 * buffer. Parsing will use flags set in \a flags. The iterator to the first
369 * element is returned in \a it.
Thiago Macieira2312efd2015-05-06 16:07:48 -0700370 *
371 * The \a parser structure needs to remain valid throughout the decoding
372 * process. It is not thread-safe to share one CborParser among multiple
373 * threads iterating at the same time, but the object can be copied so multiple
374 * threads can iterate.
Thiago Macieira54a0e102015-05-05 21:25:06 -0700375 */
Koen Zandberg81fbe2e2018-05-24 11:48:24 +0200376CborError cbor_parser_init(const uint8_t *buffer, size_t size, uint32_t flags, CborParser *parser, CborValue *it)
Thiago Macieira54a0e102015-05-05 21:25:06 -0700377{
378 memset(parser, 0, sizeof(*parser));
379 parser->end = buffer + size;
Thiago Macieira54a0e102015-05-05 21:25:06 -0700380 parser->flags = flags;
381 it->parser = parser;
382 it->ptr = buffer;
Thiago Macieiradbc01292016-06-06 17:02:25 -0700383 it->remaining = 1; /* there's one type altogether, usually an array or map */
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700384 return preparse_value(it);
Thiago Macieira2312efd2015-05-06 16:07:48 -0700385}
386
387/**
Thiago Macieira46a818e2015-10-08 15:13:05 +0200388 * \fn bool cbor_value_at_end(const CborValue *it)
389 *
390 * Returns true if \a it has reached the end of the iteration, usually when
Thiago Macieira740e29d2016-07-07 13:32:47 -0700391 * advancing after the last item in an array or map.
Thiago Macieira46a818e2015-10-08 15:13:05 +0200392 *
Thiago Macieira740e29d2016-07-07 13:32:47 -0700393 * In the case of the outermost CborValue object, this function returns true
394 * after decoding a single element. A pointer to the first byte of the
395 * remaining data (if any) can be obtained with cbor_value_get_next_byte().
396 *
397 * \sa cbor_value_advance(), cbor_value_is_valid(), cbor_value_get_next_byte()
398 */
399
400/**
401 * \fn const uint8_t *cbor_value_get_next_byte(const CborValue *it)
402 *
403 * Returns a pointer to the next byte that would be decoded if this CborValue
404 * object were advanced.
405 *
406 * This function is useful if cbor_value_at_end() returns true for the
407 * outermost CborValue: the pointer returned is the first byte of the data
408 * remaining in the buffer, if any. Code can decide whether to begin decoding a
409 * new CBOR data stream from this point, or parse some other data appended to
410 * the same buffer.
411 *
412 * This function may be used even after a parsing error. If that occurred,
413 * then this function returns a pointer to where the parsing error occurred.
414 * Note that the error recovery is not precise and the pointer may not indicate
415 * the exact byte containing bad data.
416 *
417 * \sa cbor_value_at_end()
Thiago Macieira46a818e2015-10-08 15:13:05 +0200418 */
419
420/**
421 * \fn bool cbor_value_is_valid(const CborValue *it)
422 *
423 * Returns true if the iterator \a it contains a valid value. Invalid iterators
424 * happen when iteration reaches the end of a container (see \ref
425 * cbor_value_at_end()) or when a search function resulted in no matches.
426 *
MÃ¥rten Nordheimc1ae5212018-02-06 09:42:16 +0100427 * \sa cbor_value_advance(), cbor_value_at_end(), cbor_value_get_type()
Thiago Macieira46a818e2015-10-08 15:13:05 +0200428 */
429
430/**
Thiago Macieira73881122017-02-26 00:20:47 -0800431 * Performs a basic validation of the CBOR stream pointed by \a it and returns
432 * the error it found. If no error was found, it returns CborNoError and the
433 * application can iterate over the items with certainty that no other errors
434 * will appear during parsing.
435 *
436 * A basic validation checks for:
437 * \list
438 * \li absence of undefined additional information bytes;
439 * \li well-formedness of all numbers, lengths, and simple values;
440 * \li string contents match reported sizes;
441 * \li arrays and maps contain the number of elements they are reported to have;
442 * \endlist
443 *
444 * For further checks, see cbor_value_validate().
445 *
446 * This function has the same timing and memory requirements as
447 * cbor_value_advance().
448 *
449 * \sa cbor_value_validate(), cbor_value_advance()
450 */
451CborError cbor_value_validate_basic(const CborValue *it)
452{
453 CborValue value = *it;
454 return cbor_value_advance(&value);
455}
456
457/**
Thiago Macieira2312efd2015-05-06 16:07:48 -0700458 * Advances the CBOR value \a it by one fixed-size position. Fixed-size types
459 * are: integers, tags, simple types (including boolean, null and undefined
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700460 * values) and floating point types.
Thiago Macieira2312efd2015-05-06 16:07:48 -0700461 *
Thiago Macieira46a818e2015-10-08 15:13:05 +0200462 * If the type is not of fixed size, this function has undefined behavior. Code
463 * must be sure that the current type is one of the fixed-size types before
464 * calling this function. This function is provided because it can guarantee
Thiago Macieiraf5a172b2018-02-05 14:53:14 -0800465 * that it runs in constant time (O(1)).
Thiago Macieira46a818e2015-10-08 15:13:05 +0200466 *
467 * If the caller is not able to determine whether the type is fixed or not, code
468 * can use the cbor_value_advance() function instead.
469 *
470 * \sa cbor_value_at_end(), cbor_value_advance(), cbor_value_enter_container(), cbor_value_leave_container()
Thiago Macieira2312efd2015-05-06 16:07:48 -0700471 */
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700472CborError cbor_value_advance_fixed(CborValue *it)
Thiago Macieira54a0e102015-05-05 21:25:06 -0700473{
Thiago Macieirada8e35e2017-03-05 09:58:01 +0100474 cbor_assert(it->type != CborInvalidType);
475 cbor_assert(is_fixed_type(it->type));
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700476 if (!it->remaining)
477 return CborErrorAdvancePastEOF;
478 return advance_internal(it);
Thiago Macieira54a0e102015-05-05 21:25:06 -0700479}
480
Thiago Macieira4a99af92015-05-12 10:41:45 +0900481static CborError advance_recursive(CborValue *it, int nestingLevel)
482{
Thiago Macieirad2603492018-07-08 10:57:22 -0700483 CborError err;
484 CborValue recursed;
485
Thiago Macieira4a99af92015-05-12 10:41:45 +0900486 if (is_fixed_type(it->type))
487 return advance_internal(it);
488
489 if (!cbor_value_is_container(it)) {
490 size_t len = SIZE_MAX;
Thiago Macieiraff130bc2015-06-19 15:15:33 -0700491 return _cbor_value_copy_string(it, NULL, &len, it);
Thiago Macieira4a99af92015-05-12 10:41:45 +0900492 }
493
Thiago Macieiradbc01292016-06-06 17:02:25 -0700494 /* map or array */
Thiago Macieira4df83642017-12-17 14:01:23 -0800495 if (nestingLevel == 0)
Thiago Macieira4a99af92015-05-12 10:41:45 +0900496 return CborErrorNestingTooDeep;
497
Thiago Macieira4a99af92015-05-12 10:41:45 +0900498 err = cbor_value_enter_container(it, &recursed);
499 if (err)
500 return err;
501 while (!cbor_value_at_end(&recursed)) {
Thiago Macieira4df83642017-12-17 14:01:23 -0800502 err = advance_recursive(&recursed, nestingLevel - 1);
Thiago Macieira4a99af92015-05-12 10:41:45 +0900503 if (err)
504 return err;
505 }
506 return cbor_value_leave_container(it, &recursed);
507}
508
509
Thiago Macieira2312efd2015-05-06 16:07:48 -0700510/**
511 * Advances the CBOR value \a it by one element, skipping over containers.
512 * Unlike cbor_value_advance_fixed(), this function can be called on a CBOR
513 * value of any type. However, if the type is a container (map or array) or a
514 * string with a chunked payload, this function will not run in constant time
515 * and will recurse into itself (it will run on O(n) time for the number of
516 * elements or chunks and will use O(n) memory for the number of nested
517 * containers).
518 *
Thiago Macieira73881122017-02-26 00:20:47 -0800519 * The number of recursions can be limited at compile time to avoid stack
520 * exhaustion in constrained systems.
521 *
Thiago Macieira46a818e2015-10-08 15:13:05 +0200522 * \sa cbor_value_at_end(), cbor_value_advance_fixed(), cbor_value_enter_container(), cbor_value_leave_container()
Thiago Macieira2312efd2015-05-06 16:07:48 -0700523 */
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700524CborError cbor_value_advance(CborValue *it)
Thiago Macieira2312efd2015-05-06 16:07:48 -0700525{
Thiago Macieirada8e35e2017-03-05 09:58:01 +0100526 cbor_assert(it->type != CborInvalidType);
Thiago Macieira2312efd2015-05-06 16:07:48 -0700527 if (!it->remaining)
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700528 return CborErrorAdvancePastEOF;
Thiago Macieira4df83642017-12-17 14:01:23 -0800529 return advance_recursive(it, CBOR_PARSER_MAX_RECURSIONS);
Thiago Macieira2312efd2015-05-06 16:07:48 -0700530}
531
532/**
Thiago Macieira46a818e2015-10-08 15:13:05 +0200533 * \fn bool cbor_value_is_tag(const CborValue *value)
534 *
535 * Returns true if the iterator \a value is valid and points to a CBOR tag.
536 *
537 * \sa cbor_value_get_tag(), cbor_value_skip_tag()
538 */
539
540/**
541 * \fn CborError cbor_value_get_tag(const CborValue *value, CborTag *result)
542 *
543 * Retrieves the CBOR tag value that \a value points to and stores it in \a
544 * result. If the iterator \a value does not point to a CBOR tag value, the
545 * behavior is undefined, so checking with \ref cbor_value_get_type or with
546 * \ref cbor_value_is_tag is recommended.
547 *
548 * \sa cbor_value_get_type(), cbor_value_is_valid(), cbor_value_is_tag()
549 */
550
551/**
Thiago Macieirac4a73c62015-05-09 18:14:11 -0700552 * Advances the CBOR value \a it until it no longer points to a tag. If \a it is
553 * already not pointing to a tag, then this function returns it unchanged.
554 *
Thiago Macieira46a818e2015-10-08 15:13:05 +0200555 * This function does not run in constant time: it will run on O(n) for n being
556 * the number of tags. It does use constant memory (O(1) memory requirements).
557 *
Thiago Macieirac4a73c62015-05-09 18:14:11 -0700558 * \sa cbor_value_advance_fixed(), cbor_value_advance()
559 */
560CborError cbor_value_skip_tag(CborValue *it)
561{
562 while (cbor_value_is_tag(it)) {
563 CborError err = cbor_value_advance_fixed(it);
564 if (err)
565 return err;
566 }
567 return CborNoError;
568}
569
Thiago Macieirac4a73c62015-05-09 18:14:11 -0700570/**
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700571 * \fn bool cbor_value_is_container(const CborValue *it)
572 *
Thiago Macieira2312efd2015-05-06 16:07:48 -0700573 * Returns true if the \a it value is a container and requires recursion in
574 * order to decode (maps and arrays), false otherwise.
575 */
Thiago Macieira54a0e102015-05-05 21:25:06 -0700576
Thiago Macieira2312efd2015-05-06 16:07:48 -0700577/**
578 * Creates a CborValue iterator pointing to the first element of the container
579 * represented by \a it and saves it in \a recursed. The \a it container object
580 * needs to be kept and passed again to cbor_value_leave_container() in order
581 * to continue iterating past this container.
582 *
Thiago Macieira46a818e2015-10-08 15:13:05 +0200583 * The \a it CborValue iterator must point to a container.
584 *
Thiago Macieira2312efd2015-05-06 16:07:48 -0700585 * \sa cbor_value_is_container(), cbor_value_leave_container(), cbor_value_advance()
586 */
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700587CborError cbor_value_enter_container(const CborValue *it, CborValue *recursed)
Thiago Macieira54a0e102015-05-05 21:25:06 -0700588{
Thiago Macieirada8e35e2017-03-05 09:58:01 +0100589 cbor_assert(cbor_value_is_container(it));
Thiago Macieira54a0e102015-05-05 21:25:06 -0700590 *recursed = *it;
Thiago Macieira56d99832015-05-07 14:34:27 -0700591
Thiago Macieira54a0e102015-05-05 21:25:06 -0700592 if (it->flags & CborIteratorFlag_UnknownLength) {
593 recursed->remaining = UINT32_MAX;
Thiago Macieira56d99832015-05-07 14:34:27 -0700594 ++recursed->ptr;
Thiago Macieira54a0e102015-05-05 21:25:06 -0700595 } else {
Thiago Macieira56d99832015-05-07 14:34:27 -0700596 uint64_t len;
Thiago Macieira2a38a952017-12-26 17:47:54 -0200597 CborError err = _cbor_value_extract_number(&recursed->ptr, recursed->parser->end, &len);
Thiago Macieirada8e35e2017-03-05 09:58:01 +0100598 cbor_assert(err == CborNoError);
Thiago Macieira56d99832015-05-07 14:34:27 -0700599
Thiago Macieirae12dfd02016-06-07 16:29:25 -0700600 recursed->remaining = (uint32_t)len;
Thiago Macieira3f76f632015-05-12 10:10:09 +0900601 if (recursed->remaining != len || len == UINT32_MAX) {
Thiago Macieiradbc01292016-06-06 17:02:25 -0700602 /* back track the pointer to indicate where the error occurred */
Thiago Macieira3f76f632015-05-12 10:10:09 +0900603 recursed->ptr = it->ptr;
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700604 return CborErrorDataTooLarge;
Thiago Macieira3f76f632015-05-12 10:10:09 +0900605 }
Thiago Macieirace16f052015-05-07 23:14:25 -0700606 if (recursed->type == CborMapType) {
Thiago Macieiradbc01292016-06-06 17:02:25 -0700607 /* maps have keys and values, so we need to multiply by 2 */
Thiago Macieira3f76f632015-05-12 10:10:09 +0900608 if (recursed->remaining > UINT32_MAX / 2) {
Thiago Macieiradbc01292016-06-06 17:02:25 -0700609 /* back track the pointer to indicate where the error occurred */
Thiago Macieira3f76f632015-05-12 10:10:09 +0900610 recursed->ptr = it->ptr;
Thiago Macieirace16f052015-05-07 23:14:25 -0700611 return CborErrorDataTooLarge;
Thiago Macieira3f76f632015-05-12 10:10:09 +0900612 }
Thiago Macieirace16f052015-05-07 23:14:25 -0700613 recursed->remaining *= 2;
614 }
Thiago Macieira2a38a952017-12-26 17:47:54 -0200615 if (len == 0) {
616 /* the case of the empty container */
617 recursed->type = CborInvalidType;
618 return CborNoError;
619 }
Thiago Macieira54a0e102015-05-05 21:25:06 -0700620 }
Thiago Macieira2a38a952017-12-26 17:47:54 -0200621 return preparse_next_value_nodecrement(recursed);
Thiago Macieirac70169f2015-05-06 07:49:44 -0700622}
623
Thiago Macieira2312efd2015-05-06 16:07:48 -0700624/**
625 * Updates \a it to point to the next element after the container. The \a
Thiago Macieira56d99832015-05-07 14:34:27 -0700626 * recursed object needs to point to the element obtained either by advancing
627 * the last element of the container (via cbor_value_advance(),
628 * cbor_value_advance_fixed(), a nested cbor_value_leave_container(), or the \c
629 * next pointer from cbor_value_copy_string() or cbor_value_dup_string()).
Thiago Macieira2312efd2015-05-06 16:07:48 -0700630 *
Thiago Macieira46a818e2015-10-08 15:13:05 +0200631 * The \a it and \a recursed parameters must be the exact same as passed to
632 * cbor_value_enter_container().
633 *
Thiago Macieira2312efd2015-05-06 16:07:48 -0700634 * \sa cbor_value_enter_container(), cbor_value_at_end()
635 */
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700636CborError cbor_value_leave_container(CborValue *it, const CborValue *recursed)
Thiago Macieirac70169f2015-05-06 07:49:44 -0700637{
Thiago Macieirada8e35e2017-03-05 09:58:01 +0100638 cbor_assert(cbor_value_is_container(it));
639 cbor_assert(recursed->type == CborInvalidType);
Thiago Macieirac70169f2015-05-06 07:49:44 -0700640 it->ptr = recursed->ptr;
Thiago Macieira56d99832015-05-07 14:34:27 -0700641 return preparse_next_value(it);
Thiago Macieirac70169f2015-05-06 07:49:44 -0700642}
643
Thiago Macieira46a818e2015-10-08 15:13:05 +0200644
Thiago Macieira2312efd2015-05-06 16:07:48 -0700645/**
Thiago Macieira46a818e2015-10-08 15:13:05 +0200646 * \fn CborType cbor_value_get_type(const CborValue *value)
647 *
648 * Returns the type of the CBOR value that the iterator \a value points to. If
649 * \a value does not point to a valid value, this function returns \ref
650 * CborInvalidType.
651 *
652 * TinyCBOR also provides functions to test directly if a given CborValue object
653 * is of a given type, like cbor_value_is_text_string() and cbor_value_is_null().
654 *
655 * \sa cbor_value_is_valid()
656 */
657
658/**
659 * \fn bool cbor_value_is_null(const CborValue *value)
660 *
661 * Returns true if the iterator \a value is valid and points to a CBOR null type.
662 *
663 * \sa cbor_value_is_valid(), cbor_value_is_undefined()
664 */
665
666/**
667 * \fn bool cbor_value_is_undefined(const CborValue *value)
668 *
669 * Returns true if the iterator \a value is valid and points to a CBOR undefined type.
670 *
671 * \sa cbor_value_is_valid(), cbor_value_is_null()
672 */
673
674/**
675 * \fn bool cbor_value_is_boolean(const CborValue *value)
676 *
677 * Returns true if the iterator \a value is valid and points to a CBOR boolean
678 * type (true or false).
679 *
680 * \sa cbor_value_is_valid(), cbor_value_get_boolean()
681 */
682
683/**
684 * \fn CborError cbor_value_get_boolean(const CborValue *value, bool *result)
685 *
686 * Retrieves the boolean value that \a value points to and stores it in \a
687 * result. If the iterator \a value does not point to a boolean value, the
688 * behavior is undefined, so checking with \ref cbor_value_get_type or with
689 * \ref cbor_value_is_boolean is recommended.
690 *
691 * \sa cbor_value_get_type(), cbor_value_is_valid(), cbor_value_is_boolean()
692 */
693
694/**
695 * \fn bool cbor_value_is_simple_type(const CborValue *value)
696 *
697 * Returns true if the iterator \a value is valid and points to a CBOR Simple Type
698 * type (other than true, false, null and undefined).
699 *
700 * \sa cbor_value_is_valid(), cbor_value_get_simple_type()
701 */
702
703/**
704 * \fn CborError cbor_value_get_simple_type(const CborValue *value, uint8_t *result)
705 *
706 * Retrieves the CBOR Simple Type value that \a value points to and stores it
707 * in \a result. If the iterator \a value does not point to a simple_type
708 * value, the behavior is undefined, so checking with \ref cbor_value_get_type
709 * or with \ref cbor_value_is_simple_type is recommended.
710 *
711 * \sa cbor_value_get_type(), cbor_value_is_valid(), cbor_value_is_simple_type()
712 */
713
714/**
715 * \fn bool cbor_value_is_integer(const CborValue *value)
716 *
717 * Returns true if the iterator \a value is valid and points to a CBOR integer
718 * type.
719 *
720 * \sa cbor_value_is_valid(), cbor_value_get_int, cbor_value_get_int64, cbor_value_get_uint64, cbor_value_get_raw_integer
721 */
722
723/**
724 * \fn bool cbor_value_is_unsigned_integer(const CborValue *value)
725 *
726 * Returns true if the iterator \a value is valid and points to a CBOR unsigned
727 * integer type (positive values or zero).
728 *
729 * \sa cbor_value_is_valid(), cbor_value_get_uint64()
730 */
731
732/**
733 * \fn bool cbor_value_is_negative_integer(const CborValue *value)
734 *
735 * Returns true if the iterator \a value is valid and points to a CBOR negative
736 * integer type.
737 *
738 * \sa cbor_value_is_valid(), cbor_value_get_int, cbor_value_get_int64, cbor_value_get_raw_integer
739 */
740
741/**
742 * \fn CborError cbor_value_get_int(const CborValue *value, int *result)
743 *
744 * Retrieves the CBOR integer value that \a value points to and stores it in \a
745 * result. If the iterator \a value does not point to an integer value, the
746 * behavior is undefined, so checking with \ref cbor_value_get_type or with
747 * \ref cbor_value_is_integer is recommended.
748 *
749 * Note that this function does not do range-checking: integral values that do
750 * not fit in a variable of type \c{int} are silently truncated to fit. Use
Thiago Macieiraf5a172b2018-02-05 14:53:14 -0800751 * cbor_value_get_int_checked() if that is not acceptable.
Thiago Macieira46a818e2015-10-08 15:13:05 +0200752 *
753 * \sa cbor_value_get_type(), cbor_value_is_valid(), cbor_value_is_integer()
754 */
755
756/**
757 * \fn CborError cbor_value_get_int64(const CborValue *value, int64_t *result)
758 *
759 * Retrieves the CBOR integer value that \a value points to and stores it in \a
760 * result. If the iterator \a value does not point to an integer value, the
761 * behavior is undefined, so checking with \ref cbor_value_get_type or with
762 * \ref cbor_value_is_integer is recommended.
763 *
764 * Note that this function does not do range-checking: integral values that do
765 * not fit in a variable of type \c{int64_t} are silently truncated to fit. Use
766 * cbor_value_get_int64_checked() that is not acceptable.
767 *
768 * \sa cbor_value_get_type(), cbor_value_is_valid(), cbor_value_is_integer()
769 */
770
771/**
772 * \fn CborError cbor_value_get_uint64(const CborValue *value, uint64_t *result)
773 *
774 * Retrieves the CBOR integer value that \a value points to and stores it in \a
775 * result. If the iterator \a value does not point to an unsigned integer
776 * value, the behavior is undefined, so checking with \ref cbor_value_get_type
777 * or with \ref cbor_value_is_unsigned_integer is recommended.
778 *
779 * \sa cbor_value_get_type(), cbor_value_is_valid(), cbor_value_is_unsigned_integer()
780 */
781
782/**
783 * \fn CborError cbor_value_get_raw_integer(const CborValue *value, uint64_t *result)
784 *
785 * Retrieves the CBOR integer value that \a value points to and stores it in \a
786 * result. If the iterator \a value does not point to an integer value, the
787 * behavior is undefined, so checking with \ref cbor_value_get_type or with
788 * \ref cbor_value_is_integer is recommended.
789 *
790 * This function is provided because CBOR negative integers can assume values
791 * that cannot be represented with normal 64-bit integer variables.
792 *
793 * If the integer is unsigned (that is, if cbor_value_is_unsigned_integer()
794 * returns true), then \a result will contain the actual value. If the integer
795 * is negative, then \a result will contain the absolute value of that integer,
796 * minus one. That is, \c {actual = -result - 1}. On architectures using two's
797 * complement for representation of negative integers, it is equivalent to say
798 * that \a result will contain the bitwise negation of the actual value.
799 *
800 * \sa cbor_value_get_type(), cbor_value_is_valid(), cbor_value_is_integer()
801 */
802
803/**
Thiago Macieira0f02e792016-07-07 19:55:08 -0700804 * 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 *
Thiago Macieiraf5a172b2018-02-05 14:53:14 -0800809 * Unlike \ref cbor_value_get_int64(), this function performs a check to see if the
Thiago Macieira0f02e792016-07-07 19:55:08 -0700810 * stored integer fits in \a result without data loss. If the number is outside
811 * the valid range for the data type, this function returns the recoverable
812 * error CborErrorDataTooLarge. In that case, use either
813 * cbor_value_get_uint64() (if the number is positive) or
814 * cbor_value_get_raw_integer().
815 *
816 * \sa cbor_value_get_type(), cbor_value_is_valid(), cbor_value_is_integer(), cbor_value_get_int64()
817 */
818CborError cbor_value_get_int64_checked(const CborValue *value, int64_t *result)
819{
Thiago Macieirad2603492018-07-08 10:57:22 -0700820 uint64_t v;
Thiago Macieirada8e35e2017-03-05 09:58:01 +0100821 cbor_assert(cbor_value_is_integer(value));
Thiago Macieirad2603492018-07-08 10:57:22 -0700822 v = _cbor_value_extract_int64_helper(value);
Thiago Macieira0f02e792016-07-07 19:55:08 -0700823
824 /* Check before converting, as the standard says (C11 6.3.1.3 paragraph 3):
825 * "[if] the new type is signed and the value cannot be represented in it; either the
826 * result is implementation-defined or an implementation-defined signal is raised."
827 *
828 * The range for int64_t is -2^63 to 2^63-1 (int64_t is required to be
829 * two's complement, C11 7.20.1.1 paragraph 3), which in CBOR is
830 * represented the same way, differing only on the "sign bit" (the major
831 * type).
832 */
833
834 if (unlikely(v > (uint64_t)INT64_MAX))
835 return CborErrorDataTooLarge;
836
837 *result = v;
838 if (value->flags & CborIteratorFlag_NegativeInteger)
839 *result = -*result - 1;
840 return CborNoError;
841}
842
843/**
844 * Retrieves the CBOR integer value that \a value points to and stores it in \a
845 * result. If the iterator \a value does not point to an integer value, the
846 * behavior is undefined, so checking with \ref cbor_value_get_type or with
847 * \ref cbor_value_is_integer is recommended.
848 *
Thiago Macieiraf5a172b2018-02-05 14:53:14 -0800849 * Unlike \ref cbor_value_get_int(), this function performs a check to see if the
Thiago Macieira0f02e792016-07-07 19:55:08 -0700850 * stored integer fits in \a result without data loss. If the number is outside
851 * the valid range for the data type, this function returns the recoverable
852 * error CborErrorDataTooLarge. In that case, use one of the other integer
853 * functions to obtain the value.
854 *
855 * \sa cbor_value_get_type(), cbor_value_is_valid(), cbor_value_is_integer(), cbor_value_get_int64(),
856 * cbor_value_get_uint64(), cbor_value_get_int64_checked(), cbor_value_get_raw_integer()
857 */
858CborError cbor_value_get_int_checked(const CborValue *value, int *result)
859{
Thiago Macieirad2603492018-07-08 10:57:22 -0700860 uint64_t v;
Thiago Macieirada8e35e2017-03-05 09:58:01 +0100861 cbor_assert(cbor_value_is_integer(value));
Thiago Macieirad2603492018-07-08 10:57:22 -0700862 v = _cbor_value_extract_int64_helper(value);
Thiago Macieira0f02e792016-07-07 19:55:08 -0700863
864 /* Check before converting, as the standard says (C11 6.3.1.3 paragraph 3):
865 * "[if] the new type is signed and the value cannot be represented in it; either the
866 * result is implementation-defined or an implementation-defined signal is raised."
867 *
868 * But we can convert from signed to unsigned without fault (paragraph 2).
869 *
Thiago Macieiraf5a172b2018-02-05 14:53:14 -0800870 * The range for int is implementation-defined and int is not guaranteed to use
871 * two's complement representation (although int32_t is).
Thiago Macieira0f02e792016-07-07 19:55:08 -0700872 */
873
874 if (value->flags & CborIteratorFlag_NegativeInteger) {
875 if (unlikely(v > (unsigned) -(INT_MIN + 1)))
876 return CborErrorDataTooLarge;
877
alradmsft9ba47912016-10-11 17:56:15 -0700878 *result = (int)v;
Thiago Macieira0f02e792016-07-07 19:55:08 -0700879 *result = -*result - 1;
880 } else {
881 if (unlikely(v > (uint64_t)INT_MAX))
882 return CborErrorDataTooLarge;
883
alradmsft9ba47912016-10-11 17:56:15 -0700884 *result = (int)v;
Thiago Macieira0f02e792016-07-07 19:55:08 -0700885 }
886 return CborNoError;
887
888}
889
890/**
Thiago Macieira46a818e2015-10-08 15:13:05 +0200891 * \fn bool cbor_value_is_length_known(const CborValue *value)
892 *
893 * Returns true if the length of this type is known without calculation. That
894 * is, if the length of this CBOR string, map or array is encoded in the data
895 * stream, this function returns true. If the length is not encoded, it returns
896 * false.
897 *
898 * If the length is known, code can call cbor_value_get_string_length(),
899 * cbor_value_get_array_length() or cbor_value_get_map_length() to obtain the
900 * length. If the length is not known but is necessary, code can use the
901 * cbor_value_calculate_string_length() function (no equivalent function is
902 * provided for maps and arrays).
903 */
904
905/**
906 * \fn bool cbor_value_is_text_string(const CborValue *value)
907 *
908 * Returns true if the iterator \a value is valid and points to a CBOR text
909 * string. CBOR text strings are UTF-8 encoded and usually contain
910 * human-readable text.
911 *
912 * \sa cbor_value_is_valid(), cbor_value_get_string_length(), cbor_value_calculate_string_length(),
913 * cbor_value_copy_text_string(), cbor_value_dup_text_string()
914 */
915
916/**
917 * \fn bool cbor_value_is_byte_string(const CborValue *value)
918 *
919 * Returns true if the iterator \a value is valid and points to a CBOR text
920 * string. CBOR byte strings are binary data with no specified encoding or
921 * format.
922 *
923 * \sa cbor_value_is_valid(), cbor_value_get_string_length(), cbor_value_calculate_string_length(),
924 * cbor_value_copy_byte_string(), cbor_value_dup_byte_string()
925 */
926
927/**
928 * \fn CborError cbor_value_get_string_length(const CborValue *value, size_t *length)
929 *
930 * Extracts the length of the byte or text string that \a value points to and
931 * stores it in \a result. If the iterator \a value does not point to a text
932 * string or a byte string, the behaviour is undefined, so checking with \ref
933 * cbor_value_get_type, with \ref cbor_value_is_text_string or \ref
934 * cbor_value_is_byte_string is recommended.
935 *
936 * If the length of this string is not encoded in the CBOR data stream, this
937 * function will return the recoverable error CborErrorUnknownLength. You may
938 * also check whether that is the case by using cbor_value_is_length_known().
939 *
940 * If the length of the string is required but the length was not encoded, use
941 * cbor_value_calculate_string_length(), but note that that function does not
942 * run in constant time.
943 *
944 * \note On 32-bit platforms, this function will return error condition of \ref
945 * CborErrorDataTooLarge if the stream indicates a length that is too big to
946 * fit in 32-bit.
947 *
948 * \sa cbor_value_is_valid(), cbor_value_is_length_known(), cbor_value_calculate_string_length()
949 */
950
951/**
952 * Calculates the length of the byte or text string that \a value points to and
953 * stores it in \a len. If the iterator \a value does not point to a text
954 * string or a byte string, the behaviour is undefined, so checking with \ref
955 * cbor_value_get_type, with \ref cbor_value_is_text_string or \ref
956 * cbor_value_is_byte_string is recommended.
957 *
958 * This function is different from cbor_value_get_string_length() in that it
959 * calculates the length even for strings sent in chunks. For that reason, this
960 * function may not run in constant time (it will run in O(n) time on the
961 * number of chunks). It does use constant memory (O(1)).
Thiago Macieira2312efd2015-05-06 16:07:48 -0700962 *
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700963 * \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.
Thiago Macieira2312efd2015-05-06 16:07:48 -0700966 *
Thiago Macieira51b56062017-02-23 16:03:13 -0800967 * \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 -0700968 */
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700969CborError cbor_value_calculate_string_length(const CborValue *value, size_t *len)
Thiago Macieira2312efd2015-05-06 16:07:48 -0700970{
Thiago Macieira9ae05812015-05-11 15:09:09 +0900971 *len = SIZE_MAX;
Thiago Macieiraff130bc2015-06-19 15:15:33 -0700972 return _cbor_value_copy_string(value, NULL, len, NULL);
Thiago Macieirac70169f2015-05-06 07:49:44 -0700973}
974
Thiago Macieira57ba7c92017-02-25 22:32:20 -0800975static inline void prepare_string_iteration(CborValue *it)
976{
977 if (!cbor_value_is_length_known(it)) {
978 /* chunked string: we're before the first chunk;
979 * advance to the first chunk */
980 ++it->ptr;
981 it->flags |= CborIteratorFlag_IteratingStringChunks;
982 }
983}
984
Andreas Zisowsky70aba6b2018-03-19 11:51:35 +0100985CborError CBOR_INTERNAL_API_CC _cbor_value_prepare_string_iteration(CborValue *it)
Thiago Macieira57ba7c92017-02-25 22:32:20 -0800986{
987 cbor_assert((it->flags & CborIteratorFlag_IteratingStringChunks) == 0);
988 prepare_string_iteration(it);
989
990 /* are we at the end? */
991 if (it->ptr == it->parser->end)
992 return CborErrorUnexpectedEOF;
993 return CborNoError;
994}
995
Thiago Macieira51b56062017-02-23 16:03:13 -0800996static CborError get_string_chunk(CborValue *it, const void **bufferptr, size_t *len)
997{
998 CborError err;
999
1000 /* Possible states:
1001 * length known | iterating | meaning
1002 * no | no | before the first chunk of a chunked string
1003 * yes | no | at a non-chunked string
1004 * no | yes | second or later chunk
1005 * yes | yes | after a non-chunked string
1006 */
1007 if (it->flags & CborIteratorFlag_IteratingStringChunks) {
1008 /* already iterating */
1009 if (cbor_value_is_length_known(it)) {
1010 /* if the length was known, it wasn't chunked, so finish iteration */
1011 goto last_chunk;
1012 }
Thiago Macieira57ba7c92017-02-25 22:32:20 -08001013 } else {
1014 prepare_string_iteration(it);
Thiago Macieira51b56062017-02-23 16:03:13 -08001015 }
1016
1017 /* are we at the end? */
1018 if (it->ptr == it->parser->end)
1019 return CborErrorUnexpectedEOF;
1020
1021 if (*it->ptr == BreakByte) {
1022 /* last chunk */
1023 ++it->ptr;
1024last_chunk:
1025 *bufferptr = NULL;
Thiago Macieirab0879392017-12-22 10:47:00 -02001026 *len = 0;
Thiago Macieira51b56062017-02-23 16:03:13 -08001027 return preparse_next_value(it);
1028 } else if ((uint8_t)(*it->ptr & MajorTypeMask) == it->type) {
1029 err = extract_length(it->parser, &it->ptr, len);
1030 if (err)
1031 return err;
1032 if (*len > (size_t)(it->parser->end - it->ptr))
1033 return CborErrorUnexpectedEOF;
1034
1035 *bufferptr = it->ptr;
1036 it->ptr += *len;
1037 } else {
1038 return CborErrorIllegalType;
1039 }
1040
1041 it->flags |= CborIteratorFlag_IteratingStringChunks;
1042 return CborNoError;
1043}
1044
Andreas Zisowsky70aba6b2018-03-19 11:51:35 +01001045CborError CBOR_INTERNAL_API_CC
1046_cbor_value_get_string_chunk(const CborValue *value, const void **bufferptr,
1047 size_t *len, CborValue *next)
Thiago Macieira51b56062017-02-23 16:03:13 -08001048{
1049 CborValue tmp;
1050 if (!next)
1051 next = &tmp;
1052 *next = *value;
1053 return get_string_chunk(next, bufferptr, len);
1054}
1055
Thiago Macieiradbc01292016-06-06 17:02:25 -07001056/* We return uintptr_t so that we can pass memcpy directly as the iteration
1057 * function. The choice is to optimize for memcpy, which is used in the base
1058 * parser API (cbor_value_copy_string), while memcmp is used in convenience API
1059 * only. */
Thiago Macieira5752ce52015-06-16 12:10:03 -07001060typedef uintptr_t (*IterateFunction)(char *, const uint8_t *, size_t);
Thiago Macieira9ae05812015-05-11 15:09:09 +09001061
Thiago Macieira5752ce52015-06-16 12:10:03 -07001062static uintptr_t iterate_noop(char *dest, const uint8_t *src, size_t len)
Thiago Macieira9ae05812015-05-11 15:09:09 +09001063{
1064 (void)dest;
1065 (void)src;
1066 (void)len;
1067 return true;
1068}
1069
Thiago Macieira5752ce52015-06-16 12:10:03 -07001070static uintptr_t iterate_memcmp(char *s1, const uint8_t *s2, size_t len)
Thiago Macieirac4a73c62015-05-09 18:14:11 -07001071{
Thiago Macieira5752ce52015-06-16 12:10:03 -07001072 return memcmp(s1, (const char *)s2, len) == 0;
Thiago Macieirac4a73c62015-05-09 18:14:11 -07001073}
1074
alradmsftacf202a2017-03-03 18:23:17 -08001075static uintptr_t iterate_memcpy(char *dest, const uint8_t *src, size_t len)
1076{
1077 return (uintptr_t)memcpy(dest, src, len);
1078}
1079
Thiago Macieira9ae05812015-05-11 15:09:09 +09001080static CborError iterate_string_chunks(const CborValue *value, char *buffer, size_t *buflen,
1081 bool *result, CborValue *next, IterateFunction func)
1082{
Thiago Macieira9ae05812015-05-11 15:09:09 +09001083 CborError err;
Thiago Macieira2dcf42f2017-02-24 21:17:51 -08001084 CborValue tmp;
1085 size_t total = 0;
1086 const void *ptr;
1087
Thiago Macieirad2603492018-07-08 10:57:22 -07001088 cbor_assert(cbor_value_is_byte_string(value) || cbor_value_is_text_string(value));
Thiago Macieira2dcf42f2017-02-24 21:17:51 -08001089 if (!next)
1090 next = &tmp;
1091 *next = *value;
1092 *result = true;
1093
1094 while (1) {
1095 size_t newTotal;
1096 size_t chunkLen;
1097 err = get_string_chunk(next, &ptr, &chunkLen);
Thiago Macieira9ae05812015-05-11 15:09:09 +09001098 if (err)
1099 return err;
Thiago Macieira2dcf42f2017-02-24 21:17:51 -08001100 if (!ptr)
1101 break;
1102
1103 if (unlikely(add_check_overflow(total, chunkLen, &newTotal)))
1104 return CborErrorDataTooLarge;
1105
1106 if (*result && *buflen >= newTotal)
1107 *result = !!func(buffer + total, (const uint8_t *)ptr, chunkLen);
Thiago Macieira9ae05812015-05-11 15:09:09 +09001108 else
1109 *result = false;
Thiago Macieira9ae05812015-05-11 15:09:09 +09001110
Thiago Macieira2dcf42f2017-02-24 21:17:51 -08001111 total = newTotal;
Thiago Macieira9ae05812015-05-11 15:09:09 +09001112 }
1113
Thiago Macieiradbc01292016-06-06 17:02:25 -07001114 /* is there enough room for the ending NUL byte? */
Thiago Macieirae136feb2017-02-24 21:21:04 -08001115 if (*result && *buflen > total) {
1116 uint8_t nul[] = { 0 };
1117 *result = !!func(buffer + total, nul, 1);
1118 }
Thiago Macieira9ae05812015-05-11 15:09:09 +09001119 *buflen = total;
Thiago Macieira9ae05812015-05-11 15:09:09 +09001120 return CborNoError;
1121}
1122
Thiago Macieira2312efd2015-05-06 16:07:48 -07001123/**
Thiago Macieiraff130bc2015-06-19 15:15:33 -07001124 * \fn CborError cbor_value_copy_text_string(const CborValue *value, char *buffer, size_t *buflen, CborValue *next)
1125 *
Thiago Macieiraf5a172b2018-02-05 14:53:14 -08001126 * Copies the string pointed to by \a value into the buffer provided at \a buffer
Thiago Macieira2312efd2015-05-06 16:07:48 -07001127 * of \a buflen bytes. If \a buffer is a NULL pointer, this function will not
1128 * copy anything and will only update the \a next value.
1129 *
Thiago Macieira46a818e2015-10-08 15:13:05 +02001130 * If the iterator \a value does not point to a text string, the behaviour is
1131 * undefined, so checking with \ref cbor_value_get_type or \ref
1132 * cbor_value_is_text_string is recommended.
1133 *
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -07001134 * If the provided buffer length was too small, this function returns an error
1135 * condition of \ref CborErrorOutOfMemory. If you need to calculate the length
1136 * of the string in order to preallocate a buffer, use
Thiago Macieira2312efd2015-05-06 16:07:48 -07001137 * cbor_value_calculate_string_length().
1138 *
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -07001139 * On success, this function sets the number of bytes copied to \c{*buflen}. If
1140 * the buffer is large enough, this function will insert a null byte after the
1141 * last copied byte, to facilitate manipulation of text strings. That byte is
Thiago Macieiraf5a172b2018-02-05 14:53:14 -08001142 * not included in the returned value of \c{*buflen}. If there was no space for
1143 * the terminating null, no error is returned, so callers must check the value
1144 * of *buflen after the call, before relying on the '\0'; if it has not been
1145 * changed by the call, there is no '\0'-termination on the buffer's contents.
Thiago Macieira2312efd2015-05-06 16:07:48 -07001146 *
1147 * The \a next pointer, if not null, will be updated to point to the next item
1148 * after this string. If \a value points to the last item, then \a next will be
1149 * invalid.
1150 *
Thiago Macieira46a818e2015-10-08 15:13:05 +02001151 * This function may not run in constant time (it will run in O(n) time on the
1152 * number of chunks). It requires constant memory (O(1)).
1153 *
Thiago Macieira2312efd2015-05-06 16:07:48 -07001154 * \note This function does not perform UTF-8 validation on the incoming text
1155 * string.
1156 *
Thiago Macieira51b56062017-02-23 16:03:13 -08001157 * \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 -07001158 */
Thiago Macieiraff130bc2015-06-19 15:15:33 -07001159
1160/**
1161 * \fn CborError cbor_value_copy_byte_string(const CborValue *value, uint8_t *buffer, size_t *buflen, CborValue *next)
1162 *
1163 * Copies the string pointed by \a value into the buffer provided at \a buffer
1164 * of \a buflen bytes. If \a buffer is a NULL pointer, this function will not
1165 * copy anything and will only update the \a next value.
1166 *
Thiago Macieira46a818e2015-10-08 15:13:05 +02001167 * If the iterator \a value does not point to a byte string, the behaviour is
1168 * undefined, so checking with \ref cbor_value_get_type or \ref
1169 * cbor_value_is_byte_string is recommended.
1170 *
Thiago Macieiraff130bc2015-06-19 15:15:33 -07001171 * If the provided buffer length was too small, this function returns an error
1172 * condition of \ref CborErrorOutOfMemory. If you need to calculate the length
1173 * of the string in order to preallocate a buffer, use
1174 * cbor_value_calculate_string_length().
1175 *
1176 * On success, this function sets the number of bytes copied to \c{*buflen}. If
1177 * the buffer is large enough, this function will insert a null byte after the
1178 * last copied byte, to facilitate manipulation of null-terminated strings.
1179 * That byte is not included in the returned value of \c{*buflen}.
1180 *
1181 * The \a next pointer, if not null, will be updated to point to the next item
1182 * after this string. If \a value points to the last item, then \a next will be
1183 * invalid.
1184 *
Thiago Macieira46a818e2015-10-08 15:13:05 +02001185 * This function may not run in constant time (it will run in O(n) time on the
1186 * number of chunks). It requires constant memory (O(1)).
1187 *
Thiago Macieira51b56062017-02-23 16:03:13 -08001188 * \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 -07001189 */
1190
1191CborError _cbor_value_copy_string(const CborValue *value, void *buffer,
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -07001192 size_t *buflen, CborValue *next)
Thiago Macieira2312efd2015-05-06 16:07:48 -07001193{
Thiago Macieira9ae05812015-05-11 15:09:09 +09001194 bool copied_all;
Thiago Macieiraed5b57c2015-07-07 16:38:27 -07001195 CborError err = iterate_string_chunks(value, (char*)buffer, buflen, &copied_all, next,
alradmsftacf202a2017-03-03 18:23:17 -08001196 buffer ? iterate_memcpy : iterate_noop);
Thiago Macieira9ae05812015-05-11 15:09:09 +09001197 return err ? err :
1198 copied_all ? CborNoError : CborErrorOutOfMemory;
Thiago Macieirac70169f2015-05-06 07:49:44 -07001199}
1200
Thiago Macieirac4a73c62015-05-09 18:14:11 -07001201/**
Thiago Macieiraf5a172b2018-02-05 14:53:14 -08001202 * Compares the entry \a value with the string \a string and stores the result
Thiago Macieira46a818e2015-10-08 15:13:05 +02001203 * in \a result. If the value is different from \a string \a result will
1204 * contain \c false.
Thiago Macieirac4a73c62015-05-09 18:14:11 -07001205 *
Thiago Macieiraf5a172b2018-02-05 14:53:14 -08001206 * The entry at \a value may be a tagged string. If \a value is not a string or
1207 * a tagged string, the comparison result will be false.
Thiago Macieira46a818e2015-10-08 15:13:05 +02001208 *
1209 * CBOR requires text strings to be encoded in UTF-8, but this function does
1210 * not validate either the strings in the stream or the string \a string to be
1211 * matched. Moreover, comparison is done on strict codepoint comparison,
1212 * without any Unicode normalization.
1213 *
1214 * This function may not run in constant time (it will run in O(n) time on the
1215 * number of chunks). It requires constant memory (O(1)).
1216 *
1217 * \sa cbor_value_skip_tag(), cbor_value_copy_text_string()
Thiago Macieirac4a73c62015-05-09 18:14:11 -07001218 */
1219CborError cbor_value_text_string_equals(const CborValue *value, const char *string, bool *result)
1220{
Thiago Macieirad2603492018-07-08 10:57:22 -07001221 size_t len;
Thiago Macieirac4a73c62015-05-09 18:14:11 -07001222 CborValue copy = *value;
1223 CborError err = cbor_value_skip_tag(&copy);
1224 if (err)
1225 return err;
1226 if (!cbor_value_is_text_string(&copy)) {
1227 *result = false;
1228 return CborNoError;
1229 }
1230
Thiago Macieirad2603492018-07-08 10:57:22 -07001231 len = strlen(string);
Thiago Macieirac4a73c62015-05-09 18:14:11 -07001232 return iterate_string_chunks(&copy, CONST_CAST(char *, string), &len, result, NULL, iterate_memcmp);
1233}
1234
1235/**
Thiago Macieira46a818e2015-10-08 15:13:05 +02001236 * \fn bool cbor_value_is_array(const CborValue *value)
Thiago Macieira7b623c22015-05-11 15:52:14 +09001237 *
Thiago Macieira46a818e2015-10-08 15:13:05 +02001238 * Returns true if the iterator \a value is valid and points to a CBOR array.
1239 *
1240 * \sa cbor_value_is_valid(), cbor_value_is_map()
1241 */
1242
1243/**
1244 * \fn CborError cbor_value_get_array_length(const CborValue *value, size_t *length)
1245 *
1246 * Extracts the length of the CBOR array that \a value points to and stores it
1247 * in \a result. If the iterator \a value does not point to a CBOR array, the
1248 * behaviour is undefined, so checking with \ref cbor_value_get_type or \ref
1249 * cbor_value_is_array is recommended.
1250 *
1251 * If the length of this array is not encoded in the CBOR data stream, this
1252 * function will return the recoverable error CborErrorUnknownLength. You may
1253 * also check whether that is the case by using cbor_value_is_length_known().
1254 *
1255 * \note On 32-bit platforms, this function will return error condition of \ref
1256 * CborErrorDataTooLarge if the stream indicates a length that is too big to
1257 * fit in 32-bit.
1258 *
1259 * \sa cbor_value_is_valid(), cbor_value_is_length_known()
1260 */
1261
1262/**
1263 * \fn bool cbor_value_is_map(const CborValue *value)
1264 *
1265 * Returns true if the iterator \a value is valid and points to a CBOR map.
1266 *
1267 * \sa cbor_value_is_valid(), cbor_value_is_array()
1268 */
1269
1270/**
1271 * \fn CborError cbor_value_get_map_length(const CborValue *value, size_t *length)
1272 *
1273 * Extracts the length of the CBOR map that \a value points to and stores it in
1274 * \a result. If the iterator \a value does not point to a CBOR map, the
1275 * behaviour is undefined, so checking with \ref cbor_value_get_type or \ref
1276 * cbor_value_is_map is recommended.
1277 *
1278 * If the length of this map is not encoded in the CBOR data stream, this
1279 * function will return the recoverable error CborErrorUnknownLength. You may
1280 * also check whether that is the case by using cbor_value_is_length_known().
1281 *
1282 * \note On 32-bit platforms, this function will return error condition of \ref
1283 * CborErrorDataTooLarge if the stream indicates a length that is too big to
1284 * fit in 32-bit.
1285 *
1286 * \sa cbor_value_is_valid(), cbor_value_is_length_known()
1287 */
1288
1289/**
1290 * Attempts to find the value in map \a map that corresponds to the text string
1291 * entry \a string. If the iterator \a value does not point to a CBOR map, the
1292 * behaviour is undefined, so checking with \ref cbor_value_get_type or \ref
1293 * cbor_value_is_map is recommended.
1294 *
1295 * If the item is found, it is stored in \a result. If no item is found
1296 * matching the key, then \a result will contain an element of type \ref
1297 * CborInvalidType. Matching is performed using
1298 * cbor_value_text_string_equals(), so tagged strings will also match.
1299 *
1300 * This function has a time complexity of O(n) where n is the number of
1301 * elements in the map to be searched. In addition, this function is has O(n)
1302 * memory requirement based on the number of nested containers (maps or arrays)
1303 * found as elements of this map.
1304 *
1305 * \sa cbor_value_is_valid(), cbor_value_text_string_equals(), cbor_value_advance()
Thiago Macieira7b623c22015-05-11 15:52:14 +09001306 */
1307CborError cbor_value_map_find_value(const CborValue *map, const char *string, CborValue *element)
1308{
Thiago Macieirad2603492018-07-08 10:57:22 -07001309 CborError err;
Thiago Macieira7b623c22015-05-11 15:52:14 +09001310 size_t len = strlen(string);
Thiago Macieirad2603492018-07-08 10:57:22 -07001311 cbor_assert(cbor_value_is_map(map));
1312 err = cbor_value_enter_container(map, element);
Thiago Macieira7b623c22015-05-11 15:52:14 +09001313 if (err)
1314 goto error;
1315
1316 while (!cbor_value_at_end(element)) {
Thiago Macieiradbc01292016-06-06 17:02:25 -07001317 /* find the non-tag so we can compare */
Thiago Macieira7b623c22015-05-11 15:52:14 +09001318 err = cbor_value_skip_tag(element);
1319 if (err)
1320 goto error;
1321 if (cbor_value_is_text_string(element)) {
1322 bool equals;
1323 size_t dummyLen = len;
1324 err = iterate_string_chunks(element, CONST_CAST(char *, string), &dummyLen,
1325 &equals, element, iterate_memcmp);
1326 if (err)
1327 goto error;
1328 if (equals)
1329 return preparse_value(element);
1330 } else {
Thiago Macieiradbc01292016-06-06 17:02:25 -07001331 /* skip this key */
Thiago Macieira7b623c22015-05-11 15:52:14 +09001332 err = cbor_value_advance(element);
1333 if (err)
1334 goto error;
1335 }
1336
Thiago Macieiradbc01292016-06-06 17:02:25 -07001337 /* skip this value */
Thiago Macieira7b623c22015-05-11 15:52:14 +09001338 err = cbor_value_skip_tag(element);
1339 if (err)
1340 goto error;
1341 err = cbor_value_advance(element);
1342 if (err)
1343 goto error;
1344 }
1345
Thiago Macieiradbc01292016-06-06 17:02:25 -07001346 /* not found */
Thiago Macieira7b623c22015-05-11 15:52:14 +09001347 element->type = CborInvalidType;
1348 return CborNoError;
1349
1350error:
1351 element->type = CborInvalidType;
1352 return err;
1353}
1354
1355/**
Thiago Macieira46a818e2015-10-08 15:13:05 +02001356 * \fn bool cbor_value_is_float(const CborValue *value)
1357 *
1358 * Returns true if the iterator \a value is valid and points to a CBOR
1359 * single-precision floating point (32-bit).
1360 *
1361 * \sa cbor_value_is_valid(), cbor_value_is_double(), cbor_value_is_half_float()
1362 */
1363
1364/**
1365 * \fn CborError cbor_value_get_float(const CborValue *value, float *result)
1366 *
1367 * Retrieves the CBOR single-precision floating point (32-bit) value that \a
1368 * value points to and stores it in \a result. If the iterator \a value does
1369 * not point to a single-precision floating point value, the behavior is
1370 * undefined, so checking with \ref cbor_value_get_type or with \ref
1371 * cbor_value_is_float is recommended.
1372 *
1373 * \sa cbor_value_get_type(), cbor_value_is_valid(), cbor_value_is_float(), cbor_value_get_double()
1374 */
1375
1376/**
1377 * \fn bool cbor_value_is_double(const CborValue *value)
1378 *
1379 * Returns true if the iterator \a value is valid and points to a CBOR
1380 * double-precision floating point (64-bit).
1381 *
1382 * \sa cbor_value_is_valid(), cbor_value_is_float(), cbor_value_is_half_float()
1383 */
1384
1385/**
1386 * \fn CborError cbor_value_get_double(const CborValue *value, float *result)
1387 *
1388 * Retrieves the CBOR double-precision floating point (64-bit) value that \a
1389 * value points to and stores it in \a result. If the iterator \a value does
1390 * not point to a double-precision floating point value, the behavior is
1391 * undefined, so checking with \ref cbor_value_get_type or with \ref
1392 * cbor_value_is_double is recommended.
1393 *
1394 * \sa cbor_value_get_type(), cbor_value_is_valid(), cbor_value_is_double(), cbor_value_get_float()
1395 */
1396
1397/**
1398 * \fn bool cbor_value_is_half_float(const CborValue *value)
1399 *
1400 * Returns true if the iterator \a value is valid and points to a CBOR
1401 * single-precision floating point (16-bit).
1402 *
1403 * \sa cbor_value_is_valid(), cbor_value_is_double(), cbor_value_is_float()
1404 */
1405
1406/**
1407 * Retrieves the CBOR half-precision floating point (16-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 half-precision floating point value, the behavior is
1410 * undefined, so checking with \ref cbor_value_get_type or with \ref
1411 * cbor_value_is_half_float is recommended.
1412 *
1413 * Note: since the C language does not have a standard type for half-precision
1414 * floating point, this function takes a \c{void *} as a parameter for the
1415 * storage area, which must be at least 16 bits wide.
1416 *
1417 * \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 -07001418 */
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -07001419CborError cbor_value_get_half_float(const CborValue *value, void *result)
Thiago Macieirac70169f2015-05-06 07:49:44 -07001420{
Thiago Macieirad2603492018-07-08 10:57:22 -07001421 uint16_t v;
Thiago Macieirada8e35e2017-03-05 09:58:01 +01001422 cbor_assert(cbor_value_is_half_float(value));
Thiago Macieirac70169f2015-05-06 07:49:44 -07001423
Thiago Macieiradbc01292016-06-06 17:02:25 -07001424 /* size has been computed already */
Thiago Macieirad2603492018-07-08 10:57:22 -07001425 v = get16(value->ptr + 1);
Thiago Macieirac70169f2015-05-06 07:49:44 -07001426 memcpy(result, &v, sizeof(v));
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -07001427 return CborNoError;
Thiago Macieira54a0e102015-05-05 21:25:06 -07001428}
Thiago Macieira46a818e2015-10-08 15:13:05 +02001429
1430/** @} */