Another way to decode data using libcbor is to specify a callbacks that will be invoked when upon finding certain items in the input. This service is provided by

struct cbor_decoder_result cbor_stream_decode(cbor_data buffer, size_t buffer_size, const struct cbor_callbacks *callbacks, void *context)

Stateless decoder.

Will try parsing the buffer and will invoke the appropriate callback on success. Decodes one item at a time. No memory allocations occur.

  • buffer: Input buffer
  • buffer_size: Length of the buffer
  • callbacks: The callback bundle
  • context: An arbitrary pointer to allow for maintaining context.

To get started, you might want to have a look at the simple streaming example:

#include "cbor.h"
#include <stdio.h>
#include <string.h>

 * Illustrates how one might skim through a map (which is assumed to have
 * string keys and values only), looking for the value of a specific key
 * Use the examples/data/map.cbor input to test this.

const char * key = "a secret key";
bool key_found = false;

void find_string(void * _ctx, cbor_data buffer, size_t len)
    if (key_found) {
        printf("Found the value: %*s\n", (int) len, buffer);
        key_found = false;
    } else if (len == strlen(key)) {
        key_found = (memcmp(key, buffer, len) == 0);

int main(int argc, char * argv[])
    FILE * f = fopen(argv[1], "rb");
    fseek(f, 0, SEEK_END);
    size_t length = (size_t)ftell(f);
    fseek(f, 0, SEEK_SET);
    unsigned char * buffer = malloc(length);
    fread(buffer, length, 1, f);

    struct cbor_callbacks callbacks = cbor_empty_callbacks;
    struct cbor_decoder_result decode_result;
    size_t bytes_read = 0;
    callbacks.string = find_string;
    while (bytes_read < length) {
        decode_result = cbor_stream_decode(buffer + bytes_read,
                                           length - bytes_read,
                                           &callbacks, NULL);
        bytes_read +=;


The callbacks are defined by

struct cbor_callbacks

Callback bundle passed to the decoder.

Public Members

cbor_int8_callback uint8

Unsigned int.

cbor_int16_callback uint16

Unsigned int.

cbor_int32_callback uint32

Unsigned int.

cbor_int64_callback uint64

Unsigned int.

cbor_int64_callback negint64

Negative int.

cbor_int32_callback negint32

Negative int.

cbor_int16_callback negint16

Negative int.

cbor_int8_callback negint8

Negative int.

cbor_simple_callback byte_string_start

Definite byte string.

cbor_string_callback byte_string

Indefinite byte string start.

cbor_string_callback string

Definite string.

cbor_simple_callback string_start

Indefinite string start.

cbor_simple_callback indef_array_start

Definite array.

cbor_collection_callback array_start

Indefinite array.

cbor_simple_callback indef_map_start

Definite map.

cbor_collection_callback map_start

Indefinite map.

cbor_int64_callback tag


cbor_float_callback float2

Half float.

cbor_float_callback float4

Single float.

cbor_double_callback float8

Double float.

cbor_simple_callback undefined


cbor_simple_callback null


cbor_bool_callback boolean


cbor_simple_callback indef_break

Indefinite item break.

When building custom sets of callbacks, feel free to start from

const struct cbor_callbacks cbor_empty_callbacks

Dummy callback bundle - does nothing.

Callback types definition

typedef void (*cbor_int8_callback)(void *, uint8_t)

Callback prototype.

typedef void (*cbor_int16_callback)(void *, uint16_t)

Callback prototype.

typedef void (*cbor_int32_callback)(void *, uint32_t)

Callback prototype.

typedef void (*cbor_int64_callback)(void *, uint64_t)

Callback prototype.

typedef void (*cbor_simple_callback)(void *)

Callback prototype.

typedef void (*cbor_string_callback)(void *, cbor_data, size_t)

Callback prototype.

typedef void (*cbor_collection_callback)(void *, size_t)

Callback prototype.

typedef void (*cbor_float_callback)(void *, float)

Callback prototype.

typedef void (*cbor_double_callback)(void *, double)

Callback prototype.

typedef void (*cbor_bool_callback)(void *, bool)

Callback prototype.