types

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Sep 17, 2026 License: MIT Imports: 4 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package types defines all data structures, enums, and constants for the libvhdi library.

Index

Constants

View Source
const (
	// VHD signatures
	VHDFooterSignature        = "conectix"
	VHDDynamicDiskSignature   = "cxsparse"
	VHDDynamicHeaderSignature = "cxsparse"

	// VHDX signatures
	VHDXFileSignature     = "vhdxfile"
	VHDXHeaderSignature   = "head"
	VHDXRegionSignature   = "regi"
	VHDXMetadataSignature = "metadata"

	// Unallocated block markers
	VHDUnallocatedBlockMarker  uint32 = 0xFFFFFFFF
	VHDXUnallocatedBlockMarker uint64 = 0xFFFFFFFFFFFFFFFF
)
View Source
const (
	VHDFooterSize        = 512
	VHDDynamicHeaderSize = 1024
	VHDXHeaderSize       = 4096
	VHDXFileInfoSize     = 520
	VHDXRegionTableSize  = 64 * 1024 // 64 KB
	VHDXMetadataSize     = 64 * 1024 // 64 KB

	// Standard VHD offsets
	VHDFooterOffset = -512 // From end of file

	// Standard VHDX offsets (in bytes)
	VHDXFileInfoOffset          = 0
	VHDXFirstHeaderOffset       = 64 * 1024  // 64 KB
	VHDXSecondHeaderOffset      = 128 * 1024 // 128 KB
	VHDXFirstRegionTableOffset  = 192 * 1024 // 192 KB  (= 3 × 64 KB)
	VHDXSecondRegionTableOffset = 256 * 1024 // 256 KB  (= 4 × 64 KB)
	VHDXLogAreaOffset           = 256 * 1024 // legacy alias; use VHDXSecondRegionTableOffset

	// VHD/VHDX block sizes
	DefaultSectorSize = 512

	// Parent locator
	VHDParentLocatorCount = 8
	VHDParentLocatorSize  = 24
	VHDParentFilenameSize = 512
)
View Source
const (
	VHDFormatVersion  = 0x00010000 // VHD v1.0
	VHDXFormatVersion = 0x0001     // VHDX v1.0
)
View Source
const (
	MaxCacheEntriesBlockDescriptors = 8
)
View Source
const NoOffset int64 = -1

NoOffset marks a StructuralError that is not about a particular byte.

Variables

View Source
var (
	// ErrCorruptImage is returned when an image's structures are internally
	// inconsistent, fail their checksum, or describe something that does not fit
	// the file.
	//
	// It says the image is damaged or was never valid. It does not say the
	// library is missing a feature, which is what ErrUnsupportedFeature is for --
	// and the difference matters to whoever is holding the evidence. One means
	// the file is broken; the other means to try a different tool.
	ErrCorruptImage = errors.New("libvhdi: corrupt or malformed image")

	// ErrUnsupportedFeature is returned when an image is well-formed but uses
	// something this library does not implement.
	//
	// Refusing such an image is deliberate. The alternative -- decoding the parts
	// that are understood and ignoring the rest -- produces a device that is
	// wrong in a way nothing downstream can detect.
	ErrUnsupportedFeature = errors.New("libvhdi: unsupported feature")
)
View Source
var (
	// BAT region identifier
	RegionTypeBAT = [16]byte{0x66, 0x77, 0xc2, 0x2d, 0x23, 0xf6, 0x00, 0x42, 0x9d, 0x64, 0x11, 0x5e, 0x9b, 0xfd, 0x4a, 0x08}

	// Metadata region identifier
	RegionTypeMetadata = [16]byte{0x06, 0xa2, 0x7c, 0x8b, 0x90, 0x47, 0x9a, 0x4b, 0xb8, 0xfe, 0x57, 0x5f, 0x05, 0x0f, 0x88, 0x6e}
)

Regional type identifiers (as byte arrays, little-endian stored)

