blob: 5273a9a4bc473329cf405a7e4b1496ae163cdea6 [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
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
Thiago Macieira54a0e102015-05-05 21:25:06 -070035#include <string.h>
36
Thiago Macieira4a99af92015-05-12 10:41:45 +090037#ifndef CBOR_PARSER_MAX_RECURSIONS
38# define CBOR_PARSER_MAX_RECURSIONS 1024
39#endif
40
Thiago Macieira54a0e102015-05-05 21:25:06 -070041/**
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;
64 * cbor_parser_init(buffer, len, 0, &buffer, &value);
65 * 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
72 * example does the exact same operation, but includes error parsing and
73 * 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;
81 * if (cbor_parser_init(buffer, len, 0, &buffer, &value) != CborNoError)
82 * 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
Thiago Macieira2f5d20e2017-03-06 10:11:52 +0100166CBOR_INTERNAL_API_CC CborError _cbor_value_extract_number(const uint8_t **ptr, const uint8_t *end, uint64_t *len)
Thiago Macieira2c22d712017-03-05 00:17:35 -0600167{
168 uint8_t additional_information = **ptr & SmallValueMask;
169 ++*ptr;
170 if (additional_information < Value8Bit) {
171 *len = additional_information;
172 return CborNoError;
173 }
174 if (unlikely(additional_information > Value64Bit))
175 return CborErrorIllegalNumber;
176
177 size_t bytesNeeded = (size_t)(1 << (additional_information - Value8Bit));
178 if (unlikely(bytesNeeded > (size_t)(end - *ptr))) {
179 return CborErrorUnexpectedEOF;
180 } else if (bytesNeeded == 1) {
181 *len = (uint8_t)(*ptr)[0];
182 } else if (bytesNeeded == 2) {
183 *len = get16(*ptr);
184 } else if (bytesNeeded == 4) {
185 *len = get32(*ptr);
186 } else {
187 *len = get64(*ptr);
188 }
189 *ptr += bytesNeeded;
190 return CborNoError;
191}
192
Thiago Macieiraf5cb94b2015-06-16 16:10:49 -0700193static CborError extract_length(const CborParser *parser, const uint8_t **ptr, size_t *len)
Thiago Macieira54a0e102015-05-05 21:25:06 -0700194{
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700195 uint64_t v;
Thiago Macieira2c22d712017-03-05 00:17:35 -0600196 CborError err = _cbor_value_extract_number(ptr, parser->end, &v);
Mike Colagrosso629d5b72016-02-24 15:12:34 -0700197 if (err) {
198 *len = 0;
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700199 return err;
Mike Colagrosso629d5b72016-02-24 15:12:34 -0700200 }
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700201
alradmsft9ba47912016-10-11 17:56:15 -0700202 *len = (size_t)v;
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700203 if (v != *len)
204 return CborErrorDataTooLarge;
205 return CborNoError;
206}
207
208static bool is_fixed_type(uint8_t type)
209{
210 return type != CborTextStringType && type != CborByteStringType && type != CborArrayType &&
211 type != CborMapType;
212}
213
214static CborError preparse_value(CborValue *it)
215{
216 const CborParser *parser = it->parser;
Thiago Macieira11e913f2015-05-07 13:01:18 -0700217 it->type = CborInvalidType;
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700218
Thiago Macieiradbc01292016-06-06 17:02:25 -0700219 /* are we at the end? */
Thiago Macieira54a0e102015-05-05 21:25:06 -0700220 if (it->ptr == parser->end)
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700221 return CborErrorUnexpectedEOF;
Thiago Macieira54a0e102015-05-05 21:25:06 -0700222
223 uint8_t descriptor = *it->ptr;
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700224 uint8_t type = descriptor & MajorTypeMask;
Thiago Macieira851c4812015-05-08 15:23:20 -0700225 it->type = type;
Thiago Macieira54a0e102015-05-05 21:25:06 -0700226 it->flags = 0;
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700227 it->extra = (descriptor &= SmallValueMask);
228
Thiago Macieira56d99832015-05-07 14:34:27 -0700229 if (descriptor > Value64Bit) {
230 if (unlikely(descriptor != IndefiniteLength))
Thiago Macieira3f76f632015-05-12 10:10:09 +0900231 return type == CborSimpleType ? CborErrorUnknownType : CborErrorIllegalNumber;
Thiago Macieira56d99832015-05-07 14:34:27 -0700232 if (likely(!is_fixed_type(type))) {
Thiago Macieiradbc01292016-06-06 17:02:25 -0700233 /* special case */
Thiago Macieira56d99832015-05-07 14:34:27 -0700234 it->flags |= CborIteratorFlag_UnknownLength;
235 it->type = type;
236 return CborNoError;
237 }
238 return type == CborSimpleType ? CborErrorUnexpectedBreak : CborErrorIllegalNumber;
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700239 }
Thiago Macieira54a0e102015-05-05 21:25:06 -0700240
Thiago Macieirac70169f2015-05-06 07:49:44 -0700241 size_t bytesNeeded = descriptor < Value8Bit ? 0 : (1 << (descriptor - Value8Bit));
Thiago Macieira63abed92015-10-28 17:01:14 -0700242 if (bytesNeeded + 1 > (size_t)(parser->end - it->ptr))
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700243 return CborErrorUnexpectedEOF;
Thiago Macieirac70169f2015-05-06 07:49:44 -0700244
Thiago Macieira851c4812015-05-08 15:23:20 -0700245 uint8_t majortype = type >> MajorTypeShift;
246 if (majortype == NegativeIntegerType) {
Thiago Macieira54a0e102015-05-05 21:25:06 -0700247 it->flags |= CborIteratorFlag_NegativeInteger;
Thiago Macieira851c4812015-05-08 15:23:20 -0700248 it->type = CborIntegerType;
249 } else if (majortype == SimpleTypesType) {
Thiago Macieira54a0e102015-05-05 21:25:06 -0700250 switch (descriptor) {
251 case FalseValue:
252 it->extra = false;
Thiago Macieira851c4812015-05-08 15:23:20 -0700253 it->type = CborBooleanType;
Thiago Macieira991dd922015-05-07 11:57:59 -0700254 break;
255
Thiago Macieira851c4812015-05-08 15:23:20 -0700256 case SinglePrecisionFloat:
257 case DoublePrecisionFloat:
258 it->flags |= CborIteratorFlag_IntegerValueTooLarge;
Thiago Macieiradbc01292016-06-06 17:02:25 -0700259 /* fall through */
Thiago Macieira54a0e102015-05-05 21:25:06 -0700260 case TrueValue:
261 case NullValue:
262 case UndefinedValue:
263 case HalfPrecisionFloat:
Thiago Macieira851c4812015-05-08 15:23:20 -0700264 it->type = *it->ptr;
Thiago Macieira54a0e102015-05-05 21:25:06 -0700265 break;
266
267 case SimpleTypeInNextByte:
Thiago Macieira851c4812015-05-08 15:23:20 -0700268 it->extra = (uint8_t)it->ptr[1];
Thiago Macieira54a0e102015-05-05 21:25:06 -0700269#ifndef CBOR_PARSER_NO_STRICT_CHECKS
Thiago Macieira851c4812015-05-08 15:23:20 -0700270 if (unlikely(it->extra < 32)) {
271 it->type = CborInvalidType;
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700272 return CborErrorIllegalSimpleType;
Thiago Macieira851c4812015-05-08 15:23:20 -0700273 }
Thiago Macieira54a0e102015-05-05 21:25:06 -0700274#endif
Thiago Macieira991dd922015-05-07 11:57:59 -0700275 break;
276
Thiago Macieira54a0e102015-05-05 21:25:06 -0700277 case 28:
278 case 29:
279 case 30:
Thiago Macieira54a0e102015-05-05 21:25:06 -0700280 case Break:
Thiago Macieirada8e35e2017-03-05 09:58:01 +0100281 cbor_assert(false); /* these conditions can't be reached */
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700282 return CborErrorUnexpectedBreak;
Thiago Macieira54a0e102015-05-05 21:25:06 -0700283 }
Thiago Macieira851c4812015-05-08 15:23:20 -0700284 return CborNoError;
Thiago Macieira54a0e102015-05-05 21:25:06 -0700285 }
286
Thiago Macieiradbc01292016-06-06 17:02:25 -0700287 /* try to decode up to 16 bits */
Thiago Macieira54a0e102015-05-05 21:25:06 -0700288 if (descriptor < Value8Bit)
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700289 return CborNoError;
Thiago Macieira54a0e102015-05-05 21:25:06 -0700290
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700291 if (descriptor == Value8Bit)
292 it->extra = (uint8_t)it->ptr[1];
293 else if (descriptor == Value16Bit)
Thiago Macieira54a0e102015-05-05 21:25:06 -0700294 it->extra = get16(it->ptr + 1);
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700295 else
Thiago Macieiradbc01292016-06-06 17:02:25 -0700296 it->flags |= CborIteratorFlag_IntegerValueTooLarge; /* Value32Bit or Value64Bit */
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700297 return CborNoError;
298}
Thiago Macieira54a0e102015-05-05 21:25:06 -0700299
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700300static CborError preparse_next_value(CborValue *it)
301{
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700302 if (it->remaining != UINT32_MAX) {
Thiago Macieiradbc01292016-06-06 17:02:25 -0700303 /* don't decrement the item count if the current item is tag: they don't count */
Thiago Macieira11e913f2015-05-07 13:01:18 -0700304 if (it->type != CborTagType && !--it->remaining) {
305 it->type = CborInvalidType;
Thiago Macieira56d99832015-05-07 14:34:27 -0700306 return CborNoError;
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700307 }
Thiago Macieira5752ce52015-06-16 12:10:03 -0700308 } else if (it->remaining == UINT32_MAX && it->ptr != it->parser->end && *it->ptr == (uint8_t)BreakByte) {
Thiago Macieiradbc01292016-06-06 17:02:25 -0700309 /* end of map or array */
Thiago Macieira56d99832015-05-07 14:34:27 -0700310 ++it->ptr;
311 it->type = CborInvalidType;
312 it->remaining = 0;
313 return CborNoError;
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700314 }
Thiago Macieira56d99832015-05-07 14:34:27 -0700315
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700316 return preparse_value(it);
317}
318
319static CborError advance_internal(CborValue *it)
320{
321 uint64_t length;
Thiago Macieira2c22d712017-03-05 00:17:35 -0600322 CborError err = _cbor_value_extract_number(&it->ptr, it->parser->end, &length);
Thiago Macieirada8e35e2017-03-05 09:58:01 +0100323 cbor_assert(err == CborNoError);
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700324
Thiago Macieira56d99832015-05-07 14:34:27 -0700325 if (it->type == CborByteStringType || it->type == CborTextStringType) {
Thiago Macieirada8e35e2017-03-05 09:58:01 +0100326 cbor_assert(length == (size_t)length);
327 cbor_assert((it->flags & CborIteratorFlag_UnknownLength) == 0);
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700328 it->ptr += length;
329 }
330
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700331 return preparse_next_value(it);
Thiago Macieira54a0e102015-05-05 21:25:06 -0700332}
333
Thiago Macieira2312efd2015-05-06 16:07:48 -0700334/** \internal
335 *
336 * Decodes the CBOR integer value when it is larger than the 16 bits available
337 * in value->extra. This function requires that value->flags have the
338 * CborIteratorFlag_IntegerValueTooLarge flag set.
339 *
340 * This function is also used to extract single- and double-precision floating
341 * point values (SinglePrecisionFloat == Value32Bit and DoublePrecisionFloat ==
342 * Value64Bit).
343 */
Thiago Macieira54a0e102015-05-05 21:25:06 -0700344uint64_t _cbor_value_decode_int64_internal(const CborValue *value)
345{
Thiago Macieirada8e35e2017-03-05 09:58:01 +0100346 cbor_assert(value->flags & CborIteratorFlag_IntegerValueTooLarge ||
347 value->type == CborFloatType || value->type == CborDoubleType);
Thiago Macieira851c4812015-05-08 15:23:20 -0700348
Thiago Macieiradbc01292016-06-06 17:02:25 -0700349 /* since the additional information can only be Value32Bit or Value64Bit,
350 * we just need to test for the one bit those two options differ */
Thiago Macieirada8e35e2017-03-05 09:58:01 +0100351 cbor_assert((*value->ptr & SmallValueMask) == Value32Bit || (*value->ptr & SmallValueMask) == Value64Bit);
Thiago Macieira851c4812015-05-08 15:23:20 -0700352 if ((*value->ptr & 1) == (Value32Bit & 1))
Thiago Macieira54a0e102015-05-05 21:25:06 -0700353 return get32(value->ptr + 1);
354
Thiago Macieirada8e35e2017-03-05 09:58:01 +0100355 cbor_assert((*value->ptr & SmallValueMask) == Value64Bit);
Thiago Macieira54a0e102015-05-05 21:25:06 -0700356 return get64(value->ptr + 1);
357}
358
359/**
360 * Initializes the CBOR parser for parsing \a size bytes beginning at \a
361 * buffer. Parsing will use flags set in \a flags. The iterator to the first
362 * element is returned in \a it.
Thiago Macieira2312efd2015-05-06 16:07:48 -0700363 *
364 * The \a parser structure needs to remain valid throughout the decoding
365 * process. It is not thread-safe to share one CborParser among multiple
366 * threads iterating at the same time, but the object can be copied so multiple
367 * threads can iterate.
Thiago Macieira54a0e102015-05-05 21:25:06 -0700368 */
Thiago Macieira5752ce52015-06-16 12:10:03 -0700369CborError cbor_parser_init(const uint8_t *buffer, size_t size, int flags, CborParser *parser, CborValue *it)
Thiago Macieira54a0e102015-05-05 21:25:06 -0700370{
371 memset(parser, 0, sizeof(*parser));
372 parser->end = buffer + size;
Thiago Macieira54a0e102015-05-05 21:25:06 -0700373 parser->flags = flags;
374 it->parser = parser;
375 it->ptr = buffer;
Thiago Macieiradbc01292016-06-06 17:02:25 -0700376 it->remaining = 1; /* there's one type altogether, usually an array or map */
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700377 return preparse_value(it);
Thiago Macieira2312efd2015-05-06 16:07:48 -0700378}
379
380/**
Thiago Macieira46a818e2015-10-08 15:13:05 +0200381 * \fn bool cbor_value_at_end(const CborValue *it)
382 *
383 * Returns true if \a it has reached the end of the iteration, usually when
Thiago Macieira740e29d2016-07-07 13:32:47 -0700384 * advancing after the last item in an array or map.
Thiago Macieira46a818e2015-10-08 15:13:05 +0200385 *
Thiago Macieira740e29d2016-07-07 13:32:47 -0700386 * In the case of the outermost CborValue object, this function returns true
387 * after decoding a single element. A pointer to the first byte of the
388 * remaining data (if any) can be obtained with cbor_value_get_next_byte().
389 *
390 * \sa cbor_value_advance(), cbor_value_is_valid(), cbor_value_get_next_byte()
391 */
392
393/**
394 * \fn const uint8_t *cbor_value_get_next_byte(const CborValue *it)
395 *
396 * Returns a pointer to the next byte that would be decoded if this CborValue
397 * object were advanced.
398 *
399 * This function is useful if cbor_value_at_end() returns true for the
400 * outermost CborValue: the pointer returned is the first byte of the data
401 * remaining in the buffer, if any. Code can decide whether to begin decoding a
402 * new CBOR data stream from this point, or parse some other data appended to
403 * the same buffer.
404 *
405 * This function may be used even after a parsing error. If that occurred,
406 * then this function returns a pointer to where the parsing error occurred.
407 * Note that the error recovery is not precise and the pointer may not indicate
408 * the exact byte containing bad data.
409 *
410 * \sa cbor_value_at_end()
Thiago Macieira46a818e2015-10-08 15:13:05 +0200411 */
412
413/**
414 * \fn bool cbor_value_is_valid(const CborValue *it)
415 *
416 * Returns true if the iterator \a it contains a valid value. Invalid iterators
417 * happen when iteration reaches the end of a container (see \ref
418 * cbor_value_at_end()) or when a search function resulted in no matches.
419 *
420 * \sa cbor_value_advance(), cbor_valie_at_end(), cbor_value_get_type()
421 */
422
423/**
Thiago Macieira2312efd2015-05-06 16:07:48 -0700424 * Advances the CBOR value \a it by one fixed-size position. Fixed-size types
425 * are: integers, tags, simple types (including boolean, null and undefined
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700426 * values) and floating point types.
Thiago Macieira2312efd2015-05-06 16:07:48 -0700427 *
Thiago Macieira46a818e2015-10-08 15:13:05 +0200428 * If the type is not of fixed size, this function has undefined behavior. Code
429 * must be sure that the current type is one of the fixed-size types before
430 * calling this function. This function is provided because it can guarantee
431 * that runs in constant time (O(1)).
432 *
433 * If the caller is not able to determine whether the type is fixed or not, code
434 * can use the cbor_value_advance() function instead.
435 *
436 * \sa cbor_value_at_end(), cbor_value_advance(), cbor_value_enter_container(), cbor_value_leave_container()
Thiago Macieira2312efd2015-05-06 16:07:48 -0700437 */
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700438CborError cbor_value_advance_fixed(CborValue *it)
Thiago Macieira54a0e102015-05-05 21:25:06 -0700439{
Thiago Macieirada8e35e2017-03-05 09:58:01 +0100440 cbor_assert(it->type != CborInvalidType);
441 cbor_assert(is_fixed_type(it->type));
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700442 if (!it->remaining)
443 return CborErrorAdvancePastEOF;
444 return advance_internal(it);
Thiago Macieira54a0e102015-05-05 21:25:06 -0700445}
446
Thiago Macieira4a99af92015-05-12 10:41:45 +0900447static CborError advance_recursive(CborValue *it, int nestingLevel)
448{
449 if (is_fixed_type(it->type))
450 return advance_internal(it);
451
452 if (!cbor_value_is_container(it)) {
453 size_t len = SIZE_MAX;
Thiago Macieiraff130bc2015-06-19 15:15:33 -0700454 return _cbor_value_copy_string(it, NULL, &len, it);
Thiago Macieira4a99af92015-05-12 10:41:45 +0900455 }
456
Thiago Macieiradbc01292016-06-06 17:02:25 -0700457 /* map or array */
Thiago Macieira4a99af92015-05-12 10:41:45 +0900458 if (nestingLevel == CBOR_PARSER_MAX_RECURSIONS)
459 return CborErrorNestingTooDeep;
460
461 CborError err;
462 CborValue recursed;
463 err = cbor_value_enter_container(it, &recursed);
464 if (err)
465 return err;
466 while (!cbor_value_at_end(&recursed)) {
467 err = advance_recursive(&recursed, nestingLevel + 1);
468 if (err)
469 return err;
470 }
471 return cbor_value_leave_container(it, &recursed);
472}
473
474
Thiago Macieira2312efd2015-05-06 16:07:48 -0700475/**
476 * Advances the CBOR value \a it by one element, skipping over containers.
477 * Unlike cbor_value_advance_fixed(), this function can be called on a CBOR
478 * value of any type. However, if the type is a container (map or array) or a
479 * string with a chunked payload, this function will not run in constant time
480 * and will recurse into itself (it will run on O(n) time for the number of
481 * elements or chunks and will use O(n) memory for the number of nested
482 * containers).
483 *
Thiago Macieira46a818e2015-10-08 15:13:05 +0200484 * \sa cbor_value_at_end(), cbor_value_advance_fixed(), cbor_value_enter_container(), cbor_value_leave_container()
Thiago Macieira2312efd2015-05-06 16:07:48 -0700485 */
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700486CborError cbor_value_advance(CborValue *it)
Thiago Macieira2312efd2015-05-06 16:07:48 -0700487{
Thiago Macieirada8e35e2017-03-05 09:58:01 +0100488 cbor_assert(it->type != CborInvalidType);
Thiago Macieira2312efd2015-05-06 16:07:48 -0700489 if (!it->remaining)
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700490 return CborErrorAdvancePastEOF;
Thiago Macieira4a99af92015-05-12 10:41:45 +0900491 return advance_recursive(it, 0);
Thiago Macieira2312efd2015-05-06 16:07:48 -0700492}
493
494/**
Thiago Macieira46a818e2015-10-08 15:13:05 +0200495 * \fn bool cbor_value_is_tag(const CborValue *value)
496 *
497 * Returns true if the iterator \a value is valid and points to a CBOR tag.
498 *
499 * \sa cbor_value_get_tag(), cbor_value_skip_tag()
500 */
501
502/**
503 * \fn CborError cbor_value_get_tag(const CborValue *value, CborTag *result)
504 *
505 * Retrieves the CBOR tag value that \a value points to and stores it in \a
506 * result. If the iterator \a value does not point to a CBOR tag value, the
507 * behavior is undefined, so checking with \ref cbor_value_get_type or with
508 * \ref cbor_value_is_tag is recommended.
509 *
510 * \sa cbor_value_get_type(), cbor_value_is_valid(), cbor_value_is_tag()
511 */
512
513/**
Thiago Macieirac4a73c62015-05-09 18:14:11 -0700514 * Advances the CBOR value \a it until it no longer points to a tag. If \a it is
515 * already not pointing to a tag, then this function returns it unchanged.
516 *
Thiago Macieira46a818e2015-10-08 15:13:05 +0200517 * This function does not run in constant time: it will run on O(n) for n being
518 * the number of tags. It does use constant memory (O(1) memory requirements).
519 *
Thiago Macieirac4a73c62015-05-09 18:14:11 -0700520 * \sa cbor_value_advance_fixed(), cbor_value_advance()
521 */
522CborError cbor_value_skip_tag(CborValue *it)
523{
524 while (cbor_value_is_tag(it)) {
525 CborError err = cbor_value_advance_fixed(it);
526 if (err)
527 return err;
528 }
529 return CborNoError;
530}
531
Thiago Macieirac4a73c62015-05-09 18:14:11 -0700532/**
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700533 * \fn bool cbor_value_is_container(const CborValue *it)
534 *
Thiago Macieira2312efd2015-05-06 16:07:48 -0700535 * Returns true if the \a it value is a container and requires recursion in
536 * order to decode (maps and arrays), false otherwise.
537 */
Thiago Macieira54a0e102015-05-05 21:25:06 -0700538
Thiago Macieira2312efd2015-05-06 16:07:48 -0700539/**
540 * Creates a CborValue iterator pointing to the first element of the container
541 * represented by \a it and saves it in \a recursed. The \a it container object
542 * needs to be kept and passed again to cbor_value_leave_container() in order
543 * to continue iterating past this container.
544 *
Thiago Macieira46a818e2015-10-08 15:13:05 +0200545 * The \a it CborValue iterator must point to a container.
546 *
Thiago Macieira2312efd2015-05-06 16:07:48 -0700547 * \sa cbor_value_is_container(), cbor_value_leave_container(), cbor_value_advance()
548 */
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700549CborError cbor_value_enter_container(const CborValue *it, CborValue *recursed)
Thiago Macieira54a0e102015-05-05 21:25:06 -0700550{
Thiago Macieira56d99832015-05-07 14:34:27 -0700551 CborError err;
Thiago Macieirada8e35e2017-03-05 09:58:01 +0100552 cbor_assert(cbor_value_is_container(it));
Thiago Macieira54a0e102015-05-05 21:25:06 -0700553 *recursed = *it;
Thiago Macieira56d99832015-05-07 14:34:27 -0700554
Thiago Macieira54a0e102015-05-05 21:25:06 -0700555 if (it->flags & CborIteratorFlag_UnknownLength) {
556 recursed->remaining = UINT32_MAX;
Thiago Macieira56d99832015-05-07 14:34:27 -0700557 ++recursed->ptr;
558 err = preparse_value(recursed);
559 if (err != CborErrorUnexpectedBreak)
560 return err;
Thiago Macieiradbc01292016-06-06 17:02:25 -0700561 /* actually, break was expected here
562 * it's just an empty container */
Thiago Macieira56d99832015-05-07 14:34:27 -0700563 ++recursed->ptr;
Thiago Macieira54a0e102015-05-05 21:25:06 -0700564 } else {
Thiago Macieira56d99832015-05-07 14:34:27 -0700565 uint64_t len;
Thiago Macieira2c22d712017-03-05 00:17:35 -0600566 err = _cbor_value_extract_number(&recursed->ptr, recursed->parser->end, &len);
Thiago Macieirada8e35e2017-03-05 09:58:01 +0100567 cbor_assert(err == CborNoError);
Thiago Macieira56d99832015-05-07 14:34:27 -0700568
Thiago Macieirae12dfd02016-06-07 16:29:25 -0700569 recursed->remaining = (uint32_t)len;
Thiago Macieira3f76f632015-05-12 10:10:09 +0900570 if (recursed->remaining != len || len == UINT32_MAX) {
Thiago Macieiradbc01292016-06-06 17:02:25 -0700571 /* back track the pointer to indicate where the error occurred */
Thiago Macieira3f76f632015-05-12 10:10:09 +0900572 recursed->ptr = it->ptr;
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700573 return CborErrorDataTooLarge;
Thiago Macieira3f76f632015-05-12 10:10:09 +0900574 }
Thiago Macieirace16f052015-05-07 23:14:25 -0700575 if (recursed->type == CborMapType) {
Thiago Macieiradbc01292016-06-06 17:02:25 -0700576 /* maps have keys and values, so we need to multiply by 2 */
Thiago Macieira3f76f632015-05-12 10:10:09 +0900577 if (recursed->remaining > UINT32_MAX / 2) {
Thiago Macieiradbc01292016-06-06 17:02:25 -0700578 /* back track the pointer to indicate where the error occurred */
Thiago Macieira3f76f632015-05-12 10:10:09 +0900579 recursed->ptr = it->ptr;
Thiago Macieirace16f052015-05-07 23:14:25 -0700580 return CborErrorDataTooLarge;
Thiago Macieira3f76f632015-05-12 10:10:09 +0900581 }
Thiago Macieirace16f052015-05-07 23:14:25 -0700582 recursed->remaining *= 2;
583 }
Thiago Macieira56d99832015-05-07 14:34:27 -0700584 if (len != 0)
585 return preparse_value(recursed);
Thiago Macieira54a0e102015-05-05 21:25:06 -0700586 }
Thiago Macieira56d99832015-05-07 14:34:27 -0700587
Thiago Macieiradbc01292016-06-06 17:02:25 -0700588 /* the case of the empty container */
Thiago Macieira56d99832015-05-07 14:34:27 -0700589 recursed->type = CborInvalidType;
590 recursed->remaining = 0;
591 return CborNoError;
Thiago Macieirac70169f2015-05-06 07:49:44 -0700592}
593
Thiago Macieira2312efd2015-05-06 16:07:48 -0700594/**
595 * Updates \a it to point to the next element after the container. The \a
Thiago Macieira56d99832015-05-07 14:34:27 -0700596 * recursed object needs to point to the element obtained either by advancing
597 * the last element of the container (via cbor_value_advance(),
598 * cbor_value_advance_fixed(), a nested cbor_value_leave_container(), or the \c
599 * next pointer from cbor_value_copy_string() or cbor_value_dup_string()).
Thiago Macieira2312efd2015-05-06 16:07:48 -0700600 *
Thiago Macieira46a818e2015-10-08 15:13:05 +0200601 * The \a it and \a recursed parameters must be the exact same as passed to
602 * cbor_value_enter_container().
603 *
Thiago Macieira2312efd2015-05-06 16:07:48 -0700604 * \sa cbor_value_enter_container(), cbor_value_at_end()
605 */
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700606CborError cbor_value_leave_container(CborValue *it, const CborValue *recursed)
Thiago Macieirac70169f2015-05-06 07:49:44 -0700607{
Thiago Macieirada8e35e2017-03-05 09:58:01 +0100608 cbor_assert(cbor_value_is_container(it));
609 cbor_assert(recursed->type == CborInvalidType);
Thiago Macieirac70169f2015-05-06 07:49:44 -0700610 it->ptr = recursed->ptr;
Thiago Macieira56d99832015-05-07 14:34:27 -0700611 return preparse_next_value(it);
Thiago Macieirac70169f2015-05-06 07:49:44 -0700612}
613
Thiago Macieira46a818e2015-10-08 15:13:05 +0200614
Thiago Macieira2312efd2015-05-06 16:07:48 -0700615/**
Thiago Macieira46a818e2015-10-08 15:13:05 +0200616 * \fn CborType cbor_value_get_type(const CborValue *value)
617 *
618 * Returns the type of the CBOR value that the iterator \a value points to. If
619 * \a value does not point to a valid value, this function returns \ref
620 * CborInvalidType.
621 *
622 * TinyCBOR also provides functions to test directly if a given CborValue object
623 * is of a given type, like cbor_value_is_text_string() and cbor_value_is_null().
624 *
625 * \sa cbor_value_is_valid()
626 */
627
628/**
629 * \fn bool cbor_value_is_null(const CborValue *value)
630 *
631 * Returns true if the iterator \a value is valid and points to a CBOR null type.
632 *
633 * \sa cbor_value_is_valid(), cbor_value_is_undefined()
634 */
635
636/**
637 * \fn bool cbor_value_is_undefined(const CborValue *value)
638 *
639 * Returns true if the iterator \a value is valid and points to a CBOR undefined type.
640 *
641 * \sa cbor_value_is_valid(), cbor_value_is_null()
642 */
643
644/**
645 * \fn bool cbor_value_is_boolean(const CborValue *value)
646 *
647 * Returns true if the iterator \a value is valid and points to a CBOR boolean
648 * type (true or false).
649 *
650 * \sa cbor_value_is_valid(), cbor_value_get_boolean()
651 */
652
653/**
654 * \fn CborError cbor_value_get_boolean(const CborValue *value, bool *result)
655 *
656 * Retrieves the boolean value that \a value points to and stores it in \a
657 * result. If the iterator \a value does not point to a boolean value, the
658 * behavior is undefined, so checking with \ref cbor_value_get_type or with
659 * \ref cbor_value_is_boolean is recommended.
660 *
661 * \sa cbor_value_get_type(), cbor_value_is_valid(), cbor_value_is_boolean()
662 */
663
664/**
665 * \fn bool cbor_value_is_simple_type(const CborValue *value)
666 *
667 * Returns true if the iterator \a value is valid and points to a CBOR Simple Type
668 * type (other than true, false, null and undefined).
669 *
670 * \sa cbor_value_is_valid(), cbor_value_get_simple_type()
671 */
672
673/**
674 * \fn CborError cbor_value_get_simple_type(const CborValue *value, uint8_t *result)
675 *
676 * Retrieves the CBOR Simple Type value that \a value points to and stores it
677 * in \a result. If the iterator \a value does not point to a simple_type
678 * value, the behavior is undefined, so checking with \ref cbor_value_get_type
679 * or with \ref cbor_value_is_simple_type is recommended.
680 *
681 * \sa cbor_value_get_type(), cbor_value_is_valid(), cbor_value_is_simple_type()
682 */
683
684/**
685 * \fn bool cbor_value_is_integer(const CborValue *value)
686 *
687 * Returns true if the iterator \a value is valid and points to a CBOR integer
688 * type.
689 *
690 * \sa cbor_value_is_valid(), cbor_value_get_int, cbor_value_get_int64, cbor_value_get_uint64, cbor_value_get_raw_integer
691 */
692
693/**
694 * \fn bool cbor_value_is_unsigned_integer(const CborValue *value)
695 *
696 * Returns true if the iterator \a value is valid and points to a CBOR unsigned
697 * integer type (positive values or zero).
698 *
699 * \sa cbor_value_is_valid(), cbor_value_get_uint64()
700 */
701
702/**
703 * \fn bool cbor_value_is_negative_integer(const CborValue *value)
704 *
705 * Returns true if the iterator \a value is valid and points to a CBOR negative
706 * integer type.
707 *
708 * \sa cbor_value_is_valid(), cbor_value_get_int, cbor_value_get_int64, cbor_value_get_raw_integer
709 */
710
711/**
712 * \fn CborError cbor_value_get_int(const CborValue *value, int *result)
713 *
714 * Retrieves the CBOR integer value that \a value points to and stores it in \a
715 * result. If the iterator \a value does not point to an integer value, the
716 * behavior is undefined, so checking with \ref cbor_value_get_type or with
717 * \ref cbor_value_is_integer is recommended.
718 *
719 * Note that this function does not do range-checking: integral values that do
720 * not fit in a variable of type \c{int} are silently truncated to fit. Use
721 * cbor_value_get_int_checked() that is not acceptable.
722 *
723 * \sa cbor_value_get_type(), cbor_value_is_valid(), cbor_value_is_integer()
724 */
725
726/**
727 * \fn CborError cbor_value_get_int64(const CborValue *value, int64_t *result)
728 *
729 * Retrieves the CBOR integer value that \a value points to and stores it in \a
730 * result. If the iterator \a value does not point to an integer value, the
731 * behavior is undefined, so checking with \ref cbor_value_get_type or with
732 * \ref cbor_value_is_integer is recommended.
733 *
734 * Note that this function does not do range-checking: integral values that do
735 * not fit in a variable of type \c{int64_t} are silently truncated to fit. Use
736 * cbor_value_get_int64_checked() that is not acceptable.
737 *
738 * \sa cbor_value_get_type(), cbor_value_is_valid(), cbor_value_is_integer()
739 */
740
741/**
742 * \fn CborError cbor_value_get_uint64(const CborValue *value, uint64_t *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 unsigned integer
746 * value, the behavior is undefined, so checking with \ref cbor_value_get_type
747 * or with \ref cbor_value_is_unsigned_integer is recommended.
748 *
749 * \sa cbor_value_get_type(), cbor_value_is_valid(), cbor_value_is_unsigned_integer()
750 */
751
752/**
753 * \fn CborError cbor_value_get_raw_integer(const CborValue *value, uint64_t *result)
754 *
755 * Retrieves the CBOR integer value that \a value points to and stores it in \a
756 * result. If the iterator \a value does not point to an integer value, the
757 * behavior is undefined, so checking with \ref cbor_value_get_type or with
758 * \ref cbor_value_is_integer is recommended.
759 *
760 * This function is provided because CBOR negative integers can assume values
761 * that cannot be represented with normal 64-bit integer variables.
762 *
763 * If the integer is unsigned (that is, if cbor_value_is_unsigned_integer()
764 * returns true), then \a result will contain the actual value. If the integer
765 * is negative, then \a result will contain the absolute value of that integer,
766 * minus one. That is, \c {actual = -result - 1}. On architectures using two's
767 * complement for representation of negative integers, it is equivalent to say
768 * that \a result will contain the bitwise negation of the actual value.
769 *
770 * \sa cbor_value_get_type(), cbor_value_is_valid(), cbor_value_is_integer()
771 */
772
773/**
Thiago Macieira0f02e792016-07-07 19:55:08 -0700774 * 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 integer value, the
776 * behavior is undefined, so checking with \ref cbor_value_get_type or with
777 * \ref cbor_value_is_integer is recommended.
778 *
779 * Unlike cbor_value_get_int64(), this function performs a check to see if the
780 * stored integer fits in \a result without data loss. If the number is outside
781 * the valid range for the data type, this function returns the recoverable
782 * error CborErrorDataTooLarge. In that case, use either
783 * cbor_value_get_uint64() (if the number is positive) or
784 * cbor_value_get_raw_integer().
785 *
786 * \sa cbor_value_get_type(), cbor_value_is_valid(), cbor_value_is_integer(), cbor_value_get_int64()
787 */
788CborError cbor_value_get_int64_checked(const CborValue *value, int64_t *result)
789{
Thiago Macieirada8e35e2017-03-05 09:58:01 +0100790 cbor_assert(cbor_value_is_integer(value));
Thiago Macieira0f02e792016-07-07 19:55:08 -0700791 uint64_t v = _cbor_value_extract_int64_helper(value);
792
793 /* Check before converting, as the standard says (C11 6.3.1.3 paragraph 3):
794 * "[if] the new type is signed and the value cannot be represented in it; either the
795 * result is implementation-defined or an implementation-defined signal is raised."
796 *
797 * The range for int64_t is -2^63 to 2^63-1 (int64_t is required to be
798 * two's complement, C11 7.20.1.1 paragraph 3), which in CBOR is
799 * represented the same way, differing only on the "sign bit" (the major
800 * type).
801 */
802
803 if (unlikely(v > (uint64_t)INT64_MAX))
804 return CborErrorDataTooLarge;
805
806 *result = v;
807 if (value->flags & CborIteratorFlag_NegativeInteger)
808 *result = -*result - 1;
809 return CborNoError;
810}
811
812/**
813 * Retrieves the CBOR integer value that \a value points to and stores it in \a
814 * result. If the iterator \a value does not point to an integer value, the
815 * behavior is undefined, so checking with \ref cbor_value_get_type or with
816 * \ref cbor_value_is_integer is recommended.
817 *
818 * Unlike cbor_value_get_int(), this function performs a check to see if the
819 * stored integer fits in \a result without data loss. If the number is outside
820 * the valid range for the data type, this function returns the recoverable
821 * error CborErrorDataTooLarge. In that case, use one of the other integer
822 * functions to obtain the value.
823 *
824 * \sa cbor_value_get_type(), cbor_value_is_valid(), cbor_value_is_integer(), cbor_value_get_int64(),
825 * cbor_value_get_uint64(), cbor_value_get_int64_checked(), cbor_value_get_raw_integer()
826 */
827CborError cbor_value_get_int_checked(const CborValue *value, int *result)
828{
Thiago Macieirada8e35e2017-03-05 09:58:01 +0100829 cbor_assert(cbor_value_is_integer(value));
Thiago Macieira0f02e792016-07-07 19:55:08 -0700830 uint64_t v = _cbor_value_extract_int64_helper(value);
831
832 /* Check before converting, as the standard says (C11 6.3.1.3 paragraph 3):
833 * "[if] the new type is signed and the value cannot be represented in it; either the
834 * result is implementation-defined or an implementation-defined signal is raised."
835 *
836 * But we can convert from signed to unsigned without fault (paragraph 2).
837 *
838 * The range for int is implementation-defined and int is not guaranteed use
839 * two's complement representation (int32_t is).
840 */
841
842 if (value->flags & CborIteratorFlag_NegativeInteger) {
843 if (unlikely(v > (unsigned) -(INT_MIN + 1)))
844 return CborErrorDataTooLarge;
845
alradmsft9ba47912016-10-11 17:56:15 -0700846 *result = (int)v;
Thiago Macieira0f02e792016-07-07 19:55:08 -0700847 *result = -*result - 1;
848 } else {
849 if (unlikely(v > (uint64_t)INT_MAX))
850 return CborErrorDataTooLarge;
851
alradmsft9ba47912016-10-11 17:56:15 -0700852 *result = (int)v;
Thiago Macieira0f02e792016-07-07 19:55:08 -0700853 }
854 return CborNoError;
855
856}
857
858/**
Thiago Macieira46a818e2015-10-08 15:13:05 +0200859 * \fn bool cbor_value_is_length_known(const CborValue *value)
860 *
861 * Returns true if the length of this type is known without calculation. That
862 * is, if the length of this CBOR string, map or array is encoded in the data
863 * stream, this function returns true. If the length is not encoded, it returns
864 * false.
865 *
866 * If the length is known, code can call cbor_value_get_string_length(),
867 * cbor_value_get_array_length() or cbor_value_get_map_length() to obtain the
868 * length. If the length is not known but is necessary, code can use the
869 * cbor_value_calculate_string_length() function (no equivalent function is
870 * provided for maps and arrays).
871 */
872
873/**
874 * \fn bool cbor_value_is_text_string(const CborValue *value)
875 *
876 * Returns true if the iterator \a value is valid and points to a CBOR text
877 * string. CBOR text strings are UTF-8 encoded and usually contain
878 * human-readable text.
879 *
880 * \sa cbor_value_is_valid(), cbor_value_get_string_length(), cbor_value_calculate_string_length(),
881 * cbor_value_copy_text_string(), cbor_value_dup_text_string()
882 */
883
884/**
885 * \fn bool cbor_value_is_byte_string(const CborValue *value)
886 *
887 * Returns true if the iterator \a value is valid and points to a CBOR text
888 * string. CBOR byte strings are binary data with no specified encoding or
889 * format.
890 *
891 * \sa cbor_value_is_valid(), cbor_value_get_string_length(), cbor_value_calculate_string_length(),
892 * cbor_value_copy_byte_string(), cbor_value_dup_byte_string()
893 */
894
895/**
896 * \fn CborError cbor_value_get_string_length(const CborValue *value, size_t *length)
897 *
898 * Extracts the length of the byte or text string that \a value points to and
899 * stores it in \a result. If the iterator \a value does not point to a text
900 * string or a byte string, the behaviour is undefined, so checking with \ref
901 * cbor_value_get_type, with \ref cbor_value_is_text_string or \ref
902 * cbor_value_is_byte_string is recommended.
903 *
904 * If the length of this string is not encoded in the CBOR data stream, this
905 * function will return the recoverable error CborErrorUnknownLength. You may
906 * also check whether that is the case by using cbor_value_is_length_known().
907 *
908 * If the length of the string is required but the length was not encoded, use
909 * cbor_value_calculate_string_length(), but note that that function does not
910 * run in constant time.
911 *
912 * \note On 32-bit platforms, this function will return error condition of \ref
913 * CborErrorDataTooLarge if the stream indicates a length that is too big to
914 * fit in 32-bit.
915 *
916 * \sa cbor_value_is_valid(), cbor_value_is_length_known(), cbor_value_calculate_string_length()
917 */
918
919/**
920 * Calculates the length of the byte or text string that \a value points to and
921 * stores it in \a len. If the iterator \a value does not point to a text
922 * string or a byte string, the behaviour is undefined, so checking with \ref
923 * cbor_value_get_type, with \ref cbor_value_is_text_string or \ref
924 * cbor_value_is_byte_string is recommended.
925 *
926 * This function is different from cbor_value_get_string_length() in that it
927 * calculates the length even for strings sent in chunks. For that reason, this
928 * function may not run in constant time (it will run in O(n) time on the
929 * number of chunks). It does use constant memory (O(1)).
Thiago Macieira2312efd2015-05-06 16:07:48 -0700930 *
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700931 * \note On 32-bit platforms, this function will return error condition of \ref
932 * CborErrorDataTooLarge if the stream indicates a length that is too big to
933 * fit in 32-bit.
Thiago Macieira2312efd2015-05-06 16:07:48 -0700934 *
Thiago Macieira51b56062017-02-23 16:03:13 -0800935 * \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 -0700936 */
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700937CborError cbor_value_calculate_string_length(const CborValue *value, size_t *len)
Thiago Macieira2312efd2015-05-06 16:07:48 -0700938{
Thiago Macieira9ae05812015-05-11 15:09:09 +0900939 *len = SIZE_MAX;
Thiago Macieiraff130bc2015-06-19 15:15:33 -0700940 return _cbor_value_copy_string(value, NULL, len, NULL);
Thiago Macieirac70169f2015-05-06 07:49:44 -0700941}
942
Thiago Macieira57ba7c92017-02-25 22:32:20 -0800943static inline void prepare_string_iteration(CborValue *it)
944{
945 if (!cbor_value_is_length_known(it)) {
946 /* chunked string: we're before the first chunk;
947 * advance to the first chunk */
948 ++it->ptr;
949 it->flags |= CborIteratorFlag_IteratingStringChunks;
950 }
951}
952
953CBOR_INTERNAL_API_CC CborError _cbor_value_prepare_string_iteration(CborValue *it)
954{
955 cbor_assert((it->flags & CborIteratorFlag_IteratingStringChunks) == 0);
956 prepare_string_iteration(it);
957
958 /* are we at the end? */
959 if (it->ptr == it->parser->end)
960 return CborErrorUnexpectedEOF;
961 return CborNoError;
962}
963
Thiago Macieira51b56062017-02-23 16:03:13 -0800964static CborError get_string_chunk(CborValue *it, const void **bufferptr, size_t *len)
965{
966 CborError err;
967
968 /* Possible states:
969 * length known | iterating | meaning
970 * no | no | before the first chunk of a chunked string
971 * yes | no | at a non-chunked string
972 * no | yes | second or later chunk
973 * yes | yes | after a non-chunked string
974 */
975 if (it->flags & CborIteratorFlag_IteratingStringChunks) {
976 /* already iterating */
977 if (cbor_value_is_length_known(it)) {
978 /* if the length was known, it wasn't chunked, so finish iteration */
979 goto last_chunk;
980 }
Thiago Macieira57ba7c92017-02-25 22:32:20 -0800981 } else {
982 prepare_string_iteration(it);
Thiago Macieira51b56062017-02-23 16:03:13 -0800983 }
984
985 /* are we at the end? */
986 if (it->ptr == it->parser->end)
987 return CborErrorUnexpectedEOF;
988
989 if (*it->ptr == BreakByte) {
990 /* last chunk */
991 ++it->ptr;
992last_chunk:
993 *bufferptr = NULL;
994 return preparse_next_value(it);
995 } else if ((uint8_t)(*it->ptr & MajorTypeMask) == it->type) {
996 err = extract_length(it->parser, &it->ptr, len);
997 if (err)
998 return err;
999 if (*len > (size_t)(it->parser->end - it->ptr))
1000 return CborErrorUnexpectedEOF;
1001
1002 *bufferptr = it->ptr;
1003 it->ptr += *len;
1004 } else {
1005 return CborErrorIllegalType;
1006 }
1007
1008 it->flags |= CborIteratorFlag_IteratingStringChunks;
1009 return CborNoError;
1010}
1011
1012/**
1013 * \fn CborError cbor_value_get_text_string_chunk(const CborValue *value, const char **bufferptr, size_t *len, CborValue *next)
1014 *
1015 * Extracts one text string chunk pointed to by \a value and stores a pointer
1016 * to the data in \a buffer and the size in \a len, which must not be null. If
1017 * no more chunks are available, then \a bufferptr will be set to null. This
1018 * function may be used to iterate over any string without causing its contents
1019 * to be copied to a separate buffer, like the convenience function
1020 * cbor_value_copy_text_string() does.
1021 *
1022 * It is designed to be used in code like:
1023 *
1024 * \code
1025 * if (cbor_value_is_text_string(value)) {
1026 * char *ptr;
1027 * size_t len;
1028 * while (1) {
1029 * err = cbor_value_get_text_string_chunk(value, &ptr, &len, &value));
1030 * if (err) return err;
1031 * if (ptr == NULL) return CborNoError;
1032 * consume(ptr, len);
1033 * }
1034 * }
1035 * \endcode
1036 *
1037 * If the iterator \a value does not point to a text string, the behaviour is
1038 * undefined, so checking with \ref cbor_value_get_type or \ref
1039 * cbor_value_is_text_string is recommended.
1040 *
1041 * The \a next pointer, if not null, will be updated to point to the next item
1042 * after this string. During iteration, the pointer must only be passed back
1043 * again to this function; passing it to any other function in this library
1044 * results in undefined behavior. If there are no more chunks to be read from
1045 * \a value, then \a next will be set to the next item after this string; if \a
1046 * value points to the last item, then \a next will be invalid.
1047 *
1048 * \note This function does not perform UTF-8 validation on the incoming text
1049 * string.
1050 *
1051 * \sa cbor_value_dup_text_string(), cbor_value_copy_text_string(), cbor_value_caculate_string_length(), cbor_value_get_byte_string_chunk()
1052 */
1053
1054/**
1055 * \fn CborError cbor_value_get_byte_string_chunk(const CborValue *value, const char **bufferptr, size_t *len, CborValue *next)
1056 *
1057 * Extracts one byte string chunk pointed to by \a value and stores a pointer
1058 * to the data in \a buffer and the size in \a len, which must not be null. If
1059 * no more chunks are available, then \a bufferptr will be set to null. This
1060 * function may be used to iterate over any string without causing its contents
1061 * to be copied to a separate buffer, like the convenience function
1062 * cbor_value_copy_byte_string() does.
1063 *
1064 * It is designed to be used in code like:
1065 *
1066 * \code
1067 * if (cbor_value_is_byte_string(value)) {
1068 * char *ptr;
1069 * size_t len;
1070 * while (1) {
1071 * err = cbor_value_get_byte_string_chunk(value, &ptr, &len, &value));
1072 * if (err) return err;
1073 * if (ptr == NULL) return CborNoError;
1074 * consume(ptr, len);
1075 * }
1076 * }
1077 * \endcode
1078 *
1079 * If the iterator \a value does not point to a byte string, the behaviour is
1080 * undefined, so checking with \ref cbor_value_get_type or \ref
1081 * cbor_value_is_byte_string is recommended.
1082 *
1083 * The \a next pointer, if not null, will be updated to point to the next item
1084 * after this string. During iteration, the pointer must only be passed back
1085 * again to this function; passing it to any other function in this library
1086 * results in undefined behavior. If there are no more chunks to be read from
1087 * \a value, then \a next will be set to the next item after this string; if \a
1088 * value points to the last item, then \a next will be invalid.
1089 *
1090 * \sa cbor_value_dup_byte_string(), cbor_value_copy_byte_string(), cbor_value_caculate_string_length(), cbor_value_get_text_string_chunk()
1091 */
1092
1093CborError _cbor_value_get_string_chunk(const CborValue *value, const void **bufferptr,
1094 size_t *len, CborValue *next)
1095{
1096 CborValue tmp;
1097 if (!next)
1098 next = &tmp;
1099 *next = *value;
1100 return get_string_chunk(next, bufferptr, len);
1101}
1102
Thiago Macieiradbc01292016-06-06 17:02:25 -07001103/* We return uintptr_t so that we can pass memcpy directly as the iteration
1104 * function. The choice is to optimize for memcpy, which is used in the base
1105 * parser API (cbor_value_copy_string), while memcmp is used in convenience API
1106 * only. */
Thiago Macieira5752ce52015-06-16 12:10:03 -07001107typedef uintptr_t (*IterateFunction)(char *, const uint8_t *, size_t);
Thiago Macieira9ae05812015-05-11 15:09:09 +09001108
Thiago Macieira5752ce52015-06-16 12:10:03 -07001109static uintptr_t iterate_noop(char *dest, const uint8_t *src, size_t len)
Thiago Macieira9ae05812015-05-11 15:09:09 +09001110{
1111 (void)dest;
1112 (void)src;
1113 (void)len;
1114 return true;
1115}
1116
Thiago Macieira5752ce52015-06-16 12:10:03 -07001117static uintptr_t iterate_memcmp(char *s1, const uint8_t *s2, size_t len)
Thiago Macieirac4a73c62015-05-09 18:14:11 -07001118{
Thiago Macieira5752ce52015-06-16 12:10:03 -07001119 return memcmp(s1, (const char *)s2, len) == 0;
Thiago Macieirac4a73c62015-05-09 18:14:11 -07001120}
1121
Thiago Macieira9ae05812015-05-11 15:09:09 +09001122static CborError iterate_string_chunks(const CborValue *value, char *buffer, size_t *buflen,
1123 bool *result, CborValue *next, IterateFunction func)
1124{
Thiago Macieirada8e35e2017-03-05 09:58:01 +01001125 cbor_assert(cbor_value_is_byte_string(value) || cbor_value_is_text_string(value));
Thiago Macieira9ae05812015-05-11 15:09:09 +09001126
Thiago Macieira9ae05812015-05-11 15:09:09 +09001127 CborError err;
Thiago Macieira2dcf42f2017-02-24 21:17:51 -08001128 CborValue tmp;
1129 size_t total = 0;
1130 const void *ptr;
1131
1132 if (!next)
1133 next = &tmp;
1134 *next = *value;
1135 *result = true;
1136
1137 while (1) {
1138 size_t newTotal;
1139 size_t chunkLen;
1140 err = get_string_chunk(next, &ptr, &chunkLen);
Thiago Macieira9ae05812015-05-11 15:09:09 +09001141 if (err)
1142 return err;
Thiago Macieira2dcf42f2017-02-24 21:17:51 -08001143 if (!ptr)
1144 break;
1145
1146 if (unlikely(add_check_overflow(total, chunkLen, &newTotal)))
1147 return CborErrorDataTooLarge;
1148
1149 if (*result && *buflen >= newTotal)
1150 *result = !!func(buffer + total, (const uint8_t *)ptr, chunkLen);
Thiago Macieira9ae05812015-05-11 15:09:09 +09001151 else
1152 *result = false;
Thiago Macieira9ae05812015-05-11 15:09:09 +09001153
Thiago Macieira2dcf42f2017-02-24 21:17:51 -08001154 total = newTotal;
Thiago Macieira9ae05812015-05-11 15:09:09 +09001155 }
1156
Thiago Macieiradbc01292016-06-06 17:02:25 -07001157 /* is there enough room for the ending NUL byte? */
Thiago Macieirae136feb2017-02-24 21:21:04 -08001158 if (*result && *buflen > total) {
1159 uint8_t nul[] = { 0 };
1160 *result = !!func(buffer + total, nul, 1);
1161 }
Thiago Macieira9ae05812015-05-11 15:09:09 +09001162 *buflen = total;
Thiago Macieira9ae05812015-05-11 15:09:09 +09001163 return CborNoError;
1164}
1165
Thiago Macieira2312efd2015-05-06 16:07:48 -07001166/**
Thiago Macieiraff130bc2015-06-19 15:15:33 -07001167 * \fn CborError cbor_value_copy_text_string(const CborValue *value, char *buffer, size_t *buflen, CborValue *next)
1168 *
Thiago Macieira2312efd2015-05-06 16:07:48 -07001169 * Copies the string pointed by \a value into the buffer provided at \a buffer
1170 * of \a buflen bytes. If \a buffer is a NULL pointer, this function will not
1171 * copy anything and will only update the \a next value.
1172 *
Thiago Macieira46a818e2015-10-08 15:13:05 +02001173 * If the iterator \a value does not point to a text string, the behaviour is
1174 * undefined, so checking with \ref cbor_value_get_type or \ref
1175 * cbor_value_is_text_string is recommended.
1176 *
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -07001177 * If the provided buffer length was too small, this function returns an error
1178 * condition of \ref CborErrorOutOfMemory. If you need to calculate the length
1179 * of the string in order to preallocate a buffer, use
Thiago Macieira2312efd2015-05-06 16:07:48 -07001180 * cbor_value_calculate_string_length().
1181 *
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -07001182 * On success, this function sets the number of bytes copied to \c{*buflen}. If
1183 * the buffer is large enough, this function will insert a null byte after the
1184 * last copied byte, to facilitate manipulation of text strings. That byte is
1185 * not included in the returned value of \c{*buflen}.
Thiago Macieira2312efd2015-05-06 16:07:48 -07001186 *
1187 * The \a next pointer, if not null, will be updated to point to the next item
1188 * after this string. If \a value points to the last item, then \a next will be
1189 * invalid.
1190 *
Thiago Macieira46a818e2015-10-08 15:13:05 +02001191 * This function may not run in constant time (it will run in O(n) time on the
1192 * number of chunks). It requires constant memory (O(1)).
1193 *
Thiago Macieira2312efd2015-05-06 16:07:48 -07001194 * \note This function does not perform UTF-8 validation on the incoming text
1195 * string.
1196 *
Thiago Macieira51b56062017-02-23 16:03:13 -08001197 * \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 -07001198 */
Thiago Macieiraff130bc2015-06-19 15:15:33 -07001199
1200/**
1201 * \fn CborError cbor_value_copy_byte_string(const CborValue *value, uint8_t *buffer, size_t *buflen, CborValue *next)
1202 *
1203 * Copies the string pointed by \a value into the buffer provided at \a buffer
1204 * of \a buflen bytes. If \a buffer is a NULL pointer, this function will not
1205 * copy anything and will only update the \a next value.
1206 *
Thiago Macieira46a818e2015-10-08 15:13:05 +02001207 * If the iterator \a value does not point to a byte string, the behaviour is
1208 * undefined, so checking with \ref cbor_value_get_type or \ref
1209 * cbor_value_is_byte_string is recommended.
1210 *
Thiago Macieiraff130bc2015-06-19 15:15:33 -07001211 * If the provided buffer length was too small, this function returns an error
1212 * condition of \ref CborErrorOutOfMemory. If you need to calculate the length
1213 * of the string in order to preallocate a buffer, use
1214 * cbor_value_calculate_string_length().
1215 *
1216 * On success, this function sets the number of bytes copied to \c{*buflen}. If
1217 * the buffer is large enough, this function will insert a null byte after the
1218 * last copied byte, to facilitate manipulation of null-terminated strings.
1219 * That byte is not included in the returned value of \c{*buflen}.
1220 *
1221 * The \a next pointer, if not null, will be updated to point to the next item
1222 * after this string. If \a value points to the last item, then \a next will be
1223 * invalid.
1224 *
Thiago Macieira46a818e2015-10-08 15:13:05 +02001225 * This function may not run in constant time (it will run in O(n) time on the
1226 * number of chunks). It requires constant memory (O(1)).
1227 *
Thiago Macieira51b56062017-02-23 16:03:13 -08001228 * \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 -07001229 */
1230
1231CborError _cbor_value_copy_string(const CborValue *value, void *buffer,
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -07001232 size_t *buflen, CborValue *next)
Thiago Macieira2312efd2015-05-06 16:07:48 -07001233{
Thiago Macieira9ae05812015-05-11 15:09:09 +09001234 bool copied_all;
Thiago Macieiraed5b57c2015-07-07 16:38:27 -07001235 CborError err = iterate_string_chunks(value, (char*)buffer, buflen, &copied_all, next,
Thiago Macieira9ae05812015-05-11 15:09:09 +09001236 buffer ? (IterateFunction)memcpy : iterate_noop);
1237 return err ? err :
1238 copied_all ? CborNoError : CborErrorOutOfMemory;
Thiago Macieirac70169f2015-05-06 07:49:44 -07001239}
1240
Thiago Macieirac4a73c62015-05-09 18:14:11 -07001241/**
1242 * Compares the entry \a value with the string \a string and store the result
Thiago Macieira46a818e2015-10-08 15:13:05 +02001243 * in \a result. If the value is different from \a string \a result will
1244 * contain \c false.
Thiago Macieirac4a73c62015-05-09 18:14:11 -07001245 *
1246 * The entry at \a value may be a tagged string. If \a is not a string or a
1247 * tagged string, the comparison result will be false.
Thiago Macieira46a818e2015-10-08 15:13:05 +02001248 *
1249 * CBOR requires text strings to be encoded in UTF-8, but this function does
1250 * not validate either the strings in the stream or the string \a string to be
1251 * matched. Moreover, comparison is done on strict codepoint comparison,
1252 * without any Unicode normalization.
1253 *
1254 * This function may not run in constant time (it will run in O(n) time on the
1255 * number of chunks). It requires constant memory (O(1)).
1256 *
1257 * \sa cbor_value_skip_tag(), cbor_value_copy_text_string()
Thiago Macieirac4a73c62015-05-09 18:14:11 -07001258 */
1259CborError cbor_value_text_string_equals(const CborValue *value, const char *string, bool *result)
1260{
1261 CborValue copy = *value;
1262 CborError err = cbor_value_skip_tag(&copy);
1263 if (err)
1264 return err;
1265 if (!cbor_value_is_text_string(&copy)) {
1266 *result = false;
1267 return CborNoError;
1268 }
1269
1270 size_t len = strlen(string);
1271 return iterate_string_chunks(&copy, CONST_CAST(char *, string), &len, result, NULL, iterate_memcmp);
1272}
1273
1274/**
Thiago Macieira46a818e2015-10-08 15:13:05 +02001275 * \fn bool cbor_value_is_array(const CborValue *value)
Thiago Macieira7b623c22015-05-11 15:52:14 +09001276 *
Thiago Macieira46a818e2015-10-08 15:13:05 +02001277 * Returns true if the iterator \a value is valid and points to a CBOR array.
1278 *
1279 * \sa cbor_value_is_valid(), cbor_value_is_map()
1280 */
1281
1282/**
1283 * \fn CborError cbor_value_get_array_length(const CborValue *value, size_t *length)
1284 *
1285 * Extracts the length of the CBOR array that \a value points to and stores it
1286 * in \a result. If the iterator \a value does not point to a CBOR array, the
1287 * behaviour is undefined, so checking with \ref cbor_value_get_type or \ref
1288 * cbor_value_is_array is recommended.
1289 *
1290 * If the length of this array is not encoded in the CBOR data stream, this
1291 * function will return the recoverable error CborErrorUnknownLength. You may
1292 * also check whether that is the case by using cbor_value_is_length_known().
1293 *
1294 * \note On 32-bit platforms, this function will return error condition of \ref
1295 * CborErrorDataTooLarge if the stream indicates a length that is too big to
1296 * fit in 32-bit.
1297 *
1298 * \sa cbor_value_is_valid(), cbor_value_is_length_known()
1299 */
1300
1301/**
1302 * \fn bool cbor_value_is_map(const CborValue *value)
1303 *
1304 * Returns true if the iterator \a value is valid and points to a CBOR map.
1305 *
1306 * \sa cbor_value_is_valid(), cbor_value_is_array()
1307 */
1308
1309/**
1310 * \fn CborError cbor_value_get_map_length(const CborValue *value, size_t *length)
1311 *
1312 * Extracts the length of the CBOR map that \a value points to and stores it in
1313 * \a result. If the iterator \a value does not point to a CBOR map, the
1314 * behaviour is undefined, so checking with \ref cbor_value_get_type or \ref
1315 * cbor_value_is_map is recommended.
1316 *
1317 * If the length of this map is not encoded in the CBOR data stream, this
1318 * function will return the recoverable error CborErrorUnknownLength. You may
1319 * also check whether that is the case by using cbor_value_is_length_known().
1320 *
1321 * \note On 32-bit platforms, this function will return error condition of \ref
1322 * CborErrorDataTooLarge if the stream indicates a length that is too big to
1323 * fit in 32-bit.
1324 *
1325 * \sa cbor_value_is_valid(), cbor_value_is_length_known()
1326 */
1327
1328/**
1329 * Attempts to find the value in map \a map that corresponds to the text string
1330 * entry \a string. If the iterator \a value does not point to a CBOR map, the
1331 * behaviour is undefined, so checking with \ref cbor_value_get_type or \ref
1332 * cbor_value_is_map is recommended.
1333 *
1334 * If the item is found, it is stored in \a result. If no item is found
1335 * matching the key, then \a result will contain an element of type \ref
1336 * CborInvalidType. Matching is performed using
1337 * cbor_value_text_string_equals(), so tagged strings will also match.
1338 *
1339 * This function has a time complexity of O(n) where n is the number of
1340 * elements in the map to be searched. In addition, this function is has O(n)
1341 * memory requirement based on the number of nested containers (maps or arrays)
1342 * found as elements of this map.
1343 *
1344 * \sa cbor_value_is_valid(), cbor_value_text_string_equals(), cbor_value_advance()
Thiago Macieira7b623c22015-05-11 15:52:14 +09001345 */
1346CborError cbor_value_map_find_value(const CborValue *map, const char *string, CborValue *element)
1347{
Thiago Macieirada8e35e2017-03-05 09:58:01 +01001348 cbor_assert(cbor_value_is_map(map));
Thiago Macieira7b623c22015-05-11 15:52:14 +09001349 size_t len = strlen(string);
1350 CborError err = cbor_value_enter_container(map, element);
1351 if (err)
1352 goto error;
1353
1354 while (!cbor_value_at_end(element)) {
Thiago Macieiradbc01292016-06-06 17:02:25 -07001355 /* find the non-tag so we can compare */
Thiago Macieira7b623c22015-05-11 15:52:14 +09001356 err = cbor_value_skip_tag(element);
1357 if (err)
1358 goto error;
1359 if (cbor_value_is_text_string(element)) {
1360 bool equals;
1361 size_t dummyLen = len;
1362 err = iterate_string_chunks(element, CONST_CAST(char *, string), &dummyLen,
1363 &equals, element, iterate_memcmp);
1364 if (err)
1365 goto error;
1366 if (equals)
1367 return preparse_value(element);
1368 } else {
Thiago Macieiradbc01292016-06-06 17:02:25 -07001369 /* skip this key */
Thiago Macieira7b623c22015-05-11 15:52:14 +09001370 err = cbor_value_advance(element);
1371 if (err)
1372 goto error;
1373 }
1374
Thiago Macieiradbc01292016-06-06 17:02:25 -07001375 /* skip this value */
Thiago Macieira7b623c22015-05-11 15:52:14 +09001376 err = cbor_value_skip_tag(element);
1377 if (err)
1378 goto error;
1379 err = cbor_value_advance(element);
1380 if (err)
1381 goto error;
1382 }
1383
Thiago Macieiradbc01292016-06-06 17:02:25 -07001384 /* not found */
Thiago Macieira7b623c22015-05-11 15:52:14 +09001385 element->type = CborInvalidType;
1386 return CborNoError;
1387
1388error:
1389 element->type = CborInvalidType;
1390 return err;
1391}
1392
1393/**
Thiago Macieira46a818e2015-10-08 15:13:05 +02001394 * \fn bool cbor_value_is_float(const CborValue *value)
1395 *
1396 * Returns true if the iterator \a value is valid and points to a CBOR
1397 * single-precision floating point (32-bit).
1398 *
1399 * \sa cbor_value_is_valid(), cbor_value_is_double(), cbor_value_is_half_float()
1400 */
1401
1402/**
1403 * \fn CborError cbor_value_get_float(const CborValue *value, float *result)
1404 *
1405 * Retrieves the CBOR single-precision floating point (32-bit) value that \a
1406 * value points to and stores it in \a result. If the iterator \a value does
1407 * not point to a single-precision floating point value, the behavior is
1408 * undefined, so checking with \ref cbor_value_get_type or with \ref
1409 * cbor_value_is_float is recommended.
1410 *
1411 * \sa cbor_value_get_type(), cbor_value_is_valid(), cbor_value_is_float(), cbor_value_get_double()
1412 */
1413
1414/**
1415 * \fn bool cbor_value_is_double(const CborValue *value)
1416 *
1417 * Returns true if the iterator \a value is valid and points to a CBOR
1418 * double-precision floating point (64-bit).
1419 *
1420 * \sa cbor_value_is_valid(), cbor_value_is_float(), cbor_value_is_half_float()
1421 */
1422
1423/**
1424 * \fn CborError cbor_value_get_double(const CborValue *value, float *result)
1425 *
1426 * Retrieves the CBOR double-precision floating point (64-bit) value that \a
1427 * value points to and stores it in \a result. If the iterator \a value does
1428 * not point to a double-precision floating point value, the behavior is
1429 * undefined, so checking with \ref cbor_value_get_type or with \ref
1430 * cbor_value_is_double is recommended.
1431 *
1432 * \sa cbor_value_get_type(), cbor_value_is_valid(), cbor_value_is_double(), cbor_value_get_float()
1433 */
1434
1435/**
1436 * \fn bool cbor_value_is_half_float(const CborValue *value)
1437 *
1438 * Returns true if the iterator \a value is valid and points to a CBOR
1439 * single-precision floating point (16-bit).
1440 *
1441 * \sa cbor_value_is_valid(), cbor_value_is_double(), cbor_value_is_float()
1442 */
1443
1444/**
1445 * Retrieves the CBOR half-precision floating point (16-bit) value that \a
1446 * value points to and stores it in \a result. If the iterator \a value does
1447 * not point to a half-precision floating point value, the behavior is
1448 * undefined, so checking with \ref cbor_value_get_type or with \ref
1449 * cbor_value_is_half_float is recommended.
1450 *
1451 * Note: since the C language does not have a standard type for half-precision
1452 * floating point, this function takes a \c{void *} as a parameter for the
1453 * storage area, which must be at least 16 bits wide.
1454 *
1455 * \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 -07001456 */
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -07001457CborError cbor_value_get_half_float(const CborValue *value, void *result)
Thiago Macieirac70169f2015-05-06 07:49:44 -07001458{
Thiago Macieirada8e35e2017-03-05 09:58:01 +01001459 cbor_assert(cbor_value_is_half_float(value));
Thiago Macieirac70169f2015-05-06 07:49:44 -07001460
Thiago Macieiradbc01292016-06-06 17:02:25 -07001461 /* size has been computed already */
Thiago Macieirac70169f2015-05-06 07:49:44 -07001462 uint16_t v = get16(value->ptr + 1);
1463 memcpy(result, &v, sizeof(v));
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -07001464 return CborNoError;
Thiago Macieira54a0e102015-05-05 21:25:06 -07001465}
Thiago Macieira46a818e2015-10-08 15:13:05 +02001466
1467/** @} */