Documentation
¶
Index ¶
- Constants
- Variables
- type Decoder
- func (d *Decoder) ReadBool() (bool, error)
- func (d *Decoder) ReadBytes() ([]byte, error)
- func (d *Decoder) ReadBytesInto(length uint32, buf []byte) (int, error)
- func (d *Decoder) ReadFloat64() (float64, error)
- func (d *Decoder) ReadInt64() (int64, error)
- func (d *Decoder) ReadLength() (uint32, error)
- func (d *Decoder) ReadNested(expected Tag, fn func(*Decoder) error) error
- func (d *Decoder) ReadString() (string, error)
- func (d *Decoder) ReadStrings() ([]string, error)
- func (d *Decoder) ReadTLVBool(expected Tag) (bool, error)
- func (d *Decoder) ReadTLVBytes(expected Tag) ([]byte, error)
- func (d *Decoder) ReadTLVBytesInto(expected Tag, buf []byte) (int, error)
- func (d *Decoder) ReadTLVFloat64(expected Tag) (float64, error)
- func (d *Decoder) ReadTLVInt64(expected Tag) (int64, error)
- func (d *Decoder) ReadTLVString(expected Tag) (string, error)
- func (d *Decoder) ReadTLVStrings(expected Tag) ([]string, error)
- func (d *Decoder) ReadTLVTime(expected Tag) (time.Time, error)
- func (d *Decoder) ReadTLVUint8(expected Tag) (uint8, error)
- func (d *Decoder) ReadTLVUint16(expected Tag) (uint16, error)
- func (d *Decoder) ReadTLVUint32(expected Tag) (uint32, error)
- func (d *Decoder) ReadTLVUint64(expected Tag) (uint64, error)
- func (d *Decoder) ReadTag() (Tag, error)
- func (d *Decoder) ReadTime() (time.Time, error)
- func (d *Decoder) ReadTimePrefixed() (time.Time, error)
- func (d *Decoder) ReadUint8() (uint8, error)
- func (d *Decoder) ReadUint16() (uint16, error)
- func (d *Decoder) ReadUint32() (uint32, error)
- func (d *Decoder) ReadUint64() (uint64, error)
- func (d *Decoder) Reset(r io.Reader)
- func (d *Decoder) Skip(n uint32) error
- func (d *Decoder) SkipTLV() error
- func (d *Decoder) VerifyTag(expected Tag) error
- type Encoder
- func (e *Encoder) Reset(w io.Writer)
- func (e *Encoder) WriteBool(v bool) error
- func (e *Encoder) WriteBytes(b []byte) error
- func (e *Encoder) WriteFloat64(v float64) error
- func (e *Encoder) WriteInt64(v int64) error
- func (e *Encoder) WriteLength(length uint32) error
- func (e *Encoder) WriteString(s string) error
- func (e *Encoder) WriteStrings(strs []string) error
- func (e *Encoder) WriteTLVBool(tag Tag, v bool) error
- func (e *Encoder) WriteTLVBytes(tag Tag, b []byte) error
- func (e *Encoder) WriteTLVFloat64(tag Tag, v float64) error
- func (e *Encoder) WriteTLVInt64(tag Tag, v int64) error
- func (e *Encoder) WriteTLVString(tag Tag, s string) error
- func (e *Encoder) WriteTLVStrings(tag Tag, strs []string) error
- func (e *Encoder) WriteTLVTime(tag Tag, t time.Time) error
- func (e *Encoder) WriteTLVUint8(tag Tag, v uint8) error
- func (e *Encoder) WriteTLVUint16(tag Tag, v uint16) error
- func (e *Encoder) WriteTLVUint32(tag Tag, v uint32) error
- func (e *Encoder) WriteTLVUint64(tag Tag, v uint64) error
- func (e *Encoder) WriteTag(tag Tag) error
- func (e *Encoder) WriteTime(t time.Time) error
- func (e *Encoder) WriteUint8(v uint8) error
- func (e *Encoder) WriteUint16(v uint16) error
- func (e *Encoder) WriteUint32(v uint32) error
- func (e *Encoder) WriteUint64(v uint64) error
- type Tag
- type UnexpectedTagError
Constants ¶
const ( // Default safety limits DefaultMaxStringSize = 1 << 20 // 1 MB DefaultMaxBytesSize = 10 << 20 // 10 MB DefaultMaxListCount = 1000 DefaultMaxSkipSize = 10 << 20 // 10 MB )
Variables ¶
var ( ErrPayloadTooLarge = errors.New("ztlv: payload exceeds configured limit") ErrListTooLarge = errors.New("ztlv: list count exceeds configured limit") ErrShortBuffer = errors.New("ztlv: buffer too short") ErrInvalidTag = errors.New("ztlv: invalid tag found") ErrInvalidLength = errors.New("ztlv: invalid length for type") )
Functions ¶
This section is empty.
Types ¶
type Decoder ¶
type Decoder struct {
MaxStringSize uint32
MaxBytesSize uint32
MaxListCount uint32
// This prevents blocking on malicious huge unknown tags.
MaxSkipSize uint32
// contains filtered or unexported fields
}
func NewDecoder ¶
func (*Decoder) ReadBytes ¶
ReadBytes is optimized for a single allocation. It is "Low Alloc" but safe (returns a clean copy).
func (*Decoder) ReadBytesInto ¶
ReadBytesInto enables "Zero Alloc" scenarios if the caller reuses their buffer. Warning: This method reads strictly 'length' bytes. It does NOT read the length prefix itself. The caller must have read the length beforehand (e.g. via ReadLength()). It returns the number of bytes read (which is always equal to length on success) and any error.
func (*Decoder) ReadFloat64 ¶
ReadFloat64 reads a float64 (8 bytes, IEEE 754, Big Endian).
func (*Decoder) ReadLength ¶
func (*Decoder) ReadNested ¶
ReadNested handles a TLV container (a TLV that contains other TLVs). It reads the Tag and Length, creates a limited Decoder for the body, and executes the callback.
The nested Decoder is reused from d.nestedDec to avoid a heap allocation per call. Any unread bytes within the container are drained automatically after the callback returns, ensuring the parent stream remains correctly positioned.
func (*Decoder) ReadString ¶
ReadString uses "unsafe" optimizations (Go 1.20+) to reduce allocations.
func (*Decoder) ReadStrings ¶
ReadStrings decodes a list of strings.
NOTE: Returns nil (not []string{}) for an empty list — this is intentional and idiomatic in Go. Callers should use len(strs) == 0 rather than strs == nil to test for emptiness.
This differs from ReadBytes which returns []byte{} for empty payloads, because a nil byte slice can cause unexpected downstream panics; a nil string slice cannot.
This asymmetry is a known API rough edge. Always use len(result) == 0 for both types to write forward-compatible code.
func (*Decoder) ReadTLVBool ¶
ReadTLVBool reads a Tag, verifies it, reads Length (must be 1), then reads Bool.
func (*Decoder) ReadTLVBytes ¶
ReadTLVBytes reads a Tag, verifies it, reads Length, then reads Bytes.
func (*Decoder) ReadTLVBytesInto ¶ added in v1.1.0
ReadTLVBytesInto reads a Tag, verifies it, reads Length, checks buffer size, and reads bytes directly into the provided buffer. This is the most efficient and secure way to read bytes without allocation.
func (*Decoder) ReadTLVFloat64 ¶
ReadTLVFloat64 reads a Tag, verifies it, reads Length (must be 8), then reads Float64.
func (*Decoder) ReadTLVInt64 ¶
ReadTLVInt64 reads a Tag, verifies it, reads Length (must be 8), then reads Int64.
func (*Decoder) ReadTLVString ¶
ReadTLVString reads a Tag, verifies it, reads Length, then reads String.
func (*Decoder) ReadTLVStrings ¶ added in v1.1.2
ReadTLVStrings reads a Tag, verifies it, then reads the list of strings.
func (*Decoder) ReadTLVTime ¶
ReadTLVTime reads a Tag, verifies it, reads Length (must be 12), then reads Time.
func (*Decoder) ReadTLVUint8 ¶
ReadTLVUint8 reads a Tag, verifies it, reads Length (must be 1), then reads Uint8.
func (*Decoder) ReadTLVUint16 ¶
ReadTLVUint16 reads a Tag, verifies it, reads Length (must be 2), then reads Uint16.
func (*Decoder) ReadTLVUint32 ¶
ReadTLVUint32 reads a Tag, verifies it, reads Length (must be 4), then reads Uint32.
func (*Decoder) ReadTLVUint64 ¶
ReadTLVUint64 reads a Tag, verifies it, reads Length (must be 8), then reads Uint64.
func (*Decoder) ReadTime ¶
ReadTime decodes a robust timestamp (Seconds + Nanoseconds). Note: This reads strictly 12 bytes. It does NOT read a length prefix. Use ReadTimePrefixed if you are using standard TLV (WriteTLVTime).
func (*Decoder) ReadTimePrefixed ¶
ReadTimePrefixed reads a Length prefix, verifies it is 12, and then reads the Time. This is the secure counterpart to WriteTLVTime.
func (*Decoder) ReadUint16 ¶
ReadUint16 reads a uint16 (2 bytes, Big Endian).
func (*Decoder) ReadUint32 ¶
ReadUint32 reads a uint32 (4 bytes, Big Endian).
func (*Decoder) ReadUint64 ¶
ReadUint64 reads a uint64 (8 bytes, Big Endian).
type Encoder ¶
type Encoder struct {
// contains filtered or unexported fields
}
Encoder remains lightweight. Allocations are primarily handled by the underlying Writer.
func NewEncoder ¶
func (*Encoder) WriteBytes ¶
WriteBytes writes a standard byte slice.
func (*Encoder) WriteFloat64 ¶
WriteFloat64 writes a float64 (8 bytes, IEEE 754, Big Endian).
func (*Encoder) WriteInt64 ¶
WriteInt64 writes an int64 (8 bytes, Big Endian).
func (*Encoder) WriteLength ¶
func (*Encoder) WriteString ¶
func (*Encoder) WriteStrings ¶
func (*Encoder) WriteTLVBool ¶
WriteTLVBool writes a Tag, Length (1), then the Bool (1 byte).
func (*Encoder) WriteTLVBytes ¶
WriteTLVBytes writes a Tag, then the Length of the bytes, then the Bytes themselves.
func (*Encoder) WriteTLVFloat64 ¶
WriteTLVFloat64 writes a Tag, Length (8), then the Float64 (8 bytes).
func (*Encoder) WriteTLVInt64 ¶
WriteTLVInt64 writes a Tag, Length (8), then the Int64 (8 bytes).
func (*Encoder) WriteTLVString ¶
WriteTLVString writes a Tag, then the Length of the string, then the String itself.
func (*Encoder) WriteTLVStrings ¶ added in v1.1.2
WriteTLVStrings writes a Tag, then the list of strings (count + each string TLV).
func (*Encoder) WriteTLVTime ¶
WriteTLVTime writes a Tag, then the Length of the time (12 bytes), then the Time itself.
func (*Encoder) WriteTLVUint8 ¶
WriteTLVUint8 writes a Tag, Length (1), then the Uint8 (1 byte).
func (*Encoder) WriteTLVUint16 ¶
WriteTLVUint16 writes a Tag, Length (2), then the Uint16 (2 bytes).
func (*Encoder) WriteTLVUint32 ¶
WriteTLVUint32 writes a Tag, Length (4), then the Uint32 (4 bytes).
func (*Encoder) WriteTLVUint64 ¶
WriteTLVUint64 writes a Tag, Length (8), then the Uint64 (8 bytes).
func (*Encoder) WriteTime ¶
WriteTime encodes a timestamp robustly: compatible with all dates, avoids Year 2038 issues.
func (*Encoder) WriteUint8 ¶
WriteUint8 writes a uint8 (1 byte).
func (*Encoder) WriteUint16 ¶
WriteUint16 writes a uint16 (2 bytes, Big Endian).
func (*Encoder) WriteUint32 ¶
WriteUint32 writes a uint32 (4 bytes, Big Endian).
func (*Encoder) WriteUint64 ¶
WriteUint64 writes a uint64 (8 bytes, Big Endian).
type Tag ¶
type Tag byte
Tag is a single-byte TLV identifier (0x00–0xFF). NOTE: 256 possible tags is a known limitation of this format. If your protocol requires more identifiers, consider a two-byte tag or varint encoding — this is a planned future extension.
type UnexpectedTagError ¶
UnexpectedTagError is returned when the read tag does not match the expected tag.
func (*UnexpectedTagError) Error ¶
func (e *UnexpectedTagError) Error() string