Files
Aaru/Aaru.Core/Entropy.cs

236 lines
9.1 KiB
C#
Raw Normal View History

2018-10-13 14:39:26 +01:00
// /***************************************************************************
2020-02-27 12:31:25 +00:00
// Aaru Data Preservation Suite
2018-10-13 14:39:26 +01:00
// ----------------------------------------------------------------------------
//
// Filename : Entropy.cs
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// Component : Core algorithms.
//
// --[ Description ] ----------------------------------------------------------
//
// Calculates the entropy of an image
//
// --[ 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/>.
//
// ----------------------------------------------------------------------------
2020-12-31 23:08:23 +00:00
// Copyright © 2011-2021 Natalia Portillo
2018-10-13 14:39:26 +01:00
// ****************************************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
2020-02-27 00:33:26 +00:00
using Aaru.Checksums;
2020-12-31 19:28:47 +00:00
using Aaru.CommonTypes;
using Aaru.CommonTypes.Enums;
2020-02-27 00:33:26 +00:00
using Aaru.CommonTypes.Interfaces;
using Aaru.CommonTypes.Structs;
using Aaru.Console;
2018-10-13 14:39:26 +01:00
2020-02-27 00:33:26 +00:00
namespace Aaru.Core
2018-10-13 14:39:26 +01:00
{
2021-08-17 21:23:10 +01:00
/// <summary>Media image entropy operations</summary>
2020-07-22 13:20:25 +01:00
public sealed class Entropy
2018-10-13 14:39:26 +01:00
{
2020-07-20 21:11:32 +01:00
readonly bool _debug;
readonly IMediaImage _inputFormat;
2018-10-13 14:39:26 +01:00
2021-08-17 21:23:10 +01:00
/// <summary>Initializes an instance with the specified parameters</summary>
2021-08-17 13:56:05 +01:00
/// <param name="debug">Debug enabled</param>
/// <param name="inputFormat">Media image</param>
2020-07-20 07:47:12 +01:00
public Entropy(bool debug, IMediaImage inputFormat)
2018-10-13 14:39:26 +01:00
{
2020-07-20 21:11:32 +01:00
_debug = debug;
_inputFormat = inputFormat;
2018-10-13 14:39:26 +01:00
}
2021-08-17 13:56:05 +01:00
/// <summary>Event raised when a progress bar is needed</summary>
2021-08-17 21:23:10 +01:00
public event InitProgressHandler InitProgressEvent;
2021-08-17 13:56:05 +01:00
/// <summary>Event raised to update the values of a determinate progress bar</summary>
2018-10-13 14:39:26 +01:00
public event UpdateProgressHandler UpdateProgressEvent;
2021-08-17 13:56:05 +01:00
/// <summary>Event raised when the progress bar is not longer needed</summary>
2021-08-17 21:23:10 +01:00
public event EndProgressHandler EndProgressEvent;
2021-08-17 13:56:05 +01:00
/// <summary>Event raised when a progress bar is needed</summary>
2021-08-17 21:23:10 +01:00
public event InitProgressHandler InitProgress2Event;
2021-08-17 13:56:05 +01:00
/// <summary>Event raised to update the values of a determinate progress bar</summary>
2018-10-13 14:39:26 +01:00
public event UpdateProgressHandler UpdateProgress2Event;
2021-08-17 13:56:05 +01:00
/// <summary>Event raised when the progress bar is not longer needed</summary>
2021-08-17 21:23:10 +01:00
public event EndProgressHandler EndProgress2Event;
2018-10-13 14:39:26 +01:00
2021-08-17 21:23:10 +01:00
/// <summary>Calculates the tracks entropy</summary>
2021-08-17 13:56:05 +01:00
/// <param name="duplicatedSectors">Checks for duplicated sectors</param>
/// <returns>Calculated entropy</returns>
2018-10-13 14:39:26 +01:00
public EntropyResults[] CalculateTracksEntropy(bool duplicatedSectors)
{
List<EntropyResults> entropyResults = new();
2018-10-13 14:39:26 +01:00
2020-07-20 21:11:32 +01:00
if(!(_inputFormat is IOpticalMediaImage opticalMediaImage))
{
2020-02-27 23:48:41 +00:00
AaruConsole.ErrorWriteLine("The selected image does not support tracks.");
2020-02-29 18:03:35 +00:00
2020-07-22 13:20:25 +01:00
return entropyResults.ToArray();
}
2018-10-13 14:39:26 +01:00
try
{
List<Track> inputTracks = opticalMediaImage.Tracks;
2018-10-13 14:39:26 +01:00
InitProgressEvent?.Invoke();
foreach(Track currentTrack in inputTracks)
{
2020-02-29 18:03:35 +00:00
var trackEntropy = new EntropyResults
{
2021-09-14 21:18:28 +01:00
Track = currentTrack.Sequence,
2020-07-20 04:34:16 +01:00
Entropy = 0
2020-02-29 18:03:35 +00:00
};
UpdateProgressEvent?.
2021-09-14 21:18:28 +01:00
Invoke($"Entropying track {currentTrack.Sequence} of {inputTracks.Max(t => t.Sequence)}",
currentTrack.Sequence, inputTracks.Max(t => t.Sequence));
2018-10-13 14:39:26 +01:00
ulong[] entTable = new ulong[256];
ulong trackSize = 0;
List<string> uniqueSectorsPerTrack = new();
2018-10-13 14:39:26 +01:00
2021-09-14 21:18:28 +01:00
trackEntropy.Sectors = currentTrack.EndSector - currentTrack.StartSector + 1;
2020-02-29 18:03:35 +00:00
2021-09-14 21:18:28 +01:00
AaruConsole.VerboseWriteLine("Track {0} has {1} sectors", currentTrack.Sequence,
2020-02-29 18:03:35 +00:00
trackEntropy.Sectors);
2018-10-13 14:39:26 +01:00
InitProgress2Event?.Invoke();
for(ulong i = 0; i < trackEntropy.Sectors; i++)
2018-10-13 14:39:26 +01:00
{
2021-09-14 21:18:28 +01:00
UpdateProgress2Event?.Invoke($"Entropying sector {i + 1} of track {currentTrack.Sequence}",
(long)(i + 1), (long)currentTrack.EndSector);
2020-02-29 18:03:35 +00:00
ErrorNumber errno = opticalMediaImage.ReadSector(i, currentTrack.Sequence, out byte[] sector);
if(errno != ErrorNumber.NoError)
{
AaruConsole.ErrorWriteLine($"Error {errno} while reading sector {i}, continuing...");
continue;
}
2018-10-13 14:39:26 +01:00
if(duplicatedSectors)
{
string sectorHash = Sha1Context.Data(sector, out _);
2020-02-29 18:03:35 +00:00
if(!uniqueSectorsPerTrack.Contains(sectorHash))
uniqueSectorsPerTrack.Add(sectorHash);
2018-10-13 14:39:26 +01:00
}
2020-02-29 18:03:35 +00:00
foreach(byte b in sector)
entTable[b]++;
2018-10-13 14:39:26 +01:00
trackSize += (ulong)sector.LongLength;
}
EndProgress2Event?.Invoke();
trackEntropy.Entropy += entTable.Select(l => l / (double)trackSize).
2020-02-29 18:03:35 +00:00
Select(frequency => -(frequency * Math.Log(frequency, 2))).Sum();
2018-10-13 14:39:26 +01:00
2020-02-29 18:03:35 +00:00
if(duplicatedSectors)
trackEntropy.UniqueSectors = uniqueSectorsPerTrack.Count;
2018-10-13 14:39:26 +01:00
2020-07-22 13:20:25 +01:00
entropyResults.Add(trackEntropy);
2018-10-13 14:39:26 +01:00
}
EndProgressEvent?.Invoke();
}
catch(Exception ex)
{
2020-07-20 21:11:32 +01:00
if(_debug)
2020-02-29 18:03:35 +00:00
AaruConsole.DebugWriteLine("Could not get tracks because {0}", ex.Message);
else
AaruConsole.ErrorWriteLine("Unable to get separate tracks, not calculating their entropy");
2018-10-13 14:39:26 +01:00
}
2020-07-22 13:20:25 +01:00
return entropyResults.ToArray();
2018-10-13 14:39:26 +01:00
}
2021-08-17 21:23:10 +01:00
/// <summary>Calculates the media entropy</summary>
2021-08-17 13:56:05 +01:00
/// <param name="duplicatedSectors">Checks for duplicated sectors</param>
/// <returns>Calculated entropy</returns>
2018-10-13 14:39:26 +01:00
public EntropyResults CalculateMediaEntropy(bool duplicatedSectors)
{
2020-02-29 18:03:35 +00:00
var entropy = new EntropyResults
{
Entropy = 0
};
ulong[] entTable = new ulong[256];
ulong diskSize = 0;
List<string> uniqueSectors = new();
2018-10-13 14:39:26 +01:00
2020-07-20 21:11:32 +01:00
entropy.Sectors = _inputFormat.Info.Sectors;
2020-02-27 23:48:41 +00:00
AaruConsole.WriteLine("Sectors {0}", entropy.Sectors);
2018-10-13 14:39:26 +01:00
InitProgressEvent?.Invoke();
2020-02-29 18:03:35 +00:00
2018-10-13 14:39:26 +01:00
for(ulong i = 0; i < entropy.Sectors; i++)
{
UpdateProgressEvent?.Invoke($"Entropying sector {i + 1}", (long)(i + 1), (long)entropy.Sectors);
ErrorNumber errno = _inputFormat.ReadSector(i, out byte[] sector);
if(errno != ErrorNumber.NoError)
{
AaruConsole.ErrorWriteLine($"Error {errno} while reading sector {i}, continuing...");
continue;
}
2018-10-13 14:39:26 +01:00
if(duplicatedSectors)
{
string sectorHash = Sha1Context.Data(sector, out _);
2020-02-29 18:03:35 +00:00
if(!uniqueSectors.Contains(sectorHash))
uniqueSectors.Add(sectorHash);
2018-10-13 14:39:26 +01:00
}
2020-02-29 18:03:35 +00:00
foreach(byte b in sector)
entTable[b]++;
2018-10-13 14:39:26 +01:00
diskSize += (ulong)sector.LongLength;
}
EndProgressEvent?.Invoke();
entropy.Entropy += entTable.Select(l => l / (double)diskSize).
2020-02-29 18:03:35 +00:00
Select(frequency => -(frequency * Math.Log(frequency, 2))).Sum();
2018-10-13 14:39:26 +01:00
2020-02-29 18:03:35 +00:00
if(duplicatedSectors)
entropy.UniqueSectors = uniqueSectors.Count;
2018-10-13 14:39:26 +01:00
return entropy;
}
}
2021-08-17 21:23:10 +01:00
/// <summary>Entropy results</summary>
2018-10-13 14:39:26 +01:00
public struct EntropyResults
{
2021-08-17 21:23:10 +01:00
/// <summary>Track number, if applicable</summary>
public uint Track;
/// <summary>Entropy</summary>
2018-10-13 14:39:26 +01:00
public double Entropy;
2021-08-17 21:23:10 +01:00
/// <summary>Number of unique sectors</summary>
public int? UniqueSectors;
/// <summary>Number of total sectors</summary>
public ulong Sectors;
2018-10-13 14:39:26 +01:00
}
}