Refactor image classes and split them to smaller files.

This commit is contained in:
2018-07-23 23:25:43 +01:00
parent 55ca2d23b6
commit ed88989642
445 changed files with 50086 additions and 34342 deletions

View File

@@ -0,0 +1,84 @@
// /***************************************************************************
// The Disc Image Chef
// ----------------------------------------------------------------------------
//
// Filename : CDRDAO.cs
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// Component : Disc image plugins.
//
// --[ Description ] ----------------------------------------------------------
//
// Manages cdrdao cuesheets (toc/bin).
//
// --[ 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/>.
//
// ----------------------------------------------------------------------------
// Copyright © 2011-2018 Natalia Portillo
// ****************************************************************************/
using System.Collections.Generic;
using System.IO;
using DiscImageChef.CommonTypes.Enums;
using DiscImageChef.CommonTypes.Interfaces;
using DiscImageChef.CommonTypes.Structs;
namespace DiscImageChef.DiscImages
{
// TODO: Doesn't support compositing from several files
// TODO: Doesn't support silences that are not in files
public partial class Cdrdao : IWritableImage
{
IFilter cdrdaoFilter;
StreamWriter descriptorStream;
CdrdaoDisc discimage;
ImageInfo imageInfo;
Stream imageStream;
/// <summary>Dictionary, index is track #, value is TrackFile</summary>
Dictionary<uint, ulong> offsetmap;
bool separateTracksWriting;
StreamReader tocStream;
Dictionary<byte, byte> trackFlags;
Dictionary<byte, string> trackIsrcs;
string writingBaseName;
Dictionary<uint, FileStream> writingStreams;
List<Track> writingTracks;
public Cdrdao()
{
imageInfo = new ImageInfo
{
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,
DriveFirmwareRevision = null
};
}
}
}

View File

@@ -0,0 +1,90 @@
// /***************************************************************************
// The Disc Image Chef
// ----------------------------------------------------------------------------
//
// Filename : Constants.cs
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// Component : Disk image plugins.
//
// --[ Description ] ----------------------------------------------------------
//
// Contains constants for cdrdao cuesheets (toc/bin).
//
// --[ 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/>.
//
// ----------------------------------------------------------------------------
// Copyright © 2011-2018 Natalia Portillo
// ****************************************************************************/
namespace DiscImageChef.DiscImages
{
public partial class Cdrdao
{
/// <summary>Audio track, 2352 bytes/sector</summary>
const string CDRDAO_TRACK_TYPE_AUDIO = "AUDIO";
/// <summary>Mode 1 track, cooked, 2048 bytes/sector</summary>
const string CDRDAO_TRACK_TYPE_MODE1 = "MODE1";
/// <summary>Mode 1 track, raw, 2352 bytes/sector</summary>
const string CDRDAO_TRACK_TYPE_MODE1_RAW = "MODE1_RAW";
/// <summary>Mode 2 mixed formless, cooked, 2336 bytes/sector</summary>
const string CDRDAO_TRACK_TYPE_MODE2 = "MODE2";
/// <summary>Mode 2 form 1 track, cooked, 2048 bytes/sector</summary>
const string CDRDAO_TRACK_TYPE_MODE2_FORM1 = "MODE2_FORM1";
/// <summary>Mode 2 form 2 track, cooked, 2324 bytes/sector</summary>
const string CDRDAO_TRACK_TYPE_MODE2_FORM2 = "MODE2_FORM2";
/// <summary>Mode 2 mixed forms track, cooked, 2336 bytes/sector</summary>
const string CDRDAO_TRACK_TYPE_MODE2_MIX = "MODE2_FORM_MIX";
/// <summary>Mode 2 track, raw, 2352 bytes/sector</summary>
const string CDRDAO_TRACK_TYPE_MODE2_RAW = "MODE2_RAW";
const string REGEX_COMMENT = @"^\s*\/{2}(?<comment>.+)$";
const string REGEX_COPY = @"^\s*(?<no>NO)?\s*COPY";
const string REGEX_DISCTYPE = @"^\s*(?<type>(CD_DA|CD_ROM_XA|CD_ROM|CD_I))";
const string REGEX_EMPHASIS = @"^\s*(?<no>NO)?\s*PRE_EMPHASIS";
const string REGEX_FILE_AUDIO =
@"^\s*(AUDIO)?FILE\s*""(?<filename>.+)""\s*(#(?<base_offset>\d+))?\s*((?<start>[\d]+:[\d]+:[\d]+)|(?<start_num>\d+))\s*(?<length>[\d]+:[\d]+:[\d]+)?";
const string REGEX_FILE_DATA =
@"^\s*DATAFILE\s*""(?<filename>.+)""\s*(#(?<base_offset>\d+))?\s*(?<length>[\d]+:[\d]+:[\d]+)?";
const string REGEX_INDEX = @"^\s*INDEX\s*(?<address>\d+:\d+:\d+)";
const string REGEX_ISRC = @"^\s*ISRC\s*""(?<isrc>[A-Z0-9]{5,5}[0-9]{7,7})""";
const string REGEX_MCN = @"^\s*CATALOG\s*""(?<catalog>[\x21-\x7F]{13,13})""";
const string REGEX_PREGAP = @"^\s*START\s*(?<address>\d+:\d+:\d+)?";
const string REGEX_STEREO = @"^\s*(?<num>(TWO|FOUR))_CHANNEL_AUDIO";
const string REGEX_TRACK =
@"^\s*TRACK\s*(?<type>(AUDIO|MODE1_RAW|MODE1|MODE2_FORM1|MODE2_FORM2|MODE2_FORM_MIX|MODE2_RAW|MODE2))\s*(?<subchan>(RW_RAW|RW))?";
const string REGEX_ZERO_AUDIO = @"^\s*SILENCE\s*(?<length>\d+:\d+:\d+)";
const string REGEX_ZERO_DATA = @"^\s*ZERO\s*(?<length>\d+:\d+:\d+)";
const string REGEX_ZERO_PREGAP = @"^\s*PREGAP\s*(?<length>\d+:\d+:\d+)";
// CD-Text
const string REGEX_ARRANGER = @"^\s*ARRANGER\s*""(?<arranger>.+)""";
const string REGEX_COMPOSER = @"^\s*COMPOSER\s*""(?<composer>.+)""";
const string REGEX_DISC_ID = @"^\s*DISC_ID\s*""(?<discid>.+)""";
const string REGEX_MESSAGE = @"^\s*MESSAGE\s*""(?<message>.+)""";
const string REGEX_PERFORMER = @"^\s*PERFORMER\s*""(?<performer>.+)""";
const string REGEX_SONGWRITER = @"^\s*SONGWRITER\s*""(?<songwriter>.+)""";
const string REGEX_TITLE = @"^\s*TITLE\s*""(?<title>.+)""";
const string REGEX_UPC = @"^\s*UPC_EAN\s*""(?<catalog>[\d]{13,13})""";
// Unused
const string REGEX_CD_TEXT = @"^\s*CD_TEXT\s*\{";
const string REGEX_CLOSURE = @"^\s*\}";
const string REGEX_LANGUAGE = @"^\s*LANGUAGE\s*(?<code>\d+)\s*\{";
const string REGEX_LANGUAGE_MAP = @"^\s*LANGUAGE_MAP\s*\{";
const string REGEX_LANGUAGE_MAPPING = @"^\s*(?<code>\d+)\s?\:\s?(?<language>\d+|\w+)";
}
}

