binaryutil

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jul 26, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package binaryutil provides endian-aware binary parsing utilities for the vhdi library.

Index

Constants

This section is empty.

Variables

View Source
var ErrInsufficientData = errors.New("insufficient data")

ErrInsufficientData is returned when there's not enough data to read.

Functions

func CRC32

func CRC32(data []byte) uint32

CRC32 computes a CRC-32C (Castagnoli) checksum matching the VHDX polynomial 0x82f63b78. Uses hardware acceleration where available.

This is a VHDX construct. VHD footers and dynamic disk headers use an entirely different algorithm — see VHDChecksum.

func CRC32WithInitial

func CRC32WithInitial(data []byte, initial uint32) uint32

CRC32WithInitial computes a CRC-32C checksum starting from an existing CRC state.

func GUIDEqual

func GUIDEqual(a, b [16]byte) bool

GUIDEqual compares two GUIDs for equality.

func GUIDFromString

func GUIDFromString(s string) ([16]byte, error)

GUIDFromString parses a GUID string and returns the byte array. Format: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx The first three components are stored as little-endian, matching GUIDToString.

func GUIDToString

func GUIDToString(guid [16]byte) string

GUIDToString converts a 16-byte GUID to its string representation (RFC 4122 format).

func VHDChecksum

func VHDChecksum(data []byte) uint32

VHDChecksum computes the checksum used by VHD file footers and dynamic disk headers, as defined by the VHD Image Format Specification: "a one's complement of the sum of all the bytes in the footer without the checksum field".

The caller must zero the structure's checksum field before calling.

This is deliberately not a CRC. Applying CRC32 (CRC-32C) to VHD structures rejects every conformant VHD image.

func VerifyCRC32

func VerifyCRC32(data []byte, storedChecksum uint32) bool

VerifyCRC32 verifies data against a stored CRC-32 checksum.

func VerifyVHDChecksum

func VerifyVHDChecksum(data []byte, storedChecksum uint32) bool

VerifyVHDChecksum verifies a VHD structure whose checksum field has already been zeroed against its stored checksum value.

Types

type ReadAtReader

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

ReadAtReader wraps an io.ReaderAt for offset-based binary parsing.

func NewReadAtReader

func NewReadAtReader(r io.ReaderAt, offset int64) *ReadAtReader

NewReadAtReader creates a new ReadAtReader at the given offset.

func NewReadAtReaderAtStart

func NewReadAtReaderAtStart(r io.ReaderAt) *ReadAtReader

NewReadAtReaderAtStart creates a new ReadAtReader at offset 0.

func (*ReadAtReader) Advance

func (r *ReadAtReader) Advance(delta int64)

Advance moves the offset by delta bytes.

func (*ReadAtReader) Offset

func (r *ReadAtReader) Offset() int64

Offset returns the current read offset.

func (*ReadAtReader) PeekBytes

func (r *ReadAtReader) PeekBytes(n int) ([]byte, error)

PeekBytes reads bytes without advancing the offset.

func (*ReadAtReader) PeekUint32BigEndian

func (r *ReadAtReader) PeekUint32BigEndian() (uint32, error)

PeekUint32BigEndian peeks at a 32-bit big-endian value without advancing.

func (*ReadAtReader) PeekUint32LittleEndian

func (r *ReadAtReader) PeekUint32LittleEndian() (uint32, error)

PeekUint32LittleEndian peeks at a 32-bit little-endian value without advancing.

func (*ReadAtReader) ReadByteArray

func (r *ReadAtReader) ReadByteArray(size int) ([]*byte, error)

ReadByteArray reads bytes into a fixed-size array.

func (*ReadAtReader) ReadBytes

func (r *ReadAtReader) ReadBytes(n int) ([]byte, error)

ReadBytes reads exactly n bytes from the reader at the current offset.

func (*ReadAtReader) ReadFixedBytes

func (r *ReadAtReader) ReadFixedBytes(size int) ([]byte, error)

ReadFixedBytes reads a fixed number of bytes into an array.

func (*ReadAtReader) ReadInt16BigEndian

func (r *ReadAtReader) ReadInt16BigEndian() (int16, error)

ReadInt16BigEndian reads a 16-bit big-endian signed integer.

func (*ReadAtReader) ReadInt16LittleEndian

func (r *ReadAtReader) ReadInt16LittleEndian() (int16, error)

