codec

package
v0.17.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 12, 2026 License: Apache-2.0 Imports: 4 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package codec is the ontos canonical binary codec — ontos-codec-v1 (frozen).

Atom(bytes)   = 0x00 || uvarint(len(bytes)) || bytes
Tuple(values) = 0x01 || uvarint(arity)      || encode(each child)

uvarint is unsigned LEB128 in shortest/canonical form over the u64 domain [0, 2^64-1] (spec: docs/spec/ontos-codec.md §3.1). The decoder rejects unknown tags, trailing bytes, non-canonical uvarints, uvarints whose value is >= 2^64 (uvarint_overflow), and inputs exceeding the configured safety limits or this implementation's native materialization width (limit_exceeded, whose DecodeError.LimitKind names the bound; spec §4.2).

Measure reports a value's canonical encoded length, its expanded occurrence count and its height without expanding shared subtrees, against bounds the caller states (spec §10, pinned by vectors/codec-measure.json).

This codec is kept beside the model (it imports core, never the reverse). Conformance is pinned by vectors/codec.json (codec id: ontos-codec-v1) and specified normatively in docs/spec/ontos-codec.md.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Decode

func Decode(input []byte) (core.Value, error)

Decode decodes one value with default limits, rejecting trailing bytes.

func DecodeWithLimits

func DecodeWithLimits(input []byte, limits Limits) (core.Value, error)

DecodeWithLimits decodes one value with explicit limits, rejecting trailing bytes.

func Encode

func Encode(v core.Value) []byte

Encode returns the canonical ontos-codec-v1 bytes of v.

func EncodeUvarint

func EncodeUvarint(n int) []byte

EncodeUvarint returns the shortest-form LEB128 encoding of n. It is part of the public codec API (the canonical uvarint exposed for callers and pinned by the uvarint conformance vectors); the encoder internals use the private appendUvarint.

Types

type DecodeError

type DecodeError struct {
	Code    string
	Message string
	// LimitKind names the bound that stopped decoding when Code is "limit_exceeded",
	// and is empty for every other code. It is observational: it identifies the check
	// that stopped this decode, and another implementation may name a different
	// bound for the same input (spec §4.2). An empty or unrecognized kind carries no
	// more than the generic "limit_exceeded".
	LimitKind DecodeLimitKind
}

DecodeError carries a cross-language rejection Code (docs/spec/ontos-codec.md §4). The five byte-contract codes (unexpected_eof, unknown_tag, non_canonical_uvarint, uvarint_overflow, trailing_bytes) are normative and pinned by vectors/codec.json. "limit_exceeded" is different: it reports that decoding stopped at an operational bound, so it is not vector-pinned and says nothing about whether the input is a valid encoding. For it, LimitKind names the bound responsible (spec §4.2). The Message is diagnostic only.

