SofaBuffers C/C++ 0.12.0
API Documentation
Loading...
Searching...
No Matches
C API

Data Structures

struct  sofab_istream_decoder
 Decoder context used for Sofab sequences. More...
 
struct  sofab_istream
 Internal state of the Sofab input stream. More...
 
struct  sofab_object_descr_field_t
 Description of a single field within a SofaBuffer object. More...
 
struct  sofab_object_descr
 Description of a SofaBuffer object structure. More...
 
struct  sofab_object_decoder_t
 Decoder state for a SofaBuffer object. More...
 
struct  sofab_ostream
 

Macros

#define SOFAB_ISTREAM_EXTERN   extern
 
#define SOFAB_ISTREAM_OPT_FIELDTYPE(type)   ((type) & 0x07)
 
#define SOFAB_ISTREAM_OPT_FIXLENTYPE(type)   (((type) & 0x07) << 3)
 
#define SOFAB_ISTREAM_OPT_STRINGTERM   (0x40)
 
#define SOFAB_ISTREAM_OPT_BOOLEAN   (0x80)
 
#define SOFAB_OBJECT_EXTERN   extern
 
#define SOFAB_OBJECT_FIELD(id, obj, field, type)
 Build a scalar field descriptor (sofab_object_descr_field_t).
 
#define SOFAB_OBJECT_FIELD_BLOB_SIZED(id, obj, dfield, lfield)
 Build a variable-length blob field descriptor (reuses the BLOB type).
 
#define SOFAB_OBJECT_ASSERT_FITS_PROFILE(obj, field, id)
 Compile-time check that a field fits the configured descriptor profile.
 
#define SOFAB_OBJECT_ASSERT_LEN_ADJACENT(obj, dfield, lfield)
 Compile-time check that lfield immediately precedes dfield.
 
#define SOFAB_OBJECT_ASSERT_LEN_FIRST(obj, lfield)
 Compile-time check that lfield is the first member of obj.
 
#define SOFAB_OBJECT_FIELD_SEQUENCE(id, obj, field, type, idx)
 Build a nested-object (sequence) field descriptor.
 
#define SOFAB_OBJECT_FIELD_ARRAY(id, obj, field, type)
 Build an array field descriptor.
 
#define SOFAB_OBJECT_FIELD_ARRAY_SIZED(id, obj, dfield, lfield, type)
 Build a length-carrying (sized) array field descriptor.
 
#define SOFAB_OBJECT_ASSERT_FIELD_COUNT(n)
 Compile-time limit on an OBJECT's field count (message, struct, union).
 
#define SOFAB_OBJECT_ASSERT_EXTENT(n)
 Compile-time limit on a PREFIX default image's byte extent.
 
#define SOFAB_OBJECT_ASSERT_SLOT_COUNT(n)
 Compile-time limit on a wrapper-array holder's element slot count.
 
#define SOFAB_OBJECT_DESCR_COUNT_WORD(field_count, extent)
 The sofab_object_descr_t::count_extent word of an object: the field count in the low byte, the default image's byte extent in the high byte.
 
#define SOFAB_OBJECT_DESCR(field_list, field_count, nested_list, nested_count)    { (field_list), (nested_list), NULL, SOFAB_OBJECT_DESCR_COUNT_WORD(field_count, 0u), (nested_count), 0 }
 Build an object descriptor (sofab_object_descr_t) without defaults.
 
#define SOFAB_OBJECT_DESCR_WITH_DEFAULTS(field_list, field_count, nested_list, nested_count, default_struct)
 Build an object descriptor with a default-values reference.
 
#define SOFAB_OBJECT_DESCR_WITH_PREFIX_DEFAULTS(field_list, field_count, nested_list, nested_count, default_prefix)
 Build an object descriptor whose default image is a PREFIX of the object.
 
#define SOFAB_OBJECT_DESCR_SEQ(field_list, field_count, nested_list, nested_count)
 Build a fixed-capacity sequence-holder object descriptor.
 
#define SOFAB_OBJECT_DESCR_SEQ_SIZED(field_list, field_count, nested_list, nested_count, obj, lfield)
 Build a length-carrying (sized) sequence-holder object descriptor.
 
#define SOFAB_OBJECT_DESCR_UNION(field_list, field_count, nested_list, nested_count, default_image, obj, tfield)
 Build a tagged-union object descriptor.
 
#define SOFAB_OSTREAM_EXTERN   extern
 
#define SOFAB_LAZY_SEQ_DEPTH   8
 Sofab output stream context.
 
#define SOFAB_EXTERN   extern
 
#define SOFAB_API_VERSION   1
 SofaBuffers C API version.
 
#define SOFAB_UNSIGNED_MAX   (UINT64_MAX)
 
#define SOFAB_SIGNED_MAX   (INT64_MAX)
 
#define SOFAB_SIGNED_MIN   (INT64_MIN)
 
#define SOFAB_ID_MAX   (INT32_MAX)
 Largest encodable field id (the (id<<3)|type header must fit in sofab_unsigned_t).
 
#define SOFAB_MAX_DEPTH   (UINT8_MAX)
 Maximum sequence nesting depth.
 
#define SOFAB_MIN_OUTPUT_BUFFER   1
 Smallest output buffer this port accepts for streaming (CORELIB_PLAN §5.1).
 
#define SOFAB_STRICT_UTF8   0
 Wire-construct switches — what a reduced build does on the wire.
 
#define SOFAB_SKIP_COUNTER   0
 Count fields skipped because their wire type contradicted the read bound for them (MESSAGE_SPEC §7.3).
 
#define SOFAB_OBJECT_DESCR_SMALL   1
 Narrow unsigned/signed scalar varint values from 64-bit to 32-bit.
 
#define SOFAB_OBJECT_DESCR_MEDIUM   2
 
#define SOFAB_OBJECT_DESCR_BIG   3
 
#define SOFAB_OBJECT_DESCR_PROFILE   SOFAB_OBJECT_DESCR_MEDIUM
 Selected object descriptor profile (default: MEDIUM).
 
#define SOFAB_OBJECT_DESCR_ID_MAX   (UINT16_MAX)
 
#define SOFAB_OBJECT_DESCR_OFFSET_MAX   (UINT16_MAX)
 
#define SOFAB_OBJECT_DESCR_SIZE_MAX   (UINT16_MAX)
 

Typedefs

typedef struct sofab_istream sofab_istream_t
 Opaque Sofab input stream context.
 
typedef void(* sofab_istream_field_cb_t) (sofab_istream_t *ctx, sofab_id_t id, size_t size, size_t count, void *usrptr)
 Field callback invoked whenever a new field ID is decoded.
 
typedef struct sofab_istream_decoder sofab_istream_decoder_t
 Decoder context used for Sofab sequences.
 
typedef struct sofab_object_descr sofab_object_descr_t
 Description of a SofaBuffer object structure.
 
typedef struct sofab_ostream sofab_ostream_t
 Opaque Sofab output stream context.
 
typedef void(* sofab_ostream_flush_cb_t) (sofab_ostream_t *ctx, const uint8_t *data, size_t len, void *usrptr)
 Output stream flush callback type.
 
typedef uint32_t sofab_id_t
 Field identifier type.
 
typedef uint64_t sofab_unsigned_t
 Scalar varint value types.
 
typedef int64_t sofab_signed_t
 Signed value type.
 
typedef uint16_t sofab_object_descr_id_t
 
typedef uint16_t sofab_object_descr_offset_t
 
typedef uint16_t sofab_object_descr_size_t
 
typedef char sofab_check_size_double[(sizeof(double)==8) ? 1 :-1]
 
typedef char sofab_check_size_float[(sizeof(float)==4) ? 1 :-1]
 
typedef char sofab_check_size_char_t[(sizeof(char)==1) ? 1 :-1]
 
typedef char sofab_check_size_uint8_t[(sizeof(uint8_t)==1) ? 1 :-1]
 
typedef char sofab_check_size_int8_t[(sizeof(int8_t)==1) ? 1 :-1]
 
typedef char sofab_check_size_uint16_t[(sizeof(uint16_t)==2) ? 1 :-1]
 
typedef char sofab_check_size_int16_t[(sizeof(int16_t)==2) ? 1 :-1]
 
typedef char sofab_check_size_uint32_t[(sizeof(uint32_t)==4) ? 1 :-1]
 
typedef char sofab_check_size_int32_t[(sizeof(int32_t)==4) ? 1 :-1]
 
typedef char sofab_check_size_uint64_t[(sizeof(uint64_t)==8) ? 1 :-1]
 
typedef char sofab_check_size_int64_t[(sizeof(int64_t)==8) ? 1 :-1]
 

Enumerations

enum  sofab_ret_t {
  SOFAB_RET_OK , SOFAB_RET_INCOMPLETE , SOFAB_RET_E_ARGUMENT , SOFAB_RET_E_BUFFER_FULL ,
  SOFAB_RET_E_INVALID_MSG , SOFAB_RET_E_LIMIT_EXCEEDED
}
 Return status codes. More...
 
enum  sofab_type_t {
  SOFAB_TYPE_VARINT_UNSIGNED = 0x00 , SOFAB_TYPE_VARINT_SIGNED = 0x01 , SOFAB_TYPE_FIXLEN = 0x02 , SOFAB_TYPE_VARINTARRAY_UNSIGNED = 0x03 ,
  SOFAB_TYPE_VARINTARRAY_SIGNED = 0x04 , SOFAB_TYPE_FIXLENARRAY = 0x05 , SOFAB_TYPE_SEQUENCE_START = 0x06 , SOFAB_TYPE_SEQUENCE_END = 0x07
}
 SofaBuffers 3bit field data types. More...
 
enum  sofab_fixlentype_t { SOFAB_FIXLENTYPE_FP32 = 0x00 , SOFAB_FIXLENTYPE_FP64 = 0x01 , SOFAB_FIXLENTYPE_STRING = 0x02 , SOFAB_FIXLENTYPE_BLOB = 0x03 }
 SofaBuffers 3bit fixed-length data types. More...
 

Functions

void sofab_istream_init (sofab_istream_t *ctx, sofab_istream_field_cb_t field_callback, void *usrptr)
 Initializes a Sofab input stream context.
 
sofab_ret_t sofab_istream_feed (sofab_istream_t *ctx, const void *data, size_t datalen)
 Feeds raw Sofab-encoded bytes into the input stream.
 
void sofab_istream_invalidate (sofab_istream_t *ctx)
 Reject the message in progress from within a field callback.
 
void sofab_istream_read_field (sofab_istream_t *ctx, void *var, size_t varlen, uint8_t opt)
 Number of fields skipped because their wire type contradicted the destination a field callback bound.
 
static void sofab_istream_read_i8 (sofab_istream_t *ctx, int8_t *var)
 Reads an 8-bit signed integer.
 
static void sofab_istream_read_u8 (sofab_istream_t *ctx, uint8_t *var)
 Reads an 8-bit unsigned integer.
 
static void sofab_istream_read_i16 (sofab_istream_t *ctx, int16_t *var)
 Reads a 16-bit signed integer.
 
static void sofab_istream_read_u16 (sofab_istream_t *ctx, uint16_t *var)
 Reads a 16-bit unsigned integer.
 
static void sofab_istream_read_i32 (sofab_istream_t *ctx, int32_t *var)
 Reads a 32-bit signed integer.
 
static void sofab_istream_read_u32 (sofab_istream_t *ctx, uint32_t *var)
 Reads a 32-bit unsigned integer.
 
static void sofab_istream_read_i64 (sofab_istream_t *ctx, int64_t *var)
 Reads a 64-bit signed integer.
 
static void sofab_istream_read_u64 (sofab_istream_t *ctx, uint64_t *var)
 Reads a 64-bit unsigned integer.
 
static void sofab_istream_read_bool (sofab_istream_t *ctx, bool *var)
 Reads a boolean value.
 
static void sofab_istream_read_fp32 (sofab_istream_t *ctx, float *var)
 Reads a 32-bit floating-point value.
 
static void sofab_istream_read_fp64 (sofab_istream_t *ctx, double *var)
 Reads a 64-bit floating-point value.
 
static void sofab_istream_read_string (sofab_istream_t *ctx, char *var, size_t varlen)
 Reads a string of fixed maximum size.
 
static void sofab_istream_read_string_noterm (sofab_istream_t *ctx, char *var, size_t varlen)
 Reads a string of fixed maximum size.
 
static void sofab_istream_read_blob (sofab_istream_t *ctx, void *var, size_t varlen)
 Reads a binary blob of fixed maximum size.
 
void sofab_istream_read_array (sofab_istream_t *ctx, void *var, size_t element_count, size_t element_size, uint8_t opt)
 Reads an array of elements.
 
static void sofab_istream_read_array_of_i8 (sofab_istream_t *ctx, int8_t *var, size_t element_count)
 Reads an array of 8-bit signed integers.
 
static void sofab_istream_read_array_of_u8 (sofab_istream_t *ctx, uint8_t *var, size_t element_count)
 Reads an array of 8-bit unsigned integers.
 
static void sofab_istream_read_array_of_i16 (sofab_istream_t *ctx, int16_t *var, size_t element_count)
 Reads an array of 16-bit signed integers.
 
static void sofab_istream_read_array_of_u16 (sofab_istream_t *ctx, uint16_t *var, size_t element_count)
 Reads an array of 16-bit unsigned integers.
 
static void sofab_istream_read_array_of_i32 (sofab_istream_t *ctx, int32_t *var, size_t element_count)
 Reads an array of 32-bit signed integers.
 
static void sofab_istream_read_array_of_u32 (sofab_istream_t *ctx, uint32_t *var, size_t element_count)
 Reads an array of 32-bit unsigned integers.
 
static void sofab_istream_read_array_of_bool (sofab_istream_t *ctx, bool *var, size_t element_count)
 Reads an array of booleans.
 
static void sofab_istream_read_array_of_i64 (sofab_istream_t *ctx, int64_t *var, size_t element_count)
 Reads an array of 64-bit signed integers.
 
static void sofab_istream_read_array_of_u64 (sofab_istream_t *ctx, uint64_t *var, size_t element_count)
 Reads an array of 64-bit unsigned integers.
 
static void sofab_istream_read_array_of_fp32 (sofab_istream_t *ctx, float *var, size_t element_count)
 Reads an array of 32-bit floating-point values.
 
static void sofab_istream_read_array_of_fp64 (sofab_istream_t *ctx, double *var, size_t element_count)
 Reads an array of 64-bit floating-point values.
 
void sofab_istream_read_sequence (sofab_istream_t *ctx, sofab_istream_decoder_t *decoder, sofab_istream_field_cb_t field_callback, void *usrptr)
 Begins decoding a nested Sofab sequence.
 
sofab_ret_t sofab_object_init (const sofab_object_descr_t *info, void *obj)
 Initializes an object structure with default values.
 
sofab_ret_t sofab_object_encode (sofab_ostream_t *ctx, const sofab_object_descr_t *info, const void *src)
 Encodes an object with the given descriptor into the output stream.
 
void sofab_object_field_cb (sofab_istream_t *ctx, sofab_id_t id, size_t size, size_t count, void *usrptr)
 Field callback invoked during object decoding.
 
void sofab_ostream_init (sofab_ostream_t *ctx, uint8_t *buffer, size_t buflen, size_t offset, sofab_ostream_flush_cb_t flush, void *usrptr)
 Initialize an Sofab output stream context.
 
size_t sofab_ostream_flush (sofab_ostream_t *ctx)
 Flush any pending bytes from the output stream.
 
size_t sofab_ostream_bytes_used (sofab_ostream_t *ctx)
 Get the number of bytes used in the current buffer.
 
void sofab_ostream_buffer_set (sofab_ostream_t *ctx, uint8_t *buffer, size_t buflen, size_t offset)
 Replace the active buffer used by the output stream.
 
sofab_ret_t sofab_ostream_write_unsigned (sofab_ostream_t *ctx, sofab_id_t id, sofab_unsigned_t value)
 Write an unsigned integer field.
 
sofab_ret_t sofab_ostream_write_signed (sofab_ostream_t *ctx, sofab_id_t id, sofab_signed_t value)
 Write a signed integer field.
 
sofab_ret_t sofab_ostream_write_fixlen (sofab_ostream_t *ctx, sofab_id_t id, const void *data, int32_t datalen, sofab_fixlentype_t type)
 Write a fixed-length field.
 
static sofab_ret_t sofab_ostream_write_boolean (sofab_ostream_t *ctx, sofab_id_t id, bool value)
 Write a boolean value (1 byte).
 
