Files
Aaru/Aaru.Core/Sidecar/Sidecar.cs

178 lines
6.3 KiB
C#
Raw Normal View History

// /***************************************************************************
2020-02-27 12:31:25 +00:00
// Aaru Data Preservation Suite
// ----------------------------------------------------------------------------
//
// Filename : Sidecar.cs
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// Component : Core algorithms.
//
// --[ Description ] ----------------------------------------------------------
//
// Creates sidecar from dump.
//
// --[ 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-12-21 14:30:38 +00:00
using System;
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;
using Schemas;
2020-02-27 00:33:26 +00:00
namespace Aaru.Core
{
2020-07-22 13:20:25 +01:00
public sealed partial class Sidecar
{
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;
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
}
/// <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)
{
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()
{
// For fast debugging, skip checksum
//goto skipImageChecksum;
byte[] data;
long position = 0;
UpdateStatus("Hashing image file...");
InitProgress();
2020-02-29 18:03:35 +00:00
2020-07-20 21:11:32 +01:00
while(position < _fi.Length - 1048576)
{
2020-07-20 21:11:32 +01:00
if(_aborted)
return _sidecar;
2019-04-20 19:21:00 +01:00
data = new byte[1048576];
2020-07-20 21:11:32 +01:00
_fs.Read(data, 0, 1048576);
2020-07-20 21:11:32 +01:00
UpdateProgress("Hashing image file byte {0} of {1}", position, _fi.Length);
2020-07-20 21:11:32 +01:00
_imgChkWorker.Update(data);
position += 1048576;
}
2020-07-20 21:11:32 +01:00
data = new byte[_fi.Length - position];
_fs.Read(data, 0, (int)(_fi.Length - position));
2020-07-20 21:11:32 +01:00
UpdateProgress("Hashing image file byte {0} of {1}", position, _fi.Length);
2020-07-20 21:11:32 +01:00
_imgChkWorker.Update(data);
// For fast debugging, skip checksum
//skipImageChecksum:
EndProgress();
2020-07-20 21:11:32 +01:00
_fs.Close();
2020-07-20 21:11:32 +01:00
List<ChecksumType> imgChecksums = _imgChkWorker.End();
2020-07-20 21:11:32 +01:00
_sidecar.OpticalDisc = null;
_sidecar.BlockMedia = null;
_sidecar.AudioMedia = null;
_sidecar.LinearMedia = null;
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)
{
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);
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.");
}
2017-08-08 13:40:32 +01:00
break;
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;
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;
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;
}
2020-07-20 21:11:32 +01:00
return _sidecar;
}
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()
{
UpdateStatus("Aborting...");
2020-07-20 21:11:32 +01:00
_aborted = true;
2019-04-20 19:21:00 +01:00
}
}
2017-12-19 20:33:03 +00:00
}