View File

@@ -0,0 +1,114 @@
// /***************************************************************************
// The Disc Image Chef
// ----------------------------------------------------------------------------
//
// Filename : Helpers.cs
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// Component : Disk image plugins.
//
// --[ Description ] ----------------------------------------------------------
//
// Contains helpers for cdrdao cuesheets (toc/bin).
//
// --[ 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/>.
//
// ----------------------------------------------------------------------------
// Copyright © 2011-2018 Natalia Portillo
// ****************************************************************************/
using DiscImageChef.CommonTypes.Enums;
using DiscImageChef.CommonTypes.Structs;
namespace DiscImageChef.DiscImages
{
public partial class Cdrdao
{
static ushort CdrdaoTrackTypeToBytesPerSector(string trackType)
{
switch(trackType)
{
case CDRDAO_TRACK_TYPE_MODE1:
case CDRDAO_TRACK_TYPE_MODE2_FORM1: return 2048;
case CDRDAO_TRACK_TYPE_MODE2_FORM2: return 2324;
case CDRDAO_TRACK_TYPE_MODE2:
case CDRDAO_TRACK_TYPE_MODE2_MIX: return 2336;
case CDRDAO_TRACK_TYPE_AUDIO:
case CDRDAO_TRACK_TYPE_MODE1_RAW:
case CDRDAO_TRACK_TYPE_MODE2_RAW: return 2352;
default: return 0;
}
}
static ushort CdrdaoTrackTypeToCookedBytesPerSector(string trackType)
{
switch(trackType)
{
case CDRDAO_TRACK_TYPE_MODE1:
case CDRDAO_TRACK_TYPE_MODE2_FORM1:
case CDRDAO_TRACK_TYPE_MODE1_RAW: return 2048;
case CDRDAO_TRACK_TYPE_MODE2_FORM2: return 2324;
case CDRDAO_TRACK_TYPE_MODE2:
case CDRDAO_TRACK_TYPE_MODE2_MIX:
case CDRDAO_TRACK_TYPE_MODE2_RAW: return 2336;
case CDRDAO_TRACK_TYPE_AUDIO: return 2352;
default: return 0;
}
}
static TrackType CdrdaoTrackTypeToTrackType(string trackType)
{
switch(trackType)
{
case CDRDAO_TRACK_TYPE_MODE1:
case CDRDAO_TRACK_TYPE_MODE1_RAW: return TrackType.CdMode1;
case CDRDAO_TRACK_TYPE_MODE2_FORM1: return TrackType.CdMode2Form1;
case CDRDAO_TRACK_TYPE_MODE2_FORM2: return TrackType.CdMode2Form2;
case CDRDAO_TRACK_TYPE_MODE2:
case CDRDAO_TRACK_TYPE_MODE2_MIX:
case CDRDAO_TRACK_TYPE_MODE2_RAW: return TrackType.CdMode2Formless;
case CDRDAO_TRACK_TYPE_AUDIO: return TrackType.Audio;
default: return TrackType.Data;
}
}
static (byte minute, byte second, byte frame) LbaToMsf(ulong sector)
{
return ((byte)(sector / 75 / 60), (byte)(sector / 75 % 60), (byte)(sector % 75));
}
static string GetTrackMode(Track track)
{
switch(track.TrackType)
{
case TrackType.Audio when track.TrackRawBytesPerSector == 2352: return CDRDAO_TRACK_TYPE_AUDIO;
case TrackType.Data: return CDRDAO_TRACK_TYPE_MODE1;
case TrackType.CdMode1 when track.TrackRawBytesPerSector == 2352: return CDRDAO_TRACK_TYPE_MODE1_RAW;
case TrackType.CdMode2Formless
when track.TrackRawBytesPerSector != 2352: return CDRDAO_TRACK_TYPE_MODE2;
case TrackType.CdMode2Form1
when track.TrackRawBytesPerSector != 2352: return CDRDAO_TRACK_TYPE_MODE2_FORM1;
case TrackType.CdMode2Form2
when track.TrackRawBytesPerSector != 2352: return CDRDAO_TRACK_TYPE_MODE2_FORM2;
case TrackType.CdMode2Formless when track.TrackRawBytesPerSector == 2352:
case TrackType.CdMode2Form1 when track.TrackRawBytesPerSector == 2352:
case TrackType.CdMode2Form2
when track.TrackRawBytesPerSector == 2352: return CDRDAO_TRACK_TYPE_MODE2_RAW;
default: return CDRDAO_TRACK_TYPE_MODE1;
}
}
}
}

