// /*************************************************************************** // The Disc Image Chef // ---------------------------------------------------------------------------- // // Filename : Sidecar.cs // Author(s) : Natalia Portillo // // 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 . // // ---------------------------------------------------------------------------- // Copyright © 2011-2019 Natalia Portillo // ****************************************************************************/ using System; using System.Collections.Generic; using System.IO; using System.Text; using DiscImageChef.CommonTypes; using DiscImageChef.CommonTypes.Enums; using DiscImageChef.CommonTypes.Interfaces; using DiscImageChef.Console; using Schemas; namespace DiscImageChef.Core { public partial class Sidecar { 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; public Sidecar() { plugins = GetPluginBase.Instance; imgChkWorker = new Checksum(); aborted = false; } /// Image /// Path to image /// Filter uuid /// Encoding for analysis public Sidecar(IMediaImage image, string imagePath, Guid filterId, Encoding encoding) { this.image = image; this.imagePath = imagePath; this.filterId = filterId; this.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; } /// /// Implements creating a metadata sidecar /// /// The metadata sidecar public CICMMetadataType Create() { // For fast debugging, skip checksum //goto skipImageChecksum; byte[] data; long position = 0; UpdateStatus("Hashing image file..."); InitProgress(); while(position < fi.Length - 1048576) { if(aborted) return sidecar; data = new byte[1048576]; fs.Read(data, 0, 1048576); UpdateProgress("Hashing image file byte {0} of {1}", position, fi.Length); imgChkWorker.Update(data); position += 1048576; } data = new byte[fi.Length - position]; fs.Read(data, 0, (int)(fi.Length - position)); UpdateProgress("Hashing image file byte {0} of {1}", position, fi.Length); imgChkWorker.Update(data); // For fast debugging, skip checksum //skipImageChecksum: EndProgress(); fs.Close(); List imgChecksums = imgChkWorker.End(); sidecar.OpticalDisc = null; sidecar.BlockMedia = null; sidecar.AudioMedia = null; sidecar.LinearMedia = null; if(aborted) return sidecar; switch(image.Info.XmlMediaType) { case XmlMediaType.OpticalDisc: if(image is IOpticalMediaImage opticalImage) OpticalDisc(opticalImage, filterId, imagePath, fi, plugins, imgChecksums, ref sidecar, encoding); else { DicConsole .ErrorWriteLine("The specified image says it contains an optical media but at the same time says it does not support them."); DicConsole.ErrorWriteLine("Please open an issue at Github."); } break; case XmlMediaType.BlockMedia: BlockMedia(image, filterId, imagePath, fi, plugins, imgChecksums, ref sidecar, encoding); break; case XmlMediaType.LinearMedia: LinearMedia(image, filterId, imagePath, fi, plugins, imgChecksums, ref sidecar, encoding); break; case XmlMediaType.AudioMedia: AudioMedia(image, filterId, imagePath, fi, plugins, imgChecksums, ref sidecar, encoding); break; } return sidecar; } public void Abort() { UpdateStatus("Aborting..."); aborted = true; } } }