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/>.
|
|
|
|
|
|
//
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
2020-12-31 23:08:23 +00:00
|
|
|
|
// Copyright © 2011-2021 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;
|
2020-02-27 00:33:26 +00:00
|
|
|
|
using Aaru.CommonTypes;
|
|
|
|
|
|
using Aaru.CommonTypes.Enums;
|
|
|
|
|
|
using Aaru.CommonTypes.Interfaces;
|
|
|
|
|
|
using Aaru.Console;
|
2017-05-27 20:24:06 +01:00
|
|
|
|
using Schemas;
|
|
|
|
|
|
|
2020-02-27 00:33:26 +00:00
|
|
|
|
namespace Aaru.Core
|
2017-05-27 20:24:06 +01:00
|
|
|
|
{
|
2020-07-22 13:20:25 +01:00
|
|
|
|
public sealed partial class Sidecar
|
2017-05-27 20:24:06 +01:00
|
|
|
|
{
|
2020-07-20 21:11:32 +01:00
|
|
|
|
readonly ChecksumType[] _emptyChecksums;
|
|
|
|
|
|
readonly Encoding _encoding;
|
|
|
|
|
|
readonly FileInfo _fi;
|
|
|
|
|
|
readonly Guid _filterId;
|
|
|
|
|
|
readonly IMediaImage _image;
|
|
|
|
|
|
readonly string _imagePath;
|
|
|
|
|
|
readonly Checksum _imgChkWorker;
|
|
|
|
|
|
readonly PluginBase _plugins;
|
|
|
|
|
|
bool _aborted;
|
|
|
|
|
|
FileStream _fs;
|
|
|
|
|
|
CICMMetadataType _sidecar;
|
2019-04-20 18:11:02 +01:00
|
|
|
|
|
2021-08-17 21:23:10 +01:00
|
|
|
|
/// <summary>Initializes a new instance of this class</summary>
|
2019-04-20 18:11:02 +01:00
|
|
|
|
public Sidecar()
|
|
|
|
|
|
{
|
2020-07-20 21:11:32 +01:00
|
|
|
|
_plugins = GetPluginBase.Instance;
|
|
|
|
|
|
_imgChkWorker = new Checksum();
|
|
|
|
|
|
_aborted = false;
|
2019-05-10 23:36:49 +01:00
|
|
|
|
|
2020-02-29 18:03:35 +00:00
|
|
|
|
var emptyChkWorker = new Checksum();
|
2021-08-17 18:21:12 +01:00
|
|
|
|
emptyChkWorker.Update(Array.Empty<byte>());
|
2020-07-20 21:11:32 +01:00
|
|
|
|
_emptyChecksums = emptyChkWorker.End().ToArray();
|
2019-04-20 18:11:02 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-12-23 01:46:08 +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>
|
2019-04-20 18:11:02 +01:00
|
|
|
|
public Sidecar(IMediaImage image, string imagePath, Guid filterId, Encoding encoding)
|
2017-05-27 20:24:06 +01:00
|
|
|
|
{
|
2020-07-20 21:11:32 +01:00
|
|
|
|
_image = image;
|
|
|
|
|
|
_imagePath = imagePath;
|
|
|
|
|
|
_filterId = filterId;
|
|
|
|
|
|
_encoding = encoding;
|
|
|
|
|
|
_sidecar = image.CicmMetadata ?? new CICMMetadataType();
|
|
|
|
|
|
_plugins = GetPluginBase.Instance;
|
|
|
|
|
|
_fi = new FileInfo(imagePath);
|
|
|
|
|
|
_fs = new FileStream(imagePath, FileMode.Open, FileAccess.Read);
|
|
|
|
|
|
_imgChkWorker = new Checksum();
|
|
|
|
|
|
_aborted = false;
|
2019-04-20 18:11:02 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-02-29 18:03:35 +00:00
|
|
|
|
/// <summary>Implements creating a metadata sidecar</summary>
|
2019-04-20 18:11:02 +01:00
|
|
|
|
/// <returns>The metadata sidecar</returns>
|
|
|
|
|
|
public CICMMetadataType Create()
|
|
|
|
|
|
{
|
2017-05-27 20:24:06 +01:00
|
|
|
|
// For fast debugging, skip checksum
|
|
|
|
|
|
//goto skipImageChecksum;
|
2019-02-12 18:57:20 +00:00
|
|
|
|
|
2017-05-27 20:24:06 +01:00
|
|
|
|
byte[] data;
|
2018-01-28 20:29:46 +00:00
|
|
|
|
long position = 0;
|
2019-04-20 21:30:39 +01:00
|
|
|
|
UpdateStatus("Hashing image file...");
|
2017-05-28 00:31:46 +01:00
|
|
|
|
InitProgress();
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
2020-07-20 21:11:32 +01:00
|
|
|
|
while(position < _fi.Length - 1048576)
|
2017-05-27 20:24:06 +01:00
|
|
|
|
{
|
2020-07-20 21:11:32 +01:00
|
|
|
|
if(_aborted)
|
|
|
|
|
|
return _sidecar;
|
2019-04-20 19:21:00 +01:00
|
|
|
|
|
2017-05-27 20:24:06 +01:00
|
|
|
|
data = new byte[1048576];
|
2020-07-20 21:11:32 +01:00
|
|
|
|
_fs.Read(data, 0, 1048576);
|
2017-05-27 20:24:06 +01:00
|
|
|
|
|
2020-07-20 21:11:32 +01:00
|
|
|
|
UpdateProgress("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
|
|
|
|
|
|
|
|
|
|
position += 1048576;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-07-20 21:11:32 +01:00
|
|
|
|
data = new byte[_fi.Length - position];
|
|
|
|
|
|
_fs.Read(data, 0, (int)(_fi.Length - position));
|
2017-05-27 20:24:06 +01:00
|
|
|
|
|
2020-07-20 21:11:32 +01:00
|
|
|
|
UpdateProgress("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
|
|
|
|
|
|
|
|
|
|
// For fast debugging, skip checksum
|
|
|
|
|
|
//skipImageChecksum:
|
|
|
|
|
|
|
2019-02-12 18:57:20 +00:00
|
|
|
|
EndProgress();
|
2020-07-20 21:11:32 +01:00
|
|
|
|
_fs.Close();
|
2017-05-27 20:24:06 +01:00
|
|
|
|
|
2020-07-20 21:11:32 +01:00
|
|
|
|
List<ChecksumType> imgChecksums = _imgChkWorker.End();
|
2017-05-27 20:24:06 +01:00
|
|
|
|
|
2020-07-20 21:11:32 +01:00
|
|
|
|
_sidecar.OpticalDisc = null;
|
|
|
|
|
|
_sidecar.BlockMedia = null;
|
|
|
|
|
|
_sidecar.AudioMedia = null;
|
|
|
|
|
|
_sidecar.LinearMedia = null;
|
2018-01-28 20:29:46 +00:00
|
|
|
|
|
2020-07-20 21:11:32 +01:00
|
|
|
|
if(_aborted)
|
|
|
|
|
|
return _sidecar;
|
2019-04-20 19:21:00 +01:00
|
|
|
|
|
2020-07-20 21:11:32 +01:00
|
|
|
|
switch(_image.Info.XmlMediaType)
|
2017-05-27 20:24:06 +01:00
|
|
|
|
{
|
|
|
|
|
|
case XmlMediaType.OpticalDisc:
|
2020-07-20 21:11:32 +01:00
|
|
|
|
if(_image is IOpticalMediaImage opticalImage)
|
|
|
|
|
|
OpticalDisc(opticalImage, _filterId, _imagePath, _fi, _plugins, imgChecksums, ref _sidecar,
|
|
|
|
|
|
_encoding);
|
2019-01-20 20:11:10 +00:00
|
|
|
|
else
|
|
|
|
|
|
{
|
2020-02-29 18:03:35 +00:00
|
|
|
|
AaruConsole.
|
|
|
|
|
|
ErrorWriteLine("The specified image says it contains an optical media but at the same time says it does not support them.");
|
|
|
|
|
|
|
2020-02-27 23:48:41 +00:00
|
|
|
|
AaruConsole.ErrorWriteLine("Please open an issue at Github.");
|
2019-01-20 20:11:10 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-08-08 13:40:32 +01:00
|
|
|
|
break;
|
2017-05-27 20:24:06 +01:00
|
|
|
|
case XmlMediaType.BlockMedia:
|
2020-07-20 21:11:32 +01:00
|
|
|
|
BlockMedia(_image, _filterId, _imagePath, _fi, _plugins, imgChecksums, ref _sidecar, _encoding);
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
2017-08-08 13:40:32 +01:00
|
|
|
|
break;
|
2017-05-27 20:24:06 +01:00
|
|
|
|
case XmlMediaType.LinearMedia:
|
2020-07-20 21:11:32 +01:00
|
|
|
|
LinearMedia(_image, _filterId, _imagePath, _fi, _plugins, imgChecksums, ref _sidecar, _encoding);
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
2017-08-08 13:40:32 +01:00
|
|
|
|
break;
|
2017-05-27 20:24:06 +01:00
|
|
|
|
case XmlMediaType.AudioMedia:
|
2020-07-20 21:11:32 +01:00
|
|
|
|
AudioMedia(_image, _filterId, _imagePath, _fi, _plugins, imgChecksums, ref _sidecar, _encoding);
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
2017-08-08 13:40:32 +01:00
|
|
|
|
break;
|
2017-05-27 20:24:06 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-07-20 21:11:32 +01:00
|
|
|
|
return _sidecar;
|
2017-05-27 20:24:06 +01:00
|
|
|
|
}
|
2019-04-20 19:21:00 +01:00
|
|
|
|
|
2021-08-17 21:23:10 +01:00
|
|
|
|
/// <summary>Aborts sidecar running operation</summary>
|
2019-04-20 19:21:00 +01:00
|
|
|
|
public void Abort()
|
|
|
|
|
|
{
|
2019-04-20 21:30:39 +01:00
|
|
|
|
UpdateStatus("Aborting...");
|
2020-07-20 21:11:32 +01:00
|
|
|
|
_aborted = true;
|
2019-04-20 19:21:00 +01:00
|
|
|
|
}
|
2017-05-27 20:24:06 +01:00
|
|
|
|
}
|
2017-12-19 20:33:03 +00:00
|
|
|
|
}
|