2017-05-27 20:24:06 +01:00
|
|
|
|
// /***************************************************************************
|
2020-02-27 12:31:25 +00:00
|
|
|
|
// Aaru Data Preservation Suite
|
2017-05-27 20:24:06 +01:00
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
//
|
|
|
|
|
|
// Filename : Sidecar.cs
|
2017-12-19 03:50:57 +00:00
|
|
|
|
// Author(s) : Natalia Portillo <claunia@claunia.com>
|
2017-05-27 20:24:06 +01:00
|
|
|
|
//
|
2017-12-19 03:50:57 +00:00
|
|
|
|
// Component : Core algorithms.
|
2017-05-27 20:24:06 +01:00
|
|
|
|
//
|
|
|
|
|
|
// --[ Description ] ----------------------------------------------------------
|
|
|
|
|
|
//
|
2017-12-19 03:50:57 +00:00
|
|
|
|
// Creates sidecar from dump.
|
2017-05-27 20:24:06 +01:00
|
|
|
|
//
|
|
|
|
|
|
// --[ 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
|
2017-05-27 20:24:06 +01:00
|
|
|
|
// ****************************************************************************/
|
2017-12-19 03:50:57 +00:00
|
|
|
|
|
2017-12-21 14:30:38 +00:00
|
|
|
|
using System;
|
2017-05-27 20:24:06 +01:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.IO;
|
2017-12-21 14:30:38 +00:00
|
|
|
|
using System.Text;
|
2023-10-05 13:47:59 +01:00
|
|
|
|
using Aaru.CommonTypes;
|
2022-12-15 22:21:07 +00:00
|
|
|
|
using Aaru.CommonTypes.AaruMetadata;
|
2020-02-27 00:33:26 +00:00
|
|
|
|
using Aaru.CommonTypes.Enums;
|
|
|
|
|
|
using Aaru.CommonTypes.Interfaces;
|
2022-11-14 09:43:16 +00:00
|
|
|
|
using Aaru.Helpers;
|
2025-08-17 05:50:25 +01:00
|
|
|
|
using Aaru.Logging;
|
2017-05-27 20:24:06 +01:00
|
|
|
|
|
2022-11-15 15:58:43 +00:00
|
|
|
|
namespace Aaru.Core;
|
|
|
|
|
|
|
2021-11-14 01:38:37 +00:00
|
|
|
|
public sealed partial class Sidecar
|
2017-05-27 20:24:06 +01:00
|
|
|
|
{
|
2023-10-03 17:17:16 +01:00
|
|
|
|
const string MODULE_NAME = "Sidecar creation";
|
2022-12-15 22:21:07 +00:00
|
|
|
|
readonly List<CommonTypes.AaruMetadata.Checksum> _emptyChecksums;
|
|
|
|
|
|
readonly Encoding _encoding;
|
|
|
|
|
|
readonly FileInfo _fi;
|
|
|
|
|
|
readonly Guid _filterId;
|
|
|
|
|
|
readonly IBaseImage _image;
|
|
|
|
|
|
readonly string _imagePath;
|
|
|
|
|
|
readonly Checksum _imgChkWorker;
|
2023-10-05 13:47:59 +01:00
|
|
|
|
readonly PluginRegister _plugins;
|
2022-12-15 22:21:07 +00:00
|
|
|
|
bool _aborted;
|
|
|
|
|
|
FileStream _fs;
|
|
|
|
|
|
Metadata _sidecar;
|
2021-11-14 01:38:37 +00:00
|
|
|
|
|
|
|
|
|
|
/// <summary>Initializes a new instance of this class</summary>
|
|
|
|
|
|
public Sidecar()
|
2017-05-27 20:24:06 +01:00
|
|
|
|
{
|
2023-10-05 13:47:59 +01:00
|
|
|
|
_plugins = PluginRegister.Singleton;
|
2021-11-14 01:38:37 +00:00
|
|
|
|
_imgChkWorker = new Checksum();
|
|
|
|
|
|
_aborted = false;
|
2019-04-20 18:11:02 +01:00
|
|
|
|
|
2021-11-14 01:38:37 +00:00
|
|
|
|
var emptyChkWorker = new Checksum();
|
2024-05-01 04:39:38 +01:00
|
|
|
|
emptyChkWorker.Update([]);
|
2022-12-15 22:21:07 +00:00
|
|
|
|
_emptyChecksums = emptyChkWorker.End();
|
2021-11-14 01:38:37 +00:00
|
|
|
|
}
|
2019-04-20 19:21:00 +01:00
|
|
|
|
|
2021-11-14 01:38:37 +00:00
|
|
|
|
/// <param name="image">Image</param>
|
|
|
|
|
|
/// <param name="imagePath">Path to image</param>
|
|
|
|
|
|
/// <param name="filterId">Filter uuid</param>
|
|
|
|
|
|
/// <param name="encoding">Encoding for analysis</param>
|
|
|
|
|
|
public Sidecar(IBaseImage image, string imagePath, Guid filterId, Encoding encoding)
|
|
|
|
|
|
{
|
|
|
|
|
|
_image = image;
|
|
|
|
|
|
_imagePath = imagePath;
|
|
|
|
|
|
_filterId = filterId;
|
|
|
|
|
|
_encoding = encoding;
|
2022-12-15 22:21:07 +00:00
|
|
|
|
_sidecar = image.AaruMetadata ?? new Metadata();
|
2023-10-05 13:47:59 +01:00
|
|
|
|
_plugins = PluginRegister.Singleton;
|
2021-11-14 01:38:37 +00:00
|
|
|
|
_fi = new FileInfo(imagePath);
|
|
|
|
|
|
_fs = new FileStream(imagePath, FileMode.Open, FileAccess.Read);
|
|
|
|
|
|
_imgChkWorker = new Checksum();
|
|
|
|
|
|
_aborted = false;
|
|
|
|
|
|
}
|
2017-05-27 20:24:06 +01:00
|
|
|
|
|
2021-11-14 01:38:37 +00:00
|
|
|
|
/// <summary>Implements creating a metadata sidecar</summary>
|
|
|
|
|
|
/// <returns>The metadata sidecar</returns>
|
2022-12-15 22:21:07 +00:00
|
|
|
|
public Metadata Create()
|
2021-11-14 01:38:37 +00:00
|
|
|
|
{
|
|
|
|
|
|
// For fast debugging, skip checksum
|
|
|
|
|
|
//goto skipImageChecksum;
|
2017-05-27 20:24:06 +01:00
|
|
|
|
|
2021-11-14 01:38:37 +00:00
|
|
|
|
byte[] data;
|
|
|
|
|
|
long position = 0;
|
2022-11-23 16:06:46 +00:00
|
|
|
|
UpdateStatus(Localization.Core.Hashing_image_file);
|
2021-11-14 01:38:37 +00:00
|
|
|
|
InitProgress();
|
2017-05-27 20:24:06 +01:00
|
|
|
|
|
2021-11-14 01:38:37 +00:00
|
|
|
|
while(position < _fi.Length - 1048576)
|
|
|
|
|
|
{
|
2024-05-01 04:05:22 +01:00
|
|
|
|
if(_aborted) return _sidecar;
|
2017-05-27 20:24:06 +01:00
|
|
|
|
|
2021-11-14 01:38:37 +00:00
|
|
|
|
data = new byte[1048576];
|
2022-11-14 09:43:16 +00:00
|
|
|
|
_fs.EnsureRead(data, 0, 1048576);
|
2017-05-27 20:24:06 +01:00
|
|
|
|
|
2022-11-23 16:06:46 +00:00
|
|
|
|
UpdateProgress(Localization.Core.Hashing_image_file_byte_0_of_1, position, _fi.Length);
|
2017-05-27 20:24:06 +01:00
|
|
|
|
|
2020-07-20 21:11:32 +01:00
|
|
|
|
_imgChkWorker.Update(data);
|
2017-05-27 20:24:06 +01:00
|
|
|
|
|
2021-11-14 01:38:37 +00:00
|
|
|
|
position += 1048576;
|
|
|
|
|
|
}
|
2018-01-28 20:29:46 +00:00
|
|
|
|
|
2021-11-14 01:38:37 +00:00
|
|
|
|
data = new byte[_fi.Length - position];
|
2022-11-14 09:43:16 +00:00
|
|
|
|
_fs.EnsureRead(data, 0, (int)(_fi.Length - position));
|
2019-04-20 19:21:00 +01:00
|
|
|
|
|
2022-11-23 16:06:46 +00:00
|
|
|
|
UpdateProgress(Localization.Core.Hashing_image_file_byte_0_of_1, position, _fi.Length);
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
2021-11-14 01:38:37 +00:00
|
|
|
|
_imgChkWorker.Update(data);
|
2019-01-20 20:11:10 +00:00
|
|
|
|
|
2021-11-14 01:38:37 +00:00
|
|
|
|
// For fast debugging, skip checksum
|
|
|
|
|
|
//skipImageChecksum:
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
2021-11-14 01:38:37 +00:00
|
|
|
|
EndProgress();
|
|
|
|
|
|
_fs.Close();
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
2022-12-15 22:21:07 +00:00
|
|
|
|
List<CommonTypes.AaruMetadata.Checksum> imgChecksums = _imgChkWorker.End();
|
2017-05-27 20:24:06 +01:00
|
|
|
|
|
2024-05-01 04:05:22 +01:00
|
|
|
|
if(_aborted) return _sidecar;
|
2019-04-20 19:21:00 +01:00
|
|
|
|
|
2022-12-15 22:21:07 +00:00
|
|
|
|
switch(_image.Info.MetadataMediaType)
|
2019-04-20 19:21:00 +01:00
|
|
|
|
{
|
2022-12-15 22:21:07 +00:00
|
|
|
|
case MetadataMediaType.OpticalDisc:
|
2021-11-14 01:38:37 +00:00
|
|
|
|
if(_image is IOpticalMediaImage opticalImage)
|
2023-10-03 22:57:50 +01:00
|
|
|
|
{
|
2024-05-01 04:05:22 +01:00
|
|
|
|
OpticalDisc(opticalImage,
|
|
|
|
|
|
_filterId,
|
|
|
|
|
|
_imagePath,
|
|
|
|
|
|
_fi,
|
|
|
|
|
|
_plugins,
|
|
|
|
|
|
imgChecksums,
|
|
|
|
|
|
ref _sidecar,
|
2021-11-14 01:38:37 +00:00
|
|
|
|
_encoding);
|
2023-10-03 22:57:50 +01:00
|
|
|
|
}
|
2021-11-14 01:38:37 +00:00
|
|
|
|
else
|
|
|
|
|
|
{
|
2025-08-17 06:11:22 +01:00
|
|
|
|
AaruLogging.Error(Localization.Core
|
2025-11-24 19:38:40 +00:00
|
|
|
|
.The_specified_image_says_it_contains_an_optical_media_but_at_the_same_time_says_it_does_not_support_them);
|
2021-11-14 01:38:37 +00:00
|
|
|
|
|
2025-08-17 06:11:22 +01:00
|
|
|
|
AaruLogging.Error(Localization.Core.Please_open_an_issue_at_Github);
|
2021-11-14 01:38:37 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
break;
|
2022-12-15 22:21:07 +00:00
|
|
|
|
case MetadataMediaType.BlockMedia:
|
2021-11-14 01:38:37 +00:00
|
|
|
|
if(_image is IMediaImage blockImage)
|
|
|
|
|
|
BlockMedia(blockImage, _filterId, _imagePath, _fi, _plugins, imgChecksums, ref _sidecar, _encoding);
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2025-08-17 06:11:22 +01:00
|
|
|
|
AaruLogging.Error(Localization.Core
|
2025-11-24 19:38:40 +00:00
|
|
|
|
.The_specified_image_says_it_contains_a_block_addressable_media_but_at_the_same_time_says_it_does_not_support_them);
|
2021-11-14 01:38:37 +00:00
|
|
|
|
|
2025-08-17 06:11:22 +01:00
|
|
|
|
AaruLogging.Error(Localization.Core.Please_open_an_issue_at_Github);
|
2021-11-14 01:38:37 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
break;
|
2022-12-15 22:21:07 +00:00
|
|
|
|
case MetadataMediaType.LinearMedia:
|
2021-11-14 01:38:37 +00:00
|
|
|
|
if(_image is IByteAddressableImage byteAddressableImage)
|
2023-10-03 22:57:50 +01:00
|
|
|
|
{
|
2024-05-01 04:05:22 +01:00
|
|
|
|
LinearMedia(byteAddressableImage,
|
|
|
|
|
|
_filterId,
|
|
|
|
|
|
_imagePath,
|
|
|
|
|
|
_fi,
|
|
|
|
|
|
_plugins,
|
|
|
|
|
|
imgChecksums,
|
|
|
|
|
|
ref _sidecar,
|
2021-11-14 01:38:37 +00:00
|
|
|
|
_encoding);
|
2023-10-03 22:57:50 +01:00
|
|
|
|
}
|
2021-11-14 01:38:37 +00:00
|
|
|
|
else
|
|
|
|
|
|
{
|
2025-08-17 06:11:22 +01:00
|
|
|
|
AaruLogging.Error(Localization.Core
|
2025-11-24 19:38:40 +00:00
|
|
|
|
.The_specified_image_says_it_contains_a_byte_addressable_media_but_at_the_same_time_says_it_does_not_support_them);
|
2021-11-14 01:38:37 +00:00
|
|
|
|
|
2025-08-17 06:11:22 +01:00
|
|
|
|
AaruLogging.Error(Localization.Core.Please_open_an_issue_at_Github);
|
2021-11-14 01:38:37 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
break;
|
2022-12-15 22:21:07 +00:00
|
|
|
|
case MetadataMediaType.AudioMedia:
|
2021-11-14 01:38:37 +00:00
|
|
|
|
AudioMedia(_image, _filterId, _imagePath, _fi, _plugins, imgChecksums, ref _sidecar, _encoding);
|
|
|
|
|
|
|
|
|
|
|
|
break;
|
2019-04-20 19:21:00 +01:00
|
|
|
|
}
|
2021-11-14 01:38:37 +00:00
|
|
|
|
|
|
|
|
|
|
return _sidecar;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>Aborts sidecar running operation</summary>
|
|
|
|
|
|
public void Abort()
|
|
|
|
|
|
{
|
2022-11-23 16:06:46 +00:00
|
|
|
|
UpdateStatus(Localization.Core.Aborting);
|
2021-11-14 01:38:37 +00:00
|
|
|
|
_aborted = true;
|
2017-05-27 20:24:06 +01:00
|
|
|
|
}
|
2017-12-19 20:33:03 +00:00
|
|
|
|
}
|