static sofab_ret_t sofab_ostream_write_fp32 (sofab_ostream_t *ctx, sofab_id_t id, float value)
 Write a 32-bit floating-point value.
 
static sofab_ret_t sofab_ostream_write_fp64 (sofab_ostream_t *ctx, sofab_id_t id, double value)
 Write a 64-bit floating-point value.
 
static sofab_ret_t sofab_ostream_write_string (sofab_ostream_t *ctx, sofab_id_t id, const char *text)
 Write a null-terminated string.
 
static sofab_ret_t sofab_ostream_write_blob (sofab_ostream_t *ctx, sofab_id_t id, const void *data, size_t size)
 Write a binary blob field.
 
sofab_ret_t sofab_ostream_write_array_of_unsigned (sofab_ostream_t *ctx, sofab_id_t id, const void *data, int32_t element_count, int32_t element_size)
 Write an array of unsigned integers.
 
sofab_ret_t sofab_ostream_write_array_of_signed (sofab_ostream_t *ctx, sofab_id_t id, const void *data, int32_t element_count, int32_t element_size)
 Write an array of signed integers.
 
sofab_ret_t sofab_ostream_write_array_of_fixlen (sofab_ostream_t *ctx, sofab_id_t id, const void *data, int32_t element_count, int32_t element_size, sofab_fixlentype_t type)
 Write an array of fixed-length elements.
 
static sofab_ret_t sofab_ostream_write_array_of_i8 (sofab_ostream_t *ctx, sofab_id_t id, const int8_t *var, int32_t element_count)
 Writes an array of 8-bit signed integers.
 
static sofab_ret_t sofab_ostream_write_array_of_u8 (sofab_ostream_t *ctx, sofab_id_t id, const uint8_t *var, int32_t element_count)
 Writes an array of 8-bit unsigned integers.
 
static sofab_ret_t sofab_ostream_write_array_of_i16 (sofab_ostream_t *ctx, sofab_id_t id, const int16_t *var, int32_t element_count)
 Writes an array of 16-bit signed integers.
 
static sofab_ret_t sofab_ostream_write_array_of_u16 (sofab_ostream_t *ctx, sofab_id_t id, const uint16_t *var, int32_t element_count)
 Writes an array of 16-bit unsigned integers.
 
static sofab_ret_t sofab_ostream_write_array_of_i32 (sofab_ostream_t *ctx, sofab_id_t id, const int32_t *var, int32_t element_count)
 Writes an array of 32-bit signed integers.
 
static sofab_ret_t sofab_ostream_write_array_of_u32 (sofab_ostream_t *ctx, sofab_id_t id, const uint32_t *var, int32_t element_count)
 Writes an array of 32-bit unsigned integers.
 
static sofab_ret_t sofab_ostream_write_array_of_i64 (sofab_ostream_t *ctx, sofab_id_t id, const int64_t *var, int32_t element_count)
 Writes an array of 64-bit signed integers.
 
static sofab_ret_t sofab_ostream_write_array_of_u64 (sofab_ostream_t *ctx, sofab_id_t id, const uint64_t *var, int32_t element_count)
 Writes an array of 64-bit unsigned integers.
 
static sofab_ret_t sofab_ostream_write_array_of_fp32 (sofab_ostream_t *ctx, sofab_id_t id, const float *data, int32_t element_count)
 Write an array of 32-bit floating-point values.
 
static sofab_ret_t sofab_ostream_write_array_of_fp64 (sofab_ostream_t *ctx, sofab_id_t id, const double *data, int32_t element_count)
 Write an array of 64-bit floating-point values.
 
sofab_ret_t sofab_ostream_write_sequence_begin (sofab_ostream_t *ctx, sofab_id_t id)
 Begin encoding a nested sequence (start marker).
 
sofab_ret_t sofab_ostream_write_sequence_begin_lazy (sofab_ostream_t *ctx, sofab_id_t id)
 Begin a nested sequence whose header is held back until it has content.
 
sofab_ret_t sofab_ostream_write_sequence_end_keep (sofab_ostream_t *ctx)
 End a nested sequence, keeping its frame even without content.
 
sofab_ret_t sofab_ostream_write_sequence_end (sofab_ostream_t *ctx)
 End encoding a nested sequence (end marker).
 

Object field types

Field type tags stored in sofab_object_descr_field_t::type.

Each descriptor field carries one of these tags; it selects which encode/decode path the transcoder takes for that struct member. Pass the tag as the type argument of the SOFAB_OBJECT_FIELD family of macros.

#define SOFAB_OBJECT_FIELDTYPE_UNSIGNED   0x0
 
#define SOFAB_OBJECT_FIELDTYPE_SIGNED   0x1
 
#define SOFAB_OBJECT_FIELDTYPE_FP32   0x2
 
#define SOFAB_OBJECT_FIELDTYPE_FP64   0x3
 
#define SOFAB_OBJECT_FIELDTYPE_STRING   0x4
 
#define SOFAB_OBJECT_FIELDTYPE_BLOB   0x5
 
#define SOFAB_OBJECT_FIELDTYPE_ARRAY_UNSIGNED   0x6
 
#define SOFAB_OBJECT_FIELDTYPE_ARRAY_SIGNED   0x7
 
#define SOFAB_OBJECT_FIELDTYPE_ARRAY_FP32   0x8
 
#define SOFAB_OBJECT_FIELDTYPE_ARRAY_FP64   0x9
 
#define SOFAB_OBJECT_FIELDTYPE_ARRAY_BOOLEAN   0xA
 
#define SOFAB_OBJECT_FIELDTYPE_SEQUENCE   0xB
 
#define SOFAB_OBJECT_FIELDTYPE_BOOLEAN   0xC
 

Count-word ceilings (@ref sofab_object_descr_t::count_extent)

What the 16-bit count word can describe. Generated code can hold a schema against these at compile time the way it holds a field id against SOFAB_ID_MAX, instead of discovering the ceiling as an assertion failure inside this header – which is why they are macros and not just prose.

#define SOFAB_OBJECT_DESCR_FIELD_COUNT_MAX   255u
 Fields an object (message, struct, union) may declare.
 
#define SOFAB_OBJECT_DESCR_EXTENT_MAX   255u
 Bytes a PREFIX default image may describe (SOFAB_OBJECT_DESCR_WITH_PREFIX_DEFAULTS).
 
#define SOFAB_OBJECT_DESCR_SLOT_COUNT_MAX   65535u
 Element slots a wrapper-array holder may declare (SOFAB_OBJECT_DESCR_SEQ).
 
#define SOFAB_OBJECT_DESCR_EXTENT_SHIFT   8
 Position of the extent inside the count word.
 

Wrapper-array holder marker (@ref sofab_object_descr_t::fixed_seq)

The fixed_seq slot packs two things: bit 0 flags the descriptor as a wrapper-array holder, and the remaining bits carry the byte width of the holder's companion element-count member (0 when it has none). It is the holder-level counterpart of the way nested_idx doubles as the "is sized" flag and the length width for SOFAB_OBJECT_FIELD_BLOB_SIZED / SOFAB_OBJECT_FIELD_ARRAY_SIZED — with the flag kept in bit 0 so that every plain "is this a holder?" test stays a truth test.

#define SOFAB_OBJECT_SEQ_HOLDER   0x01u
 
#define SOFAB_OBJECT_SEQ_LEN_SHIFT   1
 
#define SOFAB_OBJECT_UNION   0x80u
 

Format-wide ceilings (CORELIB_PLAN §6.2)

A declared length or element count above its ceiling is INVALID (§5.2.2), judged where the bytes that declare it are read (§5.2.3) — at the fixlen_word and at the element_count respectively.

§6.2 puts both ceilings at 2,147,483,647 and grants constrained profiles the allowance to lower them to 65,535. **This port takes that allowance exactly where the target forces it, and derives the value from SIZE_MAX rather than restating it per target.** The decoder narrows the declared length and count to size_t (istream.c), so a ceiling above SIZE_MAX would admit a value the target cannot represent and the narrowing would silently truncate it — turning an over-long field into a short or empty one and desynchronising the stream instead of rejecting it. Deriving from SIZE_MAX makes that unrepresentable: the ceiling is, by construction, never wider than what it is narrowed into. istream.c static-asserts exactly that.

The profile choice §6.2 requires be documented: 2,147,483,647 on every target whose size_t is 32-bit or wider, and 65,535 — the value §6.2 sanctions — where size_t is 16 bits (AVR).

Both are overridable (-DSOFAB_FIXLEN_MAX=... ) so a wide host can build with the constrained ceiling; that is how the lowered ceiling is exercised without a 16-bit target (test/c/test_limits.c). An override still has to fit in size_t — the static assertions hold it to that.

Note
The predecessor of this code tested SIZE_MAX == INT16_MAX. A 16-bit size_t has SIZE_MAX == UINT16_MAX (65535), never the signed 32767, so that branch was unreachable on every target and no profile ever got the constrained ceiling.
#define SOFAB_FIXLEN_MAX   (INT32_MAX)
 Maximum fixed-length field size in bytes.
 
#define SOFAB_ARRAY_MAX   (INT32_MAX)
 Maximum number of elements in an array.
 

Detailed Description

Macro Definition Documentation

◆ SOFAB_API_VERSION

#define SOFAB_API_VERSION   1

SofaBuffers C API version.

Definition at line 32 of file sofab.h.

◆ SOFAB_ARRAY_MAX

#define SOFAB_ARRAY_MAX   (INT32_MAX)

Maximum number of elements in an array.

Definition at line 192 of file sofab.h.

◆ SOFAB_EXTERN

#define SOFAB_EXTERN   extern

Definition at line 21 of file sofab.h.

◆ SOFAB_FIXLEN_MAX

#define SOFAB_FIXLEN_MAX   (INT32_MAX)

Maximum fixed-length field size in bytes.

Definition at line 183 of file sofab.h.

◆ SOFAB_ID_MAX

#define SOFAB_ID_MAX   (INT32_MAX)

Largest encodable field id (the (id<<3)|type header must fit in sofab_unsigned_t).

Definition at line 128 of file sofab.h.

◆ SOFAB_ISTREAM_EXTERN

#define SOFAB_ISTREAM_EXTERN   extern

Definition at line 36 of file istream.h.

◆ SOFAB_ISTREAM_OPT_BOOLEAN

#define SOFAB_ISTREAM_OPT_BOOLEAN   (0x80)

Store the decoded value as a boolean: value != 0, and no width bound. A boolean rides the unsigned varint wire type (CORELIB_PLAN §4.4), so this flag accompanies SOFAB_TYPE_VARINT_UNSIGNED / _VARINTARRAY_UNSIGNED.

Definition at line 68 of file istream.h.

◆ SOFAB_ISTREAM_OPT_FIELDTYPE

#define SOFAB_ISTREAM_OPT_FIELDTYPE (   type)    ((type) & 0x07)

Definition at line 62 of file istream.h.

◆ SOFAB_ISTREAM_OPT_FIXLENTYPE

#define SOFAB_ISTREAM_OPT_FIXLENTYPE (   type)    (((type) & 0x07) << 3)

Definition at line 63 of file istream.h.

◆ SOFAB_ISTREAM_OPT_STRINGTERM

#define SOFAB_ISTREAM_OPT_STRINGTERM   (0x40)

Definition at line 64 of file istream.h.

◆ SOFAB_LAZY_SEQ_DEPTH

#define SOFAB_LAZY_SEQ_DEPTH   8

Sofab output stream context.

This structure holds the state required to encode Sofab fields into a contiguous buffer. The user initializes it with sofab_ostream_init(), then repeatedly calls writer functions (sofab_ostream_write_*). The application is responsible for supplying and owning the buffer memory; the stream only holds pointers into that memory.

How many nested sequence headers sofab_ostream_write_sequence_begin_lazy() can hold back at once. Default 8.

The documented bound of this heap-free profile (CORELIB_PLAN §6, "How deep the hold-back reaches"). An implementation that can allocate MUST hold back to the full SOFAB_MAX_DEPTH and is canonical at every depth; a heap-free profile cannot, so it MAY bound the run — and MUST say where the bound is, because two encoders that disagree about it disagree about bytes.

What the bound costs, concretely:

  • At nesting depth <= SOFAB_LAZY_SEQ_DEPTH the encoder is canonical: an all-default sequence field is omitted entirely (MESSAGE_SPEC §2).
  • Deeper than that, the run is committed and the sequence is framed eagerly. If it then turns out to be all-default, the two-byte empty frame §2 would have omitted stays on the wire. That output is well-formed and decodes to the same value — it is the non-canonical form §2 already requires every decoder to accept and normalize — so a bounded and an unbounded encoder interoperate. What it is not is byte-identical to an unbounded encoder's output.

This is the same constrained-profile allowance SOFAB_FIXLEN_MAX / SOFAB_ARRAY_MAX already carry, for the same reason: a bound that costs RAM per stream is a real cost on a target that has none to spare. Raise it (-DSOFAB_LAZY_SEQ_DEPTH=N) if a schema nests sequence fields deeper than 8 and canonical output matters; every extra level costs 4 bytes of RAM per output stream.

Ceiling: 255. The run counter (sofab_ostream::npending) is a uint8_t, so a larger window could not be counted; the preprocessor check below rejects such a build rather than let the counter wrap and silently lose a pending run. 255 is already past SOFAB_MAX_DEPTH-deep sequence fields being useful, and a profile that wants an unbounded window needs a heap, which this corelib does not have.

Not a wire-format limit and not a nesting limit: nesting itself is bounded by SOFAB_MAX_DEPTH (255) and that check is unaffected — exceeding SOFAB_LAZY_SEQ_DEPTH is never an error, only a loss of canonicity.

Definition at line 139 of file ostream.h.

◆ SOFAB_MAX_DEPTH

#define SOFAB_MAX_DEPTH   (UINT8_MAX)

Maximum sequence nesting depth.

A message may nest sequences at most SOFAB_MAX_DEPTH levels deep; a decode that would exceed this bound is rejected with SOFAB_RET_E_INVALID_MSG. The limit governs the skip path — nested sequences the caller does not bind with sofab_istream_read_sequence() — where the depth counter is a uint8_t. Actively-decoded nesting is instead bounded by the number of caller-provided decoder handles (see sofab_object_decoder_t::depth).

Definition at line 206 of file sofab.h.

◆ SOFAB_MIN_OUTPUT_BUFFER

#define SOFAB_MIN_OUTPUT_BUFFER   1

Smallest output buffer this port accepts for streaming (CORELIB_PLAN §5.1).

This port declares 1. Every writer here reaches the buffer through a single byte-at-a-time push that flushes and continues when the buffer is full, so nothing has to land contiguously: a field header varint, a fixlen_word, an element_count, an integer value and an fp32/fp64 element may each be split across a flush at any byte. §5.1 calls that byte-granular case "fully conformant, and the right choice for a footprint profile" – a port declaring 1 imposes no requirement on its caller at all.

Streaming only. The minimum binds a buffer installed together with a flush callback – at sofab_ostream_init() and at every sofab_ostream_buffer_set() – which must satisfy buflen - offset >= SOFAB_MIN_OUTPUT_BUFFER. A buffer installed without a callback is subject to no minimum: no flush can occur, so no unit can be split, and the buffer either holds the message or reports SOFAB_RET_E_BUFFER_FULL. That is the case a caller sizes from a generated MAX_SIZE, and it stays exact – a message that encodes to two bytes encodes into a two-byte buffer whatever this constant says.

Not a build knob. Unlike SOFAB_LAZY_SEQ_DEPTH this states what the encoder does, not what the caller may configure: raising it would not make any write land contiguously, it would only misinform the caller. Defining it from the build is therefore an error rather than a silent redefinition.

Ceiling 20 (§5.1): a header varint and its value, 2 x 10, which is also the largest message a schema can bound to a single scalar field. Reserving further ahead must be handled by flushing, not by raising this declaration. The check below guards an edit to the value here against that ceiling.

Definition at line 241 of file sofab.h.

◆ SOFAB_OBJECT_ASSERT_EXTENT

#define SOFAB_OBJECT_ASSERT_EXTENT (   n)
Value:
(0u * sizeof(struct { int a_prefix_default_image_may_not_exceed_255_bytes \
: ((n) <= SOFAB_OBJECT_DESCR_EXTENT_MAX) ? 1 : -1; }))
#define SOFAB_OBJECT_DESCR_EXTENT_MAX
Bytes a PREFIX default image may describe (SOFAB_OBJECT_DESCR_WITH_PREFIX_DEFAULTS).
Definition object.h:293

Compile-time limit on a PREFIX default image's byte extent.