View Source
var (
	// File parameters
	MetadataItemFileParameters = [16]byte{0x37, 0x67, 0xa1, 0xca, 0x36, 0xfa, 0x43, 0x4d, 0xb3, 0xb6, 0x33, 0xf0, 0xaa, 0x44, 0xe7, 0x6b}

	// Logical sector size
	MetadataItemLogicalSectorSize = [16]byte{0x1d, 0xbf, 0x41, 0x81, 0x6f, 0xa9, 0x09, 0x47, 0xba, 0x47, 0xf2, 0x33, 0xa8, 0xfa, 0xab, 0x5f}

	// Parent locator
	MetadataItemParentLocator = [16]byte{0x2d, 0x5f, 0xd3, 0xa8, 0x0b, 0xb3, 0x4d, 0x45, 0xab, 0xf7, 0xd3, 0xd8, 0x48, 0x34, 0xab, 0x0c}

	// Physical sector size
	MetadataItemPhysicalSectorSize = [16]byte{0xc7, 0x48, 0xa3, 0xcd, 0x5d, 0x44, 0x71, 0x44, 0x9c, 0xc9, 0xe9, 0x88, 0x52, 0x51, 0xc5, 0x56}

	// Virtual disk identifier
	MetadataItemVirtualDiskIdentifier = [16]byte{0xab, 0x12, 0xca, 0xbe, 0xe6, 0xb2, 0x23, 0x45, 0x93, 0xef, 0xc3, 0x09, 0xe0, 0x00, 0xc7, 0x46}

	// Virtual disk size
	MetadataItemVirtualDiskSize = [16]byte{0x24, 0x42, 0xa5, 0x2f, 0x1b, 0xcd, 0x76, 0x48, 0xb2, 0x11, 0x5d, 0xbe, 0xd8, 0x3b, 0xf4, 0xb8}
)

Metadata item identifiers (as byte arrays, little-endian stored)

View Source
var (
	// ParentLocatorTypeVHDX is the only locator type the VHDX specification
	// defines. An image declaring a different type describes its parent by some
	// other scheme, so its entries must not be read as VHDX parent locator keys.
	ParentLocatorTypeVHDX = [16]byte{0xb7, 0xef, 0x4a, 0xb0, 0x9e, 0xd1, 0x81, 0x4a, 0xb7, 0x89, 0x25, 0xb8, 0xe9, 0x44, 0x59, 0x13}
)

Parent locator type identifiers (as byte arrays, little-endian stored)

Functions

func Corruptf added in v0.3.0

func Corruptf(op string, format FileFormat, offset int64, field, msg string, args ...any) error

Corruptf reports a malformed image, recording where the problem is.

Pass NoOffset for offset when the failure is not about a particular byte, and "" for field when no single field is at fault.

func Unsupportedf added in v0.3.0

func Unsupportedf(op string, format FileFormat, offset int64, field, msg string, args ...any) error

Unsupportedf reports a well-formed image this library cannot read.

Types

type BlockAllocationEntry

type BlockAllocationEntry struct {
	IsAllocated bool       // Derived from entry value
	FileOffset  int64      // Calculated offset in file
	BlockState  BlockState // For VHDX
}

BlockAllocationEntry represents a single BAT entry. For VHD: 4 bytes (big-endian uint32) For VHDX: 8 bytes (little-endian uint64)

type BlockDescriptor

type BlockDescriptor struct {
	FileOffset   int64
	BlockState   BlockState
	SectorRanges []SectorRangeDescriptor
}

BlockDescriptor represents information about a single block.

type BlockState

type BlockState uint8

BlockState represents the allocation state of a block (VHDX).

const (
	BlockStateNone               BlockState = 0 // PAYLOAD_BLOCK_NOT_PRESENT
	BlockStateUndefined          BlockState = 1 // PAYLOAD_BLOCK_UNDEFINED
	BlockStateZero               BlockState = 2 // PAYLOAD_BLOCK_ZERO
	BlockStateUnmapped           BlockState = 3 // PAYLOAD_BLOCK_UNMAPPED
	BlockStateFullyAllocated     BlockState = 6 // PAYLOAD_BLOCK_FULLY_PRESENT
	BlockStatePartiallyAllocated BlockState = 7 // PAYLOAD_BLOCK_PARTIALLY_PRESENT
)