View File

@@ -0,0 +1,98 @@
// /***************************************************************************
// The Disc Image Chef
// ----------------------------------------------------------------------------
//
// Filename : Identify.cs
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// Component : Disk image plugins.
//
// --[ Description ] ----------------------------------------------------------
//
// Identifies cdrdao cuesheets (toc/bin).
//
// --[ 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/>.
//
// ----------------------------------------------------------------------------
// Copyright © 2011-2018 Natalia Portillo
// ****************************************************************************/
using System;
using System.IO;
using System.Text.RegularExpressions;
using DiscImageChef.CommonTypes.Interfaces;
using DiscImageChef.Console;
namespace DiscImageChef.DiscImages
{
public partial class Cdrdao
{
public bool Identify(IFilter imageFilter)
{
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;
}
tocStream = new StreamReader(imageFilter.GetDataForkStream());
Regex cr = new Regex(REGEX_COMMENT);
Regex dr = new Regex(REGEX_DISCTYPE);
while(tocStream.Peek() >= 0)
{
string line = tocStream.ReadLine();
Match dm = dr.Match(line ?? throw new InvalidOperationException());
Match cm = cr.Match(line);
// Skip comments at start of file
if(cm.Success) continue;
return dm.Success;
}
return false;
}
catch(Exception ex)
{
DicConsole.ErrorWriteLine("Exception trying to identify image file {0}", cdrdaoFilter.GetFilename());
DicConsole.ErrorWriteLine("Exception: {0}", ex.Message);
DicConsole.ErrorWriteLine("Stack trace: {0}", ex.StackTrace);
return false;
}
}
}
}

View File

