2017-05-19 20:28:49 +01:00
|
|
|
// /***************************************************************************
|
2016-07-28 18:13:49 +01:00
|
|
|
// The Disc Image Chef
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
//
|
|
|
|
|
// Filename : Entropy.cs
|
|
|
|
|
// Author(s) : Natalia Portillo <claunia@claunia.com>
|
|
|
|
|
//
|
|
|
|
|
// Component : Verbs.
|
|
|
|
|
//
|
|
|
|
|
// --[ Description ] ----------------------------------------------------------
|
|
|
|
|
//
|
|
|
|
|
// Implements the 'entropy' 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 <http://www.gnu.org/licenses/>.
|
|
|
|
|
//
|
|
|
|
|
// ----------------------------------------------------------------------------
|
2018-12-29 17:34:38 +00:00
|
|
|
// Copyright © 2011-2019 Natalia Portillo
|
2016-07-28 18:13:49 +01:00
|
|
|
// ****************************************************************************/
|
2015-05-19 06:04:21 +01:00
|
|
|
|
2019-01-05 16:59:23 +00:00
|
|
|
using System.Collections.Generic;
|
2018-06-25 19:08:16 +01:00
|
|
|
using DiscImageChef.CommonTypes;
|
2019-01-05 19:50:56 +00:00
|
|
|
using DiscImageChef.CommonTypes.Enums;
|
2018-06-25 19:08:16 +01:00
|
|
|
using DiscImageChef.CommonTypes.Interfaces;
|
2015-10-18 22:04:03 +01:00
|
|
|
using DiscImageChef.Console;
|
2017-05-27 15:17:20 +01:00
|
|
|
using DiscImageChef.Core;
|
2019-01-05 16:59:23 +00:00
|
|
|
using Mono.Options;
|
2015-05-19 06:04:21 +01:00
|
|
|
|
|
|
|
|
namespace DiscImageChef.Commands
|
|
|
|
|
{
|
2019-01-05 16:59:23 +00:00
|
|
|
class EntropyCommand : Command
|
2015-05-19 06:04:21 +01:00
|
|
|
{
|
2019-01-05 16:59:23 +00:00
|
|
|
bool duplicatedSectors = true;
|
|
|
|
|
string inputFile;
|
|
|
|
|
bool separatedTracks = true;
|
|
|
|
|
|
|
|
|
|
bool showHelp;
|
|
|
|
|
bool wholeDisc = true;
|
|
|
|
|
|
|
|
|
|
public EntropyCommand() : base("entropy", "Calculates entropy and/or duplicated sectors of an image.")
|
|
|
|
|
{
|
|
|
|
|
Options = new OptionSet
|
|
|
|
|
{
|
|
|
|
|
$"{MainClass.AssemblyTitle} {MainClass.AssemblyVersion?.InformationalVersion}",
|
|
|
|
|
$"{MainClass.AssemblyCopyright}",
|
|
|
|
|
"",
|
|
|
|
|
$"usage: DiscImageChef {Name} [OPTIONS] imagefile",
|
|
|
|
|
"",
|
|
|
|
|
Help,
|
|
|
|
|
{
|
|
|
|
|
"duplicated-sectors|p",
|
|
|
|
|
"Calculates how many sectors are duplicated (have same exact data in user area).",
|
|
|
|
|
b => duplicatedSectors = b != null
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"separated-tracks|t", "Calculates entropy for each track separately.",
|
|
|
|
|
b => separatedTracks = b != null
|
|
|
|
|
},
|
|
|
|
|
{"whole-disc|w", "Calculates entropy for the whole disc.", b => wholeDisc = b != null},
|
|
|
|
|
{"help|h|?", "Show this message and exit.", v => showHelp = v != null}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override int Invoke(IEnumerable<string> arguments)
|
2015-05-19 06:04:21 +01:00
|
|
|
{
|
2019-01-05 16:59:23 +00:00
|
|
|
List<string> extra = Options.Parse(arguments);
|
|
|
|
|
|
|
|
|
|
if(showHelp)
|
|
|
|
|
{
|
|
|
|
|
Options.WriteOptionDescriptions(CommandSet.Out);
|
2019-01-05 19:50:56 +00:00
|
|
|
return (int)ErrorNumber.HelpRequested;
|
2019-01-05 16:59:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
MainClass.PrintCopyright();
|
|
|
|
|
if(MainClass.Debug) DicConsole.DebugWriteLineEvent += System.Console.Error.WriteLine;
|
|
|
|
|
if(MainClass.Verbose) DicConsole.VerboseWriteLineEvent += System.Console.WriteLine;
|
2019-01-05 20:21:57 +00:00
|
|
|
Statistics.AddCommand("entropy");
|
2019-01-05 16:59:23 +00:00
|
|
|
|
|
|
|
|
if(extra.Count > 1)
|
|
|
|
|
{
|
|
|
|
|
DicConsole.ErrorWriteLine("Too many arguments.");
|
2019-01-05 19:50:56 +00:00
|
|
|
return (int)ErrorNumber.UnexpectedArgumentCount;
|
2019-01-05 16:59:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(extra.Count == 0)
|
|
|
|
|
{
|
|
|
|
|
DicConsole.ErrorWriteLine("Missing input image.");
|
2019-01-05 19:50:56 +00:00
|
|
|
return (int)ErrorNumber.MissingArgument;
|
2019-01-05 16:59:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
inputFile = extra[0];
|
|
|
|
|
|
|
|
|
|
DicConsole.DebugWriteLine("Entropy command", "--debug={0}", MainClass.Debug);
|
|
|
|
|
DicConsole.DebugWriteLine("Entropy command", "--duplicated-sectors={0}", duplicatedSectors);
|
|
|
|
|
DicConsole.DebugWriteLine("Entropy command", "--input={0}", inputFile);
|
|
|
|
|
DicConsole.DebugWriteLine("Entropy command", "--separated-tracks={0}", separatedTracks);
|
|
|
|
|
DicConsole.DebugWriteLine("Entropy command", "--verbose={0}", MainClass.Verbose);
|
|
|
|
|
DicConsole.DebugWriteLine("Entropy command", "--whole-disc={0}", wholeDisc);
|
2015-05-19 06:04:21 +01:00
|
|
|
|
2016-09-05 17:37:31 +01:00
|
|
|
FiltersList filtersList = new FiltersList();
|
2019-01-05 16:59:23 +00:00
|
|
|
IFilter inputFilter = filtersList.GetFilter(inputFile);
|
2016-09-05 17:37:31 +01:00
|
|
|
|
|
|
|
|
if(inputFilter == null)
|
2016-02-04 16:51:03 +00:00
|
|
|
{
|
2016-09-05 17:37:31 +01:00
|
|
|
DicConsole.ErrorWriteLine("Cannot open specified file.");
|
2019-01-05 19:50:56 +00:00
|
|
|
return (int)ErrorNumber.CannotOpenFile;
|
2016-02-04 16:51:03 +00:00
|
|
|
}
|
|
|
|
|
|
2017-12-26 06:05:12 +00:00
|
|
|
IMediaImage inputFormat = ImageFormat.Detect(inputFilter);
|
2015-05-19 06:04:21 +01:00
|
|
|
|
2016-04-19 02:11:47 +01:00
|
|
|
if(inputFormat == null)
|
2015-05-19 06:04:21 +01:00
|
|
|
{
|
2015-10-18 22:04:03 +01:00
|
|
|
DicConsole.ErrorWriteLine("Unable to recognize image format, not checksumming");
|
2019-01-05 19:50:56 +00:00
|
|
|
return (int)ErrorNumber.UnrecognizedFormat;
|
2015-05-19 06:04:21 +01:00
|
|
|
}
|
|
|
|
|
|
2017-12-28 19:56:36 +00:00
|
|
|
inputFormat.Open(inputFilter);
|
2019-01-05 16:59:23 +00:00
|
|
|
Statistics.AddMediaFormat(inputFormat.Format);
|
|
|
|
|
Statistics.AddMedia(inputFormat.Info.MediaType, false);
|
|
|
|
|
Statistics.AddFilter(inputFilter.Name);
|
2015-05-19 06:04:21 +01:00
|
|
|
|
2019-01-05 16:59:23 +00:00
|
|
|
Entropy entropyCalculator = new Entropy(MainClass.Debug, MainClass.Verbose, inputFormat);
|
2018-10-13 14:39:26 +01:00
|
|
|
entropyCalculator.InitProgressEvent += Progress.InitProgress;
|
|
|
|
|
entropyCalculator.InitProgress2Event += Progress.InitProgress2;
|
|
|
|
|
entropyCalculator.UpdateProgressEvent += Progress.UpdateProgress;
|
|
|
|
|
entropyCalculator.UpdateProgress2Event += Progress.UpdateProgress2;
|
|
|
|
|
entropyCalculator.EndProgressEvent += Progress.EndProgress;
|
|
|
|
|
entropyCalculator.EndProgress2Event += Progress.EndProgress2;
|
2015-05-19 06:04:21 +01:00
|
|
|
|
2019-01-05 16:59:23 +00:00
|
|
|
if(separatedTracks)
|
2017-12-21 06:06:19 +00:00
|
|
|
{
|
2019-01-05 16:59:23 +00:00
|
|
|
EntropyResults[] tracksEntropy = entropyCalculator.CalculateTracksEntropy(duplicatedSectors);
|
2018-10-13 14:39:26 +01:00
|
|
|
foreach(EntropyResults trackEntropy in tracksEntropy)
|
2017-12-21 06:06:19 +00:00
|
|
|
{
|
2018-10-13 14:39:26 +01:00
|
|
|
DicConsole.WriteLine("Entropy for track {0} is {1:F4}.", trackEntropy.Track, trackEntropy.Entropy);
|
|
|
|
|
if(trackEntropy.UniqueSectors != null)
|
2018-10-14 19:10:30 +01:00
|
|
|
DicConsole.WriteLine("Track {0} has {1} unique sectors ({2:P3})", trackEntropy.Track,
|
2018-10-13 14:39:26 +01:00
|
|
|
trackEntropy.UniqueSectors,
|
|
|
|
|
(double)trackEntropy.UniqueSectors / (double)trackEntropy.Sectors);
|
2015-05-19 06:04:21 +01:00
|
|
|
}
|
2017-12-21 06:06:19 +00:00
|
|
|
}
|
|
|
|
|
|
2019-03-11 19:22:26 +00:00
|
|
|
if(!wholeDisc) return (int)ErrorNumber.NoError;
|
2015-05-19 06:04:21 +01:00
|
|
|
|
2019-01-05 16:59:23 +00:00
|
|
|
EntropyResults entropy = entropyCalculator.CalculateMediaEntropy(duplicatedSectors);
|
2015-05-19 06:04:21 +01:00
|
|
|
|
2018-10-13 14:39:26 +01:00
|
|
|
DicConsole.WriteLine("Entropy for disk is {0:F4}.", entropy.Entropy);
|
|
|
|
|
if(entropy.UniqueSectors != null)
|
|
|
|
|
DicConsole.WriteLine("Disk has {0} unique sectors ({1:P3})", entropy.UniqueSectors,
|
|
|
|
|
(double)entropy.UniqueSectors / (double)entropy.Sectors);
|
2015-05-19 06:04:21 +01:00
|
|
|
|
2019-01-05 19:50:56 +00:00
|
|
|
return (int)ErrorNumber.NoError;
|
2015-05-19 06:04:21 +01:00
|
|
|
}
|
|
|
|
|
}
|
2017-12-19 20:33:03 +00:00
|
|
|
}
|