type DiskType

type DiskType uint32

DiskType represents the type of virtual disk.

const (
	DiskTypeFixed        DiskType = 0x00000002
	DiskTypeDynamic      DiskType = 0x00000003
	DiskTypeDifferential DiskType = 0x00000004
)

func (DiskType) String added in v0.3.0

func (t DiskType) String() string

String returns the disk type name. Unknown values render with their numeric value, since a disk type this library does not recognise is exactly the detail a reader of the error needs.

type DynamicDiskHeader

type DynamicDiskHeader struct {
	Signature              [8]byte  // "cxsparse"
	NextOffset             uint64   // Big-endian
	BlockTableOffset       uint64   // Big-endian
	FormatVersion          uint32   // Big-endian
	NumberOfBlocks         uint32   // Big-endian
	BlockSize              uint32   // Big-endian
	Checksum               uint32   // Big-endian (CRC-32)
	ParentIdentifier       [16]byte // GUID
	ParentModificationTime uint32   // Big-endian (Unix timestamp)
	Reserved1              [4]byte
	ParentFilename         [512]byte    // UTF-16 BE
	ParentLocatorEntries   [8 * 24]byte // 8 entries × 24 bytes each
	Reserved2              [256]byte
}

DynamicDiskHeader represents a VHD dynamic disk header (1024 bytes).

type FileFooter

type FileFooter struct {
	Signature          [8]byte  // "conectix"
	Features           uint32   // Big-endian
	FormatVersion      uint32   // Big-endian (0x00010000 = v1.0)
	NextOffset         uint64   // Big-endian
	ModificationTime   uint32   // Big-endian (Unix timestamp)
	CreatorApplication uint32   // Big-endian
	CreatorVersion     uint32   // Big-endian
	CreatorOS          uint32   // Big-endian
	DiskSize           uint64   // Big-endian (virtual disk size)
	DataSize           uint64   // Big-endian (physical data size)
	DiskGeometry       uint32   // Big-endian (CHS)
	DiskType           uint32   // Big-endian
	Checksum           uint32   // Big-endian (CRC-32)
	Identifier         [16]byte // GUID
	SavedState         byte
	Reserved           [427]byte
}

FileFooter represents a VHD file footer (512 bytes).

type FileFormat

type FileFormat int

FileFormat represents the format of the virtual disk file.

const (
	FileFormatUnknown FileFormat = iota
	FileFormatVHD
	FileFormatVHDX
)

func (FileFormat) String added in v0.3.0

func (f FileFormat) String() string

String returns the format name, so errors and reports read as "VHDX" rather than as an integer whose meaning the reader has to look up.

type FileInformation

type FileInformation struct {
	Signature [8]byte   // "vhdxfile"
	Creator   [512]byte // UTF-8 string
}

FileInformation represents VHDX file information (520 bytes).

type ImageHeader

type ImageHeader struct {
	Signature           [4]byte  // "head"
	Checksum            uint32   // Little-endian (CRC-32)
	SequenceNumber      uint64   // Little-endian
	FileWriteIdentifier [16]byte // GUID
	DataWriteIdentifier [16]byte // GUID
	LogIdentifier       [16]byte // GUID
	LogFormatVersion    uint16   // Little-endian
	FormatVersion       uint16   // Little-endian (0x0001 = v1.0)
	LogSize             uint32   // Little-endian
	LogOffset           uint64   // Little-endian
	Reserved            [4016]byte
}

ImageHeader represents a VHDX image header (4096 bytes).

type MetadataTableEntry

type MetadataTableEntry struct {
	ItemIdentifier [16]byte // GUID
	ItemOffset     uint32   // Little-endian
	ItemSize       uint32   // Little-endian
	Reserved       [8]byte
}

MetadataTableEntry represents a single metadata table entry (32 bytes).

type MetadataTableHeader

