mirror of
https://github.com/aaru-dps/Aaru.git
synced 2025-12-16 19:24:25 +00:00
Use new little endian marshaller on media images.
This commit is contained in:
@@ -41,6 +41,170 @@ namespace DiscImageChef.DiscImages
|
||||
{
|
||||
public partial class DiscImageChef
|
||||
{
|
||||
public bool? VerifyMediaImage()
|
||||
{
|
||||
// This will traverse all blocks and check their CRC64 without uncompressing them
|
||||
DicConsole.DebugWriteLine("DiscImageChef format plugin", "Checking index integrity at {0}",
|
||||
header.indexOffset);
|
||||
imageStream.Position = (long)header.indexOffset;
|
||||
|
||||
IndexHeader idxHeader = new IndexHeader();
|
||||
structureBytes = new byte[Marshal.SizeOf(idxHeader)];
|
||||
imageStream.Read(structureBytes, 0, structureBytes.Length);
|
||||
idxHeader = Helpers.Marshal.ByteArrayToStructureLittleEndian<IndexHeader>(structureBytes);
|
||||
|
||||
if(idxHeader.identifier != BlockType.Index)
|
||||
{
|
||||
DicConsole.DebugWriteLine("DiscImageChef format plugin", "Incorrect index identifier");
|
||||
return false;
|
||||
}
|
||||
|
||||
DicConsole.DebugWriteLine("DiscImageChef format plugin", "Index at {0} contains {1} entries",
|
||||
header.indexOffset, idxHeader.entries);
|
||||
|
||||
structureBytes = new byte[Marshal.SizeOf(typeof(IndexEntry)) * idxHeader.entries];
|
||||
imageStream.Read(structureBytes, 0, structureBytes.Length);
|
||||
Crc64Context.Data(structureBytes, out byte[] verifyCrc);
|
||||
|
||||
if(BitConverter.ToUInt64(verifyCrc, 0) != idxHeader.crc64)
|
||||
{
|
||||
DicConsole.DebugWriteLine("DiscImageChef format plugin", "Expected index CRC {0:X16} but got {1:X16}",
|
||||
idxHeader.crc64, BitConverter.ToUInt64(verifyCrc, 0));
|
||||
return false;
|
||||
}
|
||||
|
||||
imageStream.Position -= structureBytes.Length;
|
||||
|
||||
List<IndexEntry> vrIndex = new List<IndexEntry>();
|
||||
for(ushort i = 0; i < idxHeader.entries; i++)
|
||||
{
|
||||
IndexEntry entry = new IndexEntry();
|
||||
structureBytes = new byte[Marshal.SizeOf(entry)];
|
||||
imageStream.Read(structureBytes, 0, structureBytes.Length);
|
||||
entry = Helpers.Marshal.ByteArrayToStructureLittleEndian<IndexEntry>(structureBytes);
|
||||
DicConsole.DebugWriteLine("DiscImageChef format plugin",
|
||||
"Block type {0} with data type {1} is indexed to be at {2}", entry.blockType,
|
||||
entry.dataType, entry.offset);
|
||||
vrIndex.Add(entry);
|
||||
}
|
||||
|
||||
// Read up to 1MiB at a time for verification
|
||||
const int VERIFY_SIZE = 1024 * 1024;
|
||||
|
||||
foreach(IndexEntry entry in vrIndex)
|
||||
{
|
||||
imageStream.Position = (long)entry.offset;
|
||||
Crc64Context crcVerify;
|
||||
ulong readBytes;
|
||||
byte[] verifyBytes;
|
||||
|
||||
switch(entry.blockType)
|
||||
{
|
||||
case BlockType.DataBlock:
|
||||
BlockHeader blockHeader = new BlockHeader();
|
||||
structureBytes = new byte[Marshal.SizeOf(blockHeader)];
|
||||
imageStream.Read(structureBytes, 0, structureBytes.Length);
|
||||
blockHeader = Helpers.Marshal.ByteArrayToStructureLittleEndian<BlockHeader>(structureBytes);
|
||||
|
||||
crcVerify = new Crc64Context();
|
||||
readBytes = 0;
|
||||
|
||||
DicConsole.DebugWriteLine("DiscImageChef format plugin",
|
||||
"Verifying data block type {0} at position {1}", entry.dataType,
|
||||
entry.offset);
|
||||
|
||||
while(readBytes + VERIFY_SIZE < blockHeader.cmpLength)
|
||||
{
|
||||
verifyBytes = new byte[VERIFY_SIZE];
|
||||
imageStream.Read(verifyBytes, 0, verifyBytes.Length);
|
||||
crcVerify.Update(verifyBytes);
|
||||
readBytes += (ulong)verifyBytes.LongLength;
|
||||
}
|
||||
|
||||
verifyBytes = new byte[blockHeader.cmpLength - readBytes];
|
||||
imageStream.Read(verifyBytes, 0, verifyBytes.Length);
|
||||
crcVerify.Update(verifyBytes);
|
||||
|
||||
verifyCrc = crcVerify.Final();
|
||||
|
||||
if(BitConverter.ToUInt64(verifyCrc, 0) != blockHeader.cmpCrc64)
|
||||
{
|
||||
DicConsole.DebugWriteLine("DiscImageChef format plugin",
|
||||
"Expected block CRC {0:X16} but got {1:X16}",
|
||||
blockHeader.cmpCrc64, BitConverter.ToUInt64(verifyCrc, 0));
|
||||
return false;
|
||||
}
|
||||
|
||||
break;
|
||||
case BlockType.DeDuplicationTable:
|
||||
DdtHeader ddtHeader = new DdtHeader();
|
||||
structureBytes = new byte[Marshal.SizeOf(ddtHeader)];
|
||||
imageStream.Read(structureBytes, 0, structureBytes.Length);
|
||||
ddtHeader = Helpers.Marshal.ByteArrayToStructureLittleEndian<DdtHeader>(structureBytes);
|
||||
|
||||
crcVerify = new Crc64Context();
|
||||
readBytes = 0;
|
||||
|
||||
DicConsole.DebugWriteLine("DiscImageChef format plugin",
|
||||
"Verifying deduplication table type {0} at position {1}",
|
||||
entry.dataType, entry.offset);
|
||||
|
||||
while(readBytes + VERIFY_SIZE < ddtHeader.cmpLength)
|
||||
{
|
||||
verifyBytes = new byte[readBytes];
|
||||
imageStream.Read(verifyBytes, 0, verifyBytes.Length);
|
||||
crcVerify.Update(verifyBytes);
|
||||
readBytes += (ulong)verifyBytes.LongLength;
|
||||
}
|
||||
|
||||
verifyBytes = new byte[ddtHeader.cmpLength - readBytes];
|
||||
imageStream.Read(verifyBytes, 0, verifyBytes.Length);
|
||||
crcVerify.Update(verifyBytes);
|
||||
|
||||
verifyCrc = crcVerify.Final();
|
||||
|
||||
if(BitConverter.ToUInt64(verifyCrc, 0) != ddtHeader.cmpCrc64)
|
||||
{
|
||||
DicConsole.DebugWriteLine("DiscImageChef format plugin",
|
||||
"Expected DDT CRC {0:X16} but got {1:X16}", ddtHeader.cmpCrc64,
|
||||
BitConverter.ToUInt64(verifyCrc, 0));
|
||||
return false;
|
||||
}
|
||||
|
||||
break;
|
||||
case BlockType.TracksBlock:
|
||||
TracksHeader trkHeader = new TracksHeader();
|
||||
structureBytes = new byte[Marshal.SizeOf(trkHeader)];
|
||||
imageStream.Read(structureBytes, 0, structureBytes.Length);
|
||||
trkHeader = Helpers.Marshal.ByteArrayToStructureLittleEndian<TracksHeader>(structureBytes);
|
||||
|
||||
DicConsole.DebugWriteLine("DiscImageChef format plugin",
|
||||
"Track block at {0} contains {1} entries", header.indexOffset,
|
||||
trkHeader.entries);
|
||||
|
||||
structureBytes = new byte[Marshal.SizeOf(typeof(TrackEntry)) * trkHeader.entries];
|
||||
imageStream.Read(structureBytes, 0, structureBytes.Length);
|
||||
Crc64Context.Data(structureBytes, out verifyCrc);
|
||||
|
||||
if(BitConverter.ToUInt64(verifyCrc, 0) != trkHeader.crc64)
|
||||
{
|
||||
DicConsole.DebugWriteLine("DiscImageChef format plugin",
|
||||
"Expected index CRC {0:X16} but got {1:X16}", trkHeader.crc64,
|
||||
BitConverter.ToUInt64(verifyCrc, 0));
|
||||
return false;
|
||||
}
|
||||
|
||||
break;
|
||||
default:
|
||||
DicConsole.DebugWriteLine("DiscImageChef format plugin", "Ignored field type {0}",
|
||||
entry.blockType);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool? VerifySector(ulong sectorAddress)
|
||||
{
|
||||
if(imageInfo.XmlMediaType != XmlMediaType.OpticalDisc) return null;
|
||||
@@ -49,14 +213,6 @@ namespace DiscImageChef.DiscImages
|
||||
return CdChecksums.CheckCdSector(buffer);
|
||||
}
|
||||
|
||||
public bool? VerifySector(ulong sectorAddress, uint track)
|
||||
{
|
||||
if(imageInfo.XmlMediaType != XmlMediaType.OpticalDisc) return null;
|
||||
|
||||
byte[] buffer = ReadSectorLong(sectorAddress, track);
|
||||
return CdChecksums.CheckCdSector(buffer);
|
||||
}
|
||||
|
||||
public bool? VerifySectors(ulong sectorAddress, uint length, out List<ulong> failingLbas,
|
||||
out List<ulong> unknownLbas)
|
||||
{
|
||||
@@ -139,183 +295,12 @@ namespace DiscImageChef.DiscImages
|
||||
return failingLbas.Count <= 0;
|
||||
}
|
||||
|
||||
public bool? VerifyMediaImage()
|
||||
public bool? VerifySector(ulong sectorAddress, uint track)
|
||||
{
|
||||
// This will traverse all blocks and check their CRC64 without uncompressing them
|
||||
DicConsole.DebugWriteLine("DiscImageChef format plugin", "Checking index integrity at {0}",
|
||||
header.indexOffset);
|
||||
imageStream.Position = (long)header.indexOffset;
|
||||
if(imageInfo.XmlMediaType != XmlMediaType.OpticalDisc) return null;
|
||||
|
||||
IndexHeader idxHeader = new IndexHeader();
|
||||
structureBytes = new byte[Marshal.SizeOf(idxHeader)];
|
||||
imageStream.Read(structureBytes, 0, structureBytes.Length);
|
||||
structurePointer = Marshal.AllocHGlobal(Marshal.SizeOf(idxHeader));
|
||||
Marshal.Copy(structureBytes, 0, structurePointer, Marshal.SizeOf(idxHeader));
|
||||
idxHeader = (IndexHeader)Marshal.PtrToStructure(structurePointer, typeof(IndexHeader));
|
||||
Marshal.FreeHGlobal(structurePointer);
|
||||
|
||||
if(idxHeader.identifier != BlockType.Index)
|
||||
{
|
||||
DicConsole.DebugWriteLine("DiscImageChef format plugin", "Incorrect index identifier");
|
||||
return false;
|
||||
}
|
||||
|
||||
DicConsole.DebugWriteLine("DiscImageChef format plugin", "Index at {0} contains {1} entries",
|
||||
header.indexOffset, idxHeader.entries);
|
||||
|
||||
structureBytes = new byte[Marshal.SizeOf(typeof(IndexEntry)) * idxHeader.entries];
|
||||
imageStream.Read(structureBytes, 0, structureBytes.Length);
|
||||
Crc64Context.Data(structureBytes, out byte[] verifyCrc);
|
||||
|
||||
if(BitConverter.ToUInt64(verifyCrc, 0) != idxHeader.crc64)
|
||||
{
|
||||
DicConsole.DebugWriteLine("DiscImageChef format plugin", "Expected index CRC {0:X16} but got {1:X16}",
|
||||
idxHeader.crc64, BitConverter.ToUInt64(verifyCrc, 0));
|
||||
return false;
|
||||
}
|
||||
|
||||
imageStream.Position -= structureBytes.Length;
|
||||
|
||||
List<IndexEntry> vrIndex = new List<IndexEntry>();
|
||||
for(ushort i = 0; i < idxHeader.entries; i++)
|
||||
{
|
||||
IndexEntry entry = new IndexEntry();
|
||||
structureBytes = new byte[Marshal.SizeOf(entry)];
|
||||
imageStream.Read(structureBytes, 0, structureBytes.Length);
|
||||
structurePointer = Marshal.AllocHGlobal(Marshal.SizeOf(entry));
|
||||
Marshal.Copy(structureBytes, 0, structurePointer, Marshal.SizeOf(entry));
|
||||
entry = (IndexEntry)Marshal.PtrToStructure(structurePointer, typeof(IndexEntry));
|
||||
Marshal.FreeHGlobal(structurePointer);
|
||||
DicConsole.DebugWriteLine("DiscImageChef format plugin",
|
||||
"Block type {0} with data type {1} is indexed to be at {2}", entry.blockType,
|
||||
entry.dataType, entry.offset);
|
||||
vrIndex.Add(entry);
|
||||
}
|
||||
|
||||
// Read up to 1MiB at a time for verification
|
||||
const int VERIFY_SIZE = 1024 * 1024;
|
||||
|
||||
foreach(IndexEntry entry in vrIndex)
|
||||
{
|
||||
imageStream.Position = (long)entry.offset;
|
||||
Crc64Context crcVerify;
|
||||
ulong readBytes;
|
||||
byte[] verifyBytes;
|
||||
|
||||
switch(entry.blockType)
|
||||
{
|
||||
case BlockType.DataBlock:
|
||||
BlockHeader blockHeader = new BlockHeader();
|
||||
structureBytes = new byte[Marshal.SizeOf(blockHeader)];
|
||||
imageStream.Read(structureBytes, 0, structureBytes.Length);
|
||||
structurePointer = Marshal.AllocHGlobal(Marshal.SizeOf(blockHeader));
|
||||
Marshal.Copy(structureBytes, 0, structurePointer, Marshal.SizeOf(blockHeader));
|
||||
blockHeader = (BlockHeader)Marshal.PtrToStructure(structurePointer, typeof(BlockHeader));
|
||||
Marshal.FreeHGlobal(structurePointer);
|
||||
|
||||
crcVerify = new Crc64Context();
|
||||
readBytes = 0;
|
||||
|
||||
DicConsole.DebugWriteLine("DiscImageChef format plugin",
|
||||
"Verifying data block type {0} at position {1}", entry.dataType,
|
||||
entry.offset);
|
||||
|
||||
while(readBytes + VERIFY_SIZE < blockHeader.cmpLength)
|
||||
{
|
||||
verifyBytes = new byte[VERIFY_SIZE];
|
||||
imageStream.Read(verifyBytes, 0, verifyBytes.Length);
|
||||
crcVerify.Update(verifyBytes);
|
||||
readBytes += (ulong)verifyBytes.LongLength;
|
||||
}
|
||||
|
||||
verifyBytes = new byte[blockHeader.cmpLength - readBytes];
|
||||
imageStream.Read(verifyBytes, 0, verifyBytes.Length);
|
||||
crcVerify.Update(verifyBytes);
|
||||
|
||||
verifyCrc = crcVerify.Final();
|
||||
|
||||
if(BitConverter.ToUInt64(verifyCrc, 0) != blockHeader.cmpCrc64)
|
||||
{
|
||||
DicConsole.DebugWriteLine("DiscImageChef format plugin",
|
||||
"Expected block CRC {0:X16} but got {1:X16}",
|
||||
blockHeader.cmpCrc64, BitConverter.ToUInt64(verifyCrc, 0));
|
||||
return false;
|
||||
}
|
||||
|
||||
break;
|
||||
case BlockType.DeDuplicationTable:
|
||||
DdtHeader ddtHeader = new DdtHeader();
|
||||
structureBytes = new byte[Marshal.SizeOf(ddtHeader)];
|
||||
imageStream.Read(structureBytes, 0, structureBytes.Length);
|
||||
structurePointer = Marshal.AllocHGlobal(Marshal.SizeOf(ddtHeader));
|
||||
Marshal.Copy(structureBytes, 0, structurePointer, Marshal.SizeOf(ddtHeader));
|
||||
ddtHeader = (DdtHeader)Marshal.PtrToStructure(structurePointer, typeof(DdtHeader));
|
||||
Marshal.FreeHGlobal(structurePointer);
|
||||
|
||||
crcVerify = new Crc64Context();
|
||||
readBytes = 0;
|
||||
|
||||
DicConsole.DebugWriteLine("DiscImageChef format plugin",
|
||||
"Verifying deduplication table type {0} at position {1}",
|
||||
entry.dataType, entry.offset);
|
||||
|
||||
while(readBytes + VERIFY_SIZE < ddtHeader.cmpLength)
|
||||
{
|
||||
verifyBytes = new byte[readBytes];
|
||||
imageStream.Read(verifyBytes, 0, verifyBytes.Length);
|
||||
crcVerify.Update(verifyBytes);
|
||||
readBytes += (ulong)verifyBytes.LongLength;
|
||||
}
|
||||
|
||||
verifyBytes = new byte[ddtHeader.cmpLength - readBytes];
|
||||
imageStream.Read(verifyBytes, 0, verifyBytes.Length);
|
||||
crcVerify.Update(verifyBytes);
|
||||
|
||||
verifyCrc = crcVerify.Final();
|
||||
|
||||
if(BitConverter.ToUInt64(verifyCrc, 0) != ddtHeader.cmpCrc64)
|
||||
{
|
||||
DicConsole.DebugWriteLine("DiscImageChef format plugin",
|
||||
"Expected DDT CRC {0:X16} but got {1:X16}", ddtHeader.cmpCrc64,
|
||||
BitConverter.ToUInt64(verifyCrc, 0));
|
||||
return false;
|
||||
}
|
||||
|
||||
break;
|
||||
case BlockType.TracksBlock:
|
||||
TracksHeader trkHeader = new TracksHeader();
|
||||
structureBytes = new byte[Marshal.SizeOf(trkHeader)];
|
||||
imageStream.Read(structureBytes, 0, structureBytes.Length);
|
||||
structurePointer = Marshal.AllocHGlobal(Marshal.SizeOf(trkHeader));
|
||||
Marshal.Copy(structureBytes, 0, structurePointer, Marshal.SizeOf(trkHeader));
|
||||
trkHeader = (TracksHeader)Marshal.PtrToStructure(structurePointer, typeof(TracksHeader));
|
||||
Marshal.FreeHGlobal(structurePointer);
|
||||
|
||||
DicConsole.DebugWriteLine("DiscImageChef format plugin",
|
||||
"Track block at {0} contains {1} entries", header.indexOffset,
|
||||
trkHeader.entries);
|
||||
|
||||
structureBytes = new byte[Marshal.SizeOf(typeof(TrackEntry)) * trkHeader.entries];
|
||||
imageStream.Read(structureBytes, 0, structureBytes.Length);
|
||||
Crc64Context.Data(structureBytes, out verifyCrc);
|
||||
|
||||
if(BitConverter.ToUInt64(verifyCrc, 0) != trkHeader.crc64)
|
||||
{
|
||||
DicConsole.DebugWriteLine("DiscImageChef format plugin",
|
||||
"Expected index CRC {0:X16} but got {1:X16}", trkHeader.crc64,
|
||||
BitConverter.ToUInt64(verifyCrc, 0));
|
||||
return false;
|
||||
}
|
||||
|
||||
break;
|
||||
default:
|
||||
DicConsole.DebugWriteLine("DiscImageChef format plugin", "Ignored field type {0}",
|
||||
entry.blockType);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
byte[] buffer = ReadSectorLong(sectorAddress, track);
|
||||
return CdChecksums.CheckCdSector(buffer);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user