blob: 18805241a96310d17b50b20e92b242340f74ed91 [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
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/**
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 Macieira54a0e102015-05-05 21:25:06 -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))) {
104 // special case
105 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;
130 // 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 Macieira851c4812015-05-08 15:23:20 -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
158 // try to decode up to 16 bits
159 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
167 it->flags |= CborIteratorFlag_IntegerValueTooLarge; // Value32Bit or Value64Bit
168 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 Macieira11e913f2015-05-07 13:01:18 -0700174 // don't decrement the item count if the current item is tag: they don't count
175 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 Macieira56d99832015-05-07 14:34:27 -0700180 // end of map or array
181 ++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
220 // since the additional information can only be Value32Bit or Value64Bit,
221 // we just need to test for the one bit those two options differ
222 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;
250 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
280 // map or array
281 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;
360 // actually, break was expected here
361 // it's just an empty container
362 ++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) {
370 // back track the pointer to indicate where the error occurred
371 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) {
375 // 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) {
377 // back track the pointer to indicate where the error occurred
378 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
387 // the case of the empty container
388 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 Macieira2312efd2015-05-06 16:07:48 -0700429/**
Thiago Macieiraff130bc2015-06-19 15:15:33 -0700430 * \fn CborError cbor_value_dup_text_string(const CborValue *value, char **buffer, size_t *buflen, CborValue *next)
431 *
Thiago Macieira2312efd2015-05-06 16:07:48 -0700432 * Allocates memory for the string pointed by \a value and copies it into this
433 * buffer. The pointer to the buffer is stored in \a buffer and the number of
434 * bytes copied is stored in \a len (those variables must not be NULL).
435 *
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700436 * If \c malloc returns a NULL pointer, this function will return error
437 * condition \ref CborErrorOutOfMemory.
Thiago Macieira2312efd2015-05-06 16:07:48 -0700438 *
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700439 * On success, \c{*buffer} will contain a valid pointer that must be freed by
440 * calling \c{free()}. This is the case even for zero-length strings.
Thiago Macieira2312efd2015-05-06 16:07:48 -0700441 *
442 * The \a next pointer, if not null, will be updated to point to the next item
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700443 * after this string. If \a value points to the last item, then \a next will be
444 * invalid.
Thiago Macieira2312efd2015-05-06 16:07:48 -0700445 *
446 * \note This function does not perform UTF-8 validation on the incoming text
447 * string.
448 *
Thiago Macieiraff130bc2015-06-19 15:15:33 -0700449 * \sa cbor_value_copy_text_string(), cbor_value_dup_byte_string()
Thiago Macieira2312efd2015-05-06 16:07:48 -0700450 */
Thiago Macieiraff130bc2015-06-19 15:15:33 -0700451
452/**
453 * \fn CborError cbor_value_dup_byte_string(const CborValue *value, uint8_t **buffer, size_t *buflen, CborValue *next)
454 *
455 * Allocates memory for the string pointed by \a value and copies it into this
456 * buffer. The pointer to the buffer is stored in \a buffer and the number of
457 * bytes copied is stored in \a len (those variables must not be NULL).
458 *
459 * If \c malloc returns a NULL pointer, this function will return error
460 * condition \ref CborErrorOutOfMemory.
461 *
462 * On success, \c{*buffer} will contain a valid pointer that must be freed by
463 * calling \c{free()}. This is the case even for zero-length strings.
464 *
465 * The \a next pointer, if not null, will be updated to point to the next item
466 * after this string. If \a value points to the last item, then \a next will be
467 * invalid.
468 *
469 * \sa cbor_value_copy_byte_string(), cbor_value_dup_text_string()
470 */
471CborError _cbor_value_dup_string(const CborValue *value, void **buffer, size_t *buflen, CborValue *next)
Thiago Macieirac70169f2015-05-06 07:49:44 -0700472{
Thiago Macieira2312efd2015-05-06 16:07:48 -0700473 assert(buffer);
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700474 assert(buflen);
Thiago Macieirafc870932015-06-19 15:01:35 -0700475 *buflen = SIZE_MAX;
Thiago Macieiraff130bc2015-06-19 15:15:33 -0700476 CborError err = _cbor_value_copy_string(value, NULL, buflen, NULL);
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700477 if (err)
478 return err;
Thiago Macieirac70169f2015-05-06 07:49:44 -0700479
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700480 ++*buflen;
481 *buffer = malloc(*buflen);
Thiago Macieira2312efd2015-05-06 16:07:48 -0700482 if (!*buffer) {
Thiago Macieirac70169f2015-05-06 07:49:44 -0700483 // out of memory
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700484 return CborErrorOutOfMemory;
Thiago Macieirac70169f2015-05-06 07:49:44 -0700485 }
Thiago Macieiraff130bc2015-06-19 15:15:33 -0700486 err = _cbor_value_copy_string(value, *buffer, buflen, next);
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700487 if (err) {
Thiago Macieira2312efd2015-05-06 16:07:48 -0700488 free(*buffer);
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700489 return err;
Thiago Macieirac70169f2015-05-06 07:49:44 -0700490 }
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700491 return CborNoError;
Thiago Macieira2312efd2015-05-06 16:07:48 -0700492}
493
Thiago Macieira9ae05812015-05-11 15:09:09 +0900494// We return uintptr_t so that we can pass memcpy directly as the iteration
495// function. The choice is to optimize for memcpy, which is used in the base
496// parser API (cbor_value_copy_string), while memcmp is used in convenience API
497// only.
Thiago Macieira5752ce52015-06-16 12:10:03 -0700498typedef uintptr_t (*IterateFunction)(char *, const uint8_t *, size_t);
Thiago Macieira9ae05812015-05-11 15:09:09 +0900499
Thiago Macieira5752ce52015-06-16 12:10:03 -0700500static uintptr_t iterate_noop(char *dest, const uint8_t *src, size_t len)
Thiago Macieira9ae05812015-05-11 15:09:09 +0900501{
502 (void)dest;
503 (void)src;
504 (void)len;
505 return true;
506}
507
Thiago Macieira5752ce52015-06-16 12:10:03 -0700508static uintptr_t iterate_memcmp(char *s1, const uint8_t *s2, size_t len)
Thiago Macieirac4a73c62015-05-09 18:14:11 -0700509{
Thiago Macieira5752ce52015-06-16 12:10:03 -0700510 return memcmp(s1, (const char *)s2, len) == 0;
Thiago Macieirac4a73c62015-05-09 18:14:11 -0700511}
512
Thiago Macieira9ae05812015-05-11 15:09:09 +0900513static CborError iterate_string_chunks(const CborValue *value, char *buffer, size_t *buflen,
514 bool *result, CborValue *next, IterateFunction func)
515{
516 assert(cbor_value_is_byte_string(value) || cbor_value_is_text_string(value));
517
518 size_t total;
519 CborError err;
Thiago Macieira5752ce52015-06-16 12:10:03 -0700520 const uint8_t *ptr = value->ptr;
Thiago Macieira9ae05812015-05-11 15:09:09 +0900521 if (cbor_value_is_length_known(value)) {
522 // easy case: fixed length
523 err = extract_length(value->parser, &ptr, &total);
524 if (err)
525 return err;
Thiago Macieira63abed92015-10-28 17:01:14 -0700526 if (total > (size_t)(value->parser->end - ptr))
Thiago Macieira9ae05812015-05-11 15:09:09 +0900527 return CborErrorUnexpectedEOF;
528 if (total <= *buflen)
Thiago Macieirae12dfd02016-06-07 16:29:25 -0700529 *result = !!func(buffer, ptr, total);
Thiago Macieira9ae05812015-05-11 15:09:09 +0900530 else
531 *result = false;
532 ptr += total;
533 } else {
534 // chunked
535 ++ptr;
536 total = 0;
537 *result = true;
538 while (true) {
539 size_t chunkLen;
540 size_t newTotal;
541
542 if (ptr == value->parser->end)
543 return CborErrorUnexpectedEOF;
544
Thiago Macieira5752ce52015-06-16 12:10:03 -0700545 if (*ptr == (uint8_t)BreakByte) {
Thiago Macieira9ae05812015-05-11 15:09:09 +0900546 ++ptr;
547 break;
548 }
549
550 // is this the right type?
551 if ((*ptr & MajorTypeMask) != value->type)
552 return CborErrorIllegalType;
553
554 err = extract_length(value->parser, &ptr, &chunkLen);
555 if (err)
556 return err;
557
Thiago Macieira1de31a42015-06-16 16:01:16 -0700558 if (unlikely(add_check_overflow(total, chunkLen, &newTotal)))
Thiago Macieira9ae05812015-05-11 15:09:09 +0900559 return CborErrorDataTooLarge;
560
Thiago Macieira63abed92015-10-28 17:01:14 -0700561 if (chunkLen > (size_t)(value->parser->end - ptr))
Thiago Macieira9ae05812015-05-11 15:09:09 +0900562 return CborErrorUnexpectedEOF;
563
564 if (*result && *buflen >= newTotal)
Thiago Macieirae12dfd02016-06-07 16:29:25 -0700565 *result = !!func(buffer + total, ptr, chunkLen);
Thiago Macieira9ae05812015-05-11 15:09:09 +0900566 else
567 *result = false;
568
569 ptr += chunkLen;
570 total = newTotal;
571 }
572 }
573
574 // is there enough room for the ending NUL byte?
575 if (*result && *buflen > total)
Thiago Macieirae12dfd02016-06-07 16:29:25 -0700576 *result = !!func(buffer + total, (const uint8_t *)"", 1);
Thiago Macieira9ae05812015-05-11 15:09:09 +0900577 *buflen = total;
578
579 if (next) {
580 *next = *value;
581 next->ptr = ptr;
582 return preparse_next_value(next);
583 }
584 return CborNoError;
585}
586
Thiago Macieira2312efd2015-05-06 16:07:48 -0700587/**
Thiago Macieiraff130bc2015-06-19 15:15:33 -0700588 * \fn CborError cbor_value_copy_text_string(const CborValue *value, char *buffer, size_t *buflen, CborValue *next)
589 *
Thiago Macieira2312efd2015-05-06 16:07:48 -0700590 * Copies the string pointed by \a value into the buffer provided at \a buffer
591 * of \a buflen bytes. If \a buffer is a NULL pointer, this function will not
592 * copy anything and will only update the \a next value.
593 *
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700594 * If the provided buffer length was too small, this function returns an error
595 * condition of \ref CborErrorOutOfMemory. If you need to calculate the length
596 * of the string in order to preallocate a buffer, use
Thiago Macieira2312efd2015-05-06 16:07:48 -0700597 * cbor_value_calculate_string_length().
598 *
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700599 * On success, this function sets the number of bytes copied to \c{*buflen}. If
600 * the buffer is large enough, this function will insert a null byte after the
601 * last copied byte, to facilitate manipulation of text strings. That byte is
602 * not included in the returned value of \c{*buflen}.
Thiago Macieira2312efd2015-05-06 16:07:48 -0700603 *
604 * The \a next pointer, if not null, will be updated to point to the next item
605 * after this string. If \a value points to the last item, then \a next will be
606 * invalid.
607 *
608 * \note This function does not perform UTF-8 validation on the incoming text
609 * string.
610 *
Thiago Macieiraff130bc2015-06-19 15:15:33 -0700611 * \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 -0700612 */
Thiago Macieiraff130bc2015-06-19 15:15:33 -0700613
614/**
615 * \fn CborError cbor_value_copy_byte_string(const CborValue *value, uint8_t *buffer, size_t *buflen, CborValue *next)
616 *
617 * Copies the string pointed by \a value into the buffer provided at \a buffer
618 * of \a buflen bytes. If \a buffer is a NULL pointer, this function will not
619 * copy anything and will only update the \a next value.
620 *
621 * If the provided buffer length was too small, this function returns an error
622 * condition of \ref CborErrorOutOfMemory. If you need to calculate the length
623 * of the string in order to preallocate a buffer, use
624 * cbor_value_calculate_string_length().
625 *
626 * On success, this function sets the number of bytes copied to \c{*buflen}. If
627 * the buffer is large enough, this function will insert a null byte after the
628 * last copied byte, to facilitate manipulation of null-terminated strings.
629 * That byte is not included in the returned value of \c{*buflen}.
630 *
631 * The \a next pointer, if not null, will be updated to point to the next item
632 * after this string. If \a value points to the last item, then \a next will be
633 * invalid.
634 *
635 * \sa cbor_value_dup_text_string(), cbor_value_copy_text_string(), cbor_value_get_string_length(), cbor_value_calculate_string_length()
636 */
637
638CborError _cbor_value_copy_string(const CborValue *value, void *buffer,
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700639 size_t *buflen, CborValue *next)
Thiago Macieira2312efd2015-05-06 16:07:48 -0700640{
Thiago Macieira9ae05812015-05-11 15:09:09 +0900641 bool copied_all;
Thiago Macieiraed5b57c2015-07-07 16:38:27 -0700642 CborError err = iterate_string_chunks(value, (char*)buffer, buflen, &copied_all, next,
Thiago Macieira9ae05812015-05-11 15:09:09 +0900643 buffer ? (IterateFunction)memcpy : iterate_noop);
644 return err ? err :
645 copied_all ? CborNoError : CborErrorOutOfMemory;
Thiago Macieirac70169f2015-05-06 07:49:44 -0700646}
647
Thiago Macieirac4a73c62015-05-09 18:14:11 -0700648/**
649 * Compares the entry \a value with the string \a string and store the result
650 * in \a result. If the value is different from \a string or if it is not a
651 * text string, \a result will contain \c false.
652 *
653 * The entry at \a value may be a tagged string. If \a is not a string or a
654 * tagged string, the comparison result will be false.
655 */
656CborError cbor_value_text_string_equals(const CborValue *value, const char *string, bool *result)
657{
658 CborValue copy = *value;
659 CborError err = cbor_value_skip_tag(&copy);
660 if (err)
661 return err;
662 if (!cbor_value_is_text_string(&copy)) {
663 *result = false;
664 return CborNoError;
665 }
666
667 size_t len = strlen(string);
668 return iterate_string_chunks(&copy, CONST_CAST(char *, string), &len, result, NULL, iterate_memcmp);
669}
670
671/**
Thiago Macieira7b623c22015-05-11 15:52:14 +0900672 * Attempts to find the value in map \a map that corresponds to the text string
673 * entry \a string. If the item is found, it is stored in \a result. If no item
674 * is found matching the key, then \a result will contain an element of type
675 * \ref CborInvalidType.
676 *
677 * \note This function may be expensive to execute.
678 */
679CborError cbor_value_map_find_value(const CborValue *map, const char *string, CborValue *element)
680{
681 assert(cbor_value_is_map(map));
682 size_t len = strlen(string);
683 CborError err = cbor_value_enter_container(map, element);
684 if (err)
685 goto error;
686
687 while (!cbor_value_at_end(element)) {
688 // find the non-tag so we can compare
689 err = cbor_value_skip_tag(element);
690 if (err)
691 goto error;
692 if (cbor_value_is_text_string(element)) {
693 bool equals;
694 size_t dummyLen = len;
695 err = iterate_string_chunks(element, CONST_CAST(char *, string), &dummyLen,
696 &equals, element, iterate_memcmp);
697 if (err)
698 goto error;
699 if (equals)
700 return preparse_value(element);
701 } else {
702 // skip this key
703 err = cbor_value_advance(element);
704 if (err)
705 goto error;
706 }
707
708 // skip this value
709 err = cbor_value_skip_tag(element);
710 if (err)
711 goto error;
712 err = cbor_value_advance(element);
713 if (err)
714 goto error;
715 }
716
717 // not found
718 element->type = CborInvalidType;
719 return CborNoError;
720
721error:
722 element->type = CborInvalidType;
723 return err;
724}
725
726/**
Thiago Macieirac4a73c62015-05-09 18:14:11 -0700727 * Extracts a half-precision floating point from \a value and stores it in \a
728 * result.
729 */
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700730CborError cbor_value_get_half_float(const CborValue *value, void *result)
Thiago Macieirac70169f2015-05-06 07:49:44 -0700731{
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700732 assert(value->type == CborHalfFloatType);
Thiago Macieirac70169f2015-05-06 07:49:44 -0700733
734 // size has been computed already
735 uint16_t v = get16(value->ptr + 1);
736 memcpy(result, &v, sizeof(v));
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700737 return CborNoError;
Thiago Macieira54a0e102015-05-05 21:25:06 -0700738}