Skip to content

File syn_cbor_write.c

FileList > src > syntropic > util > syn_cbor_write.c

Go to the source code of this file

CBOR streaming encoder implementation.

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

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
void emit_byte (SYN_CborWriter * w, uint8_t b)
Emit a single byte to the CBOR output.
void emit_head (SYN_CborWriter * w, uint8_t major, uint64_t val)
Encode a CBOR header (major type + argument).
void emit_raw (SYN_CborWriter * w, const void * data, size_t n)
Emit N raw bytes.

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 emit_byte

Emit a single byte to the CBOR output.

static void emit_byte (
    SYN_CborWriter * w,
    uint8_t b
) 

Parameters:

  • w CBOR writer.
  • b Byte to emit.

function emit_head

Encode a CBOR header (major type + argument).

static void emit_head (
    SYN_CborWriter * w,
    uint8_t major,
    uint64_t val
) 

Follows the CBOR additional-info rules for all major types 0-5.

Parameters:

  • w CBOR writer.
  • major Major type (0-7).
  • val Argument value.

function emit_raw

Emit N raw bytes.

static void emit_raw (
    SYN_CborWriter * w,
    const void * data,
    size_t n
) 

Parameters:

  • w CBOR writer.
  • data Source data.
  • n Number of bytes.


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