Skip to content

File syn_cbor_read.c

FileList > src > syntropic > util > syn_cbor_read.c

Go to the source code of this file

CBOR decoder implementation.

  • #include "syn_cbor_read.h"
  • #include <string.h>

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
uint8_t consume_byte (SYN_CborReader * r)
Consume and return one byte from the CBOR stream.
uint64_t decode_arg (SYN_CborReader * r, uint8_t info)
Decode a CBOR argument from the bottom-5-bit info field.
uint8_t peek_byte (const SYN_CborReader * r)
Peek at the next byte without consuming it.

Macros

Type Name
define SKIP_MAX_DEPTH 8u
Maximum nesting depth for syn_cbor_skip.

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 consume_byte

Consume and return one byte from the CBOR stream.

static uint8_t consume_byte (
    SYN_CborReader * r
) 

Parameters:

  • r CBOR reader.

Returns:

Consumed byte, or 0 on underrun.


function decode_arg

Decode a CBOR argument from the bottom-5-bit info field.

static uint64_t decode_arg (
    SYN_CborReader * r,
    uint8_t info
) 

Reads the argument value associated with info. Consumes 0, 1, 2, 4, or 8 additional bytes. Sets r->ok = false for reserved info values.

Parameters:

  • r CBOR reader.
  • info Bottom 5 bits of the header byte.

Returns:

Decoded argument value.


function peek_byte

Peek at the next byte without consuming it.

static uint8_t peek_byte (
    const SYN_CborReader * r
) 

Parameters:

  • r CBOR reader.

Returns:

Next byte, or 0 if at end.


Macro Definition Documentation

define SKIP_MAX_DEPTH

Maximum nesting depth for syn_cbor_skip.

#define SKIP_MAX_DEPTH `8u`



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