Beispiel #1
0
        private void ReadFooter(bool fallbackToFront)
        {
            _fileStream.Position = _fileStream.Length - Sizes.Sector;
            byte[] sector = StreamUtilities.ReadFully(_fileStream, Sizes.Sector);

            _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;
                StreamUtilities.ReadFully(_fileStream, sector, 0, Sizes.Sector);

                _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");
                }
            }
        }
Beispiel #2
0
        private void CheckHeader()
        {
            _fileStream.Position = 0;
            byte[] headerSector = StreamUtilities.ReadExact(_fileStream, Sizes.Sector);

            Footer header = Footer.FromBytes(headerSector, 0);

            if (!header.IsValid())
            {
                ReportError("Invalid VHD footer at start of file");
            }

            _fileStream.Position = _fileStream.Length - Sizes.Sector;
            byte[] footerSector = StreamUtilities.ReadExact(_fileStream, Sizes.Sector);

            if (!Utilities.AreEqual(footerSector, headerSector))
            {
                ReportError("Header and footer are different");
            }

            if (_footer == null || !_footer.IsValid())
            {
                _footer = header;
            }
        }
Beispiel #3
0
        private void CheckFooter()
        {
            _fileStream.Position = _fileStream.Length - Sizes.Sector;
            byte[] sector = StreamUtilities.ReadExact(_fileStream, Sizes.Sector);

            _footer = Footer.FromBytes(sector, 0);
            if (!_footer.IsValid())
            {
                ReportError("Invalid VHD footer at end of file");
            }
        }
Beispiel #4
0
        private void CheckFooter()
        {
            long length = _fileStream.Length;


            _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");
            }
        }
Beispiel #5
0
        public DynamicStream(Stream fileStream, DynamicHeader dynamicHeader, long length, SparseStream parentStream,
                             Ownership ownsParentStream)
        {
            if (fileStream == null)
            {
                throw new ArgumentNullException(nameof(fileStream));
            }

            if (dynamicHeader == null)
            {
                throw new ArgumentNullException(nameof(dynamicHeader));
            }

            if (parentStream == null)
            {
                throw new ArgumentNullException(nameof(parentStream));
            }

            if (length < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(length), length, "Negative lengths not allowed");
            }

            _fileStream       = fileStream;
            _dynamicHeader    = dynamicHeader;
            _length           = length;
            _parentStream     = parentStream;
            _ownsParentStream = ownsParentStream;

            _blockBitmaps    = new byte[_dynamicHeader.MaxTableEntries][];
            _blockBitmapSize =
                (int)
                MathUtilities.RoundUp(MathUtilities.Ceil(_dynamicHeader.BlockSize, Sizes.Sector * 8), Sizes.Sector);

            ReadBlockAllocationTable();

            // Detect where next block should go (cope if the footer is missing)
            _fileStream.Position = MathUtilities.RoundDown(_fileStream.Length, Sizes.Sector) - Sizes.Sector;
            byte[] footerBytes = StreamUtilities.ReadExact(_fileStream, Sizes.Sector);
            Footer footer      = Footer.FromBytes(footerBytes, 0);

            _nextBlockStart = _fileStream.Position - (footer.IsValid() ? Sizes.Sector : 0);
        }
        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);
        }
Beispiel #7
0
        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");
            }
        }
Beispiel #8
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");
                }
            }
        }