|
SofaBuffers C/C++ 0.12.0
API Documentation
|
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 | |
| #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 | |
| #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 §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 The profile choice §6.2 requires be documented: 2,147,483,647 on every target whose Both are overridable (
| |
| #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. | |
| #define SOFAB_ARRAY_MAX (INT32_MAX) |
| #define SOFAB_FIXLEN_MAX (INT32_MAX) |
| #define SOFAB_ID_MAX (INT32_MAX) |
Largest encodable field id (the (id<<3)|type header must fit in sofab_unsigned_t).
| #define SOFAB_ISTREAM_OPT_BOOLEAN (0x80) |
| #define SOFAB_ISTREAM_OPT_FIELDTYPE | ( | type | ) | ((type) & 0x07) |
| #define SOFAB_ISTREAM_OPT_FIXLENTYPE | ( | type | ) | (((type) & 0x07) << 3) |
| #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:
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.
| #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).
| #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.
| #define SOFAB_OBJECT_ASSERT_EXTENT | ( | n | ) |
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.
| #define SOFAB_OBJECT_ASSERT_FIELD_COUNT | ( | n | ) |
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).
| #define SOFAB_OBJECT_ASSERT_FITS_PROFILE | ( | obj, | |
| field, | |||
| id | |||
| ) |
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.
| #define SOFAB_OBJECT_ASSERT_LEN_ADJACENT | ( | obj, | |
| dfield, | |||
| lfield | |||
| ) |
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).
| #define SOFAB_OBJECT_ASSERT_LEN_FIRST | ( | obj, | |
| lfield | |||
| ) |
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.
| #define SOFAB_OBJECT_ASSERT_SLOT_COUNT | ( | n | ) |
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.
| #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).
| field_list | Array of sofab_object_descr_field_t for this object. |
| field_count | Number of entries in field_list. |
| nested_list | Array of pointers to nested sofab_object_descr_t (may be NULL). |
| nested_count | Number of entries in nested_list. |
| #define SOFAB_OBJECT_DESCR_BIG 3 |
| #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.
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.
| #define SOFAB_OBJECT_DESCR_EXTENT_MAX 255u |
Bytes a PREFIX default image may describe (SOFAB_OBJECT_DESCR_WITH_PREFIX_DEFAULTS).
| #define SOFAB_OBJECT_DESCR_EXTENT_SHIFT 8 |
| #define SOFAB_OBJECT_DESCR_FIELD_COUNT_MAX 255u |
| #define SOFAB_OBJECT_DESCR_MEDIUM 2 |
| #define SOFAB_OBJECT_DESCR_PROFILE SOFAB_OBJECT_DESCR_MEDIUM |
| #define SOFAB_OBJECT_DESCR_SEQ | ( | field_list, | |
| field_count, | |||
| nested_list, | |||
| nested_count | |||
| ) |
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.
| field_list | Array of sofab_object_descr_field_t (the element slots). |
| field_count | Number of element slots (the array capacity N). |
| nested_list | Array of pointers to nested sofab_object_descr_t (may be NULL). |
| nested_count | Number of entries in nested_list. |
| #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.
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:
[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.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.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).
| field_list | Array of sofab_object_descr_field_t (the element slots). |
| field_count | Number of element slots (the array capacity N). |
| nested_list | Array of pointers to nested sofab_object_descr_t (may be NULL). |
| nested_count | Number of entries in nested_list. |
| obj | The holder struct type. |
| lfield | Used-length member (in elements), declared first in obj (at offset 0), ahead of the element slots. |
| #define SOFAB_OBJECT_DESCR_SLOT_COUNT_MAX 65535u |
Element slots a wrapper-array holder may declare (SOFAB_OBJECT_DESCR_SEQ).
| #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.
UINT32_MAX>>3, because the field header is itself a varint of (id<<3)|type.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.
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)
| #define SOFAB_OBJECT_DESCR_UNION | ( | field_list, | |
| field_count, | |||
| nested_list, | |||
| nested_count, | |||
| default_image, | |||
| obj, | |||
| tfield | |||
| ) |
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.
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.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).
| field_list | Array of sofab_object_descr_field_t (one per option). |
| field_count | Number of options. |
| nested_list | Array of pointers to nested sofab_object_descr_t (may be NULL). |
| nested_count | Number of entries in nested_list. |
| default_image | Pointer to the prefix default image, or NULL (tag 0, default_id option all-zero or a sequence). |
| obj | The union object type. |
| tfield | The tag member, a sofab_object_descr_id_t declared first in obj (at offset 0). |
| #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.
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.
| field_list | Array of sofab_object_descr_field_t for this object. |
| field_count | Number of entries in field_list. |
| nested_list | Array of pointers to nested sofab_object_descr_t (may be NULL). |
| nested_count | Number of entries in nested_list. |
| default_struct | Pointer 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. |
| #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.
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.
| field_list | Array of sofab_object_descr_field_t for this object. |
| field_count | Number of entries in field_list. |
| nested_list | Array of pointers to nested sofab_object_descr_t (may be NULL). |
| nested_count | Number of entries in nested_list. |
| default_prefix | Pointer to a prefix of the object holding the field defaults. |
| #define SOFAB_OBJECT_FIELD | ( | id, | |
| obj, | |||
| field, | |||
| type | |||
| ) |
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).
| id | Field ID on the wire. |
| obj | Enclosing struct type (e.g. struct my_msg). |
| field | Member name within obj. |
| type | Field type tag (one of the scalar SOFAB_OBJECT_FIELDTYPE_*). |
| #define SOFAB_OBJECT_FIELD_ARRAY | ( | id, | |
| obj, | |||
| field, | |||
| type | |||
| ) |
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.
| id | Field ID on the wire. |
| obj | Enclosing struct type. |
| field | Array member within obj. |
| type | Field type tag (one of the array SOFAB_OBJECT_FIELDTYPE_ARRAY_*). |
| #define SOFAB_OBJECT_FIELD_ARRAY_SIZED | ( | id, | |
| obj, | |||
| dfield, | |||
| lfield, | |||
| type | |||
| ) |
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.
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.
| id | Field ID on the wire. |
| obj | Enclosing struct type. |
| dfield | Array member within obj (its size is the capacity). |
| lfield | Used-length member (in elements), declared immediately before dfield. |
| type | Field type tag (one of the array SOFAB_OBJECT_FIELDTYPE_ARRAY_*). |
| #define SOFAB_OBJECT_FIELD_BLOB_SIZED | ( | id, | |
| obj, | |||
| dfield, | |||
| lfield | |||
| ) |
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.
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.| id | Field ID on the wire. |
| obj | Enclosing struct type. |
| dfield | Blob buffer member within obj (its size is the capacity). |
| lfield | Used-length member, declared immediately before dfield. |
| #define SOFAB_OBJECT_FIELD_SEQUENCE | ( | id, | |
| obj, | |||
| field, | |||
| type, | |||
| idx | |||
| ) |
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.
| id | Field ID on the wire. |
| obj | Enclosing struct type. |
| field | Nested struct member within obj. |
| type | Field type tag (typically SOFAB_OBJECT_FIELDTYPE_SEQUENCE). |
| idx | Index of the nested descriptor in the nested_list. |
| #define SOFAB_OBJECT_FIELDTYPE_ARRAY_BOOLEAN 0xA |
| #define SOFAB_OBJECT_FIELDTYPE_ARRAY_FP32 0x8 |
| #define SOFAB_OBJECT_FIELDTYPE_ARRAY_FP64 0x9 |
| #define SOFAB_OBJECT_FIELDTYPE_ARRAY_SIGNED 0x7 |
| #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).
| #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).
| #define SOFAB_OBJECT_FIELDTYPE_BOOLEAN 0xC |
| #define SOFAB_OBJECT_FIELDTYPE_FP32 0x2 |
| #define SOFAB_OBJECT_FIELDTYPE_FP64 0x3 |
| #define SOFAB_OBJECT_FIELDTYPE_SEQUENCE 0xB |
| #define SOFAB_OBJECT_FIELDTYPE_SIGNED 0x1 |
| #define SOFAB_OBJECT_FIELDTYPE_STRING 0x4 |
| #define SOFAB_OBJECT_FIELDTYPE_UNSIGNED 0x0 |
| #define SOFAB_OBJECT_SEQ_HOLDER 0x01u |
| #define SOFAB_OBJECT_SEQ_LEN_SHIFT 1 |
| #define SOFAB_OBJECT_UNION 0x80u |
| #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.
| #define SOFAB_STRICT_UTF8 0 |
Wire-construct switches — what a reduced build does on the wire.
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):
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.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.
| typedef char sofab_check_size_char_t[(sizeof(char)==1) ? 1 :-1] |
| 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_int16_t[(sizeof(int16_t)==2) ? 1 :-1] |
| typedef char sofab_check_size_int32_t[(sizeof(int32_t)==4) ? 1 :-1] |
| typedef char sofab_check_size_int64_t[(sizeof(int64_t)==8) ? 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_uint32_t[(sizeof(uint32_t)==4) ? 1 :-1] |
| typedef char sofab_check_size_uint64_t[(sizeof(uint64_t)==8) ? 1 :-1] |
| typedef char sofab_check_size_uint8_t[(sizeof(uint8_t)==1) ? 1 :-1] |
| typedef uint32_t sofab_id_t |
| typedef struct sofab_istream_decoder 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.
| 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:
| ctx | Pointer to the active input stream context. |
| id | Field ID that was encountered. |
| size | Size of the field's value in bytes (for fixed-length types). |
| count | Number of elements (for array types). |
| usrptr | Optional user-provided pointer (provided at initialization). |
| typedef struct sofab_istream sofab_istream_t |
| 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 struct sofab_object_descr sofab_object_descr_t |
Description of a SofaBuffer object structure.
| 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.
| usrptr | User-provided pointer. |
| data | Pointer to the buffer that contains bytes to flush. |
| len | Number of bytes available at data to flush. |
| typedef struct sofab_ostream sofab_ostream_t |
| typedef int64_t sofab_signed_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
| enum sofab_fixlentype_t |
| enum 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 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 |
| enum sofab_type_t |
|
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):
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.
| ctx | Pointer to the input stream context. |
| data | Pointer to the raw serialized data (may be NULL when datalen is 0). |
| datalen | Length of data in bytes. |
|
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.
| ctx | Pointer to the input stream context. |
| field_callback | User callback invoked for each field ID. |
| usrptr | Optional user pointer passed to the callback. |
|
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.
| ctx | Pointer to the input stream context. |
|
extern |
Reads an array of elements.
| ctx | Pointer to the input stream context. |
| var | Destination array. |
| element_count | Number of fixed-size elements. |
| element_size | Size of each element in bytes. |
| opt | Field options (type and flags). |
|
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).
| ctx | Pointer to the input stream context. |
| var | Destination array. |
| element_count | Number of elements to read. |
Definition at line 623 of file istream.h.
|
inlinestatic |
|
inlinestatic |
|
inlinestatic |
|
inlinestatic |
|
inlinestatic |
|
inlinestatic |
|
inlinestatic |
|
inlinestatic |
|
inlinestatic |
|
inlinestatic |
|
inlinestatic |
|
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.
| ctx | Pointer to the input stream context. |
| var | Pointer to destination variable. |
Definition at line 426 of file istream.h.
|
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.
| ctx | Pointer to the input stream context. |
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.
| ctx | Pointer to the input stream context. |
| var | Output variable. |
| varlen | Size of the output variable in bytes. |
| opt | Field options (type and flags). |
|
inlinestatic |
|
inlinestatic |
|
inlinestatic |
|
inlinestatic |
|
inlinestatic |
|
inlinestatic |
|
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.
| ctx | Pointer to the input stream context. |
| decoder | Decoder instance used for the new sequence. |
| field_callback | Callback invoked for fields inside the nested sequence. |
| usrptr | Optional user pointer for the nested callback. |
|
inlinestatic |
|
inlinestatic |
Reads a string of fixed maximum size.
No null terminator is appended to the end of the string.
| ctx | Pointer to the input stream context. |
| var | Destination buffer. |
| varlen | Maximum number of bytes to read. |
Definition at line 488 of file istream.h.
|
inlinestatic |
|
inlinestatic |
|
inlinestatic |
|
inlinestatic |
|
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.
What "last index" means here. It is the array's length - 1, and where the length comes from is the holder descriptor's business:
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.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.
| ctx | Pointer to the output stream context. |
| info | Pointer to the object descriptor. |
| src | Pointer to the source object to serialize. |
|
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.
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.| ctx | Pointer to the active input stream context. |
| id | Field ID reported by the decoder. |
| size | Size of the field value in bytes (unused; kept for the callback signature). |
| count | Number of array elements (unused; kept for the callback signature). |
| usrptr | Pointer to the sofab_object_decoder_t driving this object. |
|
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.
| info | Pointer to the object descriptor. |
| obj | Pointer to the object structure to initialize. |
|
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.
| ctx | Pointer to the output stream context. |
| buffer | Pointer to the new buffer to use. |
| buflen | Size of buffer in bytes. |
| offset | Initial 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. |
|
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.
| ctx | Pointer to the output stream context. |
|
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).
| ctx | Pointer to the output stream context. |
|
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.
| ctx | Pointer to the output stream context to initialize. |
| buffer | Pointer to a writable buffer used to accumulate encoded bytes. |
| buflen | Size of buffer in bytes. |
| offset | Initial 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. |
| flush | Optional flush callback invoked when the buffer data must be handled. |
| usrptr | Optional user pointer passed to flush when invoked. |
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.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.
|
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.
| ctx | Pointer to the output stream context. |
| id | Field identifier. |
| data | Pointer to element data. |
| element_count | Number of elements. |
| element_size | Size of each element in bytes. |
| type | Semantic fixed-length type of elements. |
|
inlinestatic |
Write an array of 32-bit floating-point values.
| ctx | Pointer to the output stream context. |
| id | Field identifier. |
| data | Pointer to float array. |
| element_count | Number of float elements. |
Definition at line 617 of file ostream.h.
|
inlinestatic |
Write an array of 64-bit floating-point values.
| ctx | Pointer to the output stream context. |
| id | Field identifier. |
| data | Pointer to double array. |
| element_count | Number of double elements. |
Definition at line 635 of file ostream.h.
|
inlinestatic |
Writes an array of 16-bit signed integers.
| ctx | Output stream context. |
| id | Field identifier. |
| var | Destination array. |
| element_count | Number of elements to write. |
Definition at line 518 of file ostream.h.
|
inlinestatic |
Writes an array of 32-bit signed integers.
| ctx | Output stream context. |
| id | Field identifier. |
| var | Destination array. |
| element_count | Number of elements to write. |
Definition at line 550 of file ostream.h.
|
inlinestatic |
Writes an array of 64-bit signed integers.
| ctx | Output stream context. |
| id | Field identifier. |
| var | Destination array. |
| element_count | Number of elements to write. |
Definition at line 583 of file ostream.h.
|
inlinestatic |
Writes an array of 8-bit signed integers.
| ctx | Output stream context. |
| id | Field identifier. |
| var | Destination array. |
| element_count | Number of elements to write. |
Definition at line 486 of file ostream.h.
|
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.
| ctx | Pointer to the output stream context. |
| id | Field identifier. |
| data | Pointer to the array data. |
| element_count | Number of elements in data. |
| element_size | Size of each element in bytes. |
|
inlinestatic |
Writes an array of 16-bit unsigned integers.
| ctx | Output stream context. |
| id | Field identifier. |
| var | Destination array. |
| element_count | Number of elements to write. |
Definition at line 534 of file ostream.h.
|
inlinestatic |
Writes an array of 32-bit unsigned integers.
| ctx | Output stream context. |
| id | Field identifier. |
| var | Destination array. |
| element_count | Number of elements to write. |
Definition at line 566 of file ostream.h.
|
inlinestatic |
Writes an array of 64-bit unsigned integers.
| ctx | Output stream context. |
| id | Field identifier. |
| var | Destination array. |
| element_count | Number of elements to write. |
Definition at line 599 of file ostream.h.
|
inlinestatic |
Writes an array of 8-bit unsigned integers.
| ctx | Output stream context. |
| id | Field identifier. |
| var | Destination array. |
| element_count | Number of elements to write. |
Definition at line 502 of file ostream.h.
|
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.
| ctx | Pointer to the output stream context. |
| id | Field identifier. |
| data | Pointer to the array data. |
| element_count | Number of elements in data. |
| element_size | Size of each element in bytes. |
|
inlinestatic |
Write a binary blob field.
Convenience wrapper that writes raw bytes as a blob fixed-length field.
| ctx | Pointer to the output stream context. |
| id | Field identifier. |
| data | Pointer to binary data. |
| size | Number of bytes to write. |
Definition at line 400 of file ostream.h.
|
inlinestatic |
Write a boolean value (1 byte).
Convenience wrapper that encodes a boolean as an unsigned value (0 or 1).
| ctx | Pointer to the output stream context. |
| id | Field identifier. |
| value | Boolean value to encode. |
Definition at line 331 of file ostream.h.
|
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.
| ctx | Pointer to the output stream context. |
| id | Field identifier. |
| data | Pointer to bytes to write (may be NULL if datalen is 0). |
| datalen | Number of bytes to write from data. |
| type | Semantic fixed-length type (sofab_fixlentype_t). |
|
inlinestatic |
Write a 32-bit floating-point value.
| ctx | Pointer to the output stream context. |
| id | Field identifier. |
| value | Float value to encode. |
Definition at line 347 of file ostream.h.
|
inlinestatic |
Write a 64-bit floating-point value.
| ctx | Pointer to the output stream context. |
| id | Field identifier. |
| value | Double value to encode. |
Definition at line 363 of file ostream.h.
|
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).
| ctx | Pointer to the output stream context. |
| id | Field identifier representing the sequence. |
|
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():
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.
| ctx | Pointer to the output stream context. |
| id | Field identifier representing the sequence. |
|
extern |
End encoding a nested sequence (end marker).
Writes the termination marker for the current nested sequence started via sofab_ostream_write_sequence_begin().
| ctx | Pointer to the output stream context. |
|
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).
| ctx | Pointer to the output stream context. |
|
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.
| ctx | Pointer to the output stream context. |
| id | Field identifier. |
| value | Signed integer value to encode. |
|
inlinestatic |
Write a null-terminated string.
Convenience wrapper that writes a string field by using strlen() to determine length.
| ctx | Pointer to the output stream context. |
| id | Field identifier. |
| text | Null-terminated C string to write. |
Definition at line 382 of file ostream.h.
|
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.
| ctx | Pointer to the output stream context. |
| id | Field identifier (sofab_id_t). |
| value | Unsigned integer value to encode. |