sdnv

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Sep 2, 2026 License: Apache-2.0 Imports: 2 Imported by: 0

Documentation

Overview

Package sdnv encodes and decodes Self-Delimiting Numeric Values.

An SDNV packs an unsigned integer into as few octets as it needs. The top bit of each octet says whether another follows: set means "more to come", clear means "this is the last one". The value is the 7 low bits of each octet concatenated, most significant first.

   0x7F  ->  01111111                    one octet
  0x80   ->  10000001 00000000           two octets
0x4234   ->  10000001 10000100 00110100  three octets

The scheme comes from ASN.1 Object Identifier encoding. Two protocols in this library use it for nearly every variable-length field: LTP (RFC 5326 clause 1.6 item 20) and Bundle Protocol version 6 (RFC 5050 clause 4.1). It lives in its own package so neither has to carry a private copy, the same way pkg/crc serves the checksum users.

Index

Constants

View Source
const MaxEncodedSize = 10

MaxEncodedSize is the widest SDNV this package produces or accepts: ten octets, which is what a full 64-bit value needs at 7 bits per octet.

Variables

View Source
var (
	// ErrDataTooShort indicates the input ended mid-value, with every octet
	// so far having its continuation bit set.
	ErrDataTooShort = errors.New("sdnv: data ended before the value did")

	// ErrOverflow indicates a value too large for a uint64.
	ErrOverflow = errors.New("sdnv: value does not fit in 64 bits")

	// ErrTooLong indicates an encoding running past MaxEncodedSize octets.
	// RFC 6256 clause 3.2 allows leading zero-padding octets (0x80), so an encoding
	// this long is not necessarily a value overflow, but this package caps
	// what it will read at ten octets, the width a canonical 64-bit value
	// needs, and refuses longer inputs with this distinct error.
	ErrTooLong = errors.New("sdnv: encoding is longer than the 10-octet maximum")
)

Functions

func AppendEncode

func AppendEncode(dst []byte, v uint64) []byte

AppendEncode appends the SDNV encoding of v to dst and returns the extended slice. Use it to build a segment without an allocation per field.

func Decode

func Decode(data []byte) (uint64, int, error)

Decode reads one SDNV from the front of data, returning the value and the number of octets consumed.

A value wider than 64 bits is an error rather than a silent wrap. So is an input that runs out while the continuation bit is still set.

func DecodeFrom

func DecodeFrom(r io.ByteReader) (uint64, error)

DecodeFrom reads one SDNV from r, one octet at a time. It reads no further than the end of the value.

func DecodeN

func DecodeN(data []byte, count int) ([]uint64, int, error)

DecodeN reads count consecutive SDNVs from the front of data, returning the values and the total octets consumed. It is the common case when a segment carries a run of adjacent SDNV fields.

func Encode

func Encode(v uint64) []byte

Encode returns the SDNV encoding of v.

func EncodedSize

func EncodedSize(v uint64) int

EncodedSize returns how many octets Encode will produce for v.

Types

This section is empty.

Jump to

Keyboard shortcuts

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