Documentation
¶
Overview ¶
Package types defines all data structures, enums, and constants for the libvhdi library.
Index ¶
- Constants
- Variables
- func CHSGeometry(totalSectors uint64) (cylinders uint16, heads, sectorsPerTrack uint8)
- func Checksum(b []byte) uint32
- func Corruptf(op string, format FileFormat, offset int64, field, msg string, args ...any) error
- func FooterChecksum(f *FileFooter) uint32
- func PackGeometry(c uint16, h, s uint8) uint32
- func Time(v uint32) time.Time
- func Timestamp(t time.Time) uint32
- func UnpackGeometry(g uint32) (cylinders uint16, heads, sectorsPerTrack uint8)
- func Unsupportedf(op string, format FileFormat, offset int64, field, msg string, args ...any) error
- type BlockAllocationEntry
- type BlockDescriptor
- type BlockState
- type DiskType
- type DynamicDiskHeader
- type FileFooter
- type FileFormat
- type FileInformation
- type ImageHeader
- type MetadataTableEntry
- type MetadataTableHeader
- type MetadataValues
- type ParentLocatorEntry
- type ParsedDynamicDiskHeader
- type ParsedFileFooter
- type ParsedFileInformation
- type ParsedImageHeader
- type ParsedMetadataTableEntry
- type ParsedRegionTableEntry
- type RegionTableEntry
- type RegionTableHeader
- type SectorRangeDescriptor
- type SectorRangeFlag
- type StructuralError
- type VHDBlockAllocationTable
- type VHDXBlockAllocationTable
Constants ¶
const ( // VHD signatures VHDDynamicDiskSignature = "cxsparse" VHDDynamicHeaderSignature = "cxsparse" // VHDX signatures VHDXFileSignature = "vhdxfile" VHDXHeaderSignature = "head" VHDXRegionSignature = "regi" VHDXMetadataSignature = "metadata" // Unallocated block markers VHDUnallocatedBlockMarker uint32 = 0xFFFFFFFF VHDXUnallocatedBlockMarker uint64 = 0xFFFFFFFFFFFFFFFF )
const ( VHDDynamicHeaderSize = 1024 VHDXHeaderSize = 4096 VHDXFileInfoSize = 520 VHDXRegionTableSize = 64 * 1024 // 64 KB VHDXMetadataSize = 64 * 1024 // 64 KB 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 )
const ( VHDFormatVersion = 0x00010000 // VHD v1.0 VHDXFormatVersion = 0x0001 // VHDX v1.0 )
const ( // FormatVersion1 is the only File Format Version the specification // defines: 1.0. It equals VHDFormatVersion. FormatVersion1 uint32 = 0x00010000 // FeatureTemporary marks a disk the specification calls a candidate for // deletion on shutdown. FeatureReserved must always be set. FeatureTemporary uint32 = 0x00000001 FeatureReserved uint32 = 0x00000002 // FixedDataOffset is the Data Offset a fixed disk records: it has no // further structures for the field to point at. FixedDataOffset uint64 = 0xFFFFFFFFFFFFFFFF // CreatorOSWindows is the Creator Host OS code "Wi2k". CreatorOSWindows uint32 = 0x5769326B )
Values for FileFooter fields, typed to match them so a producer can assign them directly. libvhdi never writes an image; these exist for the pure codecs in this package and for tools and tests that build footers of their own.
const (
MaxCacheEntriesBlockDescriptors = 8
)
const NoOffset int64 = -1
NoOffset marks a StructuralError that is not about a particular byte.
Variables ¶
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") )
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)
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)
Epoch is the VHD format's epoch: 1 January 2000, 00:00:00 UTC.
VHD does not use the Unix epoch. Every timestamp in a VHD footer or dynamic disk header counts seconds from 2000, so decoding one as a Unix timestamp lands it thirty years early -- a disk created in 2024 reports as 1994. That is not a cosmetic error in a library whose output is evidence: an image's creation time and a parent's recorded modification time are exactly the sort of fact a report exists to state.
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 CHSGeometry ¶ added in v0.4.0
CHSGeometry computes the cylinder/head/sectors-per-track geometry the VHD specification assigns to a disk of totalSectors 512-byte sectors. It is a direct port of the algorithm in the specification's appendix, "CHS Calculation".
The encoding tops out at 65535 × 16 × 255 sectors, about 127 GB. Every larger disk gets that maximum geometry, which cannot describe it; the virtual size, not the geometry, is the authoritative size of a VHD.
func Checksum ¶ added in v0.4.0
Checksum 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; FooterChecksum does that for a footer.
This is deliberately not a CRC. CRC-32C is a VHDX construct, and applying it to VHD structures rejects every conformant VHD image.
func Corruptf ¶ added in v0.3.0
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 FooterChecksum ¶ added in v0.4.0
func FooterChecksum(f *FileFooter) uint32
FooterChecksum returns the checksum f should carry: Checksum over the marshalled footer with its Checksum field taken as zero. The value f.Checksum currently holds does not affect the result.
func PackGeometry ¶ added in v0.4.0
PackGeometry packs a CHS geometry into the footer's Disk Geometry field: cylinders in the high 16 bits, then heads and sectors per track in one byte each.
func Time ¶ added in v0.4.0
Time converts a raw VHD timestamp into a UTC time.
A raw zero is returned as the zero Time rather than as midnight on 1 January 2000. The distinction matters: a zero field means the producer recorded no timestamp -- which is what a non-differencing disk's parent modification time always is -- and rendering that as a real instant would invent a fact. Callers test with IsZero.
func Timestamp ¶ added in v0.4.0
Timestamp converts a time into the format's own encoding: whole seconds since Epoch. It is the inverse of Time for every instant the encoding can hold, which is 2000-01-01T00:00:01Z to 2136-02-07T06:28:15Z. Sub-second precision is truncated.
The encoding is unsigned and 32 bits wide, so out-of-range times are clamped rather than wrapped: the zero Time and anything at or before Epoch encode as 0, which Time reads back as "no timestamp recorded", and anything after 2136 encodes as 0xFFFFFFFF. Wrapping would turn a time just before the epoch into one in 2136, and one just after 2136 into one in 2000.
func UnpackGeometry ¶ added in v0.4.0
UnpackGeometry splits the footer's Disk Geometry field into cylinders, heads and sectors per track. It is the inverse of PackGeometry.
func Unsupportedf ¶ added in v0.3.0
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 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 (one's complement of the byte sum, this field taken as zero)
ParentIdentifier [16]byte // GUID
ParentModificationTime uint32 // Big-endian (seconds since 2000-01-01T00:00:00Z; see Time)
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 {
}
FileFooter represents a VHD file footer (512 bytes).
func (*FileFooter) MarshalBinary ¶ added in v0.4.0
func (f *FileFooter) MarshalBinary() ([]byte, error)
MarshalBinary encodes the footer as the 512-byte big-endian structure the specification defines, with every field at its specified offset.
It writes the fields exactly as given. It neither validates them nor computes the checksum: set f.Checksum = FooterChecksum(f) first. The error is always nil; the method exists to satisfy encoding.BinaryMarshaler.
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 ¶
FileInformation represents VHDX file information (520 bytes).
type ImageHeader ¶
type ImageHeader struct {
Signature [4]byte // "head"
Checksum uint32 // Little-endian (CRC-32C)
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 {
// two flags: FeatureTemporary (0x00000001) marks a temporary disk, and
// FeatureReserved (0x00000002) must always be set.
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.
// current size. The two differ on an image that has been expanded, and the
// difference is the only record that the expansion happened.
OriginalSize 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.
// 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 ¶
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-32C)
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.