Decoding¶
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
bufferand will invoke the appropriate callback on success. Decodes one item at a time. No memory allocations occur.- Parameters
buffer: Input bufferbuffer_size: Length of the buffercallbacks: The callback bundlecontext: 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 += decode_result.read;
}
free(buffer);
fclose(f);
}
The callbacks are defined by
-
struct
cbor_callbacks¶ Callback bundle passed to the decoder.
Public Members
-
cbor_int8_callback cbor_callbacks
uint8¶ Unsigned int.
-
cbor_int16_callback cbor_callbacks
uint16¶ Unsigned int.
-
cbor_int32_callback cbor_callbacks
uint32¶ Unsigned int.
-
cbor_int64_callback cbor_callbacks
uint64¶ Unsigned int.
-
cbor_int64_callback cbor_callbacks
negint64¶ Negative int.
-
cbor_int32_callback cbor_callbacks
negint32¶ Negative int.
-
cbor_int16_callback cbor_callbacks
negint16¶ Negative int.
-
cbor_int8_callback cbor_callbacks
negint8¶ Negative int.
-
cbor_simple_callback cbor_callbacks
byte_string_start¶ Definite byte string.
-
cbor_string_callback cbor_callbacks
byte_string¶ Indefinite byte string start.
-
cbor_string_callback cbor_callbacks
string¶ Definite string.
-
cbor_simple_callback cbor_callbacks
string_start¶ Indefinite string start.
-
cbor_simple_callback cbor_callbacks
indef_array_start¶ Definite array.
-
cbor_collection_callback cbor_callbacks
array_start¶ Indefinite array.
-
cbor_simple_callback cbor_callbacks
indef_map_start¶ Definite map.
-
cbor_collection_callback cbor_callbacks
map_start¶ Indefinite map.
-
cbor_int64_callback cbor_callbacks
tag¶ Tags.
-
cbor_float_callback cbor_callbacks
float2¶ Half float.
-
cbor_float_callback cbor_callbacks
float4¶ Single float.
-
cbor_double_callback cbor_callbacks
float8¶ Double float.
-
cbor_simple_callback cbor_callbacks
undefined¶ Undef.
-
cbor_simple_callback cbor_callbacks
null¶ Null.
-
cbor_bool_callback cbor_callbacks
boolean¶ Bool.
-
cbor_simple_callback cbor_callbacks
indef_break¶ Indefinite item break.
-
cbor_int8_callback cbor_callbacks
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.