2019-01-19 17:39:31 +00:00
|
|
|
// /***************************************************************************
|
2020-02-27 12:31:25 +00:00
|
|
|
// Aaru Data Preservation Suite
|
2019-01-19 17:39:31 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
//
|
|
|
|
|
// Filename : Verify.cs
|
|
|
|
|
// Author(s) : Natalia Portillo <claunia@claunia.com>
|
|
|
|
|
//
|
|
|
|
|
// Component : Disk image plugins.
|
|
|
|
|
//
|
|
|
|
|
// --[ Description ] ----------------------------------------------------------
|
|
|
|
|
//
|
|
|
|
|
// Verifies CDRWin format disc 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/>.
|
|
|
|
|
//
|
|
|
|
|
// ----------------------------------------------------------------------------
|
2022-12-03 16:07:10 +00:00
|
|
|
// Copyright © 2011-2023 Natalia Portillo
|
2019-01-19 17:39:31 +00:00
|
|
|
// ****************************************************************************/
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
2020-02-27 00:33:26 +00:00
|
|
|
using Aaru.Checksums;
|
2021-09-20 14:22:22 +01:00
|
|
|
using Aaru.CommonTypes.Enums;
|
2020-02-27 00:33:26 +00:00
|
|
|
using Aaru.CommonTypes.Interfaces;
|
|
|
|
|
using Aaru.Console;
|
2022-11-14 09:43:16 +00:00
|
|
|
using Aaru.Helpers;
|
2019-01-19 17:39:31 +00:00
|
|
|
|
2022-11-15 15:58:43 +00:00
|
|
|
namespace Aaru.DiscImages;
|
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
public sealed partial class CdrWin
|
2019-01-19 17:39:31 +00:00
|
|
|
{
|
2023-10-03 23:34:59 +01:00
|
|
|
#region IVerifiableImage Members
|
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
/// <inheritdoc />
|
|
|
|
|
public bool? VerifyMediaImage()
|
2019-01-19 17:39:31 +00:00
|
|
|
{
|
2022-03-06 13:29:38 +00:00
|
|
|
if(_discImage.DiscHashes.Count == 0)
|
|
|
|
|
return null;
|
2019-01-19 17:39:31 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
// Read up to 1 MiB at a time for verification
|
|
|
|
|
const int verifySize = 1024 * 1024;
|
|
|
|
|
long readBytes;
|
|
|
|
|
byte[] verifyBytes;
|
2019-01-19 17:39:31 +00:00
|
|
|
|
2023-10-04 17:34:40 +01:00
|
|
|
IFilter[] filters = _discImage.Tracks.OrderBy(t => t.Sequence).
|
|
|
|
|
Select(t => t.TrackFile.DataFilter).
|
|
|
|
|
Distinct().
|
2022-03-07 07:36:44 +00:00
|
|
|
ToArray();
|
2019-01-19 17:39:31 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
if(_discImage.DiscHashes.TryGetValue("sha1", out string sha1))
|
|
|
|
|
{
|
|
|
|
|
var ctx = new Sha1Context();
|
|
|
|
|
|
|
|
|
|
foreach(Stream stream in filters.Select(filter => filter.GetDataForkStream()))
|
2019-01-19 17:39:31 +00:00
|
|
|
{
|
2022-03-06 13:29:38 +00:00
|
|
|
readBytes = 0;
|
|
|
|
|
verifyBytes = new byte[verifySize];
|
2019-01-19 17:39:31 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
while(readBytes + verifySize < stream.Length)
|
2019-01-19 17:39:31 +00:00
|
|
|
{
|
2022-11-14 09:43:16 +00:00
|
|
|
stream.EnsureRead(verifyBytes, 0, verifyBytes.Length);
|
2019-01-19 17:39:31 +00:00
|
|
|
ctx.Update(verifyBytes);
|
2022-03-06 13:29:38 +00:00
|
|
|
readBytes += verifyBytes.LongLength;
|
2019-01-19 17:39:31 +00:00
|
|
|
}
|
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
verifyBytes = new byte[stream.Length - readBytes];
|
2022-11-14 09:43:16 +00:00
|
|
|
stream.EnsureRead(verifyBytes, 0, verifyBytes.Length);
|
2022-03-06 13:29:38 +00:00
|
|
|
ctx.Update(verifyBytes);
|
2019-01-19 17:39:31 +00:00
|
|
|
}
|
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
string verifySha1 = ctx.End();
|
2023-10-03 18:39:13 +01:00
|
|
|
AaruConsole.DebugWriteLine(MODULE_NAME, Localization.Calculated_SHA1_0, verifySha1);
|
2023-10-03 23:34:59 +01:00
|
|
|
AaruConsole.DebugWriteLine(MODULE_NAME, Localization.Expected_SHA1_0, sha1);
|
2019-01-19 17:39:31 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
return verifySha1 == sha1;
|
|
|
|
|
}
|
2019-01-19 17:39:31 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
if(_discImage.DiscHashes.TryGetValue("md5", out string md5))
|
|
|
|
|
{
|
|
|
|
|
var ctx = new Md5Context();
|
|
|
|
|
|
|
|
|
|
foreach(Stream stream in filters.Select(filter => filter.GetDataForkStream()))
|
|
|
|
|
{
|
|
|
|
|
readBytes = 0;
|
|
|
|
|
verifyBytes = new byte[verifySize];
|
2019-01-19 17:39:31 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
while(readBytes + verifySize < stream.Length)
|
|
|
|
|
{
|
2022-11-14 09:43:16 +00:00
|
|
|
stream.EnsureRead(verifyBytes, 0, verifyBytes.Length);
|
2019-01-19 17:39:31 +00:00
|
|
|
ctx.Update(verifyBytes);
|
2022-03-06 13:29:38 +00:00
|
|
|
readBytes += verifyBytes.LongLength;
|
2019-01-19 17:39:31 +00:00
|
|
|
}
|
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
verifyBytes = new byte[stream.Length - readBytes];
|
2022-11-14 09:43:16 +00:00
|
|
|
stream.EnsureRead(verifyBytes, 0, verifyBytes.Length);
|
2022-03-06 13:29:38 +00:00
|
|
|
ctx.Update(verifyBytes);
|
2019-01-19 17:39:31 +00:00
|
|
|
}
|
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
string verifyMd5 = ctx.End();
|
2023-10-03 18:39:13 +01:00
|
|
|
AaruConsole.DebugWriteLine(MODULE_NAME, Localization.Calculated_MD5_0, verifyMd5);
|
2023-10-03 23:34:59 +01:00
|
|
|
AaruConsole.DebugWriteLine(MODULE_NAME, Localization.Expected_MD5_0, md5);
|
2019-01-19 17:39:31 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
return verifyMd5 == md5;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(_discImage.DiscHashes.TryGetValue("crc32", out string crc32))
|
|
|
|
|
{
|
|
|
|
|
var ctx = new Crc32Context();
|
2019-01-19 17:39:31 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
foreach(Stream stream in filters.Select(filter => filter.GetDataForkStream()))
|
|
|
|
|
{
|
|
|
|
|
readBytes = 0;
|
|
|
|
|
verifyBytes = new byte[verifySize];
|
2019-01-19 17:39:31 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
while(readBytes + verifySize < stream.Length)
|
|
|
|
|
{
|
2022-11-14 09:43:16 +00:00
|
|
|
stream.EnsureRead(verifyBytes, 0, verifyBytes.Length);
|
2019-01-19 17:39:31 +00:00
|
|
|
ctx.Update(verifyBytes);
|
2022-03-06 13:29:38 +00:00
|
|
|
readBytes += verifyBytes.LongLength;
|
2019-01-19 17:39:31 +00:00
|
|
|
}
|
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
verifyBytes = new byte[stream.Length - readBytes];
|
2022-11-14 09:43:16 +00:00
|
|
|
stream.EnsureRead(verifyBytes, 0, verifyBytes.Length);
|
2022-03-06 13:29:38 +00:00
|
|
|
ctx.Update(verifyBytes);
|
2019-01-19 17:39:31 +00:00
|
|
|
}
|
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
string verifyCrc = ctx.End();
|
2023-10-03 18:39:13 +01:00
|
|
|
AaruConsole.DebugWriteLine(MODULE_NAME, Localization.Calculated_CRC32_0, verifyCrc);
|
2023-10-03 23:34:59 +01:00
|
|
|
AaruConsole.DebugWriteLine(MODULE_NAME, Localization.Expected_CRC32_0, crc32);
|
2019-01-19 17:39:31 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
return verifyCrc == crc32;
|
2019-01-19 17:39:31 +00:00
|
|
|
}
|
2019-03-01 07:35:22 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
foreach(string hash in _discImage.DiscHashes.Keys)
|
2023-10-03 18:39:13 +01:00
|
|
|
AaruConsole.DebugWriteLine(MODULE_NAME, Localization.Found_unsupported_hash_0, hash);
|
2020-01-09 19:03:46 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
return null;
|
|
|
|
|
}
|
2019-03-01 07:35:22 +00:00
|
|
|
|
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)
|
|
|
|
|
{
|
|
|
|
|
ErrorNumber errno = ReadSectorLong(sectorAddress, out byte[] buffer);
|
2021-09-20 14:22:22 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
return errno != ErrorNumber.NoError ? null : CdChecksums.CheckCdSector(buffer);
|
|
|
|
|
}
|
2021-09-20 14:22:22 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
/// <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)
|
|
|
|
|
{
|
|
|
|
|
failingLbas = new List<ulong>();
|
|
|
|
|
unknownLbas = new List<ulong>();
|
|
|
|
|
ErrorNumber errno = ReadSectorsLong(sectorAddress, length, out byte[] buffer);
|
2019-03-01 07:35:22 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
if(errno != ErrorNumber.NoError)
|
|
|
|
|
return null;
|
2019-03-01 07:35:22 +00:00
|
|
|
|
2023-10-03 23:34:59 +01:00
|
|
|
var bps = (int)(buffer.Length / length);
|
|
|
|
|
var sector = new byte[bps];
|
2020-01-09 19:03:46 +00:00
|
|
|
|
2023-10-03 23:34:59 +01:00
|
|
|
for(var 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);
|
2020-01-09 19:03:46 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
switch(sectorStatus)
|
|
|
|
|
{
|
|
|
|
|
case null:
|
|
|
|
|
unknownLbas.Add((ulong)i + sectorAddress);
|
2019-03-01 07:35:22 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
break;
|
|
|
|
|
case false:
|
|
|
|
|
failingLbas.Add((ulong)i + sectorAddress);
|
2019-03-01 07:35:22 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
break;
|
|
|
|
|
}
|
2019-03-01 07:35:22 +00:00
|
|
|
}
|
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
if(unknownLbas.Count > 0)
|
|
|
|
|
return null;
|
2021-09-21 02:59:54 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
return failingLbas.Count <= 0;
|
|
|
|
|
}
|
2019-03-01 07:35:22 +00:00
|
|
|
|
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)
|
|
|
|
|
{
|
|
|
|
|
failingLbas = new List<ulong>();
|
|
|
|
|
unknownLbas = new List<ulong>();
|
2022-03-07 07:36:44 +00:00
|
|
|
ErrorNumber errno = ReadSectorsLong(sectorAddress, length, track, out byte[] buffer);
|
2019-03-01 07:35:22 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
if(errno != ErrorNumber.NoError)
|
|
|
|
|
return null;
|
2020-01-09 19:03:46 +00:00
|
|
|
|
2023-10-03 23:34:59 +01:00
|
|
|
var bps = (int)(buffer.Length / length);
|
|
|
|
|
var sector = new byte[bps];
|
2022-03-07 07:36:44 +00:00
|
|
|
|
2023-10-03 23:34:59 +01:00
|
|
|
for(var 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);
|
2020-01-09 19:03:46 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
switch(sectorStatus)
|
|
|
|
|
{
|
|
|
|
|
case null:
|
|
|
|
|
unknownLbas.Add((ulong)i + sectorAddress);
|
2019-03-01 07:35:22 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
break;
|
|
|
|
|
case false:
|
|
|
|
|
failingLbas.Add((ulong)i + sectorAddress);
|
2019-03-01 07:35:22 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
break;
|
|
|
|
|
}
|
2019-03-01 07:35:22 +00:00
|
|
|
}
|
2022-03-06 13:29:38 +00:00
|
|
|
|
|
|
|
|
if(unknownLbas.Count > 0)
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
return failingLbas.Count <= 0;
|
2019-01-19 17:39:31 +00:00
|
|
|
}
|
2023-10-03 23:34:59 +01:00
|
|
|
|
|
|
|
|
#endregion
|
2019-01-19 17:39:31 +00:00
|
|
|
}
|