binfmt

package
v2.1.1 Latest Latest
Warning

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

Go to latest
Published: Oct 23, 2025 License: BSD-3-Clause Imports: 9 Imported by: 0

Documentation

Overview

Package binfmt exposes utilities for formatting binary data with descriptive comments.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FHexDump added in v2.1.0

func FHexDump(w io.Writer, data []byte, width int, includeOffsets bool)

FHexDump writes a hex dump of the data to w.

func HexDump added in v2.1.0

func HexDump(data []byte, width int, includeOffsets bool) string

HexDump returns a string representation of the data in a hex dump format. The width is the number of bytes per line.

Types

type Formatter

type Formatter struct {
	// contains filtered or unexported fields
}

Formatter is a utility for formatting binary data with descriptive comments.

func New

func New(data []byte) *Formatter

New constructs a new binary formatter.

func (*Formatter) Byte

func (f *Formatter) Byte(format string, args ...interface{}) int

Byte formats a single byte in binary format, displaying each bit as a zero or one.

func (*Formatter) Data

func (f *Formatter) Data() []byte

Data returns the original data slice. Offset may be used to retrieve the current offset within the slice.

func (*Formatter) HexBytesln

func (f *Formatter) HexBytesln(n int, format string, args ...interface{}) int

HexBytesln formats the next n bytes in hexadecimal format, appending the formatted comment string to each line and ending on a newline.

func (*Formatter) HexTextln

func (f *Formatter) HexTextln(n int) int

HexTextln formats the next n bytes in hexadecimal format, appending a comment to each line showing the ASCII equivalent characters for each byte for bytes that are human-readable.

func (*Formatter) Line

func (f *Formatter) Line(n int) Line

Line prepares a single line of formatted output that will consume n bytes, but formatting those n bytes in multiple ways. The line will be prefixed with the offsets for the line's entire data.

func (*Formatter) LineWidth

func (f *Formatter) LineWidth(width int) *Formatter

LineWidth sets the Formatter's maximum line width for binary data.

func (*Formatter) More

func (f *Formatter) More() bool

More returns true if there is more data in the byte slice that can be formatted.

func (*Formatter) Offset

func (f *Formatter) Offset() int

Offset returns the current offset within the original data slice.

func (*Formatter) PeekUint

func (f *Formatter) PeekUint(w int) uint64

PeekUint reads a little-endian unsigned integer of the specified width at the current offset.

func (*Formatter) Pointer

func (f *Formatter) Pointer(off int) unsafe.Pointer

Pointer returns a pointer into the original data slice at the specified offset.

func (*Formatter) RelativeData

func (f *Formatter) RelativeData() []byte

RelativeData returns the subslice of the original data slice beginning at the offset at which SetAnchorOffset was last called. If SetAnchorOffset was never called, RelativeData is equivalent to Data.

func (*Formatter) RelativeOffset

func (f *Formatter) RelativeOffset() int

RelativeOffset retrieves the current offset relative to the offset at the last time SetAnchorOffset was called. If SetAnchorOffset was never called, RelativeOffset is equivalent to Offset.

func (*Formatter) Remaining

func (f *Formatter) Remaining() int

Remaining returns the number of unformatted bytes remaining in the byte slice.

func (*Formatter) SetAnchorOffset

func (f *Formatter) SetAnchorOffset()

SetAnchorOffset sets the reference point for relative offset calculations to the current offset. Future calls to RelativeOffset() will return an offset relative to the current offset.

func (*Formatter) SetLinePrefix

func (f *Formatter) SetLinePrefix(prefix string)

SetLinePrefix sets a prefix for each line of formatted output.

func (*Formatter) String

func (f *Formatter) String() string

String returns the current formatted output.

func (*Formatter) ToTreePrinter

func (f *Formatter) ToTreePrinter(tp treeprinter.Node)

ToTreePrinter formats the current output and creates a treeprinter child node for each line. The current output is reset; the position within the binary buffer is not.

func (*Formatter) Uvarint

func (f *Formatter) Uvarint(format string, args ...interface{})

Uvarint decodes the bytes at the current offset as a uvarint, formatting them in hexadecimal and prefixing the comment with the encoded decimal value.

type Line

type Line struct {
	// contains filtered or unexported fields
}

Line is a pending line of formatted binary output.

func (Line) Append

func (l Line) Append(s string) Line

Append appends the provided string to the current line.

func (Line) Binary

func (l Line) Binary(n int) Line

Binary formats the next n bytes in binary format, displaying each bit as a zero or one.

func (Line) Done

func (l Line) Done(format string, args ...interface{}) int

Done finishes the line, appending the provided comment if any.

func (Line) HexBytes

func (l Line) HexBytes(n int) Line

HexBytes formats the next n bytes in hexadecimal format.

Jump to

Keyboard shortcuts

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