SofaBuffers C++ 0.12.0
Pure-C++20 SofaBuffers — API Documentation
Loading...
Searching...
No Matches
sofab::detail Namespace Reference

Implementation details of the wire format; not part of the public API. More...

Classes

struct  FitDest
 fitDest as the sizer a typed read runs between its checks and its delivery, so the free reads below state the §7.3 tag, the bound and the reach once — inside the read — instead of re-deriving them in front of it. More...
 
struct  NoSizer
 The "size nothing" sizer: what a typed read does with the caller's destination between its checks and its delivery when the caller did not ask for sizing (the destination is used as handed). More...
 
struct  WireInt
 The integer type an element travels as on the wire. More...
 
struct  WireInt< E, true >
 Enumeration case of WireInt: the declared underlying type. More...
 

Concepts

concept  FloatElement
 float or double, the two element types of an fp array.
 
concept  sizable
 Give dst room for want, if it is a destination the caller has to size.
 

Typedefs

template<typename E >
using WireInt_t = typename WireInt< E >::type
 Alias for WireInt.
 

Enumerations

enum class  Wire : uint8_t {
  Unsigned = 0 , Signed = 1 , Fixlen = 2 , ArrayUnsigned = 3 ,
  ArraySigned = 4 , ArrayFixlen = 5 , SequenceStart = 6 , SequenceEnd = 7
}
 Wire type stored in the low 3 bits of every field header. More...
 
enum class  Fix : uint8_t { Fp32 = 0 , Fp64 = 1 , String = 2 , Blob = 3 }
 Sub-type of a length-prefixed (Fixlen) payload, stored in the low 3 bits of its length word. More...
 

Functions

template<FloatElement T>
bool bitsEqualSpan (std::span< const T > a, std::span< const T > b) noexcept
 Length check, then one block compare of the object representation.
 
void storeLittle64 (uint8_t *out, uint64_t w) noexcept
 Store a 64-bit word as eight little-endian bytes.
 
constexpr uint64_t spread7 (uint64_t v) noexcept
 Spread the low 56 bits of v into eight 7-bit groups, one per byte.
 
constexpr uint64_t gather7 (uint64_t w) noexcept
 Inverse of spread7: pack eight bytes' low 7 bits into one value.
 
template<std::unsigned_integral U>
void storeLittleBits (uint8_t *out, U bits) noexcept
 Store the object representation of a 4- or 8-byte value as little-endian bytes.
 
template<std::unsigned_integral U>
U loadLittleBits (const uint8_t *in) noexcept
 Inverse of storeLittleBits.
 
uint64_t loadLittle64 (const uint8_t *in) noexcept
 Load eight little-endian bytes as a 64-bit word (see storeLittle64).
 
constexpr uint64_t zigzagEncode (int64_t v) noexcept
 Map a signed integer to an unsigned one with the zig-zag scheme.
 
constexpr int64_t zigzagDecode (uint64_t u) noexcept
 Inverse of zigzagEncode.
 
template<std::floating_point F>
constexpr auto floatBits (F v) noexcept
 Reinterpret a float/double as the unsigned integer holding its bits.
 
template<std::floating_point F, std::unsigned_integral U>
constexpr F bitsFloat (U bits) noexcept
 Reconstruct a floating-point value from its raw bits.
 
constexpr uint32_t utf8Advance (uint32_t state, const uint8_t *d, size_t len) noexcept
 Step the UTF-8 DFA over len bytes and return the state.
 
constexpr bool utf8Valid (const char *data, size_t len) noexcept
 
template<typename T >
constexpr long destCapacity () noexcept
 The element ceiling a decode destination T publishes, or -1 when it publishes none.
 
template<typename D >
constexpr size_t destRoom (const D &d) noexcept
 Room a decode destination already holds — bytes for a payload, elements for an array (CORELIB_PLAN §6.6.3).
 