ReadInt16LittleEndian reads a 16-bit little-endian signed integer.

func (*ReadAtReader) ReadInt32BigEndian

func (r *ReadAtReader) ReadInt32BigEndian() (int32, error)

ReadInt32BigEndian reads a 32-bit big-endian signed integer.

func (*ReadAtReader) ReadInt32LittleEndian

func (r *ReadAtReader) ReadInt32LittleEndian() (int32, error)

ReadInt32LittleEndian reads a 32-bit little-endian signed integer.

func (*ReadAtReader) ReadInt64BigEndian

func (r *ReadAtReader) ReadInt64BigEndian() (int64, error)

ReadInt64BigEndian reads a 64-bit big-endian signed integer.

func (*ReadAtReader) ReadInt64LittleEndian

func (r *ReadAtReader) ReadInt64LittleEndian() (int64, error)

ReadInt64LittleEndian reads a 64-bit little-endian signed integer.

func (*ReadAtReader) ReadString

func (r *ReadAtReader) ReadString(maxLen int) (string, error)

ReadString reads a fixed-length string, stripping null terminators.

func (*ReadAtReader) ReadUint8

func (r *ReadAtReader) ReadUint8() (uint8, error)

ReadUint8 reads a single byte.

func (*ReadAtReader) ReadUint16BigEndian

func (r *ReadAtReader) ReadUint16BigEndian() (uint16, error)

ReadUint16BigEndian reads a 16-bit big-endian unsigned integer.

func (*ReadAtReader) ReadUint16LittleEndian

func (r *ReadAtReader) ReadUint16LittleEndian() (uint16, error)

ReadUint16LittleEndian reads a 16-bit little-endian unsigned integer.

func (*ReadAtReader) ReadUint32BigEndian

func (r *ReadAtReader) ReadUint32BigEndian() (uint32, error)

ReadUint32BigEndian reads a 32-bit big-endian unsigned integer.

func (*ReadAtReader) ReadUint32LittleEndian

func (r *ReadAtReader) ReadUint32LittleEndian() (uint32, error)

ReadUint32LittleEndian reads a 32-bit little-endian unsigned integer.

func (*ReadAtReader) ReadUint64BigEndian

func (r *ReadAtReader) ReadUint64BigEndian() (uint64, error)

ReadUint64BigEndian reads a 64-bit big-endian unsigned integer.

func (*ReadAtReader) ReadUint64LittleEndian

func (r *ReadAtReader) ReadUint64LittleEndian() (uint64, error)

ReadUint64LittleEndian reads a 64-bit little-endian unsigned integer.

func (*ReadAtReader) SeekTo

func (r *ReadAtReader) SeekTo(offset int64)

SeekTo moves the offset to an absolute position.

func (*ReadAtReader) SkipBytes

func (r *ReadAtReader) SkipBytes(n int) error

SkipBytes skips n bytes without reading them.

type Reader

type Reader interface {
	// ReadUint32BigEndian reads a 32-bit unsigned integer in big-endian format.
	ReadUint32BigEndian() (uint32, error)

	// ReadUint32LittleEndian reads a 32-bit unsigned integer in little-endian format.
	ReadUint32LittleEndian() (uint32, error)

	// ReadUint64BigEndian reads a 64-bit unsigned integer in big-endian format.
	ReadUint64BigEndian() (uint64, error)

	// ReadUint64LittleEndian reads a 64-bit unsigned integer in little-endian format.
	ReadUint64LittleEndian() (uint64, error)

	// ReadUint16BigEndian reads a 16-bit unsigned integer in big-endian format.
	ReadUint16BigEndian() (uint16, error)

	// ReadUint16LittleEndian reads a 16-bit unsigned integer in little-endian format.
	ReadUint16LittleEndian() (uint16, error)

	// ReadUint8 reads a single byte.
	ReadUint8() (uint8, error)

	// ReadBytes reads exactly n bytes.
	ReadBytes(n int) ([]byte, error)

	// ReadString reads a null-terminated or fixed-length string.
	ReadString(maxLen int) (string, error)

	// Offset returns the current read offset.
	Offset() int64

	// SeekTo moves the offset to an absolute position.
	SeekTo(offset int64)

	// Advance moves the offset by delta bytes.
	Advance(delta int64)
}

Reader provides methods for reading binary data with explicit endianness handling.

Jump to

Keyboard shortcuts

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