bytestream

package
v1.1.0 Latest Latest
Warning

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

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

Documentation

Overview

Package bytestream reads binary formats a field at a time.

A Stream tracks a byte position and an intra-byte bit position, so sub-byte fields compose without the caller doing the shifting. Moving outside the buffer panics with a StreamError rather than returning one: a parser that has walked off the end has misread its input and cannot usefully continue, so the caller recovers once at the point where giving up makes sense instead of checking after every read.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Stream

type Stream struct {
	Bytes []byte
	Pos   int
	// contains filtered or unexported fields
}

Stream is a port of CyberChef's lib/Stream.mjs: a byte reader tracking a byte position and an intra-byte bit position. The packet parsers use the reading half; the file carvers additionally seek about and slice pieces out.

func New

func New(b []byte) *Stream

New returns a stream positioned at the start of b. The stream reads b in place and never copies it.

func (*Stream) Advance

func (s *Stream) Advance(n int)

Advance moves the position by n without requiring the result to lie inside the buffer. Stream.mjs has no such method: the carvers that need it assign to `position` directly, which skips the bounds check that moveForwardsBy makes, and rely on running off the end to finish a loop. Moving to before the start is still refused, as no read could follow it.

func (*Stream) At

func (s *Stream) At(i int) int

At returns the byte At i, or 0 past either end of the buffer.

func (*Stream) Carve

func (s *Stream) Carve(start, finish int) []byte

Carve returns the bytes between start and finish. A part-read byte at finish is taken whole, as the bits already consumed belong to the carved region.

func (*Stream) Clone

func (s *Stream) Clone() *Stream

Clone returns a reader over the same bytes at the same position. The two then move independently, which lets a carver look ahead without losing its place.

func (*Stream) ConsumeIf

func (s *Stream) ConsumeIf(val byte)

ConsumeIf advances over the next byte when it is val.

func (*Stream) ConsumeWhile

func (s *Stream) ConsumeWhile(val byte)

ConsumeWhile advances over a run of val.

func (*Stream) ContinueUntil

func (s *Stream) ContinueUntil(seq []byte)

ContinueUntil moves to the start of the next occurrence of seq at or after the current position, or to the end of the buffer if there is none.

CyberChef's Stream.ContinueUntil takes a different path for a sequence than for a single byte: it abandons the current position and scans from the length of the sequence, and its skip table is indexed by the pattern byte that failed rather than the text byte found, so the scan can step over a match that is there. Both are defects — the callers all mean "find the next one from here" — so this searches plainly forwards instead. On well-formed input the two agree.

func (*Stream) ContinueUntilByte

func (s *Stream) ContinueUntilByte(val byte)

ContinueUntilByte moves to the next occurrence of val, or to the end of the buffer if there is none. The byte under the current position is skipped, so repeated calls walk successive occurrences rather than standing still.

func (*Stream) GetBytes

func (s *Stream) GetBytes(numBytes int) []byte

GetBytes returns numBytes bytes from the current position (all remaining when numBytes < 0), advancing the position and clearing the bit position.

func (*Stream) HasMore

func (s *Stream) HasMore() bool

HasMore reports whether any byte remains to be read.

func (*Stream) Length

func (s *Stream) Length() int

Length returns the size of the underlying buffer.

func (*Stream) LookingAt

func (s *Stream) LookingAt(seq []byte) bool

LookingAt reports whether seq sits at the current position, without moving.

func (*Stream) MoveBackwardsBy

func (s *Stream) MoveBackwardsBy(n int)

MoveBackwardsBy retreats the stream position by n bytes.

func (*Stream) MoveForwardsBy

func (s *Stream) MoveForwardsBy(n int)

MoveForwardsBy advances the stream position by n bytes.

func (*Stream) MoveTo

func (s *Stream) MoveTo(pos int)

MoveTo sets the stream position, which must lie within the buffer. The end of the buffer counts as within it: that is where a read of the last byte lands.

func (*Stream) Peek

func (s *Stream) Peek(i int) byte

Peek returns the byte at absolute position i without moving the stream. A position outside the buffer is an out-of-bounds read, as for a move.

func (*Stream) ReadBits

func (s *Stream) ReadBits(numBits int) int

ReadBits reads numBits big-endian bits, tracking the intra-byte position so successive sub-byte reads compose correctly.

func (*Stream) ReadInt

func (s *Stream) ReadInt(numBytes int) int

ReadInt reads a big-endian integer of numBytes bytes.

func (*Stream) ReadIntLE

func (s *Stream) ReadIntLE(numBytes int) int

ReadIntLE reads a little-endian integer of numBytes bytes.

func (*Stream) ReadIntOrder

func (s *Stream) ReadIntOrder(numBytes int, little bool) int

ReadIntOrder reads an integer in either byte order. Bytes past the end of the buffer read as zero, as indexing past a Uint8Array yields undefined and the JS bitwise operators coerce that to 0.

func (*Stream) ReadString

func (s *Stream) ReadString(numBytes int) string

ReadString reads numBytes bytes as a string, stopping the string at the first null byte but still consuming the whole width. A negative width reads to the end.

type StreamError

type StreamError struct{ Pos int }

StreamError is raised when a stream is asked to move outside its buffer. Stream.mjs throws in the same places, and a carver that walks off the end of its buffer has misread the file and must abandon the carve; unwinding to the boundary that owns the read keeps that check out of every read, where a recovery turns it back into an ordinary error value.

func (StreamError) Error

func (e StreamError) Error() string

Jump to

Keyboard shortcuts

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