template<typename D >
constexpr void destSetLen (D &d, size_t n) noexcept
 Publish the decoded length in a destination that already had the room — the one container operation the codec performs.
 
std::size_t utf8Prefix (std::string_view sv, std::size_t n) noexcept
 Length of the longest prefix of sv that is at most n bytes and does not end inside a UTF-8 character; sv is longer than n.
 
template<typename D >
void fitDest (D &dst, size_t want) noexcept
 
size_t arrayReach (const IStreamImpl &is) noexcept
 Elements of the announced array the bytes in hand could still carry, clamped to the count itself.
 
template<typename Elem >
bool arrayTagMatches (const IStreamImpl &is) noexcept
 Does the delivered tag match the array kind Elem declares (§7.3)?
 
bool seqIndexAdmitted (IStreamImpl &is, sofab::id id, long cap, long dynCap, long destCap) noexcept
 Admit or refuse a wrapper-array element index, in §6.3's categories, before the container it indexes into is extended.
 
template<typename A >
constexpr bool namesSeqContainer () noexcept
 Whether a MessageSeq template argument names the destination container rather than the element type.
 

Variables

constexpr size_t VARINT_MAX_BYTES = 10
 Widest base-128 varint: ceil(64/7) == 10 bytes (§4.1).
 
constexpr uint8_t utf8Dfa []
 Validate that [data, data+len) is well-formed UTF-8.
 
constexpr uint32_t UTF8_ACCEPT = 0
 The DFA's ACCEPT and REJECT states (see utf8Dfa).
 
constexpr uint32_t UTF8_REJECT = 12
 

Detailed Description

Implementation details of the wire format; not part of the public API.

Typedef Documentation

◆ WireInt_t

template<typename E >
using sofab::detail::WireInt_t = typedef typename WireInt<E>::type

Alias for WireInt.

Definition at line 457 of file sofab.hpp.

Enumeration Type Documentation

◆ Fix

enum class sofab::detail::Fix : uint8_t
strong

Sub-type of a length-prefixed (Fixlen) payload, stored in the low 3 bits of its length word.

Internal, like Wire. §7.3 bounds the type check at wire type plus this subtype, since fp32/fp64/string/blob share Wire::Fixlen.

Enumerator
Fp32 

32-bit IEEE-754 float.

Fp64 

64-bit IEEE-754 double.

String 

UTF-8 text.

Blob 

Opaque byte string.

Definition at line 281 of file sofab.hpp.

◆ Wire

enum class sofab::detail::Wire : uint8_t
strong

Wire type stored in the low 3 bits of every field header.

Internal. The typed reads compare it themselves (§7.3), so neither generated nor hand-written code has to name it.

Enumerator
Unsigned 

Unsigned integer encoded as a varint.

Signed 

Signed integer, zig-zag encoded as a varint.

Fixlen 

Length-prefixed payload (float, string or blob).

ArrayUnsigned 

Count-prefixed array of unsigned varints.

ArraySigned 

Count-prefixed array of zig-zag varints.

ArrayFixlen 

Count-prefixed array of fixed-size elements.

SequenceStart 

Opens a nested sub-message.

SequenceEnd 

Closes the most recently opened sub-message.

Definition at line 263 of file sofab.hpp.

Function Documentation

◆ arrayReach()

size_t sofab::detail::arrayReach ( const IStreamImpl &  is)
inlinenoexcept

Elements of the announced array the bytes in hand could still carry, clamped to the count itself.

The same clamp IStreamImpl::readArray applies to its own refusal, so the helper never sizes a destination the codec would then refuse, and an announced 2^31 whose payload is absent or withheld sizes nothing.

Definition at line 6405 of file sofab.hpp.

◆ arrayTagMatches()

template<typename Elem >
bool sofab::detail::arrayTagMatches ( const IStreamImpl &  is)
inlinenoexcept

Does the delivered tag match the array kind Elem declares (§7.3)?

Definition at line 6420 of file sofab.hpp.