The high byte of sofab_object_descr_t::count_extent holds the extent, and an extent of 0 reads as "the image covers the whole object". A prefix longer than SOFAB_OBJECT_DESCR_EXTENT_MAX therefore has no extent that describes it: recorded as 0, every field past the prefix would be seeded and compared through the image, reading past its end. SOFAB_OBJECT_DESCR_WITH_PREFIX_DEFAULTS refuses it here instead. A whole-object image of any length is not affected – SOFAB_OBJECT_DESCR_WITH_DEFAULTS records 0 for it, which is exactly right.

Definition at line 324 of file object.h.

◆ SOFAB_OBJECT_ASSERT_FIELD_COUNT

#define SOFAB_OBJECT_ASSERT_FIELD_COUNT (   n)
Value:
(0u * sizeof(struct { int an_object_may_not_declare_more_than_255_fields \
: ((n) <= SOFAB_OBJECT_DESCR_FIELD_COUNT_MAX) ? 1 : -1; }))
#define SOFAB_OBJECT_DESCR_FIELD_COUNT_MAX
Fields an object (message, struct, union) may declare.
Definition object.h:291

Compile-time limit on an OBJECT's field count (message, struct, union).

The low byte of sofab_object_descr_t::count_extent holds it, so SOFAB_OBJECT_DESCR_FIELD_COUNT_MAX is the ceiling and a 256-field object fails to compile here instead of wrapping silently. A wrapper-array HOLDER is not affected: its slot count uses all 16 bits (SOFAB_OBJECT_ASSERT_SLOT_COUNT).

Definition at line 309 of file object.h.

◆ SOFAB_OBJECT_ASSERT_FITS_PROFILE

#define SOFAB_OBJECT_ASSERT_FITS_PROFILE (   obj,
  field,
  id 
)
Value:
(0u * sizeof(struct { int field_exceeds_the_SOFAB_OBJECT_DESCR_PROFILE_id_offset_or_size_ceiling \
: ((size_t)(sofab_object_descr_offset_t)(offsetof(obj, field)) \
== (size_t)(offsetof(obj, field)) \
&& (size_t)(sofab_object_descr_size_t)(sizeof(((obj *)0)->field)) \
== (size_t)(sizeof(((obj *)0)->field)) \
&& (size_t)(sofab_object_descr_id_t)(id) == (size_t)(id)) ? 1 : -1; }))
uint16_t sofab_object_descr_offset_t
Definition sofab.h:462
uint16_t sofab_object_descr_size_t
Definition sofab.h:463
uint16_t sofab_object_descr_id_t
Definition sofab.h:461

Compile-time check that a field fits the configured descriptor profile.

SOFAB_OBJECT_DESCR_PROFILE narrows the descriptor's id, offset and size members (SMALL: 8 bit, MEDIUM: 16, BIG: 32). A field past the resulting ceiling does not merely fail to be described – its offset is silently truncated, so the walk reads and writes the wrong member. The compiler's own -Woverflow catches the narrowing only as a warning, and only where it is enabled; this makes it an error that names the cause, at no cost to the descriptor.

The test is the narrowing itself – does the value survive the round trip through the profile's type? – not a comparison against the ceiling. A range test is tautological wherever size_t is no wider than the member (BIG on a 32-bit target), which -Wtype-limits rejects; the round trip is the same question and is never trivially true.

Definition at line 156 of file object.h.

◆ SOFAB_OBJECT_ASSERT_LEN_ADJACENT

#define SOFAB_OBJECT_ASSERT_LEN_ADJACENT (   obj,
  dfield,
  lfield 
)
Value:
(0 * sizeof(struct { int length_member_must_sit_immediately_before_the_data_member \
: (offsetof(obj, dfield) \
== offsetof(obj, lfield) + sizeof(((obj *)0)->lfield)) ? 1 : -1; }))

Compile-time check that lfield immediately precedes dfield.

Evaluates to 0, or fails to compile (negative array bound) when the two members are not adjacent — i.e. when the compiler inserted padding between them.

Used by the field-level *_SIZED macros (SOFAB_OBJECT_FIELD_BLOB_SIZED, SOFAB_OBJECT_FIELD_ARRAY_SIZED), and only by them. A field descriptor knows one address — the field's own offset inside the enclosing object — so "immediately before the storage" is the only anchor available to it, and the adjacency has to hold. An object descriptor is not in that position: it describes the whole object, so it can anchor at the object's start instead, and SOFAB_OBJECT_DESCR_SEQ_SIZED does exactly that (SOFAB_OBJECT_ASSERT_LEN_FIRST).

Definition at line 180 of file object.h.

◆ SOFAB_OBJECT_ASSERT_LEN_FIRST

#define SOFAB_OBJECT_ASSERT_LEN_FIRST (   obj,
  lfield 
)
Value:
(0 * sizeof(struct { int length_member_must_be_the_first_member_of_the_object \
: (offsetof(obj, lfield) == 0) ? 1 : -1; }))

Compile-time check that lfield is the first member of obj.

Evaluates to 0, or fails to compile (negative array bound) when lfield does not sit at offset 0. Used by SOFAB_OBJECT_DESCR_SEQ_SIZED, whose descriptor records only the count's width and locates it at offset 0 of the holder.

Definition at line 193 of file object.h.

◆ SOFAB_OBJECT_ASSERT_SLOT_COUNT

#define SOFAB_OBJECT_ASSERT_SLOT_COUNT (   n)
Value:
(0u * sizeof(struct { int a_wrapper_array_may_not_declare_more_than_65535_slots \
: ((n) <= SOFAB_OBJECT_DESCR_SLOT_COUNT_MAX) ? 1 : -1; }))
#define SOFAB_OBJECT_DESCR_SLOT_COUNT_MAX
Element slots a wrapper-array holder may declare (SOFAB_OBJECT_DESCR_SEQ).
Definition object.h:295

Compile-time limit on a wrapper-array holder's element slot count.

A holder spends all 16 bits of sofab_object_descr_t::count_extent on its slot count, so SOFAB_OBJECT_DESCR_SLOT_COUNT_MAX is the ceiling. Named here rather than left to the narrowing in the initialiser: -Woverflow reports that only where it is enabled, and a holder whose count wrapped walks a fraction of its slots and rejects every element id above them as an over-index (MESSAGE_SPEC §7.1) – a silent change of meaning, not a build failure.

Definition at line 339 of file object.h.

◆ SOFAB_OBJECT_DESCR

#define SOFAB_OBJECT_DESCR (   field_list,
  field_count,
  nested_list,
  nested_count 
)     { (field_list), (nested_list), NULL, SOFAB_OBJECT_DESCR_COUNT_WORD(field_count, 0u), (nested_count), 0 }

Build an object descriptor (sofab_object_descr_t) without defaults.

Fields whose value is all-zero are omitted from the encoding (see sofab_object_encode).

Parameters
field_listArray of sofab_object_descr_field_t for this object.
field_countNumber of entries in field_list.
nested_listArray of pointers to nested sofab_object_descr_t (may be NULL).
nested_countNumber of entries in nested_list.

Definition at line 376 of file object.h.

◆ SOFAB_OBJECT_DESCR_BIG

#define SOFAB_OBJECT_DESCR_BIG   3

uint32_t id/offset/size (max 4294967295)

Definition at line 446 of file sofab.h.

◆ SOFAB_OBJECT_DESCR_COUNT_WORD

