TinyCBOR 0.5.2 API
cbor.h
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2017 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 #ifndef CBOR_H
26 #define CBOR_H
27 
28 #ifndef assert
29 #include <assert.h>
30 #endif
31 #include <limits.h>
32 #include <stddef.h>
33 #include <stdint.h>
34 #include <string.h>
35 #include <stdio.h>
36 
37 #include "tinycbor-version.h"
38 
39 #define TINYCBOR_VERSION ((TINYCBOR_VERSION_MAJOR << 16) | (TINYCBOR_VERSION_MINOR << 8) | TINYCBOR_VERSION_PATCH)
40 
41 #ifdef __cplusplus
42 extern "C" {
43 #else
44 #include <stdbool.h>
45 #endif
46 
47 #ifndef SIZE_MAX
48 /* Some systems fail to define SIZE_MAX in <stdint.h>, even though C99 requires it...
49  * Conversion from signed to unsigned is defined in 6.3.1.3 (Signed and unsigned integers) p2,
50  * which says: "the value is converted by repeatedly adding or subtracting one more than the
51  * maximum value that can be represented in the new type until the value is in the range of the
52  * new type."
53  * So -1 gets converted to size_t by adding SIZE_MAX + 1, which results in SIZE_MAX.
54  */
55 # define SIZE_MAX ((size_t)-1)
56 #endif
57 
58 #ifndef CBOR_API
59 # define CBOR_API
60 #endif
61 #ifndef CBOR_PRIVATE_API
62 # define CBOR_PRIVATE_API
63 #endif
64 #ifndef CBOR_INLINE_API
65 # if defined(__cplusplus)
66 # define CBOR_INLINE inline
67 # define CBOR_INLINE_API inline
68 # else
69 # define CBOR_INLINE_API static CBOR_INLINE
70 # if defined(_MSC_VER)
71 # define CBOR_INLINE __inline
72 # elif defined(__GNUC__)
73 # define CBOR_INLINE __inline__
74 # elif defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
75 # define CBOR_INLINE inline
76 # else
77 # define CBOR_INLINE
78 # endif
79 # endif
80 #endif
81 
82 typedef enum CborType {
83  CborIntegerType = 0x00,
84  CborByteStringType = 0x40,
85  CborTextStringType = 0x60,
86  CborArrayType = 0x80,
87  CborMapType = 0xa0,
88  CborTagType = 0xc0,
89  CborSimpleType = 0xe0,
90  CborBooleanType = 0xf5,
91  CborNullType = 0xf6,
92  CborUndefinedType = 0xf7,
93  CborHalfFloatType = 0xf9,
94  CborFloatType = 0xfa,
95  CborDoubleType = 0xfb,
96 
97  CborInvalidType = 0xff /* equivalent to the break byte, so it will never be used */
98 } CborType;
99 
100 typedef uint64_t CborTag;
101 typedef enum CborKnownTags {
102  CborDateTimeStringTag = 0,
103  CborUnixTime_tTag = 1,
104  CborPositiveBignumTag = 2,
105  CborNegativeBignumTag = 3,
106  CborDecimalTag = 4,
107  CborBigfloatTag = 5,
108  CborCOSE_Encrypt0Tag = 16,
109  CborCOSE_Mac0Tag = 17,
110  CborCOSE_Sign1Tag = 18,
111  CborExpectedBase64urlTag = 21,
112  CborExpectedBase64Tag = 22,
113  CborExpectedBase16Tag = 23,
114  CborEncodedCborTag = 24,
115  CborUrlTag = 32,
116  CborBase64urlTag = 33,
117  CborBase64Tag = 34,
118  CborRegularExpressionTag = 35,
119  CborMimeMessageTag = 36,
120  CborCOSE_EncryptTag = 96,
121  CborCOSE_MacTag = 97,
122  CborCOSE_SignTag = 98,
123  CborSignatureTag = 55799
124 } CborKnownTags;
125 
126 /* #define the constants so we can check with #ifdef */
127 #define CborDateTimeStringTag CborDateTimeStringTag
128 #define CborUnixTime_tTag CborUnixTime_tTag
129 #define CborPositiveBignumTag CborPositiveBignumTag
130 #define CborNegativeBignumTag CborNegativeBignumTag
131 #define CborDecimalTag CborDecimalTag
132 #define CborBigfloatTag CborBigfloatTag
133 #define CborCOSE_Encrypt0Tag CborCOSE_Encrypt0Tag
134 #define CborCOSE_Mac0Tag CborCOSE_Mac0Tag
135 #define CborCOSE_Sign1Tag CborCOSE_Sign1Tag
136 #define CborExpectedBase64urlTag CborExpectedBase64urlTag
137 #define CborExpectedBase64Tag CborExpectedBase64Tag
138 #define CborExpectedBase16Tag CborExpectedBase16Tag
139 #define CborEncodedCborTag CborEncodedCborTag
140 #define CborUrlTag CborUrlTag
141 #define CborBase64urlTag CborBase64urlTag
142 #define CborBase64Tag CborBase64Tag
143 #define CborRegularExpressionTag CborRegularExpressionTag
144 #define CborMimeMessageTag CborMimeMessageTag
145 #define CborCOSE_EncryptTag CborCOSE_EncryptTag
146 #define CborCOSE_MacTag CborCOSE_MacTag
147 #define CborCOSE_SignTag CborCOSE_SignTag
148 #define CborSignatureTag CborSignatureTag
149 
150 /* Error API */
151 
152 typedef enum CborError {
153  CborNoError = 0,
154 
155  /* errors in all modes */
156  CborUnknownError,
157  CborErrorUnknownLength, /* request for length in array, map, or string with indeterminate length */
158  CborErrorAdvancePastEOF,
159  CborErrorIO,
160 
161  /* parser errors streaming errors */
162  CborErrorGarbageAtEnd = 256,
163  CborErrorUnexpectedEOF,
164  CborErrorUnexpectedBreak,
165  CborErrorUnknownType, /* can only happen in major type 7 */
166  CborErrorIllegalType, /* type not allowed here */
167  CborErrorIllegalNumber,
168  CborErrorIllegalSimpleType, /* types of value less than 32 encoded in two bytes */
169 
170  /* parser errors in strict mode parsing only */
171  CborErrorUnknownSimpleType = 512,
172  CborErrorUnknownTag,
173  CborErrorInappropriateTagForType,
174  CborErrorDuplicateObjectKeys,
175  CborErrorInvalidUtf8TextString,
176  CborErrorExcludedType,
177  CborErrorExcludedValue,
178  CborErrorImproperValue,
179  CborErrorOverlongEncoding,
180  CborErrorMapKeyNotString,
181  CborErrorMapNotSorted,
182  CborErrorMapKeysNotUnique,
183 
184  /* encoder errors */
185  CborErrorTooManyItems = 768,
186  CborErrorTooFewItems,
187 
188  /* internal implementation errors */
189  CborErrorDataTooLarge = 1024,
190  CborErrorNestingTooDeep,
191  CborErrorUnsupportedType,
192 
193  /* errors in converting to JSON */
194  CborErrorJsonObjectKeyIsAggregate = 1280,
195  CborErrorJsonObjectKeyNotString,
196  CborErrorJsonNotImplemented,
197 
198  CborErrorOutOfMemory = (int) (~0U / 2 + 1),
199  CborErrorInternalError = (int) (~0U / 2) /* INT_MAX on two's complement machines */
200 } CborError;
201 
202 CBOR_API const char *cbor_error_string(CborError error);
203 
204 /* Encoder API */
206 {
207  union {
208  uint8_t *ptr;
209  ptrdiff_t bytes_needed;
210  } data;
211  const uint8_t *end;
212  size_t remaining;
213  int flags;
214 };
215 typedef struct CborEncoder CborEncoder;
216 
217 static const size_t CborIndefiniteLength = SIZE_MAX;
218 
219 CBOR_API void cbor_encoder_init(CborEncoder *encoder, uint8_t *buffer, size_t size, int flags);
220 CBOR_API CborError cbor_encode_uint(CborEncoder *encoder, uint64_t value);
221 CBOR_API CborError cbor_encode_int(CborEncoder *encoder, int64_t value);
222 CBOR_API CborError cbor_encode_negative_int(CborEncoder *encoder, uint64_t absolute_value);
223 CBOR_API CborError cbor_encode_simple_value(CborEncoder *encoder, uint8_t value);
224 CBOR_API CborError cbor_encode_tag(CborEncoder *encoder, CborTag tag);
225 CBOR_API CborError cbor_encode_text_string(CborEncoder *encoder, const char *string, size_t length);
226 CBOR_INLINE_API CborError cbor_encode_text_stringz(CborEncoder *encoder, const char *string)
227 { return cbor_encode_text_string(encoder, string, strlen(string)); }
228 CBOR_API CborError cbor_encode_byte_string(CborEncoder *encoder, const uint8_t *string, size_t length);
229 CBOR_API CborError cbor_encode_floating_point(CborEncoder *encoder, CborType fpType, const void *value);
230 
231 CBOR_INLINE_API CborError cbor_encode_boolean(CborEncoder *encoder, bool value)
232 { return cbor_encode_simple_value(encoder, (int)value - 1 + (CborBooleanType & 0x1f)); }
233 CBOR_INLINE_API CborError cbor_encode_null(CborEncoder *encoder)
234 { return cbor_encode_simple_value(encoder, CborNullType & 0x1f); }
235 CBOR_INLINE_API CborError cbor_encode_undefined(CborEncoder *encoder)
236 { return cbor_encode_simple_value(encoder, CborUndefinedType & 0x1f); }
237 
238 CBOR_INLINE_API CborError cbor_encode_half_float(CborEncoder *encoder, const void *value)
239 { return cbor_encode_floating_point(encoder, CborHalfFloatType, value); }
240 CBOR_INLINE_API CborError cbor_encode_float(CborEncoder *encoder, float value)
241 { return cbor_encode_floating_point(encoder, CborFloatType, &value); }
242 CBOR_INLINE_API CborError cbor_encode_double(CborEncoder *encoder, double value)
243 { return cbor_encode_floating_point(encoder, CborDoubleType, &value); }
244 
245 CBOR_API CborError cbor_encoder_create_array(CborEncoder *encoder, CborEncoder *arrayEncoder, size_t length);
246 CBOR_API CborError cbor_encoder_create_map(CborEncoder *encoder, CborEncoder *mapEncoder, size_t length);
247 CBOR_API CborError cbor_encoder_close_container(CborEncoder *encoder, const CborEncoder *containerEncoder);
248 CBOR_API CborError cbor_encoder_close_container_checked(CborEncoder *encoder, const CborEncoder *containerEncoder);
249 
250 CBOR_INLINE_API uint8_t *_cbor_encoder_get_buffer_pointer(const CborEncoder *encoder)
251 {
252  return encoder->data.ptr;
253 }
254 
255 CBOR_INLINE_API size_t cbor_encoder_get_buffer_size(const CborEncoder *encoder, const uint8_t *buffer)
256 {
257  return (size_t)(encoder->data.ptr - buffer);
258 }
259 
260 CBOR_INLINE_API size_t cbor_encoder_get_extra_bytes_needed(const CborEncoder *encoder)
261 {
262  return encoder->end ? 0 : (size_t)encoder->data.bytes_needed;
263 }
264 
265 /* Parser API */
266 
267 enum CborParserIteratorFlags
268 {
269  CborIteratorFlag_IntegerValueTooLarge = 0x01,
270  CborIteratorFlag_NegativeInteger = 0x02,
271  CborIteratorFlag_IteratingStringChunks = 0x02,
272  CborIteratorFlag_UnknownLength = 0x04,
273  CborIteratorFlag_ContainerIsMap = 0x20
274 };
275 
276 struct CborParser
277 {
278  const uint8_t *end;
279  int flags;
280 };
281 typedef struct CborParser CborParser;
282 
283 struct CborValue
284 {
285  const CborParser *parser;
286  const uint8_t *ptr;
287  uint32_t remaining;
288  uint16_t extra;
289  uint8_t type;
290  uint8_t flags;
291 };
292 typedef struct CborValue CborValue;
293 
294 CBOR_API CborError cbor_parser_init(const uint8_t *buffer, size_t size, int flags, CborParser *parser, CborValue *it);
295 
296 CBOR_API CborError cbor_value_validate_basic(const CborValue *it);
297 
298 CBOR_INLINE_API bool cbor_value_at_end(const CborValue *it)
299 { return it->remaining == 0; }
300 CBOR_INLINE_API const uint8_t *cbor_value_get_next_byte(const CborValue *it)
301 { return it->ptr; }
304 CBOR_INLINE_API bool cbor_value_is_container(const CborValue *it)
305 { return it->type == CborArrayType || it->type == CborMapType; }
306 CBOR_API CborError cbor_value_enter_container(const CborValue *it, CborValue *recursed);
307 CBOR_API CborError cbor_value_leave_container(CborValue *it, const CborValue *recursed);
308 
309 CBOR_PRIVATE_API uint64_t _cbor_value_decode_int64_internal(const CborValue *value);
310 CBOR_INLINE_API uint64_t _cbor_value_extract_int64_helper(const CborValue *value)
311 {
312  return value->flags & CborIteratorFlag_IntegerValueTooLarge ?
313  _cbor_value_decode_int64_internal(value) : value->extra;
314 }
315 
316 CBOR_INLINE_API bool cbor_value_is_valid(const CborValue *value)
317 { return value && value->type != CborInvalidType; }
318 CBOR_INLINE_API CborType cbor_value_get_type(const CborValue *value)
319 { return (CborType)value->type; }
320 
321 /* Null & undefined type */
322 CBOR_INLINE_API bool cbor_value_is_null(const CborValue *value)
323 { return value->type == CborNullType; }
324 CBOR_INLINE_API bool cbor_value_is_undefined(const CborValue *value)
325 { return value->type == CborUndefinedType; }
326 
327 /* Booleans */
328 CBOR_INLINE_API bool cbor_value_is_boolean(const CborValue *value)
329 { return value->type == CborBooleanType; }
330 CBOR_INLINE_API CborError cbor_value_get_boolean(const CborValue *value, bool *result)
331 {
332  assert(cbor_value_is_boolean(value));
333  *result = !!value->extra;
334  return CborNoError;
335 }
336 
337 /* Simple types */
338 CBOR_INLINE_API bool cbor_value_is_simple_type(const CborValue *value)
339 { return value->type == CborSimpleType; }
340 CBOR_INLINE_API CborError cbor_value_get_simple_type(const CborValue *value, uint8_t *result)
341 {
342  assert(cbor_value_is_simple_type(value));
343  *result = (uint8_t)value->extra;
344  return CborNoError;
345 }
346 
347 /* Integers */
348 CBOR_INLINE_API bool cbor_value_is_integer(const CborValue *value)
349 { return value->type == CborIntegerType; }
350 CBOR_INLINE_API bool cbor_value_is_unsigned_integer(const CborValue *value)
351 { return cbor_value_is_integer(value) && (value->flags & CborIteratorFlag_NegativeInteger) == 0; }
352 CBOR_INLINE_API bool cbor_value_is_negative_integer(const CborValue *value)
353 { return cbor_value_is_integer(value) && (value->flags & CborIteratorFlag_NegativeInteger); }
354 
355 CBOR_INLINE_API CborError cbor_value_get_raw_integer(const CborValue *value, uint64_t *result)
356 {
357  assert(cbor_value_is_integer(value));
358  *result = _cbor_value_extract_int64_helper(value);
359  return CborNoError;
360 }
361 
362 CBOR_INLINE_API CborError cbor_value_get_uint64(const CborValue *value, uint64_t *result)
363 {
364  assert(cbor_value_is_unsigned_integer(value));
365  *result = _cbor_value_extract_int64_helper(value);
366  return CborNoError;
367 }
368 
369 CBOR_INLINE_API CborError cbor_value_get_int64(const CborValue *value, int64_t *result)
370 {
371  assert(cbor_value_is_integer(value));
372  *result = (int64_t) _cbor_value_extract_int64_helper(value);
373  if (value->flags & CborIteratorFlag_NegativeInteger)
374  *result = -*result - 1;
375  return CborNoError;
376 }
377 
378 CBOR_INLINE_API CborError cbor_value_get_int(const CborValue *value, int *result)
379 {
380  assert(cbor_value_is_integer(value));
381  *result = (int) _cbor_value_extract_int64_helper(value);
382  if (value->flags & CborIteratorFlag_NegativeInteger)
383  *result = -*result - 1;
384  return CborNoError;
385 }
386 
387 CBOR_API CborError cbor_value_get_int64_checked(const CborValue *value, int64_t *result);
388 CBOR_API CborError cbor_value_get_int_checked(const CborValue *value, int *result);
389 
390 CBOR_INLINE_API bool cbor_value_is_length_known(const CborValue *value)
391 { return (value->flags & CborIteratorFlag_UnknownLength) == 0; }
392 
393 /* Tags */
394 CBOR_INLINE_API bool cbor_value_is_tag(const CborValue *value)
395 { return value->type == CborTagType; }
396 CBOR_INLINE_API CborError cbor_value_get_tag(const CborValue *value, CborTag *result)
397 {
398  assert(cbor_value_is_tag(value));
399  *result = _cbor_value_extract_int64_helper(value);
400  return CborNoError;
401 }
403 
404 /* Strings */
405 CBOR_INLINE_API bool cbor_value_is_byte_string(const CborValue *value)
406 { return value->type == CborByteStringType; }
407 CBOR_INLINE_API bool cbor_value_is_text_string(const CborValue *value)
408 { return value->type == CborTextStringType; }
409 
410 CBOR_INLINE_API CborError cbor_value_get_string_length(const CborValue *value, size_t *length)
411 {
412  assert(cbor_value_is_byte_string(value) || cbor_value_is_text_string(value));
413  if (!cbor_value_is_length_known(value))
414  return CborErrorUnknownLength;
415  uint64_t v = _cbor_value_extract_int64_helper(value);
416  *length = (size_t)v;
417  if (*length != v)
418  return CborErrorDataTooLarge;
419  return CborNoError;
420 }
421 
422 CBOR_PRIVATE_API CborError _cbor_value_copy_string(const CborValue *value, void *buffer,
423  size_t *buflen, CborValue *next);
424 CBOR_PRIVATE_API CborError _cbor_value_dup_string(const CborValue *value, void **buffer,
425  size_t *buflen, CborValue *next);
426 
427 CBOR_API CborError cbor_value_calculate_string_length(const CborValue *value, size_t *length);
428 
429 CBOR_INLINE_API CborError cbor_value_copy_text_string(const CborValue *value, char *buffer,
430  size_t *buflen, CborValue *next)
431 {
432  assert(cbor_value_is_text_string(value));
433  return _cbor_value_copy_string(value, buffer, buflen, next);
434 }
435 CBOR_INLINE_API CborError cbor_value_copy_byte_string(const CborValue *value, uint8_t *buffer,
436  size_t *buflen, CborValue *next)
437 {
438  assert(cbor_value_is_byte_string(value));
439  return _cbor_value_copy_string(value, buffer, buflen, next);
440 }
441 
442 CBOR_INLINE_API CborError cbor_value_dup_text_string(const CborValue *value, char **buffer,
443  size_t *buflen, CborValue *next)
444 {
445  assert(cbor_value_is_text_string(value));
446  return _cbor_value_dup_string(value, (void **)buffer, buflen, next);
447 }
448 CBOR_INLINE_API CborError cbor_value_dup_byte_string(const CborValue *value, uint8_t **buffer,
449  size_t *buflen, CborValue *next)
450 {
451  assert(cbor_value_is_byte_string(value));
452  return _cbor_value_dup_string(value, (void **)buffer, buflen, next);
453 }
454 
455 CBOR_API CborError cbor_value_text_string_equals(const CborValue *value, const char *string, bool *result);
456 
457 /* Maps and arrays */
458 CBOR_INLINE_API bool cbor_value_is_array(const CborValue *value)
459 { return value->type == CborArrayType; }
460 CBOR_INLINE_API bool cbor_value_is_map(const CborValue *value)
461 { return value->type == CborMapType; }
462 
463 CBOR_INLINE_API CborError cbor_value_get_array_length(const CborValue *value, size_t *length)
464 {
465  assert(cbor_value_is_array(value));
466  if (!cbor_value_is_length_known(value))
467  return CborErrorUnknownLength;
468  uint64_t v = _cbor_value_extract_int64_helper(value);
469  *length = (size_t)v;
470  if (*length != v)
471  return CborErrorDataTooLarge;
472  return CborNoError;
473 }
474 
475 CBOR_INLINE_API CborError cbor_value_get_map_length(const CborValue *value, size_t *length)
476 {
477  assert(cbor_value_is_map(value));
478  if (!cbor_value_is_length_known(value))
479  return CborErrorUnknownLength;
480  uint64_t v = _cbor_value_extract_int64_helper(value);
481  *length = (size_t)v;
482  if (*length != v)
483  return CborErrorDataTooLarge;
484  return CborNoError;
485 }
486 
487 CBOR_API CborError cbor_value_map_find_value(const CborValue *map, const char *string, CborValue *element);
488 
489 /* Floating point */
490 CBOR_INLINE_API bool cbor_value_is_half_float(const CborValue *value)
491 { return value->type == CborHalfFloatType; }
492 CBOR_API CborError cbor_value_get_half_float(const CborValue *value, void *result);
493 
494 CBOR_INLINE_API bool cbor_value_is_float(const CborValue *value)
495 { return value->type == CborFloatType; }
496 CBOR_INLINE_API CborError cbor_value_get_float(const CborValue *value, float *result)
497 {
498  assert(cbor_value_is_float(value));
499  assert(value->flags & CborIteratorFlag_IntegerValueTooLarge);
500  uint32_t data = (uint32_t)_cbor_value_decode_int64_internal(value);
501  memcpy(result, &data, sizeof(*result));
502  return CborNoError;
503 }
504 
505 CBOR_INLINE_API bool cbor_value_is_double(const CborValue *value)
506 { return value->type == CborDoubleType; }
507 CBOR_INLINE_API CborError cbor_value_get_double(const CborValue *value, double *result)
508 {
509  assert(cbor_value_is_double(value));
510  assert(value->flags & CborIteratorFlag_IntegerValueTooLarge);
511  uint64_t data = _cbor_value_decode_int64_internal(value);
512  memcpy(result, &data, sizeof(*result));
513  return CborNoError;
514 }
515 
516 /* Validation API */
517 
519  /* Bit mapping:
520  * bits 0-7 (8 bits): canonical format
521  * bits 8-11 (4 bits): canonical format & strict mode
522  * bits 12-20 (8 bits): strict mode
523  * bits 21-31 (10 bits): other
524  */
525 
526  CborValidateShortestIntegrals = 0x0001,
527  CborValidateShortestFloatingPoint = 0x0002,
528  CborValidateShortestNumbers = CborValidateShortestIntegrals | CborValidateShortestFloatingPoint,
529  CborValidateNoIndeterminateLength = 0x0100,
530  CborValidateMapIsSorted = 0x0200 | CborValidateNoIndeterminateLength,
531 
532  CborValidateCanonicalFormat = 0x0fff,
533 
534  CborValidateMapKeysAreUnique = 0x1000 | CborValidateMapIsSorted,
535  CborValidateTagUse = 0x2000,
536  CborValidateUtf8 = 0x4000,
537 
538  CborValidateStrictMode = 0xfff00,
539 
540  CborValidateMapKeysAreString = 0x100000,
541  CborValidateNoUndefined = 0x200000,
542  CborValidateNoTags = 0x400000,
543  CborValidateFiniteFloatingPoint = 0x800000,
544  /* unused = 0x1000000, */
545  /* unused = 0x2000000, */
546 
547  CborValidateNoUnknownSimpleTypesSA = 0x4000000,
548  CborValidateNoUnknownSimpleTypes = 0x8000000 | CborValidateNoUnknownSimpleTypesSA,
549  CborValidateNoUnknownTagsSA = 0x10000000,
550  CborValidateNoUnknownTagsSR = 0x20000000 | CborValidateNoUnknownTagsSA,
551  CborValidateNoUnknownTags = 0x40000000 | CborValidateNoUnknownTagsSR,
552 
553  CborValidateCompleteData = (int)0x80000000,
554 
555  CborValidateStrictest = (int)~0U,
556  CborValidateBasic = 0
557 };
558 
559 CBOR_API CborError cbor_value_validate(const CborValue *it, int flags);
560 
561 /* Human-readable (dump) API */
562 
564  CborPrettyNumericEncodingIndicators = 0x01,
565  CborPrettyTextualEncodingIndicators = 0,
566 
567  CborPrettyIndicateIndeterminateLength = 0x02,
568  CborPrettyIndicateIndetermineLength = CborPrettyIndicateIndeterminateLength, /* deprecated */
569  CborPrettyIndicateOverlongNumbers = 0x04,
570 
571  CborPrettyShowStringFragments = 0x100,
572  CborPrettyMergeStringFragments = 0,
573 
574  CborPrettyDefaultFlags = CborPrettyIndicateIndeterminateLength
575 };
576 
577 typedef CborError (*CborStreamFunction)(void *token, const char *fmt, ...)
578 #ifdef __GNUC__
579  __attribute__((__format__(printf, 2, 3)))
580 #endif
581 ;
582 
583 CBOR_API CborError cbor_value_to_pretty_stream(CborStreamFunction streamFunction, void *token, CborValue *value, int flags);
584 
585 /* The following API requires a hosted C implementation (uses FILE*) */
586 #if !defined(__STDC_HOSTED__) || __STDC_HOSTED__-0 == 1
587 CBOR_API CborError cbor_value_to_pretty_advance_flags(FILE *out, CborValue *value, int flags);
588 CBOR_API CborError cbor_value_to_pretty_advance(FILE *out, CborValue *value);
589 CBOR_INLINE_API CborError cbor_value_to_pretty(FILE *out, const CborValue *value)
590 {
591  CborValue copy = *value;
592  return cbor_value_to_pretty_advance_flags(out, &copy, CborPrettyDefaultFlags);
593 }
594 #endif /* __STDC_HOSTED__ check */
595 
596 #ifdef __cplusplus
597 }
598 #endif
599 
600 #endif /* CBOR_H */
601 
CborError cbor_value_get_uint64(const CborValue *value, uint64_t *result)
Retrieves the CBOR integer value that value points to and stores it in result.
Definition: cbor.h:362
CBOR_API CborError cbor_encode_simple_value(CborEncoder *encoder, uint8_t value)
Appends the CBOR Simple Type of value value to the CBOR stream provided by encoder.
Definition: cborencoder.c:366
CborError cbor_value_copy_byte_string(const CborValue *value, uint8_t *buffer, size_t *buflen, CborValue *next)
Copies the string pointed by value into the buffer provided at buffer of buflen bytes.
Definition: cbor.h:435
CBOR_API CborError cbor_encode_text_string(CborEncoder *encoder, const char *string, size_t length)
Appends the byte string string of length length to the CBOR stream provided by encoder.
Definition: cborencoder.c:452
CborError cbor_value_get_string_length(const CborValue *value, size_t *length)
Extracts the length of the byte or text string that value points to and stores it in result...
Definition: cbor.h:410
CborType cbor_value_get_type(const CborValue *value)
Returns the type of the CBOR value that the iterator value points to.
Definition: cbor.h:318
CborError cbor_encode_float(CborEncoder *encoder, float value)
Appends the IEEE 754 single-precision (32-bit) floating point value value to the CBOR stream provided...
Definition: cbor.h:240
CborError cbor_value_get_int64(const CborValue *value, int64_t *result)
Retrieves the CBOR integer value that value points to and stores it in result.
Definition: cbor.h:369
CBOR_API CborError cbor_encoder_close_container(CborEncoder *encoder, const CborEncoder *containerEncoder)
Closes the CBOR container (array or map) provided by containerEncoder and updates the CBOR stream pro...
Definition: cborencoder.c:540
CBOR_API CborError cbor_value_text_string_equals(const CborValue *value, const char *string, bool *result)
Compares the entry value with the string string and stores the result in result.
Definition: cborparser.c:1216
uint64_t CborTag
This typedef is an unsigned 64-bit integer.
Definition: cbor.h:100
CborError cbor_value_get_raw_integer(const CborValue *value, uint64_t *result)
Retrieves the CBOR integer value that value points to and stores it in result.
Definition: cbor.h:355
CborType
The CborType enum contains the types known to TinyCBOR.
Definition: cbor.h:82
bool cbor_value_is_array(const CborValue *value)
Returns true if the iterator value is valid and points to a CBOR array.
Definition: cbor.h:458
CborKnownTags
The CborKnownTags enum contains known tags specified in RFC 7049, for use by the application.
Definition: cbor.h:101
CBOR_API CborError cbor_encode_negative_int(CborEncoder *encoder, uint64_t absolute_value)
Appends the negative 64-bit integer whose absolute value is absolute_value to the CBOR stream provide...
Definition: cborencoder.c:339
CborError cbor_value_get_simple_type(const CborValue *value, uint8_t *result)
Retrieves the CBOR Simple Type value that value points to and stores it in result.
Definition: cbor.h:340
CborError cbor_value_dup_text_string(const CborValue *value, char **buffer, size_t *buflen, CborValue *next)
Allocates memory for the string pointed by value and copies it into this buffer.
Definition: cbor.h:442
CBOR_API CborError cbor_encode_floating_point(CborEncoder *encoder, CborType fpType, const void *value)
Appends the floating-point value of type fpType and pointed to by value to the CBOR stream provided b...
Definition: cborencoder.c:387
CborPrettyFlags
The CborPrettyFlags enum contains flags that control the conversion of CBOR to text format...
Definition: cbor.h:563
CborError
The CborError enum contains the possible error values used by the CBOR encoder and decoder...
Definition: cbor.h:152
CBOR_API CborError cbor_value_get_int64_checked(const CborValue *value, int64_t *result)
Retrieves the CBOR integer value that value points to and stores it in result.
Definition: cborparser.c:816
CBOR_API CborError cbor_value_to_pretty_advance(FILE *out, CborValue *value)
Converts the current CBOR type pointed to by value to its textual representation and writes it to the...
Definition: cborpretty_stdio.c:63
CBOR_API CborError cbor_value_get_int_checked(const CborValue *value, int *result)
Retrieves the CBOR integer value that value points to and stores it in result.
Definition: cborparser.c:855
CborError cbor_value_copy_text_string(const CborValue *value, char *buffer, size_t *buflen, CborValue *next)
Copies the string pointed to by value into the buffer provided at buffer of buflen bytes...
Definition: cbor.h:429
CBOR_API CborError cbor_encode_byte_string(CborEncoder *encoder, const uint8_t *string, size_t length)
Appends the text string string of length length to the CBOR stream provided by encoder.
Definition: cborencoder.c:441
CBOR_API void cbor_encoder_init(CborEncoder *encoder, uint8_t *buffer, size_t size, int flags)
Initializes a CborEncoder structure encoder by pointing it to buffer buffer of size size...
Definition: cborencoder.c:203
size_t cbor_encoder_get_buffer_size(const CborEncoder *encoder, const uint8_t *buffer)
Returns the total size of the buffer starting at buffer after the encoding finished without errors...
Definition: cbor.h:255
bool cbor_value_is_float(const CborValue *value)
Returns true if the iterator value is valid and points to a CBOR single-precision floating point (32-...
Definition: cbor.h:494
bool cbor_value_is_integer(const CborValue *value)
Returns true if the iterator value is valid and points to a CBOR integer type.
Definition: cbor.h:348
CborError cbor_value_get_tag(const CborValue *value, CborTag *result)
Retrieves the CBOR tag value that value points to and stores it in result.
Definition: cbor.h:396
bool cbor_value_is_tag(const CborValue *value)
Returns true if the iterator value is valid and points to a CBOR tag.
Definition: cbor.h:394
CBOR_API const char * cbor_error_string(CborError error)
Returns the error string corresponding to the CBOR error condition error.
Definition: cborerrorstrings.c:80
CBOR_API CborError cbor_encode_uint(CborEncoder *encoder, uint64_t value)
Appends the unsigned 64-bit integer value to the CBOR stream provided by encoder. ...
Definition: cborencoder.c:326
bool cbor_value_is_byte_string(const CborValue *value)
Returns true if the iterator value is valid and points to a CBOR text string.
Definition: cbor.h:405
CBOR_API CborError cbor_encoder_create_array(CborEncoder *encoder, CborEncoder *arrayEncoder, size_t length)
Creates a CBOR array in the CBOR stream provided by encoder and initializes arrayEncoder so that item...
Definition: cborencoder.c:497
bool cbor_value_is_valid(const CborValue *value)
Returns true if the iterator it contains a valid value.
Definition: cbor.h:316
CborError cbor_encode_half_float(CborEncoder *encoder, const void *value)
Appends the IEEE 754 half-precision (16-bit) floating point value pointed to by value to the CBOR str...
Definition: cbor.h:238
bool cbor_value_is_half_float(const CborValue *value)
Returns true if the iterator value is valid and points to a CBOR single-precision floating point (16-...
Definition: cbor.h:490
CBOR_API CborError cbor_value_validate_basic(const CborValue *it)
Performs a basic validation of the CBOR stream pointed by it and returns the error it found...
Definition: cborparser.c:450
CBOR_API CborError cbor_value_validate(const CborValue *it, int flags)
Performs a full validation, controlled by the flags options, of the CBOR stream pointed by it and ret...
Definition: cborvalidation.c:651
CborError cbor_value_get_boolean(const CborValue *value, bool *result)
Retrieves the boolean value that value points to and stores it in result.
Definition: cbor.h:330
static const size_t CborIndefiniteLength
This variable is a constant used to indicate that the length of the map or array is not yet determine...
Definition: cbor.h:217
CborError cbor_value_get_map_length(const CborValue *value, size_t *length)
Extracts the length of the CBOR map that value points to and stores it in result. ...
Definition: cbor.h:475
bool cbor_value_is_boolean(const CborValue *value)
Returns true if the iterator value is valid and points to a CBOR boolean type (true or false)...
Definition: cbor.h:328
bool cbor_value_is_unsigned_integer(const CborValue *value)
Returns true if the iterator value is valid and points to a CBOR unsigned integer type (positive valu...
Definition: cbor.h:350
CBOR_API CborError cbor_value_enter_container(const CborValue *it, CborValue *recursed)
Creates a CborValue iterator pointing to the first element of the container represented by it and sav...
Definition: cborparser.c:585
CBOR_API CborError cbor_value_advance_fixed(CborValue *it)
Advances the CBOR value it by one fixed-size position.
Definition: cborparser.c:471
CborError cbor_value_get_array_length(const CborValue *value, size_t *length)
Extracts the length of the CBOR array that value points to and stores it in result.
Definition: cbor.h:463
bool cbor_value_is_negative_integer(const CborValue *value)
Returns true if the iterator value is valid and points to a CBOR negative integer type...
Definition: cbor.h:352
CborValidationFlags
The CborValidationFlags enum contains flags that control the validation of a CBOR stream...
Definition: cbor.h:518
CBOR_API CborError cbor_encode_tag(CborEncoder *encoder, CborTag tag)
Appends the CBOR tag tag to the CBOR stream provided by encoder.
Definition: cborencoder.c:409
CBOR_API CborError cbor_value_to_pretty_stream(CborStreamFunction streamFunction, void *token, CborValue *value, int flags)
Converts the current CBOR type pointed by value to its textual representation and writes it to the st...
Definition: cborpretty.c:526
CborError cbor_value_to_pretty(FILE *out, const CborValue *value)
Converts the current CBOR type pointed to by value to its textual representation and writes it to the...
Definition: cbor.h:589
CborError cbor_encode_null(CborEncoder *encoder)
Appends the CBOR type representing a null value to the CBOR stream provided by encoder.
Definition: cbor.h:233
CBOR_API CborError cbor_encoder_create_map(CborEncoder *encoder, CborEncoder *mapEncoder, size_t length)
Creates a CBOR map in the CBOR stream provided by encoder and initializes mapEncoder so that items ca...
Definition: cborencoder.c:521
size_t cbor_encoder_get_extra_bytes_needed(const CborEncoder *encoder)
Returns how many more bytes the original buffer supplied to cbor_encoder_init() needs to be extended ...
Definition: cbor.h:260
bool cbor_value_is_container(const CborValue *it)
Returns true if the it value is a container and requires recursion in order to decode (maps and array...
Definition: cbor.h:304
bool cbor_value_is_undefined(const CborValue *value)
Returns true if the iterator value is valid and points to a CBOR undefined type.
Definition: cbor.h:324
CborError cbor_encode_double(CborEncoder *encoder, double value)
Appends the IEEE 754 double-precision (64-bit) floating point value value to the CBOR stream provided...
Definition: cbor.h:242
CborError cbor_encode_text_stringz(CborEncoder *encoder, const char *string)
Appends the null-terminated text string string to the CBOR stream provided by encoder.
Definition: cbor.h:226
CborError cbor_encode_boolean(CborEncoder *encoder, bool value)
Appends the boolean value value to the CBOR stream provided by encoder.
Definition: cbor.h:231
bool cbor_value_at_end(const CborValue *it)
Returns true if it has reached the end of the iteration, usually when advancing after the last item i...
Definition: cbor.h:298
CborError cbor_encode_undefined(CborEncoder *encoder)
Appends the CBOR type representing an undefined value to the CBOR stream provided by encoder...
Definition: cbor.h:235
CborError cbor_value_get_int(const CborValue *value, int *result)
Retrieves the CBOR integer value that value points to and stores it in result.
Definition: cbor.h:378
CBOR_API CborError cbor_parser_init(const uint8_t *buffer, size_t size, int flags, CborParser *parser, CborValue *it)
Initializes the CBOR parser for parsing size bytes beginning at buffer.
Definition: cborparser.c:375
bool cbor_value_is_null(const CborValue *value)
Returns true if the iterator value is valid and points to a CBOR null type.
Definition: cbor.h:322
CBOR_API CborError cbor_value_leave_container(CborValue *it, const CborValue *recursed)
Updates it to point to the next element after the container.
Definition: cborparser.c:634
CBOR_API CborError cbor_value_to_pretty_advance_flags(FILE *out, CborValue *value, int flags)
Converts the current CBOR type pointed to by value to its textual representation and writes it to the...
Definition: cborpretty_stdio.c:83
CBOR_API CborError cbor_value_advance(CborValue *it)
Advances the CBOR value it by one element, skipping over containers.
Definition: cborparser.c:522
bool cbor_value_is_double(const CborValue *value)
Returns true if the iterator value is valid and points to a CBOR double-precision floating point (64-...
Definition: cbor.h:505
CBOR_API CborError cbor_value_map_find_value(const CborValue *map, const char *string, CborValue *element)
Attempts to find the value in map map that corresponds to the text string entry string.
Definition: cborparser.c:1303
This type contains one value parsed from the CBOR stream.
Definition: cbor.h:283
CBOR_API CborError cbor_encoder_close_container_checked(CborEncoder *encoder, const CborEncoder *containerEncoder)
Definition: cborencoder_close_container_checked.c:52
CborError cbor_value_dup_byte_string(const CborValue *value, uint8_t **buffer, size_t *buflen, CborValue *next)
Allocates memory for the string pointed by value and copies it into this buffer.
Definition: cbor.h:448
Structure used to encode to CBOR.
Definition: cbor.h:205
bool cbor_value_is_length_known(const CborValue *value)
Returns true if the length of this type is known without calculation.
Definition: cbor.h:390
CborError cbor_value_get_float(const CborValue *value, float *result)
Retrieves the CBOR single-precision floating point (32-bit) value that value points to and stores it ...
Definition: cbor.h:496
CBOR_API CborError cbor_value_get_half_float(const CborValue *value, void *result)
Retrieves the CBOR half-precision floating point (16-bit) value that value points to and stores it in...
Definition: cborparser.c:1414
CBOR_API CborError cbor_encode_int(CborEncoder *encoder, int64_t value)
Appends the signed 64-bit integer value to the CBOR stream provided by encoder.
Definition: cborencoder.c:350
CBOR_API CborError cbor_value_calculate_string_length(const CborValue *value, size_t *length)
Calculates the length of the byte or text string that value points to and stores it in len...
Definition: cborparser.c:965
bool cbor_value_is_text_string(const CborValue *value)
Returns true if the iterator value is valid and points to a CBOR text string.
Definition: cbor.h:407
bool cbor_value_is_map(const CborValue *value)
Returns true if the iterator value is valid and points to a CBOR map.
Definition: cbor.h:460
bool cbor_value_is_simple_type(const CborValue *value)
Returns true if the iterator value is valid and points to a CBOR Simple Type type (other than true...
Definition: cbor.h:338
const uint8_t * cbor_value_get_next_byte(const CborValue *it)
Returns a pointer to the next byte that would be decoded if this CborValue object were advanced...
Definition: cbor.h:300
CBOR_API CborError cbor_value_skip_tag(CborValue *it)
Advances the CBOR value it until it no longer points to a tag.
Definition: cborparser.c:558