◆ bitsEqualSpan()

template<FloatElement T>
bool sofab::detail::bitsEqualSpan ( std::span< const T >  a,
std::span< const T >  b 
)
inlinenoexcept

Length check, then one block compare of the object representation.

Definition at line 44 of file bits_equal.hpp.

◆ bitsFloat()

template<std::floating_point F, std::unsigned_integral U>
constexpr F sofab::detail::bitsFloat ( U  bits)
constexprnoexcept

Reconstruct a floating-point value from its raw bits.

Template Parameters
FTarget floating-point type.
UUnsigned integer type holding the bit pattern.
Parameters
bitsObject representation produced by floatBits.
Returns
The floating-point value those bits encode.

Definition at line 479 of file sofab.hpp.

◆ destCapacity()

template<typename T >
constexpr long sofab::detail::destCapacity ( )
constexprnoexcept

The element ceiling a decode destination T publishes, or -1 when it publishes none.

The bound IStreamImpl::readArray compares the wire count against before it fills anything (MESSAGE_SPEC §3): a heap-free destination that cannot reach the count must reject it, never truncate into it.

Told apart by the destination's own capabilities rather than by name, so a caller's own container works too — a static capacity() is the ceiling (InlineVector and friends: resize exists but clamps to N), everything else is treated as growable. T::capacity() is deliberately spelled as an unqualified static call: std::vector's capacity() is a non-static member, so naming it this way is ill-formed and the requirement fails — exactly the growable / heap-free split IStreamImpl::readString already keys on.

A destination of fixed extent that publishes nothing (std::array, a bound span) is not covered here: that is IStreamImpl::read's low-level contract, where the leading elements land in the destination and the rest is parsed only to stay framed.

Definition at line 641 of file sofab.hpp.

◆ destRoom()

template<typename D >
constexpr size_t sofab::detail::destRoom ( const D &  d)
constexprnoexcept

Room a decode destination already holds — bytes for a payload, elements for an array (CORELIB_PLAN §6.6.3).

§6.6 forbids the codec growing the caller's destination from a wire number: "the codec allocates nothing itself but **requires a growable destination** and grows it, from a wire count or otherwise — violates". §6.6.3 names the shape that replaces it — a value is delivered "into a destination the caller hands back after being told the announced count, <strong>with the codec refusing a destination too short rather than growing it</strong>" — so every typed read needs one number: how much room is there now.

Two kinds of destination answer it differently, and the difference is not a policy the codec picks but a fact the type publishes:

  • a static capacity (T::capacity() — sofab::FixedString, sofab::FixedBytes, sofab::InlineVector) is storage the caller already owns in full. Setting a length inside it allocates nothing, so the room is the capacity;
  • anything else (std::string, std::vector, std::array, a bound span) offers exactly size(). For a heap-growable container that is the point: the codec writes into what the caller sized and never creates room, so no allocator call can reach the wire.
Template Parameters
DDestination type.
Parameters
dThe destination.
Returns
Room in bytes (payload) or elements (array).

Definition at line 677 of file sofab.hpp.

◆ destSetLen()

template<typename D >
constexpr void sofab::detail::destSetLen ( D &  d,
size_t  n 
)
constexprnoexcept

Publish the decoded length in a destination that already had the room — the one container operation the codec performs.

Never grows: sofab::detail::destRoom has already been compared against the announced size, so n is at most what the destination holds and this is a length store (sofab::FixedString::set_len) or a shrink. A shrink cannot allocate — std::string / std::vector never reallocate for a size below the current one — so §6.6's question, "can a sender make this allocation bigger by sending different bytes?", has no allocation to ask it of.

A destination of fixed extent that publishes neither (std::array, a bound span) keeps its extent; the value's length is the announced count the caller was told.

Parameters
dDestination.
nDecoded length, already known to fit.

Definition at line 703 of file sofab.hpp.

◆ fitDest()

template<typename D >
void sofab::detail::fitDest ( D &  dst,
size_t  want 
)
noexcept

Definition at line 6376 of file sofab.hpp.

◆ floatBits()

template<std::floating_point F>
constexpr auto sofab::detail::floatBits ( F  v)
constexprnoexcept

Reinterpret a float/double as the unsigned integer holding its bits.

Template Parameters
FFloating-point type (float or double).
Parameters
vValue whose object representation is extracted.
Returns
uint32_t for a 4-byte F, otherwise uint64_t.

Definition at line 466 of file sofab.hpp.

◆ gather7()

constexpr uint64_t sofab::detail::gather7 ( uint64_t  w)
constexprnoexcept

Inverse of spread7: pack eight bytes' low 7 bits into one value.

Byte i of w contributes bits [7i+6 : 7i] of the result; bit 7 of each byte (the varint continuation flag) is discarded by the masks, so the caller need not clear it. The three rounds mirror spread7's, run in the opposite order.

Definition at line 352 of file sofab.hpp.

◆ loadLittle64()

uint64_t sofab::detail::loadLittle64 ( const uint8_t *  in)
inlinenoexcept

Load eight little-endian bytes as a 64-bit word (see storeLittle64).

Definition at line 400 of file sofab.hpp.

◆ loadLittleBits()

template<std::unsigned_integral U>
U sofab::detail::loadLittleBits ( const uint8_t *  in)
inlinenoexcept

Inverse of storeLittleBits.

Definition at line 385 of file sofab.hpp.

◆ namesSeqContainer()

template<typename A >
constexpr bool sofab::detail::namesSeqContainer ( )
constexprnoexcept

Whether a MessageSeq template argument names the destination container rather than the element type.

MessageSeq is templated on the container, exactly as StringSeq is, which is what lets it collect into an InlineVector as well as a std::vector. It used to be templated on the ELEMENT, with std::vector<T> hard-wired as the destination, and that spelling keeps working: the two readings are told apart here, from the argument alone.

An argument names a container when its value_type is something this collector reads as an element — a message, or a row container. The two readings can only collide for an argument whose value_type is itself one of those two, i.e. an element that is a sequence of messages or a row of rows; under the element reading such an element is handed to IStreamImpl::read, which has no overload for it, so what the container reading takes over is code that never compiled.

Definition at line 6905 of file sofab.hpp.

◆ seqIndexAdmitted()

bool sofab::detail::seqIndexAdmitted ( IStreamImpl &  is,
sofab::id  id,
long  cap,
long  dynCap,
long  destCap 
)
inlinenoexcept

Admit or refuse a wrapper-array element index, in §6.3's categories, before the container it indexes into is extended.

MESSAGE_SPEC §5.1 makes the array's length highest present id + 1, so the index is the length: "for a <strong>sequence array</strong> it surfaces the <strong>index</strong> of the element in hand … there being no count header to check", and "A limit <strong>MUST</strong> be enforced … before the container it indexes into is extended" (§6.2.1).

The three bounds do not share a verdict, so they are consulted in §6.3's order. A schema count shuts the receiver cap out entirely — a receiver limit "**MUST NOT** be applied to a field the schema already bounds" (§6.2.1).

The stream applies the same three bounds one step earlier, at the element header (IStreamImpl::rejectElementIndex), which is where a truncated element cannot outrun them. This is the second gate, for a deserialize reached directly — it is a public entry point — and for a collector that publishes no element type for the stream to key on.

Parameters
isStream to record the refusal on.
idThe element index in hand.
capSchema count N, or negative.
dynCapThe caller's max_dyn_array_count (§6.2.1), or negative when the caller supplied none — in which case no index cap applies, because there is none to fall back on.
destCapThe destination container's capacity, or negative.
Returns
true when the index may be placed.
A collector that states no bound at all is refused, not obeyed
A wrapper array's length is its highest index, so an unbounded array whose collector carries neither a schema count nor a receiver cap grows to whatever index the sender picks — the amplification §6.2.1 exists to stop, from a ~9-byte field. There is nothing to fall back on (§6.2.1 forbids this codec a limit of its own), so the call is refused with Error::InvalidArgument. A destination that cannot grow is the one exemption: its capacity already bounds the allocation, so no cap is needed for the sender not to choose it.

