// /***************************************************************************
// The Disc Image Chef
// ----------------------------------------------------------------------------
//
// Filename : CreateSidecar.cs
// Version : 1.0
// Author(s) : Natalia Portillo
//
// Component : Component
//
// Revision : $Revision$
// Last change by : $Author$
// Date : $Date$
//
// --[ Description ] ----------------------------------------------------------
//
// Description
//
// --[ 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 (C) 2011-2015 Claunia.com
// ****************************************************************************/
// //$Id$
using System;
using Schemas;
using System.Collections.Generic;
using DiscImageChef.Plugins;
using DiscImageChef.ImagePlugins;
using DiscImageChef.Console;
using DiscImageChef.Checksums;
using System.IO;
using System.Threading;
using DiscImageChef.CommonTypes;
using DiscImageChef.PartPlugins;
namespace DiscImageChef.Commands
{
public static class CreateSidecar
{
public static void doSidecar(CreateSidecarSubOptions options)
{
CICMMetadataType sidecar = new CICMMetadataType();
PluginBase plugins = new PluginBase();
plugins.RegisterAllPlugins();
ImagePlugin _imageFormat;
if (!System.IO.File.Exists(options.InputFile))
{
DicConsole.ErrorWriteLine("Specified file does not exist.");
return;
}
try
{
_imageFormat = ImageFormat.Detect(options.InputFile);
if (_imageFormat == null)
{
DicConsole.WriteLine("Image format not identified, not proceeding with analysis.");
return;
}
else
{
if (options.Verbose)
DicConsole.VerboseWriteLine("Image format identified by {0} ({1}).", _imageFormat.Name, _imageFormat.PluginUUID);
else
DicConsole.WriteLine("Image format identified by {0}.", _imageFormat.Name);
}
try
{
if (!_imageFormat.OpenImage(options.InputFile))
{
DicConsole.WriteLine("Unable to open image format");
DicConsole.WriteLine("No error given");
return;
}
DicConsole.DebugWriteLine("Analyze command", "Correctly opened image file.");
}
catch (Exception ex)
{
DicConsole.ErrorWriteLine("Unable to open image format");
DicConsole.ErrorWriteLine("Error: {0}", ex.Message);
return;
}
Core.Statistics.AddMediaFormat(_imageFormat.GetImageFormat());
FileInfo fi = new FileInfo(options.InputFile);
FileStream fs = new FileStream(options.InputFile, FileMode.Open, FileAccess.Read);
Core.Checksum imgChkWorker = new DiscImageChef.Core.Checksum();
byte[] data;
long position = 0;
while (position < (fi.Length - 1048576))
{
data = new byte[1048576];
fs.Read(data, 0, 1048576);
DicConsole.Write("\rHashing 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));
DicConsole.Write("\rHashing image file byte {0} of {1}", position, fi.Length);
imgChkWorker.Update(data);
DicConsole.WriteLine();
fs.Close();
List imgChecksums = imgChkWorker.End();
switch (_imageFormat.ImageInfo.xmlMediaType)
{
case XmlMediaType.OpticalDisc:
{
sidecar.OpticalDisc = new OpticalDiscType[1];
sidecar.OpticalDisc[0] = new OpticalDiscType();
sidecar.OpticalDisc[0].Checksums = imgChecksums.ToArray();
sidecar.OpticalDisc[0].Image = new ImageType();
sidecar.OpticalDisc[0].Image.format = _imageFormat.GetImageFormat();
sidecar.OpticalDisc[0].Image.offset = 0;
sidecar.OpticalDisc[0].Image.offsetSpecified = true;
sidecar.OpticalDisc[0].Image.Value = Path.GetFileName(options.InputFile);
sidecar.OpticalDisc[0].Size = fi.Length;
sidecar.OpticalDisc[0].Sequence = new SequenceType();
if (_imageFormat.GetMediaSequence() != 0 && _imageFormat.GetLastDiskSequence() != 0)
{
sidecar.OpticalDisc[0].Sequence.MediaSequence = _imageFormat.GetMediaSequence();
sidecar.OpticalDisc[0].Sequence.TotalMedia = _imageFormat.GetMediaSequence();
}
else
{
sidecar.OpticalDisc[0].Sequence.MediaSequence = 1;
sidecar.OpticalDisc[0].Sequence.TotalMedia = 1;
}
sidecar.OpticalDisc[0].Sequence.MediaTitle = _imageFormat.GetImageName();
MediaType dskType = _imageFormat.ImageInfo.mediaType;
foreach (MediaTagType tagType in _imageFormat.ImageInfo.readableMediaTags)
{
switch (tagType)
{
case MediaTagType.CD_ATIP:
sidecar.OpticalDisc[0].ATIP = new DumpType();
sidecar.OpticalDisc[0].ATIP.Checksums = Core.Checksum.GetChecksums(_imageFormat.ReadDiskTag(MediaTagType.CD_ATIP)).ToArray();
sidecar.OpticalDisc[0].ATIP.Size = _imageFormat.ReadDiskTag(MediaTagType.CD_ATIP).Length;
Decoders.CD.ATIP.CDATIP? atip = Decoders.CD.ATIP.Decode(_imageFormat.ReadDiskTag(MediaTagType.CD_ATIP));
if (atip.HasValue)
{
if (atip.Value.DDCD)
dskType = atip.Value.DiscType ? MediaType.DDCDRW : MediaType.DDCDR;
else
dskType = atip.Value.DiscType ? MediaType.CDRW : MediaType.CDR;
}
break;
case MediaTagType.DVD_BCA:
sidecar.OpticalDisc[0].BCA = new DumpType();
sidecar.OpticalDisc[0].BCA.Checksums = Core.Checksum.GetChecksums(_imageFormat.ReadDiskTag(MediaTagType.DVD_BCA)).ToArray();
sidecar.OpticalDisc[0].BCA.Size = _imageFormat.ReadDiskTag(MediaTagType.DVD_BCA).Length;
break;
case MediaTagType.BD_BCA:
sidecar.OpticalDisc[0].BCA = new DumpType();
sidecar.OpticalDisc[0].BCA.Checksums = Core.Checksum.GetChecksums(_imageFormat.ReadDiskTag(MediaTagType.BD_BCA)).ToArray();
sidecar.OpticalDisc[0].BCA.Size = _imageFormat.ReadDiskTag(MediaTagType.BD_BCA).Length;
break;
case MediaTagType.DVD_CMI:
sidecar.OpticalDisc[0].CMI = new DumpType();
Decoders.DVD.CSS_CPRM.LeadInCopyright? cmi = Decoders.DVD.CSS_CPRM.DecodeLeadInCopyright(_imageFormat.ReadDiskTag(MediaTagType.DVD_CMI));
if (cmi.HasValue)
{
switch (cmi.Value.CopyrightType)
{
case Decoders.DVD.CopyrightType.AACS:
sidecar.OpticalDisc[0].CopyProtection = "AACS";
break;
case Decoders.DVD.CopyrightType.CSS:
sidecar.OpticalDisc[0].CopyProtection = "CSS";
break;
case Decoders.DVD.CopyrightType.CPRM:
sidecar.OpticalDisc[0].CopyProtection = "CPRM";
break;
}
}
sidecar.OpticalDisc[0].CMI.Checksums = Core.Checksum.GetChecksums(_imageFormat.ReadDiskTag(MediaTagType.DVD_CMI)).ToArray();
sidecar.OpticalDisc[0].CMI.Size = _imageFormat.ReadDiskTag(MediaTagType.DVD_CMI).Length;
break;
case MediaTagType.DVD_DMI:
sidecar.OpticalDisc[0].DMI = new DumpType();
sidecar.OpticalDisc[0].DMI.Checksums = Core.Checksum.GetChecksums(_imageFormat.ReadDiskTag(MediaTagType.DVD_DMI)).ToArray();
sidecar.OpticalDisc[0].DMI.Size = _imageFormat.ReadDiskTag(MediaTagType.DVD_DMI).Length;
if (Decoders.Xbox.DMI.IsXbox(_imageFormat.ReadDiskTag(MediaTagType.DVD_DMI)))
{
dskType = MediaType.XGD;
sidecar.OpticalDisc[0].Dimensions = new DimensionsType();
sidecar.OpticalDisc[0].Dimensions.Diameter = 120;
}
else if (Decoders.Xbox.DMI.IsXbox360(_imageFormat.ReadDiskTag(MediaTagType.DVD_DMI)))
{
dskType = MediaType.XGD2;
sidecar.OpticalDisc[0].Dimensions = new DimensionsType();
sidecar.OpticalDisc[0].Dimensions.Diameter = 120;
}
break;
case MediaTagType.DVD_PFI:
sidecar.OpticalDisc[0].PFI = new DumpType();
sidecar.OpticalDisc[0].PFI.Checksums = Core.Checksum.GetChecksums(_imageFormat.ReadDiskTag(MediaTagType.DVD_PFI)).ToArray();
sidecar.OpticalDisc[0].PFI.Size = _imageFormat.ReadDiskTag(MediaTagType.DVD_PFI).Length;
Decoders.DVD.PFI.PhysicalFormatInformation? pfi = Decoders.DVD.PFI.Decode(_imageFormat.ReadDiskTag(MediaTagType.DVD_PFI));
if (pfi.HasValue)
{
if (dskType != MediaType.XGD &&
dskType != MediaType.XGD2 &&
dskType != MediaType.XGD3)
{
switch (pfi.Value.DiskCategory)
{
case Decoders.DVD.DiskCategory.DVDPR:
dskType = MediaType.DVDPR;
break;
case Decoders.DVD.DiskCategory.DVDPRDL:
dskType = MediaType.DVDPRDL;
break;
case Decoders.DVD.DiskCategory.DVDPRW:
dskType = MediaType.DVDPRW;
break;
case Decoders.DVD.DiskCategory.DVDPRWDL:
dskType = MediaType.DVDPRWDL;
break;
case Decoders.DVD.DiskCategory.DVDR:
dskType = MediaType.DVDR;
break;
case Decoders.DVD.DiskCategory.DVDRAM:
dskType = MediaType.DVDRAM;
break;
case Decoders.DVD.DiskCategory.DVDROM:
dskType = MediaType.DVDROM;
break;
case Decoders.DVD.DiskCategory.DVDRW:
dskType = MediaType.DVDRW;
break;
case Decoders.DVD.DiskCategory.HDDVDR:
dskType = MediaType.HDDVDR;
break;
case Decoders.DVD.DiskCategory.HDDVDRAM:
dskType = MediaType.HDDVDRAM;
break;
case Decoders.DVD.DiskCategory.HDDVDROM:
dskType = MediaType.HDDVDROM;
break;
case Decoders.DVD.DiskCategory.HDDVDRW:
dskType = MediaType.HDDVDRW;
break;
case Decoders.DVD.DiskCategory.Nintendo:
dskType = MediaType.GOD;
break;
case Decoders.DVD.DiskCategory.UMD:
dskType = MediaType.UMD;
break;
}
if (dskType == MediaType.DVDR && pfi.Value.PartVersion == 6)
dskType = MediaType.DVDRDL;
if (dskType == MediaType.DVDRW && pfi.Value.PartVersion == 3)
dskType = MediaType.DVDRWDL;
if (dskType == MediaType.GOD && pfi.Value.DiscSize == DiscImageChef.Decoders.DVD.DVDSize.OneTwenty)
dskType = MediaType.WOD;
sidecar.OpticalDisc[0].Dimensions = new DimensionsType();
if (dskType == MediaType.UMD)
sidecar.OpticalDisc[0].Dimensions.Diameter = 60;
else if (pfi.Value.DiscSize == DiscImageChef.Decoders.DVD.DVDSize.Eighty)
sidecar.OpticalDisc[0].Dimensions.Diameter = 80;
else if (pfi.Value.DiscSize == DiscImageChef.Decoders.DVD.DVDSize.OneTwenty)
sidecar.OpticalDisc[0].Dimensions.Diameter = 120;
}
}
break;
case MediaTagType.CD_PMA:
sidecar.OpticalDisc[0].PMA = new DumpType();
sidecar.OpticalDisc[0].PMA.Checksums = Core.Checksum.GetChecksums(_imageFormat.ReadDiskTag(MediaTagType.CD_PMA)).ToArray();
sidecar.OpticalDisc[0].PMA.Size = _imageFormat.ReadDiskTag(MediaTagType.CD_PMA).Length;
break;
}
}
string dscType, dscSubType;
Metadata.MediaType.MediaTypeToString(dskType, out dscType, out dscSubType);
sidecar.OpticalDisc[0].DiscType = dscType;
sidecar.OpticalDisc[0].DiscSubType = dscSubType;
Core.Statistics.AddMedia(dskType, false);
try
{
List sessions = _imageFormat.GetSessions();
sidecar.OpticalDisc[0].Sessions = sessions != null ? sessions.Count : 1;
}
catch
{
sidecar.OpticalDisc[0].Sessions = 1;
}
List