Пример #1
0
        public EntropyResults CalculateMediaEntropy(bool duplicatedSectors)
        {
            var entropy = new EntropyResults
            {
                Entropy = 0
            };

            ulong[]       entTable      = new ulong[256];
            ulong         diskSize      = 0;
            List <string> uniqueSectors = new List <string>();

            entropy.Sectors = inputFormat.Info.Sectors;
            AaruConsole.WriteLine("Sectors {0}", entropy.Sectors);
            InitProgressEvent?.Invoke();

            for (ulong i = 0; i < entropy.Sectors; i++)
            {
                UpdateProgressEvent?.Invoke($"Entropying sector {i + 1}", (long)(i + 1), (long)entropy.Sectors);
                byte[] sector = inputFormat.ReadSector(i);

                if (duplicatedSectors)
                {
                    string sectorHash = Sha1Context.Data(sector, out _);

                    if (!uniqueSectors.Contains(sectorHash))
                    {
                        uniqueSectors.Add(sectorHash);
                    }
                }

                foreach (byte b in sector)
                {
                    entTable[b]++;
                }

                diskSize += (ulong)sector.LongLength;
            }

            EndProgressEvent?.Invoke();

            entropy.Entropy += entTable.Select(l => (double)l / (double)diskSize).
                               Select(frequency => - (frequency * Math.Log(frequency, 2))).Sum();

            if (duplicatedSectors)
            {
                entropy.UniqueSectors = uniqueSectors.Count;
            }

            return(entropy);
        }
Пример #2
0
 public void InitProgress()
 {
     InitProgressEvent?.Invoke();
 }
Пример #3
0
        public EntropyResults[] CalculateTracksEntropy(bool duplicatedSectors)
        {
            List <EntropyResults> entropyResultses = new List <EntropyResults>();

            if (!(inputFormat is IOpticalMediaImage opticalMediaImage))
            {
                DicConsole.ErrorWriteLine("The selected image does not support tracks.");
                return(entropyResultses.ToArray());
            }

            try
            {
                List <Track> inputTracks = opticalMediaImage.Tracks;

                InitProgressEvent?.Invoke();

                foreach (Track currentTrack in inputTracks)
                {
                    EntropyResults trackEntropy = new EntropyResults {
                        Track = currentTrack.TrackSequence, Entropy = 0
                    };
                    UpdateProgressEvent
                    ?.Invoke($"Entropying track {currentTrack.TrackSequence} of {inputTracks.Max(t => t.TrackSequence)}",
                             currentTrack.TrackSequence, inputTracks.Max(t => t.TrackSequence));

                    ulong[]       entTable              = new ulong[256];
                    ulong         trackSize             = 0;
                    List <string> uniqueSectorsPerTrack = new List <string>();

                    trackEntropy.Sectors = currentTrack.TrackEndSector - currentTrack.TrackStartSector + 1;
                    DicConsole.VerboseWriteLine("Track {0} has {1} sectors", currentTrack.TrackSequence,
                                                trackEntropy.Sectors);

                    InitProgress2Event?.Invoke();

                    for (ulong i = currentTrack.TrackStartSector; i <= currentTrack.TrackEndSector; i++)
                    {
                        UpdateProgress2Event
                        ?.Invoke($"Entropying sector {i             + 1} of track {currentTrack.TrackSequence}",
                                 (long)(currentTrack.TrackEndSector - (i + 1)),
                                 (long)trackEntropy.Sectors);
                        byte[] sector = opticalMediaImage.ReadSector(i, currentTrack.TrackSequence);

                        if (duplicatedSectors)
                        {
                            string sectorHash = Sha1Context.Data(sector, out _);
                            if (!uniqueSectorsPerTrack.Contains(sectorHash))
                            {
                                uniqueSectorsPerTrack.Add(sectorHash);
                            }
                        }

                        foreach (byte b in sector)
                        {
                            entTable[b]++;
                        }

                        trackSize += (ulong)sector.LongLength;
                    }

                    EndProgress2Event?.Invoke();

                    trackEntropy.Entropy += entTable.Select(l => (double)l / (double)trackSize)
                                            .Select(frequency => - (frequency * Math.Log(frequency, 2))).Sum();

                    if (duplicatedSectors)
                    {
                        trackEntropy.UniqueSectors = uniqueSectorsPerTrack.Count;
                    }

                    entropyResultses.Add(trackEntropy);
                }

                EndProgressEvent?.Invoke();
            }
            catch (Exception ex)
            {
                if (debug)
                {
                    DicConsole.DebugWriteLine("Could not get tracks because {0}", ex.Message);
                }
                else
                {
                    DicConsole.ErrorWriteLine("Unable to get separate tracks, not calculating their entropy");
                }
            }

            return(entropyResultses.ToArray());
        }