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

225 lines
8.1 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/>.
//
// ----------------------------------------------------------------------------
2020-12-31 23:08:23 +00:00
// Copyright © 2011-2021 Natalia Portillo
// ****************************************************************************/
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.Interfaces;
using Aaru.Console;
2020-02-27 00:33:26 +00:00
namespace Aaru.DiscImages
{
2020-07-22 13:20:25 +01:00
public sealed partial class CdrWin
{
2021-08-17 13:56:05 +01:00
/// <inheritdoc />
public bool? VerifyMediaImage()
{
2020-01-09 19:03:46 +00:00
if(_discImage.DiscHashes.Count == 0)
return null;
2020-01-09 19:03:46 +00:00
// Read up to 1 MiB at a time for verification
const int verifySize = 1024 * 1024;
long readBytes;
byte[] verifyBytes;
2020-01-09 19:03:46 +00:00
IFilter[] filters = _discImage.Tracks.OrderBy(t => t.Sequence).Select(t => t.TrackFile.DataFilter).
Distinct().ToArray();
2020-01-09 19:03:46 +00:00
if(_discImage.DiscHashes.TryGetValue("sha1", out string sha1))
{
2020-01-09 19:03:46 +00:00
var ctx = new Sha1Context();
2021-08-17 17:51:01 +01:00
foreach(Stream stream in filters.Select(filter => filter.GetDataForkStream()))
{
readBytes = 0;
2020-01-09 19:03:46 +00:00
verifyBytes = new byte[verifySize];
2020-01-09 19:03:46 +00:00
while(readBytes + verifySize < stream.Length)
{
stream.Read(verifyBytes, 0, verifyBytes.Length);
ctx.Update(verifyBytes);
readBytes += verifyBytes.LongLength;
}
verifyBytes = new byte[stream.Length - readBytes];
stream.Read(verifyBytes, 0, verifyBytes.Length);
ctx.Update(verifyBytes);
}
string verifySha1 = ctx.End();
2020-02-27 23:48:41 +00:00
AaruConsole.DebugWriteLine("CDRWin plugin", "Calculated SHA1: {0}", verifySha1);
AaruConsole.DebugWriteLine("CDRWin plugin", "Expected SHA1: {0}", sha1);
return verifySha1 == sha1;
}
2020-01-09 19:03:46 +00:00
if(_discImage.DiscHashes.TryGetValue("md5", out string md5))
{
2020-01-09 19:03:46 +00:00
var ctx = new Md5Context();
2021-08-17 17:51:01 +01:00
foreach(Stream stream in filters.Select(filter => filter.GetDataForkStream()))
{
readBytes = 0;
2020-01-09 19:03:46 +00:00
verifyBytes = new byte[verifySize];
2020-01-09 19:03:46 +00:00
while(readBytes + verifySize < stream.Length)
{
stream.Read(verifyBytes, 0, verifyBytes.Length);
ctx.Update(verifyBytes);
readBytes += verifyBytes.LongLength;
}
verifyBytes = new byte[stream.Length - readBytes];
stream.Read(verifyBytes, 0, verifyBytes.Length);
ctx.Update(verifyBytes);
}
string verifyMd5 = ctx.End();
2020-02-27 23:48:41 +00:00
AaruConsole.DebugWriteLine("CDRWin plugin", "Calculated MD5: {0}", verifyMd5);
AaruConsole.DebugWriteLine("CDRWin plugin", "Expected MD5: {0}", md5);
return verifyMd5 == md5;
}
2020-01-09 19:03:46 +00:00
if(_discImage.DiscHashes.TryGetValue("crc32", out string crc32))
{
2020-01-09 19:03:46 +00:00
var ctx = new Crc32Context();
2021-08-17 17:51:01 +01:00
foreach(Stream stream in filters.Select(filter => filter.GetDataForkStream()))
{
readBytes = 0;
2020-01-09 19:03:46 +00:00
verifyBytes = new byte[verifySize];
2020-01-09 19:03:46 +00:00
while(readBytes + verifySize < stream.Length)
{
stream.Read(verifyBytes, 0, verifyBytes.Length);
ctx.Update(verifyBytes);
readBytes += verifyBytes.LongLength;
}
verifyBytes = new byte[stream.Length - readBytes];
stream.Read(verifyBytes, 0, verifyBytes.Length);
ctx.Update(verifyBytes);
}
string verifyCrc = ctx.End();
2020-02-27 23:48:41 +00:00
AaruConsole.DebugWriteLine("CDRWin plugin", "Calculated CRC32: {0}", verifyCrc);
AaruConsole.DebugWriteLine("CDRWin plugin", "Expected CRC32: {0}", crc32);
return verifyCrc == crc32;
}
2020-01-09 19:03:46 +00:00
foreach(string hash in _discImage.DiscHashes.Keys)
2020-02-27 23:48:41 +00:00
AaruConsole.DebugWriteLine("CDRWin plugin", "Found unsupported hash {0}", hash);
return null;
}
2021-08-17 13:56:05 +01:00
/// <inheritdoc />
public bool? VerifySector(ulong sectorAddress)
{
byte[] buffer = ReadSectorLong(sectorAddress);
2020-01-09 19:03:46 +00:00
return CdChecksums.CheckCdSector(buffer);
}
2021-08-17 13:56:05 +01:00
/// <inheritdoc />
2020-01-09 19:03:46 +00:00
public bool? VerifySectors(ulong sectorAddress, uint length, out List<ulong> failingLbas,
out List<ulong> unknownLbas)
{
byte[] buffer = ReadSectorsLong(sectorAddress, length);
int bps = (int)(buffer.Length / length);
byte[] sector = new byte[bps];
failingLbas = new List<ulong>();
unknownLbas = new List<ulong>();
for(int i = 0; i < length; i++)
{
Array.Copy(buffer, i * bps, sector, 0, bps);
bool? sectorStatus = CdChecksums.CheckCdSector(sector);
switch(sectorStatus)
{
case null:
unknownLbas.Add((ulong)i + sectorAddress);
2020-01-09 19:03:46 +00:00
break;
case false:
failingLbas.Add((ulong)i + sectorAddress);
2020-01-09 19:03:46 +00:00
break;
}
}
2020-01-09 19:03:46 +00:00
if(unknownLbas.Count > 0)
return null;
return failingLbas.Count <= 0;
}
2021-08-17 13:56:05 +01:00
/// <inheritdoc />
2020-01-09 19:03:46 +00:00
public bool? VerifySectors(ulong sectorAddress, uint length, uint track, out List<ulong> failingLbas,
out List<ulong> unknownLbas)
{
byte[] buffer = ReadSectorsLong(sectorAddress, length, track);
int bps = (int)(buffer.Length / length);
byte[] sector = new byte[bps];
failingLbas = new List<ulong>();
unknownLbas = new List<ulong>();
for(int i = 0; i < length; i++)
{
Array.Copy(buffer, i * bps, sector, 0, bps);
bool? sectorStatus = CdChecksums.CheckCdSector(sector);
switch(sectorStatus)
{
case null:
unknownLbas.Add((ulong)i + sectorAddress);
2020-01-09 19:03:46 +00:00
break;
case false:
failingLbas.Add((ulong)i + sectorAddress);
2020-01-09 19:03:46 +00:00
break;
}
}
2020-01-09 19:03:46 +00:00
if(unknownLbas.Count > 0)
return null;
return failingLbas.Count <= 0;
}
}
}