type MetadataTableHeader struct {
	Signature       [8]byte // "metadata"
	Reserved1       [2]byte
	NumberOfEntries uint16 // Little-endian
	Reserved2       [20]byte
}

MetadataTableHeader represents a VHDX metadata table header (32 bytes).

type MetadataValues

type MetadataValues struct {
	BlockSize             uint32
	DiskType              DiskType
	LogicalSectorSize     uint32
	ParentIdentifier      [16]byte
	ParentFilename        string
	PhysicalSectorSize    uint32
	VirtualDiskIdentifier [16]byte
	VirtualDiskSize       uint64
	LeaveBitsUnallocated  bool

	// ParentLocators holds every key/value pair from the parent locator
	// metadata item. A VHDX child commonly records several candidate paths
	// (relative, absolute and volume-relative), and any of them may be the one
	// that still resolves.
	ParentLocators []ParentLocatorEntry
}

MetadataValues represents extracted VHDX metadata values.

type ParentLocatorEntry

type ParentLocatorEntry struct {
	PlatformCode       uint32 // Big-endian (0=None, 1=Wi2r, 2=Wi2ku, 3=W2ru, 4=W2ku)
	PlatformDataSpace  uint32 // Big-endian (reserved)
	PlatformDataLength uint32 // Big-endian
	Reserved           uint32 // Big-endian
	PlatformDataOffset uint64 // Big-endian
	Key                string
	Value              string
}

ParentLocatorEntry represents a single parent locator entry.

type ParsedDynamicDiskHeader

type ParsedDynamicDiskHeader struct {
	FormatVersion    uint32
	BlockTableOffset int64
	NextOffset       int64
	BlockSize        uint32
	NumberOfBlocks   uint32
	ParentIdentifier [16]byte
	ParentModTime    time.Time
	ParentFilename   string
	ParentLocators   []ParentLocatorEntry
}

ParsedDynamicDiskHeader represents a parsed VHD dynamic disk header.

type ParsedFileFooter

type ParsedFileFooter struct {
	FormatVersion uint32
	NextOffset    int64
	MediaSize     uint64
	DiskType      DiskType
	Checksum      uint32
	Identifier    [16]byte
	ModTime       time.Time
	CreatorApp    string

	// Features is the footer's feature flag word. Bit 1 means the image has a
	// temporary-disk marker and bit 0 is reserved; bit 31 is always set in a
	// conformant image.
	Features uint32

	// CreatorVersion is the creating application's version, packed as two
	// 16-bit halves, and CreatorOS is its host operating system as a four-byte
	// code ("Wi2k" for Windows, "Mac " for Macintosh).
	//
	// Both identify the producer of an image, which is exactly the kind of fact
	// an examiner reasons about when two images disagree.
	CreatorVersion uint32
	CreatorOS      string

	// DataSize is the disk's size at creation time, where MediaSize is its
	// current size. The two differ on an image that has been expanded, and the
	// difference is the only record that the expansion happened.
	DataSize uint64

	// Cylinders, Heads and SectorsPerTrack are the CHS geometry the footer
	// records. Modern tools ignore CHS, but the values still constrain how a
	// legacy guest partitions the disk, and a zero geometry is itself a fact
	// worth reporting.
	Cylinders       uint16
	Heads           uint8
	SectorsPerTrack uint8

	// SavedState marks an image saved from a running machine with memory state
	// held elsewhere. Its filesystem is a crash-consistent snapshot, not a
	// cleanly unmounted one, which changes what its contents can be taken to
	// mean.
	SavedState bool
}

ParsedFileFooter represents a parsed VHD file footer.

type ParsedFileInformation

type ParsedFileInformation struct {
	Signature string
	Creator   string
}

ParsedFileInformation represents parsed VHDX file information.

type ParsedImageHeader

type ParsedImageHeader struct {
	Checksum            uint32
	SequenceNumber      uint64
	FormatVersion       uint16
	LogFormatVersion    uint16
	LogSize             uint32
	LogOffset           int64
	FileWriteIdentifier [16]byte
	DataWriteIdentifier [16]byte
	LogIdentifier       [16]byte
}

