|
SofaBuffers C/C++ 0.12.0
API Documentation
|

Structured Objects For Anyone
... so optimized, feels amazing.
The footprint-optimized SofaBuffers (Sofab) codec: a heap-free C99 object API plus a header-only C++20 wrapper (sofab/sofab.hpp) over the same C core. It packs structured fields into a caller-owned buffer and decodes them through a small, callback-driven streaming decoder — no allocator, no dependencies, no code generator required. It runs from bare-metal MCUs (C) up to IoT-class devices (the C++ wrapper), and is the authoritative source of the shared conformance vectors in assets/test_vectors.json.
For desktop/server C++ where throughput beats code size, see the sibling pure-C++20 port corelib-cpp and Choosing between the two C/C++ corelibs.
.c files and headers; drop it into any build.None. The C core uses only freestanding-friendly standard headers (<stdint.h>, <stddef.h>, <string.h>, …). The C++ wrapper is header-only and pulls in only the C++ standard library; its heap-free subset (OStreamInline, FixedString, FixedBytes, InlineVector) compiles under -fno-exceptions -fno-rtti.
CI builds the corelib across many architectures and endiannesses (non-native targets run under QEMU user-mode emulation):
| Target | Toolchain | Runs |
|---|---|---|
| x86_64 (little endian) | GCC and Clang | build + full test suite |
| AArch64 | GCC | build + test suite under QEMU |
| MIPS (big endian) | GCC | build + test suite under QEMU |
| PowerPC (big endian) | GCC | build + test suite under QEMU |
| RISC-V 64 (little endian) | GCC | build + test suite under QEMU |
| Cortex-M0/M3/M7/M23/M55 | arm-none-eabi | bare-metal build |
| RISC-V 32 (rv32i, rv32imc, rv32ec) | riscv64-unknown-elf | bare-metal build |
| RL78 | LLVM for RL78 | bare-metal build |
| AVR / ATmega | avr-gcc | bare-metal build, on demand |
Every target builds all four configurations — full, full-strict, minimal and minimal-noobj — and reports separately. The CI badge above covers all of them; the CI run shows the per-target result.
Distributed as the port sofa-buffers-corelib-c-cpp; every route exposes the same target sofa-buffers::corelib and #include <sofab/…>.
CMake (FetchContent):
Conan (installed package, then in CMake):
Arduino / PlatformIO — the corelib ships an Arduino manifest (library.properties) and a PlatformIO manifest (library.json).
| Goal | How |
|---|---|
| No allocator | The codec allocates nothing: every context, decoder and buffer is caller-provided. The C core has no heap at all; in C++ the heap appears only where you choose a growable destination. |
| No dependencies | No third-party libraries, so it drops into any toolchain. |
| Streaming out | sofab_ostream writes into a small caller buffer, invoking a flush callback when it fills — a message can exceed available RAM. |
| Streaming in | sofab_istream is a callback-driven decoder fed arbitrary byte chunks; large payloads arrive in pieces. |
| Reserve-offset | sofab_ostream_init takes a start offset leaving room for a lower-layer header (saves a copy). |
| Usable without a generator | The field-level API is explicit enough to write by hand; the descriptor-driven object API stays optional. |
| C++ without surprises | The wrapper reports errors through a sticky Result instead of throwing, and avoids std::iostream. |
| Portable | Plain C99 / C++20 with explicit endianness handling. |
| Small footprint | SOFAB_DISABLE_* switches drop whole code paths; -Os gets down to ~1 KB of .text (see Footprint). |
Four use cases — serialize a message that fits one buffer, serialize one too large for it, deserialize a whole message, deserialize one arriving in chunks — plus the generated-code path that wraps them. The examples use the C core; the header-only C++ wrapper mirrors each through sofab::OStreamInline / sofab::IStreamObject — or sofab::IStreamInto, which decodes into a message you already hold instead of owning one.
Init a sofab_ostream_t over a buffer large enough for the whole message, write fields, then read the byte count:
For a message larger than the buffer, pass a flush callback. It fires whenever the buffer fills (and on the final sofab_ostream_flush()), so a small scratch buffer streams out an arbitrarily large message:
Decoding is callback-driven: feed the bytes and, inside the callback, bind each field to a destination with a sofab_istream_read_*() call. Unread fields are skipped automatically.
A field is skipped just the same when the read_* bound for it does not match the wire type (MESSAGE_SPEC §7.3): the decode continues and the destination keeps its previous value. That is not an error — it is what lets peers on different schema revisions keep talking. sofab_istream_skipped() counts it.
sofab_istream_feed() accepts arbitrarily small chunks and resumes at any byte boundary, driving the same callback — so it decodes a piecewise stream identically to a one-shot buffer, no matter where the chunks come from:
The feed outcome is three-valued, with no separate finalize step:
| return | meaning |
|---|---|
SOFAB_RET_OK | the bytes consumed so far end exactly on a field boundary — a complete message |
SOFAB_RET_INCOMPLETE | the bytes end inside a field (a partial varint, or a fixlen/array payload shorter than declared) or with an open sequence — a valid but partial decode, not an error. Feed more bytes to continue; the caller owns end-of-input |
SOFAB_RET_E_INVALID_MSG | the bytes are malformed regardless of what follows (varint too wide, length/count/id over the limit, bad type/subtype, …) |
Truncated input is therefore reported as SOFAB_RET_INCOMPLETE — it is neither silently accepted as complete nor rejected as invalid. In the C++ wrapper the same three outcomes are surfaced by IStream::feed's Result: ok() (complete), incomplete() (partial), and invalid() (malformed). It has a fourth, limitExceeded(), for a receiver ceiling crossed on a schema-unbounded field — see Memory handling.
Sequences nest at most SOFAB_MAX_DEPTH (255) deep; deeper is SOFAB_RET_E_INVALID_MSG. The limit is enforced on the skip path, where the depth counter is a uint8_t; actively-decoded nesting is bounded instead by the number of caller-provided decoder handles.
A fixlen field's declared length and an array's element count are bounded by SOFAB_FIXLEN_MAX / SOFAB_ARRAY_MAX; a message declaring more is rejected with SOFAB_RET_E_INVALID_MSG, and so is a sofab_ostream_write_* call that asks for more (SOFAB_RET_E_ARGUMENT). The wire format puts both ceilings at 2,147,483,647 and lets a constrained profile lower them to 65,535. This port takes that allowance where — and only where — size_t is too narrow to hold the wider one:
| target | SOFAB_FIXLEN_MAX / SOFAB_ARRAY_MAX |
|---|---|
size_t 32-bit or wider (ARM, RISC-V, x86, …) | 2,147,483,647 |
size_t 16-bit (AVR / ATmega) | 65,535 |
Both are derived from SIZE_MAX rather than listed per target, because the decoder narrows the declared length and count into a size_t: a ceiling above SIZE_MAX would admit a value the target cannot represent and truncate it instead of rejecting it. Override either with -DSOFAB_FIXLEN_MAX=… if a profile wants the constrained ceiling on a wide target; a value that does not fit in size_t fails the build.
sofabgen is the schema compiler. For C it targets the descriptor-driven object API — a plain struct plus _encode / _decode, so one generic transcoder serves every message and flash stays small:
For C++ (sofabgen --lang cpp) it emits a struct deriving sofab::Message (the sofab::OStreamMessage + sofab::IStreamMessage pair) with encode() / decode() helpers over the same wire format:
The full wire format ships by default; the C core can be trimmed at compile time. What each switch is worth in bytes is measured under Footprint. A switch also narrows what the build accepts on the wire — read the callout below the table before enabling one.
Prefer the CMake option over the bare macro. Every switch except the one marked macro only is a CMake option that src/CMakeLists.txt applies PUBLIC, so the library and every consumer of its headers are configured identically. Two of these macros change a public struct's layout, so a library and a consumer that disagree is a silent ABI mismatch, not a warning. Passing the macro through CMAKE_C_FLAGS also breaks on the cross-toolchain files under utils/, which set() that variable and discard the command line.
| Switch | Set with | Default | Effect |
|---|---|---|---|
SOFAB_DISABLE_FIXLEN_SUPPORT | CMake option | off | Drop fixed-length fields: floats, strings, and blobs |
SOFAB_DISABLE_ARRAY_SUPPORT | CMake option | off | Drop array fields (scalar arrays and fixed-length arrays) |
SOFAB_DISABLE_SEQUENCE_SUPPORT | CMake option | off | Drop nested sequence framing |
SOFAB_DISABLE_LAZY_SEQ_SUPPORT | CMake option | off | Drop the hold-back sequence openers (..._begin_lazy / ..._end_keep) and the pending-run state in sofab_ostream_t. Takes back 276 B of ARMv6-m .text, 36 B of RAM per stream and 6 Ir per typical encode — see Sequence framing; a pure-C consumer encoding through sofab_object_encode() never needs them. Changes the sofab_ostream_t layout (and is rejected by the C++ wrapper) |
SOFAB_DISABLE_FP64_SUPPORT | CMake option | off | Drop 64-bit float (fp64); auto-defined where double is not 8 bytes |
SOFAB_DISABLE_INT64_SUPPORT | CMake option | off | Narrow scalar varints from 64-bit to 32-bit (drops the u64/i64 helpers) |
SOFAB_DISABLE_INTEGER_OVERFLOW_CHECK | CMake option | off | Skip integer overflow checks when decoding (smaller/faster, less safe) |
SOFAB_DISABLE_ENCODE_BOUNDS | CMake option | off | Remove the encode-side refusal of an over-bound value: sofab_object_encode() no longer returns SOFAB_RET_E_ARGUMENT for a string member that fills its char[maxlen + 1] without a terminator (the read stays bounded), and the C++ wrapper's sofab::ENCODE_BOUNDS is false. Sized blob lengths and array counts are clamped to the capacity either way |
SOFAB_DISABLE_OBJECT_API | CMake option | off | Exclude the descriptor-driven object API (object.c) and leave the bare stream corelib |
A switch that removes a wire construct makes the decoder reject messages that carry it.
SOFAB_DISABLE_FIXLEN_SUPPORT,_ARRAY_,_SEQUENCE_,_FP64_and_INT64_take the construct out of the format this build speaks, not just out of the API: such a message isSOFAB_RET_E_INVALID_MSG, terminally — every latersofab_istream_feedreturns it and no callback fires, untilsofab_istream_initresets the context.That includes fields you never asked for. The decoder is schema-agnostic, so an array in an unknown id is refused just like one in a field you read. A full build skips both (MESSAGE_SPEC §7.3); a reduced build cannot, because the code that steps over the construct is the code the switch removed. Keeping it — parsing only in order to skip — costs about 375 B of ARMv6-m
.textin the Minimal profile.The switch is therefore an interop bound. Such a build only talks to peers that never emit the construct, in any field — including fields a later schema revision adds. Enable it where both ends of the link are yours and the wire profile is fixed, not on a receiver exposed to messages you do not control.
Nothing is silently dropped, but the rejection lands mid-message: fields before the offending one have already reached your callback.
SOFAB_RET_E_INVALID_MSGcondemns the whole message — discard the partially filled destinations.
**
SOFAB_DISABLE_LAZY_SEQ_SUPPORTchanges the wire output rather than removing a wire type.** An all-default sequence field is framed empty instead of omitted (MESSAGE_SPEC §2) — well-formed, same value, accepted by every decoder, but no longer canonical. It drops an encoder mechanismsofab_object_encode()never uses, so it is a footprint switch for pure-C consumers; the C++ wrapper rejects it with an#error.
Two switches tune rather than remove:
| Switch | Set with | Default | Effect |
|---|---|---|---|
SOFAB_LAZY_SEQ_DEPTH | macro only | 8 | How many nested sequence headers can be held back at once — this profile's documented hold-back bound, see Sequence framing. Costs 4 B of RAM per output stream per level; must be 1…255 (the run counter is a uint8_t, and a build outside that range is rejected with an #error) |
SOFAB_OBJECT_DESCR_PROFILE | CMake cache variable | SOFAB_OBJECT_DESCR_MEDIUM | Integer width of the object descriptor's members: SOFAB_OBJECT_DESCR_SMALL / _MEDIUM / _BIG = uint8_t / uint16_t / uint32_t. It sizes the descriptor tables in your code, not the library — the library's own .text barely moves and SMALL even costs a few bytes there (see Footprint). What each profile can describe is under Descriptor limits. Also in a public header, hence PUBLIC |
Two knobs are opt-IN (off by default in this footprint corelib — see below):
| Switch | Set with | Default | Effect |
|---|---|---|---|
SOFAB_ENABLE_STRICT_UTF8 | CMake option | off | Enable strict UTF-8 validation of string fields (see below); off by default so the validator costs zero .text/.rodata. Resolves to the boolean SOFAB_STRICT_UTF8, which a direct -DSOFAB_STRICT_UTF8=1 sets outright and wins over both knobs; the legacy SOFAB_DISABLE_STRICT_UTF8 still forces it off |
SOFAB_ENABLE_SKIP_COUNTER | CMake option | off | Count fields skipped because their wire type contradicted the read bound for them (§7.3), readable with sofab_istream_skipped(); a pure diagnostic no decode path reads, costing 18 B of .text when on. Resolves to SOFAB_SKIP_COUNTER, which -DSOFAB_SKIP_COUNTER=1 sets outright |
One knob is C++-only and normally needs no setting at all:
| Switch | Set with | Default | Effect |
|---|---|---|---|
SOFAB_CPP_HAVE_HOSTED | macro only | __STDC_HOSTED__ | Whether sofab.hpp exposes its hosted convenience layer — the std::string/std::vector overloads, sofab::OStream (a std::shared_ptr buffer owner) and the std::function callback typedefs. Defaults to the compiler's own answer, so -ffreestanding (what every MCU toolchain file here sets) selects the no-heap surface automatically; force it with -DSOFAB_CPP_HAVE_HOSTED=0/1. See The C++ wrapper on bare metal |
Strict UTF-8 (SOFAB_STRICT_UTF8, off by default).** This is a footprint/embedded corelib, so the strict UTF-8 check defaults OFF — the constrained-profile allowance in CORELIB_PLAN §6.4 (a documented non-strict build; the validator, utf8.c, and every gated site compile to nothing, for zero .text/.rodata cost). **With the check OFF a string's wire bytes are stored verbatim (byte-container behavior), never lossy — invalid bytes are neither validated nor silently replaced/dropped. Opt in with -DSOFAB_ENABLE_STRICT_UTF8 (or a direct -DSOFAB_STRICT_UTF8=1); conformance/fuzzer builds and this repo's CI enable it explicitly.
A string is UTF-8 (MESSAGE_SPEC §8); blob is the type for opaque bytes. With the check on, a string whose bytes are not valid UTF-8 is rejected symmetrically: decoding a materialized (read, not skipped) invalid string yields SOFAB_RET_E_INVALID_MSG, and encoding one with sofab_ostream_write_fixlen(..., SOFAB_FIXLENTYPE_STRING) yields SOFAB_RET_E_ARGUMENT. The validator (sofab_utf8_valid) is a real one — it rejects overlong encodings (incl. C0 80), surrogates, and code points above U+10FFFF, while allowing embedded U+0000. It is a validation policy, never a wire-format switch (it only decides accept-vs-reject and is never lossy), so peers with different settings interoperate on all valid data.
The C++ wrapper honors these switches: type-dispatch capabilities (FP64/INT64/ARRAY) become a static_assert only when a disabled type is used, while the structural ones (FIXLEN/SEQUENCE/LAZY_SEQ) underpin most of the C++ surface and are rejected outright with a #error.
**
SOFAB_DISABLE_INT64_SUPPORThas wire- and API-level side effects.** It shrinks 64-bit varint math, smaller and faster on 32-bit MCUs, but:
- Wire: values that fit in 32 bits stay byte-identical; a wider value from a 64-bit peer is rejected (
SOFAB_RET_E_INVALID_MSG), never truncated.- ABI: the value types are in public signatures — 32-bit and 64-bit builds are not ABI-compatible.
- Field ids:
SOFAB_ID_MAXshrinks toUINT32_MAX >> 3.- Conformance: the shipped vectors carry 64-bit values and will not decode.
The object API describes a message with constant tables, and the integers in those tables have fixed widths. SOFAB_OBJECT_DESCR_PROFILE sets three of them — a field's id, its offset in the struct and its size in bytes — and everything else is fixed. Every limit below is a compile error that names its cause, in hand-written descriptors and in code generated by sofabgen alike; none of them truncates a value silently.
What the descriptor can hold:
| Limit | _SMALL | _MEDIUM (default) | _BIG | Over it, the build fails with |
|---|---|---|---|---|
| Field or element id | 255 | 65 535 | 2 147 483 647¹ | field_exceeds_the_SOFAB_OBJECT_DESCR_PROFILE_id_offset_or_size_ceiling |
Bytes in one member (offset, size) | 255 | 65 535 | 4 294 967 295⁵ | same |
| Fields of one message, struct or union | 255 | 255 | 255 | an_object_may_not_declare_more_than_255_fields |
| Prefix default image | 255 B³ | 255 B | 255 B | a_prefix_default_image_may_not_exceed_255_bytes |
| Elements of a wrapper array | ² | ² | 65 535 | a_wrapper_array_may_not_declare_more_than_65535_slots |
| Sequence nesting depth | 255 | 255 | 255 | rejected by sofabgen (MAX_DEPTH) |
sizeof(sofab_object_descr_field_t), 32-bit target | 5 B | 8 B | 16 B | |
sizeof(sofab_object_descr_t), 32-bit target | 16 B | 16 B | 16 B |
¹ the descriptor holds 4 294 967 295; the wire stops at SOFAB_ID_MAX (2 147 483 647, or 536 870 911 with SOFAB_DISABLE_INT64_SUPPORT), which the generated header checks with its own #error naming the message. ² never reached: the array's bytes run into the size ceiling first (next table). ³ under _SMALL the member behind a longer prefix is already past the profile, which is the error that fires.
"Bytes in one member" is the limit a schema meets most often, because every member counts whole: a string is char[maxlen + 1], and a wrapper array — an array of string, blob, struct, union or arrays — is one member of its parent, however many elements it holds. What that leaves, per schema type:
| Schema field | _SMALL | _MEDIUM | _BIG |
|---|---|---|---|
string, maxlen | 254 | 65 534 | 2 147 483 647⁴ |
blob, maxlen | 255 | 65 535 | 2 147 483 647⁴ |
array of u8 / i8 / boolean, count | 255 | 65 535 | 2 147 483 647⁴ |
array of u16 / i16, count | 127 | 32 767 | 2 147 483 647⁴ |
array of u32 / i32 / fp32, count | 63 | 16 383 | 1 073 741 823⁵ |
array of u64 / i64 / fp64, count | 31 | 8 191 | 536 870 911⁵ |
array of string, maxlen: 24, count | 10 | 2 621 | 65 535 |
array of blob, maxlen: 1, count (smallest element) | 127 | 32 766 | 65 535 |
| union options | 255 | 255 | 255 |
⁴ the schema itself stops maxlen and count at 2 147 483 647, and so does the wire (SOFAB_FIXLEN_MAX, SOFAB_ARRAY_MAX). ⁵ follows from the 32-bit width; not compiled at the boundary, the struct would be gigabytes.
A compact array — numbers, booleans — is one descriptor however long it is, so its count is the byte ceiling divided by the element width. A wrapper array is one descriptor per element, and the generator lays it out as a leading count — as wide as the count needs (1 B up to 255, 2 B up to 65 535), and at least as wide as the element's alignment — followed by the elements, so its count is (byte ceiling − count width) / element size: a string of maxlen 24 is 25 B, which is where 10 and 2 621 come from. Its descriptor table also grows with count — 300 string elements are 2 400 B of .rodata under _MEDIUM — and decoding one element searches that table, so a full array costs time quadratic in count.
A prefix default image is what sofabgen emits when a struct declares defaults: the defaulted members first, the image only as long as they are, read through SOFAB_OBJECT_DESCR_WITH_PREFIX_DEFAULTS. The descriptor records how far it reaches in one byte, so a prefix stops at 255 B; past that the image has to be the whole object, through SOFAB_OBJECT_DESCR_WITH_DEFAULTS, which takes one of any size.
The generated header adds its own guards on top: a message that uses a construct the corelib was built without (SOFAB_DISABLE_FIXLEN_SUPPORT, _ARRAY_, _SEQUENCE_, _FP64_, _INT64_), an id past SOFAB_ID_MAX, or a prefix image on a corelib that cannot read one stops at an #error naming the message and the switch.
Every value without a footnote was verified by generating a schema at the limit and one past it with sofabgen and compiling both against each profile; ¹ and ⁴ are bounds of the schema and the wire, ⁵ follows from the width.
A nested structure is a sequence: a start marker, the child fields, an end marker. Whether that frame reaches the wire at all depends on the position (MESSAGE_SPEC §2):
The two message layers here reach that outcome differently:
sofab_object_encode() — the descriptor knows the whole object, so it tests each field against its default before opening anything: no window, no bound, canonical at every depth...._write_sequence_begin_lazy() (OStream::sequenceBeginLazy) holds the header back; the first field written into any enclosing sequence emits the whole run; ..._sequence_end() drops a frame that never got content, ..._sequence_end_keep() forces one out for an element position. Held-back ids are stream state, not buffer content, so a small output buffer still yields identical bytes.The bound:
SOFAB_LAZY_SEQ_DEPTH(default 8). This is a heap-free corelib — it cannot grow the pending run on demand, so it bounds it, which CORELIB_PLAN §6 permits a heap-free profile provided the bound is documented. In practice:
- Up to 8 nested lazily-opened sequences, output is canonical — an all-default sequence field is omitted.
- Deeper than that the run is committed and the sequence is framed eagerly. An all-default one then leaves a two-byte empty frame on the wire — well-formed and the same value, so a bounded and an unbounded encoder interoperate; what it is not is byte-identical.
- Raise it with
-DSOFAB_LAZY_SEQ_DEPTH=Nif a schema nests sequence fields deeper than 8 and byte-exact canonical output matters; each level costs 4 B of RAM per stream. Nesting itself is bounded bySOFAB_MAX_DEPTH(255), and exceeding the hold-back window is never an error.Corelibs that can allocate hold back to the full
SOFAB_MAX_DEPTHand are canonical at every depth. The C object API above already is; only the raw-stream path carries the window.
What it costs. The commit check sits in the one function every writer funnels through, so a default build pays it per field — including a pure-C consumer that never opens a lazy sequence. Callgrind, bench_c at -O3, against the same build with -DSOFAB_DISABLE_LAZY_SEQ_SUPPORT=ON:
| Workload | default | SOFAB_DISABLE_LAZY_SEQ_SUPPORT | delta |
|---|---|---|---|
| encode: typical message | 966 Ir/op | 960 Ir/op | +6 Ir/op (+0.6 %) |
| encode: composite | 16 164 Ir/op | 16 069 Ir/op | +95 Ir/op (+0.6 %) |
| encode: u64 array (1000) | 125 999 Ir/op | 125 998 Ir/op | +1 Ir/op (+0.001 %) |
| decode: typical message | 2 165 Ir/op | 2 165 Ir/op | none |
The cost is per field header, so a message of many small fields feels it and one of few wide ones does not; decoding is untouched. With the 276 B of ARMv6-m .text and 36 B of per-stream RAM under Footprint, that is the whole price, and SOFAB_DISABLE_LAZY_SEQ_SUPPORT takes all of it back for a build that only uses the descriptor-driven encoder.
bench_c measures the stream path with the eager opener, so these deltas are the per-write commit check alone; the hold-back's own work is what the encode: composite row adds, that being the message with an omitted all-default field.
The C core never allocates — every context, decoder and buffer is caller-provided. The C++ wrapper is heap-free too with inline buffers / FixedString / FixedBytes / InlineVector; only OStream(buflen) and the std::string / std::vector read overloads use the heap.
Encode (ostream) — the buffer is caller-provided. sofab_ostream_init() takes a writable buffer the stream never allocates, copies or frees; it advances a cursor. offset reserves room at the front for a lower-layer header. When the buffer fills (or on sofab_ostream_flush()) an optional flush callback drains it; with no callback a full buffer returns SOFAB_RET_E_BUFFER_FULL.
**SOFAB_MIN_OUTPUT_BUFFER is 1.** The smallest buffer the encoder accepts for streaming: every write goes through a byte-at-a-time push, so a field header, a fixlen_word, a varint value or a float element may each straddle a flush. A buffer installed with a flush callback must satisfy buflen - offset >= SOFAB_MIN_OUTPUT_BUFFER; one installed without has no minimum at all — it either holds the message or reports SOFAB_RET_E_BUFFER_FULL, so a caller sizing from a generated MAX_SIZE gets an exact fit.
No pass-through. A flush callback is only ever handed the output buffer it was installed with, never foreign memory — on every flush of every message. A string or blob payload is copied through that buffer like anything else, however large it is.
Decode (istream) — deferred-copy binding. A read_*() / read() call copies nothing: it records only where the value goes. Later feed() calls write into that destination. Two rules follow:
feed(). C++ read(std::string&) must therefore be pre-sized; FixedString / FixedBytes / InlineVector are safe, their inline storage never moving.SOFAB_RET_E_INVALID_MSG — in the C++ wrapper an oversized field has three possible answers, one per ceiling it broke, in the table below; unbound fields are skipped untouched.A fed chunk is borrowed only for the duration of feed() and may be reused the moment it returns; what a bound destination has not received yet is carried in the stream context, not in library-owned heap memory — there is none.
The C API has no unbounded field, so it carries no receiver caps. Every C read is handed a destination that already exists together with its size: sofab_istream_read_string(ctx, var, varlen), ..._read_blob(ctx, var, varlen) and ..._read_array(ctx, var, element_count, element_size, opt). The object API is the same model expressed as descriptors — SOFAB_OBJECT_FIELD_ARRAY derives its element count from sizeof(field) / sizeof(field[0]) and a blob or string field its length from sizeof(field). There is therefore no shape in which a field arrives at the C decoder without a bound, and a wire length or count above that bound is SOFAB_RET_E_INVALID_MSG. SOFAB_RET_E_LIMIT_EXCEEDED is declared in sofab.h because sofab_ret_t is the shared C/C++ code table, and no C API function returns it.
The C++ wrapper does have unbounded fields, and both bounds for them are yours. A std::string, std::vector or growable wrapper-array destination has no capacity of its own, so a field whose schema declares no maxlen / count would otherwise be sized by the wire alone. Neither number is this library's to hold: CORELIB_PLAN §6.2.1 gives the receiver caps to generated code — "the
visitor decides. The codec never invents a limit of its own and never clamps to
one" — and MESSAGE_SPEC §7 gives it the schema bounds, because "the corelib
cannot know the schema". What this library contributes is the report and the category: the size / count your handler already receives, and a verdict it can set.
You state the number; the read performs the comparison. §6.2.1 fixes the provenance of a limit, not the site of the check — "a corelib <strong>MAY</strong> take a limit as an argument and perform the check itself, and a port that does is conformant" — and this library does, because the check has to run behind the MESSAGE_SPEC §7.3 tag test (a field whose wire type contradicts the read is skipped, and a skipped field is never capped) and before anything is sized from the wire. Only the read can see both. Handing the number in makes that a property of the structure rather than of every caller's discipline.
Each of readString / readBlob / readArray therefore has two entry points, and which one a field uses is decided by which ceiling the schema left it:
The cap parameter is required and unsigned, so "no cap" has no spelling: §6.2.1 says a codec "**MUST NOT** read an omitted argument as *unlimited*", and a dynCap = -1 default is exactly that. A read handed no ceiling at all — no schema bound, no cap, and a growable destination — is refused as sofab::Error::InvalidArgument rather than decoded: nothing is wrong with the bytes and no limit was configured to raise, so what is missing is part of the call. State one of the two ceilings, or bind an inline destination.
The same split applies to a growable wrapper array, which announces no count and delivers no callback at the element index — there is no point at which your handler could check, so its collector carries the numbers you set and applies them to the element index, before the container is extended. Its collector is the static helper layer (§6.6.1) rather than the codec: the generated layer owns it, and no codec path calls it.
sofab::DynCap is why an omitted member cannot mean unlimited: a default-constructed one is unstated, and reaching it on a schema-unbounded field is InvalidArgument, not an uncapped decode. It cannot be built from a signed value either, so the old -1 cannot re-enter as SIZE_MAX.
A refused field ends the decode, and which answer feed() returns says what was wrong:
| what was exceeded | code | who applies it |
|---|---|---|
a maxlen / count the schema declares | sofab::Error::InvalidMessage | the read, from the bound you passed |
| a receiver cap, on a field the schema leaves unbounded | sofab::Error::LimitExceeded | the …Capped read, from the cap you passed |
| neither — the destination handed over is too short | sofab::Error::InvalidArgument | the library (§6.6.3) |
| neither — no ceiling was stated at all | sofab::Error::InvalidArgument | the library (§6.2.1) |
The last two rows are the only bounds this library judges without a number from you, and neither is a limit: one is the size of the storage you handed over, the other is the absence of any ceiling to judge by. Both are refused rather than grown into or truncated to.
All of them are terminal. A field the handler skips is never capped.
Build with CMake and a C99 / C++20 toolchain:
Three suites run under CTest:
test_c / test_cpp** — Unity and Catch2 unit tests over encoder, decoder and object API, including the three-valued decode outcome and the error paths. Built in the full-feature ("max") configuration only.test_vectors_c** — the shared conformance vectors in assets/test_vectors.json, each pairing a message with its exact serialized bytes (format in `assets/test_vectors_README.md`, generator in test/vectorgen). This repo is the authoritative source of that file for the whole family.test_readme_structure** — reads this README and holds it to CORELIB_PLAN §9: the section list and order, the badge block, the §9.5 examples, the §6.4/§9.6 facts, the §6.1.1 name set, and that every in-document link resolves.The vector suite also builds standalone and feature-flag-tolerant (sofab_vectortest): each vector declares its requires, so a SOFAB_DISABLE_* build skips what it cannot handle and reports run / skipped counts. CI runs it across a feature-flag matrix:
Build-shaping options; the wire-feature switches are listed under Feature flags and are CMake options too.
| Option | Default | Description |
|---|---|---|
SOFAB_BUILD_TESTS | ON | Build the C/C++ test suites (off for a package build — it skips the Unity/Catch2 FetchContent) |
SOFAB_ENABLE_CPP | ON | Build the C++ tests |
SOFAB_ENABLE_CPP_SMOKE | OFF | Build the Catch2-free C++ wrapper smoke test (for reduced configs) |
SOFAB_ENABLE_BENCH | ON | Build the benchmarks (bench_c/bench_cpp, perf_c/perf_cpp) |
SOFAB_ENABLE_COVERAGE | OFF | Enable code coverage instrumentation (-O0 -g --coverage) |
SOFAB_ENABLE_FUZZ | OFF | Enable fuzzing instrumentation (sanitizers) |
SOFAB_ENABLE_LTO | OFF | Build with LTO and strict aliasing (-O3 -flto -fstrict-aliasing) across the C/C++ boundary — the one configuration where a compiler sees a C++ type and the C code reading it together. CI runs it on x86_64 for both GCC and Clang; no sanitizer covers this, as UBSan does not model strict aliasing |
SOFAB_ENABLE_DOXYGEN | OFF | Build the doc target (API documentation) |
SOFAB_ENABLE_VECTORGEN | OFF | Build the JSON test-vector generator (see test/vectorgen) |
SOFAB_INSTALL | ON | Generate the install and CMake package-config rules (turn off when embedding via add_subdirectory) |
The three tools BENCH_SPEC requires of every corelib-*, built for both C and C++ (SOFAB_ENABLE_BENCH, on by default). The corelib is compiled into each at -O3, independent of the -Os library the rest of the project links.
| Tool | Measures | Use it for |
|---|---|---|
bench_c / bench_cpp | practical throughput in MB/s on the current hardware | seeing real-world speed on a concrete target |
perf_c / perf_cpp | intrinsic cost in CPU cycles/operation plus throughput | comparing algorithmic cost across changes or languages |
bench/run_callgrind.sh | instructions retired per operation (Callgrind Ir/op) | the number to quote: deterministic and independent of the host's clock and scheduler, so it compares across machines — and the signal a CI regression gate should use |
Convenience targets build and run them:
All three run BENCH_SPEC's shared datasets, so the numbers compare directly against every other port: a 1000-element u64 array, the 7-field typical message (37 B), the 12-field perf message (170 B), an unbounded 1 MB blob (one-shot, streamed through a 4096-byte buffer with a flush sink, and decoded in 4096-byte chunks) and the composite message (956 B), whose wrapper array, non-ASCII UTF-8, depth-3 nesting, omitted all-default field and two-byte field header reach paths the flat datasets never touch. The last two encoded sizes (1,000,005 and 956 B) are cross-port parity checks; bench_c refuses to print a table if the first does not hold.
Read the blob 1MB rows only in Ir/op: five of that message's bytes are metadata and a million are payload, so its MB/s is the machine's memory bandwidth. They are also the only rows that exercise the divisible-run path at all. There is no encode: blob 1MB passthrough row, in this port or any other: pass-through was withdrawn, and a payload run is always copied through the installed output buffer (see Memory handling).
Because the C core never allocates, .data/.bss are 0.0KB and the whole cost is .text (flash). The tables are size libsofabuffers.a at -Os, regenerated by tools/footprint.sh. CI builds one job per architecture and configuration; atmega8 is the exception, its workflow being on manual dispatch until the test image links again.
The AVR rows carry one flag beyond -Os: -mcall-prologues, which routes every prologue and epilogue through libgcc's shared save/restore pair. It is worth 1022 B of the atmega8 Full row and is applied by the top-level CMakeLists.txt for that processor only, so a hand-rolled AVR build without it measures about 1 KB more. In a linked image it nets 912 B — the shared pair costs 110 B once, which a firmware built with the same flag amortises. It buys flash with cycles, which is the trade this library makes on these parts. The equivalents elsewhere do not pay and are deliberately not set: RISC-V -msave-restore saves 308 B in the archive but only 56 B in an image (its helper set costs 1284 B there), and on ARMv6-m every comparable knob measured ±0 B or worse.
Full configuration
| Architecture | .text | .data | .bss |
|---|---|---|---|
| ARMv6-m | ~3.8KB | 0.0KB | 0.0KB |
| ARMv7-m+fp.dp | ~3.8KB | 0.0KB | 0.0KB |
| RV32IMC | ~4.9KB | 0.0KB | 0.0KB |
| atmega8 | ~7.4KB | 0.0KB | 0.0KB |
Full configuration, strict UTF-8 on — the only rows where the validator (utf8.c) is compiled in. The delta over Full above is its entire .text/.rodata cost (~0.2–0.5 KB), which the default build does not pay:
| Architecture | .text | .data | .bss |
|---|---|---|---|
| ARMv6-m | ~4.1KB | 0.0KB | 0.0KB |
| ARMv7-m+fp.dp | ~4.0KB | 0.0KB | 0.0KB |
| RV32IMC | ~5.2KB | 0.0KB | 0.0KB |
| atmega8 | ~7.8KB | 0.0KB | 0.0KB |
The hold-back framing is part of those Full rows: 276 B on ARMv6-m, 398 B on atmega8. A pure-C consumer encoding only through sofab_object_encode() takes it back out with SOFAB_DISABLE_LAZY_SEQ_SUPPORT — ARMv6-m returns to 3662 B, sofab_ostream_t shrinks from 56 B to 20 B per stream, and a typical encode drops 6 Ir/op. The Minimal rows below disable sequences outright and are unaffected.
Minimal configuration — SOFAB_DISABLE_FIXLEN_SUPPORT, SOFAB_DISABLE_ARRAY_SUPPORT, SOFAB_DISABLE_SEQUENCE_SUPPORT, SOFAB_DISABLE_INTEGER_OVERFLOW_CHECK and the SOFAB_OBJECT_DESCR_SMALL profile:
| Architecture | .text | .data | .bss |
|---|---|---|---|
| ARMv6-m | ~1.1KB | 0.0KB | 0.0KB |
| ARMv7-m+fp.dp | ~1.1KB | 0.0KB | 0.0KB |
| RV32IMC | ~1.4KB | 0.0KB | 0.0KB |
| atmega8 | ~2.4KB | 0.0KB | 0.0KB |
Same minimal configuration, additionally without object.c (SOFAB_DISABLE_OBJECT_API):
| Architecture | .text | .data | .bss |
|---|---|---|---|
| ARMv6-m | ~0.7KB | 0.0KB | 0.0KB |
| ARMv7-m+fp.dp | ~0.7KB | 0.0KB | 0.0KB |
| RV32IMC | ~0.9KB | 0.0KB | 0.0KB |
| atmega8 | ~1.6KB | 0.0KB | 0.0KB |
The four configurations above answer how small can it get. They do not answer what is this one switch worth, which is the question you have when you need exactly one feature gone. Each row below is the full configuration plus that one switch on ARMv6-m — the smallest row above, and the one an MCU budget is usually measured against. Same -Os build, same tool, and regenerated by the same tools/footprint.sh:
| Switch | .text | delta |
|---|---|---|
| *(full, the baseline)* | 3938 B | — |
SOFAB_DISABLE_OBJECT_API | 2282 B | −1656 B |
SOFAB_DISABLE_ARRAY_SUPPORT | 2972 B | −966 B |
SOFAB_DISABLE_SEQUENCE_SUPPORT | 3002 B | −936 B |
SOFAB_DISABLE_FIXLEN_SUPPORT | 3152 B | −786 B |
SOFAB_DISABLE_INT64_SUPPORT | 3624 B | −314 B |
SOFAB_DISABLE_LAZY_SEQ_SUPPORT | 3662 B | −276 B |
SOFAB_DISABLE_INTEGER_OVERFLOW_CHECK | 3864 B | −74 B |
SOFAB_DISABLE_FP64_SUPPORT | 3896 B | −42 B |
SOFAB_OBJECT_DESCR_PROFILE=…_BIG | 3940 B | +2 B |
SOFAB_OBJECT_DESCR_PROFILE=…_SMALL | 3954 B | +16 B |
SOFAB_ENABLE_SKIP_COUNTER | 3956 B | +18 B |
SOFAB_ENABLE_STRICT_UTF8 | 4184 B | +246 B |
Three rows need a word:
SOFAB_OBJECT_DESCR_PROFILE barely moves the library and SMALL makes it slightly bigger.** The profile sizes the descriptor members in your tables, which is where the saving lands; narrower members cost the library a few widening instructions. Choose it for the descriptors, not the corelib.SOFAB_DISABLE_INTEGER_OVERFLOW_CHECK buys 74 B** by giving up a decode safety check. It is in Minimal because that profile targets trusted, schema-bounded links; it is a poor trade on untrusted input.Every table above is the C core alone. libsofabuffers.a is built from .c files only, and the C++ wrapper is header-only — it contributes 0 B to the archive in every configuration, so no row here moves when it changes. Its cost lands in your translation unit instead, and it is paid per TU, because the templates instantiate per TU.
For one representative message — five scalar fields, a nested sequence holding two scalars, and a string — on ARMv6-m at the same -Os. The measured translation units are test/footprint-tu/: consumer.c and consumer.cpp encode and decode the same message, one through the C API and one through the wrapper, so the rows are comparable. Regenerated by tools/footprint-tu.sh, which compiles each alone (-c) and reads size off the object, so library calls stay external references and only the TU's own code is counted:
| Consumer translation unit | .text | over the C API |
|---|---|---|
| C API directly | 440 B | — |
| C++ wrapper, freestanding | 962 B | +522 B |
| C++ wrapper, hosted | 1180 B | +740 B |
| C++ wrapper, hosted, RTTI on | 1447 B | +1007 B |
The same TU is used for all three wrapper rows, and it reaches only for the heap-free surface — which exists in both modes — so the 218 B from freestanding to hosted is what the header gates, not what the consumer asked for, and the 267 B on top is the RTTI the virtual bases emit. A freestanding MCU consumer therefore pays 962 B per TU, not 1447.
The two Minimal rows above are C-only by construction: the wrapper rejects SOFAB_DISABLE_FIXLEN_SUPPORT, ..._SEQUENCE_SUPPORT and ..._LAZY_SEQ_SUPPORT with an #error, and those are exactly the switches that profile sets.
Under -ffreestanding the wrapper drops to the no-heap surface automatically (SOFAB_CPP_HAVE_HOSTED, above): std::string/std::vector overloads, sofab::OStream and the std::function callback typedefs are gated out, with FixedString, FixedBytes, InlineVector, OStreamInline, OStreamView and plain function pointers in their place. The wire API is complete in both modes. test/cpp-freestanding/ compiles that surface with -ffreestanding on every CI run to hold the split closed.
Note. Only
gcc-arm-none-eabiships freestanding libstdc++ headers among the toolchains here;gcc-avrandgcc-riscv64-unknown-elfship none, so no C++ wrapper TU can be compiled for those targets at all, gated or not. The guard's CMake probe detects this and skips rather than failing the build.
SofaBuffers ships two same-format C++ codecs for opposite ends of the spectrum.
corelib-c-cpp (this repo)** — the footprint / embedded choice: a heap-free C99 object API plus a header-only C++20 wrapper. For MCUs where C is essential and every byte counts, and IoT-class C++ that forbids exceptions and std::iostream. Decoding is deferred-copy into caller-owned, address-stable storage.corelib-cpp** — the throughput choice: pure C++20 exposing the same sofab:: surface, parsing in place over the caller's buffer, so no destination has to survive a chunk. Neither port has a borrowing destination.Pick this repo for bare-metal C, MCU-class C++ or a shared C/C++ wire format; pick corelib-cpp on a hosted target that wants maximum speed.
Instruction counts from the shared Callgrind tooling — deterministic and machine-independent, all three built at -O3; lower is better. This repo's two columns were re-measured together on the current tree; the corelib-cpp column is the reading published in its own README.
| Workload | C (this) | C++ wrapper (this) | corelib-cpp |
|---|---|---|---|
| encode: u64 array (1000) | 125 999 | 126 028 | 35 033 |
| encode: typical message | 966 | 1 063 | 204 |
| encode: blob 1MB one-shot | 10 000 162 | 10 000 191 | 1 000 026 |
| encode: blob 1MB streaming | 10 004 819 | 10 009 790 | 13 009 117 |
| encode: composite | 16 164 | 16 501 | 11 494 |
| decode: u64 array (1000) | 303 434 | 303 438 | 41 852 |
| decode: typical message | 2 165 | 2 167 | 1 425 |
| decode: blob 1MB | 25 011 325 | 25 011 332 | 284 314 |
| decode: composite | 32 340 | 36 708 | 25 159 |
| decode: composite skip-all | 25 570 | 25 573 | 8 373 |
corelib-cpp runs 1.3× to 88× fewer instructions, widest where a payload moves in bulk: it establishes a varint window once and then moves whole 64-bit words, and its one-shot blob write is a memcpy at one instruction per byte, where this core pushes every payload byte through the same bounds-checked path (~10 instructions per byte encoding, ~25 decoding) and keeps per-field bookkeeping for its deferred-copy contract. That contract is the point of this repo, so the gap is the deliberate trade.
One row goes the other way. On encode: blob 1MB streaming this port needs 1.3× fewer instructions (10.0 M against 13.0 M). corelib-cpp takes its memcpy branch only while the run fits the output buffer and falls back to a byte loop when it does not — a megabyte through 4096 bytes is entirely the fallback. This core has no fast path to fall out of, so streaming costs it what the one-shot write cost (+0.05 %).
Approximate head-to-head figures from the benchmark arena (best-of-5, comparable only within a language):
| Use case | Library | vs. | Throughput | Bare-metal Cortex-M flash |
|---|---|---|---|---|
| Embedded C | corelib-c-cpp (C API) | nanopb | ~2.1× | ~4.7 KB vs ~6.6 KB |
| Embedded C++ | corelib-c-cpp (C++ wrapper) | EmbeddedProto | ~2.3× | ~7.3 KB vs ~9.3 KB |
| Throughput C++ | corelib-cpp (pure C++20) | protobuf | ~1.3× (434 vs 494-byte wire) | — (desktop/server) |
The corelib-cpp arena row predates several rounds of varint and hot-path work there and has not been re-run, so read it as a floor rather than a current figure. The two embedded rows are unaffected — this repo's codecs have not changed.
Both C++ ports share the sofab:: API; the practical differences show up in the decode-buffer contract:
| Topic | This library (C-backed) | corelib-cpp (pure C++20) |
|---|---|---|
| Decode-buffer lifetime | Deferred: read() binds the destination; bytes are filled by a later feed(), so destinations must be address-stable and outlive decoding | Immediate: parses in place over the caller's buffer and copies each value out before feed() returns, so no destination has to survive the call |
read(std::string &) | Must pre-size the string; reads into existing storage | Auto-sizes via assign |
read(std::string_view &) | Not available | Not available either — deliberately, and asking for one is a compile error naming the owning alternatives |
| Wrapper-array collectors | FixedStringSeq / FixedBlobSeq / StringSeq / BlobSeq for string and blob elements, FixedMessageSeq / MessageSeq (in sofab/seq.hpp) for struct, union and native-scalar row elements, FixedRowSeq / RowSeq (same header) for a row the stream cannot read on its own — a row that is itself a wrapper sequence, or an enum/boolean row — whose read the caller hands in as a reader, plus read(std::vector<std::string>&) and read(std::vector<std::vector<uint8_t>>&) | StringSeq / BlobSeq / MessageSeq / RowSeq, same placement rule; the fixed-capacity twins are this port's, since the heap-free containers are what they fill |
| Float-array default test | bitsEqual(a, b) (in sofab/floats.hpp) — true iff both float / double arrays have the same length and the same IEEE-754 bit pattern at every index, so -0.0 differs from +0.0 and a NaN equals only a bit-identical NaN; one memcmp after the length check, header-only, 0 B when unused | The same helper; takes any contiguous container (std::vector, InlineVector<T,N>, std::array, std::span, std::initializer_list) on either side |
| Heap-free field types | FixedString<N>, FixedBytes<N>, InlineVector<T,N> | The same three, deliberately identical in name and behaviour, so generated code for a bounded field is the same either way |
write() of an enum array | Takes the container of scoped enums as stored — std::vector<Gear> or InlineVector<Gear,N> with enum class Gear : int8_t — and encodes it at the enum's declared underlying width, which is also what picks the signed or unsigned array wire type | The same, spelled the same, for the same bytes: one expression in generated code compiles against either port, and neither needs a converted copy |
| Build-time capability flags | Honors the C core's SOFAB_DISABLE_* switches (FP64/INT64/ARRAY → static_assert on use; FIXLEN/SEQUENCE → hard #error) | None — every type always compiled in |
| Strict UTF-8 (§6.4) | Off by default (constrained-profile allowance), opt in with SOFAB_ENABLE_STRICT_UTF8 | On by default |