2018-07-23 23:25:43 +01:00
|
|
|
|
// /***************************************************************************
|
2020-02-27 12:31:25 +00:00
|
|
|
|
// Aaru Data Preservation Suite
|
2018-07-23 23:25:43 +01:00
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
//
|
|
|
|
|
|
// Filename : Write.cs
|
|
|
|
|
|
// Author(s) : Natalia Portillo <claunia@claunia.com>
|
|
|
|
|
|
//
|
|
|
|
|
|
// Component : Disk image plugins.
|
|
|
|
|
|
//
|
|
|
|
|
|
// --[ Description ] ----------------------------------------------------------
|
|
|
|
|
|
//
|
|
|
|
|
|
// Writes CloneCD disc images.
|
|
|
|
|
|
//
|
|
|
|
|
|
// --[ 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/>.
|
|
|
|
|
|
//
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
2024-05-01 04:17:32 +01:00
|
|
|
|
// Copyright © 2011-2024 Natalia Portillo
|
2018-07-23 23:25:43 +01:00
|
|
|
|
// ****************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.IO;
|
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using System.Text;
|
2020-02-27 00:33:26 +00:00
|
|
|
|
using Aaru.CommonTypes;
|
2022-12-15 22:21:07 +00:00
|
|
|
|
using Aaru.CommonTypes.AaruMetadata;
|
2020-02-27 00:33:26 +00:00
|
|
|
|
using Aaru.CommonTypes.Enums;
|
|
|
|
|
|
using Aaru.CommonTypes.Structs;
|
2023-10-08 04:10:19 +01:00
|
|
|
|
using Aaru.Console;
|
2020-02-27 00:33:26 +00:00
|
|
|
|
using Aaru.Decoders.CD;
|
2020-07-20 15:43:52 +01:00
|
|
|
|
using Aaru.Helpers;
|
2022-12-15 22:21:07 +00:00
|
|
|
|
using Track = Aaru.CommonTypes.Structs.Track;
|
2020-02-27 00:33:26 +00:00
|
|
|
|
using TrackType = Aaru.CommonTypes.Enums.TrackType;
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
2023-10-06 01:16:28 +01:00
|
|
|
|
namespace Aaru.Images;
|
2022-11-15 15:58:43 +00:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
public sealed partial class CloneCd
|
2018-07-23 23:25:43 +01:00
|
|
|
|
{
|
2023-10-03 23:34:59 +01:00
|
|
|
|
#region IWritableOpticalImage Members
|
|
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
|
public bool Create(string path, MediaType mediaType, Dictionary<string, string> options, ulong sectors,
|
2023-10-03 23:34:59 +01:00
|
|
|
|
uint sectorSize)
|
2018-07-23 23:25:43 +01:00
|
|
|
|
{
|
2022-03-06 13:29:38 +00:00
|
|
|
|
if(!SupportedMediaTypes.Contains(mediaType))
|
2018-07-23 23:25:43 +01:00
|
|
|
|
{
|
2022-11-29 02:10:37 +00:00
|
|
|
|
ErrorMessage = string.Format(Localization.Unsupported_media_format_0, mediaType);
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
return false;
|
|
|
|
|
|
}
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
_imageInfo = new ImageInfo
|
|
|
|
|
|
{
|
|
|
|
|
|
MediaType = mediaType,
|
|
|
|
|
|
SectorSize = sectorSize,
|
|
|
|
|
|
Sectors = sectors
|
|
|
|
|
|
};
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
try
|
|
|
|
|
|
{
|
2022-03-07 07:36:44 +00:00
|
|
|
|
_writingBaseName = Path.Combine(Path.GetDirectoryName(path) ?? "", Path.GetFileNameWithoutExtension(path));
|
2020-07-22 13:20:25 +01:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
_descriptorStream = new StreamWriter(path, false, Encoding.ASCII);
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
2024-05-01 04:05:22 +01:00
|
|
|
|
_dataStream = new FileStream(_writingBaseName + ".img",
|
|
|
|
|
|
FileMode.OpenOrCreate,
|
|
|
|
|
|
FileAccess.ReadWrite,
|
2022-03-06 13:29:38 +00:00
|
|
|
|
FileShare.None);
|
|
|
|
|
|
}
|
2023-10-08 04:10:19 +01:00
|
|
|
|
catch(IOException ex)
|
2022-03-06 13:29:38 +00:00
|
|
|
|
{
|
2023-10-08 04:10:19 +01:00
|
|
|
|
ErrorMessage = string.Format(Localization.Could_not_create_new_image_file_exception_0, ex.Message);
|
|
|
|
|
|
AaruConsole.WriteException(ex);
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
return false;
|
|
|
|
|
|
}
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
_imageInfo.MediaType = mediaType;
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
_trackFlags = new Dictionary<byte, byte>();
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
IsWriting = true;
|
|
|
|
|
|
ErrorMessage = null;
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
return true;
|
|
|
|
|
|
}
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
|
public bool WriteMediaTag(byte[] data, MediaTagType tag)
|
|
|
|
|
|
{
|
|
|
|
|
|
if(!IsWriting)
|
2018-07-23 23:25:43 +01:00
|
|
|
|
{
|
2022-11-29 02:10:37 +00:00
|
|
|
|
ErrorMessage = Localization.Tried_to_write_on_a_non_writable_image;
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
return false;
|
|
|
|
|
|
}
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
switch(tag)
|
|
|
|
|
|
{
|
|
|
|
|
|
case MediaTagType.CD_MCN:
|
|
|
|
|
|
_catalog = Encoding.ASCII.GetString(data);
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
return true;
|
|
|
|
|
|
case MediaTagType.CD_FullTOC:
|
|
|
|
|
|
_fullToc = data;
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
return true;
|
|
|
|
|
|
default:
|
2022-11-29 02:10:37 +00:00
|
|
|
|
ErrorMessage = string.Format(Localization.Unsupported_media_tag_0, tag);
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
return false;
|
2018-07-23 23:25:43 +01:00
|
|
|
|
}
|
2022-03-06 13:29:38 +00:00
|
|
|
|
}
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
|
public bool WriteSector(byte[] data, ulong sectorAddress)
|
|
|
|
|
|
{
|
|
|
|
|
|
if(!IsWriting)
|
2018-07-23 23:25:43 +01:00
|
|
|
|
{
|
2022-11-29 02:10:37 +00:00
|
|
|
|
ErrorMessage = Localization.Tried_to_write_on_a_non_writable_image;
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// TODO: Implement ECC generation
|
2022-11-29 02:10:37 +00:00
|
|
|
|
ErrorMessage = Localization.This_format_requires_sectors_to_be_raw_Generating_ECC_is_not_yet_implemented;
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
|
public bool WriteSectors(byte[] data, ulong sectorAddress, uint length)
|
|
|
|
|
|
{
|
|
|
|
|
|
if(!IsWriting)
|
|
|
|
|
|
{
|
2022-11-29 02:10:37 +00:00
|
|
|
|
ErrorMessage = Localization.Tried_to_write_on_a_non_writable_image;
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
2018-07-23 23:25:43 +01:00
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
// TODO: Implement ECC generation
|
2022-11-29 02:10:37 +00:00
|
|
|
|
ErrorMessage = Localization.This_format_requires_sectors_to_be_raw_Generating_ECC_is_not_yet_implemented;
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
return false;
|
|
|
|
|
|
}
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
|
public bool WriteSectorLong(byte[] data, ulong sectorAddress)
|
|
|
|
|
|
{
|
|
|
|
|
|
if(!IsWriting)
|
|
|
|
|
|
{
|
2022-11-29 02:10:37 +00:00
|
|
|
|
ErrorMessage = Localization.Tried_to_write_on_a_non_writable_image;
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
2018-07-23 23:25:43 +01:00
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-03-07 07:36:44 +00:00
|
|
|
|
Track track = Tracks.FirstOrDefault(trk => sectorAddress >= trk.StartSector && sectorAddress <= trk.EndSector);
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
if(track is null)
|
|
|
|
|
|
{
|
2022-11-29 02:10:37 +00:00
|
|
|
|
ErrorMessage = string.Format(Localization.Cant_find_track_containing_0, sectorAddress);
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
return false;
|
|
|
|
|
|
}
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
if(data.Length != track.RawBytesPerSector)
|
|
|
|
|
|
{
|
2022-11-29 02:10:37 +00:00
|
|
|
|
ErrorMessage = Localization.Incorrect_data_size;
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
return false;
|
|
|
|
|
|
}
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
2024-05-01 04:05:22 +01:00
|
|
|
|
_dataStream.Seek((long)(track.FileOffset +
|
|
|
|
|
|
(sectorAddress - track.StartSector) * (ulong)track.RawBytesPerSector),
|
|
|
|
|
|
SeekOrigin.Begin);
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
_dataStream.Write(data, 0, data.Length);
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
return true;
|
|
|
|
|
|
}
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
|
public bool WriteSectorsLong(byte[] data, ulong sectorAddress, uint length)
|
|
|
|
|
|
{
|
|
|
|
|
|
if(!IsWriting)
|
|
|
|
|
|
{
|
2022-11-29 02:10:37 +00:00
|
|
|
|
ErrorMessage = Localization.Tried_to_write_on_a_non_writable_image;
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
return false;
|
2018-07-23 23:25:43 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-03-07 07:36:44 +00:00
|
|
|
|
Track track = Tracks.FirstOrDefault(trk => sectorAddress >= trk.StartSector && sectorAddress <= trk.EndSector);
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
if(track is null)
|
|
|
|
|
|
{
|
2022-11-29 02:10:37 +00:00
|
|
|
|
ErrorMessage = string.Format(Localization.Cant_find_track_containing_0, sectorAddress);
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
return false;
|
|
|
|
|
|
}
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
if(sectorAddress + length > track.EndSector + 1)
|
|
|
|
|
|
{
|
2022-11-29 02:10:37 +00:00
|
|
|
|
ErrorMessage = Localization.Cant_cross_tracks;
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
return false;
|
|
|
|
|
|
}
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
if(data.Length % track.RawBytesPerSector != 0)
|
|
|
|
|
|
{
|
2022-11-29 02:10:37 +00:00
|
|
|
|
ErrorMessage = Localization.Incorrect_data_size;
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
return false;
|
|
|
|
|
|
}
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
2024-05-01 04:05:22 +01:00
|
|
|
|
_dataStream.Seek((long)(track.FileOffset +
|
|
|
|
|
|
(sectorAddress - track.StartSector) * (ulong)track.RawBytesPerSector),
|
|
|
|
|
|
SeekOrigin.Begin);
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
_dataStream.Write(data, 0, data.Length);
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
return true;
|
|
|
|
|
|
}
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
|
public bool SetTracks(List<Track> tracks)
|
|
|
|
|
|
{
|
|
|
|
|
|
ulong currentDataOffset = 0;
|
|
|
|
|
|
ulong currentSubchannelOffset = 0;
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
2024-05-01 04:39:38 +01:00
|
|
|
|
Tracks = [];
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
foreach(Track track in tracks.OrderBy(t => t.Sequence))
|
2018-07-23 23:25:43 +01:00
|
|
|
|
{
|
2022-03-06 13:29:38 +00:00
|
|
|
|
Track newTrack = track;
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
if(newTrack.Session > 1)
|
2018-07-23 23:25:43 +01:00
|
|
|
|
{
|
2022-03-06 13:29:38 +00:00
|
|
|
|
Track firstSessionTrack = tracks.FirstOrDefault(t => t.Session == newTrack.Session);
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
2023-10-04 17:34:40 +01:00
|
|
|
|
if(firstSessionTrack?.Sequence == newTrack.Sequence && newTrack.Pregap >= 150)
|
2020-06-15 03:01:59 +01:00
|
|
|
|
{
|
2022-03-06 13:29:38 +00:00
|
|
|
|
newTrack.Pregap -= 150;
|
|
|
|
|
|
newTrack.StartSector += 150;
|
2020-06-15 03:01:59 +01:00
|
|
|
|
}
|
2022-03-06 13:29:38 +00:00
|
|
|
|
}
|
2020-06-15 03:01:59 +01:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
uint subchannelSize;
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
switch(newTrack.SubchannelType)
|
|
|
|
|
|
{
|
|
|
|
|
|
case TrackSubchannelType.None:
|
|
|
|
|
|
subchannelSize = 0;
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
break;
|
|
|
|
|
|
case TrackSubchannelType.Raw:
|
|
|
|
|
|
case TrackSubchannelType.RawInterleaved:
|
|
|
|
|
|
subchannelSize = 96;
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
break;
|
|
|
|
|
|
default:
|
2022-11-29 02:10:37 +00:00
|
|
|
|
ErrorMessage = string.Format(Localization.Unsupported_subchannel_type_0, newTrack.SubchannelType);
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
return false;
|
|
|
|
|
|
}
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
newTrack.FileOffset = currentDataOffset;
|
|
|
|
|
|
newTrack.SubchannelOffset = currentSubchannelOffset;
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
2022-03-07 07:36:44 +00:00
|
|
|
|
currentDataOffset += (ulong)newTrack.RawBytesPerSector * (newTrack.EndSector - newTrack.StartSector + 1);
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
currentSubchannelOffset += subchannelSize * (newTrack.EndSector - newTrack.StartSector + 1);
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
Tracks.Add(newTrack);
|
2018-07-23 23:25:43 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
|
public bool Close()
|
|
|
|
|
|
{
|
|
|
|
|
|
if(!IsWriting)
|
2018-07-23 23:25:43 +01:00
|
|
|
|
{
|
2022-11-29 02:10:37 +00:00
|
|
|
|
ErrorMessage = Localization.Image_is_not_opened_for_writing;
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
return false;
|
|
|
|
|
|
}
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
_dataStream.Flush();
|
|
|
|
|
|
_dataStream.Close();
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
_subStream?.Flush();
|
|
|
|
|
|
_subStream?.Close();
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
FullTOC.CDFullTOC? nullableToc = null;
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
// Easy, just decode the real toc
|
|
|
|
|
|
if(_fullToc != null)
|
|
|
|
|
|
{
|
2023-10-03 23:34:59 +01:00
|
|
|
|
var tmp = new byte[_fullToc.Length + 2];
|
2022-03-06 13:29:38 +00:00
|
|
|
|
Array.Copy(BigEndianBitConverter.GetBytes((ushort)_fullToc.Length), 0, tmp, 0, 2);
|
2023-10-03 23:34:59 +01:00
|
|
|
|
Array.Copy(_fullToc, 0, tmp, 2, _fullToc.Length);
|
2022-03-06 13:29:38 +00:00
|
|
|
|
nullableToc = FullTOC.Decode(tmp);
|
|
|
|
|
|
}
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
// Not easy, create a toc from scratch
|
2022-11-13 20:27:32 +00:00
|
|
|
|
FullTOC.CDFullTOC toc = nullableToc ?? FullTOC.Create(Tracks, _trackFlags, true);
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
_descriptorStream.WriteLine("[CloneCD]");
|
|
|
|
|
|
_descriptorStream.WriteLine("Version=2");
|
|
|
|
|
|
_descriptorStream.WriteLine("[Disc]");
|
|
|
|
|
|
_descriptorStream.WriteLine("TocEntries={0}", toc.TrackDescriptors.Length);
|
2023-10-03 23:34:59 +01:00
|
|
|
|
_descriptorStream.WriteLine("Sessions={0}", toc.LastCompleteSession);
|
2022-03-06 13:29:38 +00:00
|
|
|
|
_descriptorStream.WriteLine("DataTracksScrambled=0");
|
|
|
|
|
|
_descriptorStream.WriteLine("CDTextLength=0");
|
|
|
|
|
|
|
2024-05-01 04:05:22 +01:00
|
|
|
|
if(!string.IsNullOrEmpty(_catalog)) _descriptorStream.WriteLine("CATALOG={0}", _catalog);
|
2022-03-06 13:29:38 +00:00
|
|
|
|
|
2023-10-03 23:34:59 +01:00
|
|
|
|
for(var i = 1; i <= toc.LastCompleteSession; i++)
|
2022-03-06 13:29:38 +00:00
|
|
|
|
{
|
|
|
|
|
|
_descriptorStream.WriteLine("[Session {0}]", i);
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
Track firstSessionTrack = Tracks.FirstOrDefault(t => t.Session == i);
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
switch(firstSessionTrack?.Type)
|
2018-07-23 23:25:43 +01:00
|
|
|
|
{
|
2022-03-06 13:29:38 +00:00
|
|
|
|
case TrackType.Audio:
|
|
|
|
|
|
// CloneCD always writes this value for first track in disc, however the Rainbow Books
|
|
|
|
|
|
// say the first track pregap is no different from other session pregaps, same mode as
|
|
|
|
|
|
// the track they belong to.
|
|
|
|
|
|
_descriptorStream.WriteLine("PreGapMode=0");
|
|
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
case TrackType.Data:
|
|
|
|
|
|
case TrackType.CdMode1:
|
|
|
|
|
|
_descriptorStream.WriteLine("PreGapMode=1");
|
|
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
case TrackType.CdMode2Formless:
|
|
|
|
|
|
case TrackType.CdMode2Form1:
|
|
|
|
|
|
case TrackType.CdMode2Form2:
|
|
|
|
|
|
_descriptorStream.WriteLine("PreGapMode=2");
|
|
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
default:
|
2022-11-29 02:10:37 +00:00
|
|
|
|
ErrorMessage = string.Format(Localization.Unexpected_first_session_track_type_0,
|
|
|
|
|
|
firstSessionTrack?.Type.ToString() ?? "null");
|
2020-06-15 19:19:45 +01:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
return false;
|
|
|
|
|
|
}
|
2020-06-15 19:19:45 +01:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
_descriptorStream.WriteLine("PreGapSubC=0");
|
|
|
|
|
|
}
|
2021-09-21 04:55:28 +01:00
|
|
|
|
|
2023-10-03 23:34:59 +01:00
|
|
|
|
for(var i = 0; i < toc.TrackDescriptors.Length; i++)
|
2022-03-06 13:29:38 +00:00
|
|
|
|
{
|
|
|
|
|
|
long alba = MsfToLba((toc.TrackDescriptors[i].Min, toc.TrackDescriptors[i].Sec,
|
|
|
|
|
|
toc.TrackDescriptors[i].Frame));
|
|
|
|
|
|
|
|
|
|
|
|
long plba = MsfToLba((toc.TrackDescriptors[i].PMIN, toc.TrackDescriptors[i].PSEC,
|
|
|
|
|
|
toc.TrackDescriptors[i].PFRAME));
|
|
|
|
|
|
|
2024-05-01 04:05:22 +01:00
|
|
|
|
if(alba > 405000) alba = (alba - 405000 + 300) * -1;
|
2022-03-06 13:29:38 +00:00
|
|
|
|
|
2024-05-01 04:05:22 +01:00
|
|
|
|
if(plba > 405000) plba = (plba - 405000 + 300) * -1;
|
2022-03-06 13:29:38 +00:00
|
|
|
|
|
2023-10-03 23:34:59 +01: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);
|
2022-03-06 13:29:38 +00:00
|
|
|
|
_descriptorStream.WriteLine("Control=0x{0:x2}", toc.TrackDescriptors[i].CONTROL);
|
2023-10-03 23:34:59 +01:00
|
|
|
|
_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}", alba);
|
2022-03-06 13:29:38 +00:00
|
|
|
|
|
|
|
|
|
|
_descriptorStream.WriteLine("Zero={0}",
|
|
|
|
|
|
((toc.TrackDescriptors[i].HOUR & 0x0F) << 4) +
|
|
|
|
|
|
(toc.TrackDescriptors[i].PHOUR & 0x0F));
|
|
|
|
|
|
|
2023-10-03 23:34:59 +01:00
|
|
|
|
_descriptorStream.WriteLine("PMin={0}", toc.TrackDescriptors[i].PMIN);
|
|
|
|
|
|
_descriptorStream.WriteLine("PSec={0}", toc.TrackDescriptors[i].PSEC);
|
2022-03-06 13:29:38 +00:00
|
|
|
|
_descriptorStream.WriteLine("PFrame={0}", toc.TrackDescriptors[i].PFRAME);
|
2023-10-03 23:34:59 +01:00
|
|
|
|
_descriptorStream.WriteLine("PLBA={0}", plba);
|
2022-03-06 13:29:38 +00:00
|
|
|
|
}
|
2020-06-15 19:19:45 +01:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
_descriptorStream.Flush();
|
|
|
|
|
|
_descriptorStream.Close();
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
IsWriting = false;
|
|
|
|
|
|
ErrorMessage = "";
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
return true;
|
|
|
|
|
|
}
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
/// <inheritdoc />
|
2022-12-15 22:21:07 +00:00
|
|
|
|
public bool SetImageInfo(ImageInfo imageInfo) => true;
|
2022-03-06 13:29:38 +00:00
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
|
public bool SetGeometry(uint cylinders, uint heads, uint sectorsPerTrack)
|
|
|
|
|
|
{
|
2022-11-29 02:10:37 +00:00
|
|
|
|
ErrorMessage = Localization.Unsupported_feature;
|
2022-03-06 13:29:38 +00:00
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
|
public bool WriteSectorTag(byte[] data, ulong sectorAddress, SectorTagType tag)
|
|
|
|
|
|
{
|
|
|
|
|
|
if(!IsWriting)
|
|
|
|
|
|
{
|
2022-11-29 02:10:37 +00:00
|
|
|
|
ErrorMessage = Localization.Tried_to_write_on_a_non_writable_image;
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
return false;
|
2018-07-23 23:25:43 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-03-07 07:36:44 +00:00
|
|
|
|
Track track = Tracks.FirstOrDefault(trk => sectorAddress >= trk.StartSector && sectorAddress <= trk.EndSector);
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
if(track is null)
|
2018-07-23 23:25:43 +01:00
|
|
|
|
{
|
2022-11-29 02:10:37 +00:00
|
|
|
|
ErrorMessage = string.Format(Localization.Cant_find_track_containing_0, sectorAddress);
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
2018-07-23 23:25:43 +01:00
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
switch(tag)
|
2018-07-23 23:25:43 +01:00
|
|
|
|
{
|
2022-03-06 13:29:38 +00:00
|
|
|
|
case SectorTagType.CdTrackFlags:
|
2018-07-23 23:25:43 +01:00
|
|
|
|
{
|
2022-03-06 13:29:38 +00:00
|
|
|
|
if(data.Length != 1)
|
|
|
|
|
|
{
|
2022-11-29 02:10:37 +00:00
|
|
|
|
ErrorMessage = Localization.Incorrect_data_size_for_track_flags;
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
return false;
|
|
|
|
|
|
}
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
_trackFlags[(byte)sectorAddress] = data[0];
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
return true;
|
2018-07-23 23:25:43 +01:00
|
|
|
|
}
|
2022-03-06 13:29:38 +00:00
|
|
|
|
case SectorTagType.CdSectorSubchannel:
|
2018-07-23 23:25:43 +01:00
|
|
|
|
{
|
2022-03-06 13:29:38 +00:00
|
|
|
|
if(track.SubchannelType == 0)
|
2018-07-23 23:25:43 +01:00
|
|
|
|
{
|
2022-11-29 02:10:37 +00:00
|
|
|
|
ErrorMessage =
|
|
|
|
|
|
string.Format(Localization.Trying_to_write_subchannel_to_track_0_that_does_not_have_subchannel,
|
|
|
|
|
|
track.Sequence);
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
return false;
|
|
|
|
|
|
}
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
if(data.Length != 96)
|
|
|
|
|
|
{
|
2022-11-29 02:10:37 +00:00
|
|
|
|
ErrorMessage = Localization.Incorrect_data_size_for_subchannel;
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
return false;
|
2018-07-23 23:25:43 +01:00
|
|
|
|
}
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
if(_subStream == null)
|
2023-10-03 23:34:59 +01:00
|
|
|
|
{
|
2022-03-06 13:29:38 +00:00
|
|
|
|
try
|
|
|
|
|
|
{
|
2024-05-01 04:05:22 +01:00
|
|
|
|
_subStream = new FileStream(_writingBaseName + ".sub",
|
|
|
|
|
|
FileMode.OpenOrCreate,
|
|
|
|
|
|
FileAccess.ReadWrite,
|
|
|
|
|
|
FileShare.None);
|
2018-07-23 23:25:43 +01:00
|
|
|
|
}
|
2023-10-08 04:10:19 +01:00
|
|
|
|
catch(IOException ex)
|
2018-07-23 23:25:43 +01:00
|
|
|
|
{
|
2022-11-29 02:10:37 +00:00
|
|
|
|
ErrorMessage = string.Format(Localization.Could_not_create_subchannel_file_exception_0,
|
2023-10-08 04:10:19 +01:00
|
|
|
|
ex.Message);
|
2024-05-01 04:05:22 +01:00
|
|
|
|
|
2023-10-08 04:10:19 +01:00
|
|
|
|
AaruConsole.WriteException(ex);
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
2018-07-23 23:25:43 +01:00
|
|
|
|
return false;
|
|
|
|
|
|
}
|
2023-10-03 23:34:59 +01:00
|
|
|
|
}
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
2023-10-03 23:34:59 +01:00
|
|
|
|
_subStream.Seek((long)(track.SubchannelOffset + (sectorAddress - track.StartSector) * 96),
|
2022-03-06 13:29:38 +00:00
|
|
|
|
SeekOrigin.Begin);
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
_subStream.Write(Subchannel.Deinterleave(data), 0, data.Length);
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
default:
|
2022-11-29 02:10:37 +00:00
|
|
|
|
ErrorMessage = string.Format(Localization.Unsupported_tag_type_0, tag);
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
|
public bool WriteSectorsTag(byte[] data, ulong sectorAddress, uint length, SectorTagType tag)
|
|
|
|
|
|
{
|
|
|
|
|
|
if(!IsWriting)
|
|
|
|
|
|
{
|
2022-11-29 02:10:37 +00:00
|
|
|
|
ErrorMessage = Localization.Tried_to_write_on_a_non_writable_image;
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
return false;
|
2018-07-23 23:25:43 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-03-07 07:36:44 +00:00
|
|
|
|
Track track = Tracks.FirstOrDefault(trk => sectorAddress >= trk.StartSector && sectorAddress <= trk.EndSector);
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
if(track is null)
|
|
|
|
|
|
{
|
2022-11-29 02:10:37 +00:00
|
|
|
|
ErrorMessage = string.Format(Localization.Cant_find_track_containing_0, sectorAddress);
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
return false;
|
|
|
|
|
|
}
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
switch(tag)
|
|
|
|
|
|
{
|
|
|
|
|
|
case SectorTagType.CdTrackFlags:
|
2023-10-03 23:34:59 +01:00
|
|
|
|
case SectorTagType.CdTrackIsrc:
|
|
|
|
|
|
return WriteSectorTag(data, sectorAddress, tag);
|
2022-03-06 13:29:38 +00:00
|
|
|
|
case SectorTagType.CdSectorSubchannel:
|
2018-07-23 23:25:43 +01:00
|
|
|
|
{
|
2022-03-06 13:29:38 +00:00
|
|
|
|
if(track.SubchannelType == 0)
|
|
|
|
|
|
{
|
2022-11-29 02:10:37 +00:00
|
|
|
|
ErrorMessage =
|
|
|
|
|
|
string.Format(Localization.Trying_to_write_subchannel_to_track_0_that_does_not_have_subchannel,
|
|
|
|
|
|
track.Sequence);
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
return false;
|
|
|
|
|
|
}
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
if(data.Length % 96 != 0)
|
2018-07-23 23:25:43 +01:00
|
|
|
|
{
|
2022-11-29 02:10:37 +00:00
|
|
|
|
ErrorMessage = Localization.Incorrect_data_size_for_subchannel;
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
return false;
|
|
|
|
|
|
}
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
if(_subStream == null)
|
2023-10-03 23:34:59 +01:00
|
|
|
|
{
|
2022-03-06 13:29:38 +00:00
|
|
|
|
try
|
|
|
|
|
|
{
|
2024-05-01 04:05:22 +01:00
|
|
|
|
_subStream = new FileStream(_writingBaseName + ".sub",
|
|
|
|
|
|
FileMode.OpenOrCreate,
|
|
|
|
|
|
FileAccess.ReadWrite,
|
|
|
|
|
|
FileShare.None);
|
2022-03-06 13:29:38 +00:00
|
|
|
|
}
|
2023-10-08 04:10:19 +01:00
|
|
|
|
catch(IOException ex)
|
2018-07-23 23:25:43 +01:00
|
|
|
|
{
|
2022-11-29 02:10:37 +00:00
|
|
|
|
ErrorMessage = string.Format(Localization.Could_not_create_subchannel_file_exception_0,
|
2023-10-08 04:10:19 +01:00
|
|
|
|
ex.Message);
|
2024-05-01 04:05:22 +01:00
|
|
|
|
|
2023-10-08 04:10:19 +01:00
|
|
|
|
AaruConsole.WriteException(ex);
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
2018-07-23 23:25:43 +01:00
|
|
|
|
return false;
|
|
|
|
|
|
}
|
2023-10-03 23:34:59 +01:00
|
|
|
|
}
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
2023-10-03 23:34:59 +01:00
|
|
|
|
_subStream.Seek((long)(track.SubchannelOffset + (sectorAddress - track.StartSector) * 96),
|
2022-03-06 13:29:38 +00:00
|
|
|
|
SeekOrigin.Begin);
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
_subStream.Write(Subchannel.Deinterleave(data), 0, data.Length);
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
return true;
|
2018-07-23 23:25:43 +01:00
|
|
|
|
}
|
2022-03-06 13:29:38 +00:00
|
|
|
|
default:
|
2022-11-29 02:10:37 +00:00
|
|
|
|
ErrorMessage = string.Format(Localization.Unsupported_tag_type_0, tag);
|
2022-03-06 13:29:38 +00:00
|
|
|
|
|
|
|
|
|
|
return false;
|
2018-07-23 23:25:43 +01:00
|
|
|
|
}
|
2022-03-06 13:29:38 +00:00
|
|
|
|
}
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
/// <inheritdoc />
|
2022-12-15 22:21:07 +00:00
|
|
|
|
public bool SetDumpHardware(List<DumpHardware> dumpHardware) => false;
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
/// <inheritdoc />
|
2022-12-15 22:21:07 +00:00
|
|
|
|
public bool SetMetadata(Metadata metadata) => false;
|
2023-10-03 23:34:59 +01:00
|
|
|
|
|
|
|
|
|
|
#endregion
|
2018-07-23 23:25:43 +01:00
|
|
|
|
}
|