blob: 7379b94bd9b038f4e6caf9a26c13105fc4d1b5f8 [file] [log] [blame]
Thiago Macieira54a0e102015-05-05 21:25:06 -07001/****************************************************************************
2**
Thiago Macieira46a818e2015-10-08 15:13:05 +02003** Copyright (C) 2016 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
Thiago Macieiraed5b57c2015-07-07 16:38:27 -070025#define _BSD_SOURCE 1
Otavio Pontese2d5dd52016-07-08 09:49:38 -030026#define _DEFAULT_SOURCE 1
Thiago Macieira86c81862016-08-04 13:56:48 -070027#ifndef __STDC_LIMIT_MACROS
28# define __STDC_LIMIT_MACROS 1
29#endif
30
Thiago Macieira54a0e102015-05-05 21:25:06 -070031#include "cbor.h"
Thiago Macieiracf3116e2017-03-04 23:36:23 -060032#include "cborinternal_p.h"
Thiago Macieira54a0e102015-05-05 21:25:06 -070033#include "compilersupport_p.h"
34
35#include <assert.h>
Thiago Macieira54a0e102015-05-05 21:25:06 -070036#include <string.h>
37
Thiago Macieira8f3fb782015-06-16 16:27:01 -070038#include "assert_p.h" /* Always include last */
39
Thiago Macieira4a99af92015-05-12 10:41:45 +090040#ifndef CBOR_PARSER_MAX_RECURSIONS
41# define CBOR_PARSER_MAX_RECURSIONS 1024
42#endif
43
Thiago Macieira54a0e102015-05-05 21:25:06 -070044/**
Thiago Macieira46a818e2015-10-08 15:13:05 +020045 * \defgroup CborParsing Parsing CBOR streams
46 * \brief Group of functions used to parse CBOR streams.
Thiago Macieira54a0e102015-05-05 21:25:06 -070047 *
Thiago Macieira46a818e2015-10-08 15:13:05 +020048 * TinyCBOR provides functions for pull-based stream parsing of a CBOR-encoded
49 * payload. The main data type for the parsing is a CborValue, which behaves
50 * like an iterator and can be used to extract the encoded data. It is first
51 * initialized with a call to cbor_parser_init() and is usually used to extract
52 * exactly one item, most often an array or map.
Thiago Macieira54a0e102015-05-05 21:25:06 -070053 *
Thiago Macieira46a818e2015-10-08 15:13:05 +020054 * Nested CborValue objects can be parsed using cbor_value_enter_container().
55 * Each call to cbor_value_enter_container() must be matched by a call to
56 * cbor_value_leave_container(), with the exact same parameters.
Thiago Macieira54a0e102015-05-05 21:25:06 -070057 *
Thiago Macieira46a818e2015-10-08 15:13:05 +020058 * The example below initializes a CborParser object, begins the parsing with a
59 * CborValue and decodes a single integer:
60 *
61 * \code
62 * int extract_int(const uint8_t *buffer, size_t len)
63 * {
64 * CborParser parser;
65 * CborValue value;
66 * int result;
67 * cbor_parser_init(buffer, len, 0, &buffer, &value);
68 * cbor_value_get_int(&value, &result);
69 * return result;
70 * }
71 * \endcode
72 *
73 * The code above does no error checking, which means it assumes the data comes
74 * from a source trusted to send one properly-encoded integer. The following
75 * example does the exact same operation, but includes error parsing and
76 * returns 0 on parsing failure:
77 *
78 * \code
79 * int extract_int(const uint8_t *buffer, size_t len)
80 * {
81 * CborParser parser;
82 * CborValue value;
83 * int result;
84 * if (cbor_parser_init(buffer, len, 0, &buffer, &value) != CborNoError)
85 * return 0;
86 * if (!cbor_value_is_integer(&value) ||
87 * cbor_value_get_int(&value, &result) != CborNoError)
88 * return 0;
89 * return result;
90 * }
91 * \endcode
92 *
93 * Note, in the example above, that one can't distinguish a parsing failure
94 * from an encoded value of zero. Reporting a parsing error is left as an
95 * exercise to the reader.
96 *
97 * The code above does not execute a range-check either: it is possible that
98 * the value decoded from the CBOR stream encodes a number larger than what can
99 * be represented in a variable of type \c{int}. If detecting that case is
100 * important, the code should call cbor_value_get_int_checked() instead.
101 *
102 * <h3 class="groupheader">Memory and parsing constraints</h3>
103 *
104 * TinyCBOR is designed to run with little memory and with minimal overhead.
105 * Except where otherwise noted, the parser functions always run on constant
106 * time (O(1)), do not recurse and never allocate memory (thus, stack usage is
107 * bounded and is O(1)).
108 *
109 * <h3 class="groupheader">Error handling and preconditions</h3>
110 *
111 * All functions operating on a CborValue return a CborError condition, with
112 * CborNoError standing for the normal situation in which no parsing error
113 * occurred. All functions may return parsing errors in case the stream cannot
114 * be decoded properly, be it due to corrupted data or due to reaching the end
115 * of the input buffer.
116 *
117 * Error conditions must not be ignored. All decoder functions have undefined
118 * behavior if called after an error has been reported, and may crash.
119 *
120 * Some functions are also documented to have preconditions, like
121 * cbor_value_get_int() requiring that the input be an integral value.
122 * Violation of preconditions also results in undefined behavior and the
123 * program may crash.
124 */
125
126/**
127 * \addtogroup CborParsing
128 * @{
129 */
130
131/**
132 * \struct CborValue
133 *
134 * This type contains one value parsed from the CBOR stream. Each CborValue
135 * behaves as an iterator in a StAX-style parser.
136 *
137 * \if privatedocs
Thiago Macieira54a0e102015-05-05 21:25:06 -0700138 * Implementation details: the CborValue contains these fields:
139 * \list
140 * \li ptr: pointer to the actual data
141 * \li flags: flags from the decoder
Thiago Macieira2312efd2015-05-06 16:07:48 -0700142 * \li extra: partially decoded integer value (0, 1 or 2 bytes)
Thiago Macieira54a0e102015-05-05 21:25:06 -0700143 * \li remaining: remaining items in this collection after this item or UINT32_MAX if length is unknown
144 * \endlist
Thiago Macieira46a818e2015-10-08 15:13:05 +0200145 * \endif
Thiago Macieira54a0e102015-05-05 21:25:06 -0700146 */
147
Thiago Macieira2c22d712017-03-05 00:17:35 -0600148static inline uint16_t get16(const uint8_t *ptr)
149{
150 uint16_t result;
151 memcpy(&result, ptr, sizeof(result));
152 return cbor_ntohs(result);
153}
154
155static inline uint32_t get32(const uint8_t *ptr)
156{
157 uint32_t result;
158 memcpy(&result, ptr, sizeof(result));
159 return cbor_ntohl(result);
160}
161
162static inline uint64_t get64(const uint8_t *ptr)
163{
164 uint64_t result;
165 memcpy(&result, ptr, sizeof(result));
166 return cbor_ntohll(result);
167}
168
169CborError _cbor_value_extract_number(const uint8_t **ptr, const uint8_t *end, uint64_t *len)
170{
171 uint8_t additional_information = **ptr & SmallValueMask;
172 ++*ptr;
173 if (additional_information < Value8Bit) {
174 *len = additional_information;
175 return CborNoError;
176 }
177 if (unlikely(additional_information > Value64Bit))
178 return CborErrorIllegalNumber;
179
180 size_t bytesNeeded = (size_t)(1 << (additional_information - Value8Bit));
181 if (unlikely(bytesNeeded > (size_t)(end - *ptr))) {
182 return CborErrorUnexpectedEOF;
183 } else if (bytesNeeded == 1) {
184 *len = (uint8_t)(*ptr)[0];
185 } else if (bytesNeeded == 2) {
186 *len = get16(*ptr);
187 } else if (bytesNeeded == 4) {
188 *len = get32(*ptr);
189 } else {
190 *len = get64(*ptr);
191 }
192 *ptr += bytesNeeded;
193 return CborNoError;
194}
195
Thiago Macieiraf5cb94b2015-06-16 16:10:49 -0700196static CborError extract_length(const CborParser *parser, const uint8_t **ptr, size_t *len)
Thiago Macieira54a0e102015-05-05 21:25:06 -0700197{
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700198 uint64_t v;
Thiago Macieira2c22d712017-03-05 00:17:35 -0600199 CborError err = _cbor_value_extract_number(ptr, parser->end, &v);
Mike Colagrosso629d5b72016-02-24 15:12:34 -0700200 if (err) {
201 *len = 0;
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700202 return err;
Mike Colagrosso629d5b72016-02-24 15:12:34 -0700203 }
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700204
alradmsft9ba47912016-10-11 17:56:15 -0700205 *len = (size_t)v;
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700206 if (v != *len)
207 return CborErrorDataTooLarge;
208 return CborNoError;
209}
210
211static bool is_fixed_type(uint8_t type)
212{
213 return type != CborTextStringType && type != CborByteStringType && type != CborArrayType &&
214 type != CborMapType;
215}
216
217static CborError preparse_value(CborValue *it)
218{
219 const CborParser *parser = it->parser;
Thiago Macieira11e913f2015-05-07 13:01:18 -0700220 it->type = CborInvalidType;
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700221
Thiago Macieiradbc01292016-06-06 17:02:25 -0700222 /* are we at the end? */
Thiago Macieira54a0e102015-05-05 21:25:06 -0700223 if (it->ptr == parser->end)
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700224 return CborErrorUnexpectedEOF;
Thiago Macieira54a0e102015-05-05 21:25:06 -0700225
226 uint8_t descriptor = *it->ptr;
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700227 uint8_t type = descriptor & MajorTypeMask;
Thiago Macieira851c4812015-05-08 15:23:20 -0700228 it->type = type;
Thiago Macieira54a0e102015-05-05 21:25:06 -0700229 it->flags = 0;
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700230 it->extra = (descriptor &= SmallValueMask);
231
Thiago Macieira56d99832015-05-07 14:34:27 -0700232 if (descriptor > Value64Bit) {
233 if (unlikely(descriptor != IndefiniteLength))
Thiago Macieira3f76f632015-05-12 10:10:09 +0900234 return type == CborSimpleType ? CborErrorUnknownType : CborErrorIllegalNumber;
Thiago Macieira56d99832015-05-07 14:34:27 -0700235 if (likely(!is_fixed_type(type))) {
Thiago Macieiradbc01292016-06-06 17:02:25 -0700236 /* special case */
Thiago Macieira56d99832015-05-07 14:34:27 -0700237 it->flags |= CborIteratorFlag_UnknownLength;
238 it->type = type;
239 return CborNoError;
240 }
241 return type == CborSimpleType ? CborErrorUnexpectedBreak : CborErrorIllegalNumber;
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700242 }
Thiago Macieira54a0e102015-05-05 21:25:06 -0700243
Thiago Macieirac70169f2015-05-06 07:49:44 -0700244 size_t bytesNeeded = descriptor < Value8Bit ? 0 : (1 << (descriptor - Value8Bit));
Thiago Macieira63abed92015-10-28 17:01:14 -0700245 if (bytesNeeded + 1 > (size_t)(parser->end - it->ptr))
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700246 return CborErrorUnexpectedEOF;
Thiago Macieirac70169f2015-05-06 07:49:44 -0700247
Thiago Macieira851c4812015-05-08 15:23:20 -0700248 uint8_t majortype = type >> MajorTypeShift;
249 if (majortype == NegativeIntegerType) {
Thiago Macieira54a0e102015-05-05 21:25:06 -0700250 it->flags |= CborIteratorFlag_NegativeInteger;
Thiago Macieira851c4812015-05-08 15:23:20 -0700251 it->type = CborIntegerType;
252 } else if (majortype == SimpleTypesType) {
Thiago Macieira54a0e102015-05-05 21:25:06 -0700253 switch (descriptor) {
254 case FalseValue:
255 it->extra = false;
Thiago Macieira851c4812015-05-08 15:23:20 -0700256 it->type = CborBooleanType;
Thiago Macieira991dd922015-05-07 11:57:59 -0700257 break;
258
Thiago Macieira851c4812015-05-08 15:23:20 -0700259 case SinglePrecisionFloat:
260 case DoublePrecisionFloat:
261 it->flags |= CborIteratorFlag_IntegerValueTooLarge;
Thiago Macieiradbc01292016-06-06 17:02:25 -0700262 /* fall through */
Thiago Macieira54a0e102015-05-05 21:25:06 -0700263 case TrueValue:
264 case NullValue:
265 case UndefinedValue:
266 case HalfPrecisionFloat:
Thiago Macieira851c4812015-05-08 15:23:20 -0700267 it->type = *it->ptr;
Thiago Macieira54a0e102015-05-05 21:25:06 -0700268 break;
269
270 case SimpleTypeInNextByte:
Thiago Macieira851c4812015-05-08 15:23:20 -0700271 it->extra = (uint8_t)it->ptr[1];
Thiago Macieira54a0e102015-05-05 21:25:06 -0700272#ifndef CBOR_PARSER_NO_STRICT_CHECKS
Thiago Macieira851c4812015-05-08 15:23:20 -0700273 if (unlikely(it->extra < 32)) {
274 it->type = CborInvalidType;
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700275 return CborErrorIllegalSimpleType;
Thiago Macieira851c4812015-05-08 15:23:20 -0700276 }
Thiago Macieira54a0e102015-05-05 21:25:06 -0700277#endif
Thiago Macieira991dd922015-05-07 11:57:59 -0700278 break;
279
Thiago Macieira54a0e102015-05-05 21:25:06 -0700280 case 28:
281 case 29:
282 case 30:
Thiago Macieira54a0e102015-05-05 21:25:06 -0700283 case Break:
Thiago Macieiradbc01292016-06-06 17:02:25 -0700284 assert(false); /* these conditions can't be reached */
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700285 return CborErrorUnexpectedBreak;
Thiago Macieira54a0e102015-05-05 21:25:06 -0700286 }
Thiago Macieira851c4812015-05-08 15:23:20 -0700287 return CborNoError;
Thiago Macieira54a0e102015-05-05 21:25:06 -0700288 }
289
Thiago Macieiradbc01292016-06-06 17:02:25 -0700290 /* try to decode up to 16 bits */
Thiago Macieira54a0e102015-05-05 21:25:06 -0700291 if (descriptor < Value8Bit)
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700292 return CborNoError;
Thiago Macieira54a0e102015-05-05 21:25:06 -0700293
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700294 if (descriptor == Value8Bit)
295 it->extra = (uint8_t)it->ptr[1];
296 else if (descriptor == Value16Bit)
Thiago Macieira54a0e102015-05-05 21:25:06 -0700297 it->extra = get16(it->ptr + 1);
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700298 else
Thiago Macieiradbc01292016-06-06 17:02:25 -0700299 it->flags |= CborIteratorFlag_IntegerValueTooLarge; /* Value32Bit or Value64Bit */
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700300 return CborNoError;
301}
Thiago Macieira54a0e102015-05-05 21:25:06 -0700302
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700303static CborError preparse_next_value(CborValue *it)
304{
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700305 if (it->remaining != UINT32_MAX) {
Thiago Macieiradbc01292016-06-06 17:02:25 -0700306 /* don't decrement the item count if the current item is tag: they don't count */
Thiago Macieira11e913f2015-05-07 13:01:18 -0700307 if (it->type != CborTagType && !--it->remaining) {
308 it->type = CborInvalidType;
Thiago Macieira56d99832015-05-07 14:34:27 -0700309 return CborNoError;
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700310 }
Thiago Macieira5752ce52015-06-16 12:10:03 -0700311 } else if (it->remaining == UINT32_MAX && it->ptr != it->parser->end && *it->ptr == (uint8_t)BreakByte) {
Thiago Macieiradbc01292016-06-06 17:02:25 -0700312 /* end of map or array */
Thiago Macieira56d99832015-05-07 14:34:27 -0700313 ++it->ptr;
314 it->type = CborInvalidType;
315 it->remaining = 0;
316 return CborNoError;
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700317 }
Thiago Macieira56d99832015-05-07 14:34:27 -0700318
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700319 return preparse_value(it);
320}
321
322static CborError advance_internal(CborValue *it)
323{
324 uint64_t length;
Thiago Macieira2c22d712017-03-05 00:17:35 -0600325 CborError err = _cbor_value_extract_number(&it->ptr, it->parser->end, &length);
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700326 assert(err == CborNoError);
327
Thiago Macieira56d99832015-05-07 14:34:27 -0700328 if (it->type == CborByteStringType || it->type == CborTextStringType) {
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700329 assert(length == (size_t)length);
Thiago Macieira56d99832015-05-07 14:34:27 -0700330 assert((it->flags & CborIteratorFlag_UnknownLength) == 0);
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700331 it->ptr += length;
332 }
333
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700334 return preparse_next_value(it);
Thiago Macieira54a0e102015-05-05 21:25:06 -0700335}
336
Thiago Macieira2312efd2015-05-06 16:07:48 -0700337/** \internal
338 *
339 * Decodes the CBOR integer value when it is larger than the 16 bits available
340 * in value->extra. This function requires that value->flags have the
341 * CborIteratorFlag_IntegerValueTooLarge flag set.
342 *
343 * This function is also used to extract single- and double-precision floating
344 * point values (SinglePrecisionFloat == Value32Bit and DoublePrecisionFloat ==
345 * Value64Bit).
346 */
Thiago Macieira54a0e102015-05-05 21:25:06 -0700347uint64_t _cbor_value_decode_int64_internal(const CborValue *value)
348{
Thiago Macieira2312efd2015-05-06 16:07:48 -0700349 assert(value->flags & CborIteratorFlag_IntegerValueTooLarge ||
350 value->type == CborFloatType || value->type == CborDoubleType);
Thiago Macieira851c4812015-05-08 15:23:20 -0700351
Thiago Macieiradbc01292016-06-06 17:02:25 -0700352 /* since the additional information can only be Value32Bit or Value64Bit,
353 * we just need to test for the one bit those two options differ */
Thiago Macieira851c4812015-05-08 15:23:20 -0700354 assert((*value->ptr & SmallValueMask) == Value32Bit || (*value->ptr & SmallValueMask) == Value64Bit);
355 if ((*value->ptr & 1) == (Value32Bit & 1))
Thiago Macieira54a0e102015-05-05 21:25:06 -0700356 return get32(value->ptr + 1);
357
358 assert((*value->ptr & SmallValueMask) == Value64Bit);
359 return get64(value->ptr + 1);
360}
361
362/**
363 * Initializes the CBOR parser for parsing \a size bytes beginning at \a
364 * buffer. Parsing will use flags set in \a flags. The iterator to the first
365 * element is returned in \a it.
Thiago Macieira2312efd2015-05-06 16:07:48 -0700366 *
367 * The \a parser structure needs to remain valid throughout the decoding
368 * process. It is not thread-safe to share one CborParser among multiple
369 * threads iterating at the same time, but the object can be copied so multiple
370 * threads can iterate.
Thiago Macieira54a0e102015-05-05 21:25:06 -0700371 */
Thiago Macieira5752ce52015-06-16 12:10:03 -0700372CborError cbor_parser_init(const uint8_t *buffer, size_t size, int flags, CborParser *parser, CborValue *it)
Thiago Macieira54a0e102015-05-05 21:25:06 -0700373{
374 memset(parser, 0, sizeof(*parser));
375 parser->end = buffer + size;
Thiago Macieira54a0e102015-05-05 21:25:06 -0700376 parser->flags = flags;
377 it->parser = parser;
378 it->ptr = buffer;
Thiago Macieiradbc01292016-06-06 17:02:25 -0700379 it->remaining = 1; /* there's one type altogether, usually an array or map */
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700380 return preparse_value(it);
Thiago Macieira2312efd2015-05-06 16:07:48 -0700381}
382
383/**
Thiago Macieira46a818e2015-10-08 15:13:05 +0200384 * \fn bool cbor_value_at_end(const CborValue *it)
385 *
386 * Returns true if \a it has reached the end of the iteration, usually when
Thiago Macieira740e29d2016-07-07 13:32:47 -0700387 * advancing after the last item in an array or map.
Thiago Macieira46a818e2015-10-08 15:13:05 +0200388 *
Thiago Macieira740e29d2016-07-07 13:32:47 -0700389 * In the case of the outermost CborValue object, this function returns true
390 * after decoding a single element. A pointer to the first byte of the
391 * remaining data (if any) can be obtained with cbor_value_get_next_byte().
392 *
393 * \sa cbor_value_advance(), cbor_value_is_valid(), cbor_value_get_next_byte()
394 */
395
396/**
397 * \fn const uint8_t *cbor_value_get_next_byte(const CborValue *it)
398 *
399 * Returns a pointer to the next byte that would be decoded if this CborValue
400 * object were advanced.
401 *
402 * This function is useful if cbor_value_at_end() returns true for the
403 * outermost CborValue: the pointer returned is the first byte of the data
404 * remaining in the buffer, if any. Code can decide whether to begin decoding a
405 * new CBOR data stream from this point, or parse some other data appended to
406 * the same buffer.
407 *
408 * This function may be used even after a parsing error. If that occurred,
409 * then this function returns a pointer to where the parsing error occurred.
410 * Note that the error recovery is not precise and the pointer may not indicate
411 * the exact byte containing bad data.
412 *
413 * \sa cbor_value_at_end()
Thiago Macieira46a818e2015-10-08 15:13:05 +0200414 */
415
416/**
417 * \fn bool cbor_value_is_valid(const CborValue *it)
418 *
419 * Returns true if the iterator \a it contains a valid value. Invalid iterators
420 * happen when iteration reaches the end of a container (see \ref
421 * cbor_value_at_end()) or when a search function resulted in no matches.
422 *
423 * \sa cbor_value_advance(), cbor_valie_at_end(), cbor_value_get_type()
424 */
425
426/**
Thiago Macieira2312efd2015-05-06 16:07:48 -0700427 * Advances the CBOR value \a it by one fixed-size position. Fixed-size types
428 * are: integers, tags, simple types (including boolean, null and undefined
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700429 * values) and floating point types.
Thiago Macieira2312efd2015-05-06 16:07:48 -0700430 *
Thiago Macieira46a818e2015-10-08 15:13:05 +0200431 * If the type is not of fixed size, this function has undefined behavior. Code
432 * must be sure that the current type is one of the fixed-size types before
433 * calling this function. This function is provided because it can guarantee
434 * that runs in constant time (O(1)).
435 *
436 * If the caller is not able to determine whether the type is fixed or not, code
437 * can use the cbor_value_advance() function instead.
438 *
439 * \sa cbor_value_at_end(), cbor_value_advance(), cbor_value_enter_container(), cbor_value_leave_container()
Thiago Macieira2312efd2015-05-06 16:07:48 -0700440 */
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700441CborError cbor_value_advance_fixed(CborValue *it)
Thiago Macieira54a0e102015-05-05 21:25:06 -0700442{
Thiago Macieira2312efd2015-05-06 16:07:48 -0700443 assert(it->type != CborInvalidType);
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700444 assert(is_fixed_type(it->type));
445 if (!it->remaining)
446 return CborErrorAdvancePastEOF;
447 return advance_internal(it);
Thiago Macieira54a0e102015-05-05 21:25:06 -0700448}
449
Thiago Macieira4a99af92015-05-12 10:41:45 +0900450static CborError advance_recursive(CborValue *it, int nestingLevel)
451{
452 if (is_fixed_type(it->type))
453 return advance_internal(it);
454
455 if (!cbor_value_is_container(it)) {
456 size_t len = SIZE_MAX;
Thiago Macieiraff130bc2015-06-19 15:15:33 -0700457 return _cbor_value_copy_string(it, NULL, &len, it);
Thiago Macieira4a99af92015-05-12 10:41:45 +0900458 }
459
Thiago Macieiradbc01292016-06-06 17:02:25 -0700460 /* map or array */
Thiago Macieira4a99af92015-05-12 10:41:45 +0900461 if (nestingLevel == CBOR_PARSER_MAX_RECURSIONS)
462 return CborErrorNestingTooDeep;
463
464 CborError err;
465 CborValue recursed;
466 err = cbor_value_enter_container(it, &recursed);
467 if (err)
468 return err;
469 while (!cbor_value_at_end(&recursed)) {
470 err = advance_recursive(&recursed, nestingLevel + 1);
471 if (err)
472 return err;
473 }
474 return cbor_value_leave_container(it, &recursed);
475}
476
477
Thiago Macieira2312efd2015-05-06 16:07:48 -0700478/**
479 * Advances the CBOR value \a it by one element, skipping over containers.
480 * Unlike cbor_value_advance_fixed(), this function can be called on a CBOR
481 * value of any type. However, if the type is a container (map or array) or a
482 * string with a chunked payload, this function will not run in constant time
483 * and will recurse into itself (it will run on O(n) time for the number of
484 * elements or chunks and will use O(n) memory for the number of nested
485 * containers).
486 *
Thiago Macieira46a818e2015-10-08 15:13:05 +0200487 * \sa cbor_value_at_end(), cbor_value_advance_fixed(), 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(CborValue *it)
Thiago Macieira2312efd2015-05-06 16:07:48 -0700490{
491 assert(it->type != CborInvalidType);
492 if (!it->remaining)
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700493 return CborErrorAdvancePastEOF;
Thiago Macieira4a99af92015-05-12 10:41:45 +0900494 return advance_recursive(it, 0);
Thiago Macieira2312efd2015-05-06 16:07:48 -0700495}
496
497/**
Thiago Macieira46a818e2015-10-08 15:13:05 +0200498 * \fn bool cbor_value_is_tag(const CborValue *value)
499 *
500 * Returns true if the iterator \a value is valid and points to a CBOR tag.
501 *
502 * \sa cbor_value_get_tag(), cbor_value_skip_tag()
503 */
504
505/**
506 * \fn CborError cbor_value_get_tag(const CborValue *value, CborTag *result)
507 *
508 * Retrieves the CBOR tag value that \a value points to and stores it in \a
509 * result. If the iterator \a value does not point to a CBOR tag value, the
510 * behavior is undefined, so checking with \ref cbor_value_get_type or with
511 * \ref cbor_value_is_tag is recommended.
512 *
513 * \sa cbor_value_get_type(), cbor_value_is_valid(), cbor_value_is_tag()
514 */
515
516/**
Thiago Macieirac4a73c62015-05-09 18:14:11 -0700517 * Advances the CBOR value \a it until it no longer points to a tag. If \a it is
518 * already not pointing to a tag, then this function returns it unchanged.
519 *
Thiago Macieira46a818e2015-10-08 15:13:05 +0200520 * This function does not run in constant time: it will run on O(n) for n being
521 * the number of tags. It does use constant memory (O(1) memory requirements).
522 *
Thiago Macieirac4a73c62015-05-09 18:14:11 -0700523 * \sa cbor_value_advance_fixed(), cbor_value_advance()
524 */
525CborError cbor_value_skip_tag(CborValue *it)
526{
527 while (cbor_value_is_tag(it)) {
528 CborError err = cbor_value_advance_fixed(it);
529 if (err)
530 return err;
531 }
532 return CborNoError;
533}
534
Thiago Macieirac4a73c62015-05-09 18:14:11 -0700535/**
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700536 * \fn bool cbor_value_is_container(const CborValue *it)
537 *
Thiago Macieira2312efd2015-05-06 16:07:48 -0700538 * Returns true if the \a it value is a container and requires recursion in
539 * order to decode (maps and arrays), false otherwise.
540 */
Thiago Macieira54a0e102015-05-05 21:25:06 -0700541
Thiago Macieira2312efd2015-05-06 16:07:48 -0700542/**
543 * Creates a CborValue iterator pointing to the first element of the container
544 * represented by \a it and saves it in \a recursed. The \a it container object
545 * needs to be kept and passed again to cbor_value_leave_container() in order
546 * to continue iterating past this container.
547 *
Thiago Macieira46a818e2015-10-08 15:13:05 +0200548 * The \a it CborValue iterator must point to a container.
549 *
Thiago Macieira2312efd2015-05-06 16:07:48 -0700550 * \sa cbor_value_is_container(), cbor_value_leave_container(), cbor_value_advance()
551 */
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700552CborError cbor_value_enter_container(const CborValue *it, CborValue *recursed)
Thiago Macieira54a0e102015-05-05 21:25:06 -0700553{
Thiago Macieira56d99832015-05-07 14:34:27 -0700554 CborError err;
Thiago Macieira2312efd2015-05-06 16:07:48 -0700555 assert(cbor_value_is_container(it));
Thiago Macieira54a0e102015-05-05 21:25:06 -0700556 *recursed = *it;
Thiago Macieira56d99832015-05-07 14:34:27 -0700557
Thiago Macieira54a0e102015-05-05 21:25:06 -0700558 if (it->flags & CborIteratorFlag_UnknownLength) {
559 recursed->remaining = UINT32_MAX;
Thiago Macieira56d99832015-05-07 14:34:27 -0700560 ++recursed->ptr;
561 err = preparse_value(recursed);
562 if (err != CborErrorUnexpectedBreak)
563 return err;
Thiago Macieiradbc01292016-06-06 17:02:25 -0700564 /* actually, break was expected here
565 * it's just an empty container */
Thiago Macieira56d99832015-05-07 14:34:27 -0700566 ++recursed->ptr;
Thiago Macieira54a0e102015-05-05 21:25:06 -0700567 } else {
Thiago Macieira56d99832015-05-07 14:34:27 -0700568 uint64_t len;
Thiago Macieira2c22d712017-03-05 00:17:35 -0600569 err = _cbor_value_extract_number(&recursed->ptr, recursed->parser->end, &len);
Thiago Macieira56d99832015-05-07 14:34:27 -0700570 assert(err == CborNoError);
Thiago Macieira56d99832015-05-07 14:34:27 -0700571
Thiago Macieirae12dfd02016-06-07 16:29:25 -0700572 recursed->remaining = (uint32_t)len;
Thiago Macieira3f76f632015-05-12 10:10:09 +0900573 if (recursed->remaining != len || len == UINT32_MAX) {
Thiago Macieiradbc01292016-06-06 17:02:25 -0700574 /* back track the pointer to indicate where the error occurred */
Thiago Macieira3f76f632015-05-12 10:10:09 +0900575 recursed->ptr = it->ptr;
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700576 return CborErrorDataTooLarge;
Thiago Macieira3f76f632015-05-12 10:10:09 +0900577 }
Thiago Macieirace16f052015-05-07 23:14:25 -0700578 if (recursed->type == CborMapType) {
Thiago Macieiradbc01292016-06-06 17:02:25 -0700579 /* maps have keys and values, so we need to multiply by 2 */
Thiago Macieira3f76f632015-05-12 10:10:09 +0900580 if (recursed->remaining > UINT32_MAX / 2) {
Thiago Macieiradbc01292016-06-06 17:02:25 -0700581 /* back track the pointer to indicate where the error occurred */
Thiago Macieira3f76f632015-05-12 10:10:09 +0900582 recursed->ptr = it->ptr;
Thiago Macieirace16f052015-05-07 23:14:25 -0700583 return CborErrorDataTooLarge;
Thiago Macieira3f76f632015-05-12 10:10:09 +0900584 }
Thiago Macieirace16f052015-05-07 23:14:25 -0700585 recursed->remaining *= 2;
586 }
Thiago Macieira56d99832015-05-07 14:34:27 -0700587 if (len != 0)
588 return preparse_value(recursed);
Thiago Macieira54a0e102015-05-05 21:25:06 -0700589 }
Thiago Macieira56d99832015-05-07 14:34:27 -0700590
Thiago Macieiradbc01292016-06-06 17:02:25 -0700591 /* the case of the empty container */
Thiago Macieira56d99832015-05-07 14:34:27 -0700592 recursed->type = CborInvalidType;
593 recursed->remaining = 0;
594 return CborNoError;
Thiago Macieirac70169f2015-05-06 07:49:44 -0700595}
596
Thiago Macieira2312efd2015-05-06 16:07:48 -0700597/**
598 * Updates \a it to point to the next element after the container. The \a
Thiago Macieira56d99832015-05-07 14:34:27 -0700599 * recursed object needs to point to the element obtained either by advancing
600 * the last element of the container (via cbor_value_advance(),
601 * cbor_value_advance_fixed(), a nested cbor_value_leave_container(), or the \c
602 * next pointer from cbor_value_copy_string() or cbor_value_dup_string()).
Thiago Macieira2312efd2015-05-06 16:07:48 -0700603 *
Thiago Macieira46a818e2015-10-08 15:13:05 +0200604 * The \a it and \a recursed parameters must be the exact same as passed to
605 * cbor_value_enter_container().
606 *
Thiago Macieira2312efd2015-05-06 16:07:48 -0700607 * \sa cbor_value_enter_container(), cbor_value_at_end()
608 */
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700609CborError cbor_value_leave_container(CborValue *it, const CborValue *recursed)
Thiago Macieirac70169f2015-05-06 07:49:44 -0700610{
Thiago Macieira2312efd2015-05-06 16:07:48 -0700611 assert(cbor_value_is_container(it));
Thiago Macieira56d99832015-05-07 14:34:27 -0700612 assert(recursed->type == CborInvalidType);
Thiago Macieirac70169f2015-05-06 07:49:44 -0700613 it->ptr = recursed->ptr;
Thiago Macieira56d99832015-05-07 14:34:27 -0700614 return preparse_next_value(it);
Thiago Macieirac70169f2015-05-06 07:49:44 -0700615}
616
Thiago Macieira46a818e2015-10-08 15:13:05 +0200617
Thiago Macieira2312efd2015-05-06 16:07:48 -0700618/**
Thiago Macieira46a818e2015-10-08 15:13:05 +0200619 * \fn CborType cbor_value_get_type(const CborValue *value)
620 *
621 * Returns the type of the CBOR value that the iterator \a value points to. If
622 * \a value does not point to a valid value, this function returns \ref
623 * CborInvalidType.
624 *
625 * TinyCBOR also provides functions to test directly if a given CborValue object
626 * is of a given type, like cbor_value_is_text_string() and cbor_value_is_null().
627 *
628 * \sa cbor_value_is_valid()
629 */
630
631/**
632 * \fn bool cbor_value_is_null(const CborValue *value)
633 *
634 * Returns true if the iterator \a value is valid and points to a CBOR null type.
635 *
636 * \sa cbor_value_is_valid(), cbor_value_is_undefined()
637 */
638
639/**
640 * \fn bool cbor_value_is_undefined(const CborValue *value)
641 *
642 * Returns true if the iterator \a value is valid and points to a CBOR undefined type.
643 *
644 * \sa cbor_value_is_valid(), cbor_value_is_null()
645 */
646
647/**
648 * \fn bool cbor_value_is_boolean(const CborValue *value)
649 *
650 * Returns true if the iterator \a value is valid and points to a CBOR boolean
651 * type (true or false).
652 *
653 * \sa cbor_value_is_valid(), cbor_value_get_boolean()
654 */
655
656/**
657 * \fn CborError cbor_value_get_boolean(const CborValue *value, bool *result)
658 *
659 * Retrieves the boolean value that \a value points to and stores it in \a
660 * result. If the iterator \a value does not point to a boolean value, the
661 * behavior is undefined, so checking with \ref cbor_value_get_type or with
662 * \ref cbor_value_is_boolean is recommended.
663 *
664 * \sa cbor_value_get_type(), cbor_value_is_valid(), cbor_value_is_boolean()
665 */
666
667/**
668 * \fn bool cbor_value_is_simple_type(const CborValue *value)
669 *
670 * Returns true if the iterator \a value is valid and points to a CBOR Simple Type
671 * type (other than true, false, null and undefined).
672 *
673 * \sa cbor_value_is_valid(), cbor_value_get_simple_type()
674 */
675
676/**
677 * \fn CborError cbor_value_get_simple_type(const CborValue *value, uint8_t *result)
678 *
679 * Retrieves the CBOR Simple Type value that \a value points to and stores it
680 * in \a result. If the iterator \a value does not point to a simple_type
681 * value, the behavior is undefined, so checking with \ref cbor_value_get_type
682 * or with \ref cbor_value_is_simple_type is recommended.
683 *
684 * \sa cbor_value_get_type(), cbor_value_is_valid(), cbor_value_is_simple_type()
685 */
686
687/**
688 * \fn bool cbor_value_is_integer(const CborValue *value)
689 *
690 * Returns true if the iterator \a value is valid and points to a CBOR integer
691 * type.
692 *
693 * \sa cbor_value_is_valid(), cbor_value_get_int, cbor_value_get_int64, cbor_value_get_uint64, cbor_value_get_raw_integer
694 */
695
696/**
697 * \fn bool cbor_value_is_unsigned_integer(const CborValue *value)
698 *
699 * Returns true if the iterator \a value is valid and points to a CBOR unsigned
700 * integer type (positive values or zero).
701 *
702 * \sa cbor_value_is_valid(), cbor_value_get_uint64()
703 */
704
705/**
706 * \fn bool cbor_value_is_negative_integer(const CborValue *value)
707 *
708 * Returns true if the iterator \a value is valid and points to a CBOR negative
709 * integer type.
710 *
711 * \sa cbor_value_is_valid(), cbor_value_get_int, cbor_value_get_int64, cbor_value_get_raw_integer
712 */
713
714/**
715 * \fn CborError cbor_value_get_int(const CborValue *value, int *result)
716 *
717 * Retrieves the CBOR integer value that \a value points to and stores it in \a
718 * result. If the iterator \a value does not point to an integer value, the
719 * behavior is undefined, so checking with \ref cbor_value_get_type or with
720 * \ref cbor_value_is_integer is recommended.
721 *
722 * Note that this function does not do range-checking: integral values that do
723 * not fit in a variable of type \c{int} are silently truncated to fit. Use
724 * cbor_value_get_int_checked() that is not acceptable.
725 *
726 * \sa cbor_value_get_type(), cbor_value_is_valid(), cbor_value_is_integer()
727 */
728
729/**
730 * \fn CborError cbor_value_get_int64(const CborValue *value, int64_t *result)
731 *
732 * Retrieves the CBOR integer value that \a value points to and stores it in \a
733 * result. If the iterator \a value does not point to an integer value, the
734 * behavior is undefined, so checking with \ref cbor_value_get_type or with
735 * \ref cbor_value_is_integer is recommended.
736 *
737 * Note that this function does not do range-checking: integral values that do
738 * not fit in a variable of type \c{int64_t} are silently truncated to fit. Use
739 * cbor_value_get_int64_checked() that is not acceptable.
740 *
741 * \sa cbor_value_get_type(), cbor_value_is_valid(), cbor_value_is_integer()
742 */
743
744/**
745 * \fn CborError cbor_value_get_uint64(const CborValue *value, uint64_t *result)
746 *
747 * Retrieves the CBOR integer value that \a value points to and stores it in \a
748 * result. If the iterator \a value does not point to an unsigned integer
749 * value, the behavior is undefined, so checking with \ref cbor_value_get_type
750 * or with \ref cbor_value_is_unsigned_integer is recommended.
751 *
752 * \sa cbor_value_get_type(), cbor_value_is_valid(), cbor_value_is_unsigned_integer()
753 */
754
755/**
756 * \fn CborError cbor_value_get_raw_integer(const CborValue *value, uint64_t *result)
757 *
758 * Retrieves the CBOR integer value that \a value points to and stores it in \a
759 * result. If the iterator \a value does not point to an integer value, the
760 * behavior is undefined, so checking with \ref cbor_value_get_type or with
761 * \ref cbor_value_is_integer is recommended.
762 *
763 * This function is provided because CBOR negative integers can assume values
764 * that cannot be represented with normal 64-bit integer variables.
765 *
766 * If the integer is unsigned (that is, if cbor_value_is_unsigned_integer()
767 * returns true), then \a result will contain the actual value. If the integer
768 * is negative, then \a result will contain the absolute value of that integer,
769 * minus one. That is, \c {actual = -result - 1}. On architectures using two's
770 * complement for representation of negative integers, it is equivalent to say
771 * that \a result will contain the bitwise negation of the actual value.
772 *
773 * \sa cbor_value_get_type(), cbor_value_is_valid(), cbor_value_is_integer()
774 */
775
776/**
Thiago Macieira0f02e792016-07-07 19:55:08 -0700777 * Retrieves the CBOR integer value that \a value points to and stores it in \a
778 * result. If the iterator \a value does not point to an integer value, the
779 * behavior is undefined, so checking with \ref cbor_value_get_type or with
780 * \ref cbor_value_is_integer is recommended.
781 *
782 * Unlike cbor_value_get_int64(), this function performs a check to see if the
783 * stored integer fits in \a result without data loss. If the number is outside
784 * the valid range for the data type, this function returns the recoverable
785 * error CborErrorDataTooLarge. In that case, use either
786 * cbor_value_get_uint64() (if the number is positive) or
787 * cbor_value_get_raw_integer().
788 *
789 * \sa cbor_value_get_type(), cbor_value_is_valid(), cbor_value_is_integer(), cbor_value_get_int64()
790 */
791CborError cbor_value_get_int64_checked(const CborValue *value, int64_t *result)
792{
793 assert(cbor_value_is_integer(value));
794 uint64_t v = _cbor_value_extract_int64_helper(value);
795
796 /* Check before converting, as the standard says (C11 6.3.1.3 paragraph 3):
797 * "[if] the new type is signed and the value cannot be represented in it; either the
798 * result is implementation-defined or an implementation-defined signal is raised."
799 *
800 * The range for int64_t is -2^63 to 2^63-1 (int64_t is required to be
801 * two's complement, C11 7.20.1.1 paragraph 3), which in CBOR is
802 * represented the same way, differing only on the "sign bit" (the major
803 * type).
804 */
805
806 if (unlikely(v > (uint64_t)INT64_MAX))
807 return CborErrorDataTooLarge;
808
809 *result = v;
810 if (value->flags & CborIteratorFlag_NegativeInteger)
811 *result = -*result - 1;
812 return CborNoError;
813}
814
815/**
816 * Retrieves the CBOR integer value that \a value points to and stores it in \a
817 * result. If the iterator \a value does not point to an integer value, the
818 * behavior is undefined, so checking with \ref cbor_value_get_type or with
819 * \ref cbor_value_is_integer is recommended.
820 *
821 * Unlike cbor_value_get_int(), this function performs a check to see if the
822 * stored integer fits in \a result without data loss. If the number is outside
823 * the valid range for the data type, this function returns the recoverable
824 * error CborErrorDataTooLarge. In that case, use one of the other integer
825 * functions to obtain the value.
826 *
827 * \sa cbor_value_get_type(), cbor_value_is_valid(), cbor_value_is_integer(), cbor_value_get_int64(),
828 * cbor_value_get_uint64(), cbor_value_get_int64_checked(), cbor_value_get_raw_integer()
829 */
830CborError cbor_value_get_int_checked(const CborValue *value, int *result)
831{
832 assert(cbor_value_is_integer(value));
833 uint64_t v = _cbor_value_extract_int64_helper(value);
834
835 /* Check before converting, as the standard says (C11 6.3.1.3 paragraph 3):
836 * "[if] the new type is signed and the value cannot be represented in it; either the
837 * result is implementation-defined or an implementation-defined signal is raised."
838 *
839 * But we can convert from signed to unsigned without fault (paragraph 2).
840 *
841 * The range for int is implementation-defined and int is not guaranteed use
842 * two's complement representation (int32_t is).
843 */
844
845 if (value->flags & CborIteratorFlag_NegativeInteger) {
846 if (unlikely(v > (unsigned) -(INT_MIN + 1)))
847 return CborErrorDataTooLarge;
848
alradmsft9ba47912016-10-11 17:56:15 -0700849 *result = (int)v;
Thiago Macieira0f02e792016-07-07 19:55:08 -0700850 *result = -*result - 1;
851 } else {
852 if (unlikely(v > (uint64_t)INT_MAX))
853 return CborErrorDataTooLarge;
854
alradmsft9ba47912016-10-11 17:56:15 -0700855 *result = (int)v;
Thiago Macieira0f02e792016-07-07 19:55:08 -0700856 }
857 return CborNoError;
858
859}
860
861/**
Thiago Macieira46a818e2015-10-08 15:13:05 +0200862 * \fn bool cbor_value_is_length_known(const CborValue *value)
863 *
864 * Returns true if the length of this type is known without calculation. That
865 * is, if the length of this CBOR string, map or array is encoded in the data
866 * stream, this function returns true. If the length is not encoded, it returns
867 * false.
868 *
869 * If the length is known, code can call cbor_value_get_string_length(),
870 * cbor_value_get_array_length() or cbor_value_get_map_length() to obtain the
871 * length. If the length is not known but is necessary, code can use the
872 * cbor_value_calculate_string_length() function (no equivalent function is
873 * provided for maps and arrays).
874 */
875
876/**
877 * \fn bool cbor_value_is_text_string(const CborValue *value)
878 *
879 * Returns true if the iterator \a value is valid and points to a CBOR text
880 * string. CBOR text strings are UTF-8 encoded and usually contain
881 * human-readable text.
882 *
883 * \sa cbor_value_is_valid(), cbor_value_get_string_length(), cbor_value_calculate_string_length(),
884 * cbor_value_copy_text_string(), cbor_value_dup_text_string()
885 */
886
887/**
888 * \fn bool cbor_value_is_byte_string(const CborValue *value)
889 *
890 * Returns true if the iterator \a value is valid and points to a CBOR text
891 * string. CBOR byte strings are binary data with no specified encoding or
892 * format.
893 *
894 * \sa cbor_value_is_valid(), cbor_value_get_string_length(), cbor_value_calculate_string_length(),
895 * cbor_value_copy_byte_string(), cbor_value_dup_byte_string()
896 */
897
898/**
899 * \fn CborError cbor_value_get_string_length(const CborValue *value, size_t *length)
900 *
901 * Extracts the length of the byte or text string that \a value points to and
902 * stores it in \a result. If the iterator \a value does not point to a text
903 * string or a byte string, the behaviour is undefined, so checking with \ref
904 * cbor_value_get_type, with \ref cbor_value_is_text_string or \ref
905 * cbor_value_is_byte_string is recommended.
906 *
907 * If the length of this string is not encoded in the CBOR data stream, this
908 * function will return the recoverable error CborErrorUnknownLength. You may
909 * also check whether that is the case by using cbor_value_is_length_known().
910 *
911 * If the length of the string is required but the length was not encoded, use
912 * cbor_value_calculate_string_length(), but note that that function does not
913 * run in constant time.
914 *
915 * \note On 32-bit platforms, this function will return error condition of \ref
916 * CborErrorDataTooLarge if the stream indicates a length that is too big to
917 * fit in 32-bit.
918 *
919 * \sa cbor_value_is_valid(), cbor_value_is_length_known(), cbor_value_calculate_string_length()
920 */
921
922/**
923 * Calculates the length of the byte or text string that \a value points to and
924 * stores it in \a len. If the iterator \a value does not point to a text
925 * string or a byte string, the behaviour is undefined, so checking with \ref
926 * cbor_value_get_type, with \ref cbor_value_is_text_string or \ref
927 * cbor_value_is_byte_string is recommended.
928 *
929 * This function is different from cbor_value_get_string_length() in that it
930 * calculates the length even for strings sent in chunks. For that reason, this
931 * function may not run in constant time (it will run in O(n) time on the
932 * number of chunks). It does use constant memory (O(1)).
Thiago Macieira2312efd2015-05-06 16:07:48 -0700933 *
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700934 * \note On 32-bit platforms, this function will return error condition of \ref
935 * CborErrorDataTooLarge if the stream indicates a length that is too big to
936 * fit in 32-bit.
Thiago Macieira2312efd2015-05-06 16:07:48 -0700937 *
938 * \sa cbor_value_get_string_length(), cbor_value_copy_string(), cbor_value_is_length_known()
939 */
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700940CborError cbor_value_calculate_string_length(const CborValue *value, size_t *len)
Thiago Macieira2312efd2015-05-06 16:07:48 -0700941{
Thiago Macieira9ae05812015-05-11 15:09:09 +0900942 *len = SIZE_MAX;
Thiago Macieiraff130bc2015-06-19 15:15:33 -0700943 return _cbor_value_copy_string(value, NULL, len, NULL);
Thiago Macieirac70169f2015-05-06 07:49:44 -0700944}
945
Thiago Macieiradbc01292016-06-06 17:02:25 -0700946/* We return uintptr_t so that we can pass memcpy directly as the iteration
947 * function. The choice is to optimize for memcpy, which is used in the base
948 * parser API (cbor_value_copy_string), while memcmp is used in convenience API
949 * only. */
Thiago Macieira5752ce52015-06-16 12:10:03 -0700950typedef uintptr_t (*IterateFunction)(char *, const uint8_t *, size_t);
Thiago Macieira9ae05812015-05-11 15:09:09 +0900951
Thiago Macieira5752ce52015-06-16 12:10:03 -0700952static uintptr_t iterate_noop(char *dest, const uint8_t *src, size_t len)
Thiago Macieira9ae05812015-05-11 15:09:09 +0900953{
954 (void)dest;
955 (void)src;
956 (void)len;
957 return true;
958}
959
Thiago Macieira5752ce52015-06-16 12:10:03 -0700960static uintptr_t iterate_memcmp(char *s1, const uint8_t *s2, size_t len)
Thiago Macieirac4a73c62015-05-09 18:14:11 -0700961{
Thiago Macieira5752ce52015-06-16 12:10:03 -0700962 return memcmp(s1, (const char *)s2, len) == 0;
Thiago Macieirac4a73c62015-05-09 18:14:11 -0700963}
964
Thiago Macieira9ae05812015-05-11 15:09:09 +0900965static CborError iterate_string_chunks(const CborValue *value, char *buffer, size_t *buflen,
966 bool *result, CborValue *next, IterateFunction func)
967{
968 assert(cbor_value_is_byte_string(value) || cbor_value_is_text_string(value));
969
970 size_t total;
971 CborError err;
Thiago Macieira5752ce52015-06-16 12:10:03 -0700972 const uint8_t *ptr = value->ptr;
Thiago Macieira9ae05812015-05-11 15:09:09 +0900973 if (cbor_value_is_length_known(value)) {
Thiago Macieiradbc01292016-06-06 17:02:25 -0700974 /* easy case: fixed length */
Thiago Macieira9ae05812015-05-11 15:09:09 +0900975 err = extract_length(value->parser, &ptr, &total);
976 if (err)
977 return err;
Thiago Macieira63abed92015-10-28 17:01:14 -0700978 if (total > (size_t)(value->parser->end - ptr))
Thiago Macieira9ae05812015-05-11 15:09:09 +0900979 return CborErrorUnexpectedEOF;
980 if (total <= *buflen)
Thiago Macieirae12dfd02016-06-07 16:29:25 -0700981 *result = !!func(buffer, ptr, total);
Thiago Macieira9ae05812015-05-11 15:09:09 +0900982 else
983 *result = false;
984 ptr += total;
985 } else {
Thiago Macieiradbc01292016-06-06 17:02:25 -0700986 /* chunked */
Thiago Macieira9ae05812015-05-11 15:09:09 +0900987 ++ptr;
988 total = 0;
989 *result = true;
990 while (true) {
991 size_t chunkLen;
992 size_t newTotal;
993
994 if (ptr == value->parser->end)
995 return CborErrorUnexpectedEOF;
996
Thiago Macieira5752ce52015-06-16 12:10:03 -0700997 if (*ptr == (uint8_t)BreakByte) {
Thiago Macieira9ae05812015-05-11 15:09:09 +0900998 ++ptr;
999 break;
1000 }
1001
Thiago Macieiradbc01292016-06-06 17:02:25 -07001002 /* is this the right type? */
Thiago Macieira9ae05812015-05-11 15:09:09 +09001003 if ((*ptr & MajorTypeMask) != value->type)
1004 return CborErrorIllegalType;
1005
1006 err = extract_length(value->parser, &ptr, &chunkLen);
1007 if (err)
1008 return err;
1009
Thiago Macieira1de31a42015-06-16 16:01:16 -07001010 if (unlikely(add_check_overflow(total, chunkLen, &newTotal)))
Thiago Macieira9ae05812015-05-11 15:09:09 +09001011 return CborErrorDataTooLarge;
1012
Thiago Macieira63abed92015-10-28 17:01:14 -07001013 if (chunkLen > (size_t)(value->parser->end - ptr))
Thiago Macieira9ae05812015-05-11 15:09:09 +09001014 return CborErrorUnexpectedEOF;
1015
1016 if (*result && *buflen >= newTotal)
Thiago Macieirae12dfd02016-06-07 16:29:25 -07001017 *result = !!func(buffer + total, ptr, chunkLen);
Thiago Macieira9ae05812015-05-11 15:09:09 +09001018 else
1019 *result = false;
1020
1021 ptr += chunkLen;
1022 total = newTotal;
1023 }
1024 }
1025
Thiago Macieiradbc01292016-06-06 17:02:25 -07001026 /* is there enough room for the ending NUL byte? */
Thiago Macieirae136feb2017-02-24 21:21:04 -08001027 if (*result && *buflen > total) {
1028 uint8_t nul[] = { 0 };
1029 *result = !!func(buffer + total, nul, 1);
1030 }
Thiago Macieira9ae05812015-05-11 15:09:09 +09001031 *buflen = total;
1032
1033 if (next) {
1034 *next = *value;
1035 next->ptr = ptr;
1036 return preparse_next_value(next);
1037 }
1038 return CborNoError;
1039}
1040
Thiago Macieira2312efd2015-05-06 16:07:48 -07001041/**
Thiago Macieiraff130bc2015-06-19 15:15:33 -07001042 * \fn CborError cbor_value_copy_text_string(const CborValue *value, char *buffer, size_t *buflen, CborValue *next)
1043 *
Thiago Macieira2312efd2015-05-06 16:07:48 -07001044 * Copies the string pointed by \a value into the buffer provided at \a buffer
1045 * of \a buflen bytes. If \a buffer is a NULL pointer, this function will not
1046 * copy anything and will only update the \a next value.
1047 *
Thiago Macieira46a818e2015-10-08 15:13:05 +02001048 * If the iterator \a value does not point to a text string, the behaviour is
1049 * undefined, so checking with \ref cbor_value_get_type or \ref
1050 * cbor_value_is_text_string is recommended.
1051 *
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -07001052 * If the provided buffer length was too small, this function returns an error
1053 * condition of \ref CborErrorOutOfMemory. If you need to calculate the length
1054 * of the string in order to preallocate a buffer, use
Thiago Macieira2312efd2015-05-06 16:07:48 -07001055 * cbor_value_calculate_string_length().
1056 *
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -07001057 * On success, this function sets the number of bytes copied to \c{*buflen}. If
1058 * the buffer is large enough, this function will insert a null byte after the
1059 * last copied byte, to facilitate manipulation of text strings. That byte is
1060 * not included in the returned value of \c{*buflen}.
Thiago Macieira2312efd2015-05-06 16:07:48 -07001061 *
1062 * The \a next pointer, if not null, will be updated to point to the next item
1063 * after this string. If \a value points to the last item, then \a next will be
1064 * invalid.
1065 *
Thiago Macieira46a818e2015-10-08 15:13:05 +02001066 * This function may not run in constant time (it will run in O(n) time on the
1067 * number of chunks). It requires constant memory (O(1)).
1068 *
Thiago Macieira2312efd2015-05-06 16:07:48 -07001069 * \note This function does not perform UTF-8 validation on the incoming text
1070 * string.
1071 *
Thiago Macieiraff130bc2015-06-19 15:15:33 -07001072 * \sa 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 -07001073 */
Thiago Macieiraff130bc2015-06-19 15:15:33 -07001074
1075/**
1076 * \fn CborError cbor_value_copy_byte_string(const CborValue *value, uint8_t *buffer, size_t *buflen, CborValue *next)
1077 *
1078 * Copies the string pointed by \a value into the buffer provided at \a buffer
1079 * of \a buflen bytes. If \a buffer is a NULL pointer, this function will not
1080 * copy anything and will only update the \a next value.
1081 *
Thiago Macieira46a818e2015-10-08 15:13:05 +02001082 * If the iterator \a value does not point to a byte string, the behaviour is
1083 * undefined, so checking with \ref cbor_value_get_type or \ref
1084 * cbor_value_is_byte_string is recommended.
1085 *
Thiago Macieiraff130bc2015-06-19 15:15:33 -07001086 * If the provided buffer length was too small, this function returns an error
1087 * condition of \ref CborErrorOutOfMemory. If you need to calculate the length
1088 * of the string in order to preallocate a buffer, use
1089 * cbor_value_calculate_string_length().
1090 *
1091 * On success, this function sets the number of bytes copied to \c{*buflen}. If
1092 * the buffer is large enough, this function will insert a null byte after the
1093 * last copied byte, to facilitate manipulation of null-terminated strings.
1094 * That byte is not included in the returned value of \c{*buflen}.
1095 *
1096 * The \a next pointer, if not null, will be updated to point to the next item
1097 * after this string. If \a value points to the last item, then \a next will be
1098 * invalid.
1099 *
Thiago Macieira46a818e2015-10-08 15:13:05 +02001100 * This function may not run in constant time (it will run in O(n) time on the
1101 * number of chunks). It requires constant memory (O(1)).
1102 *
Thiago Macieiraff130bc2015-06-19 15:15:33 -07001103 * \sa cbor_value_dup_text_string(), cbor_value_copy_text_string(), cbor_value_get_string_length(), cbor_value_calculate_string_length()
1104 */
1105
1106CborError _cbor_value_copy_string(const CborValue *value, void *buffer,
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -07001107 size_t *buflen, CborValue *next)
Thiago Macieira2312efd2015-05-06 16:07:48 -07001108{
Thiago Macieira9ae05812015-05-11 15:09:09 +09001109 bool copied_all;
Thiago Macieiraed5b57c2015-07-07 16:38:27 -07001110 CborError err = iterate_string_chunks(value, (char*)buffer, buflen, &copied_all, next,
Thiago Macieira9ae05812015-05-11 15:09:09 +09001111 buffer ? (IterateFunction)memcpy : iterate_noop);
1112 return err ? err :
1113 copied_all ? CborNoError : CborErrorOutOfMemory;
Thiago Macieirac70169f2015-05-06 07:49:44 -07001114}
1115
Thiago Macieirac4a73c62015-05-09 18:14:11 -07001116/**
1117 * Compares the entry \a value with the string \a string and store the result
Thiago Macieira46a818e2015-10-08 15:13:05 +02001118 * in \a result. If the value is different from \a string \a result will
1119 * contain \c false.
Thiago Macieirac4a73c62015-05-09 18:14:11 -07001120 *
1121 * The entry at \a value may be a tagged string. If \a is not a string or a
1122 * tagged string, the comparison result will be false.
Thiago Macieira46a818e2015-10-08 15:13:05 +02001123 *
1124 * CBOR requires text strings to be encoded in UTF-8, but this function does
1125 * not validate either the strings in the stream or the string \a string to be
1126 * matched. Moreover, comparison is done on strict codepoint comparison,
1127 * without any Unicode normalization.
1128 *
1129 * This function may not run in constant time (it will run in O(n) time on the
1130 * number of chunks). It requires constant memory (O(1)).
1131 *
1132 * \sa cbor_value_skip_tag(), cbor_value_copy_text_string()
Thiago Macieirac4a73c62015-05-09 18:14:11 -07001133 */
1134CborError cbor_value_text_string_equals(const CborValue *value, const char *string, bool *result)
1135{
1136 CborValue copy = *value;
1137 CborError err = cbor_value_skip_tag(&copy);
1138 if (err)
1139 return err;
1140 if (!cbor_value_is_text_string(&copy)) {
1141 *result = false;
1142 return CborNoError;
1143 }
1144
1145 size_t len = strlen(string);
1146 return iterate_string_chunks(&copy, CONST_CAST(char *, string), &len, result, NULL, iterate_memcmp);
1147}
1148
1149/**
Thiago Macieira46a818e2015-10-08 15:13:05 +02001150 * \fn bool cbor_value_is_array(const CborValue *value)
Thiago Macieira7b623c22015-05-11 15:52:14 +09001151 *
Thiago Macieira46a818e2015-10-08 15:13:05 +02001152 * Returns true if the iterator \a value is valid and points to a CBOR array.
1153 *
1154 * \sa cbor_value_is_valid(), cbor_value_is_map()
1155 */
1156
1157/**
1158 * \fn CborError cbor_value_get_array_length(const CborValue *value, size_t *length)
1159 *
1160 * Extracts the length of the CBOR array that \a value points to and stores it
1161 * in \a result. If the iterator \a value does not point to a CBOR array, the
1162 * behaviour is undefined, so checking with \ref cbor_value_get_type or \ref
1163 * cbor_value_is_array is recommended.
1164 *
1165 * If the length of this array is not encoded in the CBOR data stream, this
1166 * function will return the recoverable error CborErrorUnknownLength. You may
1167 * also check whether that is the case by using cbor_value_is_length_known().
1168 *
1169 * \note On 32-bit platforms, this function will return error condition of \ref
1170 * CborErrorDataTooLarge if the stream indicates a length that is too big to
1171 * fit in 32-bit.
1172 *
1173 * \sa cbor_value_is_valid(), cbor_value_is_length_known()
1174 */
1175
1176/**
1177 * \fn bool cbor_value_is_map(const CborValue *value)
1178 *
1179 * Returns true if the iterator \a value is valid and points to a CBOR map.
1180 *
1181 * \sa cbor_value_is_valid(), cbor_value_is_array()
1182 */
1183
1184/**
1185 * \fn CborError cbor_value_get_map_length(const CborValue *value, size_t *length)
1186 *
1187 * Extracts the length of the CBOR map that \a value points to and stores it in
1188 * \a result. If the iterator \a value does not point to a CBOR map, the
1189 * behaviour is undefined, so checking with \ref cbor_value_get_type or \ref
1190 * cbor_value_is_map is recommended.
1191 *
1192 * If the length of this map is not encoded in the CBOR data stream, this
1193 * function will return the recoverable error CborErrorUnknownLength. You may
1194 * also check whether that is the case by using cbor_value_is_length_known().
1195 *
1196 * \note On 32-bit platforms, this function will return error condition of \ref
1197 * CborErrorDataTooLarge if the stream indicates a length that is too big to
1198 * fit in 32-bit.
1199 *
1200 * \sa cbor_value_is_valid(), cbor_value_is_length_known()
1201 */
1202
1203/**
1204 * Attempts to find the value in map \a map that corresponds to the text string
1205 * entry \a string. If the iterator \a value does not point to a CBOR map, the
1206 * behaviour is undefined, so checking with \ref cbor_value_get_type or \ref
1207 * cbor_value_is_map is recommended.
1208 *
1209 * If the item is found, it is stored in \a result. If no item is found
1210 * matching the key, then \a result will contain an element of type \ref
1211 * CborInvalidType. Matching is performed using
1212 * cbor_value_text_string_equals(), so tagged strings will also match.
1213 *
1214 * This function has a time complexity of O(n) where n is the number of
1215 * elements in the map to be searched. In addition, this function is has O(n)
1216 * memory requirement based on the number of nested containers (maps or arrays)
1217 * found as elements of this map.
1218 *
1219 * \sa cbor_value_is_valid(), cbor_value_text_string_equals(), cbor_value_advance()
Thiago Macieira7b623c22015-05-11 15:52:14 +09001220 */
1221CborError cbor_value_map_find_value(const CborValue *map, const char *string, CborValue *element)
1222{
1223 assert(cbor_value_is_map(map));
1224 size_t len = strlen(string);
1225 CborError err = cbor_value_enter_container(map, element);
1226 if (err)
1227 goto error;
1228
1229 while (!cbor_value_at_end(element)) {
Thiago Macieiradbc01292016-06-06 17:02:25 -07001230 /* find the non-tag so we can compare */
Thiago Macieira7b623c22015-05-11 15:52:14 +09001231 err = cbor_value_skip_tag(element);
1232 if (err)
1233 goto error;
1234 if (cbor_value_is_text_string(element)) {
1235 bool equals;
1236 size_t dummyLen = len;
1237 err = iterate_string_chunks(element, CONST_CAST(char *, string), &dummyLen,
1238 &equals, element, iterate_memcmp);
1239 if (err)
1240 goto error;
1241 if (equals)
1242 return preparse_value(element);
1243 } else {
Thiago Macieiradbc01292016-06-06 17:02:25 -07001244 /* skip this key */
Thiago Macieira7b623c22015-05-11 15:52:14 +09001245 err = cbor_value_advance(element);
1246 if (err)
1247 goto error;
1248 }
1249
Thiago Macieiradbc01292016-06-06 17:02:25 -07001250 /* skip this value */
Thiago Macieira7b623c22015-05-11 15:52:14 +09001251 err = cbor_value_skip_tag(element);
1252 if (err)
1253 goto error;
1254 err = cbor_value_advance(element);
1255 if (err)
1256 goto error;
1257 }
1258
Thiago Macieiradbc01292016-06-06 17:02:25 -07001259 /* not found */
Thiago Macieira7b623c22015-05-11 15:52:14 +09001260 element->type = CborInvalidType;
1261 return CborNoError;
1262
1263error:
1264 element->type = CborInvalidType;
1265 return err;
1266}
1267
1268/**
Thiago Macieira46a818e2015-10-08 15:13:05 +02001269 * \fn bool cbor_value_is_float(const CborValue *value)
1270 *
1271 * Returns true if the iterator \a value is valid and points to a CBOR
1272 * single-precision floating point (32-bit).
1273 *
1274 * \sa cbor_value_is_valid(), cbor_value_is_double(), cbor_value_is_half_float()
1275 */
1276
1277/**
1278 * \fn CborError cbor_value_get_float(const CborValue *value, float *result)
1279 *
1280 * Retrieves the CBOR single-precision floating point (32-bit) value that \a
1281 * value points to and stores it in \a result. If the iterator \a value does
1282 * not point to a single-precision floating point value, the behavior is
1283 * undefined, so checking with \ref cbor_value_get_type or with \ref
1284 * cbor_value_is_float is recommended.
1285 *
1286 * \sa cbor_value_get_type(), cbor_value_is_valid(), cbor_value_is_float(), cbor_value_get_double()
1287 */
1288
1289/**
1290 * \fn bool cbor_value_is_double(const CborValue *value)
1291 *
1292 * Returns true if the iterator \a value is valid and points to a CBOR
1293 * double-precision floating point (64-bit).
1294 *
1295 * \sa cbor_value_is_valid(), cbor_value_is_float(), cbor_value_is_half_float()
1296 */
1297
1298/**
1299 * \fn CborError cbor_value_get_double(const CborValue *value, float *result)
1300 *
1301 * Retrieves the CBOR double-precision floating point (64-bit) value that \a
1302 * value points to and stores it in \a result. If the iterator \a value does
1303 * not point to a double-precision floating point value, the behavior is
1304 * undefined, so checking with \ref cbor_value_get_type or with \ref
1305 * cbor_value_is_double is recommended.
1306 *
1307 * \sa cbor_value_get_type(), cbor_value_is_valid(), cbor_value_is_double(), cbor_value_get_float()
1308 */
1309
1310/**
1311 * \fn bool cbor_value_is_half_float(const CborValue *value)
1312 *
1313 * Returns true if the iterator \a value is valid and points to a CBOR
1314 * single-precision floating point (16-bit).
1315 *
1316 * \sa cbor_value_is_valid(), cbor_value_is_double(), cbor_value_is_float()
1317 */
1318
1319/**
1320 * Retrieves the CBOR half-precision floating point (16-bit) value that \a
1321 * value points to and stores it in \a result. If the iterator \a value does
1322 * not point to a half-precision floating point value, the behavior is
1323 * undefined, so checking with \ref cbor_value_get_type or with \ref
1324 * cbor_value_is_half_float is recommended.
1325 *
1326 * Note: since the C language does not have a standard type for half-precision
1327 * floating point, this function takes a \c{void *} as a parameter for the
1328 * storage area, which must be at least 16 bits wide.
1329 *
1330 * \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 -07001331 */
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -07001332CborError cbor_value_get_half_float(const CborValue *value, void *result)
Thiago Macieirac70169f2015-05-06 07:49:44 -07001333{
Thiago Macieiracac5db52016-07-07 15:50:59 -07001334 assert(cbor_value_is_half_float(value));
Thiago Macieirac70169f2015-05-06 07:49:44 -07001335
Thiago Macieiradbc01292016-06-06 17:02:25 -07001336 /* size has been computed already */
Thiago Macieirac70169f2015-05-06 07:49:44 -07001337 uint16_t v = get16(value->ptr + 1);
1338 memcpy(result, &v, sizeof(v));
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -07001339 return CborNoError;
Thiago Macieira54a0e102015-05-05 21:25:06 -07001340}
Thiago Macieira46a818e2015-10-08 15:13:05 +02001341
1342/** @} */