blob: 95d49fe31366bff627114d83299ab4741d6b1b4f [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"
32#include "cborconstants_p.h"
33#include "compilersupport_p.h"
Thiago Macieira4e9626c2015-09-21 14:57:17 -070034#include "extract_number_p.h"
Thiago Macieira54a0e102015-05-05 21:25:06 -070035
36#include <assert.h>
Thiago Macieira54a0e102015-05-05 21:25:06 -070037#include <string.h>
38
Thiago Macieira8f3fb782015-06-16 16:27:01 -070039#include "assert_p.h" /* Always include last */
40
Thiago Macieira4a99af92015-05-12 10:41:45 +090041#ifndef CBOR_PARSER_MAX_RECURSIONS
42# define CBOR_PARSER_MAX_RECURSIONS 1024
43#endif
44
Thiago Macieira54a0e102015-05-05 21:25:06 -070045/**
Thiago Macieira46a818e2015-10-08 15:13:05 +020046 * \defgroup CborParsing Parsing CBOR streams
47 * \brief Group of functions used to parse CBOR streams.
Thiago Macieira54a0e102015-05-05 21:25:06 -070048 *
Thiago Macieira46a818e2015-10-08 15:13:05 +020049 * TinyCBOR provides functions for pull-based stream parsing of a CBOR-encoded
50 * payload. The main data type for the parsing is a CborValue, which behaves
51 * like an iterator and can be used to extract the encoded data. It is first
52 * initialized with a call to cbor_parser_init() and is usually used to extract
53 * exactly one item, most often an array or map.
Thiago Macieira54a0e102015-05-05 21:25:06 -070054 *
Thiago Macieira46a818e2015-10-08 15:13:05 +020055 * Nested CborValue objects can be parsed using cbor_value_enter_container().
56 * Each call to cbor_value_enter_container() must be matched by a call to
57 * cbor_value_leave_container(), with the exact same parameters.
Thiago Macieira54a0e102015-05-05 21:25:06 -070058 *
Thiago Macieira46a818e2015-10-08 15:13:05 +020059 * The example below initializes a CborParser object, begins the parsing with a
60 * CborValue and decodes a single integer:
61 *
62 * \code
63 * int extract_int(const uint8_t *buffer, size_t len)
64 * {
65 * CborParser parser;
66 * CborValue value;
67 * int result;
68 * cbor_parser_init(buffer, len, 0, &buffer, &value);
69 * cbor_value_get_int(&value, &result);
70 * return result;
71 * }
72 * \endcode
73 *
74 * The code above does no error checking, which means it assumes the data comes
75 * from a source trusted to send one properly-encoded integer. The following
76 * example does the exact same operation, but includes error parsing and
77 * returns 0 on parsing failure:
78 *
79 * \code
80 * int extract_int(const uint8_t *buffer, size_t len)
81 * {
82 * CborParser parser;
83 * CborValue value;
84 * int result;
85 * if (cbor_parser_init(buffer, len, 0, &buffer, &value) != CborNoError)
86 * return 0;
87 * if (!cbor_value_is_integer(&value) ||
88 * cbor_value_get_int(&value, &result) != CborNoError)
89 * return 0;
90 * return result;
91 * }
92 * \endcode
93 *
94 * Note, in the example above, that one can't distinguish a parsing failure
95 * from an encoded value of zero. Reporting a parsing error is left as an
96 * exercise to the reader.
97 *
98 * The code above does not execute a range-check either: it is possible that
99 * the value decoded from the CBOR stream encodes a number larger than what can
100 * be represented in a variable of type \c{int}. If detecting that case is
101 * important, the code should call cbor_value_get_int_checked() instead.
102 *
103 * <h3 class="groupheader">Memory and parsing constraints</h3>
104 *
105 * TinyCBOR is designed to run with little memory and with minimal overhead.
106 * Except where otherwise noted, the parser functions always run on constant
107 * time (O(1)), do not recurse and never allocate memory (thus, stack usage is
108 * bounded and is O(1)).
109 *
110 * <h3 class="groupheader">Error handling and preconditions</h3>
111 *
112 * All functions operating on a CborValue return a CborError condition, with
113 * CborNoError standing for the normal situation in which no parsing error
114 * occurred. All functions may return parsing errors in case the stream cannot
115 * be decoded properly, be it due to corrupted data or due to reaching the end
116 * of the input buffer.
117 *
118 * Error conditions must not be ignored. All decoder functions have undefined
119 * behavior if called after an error has been reported, and may crash.
120 *
121 * Some functions are also documented to have preconditions, like
122 * cbor_value_get_int() requiring that the input be an integral value.
123 * Violation of preconditions also results in undefined behavior and the
124 * program may crash.
125 */
126
127/**
128 * \addtogroup CborParsing
129 * @{
130 */
131
132/**
133 * \struct CborValue
134 *
135 * This type contains one value parsed from the CBOR stream. Each CborValue
136 * behaves as an iterator in a StAX-style parser.
137 *
138 * \if privatedocs
Thiago Macieira54a0e102015-05-05 21:25:06 -0700139 * Implementation details: the CborValue contains these fields:
140 * \list
141 * \li ptr: pointer to the actual data
142 * \li flags: flags from the decoder
Thiago Macieira2312efd2015-05-06 16:07:48 -0700143 * \li extra: partially decoded integer value (0, 1 or 2 bytes)
Thiago Macieira54a0e102015-05-05 21:25:06 -0700144 * \li remaining: remaining items in this collection after this item or UINT32_MAX if length is unknown
145 * \endlist
Thiago Macieira46a818e2015-10-08 15:13:05 +0200146 * \endif
Thiago Macieira54a0e102015-05-05 21:25:06 -0700147 */
148
Thiago Macieiraf5cb94b2015-06-16 16:10:49 -0700149static CborError extract_length(const CborParser *parser, const uint8_t **ptr, size_t *len)
Thiago Macieira54a0e102015-05-05 21:25:06 -0700150{
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700151 uint64_t v;
Thiago Macieira4e9626c2015-09-21 14:57:17 -0700152 CborError err = extract_number(ptr, parser->end, &v);
Mike Colagrosso629d5b72016-02-24 15:12:34 -0700153 if (err) {
154 *len = 0;
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700155 return err;
Mike Colagrosso629d5b72016-02-24 15:12:34 -0700156 }
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700157
alradmsft9ba47912016-10-11 17:56:15 -0700158 *len = (size_t)v;
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700159 if (v != *len)
160 return CborErrorDataTooLarge;
161 return CborNoError;
162}
163
164static bool is_fixed_type(uint8_t type)
165{
166 return type != CborTextStringType && type != CborByteStringType && type != CborArrayType &&
167 type != CborMapType;
168}
169
170static CborError preparse_value(CborValue *it)
171{
172 const CborParser *parser = it->parser;
Thiago Macieira11e913f2015-05-07 13:01:18 -0700173 it->type = CborInvalidType;
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700174
Thiago Macieiradbc01292016-06-06 17:02:25 -0700175 /* are we at the end? */
Thiago Macieira54a0e102015-05-05 21:25:06 -0700176 if (it->ptr == parser->end)
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700177 return CborErrorUnexpectedEOF;
Thiago Macieira54a0e102015-05-05 21:25:06 -0700178
179 uint8_t descriptor = *it->ptr;
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700180 uint8_t type = descriptor & MajorTypeMask;
Thiago Macieira851c4812015-05-08 15:23:20 -0700181 it->type = type;
Thiago Macieira54a0e102015-05-05 21:25:06 -0700182 it->flags = 0;
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700183 it->extra = (descriptor &= SmallValueMask);
184
Thiago Macieira56d99832015-05-07 14:34:27 -0700185 if (descriptor > Value64Bit) {
186 if (unlikely(descriptor != IndefiniteLength))
Thiago Macieira3f76f632015-05-12 10:10:09 +0900187 return type == CborSimpleType ? CborErrorUnknownType : CborErrorIllegalNumber;
Thiago Macieira56d99832015-05-07 14:34:27 -0700188 if (likely(!is_fixed_type(type))) {
Thiago Macieiradbc01292016-06-06 17:02:25 -0700189 /* special case */
Thiago Macieira56d99832015-05-07 14:34:27 -0700190 it->flags |= CborIteratorFlag_UnknownLength;
191 it->type = type;
192 return CborNoError;
193 }
194 return type == CborSimpleType ? CborErrorUnexpectedBreak : CborErrorIllegalNumber;
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700195 }
Thiago Macieira54a0e102015-05-05 21:25:06 -0700196
Thiago Macieirac70169f2015-05-06 07:49:44 -0700197 size_t bytesNeeded = descriptor < Value8Bit ? 0 : (1 << (descriptor - Value8Bit));
Thiago Macieira63abed92015-10-28 17:01:14 -0700198 if (bytesNeeded + 1 > (size_t)(parser->end - it->ptr))
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700199 return CborErrorUnexpectedEOF;
Thiago Macieirac70169f2015-05-06 07:49:44 -0700200
Thiago Macieira851c4812015-05-08 15:23:20 -0700201 uint8_t majortype = type >> MajorTypeShift;
202 if (majortype == NegativeIntegerType) {
Thiago Macieira54a0e102015-05-05 21:25:06 -0700203 it->flags |= CborIteratorFlag_NegativeInteger;
Thiago Macieira851c4812015-05-08 15:23:20 -0700204 it->type = CborIntegerType;
205 } else if (majortype == SimpleTypesType) {
Thiago Macieira54a0e102015-05-05 21:25:06 -0700206 switch (descriptor) {
207 case FalseValue:
208 it->extra = false;
Thiago Macieira851c4812015-05-08 15:23:20 -0700209 it->type = CborBooleanType;
Thiago Macieira991dd922015-05-07 11:57:59 -0700210 break;
211
Thiago Macieira851c4812015-05-08 15:23:20 -0700212 case SinglePrecisionFloat:
213 case DoublePrecisionFloat:
214 it->flags |= CborIteratorFlag_IntegerValueTooLarge;
Thiago Macieiradbc01292016-06-06 17:02:25 -0700215 /* fall through */
Thiago Macieira54a0e102015-05-05 21:25:06 -0700216 case TrueValue:
217 case NullValue:
218 case UndefinedValue:
219 case HalfPrecisionFloat:
Thiago Macieira851c4812015-05-08 15:23:20 -0700220 it->type = *it->ptr;
Thiago Macieira54a0e102015-05-05 21:25:06 -0700221 break;
222
223 case SimpleTypeInNextByte:
Thiago Macieira851c4812015-05-08 15:23:20 -0700224 it->extra = (uint8_t)it->ptr[1];
Thiago Macieira54a0e102015-05-05 21:25:06 -0700225#ifndef CBOR_PARSER_NO_STRICT_CHECKS
Thiago Macieira851c4812015-05-08 15:23:20 -0700226 if (unlikely(it->extra < 32)) {
227 it->type = CborInvalidType;
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700228 return CborErrorIllegalSimpleType;
Thiago Macieira851c4812015-05-08 15:23:20 -0700229 }
Thiago Macieira54a0e102015-05-05 21:25:06 -0700230#endif
Thiago Macieira991dd922015-05-07 11:57:59 -0700231 break;
232
Thiago Macieira54a0e102015-05-05 21:25:06 -0700233 case 28:
234 case 29:
235 case 30:
Thiago Macieira54a0e102015-05-05 21:25:06 -0700236 case Break:
Thiago Macieiradbc01292016-06-06 17:02:25 -0700237 assert(false); /* these conditions can't be reached */
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700238 return CborErrorUnexpectedBreak;
Thiago Macieira54a0e102015-05-05 21:25:06 -0700239 }
Thiago Macieira851c4812015-05-08 15:23:20 -0700240 return CborNoError;
Thiago Macieira54a0e102015-05-05 21:25:06 -0700241 }
242
Thiago Macieiradbc01292016-06-06 17:02:25 -0700243 /* try to decode up to 16 bits */
Thiago Macieira54a0e102015-05-05 21:25:06 -0700244 if (descriptor < Value8Bit)
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700245 return CborNoError;
Thiago Macieira54a0e102015-05-05 21:25:06 -0700246
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700247 if (descriptor == Value8Bit)
248 it->extra = (uint8_t)it->ptr[1];
249 else if (descriptor == Value16Bit)
Thiago Macieira54a0e102015-05-05 21:25:06 -0700250 it->extra = get16(it->ptr + 1);
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700251 else
Thiago Macieiradbc01292016-06-06 17:02:25 -0700252 it->flags |= CborIteratorFlag_IntegerValueTooLarge; /* Value32Bit or Value64Bit */
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700253 return CborNoError;
254}
Thiago Macieira54a0e102015-05-05 21:25:06 -0700255
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700256static CborError preparse_next_value(CborValue *it)
257{
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700258 if (it->remaining != UINT32_MAX) {
Thiago Macieiradbc01292016-06-06 17:02:25 -0700259 /* don't decrement the item count if the current item is tag: they don't count */
Thiago Macieira11e913f2015-05-07 13:01:18 -0700260 if (it->type != CborTagType && !--it->remaining) {
261 it->type = CborInvalidType;
Thiago Macieira56d99832015-05-07 14:34:27 -0700262 return CborNoError;
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700263 }
Thiago Macieira5752ce52015-06-16 12:10:03 -0700264 } else if (it->remaining == UINT32_MAX && it->ptr != it->parser->end && *it->ptr == (uint8_t)BreakByte) {
Thiago Macieiradbc01292016-06-06 17:02:25 -0700265 /* end of map or array */
Thiago Macieira56d99832015-05-07 14:34:27 -0700266 ++it->ptr;
267 it->type = CborInvalidType;
268 it->remaining = 0;
269 return CborNoError;
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700270 }
Thiago Macieira56d99832015-05-07 14:34:27 -0700271
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700272 return preparse_value(it);
273}
274
275static CborError advance_internal(CborValue *it)
276{
277 uint64_t length;
Thiago Macieira4e9626c2015-09-21 14:57:17 -0700278 CborError err = extract_number(&it->ptr, it->parser->end, &length);
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700279 assert(err == CborNoError);
280
Thiago Macieira56d99832015-05-07 14:34:27 -0700281 if (it->type == CborByteStringType || it->type == CborTextStringType) {
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700282 assert(length == (size_t)length);
Thiago Macieira56d99832015-05-07 14:34:27 -0700283 assert((it->flags & CborIteratorFlag_UnknownLength) == 0);
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700284 it->ptr += length;
285 }
286
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700287 return preparse_next_value(it);
Thiago Macieira54a0e102015-05-05 21:25:06 -0700288}
289
Thiago Macieira2312efd2015-05-06 16:07:48 -0700290/** \internal
291 *
292 * Decodes the CBOR integer value when it is larger than the 16 bits available
293 * in value->extra. This function requires that value->flags have the
294 * CborIteratorFlag_IntegerValueTooLarge flag set.
295 *
296 * This function is also used to extract single- and double-precision floating
297 * point values (SinglePrecisionFloat == Value32Bit and DoublePrecisionFloat ==
298 * Value64Bit).
299 */
Thiago Macieira54a0e102015-05-05 21:25:06 -0700300uint64_t _cbor_value_decode_int64_internal(const CborValue *value)
301{
Thiago Macieira2312efd2015-05-06 16:07:48 -0700302 assert(value->flags & CborIteratorFlag_IntegerValueTooLarge ||
303 value->type == CborFloatType || value->type == CborDoubleType);
Thiago Macieira851c4812015-05-08 15:23:20 -0700304
Thiago Macieiradbc01292016-06-06 17:02:25 -0700305 /* since the additional information can only be Value32Bit or Value64Bit,
306 * we just need to test for the one bit those two options differ */
Thiago Macieira851c4812015-05-08 15:23:20 -0700307 assert((*value->ptr & SmallValueMask) == Value32Bit || (*value->ptr & SmallValueMask) == Value64Bit);
308 if ((*value->ptr & 1) == (Value32Bit & 1))
Thiago Macieira54a0e102015-05-05 21:25:06 -0700309 return get32(value->ptr + 1);
310
311 assert((*value->ptr & SmallValueMask) == Value64Bit);
312 return get64(value->ptr + 1);
313}
314
315/**
316 * Initializes the CBOR parser for parsing \a size bytes beginning at \a
317 * buffer. Parsing will use flags set in \a flags. The iterator to the first
318 * element is returned in \a it.
Thiago Macieira2312efd2015-05-06 16:07:48 -0700319 *
320 * The \a parser structure needs to remain valid throughout the decoding
321 * process. It is not thread-safe to share one CborParser among multiple
322 * threads iterating at the same time, but the object can be copied so multiple
323 * threads can iterate.
Thiago Macieira54a0e102015-05-05 21:25:06 -0700324 */
Thiago Macieira5752ce52015-06-16 12:10:03 -0700325CborError cbor_parser_init(const uint8_t *buffer, size_t size, int flags, CborParser *parser, CborValue *it)
Thiago Macieira54a0e102015-05-05 21:25:06 -0700326{
327 memset(parser, 0, sizeof(*parser));
328 parser->end = buffer + size;
Thiago Macieira54a0e102015-05-05 21:25:06 -0700329 parser->flags = flags;
330 it->parser = parser;
331 it->ptr = buffer;
Thiago Macieiradbc01292016-06-06 17:02:25 -0700332 it->remaining = 1; /* there's one type altogether, usually an array or map */
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700333 return preparse_value(it);
Thiago Macieira2312efd2015-05-06 16:07:48 -0700334}
335
336/**
Thiago Macieira46a818e2015-10-08 15:13:05 +0200337 * \fn bool cbor_value_at_end(const CborValue *it)
338 *
339 * Returns true if \a it has reached the end of the iteration, usually when
Thiago Macieira740e29d2016-07-07 13:32:47 -0700340 * advancing after the last item in an array or map.
Thiago Macieira46a818e2015-10-08 15:13:05 +0200341 *
Thiago Macieira740e29d2016-07-07 13:32:47 -0700342 * In the case of the outermost CborValue object, this function returns true
343 * after decoding a single element. A pointer to the first byte of the
344 * remaining data (if any) can be obtained with cbor_value_get_next_byte().
345 *
346 * \sa cbor_value_advance(), cbor_value_is_valid(), cbor_value_get_next_byte()
347 */
348
349/**
350 * \fn const uint8_t *cbor_value_get_next_byte(const CborValue *it)
351 *
352 * Returns a pointer to the next byte that would be decoded if this CborValue
353 * object were advanced.
354 *
355 * This function is useful if cbor_value_at_end() returns true for the
356 * outermost CborValue: the pointer returned is the first byte of the data
357 * remaining in the buffer, if any. Code can decide whether to begin decoding a
358 * new CBOR data stream from this point, or parse some other data appended to
359 * the same buffer.
360 *
361 * This function may be used even after a parsing error. If that occurred,
362 * then this function returns a pointer to where the parsing error occurred.
363 * Note that the error recovery is not precise and the pointer may not indicate
364 * the exact byte containing bad data.
365 *
366 * \sa cbor_value_at_end()
Thiago Macieira46a818e2015-10-08 15:13:05 +0200367 */
368
369/**
370 * \fn bool cbor_value_is_valid(const CborValue *it)
371 *
372 * Returns true if the iterator \a it contains a valid value. Invalid iterators
373 * happen when iteration reaches the end of a container (see \ref
374 * cbor_value_at_end()) or when a search function resulted in no matches.
375 *
376 * \sa cbor_value_advance(), cbor_valie_at_end(), cbor_value_get_type()
377 */
378
379/**
Thiago Macieira2312efd2015-05-06 16:07:48 -0700380 * Advances the CBOR value \a it by one fixed-size position. Fixed-size types
381 * are: integers, tags, simple types (including boolean, null and undefined
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700382 * values) and floating point types.
Thiago Macieira2312efd2015-05-06 16:07:48 -0700383 *
Thiago Macieira46a818e2015-10-08 15:13:05 +0200384 * If the type is not of fixed size, this function has undefined behavior. Code
385 * must be sure that the current type is one of the fixed-size types before
386 * calling this function. This function is provided because it can guarantee
387 * that runs in constant time (O(1)).
388 *
389 * If the caller is not able to determine whether the type is fixed or not, code
390 * can use the cbor_value_advance() function instead.
391 *
392 * \sa cbor_value_at_end(), cbor_value_advance(), cbor_value_enter_container(), cbor_value_leave_container()
Thiago Macieira2312efd2015-05-06 16:07:48 -0700393 */
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700394CborError cbor_value_advance_fixed(CborValue *it)
Thiago Macieira54a0e102015-05-05 21:25:06 -0700395{
Thiago Macieira2312efd2015-05-06 16:07:48 -0700396 assert(it->type != CborInvalidType);
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700397 assert(is_fixed_type(it->type));
398 if (!it->remaining)
399 return CborErrorAdvancePastEOF;
400 return advance_internal(it);
Thiago Macieira54a0e102015-05-05 21:25:06 -0700401}
402
Thiago Macieira4a99af92015-05-12 10:41:45 +0900403static CborError advance_recursive(CborValue *it, int nestingLevel)
404{
405 if (is_fixed_type(it->type))
406 return advance_internal(it);
407
408 if (!cbor_value_is_container(it)) {
409 size_t len = SIZE_MAX;
Thiago Macieiraff130bc2015-06-19 15:15:33 -0700410 return _cbor_value_copy_string(it, NULL, &len, it);
Thiago Macieira4a99af92015-05-12 10:41:45 +0900411 }
412
Thiago Macieiradbc01292016-06-06 17:02:25 -0700413 /* map or array */
Thiago Macieira4a99af92015-05-12 10:41:45 +0900414 if (nestingLevel == CBOR_PARSER_MAX_RECURSIONS)
415 return CborErrorNestingTooDeep;
416
417 CborError err;
418 CborValue recursed;
419 err = cbor_value_enter_container(it, &recursed);
420 if (err)
421 return err;
422 while (!cbor_value_at_end(&recursed)) {
423 err = advance_recursive(&recursed, nestingLevel + 1);
424 if (err)
425 return err;
426 }
427 return cbor_value_leave_container(it, &recursed);
428}
429
430
Thiago Macieira2312efd2015-05-06 16:07:48 -0700431/**
432 * Advances the CBOR value \a it by one element, skipping over containers.
433 * Unlike cbor_value_advance_fixed(), this function can be called on a CBOR
434 * value of any type. However, if the type is a container (map or array) or a
435 * string with a chunked payload, this function will not run in constant time
436 * and will recurse into itself (it will run on O(n) time for the number of
437 * elements or chunks and will use O(n) memory for the number of nested
438 * containers).
439 *
Thiago Macieira46a818e2015-10-08 15:13:05 +0200440 * \sa cbor_value_at_end(), cbor_value_advance_fixed(), cbor_value_enter_container(), cbor_value_leave_container()
Thiago Macieira2312efd2015-05-06 16:07:48 -0700441 */
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700442CborError cbor_value_advance(CborValue *it)
Thiago Macieira2312efd2015-05-06 16:07:48 -0700443{
444 assert(it->type != CborInvalidType);
445 if (!it->remaining)
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700446 return CborErrorAdvancePastEOF;
Thiago Macieira4a99af92015-05-12 10:41:45 +0900447 return advance_recursive(it, 0);
Thiago Macieira2312efd2015-05-06 16:07:48 -0700448}
449
450/**
Thiago Macieira46a818e2015-10-08 15:13:05 +0200451 * \fn bool cbor_value_is_tag(const CborValue *value)
452 *
453 * Returns true if the iterator \a value is valid and points to a CBOR tag.
454 *
455 * \sa cbor_value_get_tag(), cbor_value_skip_tag()
456 */
457
458/**
459 * \fn CborError cbor_value_get_tag(const CborValue *value, CborTag *result)
460 *
461 * Retrieves the CBOR tag value that \a value points to and stores it in \a
462 * result. If the iterator \a value does not point to a CBOR tag value, the
463 * behavior is undefined, so checking with \ref cbor_value_get_type or with
464 * \ref cbor_value_is_tag is recommended.
465 *
466 * \sa cbor_value_get_type(), cbor_value_is_valid(), cbor_value_is_tag()
467 */
468
469/**
Thiago Macieirac4a73c62015-05-09 18:14:11 -0700470 * Advances the CBOR value \a it until it no longer points to a tag. If \a it is
471 * already not pointing to a tag, then this function returns it unchanged.
472 *
Thiago Macieira46a818e2015-10-08 15:13:05 +0200473 * This function does not run in constant time: it will run on O(n) for n being
474 * the number of tags. It does use constant memory (O(1) memory requirements).
475 *
Thiago Macieirac4a73c62015-05-09 18:14:11 -0700476 * \sa cbor_value_advance_fixed(), cbor_value_advance()
477 */
478CborError cbor_value_skip_tag(CborValue *it)
479{
480 while (cbor_value_is_tag(it)) {
481 CborError err = cbor_value_advance_fixed(it);
482 if (err)
483 return err;
484 }
485 return CborNoError;
486}
487
Thiago Macieirac4a73c62015-05-09 18:14:11 -0700488/**
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700489 * \fn bool cbor_value_is_container(const CborValue *it)
490 *
Thiago Macieira2312efd2015-05-06 16:07:48 -0700491 * Returns true if the \a it value is a container and requires recursion in
492 * order to decode (maps and arrays), false otherwise.
493 */
Thiago Macieira54a0e102015-05-05 21:25:06 -0700494
Thiago Macieira2312efd2015-05-06 16:07:48 -0700495/**
496 * Creates a CborValue iterator pointing to the first element of the container
497 * represented by \a it and saves it in \a recursed. The \a it container object
498 * needs to be kept and passed again to cbor_value_leave_container() in order
499 * to continue iterating past this container.
500 *
Thiago Macieira46a818e2015-10-08 15:13:05 +0200501 * The \a it CborValue iterator must point to a container.
502 *
Thiago Macieira2312efd2015-05-06 16:07:48 -0700503 * \sa cbor_value_is_container(), cbor_value_leave_container(), cbor_value_advance()
504 */
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700505CborError cbor_value_enter_container(const CborValue *it, CborValue *recursed)
Thiago Macieira54a0e102015-05-05 21:25:06 -0700506{
Thiago Macieira56d99832015-05-07 14:34:27 -0700507 CborError err;
Thiago Macieira2312efd2015-05-06 16:07:48 -0700508 assert(cbor_value_is_container(it));
Thiago Macieira54a0e102015-05-05 21:25:06 -0700509 *recursed = *it;
Thiago Macieira56d99832015-05-07 14:34:27 -0700510
Thiago Macieira54a0e102015-05-05 21:25:06 -0700511 if (it->flags & CborIteratorFlag_UnknownLength) {
512 recursed->remaining = UINT32_MAX;
Thiago Macieira56d99832015-05-07 14:34:27 -0700513 ++recursed->ptr;
514 err = preparse_value(recursed);
515 if (err != CborErrorUnexpectedBreak)
516 return err;
Thiago Macieiradbc01292016-06-06 17:02:25 -0700517 /* actually, break was expected here
518 * it's just an empty container */
Thiago Macieira56d99832015-05-07 14:34:27 -0700519 ++recursed->ptr;
Thiago Macieira54a0e102015-05-05 21:25:06 -0700520 } else {
Thiago Macieira56d99832015-05-07 14:34:27 -0700521 uint64_t len;
Thiago Macieira4e9626c2015-09-21 14:57:17 -0700522 err = extract_number(&recursed->ptr, recursed->parser->end, &len);
Thiago Macieira56d99832015-05-07 14:34:27 -0700523 assert(err == CborNoError);
Thiago Macieira56d99832015-05-07 14:34:27 -0700524
Thiago Macieirae12dfd02016-06-07 16:29:25 -0700525 recursed->remaining = (uint32_t)len;
Thiago Macieira3f76f632015-05-12 10:10:09 +0900526 if (recursed->remaining != len || len == UINT32_MAX) {
Thiago Macieiradbc01292016-06-06 17:02:25 -0700527 /* back track the pointer to indicate where the error occurred */
Thiago Macieira3f76f632015-05-12 10:10:09 +0900528 recursed->ptr = it->ptr;
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700529 return CborErrorDataTooLarge;
Thiago Macieira3f76f632015-05-12 10:10:09 +0900530 }
Thiago Macieirace16f052015-05-07 23:14:25 -0700531 if (recursed->type == CborMapType) {
Thiago Macieiradbc01292016-06-06 17:02:25 -0700532 /* maps have keys and values, so we need to multiply by 2 */
Thiago Macieira3f76f632015-05-12 10:10:09 +0900533 if (recursed->remaining > UINT32_MAX / 2) {
Thiago Macieiradbc01292016-06-06 17:02:25 -0700534 /* back track the pointer to indicate where the error occurred */
Thiago Macieira3f76f632015-05-12 10:10:09 +0900535 recursed->ptr = it->ptr;
Thiago Macieirace16f052015-05-07 23:14:25 -0700536 return CborErrorDataTooLarge;
Thiago Macieira3f76f632015-05-12 10:10:09 +0900537 }
Thiago Macieirace16f052015-05-07 23:14:25 -0700538 recursed->remaining *= 2;
539 }
Thiago Macieira56d99832015-05-07 14:34:27 -0700540 if (len != 0)
541 return preparse_value(recursed);
Thiago Macieira54a0e102015-05-05 21:25:06 -0700542 }
Thiago Macieira56d99832015-05-07 14:34:27 -0700543
Thiago Macieiradbc01292016-06-06 17:02:25 -0700544 /* the case of the empty container */
Thiago Macieira56d99832015-05-07 14:34:27 -0700545 recursed->type = CborInvalidType;
546 recursed->remaining = 0;
547 return CborNoError;
Thiago Macieirac70169f2015-05-06 07:49:44 -0700548}
549
Thiago Macieira2312efd2015-05-06 16:07:48 -0700550/**
551 * Updates \a it to point to the next element after the container. The \a
Thiago Macieira56d99832015-05-07 14:34:27 -0700552 * recursed object needs to point to the element obtained either by advancing
553 * the last element of the container (via cbor_value_advance(),
554 * cbor_value_advance_fixed(), a nested cbor_value_leave_container(), or the \c
555 * next pointer from cbor_value_copy_string() or cbor_value_dup_string()).
Thiago Macieira2312efd2015-05-06 16:07:48 -0700556 *
Thiago Macieira46a818e2015-10-08 15:13:05 +0200557 * The \a it and \a recursed parameters must be the exact same as passed to
558 * cbor_value_enter_container().
559 *
Thiago Macieira2312efd2015-05-06 16:07:48 -0700560 * \sa cbor_value_enter_container(), cbor_value_at_end()
561 */
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700562CborError cbor_value_leave_container(CborValue *it, const CborValue *recursed)
Thiago Macieirac70169f2015-05-06 07:49:44 -0700563{
Thiago Macieira2312efd2015-05-06 16:07:48 -0700564 assert(cbor_value_is_container(it));
Thiago Macieira56d99832015-05-07 14:34:27 -0700565 assert(recursed->type == CborInvalidType);
Thiago Macieirac70169f2015-05-06 07:49:44 -0700566 it->ptr = recursed->ptr;
Thiago Macieira56d99832015-05-07 14:34:27 -0700567 return preparse_next_value(it);
Thiago Macieirac70169f2015-05-06 07:49:44 -0700568}
569
Thiago Macieira46a818e2015-10-08 15:13:05 +0200570
Thiago Macieira2312efd2015-05-06 16:07:48 -0700571/**
Thiago Macieira46a818e2015-10-08 15:13:05 +0200572 * \fn CborType cbor_value_get_type(const CborValue *value)
573 *
574 * Returns the type of the CBOR value that the iterator \a value points to. If
575 * \a value does not point to a valid value, this function returns \ref
576 * CborInvalidType.
577 *
578 * TinyCBOR also provides functions to test directly if a given CborValue object
579 * is of a given type, like cbor_value_is_text_string() and cbor_value_is_null().
580 *
581 * \sa cbor_value_is_valid()
582 */
583
584/**
585 * \fn bool cbor_value_is_null(const CborValue *value)
586 *
587 * Returns true if the iterator \a value is valid and points to a CBOR null type.
588 *
589 * \sa cbor_value_is_valid(), cbor_value_is_undefined()
590 */
591
592/**
593 * \fn bool cbor_value_is_undefined(const CborValue *value)
594 *
595 * Returns true if the iterator \a value is valid and points to a CBOR undefined type.
596 *
597 * \sa cbor_value_is_valid(), cbor_value_is_null()
598 */
599
600/**
601 * \fn bool cbor_value_is_boolean(const CborValue *value)
602 *
603 * Returns true if the iterator \a value is valid and points to a CBOR boolean
604 * type (true or false).
605 *
606 * \sa cbor_value_is_valid(), cbor_value_get_boolean()
607 */
608
609/**
610 * \fn CborError cbor_value_get_boolean(const CborValue *value, bool *result)
611 *
612 * Retrieves the boolean value that \a value points to and stores it in \a
613 * result. If the iterator \a value does not point to a boolean value, the
614 * behavior is undefined, so checking with \ref cbor_value_get_type or with
615 * \ref cbor_value_is_boolean is recommended.
616 *
617 * \sa cbor_value_get_type(), cbor_value_is_valid(), cbor_value_is_boolean()
618 */
619
620/**
621 * \fn bool cbor_value_is_simple_type(const CborValue *value)
622 *
623 * Returns true if the iterator \a value is valid and points to a CBOR Simple Type
624 * type (other than true, false, null and undefined).
625 *
626 * \sa cbor_value_is_valid(), cbor_value_get_simple_type()
627 */
628
629/**
630 * \fn CborError cbor_value_get_simple_type(const CborValue *value, uint8_t *result)
631 *
632 * Retrieves the CBOR Simple Type value that \a value points to and stores it
633 * in \a result. If the iterator \a value does not point to a simple_type
634 * value, the behavior is undefined, so checking with \ref cbor_value_get_type
635 * or with \ref cbor_value_is_simple_type is recommended.
636 *
637 * \sa cbor_value_get_type(), cbor_value_is_valid(), cbor_value_is_simple_type()
638 */
639
640/**
641 * \fn bool cbor_value_is_integer(const CborValue *value)
642 *
643 * Returns true if the iterator \a value is valid and points to a CBOR integer
644 * type.
645 *
646 * \sa cbor_value_is_valid(), cbor_value_get_int, cbor_value_get_int64, cbor_value_get_uint64, cbor_value_get_raw_integer
647 */
648
649/**
650 * \fn bool cbor_value_is_unsigned_integer(const CborValue *value)
651 *
652 * Returns true if the iterator \a value is valid and points to a CBOR unsigned
653 * integer type (positive values or zero).
654 *
655 * \sa cbor_value_is_valid(), cbor_value_get_uint64()
656 */
657
658/**
659 * \fn bool cbor_value_is_negative_integer(const CborValue *value)
660 *
661 * Returns true if the iterator \a value is valid and points to a CBOR negative
662 * integer type.
663 *
664 * \sa cbor_value_is_valid(), cbor_value_get_int, cbor_value_get_int64, cbor_value_get_raw_integer
665 */
666
667/**
668 * \fn CborError cbor_value_get_int(const CborValue *value, int *result)
669 *
670 * Retrieves the CBOR integer value that \a value points to and stores it in \a
671 * result. If the iterator \a value does not point to an integer value, the
672 * behavior is undefined, so checking with \ref cbor_value_get_type or with
673 * \ref cbor_value_is_integer is recommended.
674 *
675 * Note that this function does not do range-checking: integral values that do
676 * not fit in a variable of type \c{int} are silently truncated to fit. Use
677 * cbor_value_get_int_checked() that is not acceptable.
678 *
679 * \sa cbor_value_get_type(), cbor_value_is_valid(), cbor_value_is_integer()
680 */
681
682/**
683 * \fn CborError cbor_value_get_int64(const CborValue *value, int64_t *result)
684 *
685 * Retrieves the CBOR integer value that \a value points to and stores it in \a
686 * result. If the iterator \a value does not point to an integer value, the
687 * behavior is undefined, so checking with \ref cbor_value_get_type or with
688 * \ref cbor_value_is_integer is recommended.
689 *
690 * Note that this function does not do range-checking: integral values that do
691 * not fit in a variable of type \c{int64_t} are silently truncated to fit. Use
692 * cbor_value_get_int64_checked() that is not acceptable.
693 *
694 * \sa cbor_value_get_type(), cbor_value_is_valid(), cbor_value_is_integer()
695 */
696
697/**
698 * \fn CborError cbor_value_get_uint64(const CborValue *value, uint64_t *result)
699 *
700 * Retrieves the CBOR integer value that \a value points to and stores it in \a
701 * result. If the iterator \a value does not point to an unsigned integer
702 * value, the behavior is undefined, so checking with \ref cbor_value_get_type
703 * or with \ref cbor_value_is_unsigned_integer is recommended.
704 *
705 * \sa cbor_value_get_type(), cbor_value_is_valid(), cbor_value_is_unsigned_integer()
706 */
707
708/**
709 * \fn CborError cbor_value_get_raw_integer(const CborValue *value, uint64_t *result)
710 *
711 * Retrieves the CBOR integer value that \a value points to and stores it in \a
712 * result. If the iterator \a value does not point to an integer value, the
713 * behavior is undefined, so checking with \ref cbor_value_get_type or with
714 * \ref cbor_value_is_integer is recommended.
715 *
716 * This function is provided because CBOR negative integers can assume values
717 * that cannot be represented with normal 64-bit integer variables.
718 *
719 * If the integer is unsigned (that is, if cbor_value_is_unsigned_integer()
720 * returns true), then \a result will contain the actual value. If the integer
721 * is negative, then \a result will contain the absolute value of that integer,
722 * minus one. That is, \c {actual = -result - 1}. On architectures using two's
723 * complement for representation of negative integers, it is equivalent to say
724 * that \a result will contain the bitwise negation of the actual value.
725 *
726 * \sa cbor_value_get_type(), cbor_value_is_valid(), cbor_value_is_integer()
727 */
728
729/**
Thiago Macieira0f02e792016-07-07 19:55:08 -0700730 * Retrieves the CBOR integer value that \a value points to and stores it in \a
731 * result. If the iterator \a value does not point to an integer value, the
732 * behavior is undefined, so checking with \ref cbor_value_get_type or with
733 * \ref cbor_value_is_integer is recommended.
734 *
735 * Unlike cbor_value_get_int64(), this function performs a check to see if the
736 * stored integer fits in \a result without data loss. If the number is outside
737 * the valid range for the data type, this function returns the recoverable
738 * error CborErrorDataTooLarge. In that case, use either
739 * cbor_value_get_uint64() (if the number is positive) or
740 * cbor_value_get_raw_integer().
741 *
742 * \sa cbor_value_get_type(), cbor_value_is_valid(), cbor_value_is_integer(), cbor_value_get_int64()
743 */
744CborError cbor_value_get_int64_checked(const CborValue *value, int64_t *result)
745{
746 assert(cbor_value_is_integer(value));
747 uint64_t v = _cbor_value_extract_int64_helper(value);
748
749 /* Check before converting, as the standard says (C11 6.3.1.3 paragraph 3):
750 * "[if] the new type is signed and the value cannot be represented in it; either the
751 * result is implementation-defined or an implementation-defined signal is raised."
752 *
753 * The range for int64_t is -2^63 to 2^63-1 (int64_t is required to be
754 * two's complement, C11 7.20.1.1 paragraph 3), which in CBOR is
755 * represented the same way, differing only on the "sign bit" (the major
756 * type).
757 */
758
759 if (unlikely(v > (uint64_t)INT64_MAX))
760 return CborErrorDataTooLarge;
761
762 *result = v;
763 if (value->flags & CborIteratorFlag_NegativeInteger)
764 *result = -*result - 1;
765 return CborNoError;
766}
767
768/**
769 * Retrieves the CBOR integer value that \a value points to and stores it in \a
770 * result. If the iterator \a value does not point to an integer value, the
771 * behavior is undefined, so checking with \ref cbor_value_get_type or with
772 * \ref cbor_value_is_integer is recommended.
773 *
774 * Unlike cbor_value_get_int(), this function performs a check to see if the
775 * stored integer fits in \a result without data loss. If the number is outside
776 * the valid range for the data type, this function returns the recoverable
777 * error CborErrorDataTooLarge. In that case, use one of the other integer
778 * functions to obtain the value.
779 *
780 * \sa cbor_value_get_type(), cbor_value_is_valid(), cbor_value_is_integer(), cbor_value_get_int64(),
781 * cbor_value_get_uint64(), cbor_value_get_int64_checked(), cbor_value_get_raw_integer()
782 */
783CborError cbor_value_get_int_checked(const CborValue *value, int *result)
784{
785 assert(cbor_value_is_integer(value));
786 uint64_t v = _cbor_value_extract_int64_helper(value);
787
788 /* Check before converting, as the standard says (C11 6.3.1.3 paragraph 3):
789 * "[if] the new type is signed and the value cannot be represented in it; either the
790 * result is implementation-defined or an implementation-defined signal is raised."
791 *
792 * But we can convert from signed to unsigned without fault (paragraph 2).
793 *
794 * The range for int is implementation-defined and int is not guaranteed use
795 * two's complement representation (int32_t is).
796 */
797
798 if (value->flags & CborIteratorFlag_NegativeInteger) {
799 if (unlikely(v > (unsigned) -(INT_MIN + 1)))
800 return CborErrorDataTooLarge;
801
alradmsft9ba47912016-10-11 17:56:15 -0700802 *result = (int)v;
Thiago Macieira0f02e792016-07-07 19:55:08 -0700803 *result = -*result - 1;
804 } else {
805 if (unlikely(v > (uint64_t)INT_MAX))
806 return CborErrorDataTooLarge;
807
alradmsft9ba47912016-10-11 17:56:15 -0700808 *result = (int)v;
Thiago Macieira0f02e792016-07-07 19:55:08 -0700809 }
810 return CborNoError;
811
812}
813
814/**
Thiago Macieira46a818e2015-10-08 15:13:05 +0200815 * \fn bool cbor_value_is_length_known(const CborValue *value)
816 *
817 * Returns true if the length of this type is known without calculation. That
818 * is, if the length of this CBOR string, map or array is encoded in the data
819 * stream, this function returns true. If the length is not encoded, it returns
820 * false.
821 *
822 * If the length is known, code can call cbor_value_get_string_length(),
823 * cbor_value_get_array_length() or cbor_value_get_map_length() to obtain the
824 * length. If the length is not known but is necessary, code can use the
825 * cbor_value_calculate_string_length() function (no equivalent function is
826 * provided for maps and arrays).
827 */
828
829/**
830 * \fn bool cbor_value_is_text_string(const CborValue *value)
831 *
832 * Returns true if the iterator \a value is valid and points to a CBOR text
833 * string. CBOR text strings are UTF-8 encoded and usually contain
834 * human-readable text.
835 *
836 * \sa cbor_value_is_valid(), cbor_value_get_string_length(), cbor_value_calculate_string_length(),
837 * cbor_value_copy_text_string(), cbor_value_dup_text_string()
838 */
839
840/**
841 * \fn bool cbor_value_is_byte_string(const CborValue *value)
842 *
843 * Returns true if the iterator \a value is valid and points to a CBOR text
844 * string. CBOR byte strings are binary data with no specified encoding or
845 * format.
846 *
847 * \sa cbor_value_is_valid(), cbor_value_get_string_length(), cbor_value_calculate_string_length(),
848 * cbor_value_copy_byte_string(), cbor_value_dup_byte_string()
849 */
850
851/**
852 * \fn CborError cbor_value_get_string_length(const CborValue *value, size_t *length)
853 *
854 * Extracts the length of the byte or text string that \a value points to and
855 * stores it in \a result. If the iterator \a value does not point to a text
856 * string or a byte string, the behaviour is undefined, so checking with \ref
857 * cbor_value_get_type, with \ref cbor_value_is_text_string or \ref
858 * cbor_value_is_byte_string is recommended.
859 *
860 * If the length of this string is not encoded in the CBOR data stream, this
861 * function will return the recoverable error CborErrorUnknownLength. You may
862 * also check whether that is the case by using cbor_value_is_length_known().
863 *
864 * If the length of the string is required but the length was not encoded, use
865 * cbor_value_calculate_string_length(), but note that that function does not
866 * run in constant time.
867 *
868 * \note On 32-bit platforms, this function will return error condition of \ref
869 * CborErrorDataTooLarge if the stream indicates a length that is too big to
870 * fit in 32-bit.
871 *
872 * \sa cbor_value_is_valid(), cbor_value_is_length_known(), cbor_value_calculate_string_length()
873 */
874
875/**
876 * Calculates the length of the byte or text string that \a value points to and
877 * stores it in \a len. If the iterator \a value does not point to a text
878 * string or a byte string, the behaviour is undefined, so checking with \ref
879 * cbor_value_get_type, with \ref cbor_value_is_text_string or \ref
880 * cbor_value_is_byte_string is recommended.
881 *
882 * This function is different from cbor_value_get_string_length() in that it
883 * calculates the length even for strings sent in chunks. For that reason, this
884 * function may not run in constant time (it will run in O(n) time on the
885 * number of chunks). It does use constant memory (O(1)).
Thiago Macieira2312efd2015-05-06 16:07:48 -0700886 *
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700887 * \note On 32-bit platforms, this function will return error condition of \ref
888 * CborErrorDataTooLarge if the stream indicates a length that is too big to
889 * fit in 32-bit.
Thiago Macieira2312efd2015-05-06 16:07:48 -0700890 *
891 * \sa cbor_value_get_string_length(), cbor_value_copy_string(), cbor_value_is_length_known()
892 */
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700893CborError cbor_value_calculate_string_length(const CborValue *value, size_t *len)
Thiago Macieira2312efd2015-05-06 16:07:48 -0700894{
Thiago Macieira9ae05812015-05-11 15:09:09 +0900895 *len = SIZE_MAX;
Thiago Macieiraff130bc2015-06-19 15:15:33 -0700896 return _cbor_value_copy_string(value, NULL, len, NULL);
Thiago Macieirac70169f2015-05-06 07:49:44 -0700897}
898
Thiago Macieiradbc01292016-06-06 17:02:25 -0700899/* We return uintptr_t so that we can pass memcpy directly as the iteration
900 * function. The choice is to optimize for memcpy, which is used in the base
901 * parser API (cbor_value_copy_string), while memcmp is used in convenience API
902 * only. */
Thiago Macieira5752ce52015-06-16 12:10:03 -0700903typedef uintptr_t (*IterateFunction)(char *, const uint8_t *, size_t);
Thiago Macieira9ae05812015-05-11 15:09:09 +0900904
Thiago Macieira5752ce52015-06-16 12:10:03 -0700905static uintptr_t iterate_noop(char *dest, const uint8_t *src, size_t len)
Thiago Macieira9ae05812015-05-11 15:09:09 +0900906{
907 (void)dest;
908 (void)src;
909 (void)len;
910 return true;
911}
912
Thiago Macieira5752ce52015-06-16 12:10:03 -0700913static uintptr_t iterate_memcmp(char *s1, const uint8_t *s2, size_t len)
Thiago Macieirac4a73c62015-05-09 18:14:11 -0700914{
Thiago Macieira5752ce52015-06-16 12:10:03 -0700915 return memcmp(s1, (const char *)s2, len) == 0;
Thiago Macieirac4a73c62015-05-09 18:14:11 -0700916}
917
Thiago Macieira9ae05812015-05-11 15:09:09 +0900918static CborError iterate_string_chunks(const CborValue *value, char *buffer, size_t *buflen,
919 bool *result, CborValue *next, IterateFunction func)
920{
921 assert(cbor_value_is_byte_string(value) || cbor_value_is_text_string(value));
922
923 size_t total;
924 CborError err;
Thiago Macieira5752ce52015-06-16 12:10:03 -0700925 const uint8_t *ptr = value->ptr;
Thiago Macieira9ae05812015-05-11 15:09:09 +0900926 if (cbor_value_is_length_known(value)) {
Thiago Macieiradbc01292016-06-06 17:02:25 -0700927 /* easy case: fixed length */
Thiago Macieira9ae05812015-05-11 15:09:09 +0900928 err = extract_length(value->parser, &ptr, &total);
929 if (err)
930 return err;
Thiago Macieira63abed92015-10-28 17:01:14 -0700931 if (total > (size_t)(value->parser->end - ptr))
Thiago Macieira9ae05812015-05-11 15:09:09 +0900932 return CborErrorUnexpectedEOF;
933 if (total <= *buflen)
Thiago Macieirae12dfd02016-06-07 16:29:25 -0700934 *result = !!func(buffer, ptr, total);
Thiago Macieira9ae05812015-05-11 15:09:09 +0900935 else
936 *result = false;
937 ptr += total;
938 } else {
Thiago Macieiradbc01292016-06-06 17:02:25 -0700939 /* chunked */
Thiago Macieira9ae05812015-05-11 15:09:09 +0900940 ++ptr;
941 total = 0;
942 *result = true;
943 while (true) {
944 size_t chunkLen;
945 size_t newTotal;
946
947 if (ptr == value->parser->end)
948 return CborErrorUnexpectedEOF;
949
Thiago Macieira5752ce52015-06-16 12:10:03 -0700950 if (*ptr == (uint8_t)BreakByte) {
Thiago Macieira9ae05812015-05-11 15:09:09 +0900951 ++ptr;
952 break;
953 }
954
Thiago Macieiradbc01292016-06-06 17:02:25 -0700955 /* is this the right type? */
Thiago Macieira9ae05812015-05-11 15:09:09 +0900956 if ((*ptr & MajorTypeMask) != value->type)
957 return CborErrorIllegalType;
958
959 err = extract_length(value->parser, &ptr, &chunkLen);
960 if (err)
961 return err;
962
Thiago Macieira1de31a42015-06-16 16:01:16 -0700963 if (unlikely(add_check_overflow(total, chunkLen, &newTotal)))
Thiago Macieira9ae05812015-05-11 15:09:09 +0900964 return CborErrorDataTooLarge;
965
Thiago Macieira63abed92015-10-28 17:01:14 -0700966 if (chunkLen > (size_t)(value->parser->end - ptr))
Thiago Macieira9ae05812015-05-11 15:09:09 +0900967 return CborErrorUnexpectedEOF;
968
969 if (*result && *buflen >= newTotal)
Thiago Macieirae12dfd02016-06-07 16:29:25 -0700970 *result = !!func(buffer + total, ptr, chunkLen);
Thiago Macieira9ae05812015-05-11 15:09:09 +0900971 else
972 *result = false;
973
974 ptr += chunkLen;
975 total = newTotal;
976 }
977 }
978
Thiago Macieiradbc01292016-06-06 17:02:25 -0700979 /* is there enough room for the ending NUL byte? */
Thiago Macieirae136feb2017-02-24 21:21:04 -0800980 if (*result && *buflen > total) {
981 uint8_t nul[] = { 0 };
982 *result = !!func(buffer + total, nul, 1);
983 }
Thiago Macieira9ae05812015-05-11 15:09:09 +0900984 *buflen = total;
985
986 if (next) {
987 *next = *value;
988 next->ptr = ptr;
989 return preparse_next_value(next);
990 }
991 return CborNoError;
992}
993
Thiago Macieira2312efd2015-05-06 16:07:48 -0700994/**
Thiago Macieiraff130bc2015-06-19 15:15:33 -0700995 * \fn CborError cbor_value_copy_text_string(const CborValue *value, char *buffer, size_t *buflen, CborValue *next)
996 *
Thiago Macieira2312efd2015-05-06 16:07:48 -0700997 * Copies the string pointed by \a value into the buffer provided at \a buffer
998 * of \a buflen bytes. If \a buffer is a NULL pointer, this function will not
999 * copy anything and will only update the \a next value.
1000 *
Thiago Macieira46a818e2015-10-08 15:13:05 +02001001 * If the iterator \a value does not point to a text string, the behaviour is
1002 * undefined, so checking with \ref cbor_value_get_type or \ref
1003 * cbor_value_is_text_string is recommended.
1004 *
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -07001005 * If the provided buffer length was too small, this function returns an error
1006 * condition of \ref CborErrorOutOfMemory. If you need to calculate the length
1007 * of the string in order to preallocate a buffer, use
Thiago Macieira2312efd2015-05-06 16:07:48 -07001008 * cbor_value_calculate_string_length().
1009 *
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -07001010 * On success, this function sets the number of bytes copied to \c{*buflen}. If
1011 * the buffer is large enough, this function will insert a null byte after the
1012 * last copied byte, to facilitate manipulation of text strings. That byte is
1013 * not included in the returned value of \c{*buflen}.
Thiago Macieira2312efd2015-05-06 16:07:48 -07001014 *
1015 * The \a next pointer, if not null, will be updated to point to the next item
1016 * after this string. If \a value points to the last item, then \a next will be
1017 * invalid.
1018 *
Thiago Macieira46a818e2015-10-08 15:13:05 +02001019 * This function may not run in constant time (it will run in O(n) time on the
1020 * number of chunks). It requires constant memory (O(1)).
1021 *
Thiago Macieira2312efd2015-05-06 16:07:48 -07001022 * \note This function does not perform UTF-8 validation on the incoming text
1023 * string.
1024 *
Thiago Macieiraff130bc2015-06-19 15:15:33 -07001025 * \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 -07001026 */
Thiago Macieiraff130bc2015-06-19 15:15:33 -07001027
1028/**
1029 * \fn CborError cbor_value_copy_byte_string(const CborValue *value, uint8_t *buffer, size_t *buflen, CborValue *next)
1030 *
1031 * Copies the string pointed by \a value into the buffer provided at \a buffer
1032 * of \a buflen bytes. If \a buffer is a NULL pointer, this function will not
1033 * copy anything and will only update the \a next value.
1034 *
Thiago Macieira46a818e2015-10-08 15:13:05 +02001035 * If the iterator \a value does not point to a byte string, the behaviour is
1036 * undefined, so checking with \ref cbor_value_get_type or \ref
1037 * cbor_value_is_byte_string is recommended.
1038 *
Thiago Macieiraff130bc2015-06-19 15:15:33 -07001039 * If the provided buffer length was too small, this function returns an error
1040 * condition of \ref CborErrorOutOfMemory. If you need to calculate the length
1041 * of the string in order to preallocate a buffer, use
1042 * cbor_value_calculate_string_length().
1043 *
1044 * On success, this function sets the number of bytes copied to \c{*buflen}. If
1045 * the buffer is large enough, this function will insert a null byte after the
1046 * last copied byte, to facilitate manipulation of null-terminated strings.
1047 * That byte is not included in the returned value of \c{*buflen}.
1048 *
1049 * The \a next pointer, if not null, will be updated to point to the next item
1050 * after this string. If \a value points to the last item, then \a next will be
1051 * invalid.
1052 *
Thiago Macieira46a818e2015-10-08 15:13:05 +02001053 * This function may not run in constant time (it will run in O(n) time on the
1054 * number of chunks). It requires constant memory (O(1)).
1055 *
Thiago Macieiraff130bc2015-06-19 15:15:33 -07001056 * \sa cbor_value_dup_text_string(), cbor_value_copy_text_string(), cbor_value_get_string_length(), cbor_value_calculate_string_length()
1057 */
1058
1059CborError _cbor_value_copy_string(const CborValue *value, void *buffer,
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -07001060 size_t *buflen, CborValue *next)
Thiago Macieira2312efd2015-05-06 16:07:48 -07001061{
Thiago Macieira9ae05812015-05-11 15:09:09 +09001062 bool copied_all;
Thiago Macieiraed5b57c2015-07-07 16:38:27 -07001063 CborError err = iterate_string_chunks(value, (char*)buffer, buflen, &copied_all, next,
Thiago Macieira9ae05812015-05-11 15:09:09 +09001064 buffer ? (IterateFunction)memcpy : iterate_noop);
1065 return err ? err :
1066 copied_all ? CborNoError : CborErrorOutOfMemory;
Thiago Macieirac70169f2015-05-06 07:49:44 -07001067}
1068
Thiago Macieirac4a73c62015-05-09 18:14:11 -07001069/**
1070 * Compares the entry \a value with the string \a string and store the result
Thiago Macieira46a818e2015-10-08 15:13:05 +02001071 * in \a result. If the value is different from \a string \a result will
1072 * contain \c false.
Thiago Macieirac4a73c62015-05-09 18:14:11 -07001073 *
1074 * The entry at \a value may be a tagged string. If \a is not a string or a
1075 * tagged string, the comparison result will be false.
Thiago Macieira46a818e2015-10-08 15:13:05 +02001076 *
1077 * CBOR requires text strings to be encoded in UTF-8, but this function does
1078 * not validate either the strings in the stream or the string \a string to be
1079 * matched. Moreover, comparison is done on strict codepoint comparison,
1080 * without any Unicode normalization.
1081 *
1082 * This function may not run in constant time (it will run in O(n) time on the
1083 * number of chunks). It requires constant memory (O(1)).
1084 *
1085 * \sa cbor_value_skip_tag(), cbor_value_copy_text_string()
Thiago Macieirac4a73c62015-05-09 18:14:11 -07001086 */
1087CborError cbor_value_text_string_equals(const CborValue *value, const char *string, bool *result)
1088{
1089 CborValue copy = *value;
1090 CborError err = cbor_value_skip_tag(&copy);
1091 if (err)
1092 return err;
1093 if (!cbor_value_is_text_string(&copy)) {
1094 *result = false;
1095 return CborNoError;
1096 }
1097
1098 size_t len = strlen(string);
1099 return iterate_string_chunks(&copy, CONST_CAST(char *, string), &len, result, NULL, iterate_memcmp);
1100}
1101
1102/**
Thiago Macieira46a818e2015-10-08 15:13:05 +02001103 * \fn bool cbor_value_is_array(const CborValue *value)
Thiago Macieira7b623c22015-05-11 15:52:14 +09001104 *
Thiago Macieira46a818e2015-10-08 15:13:05 +02001105 * Returns true if the iterator \a value is valid and points to a CBOR array.
1106 *
1107 * \sa cbor_value_is_valid(), cbor_value_is_map()
1108 */
1109
1110/**
1111 * \fn CborError cbor_value_get_array_length(const CborValue *value, size_t *length)
1112 *
1113 * Extracts the length of the CBOR array that \a value points to and stores it
1114 * in \a result. If the iterator \a value does not point to a CBOR array, the
1115 * behaviour is undefined, so checking with \ref cbor_value_get_type or \ref
1116 * cbor_value_is_array is recommended.
1117 *
1118 * If the length of this array is not encoded in the CBOR data stream, this
1119 * function will return the recoverable error CborErrorUnknownLength. You may
1120 * also check whether that is the case by using cbor_value_is_length_known().
1121 *
1122 * \note On 32-bit platforms, this function will return error condition of \ref
1123 * CborErrorDataTooLarge if the stream indicates a length that is too big to
1124 * fit in 32-bit.
1125 *
1126 * \sa cbor_value_is_valid(), cbor_value_is_length_known()
1127 */
1128
1129/**
1130 * \fn bool cbor_value_is_map(const CborValue *value)
1131 *
1132 * Returns true if the iterator \a value is valid and points to a CBOR map.
1133 *
1134 * \sa cbor_value_is_valid(), cbor_value_is_array()
1135 */
1136
1137/**
1138 * \fn CborError cbor_value_get_map_length(const CborValue *value, size_t *length)
1139 *
1140 * Extracts the length of the CBOR map that \a value points to and stores it in
1141 * \a result. If the iterator \a value does not point to a CBOR map, the
1142 * behaviour is undefined, so checking with \ref cbor_value_get_type or \ref
1143 * cbor_value_is_map is recommended.
1144 *
1145 * If the length of this map is not encoded in the CBOR data stream, this
1146 * function will return the recoverable error CborErrorUnknownLength. You may
1147 * also check whether that is the case by using cbor_value_is_length_known().
1148 *
1149 * \note On 32-bit platforms, this function will return error condition of \ref
1150 * CborErrorDataTooLarge if the stream indicates a length that is too big to
1151 * fit in 32-bit.
1152 *
1153 * \sa cbor_value_is_valid(), cbor_value_is_length_known()
1154 */
1155
1156/**
1157 * Attempts to find the value in map \a map that corresponds to the text string
1158 * entry \a string. If the iterator \a value does not point to a CBOR map, the
1159 * behaviour is undefined, so checking with \ref cbor_value_get_type or \ref
1160 * cbor_value_is_map is recommended.
1161 *
1162 * If the item is found, it is stored in \a result. If no item is found
1163 * matching the key, then \a result will contain an element of type \ref
1164 * CborInvalidType. Matching is performed using
1165 * cbor_value_text_string_equals(), so tagged strings will also match.
1166 *
1167 * This function has a time complexity of O(n) where n is the number of
1168 * elements in the map to be searched. In addition, this function is has O(n)
1169 * memory requirement based on the number of nested containers (maps or arrays)
1170 * found as elements of this map.
1171 *
1172 * \sa cbor_value_is_valid(), cbor_value_text_string_equals(), cbor_value_advance()
Thiago Macieira7b623c22015-05-11 15:52:14 +09001173 */
1174CborError cbor_value_map_find_value(const CborValue *map, const char *string, CborValue *element)
1175{
1176 assert(cbor_value_is_map(map));
1177 size_t len = strlen(string);
1178 CborError err = cbor_value_enter_container(map, element);
1179 if (err)
1180 goto error;
1181
1182 while (!cbor_value_at_end(element)) {
Thiago Macieiradbc01292016-06-06 17:02:25 -07001183 /* find the non-tag so we can compare */
Thiago Macieira7b623c22015-05-11 15:52:14 +09001184 err = cbor_value_skip_tag(element);
1185 if (err)
1186 goto error;
1187 if (cbor_value_is_text_string(element)) {
1188 bool equals;
1189 size_t dummyLen = len;
1190 err = iterate_string_chunks(element, CONST_CAST(char *, string), &dummyLen,
1191 &equals, element, iterate_memcmp);
1192 if (err)
1193 goto error;
1194 if (equals)
1195 return preparse_value(element);
1196 } else {
Thiago Macieiradbc01292016-06-06 17:02:25 -07001197 /* skip this key */
Thiago Macieira7b623c22015-05-11 15:52:14 +09001198 err = cbor_value_advance(element);
1199 if (err)
1200 goto error;
1201 }
1202
Thiago Macieiradbc01292016-06-06 17:02:25 -07001203 /* skip this value */
Thiago Macieira7b623c22015-05-11 15:52:14 +09001204 err = cbor_value_skip_tag(element);
1205 if (err)
1206 goto error;
1207 err = cbor_value_advance(element);
1208 if (err)
1209 goto error;
1210 }
1211
Thiago Macieiradbc01292016-06-06 17:02:25 -07001212 /* not found */
Thiago Macieira7b623c22015-05-11 15:52:14 +09001213 element->type = CborInvalidType;
1214 return CborNoError;
1215
1216error:
1217 element->type = CborInvalidType;
1218 return err;
1219}
1220
1221/**
Thiago Macieira46a818e2015-10-08 15:13:05 +02001222 * \fn bool cbor_value_is_float(const CborValue *value)
1223 *
1224 * Returns true if the iterator \a value is valid and points to a CBOR
1225 * single-precision floating point (32-bit).
1226 *
1227 * \sa cbor_value_is_valid(), cbor_value_is_double(), cbor_value_is_half_float()
1228 */
1229
1230/**
1231 * \fn CborError cbor_value_get_float(const CborValue *value, float *result)
1232 *
1233 * Retrieves the CBOR single-precision floating point (32-bit) value that \a
1234 * value points to and stores it in \a result. If the iterator \a value does
1235 * not point to a single-precision floating point value, the behavior is
1236 * undefined, so checking with \ref cbor_value_get_type or with \ref
1237 * cbor_value_is_float is recommended.
1238 *
1239 * \sa cbor_value_get_type(), cbor_value_is_valid(), cbor_value_is_float(), cbor_value_get_double()
1240 */
1241
1242/**
1243 * \fn bool cbor_value_is_double(const CborValue *value)
1244 *
1245 * Returns true if the iterator \a value is valid and points to a CBOR
1246 * double-precision floating point (64-bit).
1247 *
1248 * \sa cbor_value_is_valid(), cbor_value_is_float(), cbor_value_is_half_float()
1249 */
1250
1251/**
1252 * \fn CborError cbor_value_get_double(const CborValue *value, float *result)
1253 *
1254 * Retrieves the CBOR double-precision floating point (64-bit) value that \a
1255 * value points to and stores it in \a result. If the iterator \a value does
1256 * not point to a double-precision floating point value, the behavior is
1257 * undefined, so checking with \ref cbor_value_get_type or with \ref
1258 * cbor_value_is_double is recommended.
1259 *
1260 * \sa cbor_value_get_type(), cbor_value_is_valid(), cbor_value_is_double(), cbor_value_get_float()
1261 */
1262
1263/**
1264 * \fn bool cbor_value_is_half_float(const CborValue *value)
1265 *
1266 * Returns true if the iterator \a value is valid and points to a CBOR
1267 * single-precision floating point (16-bit).
1268 *
1269 * \sa cbor_value_is_valid(), cbor_value_is_double(), cbor_value_is_float()
1270 */
1271
1272/**
1273 * Retrieves the CBOR half-precision floating point (16-bit) value that \a
1274 * value points to and stores it in \a result. If the iterator \a value does
1275 * not point to a half-precision floating point value, the behavior is
1276 * undefined, so checking with \ref cbor_value_get_type or with \ref
1277 * cbor_value_is_half_float is recommended.
1278 *
1279 * Note: since the C language does not have a standard type for half-precision
1280 * floating point, this function takes a \c{void *} as a parameter for the
1281 * storage area, which must be at least 16 bits wide.
1282 *
1283 * \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 -07001284 */
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -07001285CborError cbor_value_get_half_float(const CborValue *value, void *result)
Thiago Macieirac70169f2015-05-06 07:49:44 -07001286{
Thiago Macieiracac5db52016-07-07 15:50:59 -07001287 assert(cbor_value_is_half_float(value));
Thiago Macieirac70169f2015-05-06 07:49:44 -07001288
Thiago Macieiradbc01292016-06-06 17:02:25 -07001289 /* size has been computed already */
Thiago Macieirac70169f2015-05-06 07:49:44 -07001290 uint16_t v = get16(value->ptr + 1);
1291 memcpy(result, &v, sizeof(v));
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -07001292 return CborNoError;
Thiago Macieira54a0e102015-05-05 21:25:06 -07001293}
Thiago Macieira46a818e2015-10-08 15:13:05 +02001294
1295/** @} */