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/>.
|
|
|
|
|
//
|
|
|
|
|
// ----------------------------------------------------------------------------
|
2022-12-03 16:07:10 +00:00
|
|
|
// Copyright © 2011-2023 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;
|
2021-09-19 21:16:47 +01:00
|
|
|
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
|
|
|
|
2022-11-15 15:58:43 +00:00
|
|
|
namespace Aaru.Core;
|
|
|
|
|
|
2021-11-13 19:27:46 +00:00
|
|
|
/// <summary>Media image entropy operations</summary>
|
|
|
|
|
public sealed class Entropy
|
2018-10-13 14:39:26 +01:00
|
|
|
{
|
2021-11-13 19:27:46 +00:00
|
|
|
readonly bool _debug;
|
|
|
|
|
readonly IBaseImage _inputFormat;
|
|
|
|
|
|
|
|
|
|
/// <summary>Initializes an instance with the specified parameters</summary>
|
|
|
|
|
/// <param name="debug">Debug enabled</param>
|
|
|
|
|
/// <param name="inputFormat">Media image</param>
|
|
|
|
|
public Entropy(bool debug, IBaseImage inputFormat)
|
|
|
|
|
{
|
|
|
|
|
_debug = debug;
|
|
|
|
|
_inputFormat = inputFormat;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>Event raised when a progress bar is needed</summary>
|
|
|
|
|
public event InitProgressHandler InitProgressEvent;
|
2023-10-03 22:57:50 +01:00
|
|
|
|
2021-11-13 19:27:46 +00:00
|
|
|
/// <summary>Event raised to update the values of a determinate progress bar</summary>
|
|
|
|
|
public event UpdateProgressHandler UpdateProgressEvent;
|
2023-10-03 22:57:50 +01:00
|
|
|
|
2021-11-13 19:27:46 +00:00
|
|
|
/// <summary>Event raised when the progress bar is not longer needed</summary>
|
|
|
|
|
public event EndProgressHandler EndProgressEvent;
|
2023-10-03 22:57:50 +01:00
|
|
|
|
2021-11-13 19:27:46 +00:00
|
|
|
/// <summary>Event raised when a progress bar is needed</summary>
|
|
|
|
|
public event InitProgressHandler InitProgress2Event;
|
2023-10-03 22:57:50 +01:00
|
|
|
|
2021-11-13 19:27:46 +00:00
|
|
|
/// <summary>Event raised to update the values of a determinate progress bar</summary>
|
|
|
|
|
public event UpdateProgressHandler UpdateProgress2Event;
|
2023-10-03 22:57:50 +01:00
|
|
|
|
2021-11-13 19:27:46 +00:00
|
|
|
/// <summary>Event raised when the progress bar is not longer needed</summary>
|
|
|
|
|
public event EndProgressHandler EndProgress2Event;
|
|
|
|
|
|
|
|
|
|
/// <summary>Calculates the tracks entropy</summary>
|
|
|
|
|
/// <param name="duplicatedSectors">Checks for duplicated sectors</param>
|
|
|
|
|
/// <returns>Calculated entropy</returns>
|
|
|
|
|
public EntropyResults[] CalculateTracksEntropy(bool duplicatedSectors)
|
2018-10-13 14:39:26 +01:00
|
|
|
{
|
2021-11-13 19:27:46 +00:00
|
|
|
List<EntropyResults> entropyResults = new();
|
2018-10-13 14:39:26 +01:00
|
|
|
|
2021-11-13 19:27:46 +00:00
|
|
|
if(_inputFormat is not IOpticalMediaImage opticalMediaImage)
|
2018-10-13 14:39:26 +01:00
|
|
|
{
|
2022-11-23 16:06:46 +00:00
|
|
|
AaruConsole.ErrorWriteLine(Localization.Core.The_selected_image_does_not_support_tracks);
|
2021-11-13 19:27:46 +00:00
|
|
|
|
|
|
|
|
return entropyResults.ToArray();
|
2018-10-13 14:39:26 +01:00
|
|
|
}
|
|
|
|
|
|
2021-11-13 19:27:46 +00:00
|
|
|
try
|
2018-10-13 14:39:26 +01:00
|
|
|
{
|
2021-11-13 19:27:46 +00:00
|
|
|
List<Track> inputTracks = opticalMediaImage.Tracks;
|
|
|
|
|
|
|
|
|
|
InitProgressEvent?.Invoke();
|
2018-10-13 14:39:26 +01:00
|
|
|
|
2021-11-13 19:27:46 +00:00
|
|
|
foreach(Track currentTrack in inputTracks)
|
2019-01-20 20:11:10 +00:00
|
|
|
{
|
2021-11-13 19:27:46 +00:00
|
|
|
var trackEntropy = new EntropyResults
|
|
|
|
|
{
|
|
|
|
|
Track = currentTrack.Sequence,
|
|
|
|
|
Entropy = 0
|
|
|
|
|
};
|
2020-02-29 18:03:35 +00:00
|
|
|
|
2021-11-13 19:27:46 +00:00
|
|
|
UpdateProgressEvent?.
|
2023-10-04 17:34:40 +01:00
|
|
|
Invoke(string.Format(Localization.Core.Entropying_track_0_of_1, currentTrack.Sequence, inputTracks.Max(t => t.Sequence)),
|
|
|
|
|
currentTrack.Sequence, inputTracks.Max(t => t.Sequence));
|
2019-01-20 20:11:10 +00:00
|
|
|
|
2023-10-03 22:57:50 +01:00
|
|
|
var entTable = new ulong[256];
|
2021-11-13 19:27:46 +00:00
|
|
|
ulong trackSize = 0;
|
|
|
|
|
List<string> uniqueSectorsPerTrack = new();
|
|
|
|
|
|
|
|
|
|
trackEntropy.Sectors = currentTrack.EndSector - currentTrack.StartSector + 1;
|
|
|
|
|
|
2022-11-23 16:06:46 +00:00
|
|
|
AaruConsole.VerboseWriteLine(Localization.Core.Track_0_has_1_sectors, currentTrack.Sequence,
|
|
|
|
|
trackEntropy.Sectors);
|
2018-10-13 14:39:26 +01:00
|
|
|
|
2021-11-13 19:27:46 +00:00
|
|
|
InitProgress2Event?.Invoke();
|
2018-10-13 14:39:26 +01:00
|
|
|
|
2021-11-13 19:27:46 +00:00
|
|
|
for(ulong i = 0; i < trackEntropy.Sectors; i++)
|
2018-10-13 14:39:26 +01:00
|
|
|
{
|
2022-11-23 16:06:46 +00:00
|
|
|
UpdateProgress2Event?.
|
2023-10-04 17:34:40 +01:00
|
|
|
Invoke(string.Format(Localization.Core.Entropying_sector_0_of_track_1, i + 1, currentTrack.Sequence),
|
|
|
|
|
(long)(i + 1), (long)currentTrack.EndSector);
|
2021-11-13 19:27:46 +00:00
|
|
|
|
|
|
|
|
ErrorNumber errno = opticalMediaImage.ReadSector(i, currentTrack.Sequence, out byte[] sector);
|
|
|
|
|
|
|
|
|
|
if(errno != ErrorNumber.NoError)
|
2020-02-29 18:03:35 +00:00
|
|
|
{
|
2022-11-23 16:06:46 +00:00
|
|
|
AaruConsole.
|
|
|
|
|
ErrorWriteLine(string.Format(Localization.Core.Error_0_while_reading_sector_1_continuing,
|
|
|
|
|
errno, i));
|
2020-02-29 18:03:35 +00:00
|
|
|
|
2021-11-13 19:27:46 +00:00
|
|
|
continue;
|
|
|
|
|
}
|
2018-10-13 14:39:26 +01:00
|
|
|
|
2021-11-13 19:27:46 +00:00
|
|
|
if(duplicatedSectors)
|
|
|
|
|
{
|
|
|
|
|
string sectorHash = Sha1Context.Data(sector, out _);
|
2018-10-13 14:39:26 +01:00
|
|
|
|
2021-11-13 19:27:46 +00:00
|
|
|
if(!uniqueSectorsPerTrack.Contains(sectorHash))
|
|
|
|
|
uniqueSectorsPerTrack.Add(sectorHash);
|
|
|
|
|
}
|
2020-02-29 18:03:35 +00:00
|
|
|
|
2021-11-13 19:27:46 +00:00
|
|
|
foreach(byte b in sector)
|
|
|
|
|
entTable[b]++;
|
2018-10-13 14:39:26 +01:00
|
|
|
|
2021-11-13 19:27:46 +00:00
|
|
|
trackSize += (ulong)sector.LongLength;
|
|
|
|
|
}
|
2018-10-13 14:39:26 +01:00
|
|
|
|
2021-11-13 19:27:46 +00:00
|
|
|
EndProgress2Event?.Invoke();
|
2020-02-29 18:03:35 +00:00
|
|
|
|
2021-11-13 19:27:46 +00:00
|
|
|
trackEntropy.Entropy += entTable.Select(l => l / (double)trackSize).
|
2023-10-04 17:34:40 +01:00
|
|
|
Select(frequency => -(frequency * Math.Log(frequency, 2))).
|
|
|
|
|
Sum();
|
2021-09-21 01:38:54 +01:00
|
|
|
|
2021-11-13 19:27:46 +00:00
|
|
|
if(duplicatedSectors)
|
|
|
|
|
trackEntropy.UniqueSectors = uniqueSectorsPerTrack.Count;
|
2021-09-21 01:38:54 +01:00
|
|
|
|
2021-11-13 19:27:46 +00:00
|
|
|
entropyResults.Add(trackEntropy);
|
|
|
|
|
}
|
2018-10-13 14:39:26 +01:00
|
|
|
|
2021-11-13 19:27:46 +00:00
|
|
|
EndProgressEvent?.Invoke();
|
|
|
|
|
}
|
|
|
|
|
catch(Exception ex)
|
|
|
|
|
{
|
|
|
|
|
if(_debug)
|
2022-11-23 16:06:46 +00:00
|
|
|
AaruConsole.DebugWriteLine(Localization.Core.Could_not_get_tracks_because_0, ex.Message);
|
2021-11-13 19:27:46 +00:00
|
|
|
else
|
2023-10-03 22:57:50 +01:00
|
|
|
{
|
2022-11-23 16:06:46 +00:00
|
|
|
AaruConsole.ErrorWriteLine(Localization.Core.
|
|
|
|
|
Unable_to_get_separate_tracks_not_calculating_their_entropy);
|
2023-10-03 22:57:50 +01:00
|
|
|
}
|
2021-11-13 19:27:46 +00:00
|
|
|
}
|
2020-02-29 18:03:35 +00:00
|
|
|
|
2021-11-13 19:27:46 +00:00
|
|
|
return entropyResults.ToArray();
|
|
|
|
|
}
|
2018-10-13 14:39:26 +01:00
|
|
|
|
2021-11-13 19:27:46 +00:00
|
|
|
/// <summary>Calculates the media entropy for block addressable media</summary>
|
|
|
|
|
/// <param name="duplicatedSectors">Checks for duplicated sectors</param>
|
|
|
|
|
/// <returns>Calculated entropy</returns>
|
|
|
|
|
public EntropyResults CalculateMediaEntropy(bool duplicatedSectors)
|
|
|
|
|
{
|
|
|
|
|
var entropy = new EntropyResults
|
|
|
|
|
{
|
|
|
|
|
Entropy = 0
|
|
|
|
|
};
|
2018-10-13 14:39:26 +01:00
|
|
|
|
2021-11-13 19:27:46 +00:00
|
|
|
if(_inputFormat is not IMediaImage mediaImage)
|
|
|
|
|
return entropy;
|
2018-10-13 14:39:26 +01:00
|
|
|
|
2023-10-03 22:57:50 +01:00
|
|
|
var entTable = new ulong[256];
|
2021-11-13 19:27:46 +00:00
|
|
|
ulong diskSize = 0;
|
|
|
|
|
List<string> uniqueSectors = new();
|
2018-10-13 14:39:26 +01:00
|
|
|
|
2021-11-13 19:27:46 +00:00
|
|
|
entropy.Sectors = mediaImage.Info.Sectors;
|
2022-11-23 16:06:46 +00:00
|
|
|
AaruConsole.WriteLine(Localization.Core.Sectors_0, entropy.Sectors);
|
2021-11-13 19:27:46 +00:00
|
|
|
InitProgressEvent?.Invoke();
|
2018-10-13 14:39:26 +01:00
|
|
|
|
2021-11-13 19:27:46 +00:00
|
|
|
for(ulong i = 0; i < entropy.Sectors; i++)
|
|
|
|
|
{
|
2022-11-23 16:06:46 +00:00
|
|
|
UpdateProgressEvent?.Invoke(string.Format(Localization.Core.Entropying_sector_0, i + 1), (long)(i + 1),
|
|
|
|
|
(long)entropy.Sectors);
|
|
|
|
|
|
2021-11-13 19:27:46 +00:00
|
|
|
ErrorNumber errno = mediaImage.ReadSector(i, out byte[] sector);
|
2018-10-13 14:39:26 +01:00
|
|
|
|
2021-11-13 19:27:46 +00:00
|
|
|
if(errno != ErrorNumber.NoError)
|
|
|
|
|
{
|
2022-11-23 16:06:46 +00:00
|
|
|
AaruConsole.ErrorWriteLine(string.Format(Localization.Core.Error_0_while_reading_sector_1_continuing,
|
|
|
|
|
errno, i));
|
2018-10-13 14:39:26 +01:00
|
|
|
|
2021-11-13 19:27:46 +00:00
|
|
|
continue;
|
2018-10-13 14:39:26 +01:00
|
|
|
}
|
2021-11-13 19:27:46 +00:00
|
|
|
|
|
|
|
|
if(duplicatedSectors)
|
2018-10-13 14:39:26 +01:00
|
|
|
{
|
2021-11-13 19:27:46 +00:00
|
|
|
string sectorHash = Sha1Context.Data(sector, out _);
|
|
|
|
|
|
|
|
|
|
if(!uniqueSectors.Contains(sectorHash))
|
|
|
|
|
uniqueSectors.Add(sectorHash);
|
2018-10-13 14:39:26 +01:00
|
|
|
}
|
|
|
|
|
|
2021-11-13 19:27:46 +00:00
|
|
|
foreach(byte b in sector)
|
|
|
|
|
entTable[b]++;
|
|
|
|
|
|
|
|
|
|
diskSize += (ulong)sector.LongLength;
|
2018-10-13 14:39:26 +01:00
|
|
|
}
|
|
|
|
|
|
2021-11-13 19:27:46 +00:00
|
|
|
EndProgressEvent?.Invoke();
|
2020-02-29 18:03:35 +00:00
|
|
|
|
2021-11-13 19:27:46 +00:00
|
|
|
entropy.Entropy += entTable.Select(l => l / (double)diskSize).
|
2023-10-04 17:34:40 +01:00
|
|
|
Select(frequency => -(frequency * Math.Log(frequency, 2))).
|
|
|
|
|
Sum();
|
2018-10-13 14:39:26 +01:00
|
|
|
|
2021-11-13 19:27:46 +00:00
|
|
|
if(duplicatedSectors)
|
|
|
|
|
entropy.UniqueSectors = uniqueSectors.Count;
|
2020-02-29 18:03:35 +00:00
|
|
|
|
2021-11-13 19:27:46 +00:00
|
|
|
return entropy;
|
|
|
|
|
}
|
2021-09-19 21:16:47 +01:00
|
|
|
|
2021-11-13 19:27:46 +00:00
|
|
|
/// <summary>Calculates the media entropy for byte addressable media</summary>
|
|
|
|
|
/// <returns>Calculated entropy</returns>
|
|
|
|
|
public EntropyResults CalculateLinearMediaEntropy()
|
|
|
|
|
{
|
|
|
|
|
var entropy = new EntropyResults
|
|
|
|
|
{
|
|
|
|
|
Entropy = 0
|
|
|
|
|
};
|
2021-09-19 21:16:47 +01:00
|
|
|
|
2021-11-13 19:27:46 +00:00
|
|
|
if(_inputFormat is not IByteAddressableImage byteAddressableImage)
|
|
|
|
|
return entropy;
|
2018-10-13 14:39:26 +01:00
|
|
|
|
2023-10-03 22:57:50 +01:00
|
|
|
var entTable = new ulong[256];
|
|
|
|
|
var data = new byte[byteAddressableImage.Info.Sectors];
|
2020-02-29 18:03:35 +00:00
|
|
|
|
2021-11-13 19:27:46 +00:00
|
|
|
entropy.Sectors = _inputFormat.Info.Sectors;
|
2022-11-23 16:06:46 +00:00
|
|
|
AaruConsole.WriteLine(Localization.Core._0_bytes, entropy.Sectors);
|
2021-11-13 19:27:46 +00:00
|
|
|
InitProgressEvent?.Invoke();
|
2018-10-13 14:39:26 +01:00
|
|
|
|
2021-11-13 19:27:46 +00:00
|
|
|
ErrorNumber errno = byteAddressableImage.ReadBytes(data, 0, data.Length, out int bytesRead);
|
2018-10-13 14:39:26 +01:00
|
|
|
|
2021-11-13 19:27:46 +00:00
|
|
|
if(errno != ErrorNumber.NoError)
|
|
|
|
|
{
|
2022-11-23 16:06:46 +00:00
|
|
|
AaruConsole.ErrorWriteLine(string.Format(Localization.Core.Error_0_while_reading_data__not_continuing,
|
|
|
|
|
errno));
|
2018-10-13 14:39:26 +01:00
|
|
|
|
2021-11-13 19:27:46 +00:00
|
|
|
return entropy;
|
|
|
|
|
}
|
2018-10-13 14:39:26 +01:00
|
|
|
|
2021-11-13 19:27:46 +00:00
|
|
|
if(bytesRead != data.Length)
|
|
|
|
|
{
|
2023-10-03 22:57:50 +01:00
|
|
|
var tmp = new byte[bytesRead];
|
2021-11-13 19:27:46 +00:00
|
|
|
Array.Copy(data, 0, tmp, 0, bytesRead);
|
|
|
|
|
data = tmp;
|
|
|
|
|
}
|
2018-10-13 14:39:26 +01:00
|
|
|
|
2021-11-13 19:27:46 +00:00
|
|
|
foreach(byte b in data)
|
|
|
|
|
entTable[b]++;
|
2018-10-13 14:39:26 +01:00
|
|
|
|
2021-11-13 19:27:46 +00:00
|
|
|
EndProgressEvent?.Invoke();
|
2018-10-13 14:39:26 +01:00
|
|
|
|
2021-11-13 19:27:46 +00:00
|
|
|
entropy.Entropy += entTable.Select(l => l / (double)data.Length).
|
2023-10-04 17:34:40 +01:00
|
|
|
Select(frequency => -(frequency * Math.Log(frequency, 2))).
|
|
|
|
|
Sum();
|
2021-11-13 19:27:46 +00:00
|
|
|
|
|
|
|
|
return entropy;
|
2018-10-13 14:39:26 +01:00
|
|
|
}
|
2021-11-13 19:27:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>Entropy results</summary>
|
|
|
|
|
public struct EntropyResults
|
|
|
|
|
{
|
|
|
|
|
/// <summary>Track number, if applicable</summary>
|
|
|
|
|
public uint Track;
|
|
|
|
|
/// <summary>Entropy</summary>
|
|
|
|
|
public double Entropy;
|
|
|
|
|
/// <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
|
|
|
}
|