Definition at line 6635 of file sofab.hpp.

◆ spread7()

constexpr uint64_t sofab::detail::spread7 ( uint64_t  v)
constexprnoexcept

Spread the low 56 bits of v into eight 7-bit groups, one per byte.

Byte i of the result holds bits [7i+6 : 7i] of v, with bit 7 of each byte clear — exactly the payload layout of the first eight bytes of a base-128 varint. Three halve-and-shift rounds do it in a handful of ALU ops, which is what lets the encoder emit eight varint bytes as one store instead of eight.

Definition at line 325 of file sofab.hpp.

◆ storeLittle64()

void sofab::detail::storeLittle64 ( uint8_t *  out,
uint64_t  w 
)
inlinenoexcept

Store a 64-bit word as eight little-endian bytes.

The wire is little-endian (§4), so on a little-endian host this is the native representation and compiles to a single unaligned store; on a big-endian one the word is reversed first. std::byteswap is C++23, so the reversal is spelled out.

Definition at line 306 of file sofab.hpp.

◆ storeLittleBits()

template<std::unsigned_integral U>
void sofab::detail::storeLittleBits ( uint8_t *  out,
U  bits 
)
inlinenoexcept

Store the object representation of a 4- or 8-byte value as little-endian bytes.

The wire is little-endian (§4), so on a little-endian host this is one unaligned store; elsewhere the bytes are laid down individually. The hand-rolled shift loop this replaces did not fold into a store on GCC and cost ten instructions per fp32 field.

Template Parameters
UUnsigned type holding the bits (uint32_t or uint64_t).
Parameters
outDestination, with at least sizeof(U) writable bytes.
bitsValue to lay down.

Definition at line 374 of file sofab.hpp.

◆ utf8Advance()

constexpr uint32_t sofab::detail::utf8Advance ( uint32_t  state,
const uint8_t *  d,
size_t  len 
)
constexprnoexcept

Step the UTF-8 DFA over len bytes and return the state.

The resumable half of utf8Valid, for a string payload that arrives across more than one feed: §6.4.4 requires that "a chunk boundary MUST NOT affect the outcome" and permits exactly this — "A decoder MAY validate incrementally provided it carries validator state across <tt>feed</tt> calls; no assembly buffer is required."

UTF8_REJECT is absorbing, which is what lets the verdict be held back: §6.4.4's last row makes a byte that can begin no sequence INVALID "reported at payload completion, not before", and a decoder "MUST NOT report INVALID mid-payload … that input is INCOMPLETE until the payload ends". The caller therefore steps the state per chunk and consults it once, when the declared length is reached — where a state other than UTF8_ACCEPT covers both a rejected byte and a sequence the payload ended in the middle of.

Parameters
stateState from the previous piece (UTF8_ACCEPT to start).
dBytes of this piece.
lenTheir count.
Returns
The state after them.

Definition at line 552 of file sofab.hpp.

◆ utf8Prefix()

std::size_t sofab::detail::utf8Prefix ( std::string_view  sv,
std::size_t  n 
)
inlinenoexcept

Length of the longest prefix of sv that is at most n bytes and does not end inside a UTF-8 character; sv is longer than n.

The over-long half of FixedString's assignment-time clamp, one copy for every capacity: the cut moves back while the first byte left out is a continuation byte (10xxxxxx), so a valid UTF-8 sv keeps a valid UTF-8 prefix. The common case – the value fits – stays inline in FixedString::assign, where a literal's length folds away.

Definition at line 759 of file sofab.hpp.

