示例#1
0
        public DecodeMediaTagsViewModel(IMediaImage inputFormat)
        {
            TagsList = new ObservableCollection <MediaTagModel>();

            foreach (MediaTagType tag in inputFormat.Info.ReadableMediaTags)
            {
                try
                {
                    byte[] data = inputFormat.ReadDiskTag(tag);

                    TagsList.Add(new MediaTagModel
                    {
                        Tag = tag, Data = data
                    });
                }
                catch
                {
                    //ignore
                }
            }
        }
示例#2
0
        public static void DoConvert(ConvertImageOptions options)
        {
            DicConsole.DebugWriteLine("Analyze command", "--debug={0}", options.Debug);
            DicConsole.DebugWriteLine("Analyze command", "--verbose={0}", options.Verbose);
            DicConsole.DebugWriteLine("Analyze command", "--input={0}", options.InputFile);
            DicConsole.DebugWriteLine("Analyze command", "--output={0}", options.OutputFile);
            DicConsole.DebugWriteLine("Analyze command", "--format={0}", options.OutputFormat);
            DicConsole.DebugWriteLine("Analyze command", "--count={0}", options.Count);
            DicConsole.DebugWriteLine("Analyze command", "--force={0}", options.Force);
            DicConsole.DebugWriteLine("Analyze command", "--creator={0}", options.Creator);
            DicConsole.DebugWriteLine("Analyze command", "--media-title={0}", options.MediaTitle);
            DicConsole.DebugWriteLine("Analyze command", "--comments={0}", options.Comments);
            DicConsole.DebugWriteLine("Analyze command", "--media-manufacturer={0}", options.MediaManufacturer);
            DicConsole.DebugWriteLine("Analyze command", "--media-model={0}", options.MediaModel);
            DicConsole.DebugWriteLine("Analyze command", "--media-serial={0}", options.MediaSerialNumber);
            DicConsole.DebugWriteLine("Analyze command", "--media-barcode={0}", options.MediaBarcode);
            DicConsole.DebugWriteLine("Analyze command", "--media-partnumber={0}", options.MediaPartNumber);
            DicConsole.DebugWriteLine("Analyze command", "--media-sequence={0}", options.MediaSequence);
            DicConsole.DebugWriteLine("Analyze command", "--media-lastsequence={0}", options.LastMediaSequence);
            DicConsole.DebugWriteLine("Analyze command", "--drive-manufacturer={0}", options.DriveManufacturer);
            DicConsole.DebugWriteLine("Analyze command", "--drive-model={0}", options.DriveModel);
            DicConsole.DebugWriteLine("Analyze command", "--drive-serial={0}", options.DriveSerialNumber);
            DicConsole.DebugWriteLine("Analyze command", "--drive-revision={0}", options.DriveFirmwareRevision);
            DicConsole.DebugWriteLine("Analyze command", "--cicm-xml={0}", options.CicmXml);
            DicConsole.DebugWriteLine("Analyze command", "--resume-file={0}", options.ResumeFile);
            DicConsole.DebugWriteLine("Analyze command", "--options={0}", options.Options);

            Dictionary <string, string> parsedOptions = Options.Parse(options.Options);

            DicConsole.DebugWriteLine("Analyze command", "Parsed options:");
            foreach (KeyValuePair <string, string> parsedOption in parsedOptions)
            {
                DicConsole.DebugWriteLine("Analyze command", "{0} = {1}", parsedOption.Key, parsedOption.Value);
            }

            if (options.Count == 0)
            {
                DicConsole.ErrorWriteLine("Need to specify more than 0 sectors to copy at once");
                return;
            }

            Resume           resume  = null;
            CICMMetadataType sidecar = null;

            XmlSerializer xs = new XmlSerializer(typeof(CICMMetadataType));

            if (options.CicmXml != null)
            {
                if (File.Exists(options.CicmXml))
                {
                    try
                    {
                        StreamReader sr = new StreamReader(options.CicmXml);
                        sidecar = (CICMMetadataType)xs.Deserialize(sr);
                        sr.Close();
                    }
                    catch
                    {
                        DicConsole.ErrorWriteLine("Incorrect metadata sidecar file, not continuing...");
                        return;
                    }
                }
                else
                {
                    DicConsole.ErrorWriteLine("Could not find metadata sidecar, not continuing...");
                    return;
                }
            }

            xs = new XmlSerializer(typeof(Resume));
            if (options.ResumeFile != null)
            {
                if (File.Exists(options.ResumeFile))
                {
                    try
                    {
                        StreamReader sr = new StreamReader(options.ResumeFile);
                        resume = (Resume)xs.Deserialize(sr);
                        sr.Close();
                    }
                    catch
                    {
                        DicConsole.ErrorWriteLine("Incorrect resume file, not continuing...");
                        return;
                    }
                }
                else
                {
                    DicConsole.ErrorWriteLine("Could not find resume file, not continuing...");
                    return;
                }
            }

            FiltersList filtersList = new FiltersList();
            IFilter     inputFilter = filtersList.GetFilter(options.InputFile);

            if (inputFilter == null)
            {
                DicConsole.ErrorWriteLine("Cannot open specified file.");
                return;
            }

            if (File.Exists(options.OutputFile))
            {
                DicConsole.ErrorWriteLine("Output file already exists, not continuing.");
                return;
            }

            PluginBase  plugins     = GetPluginBase.Instance;
            IMediaImage inputFormat = ImageFormat.Detect(inputFilter);

            if (inputFormat == null)
            {
                DicConsole.WriteLine("Input image format not identified, not proceeding with conversion.");
                return;
            }

            if (options.Verbose)
            {
                DicConsole.VerboseWriteLine("Input image format identified by {0} ({1}).", inputFormat.Name,
                                            inputFormat.Id);
            }
            else
            {
                DicConsole.WriteLine("Input image format identified by {0}.", inputFormat.Name);
            }

            try
            {
                if (!inputFormat.Open(inputFilter))
                {
                    DicConsole.WriteLine("Unable to open image format");
                    DicConsole.WriteLine("No error given");
                    return;
                }

                DicConsole.DebugWriteLine("Convert-image command", "Correctly opened image file.");
                DicConsole.DebugWriteLine("Convert-image command", "Image without headers is {0} bytes.",
                                          inputFormat.Info.ImageSize);
                DicConsole.DebugWriteLine("Convert-image command", "Image has {0} sectors.", inputFormat.Info.Sectors);
                DicConsole.DebugWriteLine("Convert-image command", "Image identifies media type as {0}.",
                                          inputFormat.Info.MediaType);

                Core.Statistics.AddMediaFormat(inputFormat.Format);
                Core.Statistics.AddMedia(inputFormat.Info.MediaType, false);
                Core.Statistics.AddFilter(inputFilter.Name);
            }
            catch (Exception ex)
            {
                DicConsole.ErrorWriteLine("Unable to open image format");
                DicConsole.ErrorWriteLine("Error: {0}", ex.Message);
                DicConsole.DebugWriteLine("Convert-image command", "Stack trace: {0}", ex.StackTrace);
                return;
            }

            List <IWritableImage> candidates = new List <IWritableImage>();

            // Try extension
            if (string.IsNullOrEmpty(options.OutputFormat))
            {
                candidates.AddRange(plugins.WritableImages.Values.Where(t =>
                                                                        t.KnownExtensions
                                                                        .Contains(Path.GetExtension(options
                                                                                                    .OutputFile))));
            }
            // Try Id
            else if (Guid.TryParse(options.OutputFormat, out Guid outId))
            {
                candidates.AddRange(plugins.WritableImages.Values.Where(t => t.Id.Equals(outId)));
            }
            // Try name
            else
            {
                candidates.AddRange(plugins.WritableImages.Values.Where(t => string.Equals(t.Name, options.OutputFormat,
                                                                                           StringComparison
                                                                                           .InvariantCultureIgnoreCase)));
            }

            if (candidates.Count == 0)
            {
                DicConsole.WriteLine("No plugin supports requested extension.");
                return;
            }

            if (candidates.Count > 1)
            {
                DicConsole.WriteLine("More than one plugin supports requested extension.");
                return;
            }

            IWritableImage outputFormat = candidates[0];

            if (options.Verbose)
            {
                DicConsole.VerboseWriteLine("Output image format: {0} ({1}).", outputFormat.Name, outputFormat.Id);
            }
            else
            {
                DicConsole.WriteLine("Output image format: {0}.", outputFormat.Name);
            }

            if (!outputFormat.SupportedMediaTypes.Contains(inputFormat.Info.MediaType))
            {
                DicConsole.ErrorWriteLine("Output format does not support media type, cannot continue...");
                return;
            }

            foreach (MediaTagType mediaTag in inputFormat.Info.ReadableMediaTags)
            {
                if (outputFormat.SupportedMediaTags.Contains(mediaTag) || options.Force)
                {
                    continue;
                }

                DicConsole.ErrorWriteLine("Converting image will lose media tag {0}, not continuing...", mediaTag);
                DicConsole.ErrorWriteLine("If you don't care, use force option.");
                return;
            }

            bool useLong = inputFormat.Info.ReadableSectorTags.Count != 0;

            foreach (SectorTagType sectorTag in inputFormat.Info.ReadableSectorTags)
            {
                if (outputFormat.SupportedSectorTags.Contains(sectorTag))
                {
                    continue;
                }

                if (options.Force)
                {
                    if (sectorTag != SectorTagType.CdTrackFlags && sectorTag != SectorTagType.CdTrackIsrc &&
                        sectorTag != SectorTagType.CdSectorSubchannel)
                    {
                        useLong = false;
                    }
                    continue;
                }

                DicConsole.ErrorWriteLine("Converting image will lose sector tag {0}, not continuing...", sectorTag);
                DicConsole
                .ErrorWriteLine("If you don't care, use force option. This will skip all sector tags converting only user data.");
                return;
            }

            if (!outputFormat.Create(options.OutputFile, inputFormat.Info.MediaType, parsedOptions,
                                     inputFormat.Info.Sectors, inputFormat.Info.SectorSize))
            {
                DicConsole.ErrorWriteLine("Error {0} creating output image.", outputFormat.ErrorMessage);
                return;
            }

            CommonTypes.Structs.ImageInfo metadata = new CommonTypes.Structs.ImageInfo
            {
                Application           = "DiscImageChef",
                ApplicationVersion    = Version.GetVersion(),
                Comments              = options.Comments ?? inputFormat.Info.Comments,
                Creator               = options.Creator ?? inputFormat.Info.Creator,
                DriveFirmwareRevision = options.DriveFirmwareRevision ?? inputFormat.Info.DriveFirmwareRevision,
                DriveManufacturer     = options.DriveManufacturer ?? inputFormat.Info.DriveManufacturer,
                DriveModel            = options.DriveModel ?? inputFormat.Info.DriveModel,
                DriveSerialNumber     = options.DriveSerialNumber ?? inputFormat.Info.DriveSerialNumber,
                LastMediaSequence     =
                    options.LastMediaSequence != 0 ? options.LastMediaSequence : inputFormat.Info.LastMediaSequence,
                MediaBarcode      = options.MediaBarcode ?? inputFormat.Info.MediaBarcode,
                MediaManufacturer = options.MediaManufacturer ?? inputFormat.Info.MediaManufacturer,
                MediaModel        = options.MediaModel ?? inputFormat.Info.MediaModel,
                MediaPartNumber   = options.MediaPartNumber ?? inputFormat.Info.MediaPartNumber,
                MediaSequence     = options.MediaSequence != 0 ? options.MediaSequence : inputFormat.Info.MediaSequence,
                MediaSerialNumber = options.MediaSerialNumber ?? inputFormat.Info.MediaSerialNumber,
                MediaTitle        = options.MediaTitle ?? inputFormat.Info.MediaTitle
            };

            if (!outputFormat.SetMetadata(metadata))
            {
                DicConsole.ErrorWrite("Error {0} setting metadata, ", outputFormat.ErrorMessage);
                if (!options.Force)
                {
                    DicConsole.ErrorWriteLine("not continuing...");
                    return;
                }

                DicConsole.ErrorWriteLine("continuing...");
            }

            List <Track> tracks;

            try { tracks = inputFormat.Tracks; }
            catch (Exception) { tracks = null; }

            CICMMetadataType        cicmMetadata = inputFormat.CicmMetadata;
            List <DumpHardwareType> dumpHardware = inputFormat.DumpHardware;

            if (tracks != null)
            {
                if (!outputFormat.SetTracks(tracks))
                {
                    DicConsole.ErrorWriteLine("Error {0} sending tracks list to output image.",
                                              outputFormat.ErrorMessage);
                    return;
                }
            }

            foreach (MediaTagType mediaTag in inputFormat.Info.ReadableMediaTags)
            {
                if (options.Force && !outputFormat.SupportedMediaTags.Contains(mediaTag))
                {
                    continue;
                }

                DicConsole.WriteLine("Converting media tag {0}", mediaTag);
                byte[] tag = inputFormat.ReadDiskTag(mediaTag);
                if (outputFormat.WriteMediaTag(tag, mediaTag))
                {
                    continue;
                }

                if (options.Force)
                {
                    DicConsole.ErrorWriteLine("Error {0} writing media tag, continuing...", outputFormat.ErrorMessage);
                }
                else
                {
                    DicConsole.ErrorWriteLine("Error {0} writing media tag, not continuing...",
                                              outputFormat.ErrorMessage);
                    return;
                }
            }

            DicConsole.WriteLine("{0} sectors to convert", inputFormat.Info.Sectors);
            ulong doneSectors = 0;

            if (tracks == null)
            {
                DicConsole.WriteLine("Setting geometry to {0} cylinders, {1} heads and {2} sectors per track",
                                     inputFormat.Info.Cylinders, inputFormat.Info.Heads,
                                     inputFormat.Info.SectorsPerTrack);
                if (!outputFormat.SetGeometry(inputFormat.Info.Cylinders, inputFormat.Info.Heads,
                                              inputFormat.Info.SectorsPerTrack))
                {
                    DicConsole.ErrorWriteLine("Error {0} setting geometry, image may be incorrect, continuing...",
                                              outputFormat.ErrorMessage);
                }

                while (doneSectors < inputFormat.Info.Sectors)
                {
                    byte[] sector;

                    uint sectorsToDo;
                    if (inputFormat.Info.Sectors - doneSectors >= (ulong)options.Count)
                    {
                        sectorsToDo = (uint)options.Count;
                    }
                    else
                    {
                        sectorsToDo = (uint)(inputFormat.Info.Sectors - doneSectors);
                    }

                    DicConsole.Write("\rConverting sectors {0} to {1} ({2:P2} done)", doneSectors,
                                     doneSectors + sectorsToDo, doneSectors / (double)inputFormat.Info.Sectors);

                    bool result;
                    if (useLong)
                    {
                        if (sectorsToDo == 1)
                        {
                            sector = inputFormat.ReadSectorLong(doneSectors);
                            result = outputFormat.WriteSectorLong(sector, doneSectors);
                        }
                        else
                        {
                            sector = inputFormat.ReadSectorsLong(doneSectors, sectorsToDo);
                            result = outputFormat.WriteSectorsLong(sector, doneSectors, sectorsToDo);
                        }
                    }
                    else
                    {
                        if (sectorsToDo == 1)
                        {
                            sector = inputFormat.ReadSector(doneSectors);
                            result = outputFormat.WriteSector(sector, doneSectors);
                        }
                        else
                        {
                            sector = inputFormat.ReadSectors(doneSectors, sectorsToDo);
                            result = outputFormat.WriteSectors(sector, doneSectors, sectorsToDo);
                        }
                    }

                    if (!result)
                    {
                        if (options.Force)
                        {
                            DicConsole.ErrorWriteLine("Error {0} writing sector {1}, continuing...",
                                                      outputFormat.ErrorMessage, doneSectors);
                        }
                        else
                        {
                            DicConsole.ErrorWriteLine("Error {0} writing sector {1}, not continuing...",
                                                      outputFormat.ErrorMessage, doneSectors);
                            return;
                        }
                    }

                    doneSectors += sectorsToDo;
                }

                DicConsole.Write("\rConverting sectors {0} to {1} ({2:P2} done)", inputFormat.Info.Sectors,
                                 inputFormat.Info.Sectors, 1.0);
                DicConsole.WriteLine();

                foreach (SectorTagType tag in inputFormat.Info.ReadableSectorTags)
                {
                    if (!useLong)
                    {
                        break;
                    }

                    switch (tag)
                    {
                    case SectorTagType.AppleSectorTag:
                    case SectorTagType.CdSectorSync:
                    case SectorTagType.CdSectorHeader:
                    case SectorTagType.CdSectorSubHeader:
                    case SectorTagType.CdSectorEdc:
                    case SectorTagType.CdSectorEccP:
                    case SectorTagType.CdSectorEccQ:
                    case SectorTagType.CdSectorEcc:
                        // This tags are inline in long sector
                        continue;
                    }

                    if (options.Force && !outputFormat.SupportedSectorTags.Contains(tag))
                    {
                        continue;
                    }

                    doneSectors = 0;
                    while (doneSectors < inputFormat.Info.Sectors)
                    {
                        byte[] sector;

                        uint sectorsToDo;
                        if (inputFormat.Info.Sectors - doneSectors >= (ulong)options.Count)
                        {
                            sectorsToDo = (uint)options.Count;
                        }
                        else
                        {
                            sectorsToDo = (uint)(inputFormat.Info.Sectors - doneSectors);
                        }

                        DicConsole.Write("\rConverting tag {2} for sectors {0} to {1} ({2:P2} done)", doneSectors,
                                         doneSectors + sectorsToDo, doneSectors / (double)inputFormat.Info.Sectors,
                                         tag);

                        bool result;
                        if (sectorsToDo == 1)
                        {
                            sector = inputFormat.ReadSectorTag(doneSectors, tag);
                            result = outputFormat.WriteSectorTag(sector, doneSectors, tag);
                        }
                        else
                        {
                            sector = inputFormat.ReadSectorsTag(doneSectors, sectorsToDo, tag);
                            result = outputFormat.WriteSectorsTag(sector, doneSectors, sectorsToDo, tag);
                        }

                        if (!result)
                        {
                            if (options.Force)
                            {
                                DicConsole.ErrorWriteLine("Error {0} writing sector {1}, continuing...",
                                                          outputFormat.ErrorMessage, doneSectors);
                            }
                            else
                            {
                                DicConsole.ErrorWriteLine("Error {0} writing sector {1}, not continuing...",
                                                          outputFormat.ErrorMessage, doneSectors);
                                return;
                            }
                        }

                        doneSectors += sectorsToDo;
                    }

                    DicConsole.Write("\rConverting tag {2} for sectors {0} to {1} ({2:P2} done)",
                                     inputFormat.Info.Sectors, inputFormat.Info.Sectors, 1.0, tag);
                    DicConsole.WriteLine();
                }
            }
            else
            {
                foreach (Track track in tracks)
                {
                    doneSectors = 0;
                    ulong trackSectors = track.TrackEndSector - track.TrackStartSector + 1;

                    while (doneSectors < trackSectors)
                    {
                        byte[] sector;

                        uint sectorsToDo;
                        if (trackSectors - doneSectors >= (ulong)options.Count)
                        {
                            sectorsToDo = (uint)options.Count;
                        }
                        else
                        {
                            sectorsToDo =
                                (uint)(trackSectors - doneSectors);
                        }

                        DicConsole.Write("\rConverting sectors {0} to {1} in track {3} ({2:P2} done)",
                                         doneSectors + track.TrackStartSector,
                                         doneSectors + sectorsToDo + track.TrackStartSector,
                                         (doneSectors + track.TrackStartSector) / (double)inputFormat.Info.Sectors,
                                         track.TrackSequence);

                        bool result;
                        if (useLong)
                        {
                            if (sectorsToDo == 1)
                            {
                                sector = inputFormat.ReadSectorLong(doneSectors + track.TrackStartSector);
                                result = outputFormat.WriteSectorLong(sector, doneSectors + track.TrackStartSector);
                            }
                            else
                            {
                                sector = inputFormat.ReadSectorsLong(doneSectors + track.TrackStartSector, sectorsToDo);
                                result = outputFormat.WriteSectorsLong(sector, doneSectors + track.TrackStartSector,
                                                                       sectorsToDo);
                            }
                        }
                        else
                        {
                            if (sectorsToDo == 1)
                            {
                                sector = inputFormat.ReadSector(doneSectors + track.TrackStartSector);
                                result = outputFormat.WriteSector(sector, doneSectors + track.TrackStartSector);
                            }
                            else
                            {
                                sector = inputFormat.ReadSectors(doneSectors + track.TrackStartSector, sectorsToDo);
                                result = outputFormat.WriteSectors(sector, doneSectors + track.TrackStartSector,
                                                                   sectorsToDo);
                            }
                        }

                        if (!result)
                        {
                            if (options.Force)
                            {
                                DicConsole.ErrorWriteLine("Error {0} writing sector {1}, continuing...",
                                                          outputFormat.ErrorMessage, doneSectors);
                            }
                            else
                            {
                                DicConsole.ErrorWriteLine("Error {0} writing sector {1}, not continuing...",
                                                          outputFormat.ErrorMessage, doneSectors);
                                return;
                            }
                        }

                        doneSectors += sectorsToDo;
                    }
                }

                DicConsole.Write("\rConverting sectors {0} to {1} in track {3} ({2:P2} done)", inputFormat.Info.Sectors,
                                 inputFormat.Info.Sectors, 1.0, tracks.Count);
                DicConsole.WriteLine();

                foreach (SectorTagType tag in inputFormat.Info.ReadableSectorTags.OrderBy(t => t))
                {
                    if (!useLong)
                    {
                        break;
                    }

                    switch (tag)
                    {
                    case SectorTagType.AppleSectorTag:
                    case SectorTagType.CdSectorSync:
                    case SectorTagType.CdSectorHeader:
                    case SectorTagType.CdSectorSubHeader:
                    case SectorTagType.CdSectorEdc:
                    case SectorTagType.CdSectorEccP:
                    case SectorTagType.CdSectorEccQ:
                    case SectorTagType.CdSectorEcc:
                        // This tags are inline in long sector
                        continue;
                    }

                    if (options.Force && !outputFormat.SupportedSectorTags.Contains(tag))
                    {
                        continue;
                    }

                    foreach (Track track in tracks)
                    {
                        doneSectors = 0;
                        ulong  trackSectors = track.TrackEndSector - track.TrackStartSector + 1;
                        byte[] sector;
                        bool   result;

                        switch (tag)
                        {
                        case SectorTagType.CdTrackFlags:
                        case SectorTagType.CdTrackIsrc:
                            DicConsole.Write("\rConverting tag {0} in track {1} ({2:P2} done).", tag,
                                             track.TrackSequence, track.TrackSequence / (double)tracks.Count);
                            sector = inputFormat.ReadSectorTag(track.TrackStartSector, tag);
                            result = outputFormat.WriteSectorTag(sector, track.TrackStartSector, tag);
                            if (!result)
                            {
                                if (options.Force)
                                {
                                    DicConsole.ErrorWriteLine("Error {0} writing tag, continuing...",
                                                              outputFormat.ErrorMessage);
                                }
                                else
                                {
                                    DicConsole.ErrorWriteLine("Error {0} writing tag, not continuing...",
                                                              outputFormat.ErrorMessage);
                                    return;
                                }
                            }

                            continue;
                        }

                        while (doneSectors < trackSectors)
                        {
                            uint sectorsToDo;
                            if (trackSectors - doneSectors >= (ulong)options.Count)
                            {
                                sectorsToDo = (uint)options.Count;
                            }
                            else
                            {
                                sectorsToDo =
                                    (uint)(trackSectors - doneSectors);
                            }

                            DicConsole.Write("\rConverting tag {4} for sectors {0} to {1} in track {3} ({2:P2} done)",
                                             doneSectors + track.TrackStartSector,
                                             doneSectors + sectorsToDo + track.TrackStartSector,
                                             (doneSectors + track.TrackStartSector) / (double)inputFormat.Info.Sectors,
                                             track.TrackSequence, tag);

                            if (sectorsToDo == 1)
                            {
                                sector = inputFormat.ReadSectorTag(doneSectors + track.TrackStartSector, tag);
                                result = outputFormat.WriteSectorTag(sector, doneSectors + track.TrackStartSector, tag);
                            }
                            else
                            {
                                sector = inputFormat.ReadSectorsTag(doneSectors + track.TrackStartSector, sectorsToDo,
                                                                    tag);
                                result = outputFormat.WriteSectorsTag(sector, doneSectors + track.TrackStartSector,
                                                                      sectorsToDo, tag);
                            }

                            if (!result)
                            {
                                if (options.Force)
                                {
                                    DicConsole.ErrorWriteLine("Error {0} writing tag for sector {1}, continuing...",
                                                              outputFormat.ErrorMessage, doneSectors);
                                }
                                else
                                {
                                    DicConsole.ErrorWriteLine("Error {0} writing tag for sector {1}, not continuing...",
                                                              outputFormat.ErrorMessage, doneSectors);
                                    return;
                                }
                            }

                            doneSectors += sectorsToDo;
                        }
                    }

                    switch (tag)
                    {
                    case SectorTagType.CdTrackFlags:
                    case SectorTagType.CdTrackIsrc:
                        DicConsole.Write("\rConverting tag {0} in track {1} ({2:P2} done).", tag, tracks.Count,
                                         1.0);
                        break;

                    default:
                        DicConsole.Write("\rConverting tag {4} for sectors {0} to {1} in track {3} ({2:P2} done)",
                                         inputFormat.Info.Sectors, inputFormat.Info.Sectors, 1.0, tracks.Count,
                                         tag);
                        break;
                    }

                    DicConsole.WriteLine();
                }
            }

            bool ret = false;

            if (resume != null || dumpHardware != null)
            {
                if (resume != null)
                {
                    ret = outputFormat.SetDumpHardware(resume.Tries);
                }
                else if (dumpHardware != null)
                {
                    ret = outputFormat.SetDumpHardware(dumpHardware);
                }
                if (ret)
                {
                    DicConsole.WriteLine("Written dump hardware list to output image.");
                }
            }

            ret = false;
            if (sidecar != null || cicmMetadata != null)
            {
                if (sidecar != null)
                {
                    ret = outputFormat.SetCicmMetadata(sidecar);
                }
                else if (cicmMetadata != null)
                {
                    ret = outputFormat.SetCicmMetadata(cicmMetadata);
                }
                if (ret)
                {
                    DicConsole.WriteLine("Written CICM XML metadata to output image.");
                }
            }

            DicConsole.WriteLine("Closing output image.");

            if (!outputFormat.Close())
            {
                DicConsole.ErrorWriteLine("Error {0} closing output image... Contents are not correct.",
                                          outputFormat.ErrorMessage);
            }

            DicConsole.WriteLine();
            DicConsole.WriteLine("Conversion done.");

            Core.Statistics.AddCommand("convert-image");
        }