#define SOFAB_OBJECT_DESCR_COUNT_WORD (   field_count,
  extent 
)
Value:
((uint16_t)(((field_count) + SOFAB_OBJECT_ASSERT_FIELD_COUNT(field_count)) \
| (uint16_t)(((extent) <= SOFAB_OBJECT_DESCR_EXTENT_MAX ? (extent) : 0u) \
#define SOFAB_OBJECT_DESCR_EXTENT_SHIFT
Position of the extent inside the count word.
Definition object.h:297
#define SOFAB_OBJECT_ASSERT_FIELD_COUNT(n)
Compile-time limit on an OBJECT's field count (message, struct, union).
Definition object.h:309

The sofab_object_descr_t::count_extent word of an object: the field count in the low byte, the default image's byte extent in the high byte.

An extent of 0 reads as "the image covers the whole object": what every descriptor written before the extent existed carries, what a union image uses, and what an image too long for the high byte is recorded as. That last one is right for a whole-object image – the form every generated file before the extent hands over, at any size – and wrong for a prefix, which is why a prefix goes through SOFAB_OBJECT_DESCR_WITH_PREFIX_DEFAULTS and is refused there (SOFAB_OBJECT_ASSERT_EXTENT) rather than clamped here.

Generated code tests for SOFAB_OBJECT_DESCR_WITH_PREFIX_DEFAULTS, the macro it calls, to tell a corelib that reads prefix images from one that does not – never for this helper.

Definition at line 360 of file object.h.

◆ SOFAB_OBJECT_DESCR_EXTENT_MAX

#define SOFAB_OBJECT_DESCR_EXTENT_MAX   255u

Bytes a PREFIX default image may describe (SOFAB_OBJECT_DESCR_WITH_PREFIX_DEFAULTS).

Definition at line 293 of file object.h.

◆ SOFAB_OBJECT_DESCR_EXTENT_SHIFT

#define SOFAB_OBJECT_DESCR_EXTENT_SHIFT   8

Position of the extent inside the count word.

Definition at line 297 of file object.h.

◆ SOFAB_OBJECT_DESCR_FIELD_COUNT_MAX

#define SOFAB_OBJECT_DESCR_FIELD_COUNT_MAX   255u

Fields an object (message, struct, union) may declare.

Definition at line 291 of file object.h.

◆ SOFAB_OBJECT_DESCR_ID_MAX

#define SOFAB_OBJECT_DESCR_ID_MAX   (UINT16_MAX)

Definition at line 464 of file sofab.h.

◆ SOFAB_OBJECT_DESCR_MEDIUM

#define SOFAB_OBJECT_DESCR_MEDIUM   2

uint16_t id/offset/size (max 65535)

Definition at line 445 of file sofab.h.

◆ SOFAB_OBJECT_DESCR_OFFSET_MAX

#define SOFAB_OBJECT_DESCR_OFFSET_MAX   (UINT16_MAX)

Definition at line 465 of file sofab.h.

◆ SOFAB_OBJECT_DESCR_PROFILE

#define SOFAB_OBJECT_DESCR_PROFILE   SOFAB_OBJECT_DESCR_MEDIUM

Selected object descriptor profile (default: MEDIUM).

Definition at line 450 of file sofab.h.

◆ SOFAB_OBJECT_DESCR_SEQ

#define SOFAB_OBJECT_DESCR_SEQ (   field_list,
  field_count,
  nested_list,
  nested_count 
)
Value:
{ (field_list), (nested_list), NULL, \
(uint16_t)((field_count) + SOFAB_OBJECT_ASSERT_SLOT_COUNT(field_count)), \
(nested_count), SOFAB_OBJECT_SEQ_HOLDER }
#define SOFAB_OBJECT_ASSERT_SLOT_COUNT(n)
Compile-time limit on a wrapper-array holder's element slot count.
Definition object.h:339
#define SOFAB_OBJECT_SEQ_HOLDER
Definition object.h:445

Build a fixed-capacity sequence-holder object descriptor.

Like SOFAB_OBJECT_DESCR, but marks the descriptor as a wrapper-array holder: one whose fields are exactly the element slots 0 … field_count - 1 of a bounded string / blob / struct / union array (which lowers to a wrapper sequence). For such a descriptor a wire element id outside that range is an over-index element and is rejected as SOFAB_RET_E_INVALID_MSG on decode (MESSAGE_SPEC §7/§7.1), rather than silently skipped the way a message ignores an unknown forward-compatible id.

That bound applies only to a field that survives MESSAGE_SPEC §7.3: an element header whose wire type — or, for a fixlen element type, whose fixlen subtype — contradicts the declared element type is skipped exactly as an unknown id is, so its id is not an array index at all and nothing is left for the bound to reject ("against a schema bound, this clause wins"). The element type tested against is the one all slots share, taken from slot 0.

The flag also selects the positional sparse rule of MESSAGE_SPEC §2/§5.1 on encode: inside a holder an element at an interior index equal to its default is omitted (leaving an id gap) whatever its kind, and the element at the last index is always written, because a wrapper carries no length and highest present id + 1 is what recovers it.

This macro builds the un-sized holder: it has no length member, so its field_count slots are all materialized, the last index is field_count - 1 unconditionally, and the only two lengths it can express are 0 (every slot default — the enclosing object omits the whole field) and field_count. Use SOFAB_OBJECT_DESCR_SEQ_SIZED to express 0 … N like every other target.

Parameters
field_listArray of sofab_object_descr_field_t (the element slots).
field_countNumber of element slots (the array capacity N).
nested_listArray of pointers to nested sofab_object_descr_t (may be NULL).
nested_countNumber of entries in nested_list.

Definition at line 486 of file object.h.

◆ SOFAB_OBJECT_DESCR_SEQ_SIZED

#define SOFAB_OBJECT_DESCR_SEQ_SIZED (   field_list,
  field_count,
  nested_list,
  nested_count,
  obj,
  lfield 
)
Value:
{ (field_list), (nested_list), NULL, \
(uint16_t)((field_count) + SOFAB_OBJECT_ASSERT_SLOT_COUNT(field_count)), (nested_count), \
| (sizeof(((obj *)0)->lfield) << SOFAB_OBJECT_SEQ_LEN_SHIFT) \
| 0u * sizeof(struct { int length_member_is_too_wide_to_share_fixed_seq_with_the_union_bit \
: ((sizeof(((obj *)0)->lfield) << SOFAB_OBJECT_SEQ_LEN_SHIFT) \
< SOFAB_OBJECT_UNION) ? 1 : -1; }) \
#define SOFAB_OBJECT_SEQ_LEN_SHIFT
Definition object.h:446
#define SOFAB_OBJECT_UNION
Definition object.h:447
#define SOFAB_OBJECT_ASSERT_LEN_FIRST(obj, lfield)
Compile-time check that lfield is the first member of obj.
Definition object.h:193

Build a length-carrying (sized) sequence-holder object descriptor.

The wrapper-array counterpart of SOFAB_OBJECT_FIELD_ARRAY_SIZED, and it closes the same gap one level up. An un-sized holder (SOFAB_OBJECT_DESCR_SEQ) materializes all field_count element slots and carries no length, so it can express only two lengths — 0 and N — while MESSAGE_SPEC §5.1 gives a wrapper array the length highest present id + 1, i.e. any of 0 … N. A sized holder adds the missing member: lfield holds how many elements are actually used, exactly as a sized blob's companion holds how many bytes are.

What it buys, per MESSAGE_SPEC §2/§5.1:

  • encode walks the slots [0, lfield) only — the last index is lfield - 1, not field_count - 1. An interior slot equal to its element default is omitted (id gap) whatever its kind; the slot at lfield - 1 is always written (a leaf as its value, a sequence element as an empty frame), because that is what carries the length. lfield == 0 is the empty array and the enclosing object omits the field entirely.
  • decode stores the received length — highest present element id + 1 — back into lfield, so a received [{k:1}] re-encodes as one element instead of silently growing back to N. "Present" is an element that was actually bound: one skipped because its wire type contradicts the declared element type is skipped like an unknown id (§7.3) and counts for nothing, an empty one (empty frame, empty string) is present and counts.
Warning
lfield must be the first member of obj — offsetof(obj,lfield) == 0. The descriptor stores only the count's width and reads it at offset 0 of the holder, which is where this macro requires it to be; the check is made at compile time (SOFAB_OBJECT_ASSERT_LEN_FIRST) rather than assumed.

Anchoring at the holder's start — instead of at first slot offset − width, the convention the field-level SOFAB_OBJECT_FIELD_BLOB_SIZED / SOFAB_OBJECT_FIELD_ARRAY_SIZED must keep — is what makes the count work for every element kind. The byte before slot 0 is not always free: a blob element and a native inner-array row are themselves SIZED, so each slot starts with its own used-length, and a count placed in front of the slots is read as that element's length instead. Offset 0 of the holder has no such competition and needs no padding argument either: the count may be narrower than the slots' alignment, the padding simply sits between them. That also drops the old "declare the count at least as wide as the slot's alignment" requirement.

The width sizeof(lfield) (one of 1/2/4/8) is stored in the descriptor's fixed_seq slot above the holder flag (SOFAB_OBJECT_SEQ_LEN_SHIFT); a plain SOFAB_OBJECT_DESCR_SEQ keeps that width 0 and its original full-capacity behaviour. Like SOFAB_OBJECT_DESCR_SEQ this descriptor carries no default image, so the holder's declared default is the empty array: sofab_object_init clears lfield to 0, and the enclosing ≠-default test reads "is default" as lfield == 0 (never a scan of the slots, whose content past the used length is indeterminate — the same rule a sized blob and a sized array follow).

Parameters
field_listArray of sofab_object_descr_field_t (the element slots).
field_countNumber of element slots (the array capacity N).
nested_listArray of pointers to nested sofab_object_descr_t (may be NULL).
nested_countNumber of entries in nested_list.
objThe holder struct type.
lfieldUsed-length member (in elements), declared first in obj (at offset 0), ahead of the element slots.

Definition at line 553 of file object.h.

◆ SOFAB_OBJECT_DESCR_SIZE_MAX

#define SOFAB_OBJECT_DESCR_SIZE_MAX   (UINT16_MAX)

Definition at line 466 of file sofab.h.

◆ SOFAB_OBJECT_DESCR_SLOT_COUNT_MAX

#define SOFAB_OBJECT_DESCR_SLOT_COUNT_MAX   65535u

Element slots a wrapper-array holder may declare (SOFAB_OBJECT_DESCR_SEQ).

Definition at line 295 of file object.h.

◆ SOFAB_OBJECT_DESCR_SMALL

#define SOFAB_OBJECT_DESCR_SMALL   1

Narrow unsigned/signed scalar varint values from 64-bit to 32-bit.

Changes sofab_unsigned_t / sofab_signed_t to 32-bit and removes the 64-bit integer convenience API (read_u64/read_i64, write/read array_of_u64 and array_of_i64). Useful on 32-bit MCUs where 64-bit varint math is emulated.

Warning
Change only if you know what you are doing — side effects:
  • Wire compatibility: the format itself is width-agnostic, so messages whose every value fits in 32 bits stay byte-identical and interoperable. A value beyond 32-bit range from a 64-bit peer is rejected as a malformed message (SOFAB_RET_E_INVALID_MSG) and never silently truncated.
  • ABI: the value types appear in public signatures and context structs, so 32-bit and 64-bit builds are not ABI-compatible — do not mix them.
  • Field ids: SOFAB_ID_MAX shrinks to UINT32_MAX>>3, because the field header is itself a varint of (id<<3)|type.
  • Conformance: the shipped test vectors include 64-bit values and will not decode in this mode.

Object descriptor size profiles.

The integer widths used for the sofab_object_descr_field_t members id, offset and size are selectable so the descriptor footprint can be tuned to the messages actually in use. Smaller profiles shrink every descriptor table at the cost of a lower addressable range.

offset and size both index into the described object structure, so they always share the same width; id is bounded by the highest field ID in use. All three follow the selected profile.

Profiles are ordered ascending by capacity so generated code can verify at compile time that the corelib is configured wide enough, e.g.

// require at least 16-bit descriptors
#if SOFAB_OBJECT_DESCR_PROFILE < SOFAB_OBJECT_DESCR_MEDIUM
# error "generated descriptors require at least the MEDIUM profile"
#endif
// or check concrete limits
#if MY_MESSAGE_MAX_FIELD_ID > SOFAB_OBJECT_DESCR_ID_MAX
# error "field IDs exceed the configured descriptor id width"
#endif

A field that does not fit the selected profile is a compile error naming the ceiling, not a silently truncated offset: every SOFAB_OBJECT_FIELD* macro checks its id, offset and size against the three maxima below (SOFAB_OBJECT_ASSERT_FITS_PROFILE in object.h). The check generates no code. uint8_t id/offset/size (max 255)

Definition at line 444 of file sofab.h.

◆ SOFAB_OBJECT_DESCR_UNION

#define SOFAB_OBJECT_DESCR_UNION (   field_list,
  field_count,
  nested_list,
  nested_count,
  default_image,
  obj,
  tfield 
)
Value:
{ (field_list), (nested_list), (const void *)(default_image), \
SOFAB_OBJECT_DESCR_COUNT_WORD(field_count, 0u), (nested_count), \
(uint8_t)(_SOFAB_OBJECT_UNION_BIT \
| 0u * sizeof(struct { int tag_member_must_have_the_width_of_sofab_object_descr_id_t \
: sizeof(((obj *)0)->tfield) == sizeof(sofab_object_descr_id_t) ? 1 : -1; }) \

Build a tagged-union object descriptor.

Like SOFAB_OBJECT_DESCR — one field descriptor per oneof option — but every option overlays the same storage (a C union inside obj), and the tag member tfield, a sofab_object_descr_id_t at offset 0 of obj, holds the id of the option the object currently holds. Only that option is initialised, compared against its default and encoded (MESSAGE_SPEC §4.2: a union frame carries at most one child); on decode the option received last wins, and switching to a sequence option starts it from its default.

Encode (MESSAGE_SPEC §2/§4.2). The held option is the only child the union frame can carry. When it is the default_id option it is written like an ordinary field — omitted iff it equals its default, so a union holding default_id at its default is itself default and is omitted by its parent. Any other held option is written even at its own default: a scalar as its value, a string or sized blob as the empty value, a sized array as count 0 (a fixlen array keeps its fixlen_word), and a sequence option (struct, union or wrapper-array holder) as a present, possibly empty, frame. Otherwise the receiver, whose fresh union holds default_id, could not tell that another option was selected. A union holding a non-default_id option is therefore never default, one level up included.

Decode (MESSAGE_SPEC §7.4.1). The tag switches at the point where a field is known to be bound — past the MESSAGE_SPEC §7.3 wire-type/subtype test — so a skipped child or an unknown id never switches the union. A switch to a sequence option starts it from its default (its own descriptor); the same option received again continues its scope (§7.4 merge); a leaf option is replaced whole by its payload — a scalar and a sized blob or array by their value and length, a string by its payload and terminator, an array's slots past the wire count by the element default the istream clears them to. A capacity-only blob option is the one form that could NOT be replaced whole, which is why it is not allowed (see below). Several children and re-opened frames are legal: the option received last wins.

default_image is the object's default image, or NULL. It covers the tag and the default_id option only — a prefix of obj, never a full image: the corelib reads the tag (the default_id) and, for a leaf default_id option, that option's own bytes and companion length, and nothing else. A non-default_id option is never compared (it is forced) and a sequence option is always seeded through its own descriptor. So an image type is struct { sofab_object_descr_id_t which; } when default_id names a sequence option, or struct { sofab_object_descr_id_t which; union { D; A align; } u; } for a leaf one, with offsetof(image, u) == offsetof(obj, u). NULL means tag 0 with an all-zero default_id option — exactly what an image carrying nothing but zeros would say, at no .rodata cost.

Why one image is enough (MESSAGE_SPEC §4.2 / §6): a union option of type string, blob or array may not declare a non-empty default. Init seeds only default_id; the ≠-default test only ever compares a held default_id option (any other held option is forced); a decode that switches to a sequence option re-initialises it through its own descriptor, and a leaf option is overwritten whole by its payload. No other option's default is ever needed, so the options' overlay costs nothing in correctness.

Warning
A BLOB option must carry its length (SOFAB_OBJECT_FIELD_BLOB_SIZED), never the capacity-only form SOFAB_OBJECT_FIELD(…, BLOB). A capacity-only blob has nowhere to record the length it received, so its value is always its full capacity: a peer sending fewer bytes than that (a different schema revision, or a hostile one) leaves the rest at whatever the object held, which in a union is the previously held option's payload — and encode would put it back on the wire. The sized form has no such gap, because the length says what the value is. sofab_object_init asserts it.
Selecting an option by hand is the caller writing the tag and the value — always both, for every option kind. The tag alone is never enough, not even for an option "at its default": the options overlay each other and sofab_object_init seeds only the one held, so the storage of any other option is indeterminate until the caller writes it. Encoding a string option whose buffer was never written reads past it (the encoder measures a string with strlen). For a sequence option at its default, "the value" is sofab_object_init on that option's own descriptor.

A union frame is an ordinary sequence, so this descriptor needs sequence support and carries no switch of its own: under SOFAB_DISABLE_SEQUENCE_SUPPORT the macro fails to compile rather than degrade to a plain struct, which no union survives (every overlaid option would be initialised, compared and encoded).

Parameters
field_listArray of sofab_object_descr_field_t (one per option).
field_countNumber of options.
nested_listArray of pointers to nested sofab_object_descr_t (may be NULL).
nested_countNumber of entries in nested_list.
default_imagePointer to the prefix default image, or NULL (tag 0, default_id option all-zero or a sequence).
objThe union object type.
tfieldThe tag member, a sofab_object_descr_id_t declared first in obj (at offset 0).

Definition at line 668 of file object.h.

◆ SOFAB_OBJECT_DESCR_WITH_DEFAULTS

#define SOFAB_OBJECT_DESCR_WITH_DEFAULTS (   field_list,
  field_count,
  nested_list,
  nested_count,
  default_struct 
)
Value:
{ (field_list), (nested_list), (default_struct), \
SOFAB_OBJECT_DESCR_COUNT_WORD(field_count, sizeof(*(default_struct))), (nested_count), 0 }
#define SOFAB_OBJECT_DESCR_COUNT_WORD(field_count, extent)
The sofab_object_descr_t::count_extent word of an object: the field count in the low byte,...
Definition object.h:360

Build an object descriptor with a default-values reference.

As SOFAB_OBJECT_DESCR, but fields equal to the corresponding member in default_struct are omitted from the encoding (instead of comparing against zero), and sofab_object_init seeds objects from it.

Parameters
field_listArray of sofab_object_descr_field_t for this object.
field_countNumber of entries in field_list.
nested_listArray of pointers to nested sofab_object_descr_t (may be NULL).
nested_countNumber of entries in nested_list.
default_structPointer to a fully-populated object holding the field defaults. sizeof(*default_struct) is recorded as the extent, or 0 ("the whole object") when it is longer than SOFAB_OBJECT_DESCR_EXTENT_MAX – both mean the same for a whole-object image, so this form takes one of any size, as it always has. A PREFIX of the object goes through SOFAB_OBJECT_DESCR_WITH_PREFIX_DEFAULTS: here, one longer than the extent byte would be read as the whole object and past its own end.

Definition at line 400 of file object.h.

◆ SOFAB_OBJECT_DESCR_WITH_PREFIX_DEFAULTS

#define SOFAB_OBJECT_DESCR_WITH_PREFIX_DEFAULTS (   field_list,
  field_count,
  nested_list,
  nested_count,
  default_prefix 
)
Value:
{ (field_list), (nested_list), (default_prefix), \
SOFAB_OBJECT_DESCR_COUNT_WORD(field_count, sizeof(*(default_prefix)) \
+ SOFAB_OBJECT_ASSERT_EXTENT(sizeof(*(default_prefix)))), \
(nested_count), 0 }
#define SOFAB_OBJECT_ASSERT_EXTENT(n)
Compile-time limit on a PREFIX default image's byte extent.
Definition object.h:324

Build an object descriptor whose default image is a PREFIX of the object.

As SOFAB_OBJECT_DESCR_WITH_DEFAULTS, but default_prefix need not be a whole object: any prefix of one will do, as long as every field it describes sits at the same offset as in the object itself, because that is how the fields are addressed. sizeof(*default_prefix) is recorded as the extent, and a field at or past it is seeded with and compared against zero – which is what "no declared default" means. This is what the SofaBuffers generator emits (the defaulted members first): a message that defaults one byte costs one byte of .rodata instead of a copy of the whole object.

The prefix may be at most SOFAB_OBJECT_DESCR_EXTENT_MAX bytes; a longer one is a build error (SOFAB_OBJECT_ASSERT_EXTENT), because the extent has no value that says how far it reaches. Hand over the whole object through SOFAB_OBJECT_DESCR_WITH_DEFAULTS instead.

Parameters
field_listArray of sofab_object_descr_field_t for this object.
field_countNumber of entries in field_list.
nested_listArray of pointers to nested sofab_object_descr_t (may be NULL).
nested_countNumber of entries in nested_list.
default_prefixPointer to a prefix of the object holding the field defaults.

Definition at line 427 of file object.h.

◆ SOFAB_OBJECT_EXTERN

#define SOFAB_OBJECT_EXTERN   extern

Definition at line 36 of file object.h.

◆ SOFAB_OBJECT_FIELD

#define SOFAB_OBJECT_FIELD (   id,
  obj,
  field,
  type 
)
Value:
{ id, offsetof(obj, field) + SOFAB_OBJECT_ASSERT_FITS_PROFILE(obj, field, id), \
sizeof(((obj *)0)->field), 0, type, (sizeof(((obj *)0)->field) & 0xF) }
#define SOFAB_OBJECT_ASSERT_FITS_PROFILE(obj, field, id)
Compile-time check that a field fits the configured descriptor profile.
Definition object.h:156

Build a scalar field descriptor (sofab_object_descr_field_t).

Derives the field offset and size from the struct type, so the descriptor stays in sync with the C declaration. The element size is taken as the size of the whole field (scalars are a single element).

Parameters
idField ID on the wire.
objEnclosing struct type (e.g. struct my_msg).
fieldMember name within obj.
typeField type tag (one of the scalar SOFAB_OBJECT_FIELDTYPE_*).

Definition at line 101 of file object.h.

◆ SOFAB_OBJECT_FIELD_ARRAY

#define SOFAB_OBJECT_FIELD_ARRAY (   id,
  obj,
  field,
  type 
)
Value:
{ id, offsetof(obj, field) + SOFAB_OBJECT_ASSERT_FITS_PROFILE(obj, field, id), \
sizeof(((obj *)0)->field), 0, type, (sizeof(((obj *)0)->field[0]) & 0xF) }

Build an array field descriptor.

Like SOFAB_OBJECT_FIELD but the element size is taken from a single array element (field[0]); the element count is derived at run time from the total field size divided by the element size.

Parameters
idField ID on the wire.
objEnclosing struct type.
fieldArray member within obj.
typeField type tag (one of the array SOFAB_OBJECT_FIELDTYPE_ARRAY_*).

Definition at line 226 of file object.h.

◆ SOFAB_OBJECT_FIELD_ARRAY_SIZED

#define SOFAB_OBJECT_FIELD_ARRAY_SIZED (   id,
  obj,
  dfield,
  lfield,
  type 
)
Value:
{ id, offsetof(obj, dfield) + SOFAB_OBJECT_ASSERT_FITS_PROFILE(obj, dfield, id), \
sizeof(((obj *)0)->dfield), \
(uint8_t)(sizeof(((obj *)0)->lfield) \
+ SOFAB_OBJECT_ASSERT_LEN_ADJACENT(obj, dfield, lfield)), \
type, (sizeof(((obj *)0)->dfield[0]) & 0xF) }
#define SOFAB_OBJECT_ASSERT_LEN_ADJACENT(obj, dfield, lfield)
Compile-time check that lfield immediately precedes dfield.
Definition object.h:180

Build a length-carrying (sized) array field descriptor.

A plain SOFAB_OBJECT_FIELD_ARRAY has nowhere to keep a length: it derives the element count from sizeof(field)/sizeof(field[0]), which is the array's capacity. MESSAGE_SPEC §3 makes the schema count a capacity too and the wire count M the array's length, with no element elided — so a capacity- only object can never hold an array shorter than N, and a decode of M < N followed by a re-encode silently lengthens the value back to N. A sized array fixes that the same way SOFAB_OBJECT_FIELD_BLOB_SIZED fixes it for a blob: it pairs the fixed-capacity array dfield with a companion length member lfield holding how many elements are actually used (0..N). On encode exactly lfield elements reach the wire — a trailing element equal to the element default included, because M is the length; on decode the received count is stored back into lfield. The capacity never reaches the wire.