◆ utf8Valid()

constexpr bool sofab::detail::utf8Valid ( const char *  data,
size_t  len 
)
constexprnoexcept

Definition at line 560 of file sofab.hpp.

◆ zigzagDecode()

constexpr int64_t sofab::detail::zigzagDecode ( uint64_t  u)
constexprnoexcept

Inverse of zigzagEncode.

Parameters
uZig-zag-encoded unsigned value.
Returns
The original signed value.

Definition at line 430 of file sofab.hpp.

◆ zigzagEncode()

constexpr uint64_t sofab::detail::zigzagEncode ( int64_t  v)
constexprnoexcept

Map a signed integer to an unsigned one with the zig-zag scheme.

Small-magnitude values of either sign map to small unsigned values, so they encode to short varints.

Parameters
vSigned value to transform.
Returns
The zig-zag-encoded unsigned representation.

Definition at line 421 of file sofab.hpp.

Variable Documentation

◆ UTF8_ACCEPT

constexpr uint32_t sofab::detail::UTF8_ACCEPT = 0
inlineconstexpr

The DFA's ACCEPT and REJECT states (see utf8Dfa).

Definition at line 526 of file sofab.hpp.

◆ UTF8_REJECT

constexpr uint32_t sofab::detail::UTF8_REJECT = 12
inlineconstexpr

Definition at line 527 of file sofab.hpp.

◆ utf8Dfa

constexpr uint8_t sofab::detail::utf8Dfa[]
inlineconstexpr
Initial value:
= {
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,
7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
8,8,2,2,2,2,2,2,2,2,2,2,2,2,2,2, 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
10,3,3,3,3,3,3,3,3,3,3,3,3,4,3,3, 11,6,6,6,5,8,8,8,8,8,8,8,8,8,8,8,
0,12,24,36,60,96,84,12,12,12,48,72, 12,12,12,12,12,12,12,12,12,12,12,12,
12, 0,12,12,12,12,12, 0,12, 0,12,12, 12,24,12,12,12,12,12,24,12,24,12,12,
12,12,12,12,12,12,12,24,12,12,12,12, 12,24,12,12,12,12,12,12,12,24,12,12,
12,12,12,12,12,12,12,36,12,36,12,12, 12,36,12,12,12,12,12,36,12,36,12,12,
12,36,12,12,12,12,12,12,12,12,12,12,
}

Validate that [data, data+len) is well-formed UTF-8.

A real UTF-8 validator, not a byte-range shortcut — this is a security surface (CORELIB_PLAN §6.4). It rejects:

  • overlong encodings, including C0 80 (Java "Modified UTF-8" NUL), C1-lead 2-byte forms, and the overlong 3-/4-byte E0 80.., F0 80.. forms;
  • UTF-16 surrogate code points U+D800–U+DFFF (ED A0..ED BF);
  • code points above U+10FFFF (F4 90.. and any F5–FF lead);
  • bare continuation bytes and any sequence truncated before its declared length ends (truncated-at-end is INVALID). It accepts an embedded U+0000 (a single 00 byte): NUL is valid UTF-8 and representable in the length-framed payload (§6.4).

constexpr and free of reinterpret_cast, so it can be evaluated at compile time and unit-tested directly.

Parameters
dataPointer to the candidate bytes (may be nullptr iff len is 0).
lenNumber of bytes to validate.
Returns
true iff every byte forms part of a well-formed UTF-8 sequence.

Definition at line 509 of file sofab.hpp.

◆ VARINT_MAX_BYTES

constexpr size_t sofab::detail::VARINT_MAX_BYTES = 10
inlineconstexpr

Widest base-128 varint: ceil(64/7) == 10 bytes (§4.1).

The encoder and decoder both use it as the "one more varint definitely fits" window, so a capacity check can be hoisted out of an element loop and amortised over a whole chunk instead of paid per element.

Definition at line 296 of file sofab.hpp.