blob: df237c213a31a1cf0e950b0603016e25b84197ed [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
25#define _BSD_SOURCE
26#include "cbor.h"
27#include "cborconstants_p.h"
28#include "compilersupport_p.h"
29
30#include <assert.h>
31#include <endian.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 Macieirac70169f2015-05-06 07:49:44 -070035#if defined(__GNUC__) && !defined(__INTEL_COMPILER) && !defined(__clang__) && \
36 (__GNUC__ * 100 + __GNUC_MINOR__ >= 404)
37# pragma GCC optimize("-ffunction-sections")
38#endif
39
Thiago Macieira54a0e102015-05-05 21:25:06 -070040/**
41 * \typedef CborValue
42 * This type contains one value parsed from the CBOR stream.
43 *
44 * To get the actual type, use cbor_value_get_type(). Then extract the value
45 * using one of the corresponding functions: cbor_value_get_boolean(), cbor_value_get_int64(),
46 * cbor_value_get_int(), cbor_value_copy_string(), cbor_value_get_array(), cbor_value_get_map(),
47 * cbor_value_get_double(), cbor_value_get_float().
48 *
49 * In C++ and C11 modes, you can additionally use the cbor_value_get_integer()
50 * and cbor_value_get_floating_point() generic functions.
51 *
52 * \omit
53 * Implementation details: the CborValue contains these fields:
54 * \list
55 * \li ptr: pointer to the actual data
56 * \li flags: flags from the decoder
Thiago Macieira2312efd2015-05-06 16:07:48 -070057 * \li extra: partially decoded integer value (0, 1 or 2 bytes)
Thiago Macieira54a0e102015-05-05 21:25:06 -070058 * \li remaining: remaining items in this collection after this item or UINT32_MAX if length is unknown
59 * \endlist
60 * \endomit
61 */
62
Thiago Macieira54a0e102015-05-05 21:25:06 -070063static inline uint16_t get16(const char *ptr)
64{
65 uint16_t result;
66 memcpy(&result, ptr, sizeof(result));
67 return be16toh(result);
68}
69
70static inline uint32_t get32(const char *ptr)
71{
72 uint32_t result;
73 memcpy(&result, ptr, sizeof(result));
74 return be32toh(result);
75}
76
77static inline uint64_t get64(const char *ptr)
78{
79 uint64_t result;
80 memcpy(&result, ptr, sizeof(result));
81 return be64toh(result);
82}
83
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -070084static inline CborError extract_number(const CborParser *parser, const char **ptr, uint64_t *len)
Thiago Macieira2312efd2015-05-06 16:07:48 -070085{
86 uint8_t additional_information = **ptr & SmallValueMask;
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -070087 ++*ptr;
Thiago Macieira2312efd2015-05-06 16:07:48 -070088 if (additional_information < Value8Bit) {
89 *len = additional_information;
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -070090 return CborNoError;
Thiago Macieira2312efd2015-05-06 16:07:48 -070091 }
92 if (unlikely(additional_information > Value64Bit))
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -070093 return CborErrorIllegalNumber;
Thiago Macieira2312efd2015-05-06 16:07:48 -070094
95 size_t bytesNeeded = 1 << (additional_information - Value8Bit);
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -070096 if (unlikely(*ptr + bytesNeeded > parser->end)) {
97 return CborErrorUnexpectedEOF;
Thiago Macieira2312efd2015-05-06 16:07:48 -070098 } else if (bytesNeeded == 1) {
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -070099 *len = (uint8_t)(*ptr)[0];
Thiago Macieira2312efd2015-05-06 16:07:48 -0700100 } else if (bytesNeeded == 2) {
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700101 *len = get16(*ptr);
Thiago Macieira2312efd2015-05-06 16:07:48 -0700102 } else if (bytesNeeded == 4) {
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700103 *len = get32(*ptr);
Thiago Macieira2312efd2015-05-06 16:07:48 -0700104 } else {
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700105 *len = get64(*ptr);
Thiago Macieira2312efd2015-05-06 16:07:48 -0700106 }
107 *ptr += bytesNeeded;
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700108 return CborNoError;
Thiago Macieira2312efd2015-05-06 16:07:48 -0700109}
110
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700111static inline CborError extract_length(const CborParser *parser, const char **ptr, size_t *len)
Thiago Macieira54a0e102015-05-05 21:25:06 -0700112{
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700113 uint64_t v;
114 CborError err = extract_number(parser, ptr, &v);
115 if (err)
116 return err;
117
118 *len = v;
119 if (v != *len)
120 return CborErrorDataTooLarge;
121 return CborNoError;
122}
123
124static bool is_fixed_type(uint8_t type)
125{
126 return type != CborTextStringType && type != CborByteStringType && type != CborArrayType &&
127 type != CborMapType;
128}
129
130static CborError preparse_value(CborValue *it)
131{
132 const CborParser *parser = it->parser;
Thiago Macieira11e913f2015-05-07 13:01:18 -0700133 it->type = CborInvalidType;
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700134
Thiago Macieira54a0e102015-05-05 21:25:06 -0700135 // are we at the end?
Thiago Macieira54a0e102015-05-05 21:25:06 -0700136 if (it->ptr == parser->end)
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700137 return CborErrorUnexpectedEOF;
Thiago Macieira54a0e102015-05-05 21:25:06 -0700138
139 uint8_t descriptor = *it->ptr;
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700140 uint8_t type = descriptor & MajorTypeMask;
Thiago Macieira54a0e102015-05-05 21:25:06 -0700141 it->flags = 0;
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700142 it->extra = (descriptor &= SmallValueMask);
143
144 if (descriptor == IndefiniteLength && !is_fixed_type(type)) {
145 // special case
146 it->flags |= CborIteratorFlag_UnknownLength;
147 it->type = type;
148 return CborNoError;
149 }
Thiago Macieira54a0e102015-05-05 21:25:06 -0700150
Thiago Macieirac70169f2015-05-06 07:49:44 -0700151 size_t bytesNeeded = descriptor < Value8Bit ? 0 : (1 << (descriptor - Value8Bit));
152 if (it->ptr + 1 + bytesNeeded > parser->end)
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700153 return CborErrorUnexpectedEOF;
Thiago Macieirac70169f2015-05-06 07:49:44 -0700154
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700155 switch ((CborMajorTypes)(type >> MajorTypeShift)) {
Thiago Macieira54a0e102015-05-05 21:25:06 -0700156 case NegativeIntegerType:
157 it->flags |= CborIteratorFlag_NegativeInteger;
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700158 type = CborIntegerType;
Thiago Macieira54a0e102015-05-05 21:25:06 -0700159 // fall through
160 case UnsignedIntegerType:
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700161 case ByteStringType:
162 case TextStringType:
163 case ArrayType:
164 case MapType:
Thiago Macieira54a0e102015-05-05 21:25:06 -0700165 case TagType:
166 break;
167
168 case SimpleTypesType:
169 switch (descriptor) {
170 case FalseValue:
171 it->extra = false;
Thiago Macieira991dd922015-05-07 11:57:59 -0700172 type = CborBooleanType;
173 break;
174
Thiago Macieira54a0e102015-05-05 21:25:06 -0700175 case TrueValue:
176 case NullValue:
177 case UndefinedValue:
178 case HalfPrecisionFloat:
179 case SinglePrecisionFloat:
180 case DoublePrecisionFloat:
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700181 type = *it->ptr;
Thiago Macieira54a0e102015-05-05 21:25:06 -0700182 break;
183
184 case SimpleTypeInNextByte:
Thiago Macieira54a0e102015-05-05 21:25:06 -0700185#ifndef CBOR_PARSER_NO_STRICT_CHECKS
Thiago Macieira991dd922015-05-07 11:57:59 -0700186 if ((unsigned char)it->ptr[1] < 32)
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700187 return CborErrorIllegalSimpleType;
Thiago Macieira54a0e102015-05-05 21:25:06 -0700188#endif
Thiago Macieira991dd922015-05-07 11:57:59 -0700189 break;
190
Thiago Macieira54a0e102015-05-05 21:25:06 -0700191 case 28:
192 case 29:
193 case 30:
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700194 return CborErrorUnknownType;
Thiago Macieira54a0e102015-05-05 21:25:06 -0700195
196 case Break:
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700197 return CborErrorUnexpectedBreak;
Thiago Macieira54a0e102015-05-05 21:25:06 -0700198 }
199 break;
Thiago Macieira54a0e102015-05-05 21:25:06 -0700200 }
201
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700202 if (unlikely(descriptor > Value64Bit))
203 return CborErrorIllegalNumber;
204
205 // no further errors possible
206 it->type = type;
207
Thiago Macieira54a0e102015-05-05 21:25:06 -0700208 // try to decode up to 16 bits
209 if (descriptor < Value8Bit)
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700210 return CborNoError;
Thiago Macieira54a0e102015-05-05 21:25:06 -0700211
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700212 if (descriptor == Value8Bit)
213 it->extra = (uint8_t)it->ptr[1];
214 else if (descriptor == Value16Bit)
Thiago Macieira54a0e102015-05-05 21:25:06 -0700215 it->extra = get16(it->ptr + 1);
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700216 else
217 it->flags |= CborIteratorFlag_IntegerValueTooLarge; // Value32Bit or Value64Bit
218 return CborNoError;
219}
Thiago Macieira54a0e102015-05-05 21:25:06 -0700220
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700221static CborError preparse_next_value(CborValue *it)
222{
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700223 if (it->remaining != UINT32_MAX) {
Thiago Macieira11e913f2015-05-07 13:01:18 -0700224 // don't decrement the item count if the current item is tag: they don't count
225 if (it->type != CborTagType && !--it->remaining) {
226 it->type = CborInvalidType;
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700227 return it->ptr == it->parser->end ? CborNoError : CborErrorGarbageAtEnd;
228 }
229 }
230 return preparse_value(it);
231}
232
233static CborError advance_internal(CborValue *it)
234{
235 uint64_t length;
236 CborError err = extract_number(it->parser, &it->ptr, &length);
237 assert(err == CborNoError);
238
239 if (!is_fixed_type(it->type)) {
240 assert(length == (size_t)length);
241 it->ptr += length;
242 }
243
244 if (it->remaining == UINT32_MAX && *it->ptr == (char)BreakByte) {
245 // end of map or array
246 it->remaining = 0;
247 return CborNoError;
248 }
249
250 return preparse_next_value(it);
Thiago Macieira54a0e102015-05-05 21:25:06 -0700251}
252
Thiago Macieira2312efd2015-05-06 16:07:48 -0700253/** \internal
254 *
255 * Decodes the CBOR integer value when it is larger than the 16 bits available
256 * in value->extra. This function requires that value->flags have the
257 * CborIteratorFlag_IntegerValueTooLarge flag set.
258 *
259 * This function is also used to extract single- and double-precision floating
260 * point values (SinglePrecisionFloat == Value32Bit and DoublePrecisionFloat ==
261 * Value64Bit).
262 */
Thiago Macieira54a0e102015-05-05 21:25:06 -0700263uint64_t _cbor_value_decode_int64_internal(const CborValue *value)
264{
Thiago Macieira2312efd2015-05-06 16:07:48 -0700265 assert(value->flags & CborIteratorFlag_IntegerValueTooLarge ||
266 value->type == CborFloatType || value->type == CborDoubleType);
Thiago Macieira54a0e102015-05-05 21:25:06 -0700267 if ((*value->ptr & SmallValueMask) == Value32Bit)
268 return get32(value->ptr + 1);
269
270 assert((*value->ptr & SmallValueMask) == Value64Bit);
271 return get64(value->ptr + 1);
272}
273
274/**
275 * Initializes the CBOR parser for parsing \a size bytes beginning at \a
276 * buffer. Parsing will use flags set in \a flags. The iterator to the first
277 * element is returned in \a it.
Thiago Macieira2312efd2015-05-06 16:07:48 -0700278 *
279 * The \a parser structure needs to remain valid throughout the decoding
280 * process. It is not thread-safe to share one CborParser among multiple
281 * threads iterating at the same time, but the object can be copied so multiple
282 * threads can iterate.
283 *
284 * ### Write how to determine the end pointer
285 * ### Write how to do limited-buffer windowed decoding
Thiago Macieira54a0e102015-05-05 21:25:06 -0700286 */
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700287CborError cbor_parser_init(const char *buffer, size_t size, int flags, CborParser *parser, CborValue *it)
Thiago Macieira54a0e102015-05-05 21:25:06 -0700288{
289 memset(parser, 0, sizeof(*parser));
290 parser->end = buffer + size;
Thiago Macieira54a0e102015-05-05 21:25:06 -0700291 parser->flags = flags;
292 it->parser = parser;
293 it->ptr = buffer;
294 it->remaining = 1; // there's one type altogether, usually an array or map
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700295 return preparse_value(it);
Thiago Macieira2312efd2015-05-06 16:07:48 -0700296}
297
298/**
299 * Advances the CBOR value \a it by one fixed-size position. Fixed-size types
300 * are: integers, tags, simple types (including boolean, null and undefined
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700301 * values) and floating point types.
Thiago Macieira2312efd2015-05-06 16:07:48 -0700302 *
303 * \sa cbor_value_at_end(), cbor_value_advance(), cbor_value_begin_recurse(), cbor_value_end_recurse()
304 */
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700305CborError cbor_value_advance_fixed(CborValue *it)
Thiago Macieira54a0e102015-05-05 21:25:06 -0700306{
Thiago Macieira2312efd2015-05-06 16:07:48 -0700307 assert(it->type != CborInvalidType);
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700308 assert(is_fixed_type(it->type));
309 if (!it->remaining)
310 return CborErrorAdvancePastEOF;
311 return advance_internal(it);
Thiago Macieira54a0e102015-05-05 21:25:06 -0700312}
313
Thiago Macieira2312efd2015-05-06 16:07:48 -0700314/**
315 * Advances the CBOR value \a it by one element, skipping over containers.
316 * Unlike cbor_value_advance_fixed(), this function can be called on a CBOR
317 * value of any type. However, if the type is a container (map or array) or a
318 * string with a chunked payload, this function will not run in constant time
319 * and will recurse into itself (it will run on O(n) time for the number of
320 * elements or chunks and will use O(n) memory for the number of nested
321 * containers).
322 *
Thiago Macieira2312efd2015-05-06 16:07:48 -0700323 * \sa cbor_value_at_end(), cbor_value_advance_fixed(), cbor_value_begin_recurse(), cbor_value_end_recurse()
324 */
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700325CborError cbor_value_advance(CborValue *it)
Thiago Macieira2312efd2015-05-06 16:07:48 -0700326{
327 assert(it->type != CborInvalidType);
328 if (!it->remaining)
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700329 return CborErrorAdvancePastEOF;
Thiago Macieira2312efd2015-05-06 16:07:48 -0700330 if (is_fixed_type(it->type))
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700331 return advance_internal(it);
Thiago Macieira2312efd2015-05-06 16:07:48 -0700332
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700333 if (!cbor_value_is_container(it))
334 return cbor_value_copy_string(it, NULL, 0, it);
335
336 // map or array
337 CborError err;
338 CborValue recursed;
339 err = cbor_value_enter_container(it, &recursed);
340 if (err)
341 return err;
342 while (!cbor_value_at_end(&recursed)) {
343 err = cbor_value_advance(&recursed);
344 if (err)
345 return err;
Thiago Macieira2312efd2015-05-06 16:07:48 -0700346 }
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700347 return cbor_value_leave_container(it, &recursed);
Thiago Macieira2312efd2015-05-06 16:07:48 -0700348}
349
350/**
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700351 * \fn bool cbor_value_is_container(const CborValue *it)
352 *
Thiago Macieira2312efd2015-05-06 16:07:48 -0700353 * Returns true if the \a it value is a container and requires recursion in
354 * order to decode (maps and arrays), false otherwise.
355 */
Thiago Macieira54a0e102015-05-05 21:25:06 -0700356
Thiago Macieira2312efd2015-05-06 16:07:48 -0700357/**
358 * Creates a CborValue iterator pointing to the first element of the container
359 * represented by \a it and saves it in \a recursed. The \a it container object
360 * needs to be kept and passed again to cbor_value_leave_container() in order
361 * to continue iterating past this container.
362 *
363 * \sa cbor_value_is_container(), cbor_value_leave_container(), cbor_value_advance()
364 */
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700365CborError cbor_value_enter_container(const CborValue *it, CborValue *recursed)
Thiago Macieira54a0e102015-05-05 21:25:06 -0700366{
Thiago Macieira2312efd2015-05-06 16:07:48 -0700367 assert(cbor_value_is_container(it));
Thiago Macieira54a0e102015-05-05 21:25:06 -0700368 *recursed = *it;
369 if (it->flags & CborIteratorFlag_UnknownLength) {
370 recursed->remaining = UINT32_MAX;
371 } else {
372 uint64_t len = _cbor_value_extract_int64_helper(it);
373 recursed->remaining = len;
374 if (recursed->remaining != len || len == UINT32_MAX)
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700375 return CborErrorDataTooLarge;
Thiago Macieira54a0e102015-05-05 21:25:06 -0700376 }
Thiago Macieirac70169f2015-05-06 07:49:44 -0700377 return advance_internal(recursed);
378}
379
Thiago Macieira2312efd2015-05-06 16:07:48 -0700380/**
381 * Updates \a it to point to the next element after the container. The \a
382 * recursed object needs to point to the last element of the container.
383 *
384 * \sa cbor_value_enter_container(), cbor_value_at_end()
385 */
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700386CborError cbor_value_leave_container(CborValue *it, const CborValue *recursed)
Thiago Macieirac70169f2015-05-06 07:49:44 -0700387{
Thiago Macieira2312efd2015-05-06 16:07:48 -0700388 assert(cbor_value_is_container(it));
Thiago Macieirac70169f2015-05-06 07:49:44 -0700389 assert(cbor_value_at_end(recursed));
390 it->ptr = recursed->ptr;
391 return advance_internal(it);
392}
393
Thiago Macieira2312efd2015-05-06 16:07:48 -0700394/**
395 * Calculates the length of the string in \a value and stores the result in \a
396 * len. This function is different from cbor_value_get_string_length() in that
397 * it calculates the length even for strings sent in chunks. For that reason,
398 * this function may not run in constant time (it will run in O(n) time on the
399 * number of chunks).
400 *
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700401 * \note On 32-bit platforms, this function will return error condition of \ref
402 * CborErrorDataTooLarge if the stream indicates a length that is too big to
403 * fit in 32-bit.
Thiago Macieira2312efd2015-05-06 16:07:48 -0700404 *
405 * \sa cbor_value_get_string_length(), cbor_value_copy_string(), cbor_value_is_length_known()
406 */
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700407CborError cbor_value_calculate_string_length(const CborValue *value, size_t *len)
Thiago Macieira2312efd2015-05-06 16:07:48 -0700408{
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700409 return cbor_value_copy_string(value, NULL, len, NULL);
Thiago Macieirac70169f2015-05-06 07:49:44 -0700410}
411
Thiago Macieira2312efd2015-05-06 16:07:48 -0700412/**
413 * Allocates memory for the string pointed by \a value and copies it into this
414 * buffer. The pointer to the buffer is stored in \a buffer and the number of
415 * bytes copied is stored in \a len (those variables must not be NULL).
416 *
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700417 * If \c malloc returns a NULL pointer, this function will return error
418 * condition \ref CborErrorOutOfMemory.
Thiago Macieira2312efd2015-05-06 16:07:48 -0700419 *
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700420 * On success, \c{*buffer} will contain a valid pointer that must be freed by
421 * calling \c{free()}. This is the case even for zero-length strings.
Thiago Macieira2312efd2015-05-06 16:07:48 -0700422 *
423 * The \a next pointer, if not null, will be updated to point to the next item
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700424 * after this string. If \a value points to the last item, then \a next will be
425 * invalid.
Thiago Macieira2312efd2015-05-06 16:07:48 -0700426 *
427 * \note This function does not perform UTF-8 validation on the incoming text
428 * string.
429 *
430 * \sa cbor_value_copy_string()
431 */
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700432CborError cbor_value_dup_string(const CborValue *value, char **buffer, size_t *buflen, CborValue *next)
Thiago Macieirac70169f2015-05-06 07:49:44 -0700433{
Thiago Macieira2312efd2015-05-06 16:07:48 -0700434 assert(buffer);
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700435 assert(buflen);
436 CborError err = cbor_value_calculate_string_length(value, buflen);
437 if (err)
438 return err;
Thiago Macieirac70169f2015-05-06 07:49:44 -0700439
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700440 ++*buflen;
441 *buffer = malloc(*buflen);
Thiago Macieira2312efd2015-05-06 16:07:48 -0700442 if (!*buffer) {
Thiago Macieirac70169f2015-05-06 07:49:44 -0700443 // out of memory
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700444 return CborErrorOutOfMemory;
Thiago Macieirac70169f2015-05-06 07:49:44 -0700445 }
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700446 err = cbor_value_copy_string(value, *buffer, buflen, next);
447 if (err) {
Thiago Macieira2312efd2015-05-06 16:07:48 -0700448 free(*buffer);
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700449 return err;
Thiago Macieirac70169f2015-05-06 07:49:44 -0700450 }
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700451 return CborNoError;
Thiago Macieira2312efd2015-05-06 16:07:48 -0700452}
453
454/**
455 * Copies the string pointed by \a value into the buffer provided at \a buffer
456 * of \a buflen bytes. If \a buffer is a NULL pointer, this function will not
457 * copy anything and will only update the \a next value.
458 *
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700459 * If the provided buffer length was too small, this function returns an error
460 * condition of \ref CborErrorOutOfMemory. If you need to calculate the length
461 * of the string in order to preallocate a buffer, use
Thiago Macieira2312efd2015-05-06 16:07:48 -0700462 * cbor_value_calculate_string_length().
463 *
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700464 * On success, this function sets the number of bytes copied to \c{*buflen}. If
465 * the buffer is large enough, this function will insert a null byte after the
466 * last copied byte, to facilitate manipulation of text strings. That byte is
467 * not included in the returned value of \c{*buflen}.
Thiago Macieira2312efd2015-05-06 16:07:48 -0700468 *
469 * The \a next pointer, if not null, will be updated to point to the next item
470 * after this string. If \a value points to the last item, then \a next will be
471 * invalid.
472 *
473 * \note This function does not perform UTF-8 validation on the incoming text
474 * string.
475 *
476 * \sa cbor_value_dup_string(), cbor_value_get_string_length(), cbor_value_calculate_string_length()
477 */
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700478CborError cbor_value_copy_string(const CborValue *value, char *buffer,
479 size_t *buflen, CborValue *next)
Thiago Macieira2312efd2015-05-06 16:07:48 -0700480{
481 assert(cbor_value_is_byte_string(value) || cbor_value_is_text_string(value));
482
483 size_t total;
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700484 CborError err;
Thiago Macieira2312efd2015-05-06 16:07:48 -0700485 const char *ptr = value->ptr;
486 if (cbor_value_is_length_known(value)) {
487 // easy case: fixed length
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700488 err = extract_length(value->parser, &ptr, &total);
489 if (err)
490 return err;
Thiago Macieira2312efd2015-05-06 16:07:48 -0700491 if (buffer) {
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700492 if (*buflen < total)
493 return CborErrorOutOfMemory;
Thiago Macieira2312efd2015-05-06 16:07:48 -0700494 memcpy(buffer, ptr, total);
495 ptr += total;
496 }
497 } else {
498 // chunked
499 ++ptr;
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700500 total = 0;
Thiago Macieira2312efd2015-05-06 16:07:48 -0700501 while (true) {
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700502 size_t chunkLen;
503 size_t newTotal;
504
Thiago Macieira2312efd2015-05-06 16:07:48 -0700505 if (ptr == value->parser->end)
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700506 return CborErrorUnexpectedEOF;
Thiago Macieira2312efd2015-05-06 16:07:48 -0700507
508 if (*ptr == (char)BreakByte) {
509 ++ptr;
510 break;
511 }
512
513 // is this the right type?
514 if ((*ptr & MajorTypeMask) != value->type)
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700515 return CborErrorIllegalType;
Thiago Macieira2312efd2015-05-06 16:07:48 -0700516
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700517 err = extract_length(value->parser, &ptr, &chunkLen);
518 if (err)
519 return err;
Thiago Macieira2312efd2015-05-06 16:07:48 -0700520
Thiago Macieira2312efd2015-05-06 16:07:48 -0700521 if (unlikely(!add_check_overflow(total, chunkLen, &newTotal)))
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700522 return CborErrorDataTooLarge;
Thiago Macieira2312efd2015-05-06 16:07:48 -0700523
524 if (buffer) {
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700525 if (*buflen < newTotal)
526 return CborErrorOutOfMemory;
Thiago Macieira2312efd2015-05-06 16:07:48 -0700527 memcpy(buffer + total, ptr, chunkLen);
528 }
529 ptr += chunkLen;
530 total = newTotal;
531 }
532 }
533
534 // is there enough room for the ending NUL byte?
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700535 if (buffer && *buflen > total)
Thiago Macieira2312efd2015-05-06 16:07:48 -0700536 buffer[total] = '\0';
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700537 *buflen = total;
Thiago Macieira2312efd2015-05-06 16:07:48 -0700538
539 if (next) {
540 *next = *value;
541 next->ptr = ptr;
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700542 err = preparse_next_value(next);
543 if (err)
544 return err;
Thiago Macieira2312efd2015-05-06 16:07:48 -0700545 }
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700546 return CborNoError;
Thiago Macieirac70169f2015-05-06 07:49:44 -0700547}
548
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700549CborError cbor_value_get_half_float(const CborValue *value, void *result)
Thiago Macieirac70169f2015-05-06 07:49:44 -0700550{
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700551 assert(value->type == CborHalfFloatType);
Thiago Macieirac70169f2015-05-06 07:49:44 -0700552
553 // size has been computed already
554 uint16_t v = get16(value->ptr + 1);
555 memcpy(result, &v, sizeof(v));
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700556 return CborNoError;
Thiago Macieira54a0e102015-05-05 21:25:06 -0700557}