/***************************************************************************
The Disc Image Chef
----------------------------------------------------------------------------
Filename : Checksum.cs
Version : 1.0
Author(s) : Natalia Portillo
Component : Verbs.
Revision : $Revision$
Last change by : $Author$
Date : $Date$
--[ Description ] ----------------------------------------------------------
Implements the 'checksum' verb.
--[ License ] --------------------------------------------------------------
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see .
----------------------------------------------------------------------------
Copyright (C) 2011-2014 Claunia.com
****************************************************************************/
//$Id$
using System;
using DiscImageChef.ImagePlugins;
using DiscImageChef.Checksums;
using System.Collections.Generic;
using DiscImageChef.Console;
using System.Threading;
namespace DiscImageChef.Commands
{
public static class Checksum
{
// How many sectors to read at once
const uint sectorsToRead = 256;
public static void doChecksum(ChecksumOptions options)
{
DicConsole.DebugWriteLine("Checksum command", "--debug={0}", options.Debug);
DicConsole.DebugWriteLine("Checksum command", "--verbose={0}", options.Verbose);
DicConsole.DebugWriteLine("Checksum command", "--separated-tracks={0}", options.SeparatedTracks);
DicConsole.DebugWriteLine("Checksum command", "--whole-disc={0}", options.WholeDisc);
DicConsole.DebugWriteLine("Checksum command", "--input={0}", options.InputFile);
DicConsole.DebugWriteLine("Checksum command", "--adler32={0}", options.DoAdler32);
DicConsole.DebugWriteLine("Checksum command", "--crc16={0}", options.DoCRC16);
DicConsole.DebugWriteLine("Checksum command", "--crc32={0}", options.DoCRC32);
DicConsole.DebugWriteLine("Checksum command", "--crc64={0}", options.DoCRC64);
DicConsole.DebugWriteLine("Checksum command", "--md5={0}", options.DoMD5);
DicConsole.DebugWriteLine("Checksum command", "--ripemd160={0}", options.DoRIPEMD160);
DicConsole.DebugWriteLine("Checksum command", "--sha1={0}", options.DoSHA1);
DicConsole.DebugWriteLine("Checksum command", "--sha256={0}", options.DoSHA256);
DicConsole.DebugWriteLine("Checksum command", "--sha384={0}", options.DoSHA384);
DicConsole.DebugWriteLine("Checksum command", "--sha512={0}", options.DoSHA512);
DicConsole.DebugWriteLine("Checksum command", "--spamsum={0}", options.DoSpamSum);
if(!System.IO.File.Exists(options.InputFile))
{
DicConsole.ErrorWriteLine("Specified file does not exist.");
return;
}
ImagePlugin inputFormat = ImageFormat.Detect(options.InputFile);
if(inputFormat == null)
{
DicConsole.ErrorWriteLine("Unable to recognize image format, not checksumming");
return;
}
inputFormat.OpenImage(options.InputFile);
Core.Statistics.AddMediaFormat(inputFormat.GetImageFormat());
Core.Statistics.AddMedia(inputFormat.ImageInfo.mediaType, false);
if(inputFormat.ImageInfo.imageHasPartitions)
{
try
{
Adler32Context adler32ctx = new Adler32Context();
CRC16Context crc16ctx = new CRC16Context();
CRC32Context crc32ctx = new CRC32Context();
CRC64Context crc64ctx = new CRC64Context();
//Fletcher16Context fletcher16ctx = new Fletcher16Context();
//Fletcher32Context fletcher32ctx = new Fletcher32Context();
MD5Context md5ctx = new MD5Context();
RIPEMD160Context ripemd160ctx = new RIPEMD160Context();
SHA1Context sha1ctx = new SHA1Context();
SHA256Context sha256ctx = new SHA256Context();
SHA384Context sha384ctx = new SHA384Context();
SHA512Context sha512ctx = new SHA512Context();
SpamSumContext ssctx = new SpamSumContext();
Adler32Context adler32ctxTrack = new Adler32Context();
CRC16Context crc16ctxTrack = new CRC16Context();
CRC32Context crc32ctxTrack = new CRC32Context();
CRC64Context crc64ctxTrack = new CRC64Context();
//Fletcher16Context fletcher16ctxTrack = new Fletcher16Context();
//Fletcher32Context fletcher32ctxTrack = new Fletcher32Context();
MD5Context md5ctxTrack = new MD5Context();
RIPEMD160Context ripemd160ctxTrack = new RIPEMD160Context();
SHA1Context sha1ctxTrack = new SHA1Context();
SHA256Context sha256ctxTrack = new SHA256Context();
SHA384Context sha384ctxTrack = new SHA384Context();
SHA512Context sha512ctxTrack = new SHA512Context();
SpamSumContext ssctxTrack = new SpamSumContext();
Thread adlerThread = new Thread(updateAdler);
Thread crc16Thread = new Thread(updateCRC16);
Thread crc32Thread = new Thread(updateCRC32);
Thread crc64Thread = new Thread(updateCRC64);
//Thread fletcher16Thread = new Thread(updateFletcher16);
//Thread fletcher32Thread = new Thread(updateFletcher32);
Thread md5Thread = new Thread(updateMD5);
Thread ripemd160Thread = new Thread(updateRIPEMD160);
Thread sha1Thread = new Thread(updateSHA1);
Thread sha256Thread = new Thread(updateSHA256);
Thread sha384Thread = new Thread(updateSHA384);
Thread sha512Thread = new Thread(updateSHA512);
Thread spamsumThread = new Thread(updateSpamSum);
adlerPacket adlerPkt = new adlerPacket();
crc16Packet crc16Pkt = new crc16Packet();
crc32Packet crc32Pkt = new crc32Packet();
crc64Packet crc64Pkt = new crc64Packet();
//fletcher16Packet fletcher16Pkt = new fletcher16Packet();
//fletcher32Packet fletcher32Pkt = new fletcher32Packet();
md5Packet md5Pkt = new md5Packet();
ripemd160Packet ripemd160Pkt = new ripemd160Packet();
sha1Packet sha1Pkt = new sha1Packet();
sha256Packet sha256Pkt = new sha256Packet();
sha384Packet sha384Pkt = new sha384Packet();
sha512Packet sha512Pkt = new sha512Packet();
spamsumPacket spamsumPkt = new spamsumPacket();
adlerPacket adlerPktTrack = new adlerPacket();
crc16Packet crc16PktTrack = new crc16Packet();
crc32Packet crc32PktTrack = new crc32Packet();
crc64Packet crc64PktTrack = new crc64Packet();
//fletcher16Packet fletcher16PktTrack = new fletcher16Packet();
//fletcher32Packet fletcher32PktTrack = new fletcher32Packet();
md5Packet md5PktTrack = new md5Packet();
ripemd160Packet ripemd160PktTrack = new ripemd160Packet();
sha1Packet sha1PktTrack = new sha1Packet();
sha256Packet sha256PktTrack = new sha256Packet();
sha384Packet sha384PktTrack = new sha384Packet();
sha512Packet sha512PktTrack = new sha512Packet();
spamsumPacket spamsumPktTrack = new spamsumPacket();
if(options.WholeDisc)
{
if(options.DoAdler32)
{
adler32ctx.Init();
adlerPkt.context = adler32ctx;
}
if(options.DoCRC16)
{
crc16ctx.Init();
crc16Pkt.context = crc16ctx;
}
if(options.DoCRC32)
{
crc32ctx.Init();
crc32Pkt.context = crc32ctx;
}
if(options.DoCRC64)
{
crc64ctx.Init();
crc64Pkt.context = crc64ctx;
}
/*if (options.DoFletcher16)
{
fletcher16ctx.Init();
fletcher16Pkt.context = fletcher16ctx;
}
if (options.DoFletcher32)
{
fletcher32ctx.Init();
fletcher32Pkt.context = fletcher32ctx;
}*/
if(options.DoMD5)
{
md5ctx.Init();
md5Pkt.context = md5ctx;
}
if(options.DoRIPEMD160)
{
ripemd160ctx.Init();
ripemd160Pkt.context = ripemd160ctx;
}
if(options.DoSHA1)
{
sha1ctx.Init();
sha1Pkt.context = sha1ctx;
}
if(options.DoSHA256)
{
sha256ctx.Init();
sha256Pkt.context = sha256ctx;
}
if(options.DoSHA384)
{
sha384ctx.Init();
sha384Pkt.context = sha384ctx;
}
if(options.DoSHA512)
{
sha512ctx.Init();
sha512Pkt.context = sha512ctx;
}
if(options.DoSpamSum)
{
ssctx.Init();
spamsumPkt.context = ssctx;
}
}
ulong previousTrackEnd = 0;
List