Skip to content

File syn_cbor_write.h

FileList > src > syntropic > util > syn_cbor_write.h

Go to the source code of this file

Streaming CBOR encoder — zero-alloc, caller-provided buffer. More...

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

Classes

Type Name
struct SYN_CborWriter
CBOR encoder state. Caller-allocated; zero heap.

Public Functions

Type Name
void syn_cbor_write_array_begin (SYN_CborWriter * w, size_t count)
Open a CBOR array with count items.
void syn_cbor_write_bool (SYN_CborWriter * w, bool v)
Write a boolean (0xF4 = false, 0xF5 = true).
void syn_cbor_write_bytes (SYN_CborWriter * w, const uint8_t * data, size_t len)
Write a byte string (major type 2).
void syn_cbor_write_float (SYN_CborWriter * w, float v)
Write an IEEE 754 single-precision float (major type 7, info=26).
void syn_cbor_write_int (SYN_CborWriter * w, int64_t v)
Write a signed integer.
void syn_cbor_write_map_begin (SYN_CborWriter * w, size_t count)
Open a CBOR map with count key-value pairs.
void syn_cbor_write_null (SYN_CborWriter * w)
Write a null (0xF6).
void syn_cbor_write_text (SYN_CborWriter * w, const char * str, size_t len)
Write a UTF-8 text string (major type 3).
void syn_cbor_write_text_cstr (SYN_CborWriter * w, const char * str)
Write a null-terminated UTF-8 text string.
void syn_cbor_write_uint (SYN_CborWriter * w, uint64_t v)
Write an unsigned integer.
void syn_cbor_writer_init (SYN_CborWriter * w, uint8_t * buf, size_t cap)
Initialize a CBOR writer.

Public Static Functions

Type Name
size_t syn_cbor_writer_len (const SYN_CborWriter * w)
Return bytes encoded so far.
bool syn_cbor_writer_ok (const SYN_CborWriter * w)
Return true if no overflow has occurred.

Detailed Description

Encodes CBOR (RFC 8949) items into a fixed buffer. No heap, no recursion, no indefinite-length items. Map and array sizes must be known before writing begins.

Supported types: unsigned int, signed int, float32, bool, null, text string, byte string, array, map.

** **

uint8_t buf[32];
SYN_CborWriter w;
syn_cbor_writer_init(&w, buf, sizeof(buf));
syn_cbor_write_map_begin(&w, 2);         // A2
  syn_cbor_write_uint(&w, 1);            // key 1 = temperature
  syn_cbor_write_float(&w, 23.5f);       // 23.5
  syn_cbor_write_uint(&w, 2);            // key 2 = humidity
  syn_cbor_write_uint(&w, 60);           // 60
// result: A2 01 FA41BC0000 02 183C  (10 bytes)

** **

syn_cbor_write_map_begin(&w, 1);
  syn_cbor_write_text_cstr(&w, "temp");
  syn_cbor_write_float(&w, 23.5f);

Public Functions Documentation

function syn_cbor_write_array_begin

Open a CBOR array with count items.

void syn_cbor_write_array_begin (
    SYN_CborWriter * w,
    size_t count
) 

Caller must write exactly count items after this call.

Parameters:

  • w Writer.
  • count Number of items that follow.

function syn_cbor_write_bool

Write a boolean (0xF4 = false, 0xF5 = true).

void syn_cbor_write_bool (
    SYN_CborWriter * w,
    bool v
) 

Parameters:

  • w Writer.
  • v Value.

function syn_cbor_write_bytes

Write a byte string (major type 2).

void syn_cbor_write_bytes (
    SYN_CborWriter * w,
    const uint8_t * data,
    size_t len
) 

Parameters:

  • w Writer.
  • data Pointer to raw bytes.
  • len Byte count.

function syn_cbor_write_float

Write an IEEE 754 single-precision float (major type 7, info=26).

void syn_cbor_write_float (
    SYN_CborWriter * w,
    float v
) 

Parameters:

  • w Writer.
  • v Float value.

function syn_cbor_write_int

Write a signed integer.

void syn_cbor_write_int (
    SYN_CborWriter * w,
    int64_t v
) 

Positive values are encoded as major type 0 (uint). Negative values are encoded as major type 1.

Parameters:

  • w Writer.
  • v Signed value to encode.

function syn_cbor_write_map_begin

Open a CBOR map with count key-value pairs.

void syn_cbor_write_map_begin (
    SYN_CborWriter * w,
    size_t count
) 

Caller must write exactly count pairs (key item + value item each) after this call.

Parameters:

  • w Writer.
  • count Number of key-value pairs that follow.

function syn_cbor_write_null

Write a null (0xF6).

void syn_cbor_write_null (
    SYN_CborWriter * w
) 

Parameters:

  • w Writer.

function syn_cbor_write_text

Write a UTF-8 text string (major type 3).

void syn_cbor_write_text (
    SYN_CborWriter * w,
    const char * str,
    size_t len
) 

Parameters:

  • w Writer.
  • str Pointer to string data.
  • len Byte length (not counting any null terminator).

function syn_cbor_write_text_cstr

Write a null-terminated UTF-8 text string.

void syn_cbor_write_text_cstr (
    SYN_CborWriter * w,
    const char * str
) 

Parameters:

  • w Writer.
  • str Null-terminated string.

function syn_cbor_write_uint

Write an unsigned integer.

void syn_cbor_write_uint (
    SYN_CborWriter * w,
    uint64_t v
) 

Parameters:

  • w Writer.
  • v Value to encode (CBOR major type 0).

function syn_cbor_writer_init

Initialize a CBOR writer.

void syn_cbor_writer_init (
    SYN_CborWriter * w,
    uint8_t * buf,
    size_t cap
) 

Parameters:

  • w Writer to initialize. Must not be NULL.
  • buf Output buffer. Must not be NULL.
  • cap Buffer capacity in bytes.

Public Static Functions Documentation

function syn_cbor_writer_len

Return bytes encoded so far.

static inline size_t syn_cbor_writer_len (
    const SYN_CborWriter * w
) 

Parameters:

  • w Writer.

Returns:

Byte count.


function syn_cbor_writer_ok

Return true if no overflow has occurred.

static inline bool syn_cbor_writer_ok (
    const SYN_CborWriter * w
) 

Parameters:

  • w Writer.

Returns:

true if OK.



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