示例#3
0
        public static int Invoke(bool verbose, bool debug, string cicmXml, string comments, int count, string creator,
                                 string driveFirmwareRevision, string driveManufacturer, string driveModel,
                                 string driveSerialNumber, bool force, string inputPath, int lastMediaSequence,
                                 string mediaBarcode, string mediaManufacturer, string mediaModel,
                                 string mediaPartNumber, int mediaSequence, string mediaSerialNumber, string mediaTitle,
                                 string outputPath, string outputOptions, string resumeFile, string format)
        {
            MainClass.PrintCopyright();

            if (debug)
            {
                AaruConsole.DebugWriteLineEvent += System.Console.Error.WriteLine;
            }

            if (verbose)
            {
                AaruConsole.VerboseWriteLineEvent += System.Console.WriteLine;
            }

            Statistics.AddCommand("convert-image");

            AaruConsole.DebugWriteLine("Analyze command", "--cicm-xml={0}", cicmXml);
            AaruConsole.DebugWriteLine("Analyze command", "--comments={0}", comments);
            AaruConsole.DebugWriteLine("Analyze command", "--count={0}", count);
            AaruConsole.DebugWriteLine("Analyze command", "--creator={0}", creator);
            AaruConsole.DebugWriteLine("Analyze command", "--debug={0}", debug);
            AaruConsole.DebugWriteLine("Analyze command", "--drive-manufacturer={0}", driveManufacturer);
            AaruConsole.DebugWriteLine("Analyze command", "--drive-model={0}", driveModel);
            AaruConsole.DebugWriteLine("Analyze command", "--drive-revision={0}", driveFirmwareRevision);
            AaruConsole.DebugWriteLine("Analyze command", "--drive-serial={0}", driveSerialNumber);
            AaruConsole.DebugWriteLine("Analyze command", "--force={0}", force);
            AaruConsole.DebugWriteLine("Analyze command", "--format={0}", format);
            AaruConsole.DebugWriteLine("Analyze command", "--input={0}", inputPath);
            AaruConsole.DebugWriteLine("Analyze command", "--media-barcode={0}", mediaBarcode);
            AaruConsole.DebugWriteLine("Analyze command", "--media-lastsequence={0}", lastMediaSequence);
            AaruConsole.DebugWriteLine("Analyze command", "--media-manufacturer={0}", mediaManufacturer);
            AaruConsole.DebugWriteLine("Analyze command", "--media-model={0}", mediaModel);
            AaruConsole.DebugWriteLine("Analyze command", "--media-partnumber={0}", mediaPartNumber);
            AaruConsole.DebugWriteLine("Analyze command", "--media-sequence={0}", mediaSequence);
            AaruConsole.DebugWriteLine("Analyze command", "--media-serial={0}", mediaSerialNumber);
            AaruConsole.DebugWriteLine("Analyze command", "--media-title={0}", mediaTitle);
            AaruConsole.DebugWriteLine("Analyze command", "--options={0}", outputOptions);
            AaruConsole.DebugWriteLine("Analyze command", "--output={0}", outputPath);
            AaruConsole.DebugWriteLine("Analyze command", "--resume-file={0}", resumeFile);
            AaruConsole.DebugWriteLine("Analyze command", "--verbose={0}", verbose);

            Dictionary <string, string> parsedOptions = Core.Options.Parse(outputOptions);

            AaruConsole.DebugWriteLine("Analyze command", "Parsed options:");

            foreach (KeyValuePair <string, string> parsedOption in parsedOptions)
            {
                AaruConsole.DebugWriteLine("Analyze command", "{0} = {1}", parsedOption.Key, parsedOption.Value);
            }

            if (count == 0)
            {
                AaruConsole.ErrorWriteLine("Need to specify more than 0 sectors to copy at once");

                return((int)ErrorNumber.InvalidArgument);
            }

            Resume           resume  = null;
            CICMMetadataType sidecar = null;
            MediaType        mediaType;

            var xs = new XmlSerializer(typeof(CICMMetadataType));

            if (cicmXml != null)
            {
                if (File.Exists(cicmXml))
                {
                    try
                    {
                        var sr = new StreamReader(cicmXml);
                        sidecar = (CICMMetadataType)xs.Deserialize(sr);
                        sr.Close();
                    }
                    catch (Exception ex)
                    {
                        AaruConsole.ErrorWriteLine("Incorrect metadata sidecar file, not continuing...");
                        AaruConsole.DebugWriteLine("Image conversion", $"{ex}");

                        return((int)ErrorNumber.InvalidSidecar);
                    }
                }
                else
                {
                    AaruConsole.ErrorWriteLine("Could not find metadata sidecar, not continuing...");

                    return((int)ErrorNumber.FileNotFound);
                }
            }

            xs = new XmlSerializer(typeof(Resume));

            if (resumeFile != null)
            {
                if (File.Exists(resumeFile))
                {
                    try
                    {
                        var sr = new StreamReader(resumeFile);
                        resume = (Resume)xs.Deserialize(sr);
                        sr.Close();
                    }
                    catch (Exception ex)
                    {
                        AaruConsole.ErrorWriteLine("Incorrect resume file, not continuing...");
                        AaruConsole.DebugWriteLine("Image conversion", $"{ex}");

                        return((int)ErrorNumber.InvalidResume);
                    }
                }
                else
                {
                    AaruConsole.ErrorWriteLine("Could not find resume file, not continuing...");

                    return((int)ErrorNumber.FileNotFound);
                }
            }

            var     filtersList = new FiltersList();
            IFilter inputFilter = filtersList.GetFilter(inputPath);

            if (inputFilter == null)
            {
                AaruConsole.ErrorWriteLine("Cannot open specified file.");

                return((int)ErrorNumber.CannotOpenFile);
            }

            if (File.Exists(outputPath))
            {
                AaruConsole.ErrorWriteLine("Output file already exists, not continuing.");

                return((int)ErrorNumber.DestinationExists);
            }

            PluginBase  plugins     = GetPluginBase.Instance;
            IMediaImage inputFormat = ImageFormat.Detect(inputFilter);

            if (inputFormat == null)
            {
                AaruConsole.WriteLine("Input image format not identified, not proceeding with conversion.");

                return((int)ErrorNumber.UnrecognizedFormat);
            }

            if (verbose)
            {
                AaruConsole.VerboseWriteLine("Input image format identified by {0} ({1}).", inputFormat.Name,
                                             inputFormat.Id);
            }
            else
            {
                AaruConsole.WriteLine("Input image format identified by {0}.", inputFormat.Name);
            }

            try
            {
                if (!inputFormat.Open(inputFilter))
                {
                    AaruConsole.WriteLine("Unable to open image format");
                    AaruConsole.WriteLine("No error given");

                    return((int)ErrorNumber.CannotOpenFormat);
                }

                mediaType = inputFormat.Info.MediaType;

                // Obsolete types
                #pragma warning disable 612
                switch (mediaType)
                {
                case MediaType.SQ1500:
                    mediaType = MediaType.SyJet;

                    break;

                case MediaType.Bernoulli:
                    mediaType = MediaType.Bernoulli10;

                    break;

                case MediaType.Bernoulli2:
                    mediaType = MediaType.BernoulliBox2_20;

                    break;
                }
                #pragma warning restore 612

                AaruConsole.DebugWriteLine("Convert-image command", "Correctly opened image file.");

                AaruConsole.DebugWriteLine("Convert-image command", "Image without headers is {0} bytes.",
                                           inputFormat.Info.ImageSize);

                AaruConsole.DebugWriteLine("Convert-image command", "Image has {0} sectors.", inputFormat.Info.Sectors);

                AaruConsole.DebugWriteLine("Convert-image command", "Image identifies media type as {0}.", mediaType);

                Statistics.AddMediaFormat(inputFormat.Format);
                Statistics.AddMedia(mediaType, false);
                Statistics.AddFilter(inputFilter.Name);
            }
            catch (Exception ex)
            {
                AaruConsole.ErrorWriteLine("Unable to open image format");
                AaruConsole.ErrorWriteLine("Error: {0}", ex.Message);
                AaruConsole.DebugWriteLine("Convert-image command", "Stack trace: {0}", ex.StackTrace);

                return((int)ErrorNumber.CannotOpenFormat);
            }

            List <IWritableImage> candidates = new List <IWritableImage>();

            // Try extension
            if (string.IsNullOrEmpty(format))
            {
                candidates.AddRange(plugins.WritableImages.Values.Where(t =>
                                                                        t.KnownExtensions.
                                                                        Contains(Path.GetExtension(outputPath))));
            }

            // Try Id
            else if (Guid.TryParse(format, out Guid outId))
            {
                candidates.AddRange(plugins.WritableImages.Values.Where(t => t.Id.Equals(outId)));
            }

            // Try name
            else
            {
                candidates.AddRange(plugins.WritableImages.Values.Where(t => string.Equals(t.Name, format,
                                                                                           StringComparison.
                                                                                           InvariantCultureIgnoreCase)));
            }

            if (candidates.Count == 0)
            {
                AaruConsole.WriteLine("No plugin supports requested extension.");

                return((int)ErrorNumber.FormatNotFound);
            }

            if (candidates.Count > 1)
            {
                AaruConsole.WriteLine("More than one plugin supports requested extension.");

                return((int)ErrorNumber.TooManyFormats);
            }

            IWritableImage outputFormat = candidates[0];

            if (verbose)
            {
                AaruConsole.VerboseWriteLine("Output image format: {0} ({1}).", outputFormat.Name, outputFormat.Id);
            }
            else
            {
                AaruConsole.WriteLine("Output image format: {0}.", outputFormat.Name);
            }

            if (!outputFormat.SupportedMediaTypes.Contains(mediaType))
            {
                AaruConsole.ErrorWriteLine("Output format does not support media type, cannot continue...");

                return((int)ErrorNumber.UnsupportedMedia);
            }

            foreach (MediaTagType mediaTag in inputFormat.Info.ReadableMediaTags)
            {
                if (outputFormat.SupportedMediaTags.Contains(mediaTag) || force)
                {
                    continue;
                }

                AaruConsole.ErrorWriteLine("Converting image will lose media tag {0}, not continuing...", mediaTag);
                AaruConsole.ErrorWriteLine("If you don't care, use force option.");

                return((int)ErrorNumber.DataWillBeLost);
            }

            bool useLong = inputFormat.Info.ReadableSectorTags.Count != 0;

            foreach (SectorTagType sectorTag in inputFormat.Info.ReadableSectorTags)
            {
                if (outputFormat.SupportedSectorTags.Contains(sectorTag))
                {
                    continue;
                }

                if (force)
                {
                    if (sectorTag != SectorTagType.CdTrackFlags &&
                        sectorTag != SectorTagType.CdTrackIsrc &&
                        sectorTag != SectorTagType.CdSectorSubchannel)
                    {
                        useLong = false;
                    }

                    continue;
                }

                AaruConsole.ErrorWriteLine("Converting image will lose sector tag {0}, not continuing...", sectorTag);

                AaruConsole.
                ErrorWriteLine("If you don't care, use force option. This will skip all sector tags converting only user data.");

                return((int)ErrorNumber.DataWillBeLost);
            }

            if (!outputFormat.Create(outputPath, mediaType, parsedOptions, inputFormat.Info.Sectors,
                                     inputFormat.Info.SectorSize))
            {
                AaruConsole.ErrorWriteLine("Error {0} creating output image.", outputFormat.ErrorMessage);

                return((int)ErrorNumber.CannotCreateFormat);
            }

            var metadata = new ImageInfo
            {
                Application           = "Aaru",
                ApplicationVersion    = Version.GetVersion(),
                Comments              = comments ?? inputFormat.Info.Comments,
                Creator               = creator ?? inputFormat.Info.Creator,
                DriveFirmwareRevision = driveFirmwareRevision ?? inputFormat.Info.DriveFirmwareRevision,
                DriveManufacturer     = driveManufacturer ?? inputFormat.Info.DriveManufacturer,
                DriveModel            = driveModel ?? inputFormat.Info.DriveModel,
                DriveSerialNumber     = driveSerialNumber ?? inputFormat.Info.DriveSerialNumber,
                LastMediaSequence     = lastMediaSequence != 0 ? lastMediaSequence : inputFormat.Info.LastMediaSequence,
                MediaBarcode          = mediaBarcode ?? inputFormat.Info.MediaBarcode,
                MediaManufacturer     = mediaManufacturer ?? inputFormat.Info.MediaManufacturer,
                MediaModel            = mediaModel ?? inputFormat.Info.MediaModel,
                MediaPartNumber       = mediaPartNumber ?? inputFormat.Info.MediaPartNumber,
                MediaSequence         = mediaSequence != 0 ? mediaSequence : inputFormat.Info.MediaSequence,
                MediaSerialNumber     = mediaSerialNumber ?? inputFormat.Info.MediaSerialNumber,
                MediaTitle            = mediaTitle ?? inputFormat.Info.MediaTitle
            };

            if (!outputFormat.SetMetadata(metadata))
            {
                AaruConsole.ErrorWrite("Error {0} setting metadata, ", outputFormat.ErrorMessage);

                if (!force)
                {
                    AaruConsole.ErrorWriteLine("not continuing...");

                    return((int)ErrorNumber.WriteError);
                }

                AaruConsole.ErrorWriteLine("continuing...");
            }

            CICMMetadataType        cicmMetadata = inputFormat.CicmMetadata;
            List <DumpHardwareType> dumpHardware = inputFormat.DumpHardware;

            foreach (MediaTagType mediaTag in inputFormat.Info.ReadableMediaTags)
            {
                if (force && !outputFormat.SupportedMediaTags.Contains(mediaTag))
                {
                    continue;
                }

                AaruConsole.WriteLine("Converting media tag {0}", mediaTag);
                byte[] tag = inputFormat.ReadDiskTag(mediaTag);

                if (outputFormat.WriteMediaTag(tag, mediaTag))
                {
                    continue;
                }

                if (force)
                {
                    AaruConsole.ErrorWriteLine("Error {0} writing media tag, continuing...", outputFormat.ErrorMessage);
                }
                else
                {
                    AaruConsole.ErrorWriteLine("Error {0} writing media tag, not continuing...",
                                               outputFormat.ErrorMessage);

                    return((int)ErrorNumber.WriteError);
                }
            }

            AaruConsole.WriteLine("{0} sectors to convert", inputFormat.Info.Sectors);
            ulong doneSectors = 0;

            if (inputFormat is IOpticalMediaImage inputOptical &&
                outputFormat is IWritableOpticalImage outputOptical &&
                inputOptical.Tracks != null)
            {
                if (!outputOptical.SetTracks(inputOptical.Tracks))
                {
                    AaruConsole.ErrorWriteLine("Error {0} sending tracks list to output image.",
                                               outputFormat.ErrorMessage);

                    return((int)ErrorNumber.WriteError);
                }

                foreach (Track track in inputOptical.Tracks)
                {
                    doneSectors = 0;
                    ulong trackSectors = (track.TrackEndSector - track.TrackStartSector) + 1;

                    while (doneSectors < trackSectors)
                    {
                        byte[] sector;

                        uint sectorsToDo;

                        if (trackSectors - doneSectors >= (ulong)count)
                        {
                            sectorsToDo = (uint)count;
                        }
                        else
                        {
                            sectorsToDo = (uint)(trackSectors - doneSectors);
                        }

                        AaruConsole.Write("\rConverting sectors {0} to {1} in track {3} ({2:P2} done)",
                                          doneSectors + track.TrackStartSector,
                                          doneSectors + sectorsToDo + track.TrackStartSector,
                                          (doneSectors + track.TrackStartSector) / (double)inputFormat.Info.Sectors,
                                          track.TrackSequence);

                        bool useNotLong = false;
                        bool result     = false;

                        if (useLong)
                        {
                            if (sectorsToDo == 1)
                            {
                                sector = inputFormat.ReadSectorLong(doneSectors + track.TrackStartSector);
                                result = outputFormat.WriteSectorLong(sector, doneSectors + track.TrackStartSector);
                            }
                            else
                            {
                                sector = inputFormat.ReadSectorsLong(doneSectors + track.TrackStartSector, sectorsToDo);

                                result = outputFormat.WriteSectorsLong(sector, doneSectors + track.TrackStartSector,
                                                                       sectorsToDo);
                            }

                            if (!result &&
                                sector.Length % 2352 != 0)
                            {
                                if (!force)
                                {
                                    AaruConsole.ErrorWriteLine("Input image is not returning raw sectors, use force if you want to continue...");

                                    return((int)Errno.InOutError);
                                }

                                useNotLong = true;
                            }
                        }

                        if (!useLong || useNotLong)
                        {
                            if (sectorsToDo == 1)
                            {
                                sector = inputFormat.ReadSector(doneSectors + track.TrackStartSector);
                                result = outputFormat.WriteSector(sector, doneSectors + track.TrackStartSector);
                            }
                            else
                            {
                                sector = inputFormat.ReadSectors(doneSectors + track.TrackStartSector, sectorsToDo);

                                result = outputFormat.WriteSectors(sector, doneSectors + track.TrackStartSector,
                                                                   sectorsToDo);
                            }
                        }

                        if (!result)
                        {
                            if (force)
                            {
                                AaruConsole.ErrorWriteLine("Error {0} writing sector {1}, continuing...",
                                                           outputFormat.ErrorMessage,
                                                           doneSectors + track.TrackStartSector);
                            }
                            else
                            {
                                AaruConsole.ErrorWriteLine("Error {0} writing sector {1}, not continuing...",
                                                           outputFormat.ErrorMessage,
                                                           doneSectors + track.TrackStartSector);

                                return((int)ErrorNumber.WriteError);
                            }
                        }

                        doneSectors += sectorsToDo;
                    }
                }

                AaruConsole.Write("\rConverting sectors {0} to {1} in track {3} ({2:P2} done)",
                                  inputFormat.Info.Sectors, inputFormat.Info.Sectors, 1.0, inputOptical.Tracks.Count);

                AaruConsole.WriteLine();

                foreach (SectorTagType tag in inputFormat.Info.ReadableSectorTags.OrderBy(t => t))
                {
                    if (!useLong)
                    {
                        break;
                    }

                    switch (tag)
                    {
                    case SectorTagType.AppleSectorTag:
                    case SectorTagType.CdSectorSync:
                    case SectorTagType.CdSectorHeader:
                    case SectorTagType.CdSectorSubHeader:
                    case SectorTagType.CdSectorEdc:
                    case SectorTagType.CdSectorEccP:
                    case SectorTagType.CdSectorEccQ:
                    case SectorTagType.CdSectorEcc:
                        // This tags are inline in long sector
                        continue;
                    }

                    if (force && !outputFormat.SupportedSectorTags.Contains(tag))
                    {
                        continue;
                    }

                    foreach (Track track in inputOptical.Tracks)
                    {
                        doneSectors = 0;
                        ulong  trackSectors = (track.TrackEndSector - track.TrackStartSector) + 1;
                        byte[] sector;
                        bool   result;

                        switch (tag)
                        {
                        case SectorTagType.CdTrackFlags:
                        case SectorTagType.CdTrackIsrc:
                            AaruConsole.Write("\rConverting tag {0} in track {1} ({2:P2} done).", tag,
                                              track.TrackSequence,
                                              track.TrackSequence / (double)inputOptical.Tracks.Count);

                            sector = inputFormat.ReadSectorTag(track.TrackStartSector, tag);
                            result = outputFormat.WriteSectorTag(sector, track.TrackStartSector, tag);

                            if (!result)
                            {
                                if (force)
                                {
                                    AaruConsole.ErrorWriteLine("Error {0} writing tag, continuing...",
                                                               outputFormat.ErrorMessage);
                                }
                                else
                                {
                                    AaruConsole.ErrorWriteLine("Error {0} writing tag, not continuing...",
                                                               outputFormat.ErrorMessage);

                                    return((int)ErrorNumber.WriteError);
                                }
                            }

                            continue;
                        }

                        while (doneSectors < trackSectors)
                        {
                            uint sectorsToDo;

                            if (trackSectors - doneSectors >= (ulong)count)
                            {
                                sectorsToDo = (uint)count;
                            }
                            else
                            {
                                sectorsToDo = (uint)(trackSectors - doneSectors);
                            }

                            AaruConsole.Write("\rConverting tag {4} for sectors {0} to {1} in track {3} ({2:P2} done)",
                                              doneSectors + track.TrackStartSector,
                                              doneSectors + sectorsToDo + track.TrackStartSector,
                                              (doneSectors + track.TrackStartSector) / (double)inputFormat.Info.Sectors,
                                              track.TrackSequence, tag);

                            if (sectorsToDo == 1)
                            {
                                sector = inputFormat.ReadSectorTag(doneSectors + track.TrackStartSector, tag);
                                result = outputFormat.WriteSectorTag(sector, doneSectors + track.TrackStartSector, tag);
                            }
                            else
                            {
                                sector = inputFormat.ReadSectorsTag(doneSectors + track.TrackStartSector, sectorsToDo,
                                                                    tag);

                                result = outputFormat.WriteSectorsTag(sector, doneSectors + track.TrackStartSector,
                                                                      sectorsToDo, tag);
                            }

                            if (!result)
                            {
                                if (force)
                                {
                                    AaruConsole.ErrorWriteLine("Error {0} writing tag for sector {1}, continuing...",
                                                               outputFormat.ErrorMessage,
                                                               doneSectors + track.TrackStartSector);
                                }
                                else
                                {
                                    AaruConsole.
                                    ErrorWriteLine("Error {0} writing tag for sector {1}, not continuing...",
                                                   outputFormat.ErrorMessage, doneSectors + track.TrackStartSector);

                                    return((int)ErrorNumber.WriteError);
                                }
                            }

                            doneSectors += sectorsToDo;
                        }
                    }

                    switch (tag)
                    {
                    case SectorTagType.CdTrackFlags:
                    case SectorTagType.CdTrackIsrc:
                        AaruConsole.Write("\rConverting tag {0} in track {1} ({2:P2} done).", tag,
                                          inputOptical.Tracks.Count, 1.0);

                        break;

                    default:
                        AaruConsole.Write("\rConverting tag {4} for sectors {0} to {1} in track {3} ({2:P2} done)",
                                          inputFormat.Info.Sectors, inputFormat.Info.Sectors, 1.0,
                                          inputOptical.Tracks.Count, tag);

                        break;
                    }

                    AaruConsole.WriteLine();
                }
            }
示例#4
0
        public static int Invoke(bool verbose, bool debug, bool diskTags, string imagePath, string length,
                                 bool sectorTags, ulong startSector)
        {
            MainClass.PrintCopyright();

            if (debug)
            {
                AaruConsole.DebugWriteLineEvent += System.Console.Error.WriteLine;
            }

            if (verbose)
            {
                AaruConsole.VerboseWriteLineEvent += System.Console.WriteLine;
            }

            Statistics.AddCommand("decode");

            AaruConsole.DebugWriteLine("Decode command", "--debug={0}", debug);
            AaruConsole.DebugWriteLine("Decode command", "--disk-tags={0}", diskTags);
            AaruConsole.DebugWriteLine("Decode command", "--input={0}", imagePath);
            AaruConsole.DebugWriteLine("Decode command", "--length={0}", length);
            AaruConsole.DebugWriteLine("Decode command", "--sector-tags={0}", sectorTags);
            AaruConsole.DebugWriteLine("Decode command", "--start={0}", startSector);
            AaruConsole.DebugWriteLine("Decode command", "--verbose={0}", verbose);

            var     filtersList = new FiltersList();
            IFilter inputFilter = filtersList.GetFilter(imagePath);

            if (inputFilter == null)
            {
                AaruConsole.ErrorWriteLine("Cannot open specified file.");

                return((int)ErrorNumber.CannotOpenFile);
            }

            IMediaImage inputFormat = ImageFormat.Detect(inputFilter);

            if (inputFormat == null)
            {
                AaruConsole.ErrorWriteLine("Unable to recognize image format, not decoding");

                return((int)ErrorNumber.UnrecognizedFormat);
            }

            inputFormat.Open(inputFilter);
            Statistics.AddMediaFormat(inputFormat.Format);
            Statistics.AddMedia(inputFormat.Info.MediaType, false);
            Statistics.AddFilter(inputFilter.Name);

            if (diskTags)
            {
                if (inputFormat.Info.ReadableMediaTags.Count == 0)
                {
                    AaruConsole.WriteLine("There are no disk tags in chosen disc image.");
                }
                else
                {
                    foreach (MediaTagType tag in inputFormat.Info.ReadableMediaTags)
                    {
                        switch (tag)
                        {
                        case MediaTagType.SCSI_INQUIRY:
                        {
                            byte[] inquiry = inputFormat.ReadDiskTag(MediaTagType.SCSI_INQUIRY);

                            if (inquiry == null)
                            {
                                AaruConsole.WriteLine("Error reading SCSI INQUIRY response from disc image");
                            }
                            else
                            {
                                AaruConsole.WriteLine("SCSI INQUIRY command response:");

                                AaruConsole.
                                WriteLine("================================================================================");

                                AaruConsole.WriteLine(Inquiry.Prettify(inquiry));

                                AaruConsole.
                                WriteLine("================================================================================");
                            }

                            break;
                        }

                        case MediaTagType.ATA_IDENTIFY:
                        {
                            byte[] identify = inputFormat.ReadDiskTag(MediaTagType.ATA_IDENTIFY);

                            if (identify == null)
                            {
                                AaruConsole.WriteLine("Error reading ATA IDENTIFY DEVICE response from disc image");
                            }
                            else
                            {
                                AaruConsole.WriteLine("ATA IDENTIFY DEVICE command response:");

                                AaruConsole.
                                WriteLine("================================================================================");

                                AaruConsole.WriteLine(Identify.Prettify(identify));

                                AaruConsole.
                                WriteLine("================================================================================");
                            }

                            break;
                        }

                        case MediaTagType.ATAPI_IDENTIFY:
                        {
                            byte[] identify = inputFormat.ReadDiskTag(MediaTagType.ATAPI_IDENTIFY);

                            if (identify == null)
                            {
                                AaruConsole.
                                WriteLine("Error reading ATA IDENTIFY PACKET DEVICE response from disc image");
                            }
                            else
                            {
                                AaruConsole.WriteLine("ATA IDENTIFY PACKET DEVICE command response:");

                                AaruConsole.
                                WriteLine("================================================================================");

                                AaruConsole.WriteLine(Identify.Prettify(identify));

                                AaruConsole.
                                WriteLine("================================================================================");
                            }

                            break;
                        }

                        case MediaTagType.CD_ATIP:
                        {
                            byte[] atip = inputFormat.ReadDiskTag(MediaTagType.CD_ATIP);

                            if (atip == null)
                            {
                                AaruConsole.WriteLine("Error reading CD ATIP from disc image");
                            }
                            else
                            {
                                AaruConsole.WriteLine("CD ATIP:");

                                AaruConsole.
                                WriteLine("================================================================================");

                                AaruConsole.WriteLine(ATIP.Prettify(atip));

                                AaruConsole.
                                WriteLine("================================================================================");
                            }

                            break;
                        }

                        case MediaTagType.CD_FullTOC:
                        {
                            byte[] fullToc = inputFormat.ReadDiskTag(MediaTagType.CD_FullTOC);

                            if (fullToc == null)
                            {
                                AaruConsole.WriteLine("Error reading CD full TOC from disc image");
                            }
                            else
                            {
                                AaruConsole.WriteLine("CD full TOC:");

                                AaruConsole.
                                WriteLine("================================================================================");

                                AaruConsole.WriteLine(FullTOC.Prettify(fullToc));

                                AaruConsole.
                                WriteLine("================================================================================");
                            }

                            break;
                        }

                        case MediaTagType.CD_PMA:
                        {
                            byte[] pma = inputFormat.ReadDiskTag(MediaTagType.CD_PMA);

                            if (pma == null)
                            {
                                AaruConsole.WriteLine("Error reading CD PMA from disc image");
                            }
                            else
                            {
                                AaruConsole.WriteLine("CD PMA:");

                                AaruConsole.
                                WriteLine("================================================================================");

                                AaruConsole.WriteLine(PMA.Prettify(pma));

                                AaruConsole.
                                WriteLine("================================================================================");
                            }

                            break;
                        }

                        case MediaTagType.CD_SessionInfo:
                        {
                            byte[] sessionInfo = inputFormat.ReadDiskTag(MediaTagType.CD_SessionInfo);

                            if (sessionInfo == null)
                            {
                                AaruConsole.WriteLine("Error reading CD session information from disc image");
                            }
                            else
                            {
                                AaruConsole.WriteLine("CD session information:");

                                AaruConsole.
                                WriteLine("================================================================================");

                                AaruConsole.WriteLine(Session.Prettify(sessionInfo));

                                AaruConsole.
                                WriteLine("================================================================================");
                            }

                            break;
                        }

                        case MediaTagType.CD_TEXT:
                        {
                            byte[] cdText = inputFormat.ReadDiskTag(MediaTagType.CD_TEXT);

                            if (cdText == null)
                            {
                                AaruConsole.WriteLine("Error reading CD-TEXT from disc image");
                            }
                            else
                            {
                                AaruConsole.WriteLine("CD-TEXT:");

                                AaruConsole.
                                WriteLine("================================================================================");

                                AaruConsole.WriteLine(CDTextOnLeadIn.Prettify(cdText));

                                AaruConsole.
                                WriteLine("================================================================================");
                            }

                            break;
                        }

                        case MediaTagType.CD_TOC:
                        {
                            byte[] toc = inputFormat.ReadDiskTag(MediaTagType.CD_TOC);

                            if (toc == null)
                            {
                                AaruConsole.WriteLine("Error reading CD TOC from disc image");
                            }
                            else
                            {
                                AaruConsole.WriteLine("CD TOC:");

                                AaruConsole.
                                WriteLine("================================================================================");

                                AaruConsole.WriteLine(TOC.Prettify(toc));

                                AaruConsole.
                                WriteLine("================================================================================");
                            }

                            break;
                        }

                        default:
                            AaruConsole.WriteLine("Decoder for disk tag type \"{0}\" not yet implemented, sorry.",
                                                  tag);

                            break;
                        }
                    }
                }
            }

            if (sectorTags)
            {
                if (length.ToLowerInvariant() == "all")
                {
                }
                else
                {
                    if (!ulong.TryParse(length, out ulong _))
                    {
                        AaruConsole.WriteLine("Value \"{0}\" is not a valid number for length.", length);
                        AaruConsole.WriteLine("Not decoding sectors tags");

                        return(3);
                    }
                }

                if (inputFormat.Info.ReadableSectorTags.Count == 0)
                {
                    AaruConsole.WriteLine("There are no sector tags in chosen disc image.");
                }
                else
                {
                    foreach (SectorTagType tag in inputFormat.Info.ReadableSectorTags)
                    {
                        switch (tag)
                        {
                        default:
                            AaruConsole.WriteLine("Decoder for disk tag type \"{0}\" not yet implemented, sorry.",
                                                  tag);

                            break;
                        }
                    }
                }

                // TODO: Not implemented
            }

            return((int)ErrorNumber.NoError);
        }
示例#5
0
        /// <summary>
        ///     Creates a metadata sidecar for an optical disc (e.g. CD, DVD, GD, BD, XGD, GOD)
        /// </summary>
        /// <param name="image">Image</param>
        /// <param name="filterId">Filter uuid</param>
        /// <param name="imagePath">Image path</param>
        /// <param name="fi">Image file information</param>
        /// <param name="plugins">Image plugins</param>
        /// <param name="imgChecksums">List of image checksums</param>
        /// <param name="sidecar">Metadata sidecar</param>
        static void OpticalDisc(IMediaImage image, Guid filterId, string imagePath,
                                FileInfo fi, PluginBase plugins,
                                List <ChecksumType> imgChecksums, ref CICMMetadataType sidecar, Encoding encoding)
        {
            sidecar.OpticalDisc = new[]
            {
                new OpticalDiscType
                {
                    Checksums = imgChecksums.ToArray(),
                    Image     = new ImageType
                    {
                        format          = image.Format,
                        offset          = 0,
                        offsetSpecified = true,
                        Value           = Path.GetFileName(imagePath)
                    },
                    Size     = fi.Length,
                    Sequence = new SequenceType {
                        MediaTitle = image.Info.MediaTitle
                    }
                }
            };

            if (image.Info.MediaSequence != 0 && image.Info.LastMediaSequence != 0)
            {
                sidecar.OpticalDisc[0].Sequence.MediaSequence = image.Info.MediaSequence;
                sidecar.OpticalDisc[0].Sequence.TotalMedia    = image.Info.LastMediaSequence;
            }
            else
            {
                sidecar.OpticalDisc[0].Sequence.MediaSequence = 1;
                sidecar.OpticalDisc[0].Sequence.TotalMedia    = 1;
            }

            MediaType dskType = image.Info.MediaType;

            foreach (MediaTagType tagType in image.Info.ReadableMediaTags)
            {
                switch (tagType)
                {
                case MediaTagType.CD_ATIP:
                    sidecar.OpticalDisc[0].ATIP = new DumpType
                    {
                        Image     = Path.GetFileName(imagePath),
                        Checksums = Checksum.GetChecksums(image.ReadDiskTag(MediaTagType.CD_ATIP)).ToArray(),
                        Size      = image.ReadDiskTag(MediaTagType.CD_ATIP).Length
                    };
                    ATIP.CDATIP?atip = ATIP.Decode(image.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
                    {
                        Image     = Path.GetFileName(imagePath),
                        Checksums = Checksum.GetChecksums(image.ReadDiskTag(MediaTagType.DVD_BCA)).ToArray(),
                        Size      = image.ReadDiskTag(MediaTagType.DVD_BCA).Length
                    };
                    break;

                case MediaTagType.BD_BCA:
                    sidecar.OpticalDisc[0].BCA = new DumpType
                    {
                        Image     = Path.GetFileName(imagePath),
                        Checksums = Checksum.GetChecksums(image.ReadDiskTag(MediaTagType.BD_BCA)).ToArray(),
                        Size      = image.ReadDiskTag(MediaTagType.BD_BCA).Length
                    };
                    break;

                case MediaTagType.DVD_CMI:
                    sidecar.OpticalDisc[0].CMI = new DumpType
                    {
                        Image     = Path.GetFileName(imagePath),
                        Checksums = Checksum.GetChecksums(image.ReadDiskTag(MediaTagType.DVD_CMI)).ToArray(),
                        Size      = image.ReadDiskTag(MediaTagType.DVD_CMI).Length
                    };
                    CSS_CPRM.LeadInCopyright?cmi =
                        CSS_CPRM.DecodeLeadInCopyright(image.ReadDiskTag(MediaTagType.DVD_CMI));
                    if (cmi.HasValue)
                    {
                        switch (cmi.Value.CopyrightType)
                        {
                        case CopyrightType.AACS:
                            sidecar.OpticalDisc[0].CopyProtection = "AACS";
                            break;

                        case CopyrightType.CSS:
                            sidecar.OpticalDisc[0].CopyProtection = "CSS";
                            break;

                        case CopyrightType.CPRM:
                            sidecar.OpticalDisc[0].CopyProtection = "CPRM";
                            break;
                        }
                    }

                    break;

                case MediaTagType.DVD_DMI:
                    sidecar.OpticalDisc[0].DMI = new DumpType
                    {
                        Image     = Path.GetFileName(imagePath),
                        Checksums = Checksum.GetChecksums(image.ReadDiskTag(MediaTagType.DVD_DMI)).ToArray(),
                        Size      = image.ReadDiskTag(MediaTagType.DVD_DMI).Length
                    };
                    if (DMI.IsXbox(image.ReadDiskTag(MediaTagType.DVD_DMI)))
                    {
                        dskType = MediaType.XGD;
                        sidecar.OpticalDisc[0].Dimensions = new DimensionsType {
                            Diameter = 120, Thickness = 1.2
                        };
                    }
                    else if (DMI.IsXbox360(image.ReadDiskTag(MediaTagType.DVD_DMI)))
                    {
                        dskType = MediaType.XGD2;
                        sidecar.OpticalDisc[0].Dimensions = new DimensionsType {
                            Diameter = 120, Thickness = 1.2
                        };
                    }

                    break;

                case MediaTagType.DVD_PFI:
                    sidecar.OpticalDisc[0].PFI = new DumpType
                    {
                        Image     = Path.GetFileName(imagePath),
                        Checksums = Checksum.GetChecksums(image.ReadDiskTag(MediaTagType.DVD_PFI)).ToArray(),
                        Size      = image.ReadDiskTag(MediaTagType.DVD_PFI).Length
                    };
                    PFI.PhysicalFormatInformation?pfi = PFI.Decode(image.ReadDiskTag(MediaTagType.DVD_PFI));
                    if (pfi.HasValue)
                    {
                        if (dskType != MediaType.XGD && dskType != MediaType.XGD2 && dskType != MediaType.XGD3)
                        {
                            switch (pfi.Value.DiskCategory)
                            {
                            case DiskCategory.DVDPR:
                                dskType = MediaType.DVDPR;
                                break;

                            case DiskCategory.DVDPRDL:
                                dskType = MediaType.DVDPRDL;
                                break;

                            case DiskCategory.DVDPRW:
                                dskType = MediaType.DVDPRW;
                                break;

                            case DiskCategory.DVDPRWDL:
                                dskType = MediaType.DVDPRWDL;
                                break;

                            case DiskCategory.DVDR:
                                dskType = MediaType.DVDR;
                                break;

                            case DiskCategory.DVDRAM:
                                dskType = MediaType.DVDRAM;
                                break;

                            case DiskCategory.DVDROM:
                                dskType = MediaType.DVDROM;
                                break;

                            case DiskCategory.DVDRW:
                                dskType = MediaType.DVDRW;
                                break;

                            case DiskCategory.HDDVDR:
                                dskType = MediaType.HDDVDR;
                                break;

                            case DiskCategory.HDDVDRAM:
                                dskType = MediaType.HDDVDRAM;
                                break;

                            case DiskCategory.HDDVDROM:
                                dskType = MediaType.HDDVDROM;
                                break;

                            case DiskCategory.HDDVDRW:
                                dskType = MediaType.HDDVDRW;
                                break;

                            case DiskCategory.Nintendo:
                                dskType = MediaType.GOD;
                                break;

                            case 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 == DVDSize.OneTwenty)
                            {
                                dskType = MediaType.WOD;
                            }

                            sidecar.OpticalDisc[0].Dimensions = new DimensionsType();
                            if (dskType == MediaType.UMD)
                            {
                                sidecar.OpticalDisc[0].Dimensions.Height          = 64;
                                sidecar.OpticalDisc[0].Dimensions.HeightSpecified = true;
                                sidecar.OpticalDisc[0].Dimensions.Width           = 63;
                                sidecar.OpticalDisc[0].Dimensions.WidthSpecified  = true;
                                sidecar.OpticalDisc[0].Dimensions.Thickness       = 4;
                            }
                            else
                            {
                                switch (pfi.Value.DiscSize)
                                {
                                case DVDSize.Eighty:
                                    sidecar.OpticalDisc[0].Dimensions.Diameter          = 80;
                                    sidecar.OpticalDisc[0].Dimensions.DiameterSpecified = true;
                                    sidecar.OpticalDisc[0].Dimensions.Thickness         = 1.2;
                                    break;

                                case DVDSize.OneTwenty:
                                    sidecar.OpticalDisc[0].Dimensions.Diameter          = 120;
                                    sidecar.OpticalDisc[0].Dimensions.DiameterSpecified = true;
                                    sidecar.OpticalDisc[0].Dimensions.Thickness         = 1.2;
                                    break;
                                }
                            }
                        }
                    }

                    break;

                case MediaTagType.CD_PMA:
                    sidecar.OpticalDisc[0].PMA = new DumpType
                    {
                        Image     = Path.GetFileName(imagePath),
                        Checksums = Checksum.GetChecksums(image.ReadDiskTag(MediaTagType.CD_PMA)).ToArray(),
                        Size      = image.ReadDiskTag(MediaTagType.CD_PMA).Length
                    };
                    break;

                case MediaTagType.CD_FullTOC:
                    sidecar.OpticalDisc[0].TOC = new DumpType
                    {
                        Image     = Path.GetFileName(imagePath),
                        Checksums = Checksum.GetChecksums(image.ReadDiskTag(MediaTagType.CD_FullTOC)).ToArray(),
                        Size      = image.ReadDiskTag(MediaTagType.CD_FullTOC).Length
                    };
                    break;

                case MediaTagType.CD_LeadIn:
                    sidecar.OpticalDisc[0].LeadIn = new[]
                    {
                        new BorderType
                        {
                            Image     = Path.GetFileName(imagePath),
                            Checksums = Checksum.GetChecksums(image.ReadDiskTag(MediaTagType.CD_LeadIn)).ToArray(),
                            Size      = image.ReadDiskTag(MediaTagType.CD_LeadIn).Length
                        }
                    };
                    break;

                case MediaTagType.Xbox_SecuritySector:
                    if (sidecar.OpticalDisc[0].Xbox == null)
                    {
                        sidecar.OpticalDisc[0].Xbox = new XboxType();
                    }

                    sidecar.OpticalDisc[0].Xbox.SecuritySectors = new[]
                    {
                        new XboxSecuritySectorsType
                        {
                            RequestNumber   = 0,
                            RequestVersion  = 1,
                            SecuritySectors = new DumpType
                            {
                                Image     = Path.GetFileName(imagePath),
                                Checksums =
                                    Checksum.GetChecksums(image.ReadDiskTag(MediaTagType.Xbox_SecuritySector))
                                    .ToArray(),
                                Size = image.ReadDiskTag(MediaTagType.Xbox_SecuritySector).Length
                            }
                        }
                    };

                    break;

                case MediaTagType.Xbox_PFI:
                    if (sidecar.OpticalDisc[0].Xbox == null)
                    {
                        sidecar.OpticalDisc[0].Xbox = new XboxType();
                    }

                    sidecar.OpticalDisc[0].Xbox.PFI = new DumpType
                    {
                        Image     = Path.GetFileName(imagePath),
                        Checksums = Checksum.GetChecksums(image.ReadDiskTag(MediaTagType.Xbox_PFI)).ToArray(),
                        Size      = image.ReadDiskTag(MediaTagType.Xbox_PFI).Length
                    };
                    break;

                case MediaTagType.Xbox_DMI:
                    if (sidecar.OpticalDisc[0].Xbox == null)
                    {
                        sidecar.OpticalDisc[0].Xbox = new XboxType();
                    }

                    sidecar.OpticalDisc[0].Xbox.DMI = new DumpType
                    {
                        Image     = Path.GetFileName(imagePath),
                        Checksums = Checksum.GetChecksums(image.ReadDiskTag(MediaTagType.Xbox_DMI)).ToArray(),
                        Size      = image.ReadDiskTag(MediaTagType.Xbox_DMI).Length
                    };
                    break;
                }
            }

            try
            {
                List <Session> sessions = image.Sessions;
                sidecar.OpticalDisc[0].Sessions = sessions?.Count ?? 1;
            }
            catch { sidecar.OpticalDisc[0].Sessions = 1; }

            List <Track>     tracks  = image.Tracks;
            List <TrackType> trksLst = null;

            if (tracks != null)
            {
                sidecar.OpticalDisc[0].Tracks    = new int[1];
                sidecar.OpticalDisc[0].Tracks[0] = tracks.Count;
                trksLst = new List <TrackType>();
            }

            if (sidecar.OpticalDisc[0].Dimensions == null && image.Info.MediaType != MediaType.Unknown)
            {
                sidecar.OpticalDisc[0].Dimensions = Dimensions.DimensionsFromMediaType(image.Info.MediaType);
            }

            InitProgress();

            UpdateStatus("Checking filesystems");
            List <Partition> partitions = Partitions.GetAll(image);

            Partitions.AddSchemesToStats(partitions);

            foreach (Track trk in tracks)
            {
                TrackType xmlTrk = new TrackType();
                switch (trk.TrackType)
                {
                case CommonTypes.Enums.TrackType.Audio:
                    xmlTrk.TrackType1 = TrackTypeTrackType.audio;
                    break;

                case CommonTypes.Enums.TrackType.CdMode2Form2:
                    xmlTrk.TrackType1 = TrackTypeTrackType.m2f2;
                    break;

                case CommonTypes.Enums.TrackType.CdMode2Formless:
                    xmlTrk.TrackType1 = TrackTypeTrackType.mode2;
                    break;

                case CommonTypes.Enums.TrackType.CdMode2Form1:
                    xmlTrk.TrackType1 = TrackTypeTrackType.m2f1;
                    break;

                case CommonTypes.Enums.TrackType.CdMode1:
                    xmlTrk.TrackType1 = TrackTypeTrackType.mode1;
                    break;

                case CommonTypes.Enums.TrackType.Data:
                    switch (sidecar.OpticalDisc[0].DiscType)
                    {
                    case "BD":
                        xmlTrk.TrackType1 = TrackTypeTrackType.bluray;
                        break;

                    case "DDCD":
                        xmlTrk.TrackType1 = TrackTypeTrackType.ddcd;
                        break;

                    case "DVD":
                        xmlTrk.TrackType1 = TrackTypeTrackType.dvd;
                        break;

                    case "HD DVD":
                        xmlTrk.TrackType1 = TrackTypeTrackType.hddvd;
                        break;

                    default:
                        xmlTrk.TrackType1 = TrackTypeTrackType.mode1;
                        break;
                    }

                    break;
                }

                xmlTrk.Sequence =
                    new TrackSequenceType {
                    Session = trk.TrackSession, TrackNumber = (int)trk.TrackSequence
                };
                xmlTrk.StartSector = (long)trk.TrackStartSector;
                xmlTrk.EndSector   = (long)trk.TrackEndSector;

                if (trk.Indexes != null && trk.Indexes.ContainsKey(0))
                {
                    if (trk.Indexes.TryGetValue(0, out ulong idx0))
                    {
                        xmlTrk.StartSector = (long)idx0;
                    }
                }

                switch (sidecar.OpticalDisc[0].DiscType)
                {
                case "CD":
                case "GD":
                    xmlTrk.StartMSF = LbaToMsf(xmlTrk.StartSector);
                    xmlTrk.EndMSF   = LbaToMsf(xmlTrk.EndSector);
                    break;

                case "DDCD":
                    xmlTrk.StartMSF = DdcdLbaToMsf(xmlTrk.StartSector);
                    xmlTrk.EndMSF   = DdcdLbaToMsf(xmlTrk.EndSector);
                    break;
                }

                xmlTrk.Image = new ImageType {
                    Value = Path.GetFileName(trk.TrackFile), format = trk.TrackFileType
                };

                if (trk.TrackFileOffset > 0)
                {
                    xmlTrk.Image.offset          = (long)trk.TrackFileOffset;
                    xmlTrk.Image.offsetSpecified = true;
                }

                xmlTrk.Size           = (xmlTrk.EndSector - xmlTrk.StartSector + 1) * trk.TrackRawBytesPerSector;
                xmlTrk.BytesPerSector = trk.TrackBytesPerSector;

                uint  sectorsToRead = 512;
                ulong sectors       = (ulong)(xmlTrk.EndSector - xmlTrk.StartSector + 1);
                ulong doneSectors   = 0;

                // If there is only one track, and it's the same as the image file (e.g. ".iso" files), don't re-checksum.
                if (image.Id == new Guid("12345678-AAAA-BBBB-CCCC-123456789000") &&
                    // Only if filter is none...
                    (filterId == new Guid("12345678-AAAA-BBBB-CCCC-123456789000") ||
                     // ...or AppleDouble
                     filterId == new Guid("1b2165ee-c9df-4b21-bbbb-9e5892b2df4d")))
                {
                    xmlTrk.Checksums = sidecar.OpticalDisc[0].Checksums;
                }
                else
                {
                    UpdateProgress("Track {0} of {1}", trk.TrackSequence, tracks.Count);

                    // For fast debugging, skip checksum
                    //goto skipChecksum;

                    Checksum trkChkWorker = new Checksum();

                    InitProgress2();
                    while (doneSectors < sectors)
                    {
                        byte[] sector;

                        if (sectors - doneSectors >= sectorsToRead)
                        {
                            sector = image.ReadSectorsLong(doneSectors, sectorsToRead,
                                                           (uint)xmlTrk.Sequence.TrackNumber);
                            UpdateProgress2("Hashings sector {0} of {1}", (long)doneSectors,
                                            (long)(trk.TrackEndSector - trk.TrackStartSector + 1));
                            doneSectors += sectorsToRead;
                        }
                        else
                        {
                            sector = image.ReadSectorsLong(doneSectors, (uint)(sectors - doneSectors),
                                                           (uint)xmlTrk.Sequence.TrackNumber);
                            UpdateProgress2("Hashings sector {0} of {1}", (long)doneSectors,
                                            (long)(trk.TrackEndSector - trk.TrackStartSector + 1));
                            doneSectors += sectors - doneSectors;
                        }

                        trkChkWorker.Update(sector);
                    }

                    List <ChecksumType> trkChecksums = trkChkWorker.End();

                    xmlTrk.Checksums = trkChecksums.ToArray();

                    EndProgress2();
                }

                if (trk.TrackSubchannelType != TrackSubchannelType.None)
                {
                    xmlTrk.SubChannel = new SubChannelType
                    {
                        Image = new ImageType {
                            Value = trk.TrackSubchannelFile
                        },
                        // TODO: Packed subchannel has different size?
                        Size = (xmlTrk.EndSector - xmlTrk.StartSector + 1) * 96
                    };

                    switch (trk.TrackSubchannelType)
                    {
                    case TrackSubchannelType.Packed:
                    case TrackSubchannelType.PackedInterleaved:
                        xmlTrk.SubChannel.Image.format = "rw";
                        break;

                    case TrackSubchannelType.Raw:
                    case TrackSubchannelType.RawInterleaved:
                        xmlTrk.SubChannel.Image.format = "rw_raw";
                        break;

                    case TrackSubchannelType.Q16:
                    case TrackSubchannelType.Q16Interleaved:
                        xmlTrk.SubChannel.Image.format = "q16";
                        break;
                    }

                    if (trk.TrackFileOffset > 0)
                    {
                        xmlTrk.SubChannel.Image.offset          = (long)trk.TrackSubchannelOffset;
                        xmlTrk.SubChannel.Image.offsetSpecified = true;
                    }

                    Checksum subChkWorker = new Checksum();

                    sectors     = (ulong)(xmlTrk.EndSector - xmlTrk.StartSector + 1);
                    doneSectors = 0;

                    InitProgress2();
                    while (doneSectors < sectors)
                    {
                        byte[] sector;

                        if (sectors - doneSectors >= sectorsToRead)
                        {
                            sector = image.ReadSectorsTag(doneSectors, sectorsToRead, (uint)xmlTrk.Sequence.TrackNumber,
                                                          SectorTagType.CdSectorSubchannel);
                            UpdateProgress2("Hashings subchannel sector {0} of {1}", (long)doneSectors,
                                            (long)(trk.TrackEndSector - trk.TrackStartSector + 1));
                            doneSectors += sectorsToRead;
                        }
                        else
                        {
                            sector = image.ReadSectorsTag(doneSectors, (uint)(sectors - doneSectors),
                                                          (uint)xmlTrk.Sequence.TrackNumber,
                                                          SectorTagType.CdSectorSubchannel);
                            UpdateProgress2("Hashings subchannel sector {0} of {1}", (long)doneSectors,
                                            (long)(trk.TrackEndSector - trk.TrackStartSector + 1));
                            doneSectors += sectors - doneSectors;
                        }

                        subChkWorker.Update(sector);
                    }

                    List <ChecksumType> subChecksums = subChkWorker.End();

                    xmlTrk.SubChannel.Checksums = subChecksums.ToArray();

                    EndProgress2();
                }

                // For fast debugging, skip checksum
                //skipChecksum:

                List <Partition> trkPartitions = partitions
                                                 .Where(p => p.Start >= trk.TrackStartSector &&
                                                        p.End <= trk.TrackEndSector).ToList();

                xmlTrk.FileSystemInformation = new PartitionType[1];
                if (trkPartitions.Count > 0)
                {
                    xmlTrk.FileSystemInformation = new PartitionType[trkPartitions.Count];
                    for (int i = 0; i < trkPartitions.Count; i++)
                    {
                        xmlTrk.FileSystemInformation[i] = new PartitionType
                        {
                            Description = trkPartitions[i].Description,
                            EndSector   = (int)trkPartitions[i].End,
                            Name        = trkPartitions[i].Name,
                            Sequence    = (int)trkPartitions[i].Sequence,
                            StartSector = (int)trkPartitions[i].Start,
                            Type        = trkPartitions[i].Type
                        };
                        List <FileSystemType> lstFs = new List <FileSystemType>();

                        foreach (IFilesystem plugin in plugins.PluginsList.Values)
                        {
                            try
                            {
                                if (!plugin.Identify(image, trkPartitions[i]))
                                {
                                    continue;
                                }

                                plugin.GetInformation(image, trkPartitions[i], out _, encoding);
                                lstFs.Add(plugin.XmlFsType);
                                Statistics.AddFilesystem(plugin.XmlFsType.Type);

                                switch (plugin.XmlFsType.Type)
                                {
                                case "Opera":
                                    dskType = MediaType.ThreeDO;
                                    break;

                                case "PC Engine filesystem":
                                    dskType = MediaType.SuperCDROM2;
                                    break;

                                case "Nintendo Wii filesystem":
                                    dskType = MediaType.WOD;
                                    break;

                                case "Nintendo Gamecube filesystem":
                                    dskType = MediaType.GOD;
                                    break;
                                }
                            }
                        }
示例#6
0
        public pnlImageInfo(string imagePath, IFilter filter, IMediaImage imageFormat)
        {
            this.imagePath   = imagePath;
            this.filter      = filter;
            this.imageFormat = imageFormat;
            XamlReader.Load(this);

            Stream logo =
                ResourceHandler
                .GetResourceStream($"DiscImageChef.Gui.Assets.Logos.Media.{imageFormat.Info.MediaType}.svg");

            /*            if(logo != null)
             *          {
             *              svgMediaLogo.SvgStream = logo;
             *              svgMediaLogo.Visible   = true;
             *          }
             *          else
             *          {*/
            logo =
                ResourceHandler
                .GetResourceStream($"DiscImageChef.Gui.Assets.Logos.Media.{imageFormat.Info.MediaType}.png");
            if (logo != null)
            {
                imgMediaLogo.Image   = new Bitmap(logo);
                imgMediaLogo.Visible = true;
            }
            //}

            lblImagePath.Text   = $"Path: {imagePath}";
            lblFilter.Text      = $"Filter: {filter.Name}";
            lblImageFormat.Text = $"Image format identified by {imageFormat.Name} ({imageFormat.Id}).";
            lblImageFormat.Text = !string.IsNullOrWhiteSpace(imageFormat.Info.Version)
                                      ? $"Format: {imageFormat.Format} version {imageFormat.Info.Version}"
                                      : $"Format: {imageFormat.Format}";
            lblImageSize.Text = $"Image without headers is {imageFormat.Info.ImageSize} bytes long";
            lblSectors.Text   =
                $"Contains a media of {imageFormat.Info.Sectors} sectors with a maximum sector size of {imageFormat.Info.SectorSize} bytes (if all sectors are of the same size this would be {imageFormat.Info.Sectors * imageFormat.Info.SectorSize} bytes)";
            lblMediaType.Text =
                $"Contains a media of type {imageFormat.Info.MediaType} and XML type {imageFormat.Info.XmlMediaType}";
            lblHasPartitions.Text = $"{(imageFormat.Info.HasPartitions ? "Has" : "Doesn't have")} partitions";
            lblHasSessions.Text   = $"{(imageFormat.Info.HasSessions ? "Has" : "Doesn't have")} sessions";

            if (!string.IsNullOrWhiteSpace(imageFormat.Info.Application))
            {
                lblApplication.Visible = true;
                lblApplication.Text    = !string.IsNullOrWhiteSpace(imageFormat.Info.ApplicationVersion)
                                          ? $"Was created with {imageFormat.Info.Application} version {imageFormat.Info.ApplicationVersion}"
                                          : $"Was created with {imageFormat.Info.Application}";
            }

            if (!string.IsNullOrWhiteSpace(imageFormat.Info.Creator))
            {
                lblCreator.Visible = true;
                lblCreator.Text    = $"Created by: {imageFormat.Info.Creator}";
            }

            if (imageFormat.Info.CreationTime != DateTime.MinValue)
            {
                lblCreationTime.Visible = true;
                lblCreationTime.Text    = $"Created on {imageFormat.Info.CreationTime}";
            }

            if (imageFormat.Info.LastModificationTime != DateTime.MinValue)
            {
                lblLastModificationTime.Visible = true;
                lblLastModificationTime.Text    = $"Last modified on {imageFormat.Info.LastModificationTime}";
            }

            if (!string.IsNullOrWhiteSpace(imageFormat.Info.Comments))
            {
                grpComments.Visible = true;
                txtComments.Text    = imageFormat.Info.Comments;
            }

            if (imageFormat.Info.MediaSequence != 0 && imageFormat.Info.LastMediaSequence != 0)
            {
                lblMediaSequence.Visible = true;
                lblMediaSequence.Text    =
                    $"Media is number {imageFormat.Info.MediaSequence} on a set of {imageFormat.Info.LastMediaSequence} medias";
            }

            if (!string.IsNullOrWhiteSpace(imageFormat.Info.MediaTitle))
            {
                lblMediaTitle.Visible = true;
                lblMediaTitle.Text    = $"Media title: {imageFormat.Info.MediaTitle}";
            }

            if (!string.IsNullOrWhiteSpace(imageFormat.Info.MediaManufacturer))
            {
                lblMediaManufacturer.Visible = true;
                lblMediaManufacturer.Text    = $"Media manufacturer: {imageFormat.Info.MediaManufacturer}";
            }

            if (!string.IsNullOrWhiteSpace(imageFormat.Info.MediaModel))
            {
                lblMediaModel.Visible = true;
                lblMediaModel.Text    = $"Media model: {imageFormat.Info.MediaModel}";
            }

            if (!string.IsNullOrWhiteSpace(imageFormat.Info.MediaSerialNumber))
            {
                lblMediaSerialNumber.Visible = true;
                lblMediaSerialNumber.Text    = $"Media serial number: {imageFormat.Info.MediaSerialNumber}";
            }

            if (!string.IsNullOrWhiteSpace(imageFormat.Info.MediaBarcode))
            {
                lblMediaBarcode.Visible = true;
                lblMediaBarcode.Text    = $"Media barcode: {imageFormat.Info.MediaBarcode}";
            }

            if (!string.IsNullOrWhiteSpace(imageFormat.Info.MediaPartNumber))
            {
                lblMediaPartNumber.Visible = true;
                lblMediaPartNumber.Text    = $"Media part number: {imageFormat.Info.MediaPartNumber}";
            }

            if (!string.IsNullOrWhiteSpace(imageFormat.Info.DriveManufacturer))
            {
                lblDriveManufacturer.Visible = true;
                lblDriveManufacturer.Text    = $"Drive manufacturer: {imageFormat.Info.DriveManufacturer}";
            }

            if (!string.IsNullOrWhiteSpace(imageFormat.Info.DriveModel))
            {
                lblDriveModel.Visible = true;
                lblDriveModel.Text    = $"Drive model: {imageFormat.Info.DriveModel}";
            }

            if (!string.IsNullOrWhiteSpace(imageFormat.Info.DriveSerialNumber))
            {
                lblDriveSerialNumber.Visible = true;
                lblDriveSerialNumber.Text    = $"Drive serial number: {imageFormat.Info.DriveSerialNumber}";
            }

            if (!string.IsNullOrWhiteSpace(imageFormat.Info.DriveFirmwareRevision))
            {
                lblDriveFirmwareRevision.Visible = true;
                lblDriveFirmwareRevision.Text    = $"Drive firmware info: {imageFormat.Info.DriveFirmwareRevision}";
            }

            if (imageFormat.Info.Cylinders > 0 && imageFormat.Info.Heads > 0 &&
                imageFormat.Info.SectorsPerTrack > 0 &&
                imageFormat.Info.XmlMediaType != XmlMediaType.OpticalDisc &&
                (!(imageFormat is ITapeImage tapeImage) || !tapeImage.IsTape))
            {
                lblMediaGeometry.Visible = true;
                lblMediaGeometry.Text    =
                    $"Media geometry: {imageFormat.Info.Cylinders} cylinders, {imageFormat.Info.Heads} heads, {imageFormat.Info.SectorsPerTrack} sectors per track";
            }

            grpMediaInfo.Visible = lblMediaSequence.Visible || lblMediaTitle.Visible ||
                                   lblMediaManufacturer.Visible ||
                                   lblMediaModel.Visible || lblMediaSerialNumber.Visible ||
                                   lblMediaBarcode.Visible ||
                                   lblMediaPartNumber.Visible;
            grpDriveInfo.Visible = lblDriveManufacturer.Visible || lblDriveModel.Visible ||
                                   lblDriveSerialNumber.Visible || lblDriveFirmwareRevision.Visible ||
                                   lblMediaGeometry.Visible;

            if (imageFormat.Info.ReadableMediaTags != null && imageFormat.Info.ReadableMediaTags.Count > 0)
            {
                TreeGridItemCollection mediaTagList = new TreeGridItemCollection();

                treeMediaTags.Columns.Add(new GridColumn {
                    HeaderText = "Tag", DataCell = new TextBoxCell(0)
                });

                treeMediaTags.AllowMultipleSelection = false;
                treeMediaTags.ShowHeader             = false;
                treeMediaTags.DataStore = mediaTagList;

                foreach (MediaTagType tag in imageFormat.Info.ReadableMediaTags.OrderBy(t => t))
                {
                    mediaTagList.Add(new TreeGridItem {
                        Values = new object[] { tag.ToString() }
                    });
                }

                grpMediaTags.Visible = true;
            }

            if (imageFormat.Info.ReadableSectorTags != null && imageFormat.Info.ReadableSectorTags.Count > 0)
            {
                TreeGridItemCollection sectorTagList = new TreeGridItemCollection();

                treeSectorTags.Columns.Add(new GridColumn {
                    HeaderText = "Tag", DataCell = new TextBoxCell(0)
                });

                treeSectorTags.AllowMultipleSelection = false;
                treeSectorTags.ShowHeader             = false;
                treeSectorTags.DataStore = sectorTagList;

                foreach (SectorTagType tag in imageFormat.Info.ReadableSectorTags.OrderBy(t => t))
                {
                    sectorTagList.Add(new TreeGridItem {
                        Values = new object[] { tag.ToString() }
                    });
                }

                grpSectorTags.Visible = true;
            }

            PeripheralDeviceTypes scsiDeviceType = PeripheralDeviceTypes.DirectAccess;

            byte[] scsiInquiryData          = null;
            Inquiry.SCSIInquiry?scsiInquiry = null;
            Modes.DecodedMode?  scsiMode    = null;
            byte[] scsiModeSense6           = null;
            byte[] scsiModeSense10          = null;

            if (imageFormat.Info.ReadableMediaTags != null &&
                imageFormat.Info.ReadableMediaTags.Contains(MediaTagType.SCSI_INQUIRY))
            {
                scsiInquiryData = imageFormat.ReadDiskTag(MediaTagType.SCSI_INQUIRY);

                scsiDeviceType = (PeripheralDeviceTypes)(scsiInquiryData[0] & 0x1F);

                scsiInquiry = Inquiry.Decode(scsiInquiryData);
            }

            if (imageFormat.Info.ReadableMediaTags != null &&
                imageFormat.Info.ReadableMediaTags.Contains(MediaTagType.SCSI_MODESENSE_6))
            {
                scsiModeSense6 = imageFormat.ReadDiskTag(MediaTagType.SCSI_MODESENSE_6);
                scsiMode       = Modes.DecodeMode6(scsiModeSense6, scsiDeviceType);
            }

            if (imageFormat.Info.ReadableMediaTags != null &&
                imageFormat.Info.ReadableMediaTags.Contains(MediaTagType.SCSI_MODESENSE_10))
            {
                scsiModeSense10 = imageFormat.ReadDiskTag(MediaTagType.SCSI_MODESENSE_10);
                scsiMode        = Modes.DecodeMode10(scsiModeSense10, scsiDeviceType);
            }

            tabScsiInfo tabScsiInfo = new tabScsiInfo();

            tabScsiInfo.LoadData(scsiInquiryData, scsiInquiry, null, scsiMode, scsiDeviceType, scsiModeSense6,
                                 scsiModeSense10, null);
            tabInfos.Pages.Add(tabScsiInfo);

            byte[] ataIdentify   = null;
            byte[] atapiIdentify = null;

            if (imageFormat.Info.ReadableMediaTags != null &&
                imageFormat.Info.ReadableMediaTags.Contains(MediaTagType.ATA_IDENTIFY))
            {
                ataIdentify = imageFormat.ReadDiskTag(MediaTagType.ATA_IDENTIFY);
            }

            if (imageFormat.Info.ReadableMediaTags != null &&
                imageFormat.Info.ReadableMediaTags.Contains(MediaTagType.ATAPI_IDENTIFY))
            {
                atapiIdentify = imageFormat.ReadDiskTag(MediaTagType.ATAPI_IDENTIFY);
            }

            tabAtaInfo tabAtaInfo = new tabAtaInfo();

            tabAtaInfo.LoadData(ataIdentify, atapiIdentify, null);
            tabInfos.Pages.Add(tabAtaInfo);

            byte[]                toc                  = null;
            TOC.CDTOC?            decodedToc           = null;
            byte[]                fullToc              = null;
            FullTOC.CDFullTOC?    decodedFullToc       = null;
            byte[]                pma                  = null;
            byte[]                atip                 = null;
            ATIP.CDATIP?          decodedAtip          = null;
            byte[]                cdtext               = null;
            CDTextOnLeadIn.CDText?decodedCdText        = null;
            string                mediaCatalogueNumber = null;

            if (imageFormat.Info.ReadableMediaTags != null &&
                imageFormat.Info.ReadableMediaTags.Contains(MediaTagType.CD_TOC))
            {
                toc = imageFormat.ReadDiskTag(MediaTagType.CD_TOC);

                if (toc.Length > 0)
                {
                    ushort dataLen = Swapping.Swap(BitConverter.ToUInt16(toc, 0));
                    if (dataLen + 2 != toc.Length)
                    {
                        byte[] tmp = new byte[toc.Length + 2];
                        Array.Copy(toc, 0, tmp, 2, toc.Length);
                        tmp[0] = (byte)((toc.Length & 0xFF00) >> 8);
                        tmp[1] = (byte)(toc.Length & 0xFF);
                        toc    = tmp;
                    }

                    decodedToc = TOC.Decode(toc);
                }
            }

            if (imageFormat.Info.ReadableMediaTags != null &&
                imageFormat.Info.ReadableMediaTags.Contains(MediaTagType.CD_FullTOC))
            {
                fullToc = imageFormat.ReadDiskTag(MediaTagType.CD_FullTOC);

                if (fullToc.Length > 0)
                {
                    ushort dataLen = Swapping.Swap(BitConverter.ToUInt16(fullToc, 0));
                    if (dataLen + 2 != fullToc.Length)
                    {
                        byte[] tmp = new byte[fullToc.Length + 2];
                        Array.Copy(fullToc, 0, tmp, 2, fullToc.Length);
                        tmp[0]  = (byte)((fullToc.Length & 0xFF00) >> 8);
                        tmp[1]  = (byte)(fullToc.Length & 0xFF);
                        fullToc = tmp;
                    }

                    decodedFullToc = FullTOC.Decode(fullToc);
                }
            }

            if (imageFormat.Info.ReadableMediaTags != null &&
                imageFormat.Info.ReadableMediaTags.Contains(MediaTagType.CD_PMA))
            {
                pma = imageFormat.ReadDiskTag(MediaTagType.CD_PMA);

                if (pma.Length > 0)
                {
                    ushort dataLen = Swapping.Swap(BitConverter.ToUInt16(pma, 0));
                    if (dataLen + 2 != pma.Length)
                    {
                        byte[] tmp = new byte[pma.Length + 2];
                        Array.Copy(pma, 0, tmp, 2, pma.Length);
                        tmp[0] = (byte)((pma.Length & 0xFF00) >> 8);
                        tmp[1] = (byte)(pma.Length & 0xFF);
                        pma    = tmp;
                    }
                }
            }

            if (imageFormat.Info.ReadableMediaTags != null &&
                imageFormat.Info.ReadableMediaTags.Contains(MediaTagType.CD_ATIP))
            {
                atip = imageFormat.ReadDiskTag(MediaTagType.CD_ATIP);

                uint dataLen = Swapping.Swap(BitConverter.ToUInt32(atip, 0));
                if (dataLen + 4 != atip.Length)
                {
                    byte[] tmp = new byte[atip.Length + 4];
                    Array.Copy(atip, 0, tmp, 4, atip.Length);
                    tmp[0] = (byte)((atip.Length & 0xFF000000) >> 24);
                    tmp[1] = (byte)((atip.Length & 0xFF0000) >> 16);
                    tmp[2] = (byte)((atip.Length & 0xFF00) >> 8);
                    tmp[3] = (byte)(atip.Length & 0xFF);
                    atip   = tmp;
                }

                decodedAtip = ATIP.Decode(atip);
            }

            if (imageFormat.Info.ReadableMediaTags != null &&
                imageFormat.Info.ReadableMediaTags.Contains(MediaTagType.CD_TEXT))
            {
                cdtext = imageFormat.ReadDiskTag(MediaTagType.CD_TEXT);

                uint dataLen = Swapping.Swap(BitConverter.ToUInt32(cdtext, 0));
                if (dataLen + 4 != cdtext.Length)
                {
                    byte[] tmp = new byte[cdtext.Length + 4];
                    Array.Copy(cdtext, 0, tmp, 4, cdtext.Length);
                    tmp[0] = (byte)((cdtext.Length & 0xFF000000) >> 24);
                    tmp[1] = (byte)((cdtext.Length & 0xFF0000) >> 16);
                    tmp[2] = (byte)((cdtext.Length & 0xFF00) >> 8);
                    tmp[3] = (byte)(cdtext.Length & 0xFF);
                    cdtext = tmp;
                }

                decodedCdText = CDTextOnLeadIn.Decode(cdtext);
            }

            if (imageFormat.Info.ReadableMediaTags != null &&
                imageFormat.Info.ReadableMediaTags.Contains(MediaTagType.CD_MCN))
            {
                byte[] mcn = imageFormat.ReadDiskTag(MediaTagType.CD_MCN);

                mediaCatalogueNumber = Encoding.UTF8.GetString(mcn);
            }

            tabCompactDiscInfo tabCompactDiscInfo = new tabCompactDiscInfo();

            tabCompactDiscInfo.LoadData(toc, atip, null, null, fullToc, pma, cdtext, decodedToc, decodedAtip, null,
                                        decodedFullToc, decodedCdText, null, mediaCatalogueNumber, null);
            tabInfos.Pages.Add(tabCompactDiscInfo);

            byte[] dvdPfi = null;
            byte[] dvdDmi = null;
            byte[] dvdCmi = null;
            byte[] hddvdCopyrightInformation = null;
            byte[] dvdBca = null;
            PFI.PhysicalFormatInformation?decodedPfi = null;

            if (imageFormat.Info.ReadableMediaTags != null &&
                imageFormat.Info.ReadableMediaTags.Contains(MediaTagType.DVD_PFI))
            {
                dvdPfi     = imageFormat.ReadDiskTag(MediaTagType.DVD_PFI);
                decodedPfi = PFI.Decode(dvdPfi);
            }

            if (imageFormat.Info.ReadableMediaTags != null &&
                imageFormat.Info.ReadableMediaTags.Contains(MediaTagType.DVD_DMI))
            {
                dvdDmi = imageFormat.ReadDiskTag(MediaTagType.DVD_DMI);
            }

            if (imageFormat.Info.ReadableMediaTags != null &&
                imageFormat.Info.ReadableMediaTags.Contains(MediaTagType.DVD_CMI))
            {
                dvdCmi = imageFormat.ReadDiskTag(MediaTagType.DVD_CMI);
            }

            if (imageFormat.Info.ReadableMediaTags != null &&
                imageFormat.Info.ReadableMediaTags.Contains(MediaTagType.HDDVD_CPI))
            {
                hddvdCopyrightInformation = imageFormat.ReadDiskTag(MediaTagType.HDDVD_CPI);
            }

            if (imageFormat.Info.ReadableMediaTags != null &&
                imageFormat.Info.ReadableMediaTags.Contains(MediaTagType.DVD_BCA))
            {
                dvdBca = imageFormat.ReadDiskTag(MediaTagType.DVD_BCA);
            }

            tabDvdInfo tabDvdInfo = new tabDvdInfo();

            tabDvdInfo.LoadData(imageFormat.Info.MediaType, dvdPfi, dvdDmi, dvdCmi, hddvdCopyrightInformation, dvdBca,
                                null, decodedPfi);
            tabInfos.Pages.Add(tabDvdInfo);

            byte[] dvdRamDds                     = null;
            byte[] dvdRamCartridgeStatus         = null;
            byte[] dvdRamSpareArea               = null;
            byte[] lastBorderOutRmd              = null;
            byte[] dvdPreRecordedInfo            = null;
            byte[] dvdrMediaIdentifier           = null;
            byte[] dvdrPhysicalInformation       = null;
            byte[] hddvdrMediumStatus            = null;
            byte[] dvdrLayerCapacity             = null;
            byte[] dvdrDlMiddleZoneStart         = null;
            byte[] dvdrDlJumpIntervalSize        = null;
            byte[] dvdrDlManualLayerJumpStartLba = null;
            byte[] dvdPlusAdip                   = null;
            byte[] dvdPlusDcb                    = null;

            if (imageFormat.Info.ReadableMediaTags != null &&
                imageFormat.Info.ReadableMediaTags.Contains(MediaTagType.DVDRAM_DDS))
            {
                dvdRamDds = imageFormat.ReadDiskTag(MediaTagType.DVDRAM_DDS);
            }

            if (imageFormat.Info.ReadableMediaTags != null &&
                imageFormat.Info.ReadableMediaTags.Contains(MediaTagType.DVDRAM_MediumStatus))
            {
                dvdRamCartridgeStatus = imageFormat.ReadDiskTag(MediaTagType.DVDRAM_MediumStatus);
            }

            if (imageFormat.Info.ReadableMediaTags != null &&
                imageFormat.Info.ReadableMediaTags.Contains(MediaTagType.DVDRAM_SpareArea))
            {
                dvdRamSpareArea = imageFormat.ReadDiskTag(MediaTagType.DVDRAM_SpareArea);
            }

            if (imageFormat.Info.ReadableMediaTags != null &&
                imageFormat.Info.ReadableMediaTags.Contains(MediaTagType.DVDR_RMD))
            {
                lastBorderOutRmd = imageFormat.ReadDiskTag(MediaTagType.DVDR_RMD);
            }

            if (imageFormat.Info.ReadableMediaTags != null &&
                imageFormat.Info.ReadableMediaTags.Contains(MediaTagType.DVDR_PreRecordedInfo))
            {
                dvdPreRecordedInfo = imageFormat.ReadDiskTag(MediaTagType.DVDR_PreRecordedInfo);
            }

            if (imageFormat.Info.ReadableMediaTags != null &&
                imageFormat.Info.ReadableMediaTags.Contains(MediaTagType.DVDR_MediaIdentifier))
            {
                dvdrMediaIdentifier = imageFormat.ReadDiskTag(MediaTagType.DVDR_MediaIdentifier);
            }

            if (imageFormat.Info.ReadableMediaTags != null &&
                imageFormat.Info.ReadableMediaTags.Contains(MediaTagType.DVDR_PFI))
            {
                dvdrPhysicalInformation = imageFormat.ReadDiskTag(MediaTagType.DVDR_PFI);
            }

            if (imageFormat.Info.ReadableMediaTags != null &&
                imageFormat.Info.ReadableMediaTags.Contains(MediaTagType.HDDVD_MediumStatus))
            {
                hddvdrMediumStatus = imageFormat.ReadDiskTag(MediaTagType.HDDVD_MediumStatus);
            }

            if (imageFormat.Info.ReadableMediaTags != null &&
                imageFormat.Info.ReadableMediaTags.Contains(MediaTagType.DVDDL_LayerCapacity))
            {
                dvdrLayerCapacity = imageFormat.ReadDiskTag(MediaTagType.DVDDL_LayerCapacity);
            }

            if (imageFormat.Info.ReadableMediaTags != null &&
                imageFormat.Info.ReadableMediaTags.Contains(MediaTagType.DVDDL_MiddleZoneAddress))
            {
                dvdrDlMiddleZoneStart = imageFormat.ReadDiskTag(MediaTagType.DVDDL_MiddleZoneAddress);
            }

            if (imageFormat.Info.ReadableMediaTags != null &&
                imageFormat.Info.ReadableMediaTags.Contains(MediaTagType.DVDDL_JumpIntervalSize))
            {
                dvdrDlJumpIntervalSize = imageFormat.ReadDiskTag(MediaTagType.DVDDL_JumpIntervalSize);
            }

            if (imageFormat.Info.ReadableMediaTags != null &&
                imageFormat.Info.ReadableMediaTags.Contains(MediaTagType.DVDDL_ManualLayerJumpLBA))
            {
                dvdrDlManualLayerJumpStartLba = imageFormat.ReadDiskTag(MediaTagType.DVDDL_ManualLayerJumpLBA);
            }

            if (imageFormat.Info.ReadableMediaTags != null &&
                imageFormat.Info.ReadableMediaTags.Contains(MediaTagType.DVD_ADIP))
            {
                dvdPlusAdip = imageFormat.ReadDiskTag(MediaTagType.DVD_ADIP);
            }

            if (imageFormat.Info.ReadableMediaTags != null &&
                imageFormat.Info.ReadableMediaTags.Contains(MediaTagType.DCB))
            {
                dvdPlusDcb = imageFormat.ReadDiskTag(MediaTagType.DCB);
            }

            tabDvdWritableInfo tabDvdWritableInfo = new tabDvdWritableInfo();

            tabDvdWritableInfo.LoadData(imageFormat.Info.MediaType, dvdRamDds, dvdRamCartridgeStatus, dvdRamSpareArea,
                                        lastBorderOutRmd, dvdPreRecordedInfo, dvdrMediaIdentifier,
                                        dvdrPhysicalInformation, hddvdrMediumStatus, null, dvdrLayerCapacity,
                                        dvdrDlMiddleZoneStart, dvdrDlJumpIntervalSize, dvdrDlManualLayerJumpStartLba,
                                        null, dvdPlusAdip, dvdPlusDcb);
            tabInfos.Pages.Add(tabDvdWritableInfo);

            byte[] blurayBurstCuttingArea = null;
            byte[] blurayCartridgeStatus  = null;
            byte[] blurayDds                  = null;
            byte[] blurayDiscInformation      = null;
            byte[] blurayPowResources         = null;
            byte[] bluraySpareAreaInformation = null;
            byte[] blurayTrackResources       = null;

            if (imageFormat.Info.ReadableMediaTags != null &&
                imageFormat.Info.ReadableMediaTags.Contains(MediaTagType.BD_BCA))
            {
                blurayBurstCuttingArea = imageFormat.ReadDiskTag(MediaTagType.BD_BCA);
            }

            if (imageFormat.Info.ReadableMediaTags != null &&
                imageFormat.Info.ReadableMediaTags.Contains(MediaTagType.BD_CartridgeStatus))
            {
                blurayCartridgeStatus = imageFormat.ReadDiskTag(MediaTagType.BD_CartridgeStatus);
            }

            if (imageFormat.Info.ReadableMediaTags != null &&
                imageFormat.Info.ReadableMediaTags.Contains(MediaTagType.BD_DDS))
            {
                blurayDds = imageFormat.ReadDiskTag(MediaTagType.BD_DDS);
            }

            if (imageFormat.Info.ReadableMediaTags != null &&
                imageFormat.Info.ReadableMediaTags.Contains(MediaTagType.BD_DI))
            {
                blurayDiscInformation = imageFormat.ReadDiskTag(MediaTagType.BD_DI);
            }

            if (imageFormat.Info.ReadableMediaTags != null &&
                imageFormat.Info.ReadableMediaTags.Contains(MediaTagType.MMC_POWResourcesInformation))
            {
                blurayPowResources = imageFormat.ReadDiskTag(MediaTagType.MMC_POWResourcesInformation);
            }

            if (imageFormat.Info.ReadableMediaTags != null &&
                imageFormat.Info.ReadableMediaTags.Contains(MediaTagType.BD_SpareArea))
            {
                bluraySpareAreaInformation = imageFormat.ReadDiskTag(MediaTagType.BD_SpareArea);
            }

            if (imageFormat.Info.ReadableMediaTags != null &&
                imageFormat.Info.ReadableMediaTags.Contains(MediaTagType.MMC_TrackResourcesInformation))
            {
                bluraySpareAreaInformation = imageFormat.ReadDiskTag(MediaTagType.MMC_TrackResourcesInformation);
            }

            tabBlurayInfo tabBlurayInfo = new tabBlurayInfo();

            tabBlurayInfo.LoadData(blurayDiscInformation, blurayBurstCuttingArea, blurayDds, blurayCartridgeStatus,
                                   bluraySpareAreaInformation, blurayPowResources, blurayTrackResources, null, null);
            tabInfos.Pages.Add(tabBlurayInfo);

            byte[]            xboxDmi                   = null;
            byte[]            xboxSecuritySector        = null;
            SS.SecuritySector?decodedXboxSecuritySector = null;

            if (imageFormat.Info.ReadableMediaTags != null &&
                imageFormat.Info.ReadableMediaTags.Contains(MediaTagType.Xbox_DMI))
            {
                xboxDmi = imageFormat.ReadDiskTag(MediaTagType.Xbox_DMI);
            }

            if (imageFormat.Info.ReadableMediaTags != null &&
                imageFormat.Info.ReadableMediaTags.Contains(MediaTagType.Xbox_SecuritySector))
            {
                xboxSecuritySector        = imageFormat.ReadDiskTag(MediaTagType.Xbox_SecuritySector);
                decodedXboxSecuritySector = SS.Decode(xboxSecuritySector);
            }

            tabXboxInfo tabXboxInfo = new tabXboxInfo();

            tabXboxInfo.LoadData(null, xboxDmi, xboxSecuritySector, decodedXboxSecuritySector);
            tabInfos.Pages.Add(tabXboxInfo);

            byte[] pcmciaCis = null;

            if (imageFormat.Info.ReadableMediaTags != null &&
                imageFormat.Info.ReadableMediaTags.Contains(MediaTagType.PCMCIA_CIS))
            {
                pcmciaCis = imageFormat.ReadDiskTag(MediaTagType.PCMCIA_CIS);
            }

            tabPcmciaInfo tabPcmciaInfo = new tabPcmciaInfo();

            tabPcmciaInfo.LoadData(pcmciaCis);
            tabInfos.Pages.Add(tabPcmciaInfo);

            DeviceType deviceType = DeviceType.Unknown;

            byte[] cid         = null;
            byte[] csd         = null;
            byte[] ocr         = null;
            byte[] extendedCsd = null;
            byte[] scr         = null;

            if (imageFormat.Info.ReadableMediaTags != null &&
                imageFormat.Info.ReadableMediaTags.Contains(MediaTagType.SD_CID))
            {
                cid        = imageFormat.ReadDiskTag(MediaTagType.SD_CID);
                deviceType = DeviceType.SecureDigital;
            }

            if (imageFormat.Info.ReadableMediaTags != null &&
                imageFormat.Info.ReadableMediaTags.Contains(MediaTagType.SD_CSD))
            {
                csd        = imageFormat.ReadDiskTag(MediaTagType.SD_CSD);
                deviceType = DeviceType.SecureDigital;
            }

            if (imageFormat.Info.ReadableMediaTags != null &&
                imageFormat.Info.ReadableMediaTags.Contains(MediaTagType.SD_OCR))
            {
                ocr        = imageFormat.ReadDiskTag(MediaTagType.SD_OCR);
                deviceType = DeviceType.SecureDigital;
            }

            if (imageFormat.Info.ReadableMediaTags != null &&
                imageFormat.Info.ReadableMediaTags.Contains(MediaTagType.SD_SCR))
            {
                scr        = imageFormat.ReadDiskTag(MediaTagType.SD_SCR);
                deviceType = DeviceType.SecureDigital;
            }

            if (imageFormat.Info.ReadableMediaTags != null &&
                imageFormat.Info.ReadableMediaTags.Contains(MediaTagType.MMC_CID))
            {
                cid        = imageFormat.ReadDiskTag(MediaTagType.MMC_CID);
                deviceType = DeviceType.MMC;
            }

            if (imageFormat.Info.ReadableMediaTags != null &&
                imageFormat.Info.ReadableMediaTags.Contains(MediaTagType.MMC_CSD))
            {
                csd        = imageFormat.ReadDiskTag(MediaTagType.MMC_CSD);
                deviceType = DeviceType.MMC;
            }

            if (imageFormat.Info.ReadableMediaTags != null &&
                imageFormat.Info.ReadableMediaTags.Contains(MediaTagType.MMC_OCR))
            {
                ocr        = imageFormat.ReadDiskTag(MediaTagType.MMC_OCR);
                deviceType = DeviceType.MMC;
            }

            if (imageFormat.Info.ReadableMediaTags != null &&
                imageFormat.Info.ReadableMediaTags.Contains(MediaTagType.MMC_ExtendedCSD))
            {
                extendedCsd = imageFormat.ReadDiskTag(MediaTagType.MMC_ExtendedCSD);
                deviceType  = DeviceType.MMC;
            }

            tabSdMmcInfo tabSdMmcInfo = new tabSdMmcInfo();

            tabSdMmcInfo.LoadData(deviceType, cid, csd, ocr, extendedCsd, scr);
            tabInfos.Pages.Add(tabSdMmcInfo);

            if (imageFormat is IOpticalMediaImage opticalMediaImage)
            {
                try
                {
                    if (opticalMediaImage.Sessions != null && opticalMediaImage.Sessions.Count > 0)
                    {
                        TreeGridItemCollection sessionList = new TreeGridItemCollection();

                        treeSessions.Columns.Add(new GridColumn
                        {
                            HeaderText = "Session", DataCell = new TextBoxCell(0)
                        });
                        treeSessions.Columns.Add(new GridColumn
                        {
                            HeaderText = "First track", DataCell = new TextBoxCell(1)
                        });
                        treeSessions.Columns.Add(new GridColumn
                        {
                            HeaderText = "Last track", DataCell = new TextBoxCell(2)
                        });
                        treeSessions.Columns.Add(new GridColumn {
                            HeaderText = "Start", DataCell = new TextBoxCell(3)
                        });
                        treeSessions.Columns.Add(new GridColumn {
                            HeaderText = "End", DataCell = new TextBoxCell(4)
                        });

                        treeSessions.AllowMultipleSelection = false;
                        treeSessions.ShowHeader             = true;
                        treeSessions.DataStore = sessionList;

                        foreach (Session session in opticalMediaImage.Sessions)
                        {
                            sessionList.Add(new TreeGridItem
                            {
                                Values = new object[]
                                {
                                    session.SessionSequence, session.StartTrack,
                                    session.EndTrack, session.StartSector, session.EndSector
                                }
                            });
                        }

                        tabSessions.Visible = true;
                    }
                }
                catch
                {
                    // ignored
                }

                try
                {
                    if (opticalMediaImage.Tracks != null && opticalMediaImage.Tracks.Count > 0)
                    {
                        TreeGridItemCollection tracksList = new TreeGridItemCollection();

                        treeTracks.Columns.Add(new GridColumn {
                            HeaderText = "Track", DataCell = new TextBoxCell(0)
                        });
                        treeTracks.Columns.Add(new GridColumn {
                            HeaderText = "Type", DataCell = new TextBoxCell(1)
                        });
                        treeTracks.Columns.Add(new GridColumn {
                            HeaderText = "Bps", DataCell = new TextBoxCell(2)
                        });
                        treeTracks.Columns.Add(new GridColumn {
                            HeaderText = "Raw bps", DataCell = new TextBoxCell(3)
                        });
                        treeTracks.Columns.Add(new GridColumn
                        {
                            HeaderText = "Subchannel", DataCell = new TextBoxCell(4)
                        });
                        treeTracks.Columns.Add(new GridColumn {
                            HeaderText = "Pregap", DataCell = new TextBoxCell(5)
                        });
                        treeTracks.Columns.Add(new GridColumn {
                            HeaderText = "Start", DataCell = new TextBoxCell(6)
                        });
                        treeTracks.Columns.Add(new GridColumn {
                            HeaderText = "End", DataCell = new TextBoxCell(7)
                        });

                        treeTracks.AllowMultipleSelection = false;
                        treeTracks.ShowHeader             = true;
                        treeTracks.DataStore = tracksList;

                        foreach (Track track in opticalMediaImage.Tracks)
                        {
                            tracksList.Add(new TreeGridItem
                            {
                                Values = new object[]
                                {
                                    track.TrackSequence, track.TrackType,
                                    track.TrackBytesPerSector, track.TrackRawBytesPerSector,
                                    track.TrackSubchannelType, track.TrackPregap,
                                    track.TrackStartSector, track.TrackEndSector
                                }
                            });
                        }

                        tabTracks.Visible = true;
                    }
                }
                catch
                {
                    // ignored
                }
            }

            if (imageFormat.DumpHardware == null)
            {
                return;
            }

            TreeGridItemCollection dumpHardwareList = new TreeGridItemCollection();

            treeDumpHardware.Columns.Add(new GridColumn {
                HeaderText = "Manufacturer", DataCell = new TextBoxCell(0)
            });
            treeDumpHardware.Columns.Add(new GridColumn {
                HeaderText = "Model", DataCell = new TextBoxCell(1)
            });
            treeDumpHardware.Columns.Add(new GridColumn {
                HeaderText = "Serial", DataCell = new TextBoxCell(2)
            });
            treeDumpHardware.Columns.Add(new GridColumn {
                HeaderText = "Software", DataCell = new TextBoxCell(3)
            });
            treeDumpHardware.Columns.Add(new GridColumn {
                HeaderText = "Version", DataCell = new TextBoxCell(4)
            });
            treeDumpHardware.Columns.Add(new GridColumn
            {
                HeaderText = "Operating system", DataCell = new TextBoxCell(5)
            });
            treeDumpHardware.Columns.Add(new GridColumn {
                HeaderText = "Start", DataCell = new TextBoxCell(6)
            });
            treeDumpHardware.Columns.Add(new GridColumn {
                HeaderText = "End", DataCell = new TextBoxCell(7)
            });

            treeDumpHardware.AllowMultipleSelection = false;
            treeDumpHardware.ShowHeader             = true;
            treeDumpHardware.DataStore = dumpHardwareList;

            foreach (DumpHardwareType dump in imageFormat.DumpHardware)
            {
                foreach (ExtentType extent in dump.Extents)
                {
                    dumpHardwareList.Add(new TreeGridItem
                    {
                        Values = new object[]
                        {
                            dump.Manufacturer, dump.Model, dump.Serial, dump.Software.Name,
                            dump.Software.Version, dump.Software.OperatingSystem,
                            extent.Start, extent.End
                        }
                    });
                }
            }

            tabDumpHardware.Visible = true;
        }
示例#7
0
        internal static void DoDecode(DecodeOptions options)
        {
            DicConsole.DebugWriteLine("Decode command", "--debug={0}", options.Debug);
            DicConsole.DebugWriteLine("Decode command", "--verbose={0}", options.Verbose);
            DicConsole.DebugWriteLine("Decode command", "--input={0}", options.InputFile);
            DicConsole.DebugWriteLine("Decode command", "--start={0}", options.StartSector);
            DicConsole.DebugWriteLine("Decode command", "--length={0}", options.Length);
            DicConsole.DebugWriteLine("Decode command", "--disk-tags={0}", options.DiskTags);
            DicConsole.DebugWriteLine("Decode command", "--sector-tags={0}", options.SectorTags);

            FiltersList filtersList = new FiltersList();
            IFilter     inputFilter = filtersList.GetFilter(options.InputFile);

            if (inputFilter == null)
            {
                DicConsole.ErrorWriteLine("Cannot open specified file.");
                return;
            }

            IMediaImage inputFormat = ImageFormat.Detect(inputFilter);

            if (inputFormat == null)
            {
                DicConsole.ErrorWriteLine("Unable to recognize image format, not decoding");
                return;
            }

            inputFormat.Open(inputFilter);
            Core.Statistics.AddMediaFormat(inputFormat.Format);
            Core.Statistics.AddMedia(inputFormat.Info.MediaType, false);
            Core.Statistics.AddFilter(inputFilter.Name);

            if (options.DiskTags)
            {
                if (inputFormat.Info.ReadableMediaTags.Count == 0)
                {
                    DicConsole.WriteLine("There are no disk tags in chosen disc image.");
                }
                else
                {
                    foreach (MediaTagType tag in inputFormat.Info.ReadableMediaTags)
                    {
                        switch (tag)
                        {
                        case MediaTagType.SCSI_INQUIRY:
                        {
                            byte[] inquiry = inputFormat.ReadDiskTag(MediaTagType.SCSI_INQUIRY);
                            if (inquiry == null)
                            {
                                DicConsole.WriteLine("Error reading SCSI INQUIRY response from disc image");
                            }
                            else
                            {
                                DicConsole.WriteLine("SCSI INQUIRY command response:");
                                DicConsole
                                .WriteLine("================================================================================");
                                DicConsole.WriteLine(Inquiry.Prettify(inquiry));
                                DicConsole
                                .WriteLine("================================================================================");
                            }
                            break;
                        }

                        case MediaTagType.ATA_IDENTIFY:
                        {
                            byte[] identify = inputFormat.ReadDiskTag(MediaTagType.ATA_IDENTIFY);
                            if (identify == null)
                            {
                                DicConsole.WriteLine("Error reading ATA IDENTIFY DEVICE response from disc image");
                            }
                            else
                            {
                                DicConsole.WriteLine("ATA IDENTIFY DEVICE command response:");
                                DicConsole
                                .WriteLine("================================================================================");
                                DicConsole.WriteLine(Identify.Prettify(identify));
                                DicConsole
                                .WriteLine("================================================================================");
                            }
                            break;
                        }

                        case MediaTagType.ATAPI_IDENTIFY:
                        {
                            byte[] identify = inputFormat.ReadDiskTag(MediaTagType.ATAPI_IDENTIFY);
                            if (identify == null)
                            {
                                DicConsole
                                .WriteLine("Error reading ATA IDENTIFY PACKET DEVICE response from disc image");
                            }
                            else
                            {
                                DicConsole.WriteLine("ATA IDENTIFY PACKET DEVICE command response:");
                                DicConsole
                                .WriteLine("================================================================================");
                                DicConsole.WriteLine(Identify.Prettify(identify));
                                DicConsole
                                .WriteLine("================================================================================");
                            }
                            break;
                        }

                        case MediaTagType.CD_ATIP:
                        {
                            byte[] atip = inputFormat.ReadDiskTag(MediaTagType.CD_ATIP);
                            if (atip == null)
                            {
                                DicConsole.WriteLine("Error reading CD ATIP from disc image");
                            }
                            else
                            {
                                DicConsole.WriteLine("CD ATIP:");
                                DicConsole
                                .WriteLine("================================================================================");
                                DicConsole.WriteLine(ATIP.Prettify(atip));
                                DicConsole
                                .WriteLine("================================================================================");
                            }
                            break;
                        }

                        case MediaTagType.CD_FullTOC:
                        {
                            byte[] fulltoc = inputFormat.ReadDiskTag(MediaTagType.CD_FullTOC);
                            if (fulltoc == null)
                            {
                                DicConsole.WriteLine("Error reading CD full TOC from disc image");
                            }
                            else
                            {
                                DicConsole.WriteLine("CD full TOC:");
                                DicConsole
                                .WriteLine("================================================================================");
                                DicConsole.WriteLine(FullTOC.Prettify(fulltoc));
                                DicConsole
                                .WriteLine("================================================================================");
                            }
                            break;
                        }

                        case MediaTagType.CD_PMA:
                        {
                            byte[] pma = inputFormat.ReadDiskTag(MediaTagType.CD_PMA);
                            if (pma == null)
                            {
                                DicConsole.WriteLine("Error reading CD PMA from disc image");
                            }
                            else
                            {
                                DicConsole.WriteLine("CD PMA:");
                                DicConsole
                                .WriteLine("================================================================================");
                                DicConsole.WriteLine(PMA.Prettify(pma));
                                DicConsole
                                .WriteLine("================================================================================");
                            }
                            break;
                        }

                        case MediaTagType.CD_SessionInfo:
                        {
                            byte[] sessioninfo = inputFormat.ReadDiskTag(MediaTagType.CD_SessionInfo);
                            if (sessioninfo == null)
                            {
                                DicConsole.WriteLine("Error reading CD session information from disc image");
                            }
                            else
                            {
                                DicConsole.WriteLine("CD session information:");
                                DicConsole
                                .WriteLine("================================================================================");
                                DicConsole.WriteLine(Session.Prettify(sessioninfo));
                                DicConsole
                                .WriteLine("================================================================================");
                            }
                            break;
                        }

                        case MediaTagType.CD_TEXT:
                        {
                            byte[] cdtext = inputFormat.ReadDiskTag(MediaTagType.CD_TEXT);
                            if (cdtext == null)
                            {
                                DicConsole.WriteLine("Error reading CD-TEXT from disc image");
                            }
                            else
                            {
                                DicConsole.WriteLine("CD-TEXT:");
                                DicConsole
                                .WriteLine("================================================================================");
                                DicConsole.WriteLine(CDTextOnLeadIn.Prettify(cdtext));
                                DicConsole
                                .WriteLine("================================================================================");
                            }
                            break;
                        }

                        case MediaTagType.CD_TOC:
                        {
                            byte[] toc = inputFormat.ReadDiskTag(MediaTagType.CD_TOC);
                            if (toc == null)
                            {
                                DicConsole.WriteLine("Error reading CD TOC from disc image");
                            }
                            else
                            {
                                DicConsole.WriteLine("CD TOC:");
                                DicConsole
                                .WriteLine("================================================================================");
                                DicConsole.WriteLine(TOC.Prettify(toc));
                                DicConsole
                                .WriteLine("================================================================================");
                            }
                            break;
                        }

                        default:
                            DicConsole.WriteLine("Decoder for disk tag type \"{0}\" not yet implemented, sorry.",
                                                 tag);
                            break;
                        }
                    }
                }
            }

            if (options.SectorTags)
            {
                ulong length;

                if (options.Length.ToLowerInvariant() == "all")
                {
                    length = inputFormat.Info.Sectors - 1;
                }
                else
                {
                    if (!ulong.TryParse(options.Length, out length))
                    {
                        DicConsole.WriteLine("Value \"{0}\" is not a valid number for length.", options.Length);
                        DicConsole.WriteLine("Not decoding sectors tags");
                        return;
                    }
                }

                if (inputFormat.Info.ReadableSectorTags.Count == 0)
                {
                    DicConsole.WriteLine("There are no sector tags in chosen disc image.");
                }
                else
                {
                    foreach (SectorTagType tag in inputFormat.Info.ReadableSectorTags)
                    {
                        switch (tag)
                        {
                        default:
                            DicConsole.WriteLine("Decoder for disk tag type \"{0}\" not yet implemented, sorry.",
                                                 tag);
                            break;
                        }
                    }
                }

                // TODO: Not implemented
            }

            Core.Statistics.AddCommand("decode");
        }
示例#8
0
文件: Compare.cs 项目: paulyc/Aaru
        public static int Invoke(bool debug, bool verbose, string imagePath1, string imagePath2)
        {
            MainClass.PrintCopyright();

            if (debug)
            {
                AaruConsole.DebugWriteLineEvent += System.Console.Error.WriteLine;
            }

            if (verbose)
            {
                AaruConsole.VerboseWriteLineEvent += System.Console.WriteLine;
            }

            Statistics.AddCommand("compare");

            AaruConsole.DebugWriteLine("Compare command", "--debug={0}", debug);
            AaruConsole.DebugWriteLine("Compare command", "--input1={0}", imagePath1);
            AaruConsole.DebugWriteLine("Compare command", "--input2={0}", imagePath2);
            AaruConsole.DebugWriteLine("Compare command", "--verbose={0}", verbose);

            var     filtersList  = new FiltersList();
            IFilter inputFilter1 = filtersList.GetFilter(imagePath1);

            filtersList = new FiltersList();
            IFilter inputFilter2 = filtersList.GetFilter(imagePath2);

            if (inputFilter1 == null)
            {
                AaruConsole.ErrorWriteLine("Cannot open input file 1");

                return((int)ErrorNumber.CannotOpenFile);
            }

            if (inputFilter2 == null)
            {
                AaruConsole.ErrorWriteLine("Cannot open input file 2");

                return((int)ErrorNumber.CannotOpenFile);
            }

            IMediaImage input1Format = ImageFormat.Detect(inputFilter1);
            IMediaImage input2Format = ImageFormat.Detect(inputFilter2);

            if (input1Format == null)
            {
                AaruConsole.ErrorWriteLine("Input file 1 format not identified, not proceeding with comparison.");

                return((int)ErrorNumber.UnrecognizedFormat);
            }

            if (verbose)
            {
                AaruConsole.VerboseWriteLine("Input file 1 format identified by {0} ({1}).", input1Format.Name,
                                             input1Format.Id);
            }
            else
            {
                AaruConsole.WriteLine("Input file 1 format identified by {0}.", input1Format.Name);
            }

            if (input2Format == null)
            {
                AaruConsole.ErrorWriteLine("Input file 2 format not identified, not proceeding with comparison.");

                return((int)ErrorNumber.UnrecognizedFormat);
            }

            if (verbose)
            {
                AaruConsole.VerboseWriteLine("Input file 2 format identified by {0} ({1}).", input2Format.Name,
                                             input2Format.Id);
            }
            else
            {
                AaruConsole.WriteLine("Input file 2 format identified by {0}.", input2Format.Name);
            }

            input1Format.Open(inputFilter1);
            input2Format.Open(inputFilter2);

            Statistics.AddMediaFormat(input1Format.Format);
            Statistics.AddMediaFormat(input2Format.Format);
            Statistics.AddMedia(input1Format.Info.MediaType, false);
            Statistics.AddMedia(input2Format.Info.MediaType, false);
            Statistics.AddFilter(inputFilter1.Name);
            Statistics.AddFilter(inputFilter2.Name);

            var sb = new StringBuilder();

            if (verbose)
            {
                sb.AppendFormat("{0,50}{1,20}", "Disc image 1", "Disc image 2").AppendLine();
                sb.AppendLine("================================================================================================");
                sb.AppendFormat("{0,-38}{1}", "File", imagePath1).AppendLine();

                sb.AppendFormat("                                                          {0}", imagePath2).
                AppendLine();

                sb.AppendFormat("{0,-38}{1,-20}{2}", "Disc image format", input1Format.Name, input2Format.Name).
                AppendLine();
            }
            else
            {
                sb.AppendFormat("Disc image 1: {0}", imagePath1).AppendLine();
                sb.AppendFormat("Disc image 2: {0}", imagePath2).AppendLine();
            }

            bool imagesDiffer = false;

            ImageInfo image1Info = input1Format.Info;
            ImageInfo image2Info = input2Format.Info;
            Dictionary <MediaTagType, byte[]> image1DiskTags = new Dictionary <MediaTagType, byte[]>();
            Dictionary <MediaTagType, byte[]> image2DiskTags = new Dictionary <MediaTagType, byte[]>();

            foreach (MediaTagType diskTag in Enum.GetValues(typeof(MediaTagType)))
            {
                try
                {
                    byte[] tempArray = input1Format.ReadDiskTag(diskTag);
                    image1DiskTags.Add(diskTag, tempArray);
                }
                #pragma warning disable RECS0022 // A catch clause that catches System.Exception and has an empty body
                catch
                {
                    // ignored
                }
                #pragma warning restore RECS0022 // A catch clause that catches System.Exception and has an empty body
            }

            foreach (MediaTagType diskTag in Enum.GetValues(typeof(MediaTagType)))
            {
                try
                {
                    byte[] tempArray = input2Format.ReadDiskTag(diskTag);
                    image2DiskTags.Add(diskTag, tempArray);
                }
                #pragma warning disable RECS0022 // A catch clause that catches System.Exception and has an empty body
                catch
                {
                    // ignored
                }
                #pragma warning restore RECS0022 // A catch clause that catches System.Exception and has an empty body
            }

            if (verbose)
            {
                sb.AppendFormat("{0,-38}{1,-20}{2}", "Has partitions?", image1Info.HasPartitions,
                                image2Info.HasPartitions).AppendLine();

                sb.AppendFormat("{0,-38}{1,-20}{2}", "Has sessions?", image1Info.HasSessions, image2Info.HasSessions).
                AppendLine();

                sb.AppendFormat("{0,-38}{1,-20}{2}", "Image size", image1Info.ImageSize, image2Info.ImageSize).
                AppendLine();

                sb.AppendFormat("{0,-38}{1,-20}{2}", "Sectors", image1Info.Sectors, image2Info.Sectors).AppendLine();

                sb.AppendFormat("{0,-38}{1,-20}{2}", "Sector size", image1Info.SectorSize, image2Info.SectorSize).
                AppendLine();

                sb.AppendFormat("{0,-38}{1,-20}{2}", "Creation time", image1Info.CreationTime, image2Info.CreationTime).
                AppendLine();

                sb.AppendFormat("{0,-38}{1,-20}{2}", "Last modification time", image1Info.LastModificationTime,
                                image2Info.LastModificationTime).AppendLine();

                sb.AppendFormat("{0,-38}{1,-20}{2}", "Disk type", image1Info.MediaType, image2Info.MediaType).
                AppendLine();

                sb.AppendFormat("{0,-38}{1,-20}{2}", "Image version", image1Info.Version, image2Info.Version).
                AppendLine();

                sb.AppendFormat("{0,-38}{1,-20}{2}", "Image application", image1Info.Application,
                                image2Info.Application).AppendLine();

                sb.AppendFormat("{0,-38}{1,-20}{2}", "Image application version", image1Info.ApplicationVersion,
                                image2Info.ApplicationVersion).AppendLine();

                sb.AppendFormat("{0,-38}{1,-20}{2}", "Image creator", image1Info.Creator, image2Info.Creator).
                AppendLine();

                sb.AppendFormat("{0,-38}{1,-20}{2}", "Image name", image1Info.MediaTitle, image2Info.MediaTitle).
                AppendLine();

                sb.AppendFormat("{0,-38}{1,-20}{2}", "Image comments", image1Info.Comments, image2Info.Comments).
                AppendLine();

                sb.AppendFormat("{0,-38}{1,-20}{2}", "Image comments", image1Info.Comments, image2Info.Comments).
                AppendLine();

                sb.AppendFormat("{0,-38}{1,-20}{2}", "Disk manufacturer", image1Info.MediaManufacturer,
                                image2Info.MediaManufacturer).AppendLine();

                sb.AppendFormat("{0,-38}{1,-20}{2}", "Disk model", image1Info.MediaModel, image2Info.MediaModel).
                AppendLine();

                sb.AppendFormat("{0,-38}{1,-20}{2}", "Disk serial number", image1Info.MediaSerialNumber,
                                image2Info.MediaSerialNumber).AppendLine();

                sb.AppendFormat("{0,-38}{1,-20}{2}", "Disk barcode", image1Info.MediaBarcode, image2Info.MediaBarcode).
                AppendLine();

                sb.AppendFormat("{0,-38}{1,-20}{2}", "Disk part no.", image1Info.MediaPartNumber,
                                image2Info.MediaPartNumber).AppendLine();

                sb.AppendFormat("{0,-38}{1,-20}{2}", "Disk sequence", image1Info.MediaSequence,
                                image2Info.MediaSequence).AppendLine();

                sb.AppendFormat("{0,-38}{1,-20}{2}", "Last disk on sequence", image1Info.LastMediaSequence,
                                image2Info.LastMediaSequence).AppendLine();

                sb.AppendFormat("{0,-38}{1,-20}{2}", "Drive manufacturer", image1Info.DriveManufacturer,
                                image2Info.DriveManufacturer).AppendLine();

                sb.AppendFormat("{0,-38}{1,-20}{2}", "Drive firmware revision", image1Info.DriveFirmwareRevision,
                                image2Info.DriveFirmwareRevision).AppendLine();

                sb.AppendFormat("{0,-38}{1,-20}{2}", "Drive model", image1Info.DriveModel, image2Info.DriveModel).
                AppendLine();

                sb.AppendFormat("{0,-38}{1,-20}{2}", "Drive serial number", image1Info.DriveSerialNumber,
                                image2Info.DriveSerialNumber).AppendLine();

                foreach (MediaTagType diskTag in
                         (Enum.GetValues(typeof(MediaTagType)) as MediaTagType[]).OrderBy(e => e.ToString()))
                {
                    sb.AppendFormat("{0,-38}{1,-20}{2}", $"Has {diskTag}?", image1DiskTags.ContainsKey(diskTag),
                                    image2DiskTags.ContainsKey(diskTag)).AppendLine();
                }
            }

            AaruConsole.WriteLine("Comparing disk image characteristics");

            if (image1Info.HasPartitions != image2Info.HasPartitions)
            {
                imagesDiffer = true;

                if (!verbose)
                {
                    sb.AppendLine("Image partitioned status differ");
                }
            }

            if (image1Info.HasSessions != image2Info.HasSessions)
            {
                imagesDiffer = true;

                if (!verbose)
                {
                    sb.AppendLine("Image session status differ");
                }
            }

            if (image1Info.Sectors != image2Info.Sectors)
            {
                imagesDiffer = true;

                if (!verbose)
                {
                    sb.AppendLine("Image sectors differ");
                }
            }

            if (image1Info.SectorSize != image2Info.SectorSize)
            {
                imagesDiffer = true;

                if (!verbose)
                {
                    sb.AppendLine("Image sector size differ");
                }
            }

            if (image1Info.MediaType != image2Info.MediaType)
            {
                imagesDiffer = true;

                if (!verbose)
                {
                    sb.AppendLine("Disk type differ");
                }
            }

            ulong leastSectors;

            if (image1Info.Sectors < image2Info.Sectors)
            {
                imagesDiffer = true;
                leastSectors = image1Info.Sectors;

                if (!verbose)
                {
                    sb.AppendLine("Image 2 has more sectors");
                }
            }
            else if (image1Info.Sectors > image2Info.Sectors)
            {
                imagesDiffer = true;
                leastSectors = image2Info.Sectors;

                if (!verbose)
                {
                    sb.AppendLine("Image 1 has more sectors");
                }
            }
            else
            {
                leastSectors = image1Info.Sectors;
            }

            AaruConsole.WriteLine("Comparing sectors...");

            for (ulong sector = 0; sector < leastSectors; sector++)
            {
                AaruConsole.Write("\rComparing sector {0} of {1}...", sector + 1, leastSectors);

                try
                {
                    byte[] image1Sector = input1Format.ReadSector(sector);
                    byte[] image2Sector = input2Format.ReadSector(sector);
                    ArrayHelpers.CompareBytes(out bool different, out bool sameSize, image1Sector, image2Sector);

                    if (different)
                    {
                        imagesDiffer = true;
                        sb.AppendFormat("Sector {0} is different", sector).AppendLine();
                    }
                    else if (!sameSize)
                    {
                        imagesDiffer = true;

                        sb.
                        AppendFormat("Sector {0} has different sizes ({1} bytes in image 1, {2} in image 2) but are otherwise identical",
                                     sector, image1Sector.LongLength, image2Sector.LongLength).AppendLine();
                    }
                }
                #pragma warning disable RECS0022 // A catch clause that catches System.Exception and has an empty body
                catch
                {
                    // ignored
                }
                #pragma warning restore RECS0022 // A catch clause that catches System.Exception and has an empty body
            }

            AaruConsole.WriteLine();

            sb.AppendLine(imagesDiffer ? "Images differ" : "Images do not differ");

            AaruConsole.WriteLine(sb.ToString());

            return((int)ErrorNumber.NoError);
        }
示例#9
0
        public static void PrintImageInfo(IMediaImage imageFormat)
        {
            DicConsole.WriteLine("Image information:");
            if (!string.IsNullOrWhiteSpace(imageFormat.Info.Version))
            {
                DicConsole.WriteLine("Format: {0} version {1}", imageFormat.Format, imageFormat.Info.Version);
            }
            else
            {
                DicConsole.WriteLine("Format: {0}", imageFormat.Format);
            }
            if (!string.IsNullOrWhiteSpace(imageFormat.Info.Application) &&
                !string.IsNullOrWhiteSpace(imageFormat.Info.ApplicationVersion))
            {
                DicConsole.WriteLine("Was created with {0} version {1}", imageFormat.Info.Application,
                                     imageFormat.Info.ApplicationVersion);
            }
            else if (!string.IsNullOrWhiteSpace(imageFormat.Info.Application))
            {
                DicConsole.WriteLine("Was created with {0}", imageFormat.Info.Application);
            }
            DicConsole.WriteLine("Image without headers is {0} bytes long", imageFormat.Info.ImageSize);
            DicConsole.WriteLine("Contains a media of {0} sectors with a maximum sector size of {1} bytes (if all sectors are of the same size this would be {2} bytes)",
                                 imageFormat.Info.Sectors, imageFormat.Info.SectorSize,
                                 imageFormat.Info.Sectors * imageFormat.Info.SectorSize);
            if (!string.IsNullOrWhiteSpace(imageFormat.Info.Creator))
            {
                DicConsole.WriteLine("Created by: {0}", imageFormat.Info.Creator);
            }
            if (imageFormat.Info.CreationTime != DateTime.MinValue)
            {
                DicConsole.WriteLine("Created on {0}", imageFormat.Info.CreationTime);
            }
            if (imageFormat.Info.LastModificationTime != DateTime.MinValue)
            {
                DicConsole.WriteLine("Last modified on {0}", imageFormat.Info.LastModificationTime);
            }
            DicConsole.WriteLine("Contains a media of type {0} and XML type {1}", imageFormat.Info.MediaType,
                                 imageFormat.Info.XmlMediaType);
            DicConsole.WriteLine("{0} partitions", imageFormat.Info.HasPartitions ? "Has" : "Doesn't have");
            DicConsole.WriteLine("{0} sessions", imageFormat.Info.HasSessions ? "Has" : "Doesn't have");
            if (!string.IsNullOrWhiteSpace(imageFormat.Info.Comments))
            {
                DicConsole.WriteLine("Comments: {0}", imageFormat.Info.Comments);
            }
            if (imageFormat.Info.MediaSequence != 0 && imageFormat.Info.LastMediaSequence != 0)
            {
                DicConsole.WriteLine("Media is number {0} on a set of {1} medias", imageFormat.Info.MediaSequence,
                                     imageFormat.Info.LastMediaSequence);
            }
            if (!string.IsNullOrWhiteSpace(imageFormat.Info.MediaTitle))
            {
                DicConsole.WriteLine("Media title: {0}", imageFormat.Info.MediaTitle);
            }
            if (!string.IsNullOrWhiteSpace(imageFormat.Info.MediaManufacturer))
            {
                DicConsole.WriteLine("Media manufacturer: {0}", imageFormat.Info.MediaManufacturer);
            }
            if (!string.IsNullOrWhiteSpace(imageFormat.Info.MediaModel))
            {
                DicConsole.WriteLine("Media model: {0}", imageFormat.Info.MediaModel);
            }
            if (!string.IsNullOrWhiteSpace(imageFormat.Info.MediaSerialNumber))
            {
                DicConsole.WriteLine("Media serial number: {0}", imageFormat.Info.MediaSerialNumber);
            }
            if (!string.IsNullOrWhiteSpace(imageFormat.Info.MediaBarcode))
            {
                DicConsole.WriteLine("Media barcode: {0}", imageFormat.Info.MediaBarcode);
            }
            if (!string.IsNullOrWhiteSpace(imageFormat.Info.MediaPartNumber))
            {
                DicConsole.WriteLine("Media part number: {0}", imageFormat.Info.MediaPartNumber);
            }
            if (!string.IsNullOrWhiteSpace(imageFormat.Info.DriveManufacturer))
            {
                DicConsole.WriteLine("Drive manufacturer: {0}", imageFormat.Info.DriveManufacturer);
            }
            if (!string.IsNullOrWhiteSpace(imageFormat.Info.DriveModel))
            {
                DicConsole.WriteLine("Drive model: {0}", imageFormat.Info.DriveModel);
            }
            if (!string.IsNullOrWhiteSpace(imageFormat.Info.DriveSerialNumber))
            {
                DicConsole.WriteLine("Drive serial number: {0}", imageFormat.Info.DriveSerialNumber);
            }
            if (!string.IsNullOrWhiteSpace(imageFormat.Info.DriveFirmwareRevision))
            {
                DicConsole.WriteLine("Drive firmware info: {0}", imageFormat.Info.DriveFirmwareRevision);
            }
            if (imageFormat.Info.Cylinders > 0 && imageFormat.Info.Heads > 0 &&
                imageFormat.Info.SectorsPerTrack > 0 &&
                imageFormat.Info.XmlMediaType != XmlMediaType.OpticalDisc)
            {
                DicConsole.WriteLine("Media geometry: {0} cylinders, {1} heads, {2} sectors per track",
                                     imageFormat.Info.Cylinders, imageFormat.Info.Heads,
                                     imageFormat.Info.SectorsPerTrack);
            }

            if (imageFormat.Info.ReadableMediaTags != null && imageFormat.Info.ReadableMediaTags.Count > 0)
            {
                DicConsole.WriteLine("Contains {0} readable media tags:", imageFormat.Info.ReadableMediaTags.Count);
                foreach (MediaTagType tag in imageFormat.Info.ReadableMediaTags.OrderBy(t => t))
                {
                    DicConsole.Write("{0} ", tag);
                }
                DicConsole.WriteLine();
            }

            if (imageFormat.Info.ReadableSectorTags != null && imageFormat.Info.ReadableSectorTags.Count > 0)
            {
                DicConsole.WriteLine("Contains {0} readable sector tags:", imageFormat.Info.ReadableSectorTags.Count);
                foreach (SectorTagType tag in imageFormat.Info.ReadableSectorTags.OrderBy(t => t))
                {
                    DicConsole.Write("{0} ", tag);
                }
                DicConsole.WriteLine();
            }

            DicConsole.WriteLine();
            PeripheralDeviceTypes scsiDeviceType = PeripheralDeviceTypes.DirectAccess;

            byte[] scsiVendorId = null;

            if (imageFormat.Info.ReadableMediaTags != null &&
                imageFormat.Info.ReadableMediaTags.Contains(MediaTagType.SCSI_INQUIRY))
            {
                byte[] inquiry = imageFormat.ReadDiskTag(MediaTagType.SCSI_INQUIRY);

                scsiDeviceType = (PeripheralDeviceTypes)(inquiry[0] & 0x1F);
                if (inquiry.Length >= 16)
                {
                    scsiVendorId = new byte[8];
                    Array.Copy(inquiry, 8, scsiVendorId, 0, 8);
                }

                DicConsole.WriteLine("SCSI INQUIRY contained in image:");
                DicConsole.Write("{0}", Inquiry.Prettify(inquiry));
                DicConsole.WriteLine();
            }

            if (imageFormat.Info.ReadableMediaTags != null &&
                imageFormat.Info.ReadableMediaTags.Contains(MediaTagType.ATA_IDENTIFY))
            {
                byte[] identify = imageFormat.ReadDiskTag(MediaTagType.ATA_IDENTIFY);

                DicConsole.WriteLine("ATA IDENTIFY contained in image:");
                DicConsole.Write("{0}", Identify.Prettify(identify));
                DicConsole.WriteLine();
            }

            if (imageFormat.Info.ReadableMediaTags != null &&
                imageFormat.Info.ReadableMediaTags.Contains(MediaTagType.ATAPI_IDENTIFY))
            {
                byte[] identify = imageFormat.ReadDiskTag(MediaTagType.ATAPI_IDENTIFY);

                DicConsole.WriteLine("ATAPI IDENTIFY contained in image:");
                DicConsole.Write("{0}", Identify.Prettify(identify));
                DicConsole.WriteLine();
            }

            if (imageFormat.Info.ReadableMediaTags != null &&
                imageFormat.Info.ReadableMediaTags.Contains(MediaTagType.SCSI_MODESENSE_10))
            {
                byte[]            modeSense10 = imageFormat.ReadDiskTag(MediaTagType.SCSI_MODESENSE_10);
                Modes.DecodedMode?decMode     = Modes.DecodeMode10(modeSense10, scsiDeviceType);

                if (decMode.HasValue)
                {
                    DicConsole.WriteLine("SCSI MODE SENSE (10) contained in image:");
                    PrintScsiModePages.Print(decMode.Value, scsiDeviceType, scsiVendorId);
                    DicConsole.WriteLine();
                }
            }
            else if (imageFormat.Info.ReadableMediaTags != null &&
                     imageFormat.Info.ReadableMediaTags.Contains(MediaTagType.SCSI_MODESENSE_6))
            {
                byte[]            modeSense6 = imageFormat.ReadDiskTag(MediaTagType.SCSI_MODESENSE_6);
                Modes.DecodedMode?decMode    = Modes.DecodeMode6(modeSense6, scsiDeviceType);

                if (decMode.HasValue)
                {
                    DicConsole.WriteLine("SCSI MODE SENSE (6) contained in image:");
                    PrintScsiModePages.Print(decMode.Value, scsiDeviceType, scsiVendorId);
                    DicConsole.WriteLine();
                }
            }
            else if (imageFormat.Info.ReadableMediaTags != null &&
                     imageFormat.Info.ReadableMediaTags.Contains(MediaTagType.SCSI_MODEPAGE_2A))
            {
                byte[] mode2A = imageFormat.ReadDiskTag(MediaTagType.SCSI_MODEPAGE_2A);

                DicConsole.Write("{0}", Modes.PrettifyModePage_2A(mode2A));
                DicConsole.WriteLine();
            }

            if (imageFormat.Info.ReadableMediaTags != null &&
                imageFormat.Info.ReadableMediaTags.Contains(MediaTagType.CD_FullTOC))
            {
                byte[] toc = imageFormat.ReadDiskTag(MediaTagType.CD_FullTOC);

                if (toc.Length > 0)
                {
                    ushort dataLen = Swapping.Swap(BitConverter.ToUInt16(toc, 0));
                    if (dataLen + 2 != toc.Length)
                    {
                        byte[] tmp = new byte[toc.Length + 2];
                        Array.Copy(toc, 0, tmp, 2, toc.Length);
                        tmp[0] = (byte)((toc.Length & 0xFF00) >> 8);
                        tmp[1] = (byte)(toc.Length & 0xFF);
                        toc    = tmp;
                    }

                    DicConsole.WriteLine("CompactDisc Table of Contents contained in image:");
                    DicConsole.Write("{0}", FullTOC.Prettify(toc));
                    DicConsole.WriteLine();
                }
            }

            if (imageFormat.Info.ReadableMediaTags != null &&
                imageFormat.Info.ReadableMediaTags.Contains(MediaTagType.CD_PMA))
            {
                byte[] pma = imageFormat.ReadDiskTag(MediaTagType.CD_PMA);

                if (pma.Length > 0)
                {
                    ushort dataLen = Swapping.Swap(BitConverter.ToUInt16(pma, 0));
                    if (dataLen + 2 != pma.Length)
                    {
                        byte[] tmp = new byte[pma.Length + 2];
                        Array.Copy(pma, 0, tmp, 2, pma.Length);
                        tmp[0] = (byte)((pma.Length & 0xFF00) >> 8);
                        tmp[1] = (byte)(pma.Length & 0xFF);
                        pma    = tmp;
                    }

                    DicConsole.WriteLine("CompactDisc Power Management Area contained in image:");
                    DicConsole.Write("{0}", PMA.Prettify(pma));
                    DicConsole.WriteLine();
                }
            }

            if (imageFormat.Info.ReadableMediaTags != null &&
                imageFormat.Info.ReadableMediaTags.Contains(MediaTagType.CD_ATIP))
            {
                byte[] atip = imageFormat.ReadDiskTag(MediaTagType.CD_ATIP);

                uint dataLen = Swapping.Swap(BitConverter.ToUInt32(atip, 0));
                if (dataLen + 4 != atip.Length)
                {
                    byte[] tmp = new byte[atip.Length + 4];
                    Array.Copy(atip, 0, tmp, 4, atip.Length);
                    tmp[0] = (byte)((atip.Length & 0xFF000000) >> 24);
                    tmp[1] = (byte)((atip.Length & 0xFF0000) >> 16);
                    tmp[2] = (byte)((atip.Length & 0xFF00) >> 8);
                    tmp[3] = (byte)(atip.Length & 0xFF);
                    atip   = tmp;
                }

                DicConsole.WriteLine("CompactDisc Absolute Time In Pregroove (ATIP) contained in image:");
                DicConsole.Write("{0}", ATIP.Prettify(atip));
                DicConsole.WriteLine();
            }

            if (imageFormat.Info.ReadableMediaTags != null &&
                imageFormat.Info.ReadableMediaTags.Contains(MediaTagType.CD_TEXT))
            {
                byte[] cdtext = imageFormat.ReadDiskTag(MediaTagType.CD_TEXT);

                uint dataLen = Swapping.Swap(BitConverter.ToUInt32(cdtext, 0));
                if (dataLen + 4 != cdtext.Length)
                {
                    byte[] tmp = new byte[cdtext.Length + 4];
                    Array.Copy(cdtext, 0, tmp, 4, cdtext.Length);
                    tmp[0] = (byte)((cdtext.Length & 0xFF000000) >> 24);
                    tmp[1] = (byte)((cdtext.Length & 0xFF0000) >> 16);
                    tmp[2] = (byte)((cdtext.Length & 0xFF00) >> 8);
                    tmp[3] = (byte)(cdtext.Length & 0xFF);
                    cdtext = tmp;
                }

                DicConsole.WriteLine("CompactDisc Lead-in's CD-Text contained in image:");
                DicConsole.Write("{0}", CDTextOnLeadIn.Prettify(cdtext));
                DicConsole.WriteLine();
            }

            if (imageFormat.Info.ReadableMediaTags != null &&
                imageFormat.Info.ReadableMediaTags.Contains(MediaTagType.CD_MCN))
            {
                byte[] mcn = imageFormat.ReadDiskTag(MediaTagType.CD_MCN);

                DicConsole.WriteLine("CompactDisc Media Catalogue Number contained in image: {0}",
                                     Encoding.UTF8.GetString(mcn));
                DicConsole.WriteLine();
            }

            if (imageFormat.Info.ReadableMediaTags != null &&
                imageFormat.Info.ReadableMediaTags.Contains(MediaTagType.DVD_PFI))
            {
                byte[] pfi = imageFormat.ReadDiskTag(MediaTagType.DVD_PFI);

                DicConsole.WriteLine("DVD Physical Format Information contained in image:");
                DicConsole.Write("{0}", PFI.Prettify(pfi));
                DicConsole.WriteLine();
            }

            if (imageFormat.Info.ReadableMediaTags != null &&
                imageFormat.Info.ReadableMediaTags.Contains(MediaTagType.DVDRAM_DDS))
            {
                byte[] dds = imageFormat.ReadDiskTag(MediaTagType.DVDRAM_DDS);

                DicConsole.WriteLine("DVD-RAM Disc Definition Structure contained in image:");
                DicConsole.Write("{0}", DDS.Prettify(dds));
                DicConsole.WriteLine();
            }

            if (imageFormat.Info.ReadableMediaTags != null &&
                imageFormat.Info.ReadableMediaTags.Contains(MediaTagType.DVDR_PFI))
            {
                byte[] pfi = imageFormat.ReadDiskTag(MediaTagType.DVDR_PFI);

                DicConsole.WriteLine("DVD-R Physical Format Information contained in image:");
                DicConsole.Write("{0}", PFI.Prettify(pfi));
                DicConsole.WriteLine();
            }

            if (imageFormat.Info.ReadableMediaTags != null &&
                imageFormat.Info.ReadableMediaTags.Contains(MediaTagType.BD_DI))
            {
                byte[] di = imageFormat.ReadDiskTag(MediaTagType.BD_DI);

                DicConsole.WriteLine("Bluray Disc Information contained in image:");
                DicConsole.Write("{0}", DI.Prettify(di));
                DicConsole.WriteLine();
            }

            if (imageFormat.Info.ReadableMediaTags != null &&
                imageFormat.Info.ReadableMediaTags.Contains(MediaTagType.BD_DDS))
            {
                byte[] dds = imageFormat.ReadDiskTag(MediaTagType.BD_DDS);

                DicConsole.WriteLine("Bluray Disc Definition Structure contained in image:");
                DicConsole.Write("{0}", Decoders.Bluray.DDS.Prettify(dds));
                DicConsole.WriteLine();
            }

            if (imageFormat.Info.ReadableMediaTags != null &&
                imageFormat.Info.ReadableMediaTags.Contains(MediaTagType.PCMCIA_CIS))
            {
                byte[] cis = imageFormat.ReadDiskTag(MediaTagType.PCMCIA_CIS);

                DicConsole.WriteLine("PCMCIA CIS:");
                Tuple[] tuples = CIS.GetTuples(cis);
                if (tuples != null)
                {
                    foreach (Tuple tuple in tuples)
                    {
                        switch (tuple.Code)
                        {
                        case TupleCodes.CISTPL_NULL:
                        case TupleCodes.CISTPL_END: break;

                        case TupleCodes.CISTPL_DEVICEGEO:
                        case TupleCodes.CISTPL_DEVICEGEO_A:
                            DicConsole.WriteLine("{0}", CIS.PrettifyDeviceGeometryTuple(tuple));
                            break;

                        case TupleCodes.CISTPL_MANFID:
                            DicConsole.WriteLine("{0}", CIS.PrettifyManufacturerIdentificationTuple(tuple));
                            break;

                        case TupleCodes.CISTPL_VERS_1:
                            DicConsole.WriteLine("{0}", CIS.PrettifyLevel1VersionTuple(tuple));
                            break;

                        case TupleCodes.CISTPL_ALTSTR:
                        case TupleCodes.CISTPL_BAR:
                        case TupleCodes.CISTPL_BATTERY:
                        case TupleCodes.CISTPL_BYTEORDER:
                        case TupleCodes.CISTPL_CFTABLE_ENTRY:
                        case TupleCodes.CISTPL_CFTABLE_ENTRY_CB:
                        case TupleCodes.CISTPL_CHECKSUM:
                        case TupleCodes.CISTPL_CONFIG:
                        case TupleCodes.CISTPL_CONFIG_CB:
                        case TupleCodes.CISTPL_DATE:
                        case TupleCodes.CISTPL_DEVICE:
                        case TupleCodes.CISTPL_DEVICE_A:
                        case TupleCodes.CISTPL_DEVICE_OA:
                        case TupleCodes.CISTPL_DEVICE_OC:
                        case TupleCodes.CISTPL_EXTDEVIC:
                        case TupleCodes.CISTPL_FORMAT:
                        case TupleCodes.CISTPL_FORMAT_A:
                        case TupleCodes.CISTPL_FUNCE:
                        case TupleCodes.CISTPL_FUNCID:
                        case TupleCodes.CISTPL_GEOMETRY:
                        case TupleCodes.CISTPL_INDIRECT:
                        case TupleCodes.CISTPL_JEDEC_A:
                        case TupleCodes.CISTPL_JEDEC_C:
                        case TupleCodes.CISTPL_LINKTARGET:
                        case TupleCodes.CISTPL_LONGLINK_A:
                        case TupleCodes.CISTPL_LONGLINK_C:
                        case TupleCodes.CISTPL_LONGLINK_CB:
                        case TupleCodes.CISTPL_LONGLINK_MFC:
                        case TupleCodes.CISTPL_NO_LINK:
                        case TupleCodes.CISTPL_ORG:
                        case TupleCodes.CISTPL_PWR_MGMNT:
                        case TupleCodes.CISTPL_SPCL:
                        case TupleCodes.CISTPL_SWIL:
                        case TupleCodes.CISTPL_VERS_2:
                            DicConsole.DebugWriteLine("Device-Info command", "Found undecoded tuple ID {0}",
                                                      tuple.Code);
                            break;

                        default:
                            DicConsole.DebugWriteLine("Device-Info command", "Found unknown tuple ID 0x{0:X2}",
                                                      (byte)tuple.Code);
                            break;
                        }
                    }
                }
                else
                {
                    DicConsole.DebugWriteLine("Device-Info command", "Could not get tuples");
                }
            }

            if (imageFormat.Info.ReadableMediaTags != null &&
                imageFormat.Info.ReadableMediaTags.Contains(MediaTagType.SD_CID))
            {
                byte[] cid = imageFormat.ReadDiskTag(MediaTagType.SD_CID);

                DicConsole.WriteLine("SecureDigital CID contained in image:");
                DicConsole.Write("{0}", Decoders.SecureDigital.Decoders.PrettifyCID(cid));
                DicConsole.WriteLine();
            }

            if (imageFormat.Info.ReadableMediaTags != null &&
                imageFormat.Info.ReadableMediaTags.Contains(MediaTagType.SD_CSD))
            {
                byte[] csd = imageFormat.ReadDiskTag(MediaTagType.SD_CSD);

                DicConsole.WriteLine("SecureDigital CSD contained in image:");
                DicConsole.Write("{0}", Decoders.SecureDigital.Decoders.PrettifyCSD(csd));
                DicConsole.WriteLine();
            }

            if (imageFormat.Info.ReadableMediaTags != null &&
                imageFormat.Info.ReadableMediaTags.Contains(MediaTagType.SD_SCR))
            {
                byte[] scr = imageFormat.ReadDiskTag(MediaTagType.SD_SCR);

                DicConsole.WriteLine("SecureDigital SCR contained in image:");
                DicConsole.Write("{0}", Decoders.SecureDigital.Decoders.PrettifySCR(scr));
                DicConsole.WriteLine();
            }

            if (imageFormat.Info.ReadableMediaTags != null &&
                imageFormat.Info.ReadableMediaTags.Contains(MediaTagType.SD_OCR))
            {
                byte[] ocr = imageFormat.ReadDiskTag(MediaTagType.SD_OCR);

                DicConsole.WriteLine("SecureDigital OCR contained in image:");
                DicConsole.Write("{0}", Decoders.SecureDigital.Decoders.PrettifyOCR(ocr));
                DicConsole.WriteLine();
            }

            if (imageFormat.Info.ReadableMediaTags != null &&
                imageFormat.Info.ReadableMediaTags.Contains(MediaTagType.MMC_CID))
            {
                byte[] cid = imageFormat.ReadDiskTag(MediaTagType.MMC_CID);

                DicConsole.WriteLine("MultiMediaCard CID contained in image:");
                DicConsole.Write("{0}", Decoders.MMC.Decoders.PrettifyCID(cid));
                DicConsole.WriteLine();
            }

            if (imageFormat.Info.ReadableMediaTags != null &&
                imageFormat.Info.ReadableMediaTags.Contains(MediaTagType.MMC_CSD))
            {
                byte[] csd = imageFormat.ReadDiskTag(MediaTagType.MMC_CSD);

                DicConsole.WriteLine("MultiMediaCard CSD contained in image:");
                DicConsole.Write("{0}", Decoders.MMC.Decoders.PrettifyCSD(csd));
                DicConsole.WriteLine();
            }

            if (imageFormat.Info.ReadableMediaTags != null &&
                imageFormat.Info.ReadableMediaTags.Contains(MediaTagType.MMC_ExtendedCSD))
            {
                byte[] ecsd = imageFormat.ReadDiskTag(MediaTagType.MMC_ExtendedCSD);

                DicConsole.WriteLine("MultiMediaCard ExtendedCSD contained in image:");
                DicConsole.Write("{0}", Decoders.MMC.Decoders.PrettifyExtendedCSD(ecsd));
                DicConsole.WriteLine();
            }

            if (imageFormat.Info.ReadableMediaTags != null &&
                imageFormat.Info.ReadableMediaTags.Contains(MediaTagType.MMC_OCR))
            {
                byte[] ocr = imageFormat.ReadDiskTag(MediaTagType.MMC_OCR);

                DicConsole.WriteLine("MultiMediaCard OCR contained in image:");
                DicConsole.Write("{0}", Decoders.MMC.Decoders.PrettifyOCR(ocr));
                DicConsole.WriteLine();
            }

            if (imageFormat.Info.ReadableMediaTags != null &&
                imageFormat.Info.ReadableMediaTags.Contains(MediaTagType.Xbox_PFI))
            {
                byte[] xpfi = imageFormat.ReadDiskTag(MediaTagType.Xbox_PFI);

                DicConsole.WriteLine("Xbox Physical Format Information contained in image:");
                DicConsole.Write("{0}", PFI.Prettify(xpfi));
                DicConsole.WriteLine();
            }

            if (imageFormat.Info.ReadableMediaTags != null &&
                imageFormat.Info.ReadableMediaTags.Contains(MediaTagType.Xbox_DMI))
            {
                byte[] xdmi = imageFormat.ReadDiskTag(MediaTagType.Xbox_DMI);

                if (DMI.IsXbox(xdmi))
                {
                    DMI.XboxDMI?xmi = DMI.DecodeXbox(xdmi);
                    if (xmi.HasValue)
                    {
                        DicConsole.WriteLine("Xbox DMI contained in image:");
                        DicConsole.Write("{0}", DMI.PrettifyXbox(xmi));
                        DicConsole.WriteLine();
                    }
                }

                if (DMI.IsXbox360(xdmi))
                {
                    DMI.Xbox360DMI?xmi = DMI.DecodeXbox360(xdmi);
                    if (xmi.HasValue)
                    {
                        DicConsole.WriteLine("Xbox 360 DMI contained in image:");
                        DicConsole.Write("{0}", DMI.PrettifyXbox360(xmi));
                        DicConsole.WriteLine();
                    }
                }
            }

            if (imageFormat.Info.ReadableMediaTags != null &&
                imageFormat.Info.ReadableMediaTags.Contains(MediaTagType.Xbox_SecuritySector))
            {
                byte[] toc = imageFormat.ReadDiskTag(MediaTagType.Xbox_SecuritySector);

                DicConsole.WriteLine("Xbox Security Sectors contained in image:");
                DicConsole.Write("{0}", SS.Prettify(toc));
                DicConsole.WriteLine();
            }

            try
            {
                if (imageFormat.Sessions != null && imageFormat.Sessions.Count > 0)
                {
                    DicConsole.WriteLine("Image sessions:");
                    DicConsole.WriteLine("{0,-9}{1,-13}{2,-12}{3,-12}{4,-12}", "Session", "First track", "Last track",
                                         "Start", "End");
                    DicConsole.WriteLine("=========================================================");
                    foreach (Session session in imageFormat.Sessions)
                    {
                        DicConsole.WriteLine("{0,-9}{1,-13}{2,-12}{3,-12}{4,-12}", session.SessionSequence,
                                             session.StartTrack, session.EndTrack, session.StartSector,
                                             session.EndSector);
                    }
                    DicConsole.WriteLine();
                }
            }
            catch
            {
                // ignored
            }

            try
            {
                if (imageFormat.Tracks != null && imageFormat.Tracks.Count > 0)
                {
                    DicConsole.WriteLine("Image tracks:");
                    DicConsole.WriteLine("{0,-7}{1,-17}{2,-6}{3,-8}{4,-12}{5,-8}{6,-12}{7,-12}", "Track", "Type", "Bps",
                                         "Raw bps", "Subchannel", "Pregap", "Start", "End");
                    DicConsole
                    .WriteLine("=================================================================================");
                    foreach (Track track in imageFormat.Tracks)
                    {
                        DicConsole.WriteLine("{0,-7}{1,-17}{2,-6}{3,-8}{4,-12}{5,-8}{6,-12}{7,-12}",
                                             track.TrackSequence, track.TrackType, track.TrackBytesPerSector,
                                             track.TrackRawBytesPerSector, track.TrackSubchannelType, track.TrackPregap,
                                             track.TrackStartSector, track.TrackEndSector);
                    }
                    DicConsole.WriteLine();
                }
            }
            catch
            {
                // ignored
            }

            if (imageFormat.DumpHardware == null)
            {
                return;
            }

            const string MANUFACTURER_STRING = "Manufacturer";
            const string MODEL_STRING        = "Model";
            const string SERIAL_STRING       = "Serial";
            const string SOFTWARE_STRING     = "Software";
            const string VERSION_STRING      = "Version";
            const string OS_STRING           = "Operating system";
            const string START_STRING        = "Start";
            const string END_STRING          = "End";
            int          manufacturerLen     = MANUFACTURER_STRING.Length;
            int          modelLen            = MODEL_STRING.Length;
            int          serialLen           = SERIAL_STRING.Length;
            int          softwareLen         = SOFTWARE_STRING.Length;
            int          versionLen          = VERSION_STRING.Length;
            int          osLen     = OS_STRING.Length;
            int          sectorLen = START_STRING.Length;

            foreach (DumpHardwareType dump in imageFormat.DumpHardware)
            {
                if (dump.Manufacturer?.Length > manufacturerLen)
                {
                    manufacturerLen = dump.Manufacturer.Length;
                }
                if (dump.Model?.Length > modelLen)
                {
                    modelLen = dump.Model.Length;
                }
                if (dump.Serial?.Length > serialLen)
                {
                    serialLen = dump.Serial.Length;
                }
                if (dump.Software?.Name?.Length > softwareLen)
                {
                    softwareLen = dump.Software.Name.Length;
                }
                if (dump.Software?.Version?.Length > versionLen)
                {
                    versionLen = dump.Software.Version.Length;
                }
                if (dump.Software?.OperatingSystem?.Length > osLen)
                {
                    osLen = dump.Software.OperatingSystem.Length;
                }
                foreach (ExtentType extent in dump.Extents)
                {
                    if ($"{extent.Start}".Length > sectorLen)
                    {
                        sectorLen = $"{extent.Start}".Length;
                    }
                    if ($"{extent.End}".Length > sectorLen)
                    {
                        sectorLen = $"{extent.End}".Length;
                    }
                }
            }

            manufacturerLen += 2;
            modelLen        += 2;
            serialLen       += 2;
            softwareLen     += 2;
            versionLen      += 2;
            osLen           += 2;
            sectorLen       += 2;
            sectorLen       += 2;

            char[] separator = new char[manufacturerLen + modelLen + serialLen + softwareLen + versionLen + osLen +
                                        sectorLen + sectorLen];
            for (int i = 0; i < separator.Length; i++)
            {
                separator[i] = '=';
            }
            string format =
                $"{{0,-{manufacturerLen}}}{{1,-{modelLen}}}{{2,-{serialLen}}}{{3,-{softwareLen}}}{{4,-{versionLen}}}{{5,-{osLen}}}{{6,-{sectorLen}}}{{7,-{sectorLen}}}";

            DicConsole.WriteLine("Dump hardware information:");
            DicConsole.WriteLine(format, MANUFACTURER_STRING, MODEL_STRING, SERIAL_STRING, SOFTWARE_STRING,
                                 VERSION_STRING, OS_STRING, START_STRING, END_STRING);
            DicConsole.WriteLine(new string(separator));
            foreach (DumpHardwareType dump in imageFormat.DumpHardware)
            {
                foreach (ExtentType extent in dump.Extents)
                {
                    DicConsole.WriteLine(format, dump.Manufacturer, dump.Model, dump.Serial, dump.Software.Name,
                                         dump.Software.Version, dump.Software.OperatingSystem, extent.Start,
                                         extent.End);
                }
            }

            DicConsole.WriteLine();
        }
示例#10
0
        public ImageInfoViewModel(string imagePath, IFilter filter, IMediaImage imageFormat, Window view)

        {
            _imagePath   = imagePath;
            _filter      = filter;
            _imageFormat = imageFormat;
            _view        = view;
            IAssetLoader assets = AvaloniaLocator.Current.GetService <IAssetLoader>();

            MediaTagsList         = new ObservableCollection <string>();
            SectorTagsList        = new ObservableCollection <string>();
            Sessions              = new ObservableCollection <Session>();
            Tracks                = new ObservableCollection <Track>();
            DumpHardwareList      = new ObservableCollection <DumpHardwareModel>();
            EntropyCommand        = ReactiveCommand.Create(ExecuteEntropyCommand);
            VerifyCommand         = ReactiveCommand.Create(ExecuteVerifyCommand);
            ChecksumCommand       = ReactiveCommand.Create(ExecuteChecksumCommand);
            ConvertCommand        = ReactiveCommand.Create(ExecuteConvertCommand);
            CreateSidecarCommand  = ReactiveCommand.Create(ExecuteCreateSidecarCommand);
            ViewSectorsCommand    = ReactiveCommand.Create(ExecuteViewSectorsCommand);
            DecodeMediaTagCommand = ReactiveCommand.Create(ExecuteDecodeMediaTagCommand);

            var genericHddIcon =
                new Bitmap(assets.Open(new Uri("avares://Aaru.Gui/Assets/Icons/oxygen/32x32/drive-harddisk.png")));

            var genericOpticalIcon =
                new Bitmap(assets.Open(new Uri("avares://Aaru.Gui/Assets/Icons/oxygen/32x32/drive-optical.png")));

            var genericFolderIcon =
                new Bitmap(assets.Open(new Uri("avares://Aaru.Gui/Assets/Icons/oxygen/32x32/inode-directory.png")));

            var mediaResource = new Uri($"avares://Aaru.Gui/Assets/Logos/Media/{imageFormat.Info.MediaType}.png");

            MediaLogo = assets.Exists(mediaResource)
                            ? new Bitmap(assets.Open(mediaResource))
                            : imageFormat.Info.XmlMediaType == XmlMediaType.BlockMedia
                                ? genericHddIcon
                                : imageFormat.Info.XmlMediaType == XmlMediaType.OpticalDisc
                                    ? genericOpticalIcon
                                    : genericFolderIcon;

            ImagePathText       = $"Path: {imagePath}";
            FilterText          = $"Filter: {filter.Name}";
            ImageIdentifiedText = $"Image format identified by {imageFormat.Name} ({imageFormat.Id}).";

            ImageFormatText = !string.IsNullOrWhiteSpace(imageFormat.Info.Version)
                                  ? $"Format: {imageFormat.Format} version {imageFormat.Info.Version}"
                                  : $"Format: {imageFormat.Format}";

            ImageSizeText = $"Image without headers is {imageFormat.Info.ImageSize} bytes long";

            SectorsText =
                $"Contains a media of {imageFormat.Info.Sectors} sectors with a maximum sector size of {imageFormat.Info.SectorSize} bytes (if all sectors are of the same size this would be {imageFormat.Info.Sectors * imageFormat.Info.SectorSize} bytes)";

            MediaTypeText =
                $"Contains a media of type {imageFormat.Info.MediaType} and XML type {imageFormat.Info.XmlMediaType}";

            HasPartitionsText = $"{(imageFormat.Info.HasPartitions ? "Has" : "Doesn't have")} partitions";
            HasSessionsText   = $"{(imageFormat.Info.HasSessions ? "Has" : "Doesn't have")} sessions";

            if (!string.IsNullOrWhiteSpace(imageFormat.Info.Application))
            {
                ApplicationText = !string.IsNullOrWhiteSpace(imageFormat.Info.ApplicationVersion)
                                      ? $"Was created with {imageFormat.Info.Application} version {imageFormat.Info.ApplicationVersion}"
                                      : $"Was created with {imageFormat.Info.Application}";
            }

            if (!string.IsNullOrWhiteSpace(imageFormat.Info.Creator))
            {
                CreatorText = $"Created by: {imageFormat.Info.Creator}";
            }

            if (imageFormat.Info.CreationTime != DateTime.MinValue)
            {
                CreationTimeText = $"Created on {imageFormat.Info.CreationTime}";
            }

            if (imageFormat.Info.LastModificationTime != DateTime.MinValue)
            {
                LastModificationTimeText = $"Last modified on {imageFormat.Info.LastModificationTime}";
            }

            if (imageFormat.Info.MediaSequence != 0 &&
                imageFormat.Info.LastMediaSequence != 0)
            {
                MediaSequenceText =
                    $"Media is number {imageFormat.Info.MediaSequence} on a set of {imageFormat.Info.LastMediaSequence} medias";
            }

            if (!string.IsNullOrWhiteSpace(imageFormat.Info.MediaTitle))
            {
                MediaTitleText = $"Media title: {imageFormat.Info.MediaTitle}";
            }

            if (!string.IsNullOrWhiteSpace(imageFormat.Info.MediaManufacturer))
            {
                MediaManufacturerText = $"Media manufacturer: {imageFormat.Info.MediaManufacturer}";
            }

            if (!string.IsNullOrWhiteSpace(imageFormat.Info.MediaModel))
            {
                MediaModelText = $"Media model: {imageFormat.Info.MediaModel}";
            }

            if (!string.IsNullOrWhiteSpace(imageFormat.Info.MediaSerialNumber))
            {
                MediaSerialNumberText = $"Media serial number: {imageFormat.Info.MediaSerialNumber}";
            }

            if (!string.IsNullOrWhiteSpace(imageFormat.Info.MediaBarcode))
            {
                MediaBarcodeText = $"Media barcode: {imageFormat.Info.MediaBarcode}";
            }

            if (!string.IsNullOrWhiteSpace(imageFormat.Info.MediaPartNumber))
            {
                MediaPartNumberText = $"Media part number: {imageFormat.Info.MediaPartNumber}";
            }

            if (!string.IsNullOrWhiteSpace(imageFormat.Info.DriveManufacturer))
            {
                DriveManufacturerText = $"Drive manufacturer: {imageFormat.Info.DriveManufacturer}";
            }

            if (!string.IsNullOrWhiteSpace(imageFormat.Info.DriveModel))
            {
                DriveModelText = $"Drive model: {imageFormat.Info.DriveModel}";
            }

            if (!string.IsNullOrWhiteSpace(imageFormat.Info.DriveSerialNumber))
            {
                DriveSerialNumberText = $"Drive serial number: {imageFormat.Info.DriveSerialNumber}";
            }

            if (!string.IsNullOrWhiteSpace(imageFormat.Info.DriveFirmwareRevision))
            {
                DriveFirmwareRevisionText = $"Drive firmware info: {imageFormat.Info.DriveFirmwareRevision}";
            }

            if (imageFormat.Info.Cylinders > 0 &&
                imageFormat.Info.Heads > 0 &&
                imageFormat.Info.SectorsPerTrack > 0 &&
                imageFormat.Info.XmlMediaType != XmlMediaType.OpticalDisc &&
                (!(imageFormat is ITapeImage tapeImage) || !tapeImage.IsTape))
            {
                MediaGeometryText =
                    $"Media geometry: {imageFormat.Info.Cylinders} cylinders, {imageFormat.Info.Heads} heads, {imageFormat.Info.SectorsPerTrack} sectors per track";
            }

            if (imageFormat.Info.ReadableMediaTags != null &&
                imageFormat.Info.ReadableMediaTags.Count > 0)
            {
                foreach (MediaTagType tag in imageFormat.Info.ReadableMediaTags.OrderBy(t => t))
                {
                    MediaTagsList.Add(tag.ToString());
                }
            }

            if (imageFormat.Info.ReadableSectorTags != null &&
                imageFormat.Info.ReadableSectorTags.Count > 0)
            {
                foreach (SectorTagType tag in imageFormat.Info.ReadableSectorTags.OrderBy(t => t))
                {
                    SectorTagsList.Add(tag.ToString());
                }
            }

            PeripheralDeviceTypes scsiDeviceType = PeripheralDeviceTypes.DirectAccess;

            byte[]  scsiInquiryData = null;
            Inquiry?scsiInquiry     = null;

            Modes.DecodedMode?scsiMode        = null;
            byte[]            scsiModeSense6  = null;
            byte[]            scsiModeSense10 = null;

            if (imageFormat.Info.ReadableMediaTags != null &&
                imageFormat.Info.ReadableMediaTags.Contains(MediaTagType.SCSI_INQUIRY))
            {
                scsiInquiryData = imageFormat.ReadDiskTag(MediaTagType.SCSI_INQUIRY);

                scsiDeviceType = (PeripheralDeviceTypes)(scsiInquiryData[0] & 0x1F);

                scsiInquiry = Inquiry.Decode(scsiInquiryData);
            }

            if (imageFormat.Info.ReadableMediaTags != null &&
                imageFormat.Info.ReadableMediaTags.Contains(MediaTagType.SCSI_MODESENSE_6))
            {
                scsiModeSense6 = imageFormat.ReadDiskTag(MediaTagType.SCSI_MODESENSE_6);
                scsiMode       = Modes.DecodeMode6(scsiModeSense6, scsiDeviceType);
            }

            if (imageFormat.Info.ReadableMediaTags != null &&
                imageFormat.Info.ReadableMediaTags.Contains(MediaTagType.SCSI_MODESENSE_10))
            {
                scsiModeSense10 = imageFormat.ReadDiskTag(MediaTagType.SCSI_MODESENSE_10);
                scsiMode        = Modes.DecodeMode10(scsiModeSense10, scsiDeviceType);
            }

            ScsiInfo = new ScsiInfo
            {
                DataContext = new ScsiInfoViewModel(scsiInquiryData, scsiInquiry, null, scsiMode, scsiDeviceType,
                                                    scsiModeSense6, scsiModeSense10, null, _view)
            };

            byte[] ataIdentify   = null;
            byte[] atapiIdentify = null;

            if (imageFormat.Info.ReadableMediaTags != null &&
                imageFormat.Info.ReadableMediaTags.Contains(MediaTagType.ATA_IDENTIFY))
            {
                ataIdentify = imageFormat.ReadDiskTag(MediaTagType.ATA_IDENTIFY);
            }

            if (imageFormat.Info.ReadableMediaTags != null &&
                imageFormat.Info.ReadableMediaTags.Contains(MediaTagType.ATAPI_IDENTIFY))
            {
                atapiIdentify = imageFormat.ReadDiskTag(MediaTagType.ATAPI_IDENTIFY);
            }

            AtaInfo = new AtaInfo
            {
                DataContext = new AtaInfoViewModel(ataIdentify, atapiIdentify, null, _view)
            };

            byte[]                toc                  = null;
            TOC.CDTOC?            decodedToc           = null;
            byte[]                fullToc              = null;
            FullTOC.CDFullTOC?    decodedFullToc       = null;
            byte[]                pma                  = null;
            byte[]                atip                 = null;
            ATIP.CDATIP?          decodedAtip          = null;
            byte[]                cdtext               = null;
            CDTextOnLeadIn.CDText?decodedCdText        = null;
            string                mediaCatalogueNumber = null;

            if (imageFormat.Info.ReadableMediaTags != null &&
                imageFormat.Info.ReadableMediaTags.Contains(MediaTagType.CD_TOC))
            {
                toc = imageFormat.ReadDiskTag(MediaTagType.CD_TOC);

                if (toc.Length > 0)
                {
                    ushort dataLen = Swapping.Swap(BitConverter.ToUInt16(toc, 0));

                    if (dataLen + 2 != toc.Length)
                    {
                        byte[] tmp = new byte[toc.Length + 2];
                        Array.Copy(toc, 0, tmp, 2, toc.Length);
                        tmp[0] = (byte)((toc.Length & 0xFF00) >> 8);
                        tmp[1] = (byte)(toc.Length & 0xFF);
                        toc    = tmp;
                    }

                    decodedToc = TOC.Decode(toc);
                }
            }

            if (imageFormat.Info.ReadableMediaTags != null &&
                imageFormat.Info.ReadableMediaTags.Contains(MediaTagType.CD_FullTOC))
            {
                fullToc = imageFormat.ReadDiskTag(MediaTagType.CD_FullTOC);

                if (fullToc.Length > 0)
                {
                    ushort dataLen = Swapping.Swap(BitConverter.ToUInt16(fullToc, 0));

                    if (dataLen + 2 != fullToc.Length)
                    {
                        byte[] tmp = new byte[fullToc.Length + 2];
                        Array.Copy(fullToc, 0, tmp, 2, fullToc.Length);
                        tmp[0]  = (byte)((fullToc.Length & 0xFF00) >> 8);
                        tmp[1]  = (byte)(fullToc.Length & 0xFF);
                        fullToc = tmp;
                    }

                    decodedFullToc = FullTOC.Decode(fullToc);
                }
            }

            if (imageFormat.Info.ReadableMediaTags != null &&
                imageFormat.Info.ReadableMediaTags.Contains(MediaTagType.CD_PMA))
            {
                pma = imageFormat.ReadDiskTag(MediaTagType.CD_PMA);

                if (pma.Length > 0)
                {
                    ushort dataLen = Swapping.Swap(BitConverter.ToUInt16(pma, 0));

                    if (dataLen + 2 != pma.Length)
                    {
                        byte[] tmp = new byte[pma.Length + 2];
                        Array.Copy(pma, 0, tmp, 2, pma.Length);
                        tmp[0] = (byte)((pma.Length & 0xFF00) >> 8);
                        tmp[1] = (byte)(pma.Length & 0xFF);
                        pma    = tmp;
                    }
                }
            }

            if (imageFormat.Info.ReadableMediaTags != null &&
                imageFormat.Info.ReadableMediaTags.Contains(MediaTagType.CD_ATIP))
            {
                atip = imageFormat.ReadDiskTag(MediaTagType.CD_ATIP);

                uint dataLen = Swapping.Swap(BitConverter.ToUInt32(atip, 0));

                if (dataLen + 4 != atip.Length)
                {
                    byte[] tmp = new byte[atip.Length + 4];
                    Array.Copy(atip, 0, tmp, 4, atip.Length);
                    tmp[0] = (byte)((atip.Length & 0xFF000000) >> 24);
                    tmp[1] = (byte)((atip.Length & 0xFF0000) >> 16);
                    tmp[2] = (byte)((atip.Length & 0xFF00) >> 8);
                    tmp[3] = (byte)(atip.Length & 0xFF);
                    atip   = tmp;
                }

                decodedAtip = ATIP.Decode(atip);
            }

            if (imageFormat.Info.ReadableMediaTags != null &&
                imageFormat.Info.ReadableMediaTags.Contains(MediaTagType.CD_TEXT))
            {
                cdtext = imageFormat.ReadDiskTag(MediaTagType.CD_TEXT);

                uint dataLen = Swapping.Swap(BitConverter.ToUInt32(cdtext, 0));

                if (dataLen + 4 != cdtext.Length)
                {
                    byte[] tmp = new byte[cdtext.Length + 4];
                    Array.Copy(cdtext, 0, tmp, 4, cdtext.Length);
                    tmp[0] = (byte)((cdtext.Length & 0xFF000000) >> 24);
                    tmp[1] = (byte)((cdtext.Length & 0xFF0000) >> 16);
                    tmp[2] = (byte)((cdtext.Length & 0xFF00) >> 8);
                    tmp[3] = (byte)(cdtext.Length & 0xFF);
                    cdtext = tmp;
                }

                decodedCdText = CDTextOnLeadIn.Decode(cdtext);
            }

            if (imageFormat.Info.ReadableMediaTags != null &&
                imageFormat.Info.ReadableMediaTags.Contains(MediaTagType.CD_MCN))
            {
                byte[] mcn = imageFormat.ReadDiskTag(MediaTagType.CD_MCN);

                mediaCatalogueNumber = Encoding.UTF8.GetString(mcn);
            }

            CompactDiscInfo = new CompactDiscInfo
            {
                DataContext = new CompactDiscInfoViewModel(toc, atip, null, null, fullToc, pma, cdtext, decodedToc,
                                                           decodedAtip, null, decodedFullToc, decodedCdText, null,
                                                           mediaCatalogueNumber, null, _view)
            };

            byte[] dvdPfi = null;
            byte[] dvdDmi = null;
            byte[] dvdCmi = null;
            byte[] hddvdCopyrightInformation = null;
            byte[] dvdBca = null;
            PFI.PhysicalFormatInformation?decodedPfi = null;

            if (imageFormat.Info.ReadableMediaTags != null &&
                imageFormat.Info.ReadableMediaTags.Contains(MediaTagType.DVD_PFI))
            {
                dvdPfi     = imageFormat.ReadDiskTag(MediaTagType.DVD_PFI);
                decodedPfi = PFI.Decode(dvdPfi);
            }

            if (imageFormat.Info.ReadableMediaTags != null &&
                imageFormat.Info.ReadableMediaTags.Contains(MediaTagType.DVD_DMI))
            {
                dvdDmi = imageFormat.ReadDiskTag(MediaTagType.DVD_DMI);
            }

            if (imageFormat.Info.ReadableMediaTags != null &&
                imageFormat.Info.ReadableMediaTags.Contains(MediaTagType.DVD_CMI))
            {
                dvdCmi = imageFormat.ReadDiskTag(MediaTagType.DVD_CMI);
            }

            if (imageFormat.Info.ReadableMediaTags != null &&
                imageFormat.Info.ReadableMediaTags.Contains(MediaTagType.HDDVD_CPI))
            {
                hddvdCopyrightInformation = imageFormat.ReadDiskTag(MediaTagType.HDDVD_CPI);
            }

            if (imageFormat.Info.ReadableMediaTags != null &&
                imageFormat.Info.ReadableMediaTags.Contains(MediaTagType.DVD_BCA))
            {
                dvdBca = imageFormat.ReadDiskTag(MediaTagType.DVD_BCA);
            }

            DvdInfo = new DvdInfo
            {
                DataContext = new DvdInfoViewModel(imageFormat.Info.MediaType, dvdPfi, dvdDmi, dvdCmi,
                                                   hddvdCopyrightInformation, dvdBca, null, decodedPfi, _view)
            };

            byte[] dvdRamDds                     = null;
            byte[] dvdRamCartridgeStatus         = null;
            byte[] dvdRamSpareArea               = null;
            byte[] lastBorderOutRmd              = null;
            byte[] dvdPreRecordedInfo            = null;
            byte[] dvdrMediaIdentifier           = null;
            byte[] dvdrPhysicalInformation       = null;
            byte[] hddvdrMediumStatus            = null;
            byte[] dvdrLayerCapacity             = null;
            byte[] dvdrDlMiddleZoneStart         = null;
            byte[] dvdrDlJumpIntervalSize        = null;
            byte[] dvdrDlManualLayerJumpStartLba = null;
            byte[] dvdPlusAdip                   = null;
            byte[] dvdPlusDcb                    = null;

            if (imageFormat.Info.ReadableMediaTags != null &&
                imageFormat.Info.ReadableMediaTags.Contains(MediaTagType.DVDRAM_DDS))
            {
                dvdRamDds = imageFormat.ReadDiskTag(MediaTagType.DVDRAM_DDS);
            }

            if (imageFormat.Info.ReadableMediaTags != null &&
                imageFormat.Info.ReadableMediaTags.Contains(MediaTagType.DVDRAM_MediumStatus))
            {
                dvdRamCartridgeStatus = imageFormat.ReadDiskTag(MediaTagType.DVDRAM_MediumStatus);
            }

            if (imageFormat.Info.ReadableMediaTags != null &&
                imageFormat.Info.ReadableMediaTags.Contains(MediaTagType.DVDRAM_SpareArea))
            {
                dvdRamSpareArea = imageFormat.ReadDiskTag(MediaTagType.DVDRAM_SpareArea);
            }

            if (imageFormat.Info.ReadableMediaTags != null &&
                imageFormat.Info.ReadableMediaTags.Contains(MediaTagType.DVDR_RMD))
            {
                lastBorderOutRmd = imageFormat.ReadDiskTag(MediaTagType.DVDR_RMD);
            }

            if (imageFormat.Info.ReadableMediaTags != null &&
                imageFormat.Info.ReadableMediaTags.Contains(MediaTagType.DVDR_PreRecordedInfo))
            {
                dvdPreRecordedInfo = imageFormat.ReadDiskTag(MediaTagType.DVDR_PreRecordedInfo);
            }

            if (imageFormat.Info.ReadableMediaTags != null &&
                imageFormat.Info.ReadableMediaTags.Contains(MediaTagType.DVDR_MediaIdentifier))
            {
                dvdrMediaIdentifier = imageFormat.ReadDiskTag(MediaTagType.DVDR_MediaIdentifier);
            }

            if (imageFormat.Info.ReadableMediaTags != null &&
                imageFormat.Info.ReadableMediaTags.Contains(MediaTagType.DVDR_PFI))
            {
                dvdrPhysicalInformation = imageFormat.ReadDiskTag(MediaTagType.DVDR_PFI);
            }

            if (imageFormat.Info.ReadableMediaTags != null &&
                imageFormat.Info.ReadableMediaTags.Contains(MediaTagType.HDDVD_MediumStatus))
            {
                hddvdrMediumStatus = imageFormat.ReadDiskTag(MediaTagType.HDDVD_MediumStatus);
            }

            if (imageFormat.Info.ReadableMediaTags != null &&
                imageFormat.Info.ReadableMediaTags.Contains(MediaTagType.DVDDL_LayerCapacity))
            {
                dvdrLayerCapacity = imageFormat.ReadDiskTag(MediaTagType.DVDDL_LayerCapacity);
            }

            if (imageFormat.Info.ReadableMediaTags != null &&
                imageFormat.Info.ReadableMediaTags.Contains(MediaTagType.DVDDL_MiddleZoneAddress))
            {
                dvdrDlMiddleZoneStart = imageFormat.ReadDiskTag(MediaTagType.DVDDL_MiddleZoneAddress);
            }

            if (imageFormat.Info.ReadableMediaTags != null &&
                imageFormat.Info.ReadableMediaTags.Contains(MediaTagType.DVDDL_JumpIntervalSize))
            {
                dvdrDlJumpIntervalSize = imageFormat.ReadDiskTag(MediaTagType.DVDDL_JumpIntervalSize);
            }

            if (imageFormat.Info.ReadableMediaTags != null &&
                imageFormat.Info.ReadableMediaTags.Contains(MediaTagType.DVDDL_ManualLayerJumpLBA))
            {
                dvdrDlManualLayerJumpStartLba = imageFormat.ReadDiskTag(MediaTagType.DVDDL_ManualLayerJumpLBA);
            }

            if (imageFormat.Info.ReadableMediaTags != null &&
                imageFormat.Info.ReadableMediaTags.Contains(MediaTagType.DVD_ADIP))
            {
                dvdPlusAdip = imageFormat.ReadDiskTag(MediaTagType.DVD_ADIP);
            }

            if (imageFormat.Info.ReadableMediaTags != null &&
                imageFormat.Info.ReadableMediaTags.Contains(MediaTagType.DCB))
            {
                dvdPlusDcb = imageFormat.ReadDiskTag(MediaTagType.DCB);
            }

            DvdWritableInfo = new DvdWritableInfo
            {
                DataContext = new DvdWritableInfoViewModel(imageFormat.Info.MediaType, dvdRamDds, dvdRamCartridgeStatus,
                                                           dvdRamSpareArea, lastBorderOutRmd, dvdPreRecordedInfo,
                                                           dvdrMediaIdentifier, dvdrPhysicalInformation,
                                                           hddvdrMediumStatus, null, dvdrLayerCapacity,
                                                           dvdrDlMiddleZoneStart, dvdrDlJumpIntervalSize,
                                                           dvdrDlManualLayerJumpStartLba, null, dvdPlusAdip, dvdPlusDcb,
                                                           _view)
            };

            byte[] blurayBurstCuttingArea = null;
            byte[] blurayCartridgeStatus  = null;
            byte[] blurayDds                  = null;
            byte[] blurayDiscInformation      = null;
            byte[] blurayPowResources         = null;
            byte[] bluraySpareAreaInformation = null;
            byte[] blurayTrackResources       = null;

            if (imageFormat.Info.ReadableMediaTags != null &&
                imageFormat.Info.ReadableMediaTags.Contains(MediaTagType.BD_BCA))
            {
                blurayBurstCuttingArea = imageFormat.ReadDiskTag(MediaTagType.BD_BCA);
            }

            if (imageFormat.Info.ReadableMediaTags != null &&
                imageFormat.Info.ReadableMediaTags.Contains(MediaTagType.BD_CartridgeStatus))
            {
                blurayCartridgeStatus = imageFormat.ReadDiskTag(MediaTagType.BD_CartridgeStatus);
            }

            if (imageFormat.Info.ReadableMediaTags != null &&
                imageFormat.Info.ReadableMediaTags.Contains(MediaTagType.BD_DDS))
            {
                blurayDds = imageFormat.ReadDiskTag(MediaTagType.BD_DDS);
            }

            if (imageFormat.Info.ReadableMediaTags != null &&
                imageFormat.Info.ReadableMediaTags.Contains(MediaTagType.BD_DI))
            {
                blurayDiscInformation = imageFormat.ReadDiskTag(MediaTagType.BD_DI);
            }

            if (imageFormat.Info.ReadableMediaTags != null &&
                imageFormat.Info.ReadableMediaTags.Contains(MediaTagType.MMC_POWResourcesInformation))
            {
                blurayPowResources = imageFormat.ReadDiskTag(MediaTagType.MMC_POWResourcesInformation);
            }

            if (imageFormat.Info.ReadableMediaTags != null &&
                imageFormat.Info.ReadableMediaTags.Contains(MediaTagType.BD_SpareArea))
            {
                bluraySpareAreaInformation = imageFormat.ReadDiskTag(MediaTagType.BD_SpareArea);
            }

            if (imageFormat.Info.ReadableMediaTags != null &&
                imageFormat.Info.ReadableMediaTags.Contains(MediaTagType.MMC_TrackResourcesInformation))
            {
                bluraySpareAreaInformation = imageFormat.ReadDiskTag(MediaTagType.MMC_TrackResourcesInformation);
            }

            BlurayInfo = new BlurayInfo
            {
                DataContext = new BlurayInfoViewModel(blurayDiscInformation, blurayBurstCuttingArea, blurayDds,
                                                      blurayCartridgeStatus, bluraySpareAreaInformation,
                                                      blurayPowResources, blurayTrackResources, null, null, _view)
            };

            byte[]            xboxDmi                   = null;
            byte[]            xboxSecuritySector        = null;
            SS.SecuritySector?decodedXboxSecuritySector = null;

            if (imageFormat.Info.ReadableMediaTags != null &&
                imageFormat.Info.ReadableMediaTags.Contains(MediaTagType.Xbox_DMI))
            {
                xboxDmi = imageFormat.ReadDiskTag(MediaTagType.Xbox_DMI);
            }

            if (imageFormat.Info.ReadableMediaTags != null &&
                imageFormat.Info.ReadableMediaTags.Contains(MediaTagType.Xbox_SecuritySector))
            {
                xboxSecuritySector        = imageFormat.ReadDiskTag(MediaTagType.Xbox_SecuritySector);
                decodedXboxSecuritySector = SS.Decode(xboxSecuritySector);
            }

            XboxInfo = new XboxInfo
            {
                DataContext = new XboxInfoViewModel(null, xboxDmi, xboxSecuritySector, decodedXboxSecuritySector, _view)
            };

            byte[] pcmciaCis = null;

            if (imageFormat.Info.ReadableMediaTags != null &&
                imageFormat.Info.ReadableMediaTags.Contains(MediaTagType.PCMCIA_CIS))
            {
                pcmciaCis = imageFormat.ReadDiskTag(MediaTagType.PCMCIA_CIS);
            }

            PcmciaInfo = new PcmciaInfo
            {
                DataContext = new PcmciaInfoViewModel(pcmciaCis, _view)
            };

            DeviceType deviceType = DeviceType.Unknown;

            byte[] cid         = null;
            byte[] csd         = null;
            byte[] ocr         = null;
            byte[] extendedCsd = null;
            byte[] scr         = null;

            if (imageFormat.Info.ReadableMediaTags != null &&
                imageFormat.Info.ReadableMediaTags.Contains(MediaTagType.SD_CID))
            {
                cid        = imageFormat.ReadDiskTag(MediaTagType.SD_CID);
                deviceType = DeviceType.SecureDigital;
            }

            if (imageFormat.Info.ReadableMediaTags != null &&
                imageFormat.Info.ReadableMediaTags.Contains(MediaTagType.SD_CSD))
            {
                csd        = imageFormat.ReadDiskTag(MediaTagType.SD_CSD);
                deviceType = DeviceType.SecureDigital;
            }

            if (imageFormat.Info.ReadableMediaTags != null &&
                imageFormat.Info.ReadableMediaTags.Contains(MediaTagType.SD_OCR))
            {
                ocr        = imageFormat.ReadDiskTag(MediaTagType.SD_OCR);
                deviceType = DeviceType.SecureDigital;
            }

            if (imageFormat.Info.ReadableMediaTags != null &&
                imageFormat.Info.ReadableMediaTags.Contains(MediaTagType.SD_SCR))
            {
                scr        = imageFormat.ReadDiskTag(MediaTagType.SD_SCR);
                deviceType = DeviceType.SecureDigital;
            }

            if (imageFormat.Info.ReadableMediaTags != null &&
                imageFormat.Info.ReadableMediaTags.Contains(MediaTagType.MMC_CID))
            {
                cid        = imageFormat.ReadDiskTag(MediaTagType.MMC_CID);
                deviceType = DeviceType.MMC;
            }

            if (imageFormat.Info.ReadableMediaTags != null &&
                imageFormat.Info.ReadableMediaTags.Contains(MediaTagType.MMC_CSD))
            {
                csd        = imageFormat.ReadDiskTag(MediaTagType.MMC_CSD);
                deviceType = DeviceType.MMC;
            }

            if (imageFormat.Info.ReadableMediaTags != null &&
                imageFormat.Info.ReadableMediaTags.Contains(MediaTagType.MMC_OCR))
            {
                ocr        = imageFormat.ReadDiskTag(MediaTagType.MMC_OCR);
                deviceType = DeviceType.MMC;
            }

            if (imageFormat.Info.ReadableMediaTags != null &&
                imageFormat.Info.ReadableMediaTags.Contains(MediaTagType.MMC_ExtendedCSD))
            {
                extendedCsd = imageFormat.ReadDiskTag(MediaTagType.MMC_ExtendedCSD);
                deviceType  = DeviceType.MMC;
            }

            SdMmcInfo = new SdMmcInfo
            {
                DataContext = new SdMmcInfoViewModel(deviceType, cid, csd, ocr, extendedCsd, scr)
            };

            if (imageFormat is IOpticalMediaImage opticalMediaImage)
            {
                try
                {
                    if (opticalMediaImage.Sessions != null &&
                        opticalMediaImage.Sessions.Count > 0)
                    {
                        foreach (Session session in opticalMediaImage.Sessions)
                        {
                            Sessions.Add(session);
                        }
                    }
                }
                catch
                {
                    // ignored
                }

                try
                {
                    if (opticalMediaImage.Tracks != null &&
                        opticalMediaImage.Tracks.Count > 0)
                    {
                        foreach (Track track in opticalMediaImage.Tracks)
                        {
                            Tracks.Add(track);
                        }
                    }
                }
                catch
                {
                    // ignored
                }
            }

            if (imageFormat.DumpHardware is null)
            {
                return;
            }

            foreach (DumpHardwareType dump in imageFormat.DumpHardware)
            {
                foreach (ExtentType extent in dump.Extents)
                {
                    DumpHardwareList.Add(new DumpHardwareModel
                    {
                        Manufacturer    = dump.Manufacturer, Model = dump.Model, Serial = dump.Serial,
                        SoftwareName    = dump.Software.Name, SoftwareVersion = dump.Software.Version,
                        OperatingSystem = dump.Software.OperatingSystem, Start = extent.Start, End = extent.End
                    });
                }
            }
        }
示例#11
0
        /// <summary>
        ///     Creates a metadata sidecar for a block media (e.g. floppy, hard disk, flash card, usb stick)
        /// </summary>
        /// <param name="image">Image</param>
        /// <param name="filterId">Filter uuid</param>
        /// <param name="imagePath">Image path</param>
        /// <param name="fi">Image file information</param>
        /// <param name="plugins">Image plugins</param>
        /// <param name="imgChecksums">List of image checksums</param>
        /// <param name="sidecar">Metadata sidecar</param>
        static void BlockMedia(IMediaImage image, Guid filterId, string imagePath,
                               FileInfo fi, PluginBase plugins,
                               List <ChecksumType> imgChecksums, ref CICMMetadataType sidecar, Encoding encoding)
        {
            sidecar.BlockMedia = new[]
            {
                new BlockMediaType
                {
                    Checksums = imgChecksums.ToArray(),
                    Image     = new ImageType
                    {
                        format          = image.Format,
                        offset          = 0,
                        offsetSpecified = true,
                        Value           = Path.GetFileName(imagePath)
                    },
                    Size     = fi.Length,
                    Sequence = new SequenceType {
                        MediaTitle = image.Info.MediaTitle
                    }
                }
            };

            if (image.Info.MediaSequence != 0 && image.Info.LastMediaSequence != 0)
            {
                sidecar.BlockMedia[0].Sequence.MediaSequence = image.Info.MediaSequence;
                sidecar.BlockMedia[0].Sequence.TotalMedia    = image.Info.LastMediaSequence;
            }
            else
            {
                sidecar.BlockMedia[0].Sequence.MediaSequence = 1;
                sidecar.BlockMedia[0].Sequence.TotalMedia    = 1;
            }

            foreach (MediaTagType tagType in image.Info.ReadableMediaTags)
            {
                switch (tagType)
                {
                case MediaTagType.ATAPI_IDENTIFY:
                    sidecar.BlockMedia[0].ATA = new ATAType
                    {
                        Identify = new DumpType
                        {
                            Checksums =
                                Checksum.GetChecksums(image.ReadDiskTag(MediaTagType.ATAPI_IDENTIFY)).ToArray(),
                            Size = image.ReadDiskTag(MediaTagType.ATAPI_IDENTIFY).Length
                        }
                    };
                    break;

                case MediaTagType.ATA_IDENTIFY:
                    sidecar.BlockMedia[0].ATA = new ATAType
                    {
                        Identify = new DumpType
                        {
                            Checksums =
                                Checksum.GetChecksums(image.ReadDiskTag(MediaTagType.ATA_IDENTIFY)).ToArray(),
                            Size = image.ReadDiskTag(MediaTagType.ATA_IDENTIFY).Length
                        }
                    };
                    break;

                case MediaTagType.PCMCIA_CIS:
                    byte[] cis = image.ReadDiskTag(MediaTagType.PCMCIA_CIS);
                    sidecar.BlockMedia[0].PCMCIA = new PCMCIAType
                    {
                        CIS = new DumpType {
                            Checksums = Checksum.GetChecksums(cis).ToArray(), Size = cis.Length
                        }
                    };
                    Tuple[] tuples = CIS.GetTuples(cis);
                    if (tuples != null)
                    {
                        foreach (Tuple tuple in tuples)
                        {
                            switch (tuple.Code)
                            {
                            case TupleCodes.CISTPL_MANFID:
                                ManufacturerIdentificationTuple manfid =
                                    CIS.DecodeManufacturerIdentificationTuple(tuple);

                                if (manfid != null)
                                {
                                    sidecar.BlockMedia[0].PCMCIA.ManufacturerCode =
                                        manfid.ManufacturerID;
                                    sidecar.BlockMedia[0].PCMCIA.CardCode = manfid.CardID;
                                    sidecar.BlockMedia[0].PCMCIA.ManufacturerCodeSpecified = true;
                                    sidecar.BlockMedia[0].PCMCIA.CardCodeSpecified         = true;
                                }

                                break;

                            case TupleCodes.CISTPL_VERS_1:
                                Level1VersionTuple vers = CIS.DecodeLevel1VersionTuple(tuple);

                                if (vers != null)
                                {
                                    sidecar.BlockMedia[0].PCMCIA.Manufacturer = vers.Manufacturer;
                                    sidecar.BlockMedia[0].PCMCIA.ProductName  = vers.Product;
                                    sidecar.BlockMedia[0].PCMCIA.Compliance   =
                                        $"{vers.MajorVersion}.{vers.MinorVersion}";
                                    sidecar.BlockMedia[0].PCMCIA.AdditionalInformation =
                                        vers.AdditionalInformation;
                                }

                                break;
                            }
                        }
                    }

                    break;

                case MediaTagType.SCSI_INQUIRY:
                    sidecar.BlockMedia[0].SCSI = new SCSIType
                    {
                        Inquiry = new DumpType
                        {
                            Checksums =
                                Checksum.GetChecksums(image.ReadDiskTag(MediaTagType.SCSI_INQUIRY)).ToArray(),
                            Size = image.ReadDiskTag(MediaTagType.SCSI_INQUIRY).Length
                        }
                    };
                    break;

                case MediaTagType.SD_CID:
                    if (sidecar.BlockMedia[0].SecureDigital == null)
                    {
                        sidecar.BlockMedia[0].SecureDigital = new SecureDigitalType();
                    }
                    sidecar.BlockMedia[0].SecureDigital.CID = new DumpType
                    {
                        Checksums = Checksum.GetChecksums(image.ReadDiskTag(MediaTagType.SD_CID)).ToArray(),
                        Size      = image.ReadDiskTag(MediaTagType.SD_CID).Length
                    };
                    break;

                case MediaTagType.SD_CSD:
                    if (sidecar.BlockMedia[0].SecureDigital == null)
                    {
                        sidecar.BlockMedia[0].SecureDigital = new SecureDigitalType();
                    }
                    sidecar.BlockMedia[0].SecureDigital.CSD = new DumpType
                    {
                        Checksums = Checksum.GetChecksums(image.ReadDiskTag(MediaTagType.SD_CSD)).ToArray(),
                        Size      = image.ReadDiskTag(MediaTagType.SD_CSD).Length
                    };
                    break;

                case MediaTagType.SD_SCR:
                    if (sidecar.BlockMedia[0].SecureDigital == null)
                    {
                        sidecar.BlockMedia[0].SecureDigital = new SecureDigitalType();
                    }
                    sidecar.BlockMedia[0].SecureDigital.SCR = new DumpType
                    {
                        Checksums = Checksum.GetChecksums(image.ReadDiskTag(MediaTagType.SD_SCR)).ToArray(),
                        Size      = image.ReadDiskTag(MediaTagType.SD_SCR).Length
                    };
                    break;

                case MediaTagType.SD_OCR:
                    if (sidecar.BlockMedia[0].SecureDigital == null)
                    {
                        sidecar.BlockMedia[0].SecureDigital = new SecureDigitalType();
                    }
                    sidecar.BlockMedia[0].SecureDigital.OCR = new DumpType
                    {
                        Checksums = Checksum.GetChecksums(image.ReadDiskTag(MediaTagType.SD_OCR)).ToArray(),
                        Size      = image.ReadDiskTag(MediaTagType.SD_OCR).Length
                    };
                    break;

                case MediaTagType.MMC_CID:
                    if (sidecar.BlockMedia[0].MultiMediaCard == null)
                    {
                        sidecar.BlockMedia[0].MultiMediaCard = new MultiMediaCardType();
                    }
                    sidecar.BlockMedia[0].MultiMediaCard.CID = new DumpType
                    {
                        Checksums = Checksum.GetChecksums(image.ReadDiskTag(MediaTagType.SD_CID)).ToArray(),
                        Size      = image.ReadDiskTag(MediaTagType.SD_CID).Length
                    };
                    break;

                case MediaTagType.MMC_CSD:
                    if (sidecar.BlockMedia[0].MultiMediaCard == null)
                    {
                        sidecar.BlockMedia[0].MultiMediaCard = new MultiMediaCardType();
                    }
                    sidecar.BlockMedia[0].MultiMediaCard.CSD = new DumpType
                    {
                        Checksums = Checksum.GetChecksums(image.ReadDiskTag(MediaTagType.SD_CSD)).ToArray(),
                        Size      = image.ReadDiskTag(MediaTagType.SD_CSD).Length
                    };
                    break;

                case MediaTagType.MMC_OCR:
                    if (sidecar.BlockMedia[0].MultiMediaCard == null)
                    {
                        sidecar.BlockMedia[0].MultiMediaCard = new MultiMediaCardType();
                    }
                    sidecar.BlockMedia[0].MultiMediaCard.OCR = new DumpType
                    {
                        Checksums = Checksum.GetChecksums(image.ReadDiskTag(MediaTagType.SD_OCR)).ToArray(),
                        Size      = image.ReadDiskTag(MediaTagType.SD_OCR).Length
                    };
                    break;

                case MediaTagType.MMC_ExtendedCSD:
                    if (sidecar.BlockMedia[0].MultiMediaCard == null)
                    {
                        sidecar.BlockMedia[0].MultiMediaCard = new MultiMediaCardType();
                    }
                    sidecar.BlockMedia[0].MultiMediaCard.ExtendedCSD = new DumpType
                    {
                        Checksums =
                            Checksum.GetChecksums(image.ReadDiskTag(MediaTagType.MMC_ExtendedCSD)).ToArray(),
                        Size = image.ReadDiskTag(MediaTagType.MMC_ExtendedCSD).Length
                    };
                    break;

                case MediaTagType.USB_Descriptors:
                    if (sidecar.BlockMedia[0].USB == null)
                    {
                        sidecar.BlockMedia[0].USB = new USBType();
                    }
                    sidecar.BlockMedia[0].USB.Descriptors = new DumpType
                    {
                        Checksums =
                            Checksum.GetChecksums(image.ReadDiskTag(MediaTagType.USB_Descriptors)).ToArray(),
                        Size = image.ReadDiskTag(MediaTagType.USB_Descriptors).Length
                    };
                    break;

                case MediaTagType.SCSI_MODESENSE_6:
                    if (sidecar.BlockMedia[0].SCSI == null)
                    {
                        sidecar.BlockMedia[0].SCSI = new SCSIType();
                    }
                    sidecar.BlockMedia[0].SCSI.ModeSense = new DumpType
                    {
                        Checksums =
                            Checksum.GetChecksums(image.ReadDiskTag(MediaTagType.SCSI_MODESENSE_6)).ToArray(),
                        Size = image.ReadDiskTag(MediaTagType.SCSI_MODESENSE_6).Length
                    };
                    break;

                case MediaTagType.SCSI_MODESENSE_10:
                    if (sidecar.BlockMedia[0].SCSI == null)
                    {
                        sidecar.BlockMedia[0].SCSI = new SCSIType();
                    }
                    sidecar.BlockMedia[0].SCSI.ModeSense10 = new DumpType
                    {
                        Checksums =
                            Checksum.GetChecksums(image.ReadDiskTag(MediaTagType.SCSI_MODESENSE_10)).ToArray(),
                        Size = image.ReadDiskTag(MediaTagType.SCSI_MODESENSE_10).Length
                    };
                    break;
                }
            }

            // If there is only one track, and it's the same as the image file (e.g. ".iso" files), don't re-checksum.
            if (image.Id == new Guid("12345678-AAAA-BBBB-CCCC-123456789000") &&
                filterId == new Guid("12345678-AAAA-BBBB-CCCC-123456789000"))
            {
                sidecar.BlockMedia[0].ContentChecksums = sidecar.BlockMedia[0].Checksums;
            }
            else
            {
                Checksum contentChkWorker = new Checksum();

                // For fast debugging, skip checksum
                //goto skipImageChecksum;

                uint  sectorsToRead = 512;
                ulong sectors       = image.Info.Sectors;
                ulong doneSectors   = 0;

                InitProgress2();
                while (doneSectors < sectors)
                {
                    byte[] sector;

                    if (sectors - doneSectors >= sectorsToRead)
                    {
                        sector = image.ReadSectors(doneSectors, sectorsToRead);
                        UpdateProgress2("Hashings sector {0} of {1}", (long)doneSectors, (long)sectors);
                        doneSectors += sectorsToRead;
                    }
                    else
                    {
                        sector = image.ReadSectors(doneSectors, (uint)(sectors - doneSectors));
                        UpdateProgress2("Hashings sector {0} of {1}", (long)doneSectors, (long)sectors);
                        doneSectors += sectors - doneSectors;
                    }

                    contentChkWorker.Update(sector);
                }

                // For fast debugging, skip checksum
                //skipImageChecksum:

                List <ChecksumType> cntChecksums = contentChkWorker.End();

                sidecar.BlockMedia[0].ContentChecksums = cntChecksums.ToArray();

                EndProgress2();
            }

            MediaType.MediaTypeToString(image.Info.MediaType, out string dskType, out string dskSubType);
            sidecar.BlockMedia[0].DiskType    = dskType;
            sidecar.BlockMedia[0].DiskSubType = dskSubType;
            Statistics.AddMedia(image.Info.MediaType, false);

            sidecar.BlockMedia[0].Dimensions = Dimensions.DimensionsFromMediaType(image.Info.MediaType);

            sidecar.BlockMedia[0].LogicalBlocks    = (long)image.Info.Sectors;
            sidecar.BlockMedia[0].LogicalBlockSize = (int)image.Info.SectorSize;
            // TODO: Detect it
            sidecar.BlockMedia[0].PhysicalBlockSize = (int)image.Info.SectorSize;

            UpdateStatus("Checking filesystems...");

            List <Partition> partitions = Partitions.GetAll(image);

            Partitions.AddSchemesToStats(partitions);

            sidecar.BlockMedia[0].FileSystemInformation = new PartitionType[1];
            if (partitions.Count > 0)
            {
                sidecar.BlockMedia[0].FileSystemInformation = new PartitionType[partitions.Count];
                for (int i = 0; i < partitions.Count; i++)
                {
                    sidecar.BlockMedia[0].FileSystemInformation[i] = new PartitionType
                    {
                        Description = partitions[i].Description,
                        EndSector   = (int)partitions[i].End,
                        Name        = partitions[i].Name,
                        Sequence    = (int)partitions[i].Sequence,
                        StartSector = (int)partitions[i].Start,
                        Type        = partitions[i].Type
                    };
                    List <FileSystemType> lstFs = new List <FileSystemType>();

                    foreach (IFilesystem plugin in plugins.PluginsList.Values)
                    {
                        try
                        {
                            if (!plugin.Identify(image, partitions[i]))
                            {
                                continue;
                            }

                            plugin.GetInformation(image, partitions[i], out _, encoding);
                            lstFs.Add(plugin.XmlFsType);
                            Statistics.AddFilesystem(plugin.XmlFsType.Type);
                        }
                    }
示例#12
0
        internal static void DoCompare(CompareOptions options)
        {
            DicConsole.DebugWriteLine("Compare command", "--debug={0}", options.Debug);
            DicConsole.DebugWriteLine("Compare command", "--verbose={0}", options.Verbose);
            DicConsole.DebugWriteLine("Compare command", "--input1={0}", options.InputFile1);
            DicConsole.DebugWriteLine("Compare command", "--input2={0}", options.InputFile2);

            FiltersList filtersList  = new FiltersList();
            IFilter     inputFilter1 = filtersList.GetFilter(options.InputFile1);

            filtersList = new FiltersList();
            IFilter inputFilter2 = filtersList.GetFilter(options.InputFile2);

            if (inputFilter1 == null)
            {
                DicConsole.ErrorWriteLine("Cannot open input file 1");
                return;
            }

            if (inputFilter2 == null)
            {
                DicConsole.ErrorWriteLine("Cannot open input file 2");
                return;
            }

            IMediaImage input1Format = ImageFormat.Detect(inputFilter1);
            IMediaImage input2Format = ImageFormat.Detect(inputFilter2);

            if (input1Format == null)
            {
                DicConsole.ErrorWriteLine("Input file 1 format not identified, not proceeding with comparison.");
                return;
            }

            if (options.Verbose)
            {
                DicConsole.VerboseWriteLine("Input file 1 format identified by {0} ({1}).", input1Format.Name,
                                            input1Format.Id);
            }
            else
            {
                DicConsole.WriteLine("Input file 1 format identified by {0}.", input1Format.Name);
            }

            if (input2Format == null)
            {
                DicConsole.ErrorWriteLine("Input file 2 format not identified, not proceeding with comparison.");
                return;
            }

            if (options.Verbose)
            {
                DicConsole.VerboseWriteLine("Input file 2 format identified by {0} ({1}).", input2Format.Name,
                                            input2Format.Id);
            }
            else
            {
                DicConsole.WriteLine("Input file 2 format identified by {0}.", input2Format.Name);
            }

            input1Format.Open(inputFilter1);
            input2Format.Open(inputFilter2);

            Core.Statistics.AddMediaFormat(input1Format.Format);
            Core.Statistics.AddMediaFormat(input2Format.Format);
            Core.Statistics.AddMedia(input1Format.Info.MediaType, false);
            Core.Statistics.AddMedia(input2Format.Info.MediaType, false);
            Core.Statistics.AddFilter(inputFilter1.Name);
            Core.Statistics.AddFilter(inputFilter2.Name);

            StringBuilder sb = new StringBuilder();

            if (options.Verbose)
            {
                sb.AppendLine("\tDisc image 1\tDisc image 2");
                sb.AppendLine("================================");
                sb.AppendFormat("File\t{0}\t{1}", options.InputFile1, options.InputFile2).AppendLine();
                sb.AppendFormat("Disc image format\t{0}\t{1}", input1Format.Name, input2Format.Name).AppendLine();
            }
            else
            {
                sb.AppendFormat("Disc image 1: {0}", options.InputFile1).AppendLine();
                sb.AppendFormat("Disc image 2: {0}", options.InputFile2).AppendLine();
            }

            bool imagesDiffer = false;

            DiscImages.ImageInfo image1Info     = new DiscImages.ImageInfo();
            DiscImages.ImageInfo image2Info     = new DiscImages.ImageInfo();
            List <Session>       image1Sessions = new List <Session>();
            List <Session>       image2Sessions = new List <Session>();
            Dictionary <MediaTagType, byte[]> image1DiskTags = new Dictionary <MediaTagType, byte[]>();
            Dictionary <MediaTagType, byte[]> image2DiskTags = new Dictionary <MediaTagType, byte[]>();

            image1Info.HasPartitions = input1Format.Info.HasPartitions;
            #pragma warning disable RECS0022 // A catch clause that catches System.Exception and has an empty body
            try { image1Sessions = input1Format.Sessions; }
            catch
            {
                // ignored
            }
            #pragma warning restore RECS0022 // A catch clause that catches System.Exception and has an empty body
            image1Info.HasSessions          |= image1Sessions?.Count > 0;
            image1Info.ImageSize             = input1Format.Info.ImageSize;
            image1Info.Sectors               = input1Format.Info.Sectors;
            image1Info.SectorSize            = input1Format.Info.SectorSize;
            image1Info.CreationTime          = input1Format.Info.CreationTime;
            image1Info.LastModificationTime  = input1Format.Info.LastModificationTime;
            image1Info.MediaType             = input1Format.Info.MediaType;
            image1Info.Version               = input1Format.Info.Version;
            image1Info.Application           = input1Format.Info.Application;
            image1Info.ApplicationVersion    = input1Format.Info.ApplicationVersion;
            image1Info.Creator               = input1Format.Info.Creator;
            image1Info.MediaTitle            = input1Format.Info.MediaTitle;
            image1Info.Comments              = input1Format.Info.Comments;
            image1Info.MediaManufacturer     = input1Format.Info.MediaManufacturer;
            image1Info.MediaModel            = input1Format.Info.MediaModel;
            image1Info.MediaSerialNumber     = input1Format.Info.MediaSerialNumber;
            image1Info.MediaBarcode          = input1Format.Info.MediaBarcode;
            image1Info.MediaPartNumber       = input1Format.Info.MediaPartNumber;
            image1Info.MediaSequence         = input1Format.Info.MediaSequence;
            image1Info.LastMediaSequence     = input1Format.Info.LastMediaSequence;
            image1Info.DriveManufacturer     = input1Format.Info.DriveManufacturer;
            image1Info.DriveModel            = input1Format.Info.DriveModel;
            image1Info.DriveSerialNumber     = input1Format.Info.DriveSerialNumber;
            image1Info.DriveFirmwareRevision = input1Format.Info.DriveFirmwareRevision;
            foreach (MediaTagType disktag in Enum.GetValues(typeof(MediaTagType)))
            {
                try
                {
                    byte[] temparray = input1Format.ReadDiskTag(disktag);
                    image1DiskTags.Add(disktag, temparray);
                }
                #pragma warning disable RECS0022 // A catch clause that catches System.Exception and has an empty body
                catch
                {
                    // ignored
                }
                #pragma warning restore RECS0022 // A catch clause that catches System.Exception and has an empty body
            }

            image2Info.HasPartitions = input2Format.Info.HasPartitions;
            #pragma warning disable RECS0022 // A catch clause that catches System.Exception and has an empty body
            try { image2Sessions = input2Format.Sessions; }
            catch
            {
                // ignored
            }
            #pragma warning restore RECS0022 // A catch clause that catches System.Exception and has an empty body
            image2Info.HasSessions          |= image2Sessions?.Count > 0;
            image2Info.ImageSize             = input2Format.Info.ImageSize;
            image2Info.Sectors               = input2Format.Info.Sectors;
            image2Info.SectorSize            = input2Format.Info.SectorSize;
            image2Info.CreationTime          = input2Format.Info.CreationTime;
            image2Info.LastModificationTime  = input2Format.Info.LastModificationTime;
            image2Info.MediaType             = input2Format.Info.MediaType;
            image2Info.Version               = input2Format.Info.Version;
            image2Info.Application           = input2Format.Info.Application;
            image2Info.ApplicationVersion    = input2Format.Info.ApplicationVersion;
            image2Info.Creator               = input2Format.Info.Creator;
            image2Info.MediaTitle            = input2Format.Info.MediaTitle;
            image2Info.Comments              = input2Format.Info.Comments;
            image2Info.MediaManufacturer     = input2Format.Info.MediaManufacturer;
            image2Info.MediaModel            = input2Format.Info.MediaModel;
            image2Info.MediaSerialNumber     = input2Format.Info.MediaSerialNumber;
            image2Info.MediaBarcode          = input2Format.Info.MediaBarcode;
            image2Info.MediaPartNumber       = input2Format.Info.MediaPartNumber;
            image2Info.MediaSequence         = input2Format.Info.MediaSequence;
            image2Info.LastMediaSequence     = input2Format.Info.LastMediaSequence;
            image2Info.DriveManufacturer     = input2Format.Info.DriveManufacturer;
            image2Info.DriveModel            = input2Format.Info.DriveModel;
            image2Info.DriveSerialNumber     = input2Format.Info.DriveSerialNumber;
            image2Info.DriveFirmwareRevision = input2Format.Info.DriveFirmwareRevision;
            foreach (MediaTagType disktag in Enum.GetValues(typeof(MediaTagType)))
            {
                try
                {
                    byte[] temparray = input2Format.ReadDiskTag(disktag);
                    image2DiskTags.Add(disktag, temparray);
                }
                #pragma warning disable RECS0022 // A catch clause that catches System.Exception and has an empty body
                catch
                {
                    // ignored
                }
                #pragma warning restore RECS0022 // A catch clause that catches System.Exception and has an empty body
            }

            if (options.Verbose)
            {
                sb.AppendFormat("Has partitions?\t{0}\t{1}", image1Info.HasPartitions, image2Info.HasPartitions)
                .AppendLine();
                sb.AppendFormat("Has sessions?\t{0}\t{1}", image1Info.HasSessions, image2Info.HasSessions)
                .AppendLine();
                sb.AppendFormat("Image size\t{0}\t{1}", image1Info.ImageSize, image2Info.ImageSize).AppendLine();
                sb.AppendFormat("Sectors\t{0}\t{1}", image1Info.Sectors, image2Info.Sectors).AppendLine();
                sb.AppendFormat("Sector size\t{0}\t{1}", image1Info.SectorSize, image2Info.SectorSize).AppendLine();
                sb.AppendFormat("Creation time\t{0}\t{1}", image1Info.CreationTime, image2Info.CreationTime)
                .AppendLine();
                sb.AppendFormat("Last modification time\t{0}\t{1}", image1Info.LastModificationTime,
                                image2Info.LastModificationTime).AppendLine();
                sb.AppendFormat("Disk type\t{0}\t{1}", image1Info.MediaType, image2Info.MediaType)
                .AppendLine();
                sb.AppendFormat("Image version\t{0}\t{1}", image1Info.Version, image2Info.Version).AppendLine();
                sb.AppendFormat("Image application\t{0}\t{1}", image1Info.Application, image2Info.Application)
                .AppendLine();
                sb.AppendFormat("Image application version\t{0}\t{1}", image1Info.ApplicationVersion,
                                image2Info.ApplicationVersion).AppendLine();
                sb.AppendFormat("Image creator\t{0}\t{1}", image1Info.Creator, image2Info.Creator).AppendLine();
                sb.AppendFormat("Image name\t{0}\t{1}", image1Info.MediaTitle, image2Info.MediaTitle).AppendLine();
                sb.AppendFormat("Image comments\t{0}\t{1}", image1Info.Comments, image2Info.Comments).AppendLine();
                sb.AppendFormat("Disk manufacturer\t{0}\t{1}", image1Info.MediaManufacturer,
                                image2Info.MediaManufacturer).AppendLine();
                sb.AppendFormat("Disk model\t{0}\t{1}", image1Info.MediaModel, image2Info.MediaModel).AppendLine();
                sb.AppendFormat("Disk serial number\t{0}\t{1}", image1Info.MediaSerialNumber,
                                image2Info.MediaSerialNumber).AppendLine();
                sb.AppendFormat("Disk barcode\t{0}\t{1}", image1Info.MediaBarcode, image2Info.MediaBarcode)
                .AppendLine();
                sb.AppendFormat("Disk part no.\t{0}\t{1}", image1Info.MediaPartNumber, image2Info.MediaPartNumber)
                .AppendLine();
                sb.AppendFormat("Disk sequence\t{0}\t{1}", image1Info.MediaSequence, image2Info.MediaSequence)
                .AppendLine();
                sb.AppendFormat("Last disk on sequence\t{0}\t{1}", image1Info.LastMediaSequence,
                                image2Info.LastMediaSequence).AppendLine();
                sb.AppendFormat("Drive manufacturer\t{0}\t{1}", image1Info.DriveManufacturer,
                                image2Info.DriveManufacturer).AppendLine();
                sb.AppendFormat("Drive firmware revision\t{0}\t{1}", image1Info.DriveFirmwareRevision,
                                image2Info.DriveFirmwareRevision).AppendLine();
                sb.AppendFormat("Drive model\t{0}\t{1}", image1Info.DriveModel, image2Info.DriveModel).AppendLine();
                sb.AppendFormat("Drive serial number\t{0}\t{1}", image1Info.DriveSerialNumber,
                                image2Info.DriveSerialNumber).AppendLine();
                foreach (MediaTagType disktag in Enum.GetValues(typeof(MediaTagType)))
                {
                    sb.AppendFormat("Has {0}?\t{1}\t{2}", disktag, image1DiskTags.ContainsKey(disktag),
                                    image2DiskTags.ContainsKey(disktag)).AppendLine();
                }
            }

            DicConsole.WriteLine("Comparing disk image characteristics");

            if (image1Info.HasPartitions != image2Info.HasPartitions)
            {
                imagesDiffer = true;
                if (!options.Verbose)
                {
                    sb.AppendLine("Image partitioned status differ");
                }
            }

            if (image1Info.HasSessions != image2Info.HasSessions)
            {
                imagesDiffer = true;
                if (!options.Verbose)
                {
                    sb.AppendLine("Image session status differ");
                }
            }

            if (image1Info.ImageSize != image2Info.ImageSize)
            {
                imagesDiffer = true;
                if (!options.Verbose)
                {
                    sb.AppendLine("Image size differ");
                }
            }

            if (image1Info.Sectors != image2Info.Sectors)
            {
                imagesDiffer = true;
                if (!options.Verbose)
                {
                    sb.AppendLine("Image sectors differ");
                }
            }

            if (image1Info.SectorSize != image2Info.SectorSize)
            {
                imagesDiffer = true;
                if (!options.Verbose)
                {
                    sb.AppendLine("Image sector size differ");
                }
            }

            if (image1Info.CreationTime != image2Info.CreationTime)
            {
                imagesDiffer = true;
                if (!options.Verbose)
                {
                    sb.AppendLine("Image creation time differ");
                }
            }

            if (image1Info.LastModificationTime != image2Info.LastModificationTime)
            {
                imagesDiffer = true;
                if (!options.Verbose)
                {
                    sb.AppendLine("Image last modification time differ");
                }
            }

            if (image1Info.MediaType != image2Info.MediaType)
            {
                imagesDiffer = true;
                if (!options.Verbose)
                {
                    sb.AppendLine("Disk type differ");
                }
            }

            if (image1Info.Version != image2Info.Version)
            {
                imagesDiffer = true;
                if (!options.Verbose)
                {
                    sb.AppendLine("Image version differ");
                }
            }

            if (image1Info.Application != image2Info.Application)
            {
                imagesDiffer = true;
                if (!options.Verbose)
                {
                    sb.AppendLine("Image application differ");
                }
            }

            if (image1Info.ApplicationVersion != image2Info.ApplicationVersion)
            {
                imagesDiffer = true;
                if (!options.Verbose)
                {
                    sb.AppendLine("Image application version differ");
                }
            }

            if (image1Info.Creator != image2Info.Creator)
            {
                imagesDiffer = true;
                if (!options.Verbose)
                {
                    sb.AppendLine("Image creator differ");
                }
            }

            if (image1Info.MediaTitle != image2Info.MediaTitle)
            {
                imagesDiffer = true;
                if (!options.Verbose)
                {
                    sb.AppendLine("Image name differ");
                }
            }

            if (image1Info.Comments != image2Info.Comments)
            {
                imagesDiffer = true;
                if (!options.Verbose)
                {
                    sb.AppendLine("Image comments differ");
                }
            }

            if (image1Info.MediaManufacturer != image2Info.MediaManufacturer)
            {
                imagesDiffer = true;
                if (!options.Verbose)
                {
                    sb.AppendLine("Disk manufacturer differ");
                }
            }

            if (image1Info.MediaModel != image2Info.MediaModel)
            {
                imagesDiffer = true;
                if (!options.Verbose)
                {
                    sb.AppendLine("Disk model differ");
                }
            }

            if (image1Info.MediaSerialNumber != image2Info.MediaSerialNumber)
            {
                imagesDiffer = true;
                if (!options.Verbose)
                {
                    sb.AppendLine("Disk serial number differ");
                }
            }

            if (image1Info.MediaBarcode != image2Info.MediaBarcode)
            {
                imagesDiffer = true;
                if (!options.Verbose)
                {
                    sb.AppendLine("Disk barcode differ");
                }
            }

            if (image1Info.MediaPartNumber != image2Info.MediaPartNumber)
            {
                imagesDiffer = true;
                if (!options.Verbose)
                {
                    sb.AppendLine("Disk part number differ");
                }
            }

            if (image1Info.MediaSequence != image2Info.MediaSequence)
            {
                imagesDiffer = true;
                if (!options.Verbose)
                {
                    sb.AppendLine("Disk sequence differ");
                }
            }

            if (image1Info.LastMediaSequence != image2Info.LastMediaSequence)
            {
                imagesDiffer = true;
                if (!options.Verbose)
                {
                    sb.AppendLine("Last disk in sequence differ");
                }
            }

            if (image1Info.DriveManufacturer != image2Info.DriveManufacturer)
            {
                imagesDiffer = true;
                if (!options.Verbose)
                {
                    sb.AppendLine("Drive manufacturer differ");
                }
            }

            if (image1Info.DriveModel != image2Info.DriveModel)
            {
                imagesDiffer = true;
                if (!options.Verbose)
                {
                    sb.AppendLine("Drive model differ");
                }
            }

            if (image1Info.DriveSerialNumber != image2Info.DriveSerialNumber)
            {
                imagesDiffer = true;
                if (!options.Verbose)
                {
                    sb.AppendLine("Drive serial number differ");
                }
            }

            if (image1Info.DriveFirmwareRevision != image2Info.DriveFirmwareRevision)
            {
                imagesDiffer = true;
                if (!options.Verbose)
                {
                    sb.AppendLine("Drive firmware revision differ");
                }
            }

            ulong leastSectors;
            if (image1Info.Sectors < image2Info.Sectors)
            {
                imagesDiffer = true;
                leastSectors = image1Info.Sectors;
                if (!options.Verbose)
                {
                    sb.AppendLine("Image 2 has more sectors");
                }
            }
            else if (image1Info.Sectors > image2Info.Sectors)
            {
                imagesDiffer = true;
                leastSectors = image2Info.Sectors;
                if (!options.Verbose)
                {
                    sb.AppendLine("Image 1 has more sectors");
                }
            }
            else
            {
                leastSectors = image1Info.Sectors;
            }

            DicConsole.WriteLine("Comparing sectors...");

            for (ulong sector = 0; sector < leastSectors; sector++)
            {
                DicConsole.Write("\rComparing sector {0} of {1}...", sector + 1, leastSectors);
                try
                {
                    byte[] image1Sector = input1Format.ReadSector(sector);
                    byte[] image2Sector = input2Format.ReadSector(sector);
                    ArrayHelpers.CompareBytes(out bool different, out bool sameSize, image1Sector, image2Sector);
                    if (different)
                    {
                        imagesDiffer = true;
                        sb.AppendFormat("Sector {0} is different", sector).AppendLine();
                    }
                    else if (!sameSize)
                    {
                        imagesDiffer = true;
                        sb
                        .AppendFormat("Sector {0} has different sizes ({1} bytes in image 1, {2} in image 2) but are otherwise identical",
                                      sector, image1Sector.LongLength, image2Sector.LongLength).AppendLine();
                    }
                }
                #pragma warning disable RECS0022 // A catch clause that catches System.Exception and has an empty body
                catch
                {
                    // ignored
                }
                #pragma warning restore RECS0022 // A catch clause that catches System.Exception and has an empty body
            }

            DicConsole.WriteLine();

            sb.AppendLine(imagesDiffer ? "Images differ" : "Images do not differ");

            DicConsole.WriteLine(sb.ToString());

            Core.Statistics.AddCommand("compare");
        }
        public void Convert()
        {
            Environment.CurrentDirectory = DataFolder;

            Resume           resume  = null;
            CICMMetadataType sidecar = null;

            var     filtersList = new FiltersList();
            IFilter inputFilter = filtersList.GetFilter(InputPath);

            Assert.IsNotNull(inputFilter, "Cannot open specified file.");

            string outputPath = Path.Combine(Path.GetTempPath(), SuggestedOutputFilename);

            Assert.IsFalse(File.Exists(outputPath), "Output file already exists, not continuing.");

            IMediaImage inputFormat = ImageFormat.Detect(inputFilter);

            Assert.IsNotNull(inputFormat, "Input image format not identified, not proceeding with conversion.");

            Assert.IsTrue(inputFormat.Open(inputFilter), "Unable to open image format");

            Assert.IsTrue(OutputFormat.SupportedMediaTypes.Contains(inputFormat.Info.MediaType),
                          "Output format does not support media type, cannot continue...");

            if (inputFormat.Info.ReadableSectorTags.Count == 0)
            {
                Assert.IsFalse(UseLong, "Input image does not support long sectors.");
            }

            var inputOptical  = inputFormat as IOpticalMediaImage;
            var outputOptical = OutputFormat as IWritableOpticalImage;

            Assert.IsNotNull(inputOptical, "Could not treat existing image as optical disc.");
            Assert.IsNotNull(outputOptical, "Could not treat new image as optical disc.");
            Assert.IsNotNull(inputOptical.Tracks, "Existing image contains no tracks.");

            Assert.IsTrue(outputOptical.Create(outputPath, inputFormat.Info.MediaType, ParsedOptions, inputFormat.Info.Sectors, inputFormat.Info.SectorSize),
                          $"Error {outputOptical.ErrorMessage} creating output image.");

            var metadata = new ImageInfo
            {
                Application           = "Aaru",
                ApplicationVersion    = Version.GetVersion(),
                Comments              = inputFormat.Info.Comments,
                Creator               = inputFormat.Info.Creator,
                DriveFirmwareRevision = inputFormat.Info.DriveFirmwareRevision,
                DriveManufacturer     = inputFormat.Info.DriveManufacturer,
                DriveModel            = inputFormat.Info.DriveModel,
                DriveSerialNumber     = inputFormat.Info.DriveSerialNumber,
                LastMediaSequence     = inputFormat.Info.LastMediaSequence,
                MediaBarcode          = inputFormat.Info.MediaBarcode,
                MediaManufacturer     = inputFormat.Info.MediaManufacturer,
                MediaModel            = inputFormat.Info.MediaModel,
                MediaPartNumber       = inputFormat.Info.MediaPartNumber,
                MediaSequence         = inputFormat.Info.MediaSequence,
                MediaSerialNumber     = inputFormat.Info.MediaSerialNumber,
                MediaTitle            = inputFormat.Info.MediaTitle
            };

            Assert.IsTrue(outputOptical.SetMetadata(metadata),
                          $"Error {outputOptical.ErrorMessage} setting metadata, ");

            CICMMetadataType        cicmMetadata = inputFormat.CicmMetadata;
            List <DumpHardwareType> dumpHardware = inputFormat.DumpHardware;

            foreach (MediaTagType mediaTag in inputFormat.Info.ReadableMediaTags.Where(mediaTag =>
                                                                                       outputOptical.SupportedMediaTags.Contains(mediaTag)))
            {
                AaruConsole.WriteLine("Converting media tag {0}", mediaTag);
                byte[] tag = inputFormat.ReadDiskTag(mediaTag);

                Assert.IsTrue(outputOptical.WriteMediaTag(tag, mediaTag));
            }

            AaruConsole.WriteLine("{0} sectors to convert", inputFormat.Info.Sectors);
            ulong doneSectors;

            Assert.IsTrue(outputOptical.SetTracks(inputOptical.Tracks),
                          $"Error {outputOptical.ErrorMessage} sending tracks list to output image.");

            foreach (Track track in inputOptical.Tracks)
            {
                doneSectors = 0;
                ulong trackSectors = track.TrackEndSector - track.TrackStartSector + 1;

                while (doneSectors < trackSectors)
                {
                    byte[] sector;

                    uint sectorsToDo;

                    if (trackSectors - doneSectors >= SECTORS_TO_READ)
                    {
                        sectorsToDo = SECTORS_TO_READ;
                    }
                    else
                    {
                        sectorsToDo = (uint)(trackSectors - doneSectors);
                    }

                    bool useNotLong = false;
                    bool result     = false;

                    if (UseLong)
                    {
                        if (sectorsToDo == 1)
                        {
                            sector = inputFormat.ReadSectorLong(doneSectors + track.TrackStartSector);
                            result = outputOptical.WriteSectorLong(sector, doneSectors + track.TrackStartSector);
                        }
                        else
                        {
                            sector = inputFormat.ReadSectorsLong(doneSectors + track.TrackStartSector, sectorsToDo);

                            result = outputOptical.WriteSectorsLong(sector, doneSectors + track.TrackStartSector,
                                                                    sectorsToDo);
                        }

                        if (!result &&
                            sector.Length % 2352 != 0)
                        {
                            useNotLong = true;
                        }
                    }

                    if (!UseLong || useNotLong)
                    {
                        if (sectorsToDo == 1)
                        {
                            sector = inputFormat.ReadSector(doneSectors + track.TrackStartSector);
                            result = outputOptical.WriteSector(sector, doneSectors + track.TrackStartSector);
                        }
                        else
                        {
                            sector = inputFormat.ReadSectors(doneSectors + track.TrackStartSector, sectorsToDo);

                            result = outputOptical.WriteSectors(sector, doneSectors + track.TrackStartSector,
                                                                sectorsToDo);
                        }
                    }

                    Assert.IsTrue(result,
                                  $"Error {outputOptical.ErrorMessage} writing sector {doneSectors + track.TrackStartSector}, not continuing...");

                    doneSectors += sectorsToDo;
                }
            }

            Dictionary <byte, string> isrcs                  = new Dictionary <byte, string>();
            Dictionary <byte, byte>   trackFlags             = new Dictionary <byte, byte>();
            string                 mcn                       = null;
            HashSet <int>          subchannelExtents         = new HashSet <int>();
            Dictionary <byte, int> smallestPregapLbaPerTrack = new Dictionary <byte, int>();

            Track[] tracks = new Track[inputOptical.Tracks.Count];

            for (int i = 0; i < tracks.Length; i++)
            {
                tracks[i] = new Track
                {
                    Indexes                = new Dictionary <ushort, int>(),
                    TrackDescription       = inputOptical.Tracks[i].TrackDescription,
                    TrackEndSector         = inputOptical.Tracks[i].TrackEndSector,
                    TrackStartSector       = inputOptical.Tracks[i].TrackStartSector,
                    TrackPregap            = inputOptical.Tracks[i].TrackPregap,
                    TrackSequence          = inputOptical.Tracks[i].TrackSequence,
                    TrackSession           = inputOptical.Tracks[i].TrackSession,
                    TrackBytesPerSector    = inputOptical.Tracks[i].TrackBytesPerSector,
                    TrackRawBytesPerSector = inputOptical.Tracks[i].TrackRawBytesPerSector,
                    TrackType              = inputOptical.Tracks[i].TrackType,
                    TrackSubchannelType    = inputOptical.Tracks[i].TrackSubchannelType
                };

                foreach (KeyValuePair <ushort, int> idx in inputOptical.Tracks[i].Indexes)
                {
                    tracks[i].Indexes[idx.Key] = idx.Value;
                }
            }

            foreach (SectorTagType tag in inputFormat.Info.ReadableSectorTags.Where(t => t == SectorTagType.CdTrackIsrc).
                     OrderBy(t => t))
            {
                foreach (Track track in tracks)
                {
                    byte[] isrc = inputFormat.ReadSectorTag(track.TrackSequence, tag);

                    if (isrc is null)
                    {
                        continue;
                    }

                    isrcs[(byte)track.TrackSequence] = Encoding.UTF8.GetString(isrc);
                }
            }

            foreach (SectorTagType tag in inputFormat.Info.ReadableSectorTags.
                     Where(t => t == SectorTagType.CdTrackFlags).OrderBy(t => t))
            {
                foreach (Track track in tracks)
                {
                    byte[] flags = inputFormat.ReadSectorTag(track.TrackSequence, tag);

                    if (flags is null)
                    {
                        continue;
                    }

                    trackFlags[(byte)track.TrackSequence] = flags[0];
                }
            }

            for (ulong s = 0; s < inputFormat.Info.Sectors; s++)
            {
                if (s > int.MaxValue)
                {
                    break;
                }

                subchannelExtents.Add((int)s);
            }

            foreach (SectorTagType tag in inputFormat.Info.ReadableSectorTags.OrderBy(t => t).TakeWhile(tag => UseLong))
            {
                switch (tag)
                {
                case SectorTagType.AppleSectorTag:
                case SectorTagType.CdSectorSync:
                case SectorTagType.CdSectorHeader:
                case SectorTagType.CdSectorSubHeader:
                case SectorTagType.CdSectorEdc:
                case SectorTagType.CdSectorEccP:
                case SectorTagType.CdSectorEccQ:
                case SectorTagType.CdSectorEcc:
                    // This tags are inline in long sector
                    continue;
                }

                if (!outputOptical.SupportedSectorTags.Contains(tag))
                {
                    continue;
                }

                foreach (Track track in inputOptical.Tracks)
                {
                    doneSectors = 0;
                    ulong  trackSectors = track.TrackEndSector - track.TrackStartSector + 1;
                    byte[] sector;
                    bool   result;

                    switch (tag)
                    {
                    case SectorTagType.CdTrackFlags:
                    case SectorTagType.CdTrackIsrc:
                        sector = inputFormat.ReadSectorTag(track.TrackSequence, tag);
                        result = outputOptical.WriteSectorTag(sector, track.TrackSequence, tag);

                        Assert.IsTrue(result, $"Error {outputOptical.ErrorMessage} writing tag, not continuing...");

                        continue;
                    }

                    while (doneSectors < trackSectors)
                    {
                        uint sectorsToDo;

                        if (trackSectors - doneSectors >= SECTORS_TO_READ)
                        {
                            sectorsToDo = SECTORS_TO_READ;
                        }
                        else
                        {
                            sectorsToDo = (uint)(trackSectors - doneSectors);
                        }

                        if (sectorsToDo == 1)
                        {
                            sector = inputFormat.ReadSectorTag(doneSectors + track.TrackStartSector, tag);

                            if (tag == SectorTagType.CdSectorSubchannel)
                            {
                                bool indexesChanged = CompactDisc.WriteSubchannelToImage(MmcSubchannel.Raw,
                                                                                         MmcSubchannel.Raw, sector, doneSectors + track.TrackStartSector, 1, null,
                                                                                         isrcs, (byte)track.TrackSequence, ref mcn, tracks, subchannelExtents, true,
                                                                                         outputOptical, true, true, null, null, smallestPregapLbaPerTrack, false);

                                if (indexesChanged)
                                {
                                    outputOptical.SetTracks(tracks.ToList());
                                }

                                result = true;
                            }
                            else
                            {
                                result = outputOptical.WriteSectorTag(sector, doneSectors + track.TrackStartSector,
                                                                      tag);
                            }
                        }
                        else
                        {
                            sector = inputFormat.ReadSectorsTag(doneSectors + track.TrackStartSector, sectorsToDo, tag);

                            if (tag == SectorTagType.CdSectorSubchannel)
                            {
                                bool indexesChanged = CompactDisc.WriteSubchannelToImage(MmcSubchannel.Raw,
                                                                                         MmcSubchannel.Raw, sector, doneSectors + track.TrackStartSector, sectorsToDo,
                                                                                         null, isrcs, (byte)track.TrackSequence, ref mcn, tracks, subchannelExtents,
                                                                                         true, outputOptical, true, true, null, null, smallestPregapLbaPerTrack, false);

                                if (indexesChanged)
                                {
                                    outputOptical.SetTracks(tracks.ToList());
                                }

                                result = true;
                            }
                            else
                            {
                                result = outputOptical.WriteSectorsTag(sector, doneSectors + track.TrackStartSector,
                                                                       sectorsToDo, tag);
                            }
                        }

                        Assert.IsTrue(result,
                                      $"Error {outputOptical.ErrorMessage} writing tag for sector {doneSectors + track.TrackStartSector}, not continuing...");

                        doneSectors += sectorsToDo;
                    }
                }
            }

            if (isrcs.Count > 0)
            {
                foreach (KeyValuePair <byte, string> isrc in isrcs)
                {
                    outputOptical.WriteSectorTag(Encoding.UTF8.GetBytes(isrc.Value), isrc.Key,
                                                 SectorTagType.CdTrackIsrc);
                }
            }

            if (trackFlags.Count > 0)
            {
                foreach ((byte track, byte flags) in trackFlags)
                {
                    outputOptical.WriteSectorTag(new[]
                    {
                        flags
                    }, track, SectorTagType.CdTrackFlags);
                }
            }

            if (mcn != null)
            {
                outputOptical.WriteMediaTag(Encoding.UTF8.GetBytes(mcn), MediaTagType.CD_MCN);
            }

            if (resume != null ||
                dumpHardware != null)
            {
                if (resume != null)
                {
                    outputOptical.SetDumpHardware(resume.Tries);
                }
                else if (dumpHardware != null)
                {
                    outputOptical.SetDumpHardware(dumpHardware);
                }
            }

            if (sidecar != null ||
                cicmMetadata != null)
            {
                if (sidecar != null)
                {
                    outputOptical.SetCicmMetadata(sidecar);
                }
                else if (cicmMetadata != null)
                {
                    outputOptical.SetCicmMetadata(cicmMetadata);
                }
            }

            Assert.True(outputOptical.Close(),
                        $"Error {outputOptical.ErrorMessage} closing output image... Contents are not correct.");

            // Some images will never generate the same
            if (Md5 != null)
            {
                string md5 = Md5Context.File(outputPath, out _);

                Assert.AreEqual(Md5, md5, "Hashes are different");
            }

            File.Delete(outputPath);
        }
示例#14
0
        public override int Invoke(IEnumerable <string> arguments)
        {
            List <string> extra = Options.Parse(arguments);

            if (showHelp)
            {
                Options.WriteOptionDescriptions(CommandSet.Out);
                return((int)ErrorNumber.HelpRequested);
            }

            MainClass.PrintCopyright();
            if (MainClass.Debug)
            {
                DicConsole.DebugWriteLineEvent += System.Console.Error.WriteLine;
            }
            if (MainClass.Verbose)
            {
                DicConsole.VerboseWriteLineEvent += System.Console.WriteLine;
            }
            Statistics.AddCommand("compare");

            if (extra.Count > 2)
            {
                DicConsole.ErrorWriteLine("Too many arguments.");
                return((int)ErrorNumber.UnexpectedArgumentCount);
            }

            if (extra.Count <= 1)
            {
                DicConsole.ErrorWriteLine("Missing input image.");
                return((int)ErrorNumber.MissingArgument);
            }

            inputFile1 = extra[0];
            inputFile2 = extra[1];

            DicConsole.DebugWriteLine("Compare command", "--debug={0}", MainClass.Debug);
            DicConsole.DebugWriteLine("Compare command", "--input1={0}", inputFile1);
            DicConsole.DebugWriteLine("Compare command", "--input2={0}", inputFile2);
            DicConsole.DebugWriteLine("Compare command", "--verbose={0}", MainClass.Verbose);

            FiltersList filtersList  = new FiltersList();
            IFilter     inputFilter1 = filtersList.GetFilter(inputFile1);

            filtersList = new FiltersList();
            IFilter inputFilter2 = filtersList.GetFilter(inputFile2);

            if (inputFilter1 == null)
            {
                DicConsole.ErrorWriteLine("Cannot open input file 1");
                return((int)ErrorNumber.CannotOpenFile);
            }

            if (inputFilter2 == null)
            {
                DicConsole.ErrorWriteLine("Cannot open input file 2");
                return((int)ErrorNumber.CannotOpenFile);
            }

            IMediaImage input1Format = ImageFormat.Detect(inputFilter1);
            IMediaImage input2Format = ImageFormat.Detect(inputFilter2);

            if (input1Format == null)
            {
                DicConsole.ErrorWriteLine("Input file 1 format not identified, not proceeding with comparison.");
                return((int)ErrorNumber.UnrecognizedFormat);
            }

            if (MainClass.Verbose)
            {
                DicConsole.VerboseWriteLine("Input file 1 format identified by {0} ({1}).", input1Format.Name,
                                            input1Format.Id);
            }
            else
            {
                DicConsole.WriteLine("Input file 1 format identified by {0}.", input1Format.Name);
            }

            if (input2Format == null)
            {
                DicConsole.ErrorWriteLine("Input file 2 format not identified, not proceeding with comparison.");
                return((int)ErrorNumber.UnrecognizedFormat);
            }

            if (MainClass.Verbose)
            {
                DicConsole.VerboseWriteLine("Input file 2 format identified by {0} ({1}).", input2Format.Name,
                                            input2Format.Id);
            }
            else
            {
                DicConsole.WriteLine("Input file 2 format identified by {0}.", input2Format.Name);
            }

            input1Format.Open(inputFilter1);
            input2Format.Open(inputFilter2);

            Statistics.AddMediaFormat(input1Format.Format);
            Statistics.AddMediaFormat(input2Format.Format);
            Statistics.AddMedia(input1Format.Info.MediaType, false);
            Statistics.AddMedia(input2Format.Info.MediaType, false);
            Statistics.AddFilter(inputFilter1.Name);
            Statistics.AddFilter(inputFilter2.Name);

            StringBuilder sb = new StringBuilder();

            if (MainClass.Verbose)
            {
                sb.AppendLine("\tDisc image 1\tDisc image 2");
                sb.AppendLine("================================");
                sb.AppendFormat("File\t{0}\t{1}", inputFile1, inputFile2).AppendLine();
                sb.AppendFormat("Disc image format\t{0}\t{1}", input1Format.Name, input2Format.Name).AppendLine();
            }
            else
            {
                sb.AppendFormat("Disc image 1: {0}", inputFile1).AppendLine();
                sb.AppendFormat("Disc image 2: {0}", inputFile2).AppendLine();
            }

            bool imagesDiffer = false;

            ImageInfo      image1Info     = input1Format.Info;
            ImageInfo      image2Info     = input2Format.Info;
            List <Session> image1Sessions = new List <Session>();
            List <Session> image2Sessions = new List <Session>();
            Dictionary <MediaTagType, byte[]> image1DiskTags = new Dictionary <MediaTagType, byte[]>();
            Dictionary <MediaTagType, byte[]> image2DiskTags = new Dictionary <MediaTagType, byte[]>();

            foreach (MediaTagType disktag in Enum.GetValues(typeof(MediaTagType)))
            {
                try
                {
                    byte[] temparray = input1Format.ReadDiskTag(disktag);
                    image1DiskTags.Add(disktag, temparray);
                }
                #pragma warning disable RECS0022 // A catch clause that catches System.Exception and has an empty body
                catch
                {
                    // ignored
                }
                #pragma warning restore RECS0022 // A catch clause that catches System.Exception and has an empty body
            }

            foreach (MediaTagType disktag in Enum.GetValues(typeof(MediaTagType)))
            {
                try
                {
                    byte[] temparray = input2Format.ReadDiskTag(disktag);
                    image2DiskTags.Add(disktag, temparray);
                }
                #pragma warning disable RECS0022 // A catch clause that catches System.Exception and has an empty body
                catch
                {
                    // ignored
                }
                #pragma warning restore RECS0022 // A catch clause that catches System.Exception and has an empty body
            }

            if (MainClass.Verbose)
            {
                sb.AppendFormat("Has partitions?\t{0}\t{1}", image1Info.HasPartitions, image2Info.HasPartitions)
                .AppendLine();
                sb.AppendFormat("Has sessions?\t{0}\t{1}", image1Info.HasSessions, image2Info.HasSessions).AppendLine();
                sb.AppendFormat("Image size\t{0}\t{1}", image1Info.ImageSize, image2Info.ImageSize).AppendLine();
                sb.AppendFormat("Sectors\t{0}\t{1}", image1Info.Sectors, image2Info.Sectors).AppendLine();
                sb.AppendFormat("Sector size\t{0}\t{1}", image1Info.SectorSize, image2Info.SectorSize).AppendLine();
                sb.AppendFormat("Creation time\t{0}\t{1}", image1Info.CreationTime, image2Info.CreationTime)
                .AppendLine();
                sb.AppendFormat("Last modification time\t{0}\t{1}", image1Info.LastModificationTime,
                                image2Info.LastModificationTime).AppendLine();
                sb.AppendFormat("Disk type\t{0}\t{1}", image1Info.MediaType, image2Info.MediaType).AppendLine();
                sb.AppendFormat("Image version\t{0}\t{1}", image1Info.Version, image2Info.Version).AppendLine();
                sb.AppendFormat("Image application\t{0}\t{1}", image1Info.Application, image2Info.Application)
                .AppendLine();
                sb.AppendFormat("Image application version\t{0}\t{1}", image1Info.ApplicationVersion,
                                image2Info.ApplicationVersion).AppendLine();
                sb.AppendFormat("Image creator\t{0}\t{1}", image1Info.Creator, image2Info.Creator).AppendLine();
                sb.AppendFormat("Image name\t{0}\t{1}", image1Info.MediaTitle, image2Info.MediaTitle).AppendLine();
                sb.AppendFormat("Image comments\t{0}\t{1}", image1Info.Comments, image2Info.Comments).AppendLine();
                sb.AppendFormat("Disk manufacturer\t{0}\t{1}", image1Info.MediaManufacturer,
                                image2Info.MediaManufacturer).AppendLine();
                sb.AppendFormat("Disk model\t{0}\t{1}", image1Info.MediaModel, image2Info.MediaModel).AppendLine();
                sb.AppendFormat("Disk serial number\t{0}\t{1}", image1Info.MediaSerialNumber,
                                image2Info.MediaSerialNumber).AppendLine();
                sb.AppendFormat("Disk barcode\t{0}\t{1}", image1Info.MediaBarcode, image2Info.MediaBarcode)
                .AppendLine();
                sb.AppendFormat("Disk part no.\t{0}\t{1}", image1Info.MediaPartNumber, image2Info.MediaPartNumber)
                .AppendLine();
                sb.AppendFormat("Disk sequence\t{0}\t{1}", image1Info.MediaSequence, image2Info.MediaSequence)
                .AppendLine();
                sb.AppendFormat("Last disk on sequence\t{0}\t{1}", image1Info.LastMediaSequence,
                                image2Info.LastMediaSequence).AppendLine();
                sb.AppendFormat("Drive manufacturer\t{0}\t{1}", image1Info.DriveManufacturer,
                                image2Info.DriveManufacturer).AppendLine();
                sb.AppendFormat("Drive firmware revision\t{0}\t{1}", image1Info.DriveFirmwareRevision,
                                image2Info.DriveFirmwareRevision).AppendLine();
                sb.AppendFormat("Drive model\t{0}\t{1}", image1Info.DriveModel, image2Info.DriveModel).AppendLine();
                sb.AppendFormat("Drive serial number\t{0}\t{1}", image1Info.DriveSerialNumber,
                                image2Info.DriveSerialNumber).AppendLine();
                foreach (MediaTagType disktag in Enum.GetValues(typeof(MediaTagType)))
                {
                    sb.AppendFormat("Has {0}?\t{1}\t{2}", disktag, image1DiskTags.ContainsKey(disktag),
                                    image2DiskTags.ContainsKey(disktag)).AppendLine();
                }
            }

            DicConsole.WriteLine("Comparing disk image characteristics");

            if (image1Info.HasPartitions != image2Info.HasPartitions)
            {
                imagesDiffer = true;
                if (!MainClass.Verbose)
                {
                    sb.AppendLine("Image partitioned status differ");
                }
            }

            if (image1Info.HasSessions != image2Info.HasSessions)
            {
                imagesDiffer = true;
                if (!MainClass.Verbose)
                {
                    sb.AppendLine("Image session status differ");
                }
            }

            if (image1Info.Sectors != image2Info.Sectors)
            {
                imagesDiffer = true;
                if (!MainClass.Verbose)
                {
                    sb.AppendLine("Image sectors differ");
                }
            }

            if (image1Info.SectorSize != image2Info.SectorSize)
            {
                imagesDiffer = true;
                if (!MainClass.Verbose)
                {
                    sb.AppendLine("Image sector size differ");
                }
            }

            if (image1Info.MediaType != image2Info.MediaType)
            {
                imagesDiffer = true;
                if (!MainClass.Verbose)
                {
                    sb.AppendLine("Disk type differ");
                }
            }

            ulong leastSectors;
            if (image1Info.Sectors < image2Info.Sectors)
            {
                imagesDiffer = true;
                leastSectors = image1Info.Sectors;
                if (!MainClass.Verbose)
                {
                    sb.AppendLine("Image 2 has more sectors");
                }
            }
            else if (image1Info.Sectors > image2Info.Sectors)
            {
                imagesDiffer = true;
                leastSectors = image2Info.Sectors;
                if (!MainClass.Verbose)
                {
                    sb.AppendLine("Image 1 has more sectors");
                }
            }
            else
            {
                leastSectors = image1Info.Sectors;
            }

            DicConsole.WriteLine("Comparing sectors...");

            for (ulong sector = 0; sector < leastSectors; sector++)
            {
                DicConsole.Write("\rComparing sector {0} of {1}...", sector + 1, leastSectors);
                try
                {
                    byte[] image1Sector = input1Format.ReadSector(sector);
                    byte[] image2Sector = input2Format.ReadSector(sector);
                    ArrayHelpers.CompareBytes(out bool different, out bool sameSize, image1Sector, image2Sector);
                    if (different)
                    {
                        imagesDiffer = true;
                        sb.AppendFormat("Sector {0} is different", sector).AppendLine();
                    }
                    else if (!sameSize)
                    {
                        imagesDiffer = true;
                        sb
                        .AppendFormat("Sector {0} has different sizes ({1} bytes in image 1, {2} in image 2) but are otherwise identical",
                                      sector, image1Sector.LongLength, image2Sector.LongLength).AppendLine();
                    }
                }
                #pragma warning disable RECS0022 // A catch clause that catches System.Exception and has an empty body
                catch
                {
                    // ignored
                }
                #pragma warning restore RECS0022 // A catch clause that catches System.Exception and has an empty body
            }

            DicConsole.WriteLine();

            sb.AppendLine(imagesDiffer ? "Images differ" : "Images do not differ");

            DicConsole.WriteLine(sb.ToString());

            return((int)ErrorNumber.NoError);
        }