Files
Aaru/Aaru.Images/CDRDAO/Write.cs

798 lines
27 KiB
C#
Raw Normal View History

// /***************************************************************************
2020-02-27 12:31:25 +00:00
// Aaru Data Preservation Suite
// ----------------------------------------------------------------------------
//
// 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/>.
//
// ----------------------------------------------------------------------------
2020-01-03 17:51:30 +00:00
// Copyright © 2011-2020 Natalia Portillo
// ****************************************************************************/
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;
using Aaru.CommonTypes.Enums;
using Aaru.CommonTypes.Structs;
using Schemas;
2020-02-27 00:33:26 +00:00
using TrackType = Aaru.CommonTypes.Enums.TrackType;
2020-02-27 00:33:26 +00:00
namespace Aaru.DiscImages
{
public partial class Cdrdao
{
public bool Create(string path, MediaType mediaType, Dictionary<string, string> options, ulong sectors,
2020-02-29 18:03:35 +00:00
uint sectorSize)
{
if(options != null)
{
if(options.TryGetValue("separate", out string tmpValue))
{
2020-07-20 21:11:32 +01:00
if(!bool.TryParse(tmpValue, out _separateTracksWriting))
{
ErrorMessage = "Invalid value for split option";
2020-02-29 18:03:35 +00:00
return false;
}
2020-07-20 21:11:32 +01:00
if(_separateTracksWriting)
{
ErrorMessage = "Separate tracksnot yet implemented";
2020-02-29 18:03:35 +00:00
return false;
}
}
}
2020-02-29 18:03:35 +00:00
else
2020-07-20 21:11:32 +01:00
_separateTracksWriting = false;
if(!SupportedMediaTypes.Contains(mediaType))
{
ErrorMessage = $"Unsupport media format {mediaType}";
2020-02-29 18:03:35 +00:00
return false;
}
2020-07-20 21:11:32 +01:00
_imageInfo = new ImageInfo
2020-02-29 18:03:35 +00:00
{
2020-07-20 04:34:16 +01:00
MediaType = mediaType,
SectorSize = sectorSize,
Sectors = sectors
2020-02-29 18:03:35 +00:00
};
// TODO: Separate tracks
try
{
2020-07-20 21:11:32 +01:00
_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}";
2020-02-29 18:03:35 +00:00
return false;
}
2020-07-20 21:11:32 +01:00
_discimage = new CdrdaoDisc
2020-02-29 18:03:35 +00:00
{
2020-07-20 04:34:16 +01:00
Disktype = mediaType,
Tracks = new List<CdrdaoTrack>()
2020-02-29 18:03:35 +00:00
};
2020-07-20 21:11:32 +01:00
_trackFlags = new Dictionary<byte, byte>();
_trackIsrcs = new Dictionary<byte, string>();
IsWriting = true;
ErrorMessage = null;
2020-02-29 18:03:35 +00:00
return true;
}
public bool WriteMediaTag(byte[] data, MediaTagType tag)
{
if(!IsWriting)
{
ErrorMessage = "Tried to write on a non-writable image";
2020-02-29 18:03:35 +00:00
return false;
}
switch(tag)
{
case MediaTagType.CD_MCN:
2020-07-20 21:11:32 +01:00
_discimage.Mcn = Encoding.ASCII.GetString(data);
2020-02-29 18:03:35 +00:00
return true;
default:
ErrorMessage = $"Unsupported media tag {tag}";
2020-02-29 18:03:35 +00:00
return false;
}
}
public bool WriteSector(byte[] data, ulong sectorAddress)
{
if(!IsWriting)
{
ErrorMessage = "Tried to write on a non-writable image";
2020-02-29 18:03:35 +00:00
return false;
}
Track track =
2020-07-20 21:11:32 +01:00
_writingTracks.FirstOrDefault(trk => sectorAddress >= trk.TrackStartSector &&
sectorAddress <= trk.TrackEndSector);
2020-06-21 14:49:25 +01:00
if(track is null)
{
ErrorMessage = $"Can't found track containing {sectorAddress}";
2020-02-29 18:03:35 +00:00
return false;
}
2020-07-20 21:11:32 +01:00
FileStream trackStream = _writingStreams.FirstOrDefault(kvp => kvp.Key == track.TrackSequence).Value;
if(trackStream == null)
{
ErrorMessage = $"Can't found file containing {sectorAddress}";
2020-02-29 18:03:35 +00:00
return false;
}
if(track.TrackBytesPerSector != track.TrackRawBytesPerSector)
{
ErrorMessage = "Invalid write mode for this sector";
2020-02-29 18:03:35 +00:00
return false;
}
if(data.Length != track.TrackRawBytesPerSector)
{
ErrorMessage = "Incorrect data size";
2020-02-29 18:03:35 +00:00
return false;
}
// cdrdao audio tracks are endian swapped corresponding to Aaru
if(track.TrackType == TrackType.Audio)
{
byte[] swapped = new byte[data.Length];
2020-02-29 18:03:35 +00:00
for(long i = 0; i < swapped.Length; i += 2)
{
swapped[i] = data[i + 1];
swapped[i + 1] = data[i];
}
data = swapped;
}
2020-02-29 18:03:35 +00:00
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";
2020-02-29 18:03:35 +00:00
return false;
}
Track track =
2020-07-20 21:11:32 +01:00
_writingTracks.FirstOrDefault(trk => sectorAddress >= trk.TrackStartSector &&
sectorAddress <= trk.TrackEndSector);
2020-06-21 14:49:25 +01:00
if(track is null)
{
ErrorMessage = $"Can't found track containing {sectorAddress}";
2020-02-29 18:03:35 +00:00
return false;
}
2020-07-20 21:11:32 +01:00
FileStream trackStream = _writingStreams.FirstOrDefault(kvp => kvp.Key == track.TrackSequence).Value;
if(trackStream == null)
{
ErrorMessage = $"Can't found file containing {sectorAddress}";
2020-02-29 18:03:35 +00:00
return false;
}
if(track.TrackBytesPerSector != track.TrackRawBytesPerSector)
{
ErrorMessage = "Invalid write mode for this sector";
2020-02-29 18:03:35 +00:00
return false;
}
if(sectorAddress + length > track.TrackEndSector + 1)
{
ErrorMessage = "Can't cross tracks";
2020-02-29 18:03:35 +00:00
return false;
}
if(data.Length % track.TrackRawBytesPerSector != 0)
{
ErrorMessage = "Incorrect data size";
2020-02-29 18:03:35 +00:00
return false;
}
// cdrdao audio tracks are endian swapped corresponding to Aaru
if(track.TrackType == TrackType.Audio)
{
byte[] swapped = new byte[data.Length];
2020-02-29 18:03:35 +00:00
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:
2020-02-29 18:03:35 +00:00
trackStream.
Seek((long)(track.TrackFileOffset + ((sectorAddress - track.TrackStartSector) * (ulong)track.TrackRawBytesPerSector)),
SeekOrigin.Begin);
2020-02-29 18:03:35 +00:00
trackStream.Write(data, 0, data.Length);
ErrorMessage = "";
2020-02-29 18:03:35 +00:00
return true;
case TrackSubchannelType.Raw:
case TrackSubchannelType.RawInterleaved:
2020-02-29 18:03:35 +00:00
trackStream.
Seek((long)(track.TrackFileOffset + ((sectorAddress - track.TrackStartSector) * (ulong)(track.TrackRawBytesPerSector + 96))),
SeekOrigin.Begin);
2020-02-29 18:03:35 +00:00
for(uint i = 0; i < length; i++)
{
trackStream.Write(data, (int)(i * track.TrackRawBytesPerSector), track.TrackRawBytesPerSector);
trackStream.Position += 96;
}
ErrorMessage = "";
2020-02-29 18:03:35 +00:00
return true;
default:
ErrorMessage = "Invalid subchannel mode for this sector";
2020-02-29 18:03:35 +00:00
return false;
}
}
public bool WriteSectorLong(byte[] data, ulong sectorAddress)
{
if(!IsWriting)
{
ErrorMessage = "Tried to write on a non-writable image";
2020-02-29 18:03:35 +00:00
return false;
}
Track track =
2020-07-20 21:11:32 +01:00
_writingTracks.FirstOrDefault(trk => sectorAddress >= trk.TrackStartSector &&
sectorAddress <= trk.TrackEndSector);
2020-06-21 14:49:25 +01:00
if(track is null)
{
ErrorMessage = $"Can't found track containing {sectorAddress}";
2020-02-29 18:03:35 +00:00
return false;
}
2020-07-20 21:11:32 +01:00
FileStream trackStream = _writingStreams.FirstOrDefault(kvp => kvp.Key == track.TrackSequence).Value;
if(trackStream == null)
{
ErrorMessage = $"Can't found file containing {sectorAddress}";
2020-02-29 18:03:35 +00:00
return false;
}
if(data.Length != track.TrackRawBytesPerSector)
{
ErrorMessage = "Incorrect data size";
2020-02-29 18:03:35 +00:00
return false;
}
// cdrdao audio tracks are endian swapped corresponding to Aaru
if(track.TrackType == TrackType.Audio)
{
byte[] swapped = new byte[data.Length];
2020-02-29 18:03:35 +00:00
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);
2020-02-29 18:03:35 +00:00
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";
2020-02-29 18:03:35 +00:00
return false;
}
Track track =
2020-07-20 21:11:32 +01:00
_writingTracks.FirstOrDefault(trk => sectorAddress >= trk.TrackStartSector &&
sectorAddress <= trk.TrackEndSector);
2020-06-21 14:49:25 +01:00
if(track is null)
{
ErrorMessage = $"Can't found track containing {sectorAddress}";
2020-02-29 18:03:35 +00:00
return false;
}
2020-07-20 21:11:32 +01:00
FileStream trackStream = _writingStreams.FirstOrDefault(kvp => kvp.Key == track.TrackSequence).Value;
if(trackStream == null)
{
ErrorMessage = $"Can't found file containing {sectorAddress}";
2020-02-29 18:03:35 +00:00
return false;
}
if(sectorAddress + length > track.TrackEndSector + 1)
{
ErrorMessage = "Can't cross tracks";
2020-02-29 18:03:35 +00:00
return false;
}
if(data.Length % track.TrackRawBytesPerSector != 0)
{
ErrorMessage = "Incorrect data size";
2020-02-29 18:03:35 +00:00
return false;
}
// cdrdao audio tracks are endian swapped corresponding to Aaru
if(track.TrackType == TrackType.Audio)
{
byte[] swapped = new byte[data.Length];
2020-02-29 18:03:35 +00:00
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++)
{
2020-02-29 18:03:35 +00:00
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";
2020-02-29 18:03:35 +00:00
return false;
}
2020-02-29 18:03:35 +00:00
if(tracks == null ||
tracks.Count == 0)
{
ErrorMessage = "Invalid tracks sent";
2020-02-29 18:03:35 +00:00
return false;
}
2020-07-20 21:11:32 +01:00
if(_writingTracks != null &&
_writingStreams != null)
foreach(FileStream oldTrack in _writingStreams.Select(t => t.Value).Distinct())
oldTrack.Close();
ulong currentOffset = 0;
2020-07-20 21:11:32 +01:00
_writingTracks = new List<Track>();
2020-02-29 18:03:35 +00:00
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}";
2020-02-29 18:03:35 +00:00
return false;
}
Track newTrack = track;
2020-02-29 18:03:35 +00:00
2020-07-20 21:11:32 +01:00
newTrack.TrackFile = _separateTracksWriting ? _writingBaseName + $"_track{track.TrackSequence:D2}.bin"
: _writingBaseName + ".bin";
2020-02-29 18:03:35 +00:00
2020-07-20 21:11:32 +01:00
newTrack.TrackFileOffset = _separateTracksWriting ? 0 : currentOffset;
_writingTracks.Add(newTrack);
2020-02-29 18:03:35 +00:00
currentOffset += (ulong)newTrack.TrackRawBytesPerSector *
2020-02-29 18:03:35 +00:00
((newTrack.TrackEndSector - newTrack.TrackStartSector) + 1);
if(track.TrackSubchannelType != TrackSubchannelType.None)
2020-02-29 18:03:35 +00:00
currentOffset += 96 * ((newTrack.TrackEndSector - newTrack.TrackStartSector) + 1);
}
2020-07-20 21:11:32 +01:00
_writingStreams = new Dictionary<uint, FileStream>();
2020-02-29 18:03:35 +00:00
2020-07-20 21:11:32 +01:00
if(_separateTracksWriting)
foreach(Track track in _writingTracks)
_writingStreams.Add(track.TrackSequence,
new FileStream(track.TrackFile, FileMode.OpenOrCreate, FileAccess.ReadWrite,
FileShare.None));
else
{
2020-07-20 21:11:32 +01:00
var jointstream = new FileStream(_writingBaseName + ".bin", FileMode.OpenOrCreate, FileAccess.ReadWrite,
2020-02-29 18:03:35 +00:00
FileShare.None);
2020-07-20 21:11:32 +01:00
foreach(Track track in _writingTracks)
_writingStreams.Add(track.TrackSequence, jointstream);
}
return true;
}
public bool Close()
{
if(!IsWriting)
{
ErrorMessage = "Image is not opened for writing";
2020-02-29 18:03:35 +00:00
return false;
}
2020-07-20 21:11:32 +01:00
if(_separateTracksWriting)
foreach(FileStream writingStream in _writingStreams.Values)
{
writingStream.Flush();
writingStream.Close();
}
else
{
2020-07-20 21:11:32 +01:00
_writingStreams.First().Value.Flush();
_writingStreams.First().Value.Close();
}
2020-07-20 21:11:32 +01:00
bool data = _writingTracks.Count(t => t.TrackType != TrackType.Audio) > 0;
2020-02-29 18:03:35 +00:00
2020-07-20 21:11:32 +01:00
bool mode2 = _writingTracks.Count(t => t.TrackType == TrackType.CdMode2Form1 ||
t.TrackType == TrackType.CdMode2Form2 ||
t.TrackType == TrackType.CdMode2Formless) > 0;
2020-02-29 18:03:35 +00:00
if(mode2)
2020-07-20 21:11:32 +01:00
_descriptorStream.WriteLine("CD_ROM_XA");
2020-02-29 18:03:35 +00:00
else if(data)
2020-07-20 21:11:32 +01:00
_descriptorStream.WriteLine("CD_ROM");
2020-02-29 18:03:35 +00:00
else
2020-07-20 21:11:32 +01:00
_descriptorStream.WriteLine("CD_DA");
2020-07-20 21:11:32 +01:00
if(!string.IsNullOrWhiteSpace(_discimage.Comment))
{
2020-07-20 21:11:32 +01:00
string[] commentLines = _discimage.Comment.Split(new[]
2020-02-29 18:03:35 +00:00
{
'\n'
}, StringSplitOptions.RemoveEmptyEntries);
foreach(string line in commentLines)
2020-07-20 21:11:32 +01:00
_descriptorStream.WriteLine("// {0}", line);
}
2020-07-20 21:11:32 +01:00
_descriptorStream.WriteLine();
2020-07-20 21:11:32 +01:00
if(!string.IsNullOrEmpty(_discimage.Mcn))
_descriptorStream.WriteLine("CATALOG {0}", _discimage.Mcn);
2020-07-20 21:11:32 +01:00
foreach(Track track in _writingTracks)
{
2020-07-20 21:11:32 +01:00
_descriptorStream.WriteLine();
_descriptorStream.WriteLine("// Track {0}", track.TrackSequence);
string subchannelType;
switch(track.TrackSubchannelType)
{
case TrackSubchannelType.Packed:
case TrackSubchannelType.PackedInterleaved:
subchannelType = " RW";
2020-02-29 18:03:35 +00:00
break;
case TrackSubchannelType.Raw:
case TrackSubchannelType.RawInterleaved:
subchannelType = " RW_RAW";
2020-02-29 18:03:35 +00:00
break;
default:
subchannelType = "";
2020-02-29 18:03:35 +00:00
break;
}
2020-07-20 21:11:32 +01:00
_descriptorStream.WriteLine("TRACK {0}{1}", GetTrackMode(track), subchannelType);
2020-07-20 21:11:32 +01:00
_trackFlags.TryGetValue((byte)track.TrackSequence, out byte flagsByte);
2020-02-29 18:03:35 +00:00
var flags = (CdFlags)flagsByte;
2020-07-20 21:11:32 +01:00
_descriptorStream.WriteLine("{0}COPY", flags.HasFlag(CdFlags.CopyPermitted) ? "" : "NO ");
if(track.TrackType == TrackType.Audio)
{
2020-07-20 21:11:32 +01:00
_descriptorStream.WriteLine("{0}PRE_EMPHASIS", flags.HasFlag(CdFlags.PreEmphasis) ? "" : "NO ");
2020-02-29 18:03:35 +00:00
2020-07-20 21:11:32 +01:00
_descriptorStream.WriteLine("{0}_CHANNEL_AUDIO",
flags.HasFlag(CdFlags.FourChannel) ? "FOUR" : "TWO");
}
2020-07-20 21:11:32 +01:00
if(_trackIsrcs.TryGetValue((byte)track.TrackSequence, out string isrc))
_descriptorStream.WriteLine("ISRC {0}", isrc);
2020-02-29 18:03:35 +00:00
(byte minute, byte second, byte frame) msf =
LbaToMsf((track.TrackEndSector - track.TrackStartSector) + 1);
2020-07-20 21:11:32 +01:00
_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)));
2020-07-19 21:38:55 +01:00
foreach(KeyValuePair<ushort, int> index in track.Indexes.OrderBy(i => i.Key).Where(i => i.Key > 1))
{
msf = LbaToMsf((ulong)index.Value - (track.TrackPregap + track.TrackStartSector));
2020-07-20 21:11:32 +01:00
_descriptorStream.WriteLine("INDEX {0:D2}:{1:D2}:{2:D2}", msf.minute, msf.second, msf.frame);
2020-07-19 21:38:55 +01:00
}
2020-07-20 21:11:32 +01:00
_descriptorStream.WriteLine();
}
2020-07-20 21:11:32 +01:00
_descriptorStream.Flush();
_descriptorStream.Close();
IsWriting = false;
ErrorMessage = "";
2020-02-29 18:03:35 +00:00
return true;
}
public bool SetMetadata(ImageInfo metadata)
{
2020-07-20 21:11:32 +01:00
_discimage.Barcode = metadata.MediaBarcode;
_discimage.Comment = metadata.Comments;
2020-02-29 18:03:35 +00:00
return true;
}
public bool SetGeometry(uint cylinders, uint heads, uint sectorsPerTrack)
{
ErrorMessage = "Unsupported feature";
2020-02-29 18:03:35 +00:00
return false;
}
public bool WriteSectorTag(byte[] data, ulong sectorAddress, SectorTagType tag)
{
if(!IsWriting)
{
ErrorMessage = "Tried to write on a non-writable image";
2020-02-29 18:03:35 +00:00
return false;
}
Track track =
2020-07-20 21:11:32 +01:00
_writingTracks.FirstOrDefault(trk => sectorAddress >= trk.TrackStartSector &&
sectorAddress <= trk.TrackEndSector);
2020-06-21 14:49:25 +01:00
if(track is null)
{
ErrorMessage = $"Can't found track containing {sectorAddress}";
2020-02-29 18:03:35 +00:00
return false;
}
switch(tag)
{
case SectorTagType.CdTrackFlags:
{
if(data.Length != 1)
{
ErrorMessage = "Incorrect data size for track flags";
2020-02-29 18:03:35 +00:00
return false;
}
2020-07-20 21:11:32 +01:00
_trackFlags[(byte)sectorAddress] = data[0];
return true;
}
case SectorTagType.CdTrackIsrc:
{
2020-02-29 18:03:35 +00:00
if(data != null)
2020-07-20 21:11:32 +01:00
_trackIsrcs[(byte)sectorAddress] = Encoding.UTF8.GetString(data);
2020-02-29 18:03:35 +00:00
return true;
}
case SectorTagType.CdSectorSubchannel:
{
if(track.TrackSubchannelType == 0)
{
ErrorMessage =
$"Trying to write subchannel to track {track.TrackSequence}, that does not have subchannel";
2020-02-29 18:03:35 +00:00
return false;
}
if(data.Length != 96)
{
ErrorMessage = "Incorrect data size for subchannel";
2020-02-29 18:03:35 +00:00
return false;
}
2020-07-20 21:11:32 +01:00
FileStream trackStream =
_writingStreams.FirstOrDefault(kvp => kvp.Key == track.TrackSequence).Value;
if(trackStream == null)
{
ErrorMessage = $"Can't found file containing {sectorAddress}";
2020-02-29 18:03:35 +00:00
return false;
}
2020-02-29 18:03:35 +00:00
trackStream.
Seek((long)(track.TrackFileOffset + ((sectorAddress - track.TrackStartSector) * (ulong)(track.TrackRawBytesPerSector + 96))) + track.TrackRawBytesPerSector,
SeekOrigin.Begin);
2020-02-29 18:03:35 +00:00
trackStream.Write(data, 0, data.Length);
return true;
}
default:
ErrorMessage = $"Unsupported tag type {tag}";
2020-02-29 18:03:35 +00:00
return false;
}
}
public bool WriteSectorsTag(byte[] data, ulong sectorAddress, uint length, SectorTagType tag)
{
if(!IsWriting)
{
ErrorMessage = "Tried to write on a non-writable image";
2020-02-29 18:03:35 +00:00
return false;
}
Track track =
2020-07-20 21:11:32 +01:00
_writingTracks.FirstOrDefault(trk => sectorAddress >= trk.TrackStartSector &&
sectorAddress <= trk.TrackEndSector);
2020-06-21 14:49:25 +01:00
if(track is null)
{
ErrorMessage = $"Can't found track containing {sectorAddress}";
2020-02-29 18:03:35 +00:00
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";
2020-02-29 18:03:35 +00:00
return false;
}
if(data.Length % 96 != 0)
{
ErrorMessage = "Incorrect data size for subchannel";
2020-02-29 18:03:35 +00:00
return false;
}
2020-07-20 21:11:32 +01:00
FileStream trackStream =
_writingStreams.FirstOrDefault(kvp => kvp.Key == track.TrackSequence).Value;
if(trackStream == null)
{
ErrorMessage = $"Can't found file containing {sectorAddress}";
2020-02-29 18:03:35 +00:00
return false;
}
for(uint i = 0; i < length; i++)
{
2020-02-29 18:03:35 +00:00
trackStream.
Seek((long)(track.TrackFileOffset + (((i + sectorAddress) - track.TrackStartSector) * (ulong)(track.TrackRawBytesPerSector + 96))) + track.TrackRawBytesPerSector,
SeekOrigin.Begin);
2020-02-29 18:03:35 +00:00
trackStream.Write(data, (int)(i * 96), 96);
}
return true;
}
default:
ErrorMessage = $"Unsupported tag type {tag}";
2020-02-29 18:03:35 +00:00
return false;
}
}
2018-12-31 13:17:27 +00:00
public bool SetDumpHardware(List<DumpHardwareType> dumpHardware) => false;
2018-12-31 13:17:27 +00:00
public bool SetCicmMetadata(CICMMetadataType metadata) => false;
}
}