Files
Aaru/Aaru.Images/AaruFormat/Verify.cs

336 lines
13 KiB
C#
Raw Normal View History

// /***************************************************************************
2020-02-27 12:31:25 +00:00
// Aaru Data Preservation Suite
// ----------------------------------------------------------------------------
//
// Filename : Verify.cs
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// Component : Disk image plugins.
//
// --[ Description ] ----------------------------------------------------------
//
// Verifies Aaru Format disk images.
//
// --[ License ] --------------------------------------------------------------
//
// This library is free software; you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as
// published by the Free Software Foundation; either version 2.1 of the
// License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, see <http://www.gnu.org/licenses/>.
//
// ----------------------------------------------------------------------------
2024-12-19 10:45:18 +00:00
// Copyright © 2011-2025 Natalia Portillo
// ****************************************************************************/
using System;
using System.Collections.Generic;
2020-02-27 00:33:26 +00:00
using Aaru.Checksums;
using Aaru.CommonTypes.Enums;
using Aaru.Helpers;
using Aaru.Logging;
namespace Aaru.Images;
2022-03-06 13:29:38 +00:00
public sealed partial class AaruFormat
{
2023-10-03 23:34:59 +01:00
#region IVerifiableImage Members
2022-03-06 13:29:38 +00:00
/// <inheritdoc />
public bool? VerifyMediaImage()
{
2022-03-06 13:29:38 +00:00
// This will traverse all blocks and check their CRC64 without uncompressing them
2025-08-17 06:11:22 +01:00
AaruLogging.Debug(MODULE_NAME, Localization.Checking_index_integrity_at_0, _header.indexOffset);
2022-03-06 13:29:38 +00:00
_imageStream.Position = (long)_header.indexOffset;
2022-03-06 13:29:38 +00:00
_structureBytes = new byte[Marshal.SizeOf<IndexHeader>()];
_imageStream.EnsureRead(_structureBytes, 0, _structureBytes.Length);
2022-03-06 13:29:38 +00:00
IndexHeader idxHeader = Marshal.SpanToStructureLittleEndian<IndexHeader>(_structureBytes);
2022-03-06 13:29:38 +00:00
if(idxHeader.identifier != BlockType.Index)
{
2025-08-17 06:11:22 +01:00
AaruLogging.Debug(MODULE_NAME, Localization.Incorrect_index_identifier);
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
return false;
}
2025-08-17 06:11:22 +01:00
AaruLogging.Debug(MODULE_NAME,
2024-05-01 04:05:22 +01:00
Localization.Index_at_0_contains_1_entries,
_header.indexOffset,
idxHeader.entries);
2022-03-06 13:29:38 +00:00
_structureBytes = new byte[Marshal.SizeOf<IndexEntry>() * idxHeader.entries];
_imageStream.EnsureRead(_structureBytes, 0, _structureBytes.Length);
2022-03-06 13:29:38 +00:00
Crc64Context.Data(_structureBytes, out byte[] verifyCrc);
2022-03-06 13:29:38 +00:00
if(BitConverter.ToUInt64(verifyCrc, 0) != idxHeader.crc64)
{
2025-08-17 06:11:22 +01:00
AaruLogging.Debug(MODULE_NAME,
2024-05-01 04:05:22 +01:00
Localization.Expected_index_CRC_0_X16_but_got_1_X16,
idxHeader.crc64,
BitConverter.ToUInt64(verifyCrc, 0));
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
return false;
}
2022-03-06 13:29:38 +00:00
_imageStream.Position -= _structureBytes.Length;
2024-05-01 04:39:38 +01:00
List<IndexEntry> vrIndex = [];
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
for(ushort i = 0; i < idxHeader.entries; i++)
{
_structureBytes = new byte[Marshal.SizeOf<IndexEntry>()];
_imageStream.EnsureRead(_structureBytes, 0, _structureBytes.Length);
2022-03-06 13:29:38 +00:00
IndexEntry entry = Marshal.SpanToStructureLittleEndian<IndexEntry>(_structureBytes);
2020-02-29 18:03:35 +00:00
2025-08-17 06:11:22 +01:00
AaruLogging.Debug(MODULE_NAME,
2024-05-01 04:05:22 +01:00
Localization.Block_type_0_with_data_type_1_is_indexed_to_be_at_2,
entry.blockType,
entry.dataType,
entry.offset);
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
vrIndex.Add(entry);
}
2022-03-06 13:29:38 +00:00
// Read up to 1MiB at a time for verification
const int verifySize = 1024 * 1024;
foreach(IndexEntry entry in vrIndex)
{
_imageStream.Position = (long)entry.offset;
Crc64Context crcVerify;
ulong readBytes;
byte[] verifyBytes;
2022-03-06 13:29:38 +00:00
switch(entry.blockType)
{
2022-03-06 13:29:38 +00:00
case BlockType.DataBlock:
_structureBytes = new byte[Marshal.SizeOf<BlockHeader>()];
_imageStream.EnsureRead(_structureBytes, 0, _structureBytes.Length);
2022-03-06 13:29:38 +00:00
BlockHeader blockHeader = Marshal.SpanToStructureLittleEndian<BlockHeader>(_structureBytes);
2022-03-06 13:29:38 +00:00
crcVerify = new Crc64Context();
readBytes = 0;
2025-08-17 06:11:22 +01:00
AaruLogging.Debug(MODULE_NAME,
2024-05-01 04:05:22 +01:00
Localization.Verifying_data_block_type_0_at_position_1,
entry.dataType,
entry.offset);
2022-03-06 13:29:38 +00:00
while(readBytes + verifySize < blockHeader.cmpLength)
{
verifyBytes = new byte[verifySize];
_imageStream.EnsureRead(verifyBytes, 0, verifyBytes.Length);
crcVerify.Update(verifyBytes);
2022-03-06 13:29:38 +00:00
readBytes += (ulong)verifyBytes.LongLength;
}
verifyBytes = new byte[blockHeader.cmpLength - readBytes];
_imageStream.EnsureRead(verifyBytes, 0, verifyBytes.Length);
2022-03-06 13:29:38 +00:00
crcVerify.Update(verifyBytes);
2022-03-06 13:29:38 +00:00
verifyCrc = crcVerify.Final();
2022-03-06 13:29:38 +00:00
if(BitConverter.ToUInt64(verifyCrc, 0) != blockHeader.cmpCrc64)
{
2025-08-17 06:11:22 +01:00
AaruLogging.Debug(MODULE_NAME,
2024-05-01 04:05:22 +01:00
Localization.Expected_block_CRC_0_X16_but_got_1_X16,
blockHeader.cmpCrc64,
BitConverter.ToUInt64(verifyCrc, 0));
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
return false;
}
2022-03-06 13:29:38 +00:00
break;
case BlockType.DeDuplicationTable:
_structureBytes = new byte[Marshal.SizeOf<DdtHeader>()];
_imageStream.EnsureRead(_structureBytes, 0, _structureBytes.Length);
2022-03-06 13:29:38 +00:00
DdtHeader ddtHeader = Marshal.SpanToStructureLittleEndian<DdtHeader>(_structureBytes);
2022-03-06 13:29:38 +00:00
crcVerify = new Crc64Context();
readBytes = 0;
2025-08-17 06:11:22 +01:00
AaruLogging.Debug(MODULE_NAME,
Localization.Verifying_deduplication_table_type_0_at_position_1,
2024-05-01 04:05:22 +01:00
entry.dataType,
entry.offset);
2022-03-06 13:29:38 +00:00
while(readBytes + verifySize < ddtHeader.cmpLength)
{
verifyBytes = new byte[verifySize];
_imageStream.EnsureRead(verifyBytes, 0, verifyBytes.Length);
crcVerify.Update(verifyBytes);
2022-03-06 13:29:38 +00:00
readBytes += (ulong)verifyBytes.LongLength;
}
verifyBytes = new byte[ddtHeader.cmpLength - readBytes];
_imageStream.EnsureRead(verifyBytes, 0, verifyBytes.Length);
2022-03-06 13:29:38 +00:00
crcVerify.Update(verifyBytes);
2022-03-06 13:29:38 +00:00
verifyCrc = crcVerify.Final();
2022-03-06 13:29:38 +00:00
if(BitConverter.ToUInt64(verifyCrc, 0) != ddtHeader.cmpCrc64)
{
2025-08-17 06:11:22 +01:00
AaruLogging.Debug(MODULE_NAME,
2024-05-01 04:05:22 +01:00
Localization.Expected_DDT_CRC_0_but_got_1,
ddtHeader.cmpCrc64,
BitConverter.ToUInt64(verifyCrc, 0));
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
return false;
}
2022-03-06 13:29:38 +00:00
break;
case BlockType.TracksBlock:
_structureBytes = new byte[Marshal.SizeOf<TracksHeader>()];
_imageStream.EnsureRead(_structureBytes, 0, _structureBytes.Length);
2022-03-06 13:29:38 +00:00
TracksHeader trkHeader = Marshal.SpanToStructureLittleEndian<TracksHeader>(_structureBytes);
2025-08-17 06:11:22 +01:00
AaruLogging.Debug(MODULE_NAME,
2024-05-01 04:05:22 +01:00
Localization.Track_block_at_0_contains_1_entries,
_header.indexOffset,
trkHeader.entries);
2022-03-06 13:29:38 +00:00
_structureBytes = new byte[Marshal.SizeOf<TrackEntry>() * trkHeader.entries];
_imageStream.EnsureRead(_structureBytes, 0, _structureBytes.Length);
2022-03-06 13:29:38 +00:00
Crc64Context.Data(_structureBytes, out verifyCrc);
2022-03-06 13:29:38 +00:00
if(BitConverter.ToUInt64(verifyCrc, 0) != trkHeader.crc64)
{
2025-08-17 06:11:22 +01:00
AaruLogging.Debug(MODULE_NAME,
2024-05-01 04:05:22 +01:00
Localization.Expected_index_CRC_0_X16_but_got_1_X16,
trkHeader.crc64,
BitConverter.ToUInt64(verifyCrc, 0));
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
return false;
}
2022-03-06 13:29:38 +00:00
break;
default:
2025-08-17 06:11:22 +01:00
AaruLogging.Debug(MODULE_NAME, Localization.Ignored_field_type_0, entry.blockType);
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
break;
}
}
2022-03-06 13:29:38 +00:00
return true;
}
2023-10-03 23:34:59 +01:00
#endregion
#region IWritableOpticalImage Members
2022-03-06 13:29:38 +00:00
/// <inheritdoc />
public bool? VerifySector(ulong sectorAddress)
{
2024-05-01 04:05:22 +01:00
if(_imageInfo.MetadataMediaType != MetadataMediaType.OpticalDisc) return null;
2022-03-06 13:29:38 +00:00
ErrorNumber errno = ReadSectorLong(sectorAddress, out byte[] buffer);
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
return errno != ErrorNumber.NoError ? null : CdChecksums.CheckCdSector(buffer);
}
/// <inheritdoc />
2023-10-03 23:34:59 +01:00
public bool? VerifySectors(ulong sectorAddress, uint length, out List<ulong> failingLbas,
2022-03-06 13:29:38 +00:00
out List<ulong> unknownLbas)
{
2024-05-01 04:39:38 +01:00
failingLbas = [];
unknownLbas = [];
2022-03-06 13:29:38 +00:00
// Right now only CompactDisc sectors are verifiable
if(_imageInfo.MetadataMediaType != MetadataMediaType.OpticalDisc)
{
2024-05-01 04:05:22 +01:00
for(ulong i = sectorAddress; i < sectorAddress + length; i++) unknownLbas.Add(i);
2022-03-06 13:29:38 +00:00
return null;
}
2022-03-06 13:29:38 +00:00
ErrorNumber errno = ReadSectorsLong(sectorAddress, length, out byte[] buffer);
2024-05-01 04:05:22 +01:00
if(errno != ErrorNumber.NoError) return null;
int bps = (int)(buffer.Length / length);
byte[] sector = new byte[bps];
2024-05-01 04:39:38 +01:00
failingLbas = [];
unknownLbas = [];
for(int i = 0; i < length; i++)
2022-03-06 13:29:38 +00:00
{
Array.Copy(buffer, i * bps, sector, 0, bps);
bool? sectorStatus = CdChecksums.CheckCdSector(sector);
2022-03-06 13:29:38 +00:00
switch(sectorStatus)
{
2022-03-06 13:29:38 +00:00
case null:
unknownLbas.Add((ulong)i + sectorAddress);
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
break;
case false:
failingLbas.Add((ulong)i + sectorAddress);
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
break;
}
2022-03-06 13:29:38 +00:00
}
2024-05-01 04:05:22 +01:00
if(unknownLbas.Count > 0) return null;
2022-03-06 13:29:38 +00:00
return failingLbas.Count <= 0;
}
2022-03-06 13:29:38 +00:00
/// <inheritdoc />
2023-10-03 23:34:59 +01:00
public bool? VerifySectors(ulong sectorAddress, uint length, uint track, out List<ulong> failingLbas,
2022-03-06 13:29:38 +00:00
out List<ulong> unknownLbas)
{
// Right now only CompactDisc sectors are verifiable
if(_imageInfo.MetadataMediaType != MetadataMediaType.OpticalDisc)
{
2024-05-01 04:39:38 +01:00
failingLbas = [];
unknownLbas = [];
2024-05-01 04:05:22 +01:00
for(ulong i = sectorAddress; i < sectorAddress + length; i++) unknownLbas.Add(i);
2022-03-06 13:29:38 +00:00
return null;
}
2024-05-01 04:39:38 +01:00
failingLbas = [];
unknownLbas = [];
2022-03-06 13:29:38 +00:00
ErrorNumber errno = ReadSectorsLong(sectorAddress, length, track, out byte[] buffer);
2024-05-01 04:05:22 +01:00
if(errno != ErrorNumber.NoError) return null;
int bps = (int)(buffer.Length / length);
byte[] sector = new byte[bps];
for(int i = 0; i < length; i++)
2022-03-06 13:29:38 +00:00
{
Array.Copy(buffer, i * bps, sector, 0, bps);
bool? sectorStatus = CdChecksums.CheckCdSector(sector);
2022-03-06 13:29:38 +00:00
switch(sectorStatus)
{
case null:
unknownLbas.Add((ulong)i + sectorAddress);
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
break;
case false:
failingLbas.Add((ulong)i + sectorAddress);
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
break;
}
2022-03-06 13:29:38 +00:00
}
2024-05-01 04:05:22 +01:00
if(unknownLbas.Count > 0) return null;
2022-03-06 13:29:38 +00:00
return failingLbas.Count <= 0;
}
2023-10-03 23:34:59 +01:00
#endregion
}