Files
Aaru/Aaru.Gui/ViewModels/Windows/ImageChecksumViewModel.cs

617 lines
23 KiB
C#
Raw Normal View History

2020-04-17 21:45:50 +01:00
// /***************************************************************************
// Aaru Data Preservation Suite
// ----------------------------------------------------------------------------
//
// Filename : ImageChecksumViewModel.cs
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// Component : GUI view models.
//
// --[ Description ] ----------------------------------------------------------
//
// View model and code for the image checksum window.
//
// --[ 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/>.
//
// ----------------------------------------------------------------------------
2024-12-19 10:45:18 +00:00
// Copyright © 2011-2025 Natalia Portillo
2020-04-17 21:45:50 +01:00
// ****************************************************************************/
using System;
using System.Collections.ObjectModel;
2022-11-15 01:35:06 +00:00
using System.Diagnostics.CodeAnalysis;
using System.Threading;
2025-08-20 21:19:43 +01:00
using System.Windows.Input;
using Aaru.CommonTypes.Enums;
using Aaru.CommonTypes.Interfaces;
using Aaru.CommonTypes.Structs;
using Aaru.Core;
using Aaru.Gui.Models;
using Aaru.Localization;
using Aaru.Logging;
using Avalonia.Controls;
using Avalonia.Threading;
2025-08-20 21:19:43 +01:00
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
2025-08-20 18:51:05 +01:00
using Sentry;
namespace Aaru.Gui.ViewModels.Windows;
2025-08-20 21:19:43 +01:00
public sealed partial class ImageChecksumViewModel : ViewModelBase
2022-03-06 13:29:38 +00:00
{
// How many sectors to read at once
const uint SECTORS_TO_READ = 256;
const string MODULE_NAME = "Image Checksum ViewModel";
2022-03-06 13:29:38 +00:00
readonly IMediaImage _inputFormat;
readonly Window _view;
2025-08-20 21:19:43 +01:00
[ObservableProperty]
bool _adler32Checked;
[ObservableProperty]
bool _cancel;
[ObservableProperty]
bool _checksumMediaChecked;
[ObservableProperty]
bool _checksumTracksChecked;
[ObservableProperty]
bool _checksumTracksVisible;
[ObservableProperty]
bool _closeCommandEnabled;
[ObservableProperty]
bool _closeCommandVisible;
[ObservableProperty]
bool _crc16Checked;
[ObservableProperty]
bool _crc32Checked;
[ObservableProperty]
bool _crc64Checked;
[ObservableProperty]
bool _fletcher16Checked;
[ObservableProperty]
bool _fletcher32Checked;
[ObservableProperty]
bool _md5Checked;
[ObservableProperty]
bool _mediaChecksumsVisible;
[ObservableProperty]
bool _optionsEnabled;
[ObservableProperty]
bool _progress1Visible;
[ObservableProperty]
double _progress2Max;
[ObservableProperty]
string _progress2Text;
[ObservableProperty]
double _progress2Value;
[ObservableProperty]
bool _progress2Visible;
[ObservableProperty]
double _progressMax;
[ObservableProperty]
string _progressText;
[ObservableProperty]
double _progressValue;
[ObservableProperty]
bool _progressVisible;
[ObservableProperty]
bool _resultsVisible;
[ObservableProperty]
bool _sha1Checked;
[ObservableProperty]
bool _sha256Checked;
[ObservableProperty]
bool _sha384Checked;
[ObservableProperty]
bool _sha512Checked;
[ObservableProperty]
bool _spamsumChecked;
[ObservableProperty]
bool _startCommandEnabled;
[ObservableProperty]
bool _startCommandVisible;
[ObservableProperty]
bool _stopCommandEnabled;
[ObservableProperty]
bool _stopCommandVisible;
[ObservableProperty]
string _title;
[ObservableProperty]
bool _trackChecksumsVisible;
2022-03-06 13:29:38 +00:00
public ImageChecksumViewModel(IMediaImage inputFormat, Window view)
{
_view = view;
_cancel = false;
_inputFormat = inputFormat;
ChecksumTracksChecked = ChecksumTracksVisible;
OptionsEnabled = true;
ChecksumMediaChecked = true;
ChecksumTracksChecked = true;
Adler32Checked = true;
Crc16Checked = true;
Crc32Checked = true;
Md5Checked = true;
Sha1Checked = true;
SpamsumChecked = true;
2024-05-01 04:39:38 +01:00
TrackChecksums = [];
MediaChecksums = [];
2025-08-20 21:19:43 +01:00
StartCommand = new RelayCommand(Start);
CloseCommand = new RelayCommand(Close);
StopCommand = new RelayCommand(Stop);
2022-03-06 13:29:38 +00:00
StopCommandVisible = false;
StartCommandVisible = true;
CloseCommandVisible = true;
StopCommandEnabled = true;
StartCommandEnabled = true;
CloseCommandEnabled = true;
try
{
ChecksumTracksVisible = (inputFormat as IOpticalMediaImage)?.Tracks?.Count > 0;
}
2025-08-20 18:51:05 +01:00
catch(Exception ex)
2022-03-06 13:29:38 +00:00
{
ChecksumTracksVisible = false;
2025-08-20 18:51:05 +01:00
SentrySdk.CaptureException(ex);
}
2022-03-06 13:29:38 +00:00
}
public string ChecksumMediaLabel => UI.Checksums_the_whole_disc;
public string ChecksumTracksLabel => UI.Checksums_each_track_separately;
public string Adler32Label => UI.Calculates_Adler_32;
public string Crc16Label => UI.Calculates_CRC16;
public string Crc32Label => UI.Calculates_CRC32;
public string Crc64Label => UI.Calculates_CRC64_ECMA;
public string Fletcher16Label => UI.Calculates_Fletcher_16;
public string Fletcher32Label => UI.Calculates_Fletcher_32;
public string Md5Label => UI.Calculates_MD5;
public string Sha1Label => UI.Calculates_SHA1;
public string Sha256Label => UI.Calculates_SHA256;
public string Sha384Label => UI.Calculates_SHA384;
public string Sha512Label => UI.Calculates_SHA512;
public string SpamSumLabel => UI.Calculates_SpamSum_fuzzy_hash;
public string TrackChecksumsLabel => UI.Title_Track_checksums;
public string TrackLabel => Localization.Core.Title_Track;
public string AlgorithmsLabel => UI.Title_Algorithms;
public string HashLabel => UI.Title_Hash;
public string MediaChecksumsLabel => UI.Title_Media_checksums;
public string StartLabel => UI.ButtonLabel_Start;
public string CloseLabel => UI.ButtonLabel_Close;
public string StopLabel => UI.ButtonLabel_Stop;
2022-03-06 13:29:38 +00:00
public ObservableCollection<ChecksumModel> TrackChecksums { get; }
public ObservableCollection<ChecksumModel> MediaChecksums { get; }
2025-08-20 21:19:43 +01:00
public ICommand StartCommand { get; }
public ICommand CloseCommand { get; }
public ICommand StopCommand { get; }
2025-08-20 21:19:43 +01:00
void Start()
2022-03-06 13:29:38 +00:00
{
OptionsEnabled = false;
CloseCommandVisible = false;
StartCommandVisible = false;
StopCommandVisible = true;
ProgressVisible = true;
Progress1Visible = true;
Progress2Visible = false;
new Thread(DoWork)
{
Priority = ThreadPriority.BelowNormal
}.Start();
}
2025-08-20 21:19:43 +01:00
void Close() => _view.Close();
2025-08-20 21:19:43 +01:00
internal void Stop()
2022-03-06 13:29:38 +00:00
{
_cancel = true;
StopCommandEnabled = false;
}
2022-11-15 01:35:06 +00:00
[SuppressMessage("ReSharper", "AsyncVoidMethod")]
2022-03-06 13:29:38 +00:00
async void DoWork()
{
var opticalMediaImage = _inputFormat as IOpticalMediaImage;
bool formatHasTracks = false;
2022-03-06 13:29:38 +00:00
if(opticalMediaImage != null)
2023-10-03 23:27:57 +01:00
{
2022-03-06 13:29:38 +00:00
try
{
formatHasTracks = opticalMediaImage.Tracks?.Count > 0;
}
2025-08-20 18:51:05 +01:00
catch(Exception ex)
2022-03-06 13:29:38 +00:00
{
2025-08-20 18:51:05 +01:00
SentrySdk.CaptureException(ex);
2022-03-06 13:29:38 +00:00
formatHasTracks = false;
}
2023-10-03 23:27:57 +01:00
}
2022-03-06 13:29:38 +00:00
// Setup progress bars
await Dispatcher.UIThread.InvokeAsync(() =>
{
ProgressVisible = true;
Progress1Visible = true;
Progress2Visible = true;
ProgressMax = 1;
Progress2Max = (int)(_inputFormat.Info.Sectors / SECTORS_TO_READ);
if(formatHasTracks && ChecksumTracksChecked && opticalMediaImage != null)
2022-03-06 13:29:38 +00:00
ProgressMax += opticalMediaImage.Tracks.Count;
else
{
2022-03-06 13:29:38 +00:00
ProgressMax = 2;
Progress2Visible = false;
}
});
2022-03-06 13:29:38 +00:00
var enabledChecksums = new EnableChecksum();
2024-05-01 04:05:22 +01:00
if(Adler32Checked) enabledChecksums |= EnableChecksum.Adler32;
2024-05-01 04:05:22 +01:00
if(Crc16Checked) enabledChecksums |= EnableChecksum.Crc16;
2024-05-01 04:05:22 +01:00
if(Crc32Checked) enabledChecksums |= EnableChecksum.Crc32;
2024-05-01 04:05:22 +01:00
if(Crc64Checked) enabledChecksums |= EnableChecksum.Crc64;
2024-05-01 04:05:22 +01:00
if(Md5Checked) enabledChecksums |= EnableChecksum.Md5;
2024-05-01 04:05:22 +01:00
if(Sha1Checked) enabledChecksums |= EnableChecksum.Sha1;
2024-05-01 04:05:22 +01:00
if(Sha256Checked) enabledChecksums |= EnableChecksum.Sha256;
2024-05-01 04:05:22 +01:00
if(Sha384Checked) enabledChecksums |= EnableChecksum.Sha384;
2024-05-01 04:05:22 +01:00
if(Sha512Checked) enabledChecksums |= EnableChecksum.Sha512;
2024-05-01 04:05:22 +01:00
if(SpamsumChecked) enabledChecksums |= EnableChecksum.SpamSum;
2024-05-01 04:05:22 +01:00
if(Fletcher16Checked) enabledChecksums |= EnableChecksum.Fletcher16;
2024-05-01 04:05:22 +01:00
if(Fletcher32Checked) enabledChecksums |= EnableChecksum.Fletcher32;
2022-03-06 13:29:38 +00:00
Checksum mediaChecksum = null;
ErrorNumber errno;
2022-03-06 13:29:38 +00:00
if(opticalMediaImage?.Tracks != null)
2023-10-03 23:27:57 +01:00
{
2022-03-06 13:29:38 +00:00
try
{
Checksum trackChecksum = null;
2024-05-01 04:05:22 +01:00
if(ChecksumMediaChecked) mediaChecksum = new Checksum(enabledChecksums);
2022-03-06 13:29:38 +00:00
ulong previousTrackEnd = 0;
2022-03-06 13:29:38 +00:00
foreach(Track currentTrack in opticalMediaImage.Tracks)
{
await Dispatcher.UIThread.InvokeAsync(() =>
{
2024-05-01 04:05:22 +01:00
ProgressText = string.Format(UI.Hashing_track_0_of_1,
currentTrack.Sequence,
opticalMediaImage.Tracks.Count);
2022-03-06 13:29:38 +00:00
ProgressValue++;
});
2022-03-06 13:29:38 +00:00
if(currentTrack.StartSector - previousTrackEnd != 0 && ChecksumMediaChecked)
2023-10-03 23:27:57 +01:00
{
2022-03-06 13:29:38 +00:00
for(ulong i = previousTrackEnd + 1; i < currentTrack.StartSector; i++)
{
2022-03-06 13:29:38 +00:00
ulong sector = i;
await Dispatcher.UIThread.InvokeAsync(() =>
{
2022-03-06 13:29:38 +00:00
Progress2Value = (int)(sector / SECTORS_TO_READ);
Progress2Text = $"Hashing track-less sector {sector}";
});
2022-03-06 13:29:38 +00:00
errno = opticalMediaImage.ReadSector(i, out byte[] hiddenSector);
2022-03-06 13:29:38 +00:00
if(errno != ErrorNumber.NoError)
{
2025-08-20 18:51:05 +01:00
AaruLogging.Error(string.Format(Localization.Core.Error_0_reading_sector_1, errno, i));
2022-03-06 13:29:38 +00:00
_cancel = true;
2022-03-06 13:29:38 +00:00
break;
}
2022-03-06 13:29:38 +00:00
mediaChecksum?.Update(hiddenSector);
}
2023-10-03 23:27:57 +01:00
}
2025-08-17 06:11:22 +01:00
AaruLogging.Debug(MODULE_NAME,
2025-08-20 18:51:05 +01:00
UI.Track_0_starts_at_sector_1_and_ends_at_sector_2,
currentTrack.Sequence,
currentTrack.StartSector,
currentTrack.EndSector);
2024-05-01 04:05:22 +01:00
if(ChecksumTracksChecked) trackChecksum = new Checksum(enabledChecksums);
2022-03-06 13:29:38 +00:00
ulong sectors = currentTrack.EndSector - currentTrack.StartSector + 1;
ulong doneSectors = 0;
2022-03-06 13:29:38 +00:00
while(doneSectors < sectors)
{
if(_cancel)
{
await Dispatcher.UIThread.InvokeAsync(() =>
{
2022-03-06 13:29:38 +00:00
CloseCommandVisible = true;
StartCommandVisible = false;
StopCommandVisible = false;
});
2022-03-06 13:29:38 +00:00
return;
}
2022-03-06 13:29:38 +00:00
byte[] sector;
2022-03-06 13:29:38 +00:00
if(sectors - doneSectors >= SECTORS_TO_READ)
{
2024-05-01 04:05:22 +01:00
errno = opticalMediaImage.ReadSectors(doneSectors,
SECTORS_TO_READ,
currentTrack.Sequence,
2022-03-07 07:36:44 +00:00
out sector);
2022-03-06 13:29:38 +00:00
if(errno != ErrorNumber.NoError)
{
2025-08-17 06:11:22 +01:00
AaruLogging.Error(string.Format(Localization.Core.Error_0_reading_sector_1,
2025-08-20 18:51:05 +01:00
errno,
doneSectors));
2022-03-06 13:29:38 +00:00
_cancel = true;
2022-03-06 13:29:38 +00:00
continue;
}
2022-03-06 13:29:38 +00:00
ulong doneSectorsToInvoke = doneSectors;
await Dispatcher.UIThread.InvokeAsync(() =>
{
2022-03-06 13:29:38 +00:00
Progress2Value = (int)(doneSectorsToInvoke / SECTORS_TO_READ);
Progress2Text = $"Hashing sectors {doneSectorsToInvoke} to {
doneSectorsToInvoke + SECTORS_TO_READ} of track {currentTrack.Sequence}";
});
2022-03-06 13:29:38 +00:00
doneSectors += SECTORS_TO_READ;
}
else
{
2024-05-01 04:05:22 +01:00
errno = opticalMediaImage.ReadSectors(doneSectors,
(uint)(sectors - doneSectors),
currentTrack.Sequence,
out sector);
if(errno != ErrorNumber.NoError)
{
2025-08-17 06:11:22 +01:00
AaruLogging.Error(string.Format(Localization.Core.Error_0_reading_sector_1,
2025-08-20 18:51:05 +01:00
errno,
doneSectors));
_cancel = true;
2022-03-06 13:29:38 +00:00
continue;
}
2022-03-06 13:29:38 +00:00
ulong doneSectorsToInvoke = doneSectors;
await Dispatcher.UIThread.InvokeAsync(() =>
{
Progress2Value = (int)(doneSectorsToInvoke / SECTORS_TO_READ);
Progress2Text = $"Hashing sectors {doneSectorsToInvoke} to {
doneSectorsToInvoke + (sectors - doneSectorsToInvoke)} of track {
currentTrack.Sequence}";
2022-03-06 13:29:38 +00:00
});
doneSectors += sectors - doneSectors;
}
2024-05-01 04:05:22 +01:00
if(ChecksumMediaChecked) mediaChecksum?.Update(sector);
2022-03-06 13:29:38 +00:00
2024-05-01 04:05:22 +01:00
if(ChecksumTracksChecked) trackChecksum?.Update(sector);
2022-03-06 13:29:38 +00:00
}
await Dispatcher.UIThread.InvokeAsync(() =>
{
if(!ChecksumTracksChecked) return;
2022-03-06 13:29:38 +00:00
2024-05-01 04:05:22 +01:00
if(trackChecksum == null) return;
foreach(CommonTypes.AaruMetadata.Checksum chk in trackChecksum.End())
2023-10-03 23:27:57 +01:00
{
2022-03-06 13:29:38 +00:00
TrackChecksums.Add(new ChecksumModel
{
2022-03-06 13:29:38 +00:00
Track = currentTrack.Sequence.ToString(),
Algorithm = chk.Type.ToString(),
2020-07-20 04:34:16 +01:00
Hash = chk.Value
});
2023-10-03 23:27:57 +01:00
}
});
2022-03-06 13:29:38 +00:00
previousTrackEnd = currentTrack.EndSector;
}
2022-03-06 13:29:38 +00:00
if(opticalMediaImage.Info.Sectors - previousTrackEnd != 0 && ChecksumMediaChecked)
2023-10-03 23:27:57 +01:00
{
2022-03-06 13:29:38 +00:00
for(ulong i = previousTrackEnd + 1; i < opticalMediaImage.Info.Sectors; i++)
{
2022-03-06 13:29:38 +00:00
ulong sector = i;
await Dispatcher.UIThread.InvokeAsync(() =>
{
2022-03-06 13:29:38 +00:00
Progress2Value = (int)(sector / SECTORS_TO_READ);
Progress2Text = $"Hashing track-less sector {sector}";
});
2022-03-06 13:29:38 +00:00
errno = opticalMediaImage.ReadSector(i, out byte[] hiddenSector);
if(errno != ErrorNumber.NoError)
{
2025-08-20 18:51:05 +01:00
AaruLogging.Error(string.Format(Localization.Core.Error_0_reading_sector_1, errno, i));
_cancel = true;
2022-03-06 13:29:38 +00:00
break;
}
2022-03-06 13:29:38 +00:00
mediaChecksum?.Update(hiddenSector);
}
2023-10-03 23:27:57 +01:00
}
await Dispatcher.UIThread.InvokeAsync(() =>
{
2024-05-01 04:05:22 +01:00
if(mediaChecksum == null) return;
2022-03-06 13:29:38 +00:00
foreach(CommonTypes.AaruMetadata.Checksum chk in mediaChecksum.End())
2023-10-03 23:27:57 +01:00
{
MediaChecksums.Add(new ChecksumModel
{
Algorithm = chk.Type.ToString(),
2020-07-20 04:34:16 +01:00
Hash = chk.Value
});
2023-10-03 23:27:57 +01:00
}
});
}
2022-03-06 13:29:38 +00:00
catch(Exception ex)
{
2025-08-17 06:11:22 +01:00
AaruLogging.Debug(Localization.Core.Could_not_get_tracks_because_0, ex.Message);
AaruLogging.WriteLine("Unable to get separate tracks, not checksumming them");
AaruLogging.Exception(ex, Localization.Core.Could_not_get_tracks_because_0, ex.Message);
2022-03-06 13:29:38 +00:00
}
2023-10-03 23:27:57 +01:00
}
2022-03-06 13:29:38 +00:00
else
{
2023-10-03 23:27:57 +01:00
await Dispatcher.UIThread.InvokeAsync(() => { Progress1Visible = false; });
2022-03-06 13:29:38 +00:00
mediaChecksum = new Checksum(enabledChecksums);
ulong doneSectors = 0;
while(doneSectors < _inputFormat.Info.Sectors)
{
if(_cancel)
{
2022-03-06 13:29:38 +00:00
await Dispatcher.UIThread.InvokeAsync(() =>
{
CloseCommandVisible = true;
StartCommandVisible = false;
StopCommandVisible = false;
});
2022-03-06 13:29:38 +00:00
return;
}
byte[] sector;
if(_inputFormat.Info.Sectors - doneSectors >= SECTORS_TO_READ)
{
2022-03-06 13:29:38 +00:00
errno = _inputFormat.ReadSectors(doneSectors, SECTORS_TO_READ, out sector);
if(errno != ErrorNumber.NoError)
{
2025-08-17 06:11:22 +01:00
AaruLogging.Error(string.Format(Localization.Core.Error_0_reading_sector_1,
2025-08-20 18:51:05 +01:00
errno,
doneSectors));
2022-03-06 13:29:38 +00:00
_cancel = true;
continue;
}
ulong doneSectorsToInvoke = doneSectors;
await Dispatcher.UIThread.InvokeAsync(() =>
{
Progress2Value = (int)(doneSectorsToInvoke / SECTORS_TO_READ);
2024-05-01 04:05:22 +01:00
Progress2Text = string.Format(UI.Hashing_sectors_0_to_1,
doneSectorsToInvoke,
doneSectorsToInvoke + SECTORS_TO_READ);
2022-03-06 13:29:38 +00:00
});
doneSectors += SECTORS_TO_READ;
}
else
{
2024-05-01 04:05:22 +01:00
errno = _inputFormat.ReadSectors(doneSectors,
(uint)(_inputFormat.Info.Sectors - doneSectors),
2022-03-06 13:29:38 +00:00
out sector);
if(errno != ErrorNumber.NoError)
{
2025-08-17 06:11:22 +01:00
AaruLogging.Error(string.Format(Localization.Core.Error_0_reading_sector_1,
2025-08-20 18:51:05 +01:00
errno,
doneSectors));
2022-03-06 13:29:38 +00:00
_cancel = true;
continue;
}
ulong doneSectorsToInvoke = doneSectors;
await Dispatcher.UIThread.InvokeAsync(() =>
{
Progress2Value = (int)(doneSectorsToInvoke / SECTORS_TO_READ);
2024-05-01 04:05:22 +01:00
Progress2Text = string.Format(UI.Hashing_sectors_0_to_1,
doneSectorsToInvoke,
doneSectorsToInvoke +
(_inputFormat.Info.Sectors - doneSectorsToInvoke));
2022-03-06 13:29:38 +00:00
});
doneSectors += _inputFormat.Info.Sectors - doneSectors;
}
mediaChecksum.Update(sector);
}
await Dispatcher.UIThread.InvokeAsync(() =>
{
foreach(CommonTypes.AaruMetadata.Checksum chk in mediaChecksum.End())
2023-10-03 23:27:57 +01:00
{
2022-03-06 13:29:38 +00:00
MediaChecksums.Add(new ChecksumModel
{
Algorithm = chk.Type.ToString(),
2022-03-06 13:29:38 +00:00
Hash = chk.Value
});
2023-10-03 23:27:57 +01:00
}
});
}
2022-03-06 13:29:38 +00:00
2024-05-01 04:05:22 +01:00
if(ChecksumTracksChecked) await Dispatcher.UIThread.InvokeAsync(() => { TrackChecksumsVisible = true; });
2022-03-06 13:29:38 +00:00
2024-05-01 04:05:22 +01:00
if(ChecksumMediaChecked) await Dispatcher.UIThread.InvokeAsync(() => { MediaChecksumsVisible = true; });
2022-03-06 13:29:38 +00:00
Statistics.AddCommand("checksum");
await Dispatcher.UIThread.InvokeAsync(() =>
{
OptionsEnabled = false;
ResultsVisible = true;
ProgressVisible = false;
StartCommandVisible = false;
StopCommandVisible = false;
CloseCommandVisible = true;
});
}
}