Skip to content

File syn_cbor_read.h

FileList > src > syntropic > util > syn_cbor_read.h

Go to the source code of this file

CBOR decoder — zero-alloc, streaming read from a byte buffer. More...

  • #include <stdbool.h>
  • #include <stddef.h>
  • #include <stdint.h>

Classes

Type Name
struct SYN_CborReader
CBOR decoder state. Caller-allocated; zero heap.

Public Types

Type Name
enum SYN_CborType
CBOR item type as seen by the reader.

Public Functions

Type Name
SYN_CborType syn_cbor_peek_type (const SYN_CborReader * r)
Return the type of the next item without consuming it.
size_t syn_cbor_read_array_begin (SYN_CborReader * r)
Consume an array header; return the number of items.
bool syn_cbor_read_bool (SYN_CborReader * r)
Read a boolean (major type 7, info 20 or 21).
size_t syn_cbor_read_bytes (SYN_CborReader * r, uint8_t * buf, size_t cap)
Read a byte string into a caller-provided buffer.
float syn_cbor_read_float (SYN_CborReader * r)
Read a float32 value (major type 7, info=26).
int64_t syn_cbor_read_int (SYN_CborReader * r)
Read a signed integer (major type 0 or 1).
size_t syn_cbor_read_map_begin (SYN_CborReader * r)
Consume a map header; return the number of key-value pairs.
void syn_cbor_read_null (SYN_CborReader * r)
Consume a null item (major type 7, info=22).
size_t syn_cbor_read_text (SYN_CborReader * r, char * buf, size_t cap)
Read a text string into a caller-provided buffer.
uint64_t syn_cbor_read_uint (SYN_CborReader * r)
Read an unsigned integer (major type 0).
void syn_cbor_reader_init (SYN_CborReader * r, const uint8_t * buf, size_t len)
Initialize a CBOR reader.
void syn_cbor_skip (SYN_CborReader * r)
Skip the next complete item (including nested contents).

Public Static Functions

Type Name
bool syn_cbor_reader_done (const SYN_CborReader * r)
Return true when all bytes have been consumed.
bool syn_cbor_reader_ok (const SYN_CborReader * r)
Return true if no decode error has occurred.

Detailed Description

Walks a CBOR-encoded buffer sequentially. Caller peeks the type of the next item, then calls the matching read function. Reading the wrong type sets the error flag and returns a zero/empty value.

** **

SYN_CborReader r;
syn_cbor_reader_init(&r, buf, len);

size_t pairs = syn_cbor_read_map_begin(&r);   // A2 -> 2
for (size_t i = 0; i < pairs; i++) {
    uint64_t key = syn_cbor_read_uint(&r);
    if      (key == 1) temperature = syn_cbor_read_float(&r);
    else if (key == 2) humidity    = (uint8_t)syn_cbor_read_uint(&r);
    else    syn_cbor_skip(&r);
}
if (!syn_cbor_reader_ok(&r)) { // handle error }

Public Types Documentation

enum SYN_CborType

CBOR item type as seen by the reader.

enum SYN_CborType {
    SYN_CBOR_UINT = 0,
    SYN_CBOR_INT = 1,
    SYN_CBOR_BYTES = 2,
    SYN_CBOR_TEXT = 3,
    SYN_CBOR_ARRAY = 4,
    SYN_CBOR_MAP = 5,
    SYN_CBOR_FLOAT = 6,
    SYN_CBOR_BOOL = 7,
    SYN_CBOR_NULL = 8,
    SYN_CBOR_ERROR = 0xFF
};

Returned by syn_cbor_peek_type() before consuming an item.


Public Functions Documentation

function syn_cbor_peek_type

Return the type of the next item without consuming it.

SYN_CborType syn_cbor_peek_type (
    const SYN_CborReader * r
) 

Parameters:

  • r Reader.

Returns:

Item type, or SYN_CBOR_ERROR on buffer underrun.


function syn_cbor_read_array_begin

Consume an array header; return the number of items.

size_t syn_cbor_read_array_begin (
    SYN_CborReader * r
) 

Parameters:

  • r Reader.

Returns:

Item count, or 0 on error.


function syn_cbor_read_bool

Read a boolean (major type 7, info 20 or 21).

bool syn_cbor_read_bool (
    SYN_CborReader * r
) 

Parameters:

  • r Reader.

Returns:

Decoded value, or false on error.


function syn_cbor_read_bytes

Read a byte string into a caller-provided buffer.

size_t syn_cbor_read_bytes (
    SYN_CborReader * r,
    uint8_t * buf,
    size_t cap
) 

Copies at most cap bytes. The CBOR item is fully consumed.

Parameters:

  • r Reader.
  • buf Output buffer.
  • cap Output buffer capacity.

Returns:

Actual byte count in the CBOR item (may exceed cap).


function syn_cbor_read_float

Read a float32 value (major type 7, info=26).

float syn_cbor_read_float (
    SYN_CborReader * r
) 

Parameters:

  • r Reader.

Returns:

Decoded float, or 0.0f on error.


function syn_cbor_read_int

Read a signed integer (major type 0 or 1).

int64_t syn_cbor_read_int (
    SYN_CborReader * r
) 

Parameters:

  • r Reader.

Returns:

Decoded value, or 0 on error.


function syn_cbor_read_map_begin

Consume a map header; return the number of key-value pairs.

size_t syn_cbor_read_map_begin (
    SYN_CborReader * r
) 

Parameters:

  • r Reader.

Returns:

Pair count, or 0 on error.


function syn_cbor_read_null

Consume a null item (major type 7, info=22).

void syn_cbor_read_null (
    SYN_CborReader * r
) 

Parameters:

  • r Reader.

function syn_cbor_read_text

Read a text string into a caller-provided buffer.

size_t syn_cbor_read_text (
    SYN_CborReader * r,
    char * buf,
    size_t cap
) 

Copies at most cap-1 bytes and null-terminates. The CBOR item is fully consumed regardless of buffer capacity.

Parameters:

  • r Reader.
  • buf Output buffer.
  • cap Output buffer capacity (including space for null terminator).

Returns:

Actual UTF-8 byte count in the CBOR item (may exceed cap-1).


function syn_cbor_read_uint

Read an unsigned integer (major type 0).

uint64_t syn_cbor_read_uint (
    SYN_CborReader * r
) 

Parameters:

  • r Reader.

Returns:

Decoded value, or 0 on error.


function syn_cbor_reader_init

Initialize a CBOR reader.

void syn_cbor_reader_init (
    SYN_CborReader * r,
    const uint8_t * buf,
    size_t len
) 

Parameters:

  • r Reader to initialize.
  • buf Input buffer containing encoded CBOR.
  • len Buffer length in bytes.

function syn_cbor_skip

Skip the next complete item (including nested contents).

void syn_cbor_skip (
    SYN_CborReader * r
) 

Handles nested arrays and maps up to 8 levels deep.

Parameters:

  • r Reader.

Public Static Functions Documentation

function syn_cbor_reader_done

Return true when all bytes have been consumed.

static inline bool syn_cbor_reader_done (
    const SYN_CborReader * r
) 

Parameters:

  • r Reader.

Returns:

true if done.


function syn_cbor_reader_ok

Return true if no decode error has occurred.

static inline bool syn_cbor_reader_ok (
    const SYN_CborReader * r
) 

Parameters:

  • r Reader.

Returns:

true if OK.



The documentation for this class was generated from the following file src/syntropic/util/syn_cbor_read.h