TinyCBOR 0.4 API
cbor.h
Go to the documentation of this file.
1 /****************************************************************************
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 #ifndef CBOR_H
26 #define CBOR_H
27 
28 #include <assert.h>
29 #include <limits.h>
30 #include <stddef.h>
31 #include <stdint.h>
32 #include <string.h>
33 #include <stdio.h>
34 
35 #include "tinycbor-version.h"
36 
37 #define TINYCBOR_VERSION ((TINYCBOR_VERSION_MAJOR << 16) | (TINYCBOR_VERSION_MINOR << 8) | TINYCBOR_VERSION_PATCH)
38 
39 #ifdef __cplusplus
40 extern "C" {
41 #else
42 #include <stdbool.h>
43 #endif
44 
45 #ifndef SIZE_MAX
46 /* Some systems fail to define SIZE_MAX in <stdint.h>, even though C99 requires it...
47  * Conversion from signed to unsigned is defined in 6.3.1.3 (Signed and unsigned integers) p2,
48  * which says: "the value is converted by repeatedly adding or subtracting one more than the
49  * maximum value that can be represented in the new type until the value is in the range of the
50  * new type."
51  * So -1 gets converted to size_t by adding SIZE_MAX + 1, which results in SIZE_MAX.
52  */
53 # define SIZE_MAX ((size_t)-1)
54 #endif
55 
56 #ifndef CBOR_API
57 # define CBOR_API
58 #endif
59 #ifndef CBOR_PRIVATE_API
60 # define CBOR_PRIVATE_API
61 #endif
62 #ifndef CBOR_INLINE_API
63 # if defined(__cplusplus)
64 # define CBOR_INLINE inline
65 # define CBOR_INLINE_API inline
66 # else
67 # define CBOR_INLINE_API static CBOR_INLINE
68 # if defined(_MSC_VER)
69 # define CBOR_INLINE __inline
70 # elif defined(__GNUC__)
71 # define CBOR_INLINE __inline__
72 # elif defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
73 # define CBOR_INLINE inline
74 # else
75 # define CBOR_INLINE
76 # endif
77 # endif
78 #endif
79 
80 typedef enum CborType {
81  CborIntegerType = 0x00,
82  CborByteStringType = 0x40,
83  CborTextStringType = 0x60,
84  CborArrayType = 0x80,
85  CborMapType = 0xa0,
86  CborTagType = 0xc0,
87  CborSimpleType = 0xe0,
88  CborBooleanType = 0xf5,
89  CborNullType = 0xf6,
90  CborUndefinedType = 0xf7,
91  CborHalfFloatType = 0xf9,
92  CborFloatType = 0xfa,
93  CborDoubleType = 0xfb,
94 
95  CborInvalidType = 0xff /* equivalent to the break byte, so it will never be used */
96 } CborType;
97 
98 typedef uint64_t CborTag;
99 typedef enum CborKnownTags {
100  CborDateTimeStringTag = 0, /* RFC 3339 format: YYYY-MM-DD hh:mm:ss+zzzz */
101  CborUnixTime_tTag = 1,
102  CborPositiveBignumTag = 2,
103  CborNegativeBignumTag = 3,
104  CborDecimalTag = 4,
105  CborBigfloatTag = 5,
106  CborExpectedBase64urlTag = 21,
107  CborExpectedBase64Tag = 22,
108  CborExpectedBase16Tag = 23,
109  CborUriTag = 32,
110  CborBase64urlTag = 33,
111  CborBase64Tag = 34,
112  CborRegularExpressionTag = 35,
113  CborMimeMessageTag = 36, /* RFC 2045-2047 */
114  CborSignatureTag = 55799
115 } CborKnownTags;
116 
117 /* Error API */
118 
119 typedef enum CborError {
120  CborNoError = 0,
121 
122  /* errors in all modes */
123  CborUnknownError,
124  CborErrorUnknownLength, /* request for length in array, map, or string with indeterminate length */
125  CborErrorAdvancePastEOF,
126  CborErrorIO,
127 
128  /* parser errors streaming errors */
129  CborErrorGarbageAtEnd = 256,
130  CborErrorUnexpectedEOF,
131  CborErrorUnexpectedBreak,
132  CborErrorUnknownType, /* can only heppen in major type 7 */
133  CborErrorIllegalType, /* type not allowed here */
134  CborErrorIllegalNumber,
135  CborErrorIllegalSimpleType, /* types of value less than 32 encoded in two bytes */
136 
137  /* parser errors in strict mode parsing only */
138  CborErrorUnknownSimpleType = 512,
139  CborErrorUnknownTag,
140  CborErrorInappropriateTagForType,
141  CborErrorDuplicateObjectKeys,
142  CborErrorInvalidUtf8TextString,
143 
144  /* encoder errors */
145  CborErrorTooManyItems = 768,
146  CborErrorTooFewItems,
147 
148  /* internal implementation errors */
149  CborErrorDataTooLarge = 1024,
150  CborErrorNestingTooDeep,
151  CborErrorUnsupportedType,
152 
153  /* errors in converting to JSON */
154  CborErrorJsonObjectKeyIsAggregate,
155  CborErrorJsonObjectKeyNotString,
156  CborErrorJsonNotImplemented,
157 
158  CborErrorOutOfMemory = ~0U / 2 + 1,
159  CborErrorInternalError = ~0U
160 } CborError;
161 
162 CBOR_API const char *cbor_error_string(CborError error);
163 
164 /* Encoder API */
166 {
167  union {
168  uint8_t *ptr;
169  ptrdiff_t bytes_needed;
170  } data;
171  const uint8_t *end;
172  size_t added;
173  int flags;
174 };
175 typedef struct CborEncoder CborEncoder;
176 
177 static const size_t CborIndefiniteLength = SIZE_MAX;
178 
179 CBOR_API void cbor_encoder_init(CborEncoder *encoder, uint8_t *buffer, size_t size, int flags);
180 CBOR_API CborError cbor_encode_uint(CborEncoder *encoder, uint64_t value);
181 CBOR_API CborError cbor_encode_int(CborEncoder *encoder, int64_t value);
182 CBOR_API CborError cbor_encode_negative_int(CborEncoder *encoder, uint64_t absolute_value);
183 CBOR_API CborError cbor_encode_simple_value(CborEncoder *encoder, uint8_t value);
184 CBOR_API CborError cbor_encode_tag(CborEncoder *encoder, CborTag tag);
185 CBOR_API CborError cbor_encode_text_string(CborEncoder *encoder, const char *string, size_t length);
186 CBOR_INLINE_API CborError cbor_encode_text_stringz(CborEncoder *encoder, const char *string)
187 { return cbor_encode_text_string(encoder, string, strlen(string)); }
188 CBOR_API CborError cbor_encode_byte_string(CborEncoder *encoder, const uint8_t *string, size_t length);
189 CBOR_API CborError cbor_encode_floating_point(CborEncoder *encoder, CborType fpType, const void *value);
190 
191 CBOR_INLINE_API CborError cbor_encode_boolean(CborEncoder *encoder, bool value)
192 { return cbor_encode_simple_value(encoder, (int)value - 1 + (CborBooleanType & 0x1f)); }
193 CBOR_INLINE_API CborError cbor_encode_null(CborEncoder *encoder)
194 { return cbor_encode_simple_value(encoder, CborNullType & 0x1f); }
195 CBOR_INLINE_API CborError cbor_encode_undefined(CborEncoder *encoder)
196 { return cbor_encode_simple_value(encoder, CborUndefinedType & 0x1f); }
197 
198 CBOR_INLINE_API CborError cbor_encode_half_float(CborEncoder *encoder, const void *value)
199 { return cbor_encode_floating_point(encoder, CborHalfFloatType, value); }
200 CBOR_INLINE_API CborError cbor_encode_float(CborEncoder *encoder, float value)
201 { return cbor_encode_floating_point(encoder, CborFloatType, &value); }
202 CBOR_INLINE_API CborError cbor_encode_double(CborEncoder *encoder, double value)
203 { return cbor_encode_floating_point(encoder, CborDoubleType, &value); }
204 
205 CBOR_API CborError cbor_encoder_create_array(CborEncoder *encoder, CborEncoder *arrayEncoder, size_t length);
206 CBOR_API CborError cbor_encoder_create_map(CborEncoder *encoder, CborEncoder *mapEncoder, size_t length);
207 CBOR_API CborError cbor_encoder_close_container(CborEncoder *encoder, const CborEncoder *containerEncoder);
208 CBOR_API CborError cbor_encoder_close_container_checked(CborEncoder *encoder, const CborEncoder *containerEncoder);
209 
210 CBOR_INLINE_API size_t cbor_encoder_get_buffer_size(const CborEncoder *encoder, const uint8_t *buffer)
211 {
212  return (size_t)(encoder->data.ptr - buffer);
213 }
214 
215 CBOR_INLINE_API size_t cbor_encoder_get_extra_bytes_needed(const CborEncoder *encoder)
216 {
217  return encoder->end ? 0 : (size_t)encoder->data.bytes_needed;
218 }
219 
220 /* Parser API */
221 
222 enum CborParserIteratorFlags
223 {
224  CborIteratorFlag_IntegerValueTooLarge = 0x01,
225  CborIteratorFlag_NegativeInteger = 0x02,
226  CborIteratorFlag_UnknownLength = 0x04,
227  CborIteratorFlag_ContainerIsMap = 0x20
228 };
229 
230 struct CborParser
231 {
232  const uint8_t *end;
233  int flags;
234 };
235 typedef struct CborParser CborParser;
236 
237 struct CborValue
238 {
239  const CborParser *parser;
240  const uint8_t *ptr;
241  uint32_t remaining;
242  uint16_t extra;
243  uint8_t type;
244  uint8_t flags;
245 };
246 typedef struct CborValue CborValue;
247 
248 CBOR_API CborError cbor_parser_init(const uint8_t *buffer, size_t size, int flags, CborParser *parser, CborValue *it);
249 
250 CBOR_INLINE_API bool cbor_value_at_end(const CborValue *it)
251 { return it->remaining == 0; }
252 CBOR_INLINE_API const uint8_t *cbor_value_get_next_byte(const CborValue *it)
253 { return it->ptr; }
254 CBOR_API CborError cbor_value_advance_fixed(CborValue *it);
255 CBOR_API CborError cbor_value_advance(CborValue *it);
256 CBOR_INLINE_API bool cbor_value_is_container(const CborValue *it)
257 { return it->type == CborArrayType || it->type == CborMapType; }
258 CBOR_API CborError cbor_value_enter_container(const CborValue *it, CborValue *recursed);
259 CBOR_API CborError cbor_value_leave_container(CborValue *it, const CborValue *recursed);
260 
261 CBOR_PRIVATE_API uint64_t _cbor_value_decode_int64_internal(const CborValue *value);
262 CBOR_INLINE_API uint64_t _cbor_value_extract_int64_helper(const CborValue *value)
263 {
264  return value->flags & CborIteratorFlag_IntegerValueTooLarge ?
265  _cbor_value_decode_int64_internal(value) : value->extra;
266 }
267 
268 CBOR_INLINE_API bool cbor_value_is_valid(const CborValue *value)
269 { return value && value->type != CborInvalidType; }
270 CBOR_INLINE_API CborType cbor_value_get_type(const CborValue *value)
271 { return (CborType)value->type; }
272 
273 /* Null & undefined type */
274 CBOR_INLINE_API bool cbor_value_is_null(const CborValue *value)
275 { return value->type == CborNullType; }
276 CBOR_INLINE_API bool cbor_value_is_undefined(const CborValue *value)
277 { return value->type == CborUndefinedType; }
278 
279 /* Booleans */
280 CBOR_INLINE_API bool cbor_value_is_boolean(const CborValue *value)
281 { return value->type == CborBooleanType; }
282 CBOR_INLINE_API CborError cbor_value_get_boolean(const CborValue *value, bool *result)
283 {
284  assert(cbor_value_is_boolean(value));
285  *result = !!value->extra;
286  return CborNoError;
287 }
288 
289 /* Simple types */
290 CBOR_INLINE_API bool cbor_value_is_simple_type(const CborValue *value)
291 { return value->type == CborSimpleType; }
292 CBOR_INLINE_API CborError cbor_value_get_simple_type(const CborValue *value, uint8_t *result)
293 {
294  assert(cbor_value_is_simple_type(value));
295  *result = (uint8_t)value->extra;
296  return CborNoError;
297 }
298 
299 /* Integers */
300 CBOR_INLINE_API bool cbor_value_is_integer(const CborValue *value)
301 { return value->type == CborIntegerType; }
302 CBOR_INLINE_API bool cbor_value_is_unsigned_integer(const CborValue *value)
303 { return cbor_value_is_integer(value) && (value->flags & CborIteratorFlag_NegativeInteger) == 0; }
304 CBOR_INLINE_API bool cbor_value_is_negative_integer(const CborValue *value)
305 { return cbor_value_is_integer(value) && (value->flags & CborIteratorFlag_NegativeInteger); }
306 
307 CBOR_INLINE_API CborError cbor_value_get_raw_integer(const CborValue *value, uint64_t *result)
308 {
309  assert(cbor_value_is_integer(value));
310  *result = _cbor_value_extract_int64_helper(value);
311  return CborNoError;
312 }
313 
314 CBOR_INLINE_API CborError cbor_value_get_uint64(const CborValue *value, uint64_t *result)
315 {
316  assert(cbor_value_is_unsigned_integer(value));
317  *result = _cbor_value_extract_int64_helper(value);
318  return CborNoError;
319 }
320 
321 CBOR_INLINE_API CborError cbor_value_get_int64(const CborValue *value, int64_t *result)
322 {
323  assert(cbor_value_is_integer(value));
324  *result = (int64_t) _cbor_value_extract_int64_helper(value);
325  if (value->flags & CborIteratorFlag_NegativeInteger)
326  *result = -*result - 1;
327  return CborNoError;
328 }
329 
330 CBOR_INLINE_API CborError cbor_value_get_int(const CborValue *value, int *result)
331 {
332  assert(cbor_value_is_integer(value));
333  *result = (int) _cbor_value_extract_int64_helper(value);
334  if (value->flags & CborIteratorFlag_NegativeInteger)
335  *result = -*result - 1;
336  return CborNoError;
337 }
338 
339 CBOR_API CborError cbor_value_get_int64_checked(const CborValue *value, int64_t *result);
340 CBOR_API CborError cbor_value_get_int_checked(const CborValue *value, int *result);
341 
342 CBOR_INLINE_API bool cbor_value_is_length_known(const CborValue *value)
343 { return (value->flags & CborIteratorFlag_UnknownLength) == 0; }
344 
345 /* Tags */
346 CBOR_INLINE_API bool cbor_value_is_tag(const CborValue *value)
347 { return value->type == CborTagType; }
348 CBOR_INLINE_API CborError cbor_value_get_tag(const CborValue *value, CborTag *result)
349 {
350  assert(cbor_value_is_tag(value));
351  *result = _cbor_value_extract_int64_helper(value);
352  return CborNoError;
353 }
354 CBOR_API CborError cbor_value_skip_tag(CborValue *it);
355 
356 /* Strings */
357 CBOR_INLINE_API bool cbor_value_is_byte_string(const CborValue *value)
358 { return value->type == CborByteStringType; }
359 CBOR_INLINE_API bool cbor_value_is_text_string(const CborValue *value)
360 { return value->type == CborTextStringType; }
361 
362 CBOR_INLINE_API CborError cbor_value_get_string_length(const CborValue *value, size_t *length)
363 {
364  assert(cbor_value_is_byte_string(value) || cbor_value_is_text_string(value));
365  if (!cbor_value_is_length_known(value))
366  return CborErrorUnknownLength;
367  uint64_t v = _cbor_value_extract_int64_helper(value);
368  *length = (size_t)v;
369  if (*length != v)
370  return CborErrorDataTooLarge;
371  return CborNoError;
372 }
373 
374 CBOR_PRIVATE_API CborError _cbor_value_copy_string(const CborValue *value, void *buffer,
375  size_t *buflen, CborValue *next);
376 CBOR_PRIVATE_API CborError _cbor_value_dup_string(const CborValue *value, void **buffer,
377  size_t *buflen, CborValue *next);
378 
379 CBOR_API CborError cbor_value_calculate_string_length(const CborValue *value, size_t *length);
380 
381 CBOR_INLINE_API CborError cbor_value_copy_text_string(const CborValue *value, char *buffer,
382  size_t *buflen, CborValue *next)
383 {
384  assert(cbor_value_is_text_string(value));
385  return _cbor_value_copy_string(value, buffer, buflen, next);
386 }
387 CBOR_INLINE_API CborError cbor_value_copy_byte_string(const CborValue *value, uint8_t *buffer,
388  size_t *buflen, CborValue *next)
389 {
390  assert(cbor_value_is_byte_string(value));
391  return _cbor_value_copy_string(value, buffer, buflen, next);
392 }
393 
394 CBOR_INLINE_API CborError cbor_value_dup_text_string(const CborValue *value, char **buffer,
395  size_t *buflen, CborValue *next)
396 {
397  assert(cbor_value_is_text_string(value));
398  return _cbor_value_dup_string(value, (void **)buffer, buflen, next);
399 }
400 CBOR_INLINE_API CborError cbor_value_dup_byte_string(const CborValue *value, uint8_t **buffer,
401  size_t *buflen, CborValue *next)
402 {
403  assert(cbor_value_is_byte_string(value));
404  return _cbor_value_dup_string(value, (void **)buffer, buflen, next);
405 }
406 
407 /* ### TBD: partial reading API */
408 
409 CBOR_API CborError cbor_value_text_string_equals(const CborValue *value, const char *string, bool *result);
410 
411 /* Maps and arrays */
412 CBOR_INLINE_API bool cbor_value_is_array(const CborValue *value)
413 { return value->type == CborArrayType; }
414 CBOR_INLINE_API bool cbor_value_is_map(const CborValue *value)
415 { return value->type == CborMapType; }
416 
417 CBOR_INLINE_API CborError cbor_value_get_array_length(const CborValue *value, size_t *length)
418 {
419  assert(cbor_value_is_array(value));
420  if (!cbor_value_is_length_known(value))
421  return CborErrorUnknownLength;
422  uint64_t v = _cbor_value_extract_int64_helper(value);
423  *length = (size_t)v;
424  if (*length != v)
425  return CborErrorDataTooLarge;
426  return CborNoError;
427 }
428 
429 CBOR_INLINE_API CborError cbor_value_get_map_length(const CborValue *value, size_t *length)
430 {
431  assert(cbor_value_is_map(value));
432  if (!cbor_value_is_length_known(value))
433  return CborErrorUnknownLength;
434  uint64_t v = _cbor_value_extract_int64_helper(value);
435  *length = (size_t)v;
436  if (*length != v)
437  return CborErrorDataTooLarge;
438  return CborNoError;
439 }
440 
441 CBOR_API CborError cbor_value_map_find_value(const CborValue *map, const char *string, CborValue *element);
442 
443 /* Floating point */
444 CBOR_INLINE_API bool cbor_value_is_half_float(const CborValue *value)
445 { return value->type == CborHalfFloatType; }
446 CBOR_API CborError cbor_value_get_half_float(const CborValue *value, void *result);
447 
448 CBOR_INLINE_API bool cbor_value_is_float(const CborValue *value)
449 { return value->type == CborFloatType; }
450 CBOR_INLINE_API CborError cbor_value_get_float(const CborValue *value, float *result)
451 {
452  assert(cbor_value_is_float(value));
453  assert(value->flags & CborIteratorFlag_IntegerValueTooLarge);
454  uint32_t data = (uint32_t)_cbor_value_decode_int64_internal(value);
455  memcpy(result, &data, sizeof(*result));
456  return CborNoError;
457 }
458 
459 CBOR_INLINE_API bool cbor_value_is_double(const CborValue *value)
460 { return value->type == CborDoubleType; }
461 CBOR_INLINE_API CborError cbor_value_get_double(const CborValue *value, double *result)
462 {
463  assert(cbor_value_is_double(value));
464  assert(value->flags & CborIteratorFlag_IntegerValueTooLarge);
465  uint64_t data = _cbor_value_decode_int64_internal(value);
466  memcpy(result, &data, sizeof(*result));
467  return CborNoError;
468 }
469 
470 /* Human-readable (dump) API */
471 CBOR_API CborError cbor_value_to_pretty_advance(FILE *out, CborValue *value);
472 CBOR_INLINE_API CborError cbor_value_to_pretty(FILE *out, const CborValue *value)
473 {
474  CborValue copy = *value;
475  return cbor_value_to_pretty_advance(out, &copy);
476 }
477 
478 #ifdef __cplusplus
479 }
480 #endif
481 
482 #endif /* CBOR_H */
483 
CborType cbor_value_get_type(const CborValue *value)
Returns the type of the CBOR value that the iterator value points to.
Definition: cbor.h:270
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:314
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:282
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:300
CborError cbor_value_to_pretty(FILE *out, const CborValue *value)
Converts the current CBOR type pointed by value to its textual representation and writes it to the ou...
Definition: cbor.h:472
CborError cbor_encode_undefined(CborEncoder *encoder)
Appends the CBOR type representing an undefined value to the CBOR stream provided by encoder...
Definition: cbor.h:195
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:346
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:177
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:319
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:387
CborError cbor_encode_null(CborEncoder *encoder)
Appends the CBOR type representing a null value to the CBOR stream provided by encoder.
Definition: cbor.h:193
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:198
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:341
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:444
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:400
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:357
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:417
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:304
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:378
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:414
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:348
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:200
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:321
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:202
This type contains one value parsed from the CBOR stream.
Definition: cbor.h:237
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:357
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:429
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:459
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:505
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:202
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:450
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:509
CBOR_API CborError cbor_value_advance(CborValue *it)
Advances the CBOR value it by one element, skipping over containers.
Definition: cborparser.c:442
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:783
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:1283
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:562
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:280
CborError
The CborError enum contains the possible error values used by the CBOR encoder and decoder...
Definition: cbor.h:119
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:744
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:330
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:210
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:274
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:325
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:528
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:362
CborType
The CborType enum contains the types known to TinyCBOR.
Definition: cbor.h:80
bool cbor_value_is_valid(const CborValue *value)
Returns true if the iterator it contains a valid value.
Definition: cbor.h:268
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:256
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:290
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:186
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 store the result in result.
Definition: cborparser.c:1085
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:250
bool cbor_value_is_length_known(const CborValue *value)
Returns true if the length of this type is known without calculation.
Definition: cbor.h:342
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:412
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:394
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:252
CBOR_API CborError cbor_value_advance_fixed(CborValue *it)
Advances the CBOR value it by one fixed-size position.
Definition: cborparser.c:394
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:893
CBOR_API CborError cbor_value_to_pretty_advance(FILE *out, CborValue *value)
Converts the current CBOR type pointed by value to its textual representation and writes it to the ou...
Definition: cborpretty.c:466
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:443
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:478
Structure used to encode to CBOR.
Definition: cbor.h:165
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:215
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:400
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:276
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_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:1172
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:432
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:448
CBOR_API CborError cbor_encoder_close_container_checked(CborEncoder *encoder, const CborEncoder *containerEncoder)
Closes the CBOR container (array or map) provided by containerEncoder and updates the CBOR stream pro...
Definition: cborencoder_close_container_checked.c:60
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:307
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:486
uint64_t CborTag
This typedef is an unsigned 64-bit integer.
Definition: cbor.h:98
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:302
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:292
CborError cbor_value_copy_text_string(const CborValue *value, char *buffer, size_t *buflen, CborValue *next)
Copies the string pointed by value into the buffer provided at buffer of buflen bytes.
Definition: cbor.h:381
CborKnownTags
The CborKnownTags enum contains known tags specified in RFC 7049, for use by the application.
Definition: cbor.h:99
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:330
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:359
CborError cbor_encode_boolean(CborEncoder *encoder, bool value)
Appends the boolean value value to the CBOR stream provided by encoder.
Definition: cbor.h:191