@@ -0,0 +1,124 @@
// /***************************************************************************
// The Disc Image Chef
// ----------------------------------------------------------------------------
//
// Filename : Properties.cs
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// Component : Disk image plugins.
//
// --[ Description ] ----------------------------------------------------------
//
// Contains properties for cdrdao cuesheets (toc/bin).
//
// --[ 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/>.
//
// ----------------------------------------------------------------------------
// Copyright © 2011-2018 Natalia Portillo
// ****************************************************************************/
using System;
using System.Collections.Generic;
using DiscImageChef.CommonTypes;
using DiscImageChef.CommonTypes.Enums;
using DiscImageChef.CommonTypes.Structs;
using Schemas;
namespace DiscImageChef.DiscImages
{
public partial class Cdrdao
{
public ImageInfo Info => imageInfo;
public string Name => "CDRDAO tocfile";
public Guid Id => new Guid("04D7BA12-1BE8-44D4-97A4-1B48A505463E");
public string Format => "CDRDAO tocfile";
public List<Partition> Partitions { get; private set; }
public List<Session> Sessions => throw new NotImplementedException();
public List<Track> Tracks
{
get
{
List<Track> tracks = new List<Track>();
foreach(CdrdaoTrack cdrTrack in discimage.Tracks)
{
Track dicTrack = new Track
{
Indexes = cdrTrack.Indexes,
TrackDescription = cdrTrack.Title,
TrackStartSector = cdrTrack.StartSector,
TrackPregap = cdrTrack.Pregap,
TrackSession = 1,
TrackSequence = cdrTrack.Sequence,
TrackType = CdrdaoTrackTypeToTrackType(cdrTrack.Tracktype),
TrackFilter = cdrTrack.Trackfile.Datafilter,
TrackFile = cdrTrack.Trackfile.Datafilter.GetFilename(),
TrackFileOffset = cdrTrack.Trackfile.Offset,
TrackFileType = cdrTrack.Trackfile.Filetype,
TrackRawBytesPerSector = cdrTrack.Bps,
TrackBytesPerSector = CdrdaoTrackTypeToCookedBytesPerSector(cdrTrack.Tracktype)
};
dicTrack.TrackEndSector = dicTrack.TrackStartSector + cdrTrack.Sectors - 1;
if(!cdrTrack.Indexes.TryGetValue(0, out dicTrack.TrackStartSector))
cdrTrack.Indexes.TryGetValue(1, out dicTrack.TrackStartSector);
if(cdrTrack.Subchannel)
{
dicTrack.TrackSubchannelType = cdrTrack.Packedsubchannel
? TrackSubchannelType.PackedInterleaved
: TrackSubchannelType.RawInterleaved;
dicTrack.TrackSubchannelFilter = cdrTrack.Trackfile.Datafilter;
dicTrack.TrackSubchannelFile = cdrTrack.Trackfile.Datafilter.GetFilename();
dicTrack.TrackSubchannelOffset = cdrTrack.Trackfile.Offset;
}
else dicTrack.TrackSubchannelType = TrackSubchannelType.None;
tracks.Add(dicTrack);
}
return tracks;
}
}
public List<DumpHardwareType> DumpHardware => null;
public CICMMetadataType CicmMetadata => null;
// TODO: Decode CD-Text to text
public IEnumerable<MediaTagType> SupportedMediaTags => new[] {MediaTagType.CD_MCN};
public IEnumerable<SectorTagType> SupportedSectorTags =>
new[]
{
SectorTagType.CdSectorEcc, SectorTagType.CdSectorEccP, SectorTagType.CdSectorEccQ,
SectorTagType.CdSectorEdc, SectorTagType.CdSectorHeader, SectorTagType.CdSectorSubchannel,
SectorTagType.CdSectorSubHeader, SectorTagType.CdSectorSync, SectorTagType.CdTrackFlags,
SectorTagType.CdTrackIsrc
};
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.JaguarCD, MediaType.MEGACD,
MediaType.PD650, MediaType.PD650_WORM, MediaType.PS1CD, MediaType.PS2CD, MediaType.SuperCDROM2,
MediaType.SVCD, MediaType.SATURNCD, MediaType.ThreeDO, MediaType.VCD, MediaType.VCDHD,
MediaType.NeoGeoCD, MediaType.PCFX
};
public IEnumerable<(string name, Type type, string description)> SupportedOptions =>
new[] {("separate", typeof(bool), "Write each track to a separate file.")};
public IEnumerable<string> KnownExtensions => new[] {".toc"};
public bool IsWriting { get; private set; }
public string ErrorMessage { get; private set; }
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,139 @@
// /***************************************************************************
// The Disc Image Chef
// ----------------------------------------------------------------------------
//
// Filename : Structs.cs
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// Component : Disk image plugins.
//
// --[ Description ] ----------------------------------------------------------
//
// Contains structures for cdrdao cuesheets (toc/bin).
//
// --[ 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/>.
//
// ----------------------------------------------------------------------------
// Copyright © 2011-2018 Natalia Portillo
// ****************************************************************************/
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using DiscImageChef.CommonTypes;
using DiscImageChef.CommonTypes.Interfaces;
namespace DiscImageChef.DiscImages
{
public partial class Cdrdao
{
[SuppressMessage("ReSharper", "NotAccessedField.Local")]
struct CdrdaoTrackFile
{
/// <summary>Track #</summary>
public uint Sequence;
/// <summary>Filter of file containing track</summary>
public IFilter Datafilter;
/// <summary>Path of file containing track</summary>
public string Datafile;
/// <summary>Offset of track start in file</summary>
public ulong Offset;
/// <summary>Type of file</summary>
public string Filetype;
}
#pragma warning disable 169
[SuppressMessage("ReSharper", "NotAccessedField.Local")]
struct CdrdaoTrack
{
/// <summary>Track #</summary>
public uint Sequence;
/// <summary>Track title (from CD-Text)</summary>
public string Title;
/// <summary>Track genre (from CD-Text)</summary>
public string Genre;
/// <summary>Track arranger (from CD-Text)</summary>
public string Arranger;
/// <summary>Track composer (from CD-Text)</summary>
public string Composer;
/// <summary>Track performer (from CD-Text)</summary>
public string Performer;
/// <summary>Track song writer (from CD-Text)</summary>
public string Songwriter;
/// <summary>Track ISRC</summary>
public string Isrc;
/// <summary>Disk provider's message (from CD-Text)</summary>
public string Message;
/// <summary>File struct for this track</summary>
public CdrdaoTrackFile Trackfile;
/// <summary>Indexes on this track</summary>
public Dictionary<int, ulong> Indexes;
/// <summary>Track pre-gap in sectors</summary>
public ulong Pregap;
/// <summary>Track post-gap in sectors</summary>
public ulong Postgap;
/// <summary>Digical Copy Permitted</summary>
public bool FlagDcp;
/// <summary>Track is quadraphonic</summary>
public bool Flag_4Ch;
/// <summary>Track has preemphasis</summary>
public bool FlagPre;
/// <summary>Bytes per sector</summary>
public ushort Bps;
/// <summary>Sectors in track</summary>
public ulong Sectors;
/// <summary>Starting sector in track</summary>
public ulong StartSector;
/// <summary>Track type</summary>
public string Tracktype;
public bool Subchannel;
public bool Packedsubchannel;
}
[SuppressMessage("ReSharper", "NotAccessedField.Local")]
struct CdrdaoDisc
{
/// <summary>Disk title (from CD-Text)</summary>
public string Title;
/// <summary>Disk genre (from CD-Text)</summary>
public string Genre;
/// <summary>Disk arranger (from CD-Text)</summary>
public string Arranger;
/// <summary>Disk composer (from CD-Text)</summary>
public string Composer;
/// <summary>Disk performer (from CD-Text)</summary>
public string Performer;
/// <summary>Disk song writer (from CD-Text)</summary>
public string Songwriter;
/// <summary>Disk provider's message (from CD-Text)</summary>
public string Message;
/// <summary>Media catalog number</summary>
public string Mcn;
/// <summary>Disk type</summary>
public MediaType Disktype;
/// <summary>Disk type string</summary>
public string Disktypestr;
/// <summary>Disk CDDB ID</summary>
public string DiskId;
/// <summary>Disk UPC/EAN</summary>
public string Barcode;
/// <summary>Tracks</summary>
public List<CdrdaoTrack> Tracks;
/// <summary>Disk comment</summary>
public string Comment;
}
#pragma warning restore 169
}
}

View File

@@ -0,0 +1,42 @@
// /***************************************************************************
// The Disc Image Chef
// ----------------------------------------------------------------------------
//
// Filename : Unsupported.cs
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// Component : Disk image plugins.
//
// --[ Description ] ----------------------------------------------------------
//
// Contains features unsupported by cdrdao cuesheets (toc/bin).
//
// --[ 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/>.
//
// ----------------------------------------------------------------------------
// Copyright © 2011-2018 Natalia Portillo
// ****************************************************************************/
namespace DiscImageChef.DiscImages
{
public partial class Cdrdao
{
public bool? VerifyMediaImage()
{
return null;
}
}
}

View File

@@ -0,0 +1,698 @@
// /***************************************************************************
// The Disc Image Chef
// ----------------------------------------------------------------------------
//
// Filename : Write.cs
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// Component : Disk image plugins.
//
// --[ Description ] ----------------------------------------------------------
//
// Writes cdrdao cuesheets (toc/bin).
//
// --[ 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/>.
//
// ----------------------------------------------------------------------------
// Copyright © 2011-2018 Natalia Portillo
// ****************************************************************************/
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using DiscImageChef.CommonTypes;
using DiscImageChef.CommonTypes.Enums;
using DiscImageChef.CommonTypes.Structs;
using Schemas;
using TrackType = DiscImageChef.CommonTypes.Enums.TrackType;
namespace DiscImageChef.DiscImages
{
public partial class Cdrdao
{
public bool Create(string path, MediaType mediaType, Dictionary<string, string> options, ulong sectors,
uint sectorSize)
{
if(options != null)
{
if(options.TryGetValue("separate", out string tmpValue))
{
if(!bool.TryParse(tmpValue, out separateTracksWriting))
{
ErrorMessage = "Invalid value for split option";
return false;
}
if(separateTracksWriting)
{
ErrorMessage = "Separate tracksnot yet implemented";
return false;
}
}
}
else separateTracksWriting = false;
if(!SupportedMediaTypes.Contains(mediaType))
{
ErrorMessage = $"Unsupport media format {mediaType}";
return false;
}
imageInfo = new ImageInfo {MediaType = mediaType, SectorSize = sectorSize, Sectors = sectors};
// TODO: Separate tracks
try
{
writingBaseName = Path.Combine(Path.GetDirectoryName(path), Path.GetFileNameWithoutExtension(path));
descriptorStream = new StreamWriter(path, false, Encoding.ASCII);
}
catch(IOException e)
{
ErrorMessage = $"Could not create new image file, exception {e.Message}";
return false;
}
discimage = new CdrdaoDisc {Disktype = mediaType, Tracks = new List<CdrdaoTrack>()};
trackFlags = new Dictionary<byte, byte>();
trackIsrcs = new Dictionary<byte, string>();
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:
discimage.Mcn = Encoding.ASCII.GetString(data);
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;
}
Track track =
writingTracks.FirstOrDefault(trk => sectorAddress >= trk.TrackStartSector &&
sectorAddress <= trk.TrackEndSector);
if(track.TrackSequence == 0)
{
ErrorMessage = $"Can't found track containing {sectorAddress}";
return false;
}
FileStream trackStream = writingStreams.FirstOrDefault(kvp => kvp.Key == track.TrackSequence).Value;
if(trackStream == null)
{
ErrorMessage = $"Can't found file containing {sectorAddress}";
return false;
}
if(track.TrackBytesPerSector != track.TrackRawBytesPerSector)
{
ErrorMessage = "Invalid write mode for this sector";
return false;
}
if(data.Length != track.TrackRawBytesPerSector)
{
ErrorMessage = "Incorrect data size";
return false;
}
// cdrdao audio tracks are endian swapped corresponding to DiscImageChef
if(track.TrackType == TrackType.Audio)
{
byte[] swapped = new byte[data.Length];
for(long i = 0; i < swapped.Length; i += 2)
{
swapped[i] = data[i + 1];
swapped[i + 1] = data[i];
}
data = swapped;
}
trackStream.Seek((long)(track.TrackFileOffset + (sectorAddress - track.TrackStartSector) * (ulong)track.TrackRawBytesPerSector),
SeekOrigin.Begin);
trackStream.Write(data, 0, data.Length);
return true;
}
public bool WriteSectors(byte[] data, ulong sectorAddress, uint length)
{
if(!IsWriting)
{
ErrorMessage = "Tried to write on a non-writable image";
return false;
}
Track track =
writingTracks.FirstOrDefault(trk => sectorAddress >= trk.TrackStartSector &&
sectorAddress <= trk.TrackEndSector);
if(track.TrackSequence == 0)
{
ErrorMessage = $"Can't found track containing {sectorAddress}";
return false;
}
FileStream trackStream = writingStreams.FirstOrDefault(kvp => kvp.Key == track.TrackSequence).Value;
if(trackStream == null)
{
ErrorMessage = $"Can't found file containing {sectorAddress}";
return false;
}
if(track.TrackBytesPerSector != track.TrackRawBytesPerSector)
{
ErrorMessage = "Invalid write mode for this sector";
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;
}
// cdrdao audio tracks are endian swapped corresponding to DiscImageChef
if(track.TrackType == TrackType.Audio)
{
byte[] swapped = new byte[data.Length];
for(long i = 0; i < swapped.Length; i += 2)
{
swapped[i] = data[i + 1];
swapped[i + 1] = data[i];
}
data = swapped;
}
switch(track.TrackSubchannelType)
{
case TrackSubchannelType.None:
trackStream
.Seek((long)(track.TrackFileOffset + (sectorAddress - track.TrackStartSector) * (ulong)track.TrackRawBytesPerSector),
SeekOrigin.Begin);
trackStream.Write(data, 0, data.Length);
ErrorMessage = "";
return true;
case TrackSubchannelType.Raw:
case TrackSubchannelType.RawInterleaved:
trackStream
.Seek((long)(track.TrackFileOffset + (sectorAddress - track.TrackStartSector) * (ulong)(track.TrackRawBytesPerSector + 96)),
SeekOrigin.Begin);
for(uint i = 0; i < length; i++)
{
trackStream.Write(data, (int)(i * track.TrackRawBytesPerSector), track.TrackRawBytesPerSector);
trackStream.Position += 96;
}
ErrorMessage = "";
return true;
default:
ErrorMessage = "Invalid subchannel mode for this sector";
return false;
}
}
public bool WriteSectorLong(byte[] data, ulong sectorAddress)
{
if(!IsWriting)
{
ErrorMessage = "Tried to write on a non-writable image";
return false;
}
Track track =
writingTracks.FirstOrDefault(trk => sectorAddress >= trk.TrackStartSector &&
sectorAddress <= trk.TrackEndSector);
if(track.TrackSequence == 0)
{
ErrorMessage = $"Can't found track containing {sectorAddress}";
return false;
}
FileStream trackStream = writingStreams.FirstOrDefault(kvp => kvp.Key == track.TrackSequence).Value;
if(trackStream == null)
{
ErrorMessage = $"Can't found file containing {sectorAddress}";
return false;
}
if(data.Length != track.TrackRawBytesPerSector)
{
ErrorMessage = "Incorrect data size";
return false;
}
// cdrdao audio tracks are endian swapped corresponding to DiscImageChef
if(track.TrackType == TrackType.Audio)
{
byte[] swapped = new byte[data.Length];
for(long i = 0; i < swapped.Length; i += 2)
{
swapped[i] = data[i + 1];
swapped[i + 1] = data[i];
}
data = swapped;
}
uint subchannelSize = (uint)(track.TrackSubchannelType != TrackSubchannelType.None ? 96 : 0);
trackStream.Seek((long)(track.TrackFileOffset + (sectorAddress - track.TrackStartSector) * (ulong)(track.TrackRawBytesPerSector + subchannelSize)),
SeekOrigin.Begin);
trackStream.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 =
writingTracks.FirstOrDefault(trk => sectorAddress >= trk.TrackStartSector &&
sectorAddress <= trk.TrackEndSector);
if(track.TrackSequence == 0)
{
ErrorMessage = $"Can't found track containing {sectorAddress}";
return false;
}
FileStream trackStream = writingStreams.FirstOrDefault(kvp => kvp.Key == track.TrackSequence).Value;
if(trackStream == null)
{
ErrorMessage = $"Can't found file 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;
}
// cdrdao audio tracks are endian swapped corresponding to DiscImageChef
if(track.TrackType == TrackType.Audio)
{
byte[] swapped = new byte[data.Length];
for(long i = 0; i < swapped.Length; i += 2)
{
swapped[i] = data[i + 1];
swapped[i + 1] = data[i];
}
data = swapped;
}
uint subchannelSize = (uint)(track.TrackSubchannelType != TrackSubchannelType.None ? 96 : 0);
for(uint i = 0; i < length; i++)
{
trackStream.Seek((long)(track.TrackFileOffset + (i + sectorAddress - track.TrackStartSector) * (ulong)(track.TrackRawBytesPerSector + subchannelSize)),
SeekOrigin.Begin);
trackStream.Write(data, (int)(i * track.TrackRawBytesPerSector), track.TrackRawBytesPerSector);
}
return true;
}
public bool SetTracks(List<Track> tracks)
{
if(!IsWriting)
{
ErrorMessage = "Tried to write on a non-writable image";
return false;
}
if(tracks == null || tracks.Count == 0)
{
ErrorMessage = "Invalid tracks sent";
return false;
}
if(writingTracks != null && writingStreams != null)
foreach(FileStream oldTrack in writingStreams.Select(t => t.Value).Distinct())
oldTrack.Close();
ulong currentOffset = 0;
writingTracks = new List<Track>();
foreach(Track track in tracks.OrderBy(t => t.TrackSequence))
{
if(track.TrackSubchannelType == TrackSubchannelType.Q16 ||
track.TrackSubchannelType == TrackSubchannelType.Q16Interleaved)
{
ErrorMessage =
$"Unsupported subchannel type {track.TrackSubchannelType} for track {track.TrackSequence}";
return false;
}
Track newTrack = track;
newTrack.TrackFile = separateTracksWriting
? writingBaseName + $"_track{track.TrackSequence:D2}.bin"
: writingBaseName + ".bin";
newTrack.TrackFileOffset = separateTracksWriting ? 0 : currentOffset;
writingTracks.Add(newTrack);
currentOffset += (ulong)newTrack.TrackRawBytesPerSector *
(newTrack.TrackEndSector - newTrack.TrackStartSector + 1);
if(track.TrackSubchannelType != TrackSubchannelType.None)
currentOffset += 96 * (newTrack.TrackEndSector - newTrack.TrackStartSector + 1);
}
writingStreams = new Dictionary<uint, FileStream>();
if(separateTracksWriting)
foreach(Track track in writingTracks)
writingStreams.Add(track.TrackSequence,
new FileStream(track.TrackFile, FileMode.OpenOrCreate, FileAccess.ReadWrite,
FileShare.None));
else
{
FileStream jointstream = new FileStream(writingBaseName + ".bin", FileMode.OpenOrCreate,
FileAccess.ReadWrite, FileShare.None);
foreach(Track track in writingTracks) writingStreams.Add(track.TrackSequence, jointstream);
}
return true;
}
public bool Close()
{
if(!IsWriting)
{
ErrorMessage = "Image is not opened for writing";
return false;
}
if(separateTracksWriting)
foreach(FileStream writingStream in writingStreams.Values)
{
writingStream.Flush();
writingStream.Close();
}
else
{
writingStreams.First().Value.Flush();
writingStreams.First().Value.Close();
}
bool data = writingTracks.Count(t => t.TrackType != TrackType.Audio) > 0;
bool mode2 = writingTracks.Count(t => t.TrackType == TrackType.CdMode2Form1 ||
t.TrackType == TrackType.CdMode2Form2 ||
t.TrackType == TrackType.CdMode2Formless) > 0;
if(mode2) descriptorStream.WriteLine("CD_ROM_XA");
else if(data) descriptorStream.WriteLine("CD_ROM");
else descriptorStream.WriteLine("CD_DA");
if(!string.IsNullOrWhiteSpace(discimage.Comment))
{
string[] commentLines = discimage.Comment.Split(new[] {'\n'}, StringSplitOptions.RemoveEmptyEntries);
foreach(string line in commentLines) descriptorStream.WriteLine("// {0}", line);
}
descriptorStream.WriteLine();
if(!string.IsNullOrEmpty(discimage.Mcn)) descriptorStream.WriteLine("CATALOG {0}", discimage.Mcn);
foreach(Track track in writingTracks)
{
descriptorStream.WriteLine();
descriptorStream.WriteLine("// Track {0}", track.TrackSequence);
string subchannelType;
switch(track.TrackSubchannelType)
{
case TrackSubchannelType.Packed:
case TrackSubchannelType.PackedInterleaved:
subchannelType = " RW";
break;
case TrackSubchannelType.Raw:
case TrackSubchannelType.RawInterleaved:
subchannelType = " RW_RAW";
break;
default:
subchannelType = "";
break;
}
descriptorStream.WriteLine("TRACK {0}{1}", GetTrackMode(track), subchannelType);
trackFlags.TryGetValue((byte)track.TrackSequence, out byte flagsByte);
CdFlags flags = (CdFlags)flagsByte;
descriptorStream.WriteLine("{0}COPY", flags.HasFlag(CdFlags.CopyPermitted) ? "" : "NO ");
if(track.TrackType == TrackType.Audio)
{
descriptorStream.WriteLine("{0}PRE_EMPHASIS", flags.HasFlag(CdFlags.PreEmphasis) ? "" : "NO ");
descriptorStream.WriteLine("{0}_CHANNEL_AUDIO",
flags.HasFlag(CdFlags.FourChannel) ? "FOUR" : "TWO");
}
if(trackIsrcs.TryGetValue((byte)track.TrackSequence, out string isrc))
descriptorStream.WriteLine("ISRC {0}", isrc);
(byte minute, byte second, byte frame)
msf = LbaToMsf(track.TrackEndSector - track.TrackStartSector + 1);
descriptorStream.WriteLine("DATAFILE \"{0}\" #{1} {2:D2}:{3:D2}:{4:D2} // length in bytes: {5}",
Path.GetFileName(track.TrackFile), track.TrackFileOffset, msf.minute,
msf.second, msf.frame,
(track.TrackEndSector - track.TrackStartSector + 1) *
(ulong)(track.TrackRawBytesPerSector +
(track.TrackSubchannelType != TrackSubchannelType.None ? 96 : 0)));
descriptorStream.WriteLine();
}
descriptorStream.Flush();
descriptorStream.Close();
IsWriting = false;
ErrorMessage = "";
return true;
}
public bool SetMetadata(ImageInfo metadata)
{
discimage.Barcode = metadata.MediaBarcode;
discimage.Comment = metadata.Comments;
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 =
writingTracks.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.CdTrackIsrc:
{
if(data != null) trackIsrcs.Add((byte)track.TrackSequence, Encoding.UTF8.GetString(data));
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;
}
FileStream trackStream = writingStreams.FirstOrDefault(kvp => kvp.Key == track.TrackSequence).Value;
if(trackStream == null)
{
ErrorMessage = $"Can't found file containing {sectorAddress}";
return false;
}
trackStream
.Seek((long)(track.TrackFileOffset + (sectorAddress - track.TrackStartSector) * (ulong)(track.TrackRawBytesPerSector + 96)) + track.TrackRawBytesPerSector,
SeekOrigin.Begin);
trackStream.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 =
writingTracks.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;
}
FileStream trackStream = writingStreams.FirstOrDefault(kvp => kvp.Key == track.TrackSequence).Value;
if(trackStream == null)
{
ErrorMessage = $"Can't found file containing {sectorAddress}";
return false;
}
for(uint i = 0; i < length; i++)
{
trackStream
.Seek((long)(track.TrackFileOffset + (i + sectorAddress - track.TrackStartSector) * (ulong)(track.TrackRawBytesPerSector + 96)) + track.TrackRawBytesPerSector,
SeekOrigin.Begin);
trackStream.Write(data, (int)(i * 96), 96);
}
return true;
}
default:
ErrorMessage = $"Unsupported tag type {tag}";
return false;
}
}
public bool SetDumpHardware(List<DumpHardwareType> dumpHardware)
{
// Not supported
return false;
}
public bool SetCicmMetadata(CICMMetadataType metadata)
{
// Not supported
return false;
}
}
}