public bool Open(IFilter imageFilter) { Stream stream = imageFilter.GetDataForkStream(); stream.Seek(0, SeekOrigin.Begin); if (stream.Length < 512) { return(false); } byte[] qHdrB = new byte[Marshal.SizeOf <QCow2Header>()]; stream.Read(qHdrB, 0, Marshal.SizeOf <QCow2Header>()); _qHdr = Marshal.SpanToStructureBigEndian <QCow2Header>(qHdrB); AaruConsole.DebugWriteLine("QCOW plugin", "qHdr.magic = 0x{0:X8}", _qHdr.magic); AaruConsole.DebugWriteLine("QCOW plugin", "qHdr.version = {0}", _qHdr.version); AaruConsole.DebugWriteLine("QCOW plugin", "qHdr.backing_file_offset = {0}", _qHdr.backing_file_offset); AaruConsole.DebugWriteLine("QCOW plugin", "qHdr.backing_file_size = {0}", _qHdr.backing_file_size); AaruConsole.DebugWriteLine("QCOW plugin", "qHdr.cluster_bits = {0}", _qHdr.cluster_bits); AaruConsole.DebugWriteLine("QCOW plugin", "qHdr.size = {0}", _qHdr.size); AaruConsole.DebugWriteLine("QCOW plugin", "qHdr.crypt_method = {0}", _qHdr.crypt_method); AaruConsole.DebugWriteLine("QCOW plugin", "qHdr.l1_size = {0}", _qHdr.l1_size); AaruConsole.DebugWriteLine("QCOW plugin", "qHdr.l1_table_offset = {0}", _qHdr.l1_table_offset); AaruConsole.DebugWriteLine("QCOW plugin", "qHdr.refcount_table_offset = {0}", _qHdr.refcount_table_offset); AaruConsole.DebugWriteLine("QCOW plugin", "qHdr.refcount_table_clusters = {0}", _qHdr.refcount_table_clusters); AaruConsole.DebugWriteLine("QCOW plugin", "qHdr.nb_snapshots = {0}", _qHdr.nb_snapshots); AaruConsole.DebugWriteLine("QCOW plugin", "qHdr.snapshots_offset = {0}", _qHdr.snapshots_offset); if (_qHdr.version >= QCOW_VERSION3) { AaruConsole.DebugWriteLine("QCOW plugin", "qHdr.features = {0:X}", _qHdr.features); AaruConsole.DebugWriteLine("QCOW plugin", "qHdr.compat_features = {0:X}", _qHdr.compat_features); AaruConsole.DebugWriteLine("QCOW plugin", "qHdr.autoclear_features = {0:X}", _qHdr.autoclear_features); AaruConsole.DebugWriteLine("QCOW plugin", "qHdr.refcount_order = {0}", _qHdr.refcount_order); AaruConsole.DebugWriteLine("QCOW plugin", "qHdr.header_length = {0}", _qHdr.header_length); if ((_qHdr.features & QCOW_FEATURE_MASK) != 0) { throw new ImageNotSupportedException($"Unknown incompatible features {_qHdr.features & QCOW_FEATURE_MASK:X} enabled, not proceeding."); } } if (_qHdr.size <= 1) { throw new ArgumentOutOfRangeException(nameof(_qHdr.size), "Image size is too small"); } if (_qHdr.cluster_bits < 9 || _qHdr.cluster_bits > 16) { throw new ArgumentOutOfRangeException(nameof(_qHdr.cluster_bits), "Cluster size must be between 512 bytes and 64 Kbytes"); } if (_qHdr.crypt_method > QCOW_ENCRYPTION_AES) { throw new ArgumentOutOfRangeException(nameof(_qHdr.crypt_method), "Invalid encryption method"); } if (_qHdr.crypt_method > QCOW_ENCRYPTION_NONE) { throw new NotImplementedException("AES encrypted images not yet supported"); } if (_qHdr.backing_file_offset != 0) { throw new NotImplementedException("Differencing images not yet supported"); } _clusterSize = 1 << (int)_qHdr.cluster_bits; _clusterSectors = 1 << ((int)_qHdr.cluster_bits - 9); _l2Bits = (int)(_qHdr.cluster_bits - 3); _l2Size = 1 << _l2Bits; AaruConsole.DebugWriteLine("QCOW plugin", "qHdr.clusterSize = {0}", _clusterSize); AaruConsole.DebugWriteLine("QCOW plugin", "qHdr.clusterSectors = {0}", _clusterSectors); AaruConsole.DebugWriteLine("QCOW plugin", "qHdr.qHdr.l1_size = {0}", _qHdr.l1_size); AaruConsole.DebugWriteLine("QCOW plugin", "qHdr.l2Size = {0}", _l2Size); AaruConsole.DebugWriteLine("QCOW plugin", "qHdr.sectors = {0}", _imageInfo.Sectors); byte[] l1TableB = new byte[_qHdr.l1_size * 8]; stream.Seek((long)_qHdr.l1_table_offset, SeekOrigin.Begin); stream.Read(l1TableB, 0, (int)_qHdr.l1_size * 8); _l1Table = MemoryMarshal.Cast <byte, ulong>(l1TableB).ToArray(); AaruConsole.DebugWriteLine("QCOW plugin", "Reading L1 table"); for (long i = 0; i < _l1Table.LongLength; i++) { _l1Table[i] = Swapping.Swap(_l1Table[i]); } _l1Mask = 0; int c = 0; _l1Shift = (int)(_l2Bits + _qHdr.cluster_bits); for (int i = 0; i < 64; i++) { _l1Mask <<= 1; if (c >= 64 - _l1Shift) { continue; } _l1Mask += 1; c++; } _l2Mask = 0; for (int i = 0; i < _l2Bits; i++) { _l2Mask = (_l2Mask << 1) + 1; } _l2Mask <<= (int)_qHdr.cluster_bits; _sectorMask = 0; for (int i = 0; i < _qHdr.cluster_bits; i++) { _sectorMask = (_sectorMask << 1) + 1; } AaruConsole.DebugWriteLine("QCOW plugin", "qHdr.l1Mask = {0:X}", _l1Mask); AaruConsole.DebugWriteLine("QCOW plugin", "qHdr.l1Shift = {0}", _l1Shift); AaruConsole.DebugWriteLine("QCOW plugin", "qHdr.l2Mask = {0:X}", _l2Mask); AaruConsole.DebugWriteLine("QCOW plugin", "qHdr.sectorMask = {0:X}", _sectorMask); _maxL2TableCache = MAX_CACHE_SIZE / (_l2Size * 8); _maxClusterCache = MAX_CACHE_SIZE / _clusterSize; _imageStream = stream; _sectorCache = new Dictionary <ulong, byte[]>(); _l2TableCache = new Dictionary <ulong, ulong[]>(); _clusterCache = new Dictionary <ulong, byte[]>(); _imageInfo.CreationTime = imageFilter.GetCreationTime(); _imageInfo.LastModificationTime = imageFilter.GetLastWriteTime(); _imageInfo.MediaTitle = Path.GetFileNameWithoutExtension(imageFilter.GetFilename()); _imageInfo.Sectors = _qHdr.size / 512; _imageInfo.SectorSize = 512; _imageInfo.XmlMediaType = XmlMediaType.BlockMedia; _imageInfo.MediaType = MediaType.GENERIC_HDD; _imageInfo.ImageSize = _qHdr.size; _imageInfo.Version = $"{_qHdr.version}"; _imageInfo.Cylinders = (uint)(_imageInfo.Sectors / 16 / 63); _imageInfo.Heads = 16; _imageInfo.SectorsPerTrack = 63; return(true); }
public bool Open(IFilter imageFilter) { Stream stream = imageFilter.GetDataForkStream(); stream.Seek(0, SeekOrigin.Begin); if (stream.Length < 512) { return(false); } byte[] qHdrB = new byte[48]; stream.Read(qHdrB, 0, 48); _qHdr = Marshal.SpanToStructureBigEndian <QCowHeader>(qHdrB); AaruConsole.DebugWriteLine("QCOW plugin", "qHdr.magic = 0x{0:X8}", _qHdr.magic); AaruConsole.DebugWriteLine("QCOW plugin", "qHdr.version = {0}", _qHdr.version); AaruConsole.DebugWriteLine("QCOW plugin", "qHdr.backing_file_offset = {0}", _qHdr.backing_file_offset); AaruConsole.DebugWriteLine("QCOW plugin", "qHdr.backing_file_size = {0}", _qHdr.backing_file_size); AaruConsole.DebugWriteLine("QCOW plugin", "qHdr.mtime = {0}", _qHdr.mtime); AaruConsole.DebugWriteLine("QCOW plugin", "qHdr.size = {0}", _qHdr.size); AaruConsole.DebugWriteLine("QCOW plugin", "qHdr.cluster_bits = {0}", _qHdr.cluster_bits); AaruConsole.DebugWriteLine("QCOW plugin", "qHdr.l2_bits = {0}", _qHdr.l2_bits); AaruConsole.DebugWriteLine("QCOW plugin", "qHdr.padding = {0}", _qHdr.padding); AaruConsole.DebugWriteLine("QCOW plugin", "qHdr.crypt_method = {0}", _qHdr.crypt_method); AaruConsole.DebugWriteLine("QCOW plugin", "qHdr.l1_table_offset = {0}", _qHdr.l1_table_offset); if (_qHdr.size <= 1) { throw new ArgumentOutOfRangeException(nameof(_qHdr.size), "Image size is too small"); } if (_qHdr.cluster_bits < 9 || _qHdr.cluster_bits > 16) { throw new ArgumentOutOfRangeException(nameof(_qHdr.cluster_bits), "Cluster size must be between 512 bytes and 64 Kbytes"); } if (_qHdr.l2_bits < 9 - 3 || _qHdr.l2_bits > 16 - 3) { throw new ArgumentOutOfRangeException(nameof(_qHdr.l2_bits), "L2 size must be between 512 bytes and 64 Kbytes"); } if (_qHdr.crypt_method > QCOW_ENCRYPTION_AES) { throw new ArgumentOutOfRangeException(nameof(_qHdr.crypt_method), "Invalid encryption method"); } if (_qHdr.crypt_method > QCOW_ENCRYPTION_NONE) { throw new NotImplementedException("AES encrypted images not yet supported"); } if (_qHdr.backing_file_offset != 0) { throw new NotImplementedException("Differencing images not yet supported"); } int shift = _qHdr.cluster_bits + _qHdr.l2_bits; if (_qHdr.size > ulong.MaxValue - (ulong)(1 << shift)) { throw new ArgumentOutOfRangeException(nameof(_qHdr.size), "Image is too large"); } _clusterSize = 1 << _qHdr.cluster_bits; _clusterSectors = 1 << (_qHdr.cluster_bits - 9); _l1Size = (uint)(((_qHdr.size + (ulong)(1 << shift)) - 1) >> shift); _l2Size = 1 << _qHdr.l2_bits; AaruConsole.DebugWriteLine("QCOW plugin", "qHdr.clusterSize = {0}", _clusterSize); AaruConsole.DebugWriteLine("QCOW plugin", "qHdr.clusterSectors = {0}", _clusterSectors); AaruConsole.DebugWriteLine("QCOW plugin", "qHdr.l1Size = {0}", _l1Size); AaruConsole.DebugWriteLine("QCOW plugin", "qHdr.l2Size = {0}", _l2Size); AaruConsole.DebugWriteLine("QCOW plugin", "qHdr.sectors = {0}", _imageInfo.Sectors); byte[] l1TableB = new byte[_l1Size * 8]; stream.Seek((long)_qHdr.l1_table_offset, SeekOrigin.Begin); stream.Read(l1TableB, 0, (int)_l1Size * 8); _l1Table = MemoryMarshal.Cast <byte, ulong>(l1TableB).ToArray(); AaruConsole.DebugWriteLine("QCOW plugin", "Reading L1 table"); for (long i = 0; i < _l1Table.LongLength; i++) { _l1Table[i] = Swapping.Swap(_l1Table[i]); } _l1Mask = 0; int c = 0; _l1Shift = _qHdr.l2_bits + _qHdr.cluster_bits; for (int i = 0; i < 64; i++) { _l1Mask <<= 1; if (c >= 64 - _l1Shift) { continue; } _l1Mask += 1; c++; } _l2Mask = 0; for (int i = 0; i < _qHdr.l2_bits; i++) { _l2Mask = (_l2Mask << 1) + 1; } _l2Mask <<= _qHdr.cluster_bits; _sectorMask = 0; for (int i = 0; i < _qHdr.cluster_bits; i++) { _sectorMask = (_sectorMask << 1) + 1; } AaruConsole.DebugWriteLine("QCOW plugin", "qHdr.l1Mask = {0:X}", _l1Mask); AaruConsole.DebugWriteLine("QCOW plugin", "qHdr.l1Shift = {0}", _l1Shift); AaruConsole.DebugWriteLine("QCOW plugin", "qHdr.l2Mask = {0:X}", _l2Mask); AaruConsole.DebugWriteLine("QCOW plugin", "qHdr.sectorMask = {0:X}", _sectorMask); _maxL2TableCache = MAX_CACHE_SIZE / (_l2Size * 8); _maxClusterCache = MAX_CACHE_SIZE / _clusterSize; _imageStream = stream; _sectorCache = new Dictionary <ulong, byte[]>(); _l2TableCache = new Dictionary <ulong, ulong[]>(); _clusterCache = new Dictionary <ulong, byte[]>(); _imageInfo.CreationTime = imageFilter.GetCreationTime(); _imageInfo.LastModificationTime = _qHdr.mtime > 0 ? DateHandlers.UnixUnsignedToDateTime(_qHdr.mtime) : imageFilter.GetLastWriteTime(); _imageInfo.MediaTitle = Path.GetFileNameWithoutExtension(imageFilter.GetFilename()); _imageInfo.Sectors = _qHdr.size / 512; _imageInfo.SectorSize = 512; _imageInfo.XmlMediaType = XmlMediaType.BlockMedia; _imageInfo.MediaType = MediaType.GENERIC_HDD; _imageInfo.ImageSize = _qHdr.size; _imageInfo.Cylinders = (uint)(_imageInfo.Sectors / 16 / 63); _imageInfo.Heads = 16; _imageInfo.SectorsPerTrack = 63; return(true); }