Construct a DecodeError with field names. Since LimitKind was added (ontos-internal#352), an unkeyed composite literal such as DecodeError{code, msg} no longer compiles.

func (*DecodeError) Error

func (e *DecodeError) Error() string

type DecodeLimitKind

type DecodeLimitKind string

DecodeLimitKind names an operational decode bound (docs/spec/ontos-codec.md §4.2). The vocabulary is append-only: a name never changes meaning, but a later version may add names, so a switch over it needs a default case.

const (
	// LimitDecodeDepth: the value about to be read is nested deeper than
	// Limits.MaxDepth, counting the root as depth 0.
	LimitDecodeDepth DecodeLimitKind = "decode_depth"
	// LimitAtomBytes: a declared atom byte length exceeds Limits.MaxAtomBytes.
	LimitAtomBytes DecodeLimitKind = "atom_bytes"
	// LimitTupleArity: a declared tuple arity exceeds Limits.MaxTupleArity.
	LimitTupleArity DecodeLimitKind = "tuple_arity"
	// LimitNativeWidth: a valid uvarint length or arity does not fit in Go's int
	// (above 2^63-1, or 2^31-1 on a 32-bit build).
	LimitNativeWidth DecodeLimitKind = "native_width"
)

type Limits

type Limits struct {
	MaxDepth      int
	MaxAtomBytes  int
	MaxTupleArity int
}

Limits bounds decoding of untrusted input. Operational policy, not value semantics. Every field is enforced exactly as given, so a zero field is a bound of zero, not a request for the default: Limits{} admits only an empty atom or an empty tuple at the root. Start from DefaultLimits and override the fields you mean to set.

func DefaultLimits

func DefaultLimits() Limits

DefaultLimits returns the standard decode limits.

type MeasureBounds added in v0.17.0

type MeasureBounds struct {
	MaxEncodedBytes uint64
	MaxOccurrences  uint64
	MaxHeight       uint64
}

MeasureBounds states one bound for each measure. Every field is enforced exactly as given, so a field left out of a composite literal is a bound of zero, not a default: MeasureBounds{} is exceeded by every value, which has at least two encoded bytes and one occurrence. A measure equal to its bound is within it.

type MeasureError added in v0.17.0

type MeasureError struct {
	Exceeded []MeasureKind
}

MeasureError is Measure's over-limit outcome. Exceeded names every measure that exceeded its bound, in ascending order of the names (encoded_bytes, height, occurrences), and is never empty. It is a function of the value and the bounds alone (spec §10.2), unlike DecodeError's LimitKind. It carries no numbers: a measure past its bound was not counted to the end.

func (*MeasureError) Error added in v0.17.0

func (e *MeasureError) Error() string

type MeasureKind added in v0.17.0

type MeasureKind string

MeasureKind names one of the three measures that Measure reports (docs/spec/ontos-codec.md §10). The vocabulary is append-only, as DecodeLimitKind's is: a name never changes meaning, but a later version may add names, so a switch over it needs a default case, and an unrecognized name still means over limit.

const (
	// MeasureEncodedBytes is the length of the value's canonical encoding: len(Encode(v)).
	MeasureEncodedBytes MeasureKind = "encoded_bytes"
	// MeasureHeight is how deep the value is nested, as the decoder's depth bound
	// counts it: 0 for an atom and for the empty tuple, 1 + the deepest child otherwise.
	MeasureHeight MeasureKind = "height"
	// MeasureOccurrences is the number of values in the expanded tree, the root
	// included: a subtree that appears twice counts twice.
	MeasureOccurrences MeasureKind = "occurrences"
)

type Measures added in v0.17.0

type Measures struct {
	EncodedBytes uint64
	Occurrences  uint64
	Height       uint64
}

Measures are a value's three measures, each exact, as Measure reports them when every one is within its bound.

func Measure added in v0.17.0

func Measure(v core.Value, bounds MeasureBounds) (Measures, error)

Measure reports the length of v's canonical encoding, the number of values in its expanded tree and its height, without expanding it (docs/spec/ontos-codec.md §10). When every measure is within its bound it returns them exactly, with a nil error. Otherwise it returns the zero Measures and a *MeasureError naming every measure over its bound.

A value built with NewTuple may hold the same subtree under several parents, and a chain of k such tuples can denote a tree whose encoding is 5·2^k − 2 bytes long. Measure visits each distinct tuple once and adds its measures once per incoming edge, so its work is linear in the distinct tuples plus their child edges, whatever the bounds. It walks with an explicit stack, so a value of any height can be measured. Its arithmetic saturates at each bound, so it never overflows, and it always finishes the walk, so the exceeded set does not depend on the traversal order.

A tuple is recognized by the identity of its child storage. A core.Tuple is a struct around a slice, and every copy of a tuple shares that slice's backing array, so Measure keys its memo on the array's address and the tuple's arity. core provides that key through the module-internal package tupleid, so the codec reads no unexported field and core's exported API is unchanged. Structurally equal tuples built separately have separate arrays: they are measured correctly, just not shared.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL