Files
Aaru/Aaru.Images/CDRWin/Verify.cs

233 lines
7.8 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 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-02-18 10:02:53 +00:00
// Copyright © 2011-2022 Natalia Portillo
// ****************************************************************************/
2022-03-07 07:36:44 +00:00
namespace Aaru.DiscImages;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
2020-02-27 00:33:26 +00:00
using Aaru.Checksums;
using Aaru.CommonTypes.Enums;
2020-02-27 00:33:26 +00:00
using Aaru.CommonTypes.Interfaces;
using Aaru.Console;
2022-03-06 13:29:38 +00:00
public sealed partial class CdrWin
{
2022-03-06 13:29:38 +00:00
/// <inheritdoc />
public bool? VerifyMediaImage()
{
2022-03-06 13:29:38 +00:00
if(_discImage.DiscHashes.Count == 0)
return null;
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;
2022-03-07 07:36:44 +00:00
IFilter[] filters = _discImage.Tracks.OrderBy(t => t.Sequence).Select(t => t.TrackFile.DataFilter).Distinct().
ToArray();
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()))
{
2022-03-06 13:29:38 +00:00
readBytes = 0;
verifyBytes = new byte[verifySize];
2022-03-06 13:29:38 +00:00
while(readBytes + verifySize < stream.Length)
{
stream.Read(verifyBytes, 0, verifyBytes.Length);
ctx.Update(verifyBytes);
2022-03-06 13:29:38 +00:00
readBytes += verifyBytes.LongLength;
}
2022-03-06 13:29:38 +00:00
verifyBytes = new byte[stream.Length - readBytes];
stream.Read(verifyBytes, 0, verifyBytes.Length);
ctx.Update(verifyBytes);
}
2022-03-06 13:29:38 +00:00
string verifySha1 = ctx.End();
AaruConsole.DebugWriteLine("CDRWin plugin", "Calculated SHA1: {0}", verifySha1);
AaruConsole.DebugWriteLine("CDRWin plugin", "Expected SHA1: {0}", sha1);
2022-03-06 13:29:38 +00:00
return verifySha1 == sha1;
}
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];
2022-03-06 13:29:38 +00:00
while(readBytes + verifySize < stream.Length)
{
stream.Read(verifyBytes, 0, verifyBytes.Length);
ctx.Update(verifyBytes);
2022-03-06 13:29:38 +00:00
readBytes += verifyBytes.LongLength;
}
2022-03-06 13:29:38 +00:00
verifyBytes = new byte[stream.Length - readBytes];
stream.Read(verifyBytes, 0, verifyBytes.Length);
ctx.Update(verifyBytes);
}
2022-03-06 13:29:38 +00:00
string verifyMd5 = ctx.End();
AaruConsole.DebugWriteLine("CDRWin plugin", "Calculated MD5: {0}", verifyMd5);
AaruConsole.DebugWriteLine("CDRWin plugin", "Expected MD5: {0}", md5);
2022-03-06 13:29:38 +00:00
return verifyMd5 == md5;
}
if(_discImage.DiscHashes.TryGetValue("crc32", out string crc32))
{
var ctx = new Crc32Context();
2022-03-06 13:29:38 +00:00
foreach(Stream stream in filters.Select(filter => filter.GetDataForkStream()))
{
readBytes = 0;
verifyBytes = new byte[verifySize];
2022-03-06 13:29:38 +00:00
while(readBytes + verifySize < stream.Length)
{
stream.Read(verifyBytes, 0, verifyBytes.Length);
ctx.Update(verifyBytes);
2022-03-06 13:29:38 +00:00
readBytes += verifyBytes.LongLength;
}
2022-03-06 13:29:38 +00:00
verifyBytes = new byte[stream.Length - readBytes];
stream.Read(verifyBytes, 0, verifyBytes.Length);
ctx.Update(verifyBytes);
}
2022-03-06 13:29:38 +00:00
string verifyCrc = ctx.End();
AaruConsole.DebugWriteLine("CDRWin plugin", "Calculated CRC32: {0}", verifyCrc);
AaruConsole.DebugWriteLine("CDRWin plugin", "Expected CRC32: {0}", crc32);
2022-03-06 13:29:38 +00:00
return verifyCrc == crc32;
}
2022-03-06 13:29:38 +00:00
foreach(string hash in _discImage.DiscHashes.Keys)
AaruConsole.DebugWriteLine("CDRWin plugin", "Found unsupported hash {0}", hash);
2020-01-09 19:03:46 +00:00
2022-03-06 13:29:38 +00:00
return null;
}
2022-03-06 13:29:38 +00:00
/// <inheritdoc />
public bool? VerifySector(ulong sectorAddress)
{
ErrorNumber errno = ReadSectorLong(sectorAddress, out byte[] buffer);
2022-03-06 13:29:38 +00:00
return errno != ErrorNumber.NoError ? null : CdChecksums.CheckCdSector(buffer);
}
2022-03-06 13:29:38 +00:00
/// <inheritdoc />
public bool? VerifySectors(ulong sectorAddress, uint length, out List<ulong> failingLbas,
out List<ulong> unknownLbas)
{
failingLbas = new List<ulong>();
unknownLbas = new List<ulong>();
ErrorNumber errno = ReadSectorsLong(sectorAddress, length, out byte[] buffer);
2022-03-06 13:29:38 +00:00
if(errno != ErrorNumber.NoError)
return null;
2022-03-07 07:36:44 +00:00
var bps = (int)(buffer.Length / length);
var sector = new byte[bps];
2020-01-09 19:03:46 +00:00
2022-03-07 07:36:44 +00: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);
2022-03-06 13:29:38 +00:00
break;
case false:
failingLbas.Add((ulong)i + sectorAddress);
2022-03-06 13:29:38 +00:00
break;
}
}
2022-03-06 13:29:38 +00: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 />
public bool? VerifySectors(ulong sectorAddress, uint length, uint track, out List<ulong> failingLbas,
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);
2022-03-06 13:29:38 +00:00
if(errno != ErrorNumber.NoError)
return null;
2020-01-09 19:03:46 +00:00
2022-03-07 07:36:44 +00:00
var bps = (int)(buffer.Length / length);
var sector = new byte[bps];
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);
2022-03-06 13:29:38 +00:00
break;
case false:
failingLbas.Add((ulong)i + sectorAddress);
2022-03-06 13:29:38 +00:00
break;
}
}
2022-03-06 13:29:38 +00:00
if(unknownLbas.Count > 0)
return null;
return failingLbas.Count <= 0;
}
}