blob: a3883baee2e11c883f3000d7f602d2ffba9598f8 [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;
133
Thiago Macieira54a0e102015-05-05 21:25:06 -0700134 // are we at the end?
Thiago Macieira54a0e102015-05-05 21:25:06 -0700135 if (it->ptr == parser->end)
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700136 return CborErrorUnexpectedEOF;
Thiago Macieira54a0e102015-05-05 21:25:06 -0700137
138 uint8_t descriptor = *it->ptr;
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700139 uint8_t type = descriptor & MajorTypeMask;
Thiago Macieira54a0e102015-05-05 21:25:06 -0700140 it->flags = 0;
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700141 it->type = CborInvalidType;
142 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 Macieira54a0e102015-05-05 21:25:06 -0700223 it->type = CborInvalidType;
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700224 if (it->remaining != UINT32_MAX) {
225 if (!--it->remaining) {
226 return it->ptr == it->parser->end ? CborNoError : CborErrorGarbageAtEnd;
227 }
228 }
229 return preparse_value(it);
230}
231
232static CborError advance_internal(CborValue *it)
233{
234 uint64_t length;
235 CborError err = extract_number(it->parser, &it->ptr, &length);
236 assert(err == CborNoError);
237
238 if (!is_fixed_type(it->type)) {
239 assert(length == (size_t)length);
240 it->ptr += length;
241 }
242
243 if (it->remaining == UINT32_MAX && *it->ptr == (char)BreakByte) {
244 // end of map or array
245 it->remaining = 0;
246 return CborNoError;
247 }
248
249 return preparse_next_value(it);
Thiago Macieira54a0e102015-05-05 21:25:06 -0700250}
251
Thiago Macieira2312efd2015-05-06 16:07:48 -0700252/** \internal
253 *
254 * Decodes the CBOR integer value when it is larger than the 16 bits available
255 * in value->extra. This function requires that value->flags have the
256 * CborIteratorFlag_IntegerValueTooLarge flag set.
257 *
258 * This function is also used to extract single- and double-precision floating
259 * point values (SinglePrecisionFloat == Value32Bit and DoublePrecisionFloat ==
260 * Value64Bit).
261 */
Thiago Macieira54a0e102015-05-05 21:25:06 -0700262uint64_t _cbor_value_decode_int64_internal(const CborValue *value)
263{
Thiago Macieira2312efd2015-05-06 16:07:48 -0700264 assert(value->flags & CborIteratorFlag_IntegerValueTooLarge ||
265 value->type == CborFloatType || value->type == CborDoubleType);
Thiago Macieira54a0e102015-05-05 21:25:06 -0700266 if ((*value->ptr & SmallValueMask) == Value32Bit)
267 return get32(value->ptr + 1);
268
269 assert((*value->ptr & SmallValueMask) == Value64Bit);
270 return get64(value->ptr + 1);
271}
272
273/**
274 * Initializes the CBOR parser for parsing \a size bytes beginning at \a
275 * buffer. Parsing will use flags set in \a flags. The iterator to the first
276 * element is returned in \a it.
Thiago Macieira2312efd2015-05-06 16:07:48 -0700277 *
278 * The \a parser structure needs to remain valid throughout the decoding
279 * process. It is not thread-safe to share one CborParser among multiple
280 * threads iterating at the same time, but the object can be copied so multiple
281 * threads can iterate.
282 *
283 * ### Write how to determine the end pointer
284 * ### Write how to do limited-buffer windowed decoding
Thiago Macieira54a0e102015-05-05 21:25:06 -0700285 */
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700286CborError cbor_parser_init(const char *buffer, size_t size, int flags, CborParser *parser, CborValue *it)
Thiago Macieira54a0e102015-05-05 21:25:06 -0700287{
288 memset(parser, 0, sizeof(*parser));
289 parser->end = buffer + size;
Thiago Macieira54a0e102015-05-05 21:25:06 -0700290 parser->flags = flags;
291 it->parser = parser;
292 it->ptr = buffer;
293 it->remaining = 1; // there's one type altogether, usually an array or map
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700294 return preparse_value(it);
Thiago Macieira2312efd2015-05-06 16:07:48 -0700295}
296
297/**
298 * Advances the CBOR value \a it by one fixed-size position. Fixed-size types
299 * are: integers, tags, simple types (including boolean, null and undefined
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700300 * values) and floating point types.
Thiago Macieira2312efd2015-05-06 16:07:48 -0700301 *
302 * \sa cbor_value_at_end(), cbor_value_advance(), cbor_value_begin_recurse(), cbor_value_end_recurse()
303 */
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700304CborError cbor_value_advance_fixed(CborValue *it)
Thiago Macieira54a0e102015-05-05 21:25:06 -0700305{
Thiago Macieira2312efd2015-05-06 16:07:48 -0700306 assert(it->type != CborInvalidType);
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700307 assert(is_fixed_type(it->type));
308 if (!it->remaining)
309 return CborErrorAdvancePastEOF;
310 return advance_internal(it);
Thiago Macieira54a0e102015-05-05 21:25:06 -0700311}
312
Thiago Macieira2312efd2015-05-06 16:07:48 -0700313/**
314 * Advances the CBOR value \a it by one element, skipping over containers.
315 * Unlike cbor_value_advance_fixed(), this function can be called on a CBOR
316 * value of any type. However, if the type is a container (map or array) or a
317 * string with a chunked payload, this function will not run in constant time
318 * and will recurse into itself (it will run on O(n) time for the number of
319 * elements or chunks and will use O(n) memory for the number of nested
320 * containers).
321 *
Thiago Macieira2312efd2015-05-06 16:07:48 -0700322 * \sa cbor_value_at_end(), cbor_value_advance_fixed(), cbor_value_begin_recurse(), cbor_value_end_recurse()
323 */
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700324CborError cbor_value_advance(CborValue *it)
Thiago Macieira2312efd2015-05-06 16:07:48 -0700325{
326 assert(it->type != CborInvalidType);
327 if (!it->remaining)
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700328 return CborErrorAdvancePastEOF;
Thiago Macieira2312efd2015-05-06 16:07:48 -0700329 if (is_fixed_type(it->type))
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700330 return advance_internal(it);
Thiago Macieira2312efd2015-05-06 16:07:48 -0700331
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700332 if (!cbor_value_is_container(it))
333 return cbor_value_copy_string(it, NULL, 0, it);
334
335 // map or array
336 CborError err;
337 CborValue recursed;
338 err = cbor_value_enter_container(it, &recursed);
339 if (err)
340 return err;
341 while (!cbor_value_at_end(&recursed)) {
342 err = cbor_value_advance(&recursed);
343 if (err)
344 return err;
Thiago Macieira2312efd2015-05-06 16:07:48 -0700345 }
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700346 return cbor_value_leave_container(it, &recursed);
Thiago Macieira2312efd2015-05-06 16:07:48 -0700347}
348
349/**
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700350 * \fn bool cbor_value_is_container(const CborValue *it)
351 *
Thiago Macieira2312efd2015-05-06 16:07:48 -0700352 * Returns true if the \a it value is a container and requires recursion in
353 * order to decode (maps and arrays), false otherwise.
354 */
Thiago Macieira54a0e102015-05-05 21:25:06 -0700355
Thiago Macieira2312efd2015-05-06 16:07:48 -0700356/**
357 * Creates a CborValue iterator pointing to the first element of the container
358 * represented by \a it and saves it in \a recursed. The \a it container object
359 * needs to be kept and passed again to cbor_value_leave_container() in order
360 * to continue iterating past this container.
361 *
362 * \sa cbor_value_is_container(), cbor_value_leave_container(), cbor_value_advance()
363 */
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700364CborError cbor_value_enter_container(const CborValue *it, CborValue *recursed)
Thiago Macieira54a0e102015-05-05 21:25:06 -0700365{
Thiago Macieira2312efd2015-05-06 16:07:48 -0700366 assert(cbor_value_is_container(it));
Thiago Macieira54a0e102015-05-05 21:25:06 -0700367 *recursed = *it;
368 if (it->flags & CborIteratorFlag_UnknownLength) {
369 recursed->remaining = UINT32_MAX;
370 } else {
371 uint64_t len = _cbor_value_extract_int64_helper(it);
372 recursed->remaining = len;
373 if (recursed->remaining != len || len == UINT32_MAX)
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700374 return CborErrorDataTooLarge;
Thiago Macieira54a0e102015-05-05 21:25:06 -0700375 }
Thiago Macieirac70169f2015-05-06 07:49:44 -0700376 return advance_internal(recursed);
377}
378
Thiago Macieira2312efd2015-05-06 16:07:48 -0700379/**
380 * Updates \a it to point to the next element after the container. The \a
381 * recursed object needs to point to the last element of the container.
382 *
383 * \sa cbor_value_enter_container(), cbor_value_at_end()
384 */
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700385CborError cbor_value_leave_container(CborValue *it, const CborValue *recursed)
Thiago Macieirac70169f2015-05-06 07:49:44 -0700386{
Thiago Macieira2312efd2015-05-06 16:07:48 -0700387 assert(cbor_value_is_container(it));
Thiago Macieirac70169f2015-05-06 07:49:44 -0700388 assert(cbor_value_at_end(recursed));
389 it->ptr = recursed->ptr;
390 return advance_internal(it);
391}
392
Thiago Macieira2312efd2015-05-06 16:07:48 -0700393/**
394 * Calculates the length of the string in \a value and stores the result in \a
395 * len. This function is different from cbor_value_get_string_length() in that
396 * it calculates the length even for strings sent in chunks. For that reason,
397 * this function may not run in constant time (it will run in O(n) time on the
398 * number of chunks).
399 *
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700400 * \note On 32-bit platforms, this function will return error condition of \ref
401 * CborErrorDataTooLarge if the stream indicates a length that is too big to
402 * fit in 32-bit.
Thiago Macieira2312efd2015-05-06 16:07:48 -0700403 *
404 * \sa cbor_value_get_string_length(), cbor_value_copy_string(), cbor_value_is_length_known()
405 */
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700406CborError cbor_value_calculate_string_length(const CborValue *value, size_t *len)
Thiago Macieira2312efd2015-05-06 16:07:48 -0700407{
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700408 return cbor_value_copy_string(value, NULL, len, NULL);
Thiago Macieirac70169f2015-05-06 07:49:44 -0700409}
410
Thiago Macieira2312efd2015-05-06 16:07:48 -0700411/**
412 * Allocates memory for the string pointed by \a value and copies it into this
413 * buffer. The pointer to the buffer is stored in \a buffer and the number of
414 * bytes copied is stored in \a len (those variables must not be NULL).
415 *
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700416 * If \c malloc returns a NULL pointer, this function will return error
417 * condition \ref CborErrorOutOfMemory.
Thiago Macieira2312efd2015-05-06 16:07:48 -0700418 *
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700419 * On success, \c{*buffer} will contain a valid pointer that must be freed by
420 * calling \c{free()}. This is the case even for zero-length strings.
Thiago Macieira2312efd2015-05-06 16:07:48 -0700421 *
422 * The \a next pointer, if not null, will be updated to point to the next item
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700423 * after this string. If \a value points to the last item, then \a next will be
424 * invalid.
Thiago Macieira2312efd2015-05-06 16:07:48 -0700425 *
426 * \note This function does not perform UTF-8 validation on the incoming text
427 * string.
428 *
429 * \sa cbor_value_copy_string()
430 */
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700431CborError cbor_value_dup_string(const CborValue *value, char **buffer, size_t *buflen, CborValue *next)
Thiago Macieirac70169f2015-05-06 07:49:44 -0700432{
Thiago Macieira2312efd2015-05-06 16:07:48 -0700433 assert(buffer);
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700434 assert(buflen);
435 CborError err = cbor_value_calculate_string_length(value, buflen);
436 if (err)
437 return err;
Thiago Macieirac70169f2015-05-06 07:49:44 -0700438
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700439 ++*buflen;
440 *buffer = malloc(*buflen);
Thiago Macieira2312efd2015-05-06 16:07:48 -0700441 if (!*buffer) {
Thiago Macieirac70169f2015-05-06 07:49:44 -0700442 // out of memory
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700443 return CborErrorOutOfMemory;
Thiago Macieirac70169f2015-05-06 07:49:44 -0700444 }
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700445 err = cbor_value_copy_string(value, *buffer, buflen, next);
446 if (err) {
Thiago Macieira2312efd2015-05-06 16:07:48 -0700447 free(*buffer);
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700448 return err;
Thiago Macieirac70169f2015-05-06 07:49:44 -0700449 }
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700450 return CborNoError;
Thiago Macieira2312efd2015-05-06 16:07:48 -0700451}
452
453/**
454 * Copies the string pointed by \a value into the buffer provided at \a buffer
455 * of \a buflen bytes. If \a buffer is a NULL pointer, this function will not
456 * copy anything and will only update the \a next value.
457 *
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700458 * If the provided buffer length was too small, this function returns an error
459 * condition of \ref CborErrorOutOfMemory. If you need to calculate the length
460 * of the string in order to preallocate a buffer, use
Thiago Macieira2312efd2015-05-06 16:07:48 -0700461 * cbor_value_calculate_string_length().
462 *
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700463 * On success, this function sets the number of bytes copied to \c{*buflen}. If
464 * the buffer is large enough, this function will insert a null byte after the
465 * last copied byte, to facilitate manipulation of text strings. That byte is
466 * not included in the returned value of \c{*buflen}.
Thiago Macieira2312efd2015-05-06 16:07:48 -0700467 *
468 * The \a next pointer, if not null, will be updated to point to the next item
469 * after this string. If \a value points to the last item, then \a next will be
470 * invalid.
471 *
472 * \note This function does not perform UTF-8 validation on the incoming text
473 * string.
474 *
475 * \sa cbor_value_dup_string(), cbor_value_get_string_length(), cbor_value_calculate_string_length()
476 */
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700477CborError cbor_value_copy_string(const CborValue *value, char *buffer,
478 size_t *buflen, CborValue *next)
Thiago Macieira2312efd2015-05-06 16:07:48 -0700479{
480 assert(cbor_value_is_byte_string(value) || cbor_value_is_text_string(value));
481
482 size_t total;
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700483 CborError err;
Thiago Macieira2312efd2015-05-06 16:07:48 -0700484 const char *ptr = value->ptr;
485 if (cbor_value_is_length_known(value)) {
486 // easy case: fixed length
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700487 err = extract_length(value->parser, &ptr, &total);
488 if (err)
489 return err;
Thiago Macieira2312efd2015-05-06 16:07:48 -0700490 if (buffer) {
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700491 if (*buflen < total)
492 return CborErrorOutOfMemory;
Thiago Macieira2312efd2015-05-06 16:07:48 -0700493 memcpy(buffer, ptr, total);
494 ptr += total;
495 }
496 } else {
497 // chunked
498 ++ptr;
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700499 total = 0;
Thiago Macieira2312efd2015-05-06 16:07:48 -0700500 while (true) {
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700501 size_t chunkLen;
502 size_t newTotal;
503
Thiago Macieira2312efd2015-05-06 16:07:48 -0700504 if (ptr == value->parser->end)
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700505 return CborErrorUnexpectedEOF;
Thiago Macieira2312efd2015-05-06 16:07:48 -0700506
507 if (*ptr == (char)BreakByte) {
508 ++ptr;
509 break;
510 }
511
512 // is this the right type?
513 if ((*ptr & MajorTypeMask) != value->type)
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700514 return CborErrorIllegalType;
Thiago Macieira2312efd2015-05-06 16:07:48 -0700515
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700516 err = extract_length(value->parser, &ptr, &chunkLen);
517 if (err)
518 return err;
Thiago Macieira2312efd2015-05-06 16:07:48 -0700519
Thiago Macieira2312efd2015-05-06 16:07:48 -0700520 if (unlikely(!add_check_overflow(total, chunkLen, &newTotal)))
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700521 return CborErrorDataTooLarge;
Thiago Macieira2312efd2015-05-06 16:07:48 -0700522
523 if (buffer) {
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700524 if (*buflen < newTotal)
525 return CborErrorOutOfMemory;
Thiago Macieira2312efd2015-05-06 16:07:48 -0700526 memcpy(buffer + total, ptr, chunkLen);
527 }
528 ptr += chunkLen;
529 total = newTotal;
530 }
531 }
532
533 // is there enough room for the ending NUL byte?
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700534 if (buffer && *buflen > total)
Thiago Macieira2312efd2015-05-06 16:07:48 -0700535 buffer[total] = '\0';
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700536 *buflen = total;
Thiago Macieira2312efd2015-05-06 16:07:48 -0700537
538 if (next) {
539 *next = *value;
540 next->ptr = ptr;
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700541 err = preparse_next_value(next);
542 if (err)
543 return err;
Thiago Macieira2312efd2015-05-06 16:07:48 -0700544 }
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700545 return CborNoError;
Thiago Macieirac70169f2015-05-06 07:49:44 -0700546}
547
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700548CborError cbor_value_get_half_float(const CborValue *value, void *result)
Thiago Macieirac70169f2015-05-06 07:49:44 -0700549{
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700550 assert(value->type == CborHalfFloatType);
Thiago Macieirac70169f2015-05-06 07:49:44 -0700551
552 // size has been computed already
553 uint16_t v = get16(value->ptr + 1);
554 memcpy(result, &v, sizeof(v));
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700555 return CborNoError;
Thiago Macieira54a0e102015-05-05 21:25:06 -0700556}