48 if (source_size == 0) {
56 .
stack = &
stack, .creation_failed =
false, .syntax_error =
false};
62 if (source_size > result->
read) {
65 &callbacks, &context);
68 .position = result->
read};
72 switch (decode_result.
status) {
73 case CBOR_DECODER_FINISHED:
76 result->read += decode_result.read;
82 result->error.code = CBOR_ERR_NOTENOUGHDATA;
88 result->error.code = CBOR_ERR_MALFORMATED;
95 result->error.code = CBOR_ERR_MEMERROR;
98 result->error.code = CBOR_ERR_SYNTAXERROR;
101 }
while (stack.size > 0);
106 result->error.position = result->read;
110 while (stack.size > 0) {
111 cbor_decref(&stack.top->item);
112 _cbor_stack_pop(&stack);
158 return _cbor_copy_int(item,
false);
160 return _cbor_copy_int(item,
true);
174 if (chunk_copy == NULL) {
200 if (chunk_copy == NULL) {
226 if (entry_copy == NULL) {
253 if (key_copy == NULL) {
258 if (value_copy == NULL) {
264 .value = value_copy})) {
277 if (item_copy == NULL) {
285 return _cbor_copy_float_ctrl(item);
292#if CBOR_PRETTY_PRINTER
298#define __STDC_FORMAT_MACROS
300static int _pow(
int b,
int ex) {
301 if (ex == 0)
return 1;
303 while (--ex > 0) res *= b;
307static void _cbor_type_marquee(FILE* out,
char* label,
int indent) {
308 fprintf(out,
"%*.*s[%s] ", indent, indent,
" ", label);
311static void _cbor_nested_describe(
cbor_item_t* item, FILE* out,
int indent) {
312 const int indent_offset = 4;
315 _cbor_type_marquee(out,
"CBOR_TYPE_UINT", indent);
317 fprintf(out,
"Value: %" PRIu64
"\n",
cbor_get_int(item));
321 _cbor_type_marquee(out,
"CBOR_TYPE_NEGINT", indent);
323 fprintf(out,
"Value: -%" PRIu64
" - 1\n",
cbor_get_int(item));
327 _cbor_type_marquee(out,
"CBOR_TYPE_BYTESTRING", indent);
329 fprintf(out,
"Indefinite, Chunks: %zu, Chunk data:\n",
333 indent + indent_offset);
336 fprintf(out,
"Definite, Length: %zuB, Data:\n",
338 fprintf(out,
"%*s", indent + indent_offset,
" ");
340 fprintf(out,
"%02x", (
int)(data[i] & 0xff));
346 _cbor_type_marquee(out,
"CBOR_TYPE_STRING", indent);
348 fprintf(out,
"Indefinite, Chunks: %zu, Chunk data:\n",
352 indent + indent_offset);
354 fprintf(out,
"Definite, Length: %zuB, Codepoints: %zu, Data:\n",
356 fprintf(out,
"%*s", indent + indent_offset,
" ");
366 _cbor_type_marquee(out,
"CBOR_TYPE_ARRAY", indent);
368 fprintf(out,
"Definite, Size: %zu, Contents:\n",
cbor_array_size(item));
370 fprintf(out,
"Indefinite, Size: %zu, Contents:\n",
376 indent + indent_offset);
380 _cbor_type_marquee(out,
"CBOR_TYPE_MAP", indent);
382 fprintf(out,
"Definite, Size: %zu, Contents:\n",
cbor_map_size(item));
384 fprintf(out,
"Indefinite, Size: %zu, Contents:\n",
cbor_map_size(item));
389 fprintf(out,
"%*sMap entry %zu\n", indent + indent_offset,
" ", i);
391 indent + 2 * indent_offset);
393 indent + 2 * indent_offset);
398 _cbor_type_marquee(out,
"CBOR_TYPE_TAG", indent);
401 indent + indent_offset);
405 _cbor_type_marquee(out,
"CBOR_TYPE_FLOAT_CTRL", indent);
408 fprintf(out,
"Bool: %s\n",
cbor_get_bool(item) ?
"true" :
"false");
410 fprintf(out,
"Undefined\n");
412 fprintf(out,
"Null\n");
425 _cbor_nested_describe(item, out, 0);
size_t cbor_array_size(const cbor_item_t *item)
Get the number of members.
cbor_item_t ** cbor_array_handle(const cbor_item_t *item)
Get the array contents.
bool cbor_array_push(cbor_item_t *array, cbor_item_t *pushee)
Append to the end.
bool cbor_array_is_definite(const cbor_item_t *item)
Is the array definite?
cbor_item_t * cbor_new_definite_array(size_t size)
Create new definite array.
cbor_item_t * cbor_array_get(const cbor_item_t *item, size_t index)
Get item by index.
cbor_item_t * cbor_new_indefinite_array(void)
Create new indefinite array.
void cbor_builder_byte_string_callback(void *context, cbor_data data, uint64_t length)
void cbor_builder_uint64_callback(void *context, uint64_t value)
void cbor_builder_boolean_callback(void *context, bool value)
void cbor_builder_byte_string_start_callback(void *context)
void cbor_builder_array_start_callback(void *context, uint64_t size)
void cbor_builder_string_start_callback(void *context)
void cbor_builder_indef_map_start_callback(void *context)
void cbor_builder_negint16_callback(void *context, uint16_t value)
void cbor_builder_undefined_callback(void *context)
void cbor_builder_negint32_callback(void *context, uint32_t value)
void cbor_builder_null_callback(void *context)
void cbor_builder_map_start_callback(void *context, uint64_t size)
void cbor_builder_uint32_callback(void *context, uint32_t value)
void cbor_builder_indef_array_start_callback(void *context)
void cbor_builder_uint16_callback(void *context, uint16_t value)
void cbor_builder_negint8_callback(void *context, uint8_t value)
void cbor_builder_string_callback(void *context, cbor_data data, uint64_t length)
void cbor_builder_tag_callback(void *context, uint64_t value)
void cbor_builder_uint8_callback(void *context, uint8_t value)
void cbor_builder_float4_callback(void *context, float value)
void cbor_builder_negint64_callback(void *context, uint64_t value)
void cbor_builder_float8_callback(void *context, double value)
void cbor_builder_float2_callback(void *context, float value)
void cbor_builder_indef_break_callback(void *context)
bool cbor_bytestring_is_definite(const cbor_item_t *item)
Is the byte string definite?
bool cbor_bytestring_is_indefinite(const cbor_item_t *item)
Is the byte string indefinite?
bool cbor_bytestring_add_chunk(cbor_item_t *item, cbor_item_t *chunk)
Appends a chunk to the bytestring.
size_t cbor_bytestring_length(const cbor_item_t *item)
Returns the length of the binary data.
cbor_item_t ** cbor_bytestring_chunks_handle(const cbor_item_t *item)
Get the handle to the array of chunks.
cbor_item_t * cbor_new_indefinite_bytestring(void)
Creates a new indefinite byte string.
cbor_item_t * cbor_build_bytestring(cbor_data handle, size_t length)
Creates a new byte string and initializes it.
size_t cbor_bytestring_chunk_count(const cbor_item_t *item)
Get the number of chunks this string consist of.
unsigned char * cbor_bytestring_handle(const cbor_item_t *item)
Get the handle to the binary data.
cbor_item_t * cbor_copy(cbor_item_t *item)
Take a deep copy of an item.
cbor_item_t * cbor_load(cbor_data source, size_t source_size, struct cbor_load_result *result)
Loads data item from a buffer.
cbor_item_t * cbor_move(cbor_item_t *item)
Provides CPP-like move construct.
bool cbor_is_bool(const cbor_item_t *item)
Is the item an a boolean?
cbor_type cbor_typeof(const cbor_item_t *item)
Get the type of the item.
bool cbor_is_null(const cbor_item_t *item)
Does this item represent null
void cbor_decref(cbor_item_t **item_ref)
Decreases the item's reference count by one, deallocating the item if needed.
bool cbor_is_undef(const cbor_item_t *item)
Does this item represent undefined
#define _CBOR_UNREACHABLE
const unsigned char * cbor_data
@ CBOR_TYPE_FLOAT_CTRL
7 - decimals and special values (true, false, nil, ...)
@ CBOR_TYPE_UINT
0 - positive integers
@ CBOR_TYPE_BYTESTRING
2 - byte strings
@ CBOR_TYPE_STRING
3 - strings
@ CBOR_TYPE_NEGINT
1 - negative integers
@ CBOR_TYPE_ARRAY
4 - arrays
@ CBOR_DECODER_NEDATA
Not enough data to invoke a callback.
@ CBOR_DECODER_ERROR
Bad data (reserved MTB, malformed value, etc.)
@ CBOR_FLOAT_32
Single float.
@ CBOR_FLOAT_16
Half float.
@ CBOR_FLOAT_0
Internal use - ctrl and special values.
cbor_float_width cbor_float_get_width(const cbor_item_t *item)
Get the float width.
cbor_item_t * cbor_build_ctrl(uint8_t value)
Constructs a ctrl item.
bool cbor_float_ctrl_is_ctrl(const cbor_item_t *item)
Is this a ctrl value?
double cbor_float_get_float8(const cbor_item_t *item)
Get a double precision float.
cbor_item_t * cbor_build_float4(float value)
Constructs a new float.
uint8_t cbor_ctrl_value(const cbor_item_t *item)
Reads the control value.
float cbor_float_get_float2(const cbor_item_t *item)
Get a half precision float.
cbor_item_t * cbor_build_float8(double value)
Constructs a new float.
float cbor_float_get_float4(const cbor_item_t *item)
Get a single precision float.
double cbor_float_get_float(const cbor_item_t *item)
Get the float value represented as double.
cbor_item_t * cbor_build_float2(float value)
Constructs a new float.
bool cbor_get_bool(const cbor_item_t *item)
Get value from a boolean ctrl item.
uint32_t cbor_get_uint32(const cbor_item_t *item)
Extracts the integer value.
uint16_t cbor_get_uint16(const cbor_item_t *item)
Extracts the integer value.
uint8_t cbor_get_uint8(const cbor_item_t *item)
Extracts the integer value.
cbor_item_t * cbor_build_uint16(uint16_t value)
Constructs a new positive integer.
cbor_int_width cbor_int_get_width(const cbor_item_t *item)
Queries the integer width.
void cbor_mark_negint(cbor_item_t *item)
Marks the integer item as a negative integer.
uint64_t cbor_get_uint64(const cbor_item_t *item)
Extracts the integer value.
uint64_t cbor_get_int(const cbor_item_t *item)
Extracts the integer value.
cbor_item_t * cbor_build_uint32(uint32_t value)
Constructs a new positive integer.
cbor_item_t * cbor_build_uint8(uint8_t value)
Constructs a new positive integer.
cbor_item_t * cbor_build_uint64(uint64_t value)
Constructs a new positive integer.
bool cbor_map_is_definite(const cbor_item_t *item)
Is this map definite?
struct cbor_pair * cbor_map_handle(const cbor_item_t *item)
Get the pairs storage.
size_t cbor_map_size(const cbor_item_t *item)
Get the number of pairs.
cbor_item_t * cbor_new_indefinite_map(void)
Create a new indefinite map.
cbor_item_t * cbor_new_definite_map(size_t size)
Create a new definite map.
bool cbor_map_add(cbor_item_t *item, struct cbor_pair pair)
Add a pair to the map.
struct _cbor_stack _cbor_stack_init(void)
struct cbor_decoder_result cbor_stream_decode(cbor_data source, size_t source_size, const struct cbor_callbacks *callbacks, void *context)
Stateless decoder.
bool cbor_string_add_chunk(cbor_item_t *item, cbor_item_t *chunk)
Appends a chunk to the string.
cbor_item_t * cbor_new_indefinite_string(void)
Creates a new indefinite string.
size_t cbor_string_codepoint_count(const cbor_item_t *item)
The number of codepoints in this string.
cbor_item_t * cbor_build_stringn(const char *val, size_t length)
Creates a new string and initializes it.
size_t cbor_string_length(const cbor_item_t *item)
Returns the length of the underlying string in bytes.
bool cbor_string_is_indefinite(const cbor_item_t *item)
Is the string indefinite?
bool cbor_string_is_definite(const cbor_item_t *item)
Is the string definite?
cbor_item_t ** cbor_string_chunks_handle(const cbor_item_t *item)
Get the handle to the array of chunks.
unsigned char * cbor_string_handle(const cbor_item_t *item)
Get the handle to the underlying string.
size_t cbor_string_chunk_count(const cbor_item_t *item)
Get the number of chunks this string consist of.
High-level decoding context.
bool creation_failed
Callback creating the last item has failed.
bool syntax_error
Stack expectation mismatch.
struct _cbor_stack * stack
Stack handle - contents and size.
Callback bundle – passed to the decoder.
Streaming decoder result.
enum cbor_decoder_status status
The decoding status.
High-level decoding error.
cbor_error_code code
Description.
High-level decoding result.
size_t read
Number of bytes read.
struct cbor_error error
Error indicator.
Simple pair of items for use in maps.