ddn.encoding.binary

ddn.encoding.binary — endian-aware byte cursor for wire and file formats.

One reader and one writer for all DDN wire and file formats, with the byte order as a run-time value (the X protocol negotiates it per connection) or as a compile-time parameter (Wayland is little-endian only and pays no per-call order branch; SSH is big-endian only). Failures are Result values carrying a BinaryError, never exceptions; the fixed-capacity forms are @safe pure nothrow @nogc and CTFE-capable, so generated codecs can be validated at compile time.

Surface:

  • ByteOrder — LITTLE or BIG; native reports

    the host order.

  • BinaryReader — run-time-order sequential reader over a

    const(ubyte)[]. Byte slices returned by getBytes and getPrefixed alias the input buffer: nothing is copied, which protocols that hash the exact received bytes (SSH) rely on; the input must outlive the slices.

  • FixedBinaryReader — the same reader with the order

    fixed at compile time.

  • BinaryWriter — run-time-order writer over a

    caller-provided fixed-capacity ubyte[]; @nogc; non-committing on capacity failure (the buffer prefix and the position are unchanged when an operation fails).

  • FixedBinaryWriter — the same writer with the order

    fixed at compile time.

  • AppenderBinaryWriter /

    FixedAppenderBinaryWriter — growable writers that extend a GC-allocated buffer (the only allocating form; the allocation is the point).

  • encode / decode — one-off scalar conversions

    to and from fixed-size byte arrays.

Fixed-order design choice (the DL-4 binary-cursor task): the fixed-order forms are separate aliases over a shared implementation instead of a ByteOrder.RUNTIME enum member, so ByteOrder stays a two-value type that every consumer can switch over exhaustively, and the run-time-order path (X) is a plain struct with no template noise in diagnostics.

float and double are read and written as their IEEE-754 binary32 / binary64 bit patterns (real is not a wire type and is rejected at compile time). In CTFE the conversion uses exact frexp/ldexp arithmetic (D forbids bit reinterpretation at compile time) and NaN payloads are canonicalised; the two paths agree bit-exactly for every other value.

Cursors are small value types: a copy shares the underlying buffer but keeps an independent position.

Example:

import ddn.encoding.binary;

ubyte[32] buf;
auto w = BinaryWriter(buf[], ByteOrder.BIG);
assert(w.put!uint(0x29b7f4aa).ok);
assert(w.putPrefixed!ushort(cast(const(ubyte)[]) "hi").ok);
assert(w.written == [0x29, 0xb7, 0xf4, 0xaa, 0x00, 0x02, 'h', 'i']);

auto r = BinaryReader(w.written, ByteOrder.BIG);
assert(r.get!uint.value == 0x29b7f4aa);
assert(r.getPrefixed!ushort(255).value == cast(const(ubyte)[]) "hi");
assert(r.requireFullyConsumed().ok);

Types 9

Byte order of a wire or file format.

LITTLELeast significant byte first (X11 little-endian setup, Wayland).
BIGMost significant byte first (network order: SSH; X11 MSB setup).

Failure codes of the binary cursor.

SHORT_INPUTFewer bytes (reader) or less remaining capacity (fixed-capacity writer) than the operation needs. Retryable with more input or a larger buffer; the cursor position is unchanged.
BAD_ALIGNMENTAn `alignTo` / `padTo` argument of zero.
OVERFLOWA value does not fit its destination: a length prefix declaring more than the caller's limit, a `putPrefixed` payload longer than the prefix type can express, a size computation overflowing `size_t...

Recoverable binary cursor failure with bounded context.

offset is the buffer position at which the failed operation started (for patch, the patch target; for getPrefixed, the length prefix). No strings, no pointers: returnable in @nogc nothrow contexts everywhere.

Fields
BinaryErrorCode codeWhich failure happened.
size_t offsetBuffer position of the failed operation.
aliasBinaryReader = BinaryReaderImpl!false

Sequential reader over a const(ubyte)[] with the byte order chosen at run time (the X path). Returned byte slices alias the input.

aliasFixedBinaryReader(ByteOrder order) = BinaryReaderImpl!(true, order)

Sequential reader with the byte order fixed at compile time, so the hot path carries no per-call order branch (the Wayland path). Instantiate as FixedBinaryReader!(ByteOrder.LITTLE).

aliasBinaryWriter = BinaryWriterImpl!(false, false)

Writer over a caller-provided fixed-capacity ubyte[] with the byte order chosen at run time. Writes are non-committing on capacity failure and never allocate (@nogc).

aliasFixedBinaryWriter(ByteOrder order) = BinaryWriterImpl!(true, false, order)

Fixed-capacity writer with the byte order fixed at compile time. Instantiate as FixedBinaryWriter!(ByteOrder.LITTLE).

aliasAppenderBinaryWriter = BinaryWriterImpl!(false, true)

Growable writer with the byte order chosen at run time. The buffer is extended through the GC as needed (documented allocation; use the fixed-capacity forms where @nogc is required).

aliasFixedAppenderBinaryWriter(ByteOrder order) = BinaryWriterImpl!(true, true, order)

Growable writer with the byte order fixed at compile time. Instantiate as FixedAppenderBinaryWriter!(ByteOrder.BIG).

Functions 3

fnByteOrder native() @safe pure nothrow @nogcThe host's own byte order.
fnubyte[T.sizeof] encode(T)(T value, ByteOrder order) if (isWireScalar!T) @safe pure nothrow @nogcEncodes one scalar of type `T` as `T.sizeof` bytes in byte order `order` — the one-off form of the writer's `put`.
fnResult!(T, BinaryError) decode(T)(scope const(ubyte)[] src, ByteOrder order) if (isWireScalar!T) @safe pure nothrow @nogcDecodes one scalar of type `T` from the start of `src` in byte order `order` — the one-off form of the reader's `get`. Bytes beyond `T.sizeof` are ignored.