ParsedImageHeader represents a parsed VHDX image header.

type ParsedMetadataTableEntry

type ParsedMetadataTableEntry struct {
	ItemIdentifier [16]byte
	ItemOffset     uint32
	ItemSize       uint32
	IsRequired     bool // bit 2 of the flags word
}

ParsedMetadataTableEntry represents a parsed metadata table entry.

type ParsedRegionTableEntry

type ParsedRegionTableEntry struct {
	TypeIdentifier [16]byte
	DataOffset     int64
	DataSize       uint32
	IsRequired     bool
}

ParsedRegionTableEntry represents a parsed region table entry.

type RegionTableEntry

type RegionTableEntry struct {
	TypeIdentifier [16]byte // GUID
	DataOffset     uint64   // Little-endian
	DataSize       uint32   // Little-endian
	IsRequired     uint32   // Little-endian (0 or 1)
}

RegionTableEntry represents a single region table entry (32 bytes).

type RegionTableHeader

type RegionTableHeader struct {
	Signature       [4]byte // "regi"
	Checksum        uint32  // Little-endian (CRC-32)
	NumberOfEntries uint32  // Little-endian
	Reserved        uint32  // Little-endian
}

RegionTableHeader represents a VHDX region table header (16 bytes).

type SectorRangeDescriptor

type SectorRangeDescriptor struct {
	StartOffset uint64 // Start of allocated region
	EndOffset   uint64 // End of allocated region
	Flags       SectorRangeFlag
}

SectorRangeDescriptor represents an allocated sector range within a block.

type SectorRangeFlag

type SectorRangeFlag uint32

SectorRangeFlag represents flags for sector ranges.

const (
	SectorRangeFlagUnallocated SectorRangeFlag = 0x00000001
)

type StructuralError added in v0.3.0

type StructuralError struct {
	// Op is what was being attempted, in the imperative: "read VHD footer",
	// "parse VHDX metadata table".
	Op string

	// Format is the image format being parsed, or FileFormatUnknown before the
	// format has been established.
	Format FileFormat

	// Offset is the byte offset in the image the failure concerns. It is
	// NoOffset when the failure is not about a particular byte, such as two
	// fields disagreeing with each other.
	Offset int64

	// Field names the structure field at fault, spelled as the specification
	// spells it, so it can be looked up.
	Field string

	// Err is the underlying cause, wrapping ErrCorruptImage or
	// ErrUnsupportedFeature.
	Err error
}

StructuralError says what was being parsed, where, and what was wrong with it.

A bare "invalid VHD footer signature" tells an examiner that something failed and nothing else. Which of the two footer copies? At what offset? Carrying the location turns an error into a starting point for a manual examination of the image, which is the only recourse once the library has given up.

It wraps one of ErrCorruptImage or ErrUnsupportedFeature, so errors.Is continues to answer the coarse question.

func (*StructuralError) Error added in v0.3.0

func (e *StructuralError) Error() string

Error implements error.

func (*StructuralError) Unwrap added in v0.3.0

func (e *StructuralError) Unwrap() error

Unwrap returns the underlying cause, so errors.Is reaches the sentinel.

type VHDBlockAllocationTable

type VHDBlockAllocationTable struct {
	NumberOfEntries  uint32
	TableOffset      int64
	BlockSize        uint32
	SectorBitmapSize uint32
	Entries          []BlockAllocationEntry
}

VHDBlockAllocationTable represents VHD BAT information.

type VHDXBlockAllocationTable

type VHDXBlockAllocationTable struct {
	NumberOfEntries     uint32
	TableOffset         int64
	BlockSize           uint32
	SectorBitmapSize    uint32
	ChunkRatio          uint32  // data blocks per sector bitmap entry
	SectorSize          uint32  // logical sector size in bytes
	SectorBitmapOffsets []int64 // per-chunk sector bitmap file offsets; -1 = not present
	Entries             []BlockAllocationEntry
}

VHDXBlockAllocationTable represents VHDX BAT information.

Jump to

Keyboard shortcuts

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