public bool Identify(IFilter imageFilter) { Stream stream = imageFilter.GetDataForkStream(); stream.Seek(0, SeekOrigin.Begin); if (stream.Length < 512) { return(false); } byte[] qHdrB = new byte[Marshal.SizeOf(qHdr)]; stream.Read(qHdrB, 0, Marshal.SizeOf(qHdr)); qHdr = BigEndianMarshal.ByteArrayToStructureBigEndian <QCow2Header>(qHdrB); DicConsole.DebugWriteLine("QCOW plugin", "qHdr.magic = 0x{0:X8}", qHdr.magic); DicConsole.DebugWriteLine("QCOW plugin", "qHdr.version = {0}", qHdr.version); return(qHdr.magic == QCOW_MAGIC && (qHdr.version == QCOW_VERSION2 || qHdr.version == QCOW_VERSION3)); }
public bool Create(string path, MediaType mediaType, Dictionary <string, string> options, ulong sectors, uint sectorSize) { if (sectorSize != 512) { ErrorMessage = "Unsupported sector size"; return(false); } if (!SupportedMediaTypes.Contains(mediaType)) { ErrorMessage = $"Unsupported media format {mediaType}"; return(false); } // TODO: Correct this calculation if ((sectors * sectorSize) / 65536 > uint.MaxValue) { ErrorMessage = "Too many sectors for selected cluster size"; return(false); } _imageInfo = new ImageInfo { MediaType = mediaType, SectorSize = sectorSize, Sectors = sectors }; try { _writingStream = new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None); } catch (IOException e) { ErrorMessage = $"Could not create new image file, exception {e.Message}"; return(false); } string extension = Path.GetExtension(path); bool version3 = extension == ".qcow3" || extension == ".qc3"; _qHdr = new QCow2Header { magic = QCOW_MAGIC, version = version3 ? QCOW_VERSION3 : QCOW_VERSION2, size = sectors * sectorSize, cluster_bits = 16, header_length = (uint)Marshal.SizeOf <QCow2Header>() }; _clusterSize = 1 << (int)_qHdr.cluster_bits; _clusterSectors = 1 << ((int)_qHdr.cluster_bits - 9); _l2Bits = (int)(_qHdr.cluster_bits - 3); _l2Size = 1 << _l2Bits; _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; } _qHdr.l1_size = (uint)(_qHdr.size >> _l1Shift); if (_qHdr.l1_size == 0) { _qHdr.l1_size = 1; } _l1Table = new ulong[_qHdr.l1_size]; ulong clusters = _qHdr.size / (ulong)_clusterSize; ulong refCountBlocks = (clusters * 2) / (ulong)_clusterSize; if (refCountBlocks == 0) { refCountBlocks = 1; } _qHdr.refcount_table_offset = (ulong)_clusterSize; _qHdr.refcount_table_clusters = (uint)((refCountBlocks * 8) / (ulong)_clusterSize); if (_qHdr.refcount_table_clusters == 0) { _qHdr.refcount_table_clusters = 1; } _refCountTable = new ulong[refCountBlocks]; _qHdr.l1_table_offset = _qHdr.refcount_table_offset + (ulong)(_qHdr.refcount_table_clusters * _clusterSize); ulong l1TableClusters = (_qHdr.l1_size * 8) / (ulong)_clusterSize; if (l1TableClusters == 0) { l1TableClusters = 1; } byte[] empty = new byte[_qHdr.l1_table_offset + (l1TableClusters * (ulong)_clusterSize)]; _writingStream.Write(empty, 0, empty.Length); IsWriting = true; ErrorMessage = null; 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[Marshal.SizeOf(qHdr)]; stream.Read(qHdrB, 0, Marshal.SizeOf(qHdr)); qHdr = BigEndianMarshal.ByteArrayToStructureBigEndian <QCow2Header>(qHdrB); DicConsole.DebugWriteLine("QCOW plugin", "qHdr.magic = 0x{0:X8}", qHdr.magic); DicConsole.DebugWriteLine("QCOW plugin", "qHdr.version = {0}", qHdr.version); DicConsole.DebugWriteLine("QCOW plugin", "qHdr.backing_file_offset = {0}", qHdr.backing_file_offset); DicConsole.DebugWriteLine("QCOW plugin", "qHdr.backing_file_size = {0}", qHdr.backing_file_size); DicConsole.DebugWriteLine("QCOW plugin", "qHdr.cluster_bits = {0}", qHdr.cluster_bits); DicConsole.DebugWriteLine("QCOW plugin", "qHdr.size = {0}", qHdr.size); DicConsole.DebugWriteLine("QCOW plugin", "qHdr.crypt_method = {0}", qHdr.crypt_method); DicConsole.DebugWriteLine("QCOW plugin", "qHdr.l1_size = {0}", qHdr.l1_size); DicConsole.DebugWriteLine("QCOW plugin", "qHdr.l1_table_offset = {0}", qHdr.l1_table_offset); DicConsole.DebugWriteLine("QCOW plugin", "qHdr.refcount_table_offset = {0}", qHdr.refcount_table_offset); DicConsole.DebugWriteLine("QCOW plugin", "qHdr.refcount_table_clusters = {0}", qHdr.refcount_table_clusters); DicConsole.DebugWriteLine("QCOW plugin", "qHdr.nb_snapshots = {0}", qHdr.nb_snapshots); DicConsole.DebugWriteLine("QCOW plugin", "qHdr.snapshots_offset = {0}", qHdr.snapshots_offset); if (qHdr.version >= QCOW_VERSION3) { DicConsole.DebugWriteLine("QCOW plugin", "qHdr.features = {0:X}", qHdr.features); DicConsole.DebugWriteLine("QCOW plugin", "qHdr.compat_features = {0:X}", qHdr.compat_features); DicConsole.DebugWriteLine("QCOW plugin", "qHdr.autoclear_features = {0:X}", qHdr.autoclear_features); DicConsole.DebugWriteLine("QCOW plugin", "qHdr.refcount_order = {0}", qHdr.refcount_order); DicConsole.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; DicConsole.DebugWriteLine("QCOW plugin", "qHdr.clusterSize = {0}", clusterSize); DicConsole.DebugWriteLine("QCOW plugin", "qHdr.clusterSectors = {0}", clusterSectors); DicConsole.DebugWriteLine("QCOW plugin", "qHdr.qHdr.l1_size = {0}", qHdr.l1_size); DicConsole.DebugWriteLine("QCOW plugin", "qHdr.l2Size = {0}", l2Size); DicConsole.DebugWriteLine("QCOW plugin", "qHdr.sectors = {0}", imageInfo.Sectors); BigEndianBitConverter.IsLittleEndian = BitConverter.IsLittleEndian; 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 = new ulong[qHdr.l1_size]; // TODO: Optimize this DicConsole.DebugWriteLine("QCOW plugin", "Reading L1 table"); for (long i = 0; i < l1Table.LongLength; i++) { l1Table[i] = BigEndianBitConverter.ToUInt64(l1TableB, (int)(i * 8)); } 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; } DicConsole.DebugWriteLine("QCOW plugin", "qHdr.l1Mask = {0:X}", l1Mask); DicConsole.DebugWriteLine("QCOW plugin", "qHdr.l1Shift = {0}", l1Shift); DicConsole.DebugWriteLine("QCOW plugin", "qHdr.l2Mask = {0:X}", l2Mask); DicConsole.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); }