Warning
lfield must immediately precede dfield in obj, and the two must be adjacent — offsetof(obj,dfield) == offsetof(obj,lfield)+sizeof(lfield). For a sized blob that is automatic: a byte buffer has alignment 1, so it abuts any length width. It is not automatic here. An element wider than a byte carries a stricter alignment, so a narrower length in front of it is padded away — { uint8_t len; uint32_t vals[4]; } places vals at offset 4, three bytes past the length, and the descriptor (which stores only the length's width and reads it at offset - width) would address the padding instead. This macro therefore does not assume the invariant, it asserts it: the adjacency is checked at compile time (SOFAB_OBJECT_ASSERT_LEN_ADJACENT), so a padded pair is a build error rather than a silent misread. Declare the length at least as wide as one element — { uint32_t len; uint32_t vals[4]; } — or otherwise arrange the two members to leave no gap.

The companion width sizeof(lfield) (one of 1/2/4/8) is stored in the descriptor's nested_idx slot, which also flags the array as sized: a plain SOFAB_OBJECT_FIELD_ARRAY keeps nested_idx == 0 and encodes its full capacity.

Parameters
idField ID on the wire.
objEnclosing struct type.
dfieldArray member within obj (its size is the capacity).
lfieldUsed-length member (in elements), declared immediately before dfield.
typeField type tag (one of the array SOFAB_OBJECT_FIELDTYPE_ARRAY_*).

Definition at line 274 of file object.h.

◆ SOFAB_OBJECT_FIELD_BLOB_SIZED

#define SOFAB_OBJECT_FIELD_BLOB_SIZED (   id,
  obj,
  dfield,
  lfield 
)
Value:
{ id, offsetof(obj, dfield) + SOFAB_OBJECT_ASSERT_FITS_PROFILE(obj, dfield, id), \
sizeof(((obj *)0)->dfield), \
(uint8_t)(sizeof(((obj *)0)->lfield) \
+ SOFAB_OBJECT_ASSERT_LEN_ADJACENT(obj, dfield, lfield)), \
(sizeof(((obj *)0)->dfield) & 0xF) }
#define SOFAB_OBJECT_FIELDTYPE_BLOB
Definition object.h:62

Build a variable-length blob field descriptor (reuses the BLOB type).

A sized blob pairs a fixed-capacity buffer dfield (sizeof(dfield) bytes) with a companion length member lfield holding how many bytes are actually used (0..sizeof(dfield)). On encode only lfield bytes reach the wire; on decode the received length is stored back into lfield. The buffer capacity never reaches the wire. This is the C counterpart of C++ sofab::FixedBytes, and it produces byte-identical wire to a plain blob of the same actual length.

Warning
lfield must immediately precede dfield in obj, i.e. offsetof(obj,dfield) == offsetof(obj,lfield)+sizeof(lfield); declare them adjacently as { uintX lfield; uint8_t dfield[N]; }. Placing the length before the buffer is alignment-robust: a byte buffer (alignment 1) always abuts the length with no padding, for any lfield width and any N (a length placed after the buffer could be padded away from offset+size). The companion width sizeof(lfield) (one of 1/2/4/8) is stored in the descriptor's nested_idx slot, which also flags the blob as sized: a plain SOFAB_OBJECT_FIELD blob keeps nested_idx == 0 and its original fixed full-capacity behaviour.
Parameters
idField ID on the wire.
objEnclosing struct type.
dfieldBlob buffer member within obj (its size is the capacity).
lfieldUsed-length member, declared immediately before dfield.

Definition at line 131 of file object.h.

◆ SOFAB_OBJECT_FIELD_SEQUENCE

#define SOFAB_OBJECT_FIELD_SEQUENCE (   id,
  obj,
  field,
  type,
  idx 
)
Value:
{ id, offsetof(obj, field) + SOFAB_OBJECT_ASSERT_FITS_PROFILE(obj, field, id), \
sizeof(((obj *)0)->field), idx, type, (sizeof(((obj *)0)->field) & 0xF) }

Build a nested-object (sequence) field descriptor.

Like SOFAB_OBJECT_FIELD but for a nested struct serialized as a sequence. idx selects the child descriptor from the enclosing object's nested_list.

Parameters
idField ID on the wire.
objEnclosing struct type.
fieldNested struct member within obj.
typeField type tag (typically SOFAB_OBJECT_FIELDTYPE_SEQUENCE).
idxIndex of the nested descriptor in the nested_list.

Definition at line 210 of file object.h.

◆ SOFAB_OBJECT_FIELDTYPE_ARRAY_BOOLEAN

#define SOFAB_OBJECT_FIELDTYPE_ARRAY_BOOLEAN   0xA

Array of booleans (sized variant as above); each element follows the §4.4 rule.

Definition at line 67 of file object.h.

◆ SOFAB_OBJECT_FIELDTYPE_ARRAY_FP32

#define SOFAB_OBJECT_FIELDTYPE_ARRAY_FP32   0x8

Array of 32-bit floats (sized variant as above).

Definition at line 65 of file object.h.

◆ SOFAB_OBJECT_FIELDTYPE_ARRAY_FP64

#define SOFAB_OBJECT_FIELDTYPE_ARRAY_FP64   0x9

Array of 64-bit doubles (sized variant as above).

Definition at line 66 of file object.h.

◆ SOFAB_OBJECT_FIELDTYPE_ARRAY_SIGNED

#define SOFAB_OBJECT_FIELDTYPE_ARRAY_SIGNED   0x7

Array of signed integers (sized variant as above).

Definition at line 64 of file object.h.

◆ SOFAB_OBJECT_FIELDTYPE_ARRAY_UNSIGNED

#define SOFAB_OBJECT_FIELDTYPE_ARRAY_UNSIGNED   0x6

Array of unsigned integers. Fixed full-capacity by default; a length-carrying variant is built with SOFAB_OBJECT_FIELD_ARRAY_SIZED (flagged via a non-zero nested_idx).

Definition at line 63 of file object.h.

◆ SOFAB_OBJECT_FIELDTYPE_BLOB

#define SOFAB_OBJECT_FIELDTYPE_BLOB   0x5

Raw binary blob. Fixed full-capacity by default; a variable used-length variant is built with SOFAB_OBJECT_FIELD_BLOB_SIZED (flagged via a non-zero nested_idx).

Definition at line 62 of file object.h.

◆ SOFAB_OBJECT_FIELDTYPE_BOOLEAN

#define SOFAB_OBJECT_FIELDTYPE_BOOLEAN   0xC

Boolean. Rides the unsigned varint wire type, but every non-zero value decodes to true and is normalized (CORELIB_PLAN §4.4).

Definition at line 69 of file object.h.

◆ SOFAB_OBJECT_FIELDTYPE_FP32

#define SOFAB_OBJECT_FIELDTYPE_FP32   0x2

32-bit floating point (float).

Definition at line 59 of file object.h.

◆ SOFAB_OBJECT_FIELDTYPE_FP64

#define SOFAB_OBJECT_FIELDTYPE_FP64   0x3

64-bit floating point (double).

Definition at line 60 of file object.h.

◆ SOFAB_OBJECT_FIELDTYPE_SEQUENCE

#define SOFAB_OBJECT_FIELDTYPE_SEQUENCE   0xB

Nested object (encoded as a sequence).

Definition at line 68 of file object.h.

◆ SOFAB_OBJECT_FIELDTYPE_SIGNED

#define SOFAB_OBJECT_FIELDTYPE_SIGNED   0x1

Signed integer (int8/16/32/64).

Definition at line 58 of file object.h.

◆ SOFAB_OBJECT_FIELDTYPE_STRING

#define SOFAB_OBJECT_FIELDTYPE_STRING   0x4

Null-terminated string.

Definition at line 61 of file object.h.

◆ SOFAB_OBJECT_FIELDTYPE_UNSIGNED

#define SOFAB_OBJECT_FIELDTYPE_UNSIGNED   0x0

Unsigned integer (uint8/16/32/64).

Definition at line 57 of file object.h.

◆ SOFAB_OBJECT_SEQ_HOLDER

#define SOFAB_OBJECT_SEQ_HOLDER   0x01u

Bit 0: this descriptor is a wrapper-array holder.

Definition at line 445 of file object.h.

◆ SOFAB_OBJECT_SEQ_LEN_SHIFT

#define SOFAB_OBJECT_SEQ_LEN_SHIFT   1

Shift of the length member's byte width (0 = unsized).

Definition at line 446 of file object.h.

◆ SOFAB_OBJECT_UNION

#define SOFAB_OBJECT_UNION   0x80u

Bit 7: a union; its tag is a sofab_object_descr_id_t at offset 0.

Definition at line 447 of file object.h.

◆ SOFAB_OSTREAM_EXTERN

#define SOFAB_OSTREAM_EXTERN   extern

Definition at line 45 of file ostream.h.

◆ SOFAB_SIGNED_MAX

#define SOFAB_SIGNED_MAX   (INT64_MAX)

Definition at line 123 of file sofab.h.

◆ SOFAB_SIGNED_MIN

#define SOFAB_SIGNED_MIN   (INT64_MIN)

Definition at line 124 of file sofab.h.

◆ SOFAB_SKIP_COUNTER

#define SOFAB_SKIP_COUNTER   0

Count fields skipped because their wire type contradicted the read bound for them (MESSAGE_SPEC §7.3).

Such a skip is normal traffic, not an error: it is how a reader and a writer built from different revisions of a schema keep talking. The counter exists only so an application can notice it — a non-zero count after a successful decode means the two sides disagree about what an id means, which belongs in a log line or a health metric. Nothing in the decode path reads it.

Being a pure diagnostic, it follows the same opt-in style as SOFAB_STRICT_UTF8 and defaults OFF for this footprint corelib. Measured cost when ON: 16–24 bytes of .text (the saturating increment plus sofab_istream_skipped), and no .data / .bss — the counter shares the alignment padding the context already had. Enable it by defining SOFAB_ENABLE_SKIP_COUNTER; a direct -DSOFAB_SKIP_COUNTER=1 also works and wins. The resolved boolean SOFAB_SKIP_COUNTER is what the corelib and the C++ wrapper gate on.

Definition at line 387 of file sofab.h.

◆ SOFAB_STRICT_UTF8

#define SOFAB_STRICT_UTF8   0

Wire-construct switches — what a reduced build does on the wire.

Warning
The SOFAB_DISABLE_*_SUPPORT switches below remove a wire construct, not merely the ability to store one. A decoder built without a construct rejects any message that carries it with SOFAB_RET_E_INVALID_MSG — terminally, per sofab_istream_feed — even where the field would otherwise have been skipped: an unknown id, a field the callback declined, or a field inside a sequence nobody bound. The decoder is schema-agnostic and cannot tell those apart from a field the caller wanted.

The consequence is an interop bound, and it is the caller's to manage: such a build only talks to peers that never emit the removed construct, in any field, ever — including fields added by a later revision of the schema. That rules out the usual forward-compatibility story, where a reader ignores ids it does not know (MESSAGE_SPEC §7.3 describes the skip a full build performs). Enable these when both ends of the link are yours and the wire profile is fixed; do not enable them on a receiver exposed to messages you do not control.

Nothing is silently dropped either way: the message is rejected, never half-accepted. But note that fields preceding the offending one have already been delivered to the callback when the rejection lands — sofab_istream_feed documents what the caller must do about that.

Disable support for 64-bit floating point types (if the check above fails). Rejects any message carrying an fp64 fixlen subtype — see the warning above.

Disable support for fixed-length fields (floating point types, strings, and blobs). Rejects any message carrying a fixlen or fixlen-array field — see the warning above.

Disable support for array fields. Rejects any message carrying an array field — see the warning above.

Disable support for sequence fields. Rejects any message carrying a sequence — see the warning above.

Disable integer overflow checks when reading integer values.

Disable the encode-side bound refusal.

By default sofab_object_encode reads a STRING field bounded by its own storage (char[maxlen + 1]) and refuses, with SOFAB_RET_E_ARGUMENT, a value that fills that storage without a terminator: it is longer than the field's maxlen. Defining this switch removes the refusal (a compare and a branch per string field) for a caller that guarantees its strings are terminated within their bound; the read stays bounded, so such a value is emitted as the whole buffer instead of read past it. The C++ wrapper reads the same switch as sofab::ENCODE_BOUNDS. A sized blob's length and a sized array's or wrapper holder's element count are clamped to the capacity in either build (the documented clamp contract), and decode-side bound checks are not affected.

Strict UTF-8 validation of string fields (default OFF — opt-in).

