1 /****************************************************************************
3 ** Copyright (C) 2015 Intel Corporation
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:
12 ** The above copyright notice and this permission notice shall be included in
13 ** all copies or substantial portions of the Software.
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
23 ****************************************************************************/
42 /* Some systems fail to define SIZE_MAX in <stdint.h>, even though C99 requires it...
43 * Conversion from signed to unsigned is defined in 6.3.1.3 (Signed and unsigned integers) p2,
44 * which says: "the value is converted by repeatedly adding or subtracting one more than the
45 * maximum value that can be represented in the new type until the value is in the range of the
47 * So -1 gets converted to size_t by adding SIZE_MAX + 1, which results in SIZE_MAX.
49 # define SIZE_MAX ((size_t)-1)
55 #ifndef CBOR_PRIVATE_API
56 # define CBOR_PRIVATE_API
58 #ifndef CBOR_INLINE_API
59 # if defined(__cplusplus)
60 # define CBOR_INLINE inline
61 # define CBOR_INLINE_API inline
63 # define CBOR_INLINE_API static CBOR_INLINE
64 # if defined(_MSC_VER)
65 # define CBOR_INLINE __inline
66 # elif defined(__GNUC__)
67 # define CBOR_INLINE __inline__
68 # elif defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
69 # define CBOR_INLINE inline
76 typedef enum CborType {
77 CborIntegerType = 0x00,
78 CborByteStringType = 0x40,
79 CborTextStringType = 0x60,
83 CborSimpleType = 0xe0,
84 CborBooleanType = 0xf5,
86 CborUndefinedType = 0xf7,
87 CborHalfFloatType = 0xf9,
89 CborDoubleType = 0xfb,
91 CborInvalidType = 0xff /* equivalent to the break byte, so it will never be used */
94 typedef uint64_t CborTag;
95 typedef enum CborKnownTags {
96 CborDateTimeStringTag = 0, /* RFC 3339 format: YYYY-MM-DD hh:mm:ss+zzzz */
97 CborUnixTime_tTag = 1,
98 CborPositiveBignumTag = 2,
99 CborNegativeBignumTag = 3,
102 CborExpectedBase64urlTag = 21,
103 CborExpectedBase64Tag = 22,
104 CborExpectedBase16Tag = 23,
106 CborBase64urlTag = 33,
108 CborRegularExpressionTag = 35,
109 CborMimeMessageTag = 36, /* RFC 2045-2047 */
110 CborSignatureTag = 55799
115 typedef enum CborError {
118 /* errors in all modes */
120 CborErrorUnknownLength, /* request for length in array, map, or string with indeterminate length */
121 CborErrorAdvancePastEOF,
124 /* parser errors streaming errors */
125 CborErrorGarbageAtEnd = 256,
126 CborErrorUnexpectedEOF,
127 CborErrorUnexpectedBreak,
128 CborErrorUnknownType, /* can only heppen in major type 7 */
129 CborErrorIllegalType, /* type not allowed here */
130 CborErrorIllegalNumber,
131 CborErrorIllegalSimpleType, /* types of value less than 32 encoded in two bytes */
133 /* parser errors in strict mode parsing only */
134 CborErrorUnknownSimpleType = 512,
136 CborErrorInappropriateTagForType,
137 CborErrorDuplicateObjectKeys,
138 CborErrorInvalidUtf8TextString,
141 CborErrorTooManyItems = 768,
142 CborErrorTooFewItems,
144 /* internal implementation errors */
145 CborErrorDataTooLarge = 1024,
146 CborErrorNestingTooDeep,
147 CborErrorUnsupportedType,
149 /* errors in converting to JSON */
150 CborErrorJsonObjectKeyIsAggregate,
151 CborErrorJsonObjectKeyNotString,
152 CborErrorJsonNotImplemented,
154 CborErrorOutOfMemory = ~0U / 2 + 1,
155 CborErrorInternalError = ~0U
158 CBOR_API const char *cbor_error_string(CborError error);
165 ptrdiff_t bytes_needed;
171 typedef struct CborEncoder CborEncoder;
173 static const size_t CborIndefiniteLength = SIZE_MAX;
175 CBOR_API void cbor_encoder_init(CborEncoder *encoder, uint8_t *buffer, size_t size, int flags);
176 CBOR_API CborError cbor_encode_uint(CborEncoder *encoder, uint64_t value);
177 CBOR_API CborError cbor_encode_int(CborEncoder *encoder, int64_t value);
178 CBOR_API CborError cbor_encode_negative_int(CborEncoder *encoder, uint64_t absolute_value);
179 CBOR_API CborError cbor_encode_simple_value(CborEncoder *encoder, uint8_t value);
180 CBOR_API CborError cbor_encode_tag(CborEncoder *encoder, CborTag tag);
181 CBOR_API CborError cbor_encode_text_string(CborEncoder *encoder, const char *string, size_t length);
182 CBOR_INLINE_API CborError cbor_encode_text_stringz(CborEncoder *encoder, const char *string)
183 { return cbor_encode_text_string(encoder, string, strlen(string)); }
184 CBOR_API CborError cbor_encode_byte_string(CborEncoder *encoder, const uint8_t *string, size_t length);
185 CBOR_API CborError cbor_encode_floating_point(CborEncoder *encoder, CborType fpType, const void *value);
187 CBOR_INLINE_API CborError cbor_encode_boolean(CborEncoder *encoder, bool value)
188 { return cbor_encode_simple_value(encoder, (int)value - 1 + (CborBooleanType & 0x1f)); }
189 CBOR_INLINE_API CborError cbor_encode_null(CborEncoder *encoder)
190 { return cbor_encode_simple_value(encoder, CborNullType & 0x1f); }
191 CBOR_INLINE_API CborError cbor_encode_undefined(CborEncoder *encoder)
192 { return cbor_encode_simple_value(encoder, CborUndefinedType & 0x1f); }
194 CBOR_INLINE_API CborError cbor_encode_half_float(CborEncoder *encoder, const void *value)
195 { return cbor_encode_floating_point(encoder, CborHalfFloatType, value); }
196 CBOR_INLINE_API CborError cbor_encode_float(CborEncoder *encoder, float value)
197 { return cbor_encode_floating_point(encoder, CborFloatType, &value); }
198 CBOR_INLINE_API CborError cbor_encode_double(CborEncoder *encoder, double value)
199 { return cbor_encode_floating_point(encoder, CborDoubleType, &value); }
201 CBOR_API CborError cbor_encoder_create_array(CborEncoder *encoder, CborEncoder *arrayEncoder, size_t length);
202 CBOR_API CborError cbor_encoder_create_map(CborEncoder *encoder, CborEncoder *mapEncoder, size_t length);
203 CBOR_API CborError cbor_encoder_close_container(CborEncoder *encoder, const CborEncoder *containerEncoder);
204 CBOR_API CborError cbor_encoder_close_container_checked(CborEncoder *encoder, const CborEncoder *containerEncoder);
206 CBOR_INLINE_API size_t cbor_encoder_get_buffer_size(const CborEncoder *encoder, const uint8_t *buffer)
208 return (size_t)(encoder->data.ptr - buffer);
211 CBOR_INLINE_API size_t cbor_encoder_get_extra_bytes_needed(const CborEncoder *encoder)
213 return encoder->end ? 0 : (size_t)encoder->data.bytes_needed;
218 enum CborParserIteratorFlags
220 CborIteratorFlag_IntegerValueTooLarge = 0x01,
221 CborIteratorFlag_NegativeInteger = 0x02,
222 CborIteratorFlag_UnknownLength = 0x04,
223 CborIteratorFlag_ContainerIsMap = 0x20
231 typedef struct CborParser CborParser;
235 const CborParser *parser;
242 typedef struct CborValue CborValue;
244 CBOR_API CborError cbor_parser_init(const uint8_t *buffer, size_t size, int flags, CborParser *parser, CborValue *it);
246 CBOR_INLINE_API bool cbor_value_at_end(const CborValue *it)
247 { return it->remaining == 0; }
248 CBOR_INLINE_API const uint8_t *cbor_value_get_next_byte(const CborValue *it)
250 CBOR_API CborError cbor_value_advance_fixed(CborValue *it);
251 CBOR_API CborError cbor_value_advance(CborValue *it);
252 CBOR_INLINE_API bool cbor_value_is_container(const CborValue *it)
253 { return it->type == CborArrayType || it->type == CborMapType; }
254 CBOR_API CborError cbor_value_enter_container(const CborValue *it, CborValue *recursed);
255 CBOR_API CborError cbor_value_leave_container(CborValue *it, const CborValue *recursed);
257 CBOR_PRIVATE_API uint64_t _cbor_value_decode_int64_internal(const CborValue *value);
258 CBOR_INLINE_API uint64_t _cbor_value_extract_int64_helper(const CborValue *value)
260 return value->flags & CborIteratorFlag_IntegerValueTooLarge ?
261 _cbor_value_decode_int64_internal(value) : value->extra;
264 CBOR_INLINE_API bool cbor_value_is_valid(const CborValue *value)
265 { return value && value->type != CborInvalidType; }
266 CBOR_INLINE_API CborType cbor_value_get_type(const CborValue *value)
267 { return (CborType)value->type; }
269 /* Null & undefined type */
270 CBOR_INLINE_API bool cbor_value_is_null(const CborValue *value)
271 { return value->type == CborNullType; }
272 CBOR_INLINE_API bool cbor_value_is_undefined(const CborValue *value)
273 { return value->type == CborUndefinedType; }
276 CBOR_INLINE_API bool cbor_value_is_boolean(const CborValue *value)
277 { return value->type == CborBooleanType; }
278 CBOR_INLINE_API CborError cbor_value_get_boolean(const CborValue *value, bool *result)
280 assert(cbor_value_is_boolean(value));
281 *result = !!value->extra;
286 CBOR_INLINE_API bool cbor_value_is_simple_type(const CborValue *value)
287 { return value->type == CborSimpleType; }
288 CBOR_INLINE_API CborError cbor_value_get_simple_type(const CborValue *value, uint8_t *result)
290 assert(cbor_value_is_simple_type(value));
291 *result = (uint8_t)value->extra;
296 CBOR_INLINE_API bool cbor_value_is_integer(const CborValue *value)
297 { return value->type == CborIntegerType; }
298 CBOR_INLINE_API bool cbor_value_is_unsigned_integer(const CborValue *value)
299 { return cbor_value_is_integer(value) && (value->flags & CborIteratorFlag_NegativeInteger) == 0; }
300 CBOR_INLINE_API bool cbor_value_is_negative_integer(const CborValue *value)
301 { return cbor_value_is_integer(value) && (value->flags & CborIteratorFlag_NegativeInteger); }
303 CBOR_INLINE_API CborError cbor_value_get_raw_integer(const CborValue *value, uint64_t *result)
305 assert(cbor_value_is_integer(value));
306 *result = _cbor_value_extract_int64_helper(value);
310 CBOR_INLINE_API CborError cbor_value_get_uint64(const CborValue *value, uint64_t *result)
312 assert(cbor_value_is_unsigned_integer(value));
313 *result = _cbor_value_extract_int64_helper(value);
317 CBOR_INLINE_API CborError cbor_value_get_int64(const CborValue *value, int64_t *result)
319 assert(cbor_value_is_integer(value));
320 *result = (int64_t) _cbor_value_extract_int64_helper(value);
321 if (value->flags & CborIteratorFlag_NegativeInteger)
322 *result = -*result - 1;
326 CBOR_INLINE_API CborError cbor_value_get_int(const CborValue *value, int *result)
328 assert(cbor_value_is_integer(value));
329 *result = (int) _cbor_value_extract_int64_helper(value);
330 if (value->flags & CborIteratorFlag_NegativeInteger)
331 *result = -*result - 1;
335 CBOR_API CborError cbor_value_get_int64_checked(const CborValue *value, int64_t *result);
336 CBOR_API CborError cbor_value_get_int_checked(const CborValue *value, int *result);
338 CBOR_INLINE_API bool cbor_value_is_length_known(const CborValue *value)
339 { return (value->flags & CborIteratorFlag_UnknownLength) == 0; }
342 CBOR_INLINE_API bool cbor_value_is_tag(const CborValue *value)
343 { return value->type == CborTagType; }
344 CBOR_INLINE_API CborError cbor_value_get_tag(const CborValue *value, CborTag *result)
346 assert(cbor_value_is_tag(value));
347 *result = _cbor_value_extract_int64_helper(value);
350 CBOR_API CborError cbor_value_skip_tag(CborValue *it);
353 CBOR_INLINE_API bool cbor_value_is_byte_string(const CborValue *value)
354 { return value->type == CborByteStringType; }
355 CBOR_INLINE_API bool cbor_value_is_text_string(const CborValue *value)
356 { return value->type == CborTextStringType; }
358 CBOR_INLINE_API CborError cbor_value_get_string_length(const CborValue *value, size_t *length)
360 assert(cbor_value_is_byte_string(value) || cbor_value_is_text_string(value));
361 if (!cbor_value_is_length_known(value))
362 return CborErrorUnknownLength;
363 uint64_t v = _cbor_value_extract_int64_helper(value);
366 return CborErrorDataTooLarge;
370 CBOR_PRIVATE_API CborError _cbor_value_copy_string(const CborValue *value, void *buffer,
371 size_t *buflen, CborValue *next);
372 CBOR_PRIVATE_API CborError _cbor_value_dup_string(const CborValue *value, void **buffer,
373 size_t *buflen, CborValue *next);
375 CBOR_API CborError cbor_value_calculate_string_length(const CborValue *value, size_t *length);
377 CBOR_INLINE_API CborError cbor_value_copy_text_string(const CborValue *value, char *buffer,
378 size_t *buflen, CborValue *next)
380 assert(cbor_value_is_text_string(value));
381 return _cbor_value_copy_string(value, buffer, buflen, next);
383 CBOR_INLINE_API CborError cbor_value_copy_byte_string(const CborValue *value, uint8_t *buffer,
384 size_t *buflen, CborValue *next)
386 assert(cbor_value_is_byte_string(value));
387 return _cbor_value_copy_string(value, buffer, buflen, next);
390 CBOR_INLINE_API CborError cbor_value_dup_text_string(const CborValue *value, char **buffer,
391 size_t *buflen, CborValue *next)
393 assert(cbor_value_is_text_string(value));
394 return _cbor_value_dup_string(value, (void **)buffer, buflen, next);
396 CBOR_INLINE_API CborError cbor_value_dup_byte_string(const CborValue *value, uint8_t **buffer,
397 size_t *buflen, CborValue *next)
399 assert(cbor_value_is_byte_string(value));
400 return _cbor_value_dup_string(value, (void **)buffer, buflen, next);
403 /* ### TBD: partial reading API */
405 CBOR_API CborError cbor_value_text_string_equals(const CborValue *value, const char *string, bool *result);
407 /* Maps and arrays */
408 CBOR_INLINE_API bool cbor_value_is_array(const CborValue *value)
409 { return value->type == CborArrayType; }
410 CBOR_INLINE_API bool cbor_value_is_map(const CborValue *value)
411 { return value->type == CborMapType; }
413 CBOR_INLINE_API CborError cbor_value_get_array_length(const CborValue *value, size_t *length)
415 assert(cbor_value_is_array(value));
416 if (!cbor_value_is_length_known(value))
417 return CborErrorUnknownLength;
418 uint64_t v = _cbor_value_extract_int64_helper(value);
421 return CborErrorDataTooLarge;
425 CBOR_INLINE_API CborError cbor_value_get_map_length(const CborValue *value, size_t *length)
427 assert(cbor_value_is_map(value));
428 if (!cbor_value_is_length_known(value))
429 return CborErrorUnknownLength;
430 uint64_t v = _cbor_value_extract_int64_helper(value);
433 return CborErrorDataTooLarge;
437 CBOR_API CborError cbor_value_map_find_value(const CborValue *map, const char *string, CborValue *element);
440 CBOR_INLINE_API bool cbor_value_is_half_float(const CborValue *value)
441 { return value->type == CborHalfFloatType; }
442 CBOR_API CborError cbor_value_get_half_float(const CborValue *value, void *result);
444 CBOR_INLINE_API bool cbor_value_is_float(const CborValue *value)
445 { return value->type == CborFloatType; }
446 CBOR_INLINE_API CborError cbor_value_get_float(const CborValue *value, float *result)
448 assert(cbor_value_is_float(value));
449 assert(value->flags & CborIteratorFlag_IntegerValueTooLarge);
450 uint32_t data = (uint32_t)_cbor_value_decode_int64_internal(value);
451 memcpy(result, &data, sizeof(*result));
455 CBOR_INLINE_API bool cbor_value_is_double(const CborValue *value)
456 { return value->type == CborDoubleType; }
457 CBOR_INLINE_API CborError cbor_value_get_double(const CborValue *value, double *result)
459 assert(cbor_value_is_double(value));
460 assert(value->flags & CborIteratorFlag_IntegerValueTooLarge);
461 uint64_t data = _cbor_value_decode_int64_internal(value);
462 memcpy(result, &data, sizeof(*result));
466 /* Human-readable (dump) API */
467 CBOR_API CborError cbor_value_to_pretty_advance(FILE *out, CborValue *value);
468 CBOR_INLINE_API CborError cbor_value_to_pretty(FILE *out, const CborValue *value)
470 CborValue copy = *value;
471 return cbor_value_to_pretty_advance(out, ©);