private void CheckHeader() { _fileStream.Position = 0; byte[] headerSector = Utilities.ReadFully(_fileStream, Utilities.SectorSize); Footer header = Footer.FromBytes(headerSector, 0); if (!header.IsValid()) { ReportError("Invalid VHD footer at start of file"); } _fileStream.Position = _fileStream.Length - Utilities.SectorSize; byte[] footerSector = Utilities.ReadFully(_fileStream, Utilities.SectorSize); if (!Utilities.AreEqual(footerSector, headerSector)) { ReportError("Header and footer are different"); } if (_footer == null || !_footer.IsValid()) { _footer = header; } }
private void CheckFooter() { _fileStream.Position = _fileStream.Length - Utilities.SectorSize; byte[] sector = Utilities.ReadFully(_fileStream, Utilities.SectorSize); _footer = Footer.FromBytes(sector, 0); if (!_footer.IsValid()) { ReportError("Invalid VHD footer at end of file"); } }
public DynamicStream(Stream fileStream, DynamicHeader dynamicHeader, long length, SparseStream parentStream, Ownership ownsParentStream) { if (fileStream == null) { throw new ArgumentNullException("fileStream"); } if (dynamicHeader == null) { throw new ArgumentNullException("dynamicHeader"); } if (parentStream == null) { throw new ArgumentNullException("parentStream"); } if (length < 0) { throw new ArgumentOutOfRangeException("length", length, "Negative lengths not allowed"); } _fileStream = fileStream; _dynamicHeader = dynamicHeader; _length = length; _parentStream = parentStream; _ownsParentStream = ownsParentStream; _blockBitmaps = new byte[_dynamicHeader.MaxTableEntries][]; _blockBitmapSize = (int)Utilities.RoundUp((_dynamicHeader.BlockSize / Utilities.SectorSize) / 8, Utilities.SectorSize); ReadBlockAllocationTable(); // Detect where next block should go (cope if the footer is missing) _fileStream.Position = Utilities.RoundDown(_fileStream.Length, Utilities.SectorSize) - Utilities.SectorSize; byte[] footerBytes = Utilities.ReadFully(_fileStream, Utilities.SectorSize); Footer footer = Footer.FromBytes(footerBytes, 0); _nextBlockStart = _fileStream.Position - (footer.IsValid() ? Utilities.SectorSize : 0); }
private void ReadFooter(bool fallbackToFront) { _fileStream.Position = _fileStream.Length - Utilities.SectorSize; byte[] sector = Utilities.ReadFully(_fileStream, Utilities.SectorSize); _footer = Footer.FromBytes(sector, 0); if (!_footer.IsValid()) { if (!fallbackToFront) { throw new IOException("Corrupt VHD file - invalid footer at end (did not check front of file)"); } _fileStream.Position = 0; Utilities.ReadFully(_fileStream, sector, 0, Utilities.SectorSize); _footer = Footer.FromBytes(sector, 0); if (!_footer.IsValid()) { throw new IOException("Failed to find a valid VHD footer at start or end of file - VHD file is corrupt"); } } }