A string payload is UTF-8 (MESSAGE_SPEC §8); blob is the type for opaque bytes. With strict validation enabled a string whose bytes are not valid UTF-8 is rejected symmetrically (CORELIB_PLAN §6.4):

  • decode: a materialized (read, not skipped) string with invalid bytes makes the message the SOFAB_RET_E_INVALID_MSG outcome — the same terminal class as any other malformed message. Skipped string fields are never validated (wire validity of unread content is the producer's responsibility). Cross-chunk: a multi-byte sequence split at end-of-chunk stays SOFAB_RET_INCOMPLETE; only a sequence still ill-formed once the complete declared payload has arrived is INVALID.
  • encode: a string value that is not valid UTF-8 is refused with SOFAB_RET_E_ARGUMENT (sofab_ostream_write_fixlen with the SOFAB_FIXLENTYPE_STRING subtype). blob bytes are never validated.

This is a validation policy, never a wire-format switch: it only decides accept-vs-reject and never changes how valid data is encoded, so peers with different settings interoperate on all valid data. It is never lossy — an invalid string is rejected, never silently replaced with U+FFFD or truncated, in either direction and in either state.

This is a footprint/embedded corelib (the c and c-cpp targets), so the check defaults OFF and is opt-in — the constrained-profile allowance in CORELIB_PLAN §6.4 (a documented non-strict build; zero .text / .rodata cost when OFF, since utf8.c and every gated site compile to nothing). With the check OFF the wire bytes are stored verbatim (byte-container behavior), never lossy — an invalid string is never silently replaced or dropped, only accepted verbatim.

The check follows the corelib's opt-in style: it is enabled by defining SOFAB_ENABLE_STRICT_UTF8 (conformance/fuzzer/CI builds do this). A direct -DSOFAB_STRICT_UTF8=1 also works and wins over both knobs. The legacy SOFAB_DISABLE_STRICT_UTF8 opt-out is still honored — it forces the check OFF, which is now also the default, so it is harmless. The resolved boolean SOFAB_STRICT_UTF8 (1 when on, 0 when off) is what the corelib and generated code gate on, so flipping the flag never requires regenerating code.

Definition at line 359 of file sofab.h.

◆ SOFAB_UNSIGNED_MAX

#define SOFAB_UNSIGNED_MAX   (UINT64_MAX)

Definition at line 119 of file sofab.h.

Typedef Documentation

◆ sofab_check_size_char_t

typedef char sofab_check_size_char_t[(sizeof(char)==1) ? 1 :-1]

Definition at line 484 of file sofab.h.

◆ sofab_check_size_double

typedef char sofab_check_size_double[(sizeof(double)==8) ? 1 :-1]

Definition at line 480 of file sofab.h.

◆ sofab_check_size_float

typedef char sofab_check_size_float[(sizeof(float)==4) ? 1 :-1]

Definition at line 483 of file sofab.h.

◆ sofab_check_size_int16_t

typedef char sofab_check_size_int16_t[(sizeof(int16_t)==2) ? 1 :-1]

Definition at line 488 of file sofab.h.

◆ sofab_check_size_int32_t

typedef char sofab_check_size_int32_t[(sizeof(int32_t)==4) ? 1 :-1]

Definition at line 490 of file sofab.h.

◆ sofab_check_size_int64_t

typedef char sofab_check_size_int64_t[(sizeof(int64_t)==8) ? 1 :-1]

Definition at line 492 of file sofab.h.

◆ sofab_check_size_int8_t

typedef char sofab_check_size_int8_t[(sizeof(int8_t)==1) ? 1 :-1]

Definition at line 486 of file sofab.h.

◆ sofab_check_size_uint16_t

typedef char sofab_check_size_uint16_t[(sizeof(uint16_t)==2) ? 1 :-1]

Definition at line 487 of file sofab.h.

◆ sofab_check_size_uint32_t

typedef char sofab_check_size_uint32_t[(sizeof(uint32_t)==4) ? 1 :-1]

Definition at line 489 of file sofab.h.

◆ sofab_check_size_uint64_t

typedef char sofab_check_size_uint64_t[(sizeof(uint64_t)==8) ? 1 :-1]

Definition at line 491 of file sofab.h.

◆ sofab_check_size_uint8_t

typedef char sofab_check_size_uint8_t[(sizeof(uint8_t)==1) ? 1 :-1]

Definition at line 485 of file sofab.h.

◆ sofab_id_t

typedef uint32_t sofab_id_t

Field identifier type.

Definition at line 106 of file sofab.h.

◆ sofab_istream_decoder_t

Decoder context used for Sofab sequences.

Each sequence has its own decoder instance. Decoders form a parent-child chain and inherit the decoding state related to sequence depth and skip levels.

◆ sofab_istream_field_cb_t

typedef void(* sofab_istream_field_cb_t) (sofab_istream_t *ctx, sofab_id_t id, size_t size, size_t count, void *usrptr)

Field callback invoked whenever a new field ID is decoded.

The callback is triggered once the decoder has determined a complete field ID. The user must then call the appropriate sofab_istream_read_* function to bind a destination buffer or initiate a nested sequence. If no read function is called, the field will be skipped.

Binding a read whose type contradicts the field on the wire skips the field too (MESSAGE_SPEC §7.3): the destination is left untouched and decoding continues, since an id that means different things to the two peers is a disagreement between them, not a malformed message. sofab_istream_skipped counts those skips.

Example usage:

void my_field_cb(sofab_istream_t *ctx, sofab_id_t id,
size_t size, size_t count, void *usrptr) {
switch(id) {
case FIELD_TEMP: sofab_istream_read_fp32(ctx, &state.temperature); break;
case FIELD_NAME: sofab_istream_read_string(ctx, buffer, sizeof(buffer)); break;
case FIELD_CHILD: sofab_istream_read_sequence(ctx, &child_decoder, child_cb, NULL); break;
}
}
static void sofab_istream_read_string(sofab_istream_t *ctx, char *var, size_t varlen)
Reads a string of fixed maximum size.
Definition istream.h:471
void sofab_istream_read_sequence(sofab_istream_t *ctx, sofab_istream_decoder_t *decoder, sofab_istream_field_cb_t field_callback, void *usrptr)
Begins decoding a nested Sofab sequence.
uint32_t sofab_id_t
Field identifier type.
Definition sofab.h:106
static void sofab_istream_read_fp32(sofab_istream_t *ctx, float *var)
Reads a 32-bit floating-point value.
Definition istream.h:440
Internal state of the Sofab input stream.
Definition istream.h:135
Parameters
ctxPointer to the active input stream context.
idField ID that was encountered.
sizeSize of the field's value in bytes (for fixed-length types).
countNumber of elements (for array types).
usrptrOptional user-provided pointer (provided at initialization).

Definition at line 113 of file istream.h.

◆ sofab_istream_t

Opaque Sofab input stream context.

This structure contains all state related to the incremental parsing of Sofab-encoded data. Users should not access its fields directly; instead, they interact with it through the provided API.

Definition at line 79 of file istream.h.

◆ sofab_object_descr_id_t

typedef uint16_t sofab_object_descr_id_t

Definition at line 461 of file sofab.h.

◆ sofab_object_descr_offset_t

typedef uint16_t sofab_object_descr_offset_t

Definition at line 462 of file sofab.h.

◆ sofab_object_descr_size_t

typedef uint16_t sofab_object_descr_size_t

Definition at line 463 of file sofab.h.

◆ sofab_object_descr_t

Description of a SofaBuffer object structure.

◆ sofab_ostream_flush_cb_t

typedef void(* sofab_ostream_flush_cb_t) (sofab_ostream_t *ctx, const uint8_t *data, size_t len, void *usrptr)

Output stream flush callback type.

The flush callback is invoked when the output stream needs to free or hand off previously written bytes (for example, when the internal buffer is full or when sofab_ostream_flush() is called). The callback implementation is responsible for processing the bytes (e.g. sending them over a transport, writing to storage) and for providing, if desired, a new buffer through sofab_ostream_buffer_set().

The callback must not block the stream reentrantly (the library does not perform internal locking). If required, the callback should copy or queue data and return quickly.

Parameters
usrptrUser-provided pointer.
dataPointer to the buffer that contains bytes to flush.
lenNumber of bytes available at data to flush.

Definition at line 87 of file ostream.h.

◆ sofab_ostream_t

Opaque Sofab output stream context.

This structure holds all state related to the incremental encoding of Sofab-encoded data. Users should not access its fields directly; instead, they interact with it through the provided API.

Definition at line 67 of file ostream.h.

◆ sofab_signed_t

typedef int64_t sofab_signed_t

Signed value type.

Definition at line 122 of file sofab.h.

◆ sofab_unsigned_t

typedef uint64_t sofab_unsigned_t

Scalar varint value types.

By default the unsigned/signed scalar values carried by varint fields are 64-bit. Defining SOFAB_DISABLE_INT64_SUPPORT narrows them to 32-bit, which removes the 64-bit varint math (smaller/faster on 32-bit MCUs) at the cost of the 64-bit value range and wire compatibility for larger values — see the SOFAB_DISABLE_INT64_SUPPORT notes below before enabling it.

Unsigned value type

Definition at line 118 of file sofab.h.

Enumeration Type Documentation

◆ sofab_fixlentype_t

SofaBuffers 3bit fixed-length data types.

Enumerator
SOFAB_FIXLENTYPE_FP32 
SOFAB_FIXLENTYPE_FP64 
SOFAB_FIXLENTYPE_STRING 
SOFAB_FIXLENTYPE_BLOB 

Definition at line 97 of file sofab.h.

◆ sofab_ret_t

Return status codes.

Enumerator
SOFAB_RET_OK 

OK.

SOFAB_RET_INCOMPLETE 

Consumed bytes end inside a field or with an open sequence: a valid but partial decode.

NOT an error — the caller owns end-of-input and may feed more bytes. Distinct from SOFAB_RET_OK (a complete message boundary) and SOFAB_RET_E_INVALID_MSG (malformed).

SOFAB_RET_E_ARGUMENT 

The caller handed the library a value it cannot act on: an id above SOFAB_ID_MAX, a destination or descriptor width that is not 1/2/4/8, a descriptor field type that does not exist, or (with strict UTF-8 on) a string that is not valid UTF-8.

Always a defect in the calling code, never in the data on the wire.

SOFAB_RET_E_BUFFER_FULL 

Sofab serialization failed due to buffer overflow.

SOFAB_RET_E_INVALID_MSG 

Sofab deserialization failed: the message is malformed, or it carries a wire construct this build was compiled without (the SOFAB_DISABLE_*_SUPPORT switches below).

Terminal: once sofab_istream_feed returns it, every subsequent feed on that context returns it too and no further callback fires (CORELIB_PLAN §5.2) — only sofab_istream_init resets it.

SOFAB_RET_E_LIMIT_EXCEEDED 

A configured receiver-side limit (CORELIB_PLAN §6.2.1) was exceeded on a schema-**unbounded** field.

The message is well-formed — the same bytes decode under a looser limit — so it is neither SOFAB_RET_E_INVALID_MSG nor the INVALID outcome, but a terminal, receiver-local policy rejection (§6.3).

No C API function returns this code. A receiver limit bounds a field the schema leaves unbounded, and this C API has no such field: every read is handed a destination that already exists, so its capacity is the bound (see sofab_object_field_t and sofab_istream_read_string / _read_blob / _read_array, all of which take the destination's own size). The code is declared here because sofab_ret_t is the C half of §6.3's shared code table: the C++ wrapper, whose string/blob/array destinations may be growable and therefore can be schema-unbounded, enforces the limits and reports them as sofab::Error::LimitExceeded, which is this enumerator. Declaring it once here is what keeps the two enums a single numbering.

Definition at line 38 of file sofab.h.

◆ sofab_type_t

SofaBuffers 3bit field data types.

Enumerator
SOFAB_TYPE_VARINT_UNSIGNED 
SOFAB_TYPE_VARINT_SIGNED 
SOFAB_TYPE_FIXLEN 
SOFAB_TYPE_VARINTARRAY_UNSIGNED 
SOFAB_TYPE_VARINTARRAY_SIGNED 
SOFAB_TYPE_FIXLENARRAY 
SOFAB_TYPE_SEQUENCE_START 
SOFAB_TYPE_SEQUENCE_END 

Definition at line 84 of file sofab.h.

Function Documentation

◆ sofab_istream_feed()

sofab_ret_t sofab_istream_feed ( sofab_istream_t *  ctx,
const void *  data,
size_t  datalen 
)
extern

Feeds raw Sofab-encoded bytes into the input stream.

The decoder consumes the buffer incrementally. Field callbacks are triggered when field headers are parsed. If the callback binds a destination buffer via a sofab_istream_read_* function, subsequent bytes are written there.

The decoder is resumable, so a call may end mid-field. The outcome is three-valued (there is no separate finalize step):

  • SOFAB_RET_OK: the consumed bytes end exactly on a field boundary — a complete message so far.
  • SOFAB_RET_INCOMPLETE: the consumed bytes end inside a field (a partial varint, a fixlen/array payload shorter than declared) or with an open sequence. This is a valid but partial decode, NOT an error: the caller owns end-of-input and may feed more bytes to continue. Truncated input is reported here — it is neither accepted as complete nor rejected as invalid.
  • SOFAB_RET_E_INVALID_MSG: the bytes are malformed regardless of what follows (varint too wide, length/count/id over the limit, bad type, ...), or they use a wire construct this build was compiled without (see below).

INVALID is terminal and sticky (normative). CORELIB_PLAN §5.2: no continuation of bytes can make rejected input valid. Once this function returns SOFAB_RET_E_INVALID_MSG it returns it for every subsequent call on the same context, no further field callback fires, and the only way forward is sofab_istream_init. That is enforcement, not just documentation: the decoder stops mid-message but keeps its position, so resuming would resynchronize on the payload of the field it just refused and deliver those bytes as if they were new field headers.

The rejection does not un-deliver what already arrived. Fields decoded before the offending one are already in the caller's destinations — a streaming decoder cannot know a later field will be refused. So SOFAB_RET_E_INVALID_MSG condemns the whole message: discard everything this stream wrote, do not use the partially-filled destinations.

A build with features compiled out rejects what it cannot decode. Defining SOFAB_DISABLE_FIXLEN_SUPPORT, SOFAB_DISABLE_ARRAY_SUPPORT, SOFAB_DISABLE_SEQUENCE_SUPPORT, SOFAB_DISABLE_FP64_SUPPORT or SOFAB_DISABLE_INT64_SUPPORT removes the wire construct itself, not merely the ability to store it: a message carrying one is SOFAB_RET_E_INVALID_MSG, even if the field would otherwise have been skipped. Such a build therefore only interoperates with peers that never emit the removed constructs — see the configuration notes in sofab.h before enabling one.

A zero-length feed is legal and reports the outcome so far; data may then be NULL. That is how the all-default message arrives: MESSAGE_SPEC §2 omits a sequence-typed field equal to its default, so an all-default message is the empty byte string and sofab_istream_feed(ctx, NULL, 0) is its whole input.

Parameters
ctxPointer to the input stream context.
dataPointer to the raw serialized data (may be NULL when datalen is 0).
datalenLength of data in bytes.
Returns
SOFAB_RET_OK on a complete boundary, SOFAB_RET_INCOMPLETE on a partial (mid-field / open-sequence) decode, or another sofab_ret_t error code.
Here is the caller graph for this function:

◆ sofab_istream_init()

void sofab_istream_init ( sofab_istream_t *  ctx,
sofab_istream_field_cb_t  field_callback,
void *  usrptr 
)
extern

Initializes a Sofab input stream context.

Resets all decoder state, assigns the default decoder, and registers the callback that will be triggered for each incoming field ID at the top level sequence.

Parameters
ctxPointer to the input stream context.
field_callbackUser callback invoked for each field ID.
usrptrOptional user pointer passed to the callback.
Here is the caller graph for this function:

◆ sofab_istream_invalidate()

void sofab_istream_invalidate ( sofab_istream_t *  ctx)
extern

Reject the message in progress from within a field callback.

A field callback usually binds a destination (a sofab_istream_read_* call) or skips the field by binding nothing. Some declared bounds, however, can only be checked by the callback itself — most notably an element whose wire index is at or beyond a fixed-count array's capacity, delivered to the callback keyed by its wire id with no way for the core to know the schema count. For those cases a silent skip would let a malformed message decode as valid, violating MESSAGE_SPEC §7/§7.1 ("a declared bound binds every target").

Calling this sets the same sticky invalid flag the decoder sets when it rejects a message itself: the enclosing sofab_istream_feed (and every subsequent one) returns SOFAB_RET_E_INVALID_MSG regardless of what else is decoded, and no further field callback fires. The flag is set synchronously inside the callback, so it takes effect even when the field's payload fill is deferred across chunked feeds. It is cleared only by sofab_istream_init.

Parameters
ctxPointer to the input stream context.
Here is the caller graph for this function:

◆ sofab_istream_read_array()

void sofab_istream_read_array ( sofab_istream_t *  ctx,
void *  var,
size_t  element_count,
size_t  element_size,
uint8_t  opt 
)
extern

Reads an array of elements.

Parameters
ctxPointer to the input stream context.
varDestination array.
element_countNumber of fixed-size elements.
element_sizeSize of each element in bytes.
optField options (type and flags).
Here is the caller graph for this function:

◆ sofab_istream_read_array_of_bool()

static void sofab_istream_read_array_of_bool ( sofab_istream_t *  ctx,
bool *  var,
size_t  element_count 
)
inlinestatic

Reads an array of booleans.

The elements ride the unsigned varint array wire form; each is normalized on store exactly as sofab_istream_read_bool normalizes a scalar (§4.4).

Parameters
ctxPointer to the input stream context.
varDestination array.
element_countNumber of elements to read.

Definition at line 623 of file istream.h.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ sofab_istream_read_array_of_fp32()

static void sofab_istream_read_array_of_fp32 ( sofab_istream_t *  ctx,
float *  var,
size_t  element_count 
)
inlinestatic

Reads an array of 32-bit floating-point values.

Parameters
ctxPointer to the input stream context.
varDestination array.
element_countNumber of elements to read.

Definition at line 669 of file istream.h.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ sofab_istream_read_array_of_fp64()

static void sofab_istream_read_array_of_fp64 ( sofab_istream_t *  ctx,
double *  var,
size_t  element_count 
)
inlinestatic

Reads an array of 64-bit floating-point values.

Parameters
ctxPointer to the input stream context.
varDestination array.
element_countNumber of elements to read.

Definition at line 685 of file istream.h.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ sofab_istream_read_array_of_i16()

static void sofab_istream_read_array_of_i16 ( sofab_istream_t *  ctx,
int16_t *  var,
size_t  element_count 
)
inlinestatic

Reads an array of 16-bit signed integers.

Parameters
ctxPointer to the input stream context.
varDestination array.
element_countNumber of elements to read.

Definition at line 564 of file istream.h.

Here is the call graph for this function:

◆ sofab_istream_read_array_of_i32()

static void sofab_istream_read_array_of_i32 ( sofab_istream_t *  ctx,
int32_t *  var,
size_t  element_count 
)
inlinestatic

Reads an array of 32-bit signed integers.

Parameters
ctxPointer to the input stream context.
varDestination array.
element_countNumber of elements to read.

Definition at line 592 of file istream.h.

Here is the call graph for this function:

◆ sofab_istream_read_array_of_i64()

static void sofab_istream_read_array_of_i64 ( sofab_istream_t *  ctx,
int64_t *  var,
size_t  element_count 
)
inlinestatic

Reads an array of 64-bit signed integers.

Parameters
ctxPointer to the input stream context.
varDestination array.
element_countNumber of elements to read.

Definition at line 639 of file istream.h.

Here is the call graph for this function:

◆ sofab_istream_read_array_of_i8()

static void sofab_istream_read_array_of_i8 ( sofab_istream_t *  ctx,
int8_t *  var,
size_t  element_count 
)
inlinestatic

Reads an array of 8-bit signed integers.

Parameters
ctxPointer to the input stream context.
varDestination array.
element_countNumber of elements to read.

Definition at line 536 of file istream.h.

Here is the call graph for this function:

◆ sofab_istream_read_array_of_u16()

static void sofab_istream_read_array_of_u16 ( sofab_istream_t *  ctx,
uint16_t *  var,
size_t  element_count 
)
inlinestatic

Reads an array of 16-bit unsigned integers.

Parameters
ctxPointer to the input stream context.
varDestination array.
element_countNumber of elements to read.

Definition at line 578 of file istream.h.

Here is the call graph for this function:

◆ sofab_istream_read_array_of_u32()

static void sofab_istream_read_array_of_u32 ( sofab_istream_t *  ctx,
uint32_t *  var,
size_t  element_count 
)
inlinestatic

Reads an array of 32-bit unsigned integers.

Parameters
ctxPointer to the input stream context.
varDestination array.
element_countNumber of elements to read.

Definition at line 606 of file istream.h.

Here is the call graph for this function:

◆ sofab_istream_read_array_of_u64()

static void sofab_istream_read_array_of_u64 ( sofab_istream_t *  ctx,
uint64_t *  var,
size_t  element_count 
)
inlinestatic

Reads an array of 64-bit unsigned integers.

Parameters
ctxPointer to the input stream context.
varDestination array.
element_countNumber of elements to read.

Definition at line 653 of file istream.h.

Here is the call graph for this function:

◆ sofab_istream_read_array_of_u8()

static void sofab_istream_read_array_of_u8 ( sofab_istream_t *  ctx,
uint8_t *  var,
size_t  element_count 
)
inlinestatic

Reads an array of 8-bit unsigned integers.

Parameters
ctxPointer to the input stream context.
varDestination array.
element_countNumber of elements to read.

Definition at line 550 of file istream.h.

Here is the call graph for this function:

◆ sofab_istream_read_blob()

static void sofab_istream_read_blob ( sofab_istream_t *  ctx,
void *  var,
size_t  varlen 
)
inlinestatic

Reads a binary blob of fixed maximum size.

Parameters
ctxPointer to the input stream context.
varDestination buffer.
varlenMaximum number of bytes to read.

Definition at line 502 of file istream.h.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ sofab_istream_read_bool()

static void sofab_istream_read_bool ( sofab_istream_t *  ctx,
bool *  var 
)
inlinestatic

Reads a boolean value.

Canonical on encode, tolerant on decode (CORELIB_PLAN §4.4): the wire carries a plain unsigned varint, and every value other than 0 reads as true. The value is normalized on store, so the destination always ends up holding 0 or 1 — the only two states a bool object may have — and a re-encode emits 1. A boolean carries no width bound, so a value that does not fit one byte is true, not INVALID.

Parameters
ctxPointer to the input stream context.
varPointer to destination variable.

Definition at line 426 of file istream.h.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ sofab_istream_read_field()

void sofab_istream_read_field ( sofab_istream_t *  ctx,
void *  var,
size_t  varlen,
uint8_t  opt 
)
extern

Number of fields skipped because their wire type contradicted the destination a field callback bound.

A field whose type on the wire does not match the type of the sofab_istream_read_* call made for it carries no value for that destination, so it is skipped like an unknown id and decoding continues (MESSAGE_SPEC §7.3). That is not an error: it is what lets a reader and a writer built from different revisions of a schema keep talking.

This counter makes those skips visible. A non-zero value on a message that decoded successfully means the two sides disagree about what an id means — useful in a log or a health metric, and the signal that used to be reported as a usage error. The count saturates at 255; only zero versus non-zero carries meaning. It is cleared by sofab_istream_init.

Parameters
ctxPointer to the input stream context.
Returns
Number of type-contradicting fields skipped, saturating at 255.

Reads a variable-length integer field.

This function must only be invoked inside a field callback. The value is written to var using varlen bytes.

Parameters
ctxPointer to the input stream context.
varOutput variable.
varlenSize of the output variable in bytes.
optField options (type and flags).
Here is the caller graph for this function:

◆ sofab_istream_read_fp32()

static void sofab_istream_read_fp32 ( sofab_istream_t *  ctx,
float *  var 
)
inlinestatic

Reads a 32-bit floating-point value.

Parameters
ctxPointer to the input stream context.
varPointer to destination variable.

Definition at line 440 of file istream.h.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ sofab_istream_read_fp64()

static void sofab_istream_read_fp64 ( sofab_istream_t *  ctx,
double *  var 
)
inlinestatic

Reads a 64-bit floating-point value.

Parameters
ctxPointer to the input stream context.
varPointer to destination variable.

Definition at line 454 of file istream.h.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ sofab_istream_read_i16()

static void sofab_istream_read_i16 ( sofab_istream_t *  ctx,
int16_t *  var 
)
inlinestatic

Reads a 16-bit signed integer.

Parameters
ctxPointer to the input stream context.
varPointer to destination variable.

Definition at line 345 of file istream.h.

Here is the call graph for this function:

◆ sofab_istream_read_i32()

static void sofab_istream_read_i32 ( sofab_istream_t *  ctx,
int32_t *  var 
)
inlinestatic

Reads a 32-bit signed integer.

Parameters
ctxPointer to the input stream context.
varPointer to destination variable.

Definition at line 369 of file istream.h.

Here is the call graph for this function:

◆ sofab_istream_read_i64()

static void sofab_istream_read_i64 ( sofab_istream_t *  ctx,
int64_t *  var 
)
inlinestatic

Reads a 64-bit signed integer.

Parameters
ctxPointer to the input stream context.
varPointer to destination variable.

Definition at line 394 of file istream.h.

Here is the call graph for this function:

◆ sofab_istream_read_i8()

static void sofab_istream_read_i8 ( sofab_istream_t *  ctx,
int8_t *  var 
)
inlinestatic

Reads an 8-bit signed integer.

Parameters
ctxPointer to the input stream context.
varPointer to destination variable.

Definition at line 321 of file istream.h.

Here is the call graph for this function:

◆ sofab_istream_read_sequence()

void sofab_istream_read_sequence ( sofab_istream_t *  ctx,
sofab_istream_decoder_t *  decoder,
sofab_istream_field_cb_t  field_callback,
void *  usrptr 
)
extern

Begins decoding a nested Sofab sequence.

A nested sequence has its own decoder and callback. This allows hierarchical message structures with independent interpretation of child fields.

Parameters
ctxPointer to the input stream context.
decoderDecoder instance used for the new sequence.
field_callbackCallback invoked for fields inside the nested sequence.
usrptrOptional user pointer for the nested callback.

◆ sofab_istream_read_string()

static void sofab_istream_read_string ( sofab_istream_t *  ctx,
char *  var,
size_t  varlen 
)
inlinestatic

Reads a string of fixed maximum size.

A null terminator is always appended to the end of the string.

Parameters
ctxPointer to the input stream context.
varDestination buffer.
varlenMaximum number of bytes to read.

Definition at line 471 of file istream.h.

Here is the call graph for this function:

◆ sofab_istream_read_string_noterm()

static void sofab_istream_read_string_noterm ( sofab_istream_t *  ctx,
char *  var,
size_t  varlen 
)
inlinestatic

Reads a string of fixed maximum size.

No null terminator is appended to the end of the string.

Parameters
ctxPointer to the input stream context.
varDestination buffer.
varlenMaximum number of bytes to read.

Definition at line 488 of file istream.h.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ sofab_istream_read_u16()

static void sofab_istream_read_u16 ( sofab_istream_t *  ctx,
uint16_t *  var 
)
inlinestatic

Reads a 16-bit unsigned integer.

Parameters
ctxPointer to the input stream context.
varPointer to destination variable.

Definition at line 357 of file istream.h.

Here is the call graph for this function:

◆ sofab_istream_read_u32()

static void sofab_istream_read_u32 ( sofab_istream_t *  ctx,
uint32_t *  var 
)
inlinestatic

Reads a 32-bit unsigned integer.

Parameters
ctxPointer to the input stream context.
varPointer to destination variable.

Definition at line 381 of file istream.h.

Here is the call graph for this function:

◆ sofab_istream_read_u64()

static void sofab_istream_read_u64 ( sofab_istream_t *  ctx,
uint64_t *  var 
)
inlinestatic

Reads a 64-bit unsigned integer.

Parameters
ctxPointer to the input stream context.
varPointer to destination variable.

Definition at line 406 of file istream.h.

Here is the call graph for this function:

◆ sofab_istream_read_u8()

static void sofab_istream_read_u8 ( sofab_istream_t *  ctx,
uint8_t *  var 
)
inlinestatic

Reads an 8-bit unsigned integer.

Parameters
ctxPointer to the input stream context.
varPointer to destination variable.

Definition at line 333 of file istream.h.

Here is the call graph for this function:

◆ sofab_object_encode()

sofab_ret_t sofab_object_encode ( sofab_ostream_t *  ctx,
const sofab_object_descr_t *  info,
const void *  src 
)
extern

Encodes an object with the given descriptor into the output stream.

The output stream context must be initialized prior to calling this function. Fields equal to their default (the matching member of the descriptor's default-values object, or zero when none is set) are skipped. Nested objects are written as sequences.

The skip rule covers a nested-object field too (MESSAGE_SPEC §2): one whose every child equals its default is omitted entirely rather than framed as an empty sequence, so an all-default object encodes to zero bytes. The comparison is per child field, recursively, against the nested descriptor's declared defaults — never a raw byte image — so struct padding cannot influence it and a non-zero nested default is handled like any other. Absence reconstructs exactly that default (sofab_object_init), so the omission is value-preserving.

Inside a wrapper-array holder (SOFAB_OBJECT_DESCR_SEQ) the fields are the array's element slots and the rule becomes positional (MESSAGE_SPEC §2/§5.1): a wrapper carries no length, so the decoded length is highest present id + 1 and nothing that carries it may be elided.

  • An element at an interior index equal to its default is omitted, leaving an id gap — a leaf is not written and a sequence-form element is not framed either. Both kinds obey one rule; there is no trailing-run elision and no fill-to-N.
  • The element at the last index is always written: a leaf as its (default) value, a sequence element as an empty frame.

What "last index" means here. It is the array's length - 1, and where the length comes from is the holder descriptor's business:

  • SOFAB_OBJECT_DESCR_SEQ_SIZED carries an element-count member, so the length is that member (clamped to the capacity). Slots at or past it are not walked at all; the slot at length - 1 is the one that is always written; a length of 0 writes nothing, and the field-level ≠-default test above omits the whole wrapper (the canonical encoding of the empty array, §2). All of 0 … N are expressible, as on every other target.
  • a plain SOFAB_OBJECT_DESCR_SEQ has no length member, so the value it holds occupies every slot and the length is field_count: the last index is field_count - 1 unconditionally. An all-default holder is the one exception — indistinguishable from the empty array there — and is omitted whole by the same field-level test. Lengths 1 … N-1 are not representable in that form.

A compact scalar array encodes every element it holds (§3): its element count is the length member of SOFAB_OBJECT_FIELD_ARRAY_SIZED, or the full capacity for a plain SOFAB_OBJECT_FIELD_ARRAY. A trailing element equal to the element default is not dropped — [1,2,3,0,0] and [1,2,3] are different values.

This descriptor-driven encoder uses none of the output stream's hold-back framing (sofab_ostream_write_sequence_begin_lazy): it tests a field against its default before opening anything, so it is canonical at every nesting depth and no SOFAB_LAZY_SEQ_DEPTH window applies to it.

Parameters
ctxPointer to the output stream context.
infoPointer to the object descriptor.
srcPointer to the source object to serialize.
Returns
SOFAB_RET_OK on success, otherwise an sofab_ret_t error code (e.g. SOFAB_RET_E_ARGUMENT for an unsupported descriptor field type, or a write error propagated from the output stream).

◆ sofab_object_field_cb()

void sofab_object_field_cb ( sofab_istream_t *  ctx,
sofab_id_t  id,
size_t  size,
size_t  count,
void *  usrptr 
)
extern

Field callback invoked during object decoding.

Use this function as the field callback when initializing an input stream for decoding objects with sofab_istream_init. The usrptr must point to a sofab_object_decoder_t whose info and dst describe the target object; for nested objects the decoder must be the first element of an array with one slot per supported nesting level (see depth). The callback binds the appropriate read for each known field ID. An unknown field ID is ignored (skipped) for a normal message descriptor, but for a fixed-capacity sequence-holder descriptor (see SOFAB_OBJECT_DESCR_SEQ) it is an over-index element and rejects the message via sofab_istream_invalidate — unless its header contradicts the declared element type, in which case §7.3 skips it first and no element, and therefore no element index, ever exists.

Inside a sized holder (SOFAB_OBJECT_DESCR_SEQ_SIZED) a matched element id also raises the companion element-count member to id + 1, so that when the wrapper closes it holds highest present id + 1 — the array's length per MESSAGE_SPEC §5.1, stored back exactly as a sized blob stores its received byte length. The §7.4 reset below re-zeroes the member whenever the wrapper re-opens, so a replaced array reports its own length and not the previous one's.

Only a bound element counts. An element whose header wire type contradicts the declared one is skipped "exactly as a field with an unknown id is skipped" (§7.3) — and an unknown id leaves nothing behind, so it does not occupy its id and does not count toward the length: the slot is reconstructed from the element default and the array is byte-for-byte what it would have been had the element never arrived. The ids §5.1 counts are the ones consumed as elements, not the ones that merely appeared on the wire. A well-typed but empty element — an empty frame, an empty string — is bound and does count; that is a present element and not the same thing at all.

Warning
**Initialize dst with sofab_object_init before every message.** Decoding writes only the fields the wire actually carries; a field the sender omitted because it equals its default is not written, so whatever the destination already held stays. Re-using one object for a second message therefore keeps stale values unless it is re-initialized first. This applies to a sequence field as much as to a leaf one: MESSAGE_SPEC §2 omits an all-default sequence field outright, and the §7.4 wrapper-replace reset inside this callback only runs when a wrapper actually opens on the wire.
Parameters
ctxPointer to the active input stream context.
idField ID reported by the decoder.
sizeSize of the field value in bytes (unused; kept for the callback signature).
countNumber of array elements (unused; kept for the callback signature).
usrptrPointer to the sofab_object_decoder_t driving this object.

◆ sofab_object_init()

sofab_ret_t sofab_object_init ( const sofab_object_descr_t *  info,
void *  obj 
)
extern

Initializes an object structure with default values.

This function populates the provided object structure with default values as specified in the object descriptor. If no default values are provided, the object is zero-initialized.

Parameters
infoPointer to the object descriptor.
objPointer to the object structure to initialize.
Returns
SOFAB_RET_OK on success, or an error code on failure.

◆ sofab_ostream_buffer_set()

void sofab_ostream_buffer_set ( sofab_ostream_t *  ctx,
uint8_t *  buffer,
size_t  buflen,
size_t  offset 
)
extern

Replace the active buffer used by the output stream.

This function updates the stream to use a new buffer region. Typical usage is inside a flush callback: after handing off the old buffer bytes the callback calls sofab_ostream_buffer_set() to present a fresh buffer and resume encoding without losing data.

The buffer is not copied; the stream simply adjusts pointers to the new region. The caller retains ownership of the memory.

Parameters
ctxPointer to the output stream context.
bufferPointer to the new buffer to use.
buflenSize of buffer in bytes.
offsetInitial offset within the new buffer (0..buflen). The offset belongs to this installation (CORELIB_PLAN §5.1): the cursor starts at it, and it is consumed, so a later flush the callback returns from without installing anything resumes at 0. Passing the same buffer again is a new installation like any other – that is how a sink re-arms header room in every flushed unit, one framing header per packet.

◆ sofab_ostream_bytes_used()

size_t sofab_ostream_bytes_used ( sofab_ostream_t *  ctx)
extern

Get the number of bytes used in the current buffer.

This function returns the number of bytes that have been written into the active buffer since the last flush or initialization.

Parameters
ctxPointer to the output stream context.
Returns
Number of bytes used in the current buffer.
Here is the caller graph for this function:

◆ sofab_ostream_flush()

size_t sofab_ostream_flush ( sofab_ostream_t *  ctx)
extern

Flush any pending bytes from the output stream.

If a flush callback was registered during initialization, it is invoked with any pending bytes currently present in the active buffer. After the call, the internal buffer is considered empty; offset is reset to the start of the buffer (unless the flush callback calls sofab_ostream_buffer_set() to provide a different buffer).

Parameters
ctxPointer to the output stream context.
Returns
Number of bytes flushed.
Here is the caller graph for this function:

◆ sofab_ostream_init()

void sofab_ostream_init ( sofab_ostream_t *  ctx,
uint8_t *  buffer,
size_t  buflen,
size_t  offset,
sofab_ostream_flush_cb_t  flush,
void *  usrptr 
)
extern

Initialize an Sofab output stream context.

Prepares the sofab_ostream_t for usage. The supplied buffer will be used for subsequent writes. If flush is non-NULL it will be called whenever the implementation needs to hand off buffered bytes (for example when the buffer is full or when sofab_ostream_flush() is invoked). The user pointer usrptr is passed unchanged to that callback.

The function does not copy the provided buffer; the caller retains ownership and must ensure the memory remains valid until replaced by sofab_ostream_buffer_set() or the stream is destroyed.

Parameters
ctxPointer to the output stream context to initialize.
bufferPointer to a writable buffer used to accumulate encoded bytes.
buflenSize of buffer in bytes.
offsetInitial offset (0..buflen) to start writing from. The offset is applied relative to buffer; e.g. offset==0 writes at buffer[0], offset==buflen makes the buffer effectively full.
flushOptional flush callback invoked when the buffer data must be handled.
usrptrOptional user pointer passed to flush when invoked.
Note
When flush is non-NULL the buffer is installed for streaming and must satisfy buflen - offset >= SOFAB_MIN_OUTPUT_BUFFER (CORELIB_PLAN §5.1). With flush NULL no flush can occur and no minimum applies: buflen may be 0 and offset may equal buflen, and the buffer either holds the message or reports SOFAB_RET_E_BUFFER_FULL. The all-default message is the empty byte string (MESSAGE_SPEC §2), so the zero-length case is a real one.
Both preconditions are checked with assert(), which is this port's mechanism for an out-of-range argument on a void entry point. A release build (NDEBUG) does not reject; an installation that leaves no room then surfaces as SOFAB_RET_E_BUFFER_FULL on the first write that needs space, never as a write past the buffer.
Here is the caller graph for this function:

◆ sofab_ostream_write_array_of_fixlen()

sofab_ret_t sofab_ostream_write_array_of_fixlen ( sofab_ostream_t *  ctx,
sofab_id_t  id,
const void *  data,
int32_t  element_count,
int32_t  element_size,
sofab_fixlentype_t  type 
)
extern

Write an array of fixed-length elements.

Writes a contiguous block representing element_count elements of fixed size element_size. The type describes the semantic fixed-length type for the array elements. Only floating-point types (FP32, FP64) are supported for fixed-length arrays.

Parameters
ctxPointer to the output stream context.
idField identifier.
dataPointer to element data.
element_countNumber of elements.
element_sizeSize of each element in bytes.
typeSemantic fixed-length type of elements.
Returns
SOFAB_RET_OK on success, otherwise an error code.
Here is the caller graph for this function:

◆ sofab_ostream_write_array_of_fp32()

static sofab_ret_t sofab_ostream_write_array_of_fp32 ( sofab_ostream_t *  ctx,
sofab_id_t  id,
const float *  data,
int32_t  element_count 
)
inlinestatic

Write an array of 32-bit floating-point values.

Parameters
ctxPointer to the output stream context.
idField identifier.
dataPointer to float array.
element_countNumber of float elements.
Returns
See sofab_ostream_write_array_of_fixlen().

Definition at line 617 of file ostream.h.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ sofab_ostream_write_array_of_fp64()

static sofab_ret_t sofab_ostream_write_array_of_fp64 ( sofab_ostream_t *  ctx,
sofab_id_t  id,
const double *  data,
int32_t  element_count 
)
inlinestatic

Write an array of 64-bit floating-point values.

Parameters
ctxPointer to the output stream context.
idField identifier.
dataPointer to double array.
element_countNumber of double elements.
Returns
See sofab_ostream_write_array_of_fixlen().

Definition at line 635 of file ostream.h.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ sofab_ostream_write_array_of_i16()

static sofab_ret_t sofab_ostream_write_array_of_i16 ( sofab_ostream_t *  ctx,
sofab_id_t  id,
const int16_t *  var,
int32_t  element_count 
)
inlinestatic

Writes an array of 16-bit signed integers.

Parameters
ctxOutput stream context.
idField identifier.
varDestination array.
element_countNumber of elements to write.
Returns
See sofab_ostream_write_array_of_signed().

Definition at line 518 of file ostream.h.

Here is the call graph for this function:

◆ sofab_ostream_write_array_of_i32()

static sofab_ret_t sofab_ostream_write_array_of_i32 ( sofab_ostream_t *  ctx,
sofab_id_t  id,
const int32_t *  var,
int32_t  element_count 
)
inlinestatic

Writes an array of 32-bit signed integers.

Parameters
ctxOutput stream context.
idField identifier.
varDestination array.
element_countNumber of elements to write.
Returns
See sofab_ostream_write_array_of_signed().

Definition at line 550 of file ostream.h.

Here is the call graph for this function:

◆ sofab_ostream_write_array_of_i64()

static sofab_ret_t sofab_ostream_write_array_of_i64 ( sofab_ostream_t *  ctx,
sofab_id_t  id,
const int64_t *  var,
int32_t  element_count 
)
inlinestatic

Writes an array of 64-bit signed integers.

Parameters
ctxOutput stream context.
idField identifier.
varDestination array.
element_countNumber of elements to write.
Returns
See sofab_ostream_write_array_of_signed().

Definition at line 583 of file ostream.h.

Here is the call graph for this function:

◆ sofab_ostream_write_array_of_i8()

static sofab_ret_t sofab_ostream_write_array_of_i8 ( sofab_ostream_t *  ctx,
sofab_id_t  id,
const int8_t *  var,
int32_t  element_count 
)
inlinestatic

Writes an array of 8-bit signed integers.

Parameters
ctxOutput stream context.
idField identifier.
varDestination array.
element_countNumber of elements to write.
Returns
See sofab_ostream_write_array_of_signed().

Definition at line 486 of file ostream.h.

Here is the call graph for this function:

◆ sofab_ostream_write_array_of_signed()

sofab_ret_t sofab_ostream_write_array_of_signed ( sofab_ostream_t *  ctx,
sofab_id_t  id,
const void *  data,
int32_t  element_count,
int32_t  element_size 
)
extern

Write an array of signed integers.

Encodes an array field composed of signed integer elements. Each array element is encoded according to the element size and signed varint rules. Supported types: int8_t, int16_t, int32_t, int64_t.

Parameters
ctxPointer to the output stream context.
idField identifier.
dataPointer to the array data.
element_countNumber of elements in data.
element_sizeSize of each element in bytes.
Returns
SOFAB_RET_OK on success, otherwise an error code.
Here is the caller graph for this function:

◆ sofab_ostream_write_array_of_u16()

static sofab_ret_t sofab_ostream_write_array_of_u16 ( sofab_ostream_t *  ctx,
sofab_id_t  id,
const uint16_t *  var,
int32_t  element_count 
)
inlinestatic

Writes an array of 16-bit unsigned integers.

Parameters
ctxOutput stream context.
idField identifier.
varDestination array.
element_countNumber of elements to write.
Returns
See sofab_ostream_write_array_of_unsigned().

Definition at line 534 of file ostream.h.

Here is the call graph for this function:

◆ sofab_ostream_write_array_of_u32()

static sofab_ret_t sofab_ostream_write_array_of_u32 ( sofab_ostream_t *  ctx,
sofab_id_t  id,
const uint32_t *  var,
int32_t  element_count 
)
inlinestatic

Writes an array of 32-bit unsigned integers.

Parameters
ctxOutput stream context.
idField identifier.
varDestination array.
element_countNumber of elements to write.
Returns
See sofab_ostream_write_array_of_unsigned().

Definition at line 566 of file ostream.h.

Here is the call graph for this function:

◆ sofab_ostream_write_array_of_u64()

static sofab_ret_t sofab_ostream_write_array_of_u64 ( sofab_ostream_t *  ctx,
sofab_id_t  id,
const uint64_t *  var,
int32_t  element_count 
)
inlinestatic

Writes an array of 64-bit unsigned integers.

Parameters
ctxOutput stream context.
idField identifier.
varDestination array.
element_countNumber of elements to write.
Returns
See sofab_ostream_write_array_of_unsigned().

Definition at line 599 of file ostream.h.

Here is the call graph for this function:

◆ sofab_ostream_write_array_of_u8()

static sofab_ret_t sofab_ostream_write_array_of_u8 ( sofab_ostream_t *  ctx,
sofab_id_t  id,
const uint8_t *  var,
int32_t  element_count 
)
inlinestatic

Writes an array of 8-bit unsigned integers.

Parameters
ctxOutput stream context.
idField identifier.
varDestination array.
element_countNumber of elements to write.
Returns
See sofab_ostream_write_array_of_unsigned().

Definition at line 502 of file ostream.h.

Here is the call graph for this function:

◆ sofab_ostream_write_array_of_unsigned()

sofab_ret_t sofab_ostream_write_array_of_unsigned ( sofab_ostream_t *  ctx,
sofab_id_t  id,
const void *  data,
int32_t  element_count,
int32_t  element_size 
)
extern

Write an array of unsigned integers.

Encodes an array field composed of unsigned integer elements. Each array element is encoded according to the element size and unsigned varint rules. Supported types: uint8_t, uint16_t, uint32_t, uint64_t.

Parameters
ctxPointer to the output stream context.
idField identifier.
dataPointer to the array data.
element_countNumber of elements in data.
element_sizeSize of each element in bytes.
Returns
SOFAB_RET_OK on success, otherwise an error code.
Here is the caller graph for this function:

◆ sofab_ostream_write_blob()

static sofab_ret_t sofab_ostream_write_blob ( sofab_ostream_t *  ctx,
sofab_id_t  id,
const void *  data,
size_t  size 
)
inlinestatic

Write a binary blob field.

Convenience wrapper that writes raw bytes as a blob fixed-length field.

Parameters
ctxPointer to the output stream context.
idField identifier.
dataPointer to binary data.
sizeNumber of bytes to write.
Returns
See sofab_ostream_write_fixlen().

Definition at line 400 of file ostream.h.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ sofab_ostream_write_boolean()

static sofab_ret_t sofab_ostream_write_boolean ( sofab_ostream_t *  ctx,
sofab_id_t  id,
bool  value 
)
inlinestatic

Write a boolean value (1 byte).

Convenience wrapper that encodes a boolean as an unsigned value (0 or 1).

Parameters
ctxPointer to the output stream context.
idField identifier.
valueBoolean value to encode.
Returns
See sofab_ostream_write_unsigned().

Definition at line 331 of file ostream.h.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ sofab_ostream_write_fixlen()

sofab_ret_t sofab_ostream_write_fixlen ( sofab_ostream_t *  ctx,
sofab_id_t  id,
const void *  data,
int32_t  datalen,
sofab_fixlentype_t  type 
)
extern

Write a fixed-length field.

Encodes a fixed-length field header for id and copies datalen bytes from data into the output stream. The type parameter specifies the semantic type of the fixed-length data.

Parameters
ctxPointer to the output stream context.
idField identifier.
dataPointer to bytes to write (may be NULL if datalen is 0).
datalenNumber of bytes to write from data.
typeSemantic fixed-length type (sofab_fixlentype_t).
Returns
SOFAB_RET_OK on success, otherwise an sofab_ret_t error code.
Here is the caller graph for this function:

◆ sofab_ostream_write_fp32()

static sofab_ret_t sofab_ostream_write_fp32 ( sofab_ostream_t *  ctx,
sofab_id_t  id,
float  value 
)
inlinestatic

Write a 32-bit floating-point value.

Parameters
ctxPointer to the output stream context.
idField identifier.
valueFloat value to encode.
Returns
See sofab_ostream_write_fixlen().

Definition at line 347 of file ostream.h.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ sofab_ostream_write_fp64()

static sofab_ret_t sofab_ostream_write_fp64 ( sofab_ostream_t *  ctx,
sofab_id_t  id,
double  value 
)
inlinestatic

Write a 64-bit floating-point value.

Parameters
ctxPointer to the output stream context.
idField identifier.
valueDouble value to encode.
Returns
See sofab_ostream_write_fixlen().

Definition at line 363 of file ostream.h.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ sofab_ostream_write_sequence_begin()

sofab_ret_t sofab_ostream_write_sequence_begin ( sofab_ostream_t *  ctx,
sofab_id_t  id 
)
extern

Begin encoding a nested sequence (start marker).

Writes the header that begins a nested sequence for field id. After this call, subsequent writes belong to the nested sequence until sofab_ostream_write_sequence_end() is called. Nested sequences allow hierarchical message structures with a new field identifier space.

This opener frames unconditionally: the header is on the wire before the content is known, so the sequence reaches the wire even if nothing is written into it. That is what a hand-written encoder wants (an empty sequence is a legal wire primitive, §4.9) and what an element position needs (§5.1). A sequence-typed field that may equal its declared default belongs to sofab_ostream_write_sequence_begin_lazy() instead, which omits it (MESSAGE_SPEC §2).

Parameters
ctxPointer to the output stream context.
idField identifier representing the sequence.
Returns
SOFAB_RET_OK on success, otherwise an sofab_ret_t error code.

◆ sofab_ostream_write_sequence_begin_lazy()

sofab_ret_t sofab_ostream_write_sequence_begin_lazy ( sofab_ostream_t *  ctx,
sofab_id_t  id 
)
extern

Begin a nested sequence whose header is held back until it has content.

MESSAGE_SPEC §2 omits a sequence-typed field whose value equals its declared default, and "not one child field was written" is exactly that condition – evaluated per child field, recursively, for free. A sequence opened with this function and closed without a single field in it emits nothing instead of a two-byte empty frame, so an all-default message becomes the empty byte string.

The predicate never touches a byte image of the object, so struct padding cannot influence it and a non-zero nested default is handled by the caller's ordinary per-field test.

This is the message-layer primitive, for generated code and for the C++ wrapper. Two callers keep using the eager sofab_ostream_write_sequence_begin():

  • a direct (hand-written) encoder, for which an empty sequence is a legal wire primitive in its own right (§4.9);
  • an array wrapper whose field declares a non-empty default, where the empty frame is the only encoding of "explicitly empty" (§2, §3).

The descriptor-driven sofab_object_encode() needs neither: it decides omission per field, before opening anything.

Bounded: at most SOFAB_LAZY_SEQ_DEPTH headers are held back at once. A sequence opened deeper than that is framed eagerly and therefore keeps its empty frame when it turns out to be all-default — well-formed and value-identical, but not canonical. See SOFAB_LAZY_SEQ_DEPTH for why this heap-free profile takes that allowance and how to raise the bound.

On a full buffer: whatever commits the run (a field write, an eager sequence_begin, sofab_ostream_write_sequence_end_keep(), or overflowing the window) can hit SOFAB_RET_E_BUFFER_FULL partway through it. The ids that did not reach the buffer stay held back, so a caller that supplies a fresh buffer with sofab_ostream_buffer_set() and retries emits them then — the pending run never silently loses a sequence whose end marker is still coming. As everywhere in this encoder, the bytes are not rolled back: the header that hit the wall may have left a partial varint behind.

Parameters
ctxPointer to the output stream context.
idField identifier representing the sequence.
Returns
SOFAB_RET_OK on success, otherwise an sofab_ret_t error code.
Here is the caller graph for this function:

◆ sofab_ostream_write_sequence_end()

sofab_ret_t sofab_ostream_write_sequence_end ( sofab_ostream_t *  ctx)
extern

End encoding a nested sequence (end marker).

Writes the termination marker for the current nested sequence started via sofab_ostream_write_sequence_begin().

Parameters
ctxPointer to the output stream context.
Returns
SOFAB_RET_OK on success, otherwise an sofab_ret_t error code.
Here is the caller graph for this function:

◆ sofab_ostream_write_sequence_end_keep()

sofab_ret_t sofab_ostream_write_sequence_end_keep ( sofab_ostream_t *  ctx)
extern

End a nested sequence, keeping its frame even without content.

The counterpart of sofab_ostream_write_sequence_end() for a sequence opened with sofab_ostream_write_sequence_begin_lazy(): it first emits the held-back headers – this frame's and every enclosing one's – and then the end marker, so an empty sequence reaches the wire as begin + end.

Required wherever the frame carries information beyond its contents: a wrapper-array ELEMENT (element presence carries a dynamic array's length, MESSAGE_SPEC §5.1), and an array field already known to differ from a non-empty declared default (§2, §3).

Parameters
ctxPointer to the output stream context.
Returns
SOFAB_RET_OK on success, otherwise an sofab_ret_t error code.
Here is the caller graph for this function:

◆ sofab_ostream_write_signed()

sofab_ret_t sofab_ostream_write_signed ( sofab_ostream_t *  ctx,
sofab_id_t  id,
sofab_signed_t  value 
)
extern

Write a signed integer field.

Encodes a field header for id and writes a signed integer value. The value encoding typically uses zigzag transformation followed by varint encoding.

Parameters
ctxPointer to the output stream context.
idField identifier.
valueSigned integer value to encode.
Returns
SOFAB_RET_OK on success, otherwise an error code.
Here is the caller graph for this function:

◆ sofab_ostream_write_string()

static sofab_ret_t sofab_ostream_write_string ( sofab_ostream_t *  ctx,
sofab_id_t  id,
const char *  text 
)
inlinestatic

Write a null-terminated string.

Convenience wrapper that writes a string field by using strlen() to determine length.

Parameters
ctxPointer to the output stream context.
idField identifier.
textNull-terminated C string to write.
Returns
See sofab_ostream_write_fixlen().

Definition at line 382 of file ostream.h.

Here is the call graph for this function:

◆ sofab_ostream_write_unsigned()

sofab_ret_t sofab_ostream_write_unsigned ( sofab_ostream_t *  ctx,
sofab_id_t  id,
sofab_unsigned_t  value 
)
extern

Write an unsigned integer field.

Encodes a field header for the given id with an unsigned varint payload and writes the varint-encoded value into the stream buffer.

Parameters
ctxPointer to the output stream context.
idField identifier (sofab_id_t).
valueUnsigned integer value to encode.
Returns
SOFAB_RET_OK on success, otherwise an sofab_ret_t error code will be returned.
Here is the caller graph for this function: