blob: a1a360dae0ef9fe386780e948f50a1ce0bbc9a6f [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
Thiago Macieira54a0e102015-05-05 21:25:06 -070026#include "cbor.h"
27#include "cborconstants_p.h"
28#include "compilersupport_p.h"
Thiago Macieira4e9626c2015-09-21 14:57:17 -070029#include "extract_number_p.h"
Thiago Macieira54a0e102015-05-05 21:25:06 -070030
31#include <assert.h>
Thiago Macieira2312efd2015-05-06 16:07:48 -070032#include <stdlib.h>
Thiago Macieira54a0e102015-05-05 21:25:06 -070033#include <string.h>
34
Thiago Macieira8f3fb782015-06-16 16:27:01 -070035#include "assert_p.h" /* Always include last */
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 Macieiraf5cb94b2015-06-16 16:10:49 -0700145static CborError extract_length(const CborParser *parser, const uint8_t **ptr, size_t *len)
Thiago Macieira54a0e102015-05-05 21:25:06 -0700146{
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700147 uint64_t v;
Thiago Macieira4e9626c2015-09-21 14:57:17 -0700148 CborError err = extract_number(ptr, parser->end, &v);
Mike Colagrosso629d5b72016-02-24 15:12:34 -0700149 if (err) {
150 *len = 0;
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700151 return err;
Mike Colagrosso629d5b72016-02-24 15:12:34 -0700152 }
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700153
154 *len = v;
155 if (v != *len)
156 return CborErrorDataTooLarge;
157 return CborNoError;
158}
159
160static bool is_fixed_type(uint8_t type)
161{
162 return type != CborTextStringType && type != CborByteStringType && type != CborArrayType &&
163 type != CborMapType;
164}
165
166static CborError preparse_value(CborValue *it)
167{
168 const CborParser *parser = it->parser;
Thiago Macieira11e913f2015-05-07 13:01:18 -0700169 it->type = CborInvalidType;
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700170
Thiago Macieiradbc01292016-06-06 17:02:25 -0700171 /* are we at the end? */
Thiago Macieira54a0e102015-05-05 21:25:06 -0700172 if (it->ptr == parser->end)
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700173 return CborErrorUnexpectedEOF;
Thiago Macieira54a0e102015-05-05 21:25:06 -0700174
175 uint8_t descriptor = *it->ptr;
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700176 uint8_t type = descriptor & MajorTypeMask;
Thiago Macieira851c4812015-05-08 15:23:20 -0700177 it->type = type;
Thiago Macieira54a0e102015-05-05 21:25:06 -0700178 it->flags = 0;
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700179 it->extra = (descriptor &= SmallValueMask);
180
Thiago Macieira56d99832015-05-07 14:34:27 -0700181 if (descriptor > Value64Bit) {
182 if (unlikely(descriptor != IndefiniteLength))
Thiago Macieira3f76f632015-05-12 10:10:09 +0900183 return type == CborSimpleType ? CborErrorUnknownType : CborErrorIllegalNumber;
Thiago Macieira56d99832015-05-07 14:34:27 -0700184 if (likely(!is_fixed_type(type))) {
Thiago Macieiradbc01292016-06-06 17:02:25 -0700185 /* special case */
Thiago Macieira56d99832015-05-07 14:34:27 -0700186 it->flags |= CborIteratorFlag_UnknownLength;
187 it->type = type;
188 return CborNoError;
189 }
190 return type == CborSimpleType ? CborErrorUnexpectedBreak : CborErrorIllegalNumber;
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700191 }
Thiago Macieira54a0e102015-05-05 21:25:06 -0700192
Thiago Macieirac70169f2015-05-06 07:49:44 -0700193 size_t bytesNeeded = descriptor < Value8Bit ? 0 : (1 << (descriptor - Value8Bit));
Thiago Macieira63abed92015-10-28 17:01:14 -0700194 if (bytesNeeded + 1 > (size_t)(parser->end - it->ptr))
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700195 return CborErrorUnexpectedEOF;
Thiago Macieirac70169f2015-05-06 07:49:44 -0700196
Thiago Macieira851c4812015-05-08 15:23:20 -0700197 uint8_t majortype = type >> MajorTypeShift;
198 if (majortype == NegativeIntegerType) {
Thiago Macieira54a0e102015-05-05 21:25:06 -0700199 it->flags |= CborIteratorFlag_NegativeInteger;
Thiago Macieira851c4812015-05-08 15:23:20 -0700200 it->type = CborIntegerType;
201 } else if (majortype == SimpleTypesType) {
Thiago Macieira54a0e102015-05-05 21:25:06 -0700202 switch (descriptor) {
203 case FalseValue:
204 it->extra = false;
Thiago Macieira851c4812015-05-08 15:23:20 -0700205 it->type = CborBooleanType;
Thiago Macieira991dd922015-05-07 11:57:59 -0700206 break;
207
Thiago Macieira851c4812015-05-08 15:23:20 -0700208 case SinglePrecisionFloat:
209 case DoublePrecisionFloat:
210 it->flags |= CborIteratorFlag_IntegerValueTooLarge;
Thiago Macieiradbc01292016-06-06 17:02:25 -0700211 /* fall through */
Thiago Macieira54a0e102015-05-05 21:25:06 -0700212 case TrueValue:
213 case NullValue:
214 case UndefinedValue:
215 case HalfPrecisionFloat:
Thiago Macieira851c4812015-05-08 15:23:20 -0700216 it->type = *it->ptr;
Thiago Macieira54a0e102015-05-05 21:25:06 -0700217 break;
218
219 case SimpleTypeInNextByte:
Thiago Macieira851c4812015-05-08 15:23:20 -0700220 it->extra = (uint8_t)it->ptr[1];
Thiago Macieira54a0e102015-05-05 21:25:06 -0700221#ifndef CBOR_PARSER_NO_STRICT_CHECKS
Thiago Macieira851c4812015-05-08 15:23:20 -0700222 if (unlikely(it->extra < 32)) {
223 it->type = CborInvalidType;
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700224 return CborErrorIllegalSimpleType;
Thiago Macieira851c4812015-05-08 15:23:20 -0700225 }
Thiago Macieira54a0e102015-05-05 21:25:06 -0700226#endif
Thiago Macieira991dd922015-05-07 11:57:59 -0700227 break;
228
Thiago Macieira54a0e102015-05-05 21:25:06 -0700229 case 28:
230 case 29:
231 case 30:
Thiago Macieira54a0e102015-05-05 21:25:06 -0700232 case Break:
Thiago Macieiradbc01292016-06-06 17:02:25 -0700233 assert(false); /* these conditions can't be reached */
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700234 return CborErrorUnexpectedBreak;
Thiago Macieira54a0e102015-05-05 21:25:06 -0700235 }
Thiago Macieira851c4812015-05-08 15:23:20 -0700236 return CborNoError;
Thiago Macieira54a0e102015-05-05 21:25:06 -0700237 }
238
Thiago Macieiradbc01292016-06-06 17:02:25 -0700239 /* try to decode up to 16 bits */
Thiago Macieira54a0e102015-05-05 21:25:06 -0700240 if (descriptor < Value8Bit)
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700241 return CborNoError;
Thiago Macieira54a0e102015-05-05 21:25:06 -0700242
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700243 if (descriptor == Value8Bit)
244 it->extra = (uint8_t)it->ptr[1];
245 else if (descriptor == Value16Bit)
Thiago Macieira54a0e102015-05-05 21:25:06 -0700246 it->extra = get16(it->ptr + 1);
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700247 else
Thiago Macieiradbc01292016-06-06 17:02:25 -0700248 it->flags |= CborIteratorFlag_IntegerValueTooLarge; /* Value32Bit or Value64Bit */
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700249 return CborNoError;
250}
Thiago Macieira54a0e102015-05-05 21:25:06 -0700251
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700252static CborError preparse_next_value(CborValue *it)
253{
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700254 if (it->remaining != UINT32_MAX) {
Thiago Macieiradbc01292016-06-06 17:02:25 -0700255 /* don't decrement the item count if the current item is tag: they don't count */
Thiago Macieira11e913f2015-05-07 13:01:18 -0700256 if (it->type != CborTagType && !--it->remaining) {
257 it->type = CborInvalidType;
Thiago Macieira56d99832015-05-07 14:34:27 -0700258 return CborNoError;
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700259 }
Thiago Macieira5752ce52015-06-16 12:10:03 -0700260 } else if (it->remaining == UINT32_MAX && it->ptr != it->parser->end && *it->ptr == (uint8_t)BreakByte) {
Thiago Macieiradbc01292016-06-06 17:02:25 -0700261 /* end of map or array */
Thiago Macieira56d99832015-05-07 14:34:27 -0700262 ++it->ptr;
263 it->type = CborInvalidType;
264 it->remaining = 0;
265 return CborNoError;
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700266 }
Thiago Macieira56d99832015-05-07 14:34:27 -0700267
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700268 return preparse_value(it);
269}
270
271static CborError advance_internal(CborValue *it)
272{
273 uint64_t length;
Thiago Macieira4e9626c2015-09-21 14:57:17 -0700274 CborError err = extract_number(&it->ptr, it->parser->end, &length);
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700275 assert(err == CborNoError);
276
Thiago Macieira56d99832015-05-07 14:34:27 -0700277 if (it->type == CborByteStringType || it->type == CborTextStringType) {
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700278 assert(length == (size_t)length);
Thiago Macieira56d99832015-05-07 14:34:27 -0700279 assert((it->flags & CborIteratorFlag_UnknownLength) == 0);
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700280 it->ptr += length;
281 }
282
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700283 return preparse_next_value(it);
Thiago Macieira54a0e102015-05-05 21:25:06 -0700284}
285
Thiago Macieira2312efd2015-05-06 16:07:48 -0700286/** \internal
287 *
288 * Decodes the CBOR integer value when it is larger than the 16 bits available
289 * in value->extra. This function requires that value->flags have the
290 * CborIteratorFlag_IntegerValueTooLarge flag set.
291 *
292 * This function is also used to extract single- and double-precision floating
293 * point values (SinglePrecisionFloat == Value32Bit and DoublePrecisionFloat ==
294 * Value64Bit).
295 */
Thiago Macieira54a0e102015-05-05 21:25:06 -0700296uint64_t _cbor_value_decode_int64_internal(const CborValue *value)
297{
Thiago Macieira2312efd2015-05-06 16:07:48 -0700298 assert(value->flags & CborIteratorFlag_IntegerValueTooLarge ||
299 value->type == CborFloatType || value->type == CborDoubleType);
Thiago Macieira851c4812015-05-08 15:23:20 -0700300
Thiago Macieiradbc01292016-06-06 17:02:25 -0700301 /* since the additional information can only be Value32Bit or Value64Bit,
302 * we just need to test for the one bit those two options differ */
Thiago Macieira851c4812015-05-08 15:23:20 -0700303 assert((*value->ptr & SmallValueMask) == Value32Bit || (*value->ptr & SmallValueMask) == Value64Bit);
304 if ((*value->ptr & 1) == (Value32Bit & 1))
Thiago Macieira54a0e102015-05-05 21:25:06 -0700305 return get32(value->ptr + 1);
306
307 assert((*value->ptr & SmallValueMask) == Value64Bit);
308 return get64(value->ptr + 1);
309}
310
311/**
312 * Initializes the CBOR parser for parsing \a size bytes beginning at \a
313 * buffer. Parsing will use flags set in \a flags. The iterator to the first
314 * element is returned in \a it.
Thiago Macieira2312efd2015-05-06 16:07:48 -0700315 *
316 * The \a parser structure needs to remain valid throughout the decoding
317 * process. It is not thread-safe to share one CborParser among multiple
318 * threads iterating at the same time, but the object can be copied so multiple
319 * threads can iterate.
Thiago Macieira54a0e102015-05-05 21:25:06 -0700320 */
Thiago Macieira5752ce52015-06-16 12:10:03 -0700321CborError cbor_parser_init(const uint8_t *buffer, size_t size, int flags, CborParser *parser, CborValue *it)
Thiago Macieira54a0e102015-05-05 21:25:06 -0700322{
323 memset(parser, 0, sizeof(*parser));
324 parser->end = buffer + size;
Thiago Macieira54a0e102015-05-05 21:25:06 -0700325 parser->flags = flags;
326 it->parser = parser;
327 it->ptr = buffer;
Thiago Macieiradbc01292016-06-06 17:02:25 -0700328 it->remaining = 1; /* there's one type altogether, usually an array or map */
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700329 return preparse_value(it);
Thiago Macieira2312efd2015-05-06 16:07:48 -0700330}
331
332/**
Thiago Macieira46a818e2015-10-08 15:13:05 +0200333 * \fn bool cbor_value_at_end(const CborValue *it)
334 *
335 * Returns true if \a it has reached the end of the iteration, usually when
Thiago Macieira740e29d2016-07-07 13:32:47 -0700336 * advancing after the last item in an array or map.
Thiago Macieira46a818e2015-10-08 15:13:05 +0200337 *
Thiago Macieira740e29d2016-07-07 13:32:47 -0700338 * In the case of the outermost CborValue object, this function returns true
339 * after decoding a single element. A pointer to the first byte of the
340 * remaining data (if any) can be obtained with cbor_value_get_next_byte().
341 *
342 * \sa cbor_value_advance(), cbor_value_is_valid(), cbor_value_get_next_byte()
343 */
344
345/**
346 * \fn const uint8_t *cbor_value_get_next_byte(const CborValue *it)
347 *
348 * Returns a pointer to the next byte that would be decoded if this CborValue
349 * object were advanced.
350 *
351 * This function is useful if cbor_value_at_end() returns true for the
352 * outermost CborValue: the pointer returned is the first byte of the data
353 * remaining in the buffer, if any. Code can decide whether to begin decoding a
354 * new CBOR data stream from this point, or parse some other data appended to
355 * the same buffer.
356 *
357 * This function may be used even after a parsing error. If that occurred,
358 * then this function returns a pointer to where the parsing error occurred.
359 * Note that the error recovery is not precise and the pointer may not indicate
360 * the exact byte containing bad data.
361 *
362 * \sa cbor_value_at_end()
Thiago Macieira46a818e2015-10-08 15:13:05 +0200363 */
364
365/**
366 * \fn bool cbor_value_is_valid(const CborValue *it)
367 *
368 * Returns true if the iterator \a it contains a valid value. Invalid iterators
369 * happen when iteration reaches the end of a container (see \ref
370 * cbor_value_at_end()) or when a search function resulted in no matches.
371 *
372 * \sa cbor_value_advance(), cbor_valie_at_end(), cbor_value_get_type()
373 */
374
375/**
Thiago Macieira2312efd2015-05-06 16:07:48 -0700376 * Advances the CBOR value \a it by one fixed-size position. Fixed-size types
377 * are: integers, tags, simple types (including boolean, null and undefined
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700378 * values) and floating point types.
Thiago Macieira2312efd2015-05-06 16:07:48 -0700379 *
Thiago Macieira46a818e2015-10-08 15:13:05 +0200380 * If the type is not of fixed size, this function has undefined behavior. Code
381 * must be sure that the current type is one of the fixed-size types before
382 * calling this function. This function is provided because it can guarantee
383 * that runs in constant time (O(1)).
384 *
385 * If the caller is not able to determine whether the type is fixed or not, code
386 * can use the cbor_value_advance() function instead.
387 *
388 * \sa cbor_value_at_end(), cbor_value_advance(), cbor_value_enter_container(), cbor_value_leave_container()
Thiago Macieira2312efd2015-05-06 16:07:48 -0700389 */
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700390CborError cbor_value_advance_fixed(CborValue *it)
Thiago Macieira54a0e102015-05-05 21:25:06 -0700391{
Thiago Macieira2312efd2015-05-06 16:07:48 -0700392 assert(it->type != CborInvalidType);
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700393 assert(is_fixed_type(it->type));
394 if (!it->remaining)
395 return CborErrorAdvancePastEOF;
396 return advance_internal(it);
Thiago Macieira54a0e102015-05-05 21:25:06 -0700397}
398
Thiago Macieira4a99af92015-05-12 10:41:45 +0900399static CborError advance_recursive(CborValue *it, int nestingLevel)
400{
401 if (is_fixed_type(it->type))
402 return advance_internal(it);
403
404 if (!cbor_value_is_container(it)) {
405 size_t len = SIZE_MAX;
Thiago Macieiraff130bc2015-06-19 15:15:33 -0700406 return _cbor_value_copy_string(it, NULL, &len, it);
Thiago Macieira4a99af92015-05-12 10:41:45 +0900407 }
408
Thiago Macieiradbc01292016-06-06 17:02:25 -0700409 /* map or array */
Thiago Macieira4a99af92015-05-12 10:41:45 +0900410 if (nestingLevel == CBOR_PARSER_MAX_RECURSIONS)
411 return CborErrorNestingTooDeep;
412
413 CborError err;
414 CborValue recursed;
415 err = cbor_value_enter_container(it, &recursed);
416 if (err)
417 return err;
418 while (!cbor_value_at_end(&recursed)) {
419 err = advance_recursive(&recursed, nestingLevel + 1);
420 if (err)
421 return err;
422 }
423 return cbor_value_leave_container(it, &recursed);
424}
425
426
Thiago Macieira2312efd2015-05-06 16:07:48 -0700427/**
428 * Advances the CBOR value \a it by one element, skipping over containers.
429 * Unlike cbor_value_advance_fixed(), this function can be called on a CBOR
430 * value of any type. However, if the type is a container (map or array) or a
431 * string with a chunked payload, this function will not run in constant time
432 * and will recurse into itself (it will run on O(n) time for the number of
433 * elements or chunks and will use O(n) memory for the number of nested
434 * containers).
435 *
Thiago Macieira46a818e2015-10-08 15:13:05 +0200436 * \sa cbor_value_at_end(), cbor_value_advance_fixed(), 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(CborValue *it)
Thiago Macieira2312efd2015-05-06 16:07:48 -0700439{
440 assert(it->type != CborInvalidType);
441 if (!it->remaining)
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700442 return CborErrorAdvancePastEOF;
Thiago Macieira4a99af92015-05-12 10:41:45 +0900443 return advance_recursive(it, 0);
Thiago Macieira2312efd2015-05-06 16:07:48 -0700444}
445
446/**
Thiago Macieira46a818e2015-10-08 15:13:05 +0200447 * \fn bool cbor_value_is_tag(const CborValue *value)
448 *
449 * Returns true if the iterator \a value is valid and points to a CBOR tag.
450 *
451 * \sa cbor_value_get_tag(), cbor_value_skip_tag()
452 */
453
454/**
455 * \fn CborError cbor_value_get_tag(const CborValue *value, CborTag *result)
456 *
457 * Retrieves the CBOR tag value that \a value points to and stores it in \a
458 * result. If the iterator \a value does not point to a CBOR tag value, the
459 * behavior is undefined, so checking with \ref cbor_value_get_type or with
460 * \ref cbor_value_is_tag is recommended.
461 *
462 * \sa cbor_value_get_type(), cbor_value_is_valid(), cbor_value_is_tag()
463 */
464
465/**
Thiago Macieirac4a73c62015-05-09 18:14:11 -0700466 * Advances the CBOR value \a it until it no longer points to a tag. If \a it is
467 * already not pointing to a tag, then this function returns it unchanged.
468 *
Thiago Macieira46a818e2015-10-08 15:13:05 +0200469 * This function does not run in constant time: it will run on O(n) for n being
470 * the number of tags. It does use constant memory (O(1) memory requirements).
471 *
Thiago Macieirac4a73c62015-05-09 18:14:11 -0700472 * \sa cbor_value_advance_fixed(), cbor_value_advance()
473 */
474CborError cbor_value_skip_tag(CborValue *it)
475{
476 while (cbor_value_is_tag(it)) {
477 CborError err = cbor_value_advance_fixed(it);
478 if (err)
479 return err;
480 }
481 return CborNoError;
482}
483
Thiago Macieirac4a73c62015-05-09 18:14:11 -0700484/**
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700485 * \fn bool cbor_value_is_container(const CborValue *it)
486 *
Thiago Macieira2312efd2015-05-06 16:07:48 -0700487 * Returns true if the \a it value is a container and requires recursion in
488 * order to decode (maps and arrays), false otherwise.
489 */
Thiago Macieira54a0e102015-05-05 21:25:06 -0700490
Thiago Macieira2312efd2015-05-06 16:07:48 -0700491/**
492 * Creates a CborValue iterator pointing to the first element of the container
493 * represented by \a it and saves it in \a recursed. The \a it container object
494 * needs to be kept and passed again to cbor_value_leave_container() in order
495 * to continue iterating past this container.
496 *
Thiago Macieira46a818e2015-10-08 15:13:05 +0200497 * The \a it CborValue iterator must point to a container.
498 *
Thiago Macieira2312efd2015-05-06 16:07:48 -0700499 * \sa cbor_value_is_container(), cbor_value_leave_container(), cbor_value_advance()
500 */
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700501CborError cbor_value_enter_container(const CborValue *it, CborValue *recursed)
Thiago Macieira54a0e102015-05-05 21:25:06 -0700502{
Thiago Macieira56d99832015-05-07 14:34:27 -0700503 CborError err;
Thiago Macieira2312efd2015-05-06 16:07:48 -0700504 assert(cbor_value_is_container(it));
Thiago Macieira54a0e102015-05-05 21:25:06 -0700505 *recursed = *it;
Thiago Macieira56d99832015-05-07 14:34:27 -0700506
Thiago Macieira54a0e102015-05-05 21:25:06 -0700507 if (it->flags & CborIteratorFlag_UnknownLength) {
508 recursed->remaining = UINT32_MAX;
Thiago Macieira56d99832015-05-07 14:34:27 -0700509 ++recursed->ptr;
510 err = preparse_value(recursed);
511 if (err != CborErrorUnexpectedBreak)
512 return err;
Thiago Macieiradbc01292016-06-06 17:02:25 -0700513 /* actually, break was expected here
514 * it's just an empty container */
Thiago Macieira56d99832015-05-07 14:34:27 -0700515 ++recursed->ptr;
Thiago Macieira54a0e102015-05-05 21:25:06 -0700516 } else {
Thiago Macieira56d99832015-05-07 14:34:27 -0700517 uint64_t len;
Thiago Macieira4e9626c2015-09-21 14:57:17 -0700518 err = extract_number(&recursed->ptr, recursed->parser->end, &len);
Thiago Macieira56d99832015-05-07 14:34:27 -0700519 assert(err == CborNoError);
Thiago Macieira56d99832015-05-07 14:34:27 -0700520
Thiago Macieirae12dfd02016-06-07 16:29:25 -0700521 recursed->remaining = (uint32_t)len;
Thiago Macieira3f76f632015-05-12 10:10:09 +0900522 if (recursed->remaining != len || len == UINT32_MAX) {
Thiago Macieiradbc01292016-06-06 17:02:25 -0700523 /* back track the pointer to indicate where the error occurred */
Thiago Macieira3f76f632015-05-12 10:10:09 +0900524 recursed->ptr = it->ptr;
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700525 return CborErrorDataTooLarge;
Thiago Macieira3f76f632015-05-12 10:10:09 +0900526 }
Thiago Macieirace16f052015-05-07 23:14:25 -0700527 if (recursed->type == CborMapType) {
Thiago Macieiradbc01292016-06-06 17:02:25 -0700528 /* maps have keys and values, so we need to multiply by 2 */
Thiago Macieira3f76f632015-05-12 10:10:09 +0900529 if (recursed->remaining > UINT32_MAX / 2) {
Thiago Macieiradbc01292016-06-06 17:02:25 -0700530 /* back track the pointer to indicate where the error occurred */
Thiago Macieira3f76f632015-05-12 10:10:09 +0900531 recursed->ptr = it->ptr;
Thiago Macieirace16f052015-05-07 23:14:25 -0700532 return CborErrorDataTooLarge;
Thiago Macieira3f76f632015-05-12 10:10:09 +0900533 }
Thiago Macieirace16f052015-05-07 23:14:25 -0700534 recursed->remaining *= 2;
535 }
Thiago Macieira56d99832015-05-07 14:34:27 -0700536 if (len != 0)
537 return preparse_value(recursed);
Thiago Macieira54a0e102015-05-05 21:25:06 -0700538 }
Thiago Macieira56d99832015-05-07 14:34:27 -0700539
Thiago Macieiradbc01292016-06-06 17:02:25 -0700540 /* the case of the empty container */
Thiago Macieira56d99832015-05-07 14:34:27 -0700541 recursed->type = CborInvalidType;
542 recursed->remaining = 0;
543 return CborNoError;
Thiago Macieirac70169f2015-05-06 07:49:44 -0700544}
545
Thiago Macieira2312efd2015-05-06 16:07:48 -0700546/**
547 * Updates \a it to point to the next element after the container. The \a
Thiago Macieira56d99832015-05-07 14:34:27 -0700548 * recursed object needs to point to the element obtained either by advancing
549 * the last element of the container (via cbor_value_advance(),
550 * cbor_value_advance_fixed(), a nested cbor_value_leave_container(), or the \c
551 * next pointer from cbor_value_copy_string() or cbor_value_dup_string()).
Thiago Macieira2312efd2015-05-06 16:07:48 -0700552 *
Thiago Macieira46a818e2015-10-08 15:13:05 +0200553 * The \a it and \a recursed parameters must be the exact same as passed to
554 * cbor_value_enter_container().
555 *
Thiago Macieira2312efd2015-05-06 16:07:48 -0700556 * \sa cbor_value_enter_container(), cbor_value_at_end()
557 */
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700558CborError cbor_value_leave_container(CborValue *it, const CborValue *recursed)
Thiago Macieirac70169f2015-05-06 07:49:44 -0700559{
Thiago Macieira2312efd2015-05-06 16:07:48 -0700560 assert(cbor_value_is_container(it));
Thiago Macieira56d99832015-05-07 14:34:27 -0700561 assert(recursed->type == CborInvalidType);
Thiago Macieirac70169f2015-05-06 07:49:44 -0700562 it->ptr = recursed->ptr;
Thiago Macieira56d99832015-05-07 14:34:27 -0700563 return preparse_next_value(it);
Thiago Macieirac70169f2015-05-06 07:49:44 -0700564}
565
Thiago Macieira46a818e2015-10-08 15:13:05 +0200566
Thiago Macieira2312efd2015-05-06 16:07:48 -0700567/**
Thiago Macieira46a818e2015-10-08 15:13:05 +0200568 * \fn CborType cbor_value_get_type(const CborValue *value)
569 *
570 * Returns the type of the CBOR value that the iterator \a value points to. If
571 * \a value does not point to a valid value, this function returns \ref
572 * CborInvalidType.
573 *
574 * TinyCBOR also provides functions to test directly if a given CborValue object
575 * is of a given type, like cbor_value_is_text_string() and cbor_value_is_null().
576 *
577 * \sa cbor_value_is_valid()
578 */
579
580/**
581 * \fn bool cbor_value_is_null(const CborValue *value)
582 *
583 * Returns true if the iterator \a value is valid and points to a CBOR null type.
584 *
585 * \sa cbor_value_is_valid(), cbor_value_is_undefined()
586 */
587
588/**
589 * \fn bool cbor_value_is_undefined(const CborValue *value)
590 *
591 * Returns true if the iterator \a value is valid and points to a CBOR undefined type.
592 *
593 * \sa cbor_value_is_valid(), cbor_value_is_null()
594 */
595
596/**
597 * \fn bool cbor_value_is_boolean(const CborValue *value)
598 *
599 * Returns true if the iterator \a value is valid and points to a CBOR boolean
600 * type (true or false).
601 *
602 * \sa cbor_value_is_valid(), cbor_value_get_boolean()
603 */
604
605/**
606 * \fn CborError cbor_value_get_boolean(const CborValue *value, bool *result)
607 *
608 * Retrieves the boolean value that \a value points to and stores it in \a
609 * result. If the iterator \a value does not point to a boolean value, the
610 * behavior is undefined, so checking with \ref cbor_value_get_type or with
611 * \ref cbor_value_is_boolean is recommended.
612 *
613 * \sa cbor_value_get_type(), cbor_value_is_valid(), cbor_value_is_boolean()
614 */
615
616/**
617 * \fn bool cbor_value_is_simple_type(const CborValue *value)
618 *
619 * Returns true if the iterator \a value is valid and points to a CBOR Simple Type
620 * type (other than true, false, null and undefined).
621 *
622 * \sa cbor_value_is_valid(), cbor_value_get_simple_type()
623 */
624
625/**
626 * \fn CborError cbor_value_get_simple_type(const CborValue *value, uint8_t *result)
627 *
628 * Retrieves the CBOR Simple Type value that \a value points to and stores it
629 * in \a result. If the iterator \a value does not point to a simple_type
630 * value, the behavior is undefined, so checking with \ref cbor_value_get_type
631 * or with \ref cbor_value_is_simple_type is recommended.
632 *
633 * \sa cbor_value_get_type(), cbor_value_is_valid(), cbor_value_is_simple_type()
634 */
635
636/**
637 * \fn bool cbor_value_is_integer(const CborValue *value)
638 *
639 * Returns true if the iterator \a value is valid and points to a CBOR integer
640 * type.
641 *
642 * \sa cbor_value_is_valid(), cbor_value_get_int, cbor_value_get_int64, cbor_value_get_uint64, cbor_value_get_raw_integer
643 */
644
645/**
646 * \fn bool cbor_value_is_unsigned_integer(const CborValue *value)
647 *
648 * Returns true if the iterator \a value is valid and points to a CBOR unsigned
649 * integer type (positive values or zero).
650 *
651 * \sa cbor_value_is_valid(), cbor_value_get_uint64()
652 */
653
654/**
655 * \fn bool cbor_value_is_negative_integer(const CborValue *value)
656 *
657 * Returns true if the iterator \a value is valid and points to a CBOR negative
658 * integer type.
659 *
660 * \sa cbor_value_is_valid(), cbor_value_get_int, cbor_value_get_int64, cbor_value_get_raw_integer
661 */
662
663/**
664 * \fn CborError cbor_value_get_int(const CborValue *value, int *result)
665 *
666 * Retrieves the CBOR integer value that \a value points to and stores it in \a
667 * result. If the iterator \a value does not point to an integer value, the
668 * behavior is undefined, so checking with \ref cbor_value_get_type or with
669 * \ref cbor_value_is_integer is recommended.
670 *
671 * Note that this function does not do range-checking: integral values that do
672 * not fit in a variable of type \c{int} are silently truncated to fit. Use
673 * cbor_value_get_int_checked() that is not acceptable.
674 *
675 * \sa cbor_value_get_type(), cbor_value_is_valid(), cbor_value_is_integer()
676 */
677
678/**
679 * \fn CborError cbor_value_get_int64(const CborValue *value, int64_t *result)
680 *
681 * Retrieves the CBOR integer value that \a value points to and stores it in \a
682 * result. If the iterator \a value does not point to an integer value, the
683 * behavior is undefined, so checking with \ref cbor_value_get_type or with
684 * \ref cbor_value_is_integer is recommended.
685 *
686 * Note that this function does not do range-checking: integral values that do
687 * not fit in a variable of type \c{int64_t} are silently truncated to fit. Use
688 * cbor_value_get_int64_checked() that is not acceptable.
689 *
690 * \sa cbor_value_get_type(), cbor_value_is_valid(), cbor_value_is_integer()
691 */
692
693/**
694 * \fn CborError cbor_value_get_uint64(const CborValue *value, uint64_t *result)
695 *
696 * Retrieves the CBOR integer value that \a value points to and stores it in \a
697 * result. If the iterator \a value does not point to an unsigned integer
698 * value, the behavior is undefined, so checking with \ref cbor_value_get_type
699 * or with \ref cbor_value_is_unsigned_integer is recommended.
700 *
701 * \sa cbor_value_get_type(), cbor_value_is_valid(), cbor_value_is_unsigned_integer()
702 */
703
704/**
705 * \fn CborError cbor_value_get_raw_integer(const CborValue *value, uint64_t *result)
706 *
707 * Retrieves the CBOR integer value that \a value points to and stores it in \a
708 * result. If the iterator \a value does not point to an integer value, the
709 * behavior is undefined, so checking with \ref cbor_value_get_type or with
710 * \ref cbor_value_is_integer is recommended.
711 *
712 * This function is provided because CBOR negative integers can assume values
713 * that cannot be represented with normal 64-bit integer variables.
714 *
715 * If the integer is unsigned (that is, if cbor_value_is_unsigned_integer()
716 * returns true), then \a result will contain the actual value. If the integer
717 * is negative, then \a result will contain the absolute value of that integer,
718 * minus one. That is, \c {actual = -result - 1}. On architectures using two's
719 * complement for representation of negative integers, it is equivalent to say
720 * that \a result will contain the bitwise negation of the actual value.
721 *
722 * \sa cbor_value_get_type(), cbor_value_is_valid(), cbor_value_is_integer()
723 */
724
725/**
726 * \fn bool cbor_value_is_length_known(const CborValue *value)
727 *
728 * Returns true if the length of this type is known without calculation. That
729 * is, if the length of this CBOR string, map or array is encoded in the data
730 * stream, this function returns true. If the length is not encoded, it returns
731 * false.
732 *
733 * If the length is known, code can call cbor_value_get_string_length(),
734 * cbor_value_get_array_length() or cbor_value_get_map_length() to obtain the
735 * length. If the length is not known but is necessary, code can use the
736 * cbor_value_calculate_string_length() function (no equivalent function is
737 * provided for maps and arrays).
738 */
739
740/**
741 * \fn bool cbor_value_is_text_string(const CborValue *value)
742 *
743 * Returns true if the iterator \a value is valid and points to a CBOR text
744 * string. CBOR text strings are UTF-8 encoded and usually contain
745 * human-readable text.
746 *
747 * \sa cbor_value_is_valid(), cbor_value_get_string_length(), cbor_value_calculate_string_length(),
748 * cbor_value_copy_text_string(), cbor_value_dup_text_string()
749 */
750
751/**
752 * \fn bool cbor_value_is_byte_string(const CborValue *value)
753 *
754 * Returns true if the iterator \a value is valid and points to a CBOR text
755 * string. CBOR byte strings are binary data with no specified encoding or
756 * format.
757 *
758 * \sa cbor_value_is_valid(), cbor_value_get_string_length(), cbor_value_calculate_string_length(),
759 * cbor_value_copy_byte_string(), cbor_value_dup_byte_string()
760 */
761
762/**
763 * \fn CborError cbor_value_get_string_length(const CborValue *value, size_t *length)
764 *
765 * Extracts the length of the byte or text string that \a value points to and
766 * stores it in \a result. If the iterator \a value does not point to a text
767 * string or a byte string, the behaviour is undefined, so checking with \ref
768 * cbor_value_get_type, with \ref cbor_value_is_text_string or \ref
769 * cbor_value_is_byte_string is recommended.
770 *
771 * If the length of this string is not encoded in the CBOR data stream, this
772 * function will return the recoverable error CborErrorUnknownLength. You may
773 * also check whether that is the case by using cbor_value_is_length_known().
774 *
775 * If the length of the string is required but the length was not encoded, use
776 * cbor_value_calculate_string_length(), but note that that function does not
777 * run in constant time.
778 *
779 * \note On 32-bit platforms, this function will return error condition of \ref
780 * CborErrorDataTooLarge if the stream indicates a length that is too big to
781 * fit in 32-bit.
782 *
783 * \sa cbor_value_is_valid(), cbor_value_is_length_known(), cbor_value_calculate_string_length()
784 */
785
786/**
787 * Calculates the length of the byte or text string that \a value points to and
788 * stores it in \a len. If the iterator \a value does not point to a text
789 * string or a byte string, the behaviour is undefined, so checking with \ref
790 * cbor_value_get_type, with \ref cbor_value_is_text_string or \ref
791 * cbor_value_is_byte_string is recommended.
792 *
793 * This function is different from cbor_value_get_string_length() in that it
794 * calculates the length even for strings sent in chunks. For that reason, this
795 * function may not run in constant time (it will run in O(n) time on the
796 * number of chunks). It does use constant memory (O(1)).
Thiago Macieira2312efd2015-05-06 16:07:48 -0700797 *
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700798 * \note On 32-bit platforms, this function will return error condition of \ref
799 * CborErrorDataTooLarge if the stream indicates a length that is too big to
800 * fit in 32-bit.
Thiago Macieira2312efd2015-05-06 16:07:48 -0700801 *
802 * \sa cbor_value_get_string_length(), cbor_value_copy_string(), cbor_value_is_length_known()
803 */
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700804CborError cbor_value_calculate_string_length(const CborValue *value, size_t *len)
Thiago Macieira2312efd2015-05-06 16:07:48 -0700805{
Thiago Macieira9ae05812015-05-11 15:09:09 +0900806 *len = SIZE_MAX;
Thiago Macieiraff130bc2015-06-19 15:15:33 -0700807 return _cbor_value_copy_string(value, NULL, len, NULL);
Thiago Macieirac70169f2015-05-06 07:49:44 -0700808}
809
Thiago Macieira2312efd2015-05-06 16:07:48 -0700810/**
Thiago Macieiraff130bc2015-06-19 15:15:33 -0700811 * \fn CborError cbor_value_dup_text_string(const CborValue *value, char **buffer, size_t *buflen, CborValue *next)
812 *
Thiago Macieira2312efd2015-05-06 16:07:48 -0700813 * Allocates memory for the string pointed by \a value and copies it into this
814 * buffer. The pointer to the buffer is stored in \a buffer and the number of
815 * bytes copied is stored in \a len (those variables must not be NULL).
816 *
Thiago Macieira46a818e2015-10-08 15:13:05 +0200817 * If the iterator \a value does not point to a text string, the behaviour is
818 * undefined, so checking with \ref cbor_value_get_type or \ref
819 * cbor_value_is_text_string is recommended.
820 *
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700821 * If \c malloc returns a NULL pointer, this function will return error
822 * condition \ref CborErrorOutOfMemory.
Thiago Macieira2312efd2015-05-06 16:07:48 -0700823 *
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700824 * On success, \c{*buffer} will contain a valid pointer that must be freed by
825 * calling \c{free()}. This is the case even for zero-length strings.
Thiago Macieira2312efd2015-05-06 16:07:48 -0700826 *
827 * The \a next pointer, if not null, will be updated to point to the next item
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700828 * after this string. If \a value points to the last item, then \a next will be
829 * invalid.
Thiago Macieira2312efd2015-05-06 16:07:48 -0700830 *
Thiago Macieira46a818e2015-10-08 15:13:05 +0200831 * This function may not run in constant time (it will run in O(n) time on the
832 * number of chunks). It requires constant memory (O(1)) in addition to the
833 * malloc'ed block.
834 *
Thiago Macieira2312efd2015-05-06 16:07:48 -0700835 * \note This function does not perform UTF-8 validation on the incoming text
836 * string.
837 *
Thiago Macieiraff130bc2015-06-19 15:15:33 -0700838 * \sa cbor_value_copy_text_string(), cbor_value_dup_byte_string()
Thiago Macieira2312efd2015-05-06 16:07:48 -0700839 */
Thiago Macieiraff130bc2015-06-19 15:15:33 -0700840
841/**
842 * \fn CborError cbor_value_dup_byte_string(const CborValue *value, uint8_t **buffer, size_t *buflen, CborValue *next)
843 *
844 * Allocates memory for the string pointed by \a value and copies it into this
845 * buffer. The pointer to the buffer is stored in \a buffer and the number of
846 * bytes copied is stored in \a len (those variables must not be NULL).
847 *
Thiago Macieira46a818e2015-10-08 15:13:05 +0200848 * If the iterator \a value does not point to a byte string, the behaviour is
849 * undefined, so checking with \ref cbor_value_get_type or \ref
850 * cbor_value_is_byte_string is recommended.
851 *
Thiago Macieiraff130bc2015-06-19 15:15:33 -0700852 * If \c malloc returns a NULL pointer, this function will return error
853 * condition \ref CborErrorOutOfMemory.
854 *
855 * On success, \c{*buffer} will contain a valid pointer that must be freed by
856 * calling \c{free()}. This is the case even for zero-length strings.
857 *
858 * The \a next pointer, if not null, will be updated to point to the next item
859 * after this string. If \a value points to the last item, then \a next will be
860 * invalid.
861 *
Thiago Macieira46a818e2015-10-08 15:13:05 +0200862 * This function may not run in constant time (it will run in O(n) time on the
863 * number of chunks). It requires constant memory (O(1)) in addition to the
864 * malloc'ed block.
865 *
Thiago Macieiraff130bc2015-06-19 15:15:33 -0700866 * \sa cbor_value_copy_byte_string(), cbor_value_dup_text_string()
867 */
Thiago Macieira46a818e2015-10-08 15:13:05 +0200868
Thiago Macieiraff130bc2015-06-19 15:15:33 -0700869CborError _cbor_value_dup_string(const CborValue *value, void **buffer, size_t *buflen, CborValue *next)
Thiago Macieirac70169f2015-05-06 07:49:44 -0700870{
Thiago Macieira2312efd2015-05-06 16:07:48 -0700871 assert(buffer);
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700872 assert(buflen);
Thiago Macieirafc870932015-06-19 15:01:35 -0700873 *buflen = SIZE_MAX;
Thiago Macieiraff130bc2015-06-19 15:15:33 -0700874 CborError err = _cbor_value_copy_string(value, NULL, buflen, NULL);
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700875 if (err)
876 return err;
Thiago Macieirac70169f2015-05-06 07:49:44 -0700877
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700878 ++*buflen;
879 *buffer = malloc(*buflen);
Thiago Macieira2312efd2015-05-06 16:07:48 -0700880 if (!*buffer) {
Thiago Macieiradbc01292016-06-06 17:02:25 -0700881 /* out of memory */
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700882 return CborErrorOutOfMemory;
Thiago Macieirac70169f2015-05-06 07:49:44 -0700883 }
Thiago Macieiraff130bc2015-06-19 15:15:33 -0700884 err = _cbor_value_copy_string(value, *buffer, buflen, next);
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700885 if (err) {
Thiago Macieira2312efd2015-05-06 16:07:48 -0700886 free(*buffer);
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700887 return err;
Thiago Macieirac70169f2015-05-06 07:49:44 -0700888 }
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700889 return CborNoError;
Thiago Macieira2312efd2015-05-06 16:07:48 -0700890}
891
Thiago Macieiradbc01292016-06-06 17:02:25 -0700892/* We return uintptr_t so that we can pass memcpy directly as the iteration
893 * function. The choice is to optimize for memcpy, which is used in the base
894 * parser API (cbor_value_copy_string), while memcmp is used in convenience API
895 * only. */
Thiago Macieira5752ce52015-06-16 12:10:03 -0700896typedef uintptr_t (*IterateFunction)(char *, const uint8_t *, size_t);
Thiago Macieira9ae05812015-05-11 15:09:09 +0900897
Thiago Macieira5752ce52015-06-16 12:10:03 -0700898static uintptr_t iterate_noop(char *dest, const uint8_t *src, size_t len)
Thiago Macieira9ae05812015-05-11 15:09:09 +0900899{
900 (void)dest;
901 (void)src;
902 (void)len;
903 return true;
904}
905
Thiago Macieira5752ce52015-06-16 12:10:03 -0700906static uintptr_t iterate_memcmp(char *s1, const uint8_t *s2, size_t len)
Thiago Macieirac4a73c62015-05-09 18:14:11 -0700907{
Thiago Macieira5752ce52015-06-16 12:10:03 -0700908 return memcmp(s1, (const char *)s2, len) == 0;
Thiago Macieirac4a73c62015-05-09 18:14:11 -0700909}
910
Thiago Macieira9ae05812015-05-11 15:09:09 +0900911static CborError iterate_string_chunks(const CborValue *value, char *buffer, size_t *buflen,
912 bool *result, CborValue *next, IterateFunction func)
913{
914 assert(cbor_value_is_byte_string(value) || cbor_value_is_text_string(value));
915
916 size_t total;
917 CborError err;
Thiago Macieira5752ce52015-06-16 12:10:03 -0700918 const uint8_t *ptr = value->ptr;
Thiago Macieira9ae05812015-05-11 15:09:09 +0900919 if (cbor_value_is_length_known(value)) {
Thiago Macieiradbc01292016-06-06 17:02:25 -0700920 /* easy case: fixed length */
Thiago Macieira9ae05812015-05-11 15:09:09 +0900921 err = extract_length(value->parser, &ptr, &total);
922 if (err)
923 return err;
Thiago Macieira63abed92015-10-28 17:01:14 -0700924 if (total > (size_t)(value->parser->end - ptr))
Thiago Macieira9ae05812015-05-11 15:09:09 +0900925 return CborErrorUnexpectedEOF;
926 if (total <= *buflen)
Thiago Macieirae12dfd02016-06-07 16:29:25 -0700927 *result = !!func(buffer, ptr, total);
Thiago Macieira9ae05812015-05-11 15:09:09 +0900928 else
929 *result = false;
930 ptr += total;
931 } else {
Thiago Macieiradbc01292016-06-06 17:02:25 -0700932 /* chunked */
Thiago Macieira9ae05812015-05-11 15:09:09 +0900933 ++ptr;
934 total = 0;
935 *result = true;
936 while (true) {
937 size_t chunkLen;
938 size_t newTotal;
939
940 if (ptr == value->parser->end)
941 return CborErrorUnexpectedEOF;
942
Thiago Macieira5752ce52015-06-16 12:10:03 -0700943 if (*ptr == (uint8_t)BreakByte) {
Thiago Macieira9ae05812015-05-11 15:09:09 +0900944 ++ptr;
945 break;
946 }
947
Thiago Macieiradbc01292016-06-06 17:02:25 -0700948 /* is this the right type? */
Thiago Macieira9ae05812015-05-11 15:09:09 +0900949 if ((*ptr & MajorTypeMask) != value->type)
950 return CborErrorIllegalType;
951
952 err = extract_length(value->parser, &ptr, &chunkLen);
953 if (err)
954 return err;
955
Thiago Macieira1de31a42015-06-16 16:01:16 -0700956 if (unlikely(add_check_overflow(total, chunkLen, &newTotal)))
Thiago Macieira9ae05812015-05-11 15:09:09 +0900957 return CborErrorDataTooLarge;
958
Thiago Macieira63abed92015-10-28 17:01:14 -0700959 if (chunkLen > (size_t)(value->parser->end - ptr))
Thiago Macieira9ae05812015-05-11 15:09:09 +0900960 return CborErrorUnexpectedEOF;
961
962 if (*result && *buflen >= newTotal)
Thiago Macieirae12dfd02016-06-07 16:29:25 -0700963 *result = !!func(buffer + total, ptr, chunkLen);
Thiago Macieira9ae05812015-05-11 15:09:09 +0900964 else
965 *result = false;
966
967 ptr += chunkLen;
968 total = newTotal;
969 }
970 }
971
Thiago Macieiradbc01292016-06-06 17:02:25 -0700972 /* is there enough room for the ending NUL byte? */
Thiago Macieira9ae05812015-05-11 15:09:09 +0900973 if (*result && *buflen > total)
Thiago Macieirae12dfd02016-06-07 16:29:25 -0700974 *result = !!func(buffer + total, (const uint8_t *)"", 1);
Thiago Macieira9ae05812015-05-11 15:09:09 +0900975 *buflen = total;
976
977 if (next) {
978 *next = *value;
979 next->ptr = ptr;
980 return preparse_next_value(next);
981 }
982 return CborNoError;
983}
984
Thiago Macieira2312efd2015-05-06 16:07:48 -0700985/**
Thiago Macieiraff130bc2015-06-19 15:15:33 -0700986 * \fn CborError cbor_value_copy_text_string(const CborValue *value, char *buffer, size_t *buflen, CborValue *next)
987 *
Thiago Macieira2312efd2015-05-06 16:07:48 -0700988 * Copies the string pointed by \a value into the buffer provided at \a buffer
989 * of \a buflen bytes. If \a buffer is a NULL pointer, this function will not
990 * copy anything and will only update the \a next value.
991 *
Thiago Macieira46a818e2015-10-08 15:13:05 +0200992 * If the iterator \a value does not point to a text string, the behaviour is
993 * undefined, so checking with \ref cbor_value_get_type or \ref
994 * cbor_value_is_text_string is recommended.
995 *
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700996 * If the provided buffer length was too small, this function returns an error
997 * condition of \ref CborErrorOutOfMemory. If you need to calculate the length
998 * of the string in order to preallocate a buffer, use
Thiago Macieira2312efd2015-05-06 16:07:48 -0700999 * cbor_value_calculate_string_length().
1000 *
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -07001001 * On success, this function sets the number of bytes copied to \c{*buflen}. If
1002 * the buffer is large enough, this function will insert a null byte after the
1003 * last copied byte, to facilitate manipulation of text strings. That byte is
1004 * not included in the returned value of \c{*buflen}.
Thiago Macieira2312efd2015-05-06 16:07:48 -07001005 *
1006 * The \a next pointer, if not null, will be updated to point to the next item
1007 * after this string. If \a value points to the last item, then \a next will be
1008 * invalid.
1009 *
Thiago Macieira46a818e2015-10-08 15:13:05 +02001010 * This function may not run in constant time (it will run in O(n) time on the
1011 * number of chunks). It requires constant memory (O(1)).
1012 *
Thiago Macieira2312efd2015-05-06 16:07:48 -07001013 * \note This function does not perform UTF-8 validation on the incoming text
1014 * string.
1015 *
Thiago Macieiraff130bc2015-06-19 15:15:33 -07001016 * \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 -07001017 */
Thiago Macieiraff130bc2015-06-19 15:15:33 -07001018
1019/**
1020 * \fn CborError cbor_value_copy_byte_string(const CborValue *value, uint8_t *buffer, size_t *buflen, CborValue *next)
1021 *
1022 * Copies the string pointed by \a value into the buffer provided at \a buffer
1023 * of \a buflen bytes. If \a buffer is a NULL pointer, this function will not
1024 * copy anything and will only update the \a next value.
1025 *
Thiago Macieira46a818e2015-10-08 15:13:05 +02001026 * If the iterator \a value does not point to a byte string, the behaviour is
1027 * undefined, so checking with \ref cbor_value_get_type or \ref
1028 * cbor_value_is_byte_string is recommended.
1029 *
Thiago Macieiraff130bc2015-06-19 15:15:33 -07001030 * If the provided buffer length was too small, this function returns an error
1031 * condition of \ref CborErrorOutOfMemory. If you need to calculate the length
1032 * of the string in order to preallocate a buffer, use
1033 * cbor_value_calculate_string_length().
1034 *
1035 * On success, this function sets the number of bytes copied to \c{*buflen}. If
1036 * the buffer is large enough, this function will insert a null byte after the
1037 * last copied byte, to facilitate manipulation of null-terminated strings.
1038 * That byte is not included in the returned value of \c{*buflen}.
1039 *
1040 * The \a next pointer, if not null, will be updated to point to the next item
1041 * after this string. If \a value points to the last item, then \a next will be
1042 * invalid.
1043 *
Thiago Macieira46a818e2015-10-08 15:13:05 +02001044 * This function may not run in constant time (it will run in O(n) time on the
1045 * number of chunks). It requires constant memory (O(1)).
1046 *
Thiago Macieiraff130bc2015-06-19 15:15:33 -07001047 * \sa cbor_value_dup_text_string(), cbor_value_copy_text_string(), cbor_value_get_string_length(), cbor_value_calculate_string_length()
1048 */
1049
1050CborError _cbor_value_copy_string(const CborValue *value, void *buffer,
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -07001051 size_t *buflen, CborValue *next)
Thiago Macieira2312efd2015-05-06 16:07:48 -07001052{
Thiago Macieira9ae05812015-05-11 15:09:09 +09001053 bool copied_all;
Thiago Macieiraed5b57c2015-07-07 16:38:27 -07001054 CborError err = iterate_string_chunks(value, (char*)buffer, buflen, &copied_all, next,
Thiago Macieira9ae05812015-05-11 15:09:09 +09001055 buffer ? (IterateFunction)memcpy : iterate_noop);
1056 return err ? err :
1057 copied_all ? CborNoError : CborErrorOutOfMemory;
Thiago Macieirac70169f2015-05-06 07:49:44 -07001058}
1059
Thiago Macieirac4a73c62015-05-09 18:14:11 -07001060/**
1061 * Compares the entry \a value with the string \a string and store the result
Thiago Macieira46a818e2015-10-08 15:13:05 +02001062 * in \a result. If the value is different from \a string \a result will
1063 * contain \c false.
Thiago Macieirac4a73c62015-05-09 18:14:11 -07001064 *
1065 * The entry at \a value may be a tagged string. If \a is not a string or a
1066 * tagged string, the comparison result will be false.
Thiago Macieira46a818e2015-10-08 15:13:05 +02001067 *
1068 * CBOR requires text strings to be encoded in UTF-8, but this function does
1069 * not validate either the strings in the stream or the string \a string to be
1070 * matched. Moreover, comparison is done on strict codepoint comparison,
1071 * without any Unicode normalization.
1072 *
1073 * This function may not run in constant time (it will run in O(n) time on the
1074 * number of chunks). It requires constant memory (O(1)).
1075 *
1076 * \sa cbor_value_skip_tag(), cbor_value_copy_text_string()
Thiago Macieirac4a73c62015-05-09 18:14:11 -07001077 */
1078CborError cbor_value_text_string_equals(const CborValue *value, const char *string, bool *result)
1079{
1080 CborValue copy = *value;
1081 CborError err = cbor_value_skip_tag(&copy);
1082 if (err)
1083 return err;
1084 if (!cbor_value_is_text_string(&copy)) {
1085 *result = false;
1086 return CborNoError;
1087 }
1088
1089 size_t len = strlen(string);
1090 return iterate_string_chunks(&copy, CONST_CAST(char *, string), &len, result, NULL, iterate_memcmp);
1091}
1092
1093/**
Thiago Macieira46a818e2015-10-08 15:13:05 +02001094 * \fn bool cbor_value_is_array(const CborValue *value)
Thiago Macieira7b623c22015-05-11 15:52:14 +09001095 *
Thiago Macieira46a818e2015-10-08 15:13:05 +02001096 * Returns true if the iterator \a value is valid and points to a CBOR array.
1097 *
1098 * \sa cbor_value_is_valid(), cbor_value_is_map()
1099 */
1100
1101/**
1102 * \fn CborError cbor_value_get_array_length(const CborValue *value, size_t *length)
1103 *
1104 * Extracts the length of the CBOR array that \a value points to and stores it
1105 * in \a result. If the iterator \a value does not point to a CBOR array, the
1106 * behaviour is undefined, so checking with \ref cbor_value_get_type or \ref
1107 * cbor_value_is_array is recommended.
1108 *
1109 * If the length of this array is not encoded in the CBOR data stream, this
1110 * function will return the recoverable error CborErrorUnknownLength. You may
1111 * also check whether that is the case by using cbor_value_is_length_known().
1112 *
1113 * \note On 32-bit platforms, this function will return error condition of \ref
1114 * CborErrorDataTooLarge if the stream indicates a length that is too big to
1115 * fit in 32-bit.
1116 *
1117 * \sa cbor_value_is_valid(), cbor_value_is_length_known()
1118 */
1119
1120/**
1121 * \fn bool cbor_value_is_map(const CborValue *value)
1122 *
1123 * Returns true if the iterator \a value is valid and points to a CBOR map.
1124 *
1125 * \sa cbor_value_is_valid(), cbor_value_is_array()
1126 */
1127
1128/**
1129 * \fn CborError cbor_value_get_map_length(const CborValue *value, size_t *length)
1130 *
1131 * Extracts the length of the CBOR map that \a value points to and stores it in
1132 * \a result. If the iterator \a value does not point to a CBOR map, the
1133 * behaviour is undefined, so checking with \ref cbor_value_get_type or \ref
1134 * cbor_value_is_map is recommended.
1135 *
1136 * If the length of this map is not encoded in the CBOR data stream, this
1137 * function will return the recoverable error CborErrorUnknownLength. You may
1138 * also check whether that is the case by using cbor_value_is_length_known().
1139 *
1140 * \note On 32-bit platforms, this function will return error condition of \ref
1141 * CborErrorDataTooLarge if the stream indicates a length that is too big to
1142 * fit in 32-bit.
1143 *
1144 * \sa cbor_value_is_valid(), cbor_value_is_length_known()
1145 */
1146
1147/**
1148 * Attempts to find the value in map \a map that corresponds to the text string
1149 * entry \a string. If the iterator \a value does not point to a CBOR map, the
1150 * behaviour is undefined, so checking with \ref cbor_value_get_type or \ref
1151 * cbor_value_is_map is recommended.
1152 *
1153 * If the item is found, it is stored in \a result. If no item is found
1154 * matching the key, then \a result will contain an element of type \ref
1155 * CborInvalidType. Matching is performed using
1156 * cbor_value_text_string_equals(), so tagged strings will also match.
1157 *
1158 * This function has a time complexity of O(n) where n is the number of
1159 * elements in the map to be searched. In addition, this function is has O(n)
1160 * memory requirement based on the number of nested containers (maps or arrays)
1161 * found as elements of this map.
1162 *
1163 * \sa cbor_value_is_valid(), cbor_value_text_string_equals(), cbor_value_advance()
Thiago Macieira7b623c22015-05-11 15:52:14 +09001164 */
1165CborError cbor_value_map_find_value(const CborValue *map, const char *string, CborValue *element)
1166{
1167 assert(cbor_value_is_map(map));
1168 size_t len = strlen(string);
1169 CborError err = cbor_value_enter_container(map, element);
1170 if (err)
1171 goto error;
1172
1173 while (!cbor_value_at_end(element)) {
Thiago Macieiradbc01292016-06-06 17:02:25 -07001174 /* find the non-tag so we can compare */
Thiago Macieira7b623c22015-05-11 15:52:14 +09001175 err = cbor_value_skip_tag(element);
1176 if (err)
1177 goto error;
1178 if (cbor_value_is_text_string(element)) {
1179 bool equals;
1180 size_t dummyLen = len;
1181 err = iterate_string_chunks(element, CONST_CAST(char *, string), &dummyLen,
1182 &equals, element, iterate_memcmp);
1183 if (err)
1184 goto error;
1185 if (equals)
1186 return preparse_value(element);
1187 } else {
Thiago Macieiradbc01292016-06-06 17:02:25 -07001188 /* skip this key */
Thiago Macieira7b623c22015-05-11 15:52:14 +09001189 err = cbor_value_advance(element);
1190 if (err)
1191 goto error;
1192 }
1193
Thiago Macieiradbc01292016-06-06 17:02:25 -07001194 /* skip this value */
Thiago Macieira7b623c22015-05-11 15:52:14 +09001195 err = cbor_value_skip_tag(element);
1196 if (err)
1197 goto error;
1198 err = cbor_value_advance(element);
1199 if (err)
1200 goto error;
1201 }
1202
Thiago Macieiradbc01292016-06-06 17:02:25 -07001203 /* not found */
Thiago Macieira7b623c22015-05-11 15:52:14 +09001204 element->type = CborInvalidType;
1205 return CborNoError;
1206
1207error:
1208 element->type = CborInvalidType;
1209 return err;
1210}
1211
1212/**
Thiago Macieira46a818e2015-10-08 15:13:05 +02001213 * \fn bool cbor_value_is_float(const CborValue *value)
1214 *
1215 * Returns true if the iterator \a value is valid and points to a CBOR
1216 * single-precision floating point (32-bit).
1217 *
1218 * \sa cbor_value_is_valid(), cbor_value_is_double(), cbor_value_is_half_float()
1219 */
1220
1221/**
1222 * \fn CborError cbor_value_get_float(const CborValue *value, float *result)
1223 *
1224 * Retrieves the CBOR single-precision floating point (32-bit) value that \a
1225 * value points to and stores it in \a result. If the iterator \a value does
1226 * not point to a single-precision floating point value, the behavior is
1227 * undefined, so checking with \ref cbor_value_get_type or with \ref
1228 * cbor_value_is_float is recommended.
1229 *
1230 * \sa cbor_value_get_type(), cbor_value_is_valid(), cbor_value_is_float(), cbor_value_get_double()
1231 */
1232
1233/**
1234 * \fn bool cbor_value_is_double(const CborValue *value)
1235 *
1236 * Returns true if the iterator \a value is valid and points to a CBOR
1237 * double-precision floating point (64-bit).
1238 *
1239 * \sa cbor_value_is_valid(), cbor_value_is_float(), cbor_value_is_half_float()
1240 */
1241
1242/**
1243 * \fn CborError cbor_value_get_double(const CborValue *value, float *result)
1244 *
1245 * Retrieves the CBOR double-precision floating point (64-bit) value that \a
1246 * value points to and stores it in \a result. If the iterator \a value does
1247 * not point to a double-precision floating point value, the behavior is
1248 * undefined, so checking with \ref cbor_value_get_type or with \ref
1249 * cbor_value_is_double is recommended.
1250 *
1251 * \sa cbor_value_get_type(), cbor_value_is_valid(), cbor_value_is_double(), cbor_value_get_float()
1252 */
1253
1254/**
1255 * \fn bool cbor_value_is_half_float(const CborValue *value)
1256 *
1257 * Returns true if the iterator \a value is valid and points to a CBOR
1258 * single-precision floating point (16-bit).
1259 *
1260 * \sa cbor_value_is_valid(), cbor_value_is_double(), cbor_value_is_float()
1261 */
1262
1263/**
1264 * Retrieves the CBOR half-precision floating point (16-bit) value that \a
1265 * value points to and stores it in \a result. If the iterator \a value does
1266 * not point to a half-precision floating point value, the behavior is
1267 * undefined, so checking with \ref cbor_value_get_type or with \ref
1268 * cbor_value_is_half_float is recommended.
1269 *
1270 * Note: since the C language does not have a standard type for half-precision
1271 * floating point, this function takes a \c{void *} as a parameter for the
1272 * storage area, which must be at least 16 bits wide.
1273 *
1274 * \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 -07001275 */
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -07001276CborError cbor_value_get_half_float(const CborValue *value, void *result)
Thiago Macieirac70169f2015-05-06 07:49:44 -07001277{
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -07001278 assert(value->type == CborHalfFloatType);
Thiago Macieirac70169f2015-05-06 07:49:44 -07001279
Thiago Macieiradbc01292016-06-06 17:02:25 -07001280 /* size has been computed already */
Thiago Macieirac70169f2015-05-06 07:49:44 -07001281 uint16_t v = get16(value->ptr + 1);
1282 memcpy(result, &v, sizeof(v));
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -07001283 return CborNoError;
Thiago Macieira54a0e102015-05-05 21:25:06 -07001284}
Thiago Macieira46a818e2015-10-08 15:13:05 +02001285
1286/** @} */