Reading CBOR

Overview

#include <minicbor.h>

static minicbor_reader_fns Callbacks = {
   .PositiveFn = ...,
   .NegativeFn = ...,
   ...
   .ErrorFn = ...
};

void example_read() {
   minicbor_reader_t Reader;
   Reader.Callbacks = Callbacks;
   Reader.UserData = ...;

   // Initialize Reader
   minicbor_reader_init(&Reader);

   // Parse each block
   unsigned char Bytes[256];
   for (;;) {
      int Count = read(Stream, Bytes, 256);
      if (Count <= 0) break;
      minicbor_read(&Reader, Bytes, Size);
   }
}

Defines

CBOR_SIMPLE_FALSE

Simple false value.

CBOR_SIMPLE_TRUE

Simple true value.

CBOR_SIMPLE_NULL

Simple null value.

CBOR_SIMPLE_UNDEF

Simple undefined value.

Types

struct minicbor_reader_t

A reader for a CBOR stream. Must be initialized with minicbor_reader_init() before each use.

minicbor_reader_fns *Callbacks
minicbor_readdata_t UserData
struct minicbor_reader_fns
void (*PositiveFn)(void *UserData, uint64_t Number)

Called when a positive integer is encountered.

void (*NegativeFn)(void *UserData, uint64_t Number)

Called when a negative integer is encountered.

void (*BytesFn)(void *UserData, int Size)

Called when a bytestring is encountered. Size is nonnegative for definite bytestrings and -1 for indefinite strings. For definite empty bytestrings, Size is 0 and BytesPieceFn() is not called. Otherwise, BytesPieceFn() will be called one or more times, with the last call having Final set to 1.

void (*BytesPieceFn)(void *UserData, void *Bytes, int Size, int Final)

Called for each piece of a bytestring. Note that pieces here do not correspond to CBOR chunks: there may be more pieces than chunks due to streaming.

void (*StringFn)(void *UserData, int Size)

Called when a string is encountered. Size is nonnegative for definite strings and -1 for indefinite strings. For definite empty strings, Size is 0 and StringPieceFn() is not called. Otherwise, StringPieceFn() will be called one or more times, with the last call having Final set to 1.

void (*StringPieceFn)(void *UserData, void *Bytes, int Size, int Final)

Called for each piece of a string. Note that pieces here do not correspond to CBOR chunks: there may be more pieces than chunks due to streaming.

void (*ArrayFn)(void *UserData, int Size)

Called when an array is encountered. Size is nonnegative for definite array and -1 for indefinite arrays.

void (*MapFn)(void *UserData, int Size)

Called when an map is encountered. Size is nonnegative for definite map and -1 for indefinite maps.

void (*TagFn)(void *UserData, uint64_t Tag)

Called when a tag is encountered.

void (*SimpleFn)(void *UserData, int Value)

Called when a simple value is encounted.

void (*FloatFn)(void *UserData, double Number)

Called when a floating point number is encountered.

void (*BreakFn)(void *UserData)

Called when a break is encountered. This is not called for breaks at the end of an indefinite bytestring or string, instead Final is set to 1 in the corresponding piece callback.

void (*ErrorFn)(void *UserData, int Position, const char *Message)

Called when an invalid CBOR sequence is detected. This puts the reader in an invalid state, any further calls will simply trigger another call ErrorFn();

Functions

void minicbor_reader_init(minicbor_reader_t *Reader)

Initializes Reader for decoding a new CBOR stream. Must be called before any call to minicbor_read(). A minicbor_reader_t can be reused by calling this function again.

int minicbor_read(minicbor_reader_t *Reader, unsigned char *Bytes, unsigned Size)

Parse some CBOR bytes and call the appropriate callbacks. Returns the 1 if minicbor_reader_finish() was called within a callback, otherwise returns 0.

void minicbor_reader_finish(minicbor_reader_t *Reader)

Set Reader state to MCS_FINISHED. Must be called from within a reader callback.

int minicbor_reader_remaining(minicbor_reader_t *Reader)

Returns the number of bytes remainining to be parsed by the reader.