blob: 4713eac7a779587ea15351de314e06fcb488b6d5 [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
336 * advancing after the last item in an array or map. In the case of the
337 * outermost CborValue object, this function returns true after decoding a
338 * single element.
339 *
340 * \sa cbor_value_advance(), cbor_value_is_valid()
341 */
342
343/**
344 * \fn bool cbor_value_is_valid(const CborValue *it)
345 *
346 * Returns true if the iterator \a it contains a valid value. Invalid iterators
347 * happen when iteration reaches the end of a container (see \ref
348 * cbor_value_at_end()) or when a search function resulted in no matches.
349 *
350 * \sa cbor_value_advance(), cbor_valie_at_end(), cbor_value_get_type()
351 */
352
353/**
Thiago Macieira2312efd2015-05-06 16:07:48 -0700354 * Advances the CBOR value \a it by one fixed-size position. Fixed-size types
355 * are: integers, tags, simple types (including boolean, null and undefined
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700356 * values) and floating point types.
Thiago Macieira2312efd2015-05-06 16:07:48 -0700357 *
Thiago Macieira46a818e2015-10-08 15:13:05 +0200358 * If the type is not of fixed size, this function has undefined behavior. Code
359 * must be sure that the current type is one of the fixed-size types before
360 * calling this function. This function is provided because it can guarantee
361 * that runs in constant time (O(1)).
362 *
363 * If the caller is not able to determine whether the type is fixed or not, code
364 * can use the cbor_value_advance() function instead.
365 *
366 * \sa cbor_value_at_end(), cbor_value_advance(), cbor_value_enter_container(), cbor_value_leave_container()
Thiago Macieira2312efd2015-05-06 16:07:48 -0700367 */
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700368CborError cbor_value_advance_fixed(CborValue *it)
Thiago Macieira54a0e102015-05-05 21:25:06 -0700369{
Thiago Macieira2312efd2015-05-06 16:07:48 -0700370 assert(it->type != CborInvalidType);
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700371 assert(is_fixed_type(it->type));
372 if (!it->remaining)
373 return CborErrorAdvancePastEOF;
374 return advance_internal(it);
Thiago Macieira54a0e102015-05-05 21:25:06 -0700375}
376
Thiago Macieira4a99af92015-05-12 10:41:45 +0900377static CborError advance_recursive(CborValue *it, int nestingLevel)
378{
379 if (is_fixed_type(it->type))
380 return advance_internal(it);
381
382 if (!cbor_value_is_container(it)) {
383 size_t len = SIZE_MAX;
Thiago Macieiraff130bc2015-06-19 15:15:33 -0700384 return _cbor_value_copy_string(it, NULL, &len, it);
Thiago Macieira4a99af92015-05-12 10:41:45 +0900385 }
386
Thiago Macieiradbc01292016-06-06 17:02:25 -0700387 /* map or array */
Thiago Macieira4a99af92015-05-12 10:41:45 +0900388 if (nestingLevel == CBOR_PARSER_MAX_RECURSIONS)
389 return CborErrorNestingTooDeep;
390
391 CborError err;
392 CborValue recursed;
393 err = cbor_value_enter_container(it, &recursed);
394 if (err)
395 return err;
396 while (!cbor_value_at_end(&recursed)) {
397 err = advance_recursive(&recursed, nestingLevel + 1);
398 if (err)
399 return err;
400 }
401 return cbor_value_leave_container(it, &recursed);
402}
403
404
Thiago Macieira2312efd2015-05-06 16:07:48 -0700405/**
406 * Advances the CBOR value \a it by one element, skipping over containers.
407 * Unlike cbor_value_advance_fixed(), this function can be called on a CBOR
408 * value of any type. However, if the type is a container (map or array) or a
409 * string with a chunked payload, this function will not run in constant time
410 * and will recurse into itself (it will run on O(n) time for the number of
411 * elements or chunks and will use O(n) memory for the number of nested
412 * containers).
413 *
Thiago Macieira46a818e2015-10-08 15:13:05 +0200414 * \sa cbor_value_at_end(), cbor_value_advance_fixed(), cbor_value_enter_container(), cbor_value_leave_container()
Thiago Macieira2312efd2015-05-06 16:07:48 -0700415 */
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700416CborError cbor_value_advance(CborValue *it)
Thiago Macieira2312efd2015-05-06 16:07:48 -0700417{
418 assert(it->type != CborInvalidType);
419 if (!it->remaining)
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700420 return CborErrorAdvancePastEOF;
Thiago Macieira4a99af92015-05-12 10:41:45 +0900421 return advance_recursive(it, 0);
Thiago Macieira2312efd2015-05-06 16:07:48 -0700422}
423
424/**
Thiago Macieira46a818e2015-10-08 15:13:05 +0200425 * \fn bool cbor_value_is_tag(const CborValue *value)
426 *
427 * Returns true if the iterator \a value is valid and points to a CBOR tag.
428 *
429 * \sa cbor_value_get_tag(), cbor_value_skip_tag()
430 */
431
432/**
433 * \fn CborError cbor_value_get_tag(const CborValue *value, CborTag *result)
434 *
435 * Retrieves the CBOR tag value that \a value points to and stores it in \a
436 * result. If the iterator \a value does not point to a CBOR tag value, the
437 * behavior is undefined, so checking with \ref cbor_value_get_type or with
438 * \ref cbor_value_is_tag is recommended.
439 *
440 * \sa cbor_value_get_type(), cbor_value_is_valid(), cbor_value_is_tag()
441 */
442
443/**
Thiago Macieirac4a73c62015-05-09 18:14:11 -0700444 * Advances the CBOR value \a it until it no longer points to a tag. If \a it is
445 * already not pointing to a tag, then this function returns it unchanged.
446 *
Thiago Macieira46a818e2015-10-08 15:13:05 +0200447 * This function does not run in constant time: it will run on O(n) for n being
448 * the number of tags. It does use constant memory (O(1) memory requirements).
449 *
Thiago Macieirac4a73c62015-05-09 18:14:11 -0700450 * \sa cbor_value_advance_fixed(), cbor_value_advance()
451 */
452CborError cbor_value_skip_tag(CborValue *it)
453{
454 while (cbor_value_is_tag(it)) {
455 CborError err = cbor_value_advance_fixed(it);
456 if (err)
457 return err;
458 }
459 return CborNoError;
460}
461
Thiago Macieirac4a73c62015-05-09 18:14:11 -0700462/**
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700463 * \fn bool cbor_value_is_container(const CborValue *it)
464 *
Thiago Macieira2312efd2015-05-06 16:07:48 -0700465 * Returns true if the \a it value is a container and requires recursion in
466 * order to decode (maps and arrays), false otherwise.
467 */
Thiago Macieira54a0e102015-05-05 21:25:06 -0700468
Thiago Macieira2312efd2015-05-06 16:07:48 -0700469/**
470 * Creates a CborValue iterator pointing to the first element of the container
471 * represented by \a it and saves it in \a recursed. The \a it container object
472 * needs to be kept and passed again to cbor_value_leave_container() in order
473 * to continue iterating past this container.
474 *
Thiago Macieira46a818e2015-10-08 15:13:05 +0200475 * The \a it CborValue iterator must point to a container.
476 *
Thiago Macieira2312efd2015-05-06 16:07:48 -0700477 * \sa cbor_value_is_container(), cbor_value_leave_container(), cbor_value_advance()
478 */
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700479CborError cbor_value_enter_container(const CborValue *it, CborValue *recursed)
Thiago Macieira54a0e102015-05-05 21:25:06 -0700480{
Thiago Macieira56d99832015-05-07 14:34:27 -0700481 CborError err;
Thiago Macieira2312efd2015-05-06 16:07:48 -0700482 assert(cbor_value_is_container(it));
Thiago Macieira54a0e102015-05-05 21:25:06 -0700483 *recursed = *it;
Thiago Macieira56d99832015-05-07 14:34:27 -0700484
Thiago Macieira54a0e102015-05-05 21:25:06 -0700485 if (it->flags & CborIteratorFlag_UnknownLength) {
486 recursed->remaining = UINT32_MAX;
Thiago Macieira56d99832015-05-07 14:34:27 -0700487 ++recursed->ptr;
488 err = preparse_value(recursed);
489 if (err != CborErrorUnexpectedBreak)
490 return err;
Thiago Macieiradbc01292016-06-06 17:02:25 -0700491 /* actually, break was expected here
492 * it's just an empty container */
Thiago Macieira56d99832015-05-07 14:34:27 -0700493 ++recursed->ptr;
Thiago Macieira54a0e102015-05-05 21:25:06 -0700494 } else {
Thiago Macieira56d99832015-05-07 14:34:27 -0700495 uint64_t len;
Thiago Macieira4e9626c2015-09-21 14:57:17 -0700496 err = extract_number(&recursed->ptr, recursed->parser->end, &len);
Thiago Macieira56d99832015-05-07 14:34:27 -0700497 assert(err == CborNoError);
Thiago Macieira56d99832015-05-07 14:34:27 -0700498
Thiago Macieirae12dfd02016-06-07 16:29:25 -0700499 recursed->remaining = (uint32_t)len;
Thiago Macieira3f76f632015-05-12 10:10:09 +0900500 if (recursed->remaining != len || len == UINT32_MAX) {
Thiago Macieiradbc01292016-06-06 17:02:25 -0700501 /* back track the pointer to indicate where the error occurred */
Thiago Macieira3f76f632015-05-12 10:10:09 +0900502 recursed->ptr = it->ptr;
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700503 return CborErrorDataTooLarge;
Thiago Macieira3f76f632015-05-12 10:10:09 +0900504 }
Thiago Macieirace16f052015-05-07 23:14:25 -0700505 if (recursed->type == CborMapType) {
Thiago Macieiradbc01292016-06-06 17:02:25 -0700506 /* maps have keys and values, so we need to multiply by 2 */
Thiago Macieira3f76f632015-05-12 10:10:09 +0900507 if (recursed->remaining > UINT32_MAX / 2) {
Thiago Macieiradbc01292016-06-06 17:02:25 -0700508 /* back track the pointer to indicate where the error occurred */
Thiago Macieira3f76f632015-05-12 10:10:09 +0900509 recursed->ptr = it->ptr;
Thiago Macieirace16f052015-05-07 23:14:25 -0700510 return CborErrorDataTooLarge;
Thiago Macieira3f76f632015-05-12 10:10:09 +0900511 }
Thiago Macieirace16f052015-05-07 23:14:25 -0700512 recursed->remaining *= 2;
513 }
Thiago Macieira56d99832015-05-07 14:34:27 -0700514 if (len != 0)
515 return preparse_value(recursed);
Thiago Macieira54a0e102015-05-05 21:25:06 -0700516 }
Thiago Macieira56d99832015-05-07 14:34:27 -0700517
Thiago Macieiradbc01292016-06-06 17:02:25 -0700518 /* the case of the empty container */
Thiago Macieira56d99832015-05-07 14:34:27 -0700519 recursed->type = CborInvalidType;
520 recursed->remaining = 0;
521 return CborNoError;
Thiago Macieirac70169f2015-05-06 07:49:44 -0700522}
523
Thiago Macieira2312efd2015-05-06 16:07:48 -0700524/**
525 * Updates \a it to point to the next element after the container. The \a
Thiago Macieira56d99832015-05-07 14:34:27 -0700526 * recursed object needs to point to the element obtained either by advancing
527 * the last element of the container (via cbor_value_advance(),
528 * cbor_value_advance_fixed(), a nested cbor_value_leave_container(), or the \c
529 * next pointer from cbor_value_copy_string() or cbor_value_dup_string()).
Thiago Macieira2312efd2015-05-06 16:07:48 -0700530 *
Thiago Macieira46a818e2015-10-08 15:13:05 +0200531 * The \a it and \a recursed parameters must be the exact same as passed to
532 * cbor_value_enter_container().
533 *
Thiago Macieira2312efd2015-05-06 16:07:48 -0700534 * \sa cbor_value_enter_container(), cbor_value_at_end()
535 */
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700536CborError cbor_value_leave_container(CborValue *it, const CborValue *recursed)
Thiago Macieirac70169f2015-05-06 07:49:44 -0700537{
Thiago Macieira2312efd2015-05-06 16:07:48 -0700538 assert(cbor_value_is_container(it));
Thiago Macieira56d99832015-05-07 14:34:27 -0700539 assert(recursed->type == CborInvalidType);
Thiago Macieirac70169f2015-05-06 07:49:44 -0700540 it->ptr = recursed->ptr;
Thiago Macieira56d99832015-05-07 14:34:27 -0700541 return preparse_next_value(it);
Thiago Macieirac70169f2015-05-06 07:49:44 -0700542}
543
Thiago Macieira46a818e2015-10-08 15:13:05 +0200544
Thiago Macieira2312efd2015-05-06 16:07:48 -0700545/**
Thiago Macieira46a818e2015-10-08 15:13:05 +0200546 * \fn CborType cbor_value_get_type(const CborValue *value)
547 *
548 * Returns the type of the CBOR value that the iterator \a value points to. If
549 * \a value does not point to a valid value, this function returns \ref
550 * CborInvalidType.
551 *
552 * TinyCBOR also provides functions to test directly if a given CborValue object
553 * is of a given type, like cbor_value_is_text_string() and cbor_value_is_null().
554 *
555 * \sa cbor_value_is_valid()
556 */
557
558/**
559 * \fn bool cbor_value_is_null(const CborValue *value)
560 *
561 * Returns true if the iterator \a value is valid and points to a CBOR null type.
562 *
563 * \sa cbor_value_is_valid(), cbor_value_is_undefined()
564 */
565
566/**
567 * \fn bool cbor_value_is_undefined(const CborValue *value)
568 *
569 * Returns true if the iterator \a value is valid and points to a CBOR undefined type.
570 *
571 * \sa cbor_value_is_valid(), cbor_value_is_null()
572 */
573
574/**
575 * \fn bool cbor_value_is_boolean(const CborValue *value)
576 *
577 * Returns true if the iterator \a value is valid and points to a CBOR boolean
578 * type (true or false).
579 *
580 * \sa cbor_value_is_valid(), cbor_value_get_boolean()
581 */
582
583/**
584 * \fn CborError cbor_value_get_boolean(const CborValue *value, bool *result)
585 *
586 * Retrieves the boolean value that \a value points to and stores it in \a
587 * result. If the iterator \a value does not point to a boolean value, the
588 * behavior is undefined, so checking with \ref cbor_value_get_type or with
589 * \ref cbor_value_is_boolean is recommended.
590 *
591 * \sa cbor_value_get_type(), cbor_value_is_valid(), cbor_value_is_boolean()
592 */
593
594/**
595 * \fn bool cbor_value_is_simple_type(const CborValue *value)
596 *
597 * Returns true if the iterator \a value is valid and points to a CBOR Simple Type
598 * type (other than true, false, null and undefined).
599 *
600 * \sa cbor_value_is_valid(), cbor_value_get_simple_type()
601 */
602
603/**
604 * \fn CborError cbor_value_get_simple_type(const CborValue *value, uint8_t *result)
605 *
606 * Retrieves the CBOR Simple Type value that \a value points to and stores it
607 * in \a result. If the iterator \a value does not point to a simple_type
608 * value, the behavior is undefined, so checking with \ref cbor_value_get_type
609 * or with \ref cbor_value_is_simple_type is recommended.
610 *
611 * \sa cbor_value_get_type(), cbor_value_is_valid(), cbor_value_is_simple_type()
612 */
613
614/**
615 * \fn bool cbor_value_is_integer(const CborValue *value)
616 *
617 * Returns true if the iterator \a value is valid and points to a CBOR integer
618 * type.
619 *
620 * \sa cbor_value_is_valid(), cbor_value_get_int, cbor_value_get_int64, cbor_value_get_uint64, cbor_value_get_raw_integer
621 */
622
623/**
624 * \fn bool cbor_value_is_unsigned_integer(const CborValue *value)
625 *
626 * Returns true if the iterator \a value is valid and points to a CBOR unsigned
627 * integer type (positive values or zero).
628 *
629 * \sa cbor_value_is_valid(), cbor_value_get_uint64()
630 */
631
632/**
633 * \fn bool cbor_value_is_negative_integer(const CborValue *value)
634 *
635 * Returns true if the iterator \a value is valid and points to a CBOR negative
636 * integer type.
637 *
638 * \sa cbor_value_is_valid(), cbor_value_get_int, cbor_value_get_int64, cbor_value_get_raw_integer
639 */
640
641/**
642 * \fn CborError cbor_value_get_int(const CborValue *value, int *result)
643 *
644 * Retrieves the CBOR integer value that \a value points to and stores it in \a
645 * result. If the iterator \a value does not point to an integer value, the
646 * behavior is undefined, so checking with \ref cbor_value_get_type or with
647 * \ref cbor_value_is_integer is recommended.
648 *
649 * Note that this function does not do range-checking: integral values that do
650 * not fit in a variable of type \c{int} are silently truncated to fit. Use
651 * cbor_value_get_int_checked() that is not acceptable.
652 *
653 * \sa cbor_value_get_type(), cbor_value_is_valid(), cbor_value_is_integer()
654 */
655
656/**
657 * \fn CborError cbor_value_get_int64(const CborValue *value, int64_t *result)
658 *
659 * Retrieves the CBOR integer value that \a value points to and stores it in \a
660 * result. If the iterator \a value does not point to an integer value, the
661 * behavior is undefined, so checking with \ref cbor_value_get_type or with
662 * \ref cbor_value_is_integer is recommended.
663 *
664 * Note that this function does not do range-checking: integral values that do
665 * not fit in a variable of type \c{int64_t} are silently truncated to fit. Use
666 * cbor_value_get_int64_checked() that is not acceptable.
667 *
668 * \sa cbor_value_get_type(), cbor_value_is_valid(), cbor_value_is_integer()
669 */
670
671/**
672 * \fn CborError cbor_value_get_uint64(const CborValue *value, uint64_t *result)
673 *
674 * Retrieves the CBOR integer value that \a value points to and stores it in \a
675 * result. If the iterator \a value does not point to an unsigned integer
676 * value, the behavior is undefined, so checking with \ref cbor_value_get_type
677 * or with \ref cbor_value_is_unsigned_integer is recommended.
678 *
679 * \sa cbor_value_get_type(), cbor_value_is_valid(), cbor_value_is_unsigned_integer()
680 */
681
682/**
683 * \fn CborError cbor_value_get_raw_integer(const CborValue *value, uint64_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 * This function is provided because CBOR negative integers can assume values
691 * that cannot be represented with normal 64-bit integer variables.
692 *
693 * If the integer is unsigned (that is, if cbor_value_is_unsigned_integer()
694 * returns true), then \a result will contain the actual value. If the integer
695 * is negative, then \a result will contain the absolute value of that integer,
696 * minus one. That is, \c {actual = -result - 1}. On architectures using two's
697 * complement for representation of negative integers, it is equivalent to say
698 * that \a result will contain the bitwise negation of the actual value.
699 *
700 * \sa cbor_value_get_type(), cbor_value_is_valid(), cbor_value_is_integer()
701 */
702
703/**
704 * \fn bool cbor_value_is_length_known(const CborValue *value)
705 *
706 * Returns true if the length of this type is known without calculation. That
707 * is, if the length of this CBOR string, map or array is encoded in the data
708 * stream, this function returns true. If the length is not encoded, it returns
709 * false.
710 *
711 * If the length is known, code can call cbor_value_get_string_length(),
712 * cbor_value_get_array_length() or cbor_value_get_map_length() to obtain the
713 * length. If the length is not known but is necessary, code can use the
714 * cbor_value_calculate_string_length() function (no equivalent function is
715 * provided for maps and arrays).
716 */
717
718/**
719 * \fn bool cbor_value_is_text_string(const CborValue *value)
720 *
721 * Returns true if the iterator \a value is valid and points to a CBOR text
722 * string. CBOR text strings are UTF-8 encoded and usually contain
723 * human-readable text.
724 *
725 * \sa cbor_value_is_valid(), cbor_value_get_string_length(), cbor_value_calculate_string_length(),
726 * cbor_value_copy_text_string(), cbor_value_dup_text_string()
727 */
728
729/**
730 * \fn bool cbor_value_is_byte_string(const CborValue *value)
731 *
732 * Returns true if the iterator \a value is valid and points to a CBOR text
733 * string. CBOR byte strings are binary data with no specified encoding or
734 * format.
735 *
736 * \sa cbor_value_is_valid(), cbor_value_get_string_length(), cbor_value_calculate_string_length(),
737 * cbor_value_copy_byte_string(), cbor_value_dup_byte_string()
738 */
739
740/**
741 * \fn CborError cbor_value_get_string_length(const CborValue *value, size_t *length)
742 *
743 * Extracts the length of the byte or text string that \a value points to and
744 * stores it in \a result. If the iterator \a value does not point to a text
745 * string or a byte string, the behaviour is undefined, so checking with \ref
746 * cbor_value_get_type, with \ref cbor_value_is_text_string or \ref
747 * cbor_value_is_byte_string is recommended.
748 *
749 * If the length of this string is not encoded in the CBOR data stream, this
750 * function will return the recoverable error CborErrorUnknownLength. You may
751 * also check whether that is the case by using cbor_value_is_length_known().
752 *
753 * If the length of the string is required but the length was not encoded, use
754 * cbor_value_calculate_string_length(), but note that that function does not
755 * run in constant time.
756 *
757 * \note On 32-bit platforms, this function will return error condition of \ref
758 * CborErrorDataTooLarge if the stream indicates a length that is too big to
759 * fit in 32-bit.
760 *
761 * \sa cbor_value_is_valid(), cbor_value_is_length_known(), cbor_value_calculate_string_length()
762 */
763
764/**
765 * Calculates the length of the byte or text string that \a value points to and
766 * stores it in \a len. 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 * This function is different from cbor_value_get_string_length() in that it
772 * calculates the length even for strings sent in chunks. For that reason, this
773 * function may not run in constant time (it will run in O(n) time on the
774 * number of chunks). It does use constant memory (O(1)).
Thiago Macieira2312efd2015-05-06 16:07:48 -0700775 *
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700776 * \note On 32-bit platforms, this function will return error condition of \ref
777 * CborErrorDataTooLarge if the stream indicates a length that is too big to
778 * fit in 32-bit.
Thiago Macieira2312efd2015-05-06 16:07:48 -0700779 *
780 * \sa cbor_value_get_string_length(), cbor_value_copy_string(), cbor_value_is_length_known()
781 */
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700782CborError cbor_value_calculate_string_length(const CborValue *value, size_t *len)
Thiago Macieira2312efd2015-05-06 16:07:48 -0700783{
Thiago Macieira9ae05812015-05-11 15:09:09 +0900784 *len = SIZE_MAX;
Thiago Macieiraff130bc2015-06-19 15:15:33 -0700785 return _cbor_value_copy_string(value, NULL, len, NULL);
Thiago Macieirac70169f2015-05-06 07:49:44 -0700786}
787
Thiago Macieira2312efd2015-05-06 16:07:48 -0700788/**
Thiago Macieiraff130bc2015-06-19 15:15:33 -0700789 * \fn CborError cbor_value_dup_text_string(const CborValue *value, char **buffer, size_t *buflen, CborValue *next)
790 *
Thiago Macieira2312efd2015-05-06 16:07:48 -0700791 * Allocates memory for the string pointed by \a value and copies it into this
792 * buffer. The pointer to the buffer is stored in \a buffer and the number of
793 * bytes copied is stored in \a len (those variables must not be NULL).
794 *
Thiago Macieira46a818e2015-10-08 15:13:05 +0200795 * If the iterator \a value does not point to a text string, the behaviour is
796 * undefined, so checking with \ref cbor_value_get_type or \ref
797 * cbor_value_is_text_string is recommended.
798 *
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700799 * If \c malloc returns a NULL pointer, this function will return error
800 * condition \ref CborErrorOutOfMemory.
Thiago Macieira2312efd2015-05-06 16:07:48 -0700801 *
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700802 * On success, \c{*buffer} will contain a valid pointer that must be freed by
803 * calling \c{free()}. This is the case even for zero-length strings.
Thiago Macieira2312efd2015-05-06 16:07:48 -0700804 *
805 * The \a next pointer, if not null, will be updated to point to the next item
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700806 * after this string. If \a value points to the last item, then \a next will be
807 * invalid.
Thiago Macieira2312efd2015-05-06 16:07:48 -0700808 *
Thiago Macieira46a818e2015-10-08 15:13:05 +0200809 * This function may not run in constant time (it will run in O(n) time on the
810 * number of chunks). It requires constant memory (O(1)) in addition to the
811 * malloc'ed block.
812 *
Thiago Macieira2312efd2015-05-06 16:07:48 -0700813 * \note This function does not perform UTF-8 validation on the incoming text
814 * string.
815 *
Thiago Macieiraff130bc2015-06-19 15:15:33 -0700816 * \sa cbor_value_copy_text_string(), cbor_value_dup_byte_string()
Thiago Macieira2312efd2015-05-06 16:07:48 -0700817 */
Thiago Macieiraff130bc2015-06-19 15:15:33 -0700818
819/**
820 * \fn CborError cbor_value_dup_byte_string(const CborValue *value, uint8_t **buffer, size_t *buflen, CborValue *next)
821 *
822 * Allocates memory for the string pointed by \a value and copies it into this
823 * buffer. The pointer to the buffer is stored in \a buffer and the number of
824 * bytes copied is stored in \a len (those variables must not be NULL).
825 *
Thiago Macieira46a818e2015-10-08 15:13:05 +0200826 * If the iterator \a value does not point to a byte string, the behaviour is
827 * undefined, so checking with \ref cbor_value_get_type or \ref
828 * cbor_value_is_byte_string is recommended.
829 *
Thiago Macieiraff130bc2015-06-19 15:15:33 -0700830 * If \c malloc returns a NULL pointer, this function will return error
831 * condition \ref CborErrorOutOfMemory.
832 *
833 * On success, \c{*buffer} will contain a valid pointer that must be freed by
834 * calling \c{free()}. This is the case even for zero-length strings.
835 *
836 * The \a next pointer, if not null, will be updated to point to the next item
837 * after this string. If \a value points to the last item, then \a next will be
838 * invalid.
839 *
Thiago Macieira46a818e2015-10-08 15:13:05 +0200840 * This function may not run in constant time (it will run in O(n) time on the
841 * number of chunks). It requires constant memory (O(1)) in addition to the
842 * malloc'ed block.
843 *
Thiago Macieiraff130bc2015-06-19 15:15:33 -0700844 * \sa cbor_value_copy_byte_string(), cbor_value_dup_text_string()
845 */
Thiago Macieira46a818e2015-10-08 15:13:05 +0200846
Thiago Macieiraff130bc2015-06-19 15:15:33 -0700847CborError _cbor_value_dup_string(const CborValue *value, void **buffer, size_t *buflen, CborValue *next)
Thiago Macieirac70169f2015-05-06 07:49:44 -0700848{
Thiago Macieira2312efd2015-05-06 16:07:48 -0700849 assert(buffer);
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700850 assert(buflen);
Thiago Macieirafc870932015-06-19 15:01:35 -0700851 *buflen = SIZE_MAX;
Thiago Macieiraff130bc2015-06-19 15:15:33 -0700852 CborError err = _cbor_value_copy_string(value, NULL, buflen, NULL);
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700853 if (err)
854 return err;
Thiago Macieirac70169f2015-05-06 07:49:44 -0700855
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700856 ++*buflen;
857 *buffer = malloc(*buflen);
Thiago Macieira2312efd2015-05-06 16:07:48 -0700858 if (!*buffer) {
Thiago Macieiradbc01292016-06-06 17:02:25 -0700859 /* out of memory */
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700860 return CborErrorOutOfMemory;
Thiago Macieirac70169f2015-05-06 07:49:44 -0700861 }
Thiago Macieiraff130bc2015-06-19 15:15:33 -0700862 err = _cbor_value_copy_string(value, *buffer, buflen, next);
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700863 if (err) {
Thiago Macieira2312efd2015-05-06 16:07:48 -0700864 free(*buffer);
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700865 return err;
Thiago Macieirac70169f2015-05-06 07:49:44 -0700866 }
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700867 return CborNoError;
Thiago Macieira2312efd2015-05-06 16:07:48 -0700868}
869
Thiago Macieiradbc01292016-06-06 17:02:25 -0700870/* We return uintptr_t so that we can pass memcpy directly as the iteration
871 * function. The choice is to optimize for memcpy, which is used in the base
872 * parser API (cbor_value_copy_string), while memcmp is used in convenience API
873 * only. */
Thiago Macieira5752ce52015-06-16 12:10:03 -0700874typedef uintptr_t (*IterateFunction)(char *, const uint8_t *, size_t);
Thiago Macieira9ae05812015-05-11 15:09:09 +0900875
Thiago Macieira5752ce52015-06-16 12:10:03 -0700876static uintptr_t iterate_noop(char *dest, const uint8_t *src, size_t len)
Thiago Macieira9ae05812015-05-11 15:09:09 +0900877{
878 (void)dest;
879 (void)src;
880 (void)len;
881 return true;
882}
883
Thiago Macieira5752ce52015-06-16 12:10:03 -0700884static uintptr_t iterate_memcmp(char *s1, const uint8_t *s2, size_t len)
Thiago Macieirac4a73c62015-05-09 18:14:11 -0700885{
Thiago Macieira5752ce52015-06-16 12:10:03 -0700886 return memcmp(s1, (const char *)s2, len) == 0;
Thiago Macieirac4a73c62015-05-09 18:14:11 -0700887}
888
Thiago Macieira9ae05812015-05-11 15:09:09 +0900889static CborError iterate_string_chunks(const CborValue *value, char *buffer, size_t *buflen,
890 bool *result, CborValue *next, IterateFunction func)
891{
892 assert(cbor_value_is_byte_string(value) || cbor_value_is_text_string(value));
893
894 size_t total;
895 CborError err;
Thiago Macieira5752ce52015-06-16 12:10:03 -0700896 const uint8_t *ptr = value->ptr;
Thiago Macieira9ae05812015-05-11 15:09:09 +0900897 if (cbor_value_is_length_known(value)) {
Thiago Macieiradbc01292016-06-06 17:02:25 -0700898 /* easy case: fixed length */
Thiago Macieira9ae05812015-05-11 15:09:09 +0900899 err = extract_length(value->parser, &ptr, &total);
900 if (err)
901 return err;
Thiago Macieira63abed92015-10-28 17:01:14 -0700902 if (total > (size_t)(value->parser->end - ptr))
Thiago Macieira9ae05812015-05-11 15:09:09 +0900903 return CborErrorUnexpectedEOF;
904 if (total <= *buflen)
Thiago Macieirae12dfd02016-06-07 16:29:25 -0700905 *result = !!func(buffer, ptr, total);
Thiago Macieira9ae05812015-05-11 15:09:09 +0900906 else
907 *result = false;
908 ptr += total;
909 } else {
Thiago Macieiradbc01292016-06-06 17:02:25 -0700910 /* chunked */
Thiago Macieira9ae05812015-05-11 15:09:09 +0900911 ++ptr;
912 total = 0;
913 *result = true;
914 while (true) {
915 size_t chunkLen;
916 size_t newTotal;
917
918 if (ptr == value->parser->end)
919 return CborErrorUnexpectedEOF;
920
Thiago Macieira5752ce52015-06-16 12:10:03 -0700921 if (*ptr == (uint8_t)BreakByte) {
Thiago Macieira9ae05812015-05-11 15:09:09 +0900922 ++ptr;
923 break;
924 }
925
Thiago Macieiradbc01292016-06-06 17:02:25 -0700926 /* is this the right type? */
Thiago Macieira9ae05812015-05-11 15:09:09 +0900927 if ((*ptr & MajorTypeMask) != value->type)
928 return CborErrorIllegalType;
929
930 err = extract_length(value->parser, &ptr, &chunkLen);
931 if (err)
932 return err;
933
Thiago Macieira1de31a42015-06-16 16:01:16 -0700934 if (unlikely(add_check_overflow(total, chunkLen, &newTotal)))
Thiago Macieira9ae05812015-05-11 15:09:09 +0900935 return CborErrorDataTooLarge;
936
Thiago Macieira63abed92015-10-28 17:01:14 -0700937 if (chunkLen > (size_t)(value->parser->end - ptr))
Thiago Macieira9ae05812015-05-11 15:09:09 +0900938 return CborErrorUnexpectedEOF;
939
940 if (*result && *buflen >= newTotal)
Thiago Macieirae12dfd02016-06-07 16:29:25 -0700941 *result = !!func(buffer + total, ptr, chunkLen);
Thiago Macieira9ae05812015-05-11 15:09:09 +0900942 else
943 *result = false;
944
945 ptr += chunkLen;
946 total = newTotal;
947 }
948 }
949
Thiago Macieiradbc01292016-06-06 17:02:25 -0700950 /* is there enough room for the ending NUL byte? */
Thiago Macieira9ae05812015-05-11 15:09:09 +0900951 if (*result && *buflen > total)
Thiago Macieirae12dfd02016-06-07 16:29:25 -0700952 *result = !!func(buffer + total, (const uint8_t *)"", 1);
Thiago Macieira9ae05812015-05-11 15:09:09 +0900953 *buflen = total;
954
955 if (next) {
956 *next = *value;
957 next->ptr = ptr;
958 return preparse_next_value(next);
959 }
960 return CborNoError;
961}
962
Thiago Macieira2312efd2015-05-06 16:07:48 -0700963/**
Thiago Macieiraff130bc2015-06-19 15:15:33 -0700964 * \fn CborError cbor_value_copy_text_string(const CborValue *value, char *buffer, size_t *buflen, CborValue *next)
965 *
Thiago Macieira2312efd2015-05-06 16:07:48 -0700966 * Copies the string pointed by \a value into the buffer provided at \a buffer
967 * of \a buflen bytes. If \a buffer is a NULL pointer, this function will not
968 * copy anything and will only update the \a next value.
969 *
Thiago Macieira46a818e2015-10-08 15:13:05 +0200970 * If the iterator \a value does not point to a text string, the behaviour is
971 * undefined, so checking with \ref cbor_value_get_type or \ref
972 * cbor_value_is_text_string is recommended.
973 *
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700974 * If the provided buffer length was too small, this function returns an error
975 * condition of \ref CborErrorOutOfMemory. If you need to calculate the length
976 * of the string in order to preallocate a buffer, use
Thiago Macieira2312efd2015-05-06 16:07:48 -0700977 * cbor_value_calculate_string_length().
978 *
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700979 * On success, this function sets the number of bytes copied to \c{*buflen}. If
980 * the buffer is large enough, this function will insert a null byte after the
981 * last copied byte, to facilitate manipulation of text strings. That byte is
982 * not included in the returned value of \c{*buflen}.
Thiago Macieira2312efd2015-05-06 16:07:48 -0700983 *
984 * The \a next pointer, if not null, will be updated to point to the next item
985 * after this string. If \a value points to the last item, then \a next will be
986 * invalid.
987 *
Thiago Macieira46a818e2015-10-08 15:13:05 +0200988 * This function may not run in constant time (it will run in O(n) time on the
989 * number of chunks). It requires constant memory (O(1)).
990 *
Thiago Macieira2312efd2015-05-06 16:07:48 -0700991 * \note This function does not perform UTF-8 validation on the incoming text
992 * string.
993 *
Thiago Macieiraff130bc2015-06-19 15:15:33 -0700994 * \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 -0700995 */
Thiago Macieiraff130bc2015-06-19 15:15:33 -0700996
997/**
998 * \fn CborError cbor_value_copy_byte_string(const CborValue *value, uint8_t *buffer, size_t *buflen, CborValue *next)
999 *
1000 * Copies the string pointed by \a value into the buffer provided at \a buffer
1001 * of \a buflen bytes. If \a buffer is a NULL pointer, this function will not
1002 * copy anything and will only update the \a next value.
1003 *
Thiago Macieira46a818e2015-10-08 15:13:05 +02001004 * If the iterator \a value does not point to a byte string, the behaviour is
1005 * undefined, so checking with \ref cbor_value_get_type or \ref
1006 * cbor_value_is_byte_string is recommended.
1007 *
Thiago Macieiraff130bc2015-06-19 15:15:33 -07001008 * If the provided buffer length was too small, this function returns an error
1009 * condition of \ref CborErrorOutOfMemory. If you need to calculate the length
1010 * of the string in order to preallocate a buffer, use
1011 * cbor_value_calculate_string_length().
1012 *
1013 * On success, this function sets the number of bytes copied to \c{*buflen}. If
1014 * the buffer is large enough, this function will insert a null byte after the
1015 * last copied byte, to facilitate manipulation of null-terminated strings.
1016 * That byte is not included in the returned value of \c{*buflen}.
1017 *
1018 * The \a next pointer, if not null, will be updated to point to the next item
1019 * after this string. If \a value points to the last item, then \a next will be
1020 * invalid.
1021 *
Thiago Macieira46a818e2015-10-08 15:13:05 +02001022 * This function may not run in constant time (it will run in O(n) time on the
1023 * number of chunks). It requires constant memory (O(1)).
1024 *
Thiago Macieiraff130bc2015-06-19 15:15:33 -07001025 * \sa cbor_value_dup_text_string(), cbor_value_copy_text_string(), cbor_value_get_string_length(), cbor_value_calculate_string_length()
1026 */
1027
1028CborError _cbor_value_copy_string(const CborValue *value, void *buffer,
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -07001029 size_t *buflen, CborValue *next)
Thiago Macieira2312efd2015-05-06 16:07:48 -07001030{
Thiago Macieira9ae05812015-05-11 15:09:09 +09001031 bool copied_all;
Thiago Macieiraed5b57c2015-07-07 16:38:27 -07001032 CborError err = iterate_string_chunks(value, (char*)buffer, buflen, &copied_all, next,
Thiago Macieira9ae05812015-05-11 15:09:09 +09001033 buffer ? (IterateFunction)memcpy : iterate_noop);
1034 return err ? err :
1035 copied_all ? CborNoError : CborErrorOutOfMemory;
Thiago Macieirac70169f2015-05-06 07:49:44 -07001036}
1037
Thiago Macieirac4a73c62015-05-09 18:14:11 -07001038/**
1039 * Compares the entry \a value with the string \a string and store the result
Thiago Macieira46a818e2015-10-08 15:13:05 +02001040 * in \a result. If the value is different from \a string \a result will
1041 * contain \c false.
Thiago Macieirac4a73c62015-05-09 18:14:11 -07001042 *
1043 * The entry at \a value may be a tagged string. If \a is not a string or a
1044 * tagged string, the comparison result will be false.
Thiago Macieira46a818e2015-10-08 15:13:05 +02001045 *
1046 * CBOR requires text strings to be encoded in UTF-8, but this function does
1047 * not validate either the strings in the stream or the string \a string to be
1048 * matched. Moreover, comparison is done on strict codepoint comparison,
1049 * without any Unicode normalization.
1050 *
1051 * This function may not run in constant time (it will run in O(n) time on the
1052 * number of chunks). It requires constant memory (O(1)).
1053 *
1054 * \sa cbor_value_skip_tag(), cbor_value_copy_text_string()
Thiago Macieirac4a73c62015-05-09 18:14:11 -07001055 */
1056CborError cbor_value_text_string_equals(const CborValue *value, const char *string, bool *result)
1057{
1058 CborValue copy = *value;
1059 CborError err = cbor_value_skip_tag(&copy);
1060 if (err)
1061 return err;
1062 if (!cbor_value_is_text_string(&copy)) {
1063 *result = false;
1064 return CborNoError;
1065 }
1066
1067 size_t len = strlen(string);
1068 return iterate_string_chunks(&copy, CONST_CAST(char *, string), &len, result, NULL, iterate_memcmp);
1069}
1070
1071/**
Thiago Macieira46a818e2015-10-08 15:13:05 +02001072 * \fn bool cbor_value_is_array(const CborValue *value)
Thiago Macieira7b623c22015-05-11 15:52:14 +09001073 *
Thiago Macieira46a818e2015-10-08 15:13:05 +02001074 * Returns true if the iterator \a value is valid and points to a CBOR array.
1075 *
1076 * \sa cbor_value_is_valid(), cbor_value_is_map()
1077 */
1078
1079/**
1080 * \fn CborError cbor_value_get_array_length(const CborValue *value, size_t *length)
1081 *
1082 * Extracts the length of the CBOR array that \a value points to and stores it
1083 * in \a result. If the iterator \a value does not point to a CBOR array, the
1084 * behaviour is undefined, so checking with \ref cbor_value_get_type or \ref
1085 * cbor_value_is_array is recommended.
1086 *
1087 * If the length of this array is not encoded in the CBOR data stream, this
1088 * function will return the recoverable error CborErrorUnknownLength. You may
1089 * also check whether that is the case by using cbor_value_is_length_known().
1090 *
1091 * \note On 32-bit platforms, this function will return error condition of \ref
1092 * CborErrorDataTooLarge if the stream indicates a length that is too big to
1093 * fit in 32-bit.
1094 *
1095 * \sa cbor_value_is_valid(), cbor_value_is_length_known()
1096 */
1097
1098/**
1099 * \fn bool cbor_value_is_map(const CborValue *value)
1100 *
1101 * Returns true if the iterator \a value is valid and points to a CBOR map.
1102 *
1103 * \sa cbor_value_is_valid(), cbor_value_is_array()
1104 */
1105
1106/**
1107 * \fn CborError cbor_value_get_map_length(const CborValue *value, size_t *length)
1108 *
1109 * Extracts the length of the CBOR map that \a value points to and stores it in
1110 * \a result. If the iterator \a value does not point to a CBOR map, the
1111 * behaviour is undefined, so checking with \ref cbor_value_get_type or \ref
1112 * cbor_value_is_map is recommended.
1113 *
1114 * If the length of this map is not encoded in the CBOR data stream, this
1115 * function will return the recoverable error CborErrorUnknownLength. You may
1116 * also check whether that is the case by using cbor_value_is_length_known().
1117 *
1118 * \note On 32-bit platforms, this function will return error condition of \ref
1119 * CborErrorDataTooLarge if the stream indicates a length that is too big to
1120 * fit in 32-bit.
1121 *
1122 * \sa cbor_value_is_valid(), cbor_value_is_length_known()
1123 */
1124
1125/**
1126 * Attempts to find the value in map \a map that corresponds to the text string
1127 * entry \a string. If the iterator \a value does not point to a CBOR map, the
1128 * behaviour is undefined, so checking with \ref cbor_value_get_type or \ref
1129 * cbor_value_is_map is recommended.
1130 *
1131 * If the item is found, it is stored in \a result. If no item is found
1132 * matching the key, then \a result will contain an element of type \ref
1133 * CborInvalidType. Matching is performed using
1134 * cbor_value_text_string_equals(), so tagged strings will also match.
1135 *
1136 * This function has a time complexity of O(n) where n is the number of
1137 * elements in the map to be searched. In addition, this function is has O(n)
1138 * memory requirement based on the number of nested containers (maps or arrays)
1139 * found as elements of this map.
1140 *
1141 * \sa cbor_value_is_valid(), cbor_value_text_string_equals(), cbor_value_advance()
Thiago Macieira7b623c22015-05-11 15:52:14 +09001142 */
1143CborError cbor_value_map_find_value(const CborValue *map, const char *string, CborValue *element)
1144{
1145 assert(cbor_value_is_map(map));
1146 size_t len = strlen(string);
1147 CborError err = cbor_value_enter_container(map, element);
1148 if (err)
1149 goto error;
1150
1151 while (!cbor_value_at_end(element)) {
Thiago Macieiradbc01292016-06-06 17:02:25 -07001152 /* find the non-tag so we can compare */
Thiago Macieira7b623c22015-05-11 15:52:14 +09001153 err = cbor_value_skip_tag(element);
1154 if (err)
1155 goto error;
1156 if (cbor_value_is_text_string(element)) {
1157 bool equals;
1158 size_t dummyLen = len;
1159 err = iterate_string_chunks(element, CONST_CAST(char *, string), &dummyLen,
1160 &equals, element, iterate_memcmp);
1161 if (err)
1162 goto error;
1163 if (equals)
1164 return preparse_value(element);
1165 } else {
Thiago Macieiradbc01292016-06-06 17:02:25 -07001166 /* skip this key */
Thiago Macieira7b623c22015-05-11 15:52:14 +09001167 err = cbor_value_advance(element);
1168 if (err)
1169 goto error;
1170 }
1171
Thiago Macieiradbc01292016-06-06 17:02:25 -07001172 /* skip this value */
Thiago Macieira7b623c22015-05-11 15:52:14 +09001173 err = cbor_value_skip_tag(element);
1174 if (err)
1175 goto error;
1176 err = cbor_value_advance(element);
1177 if (err)
1178 goto error;
1179 }
1180
Thiago Macieiradbc01292016-06-06 17:02:25 -07001181 /* not found */
Thiago Macieira7b623c22015-05-11 15:52:14 +09001182 element->type = CborInvalidType;
1183 return CborNoError;
1184
1185error:
1186 element->type = CborInvalidType;
1187 return err;
1188}
1189
1190/**
Thiago Macieira46a818e2015-10-08 15:13:05 +02001191 * \fn bool cbor_value_is_float(const CborValue *value)
1192 *
1193 * Returns true if the iterator \a value is valid and points to a CBOR
1194 * single-precision floating point (32-bit).
1195 *
1196 * \sa cbor_value_is_valid(), cbor_value_is_double(), cbor_value_is_half_float()
1197 */
1198
1199/**
1200 * \fn CborError cbor_value_get_float(const CborValue *value, float *result)
1201 *
1202 * Retrieves the CBOR single-precision floating point (32-bit) value that \a
1203 * value points to and stores it in \a result. If the iterator \a value does
1204 * not point to a single-precision floating point value, the behavior is
1205 * undefined, so checking with \ref cbor_value_get_type or with \ref
1206 * cbor_value_is_float is recommended.
1207 *
1208 * \sa cbor_value_get_type(), cbor_value_is_valid(), cbor_value_is_float(), cbor_value_get_double()
1209 */
1210
1211/**
1212 * \fn bool cbor_value_is_double(const CborValue *value)
1213 *
1214 * Returns true if the iterator \a value is valid and points to a CBOR
1215 * double-precision floating point (64-bit).
1216 *
1217 * \sa cbor_value_is_valid(), cbor_value_is_float(), cbor_value_is_half_float()
1218 */
1219
1220/**
1221 * \fn CborError cbor_value_get_double(const CborValue *value, float *result)
1222 *
1223 * Retrieves the CBOR double-precision floating point (64-bit) value that \a
1224 * value points to and stores it in \a result. If the iterator \a value does
1225 * not point to a double-precision floating point value, the behavior is
1226 * undefined, so checking with \ref cbor_value_get_type or with \ref
1227 * cbor_value_is_double is recommended.
1228 *
1229 * \sa cbor_value_get_type(), cbor_value_is_valid(), cbor_value_is_double(), cbor_value_get_float()
1230 */
1231
1232/**
1233 * \fn bool cbor_value_is_half_float(const CborValue *value)
1234 *
1235 * Returns true if the iterator \a value is valid and points to a CBOR
1236 * single-precision floating point (16-bit).
1237 *
1238 * \sa cbor_value_is_valid(), cbor_value_is_double(), cbor_value_is_float()
1239 */
1240
1241/**
1242 * Retrieves the CBOR half-precision floating point (16-bit) value that \a
1243 * value points to and stores it in \a result. If the iterator \a value does
1244 * not point to a half-precision floating point value, the behavior is
1245 * undefined, so checking with \ref cbor_value_get_type or with \ref
1246 * cbor_value_is_half_float is recommended.
1247 *
1248 * Note: since the C language does not have a standard type for half-precision
1249 * floating point, this function takes a \c{void *} as a parameter for the
1250 * storage area, which must be at least 16 bits wide.
1251 *
1252 * \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 -07001253 */
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -07001254CborError cbor_value_get_half_float(const CborValue *value, void *result)
Thiago Macieirac70169f2015-05-06 07:49:44 -07001255{
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -07001256 assert(value->type == CborHalfFloatType);
Thiago Macieirac70169f2015-05-06 07:49:44 -07001257
Thiago Macieiradbc01292016-06-06 17:02:25 -07001258 /* size has been computed already */
Thiago Macieirac70169f2015-05-06 07:49:44 -07001259 uint16_t v = get16(value->ptr + 1);
1260 memcpy(result, &v, sizeof(v));
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -07001261 return CborNoError;
Thiago Macieira54a0e102015-05-05 21:25:06 -07001262}
Thiago Macieira46a818e2015-10-08 15:13:05 +02001263
1264/** @} */