2017-05-19 20:28:49 +01:00
|
|
|
// /***************************************************************************
|
2016-10-08 04:29:04 +01:00
|
|
|
// The Disc Image Chef
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
//
|
|
|
|
|
// Filename : CloneCD.cs
|
|
|
|
|
// Author(s) : Natalia Portillo <claunia@claunia.com>
|
|
|
|
|
//
|
2017-12-19 03:50:57 +00:00
|
|
|
// Component : Disc image plugins.
|
2016-10-08 04:29:04 +01:00
|
|
|
//
|
|
|
|
|
// --[ Description ] ----------------------------------------------------------
|
|
|
|
|
//
|
2017-12-19 03:50:57 +00:00
|
|
|
// Manages CloneCD disc images.
|
2016-10-08 04:29:04 +01:00
|
|
|
//
|
|
|
|
|
// --[ License ] --------------------------------------------------------------
|
|
|
|
|
//
|
|
|
|
|
// This library is free software; you can redistribute it and/or modify
|
|
|
|
|
// it under the terms of the GNU Lesser General Public License as
|
|
|
|
|
// published by the Free Software Foundation; either version 2.1 of the
|
|
|
|
|
// License, or (at your option) any later version.
|
|
|
|
|
//
|
|
|
|
|
// This library is distributed in the hope that it will be useful, but
|
|
|
|
|
// WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
|
// Lesser General Public License for more details.
|
|
|
|
|
//
|
|
|
|
|
// You should have received a copy of the GNU Lesser General Public
|
|
|
|
|
// License along with this library; if not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
//
|
|
|
|
|
// ----------------------------------------------------------------------------
|
2017-12-19 03:50:57 +00:00
|
|
|
// Copyright © 2011-2018 Natalia Portillo
|
2016-10-08 04:29:04 +01:00
|
|
|
// ****************************************************************************/
|
2017-12-19 19:33:46 +00:00
|
|
|
|
2016-10-08 04:29:04 +01:00
|
|
|
using System;
|
2016-10-08 04:29:33 +01:00
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
2018-01-07 13:45:36 +00:00
|
|
|
using System.Text;
|
2016-10-08 04:29:33 +01:00
|
|
|
using System.Text.RegularExpressions;
|
2017-12-21 14:30:38 +00:00
|
|
|
using DiscImageChef.Checksums;
|
2016-10-08 04:29:33 +01:00
|
|
|
using DiscImageChef.CommonTypes;
|
|
|
|
|
using DiscImageChef.Console;
|
|
|
|
|
using DiscImageChef.Decoders.CD;
|
|
|
|
|
using DiscImageChef.Filters;
|
2018-01-28 20:29:46 +00:00
|
|
|
using Schemas;
|
2016-10-08 04:29:33 +01:00
|
|
|
|
2016-10-08 04:29:04 +01:00
|
|
|
namespace DiscImageChef.DiscImages
|
|
|
|
|
{
|
2018-01-07 13:45:36 +00:00
|
|
|
// TODO: CloneCD stores subchannel deinterleaved
|
|
|
|
|
public class CloneCd : IWritableImage
|
2016-10-08 04:29:04 +01:00
|
|
|
{
|
2018-01-01 21:32:12 +00:00
|
|
|
const string CCD_IDENTIFIER = @"^\s*\[CloneCD\]";
|
|
|
|
|
const string DISC_IDENTIFIER = @"^\s*\[Disc\]";
|
2017-12-24 20:26:23 +00:00
|
|
|
const string SESSION_IDENTIFIER = @"^\s*\[Session\s*(?<number>\d+)\]";
|
2018-01-01 21:32:12 +00:00
|
|
|
const string ENTRY_IDENTIFIER = @"^\s*\[Entry\s*(?<number>\d+)\]";
|
|
|
|
|
const string TRACK_IDENTIFIER = @"^\s*\[TRACK\s*(?<number>\d+)\]";
|
|
|
|
|
const string CDTEXT_IDENTIFIER = @"^\s*\[CDText\]";
|
|
|
|
|
const string CCD_VERSION = @"^\s*Version\s*=\s*(?<value>\d+)";
|
|
|
|
|
const string DISC_ENTRIES = @"^\s*TocEntries\s*=\s*(?<value>\d+)";
|
|
|
|
|
const string DISC_SESSIONS = @"^\s*Sessions\s*=\s*(?<value>\d+)";
|
|
|
|
|
const string DISC_SCRAMBLED = @"^\s*DataTracksScrambled\s*=\s*(?<value>\d+)";
|
|
|
|
|
const string CDTEXT_LENGTH = @"^\s*CDTextLength\s*=\s*(?<value>\d+)";
|
2018-06-23 18:18:58 +01:00
|
|
|
const string DISC_CATALOG = @"^\s*CATALOG\s*=\s*(?<value>[\x21-\x7F]{13})";
|
2018-01-01 21:32:12 +00:00
|
|
|
const string SESSION_PREGAP = @"^\s*PreGapMode\s*=\s*(?<value>\d+)";
|
2017-12-24 20:26:23 +00:00
|
|
|
const string SESSION_SUBCHANNEL = @"^\s*PreGapSubC\s*=\s*(?<value>\d+)";
|
2018-01-01 21:32:12 +00:00
|
|
|
const string ENTRY_SESSION = @"^\s*Session\s*=\s*(?<value>\d+)";
|
|
|
|
|
const string ENTRY_POINT = @"^\s*Point\s*=\s*(?<value>[\w+]+)";
|
|
|
|
|
const string ENTRY_ADR = @"^\s*ADR\s*=\s*(?<value>\w+)";
|
|
|
|
|
const string ENTRY_CONTROL = @"^\s*Control\s*=\s*(?<value>\w+)";
|
|
|
|
|
const string ENTRY_TRACKNO = @"^\s*TrackNo\s*=\s*(?<value>\d+)";
|
|
|
|
|
const string ENTRY_AMIN = @"^\s*AMin\s*=\s*(?<value>\d+)";
|
|
|
|
|
const string ENTRY_ASEC = @"^\s*ASec\s*=\s*(?<value>\d+)";
|
|
|
|
|
const string ENTRY_AFRAME = @"^\s*AFrame\s*=\s*(?<value>\d+)";
|
|
|
|
|
const string ENTRY_ALBA = @"^\s*ALBA\s*=\s*(?<value>-?\d+)";
|
|
|
|
|
const string ENTRY_ZERO = @"^\s*Zero\s*=\s*(?<value>\d+)";
|
|
|
|
|
const string ENTRY_PMIN = @"^\s*PMin\s*=\s*(?<value>\d+)";
|
|
|
|
|
const string ENTRY_PSEC = @"^\s*PSec\s*=\s*(?<value>\d+)";
|
|
|
|
|
const string ENTRY_PFRAME = @"^\s*PFrame\s*=\s*(?<value>\d+)";
|
|
|
|
|
const string ENTRY_PLBA = @"^\s*PLBA\s*=\s*(?<value>\d+)";
|
|
|
|
|
const string CDTEXT_ENTRIES = @"^\s*Entries\s*=\s*(?<value>\d+)";
|
|
|
|
|
const string CDTEXT_ENTRY = @"^\s*Entry\s*(?<number>\d+)\s*=\s*(?<value>([0-9a-fA-F]+\s*)+)";
|
|
|
|
|
string catalog; // TODO: Use it
|
|
|
|
|
|
|
|
|
|
IFilter ccdFilter;
|
|
|
|
|
byte[] cdtext;
|
|
|
|
|
StreamReader cueStream;
|
|
|
|
|
IFilter dataFilter;
|
|
|
|
|
Stream dataStream;
|
2018-01-07 13:45:36 +00:00
|
|
|
StreamWriter descriptorStream;
|
2018-01-01 21:32:12 +00:00
|
|
|
byte[] fulltoc;
|
|
|
|
|
ImageInfo imageInfo;
|
2017-12-24 00:12:31 +00:00
|
|
|
Dictionary<uint, ulong> offsetmap;
|
2018-01-01 21:32:12 +00:00
|
|
|
bool scrambled;
|
|
|
|
|
IFilter subFilter;
|
|
|
|
|
Stream subStream;
|
|
|
|
|
Dictionary<byte, byte> trackFlags;
|
2018-01-07 13:45:36 +00:00
|
|
|
string writingBaseName;
|
2017-12-19 20:33:03 +00:00
|
|
|
|
2017-12-20 17:15:26 +00:00
|
|
|
public CloneCd()
|
2017-12-19 20:33:03 +00:00
|
|
|
{
|
2017-12-26 06:05:12 +00:00
|
|
|
imageInfo = new ImageInfo
|
2017-12-22 06:55:04 +00:00
|
|
|
{
|
2018-01-01 21:32:12 +00:00
|
|
|
ReadableSectorTags = new List<SectorTagType>(),
|
|
|
|
|
ReadableMediaTags = new List<MediaTagType>(),
|
|
|
|
|
HasPartitions = true,
|
|
|
|
|
HasSessions = true,
|
|
|
|
|
Version = null,
|
|
|
|
|
ApplicationVersion = null,
|
|
|
|
|
MediaTitle = null,
|
|
|
|
|
Creator = null,
|
|
|
|
|
MediaManufacturer = null,
|
|
|
|
|
MediaModel = null,
|
|
|
|
|
MediaPartNumber = null,
|
|
|
|
|
MediaSequence = 0,
|
|
|
|
|
LastMediaSequence = 0,
|
|
|
|
|
DriveManufacturer = null,
|
|
|
|
|
DriveModel = null,
|
|
|
|
|
DriveSerialNumber = null,
|
2017-12-22 06:55:04 +00:00
|
|
|
DriveFirmwareRevision = null
|
|
|
|
|
};
|
2017-12-19 20:33:03 +00:00
|
|
|
}
|
|
|
|
|
|
2017-12-26 07:28:40 +00:00
|
|
|
public ImageInfo Info => imageInfo;
|
2017-12-26 02:51:10 +00:00
|
|
|
|
2017-12-26 07:28:40 +00:00
|
|
|
public string Name => "CloneCD";
|
2018-01-01 21:32:12 +00:00
|
|
|
public Guid Id => new Guid("EE9C2975-2E79-427A-8EE9-F86F19165784");
|
2017-12-26 02:51:10 +00:00
|
|
|
|
2017-12-28 19:56:36 +00:00
|
|
|
public string Format => "CloneCD";
|
2017-12-26 02:51:10 +00:00
|
|
|
|
2018-06-20 22:22:21 +01:00
|
|
|
public List<Partition> Partitions { get; private set; }
|
2017-12-26 02:51:10 +00:00
|
|
|
|
2018-06-20 22:22:21 +01:00
|
|
|
public List<Track> Tracks { get; private set; }
|
2017-12-26 06:05:12 +00:00
|
|
|
|
2018-06-20 22:22:21 +01:00
|
|
|
public List<Session> Sessions { get; private set; }
|
2017-12-26 06:05:12 +00:00
|
|
|
|
2017-12-28 19:56:36 +00:00
|
|
|
public bool Identify(IFilter imageFilter)
|
2017-12-19 20:33:03 +00:00
|
|
|
{
|
2017-12-22 06:55:04 +00:00
|
|
|
ccdFilter = imageFilter;
|
2017-12-19 20:33:03 +00:00
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
imageFilter.GetDataForkStream().Seek(0, SeekOrigin.Begin);
|
|
|
|
|
byte[] testArray = new byte[512];
|
|
|
|
|
imageFilter.GetDataForkStream().Read(testArray, 0, 512);
|
|
|
|
|
imageFilter.GetDataForkStream().Seek(0, SeekOrigin.Begin);
|
|
|
|
|
// Check for unexpected control characters that shouldn't be present in a text file and can crash this plugin
|
|
|
|
|
bool twoConsecutiveNulls = false;
|
|
|
|
|
for(int i = 0; i < 512; i++)
|
|
|
|
|
{
|
|
|
|
|
if(i >= imageFilter.GetDataForkStream().Length) break;
|
|
|
|
|
|
|
|
|
|
if(testArray[i] == 0)
|
|
|
|
|
{
|
|
|
|
|
if(twoConsecutiveNulls) return false;
|
|
|
|
|
|
|
|
|
|
twoConsecutiveNulls = true;
|
|
|
|
|
}
|
|
|
|
|
else twoConsecutiveNulls = false;
|
|
|
|
|
|
|
|
|
|
if(testArray[i] < 0x20 && testArray[i] != 0x0A && testArray[i] != 0x0D && testArray[i] != 0x00)
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-22 06:55:04 +00:00
|
|
|
cueStream = new StreamReader(ccdFilter.GetDataForkStream());
|
2017-12-19 20:33:03 +00:00
|
|
|
|
2017-12-22 06:55:04 +00:00
|
|
|
string line = cueStream.ReadLine();
|
2017-12-19 20:33:03 +00:00
|
|
|
|
2017-12-20 17:15:26 +00:00
|
|
|
Regex hdr = new Regex(CCD_IDENTIFIER);
|
2017-12-19 20:33:03 +00:00
|
|
|
|
2017-12-22 06:55:04 +00:00
|
|
|
Match hdm = hdr.Match(line ?? throw new InvalidOperationException());
|
2017-12-19 20:33:03 +00:00
|
|
|
|
2017-12-20 17:15:26 +00:00
|
|
|
return hdm.Success;
|
2017-12-19 20:33:03 +00:00
|
|
|
}
|
|
|
|
|
catch(Exception ex)
|
|
|
|
|
{
|
2017-12-22 06:55:04 +00:00
|
|
|
DicConsole.ErrorWriteLine("Exception trying to identify image file {0}", ccdFilter);
|
2018-01-01 21:32:12 +00:00
|
|
|
DicConsole.ErrorWriteLine("Exception: {0}", ex.Message);
|
|
|
|
|
DicConsole.ErrorWriteLine("Stack trace: {0}", ex.StackTrace);
|
2017-12-19 20:33:03 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-28 19:56:36 +00:00
|
|
|
public bool Open(IFilter imageFilter)
|
2017-12-19 20:33:03 +00:00
|
|
|
{
|
|
|
|
|
if(imageFilter == null) return false;
|
|
|
|
|
|
2017-12-22 06:55:04 +00:00
|
|
|
ccdFilter = imageFilter;
|
2017-12-19 20:33:03 +00:00
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
imageFilter.GetDataForkStream().Seek(0, SeekOrigin.Begin);
|
2018-06-22 08:08:38 +01:00
|
|
|
cueStream = new StreamReader(imageFilter.GetDataForkStream());
|
2017-12-22 06:55:04 +00:00
|
|
|
int lineNumber = 0;
|
2017-12-19 20:33:03 +00:00
|
|
|
|
2018-01-01 21:32:12 +00:00
|
|
|
Regex ccdIdRegex = new Regex(CCD_IDENTIFIER);
|
|
|
|
|
Regex discIdRegex = new Regex(DISC_IDENTIFIER);
|
|
|
|
|
Regex sessIdRegex = new Regex(SESSION_IDENTIFIER);
|
|
|
|
|
Regex entryIdRegex = new Regex(ENTRY_IDENTIFIER);
|
|
|
|
|
Regex trackIdRegex = new Regex(TRACK_IDENTIFIER);
|
|
|
|
|
Regex cdtIdRegex = new Regex(CDTEXT_IDENTIFIER);
|
|
|
|
|
Regex ccdVerRegex = new Regex(CCD_VERSION);
|
|
|
|
|
Regex discEntRegex = new Regex(DISC_ENTRIES);
|
|
|
|
|
Regex discSessRegex = new Regex(DISC_SESSIONS);
|
|
|
|
|
Regex discScrRegex = new Regex(DISC_SCRAMBLED);
|
|
|
|
|
Regex cdtLenRegex = new Regex(CDTEXT_LENGTH);
|
|
|
|
|
Regex discCatRegex = new Regex(DISC_CATALOG);
|
|
|
|
|
Regex sessPregRegex = new Regex(SESSION_PREGAP);
|
|
|
|
|
Regex sessSubcRegex = new Regex(SESSION_SUBCHANNEL);
|
|
|
|
|
Regex entSessRegex = new Regex(ENTRY_SESSION);
|
|
|
|
|
Regex entPointRegex = new Regex(ENTRY_POINT);
|
|
|
|
|
Regex entAdrRegex = new Regex(ENTRY_ADR);
|
|
|
|
|
Regex entCtrlRegex = new Regex(ENTRY_CONTROL);
|
|
|
|
|
Regex entTnoRegex = new Regex(ENTRY_TRACKNO);
|
|
|
|
|
Regex entAMinRegex = new Regex(ENTRY_AMIN);
|
|
|
|
|
Regex entASecRegex = new Regex(ENTRY_ASEC);
|
2017-12-20 17:15:26 +00:00
|
|
|
Regex entAFrameRegex = new Regex(ENTRY_AFRAME);
|
2018-01-01 21:32:12 +00:00
|
|
|
Regex entAlbaRegex = new Regex(ENTRY_ALBA);
|
|
|
|
|
Regex entZeroRegex = new Regex(ENTRY_ZERO);
|
|
|
|
|
Regex entPMinRegex = new Regex(ENTRY_PMIN);
|
|
|
|
|
Regex entPSecRegex = new Regex(ENTRY_PSEC);
|
2017-12-20 17:15:26 +00:00
|
|
|
Regex entPFrameRegex = new Regex(ENTRY_PFRAME);
|
2018-01-01 21:32:12 +00:00
|
|
|
Regex entPlbaRegex = new Regex(ENTRY_PLBA);
|
|
|
|
|
Regex cdtEntsRegex = new Regex(CDTEXT_ENTRIES);
|
|
|
|
|
Regex cdtEntRegex = new Regex(CDTEXT_ENTRY);
|
|
|
|
|
|
|
|
|
|
bool inCcd = false;
|
|
|
|
|
bool inDisk = false;
|
|
|
|
|
bool inSession = false;
|
|
|
|
|
bool inEntry = false;
|
|
|
|
|
bool inTrack = false;
|
|
|
|
|
bool inCdText = false;
|
|
|
|
|
MemoryStream cdtMs = new MemoryStream();
|
|
|
|
|
int minSession = int.MaxValue;
|
|
|
|
|
int maxSession = int.MinValue;
|
|
|
|
|
FullTOC.TrackDataDescriptor currentEntry = new FullTOC.TrackDataDescriptor();
|
|
|
|
|
List<FullTOC.TrackDataDescriptor> entries = new List<FullTOC.TrackDataDescriptor>();
|
2018-06-22 08:08:38 +01:00
|
|
|
scrambled = false;
|
|
|
|
|
catalog = null;
|
2017-12-19 20:33:03 +00:00
|
|
|
|
|
|
|
|
while(cueStream.Peek() >= 0)
|
|
|
|
|
{
|
2017-12-22 06:55:04 +00:00
|
|
|
lineNumber++;
|
|
|
|
|
string line = cueStream.ReadLine();
|
2017-12-19 20:33:03 +00:00
|
|
|
|
2018-01-01 21:32:12 +00:00
|
|
|
Match ccdIdMatch = ccdIdRegex.Match(line);
|
|
|
|
|
Match discIdMatch = discIdRegex.Match(line);
|
|
|
|
|
Match sessIdMatch = sessIdRegex.Match(line);
|
2017-12-22 06:55:04 +00:00
|
|
|
Match entryIdMatch = entryIdRegex.Match(line);
|
|
|
|
|
Match trackIdMatch = trackIdRegex.Match(line);
|
2018-01-01 21:32:12 +00:00
|
|
|
Match cdtIdMatch = cdtIdRegex.Match(line);
|
2017-12-19 20:33:03 +00:00
|
|
|
|
|
|
|
|
// [CloneCD]
|
2017-12-20 17:15:26 +00:00
|
|
|
if(ccdIdMatch.Success)
|
2017-12-19 20:33:03 +00:00
|
|
|
{
|
2017-12-20 17:15:26 +00:00
|
|
|
if(inDisk || inSession || inEntry || inTrack || inCdText)
|
2017-12-19 20:33:03 +00:00
|
|
|
throw new
|
2017-12-22 06:55:04 +00:00
|
|
|
FeatureUnsupportedImageException($"Found [CloneCD] out of order in line {lineNumber}");
|
2017-12-19 20:33:03 +00:00
|
|
|
|
2018-01-01 21:32:12 +00:00
|
|
|
inCcd = true;
|
|
|
|
|
inDisk = false;
|
2017-12-19 20:33:03 +00:00
|
|
|
inSession = false;
|
2018-01-01 21:32:12 +00:00
|
|
|
inEntry = false;
|
|
|
|
|
inTrack = false;
|
|
|
|
|
inCdText = false;
|
2017-12-19 20:33:03 +00:00
|
|
|
}
|
2018-01-01 21:32:12 +00:00
|
|
|
else if(discIdMatch.Success || sessIdMatch.Success || entryIdMatch.Success ||
|
2017-12-20 17:15:26 +00:00
|
|
|
trackIdMatch.Success || cdtIdMatch.Success)
|
2017-12-19 20:33:03 +00:00
|
|
|
{
|
|
|
|
|
if(inEntry)
|
|
|
|
|
{
|
|
|
|
|
entries.Add(currentEntry);
|
|
|
|
|
currentEntry = new FullTOC.TrackDataDescriptor();
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-01 21:32:12 +00:00
|
|
|
inCcd = false;
|
|
|
|
|
inDisk = discIdMatch.Success;
|
2017-12-20 17:15:26 +00:00
|
|
|
inSession = sessIdMatch.Success;
|
2018-01-01 21:32:12 +00:00
|
|
|
inEntry = entryIdMatch.Success;
|
|
|
|
|
inTrack = trackIdMatch.Success;
|
|
|
|
|
inCdText = cdtIdMatch.Success;
|
2017-12-19 20:33:03 +00:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if(inCcd)
|
|
|
|
|
{
|
2017-12-22 06:55:04 +00:00
|
|
|
Match ccdVerMatch = ccdVerRegex.Match(line);
|
2017-12-19 20:33:03 +00:00
|
|
|
|
2017-12-21 06:06:19 +00:00
|
|
|
if(!ccdVerMatch.Success) continue;
|
2017-12-19 20:33:03 +00:00
|
|
|
|
2017-12-22 06:55:04 +00:00
|
|
|
DicConsole.DebugWriteLine("CloneCD plugin", "Found Version at line {0}", lineNumber);
|
2017-12-21 06:06:19 +00:00
|
|
|
|
2017-12-26 06:05:12 +00:00
|
|
|
imageInfo.Version = ccdVerMatch.Groups["value"].Value;
|
|
|
|
|
if(imageInfo.Version != "2" && imageInfo.Version != "3")
|
2017-12-21 06:06:19 +00:00
|
|
|
DicConsole
|
2018-01-01 21:32:12 +00:00
|
|
|
.ErrorWriteLine("(CloneCD plugin): Warning! Unknown CCD image version {0}, may not work!",
|
|
|
|
|
imageInfo.Version);
|
2017-12-19 20:33:03 +00:00
|
|
|
}
|
|
|
|
|
else if(inDisk)
|
|
|
|
|
{
|
2018-01-01 21:32:12 +00:00
|
|
|
Match discEntMatch = discEntRegex.Match(line);
|
2017-12-22 06:55:04 +00:00
|
|
|
Match discSessMatch = discSessRegex.Match(line);
|
2018-01-01 21:32:12 +00:00
|
|
|
Match discScrMatch = discScrRegex.Match(line);
|
|
|
|
|
Match cdtLenMatch = cdtLenRegex.Match(line);
|
|
|
|
|
Match discCatMatch = discCatRegex.Match(line);
|
2017-12-22 06:55:04 +00:00
|
|
|
|
2017-12-24 00:12:31 +00:00
|
|
|
if(discEntMatch.Success)
|
|
|
|
|
DicConsole.DebugWriteLine("CloneCD plugin", "Found TocEntries at line {0}", lineNumber);
|
|
|
|
|
else if(discSessMatch.Success)
|
|
|
|
|
DicConsole.DebugWriteLine("CloneCD plugin", "Found Sessions at line {0}", lineNumber);
|
2017-12-20 17:15:26 +00:00
|
|
|
else if(discScrMatch.Success)
|
2017-12-19 20:33:03 +00:00
|
|
|
{
|
|
|
|
|
DicConsole.DebugWriteLine("CloneCD plugin", "Found DataTracksScrambled at line {0}",
|
2017-12-22 06:55:04 +00:00
|
|
|
lineNumber);
|
2017-12-20 17:15:26 +00:00
|
|
|
scrambled |= discScrMatch.Groups["value"].Value == "1";
|
2017-12-19 20:33:03 +00:00
|
|
|
}
|
2017-12-24 00:12:31 +00:00
|
|
|
else if(cdtLenMatch.Success)
|
|
|
|
|
DicConsole.DebugWriteLine("CloneCD plugin", "Found CDTextLength at line {0}",
|
|
|
|
|
lineNumber);
|
2017-12-20 17:15:26 +00:00
|
|
|
else if(discCatMatch.Success)
|
2017-12-19 20:33:03 +00:00
|
|
|
{
|
2017-12-22 06:55:04 +00:00
|
|
|
DicConsole.DebugWriteLine("CloneCD plugin", "Found Catalog at line {0}", lineNumber);
|
2017-12-20 17:15:26 +00:00
|
|
|
catalog = discCatMatch.Groups["value"].Value;
|
2017-12-19 20:33:03 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// TODO: Do not suppose here entries come sorted
|
2017-12-20 17:15:26 +00:00
|
|
|
else if(inCdText)
|
2017-12-19 20:33:03 +00:00
|
|
|
{
|
2017-12-22 06:55:04 +00:00
|
|
|
Match cdtEntsMatch = cdtEntsRegex.Match(line);
|
2018-01-01 21:32:12 +00:00
|
|
|
Match cdtEntMatch = cdtEntRegex.Match(line);
|
2017-12-19 20:33:03 +00:00
|
|
|
|
2017-12-24 00:12:31 +00:00
|
|
|
if(cdtEntsMatch.Success)
|
|
|
|
|
DicConsole.DebugWriteLine("CloneCD plugin", "Found CD-Text Entries at line {0}",
|
|
|
|
|
lineNumber);
|
2017-12-20 17:15:26 +00:00
|
|
|
else if(cdtEntMatch.Success)
|
2017-12-19 20:33:03 +00:00
|
|
|
{
|
2017-12-24 00:12:31 +00:00
|
|
|
DicConsole.DebugWriteLine("CloneCD plugin", "Found CD-Text Entry at line {0}",
|
|
|
|
|
lineNumber);
|
2017-12-21 14:30:38 +00:00
|
|
|
string[] bytes = cdtEntMatch.Groups["value"].Value.Split(new[] {' '},
|
2017-12-24 00:12:31 +00:00
|
|
|
StringSplitOptions
|
2018-01-01 21:32:12 +00:00
|
|
|
.RemoveEmptyEntries);
|
2017-12-19 20:33:03 +00:00
|
|
|
foreach(string byt in bytes) cdtMs.WriteByte(Convert.ToByte(byt, 16));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// Is this useful?
|
|
|
|
|
else if(inSession)
|
|
|
|
|
{
|
2017-12-22 06:55:04 +00:00
|
|
|
Match sessPregMatch = sessPregRegex.Match(line);
|
|
|
|
|
Match sessSubcMatch = sessSubcRegex.Match(line);
|
2017-12-19 20:33:03 +00:00
|
|
|
|
2017-12-24 00:12:31 +00:00
|
|
|
if(sessPregMatch.Success)
|
|
|
|
|
DicConsole.DebugWriteLine("CloneCD plugin", "Found PreGapMode at line {0}", lineNumber);
|
|
|
|
|
else if(sessSubcMatch.Success)
|
|
|
|
|
DicConsole.DebugWriteLine("CloneCD plugin", "Found PreGapSubC at line {0}", lineNumber);
|
2017-12-19 20:33:03 +00:00
|
|
|
}
|
|
|
|
|
else if(inEntry)
|
|
|
|
|
{
|
2018-01-01 21:32:12 +00:00
|
|
|
Match entSessMatch = entSessRegex.Match(line);
|
|
|
|
|
Match entPointMatch = entPointRegex.Match(line);
|
|
|
|
|
Match entAdrMatch = entAdrRegex.Match(line);
|
|
|
|
|
Match entCtrlMatch = entCtrlRegex.Match(line);
|
|
|
|
|
Match entTnoMatch = entTnoRegex.Match(line);
|
|
|
|
|
Match entAMinMatch = entAMinRegex.Match(line);
|
|
|
|
|
Match entASecMatch = entASecRegex.Match(line);
|
2017-12-22 06:55:04 +00:00
|
|
|
Match entAFrameMatch = entAFrameRegex.Match(line);
|
2018-01-01 21:32:12 +00:00
|
|
|
Match entAlbaMatch = entAlbaRegex.Match(line);
|
|
|
|
|
Match entZeroMatch = entZeroRegex.Match(line);
|
|
|
|
|
Match entPMinMatch = entPMinRegex.Match(line);
|
|
|
|
|
Match entPSecMatch = entPSecRegex.Match(line);
|
2017-12-22 06:55:04 +00:00
|
|
|
Match entPFrameMatch = entPFrameRegex.Match(line);
|
2018-01-01 21:32:12 +00:00
|
|
|
Match entPlbaMatch = entPlbaRegex.Match(line);
|
2017-12-20 17:15:26 +00:00
|
|
|
|
|
|
|
|
if(entSessMatch.Success)
|
2017-12-19 20:33:03 +00:00
|
|
|
{
|
2017-12-22 06:55:04 +00:00
|
|
|
DicConsole.DebugWriteLine("CloneCD plugin", "Found Session at line {0}", lineNumber);
|
2018-06-22 08:08:38 +01:00
|
|
|
currentEntry.SessionNumber = Convert.ToByte(entSessMatch.Groups["value"].Value, 10);
|
2017-12-19 20:33:03 +00:00
|
|
|
if(currentEntry.SessionNumber < minSession) minSession = currentEntry.SessionNumber;
|
|
|
|
|
if(currentEntry.SessionNumber > maxSession) maxSession = currentEntry.SessionNumber;
|
|
|
|
|
}
|
2017-12-20 17:15:26 +00:00
|
|
|
else if(entPointMatch.Success)
|
2017-12-19 20:33:03 +00:00
|
|
|
{
|
2017-12-22 06:55:04 +00:00
|
|
|
DicConsole.DebugWriteLine("CloneCD plugin", "Found Point at line {0}", lineNumber);
|
2017-12-20 17:15:26 +00:00
|
|
|
currentEntry.POINT = Convert.ToByte(entPointMatch.Groups["value"].Value, 16);
|
2017-12-19 20:33:03 +00:00
|
|
|
}
|
2017-12-20 17:15:26 +00:00
|
|
|
else if(entAdrMatch.Success)
|
2017-12-19 20:33:03 +00:00
|
|
|
{
|
2017-12-22 06:55:04 +00:00
|
|
|
DicConsole.DebugWriteLine("CloneCD plugin", "Found ADR at line {0}", lineNumber);
|
2017-12-20 17:15:26 +00:00
|
|
|
currentEntry.ADR = Convert.ToByte(entAdrMatch.Groups["value"].Value, 16);
|
2017-12-19 20:33:03 +00:00
|
|
|
}
|
2017-12-20 17:15:26 +00:00
|
|
|
else if(entCtrlMatch.Success)
|
2017-12-19 20:33:03 +00:00
|
|
|
{
|
2017-12-22 06:55:04 +00:00
|
|
|
DicConsole.DebugWriteLine("CloneCD plugin", "Found Control at line {0}", lineNumber);
|
2017-12-20 17:15:26 +00:00
|
|
|
currentEntry.CONTROL = Convert.ToByte(entCtrlMatch.Groups["value"].Value, 16);
|
2017-12-19 20:33:03 +00:00
|
|
|
}
|
2017-12-20 17:15:26 +00:00
|
|
|
else if(entTnoMatch.Success)
|
2017-12-19 20:33:03 +00:00
|
|
|
{
|
2017-12-22 06:55:04 +00:00
|
|
|
DicConsole.DebugWriteLine("CloneCD plugin", "Found TrackNo at line {0}", lineNumber);
|
2017-12-20 17:15:26 +00:00
|
|
|
currentEntry.TNO = Convert.ToByte(entTnoMatch.Groups["value"].Value, 10);
|
2017-12-19 20:33:03 +00:00
|
|
|
}
|
2017-12-20 17:15:26 +00:00
|
|
|
else if(entAMinMatch.Success)
|
2017-12-19 20:33:03 +00:00
|
|
|
{
|
2017-12-22 06:55:04 +00:00
|
|
|
DicConsole.DebugWriteLine("CloneCD plugin", "Found AMin at line {0}", lineNumber);
|
2017-12-20 17:15:26 +00:00
|
|
|
currentEntry.Min = Convert.ToByte(entAMinMatch.Groups["value"].Value, 10);
|
2017-12-19 20:33:03 +00:00
|
|
|
}
|
2017-12-20 17:15:26 +00:00
|
|
|
else if(entASecMatch.Success)
|
2017-12-19 20:33:03 +00:00
|
|
|
{
|
2017-12-22 06:55:04 +00:00
|
|
|
DicConsole.DebugWriteLine("CloneCD plugin", "Found ASec at line {0}", lineNumber);
|
2017-12-20 17:15:26 +00:00
|
|
|
currentEntry.Sec = Convert.ToByte(entASecMatch.Groups["value"].Value, 10);
|
2017-12-19 20:33:03 +00:00
|
|
|
}
|
2017-12-20 17:15:26 +00:00
|
|
|
else if(entAFrameMatch.Success)
|
2017-12-19 20:33:03 +00:00
|
|
|
{
|
2017-12-22 06:55:04 +00:00
|
|
|
DicConsole.DebugWriteLine("CloneCD plugin", "Found AFrame at line {0}", lineNumber);
|
2017-12-20 17:15:26 +00:00
|
|
|
currentEntry.Frame = Convert.ToByte(entAFrameMatch.Groups["value"].Value, 10);
|
2017-12-19 20:33:03 +00:00
|
|
|
}
|
2017-12-24 00:12:31 +00:00
|
|
|
else if(entAlbaMatch.Success)
|
|
|
|
|
DicConsole.DebugWriteLine("CloneCD plugin", "Found ALBA at line {0}", lineNumber);
|
2017-12-20 17:15:26 +00:00
|
|
|
else if(entZeroMatch.Success)
|
2017-12-19 20:33:03 +00:00
|
|
|
{
|
2017-12-22 06:55:04 +00:00
|
|
|
DicConsole.DebugWriteLine("CloneCD plugin", "Found Zero at line {0}", lineNumber);
|
2018-01-01 21:32:12 +00:00
|
|
|
currentEntry.Zero = Convert.ToByte(entZeroMatch.Groups["value"].Value, 10);
|
|
|
|
|
currentEntry.HOUR = (byte)((currentEntry.Zero & 0xF0) >> 4);
|
2018-06-22 08:08:38 +01:00
|
|
|
currentEntry.PHOUR = (byte)(currentEntry.Zero & 0x0F);
|
2017-12-19 20:33:03 +00:00
|
|
|
}
|
2017-12-20 17:15:26 +00:00
|
|
|
else if(entPMinMatch.Success)
|
2017-12-19 20:33:03 +00:00
|
|
|
{
|
2017-12-22 06:55:04 +00:00
|
|
|
DicConsole.DebugWriteLine("CloneCD plugin", "Found PMin at line {0}", lineNumber);
|
2017-12-20 17:15:26 +00:00
|
|
|
currentEntry.PMIN = Convert.ToByte(entPMinMatch.Groups["value"].Value, 10);
|
2017-12-19 20:33:03 +00:00
|
|
|
}
|
2017-12-20 17:15:26 +00:00
|
|
|
else if(entPSecMatch.Success)
|
2017-12-19 20:33:03 +00:00
|
|
|
{
|
2017-12-22 06:55:04 +00:00
|
|
|
DicConsole.DebugWriteLine("CloneCD plugin", "Found PSec at line {0}", lineNumber);
|
2017-12-20 17:15:26 +00:00
|
|
|
currentEntry.PSEC = Convert.ToByte(entPSecMatch.Groups["value"].Value, 10);
|
2017-12-19 20:33:03 +00:00
|
|
|
}
|
2017-12-20 17:15:26 +00:00
|
|
|
else if(entPFrameMatch.Success)
|
2017-12-19 20:33:03 +00:00
|
|
|
{
|
2017-12-22 06:55:04 +00:00
|
|
|
DicConsole.DebugWriteLine("CloneCD plugin", "Found PFrame at line {0}", lineNumber);
|
2017-12-20 17:15:26 +00:00
|
|
|
currentEntry.PFRAME = Convert.ToByte(entPFrameMatch.Groups["value"].Value, 10);
|
2017-12-19 20:33:03 +00:00
|
|
|
}
|
2017-12-24 00:12:31 +00:00
|
|
|
else if(entPlbaMatch.Success)
|
|
|
|
|
DicConsole.DebugWriteLine("CloneCD plugin", "Found PLBA at line {0}", lineNumber);
|
2017-12-19 20:33:03 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(inEntry) entries.Add(currentEntry);
|
|
|
|
|
|
|
|
|
|
if(entries.Count == 0) throw new FeatureUnsupportedImageException("Did not find any track.");
|
|
|
|
|
|
|
|
|
|
FullTOC.CDFullTOC toc;
|
2018-01-01 21:32:12 +00:00
|
|
|
toc.TrackDescriptors = entries.ToArray();
|
|
|
|
|
toc.LastCompleteSession = (byte)maxSession;
|
2017-12-19 20:33:03 +00:00
|
|
|
toc.FirstCompleteSession = (byte)minSession;
|
2018-01-01 21:32:12 +00:00
|
|
|
toc.DataLength = (ushort)(entries.Count * 11 + 2);
|
2018-06-22 08:08:38 +01:00
|
|
|
MemoryStream tocMs = new MemoryStream();
|
2017-12-19 20:33:03 +00:00
|
|
|
tocMs.Write(BigEndianBitConverter.GetBytes(toc.DataLength), 0, 2);
|
|
|
|
|
tocMs.WriteByte(toc.FirstCompleteSession);
|
|
|
|
|
tocMs.WriteByte(toc.LastCompleteSession);
|
|
|
|
|
foreach(FullTOC.TrackDataDescriptor descriptor in toc.TrackDescriptors)
|
|
|
|
|
{
|
|
|
|
|
tocMs.WriteByte(descriptor.SessionNumber);
|
|
|
|
|
tocMs.WriteByte((byte)((descriptor.ADR << 4) + descriptor.CONTROL));
|
|
|
|
|
tocMs.WriteByte(descriptor.TNO);
|
|
|
|
|
tocMs.WriteByte(descriptor.POINT);
|
|
|
|
|
tocMs.WriteByte(descriptor.Min);
|
|
|
|
|
tocMs.WriteByte(descriptor.Sec);
|
|
|
|
|
tocMs.WriteByte(descriptor.Frame);
|
|
|
|
|
tocMs.WriteByte(descriptor.Zero);
|
|
|
|
|
tocMs.WriteByte(descriptor.PMIN);
|
|
|
|
|
tocMs.WriteByte(descriptor.PSEC);
|
|
|
|
|
tocMs.WriteByte(descriptor.PFRAME);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fulltoc = tocMs.ToArray();
|
2017-12-26 06:05:12 +00:00
|
|
|
imageInfo.ReadableMediaTags.Add(MediaTagType.CD_FullTOC);
|
2017-12-19 20:33:03 +00:00
|
|
|
|
|
|
|
|
DicConsole.DebugWriteLine("CloneCD plugin", "{0}", FullTOC.Prettify(toc));
|
|
|
|
|
|
|
|
|
|
string dataFile = Path.GetFileNameWithoutExtension(imageFilter.GetBasePath()) + ".img";
|
2018-01-01 21:32:12 +00:00
|
|
|
string subFile = Path.GetFileNameWithoutExtension(imageFilter.GetBasePath()) + ".sub";
|
2017-12-19 20:33:03 +00:00
|
|
|
|
|
|
|
|
FiltersList filtersList = new FiltersList();
|
2018-06-22 08:08:38 +01:00
|
|
|
dataFilter = filtersList.GetFilter(dataFile);
|
2017-12-19 20:33:03 +00:00
|
|
|
|
|
|
|
|
if(dataFilter == null) throw new Exception("Cannot open data file");
|
|
|
|
|
|
|
|
|
|
filtersList = new FiltersList();
|
2018-01-01 21:32:12 +00:00
|
|
|
subFilter = filtersList.GetFilter(subFile);
|
2017-12-19 20:33:03 +00:00
|
|
|
|
2018-01-01 21:32:12 +00:00
|
|
|
int curSessionNo = 0;
|
|
|
|
|
Track currentTrack = new Track();
|
|
|
|
|
bool firstTrackInSession = true;
|
2018-06-22 08:08:38 +01:00
|
|
|
Tracks = new List<Track>();
|
|
|
|
|
ulong leadOutStart = 0;
|
2017-12-19 20:33:03 +00:00
|
|
|
|
2018-06-22 08:08:38 +01:00
|
|
|
dataStream = dataFilter.GetDataForkStream();
|
2017-12-19 20:33:03 +00:00
|
|
|
if(subFilter != null) subStream = subFilter.GetDataForkStream();
|
2018-06-22 08:08:38 +01:00
|
|
|
trackFlags = new Dictionary<byte, byte>();
|
2017-12-19 20:33:03 +00:00
|
|
|
|
|
|
|
|
foreach(FullTOC.TrackDataDescriptor descriptor in entries)
|
|
|
|
|
{
|
|
|
|
|
if(descriptor.SessionNumber > curSessionNo)
|
|
|
|
|
{
|
|
|
|
|
curSessionNo = descriptor.SessionNumber;
|
|
|
|
|
if(!firstTrackInSession)
|
|
|
|
|
{
|
2017-12-20 17:15:26 +00:00
|
|
|
currentTrack.TrackEndSector = leadOutStart - 1;
|
2018-01-01 21:32:12 +00:00
|
|
|
Tracks.Add(currentTrack);
|
2017-12-19 20:33:03 +00:00
|
|
|
}
|
2018-01-01 21:32:12 +00:00
|
|
|
|
2017-12-19 20:33:03 +00:00
|
|
|
firstTrackInSession = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch(descriptor.ADR)
|
|
|
|
|
{
|
|
|
|
|
case 1:
|
|
|
|
|
case 4:
|
|
|
|
|
switch(descriptor.POINT)
|
|
|
|
|
{
|
|
|
|
|
case 0xA0:
|
2017-12-22 06:55:04 +00:00
|
|
|
byte discType = descriptor.PSEC;
|
2017-12-19 20:33:03 +00:00
|
|
|
DicConsole.DebugWriteLine("CloneCD plugin", "Disc Type: {0}", discType);
|
|
|
|
|
break;
|
|
|
|
|
case 0xA2:
|
2017-12-20 17:15:26 +00:00
|
|
|
leadOutStart = GetLba(descriptor.PHOUR, descriptor.PMIN, descriptor.PSEC,
|
2017-12-19 20:33:03 +00:00
|
|
|
descriptor.PFRAME);
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
if(descriptor.POINT >= 0x01 && descriptor.POINT <= 0x63)
|
|
|
|
|
{
|
|
|
|
|
if(!firstTrackInSession)
|
|
|
|
|
{
|
|
|
|
|
currentTrack.TrackEndSector =
|
2017-12-20 17:15:26 +00:00
|
|
|
GetLba(descriptor.PHOUR, descriptor.PMIN, descriptor.PSEC,
|
2017-12-19 20:33:03 +00:00
|
|
|
descriptor.PFRAME) - 1;
|
2018-01-01 21:32:12 +00:00
|
|
|
Tracks.Add(currentTrack);
|
2017-12-19 20:33:03 +00:00
|
|
|
}
|
|
|
|
|
else firstTrackInSession = false;
|
|
|
|
|
|
2017-12-22 06:55:04 +00:00
|
|
|
currentTrack = new Track
|
|
|
|
|
{
|
2018-01-01 21:32:12 +00:00
|
|
|
TrackBytesPerSector = 2352,
|
|
|
|
|
TrackFile = dataFilter.GetFilename(),
|
|
|
|
|
TrackFileType = scrambled ? "SCRAMBLED" : "BINARY",
|
|
|
|
|
TrackFilter = dataFilter,
|
2017-12-22 06:55:04 +00:00
|
|
|
TrackRawBytesPerSector = 2352,
|
2018-01-01 21:32:12 +00:00
|
|
|
TrackSequence = descriptor.POINT,
|
2018-06-22 08:08:38 +01:00
|
|
|
TrackStartSector =
|
2017-12-22 06:55:04 +00:00
|
|
|
GetLba(descriptor.PHOUR, descriptor.PMIN, descriptor.PSEC,
|
|
|
|
|
descriptor.PFRAME),
|
|
|
|
|
TrackSession = descriptor.SessionNumber
|
|
|
|
|
};
|
2017-12-19 20:33:03 +00:00
|
|
|
currentTrack.TrackFileOffset = currentTrack.TrackStartSector * 2352;
|
|
|
|
|
|
|
|
|
|
// Need to check exact data type later
|
2017-12-22 02:04:18 +00:00
|
|
|
if((TocControl)(descriptor.CONTROL & 0x0D) == TocControl.DataTrack ||
|
|
|
|
|
(TocControl)(descriptor.CONTROL & 0x0D) == TocControl.DataTrackIncremental)
|
2018-01-01 21:32:12 +00:00
|
|
|
currentTrack.TrackType = TrackType.Data;
|
2017-12-19 20:33:03 +00:00
|
|
|
else currentTrack.TrackType = TrackType.Audio;
|
|
|
|
|
|
2018-01-01 21:32:12 +00:00
|
|
|
if(!trackFlags.ContainsKey(descriptor.POINT))
|
|
|
|
|
trackFlags.Add(descriptor.POINT, descriptor.CONTROL);
|
|
|
|
|
|
2017-12-19 20:33:03 +00:00
|
|
|
if(subFilter != null)
|
|
|
|
|
{
|
2018-01-01 21:32:12 +00:00
|
|
|
currentTrack.TrackSubchannelFile = subFilter.GetFilename();
|
2017-12-19 20:33:03 +00:00
|
|
|
currentTrack.TrackSubchannelFilter = subFilter;
|
|
|
|
|
currentTrack.TrackSubchannelOffset = currentTrack.TrackStartSector * 96;
|
2018-01-01 21:32:12 +00:00
|
|
|
currentTrack.TrackSubchannelType = TrackSubchannelType.Raw;
|
2017-12-19 20:33:03 +00:00
|
|
|
}
|
|
|
|
|
else currentTrack.TrackSubchannelType = TrackSubchannelType.None;
|
|
|
|
|
|
|
|
|
|
if(currentTrack.TrackType == TrackType.Data)
|
|
|
|
|
{
|
|
|
|
|
byte[] syncTest = new byte[12];
|
|
|
|
|
byte[] sectTest = new byte[2352];
|
|
|
|
|
dataStream.Seek((long)currentTrack.TrackFileOffset, SeekOrigin.Begin);
|
|
|
|
|
dataStream.Read(sectTest, 0, 2352);
|
|
|
|
|
Array.Copy(sectTest, 0, syncTest, 0, 12);
|
|
|
|
|
|
|
|
|
|
if(Sector.SyncMark.SequenceEqual(syncTest))
|
|
|
|
|
{
|
|
|
|
|
if(scrambled) sectTest = Sector.Scramble(sectTest);
|
|
|
|
|
|
|
|
|
|
if(sectTest[15] == 1)
|
|
|
|
|
{
|
|
|
|
|
currentTrack.TrackBytesPerSector = 2048;
|
2018-01-01 21:32:12 +00:00
|
|
|
currentTrack.TrackType = TrackType.CdMode1;
|
2017-12-26 06:05:12 +00:00
|
|
|
if(!imageInfo.ReadableSectorTags
|
2017-12-20 17:15:26 +00:00
|
|
|
.Contains(SectorTagType.CdSectorSync))
|
2017-12-26 06:05:12 +00:00
|
|
|
imageInfo.ReadableSectorTags.Add(SectorTagType.CdSectorSync);
|
|
|
|
|
if(!imageInfo.ReadableSectorTags.Contains(SectorTagType
|
2018-01-01 21:32:12 +00:00
|
|
|
.CdSectorHeader))
|
2017-12-26 06:05:12 +00:00
|
|
|
imageInfo.ReadableSectorTags.Add(SectorTagType.CdSectorHeader);
|
|
|
|
|
if(!imageInfo.ReadableSectorTags.Contains(SectorTagType.CdSectorEcc)
|
|
|
|
|
) imageInfo.ReadableSectorTags.Add(SectorTagType.CdSectorEcc);
|
|
|
|
|
if(!imageInfo.ReadableSectorTags
|
2017-12-24 00:12:31 +00:00
|
|
|
.Contains(SectorTagType.CdSectorEccP))
|
2017-12-26 06:05:12 +00:00
|
|
|
imageInfo.ReadableSectorTags.Add(SectorTagType.CdSectorEccP);
|
|
|
|
|
if(!imageInfo.ReadableSectorTags
|
2017-12-24 00:12:31 +00:00
|
|
|
.Contains(SectorTagType.CdSectorEccQ))
|
2017-12-26 06:05:12 +00:00
|
|
|
imageInfo.ReadableSectorTags.Add(SectorTagType.CdSectorEccQ);
|
|
|
|
|
if(!imageInfo.ReadableSectorTags.Contains(SectorTagType.CdSectorEdc)
|
|
|
|
|
) imageInfo.ReadableSectorTags.Add(SectorTagType.CdSectorEdc);
|
|
|
|
|
if(imageInfo.SectorSize < 2048) imageInfo.SectorSize = 2048;
|
2017-12-19 20:33:03 +00:00
|
|
|
}
|
|
|
|
|
else if(sectTest[15] == 2)
|
|
|
|
|
{
|
|
|
|
|
byte[] subHdr1 = new byte[4];
|
|
|
|
|
byte[] subHdr2 = new byte[4];
|
2018-01-01 21:32:12 +00:00
|
|
|
byte[] empHdr = new byte[4];
|
2017-12-19 20:33:03 +00:00
|
|
|
|
|
|
|
|
Array.Copy(sectTest, 16, subHdr1, 0, 4);
|
|
|
|
|
Array.Copy(sectTest, 20, subHdr2, 0, 4);
|
|
|
|
|
|
|
|
|
|
if(subHdr1.SequenceEqual(subHdr2) && !empHdr.SequenceEqual(subHdr1))
|
|
|
|
|
if((subHdr1[2] & 0x20) == 0x20)
|
|
|
|
|
{
|
|
|
|
|
currentTrack.TrackBytesPerSector = 2324;
|
2018-01-01 21:32:12 +00:00
|
|
|
currentTrack.TrackType = TrackType.CdMode2Form2;
|
2017-12-26 06:05:12 +00:00
|
|
|
if(!imageInfo.ReadableSectorTags.Contains(SectorTagType
|
2018-01-01 21:32:12 +00:00
|
|
|
.CdSectorSync))
|
2017-12-26 06:05:12 +00:00
|
|
|
imageInfo.ReadableSectorTags.Add(SectorTagType
|
2018-01-01 21:32:12 +00:00
|
|
|
.CdSectorSync);
|
2017-12-26 06:05:12 +00:00
|
|
|
if(!imageInfo.ReadableSectorTags.Contains(SectorTagType
|
2018-01-01 21:32:12 +00:00
|
|
|
.CdSectorHeader)
|
2017-12-19 20:33:03 +00:00
|
|
|
)
|
2017-12-26 06:05:12 +00:00
|
|
|
imageInfo.ReadableSectorTags.Add(SectorTagType
|
2018-01-01 21:32:12 +00:00
|
|
|
.CdSectorHeader);
|
2017-12-26 06:05:12 +00:00
|
|
|
if(!imageInfo.ReadableSectorTags.Contains(SectorTagType
|
2018-01-01 21:32:12 +00:00
|
|
|
.CdSectorSubHeader)
|
2017-12-19 20:33:03 +00:00
|
|
|
)
|
2017-12-26 06:05:12 +00:00
|
|
|
imageInfo.ReadableSectorTags.Add(SectorTagType
|
2018-01-01 21:32:12 +00:00
|
|
|
.CdSectorSubHeader);
|
2017-12-26 06:05:12 +00:00
|
|
|
if(!imageInfo.ReadableSectorTags.Contains(SectorTagType
|
2018-01-01 21:32:12 +00:00
|
|
|
.CdSectorEdc))
|
2017-12-26 06:05:12 +00:00
|
|
|
imageInfo.ReadableSectorTags.Add(SectorTagType
|
2018-01-01 21:32:12 +00:00
|
|
|
.CdSectorEdc);
|
2017-12-26 06:05:12 +00:00
|
|
|
if(imageInfo.SectorSize < 2324) imageInfo.SectorSize = 2324;
|
2017-12-19 20:33:03 +00:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
currentTrack.TrackBytesPerSector = 2048;
|
2018-01-01 21:32:12 +00:00
|
|
|
currentTrack.TrackType = TrackType.CdMode2Form1;
|
2017-12-26 06:05:12 +00:00
|
|
|
if(!imageInfo.ReadableSectorTags.Contains(SectorTagType
|
2018-01-01 21:32:12 +00:00
|
|
|
.CdSectorSync))
|
2017-12-26 06:05:12 +00:00
|
|
|
imageInfo.ReadableSectorTags.Add(SectorTagType
|
2018-01-01 21:32:12 +00:00
|
|
|
.CdSectorSync);
|
2017-12-26 06:05:12 +00:00
|
|
|
if(!imageInfo.ReadableSectorTags.Contains(SectorTagType
|
2018-01-01 21:32:12 +00:00
|
|
|
.CdSectorHeader)
|
2017-12-19 20:33:03 +00:00
|
|
|
)
|
2017-12-26 06:05:12 +00:00
|
|
|
imageInfo.ReadableSectorTags.Add(SectorTagType
|
2018-01-01 21:32:12 +00:00
|
|
|
.CdSectorHeader);
|
2017-12-26 06:05:12 +00:00
|
|
|
if(!imageInfo.ReadableSectorTags.Contains(SectorTagType
|
2018-01-01 21:32:12 +00:00
|
|
|
.CdSectorSubHeader)
|
2017-12-19 20:33:03 +00:00
|
|
|
)
|
2017-12-26 06:05:12 +00:00
|
|
|
imageInfo.ReadableSectorTags.Add(SectorTagType
|
2018-01-01 21:32:12 +00:00
|
|
|
.CdSectorSubHeader);
|
2017-12-26 06:05:12 +00:00
|
|
|
if(!imageInfo.ReadableSectorTags.Contains(SectorTagType
|
2018-01-01 21:32:12 +00:00
|
|
|
.CdSectorEcc))
|
2017-12-26 06:05:12 +00:00
|
|
|
imageInfo.ReadableSectorTags.Add(SectorTagType
|
2018-01-01 21:32:12 +00:00
|
|
|
.CdSectorEcc);
|
2017-12-26 06:05:12 +00:00
|
|
|
if(!imageInfo.ReadableSectorTags.Contains(SectorTagType
|
2018-01-01 21:32:12 +00:00
|
|
|
.CdSectorEccP))
|
2017-12-26 06:05:12 +00:00
|
|
|
imageInfo.ReadableSectorTags.Add(SectorTagType
|
2018-01-01 21:32:12 +00:00
|
|
|
.CdSectorEccP);
|
2017-12-26 06:05:12 +00:00
|
|
|
if(!imageInfo.ReadableSectorTags.Contains(SectorTagType
|
2018-01-01 21:32:12 +00:00
|
|
|
.CdSectorEccQ))
|
2017-12-26 06:05:12 +00:00
|
|
|
imageInfo.ReadableSectorTags.Add(SectorTagType
|
2018-01-01 21:32:12 +00:00
|
|
|
.CdSectorEccQ);
|
2017-12-26 06:05:12 +00:00
|
|
|
if(!imageInfo.ReadableSectorTags.Contains(SectorTagType
|
2018-01-01 21:32:12 +00:00
|
|
|
.CdSectorEdc))
|
2017-12-26 06:05:12 +00:00
|
|
|
imageInfo.ReadableSectorTags.Add(SectorTagType
|
2018-01-01 21:32:12 +00:00
|
|
|
.CdSectorEdc);
|
2017-12-26 06:05:12 +00:00
|
|
|
if(imageInfo.SectorSize < 2048) imageInfo.SectorSize = 2048;
|
2017-12-19 20:33:03 +00:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
currentTrack.TrackBytesPerSector = 2336;
|
2018-01-01 21:32:12 +00:00
|
|
|
currentTrack.TrackType = TrackType.CdMode2Formless;
|
2017-12-26 06:05:12 +00:00
|
|
|
if(!imageInfo.ReadableSectorTags.Contains(SectorTagType
|
2018-01-01 21:32:12 +00:00
|
|
|
.CdSectorSync))
|
2017-12-26 06:05:12 +00:00
|
|
|
imageInfo.ReadableSectorTags
|
2017-12-20 17:15:26 +00:00
|
|
|
.Add(SectorTagType.CdSectorSync);
|
2017-12-26 06:05:12 +00:00
|
|
|
if(!imageInfo.ReadableSectorTags.Contains(SectorTagType
|
2018-01-01 21:32:12 +00:00
|
|
|
.CdSectorHeader))
|
2017-12-26 06:05:12 +00:00
|
|
|
imageInfo.ReadableSectorTags.Add(SectorTagType
|
2018-01-01 21:32:12 +00:00
|
|
|
.CdSectorHeader);
|
2017-12-26 06:05:12 +00:00
|
|
|
if(imageInfo.SectorSize < 2336) imageInfo.SectorSize = 2336;
|
2017-12-19 20:33:03 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-01-01 21:32:12 +00:00
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if(imageInfo.SectorSize < 2352) imageInfo.SectorSize = 2352;
|
|
|
|
|
}
|
2017-12-19 20:33:03 +00:00
|
|
|
}
|
2018-01-01 21:32:12 +00:00
|
|
|
|
2017-12-19 20:33:03 +00:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
case 5:
|
|
|
|
|
switch(descriptor.POINT)
|
|
|
|
|
{
|
|
|
|
|
case 0xC0:
|
|
|
|
|
if(descriptor.PMIN == 97)
|
|
|
|
|
{
|
|
|
|
|
int type = descriptor.PFRAME % 10;
|
2018-01-01 21:32:12 +00:00
|
|
|
int frm = descriptor.PFRAME - type;
|
2017-12-19 20:33:03 +00:00
|
|
|
|
2017-12-26 06:05:12 +00:00
|
|
|
imageInfo.MediaManufacturer = ATIP.ManufacturerFromATIP(descriptor.PSEC, frm);
|
2017-12-19 20:33:03 +00:00
|
|
|
|
2017-12-26 06:05:12 +00:00
|
|
|
if(imageInfo.MediaManufacturer != "")
|
2017-12-19 20:33:03 +00:00
|
|
|
DicConsole.DebugWriteLine("CloneCD plugin", "Disc manufactured by: {0}",
|
2017-12-26 06:05:12 +00:00
|
|
|
imageInfo.MediaManufacturer);
|
2017-12-19 20:33:03 +00:00
|
|
|
}
|
2018-01-01 21:32:12 +00:00
|
|
|
|
2017-12-19 20:33:03 +00:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
case 6:
|
|
|
|
|
{
|
|
|
|
|
uint id = (uint)((descriptor.Min << 16) + (descriptor.Sec << 8) + descriptor.Frame);
|
|
|
|
|
DicConsole.DebugWriteLine("CloneCD plugin", "Disc ID: {0:X6}", id & 0x00FFFFFF);
|
2018-01-01 21:32:12 +00:00
|
|
|
imageInfo.MediaSerialNumber = $"{id & 0x00FFFFFF:X6}";
|
2017-12-19 20:33:03 +00:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(!firstTrackInSession)
|
|
|
|
|
{
|
2017-12-20 17:15:26 +00:00
|
|
|
currentTrack.TrackEndSector = leadOutStart - 1;
|
2018-01-01 21:32:12 +00:00
|
|
|
Tracks.Add(currentTrack);
|
2017-12-19 20:33:03 +00:00
|
|
|
}
|
|
|
|
|
|
2017-12-26 06:05:12 +00:00
|
|
|
if(subFilter != null && !imageInfo.ReadableSectorTags.Contains(SectorTagType.CdSectorSubchannel))
|
|
|
|
|
imageInfo.ReadableSectorTags.Add(SectorTagType.CdSectorSubchannel);
|
2017-12-19 20:33:03 +00:00
|
|
|
|
2018-01-01 21:32:12 +00:00
|
|
|
imageInfo.ReadableSectorTags.Add(SectorTagType.CdTrackFlags);
|
|
|
|
|
|
2018-06-22 08:08:38 +01:00
|
|
|
Sessions = new List<Session>();
|
2017-12-22 06:55:04 +00:00
|
|
|
Session currentSession = new Session
|
|
|
|
|
{
|
2018-01-01 21:32:12 +00:00
|
|
|
EndTrack = uint.MinValue,
|
|
|
|
|
StartTrack = uint.MaxValue,
|
2017-12-22 06:55:04 +00:00
|
|
|
SessionSequence = 1
|
|
|
|
|
};
|
2018-01-01 21:32:12 +00:00
|
|
|
Partitions = new List<Partition>();
|
|
|
|
|
offsetmap = new Dictionary<uint, ulong>();
|
2017-12-19 20:33:03 +00:00
|
|
|
|
2018-01-01 21:32:12 +00:00
|
|
|
foreach(Track track in Tracks)
|
2017-12-19 20:33:03 +00:00
|
|
|
{
|
|
|
|
|
if(track.TrackSession == currentSession.SessionSequence)
|
|
|
|
|
{
|
|
|
|
|
if(track.TrackSequence > currentSession.EndTrack)
|
|
|
|
|
{
|
|
|
|
|
currentSession.EndSector = track.TrackEndSector;
|
2018-01-01 21:32:12 +00:00
|
|
|
currentSession.EndTrack = track.TrackSequence;
|
2017-12-19 20:33:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(track.TrackSequence < currentSession.StartTrack)
|
|
|
|
|
{
|
|
|
|
|
currentSession.StartSector = track.TrackStartSector;
|
2018-01-01 21:32:12 +00:00
|
|
|
currentSession.StartTrack = track.TrackSequence;
|
2017-12-19 20:33:03 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2018-01-01 21:32:12 +00:00
|
|
|
Sessions.Add(currentSession);
|
2017-12-22 06:55:04 +00:00
|
|
|
currentSession = new Session
|
|
|
|
|
{
|
2018-01-01 21:32:12 +00:00
|
|
|
EndTrack = uint.MinValue,
|
|
|
|
|
StartTrack = uint.MaxValue,
|
2017-12-22 06:55:04 +00:00
|
|
|
SessionSequence = track.TrackSession
|
|
|
|
|
};
|
2017-12-19 20:33:03 +00:00
|
|
|
}
|
|
|
|
|
|
2017-12-22 06:55:04 +00:00
|
|
|
Partition partition = new Partition
|
|
|
|
|
{
|
|
|
|
|
Description = track.TrackDescription,
|
2018-06-22 08:08:38 +01:00
|
|
|
Size =
|
|
|
|
|
(track.TrackEndSector - track.TrackStartSector + 1) * (ulong)track.TrackRawBytesPerSector,
|
2018-01-01 21:32:12 +00:00
|
|
|
Length = track.TrackEndSector - track.TrackStartSector + 1,
|
2017-12-22 06:55:04 +00:00
|
|
|
Sequence = track.TrackSequence,
|
2018-01-01 21:32:12 +00:00
|
|
|
Offset = track.TrackFileOffset,
|
|
|
|
|
Start = track.TrackStartSector,
|
|
|
|
|
Type = track.TrackType.ToString()
|
2017-12-22 06:55:04 +00:00
|
|
|
};
|
2017-12-26 06:05:12 +00:00
|
|
|
imageInfo.Sectors += partition.Length;
|
2018-01-01 21:32:12 +00:00
|
|
|
Partitions.Add(partition);
|
2017-12-19 20:33:03 +00:00
|
|
|
offsetmap.Add(track.TrackSequence, track.TrackStartSector);
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-01 21:32:12 +00:00
|
|
|
bool data = false;
|
|
|
|
|
bool mode2 = false;
|
2017-12-19 20:33:03 +00:00
|
|
|
bool firstaudio = false;
|
2018-01-01 21:32:12 +00:00
|
|
|
bool firstdata = false;
|
|
|
|
|
bool audio = false;
|
2017-12-19 20:33:03 +00:00
|
|
|
|
2018-01-01 21:32:12 +00:00
|
|
|
for(int i = 0; i < Tracks.Count; i++)
|
2017-12-19 20:33:03 +00:00
|
|
|
{
|
|
|
|
|
// First track is audio
|
2018-01-01 21:32:12 +00:00
|
|
|
firstaudio |= i == 0 && Tracks[i].TrackType == TrackType.Audio;
|
2017-12-19 20:33:03 +00:00
|
|
|
|
|
|
|
|
// First track is data
|
2018-01-01 21:32:12 +00:00
|
|
|
firstdata |= i == 0 && Tracks[i].TrackType != TrackType.Audio;
|
2017-12-19 20:33:03 +00:00
|
|
|
|
|
|
|
|
// Any non first track is data
|
2018-01-01 21:32:12 +00:00
|
|
|
data |= i != 0 && Tracks[i].TrackType != TrackType.Audio;
|
2017-12-19 20:33:03 +00:00
|
|
|
|
|
|
|
|
// Any non first track is audio
|
2018-01-01 21:32:12 +00:00
|
|
|
audio |= i != 0 && Tracks[i].TrackType == TrackType.Audio;
|
2017-12-19 20:33:03 +00:00
|
|
|
|
2018-01-01 21:32:12 +00:00
|
|
|
switch(Tracks[i].TrackType)
|
2017-12-19 20:33:03 +00:00
|
|
|
{
|
2017-12-20 17:15:26 +00:00
|
|
|
case TrackType.CdMode2Form1:
|
|
|
|
|
case TrackType.CdMode2Form2:
|
|
|
|
|
case TrackType.CdMode2Formless:
|
2017-12-19 20:33:03 +00:00
|
|
|
mode2 = true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TODO: Check format
|
|
|
|
|
cdtext = cdtMs.ToArray();
|
|
|
|
|
|
2018-06-22 08:08:38 +01:00
|
|
|
if(!data && !firstdata) imageInfo.MediaType = MediaType.CDDA;
|
|
|
|
|
else if(firstaudio && data && Sessions.Count > 1 && mode2) imageInfo.MediaType = MediaType.CDPLUS;
|
|
|
|
|
else if(firstdata && audio || mode2) imageInfo.MediaType = MediaType.CDROMXA;
|
|
|
|
|
else if(!audio) imageInfo.MediaType = MediaType.CDROM;
|
|
|
|
|
else imageInfo.MediaType = MediaType.CD;
|
2018-01-01 21:32:12 +00:00
|
|
|
|
|
|
|
|
imageInfo.Application = "CloneCD";
|
|
|
|
|
imageInfo.ImageSize = (ulong)imageFilter.GetDataForkLength();
|
|
|
|
|
imageInfo.CreationTime = imageFilter.GetCreationTime();
|
2017-12-26 06:05:12 +00:00
|
|
|
imageInfo.LastModificationTime = imageFilter.GetLastWriteTime();
|
2018-01-01 21:32:12 +00:00
|
|
|
imageInfo.XmlMediaType = XmlMediaType.OpticalDisc;
|
2017-12-19 20:33:03 +00:00
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
catch(Exception ex)
|
|
|
|
|
{
|
|
|
|
|
DicConsole.ErrorWriteLine("Exception trying to identify image file {0}", imageFilter.GetFilename());
|
2018-01-01 21:32:12 +00:00
|
|
|
DicConsole.ErrorWriteLine("Exception: {0}", ex.Message);
|
|
|
|
|
DicConsole.ErrorWriteLine("Stack trace: {0}", ex.StackTrace);
|
2017-12-19 20:33:03 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-26 07:28:40 +00:00
|
|
|
public byte[] ReadDiskTag(MediaTagType tag)
|
2017-12-19 20:33:03 +00:00
|
|
|
{
|
|
|
|
|
switch(tag)
|
|
|
|
|
{
|
2018-01-01 21:32:12 +00:00
|
|
|
case MediaTagType.CD_FullTOC: { return fulltoc; }
|
2017-12-19 20:33:03 +00:00
|
|
|
case MediaTagType.CD_TEXT:
|
|
|
|
|
{
|
|
|
|
|
if(cdtext != null && cdtext.Length > 0) return cdtext;
|
|
|
|
|
|
|
|
|
|
throw new FeatureNotPresentImageException("Image does not contain CD-TEXT information.");
|
|
|
|
|
}
|
|
|
|
|
default:
|
|
|
|
|
throw new FeatureSupportedButNotImplementedImageException("Feature not supported by image format");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-26 07:28:40 +00:00
|
|
|
public byte[] ReadSector(ulong sectorAddress)
|
2017-12-19 20:33:03 +00:00
|
|
|
{
|
|
|
|
|
return ReadSectors(sectorAddress, 1);
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-26 07:28:40 +00:00
|
|
|
public byte[] ReadSectorTag(ulong sectorAddress, SectorTagType tag)
|
2017-12-19 20:33:03 +00:00
|
|
|
{
|
|
|
|
|
return ReadSectorsTag(sectorAddress, 1, tag);
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-26 07:28:40 +00:00
|
|
|
public byte[] ReadSector(ulong sectorAddress, uint track)
|
2017-12-19 20:33:03 +00:00
|
|
|
{
|
|
|
|
|
return ReadSectors(sectorAddress, 1, track);
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-26 07:28:40 +00:00
|
|
|
public byte[] ReadSectorTag(ulong sectorAddress, uint track, SectorTagType tag)
|
2017-12-19 20:33:03 +00:00
|
|
|
{
|
|
|
|
|
return ReadSectorsTag(sectorAddress, 1, track, tag);
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-26 07:28:40 +00:00
|
|
|
public byte[] ReadSectors(ulong sectorAddress, uint length)
|
2017-12-19 20:33:03 +00:00
|
|
|
{
|
2017-12-24 00:12:31 +00:00
|
|
|
foreach(KeyValuePair<uint, ulong> kvp in from kvp in offsetmap
|
|
|
|
|
where sectorAddress >= kvp.Value
|
2018-01-01 21:32:12 +00:00
|
|
|
from track in Tracks
|
2017-12-24 00:12:31 +00:00
|
|
|
where track.TrackSequence == kvp.Key
|
2018-01-01 21:32:12 +00:00
|
|
|
where sectorAddress <= track.TrackEndSector
|
2017-12-24 00:12:31 +00:00
|
|
|
select kvp)
|
|
|
|
|
return ReadSectors(sectorAddress - kvp.Value, length, kvp.Key);
|
2017-12-19 20:33:03 +00:00
|
|
|
|
2017-12-21 17:58:51 +00:00
|
|
|
throw new ArgumentOutOfRangeException(nameof(sectorAddress), $"Sector address {sectorAddress} not found");
|
2017-12-19 20:33:03 +00:00
|
|
|
}
|
|
|
|
|
|
2017-12-26 07:28:40 +00:00
|
|
|
public byte[] ReadSectorsTag(ulong sectorAddress, uint length, SectorTagType tag)
|
2017-12-19 20:33:03 +00:00
|
|
|
{
|
2017-12-24 00:12:31 +00:00
|
|
|
foreach(KeyValuePair<uint, ulong> kvp in from kvp in offsetmap
|
|
|
|
|
where sectorAddress >= kvp.Value
|
2018-01-01 21:32:12 +00:00
|
|
|
from track in Tracks
|
2017-12-24 00:12:31 +00:00
|
|
|
where track.TrackSequence == kvp.Key
|
2018-01-01 21:32:12 +00:00
|
|
|
where sectorAddress <= track.TrackEndSector
|
2017-12-24 00:12:31 +00:00
|
|
|
select kvp)
|
|
|
|
|
return ReadSectorsTag(sectorAddress - kvp.Value, length, kvp.Key, tag);
|
2017-12-19 20:33:03 +00:00
|
|
|
|
2017-12-21 17:58:51 +00:00
|
|
|
throw new ArgumentOutOfRangeException(nameof(sectorAddress), $"Sector address {sectorAddress} not found");
|
2017-12-19 20:33:03 +00:00
|
|
|
}
|
|
|
|
|
|
2017-12-26 07:28:40 +00:00
|
|
|
public byte[] ReadSectors(ulong sectorAddress, uint length, uint track)
|
2017-12-19 20:33:03 +00:00
|
|
|
{
|
2017-12-22 06:55:04 +00:00
|
|
|
Track dicTrack = new Track {TrackSequence = 0};
|
2017-12-19 20:33:03 +00:00
|
|
|
|
2018-01-01 21:32:12 +00:00
|
|
|
foreach(Track linqTrack in Tracks.Where(linqTrack => linqTrack.TrackSequence == track))
|
2017-12-24 00:12:31 +00:00
|
|
|
{
|
2017-12-22 06:55:04 +00:00
|
|
|
dicTrack = linqTrack;
|
2017-12-21 07:08:26 +00:00
|
|
|
break;
|
|
|
|
|
}
|
2017-12-19 20:33:03 +00:00
|
|
|
|
2017-12-22 06:55:04 +00:00
|
|
|
if(dicTrack.TrackSequence == 0)
|
2017-12-19 20:33:03 +00:00
|
|
|
throw new ArgumentOutOfRangeException(nameof(track), "Track does not exist in disc image");
|
|
|
|
|
|
2017-12-22 06:55:04 +00:00
|
|
|
if(length + sectorAddress - 1 > dicTrack.TrackEndSector)
|
2017-12-19 20:33:03 +00:00
|
|
|
throw new ArgumentOutOfRangeException(nameof(length),
|
|
|
|
|
string
|
2018-01-01 21:32:12 +00:00
|
|
|
.Format("Requested more sectors ({0} {2}) than present in track ({1}), won't cross tracks",
|
|
|
|
|
length + sectorAddress, dicTrack.TrackEndSector,
|
|
|
|
|
sectorAddress));
|
2017-12-19 20:33:03 +00:00
|
|
|
|
2017-12-20 17:15:26 +00:00
|
|
|
uint sectorOffset;
|
|
|
|
|
uint sectorSize;
|
|
|
|
|
uint sectorSkip;
|
2017-12-19 20:33:03 +00:00
|
|
|
|
2017-12-22 06:55:04 +00:00
|
|
|
switch(dicTrack.TrackType)
|
2017-12-19 20:33:03 +00:00
|
|
|
{
|
|
|
|
|
case TrackType.Audio:
|
|
|
|
|
{
|
2017-12-20 17:15:26 +00:00
|
|
|
sectorOffset = 0;
|
2018-01-01 21:32:12 +00:00
|
|
|
sectorSize = 2352;
|
|
|
|
|
sectorSkip = 0;
|
2017-12-19 20:33:03 +00:00
|
|
|
break;
|
|
|
|
|
}
|
2017-12-20 17:15:26 +00:00
|
|
|
case TrackType.CdMode1:
|
2017-12-19 20:33:03 +00:00
|
|
|
{
|
2017-12-20 17:15:26 +00:00
|
|
|
sectorOffset = 16;
|
2018-01-01 21:32:12 +00:00
|
|
|
sectorSize = 2048;
|
|
|
|
|
sectorSkip = 288;
|
2017-12-19 20:33:03 +00:00
|
|
|
break;
|
|
|
|
|
}
|
2017-12-20 17:15:26 +00:00
|
|
|
case TrackType.CdMode2Formless:
|
2017-12-19 20:33:03 +00:00
|
|
|
{
|
2017-12-20 17:15:26 +00:00
|
|
|
sectorOffset = 16;
|
2018-01-01 21:32:12 +00:00
|
|
|
sectorSize = 2336;
|
|
|
|
|
sectorSkip = 0;
|
2017-12-19 20:33:03 +00:00
|
|
|
break;
|
|
|
|
|
}
|
2017-12-20 17:15:26 +00:00
|
|
|
case TrackType.CdMode2Form1:
|
2017-12-19 20:33:03 +00:00
|
|
|
{
|
2017-12-20 17:15:26 +00:00
|
|
|
sectorOffset = 24;
|
2018-01-01 21:32:12 +00:00
|
|
|
sectorSize = 2048;
|
|
|
|
|
sectorSkip = 280;
|
2017-12-19 20:33:03 +00:00
|
|
|
break;
|
|
|
|
|
}
|
2017-12-20 17:15:26 +00:00
|
|
|
case TrackType.CdMode2Form2:
|
2017-12-19 20:33:03 +00:00
|
|
|
{
|
2017-12-20 17:15:26 +00:00
|
|
|
sectorOffset = 24;
|
2018-01-01 21:32:12 +00:00
|
|
|
sectorSize = 2324;
|
|
|
|
|
sectorSkip = 4;
|
2017-12-19 20:33:03 +00:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
default: throw new FeatureSupportedButNotImplementedImageException("Unsupported track type");
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-20 17:15:26 +00:00
|
|
|
byte[] buffer = new byte[sectorSize * length];
|
2017-12-19 20:33:03 +00:00
|
|
|
|
2017-12-22 06:55:04 +00:00
|
|
|
dataStream.Seek((long)(dicTrack.TrackFileOffset + sectorAddress * 2352), SeekOrigin.Begin);
|
2017-12-20 17:15:26 +00:00
|
|
|
if(sectorOffset == 0 && sectorSkip == 0) dataStream.Read(buffer, 0, buffer.Length);
|
2017-12-19 20:33:03 +00:00
|
|
|
else
|
|
|
|
|
for(int i = 0; i < length; i++)
|
|
|
|
|
{
|
2017-12-20 17:15:26 +00:00
|
|
|
byte[] sector = new byte[sectorSize];
|
|
|
|
|
dataStream.Seek(sectorOffset, SeekOrigin.Current);
|
2017-12-19 20:33:03 +00:00
|
|
|
dataStream.Read(sector, 0, sector.Length);
|
2017-12-20 17:15:26 +00:00
|
|
|
dataStream.Seek(sectorSkip, SeekOrigin.Current);
|
|
|
|
|
Array.Copy(sector, 0, buffer, i * sectorSize, sectorSize);
|
2017-12-19 20:33:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return buffer;
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-26 07:28:40 +00:00
|
|
|
public byte[] ReadSectorsTag(ulong sectorAddress, uint length, uint track, SectorTagType tag)
|
2017-12-19 20:33:03 +00:00
|
|
|
{
|
2017-12-22 06:55:04 +00:00
|
|
|
Track dicTrack = new Track {TrackSequence = 0};
|
2017-12-19 20:33:03 +00:00
|
|
|
|
2018-01-01 21:32:12 +00:00
|
|
|
foreach(Track linqTrack in Tracks.Where(linqTrack => linqTrack.TrackSequence == track))
|
2017-12-24 00:12:31 +00:00
|
|
|
{
|
2017-12-22 06:55:04 +00:00
|
|
|
dicTrack = linqTrack;
|
2017-12-21 07:08:26 +00:00
|
|
|
break;
|
|
|
|
|
}
|
2017-12-19 20:33:03 +00:00
|
|
|
|
2017-12-22 06:55:04 +00:00
|
|
|
if(dicTrack.TrackSequence == 0)
|
2017-12-19 20:33:03 +00:00
|
|
|
throw new ArgumentOutOfRangeException(nameof(track), "Track does not exist in disc image");
|
|
|
|
|
|
2017-12-22 06:55:04 +00:00
|
|
|
if(length + sectorAddress - 1 > dicTrack.TrackEndSector)
|
2017-12-19 20:33:03 +00:00
|
|
|
throw new ArgumentOutOfRangeException(nameof(length),
|
2017-12-22 06:55:04 +00:00
|
|
|
$"Requested more sectors ({length + sectorAddress}) than present in track ({dicTrack.TrackEndSector}), won't cross tracks");
|
2017-12-19 20:33:03 +00:00
|
|
|
|
2017-12-22 06:55:04 +00:00
|
|
|
if(dicTrack.TrackType == TrackType.Data)
|
2017-12-19 20:33:03 +00:00
|
|
|
throw new ArgumentException("Unsupported tag requested", nameof(tag));
|
|
|
|
|
|
|
|
|
|
byte[] buffer;
|
|
|
|
|
|
|
|
|
|
switch(tag)
|
|
|
|
|
{
|
2017-12-20 17:15:26 +00:00
|
|
|
case SectorTagType.CdSectorEcc:
|
|
|
|
|
case SectorTagType.CdSectorEccP:
|
|
|
|
|
case SectorTagType.CdSectorEccQ:
|
|
|
|
|
case SectorTagType.CdSectorEdc:
|
|
|
|
|
case SectorTagType.CdSectorHeader:
|
|
|
|
|
case SectorTagType.CdSectorSubHeader:
|
|
|
|
|
case SectorTagType.CdSectorSync: break;
|
2018-01-01 21:32:12 +00:00
|
|
|
case SectorTagType.CdTrackFlags:
|
|
|
|
|
return !trackFlags.TryGetValue((byte)dicTrack.TrackSequence, out byte flags)
|
|
|
|
|
? new[] {flags}
|
|
|
|
|
: new byte[1];
|
2017-12-20 17:15:26 +00:00
|
|
|
case SectorTagType.CdSectorSubchannel:
|
2018-06-22 08:08:38 +01:00
|
|
|
buffer = new byte[96 * length];
|
2017-12-22 06:55:04 +00:00
|
|
|
subStream.Seek((long)(dicTrack.TrackSubchannelOffset + sectorAddress * 96), SeekOrigin.Begin);
|
2017-12-19 20:33:03 +00:00
|
|
|
subStream.Read(buffer, 0, buffer.Length);
|
|
|
|
|
return buffer;
|
|
|
|
|
default: throw new ArgumentException("Unsupported tag requested", nameof(tag));
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-20 17:15:26 +00:00
|
|
|
uint sectorOffset;
|
|
|
|
|
uint sectorSize;
|
|
|
|
|
uint sectorSkip;
|
2017-12-19 20:33:03 +00:00
|
|
|
|
2017-12-22 06:55:04 +00:00
|
|
|
switch(dicTrack.TrackType)
|
2017-12-19 20:33:03 +00:00
|
|
|
{
|
2017-12-20 17:15:26 +00:00
|
|
|
case TrackType.CdMode1:
|
2017-12-19 20:33:03 +00:00
|
|
|
switch(tag)
|
|
|
|
|
{
|
2017-12-20 17:15:26 +00:00
|
|
|
case SectorTagType.CdSectorSync:
|
2017-12-19 20:33:03 +00:00
|
|
|
{
|
2017-12-20 17:15:26 +00:00
|
|
|
sectorOffset = 0;
|
2018-01-01 21:32:12 +00:00
|
|
|
sectorSize = 12;
|
|
|
|
|
sectorSkip = 2340;
|
2017-12-19 20:33:03 +00:00
|
|
|
break;
|
|
|
|
|
}
|
2017-12-20 17:15:26 +00:00
|
|
|
case SectorTagType.CdSectorHeader:
|
2017-12-19 20:33:03 +00:00
|
|
|
{
|
2017-12-20 17:15:26 +00:00
|
|
|
sectorOffset = 12;
|
2018-01-01 21:32:12 +00:00
|
|
|
sectorSize = 4;
|
|
|
|
|
sectorSkip = 2336;
|
2017-12-19 20:33:03 +00:00
|
|
|
break;
|
|
|
|
|
}
|
2017-12-20 17:15:26 +00:00
|
|
|
case SectorTagType.CdSectorSubHeader:
|
2017-12-19 20:33:03 +00:00
|
|
|
throw new ArgumentException("Unsupported tag requested for this track", nameof(tag));
|
2017-12-20 17:15:26 +00:00
|
|
|
case SectorTagType.CdSectorEcc:
|
2017-12-19 20:33:03 +00:00
|
|
|
{
|
2017-12-20 17:15:26 +00:00
|
|
|
sectorOffset = 2076;
|
2018-01-01 21:32:12 +00:00
|
|
|
sectorSize = 276;
|
|
|
|
|
sectorSkip = 0;
|
2017-12-19 20:33:03 +00:00
|
|
|
break;
|
|
|
|
|
}
|
2017-12-20 17:15:26 +00:00
|
|
|
case SectorTagType.CdSectorEccP:
|
2017-12-19 20:33:03 +00:00
|
|
|
{
|
2017-12-20 17:15:26 +00:00
|
|
|
sectorOffset = 2076;
|
2018-01-01 21:32:12 +00:00
|
|
|
sectorSize = 172;
|
|
|
|
|
sectorSkip = 104;
|
2017-12-19 20:33:03 +00:00
|
|
|
break;
|
|
|
|
|
}
|
2017-12-20 17:15:26 +00:00
|
|
|
case SectorTagType.CdSectorEccQ:
|
2017-12-19 20:33:03 +00:00
|
|
|
{
|
2017-12-20 17:15:26 +00:00
|
|
|
sectorOffset = 2248;
|
2018-01-01 21:32:12 +00:00
|
|
|
sectorSize = 104;
|
|
|
|
|
sectorSkip = 0;
|
2017-12-19 20:33:03 +00:00
|
|
|
break;
|
|
|
|
|
}
|
2017-12-20 17:15:26 +00:00
|
|
|
case SectorTagType.CdSectorEdc:
|
2017-12-19 20:33:03 +00:00
|
|
|
{
|
2017-12-20 17:15:26 +00:00
|
|
|
sectorOffset = 2064;
|
2018-01-01 21:32:12 +00:00
|
|
|
sectorSize = 4;
|
|
|
|
|
sectorSkip = 284;
|
2017-12-19 20:33:03 +00:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
default: throw new ArgumentException("Unsupported tag requested", nameof(tag));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
break;
|
2017-12-20 17:15:26 +00:00
|
|
|
case TrackType.CdMode2Formless:
|
2017-12-19 20:33:03 +00:00
|
|
|
{
|
|
|
|
|
switch(tag)
|
|
|
|
|
{
|
2017-12-20 17:15:26 +00:00
|
|
|
case SectorTagType.CdSectorSync:
|
|
|
|
|
case SectorTagType.CdSectorHeader:
|
|
|
|
|
case SectorTagType.CdSectorEcc:
|
|
|
|
|
case SectorTagType.CdSectorEccP:
|
|
|
|
|
case SectorTagType.CdSectorEccQ:
|
2017-12-19 20:33:03 +00:00
|
|
|
throw new ArgumentException("Unsupported tag requested for this track", nameof(tag));
|
2017-12-20 17:15:26 +00:00
|
|
|
case SectorTagType.CdSectorSubHeader:
|
2017-12-19 20:33:03 +00:00
|
|
|
{
|
2017-12-20 17:15:26 +00:00
|
|
|
sectorOffset = 0;
|
2018-01-01 21:32:12 +00:00
|
|
|
sectorSize = 8;
|
|
|
|
|
sectorSkip = 2328;
|
2017-12-19 20:33:03 +00:00
|
|
|
break;
|
|
|
|
|
}
|
2017-12-20 17:15:26 +00:00
|
|
|
case SectorTagType.CdSectorEdc:
|
2017-12-19 20:33:03 +00:00
|
|
|
{
|
2017-12-20 17:15:26 +00:00
|
|
|
sectorOffset = 2332;
|
2018-01-01 21:32:12 +00:00
|
|
|
sectorSize = 4;
|
|
|
|
|
sectorSkip = 0;
|
2017-12-19 20:33:03 +00:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
default: throw new ArgumentException("Unsupported tag requested", nameof(tag));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
2017-12-20 17:15:26 +00:00
|
|
|
case TrackType.CdMode2Form1:
|
2017-12-19 20:33:03 +00:00
|
|
|
switch(tag)
|
|
|
|
|
{
|
2017-12-20 17:15:26 +00:00
|
|
|
case SectorTagType.CdSectorSync:
|
2017-12-19 20:33:03 +00:00
|
|
|
{
|
2017-12-20 17:15:26 +00:00
|
|
|
sectorOffset = 0;
|
2018-01-01 21:32:12 +00:00
|
|
|
sectorSize = 12;
|
|
|
|
|
sectorSkip = 2340;
|
2017-12-19 20:33:03 +00:00
|
|
|
break;
|
|
|
|
|
}
|
2017-12-20 17:15:26 +00:00
|
|
|
case SectorTagType.CdSectorHeader:
|
2017-12-19 20:33:03 +00:00
|
|
|
{
|
2017-12-20 17:15:26 +00:00
|
|
|
sectorOffset = 12;
|
2018-01-01 21:32:12 +00:00
|
|
|
sectorSize = 4;
|
|
|
|
|
sectorSkip = 2336;
|
2017-12-19 20:33:03 +00:00
|
|
|
break;
|
|
|
|
|
}
|
2017-12-20 17:15:26 +00:00
|
|
|
case SectorTagType.CdSectorSubHeader:
|
2017-12-19 20:33:03 +00:00
|
|
|
{
|
2017-12-20 17:15:26 +00:00
|
|
|
sectorOffset = 16;
|
2018-01-01 21:32:12 +00:00
|
|
|
sectorSize = 8;
|
|
|
|
|
sectorSkip = 2328;
|
2017-12-19 20:33:03 +00:00
|
|
|
break;
|
|
|
|
|
}
|
2017-12-20 17:15:26 +00:00
|
|
|
case SectorTagType.CdSectorEcc:
|
2017-12-19 20:33:03 +00:00
|
|
|
{
|
2017-12-20 17:15:26 +00:00
|
|
|
sectorOffset = 2076;
|
2018-01-01 21:32:12 +00:00
|
|
|
sectorSize = 276;
|
|
|
|
|
sectorSkip = 0;
|
2017-12-19 20:33:03 +00:00
|
|
|
break;
|
|
|
|
|
}
|
2017-12-20 17:15:26 +00:00
|
|
|
case SectorTagType.CdSectorEccP:
|
2017-12-19 20:33:03 +00:00
|
|
|
{
|
2017-12-20 17:15:26 +00:00
|
|
|
sectorOffset = 2076;
|
2018-01-01 21:32:12 +00:00
|
|
|
sectorSize = 172;
|
|
|
|
|
sectorSkip = 104;
|
2017-12-19 20:33:03 +00:00
|
|
|
break;
|
|
|
|
|
}
|
2017-12-20 17:15:26 +00:00
|
|
|
case SectorTagType.CdSectorEccQ:
|
2017-12-19 20:33:03 +00:00
|
|
|
{
|
2017-12-20 17:15:26 +00:00
|
|
|
sectorOffset = 2248;
|
2018-01-01 21:32:12 +00:00
|
|
|
sectorSize = 104;
|
|
|
|
|
sectorSkip = 0;
|
2017-12-19 20:33:03 +00:00
|
|
|
break;
|
|
|
|
|
}
|
2017-12-20 17:15:26 +00:00
|
|
|
case SectorTagType.CdSectorEdc:
|
2017-12-19 20:33:03 +00:00
|
|
|
{
|
2017-12-20 17:15:26 +00:00
|
|
|
sectorOffset = 2072;
|
2018-01-01 21:32:12 +00:00
|
|
|
sectorSize = 4;
|
|
|
|
|
sectorSkip = 276;
|
2017-12-19 20:33:03 +00:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
default: throw new ArgumentException("Unsupported tag requested", nameof(tag));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
break;
|
2017-12-20 17:15:26 +00:00
|
|
|
case TrackType.CdMode2Form2:
|
2017-12-19 20:33:03 +00:00
|
|
|
switch(tag)
|
|
|
|
|
{
|
2017-12-20 17:15:26 +00:00
|
|
|
case SectorTagType.CdSectorSync:
|
2017-12-19 20:33:03 +00:00
|
|
|
{
|
2017-12-20 17:15:26 +00:00
|
|
|
sectorOffset = 0;
|
2018-01-01 21:32:12 +00:00
|
|
|
sectorSize = 12;
|
|
|
|
|
sectorSkip = 2340;
|
2017-12-19 20:33:03 +00:00
|
|
|
break;
|
|
|
|
|
}
|
2017-12-20 17:15:26 +00:00
|
|
|
case SectorTagType.CdSectorHeader:
|
2017-12-19 20:33:03 +00:00
|
|
|
{
|
2017-12-20 17:15:26 +00:00
|
|
|
sectorOffset = 12;
|
2018-01-01 21:32:12 +00:00
|
|
|
sectorSize = 4;
|
|
|
|
|
sectorSkip = 2336;
|
2017-12-19 20:33:03 +00:00
|
|
|
break;
|
|
|
|
|
}
|
2017-12-20 17:15:26 +00:00
|
|
|
case SectorTagType.CdSectorSubHeader:
|
2017-12-19 20:33:03 +00:00
|
|
|
{
|
2017-12-20 17:15:26 +00:00
|
|
|
sectorOffset = 16;
|
2018-01-01 21:32:12 +00:00
|
|
|
sectorSize = 8;
|
|
|
|
|
sectorSkip = 2328;
|
2017-12-19 20:33:03 +00:00
|
|
|
break;
|
|
|
|
|
}
|
2017-12-20 17:15:26 +00:00
|
|
|
case SectorTagType.CdSectorEdc:
|
2017-12-19 20:33:03 +00:00
|
|
|
{
|
2017-12-20 17:15:26 +00:00
|
|
|
sectorOffset = 2348;
|
2018-01-01 21:32:12 +00:00
|
|
|
sectorSize = 4;
|
|
|
|
|
sectorSkip = 0;
|
2017-12-19 20:33:03 +00:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
default: throw new ArgumentException("Unsupported tag requested", nameof(tag));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
break;
|
2018-01-01 21:32:12 +00:00
|
|
|
case TrackType.Audio: { throw new ArgumentException("Unsupported tag requested", nameof(tag)); }
|
|
|
|
|
default:
|
|
|
|
|
throw new FeatureSupportedButNotImplementedImageException("Unsupported track type");
|
2017-12-19 20:33:03 +00:00
|
|
|
}
|
|
|
|
|
|
2017-12-20 17:15:26 +00:00
|
|
|
buffer = new byte[sectorSize * length];
|
2017-12-19 20:33:03 +00:00
|
|
|
|
2017-12-22 06:55:04 +00:00
|
|
|
dataStream.Seek((long)(dicTrack.TrackFileOffset + sectorAddress * 2352), SeekOrigin.Begin);
|
2017-12-20 17:15:26 +00:00
|
|
|
if(sectorOffset == 0 && sectorSkip == 0) dataStream.Read(buffer, 0, buffer.Length);
|
2017-12-19 20:33:03 +00:00
|
|
|
else
|
|
|
|
|
for(int i = 0; i < length; i++)
|
|
|
|
|
{
|
2017-12-20 17:15:26 +00:00
|
|
|
byte[] sector = new byte[sectorSize];
|
|
|
|
|
dataStream.Seek(sectorOffset, SeekOrigin.Current);
|
2017-12-19 20:33:03 +00:00
|
|
|
dataStream.Read(sector, 0, sector.Length);
|
2017-12-20 17:15:26 +00:00
|
|
|
dataStream.Seek(sectorSkip, SeekOrigin.Current);
|
|
|
|
|
Array.Copy(sector, 0, buffer, i * sectorSize, sectorSize);
|
2017-12-19 20:33:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return buffer;
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-26 07:28:40 +00:00
|
|
|
public byte[] ReadSectorLong(ulong sectorAddress)
|
2017-12-19 20:33:03 +00:00
|
|
|
{
|
|
|
|
|
return ReadSectorsLong(sectorAddress, 1);
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-26 07:28:40 +00:00
|
|
|
public byte[] ReadSectorLong(ulong sectorAddress, uint track)
|
2017-12-19 20:33:03 +00:00
|
|
|
{
|
|
|
|
|
return ReadSectorsLong(sectorAddress, 1, track);
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-26 07:28:40 +00:00
|
|
|
public byte[] ReadSectorsLong(ulong sectorAddress, uint length)
|
2017-12-19 20:33:03 +00:00
|
|
|
{
|
2017-12-24 00:12:31 +00:00
|
|
|
foreach(KeyValuePair<uint, ulong> kvp in from kvp in offsetmap
|
|
|
|
|
where sectorAddress >= kvp.Value
|
2018-01-01 21:32:12 +00:00
|
|
|
from track in Tracks
|
2018-06-22 08:08:38 +01:00
|
|
|
where track.TrackSequence == kvp.Key
|
|
|
|
|
where sectorAddress - kvp.Value <
|
2017-12-24 00:12:31 +00:00
|
|
|
track.TrackEndSector - track.TrackStartSector + 1
|
|
|
|
|
select kvp)
|
|
|
|
|
return ReadSectorsLong(sectorAddress - kvp.Value, length, kvp.Key);
|
2017-12-19 20:33:03 +00:00
|
|
|
|
2017-12-21 17:58:51 +00:00
|
|
|
throw new ArgumentOutOfRangeException(nameof(sectorAddress), $"Sector address {sectorAddress} not found");
|
2017-12-19 20:33:03 +00:00
|
|
|
}
|
|
|
|
|
|
2017-12-26 07:28:40 +00:00
|
|
|
public byte[] ReadSectorsLong(ulong sectorAddress, uint length, uint track)
|
2017-12-19 20:33:03 +00:00
|
|
|
{
|
2017-12-22 06:55:04 +00:00
|
|
|
Track dicTrack = new Track {TrackSequence = 0};
|
2017-12-19 20:33:03 +00:00
|
|
|
|
2018-01-01 21:32:12 +00:00
|
|
|
foreach(Track linqTrack in Tracks.Where(linqTrack => linqTrack.TrackSequence == track))
|
2017-12-24 00:12:31 +00:00
|
|
|
{
|
2017-12-22 06:55:04 +00:00
|
|
|
dicTrack = linqTrack;
|
2017-12-21 07:08:26 +00:00
|
|
|
break;
|
|
|
|
|
}
|
2017-12-19 20:33:03 +00:00
|
|
|
|
2017-12-22 06:55:04 +00:00
|
|
|
if(dicTrack.TrackSequence == 0)
|
2017-12-19 20:33:03 +00:00
|
|
|
throw new ArgumentOutOfRangeException(nameof(track), "Track does not exist in disc image");
|
|
|
|
|
|
2017-12-22 06:55:04 +00:00
|
|
|
if(length + sectorAddress - 1 > dicTrack.TrackEndSector)
|
2017-12-19 20:33:03 +00:00
|
|
|
throw new ArgumentOutOfRangeException(nameof(length),
|
2017-12-22 06:55:04 +00:00
|
|
|
$"Requested more sectors ({length + sectorAddress}) than present in track ({dicTrack.TrackEndSector}), won't cross tracks");
|
2017-12-19 20:33:03 +00:00
|
|
|
|
|
|
|
|
byte[] buffer = new byte[2352 * length];
|
|
|
|
|
|
2017-12-22 06:55:04 +00:00
|
|
|
dataStream.Seek((long)(dicTrack.TrackFileOffset + sectorAddress * 2352), SeekOrigin.Begin);
|
2017-12-19 20:33:03 +00:00
|
|
|
dataStream.Read(buffer, 0, buffer.Length);
|
|
|
|
|
|
|
|
|
|
return buffer;
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-26 07:28:40 +00:00
|
|
|
public List<Track> GetSessionTracks(Session session)
|
2017-12-19 20:33:03 +00:00
|
|
|
{
|
2018-01-01 21:32:12 +00:00
|
|
|
if(Sessions.Contains(session)) return GetSessionTracks(session.SessionSequence);
|
2017-12-19 20:33:03 +00:00
|
|
|
|
|
|
|
|
throw new ImageNotSupportedException("Session does not exist in disc image");
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-26 07:28:40 +00:00
|
|
|
public List<Track> GetSessionTracks(ushort session)
|
2017-12-19 20:33:03 +00:00
|
|
|
{
|
2018-01-01 21:32:12 +00:00
|
|
|
return Tracks.Where(track => track.TrackSession == session).ToList();
|
2017-12-19 20:33:03 +00:00
|
|
|
}
|
|
|
|
|
|
2017-12-26 07:28:40 +00:00
|
|
|
public bool? VerifySector(ulong sectorAddress)
|
2017-12-19 20:33:03 +00:00
|
|
|
{
|
|
|
|
|
byte[] buffer = ReadSectorLong(sectorAddress);
|
2017-12-21 14:30:38 +00:00
|
|
|
return CdChecksums.CheckCdSector(buffer);
|
2017-12-19 20:33:03 +00:00
|
|
|
}
|
|
|
|
|
|
2017-12-26 07:28:40 +00:00
|
|
|
public bool? VerifySector(ulong sectorAddress, uint track)
|
2017-12-19 20:33:03 +00:00
|
|
|
{
|
|
|
|
|
byte[] buffer = ReadSectorLong(sectorAddress, track);
|
2017-12-21 14:30:38 +00:00
|
|
|
return CdChecksums.CheckCdSector(buffer);
|
2017-12-19 20:33:03 +00:00
|
|
|
}
|
|
|
|
|
|
2018-06-22 08:08:38 +01:00
|
|
|
public bool? VerifySectors(ulong sectorAddress, uint length, out List<ulong> failingLbas,
|
|
|
|
|
out List<ulong> unknownLbas)
|
2017-12-19 20:33:03 +00:00
|
|
|
{
|
|
|
|
|
byte[] buffer = ReadSectorsLong(sectorAddress, length);
|
2018-01-01 21:32:12 +00:00
|
|
|
int bps = (int)(buffer.Length / length);
|
2017-12-19 20:33:03 +00:00
|
|
|
byte[] sector = new byte[bps];
|
2018-06-22 08:08:38 +01:00
|
|
|
failingLbas = new List<ulong>();
|
|
|
|
|
unknownLbas = new List<ulong>();
|
2017-12-19 20:33:03 +00:00
|
|
|
|
|
|
|
|
for(int i = 0; i < length; i++)
|
|
|
|
|
{
|
|
|
|
|
Array.Copy(buffer, i * bps, sector, 0, bps);
|
2017-12-21 14:30:38 +00:00
|
|
|
bool? sectorStatus = CdChecksums.CheckCdSector(sector);
|
2017-12-19 20:33:03 +00:00
|
|
|
|
|
|
|
|
switch(sectorStatus)
|
|
|
|
|
{
|
|
|
|
|
case null:
|
2017-12-20 17:15:26 +00:00
|
|
|
unknownLbas.Add((ulong)i + sectorAddress);
|
2017-12-19 20:33:03 +00:00
|
|
|
break;
|
|
|
|
|
case false:
|
2017-12-20 17:15:26 +00:00
|
|
|
failingLbas.Add((ulong)i + sectorAddress);
|
2017-12-19 20:33:03 +00:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-20 17:15:26 +00:00
|
|
|
if(unknownLbas.Count > 0) return null;
|
2017-12-26 02:51:10 +00:00
|
|
|
|
2017-12-24 00:12:31 +00:00
|
|
|
return failingLbas.Count <= 0;
|
2017-12-19 20:33:03 +00:00
|
|
|
}
|
|
|
|
|
|
2018-06-22 08:08:38 +01:00
|
|
|
public bool? VerifySectors(ulong sectorAddress, uint length, uint track, out List<ulong> failingLbas,
|
|
|
|
|
out List<ulong> unknownLbas)
|
2017-12-19 20:33:03 +00:00
|
|
|
{
|
|
|
|
|
byte[] buffer = ReadSectorsLong(sectorAddress, length, track);
|
2018-01-01 21:32:12 +00:00
|
|
|
int bps = (int)(buffer.Length / length);
|
2017-12-19 20:33:03 +00:00
|
|
|
byte[] sector = new byte[bps];
|
2018-06-22 08:08:38 +01:00
|
|
|
failingLbas = new List<ulong>();
|
|
|
|
|
unknownLbas = new List<ulong>();
|
2017-12-19 20:33:03 +00:00
|
|
|
|
|
|
|
|
for(int i = 0; i < length; i++)
|
|
|
|
|
{
|
|
|
|
|
Array.Copy(buffer, i * bps, sector, 0, bps);
|
2017-12-21 14:30:38 +00:00
|
|
|
bool? sectorStatus = CdChecksums.CheckCdSector(sector);
|
2017-12-19 20:33:03 +00:00
|
|
|
|
|
|
|
|
switch(sectorStatus)
|
|
|
|
|
{
|
|
|
|
|
case null:
|
2017-12-20 17:15:26 +00:00
|
|
|
unknownLbas.Add((ulong)i + sectorAddress);
|
2017-12-19 20:33:03 +00:00
|
|
|
break;
|
|
|
|
|
case false:
|
2017-12-20 17:15:26 +00:00
|
|
|
failingLbas.Add((ulong)i + sectorAddress);
|
2017-12-19 20:33:03 +00:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-20 17:15:26 +00:00
|
|
|
if(unknownLbas.Count > 0) return null;
|
2017-12-26 02:51:10 +00:00
|
|
|
|
2017-12-24 00:12:31 +00:00
|
|
|
return failingLbas.Count <= 0;
|
2017-12-19 20:33:03 +00:00
|
|
|
}
|
|
|
|
|
|
2017-12-26 07:28:40 +00:00
|
|
|
public bool? VerifyMediaImage()
|
2017-12-19 20:33:03 +00:00
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2018-01-01 21:32:12 +00:00
|
|
|
|
2018-01-28 20:29:46 +00:00
|
|
|
public List<DumpHardwareType> DumpHardware => null;
|
|
|
|
|
public CICMMetadataType CicmMetadata => null;
|
|
|
|
|
|
2018-06-22 08:08:38 +01:00
|
|
|
public IEnumerable<MediaTagType> SupportedMediaTags => new[] {MediaTagType.CD_MCN, MediaTagType.CD_FullTOC};
|
2018-01-07 13:45:36 +00:00
|
|
|
public IEnumerable<SectorTagType> SupportedSectorTags =>
|
|
|
|
|
new[]
|
|
|
|
|
{
|
|
|
|
|
SectorTagType.CdSectorEcc, SectorTagType.CdSectorEccP, SectorTagType.CdSectorEccQ,
|
|
|
|
|
SectorTagType.CdSectorEdc, SectorTagType.CdSectorHeader, SectorTagType.CdSectorSubHeader,
|
|
|
|
|
SectorTagType.CdSectorSync, SectorTagType.CdTrackFlags, SectorTagType.CdSectorSubchannel
|
|
|
|
|
};
|
|
|
|
|
public IEnumerable<MediaType> SupportedMediaTypes =>
|
|
|
|
|
new[]
|
|
|
|
|
{
|
|
|
|
|
MediaType.CD, MediaType.CDDA, MediaType.CDEG, MediaType.CDG, MediaType.CDI, MediaType.CDMIDI,
|
|
|
|
|
MediaType.CDMRW, MediaType.CDPLUS, MediaType.CDR, MediaType.CDROM, MediaType.CDROMXA, MediaType.CDRW,
|
|
|
|
|
MediaType.CDV, MediaType.DDCD, MediaType.DDCDR, MediaType.DDCDRW, MediaType.DTSCD, MediaType.JaguarCD,
|
|
|
|
|
MediaType.MEGACD, MediaType.PS1CD, MediaType.PS2CD, MediaType.SuperCDROM2, MediaType.SVCD,
|
2018-02-06 03:13:49 +00:00
|
|
|
MediaType.SATURNCD, MediaType.ThreeDO, MediaType.VCD, MediaType.VCDHD, MediaType.NeoGeoCD,
|
|
|
|
|
MediaType.PCFX
|
2018-01-07 13:45:36 +00:00
|
|
|
};
|
|
|
|
|
public IEnumerable<(string name, Type type, string description)> SupportedOptions =>
|
|
|
|
|
new (string name, Type type, string description)[] { };
|
|
|
|
|
public IEnumerable<string> KnownExtensions => new[] {".ccd"};
|
|
|
|
|
public bool IsWriting { get; private set; }
|
|
|
|
|
public string ErrorMessage { get; private set; }
|
|
|
|
|
|
|
|
|
|
public bool Create(string path, MediaType mediaType, Dictionary<string, string> options, ulong sectors,
|
|
|
|
|
uint sectorSize)
|
|
|
|
|
{
|
|
|
|
|
if(!SupportedMediaTypes.Contains(mediaType))
|
|
|
|
|
{
|
|
|
|
|
ErrorMessage = $"Unsupport media format {mediaType}";
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
imageInfo = new ImageInfo {MediaType = mediaType, SectorSize = sectorSize, Sectors = sectors};
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
writingBaseName = Path.Combine(Path.GetDirectoryName(path), Path.GetFileNameWithoutExtension(path));
|
|
|
|
|
descriptorStream = new StreamWriter(path, false, Encoding.ASCII);
|
2018-06-22 08:08:38 +01:00
|
|
|
dataStream = new FileStream(writingBaseName + ".img", FileMode.OpenOrCreate, FileAccess.ReadWrite,
|
|
|
|
|
FileShare.None);
|
2018-01-07 13:45:36 +00:00
|
|
|
}
|
|
|
|
|
catch(IOException e)
|
|
|
|
|
{
|
|
|
|
|
ErrorMessage = $"Could not create new image file, exception {e.Message}";
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
imageInfo.MediaType = mediaType;
|
|
|
|
|
|
|
|
|
|
trackFlags = new Dictionary<byte, byte>();
|
|
|
|
|
|
|
|
|
|
IsWriting = true;
|
|
|
|
|
ErrorMessage = null;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool WriteMediaTag(byte[] data, MediaTagType tag)
|
|
|
|
|
{
|
|
|
|
|
if(!IsWriting)
|
|
|
|
|
{
|
|
|
|
|
ErrorMessage = "Tried to write on a non-writable image";
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch(tag)
|
|
|
|
|
{
|
|
|
|
|
case MediaTagType.CD_MCN:
|
|
|
|
|
catalog = Encoding.ASCII.GetString(data);
|
|
|
|
|
return true;
|
|
|
|
|
case MediaTagType.CD_FullTOC:
|
2018-06-23 19:41:23 +01:00
|
|
|
fulltoc = new byte[data.Length + 2];
|
|
|
|
|
Array.Copy(data, 0, fulltoc, 2, data.Length);
|
|
|
|
|
fulltoc[0] = (byte)((data.Length & 0xFF00) >> 8);
|
|
|
|
|
fulltoc[1] = (byte)(data.Length & 0xFF);
|
2018-01-07 13:45:36 +00:00
|
|
|
return true;
|
|
|
|
|
default:
|
|
|
|
|
ErrorMessage = $"Unsupported media tag {tag}";
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool WriteSector(byte[] data, ulong sectorAddress)
|
|
|
|
|
{
|
|
|
|
|
if(!IsWriting)
|
|
|
|
|
{
|
|
|
|
|
ErrorMessage = "Tried to write on a non-writable image";
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TODO: Implement ECC generation
|
|
|
|
|
ErrorMessage = "This format requires sectors to be raw. Generating ECC is not yet implemented";
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool WriteSectors(byte[] data, ulong sectorAddress, uint length)
|
|
|
|
|
{
|
|
|
|
|
if(!IsWriting)
|
|
|
|
|
{
|
|
|
|
|
ErrorMessage = "Tried to write on a non-writable image";
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TODO: Implement ECC generation
|
|
|
|
|
ErrorMessage = "This format requires sectors to be raw. Generating ECC is not yet implemented";
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool WriteSectorLong(byte[] data, ulong sectorAddress)
|
|
|
|
|
{
|
|
|
|
|
if(!IsWriting)
|
|
|
|
|
{
|
|
|
|
|
ErrorMessage = "Tried to write on a non-writable image";
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Track track =
|
|
|
|
|
Tracks.FirstOrDefault(trk => sectorAddress >= trk.TrackStartSector &&
|
|
|
|
|
sectorAddress <= trk.TrackEndSector);
|
|
|
|
|
|
|
|
|
|
if(track.TrackSequence == 0)
|
|
|
|
|
{
|
|
|
|
|
ErrorMessage = $"Can't found track containing {sectorAddress}";
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(data.Length != track.TrackRawBytesPerSector)
|
|
|
|
|
{
|
|
|
|
|
ErrorMessage = "Incorrect data size";
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
dataStream.Seek((long)(track.TrackFileOffset + (sectorAddress - track.TrackStartSector) * (ulong)track.TrackRawBytesPerSector),
|
|
|
|
|
SeekOrigin.Begin);
|
|
|
|
|
dataStream.Write(data, 0, data.Length);
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool WriteSectorsLong(byte[] data, ulong sectorAddress, uint length)
|
|
|
|
|
{
|
|
|
|
|
if(!IsWriting)
|
|
|
|
|
{
|
|
|
|
|
ErrorMessage = "Tried to write on a non-writable image";
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Track track =
|
|
|
|
|
Tracks.FirstOrDefault(trk => sectorAddress >= trk.TrackStartSector &&
|
|
|
|
|
sectorAddress <= trk.TrackEndSector);
|
|
|
|
|
|
|
|
|
|
if(track.TrackSequence == 0)
|
|
|
|
|
{
|
|
|
|
|
ErrorMessage = $"Can't found track containing {sectorAddress}";
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(sectorAddress + length > track.TrackEndSector + 1)
|
|
|
|
|
{
|
|
|
|
|
ErrorMessage = "Can't cross tracks";
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(data.Length % track.TrackRawBytesPerSector != 0)
|
|
|
|
|
{
|
|
|
|
|
ErrorMessage = "Incorrect data size";
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
dataStream.Seek((long)(track.TrackFileOffset + (sectorAddress - track.TrackStartSector) * (ulong)track.TrackRawBytesPerSector),
|
|
|
|
|
SeekOrigin.Begin);
|
|
|
|
|
dataStream.Write(data, 0, data.Length);
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool SetTracks(List<Track> tracks)
|
|
|
|
|
{
|
|
|
|
|
ulong currentDataOffset = 0;
|
|
|
|
|
ulong currentSubchannelOffset = 0;
|
|
|
|
|
|
|
|
|
|
Tracks = new List<Track>();
|
|
|
|
|
foreach(Track track in tracks.OrderBy(t => t.TrackSequence))
|
|
|
|
|
{
|
|
|
|
|
Track newTrack = track;
|
|
|
|
|
uint subchannelSize;
|
|
|
|
|
switch(track.TrackSubchannelType)
|
|
|
|
|
{
|
|
|
|
|
case TrackSubchannelType.None:
|
|
|
|
|
subchannelSize = 0;
|
|
|
|
|
break;
|
|
|
|
|
case TrackSubchannelType.Raw:
|
|
|
|
|
case TrackSubchannelType.RawInterleaved:
|
|
|
|
|
subchannelSize = 96;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
ErrorMessage = $"Unsupported subchannel type {track.TrackSubchannelType}";
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
newTrack.TrackFileOffset = currentDataOffset;
|
|
|
|
|
newTrack.TrackSubchannelOffset = currentSubchannelOffset;
|
|
|
|
|
|
|
|
|
|
currentDataOffset += (ulong)newTrack.TrackRawBytesPerSector *
|
2018-06-22 08:08:38 +01:00
|
|
|
(newTrack.TrackEndSector - newTrack.TrackStartSector + 1);
|
2018-01-07 13:45:36 +00:00
|
|
|
currentSubchannelOffset += subchannelSize * (newTrack.TrackEndSector - newTrack.TrackStartSector + 1);
|
|
|
|
|
|
|
|
|
|
Tracks.Add(newTrack);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool Close()
|
|
|
|
|
{
|
|
|
|
|
if(!IsWriting)
|
|
|
|
|
{
|
|
|
|
|
ErrorMessage = "Image is not opened for writing";
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
dataStream.Flush();
|
|
|
|
|
dataStream.Close();
|
|
|
|
|
|
|
|
|
|
subStream?.Flush();
|
|
|
|
|
subStream?.Close();
|
|
|
|
|
|
2018-06-22 08:08:38 +01:00
|
|
|
FullTOC.CDFullTOC? nullableToc = null;
|
2018-06-20 22:22:21 +01:00
|
|
|
FullTOC.CDFullTOC toc;
|
2018-01-07 13:45:36 +00:00
|
|
|
|
|
|
|
|
// Easy, just decode the real toc
|
|
|
|
|
if(fulltoc != null) nullableToc = FullTOC.Decode(fulltoc);
|
|
|
|
|
|
|
|
|
|
// Not easy, create a toc from scratch
|
|
|
|
|
if(nullableToc == null)
|
|
|
|
|
{
|
2018-06-22 08:08:38 +01:00
|
|
|
toc = new FullTOC.CDFullTOC();
|
|
|
|
|
Dictionary<byte, byte> sessionEndingTrack = new Dictionary<byte, byte>();
|
|
|
|
|
toc.FirstCompleteSession = byte.MaxValue;
|
|
|
|
|
toc.LastCompleteSession = byte.MinValue;
|
2018-01-07 13:45:36 +00:00
|
|
|
List<FullTOC.TrackDataDescriptor> trackDescriptors = new List<FullTOC.TrackDataDescriptor>();
|
|
|
|
|
byte currentTrack = 0;
|
|
|
|
|
|
|
|
|
|
foreach(Track track in Tracks.OrderBy(t => t.TrackSession).ThenBy(t => t.TrackSequence))
|
|
|
|
|
{
|
|
|
|
|
if(track.TrackSession < toc.FirstCompleteSession)
|
|
|
|
|
toc.FirstCompleteSession = (byte)track.TrackSession;
|
|
|
|
|
|
|
|
|
|
if(track.TrackSession <= toc.LastCompleteSession)
|
|
|
|
|
{
|
|
|
|
|
currentTrack = (byte)track.TrackSequence;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(toc.LastCompleteSession > 0) sessionEndingTrack.Add(toc.LastCompleteSession, currentTrack);
|
|
|
|
|
|
|
|
|
|
toc.LastCompleteSession = (byte)track.TrackSession;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
byte currentSession = 0;
|
|
|
|
|
|
|
|
|
|
foreach(Track track in Tracks.OrderBy(t => t.TrackSession).ThenBy(t => t.TrackSequence))
|
|
|
|
|
{
|
|
|
|
|
trackFlags.TryGetValue((byte)track.TrackSequence, out byte trackControl);
|
|
|
|
|
|
|
|
|
|
if(trackControl == 0 && track.TrackType != TrackType.Audio) trackControl = (byte)CdFlags.DataTrack;
|
|
|
|
|
|
|
|
|
|
// Lead-Out
|
|
|
|
|
if(track.TrackSession > currentSession && currentSession != 0)
|
|
|
|
|
{
|
|
|
|
|
(byte hour, byte minute, byte second, byte frame) leadoutAmsf =
|
|
|
|
|
LbaToMsf(track.TrackStartSector - 150);
|
|
|
|
|
(byte hour, byte minute, byte second, byte frame) leadoutPmsf =
|
|
|
|
|
LbaToMsf(Tracks.OrderBy(t => t.TrackSession).ThenBy(t => t.TrackSequence).Last()
|
|
|
|
|
.TrackStartSector);
|
|
|
|
|
|
|
|
|
|
// Lead-out
|
|
|
|
|
trackDescriptors.Add(new FullTOC.TrackDataDescriptor
|
|
|
|
|
{
|
|
|
|
|
SessionNumber = currentSession,
|
|
|
|
|
POINT = 0xB0,
|
|
|
|
|
ADR = 5,
|
|
|
|
|
CONTROL = 0,
|
|
|
|
|
HOUR = leadoutAmsf.hour,
|
|
|
|
|
Min = leadoutAmsf.minute,
|
|
|
|
|
Sec = leadoutAmsf.second,
|
|
|
|
|
Frame = leadoutAmsf.frame,
|
|
|
|
|
PHOUR = leadoutPmsf.hour,
|
|
|
|
|
PMIN = leadoutPmsf.minute,
|
|
|
|
|
PSEC = leadoutPmsf.second,
|
|
|
|
|
PFRAME = leadoutPmsf.frame
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// This seems to be constant? It should not exist on CD-ROM but CloneCD creates them anyway
|
|
|
|
|
// Format seems like ATIP, but ATIP should not be as 0xC0 in TOC...
|
|
|
|
|
trackDescriptors.Add(new FullTOC.TrackDataDescriptor
|
|
|
|
|
{
|
|
|
|
|
SessionNumber = currentSession,
|
|
|
|
|
POINT = 0xC0,
|
|
|
|
|
ADR = 5,
|
|
|
|
|
CONTROL = 0,
|
|
|
|
|
Min = 128,
|
|
|
|
|
PMIN = 97,
|
|
|
|
|
PSEC = 25
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Lead-in
|
|
|
|
|
if(track.TrackSession > currentSession)
|
|
|
|
|
{
|
|
|
|
|
currentSession = (byte)track.TrackSession;
|
|
|
|
|
sessionEndingTrack.TryGetValue(currentSession, out byte endingTrackNumber);
|
|
|
|
|
(byte hour, byte minute, byte second, byte frame) leadinPmsf =
|
|
|
|
|
LbaToMsf(Tracks.FirstOrDefault(t => t.TrackSequence == endingTrackNumber).TrackEndSector +
|
|
|
|
|
1);
|
|
|
|
|
|
|
|
|
|
// Starting track
|
|
|
|
|
trackDescriptors.Add(new FullTOC.TrackDataDescriptor
|
|
|
|
|
{
|
|
|
|
|
SessionNumber = currentSession,
|
|
|
|
|
POINT = 0xA0,
|
|
|
|
|
ADR = 1,
|
|
|
|
|
CONTROL = trackControl,
|
|
|
|
|
PMIN = (byte)track.TrackSequence
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Ending track
|
|
|
|
|
trackDescriptors.Add(new FullTOC.TrackDataDescriptor
|
|
|
|
|
{
|
|
|
|
|
SessionNumber = currentSession,
|
|
|
|
|
POINT = 0xA1,
|
|
|
|
|
ADR = 1,
|
|
|
|
|
CONTROL = trackControl,
|
|
|
|
|
PMIN = endingTrackNumber
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Lead-out start
|
|
|
|
|
trackDescriptors.Add(new FullTOC.TrackDataDescriptor
|
|
|
|
|
{
|
|
|
|
|
SessionNumber = currentSession,
|
|
|
|
|
POINT = 0xA2,
|
|
|
|
|
ADR = 1,
|
|
|
|
|
CONTROL = trackControl,
|
|
|
|
|
PHOUR = leadinPmsf.hour,
|
|
|
|
|
PMIN = leadinPmsf.minute,
|
|
|
|
|
PSEC = leadinPmsf.second,
|
|
|
|
|
PFRAME = leadinPmsf.frame
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
(byte hour, byte minute, byte second, byte frame) pmsf = LbaToMsf(track.TrackStartSector);
|
|
|
|
|
|
|
|
|
|
// Track
|
|
|
|
|
trackDescriptors.Add(new FullTOC.TrackDataDescriptor
|
|
|
|
|
{
|
|
|
|
|
SessionNumber = (byte)track.TrackSession,
|
|
|
|
|
POINT = (byte)track.TrackSequence,
|
|
|
|
|
ADR = 1,
|
|
|
|
|
CONTROL = trackControl,
|
|
|
|
|
PHOUR = pmsf.hour,
|
|
|
|
|
PMIN = pmsf.minute,
|
|
|
|
|
PSEC = pmsf.second,
|
|
|
|
|
PFRAME = pmsf.frame
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
toc.TrackDescriptors = trackDescriptors.ToArray();
|
|
|
|
|
}
|
|
|
|
|
else toc = nullableToc.Value;
|
|
|
|
|
|
|
|
|
|
descriptorStream.WriteLine("[CloneCD]");
|
|
|
|
|
descriptorStream.WriteLine("Version=2");
|
|
|
|
|
descriptorStream.WriteLine("[Disc]");
|
|
|
|
|
descriptorStream.WriteLine("TocEntries={0}", toc.TrackDescriptors.Length);
|
|
|
|
|
descriptorStream.WriteLine("Sessions={0}", toc.LastCompleteSession);
|
|
|
|
|
descriptorStream.WriteLine("DataTracksScrambled=0");
|
|
|
|
|
descriptorStream.WriteLine("CDTextLength=0");
|
|
|
|
|
if(!string.IsNullOrEmpty(catalog)) descriptorStream.WriteLine("CATALOG={0}", catalog);
|
|
|
|
|
for(int i = 1; i <= toc.LastCompleteSession; i++)
|
|
|
|
|
{
|
|
|
|
|
descriptorStream.WriteLine("[Session {0}]", i);
|
|
|
|
|
descriptorStream.WriteLine("PreGapMode=0");
|
|
|
|
|
descriptorStream.WriteLine("PreGapSubC=0");
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-23 19:41:23 +01:00
|
|
|
if(nullableToc == null) System.Console.WriteLine("Using fake toc");
|
|
|
|
|
|
2018-01-07 13:45:36 +00:00
|
|
|
for(int i = 0; i < toc.TrackDescriptors.Length; i++)
|
|
|
|
|
{
|
2018-06-22 08:08:38 +01:00
|
|
|
long alba = MsfToLba((toc.TrackDescriptors[i].HOUR, toc.TrackDescriptors[i].Min,
|
|
|
|
|
toc.TrackDescriptors[i].Sec, toc.TrackDescriptors[i].Frame));
|
|
|
|
|
long plba = MsfToLba((toc.TrackDescriptors[i].PHOUR, toc.TrackDescriptors[i].PMIN,
|
|
|
|
|
toc.TrackDescriptors[i].PSEC, toc.TrackDescriptors[i].PFRAME));
|
2018-01-07 13:45:36 +00:00
|
|
|
|
|
|
|
|
descriptorStream.WriteLine("[Entry {0}]", i);
|
|
|
|
|
descriptorStream.WriteLine("Session={0}", toc.TrackDescriptors[i].SessionNumber);
|
|
|
|
|
descriptorStream.WriteLine("Point=0x{0:x2}", toc.TrackDescriptors[i].POINT);
|
|
|
|
|
descriptorStream.WriteLine("ADR=0x{0:x2}", toc.TrackDescriptors[i].ADR);
|
|
|
|
|
descriptorStream.WriteLine("Control=0x{0:x2}", toc.TrackDescriptors[i].CONTROL);
|
|
|
|
|
descriptorStream.WriteLine("TrackNo={0}", toc.TrackDescriptors[i].TNO);
|
|
|
|
|
descriptorStream.WriteLine("AMin={0}", toc.TrackDescriptors[i].Min);
|
|
|
|
|
descriptorStream.WriteLine("ASec={0}", toc.TrackDescriptors[i].Sec);
|
|
|
|
|
descriptorStream.WriteLine("AFrame={0}", toc.TrackDescriptors[i].Frame);
|
|
|
|
|
descriptorStream.WriteLine("ALBA={0}", toc.TrackDescriptors[i].POINT == 0xC0 ? 125850 : alba);
|
|
|
|
|
descriptorStream.WriteLine("Zero={0}",
|
|
|
|
|
((toc.TrackDescriptors[i].HOUR & 0x0F) << 4) +
|
|
|
|
|
(toc.TrackDescriptors[i].PHOUR & 0x0F));
|
|
|
|
|
descriptorStream.WriteLine("PMin={0}", toc.TrackDescriptors[i].PMIN);
|
|
|
|
|
descriptorStream.WriteLine("PSec={0}", toc.TrackDescriptors[i].PSEC);
|
|
|
|
|
descriptorStream.WriteLine("PFrame={0}", toc.TrackDescriptors[i].PFRAME);
|
|
|
|
|
descriptorStream.WriteLine("PLBA={0}", toc.TrackDescriptors[i].POINT == 0xC0 ? -11775 : plba);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
descriptorStream.Flush();
|
|
|
|
|
descriptorStream.Close();
|
|
|
|
|
|
2018-01-18 23:18:56 +00:00
|
|
|
IsWriting = false;
|
2018-01-07 13:45:36 +00:00
|
|
|
ErrorMessage = "";
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool SetMetadata(ImageInfo metadata)
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool SetGeometry(uint cylinders, uint heads, uint sectorsPerTrack)
|
|
|
|
|
{
|
|
|
|
|
ErrorMessage = "Unsupported feature";
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool WriteSectorTag(byte[] data, ulong sectorAddress, SectorTagType tag)
|
|
|
|
|
{
|
|
|
|
|
if(!IsWriting)
|
|
|
|
|
{
|
|
|
|
|
ErrorMessage = "Tried to write on a non-writable image";
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Track track =
|
|
|
|
|
Tracks.FirstOrDefault(trk => sectorAddress >= trk.TrackStartSector &&
|
|
|
|
|
sectorAddress <= trk.TrackEndSector);
|
|
|
|
|
|
|
|
|
|
if(track.TrackSequence == 0)
|
|
|
|
|
{
|
|
|
|
|
ErrorMessage = $"Can't found track containing {sectorAddress}";
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch(tag)
|
|
|
|
|
{
|
|
|
|
|
case SectorTagType.CdTrackFlags:
|
|
|
|
|
{
|
|
|
|
|
if(data.Length != 1)
|
|
|
|
|
{
|
|
|
|
|
ErrorMessage = "Incorrect data size for track flags";
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
trackFlags.Add((byte)track.TrackSequence, data[0]);
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
case SectorTagType.CdSectorSubchannel:
|
|
|
|
|
{
|
|
|
|
|
if(track.TrackSubchannelType == 0)
|
|
|
|
|
{
|
|
|
|
|
ErrorMessage =
|
|
|
|
|
$"Trying to write subchannel to track {track.TrackSequence}, that does not have subchannel";
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(data.Length != 96)
|
|
|
|
|
{
|
|
|
|
|
ErrorMessage = "Incorrect data size for subchannel";
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(subStream == null)
|
|
|
|
|
try
|
|
|
|
|
{
|
2018-01-18 23:18:56 +00:00
|
|
|
subStream = new FileStream(writingBaseName + ".sub", FileMode.OpenOrCreate,
|
2018-01-07 13:45:36 +00:00
|
|
|
FileAccess.ReadWrite, FileShare.None);
|
|
|
|
|
}
|
|
|
|
|
catch(IOException e)
|
|
|
|
|
{
|
|
|
|
|
ErrorMessage = $"Could not create subchannel file, exception {e.Message}";
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
subStream.Seek((long)(track.TrackSubchannelOffset + (sectorAddress - track.TrackStartSector) * 96),
|
|
|
|
|
SeekOrigin.Begin);
|
|
|
|
|
subStream.Write(data, 0, data.Length);
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
default:
|
|
|
|
|
ErrorMessage = $"Unsupported tag type {tag}";
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool WriteSectorsTag(byte[] data, ulong sectorAddress, uint length, SectorTagType tag)
|
|
|
|
|
{
|
|
|
|
|
if(!IsWriting)
|
|
|
|
|
{
|
|
|
|
|
ErrorMessage = "Tried to write on a non-writable image";
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Track track =
|
|
|
|
|
Tracks.FirstOrDefault(trk => sectorAddress >= trk.TrackStartSector &&
|
|
|
|
|
sectorAddress <= trk.TrackEndSector);
|
|
|
|
|
|
|
|
|
|
if(track.TrackSequence == 0)
|
|
|
|
|
{
|
|
|
|
|
ErrorMessage = $"Can't found track containing {sectorAddress}";
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch(tag)
|
|
|
|
|
{
|
|
|
|
|
case SectorTagType.CdTrackFlags:
|
|
|
|
|
case SectorTagType.CdTrackIsrc: return WriteSectorTag(data, sectorAddress, tag);
|
|
|
|
|
case SectorTagType.CdSectorSubchannel:
|
|
|
|
|
{
|
|
|
|
|
if(track.TrackSubchannelType == 0)
|
|
|
|
|
{
|
|
|
|
|
ErrorMessage =
|
|
|
|
|
$"Trying to write subchannel to track {track.TrackSequence}, that does not have subchannel";
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(data.Length % 96 != 0)
|
|
|
|
|
{
|
|
|
|
|
ErrorMessage = "Incorrect data size for subchannel";
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(subStream == null)
|
|
|
|
|
try
|
|
|
|
|
{
|
2018-01-18 23:18:56 +00:00
|
|
|
subStream = new FileStream(writingBaseName + ".sub", FileMode.OpenOrCreate,
|
2018-01-07 13:45:36 +00:00
|
|
|
FileAccess.ReadWrite, FileShare.None);
|
|
|
|
|
}
|
|
|
|
|
catch(IOException e)
|
|
|
|
|
{
|
|
|
|
|
ErrorMessage = $"Could not create subchannel file, exception {e.Message}";
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
subStream.Seek((long)(track.TrackSubchannelOffset + (sectorAddress - track.TrackStartSector) * 96),
|
|
|
|
|
SeekOrigin.Begin);
|
|
|
|
|
subStream.Write(data, 0, data.Length);
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
default:
|
|
|
|
|
ErrorMessage = $"Unsupported tag type {tag}";
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-28 20:29:46 +00:00
|
|
|
public bool SetDumpHardware(List<DumpHardwareType> dumpHardware)
|
|
|
|
|
{
|
|
|
|
|
// Not supported
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool SetCicmMetadata(CICMMetadataType metadata)
|
|
|
|
|
{
|
|
|
|
|
// Not supported
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-01 21:32:12 +00:00
|
|
|
static ulong GetLba(int hour, int minute, int second, int frame)
|
|
|
|
|
{
|
|
|
|
|
return (ulong)(hour * 60 * 60 * 75 + minute * 60 * 75 + second * 75 + frame - 150);
|
|
|
|
|
}
|
2018-01-07 13:45:36 +00:00
|
|
|
|
|
|
|
|
static long MsfToLba((byte hour, byte minute, byte second, byte frame) msf)
|
|
|
|
|
{
|
|
|
|
|
return msf.hour * 60 * 60 * 75 + msf.minute * 60 * 75 + msf.second * 75 + msf.frame - 150;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static (byte hour, byte minute, byte second, byte frame) LbaToMsf(ulong sector)
|
|
|
|
|
{
|
|
|
|
|
return ((byte)((sector + 150) / 75 / 60 / 60), (byte)((sector + 150) / 75 / 60 % 60),
|
2018-06-22 08:08:38 +01:00
|
|
|
(byte)((sector + 150) / 75 % 60),
|
|
|
|
|
(byte)((sector + 150) % 75));
|
2018-01-07 13:45:36 +00:00
|
|
|
}
|
2017-12-19 20:33:03 +00:00
|
|
|
}
|
|
|
|
|
}
|