blob: 6b413461b96f0af9cd5ee21b2dfc9ed93f931ace [file] [log] [blame]
Thiago Macieira54a0e102015-05-05 21:25:06 -07001/****************************************************************************
2**
3** Copyright (C) 2015 Intel Corporation
4**
5** Permission is hereby granted, free of charge, to any person obtaining a copy
6** of this software and associated documentation files (the "Software"), to deal
7** in the Software without restriction, including without limitation the rights
8** to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9** copies of the Software, and to permit persons to whom the Software is
10** furnished to do so, subject to the following conditions:
11**
12** The above copyright notice and this permission notice shall be included in
13** all copies or substantial portions of the Software.
14**
15** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16** IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17** FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18** AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19** LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20** OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21** THE SOFTWARE.
22**
23****************************************************************************/
24
Thiago Macieiraed5b57c2015-07-07 16:38:27 -070025#define _BSD_SOURCE 1
Otavio Pontese2d5dd52016-07-08 09:49:38 -030026#define _DEFAULT_SOURCE 1
Thiago Macieira54a0e102015-05-05 21:25:06 -070027#include "cbor.h"
28#include "cborconstants_p.h"
29#include "compilersupport_p.h"
Thiago Macieira4e9626c2015-09-21 14:57:17 -070030#include "extract_number_p.h"
Thiago Macieira54a0e102015-05-05 21:25:06 -070031
32#include <assert.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/**
42 * \typedef CborValue
43 * This type contains one value parsed from the CBOR stream.
44 *
45 * To get the actual type, use cbor_value_get_type(). Then extract the value
46 * using one of the corresponding functions: cbor_value_get_boolean(), cbor_value_get_int64(),
47 * cbor_value_get_int(), cbor_value_copy_string(), cbor_value_get_array(), cbor_value_get_map(),
48 * cbor_value_get_double(), cbor_value_get_float().
49 *
50 * In C++ and C11 modes, you can additionally use the cbor_value_get_integer()
51 * and cbor_value_get_floating_point() generic functions.
52 *
53 * \omit
54 * Implementation details: the CborValue contains these fields:
55 * \list
56 * \li ptr: pointer to the actual data
57 * \li flags: flags from the decoder
Thiago Macieira2312efd2015-05-06 16:07:48 -070058 * \li extra: partially decoded integer value (0, 1 or 2 bytes)
Thiago Macieira54a0e102015-05-05 21:25:06 -070059 * \li remaining: remaining items in this collection after this item or UINT32_MAX if length is unknown
60 * \endlist
61 * \endomit
62 */
63
Thiago Macieiraf5cb94b2015-06-16 16:10:49 -070064static CborError extract_length(const CborParser *parser, const uint8_t **ptr, size_t *len)
Thiago Macieira54a0e102015-05-05 21:25:06 -070065{
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -070066 uint64_t v;
Thiago Macieira4e9626c2015-09-21 14:57:17 -070067 CborError err = extract_number(ptr, parser->end, &v);
Mike Colagrosso629d5b72016-02-24 15:12:34 -070068 if (err) {
69 *len = 0;
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -070070 return err;
Mike Colagrosso629d5b72016-02-24 15:12:34 -070071 }
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -070072
73 *len = v;
74 if (v != *len)
75 return CborErrorDataTooLarge;
76 return CborNoError;
77}
78
79static bool is_fixed_type(uint8_t type)
80{
81 return type != CborTextStringType && type != CborByteStringType && type != CborArrayType &&
82 type != CborMapType;
83}
84
85static CborError preparse_value(CborValue *it)
86{
87 const CborParser *parser = it->parser;
Thiago Macieira11e913f2015-05-07 13:01:18 -070088 it->type = CborInvalidType;
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -070089
Thiago Macieiradbc01292016-06-06 17:02:25 -070090 /* are we at the end? */
Thiago Macieira54a0e102015-05-05 21:25:06 -070091 if (it->ptr == parser->end)
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -070092 return CborErrorUnexpectedEOF;
Thiago Macieira54a0e102015-05-05 21:25:06 -070093
94 uint8_t descriptor = *it->ptr;
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -070095 uint8_t type = descriptor & MajorTypeMask;
Thiago Macieira851c4812015-05-08 15:23:20 -070096 it->type = type;
Thiago Macieira54a0e102015-05-05 21:25:06 -070097 it->flags = 0;
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -070098 it->extra = (descriptor &= SmallValueMask);
99
Thiago Macieira56d99832015-05-07 14:34:27 -0700100 if (descriptor > Value64Bit) {
101 if (unlikely(descriptor != IndefiniteLength))
Thiago Macieira3f76f632015-05-12 10:10:09 +0900102 return type == CborSimpleType ? CborErrorUnknownType : CborErrorIllegalNumber;
Thiago Macieira56d99832015-05-07 14:34:27 -0700103 if (likely(!is_fixed_type(type))) {
Thiago Macieiradbc01292016-06-06 17:02:25 -0700104 /* special case */
Thiago Macieira56d99832015-05-07 14:34:27 -0700105 it->flags |= CborIteratorFlag_UnknownLength;
106 it->type = type;
107 return CborNoError;
108 }
109 return type == CborSimpleType ? CborErrorUnexpectedBreak : CborErrorIllegalNumber;
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700110 }
Thiago Macieira54a0e102015-05-05 21:25:06 -0700111
Thiago Macieirac70169f2015-05-06 07:49:44 -0700112 size_t bytesNeeded = descriptor < Value8Bit ? 0 : (1 << (descriptor - Value8Bit));
Thiago Macieira63abed92015-10-28 17:01:14 -0700113 if (bytesNeeded + 1 > (size_t)(parser->end - it->ptr))
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700114 return CborErrorUnexpectedEOF;
Thiago Macieirac70169f2015-05-06 07:49:44 -0700115
Thiago Macieira851c4812015-05-08 15:23:20 -0700116 uint8_t majortype = type >> MajorTypeShift;
117 if (majortype == NegativeIntegerType) {
Thiago Macieira54a0e102015-05-05 21:25:06 -0700118 it->flags |= CborIteratorFlag_NegativeInteger;
Thiago Macieira851c4812015-05-08 15:23:20 -0700119 it->type = CborIntegerType;
120 } else if (majortype == SimpleTypesType) {
Thiago Macieira54a0e102015-05-05 21:25:06 -0700121 switch (descriptor) {
122 case FalseValue:
123 it->extra = false;
Thiago Macieira851c4812015-05-08 15:23:20 -0700124 it->type = CborBooleanType;
Thiago Macieira991dd922015-05-07 11:57:59 -0700125 break;
126
Thiago Macieira851c4812015-05-08 15:23:20 -0700127 case SinglePrecisionFloat:
128 case DoublePrecisionFloat:
129 it->flags |= CborIteratorFlag_IntegerValueTooLarge;
Thiago Macieiradbc01292016-06-06 17:02:25 -0700130 /* fall through */
Thiago Macieira54a0e102015-05-05 21:25:06 -0700131 case TrueValue:
132 case NullValue:
133 case UndefinedValue:
134 case HalfPrecisionFloat:
Thiago Macieira851c4812015-05-08 15:23:20 -0700135 it->type = *it->ptr;
Thiago Macieira54a0e102015-05-05 21:25:06 -0700136 break;
137
138 case SimpleTypeInNextByte:
Thiago Macieira851c4812015-05-08 15:23:20 -0700139 it->extra = (uint8_t)it->ptr[1];
Thiago Macieira54a0e102015-05-05 21:25:06 -0700140#ifndef CBOR_PARSER_NO_STRICT_CHECKS
Thiago Macieira851c4812015-05-08 15:23:20 -0700141 if (unlikely(it->extra < 32)) {
142 it->type = CborInvalidType;
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700143 return CborErrorIllegalSimpleType;
Thiago Macieira851c4812015-05-08 15:23:20 -0700144 }
Thiago Macieira54a0e102015-05-05 21:25:06 -0700145#endif
Thiago Macieira991dd922015-05-07 11:57:59 -0700146 break;
147
Thiago Macieira54a0e102015-05-05 21:25:06 -0700148 case 28:
149 case 29:
150 case 30:
Thiago Macieira54a0e102015-05-05 21:25:06 -0700151 case Break:
Thiago Macieiradbc01292016-06-06 17:02:25 -0700152 assert(false); /* these conditions can't be reached */
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700153 return CborErrorUnexpectedBreak;
Thiago Macieira54a0e102015-05-05 21:25:06 -0700154 }
Thiago Macieira851c4812015-05-08 15:23:20 -0700155 return CborNoError;
Thiago Macieira54a0e102015-05-05 21:25:06 -0700156 }
157
Thiago Macieiradbc01292016-06-06 17:02:25 -0700158 /* try to decode up to 16 bits */
Thiago Macieira54a0e102015-05-05 21:25:06 -0700159 if (descriptor < Value8Bit)
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700160 return CborNoError;
Thiago Macieira54a0e102015-05-05 21:25:06 -0700161
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700162 if (descriptor == Value8Bit)
163 it->extra = (uint8_t)it->ptr[1];
164 else if (descriptor == Value16Bit)
Thiago Macieira54a0e102015-05-05 21:25:06 -0700165 it->extra = get16(it->ptr + 1);
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700166 else
Thiago Macieiradbc01292016-06-06 17:02:25 -0700167 it->flags |= CborIteratorFlag_IntegerValueTooLarge; /* Value32Bit or Value64Bit */
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700168 return CborNoError;
169}
Thiago Macieira54a0e102015-05-05 21:25:06 -0700170
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700171static CborError preparse_next_value(CborValue *it)
172{
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700173 if (it->remaining != UINT32_MAX) {
Thiago Macieiradbc01292016-06-06 17:02:25 -0700174 /* don't decrement the item count if the current item is tag: they don't count */
Thiago Macieira11e913f2015-05-07 13:01:18 -0700175 if (it->type != CborTagType && !--it->remaining) {
176 it->type = CborInvalidType;
Thiago Macieira56d99832015-05-07 14:34:27 -0700177 return CborNoError;
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700178 }
Thiago Macieira5752ce52015-06-16 12:10:03 -0700179 } else if (it->remaining == UINT32_MAX && it->ptr != it->parser->end && *it->ptr == (uint8_t)BreakByte) {
Thiago Macieiradbc01292016-06-06 17:02:25 -0700180 /* end of map or array */
Thiago Macieira56d99832015-05-07 14:34:27 -0700181 ++it->ptr;
182 it->type = CborInvalidType;
183 it->remaining = 0;
184 return CborNoError;
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700185 }
Thiago Macieira56d99832015-05-07 14:34:27 -0700186
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700187 return preparse_value(it);
188}
189
190static CborError advance_internal(CborValue *it)
191{
192 uint64_t length;
Thiago Macieira4e9626c2015-09-21 14:57:17 -0700193 CborError err = extract_number(&it->ptr, it->parser->end, &length);
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700194 assert(err == CborNoError);
195
Thiago Macieira56d99832015-05-07 14:34:27 -0700196 if (it->type == CborByteStringType || it->type == CborTextStringType) {
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700197 assert(length == (size_t)length);
Thiago Macieira56d99832015-05-07 14:34:27 -0700198 assert((it->flags & CborIteratorFlag_UnknownLength) == 0);
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700199 it->ptr += length;
200 }
201
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700202 return preparse_next_value(it);
Thiago Macieira54a0e102015-05-05 21:25:06 -0700203}
204
Thiago Macieira2312efd2015-05-06 16:07:48 -0700205/** \internal
206 *
207 * Decodes the CBOR integer value when it is larger than the 16 bits available
208 * in value->extra. This function requires that value->flags have the
209 * CborIteratorFlag_IntegerValueTooLarge flag set.
210 *
211 * This function is also used to extract single- and double-precision floating
212 * point values (SinglePrecisionFloat == Value32Bit and DoublePrecisionFloat ==
213 * Value64Bit).
214 */
Thiago Macieira54a0e102015-05-05 21:25:06 -0700215uint64_t _cbor_value_decode_int64_internal(const CborValue *value)
216{
Thiago Macieira2312efd2015-05-06 16:07:48 -0700217 assert(value->flags & CborIteratorFlag_IntegerValueTooLarge ||
218 value->type == CborFloatType || value->type == CborDoubleType);
Thiago Macieira851c4812015-05-08 15:23:20 -0700219
Thiago Macieiradbc01292016-06-06 17:02:25 -0700220 /* since the additional information can only be Value32Bit or Value64Bit,
221 * we just need to test for the one bit those two options differ */
Thiago Macieira851c4812015-05-08 15:23:20 -0700222 assert((*value->ptr & SmallValueMask) == Value32Bit || (*value->ptr & SmallValueMask) == Value64Bit);
223 if ((*value->ptr & 1) == (Value32Bit & 1))
Thiago Macieira54a0e102015-05-05 21:25:06 -0700224 return get32(value->ptr + 1);
225
226 assert((*value->ptr & SmallValueMask) == Value64Bit);
227 return get64(value->ptr + 1);
228}
229
230/**
231 * Initializes the CBOR parser for parsing \a size bytes beginning at \a
232 * buffer. Parsing will use flags set in \a flags. The iterator to the first
233 * element is returned in \a it.
Thiago Macieira2312efd2015-05-06 16:07:48 -0700234 *
235 * The \a parser structure needs to remain valid throughout the decoding
236 * process. It is not thread-safe to share one CborParser among multiple
237 * threads iterating at the same time, but the object can be copied so multiple
238 * threads can iterate.
239 *
240 * ### Write how to determine the end pointer
241 * ### Write how to do limited-buffer windowed decoding
Thiago Macieira54a0e102015-05-05 21:25:06 -0700242 */
Thiago Macieira5752ce52015-06-16 12:10:03 -0700243CborError cbor_parser_init(const uint8_t *buffer, size_t size, int flags, CborParser *parser, CborValue *it)
Thiago Macieira54a0e102015-05-05 21:25:06 -0700244{
245 memset(parser, 0, sizeof(*parser));
246 parser->end = buffer + size;
Thiago Macieira54a0e102015-05-05 21:25:06 -0700247 parser->flags = flags;
248 it->parser = parser;
249 it->ptr = buffer;
Thiago Macieiradbc01292016-06-06 17:02:25 -0700250 it->remaining = 1; /* there's one type altogether, usually an array or map */
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700251 return preparse_value(it);
Thiago Macieira2312efd2015-05-06 16:07:48 -0700252}
253
254/**
255 * Advances the CBOR value \a it by one fixed-size position. Fixed-size types
256 * are: integers, tags, simple types (including boolean, null and undefined
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700257 * values) and floating point types.
Thiago Macieira2312efd2015-05-06 16:07:48 -0700258 *
259 * \sa cbor_value_at_end(), cbor_value_advance(), cbor_value_begin_recurse(), cbor_value_end_recurse()
260 */
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700261CborError cbor_value_advance_fixed(CborValue *it)
Thiago Macieira54a0e102015-05-05 21:25:06 -0700262{
Thiago Macieira2312efd2015-05-06 16:07:48 -0700263 assert(it->type != CborInvalidType);
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700264 assert(is_fixed_type(it->type));
265 if (!it->remaining)
266 return CborErrorAdvancePastEOF;
267 return advance_internal(it);
Thiago Macieira54a0e102015-05-05 21:25:06 -0700268}
269
Thiago Macieira4a99af92015-05-12 10:41:45 +0900270static CborError advance_recursive(CborValue *it, int nestingLevel)
271{
272 if (is_fixed_type(it->type))
273 return advance_internal(it);
274
275 if (!cbor_value_is_container(it)) {
276 size_t len = SIZE_MAX;
Thiago Macieiraff130bc2015-06-19 15:15:33 -0700277 return _cbor_value_copy_string(it, NULL, &len, it);
Thiago Macieira4a99af92015-05-12 10:41:45 +0900278 }
279
Thiago Macieiradbc01292016-06-06 17:02:25 -0700280 /* map or array */
Thiago Macieira4a99af92015-05-12 10:41:45 +0900281 if (nestingLevel == CBOR_PARSER_MAX_RECURSIONS)
282 return CborErrorNestingTooDeep;
283
284 CborError err;
285 CborValue recursed;
286 err = cbor_value_enter_container(it, &recursed);
287 if (err)
288 return err;
289 while (!cbor_value_at_end(&recursed)) {
290 err = advance_recursive(&recursed, nestingLevel + 1);
291 if (err)
292 return err;
293 }
294 return cbor_value_leave_container(it, &recursed);
295}
296
297
Thiago Macieira2312efd2015-05-06 16:07:48 -0700298/**
299 * Advances the CBOR value \a it by one element, skipping over containers.
300 * Unlike cbor_value_advance_fixed(), this function can be called on a CBOR
301 * value of any type. However, if the type is a container (map or array) or a
302 * string with a chunked payload, this function will not run in constant time
303 * and will recurse into itself (it will run on O(n) time for the number of
304 * elements or chunks and will use O(n) memory for the number of nested
305 * containers).
306 *
Thiago Macieira2312efd2015-05-06 16:07:48 -0700307 * \sa cbor_value_at_end(), cbor_value_advance_fixed(), cbor_value_begin_recurse(), cbor_value_end_recurse()
308 */
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700309CborError cbor_value_advance(CborValue *it)
Thiago Macieira2312efd2015-05-06 16:07:48 -0700310{
311 assert(it->type != CborInvalidType);
312 if (!it->remaining)
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700313 return CborErrorAdvancePastEOF;
Thiago Macieira4a99af92015-05-12 10:41:45 +0900314 return advance_recursive(it, 0);
Thiago Macieira2312efd2015-05-06 16:07:48 -0700315}
316
317/**
Thiago Macieirac4a73c62015-05-09 18:14:11 -0700318 * Advances the CBOR value \a it until it no longer points to a tag. If \a it is
319 * already not pointing to a tag, then this function returns it unchanged.
320 *
321 * \sa cbor_value_advance_fixed(), cbor_value_advance()
322 */
323CborError cbor_value_skip_tag(CborValue *it)
324{
325 while (cbor_value_is_tag(it)) {
326 CborError err = cbor_value_advance_fixed(it);
327 if (err)
328 return err;
329 }
330 return CborNoError;
331}
332
Thiago Macieirac4a73c62015-05-09 18:14:11 -0700333/**
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700334 * \fn bool cbor_value_is_container(const CborValue *it)
335 *
Thiago Macieira2312efd2015-05-06 16:07:48 -0700336 * Returns true if the \a it value is a container and requires recursion in
337 * order to decode (maps and arrays), false otherwise.
338 */
Thiago Macieira54a0e102015-05-05 21:25:06 -0700339
Thiago Macieira2312efd2015-05-06 16:07:48 -0700340/**
341 * Creates a CborValue iterator pointing to the first element of the container
342 * represented by \a it and saves it in \a recursed. The \a it container object
343 * needs to be kept and passed again to cbor_value_leave_container() in order
344 * to continue iterating past this container.
345 *
346 * \sa cbor_value_is_container(), cbor_value_leave_container(), cbor_value_advance()
347 */
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700348CborError cbor_value_enter_container(const CborValue *it, CborValue *recursed)
Thiago Macieira54a0e102015-05-05 21:25:06 -0700349{
Thiago Macieira56d99832015-05-07 14:34:27 -0700350 CborError err;
Thiago Macieira2312efd2015-05-06 16:07:48 -0700351 assert(cbor_value_is_container(it));
Thiago Macieira54a0e102015-05-05 21:25:06 -0700352 *recursed = *it;
Thiago Macieira56d99832015-05-07 14:34:27 -0700353
Thiago Macieira54a0e102015-05-05 21:25:06 -0700354 if (it->flags & CborIteratorFlag_UnknownLength) {
355 recursed->remaining = UINT32_MAX;
Thiago Macieira56d99832015-05-07 14:34:27 -0700356 ++recursed->ptr;
357 err = preparse_value(recursed);
358 if (err != CborErrorUnexpectedBreak)
359 return err;
Thiago Macieiradbc01292016-06-06 17:02:25 -0700360 /* actually, break was expected here
361 * it's just an empty container */
Thiago Macieira56d99832015-05-07 14:34:27 -0700362 ++recursed->ptr;
Thiago Macieira54a0e102015-05-05 21:25:06 -0700363 } else {
Thiago Macieira56d99832015-05-07 14:34:27 -0700364 uint64_t len;
Thiago Macieira4e9626c2015-09-21 14:57:17 -0700365 err = extract_number(&recursed->ptr, recursed->parser->end, &len);
Thiago Macieira56d99832015-05-07 14:34:27 -0700366 assert(err == CborNoError);
Thiago Macieira56d99832015-05-07 14:34:27 -0700367
Thiago Macieirae12dfd02016-06-07 16:29:25 -0700368 recursed->remaining = (uint32_t)len;
Thiago Macieira3f76f632015-05-12 10:10:09 +0900369 if (recursed->remaining != len || len == UINT32_MAX) {
Thiago Macieiradbc01292016-06-06 17:02:25 -0700370 /* back track the pointer to indicate where the error occurred */
Thiago Macieira3f76f632015-05-12 10:10:09 +0900371 recursed->ptr = it->ptr;
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700372 return CborErrorDataTooLarge;
Thiago Macieira3f76f632015-05-12 10:10:09 +0900373 }
Thiago Macieirace16f052015-05-07 23:14:25 -0700374 if (recursed->type == CborMapType) {
Thiago Macieiradbc01292016-06-06 17:02:25 -0700375 /* maps have keys and values, so we need to multiply by 2 */
Thiago Macieira3f76f632015-05-12 10:10:09 +0900376 if (recursed->remaining > UINT32_MAX / 2) {
Thiago Macieiradbc01292016-06-06 17:02:25 -0700377 /* back track the pointer to indicate where the error occurred */
Thiago Macieira3f76f632015-05-12 10:10:09 +0900378 recursed->ptr = it->ptr;
Thiago Macieirace16f052015-05-07 23:14:25 -0700379 return CborErrorDataTooLarge;
Thiago Macieira3f76f632015-05-12 10:10:09 +0900380 }
Thiago Macieirace16f052015-05-07 23:14:25 -0700381 recursed->remaining *= 2;
382 }
Thiago Macieira56d99832015-05-07 14:34:27 -0700383 if (len != 0)
384 return preparse_value(recursed);
Thiago Macieira54a0e102015-05-05 21:25:06 -0700385 }
Thiago Macieira56d99832015-05-07 14:34:27 -0700386
Thiago Macieiradbc01292016-06-06 17:02:25 -0700387 /* the case of the empty container */
Thiago Macieira56d99832015-05-07 14:34:27 -0700388 recursed->type = CborInvalidType;
389 recursed->remaining = 0;
390 return CborNoError;
Thiago Macieirac70169f2015-05-06 07:49:44 -0700391}
392
Thiago Macieira2312efd2015-05-06 16:07:48 -0700393/**
394 * Updates \a it to point to the next element after the container. The \a
Thiago Macieira56d99832015-05-07 14:34:27 -0700395 * recursed object needs to point to the element obtained either by advancing
396 * the last element of the container (via cbor_value_advance(),
397 * cbor_value_advance_fixed(), a nested cbor_value_leave_container(), or the \c
398 * next pointer from cbor_value_copy_string() or cbor_value_dup_string()).
Thiago Macieira2312efd2015-05-06 16:07:48 -0700399 *
400 * \sa cbor_value_enter_container(), cbor_value_at_end()
401 */
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700402CborError cbor_value_leave_container(CborValue *it, const CborValue *recursed)
Thiago Macieirac70169f2015-05-06 07:49:44 -0700403{
Thiago Macieira2312efd2015-05-06 16:07:48 -0700404 assert(cbor_value_is_container(it));
Thiago Macieira56d99832015-05-07 14:34:27 -0700405 assert(recursed->type == CborInvalidType);
Thiago Macieirac70169f2015-05-06 07:49:44 -0700406 it->ptr = recursed->ptr;
Thiago Macieira56d99832015-05-07 14:34:27 -0700407 return preparse_next_value(it);
Thiago Macieirac70169f2015-05-06 07:49:44 -0700408}
409
Thiago Macieira2312efd2015-05-06 16:07:48 -0700410/**
411 * Calculates the length of the string in \a value and stores the result in \a
412 * len. This function is different from cbor_value_get_string_length() in that
413 * it calculates the length even for strings sent in chunks. For that reason,
414 * this function may not run in constant time (it will run in O(n) time on the
415 * number of chunks).
416 *
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700417 * \note On 32-bit platforms, this function will return error condition of \ref
418 * CborErrorDataTooLarge if the stream indicates a length that is too big to
419 * fit in 32-bit.
Thiago Macieira2312efd2015-05-06 16:07:48 -0700420 *
421 * \sa cbor_value_get_string_length(), cbor_value_copy_string(), cbor_value_is_length_known()
422 */
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700423CborError cbor_value_calculate_string_length(const CborValue *value, size_t *len)
Thiago Macieira2312efd2015-05-06 16:07:48 -0700424{
Thiago Macieira9ae05812015-05-11 15:09:09 +0900425 *len = SIZE_MAX;
Thiago Macieiraff130bc2015-06-19 15:15:33 -0700426 return _cbor_value_copy_string(value, NULL, len, NULL);
Thiago Macieirac70169f2015-05-06 07:49:44 -0700427}
428
Thiago Macieiradbc01292016-06-06 17:02:25 -0700429/* We return uintptr_t so that we can pass memcpy directly as the iteration
430 * function. The choice is to optimize for memcpy, which is used in the base
431 * parser API (cbor_value_copy_string), while memcmp is used in convenience API
432 * only. */
Thiago Macieira5752ce52015-06-16 12:10:03 -0700433typedef uintptr_t (*IterateFunction)(char *, const uint8_t *, size_t);
Thiago Macieira9ae05812015-05-11 15:09:09 +0900434
Thiago Macieira5752ce52015-06-16 12:10:03 -0700435static uintptr_t iterate_noop(char *dest, const uint8_t *src, size_t len)
Thiago Macieira9ae05812015-05-11 15:09:09 +0900436{
437 (void)dest;
438 (void)src;
439 (void)len;
440 return true;
441}
442
Thiago Macieira5752ce52015-06-16 12:10:03 -0700443static uintptr_t iterate_memcmp(char *s1, const uint8_t *s2, size_t len)
Thiago Macieirac4a73c62015-05-09 18:14:11 -0700444{
Thiago Macieira5752ce52015-06-16 12:10:03 -0700445 return memcmp(s1, (const char *)s2, len) == 0;
Thiago Macieirac4a73c62015-05-09 18:14:11 -0700446}
447
Thiago Macieira9ae05812015-05-11 15:09:09 +0900448static CborError iterate_string_chunks(const CborValue *value, char *buffer, size_t *buflen,
449 bool *result, CborValue *next, IterateFunction func)
450{
451 assert(cbor_value_is_byte_string(value) || cbor_value_is_text_string(value));
452
453 size_t total;
454 CborError err;
Thiago Macieira5752ce52015-06-16 12:10:03 -0700455 const uint8_t *ptr = value->ptr;
Thiago Macieira9ae05812015-05-11 15:09:09 +0900456 if (cbor_value_is_length_known(value)) {
Thiago Macieiradbc01292016-06-06 17:02:25 -0700457 /* easy case: fixed length */
Thiago Macieira9ae05812015-05-11 15:09:09 +0900458 err = extract_length(value->parser, &ptr, &total);
459 if (err)
460 return err;
Thiago Macieira63abed92015-10-28 17:01:14 -0700461 if (total > (size_t)(value->parser->end - ptr))
Thiago Macieira9ae05812015-05-11 15:09:09 +0900462 return CborErrorUnexpectedEOF;
463 if (total <= *buflen)
Thiago Macieirae12dfd02016-06-07 16:29:25 -0700464 *result = !!func(buffer, ptr, total);
Thiago Macieira9ae05812015-05-11 15:09:09 +0900465 else
466 *result = false;
467 ptr += total;
468 } else {
Thiago Macieiradbc01292016-06-06 17:02:25 -0700469 /* chunked */
Thiago Macieira9ae05812015-05-11 15:09:09 +0900470 ++ptr;
471 total = 0;
472 *result = true;
473 while (true) {
474 size_t chunkLen;
475 size_t newTotal;
476
477 if (ptr == value->parser->end)
478 return CborErrorUnexpectedEOF;
479
Thiago Macieira5752ce52015-06-16 12:10:03 -0700480 if (*ptr == (uint8_t)BreakByte) {
Thiago Macieira9ae05812015-05-11 15:09:09 +0900481 ++ptr;
482 break;
483 }
484
Thiago Macieiradbc01292016-06-06 17:02:25 -0700485 /* is this the right type? */
Thiago Macieira9ae05812015-05-11 15:09:09 +0900486 if ((*ptr & MajorTypeMask) != value->type)
487 return CborErrorIllegalType;
488
489 err = extract_length(value->parser, &ptr, &chunkLen);
490 if (err)
491 return err;
492
Thiago Macieira1de31a42015-06-16 16:01:16 -0700493 if (unlikely(add_check_overflow(total, chunkLen, &newTotal)))
Thiago Macieira9ae05812015-05-11 15:09:09 +0900494 return CborErrorDataTooLarge;
495
Thiago Macieira63abed92015-10-28 17:01:14 -0700496 if (chunkLen > (size_t)(value->parser->end - ptr))
Thiago Macieira9ae05812015-05-11 15:09:09 +0900497 return CborErrorUnexpectedEOF;
498
499 if (*result && *buflen >= newTotal)
Thiago Macieirae12dfd02016-06-07 16:29:25 -0700500 *result = !!func(buffer + total, ptr, chunkLen);
Thiago Macieira9ae05812015-05-11 15:09:09 +0900501 else
502 *result = false;
503
504 ptr += chunkLen;
505 total = newTotal;
506 }
507 }
508
Thiago Macieiradbc01292016-06-06 17:02:25 -0700509 /* is there enough room for the ending NUL byte? */
Thiago Macieira9ae05812015-05-11 15:09:09 +0900510 if (*result && *buflen > total)
Thiago Macieirae12dfd02016-06-07 16:29:25 -0700511 *result = !!func(buffer + total, (const uint8_t *)"", 1);
Thiago Macieira9ae05812015-05-11 15:09:09 +0900512 *buflen = total;
513
514 if (next) {
515 *next = *value;
516 next->ptr = ptr;
517 return preparse_next_value(next);
518 }
519 return CborNoError;
520}
521
Thiago Macieira2312efd2015-05-06 16:07:48 -0700522/**
Thiago Macieiraff130bc2015-06-19 15:15:33 -0700523 * \fn CborError cbor_value_copy_text_string(const CborValue *value, char *buffer, size_t *buflen, CborValue *next)
524 *
Thiago Macieira2312efd2015-05-06 16:07:48 -0700525 * Copies the string pointed by \a value into the buffer provided at \a buffer
526 * of \a buflen bytes. If \a buffer is a NULL pointer, this function will not
527 * copy anything and will only update the \a next value.
528 *
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700529 * If the provided buffer length was too small, this function returns an error
530 * condition of \ref CborErrorOutOfMemory. If you need to calculate the length
531 * of the string in order to preallocate a buffer, use
Thiago Macieira2312efd2015-05-06 16:07:48 -0700532 * cbor_value_calculate_string_length().
533 *
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700534 * On success, this function sets the number of bytes copied to \c{*buflen}. If
535 * the buffer is large enough, this function will insert a null byte after the
536 * last copied byte, to facilitate manipulation of text strings. That byte is
537 * not included in the returned value of \c{*buflen}.
Thiago Macieira2312efd2015-05-06 16:07:48 -0700538 *
539 * The \a next pointer, if not null, will be updated to point to the next item
540 * after this string. If \a value points to the last item, then \a next will be
541 * invalid.
542 *
543 * \note This function does not perform UTF-8 validation on the incoming text
544 * string.
545 *
Thiago Macieiraff130bc2015-06-19 15:15:33 -0700546 * \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 -0700547 */
Thiago Macieiraff130bc2015-06-19 15:15:33 -0700548
549/**
550 * \fn CborError cbor_value_copy_byte_string(const CborValue *value, uint8_t *buffer, size_t *buflen, CborValue *next)
551 *
552 * Copies the string pointed by \a value into the buffer provided at \a buffer
553 * of \a buflen bytes. If \a buffer is a NULL pointer, this function will not
554 * copy anything and will only update the \a next value.
555 *
556 * If the provided buffer length was too small, this function returns an error
557 * condition of \ref CborErrorOutOfMemory. If you need to calculate the length
558 * of the string in order to preallocate a buffer, use
559 * cbor_value_calculate_string_length().
560 *
561 * On success, this function sets the number of bytes copied to \c{*buflen}. If
562 * the buffer is large enough, this function will insert a null byte after the
563 * last copied byte, to facilitate manipulation of null-terminated strings.
564 * That byte is not included in the returned value of \c{*buflen}.
565 *
566 * The \a next pointer, if not null, will be updated to point to the next item
567 * after this string. If \a value points to the last item, then \a next will be
568 * invalid.
569 *
570 * \sa cbor_value_dup_text_string(), cbor_value_copy_text_string(), cbor_value_get_string_length(), cbor_value_calculate_string_length()
571 */
572
573CborError _cbor_value_copy_string(const CborValue *value, void *buffer,
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700574 size_t *buflen, CborValue *next)
Thiago Macieira2312efd2015-05-06 16:07:48 -0700575{
Thiago Macieira9ae05812015-05-11 15:09:09 +0900576 bool copied_all;
Thiago Macieiraed5b57c2015-07-07 16:38:27 -0700577 CborError err = iterate_string_chunks(value, (char*)buffer, buflen, &copied_all, next,
Thiago Macieira9ae05812015-05-11 15:09:09 +0900578 buffer ? (IterateFunction)memcpy : iterate_noop);
579 return err ? err :
580 copied_all ? CborNoError : CborErrorOutOfMemory;
Thiago Macieirac70169f2015-05-06 07:49:44 -0700581}
582
Thiago Macieirac4a73c62015-05-09 18:14:11 -0700583/**
584 * Compares the entry \a value with the string \a string and store the result
585 * in \a result. If the value is different from \a string or if it is not a
586 * text string, \a result will contain \c false.
587 *
588 * The entry at \a value may be a tagged string. If \a is not a string or a
589 * tagged string, the comparison result will be false.
590 */
591CborError cbor_value_text_string_equals(const CborValue *value, const char *string, bool *result)
592{
593 CborValue copy = *value;
594 CborError err = cbor_value_skip_tag(&copy);
595 if (err)
596 return err;
597 if (!cbor_value_is_text_string(&copy)) {
598 *result = false;
599 return CborNoError;
600 }
601
602 size_t len = strlen(string);
603 return iterate_string_chunks(&copy, CONST_CAST(char *, string), &len, result, NULL, iterate_memcmp);
604}
605
606/**
Thiago Macieira7b623c22015-05-11 15:52:14 +0900607 * Attempts to find the value in map \a map that corresponds to the text string
608 * entry \a string. If the item is found, it is stored in \a result. If no item
609 * is found matching the key, then \a result will contain an element of type
610 * \ref CborInvalidType.
611 *
612 * \note This function may be expensive to execute.
613 */
614CborError cbor_value_map_find_value(const CborValue *map, const char *string, CborValue *element)
615{
616 assert(cbor_value_is_map(map));
617 size_t len = strlen(string);
618 CborError err = cbor_value_enter_container(map, element);
619 if (err)
620 goto error;
621
622 while (!cbor_value_at_end(element)) {
Thiago Macieiradbc01292016-06-06 17:02:25 -0700623 /* find the non-tag so we can compare */
Thiago Macieira7b623c22015-05-11 15:52:14 +0900624 err = cbor_value_skip_tag(element);
625 if (err)
626 goto error;
627 if (cbor_value_is_text_string(element)) {
628 bool equals;
629 size_t dummyLen = len;
630 err = iterate_string_chunks(element, CONST_CAST(char *, string), &dummyLen,
631 &equals, element, iterate_memcmp);
632 if (err)
633 goto error;
634 if (equals)
635 return preparse_value(element);
636 } else {
Thiago Macieiradbc01292016-06-06 17:02:25 -0700637 /* skip this key */
Thiago Macieira7b623c22015-05-11 15:52:14 +0900638 err = cbor_value_advance(element);
639 if (err)
640 goto error;
641 }
642
Thiago Macieiradbc01292016-06-06 17:02:25 -0700643 /* skip this value */
Thiago Macieira7b623c22015-05-11 15:52:14 +0900644 err = cbor_value_skip_tag(element);
645 if (err)
646 goto error;
647 err = cbor_value_advance(element);
648 if (err)
649 goto error;
650 }
651
Thiago Macieiradbc01292016-06-06 17:02:25 -0700652 /* not found */
Thiago Macieira7b623c22015-05-11 15:52:14 +0900653 element->type = CborInvalidType;
654 return CborNoError;
655
656error:
657 element->type = CborInvalidType;
658 return err;
659}
660
661/**
Thiago Macieirac4a73c62015-05-09 18:14:11 -0700662 * Extracts a half-precision floating point from \a value and stores it in \a
663 * result.
664 */
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700665CborError cbor_value_get_half_float(const CborValue *value, void *result)
Thiago Macieirac70169f2015-05-06 07:49:44 -0700666{
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700667 assert(value->type == CborHalfFloatType);
Thiago Macieirac70169f2015-05-06 07:49:44 -0700668
Thiago Macieiradbc01292016-06-06 17:02:25 -0700669 /* size has been computed already */
Thiago Macieirac70169f2015-05-06 07:49:44 -0700670 uint16_t v = get16(value->ptr + 1);
671 memcpy(result, &v, sizeof(v));
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700672 return CborNoError;
Thiago Macieira54a0e102015-05-05 21:25:06 -0700673}