blob: 682569878cacbb83bb854b3cfef07bf768db59fe [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;
172 // fall through
173 case TrueValue:
174 case NullValue:
175 case UndefinedValue:
176 case HalfPrecisionFloat:
177 case SinglePrecisionFloat:
178 case DoublePrecisionFloat:
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700179 type = *it->ptr;
Thiago Macieira54a0e102015-05-05 21:25:06 -0700180 break;
181
182 case SimpleTypeInNextByte:
Thiago Macieira54a0e102015-05-05 21:25:06 -0700183 it->extra = it->ptr[1];
184#ifndef CBOR_PARSER_NO_STRICT_CHECKS
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700185 if (it->extra < 32)
186 return CborErrorIllegalSimpleType;
Thiago Macieira54a0e102015-05-05 21:25:06 -0700187#endif
188 case 28:
189 case 29:
190 case 30:
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700191 return CborErrorUnknownType;
Thiago Macieira54a0e102015-05-05 21:25:06 -0700192
193 case Break:
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700194 return CborErrorUnexpectedBreak;
Thiago Macieira54a0e102015-05-05 21:25:06 -0700195 }
196 break;
Thiago Macieira54a0e102015-05-05 21:25:06 -0700197 }
198
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700199 if (unlikely(descriptor > Value64Bit))
200 return CborErrorIllegalNumber;
201
202 // no further errors possible
203 it->type = type;
204
Thiago Macieira54a0e102015-05-05 21:25:06 -0700205 // try to decode up to 16 bits
206 if (descriptor < Value8Bit)
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700207 return CborNoError;
Thiago Macieira54a0e102015-05-05 21:25:06 -0700208
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700209 if (descriptor == Value8Bit)
210 it->extra = (uint8_t)it->ptr[1];
211 else if (descriptor == Value16Bit)
Thiago Macieira54a0e102015-05-05 21:25:06 -0700212 it->extra = get16(it->ptr + 1);
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700213 else
214 it->flags |= CborIteratorFlag_IntegerValueTooLarge; // Value32Bit or Value64Bit
215 return CborNoError;
216}
Thiago Macieira54a0e102015-05-05 21:25:06 -0700217
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700218static CborError preparse_next_value(CborValue *it)
219{
Thiago Macieira54a0e102015-05-05 21:25:06 -0700220 it->type = CborInvalidType;
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700221 if (it->remaining != UINT32_MAX) {
222 if (!--it->remaining) {
223 return it->ptr == it->parser->end ? CborNoError : CborErrorGarbageAtEnd;
224 }
225 }
226 return preparse_value(it);
227}
228
229static CborError advance_internal(CborValue *it)
230{
231 uint64_t length;
232 CborError err = extract_number(it->parser, &it->ptr, &length);
233 assert(err == CborNoError);
234
235 if (!is_fixed_type(it->type)) {
236 assert(length == (size_t)length);
237 it->ptr += length;
238 }
239
240 if (it->remaining == UINT32_MAX && *it->ptr == (char)BreakByte) {
241 // end of map or array
242 it->remaining = 0;
243 return CborNoError;
244 }
245
246 return preparse_next_value(it);
Thiago Macieira54a0e102015-05-05 21:25:06 -0700247}
248
Thiago Macieira2312efd2015-05-06 16:07:48 -0700249/** \internal
250 *
251 * Decodes the CBOR integer value when it is larger than the 16 bits available
252 * in value->extra. This function requires that value->flags have the
253 * CborIteratorFlag_IntegerValueTooLarge flag set.
254 *
255 * This function is also used to extract single- and double-precision floating
256 * point values (SinglePrecisionFloat == Value32Bit and DoublePrecisionFloat ==
257 * Value64Bit).
258 */
Thiago Macieira54a0e102015-05-05 21:25:06 -0700259uint64_t _cbor_value_decode_int64_internal(const CborValue *value)
260{
Thiago Macieira2312efd2015-05-06 16:07:48 -0700261 assert(value->flags & CborIteratorFlag_IntegerValueTooLarge ||
262 value->type == CborFloatType || value->type == CborDoubleType);
Thiago Macieira54a0e102015-05-05 21:25:06 -0700263 if ((*value->ptr & SmallValueMask) == Value32Bit)
264 return get32(value->ptr + 1);
265
266 assert((*value->ptr & SmallValueMask) == Value64Bit);
267 return get64(value->ptr + 1);
268}
269
270/**
271 * Initializes the CBOR parser for parsing \a size bytes beginning at \a
272 * buffer. Parsing will use flags set in \a flags. The iterator to the first
273 * element is returned in \a it.
Thiago Macieira2312efd2015-05-06 16:07:48 -0700274 *
275 * The \a parser structure needs to remain valid throughout the decoding
276 * process. It is not thread-safe to share one CborParser among multiple
277 * threads iterating at the same time, but the object can be copied so multiple
278 * threads can iterate.
279 *
280 * ### Write how to determine the end pointer
281 * ### Write how to do limited-buffer windowed decoding
Thiago Macieira54a0e102015-05-05 21:25:06 -0700282 */
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700283CborError cbor_parser_init(const char *buffer, size_t size, int flags, CborParser *parser, CborValue *it)
Thiago Macieira54a0e102015-05-05 21:25:06 -0700284{
285 memset(parser, 0, sizeof(*parser));
286 parser->end = buffer + size;
Thiago Macieira54a0e102015-05-05 21:25:06 -0700287 parser->flags = flags;
288 it->parser = parser;
289 it->ptr = buffer;
290 it->remaining = 1; // there's one type altogether, usually an array or map
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700291 return preparse_value(it);
Thiago Macieira2312efd2015-05-06 16:07:48 -0700292}
293
294/**
295 * Advances the CBOR value \a it by one fixed-size position. Fixed-size types
296 * are: integers, tags, simple types (including boolean, null and undefined
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700297 * values) and floating point types.
Thiago Macieira2312efd2015-05-06 16:07:48 -0700298 *
299 * \sa cbor_value_at_end(), cbor_value_advance(), cbor_value_begin_recurse(), cbor_value_end_recurse()
300 */
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700301CborError cbor_value_advance_fixed(CborValue *it)
Thiago Macieira54a0e102015-05-05 21:25:06 -0700302{
Thiago Macieira2312efd2015-05-06 16:07:48 -0700303 assert(it->type != CborInvalidType);
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700304 assert(is_fixed_type(it->type));
305 if (!it->remaining)
306 return CborErrorAdvancePastEOF;
307 return advance_internal(it);
Thiago Macieira54a0e102015-05-05 21:25:06 -0700308}
309
Thiago Macieira2312efd2015-05-06 16:07:48 -0700310/**
311 * Advances the CBOR value \a it by one element, skipping over containers.
312 * Unlike cbor_value_advance_fixed(), this function can be called on a CBOR
313 * value of any type. However, if the type is a container (map or array) or a
314 * string with a chunked payload, this function will not run in constant time
315 * and will recurse into itself (it will run on O(n) time for the number of
316 * elements or chunks and will use O(n) memory for the number of nested
317 * containers).
318 *
Thiago Macieira2312efd2015-05-06 16:07:48 -0700319 * \sa cbor_value_at_end(), cbor_value_advance_fixed(), cbor_value_begin_recurse(), cbor_value_end_recurse()
320 */
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700321CborError cbor_value_advance(CborValue *it)
Thiago Macieira2312efd2015-05-06 16:07:48 -0700322{
323 assert(it->type != CborInvalidType);
324 if (!it->remaining)
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700325 return CborErrorAdvancePastEOF;
Thiago Macieira2312efd2015-05-06 16:07:48 -0700326 if (is_fixed_type(it->type))
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700327 return advance_internal(it);
Thiago Macieira2312efd2015-05-06 16:07:48 -0700328
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700329 if (!cbor_value_is_container(it))
330 return cbor_value_copy_string(it, NULL, 0, it);
331
332 // map or array
333 CborError err;
334 CborValue recursed;
335 err = cbor_value_enter_container(it, &recursed);
336 if (err)
337 return err;
338 while (!cbor_value_at_end(&recursed)) {
339 err = cbor_value_advance(&recursed);
340 if (err)
341 return err;
Thiago Macieira2312efd2015-05-06 16:07:48 -0700342 }
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700343 return cbor_value_leave_container(it, &recursed);
Thiago Macieira2312efd2015-05-06 16:07:48 -0700344}
345
346/**
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700347 * \fn bool cbor_value_is_container(const CborValue *it)
348 *
Thiago Macieira2312efd2015-05-06 16:07:48 -0700349 * Returns true if the \a it value is a container and requires recursion in
350 * order to decode (maps and arrays), false otherwise.
351 */
Thiago Macieira54a0e102015-05-05 21:25:06 -0700352
Thiago Macieira2312efd2015-05-06 16:07:48 -0700353/**
354 * Creates a CborValue iterator pointing to the first element of the container
355 * represented by \a it and saves it in \a recursed. The \a it container object
356 * needs to be kept and passed again to cbor_value_leave_container() in order
357 * to continue iterating past this container.
358 *
359 * \sa cbor_value_is_container(), cbor_value_leave_container(), cbor_value_advance()
360 */
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700361CborError cbor_value_enter_container(const CborValue *it, CborValue *recursed)
Thiago Macieira54a0e102015-05-05 21:25:06 -0700362{
Thiago Macieira2312efd2015-05-06 16:07:48 -0700363 assert(cbor_value_is_container(it));
Thiago Macieira54a0e102015-05-05 21:25:06 -0700364 *recursed = *it;
365 if (it->flags & CborIteratorFlag_UnknownLength) {
366 recursed->remaining = UINT32_MAX;
367 } else {
368 uint64_t len = _cbor_value_extract_int64_helper(it);
369 recursed->remaining = len;
370 if (recursed->remaining != len || len == UINT32_MAX)
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700371 return CborErrorDataTooLarge;
Thiago Macieira54a0e102015-05-05 21:25:06 -0700372 }
Thiago Macieirac70169f2015-05-06 07:49:44 -0700373 return advance_internal(recursed);
374}
375
Thiago Macieira2312efd2015-05-06 16:07:48 -0700376/**
377 * Updates \a it to point to the next element after the container. The \a
378 * recursed object needs to point to the last element of the container.
379 *
380 * \sa cbor_value_enter_container(), cbor_value_at_end()
381 */
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700382CborError cbor_value_leave_container(CborValue *it, const CborValue *recursed)
Thiago Macieirac70169f2015-05-06 07:49:44 -0700383{
Thiago Macieira2312efd2015-05-06 16:07:48 -0700384 assert(cbor_value_is_container(it));
Thiago Macieirac70169f2015-05-06 07:49:44 -0700385 assert(cbor_value_at_end(recursed));
386 it->ptr = recursed->ptr;
387 return advance_internal(it);
388}
389
Thiago Macieira2312efd2015-05-06 16:07:48 -0700390/**
391 * Calculates the length of the string in \a value and stores the result in \a
392 * len. This function is different from cbor_value_get_string_length() in that
393 * it calculates the length even for strings sent in chunks. For that reason,
394 * this function may not run in constant time (it will run in O(n) time on the
395 * number of chunks).
396 *
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700397 * \note On 32-bit platforms, this function will return error condition of \ref
398 * CborErrorDataTooLarge if the stream indicates a length that is too big to
399 * fit in 32-bit.
Thiago Macieira2312efd2015-05-06 16:07:48 -0700400 *
401 * \sa cbor_value_get_string_length(), cbor_value_copy_string(), cbor_value_is_length_known()
402 */
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700403CborError cbor_value_calculate_string_length(const CborValue *value, size_t *len)
Thiago Macieira2312efd2015-05-06 16:07:48 -0700404{
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700405 return cbor_value_copy_string(value, NULL, len, NULL);
Thiago Macieirac70169f2015-05-06 07:49:44 -0700406}
407
Thiago Macieira2312efd2015-05-06 16:07:48 -0700408/**
409 * Allocates memory for the string pointed by \a value and copies it into this
410 * buffer. The pointer to the buffer is stored in \a buffer and the number of
411 * bytes copied is stored in \a len (those variables must not be NULL).
412 *
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700413 * If \c malloc returns a NULL pointer, this function will return error
414 * condition \ref CborErrorOutOfMemory.
Thiago Macieira2312efd2015-05-06 16:07:48 -0700415 *
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700416 * On success, \c{*buffer} will contain a valid pointer that must be freed by
417 * calling \c{free()}. This is the case even for zero-length strings.
Thiago Macieira2312efd2015-05-06 16:07:48 -0700418 *
419 * The \a next pointer, if not null, will be updated to point to the next item
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700420 * after this string. If \a value points to the last item, then \a next will be
421 * invalid.
Thiago Macieira2312efd2015-05-06 16:07:48 -0700422 *
423 * \note This function does not perform UTF-8 validation on the incoming text
424 * string.
425 *
426 * \sa cbor_value_copy_string()
427 */
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700428CborError cbor_value_dup_string(const CborValue *value, char **buffer, size_t *buflen, CborValue *next)
Thiago Macieirac70169f2015-05-06 07:49:44 -0700429{
Thiago Macieira2312efd2015-05-06 16:07:48 -0700430 assert(buffer);
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700431 assert(buflen);
432 CborError err = cbor_value_calculate_string_length(value, buflen);
433 if (err)
434 return err;
Thiago Macieirac70169f2015-05-06 07:49:44 -0700435
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700436 ++*buflen;
437 *buffer = malloc(*buflen);
Thiago Macieira2312efd2015-05-06 16:07:48 -0700438 if (!*buffer) {
Thiago Macieirac70169f2015-05-06 07:49:44 -0700439 // out of memory
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700440 return CborErrorOutOfMemory;
Thiago Macieirac70169f2015-05-06 07:49:44 -0700441 }
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700442 err = cbor_value_copy_string(value, *buffer, buflen, next);
443 if (err) {
Thiago Macieira2312efd2015-05-06 16:07:48 -0700444 free(*buffer);
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700445 return err;
Thiago Macieirac70169f2015-05-06 07:49:44 -0700446 }
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700447 return CborNoError;
Thiago Macieira2312efd2015-05-06 16:07:48 -0700448}
449
450/**
451 * Copies the string pointed by \a value into the buffer provided at \a buffer
452 * of \a buflen bytes. If \a buffer is a NULL pointer, this function will not
453 * copy anything and will only update the \a next value.
454 *
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700455 * If the provided buffer length was too small, this function returns an error
456 * condition of \ref CborErrorOutOfMemory. If you need to calculate the length
457 * of the string in order to preallocate a buffer, use
Thiago Macieira2312efd2015-05-06 16:07:48 -0700458 * cbor_value_calculate_string_length().
459 *
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700460 * On success, this function sets the number of bytes copied to \c{*buflen}. If
461 * the buffer is large enough, this function will insert a null byte after the
462 * last copied byte, to facilitate manipulation of text strings. That byte is
463 * not included in the returned value of \c{*buflen}.
Thiago Macieira2312efd2015-05-06 16:07:48 -0700464 *
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 * \note This function does not perform UTF-8 validation on the incoming text
470 * string.
471 *
472 * \sa cbor_value_dup_string(), cbor_value_get_string_length(), cbor_value_calculate_string_length()
473 */
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700474CborError cbor_value_copy_string(const CborValue *value, char *buffer,
475 size_t *buflen, CborValue *next)
Thiago Macieira2312efd2015-05-06 16:07:48 -0700476{
477 assert(cbor_value_is_byte_string(value) || cbor_value_is_text_string(value));
478
479 size_t total;
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700480 CborError err;
Thiago Macieira2312efd2015-05-06 16:07:48 -0700481 const char *ptr = value->ptr;
482 if (cbor_value_is_length_known(value)) {
483 // easy case: fixed length
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700484 err = extract_length(value->parser, &ptr, &total);
485 if (err)
486 return err;
Thiago Macieira2312efd2015-05-06 16:07:48 -0700487 if (buffer) {
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700488 if (*buflen < total)
489 return CborErrorOutOfMemory;
Thiago Macieira2312efd2015-05-06 16:07:48 -0700490 memcpy(buffer, ptr, total);
491 ptr += total;
492 }
493 } else {
494 // chunked
495 ++ptr;
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700496 total = 0;
Thiago Macieira2312efd2015-05-06 16:07:48 -0700497 while (true) {
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700498 size_t chunkLen;
499 size_t newTotal;
500
Thiago Macieira2312efd2015-05-06 16:07:48 -0700501 if (ptr == value->parser->end)
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700502 return CborErrorUnexpectedEOF;
Thiago Macieira2312efd2015-05-06 16:07:48 -0700503
504 if (*ptr == (char)BreakByte) {
505 ++ptr;
506 break;
507 }
508
509 // is this the right type?
510 if ((*ptr & MajorTypeMask) != value->type)
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700511 return CborErrorIllegalType;
Thiago Macieira2312efd2015-05-06 16:07:48 -0700512
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700513 err = extract_length(value->parser, &ptr, &chunkLen);
514 if (err)
515 return err;
Thiago Macieira2312efd2015-05-06 16:07:48 -0700516
Thiago Macieira2312efd2015-05-06 16:07:48 -0700517 if (unlikely(!add_check_overflow(total, chunkLen, &newTotal)))
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700518 return CborErrorDataTooLarge;
Thiago Macieira2312efd2015-05-06 16:07:48 -0700519
520 if (buffer) {
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700521 if (*buflen < newTotal)
522 return CborErrorOutOfMemory;
Thiago Macieira2312efd2015-05-06 16:07:48 -0700523 memcpy(buffer + total, ptr, chunkLen);
524 }
525 ptr += chunkLen;
526 total = newTotal;
527 }
528 }
529
530 // is there enough room for the ending NUL byte?
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700531 if (buffer && *buflen > total)
Thiago Macieira2312efd2015-05-06 16:07:48 -0700532 buffer[total] = '\0';
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700533 *buflen = total;
Thiago Macieira2312efd2015-05-06 16:07:48 -0700534
535 if (next) {
536 *next = *value;
537 next->ptr = ptr;
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700538 err = preparse_next_value(next);
539 if (err)
540 return err;
Thiago Macieira2312efd2015-05-06 16:07:48 -0700541 }
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700542 return CborNoError;
Thiago Macieirac70169f2015-05-06 07:49:44 -0700543}
544
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700545CborError cbor_value_get_half_float(const CborValue *value, void *result)
Thiago Macieirac70169f2015-05-06 07:49:44 -0700546{
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700547 assert(value->type == CborHalfFloatType);
Thiago Macieirac70169f2015-05-06 07:49:44 -0700548
549 // size has been computed already
550 uint16_t v = get16(value->ptr + 1);
551 memcpy(result, &v, sizeof(v));
Thiago Macieiraa43a4ef2015-05-06 20:25:18 -0700552 return CborNoError;
Thiago Macieira54a0e102015-05-05 21:25:06 -0700553}