Files
Aaru/Aaru.Images/DART/Read.cs

413 lines
15 KiB
C#
Raw Normal View History

// /***************************************************************************
2020-02-27 12:31:25 +00:00
// Aaru Data Preservation Suite
// ----------------------------------------------------------------------------
//
// Filename : Read.cs
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// Component : Disk image plugins.
//
// --[ Description ] ----------------------------------------------------------
//
// Reads Apple Disk Archival/Retrieval Tool format.
//
// --[ 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-12-19 10:45:18 +00:00
// Copyright © 2011-2025 Natalia Portillo
// ****************************************************************************/
using System;
using System.IO;
2016-09-09 23:03:59 +01:00
using System.Text.RegularExpressions;
2020-02-27 00:33:26 +00:00
using Aaru.CommonTypes;
using Aaru.CommonTypes.Enums;
using Aaru.CommonTypes.Interfaces;
using Aaru.Compression;
using Aaru.Helpers;
using Aaru.Logging;
2020-02-29 18:03:35 +00:00
using Claunia.Encoding;
using Claunia.RsrcFork;
2025-08-20 18:51:05 +01:00
using Sentry;
2017-12-21 14:30:38 +00:00
using Version = Resources.Version;
namespace Aaru.Images;
2022-03-06 13:29:38 +00:00
public sealed partial class Dart
{
2023-10-03 23:34:59 +01:00
#region IMediaImage Members
2022-03-06 13:29:38 +00:00
/// <inheritdoc />
public ErrorNumber Open(IFilter imageFilter)
2017-12-19 20:33:03 +00:00
{
2022-03-06 13:29:38 +00:00
Stream stream = imageFilter.GetDataForkStream();
2017-12-19 20:33:03 +00:00
2024-05-01 04:05:22 +01:00
if(stream.Length < 84) return ErrorNumber.InvalidArgument;
2017-12-19 20:33:03 +00:00
2022-03-06 13:29:38 +00:00
stream.Seek(0, SeekOrigin.Begin);
var headerB = new byte[Marshal.SizeOf<Header>()];
2017-12-19 20:33:03 +00:00
stream.EnsureRead(headerB, 0, Marshal.SizeOf<Header>());
Header header = Marshal.ByteArrayToStructureBigEndian<Header>(headerB);
2017-12-19 20:33:03 +00:00
2024-05-01 04:05:22 +01:00
if(header.srcCmp > COMPRESS_NONE) return ErrorNumber.NotSupported;
2017-12-19 20:33:03 +00:00
2023-10-03 23:34:59 +01:00
int expectedMaxSize = 84 + header.srcSize * 2 * 524;
2017-12-19 20:33:03 +00:00
2022-03-06 13:29:38 +00:00
switch(header.srcType)
{
case DISK_MAC:
2024-05-01 04:05:22 +01:00
if(header.srcSize != SIZE_MAC_SS && header.srcSize != SIZE_MAC) return ErrorNumber.InvalidArgument;
2022-03-06 13:29:38 +00:00
break;
case DISK_LISA:
2024-05-01 04:05:22 +01:00
if(header.srcSize != SIZE_LISA) return ErrorNumber.InvalidArgument;
2022-03-06 13:29:38 +00:00
break;
case DISK_APPLE2:
2024-05-01 04:05:22 +01:00
if(header.srcSize != DISK_APPLE2) return ErrorNumber.InvalidArgument;
2022-03-06 13:29:38 +00:00
break;
case DISK_MAC_HD:
2024-05-01 04:05:22 +01:00
if(header.srcSize != SIZE_MAC_HD) return ErrorNumber.InvalidArgument;
2022-03-06 13:29:38 +00:00
expectedMaxSize += 64;
break;
case DISK_DOS:
2024-05-01 04:05:22 +01:00
if(header.srcSize != SIZE_DOS) return ErrorNumber.InvalidArgument;
2022-03-06 13:29:38 +00:00
break;
case DISK_DOS_HD:
2024-05-01 04:05:22 +01:00
if(header.srcSize != SIZE_DOS_HD) return ErrorNumber.InvalidArgument;
2022-03-06 13:29:38 +00:00
expectedMaxSize += 64;
break;
2023-10-03 23:34:59 +01:00
default:
return ErrorNumber.InvalidArgument;
2022-03-06 13:29:38 +00:00
}
2017-12-19 20:33:03 +00:00
2024-05-01 04:05:22 +01:00
if(stream.Length > expectedMaxSize) return ErrorNumber.InvalidArgument;
2017-12-19 20:33:03 +00:00
var bLength =
2022-11-14 01:27:11 +00:00
new short[header.srcType is DISK_MAC_HD or DISK_DOS_HD ? BLOCK_ARRAY_LEN_HIGH : BLOCK_ARRAY_LEN_LOW];
2017-12-19 20:33:03 +00:00
for(var i = 0; i < bLength.Length; i++)
2022-03-06 13:29:38 +00:00
{
var tmpShort = new byte[2];
stream.EnsureRead(tmpShort, 0, 2);
2022-03-06 13:29:38 +00:00
bLength[i] = BigEndianBitConverter.ToInt16(tmpShort, 0);
}
var dataMs = new MemoryStream();
var tagMs = new MemoryStream();
2017-12-19 20:33:03 +00:00
2022-03-06 13:29:38 +00:00
foreach(short l in bLength)
2023-10-03 23:34:59 +01:00
{
2024-05-01 04:05:22 +01:00
if(l == 0) continue;
var buffer = new byte[BUFFER_SIZE];
if(l == -1)
{
stream.EnsureRead(buffer, 0, BUFFER_SIZE);
dataMs.Write(buffer, 0, DATA_SIZE);
tagMs.Write(buffer, DATA_SIZE, TAG_SIZE);
}
else
2022-03-06 13:29:38 +00:00
{
byte[] temp;
2017-12-19 20:33:03 +00:00
if(header.srcCmp == COMPRESS_RLE)
2022-03-06 13:29:38 +00:00
{
temp = new byte[l * 2];
stream.EnsureRead(temp, 0, temp.Length);
buffer = new byte[BUFFER_SIZE];
AppleRle.DecodeBuffer(temp, buffer);
2022-03-06 13:29:38 +00:00
dataMs.Write(buffer, 0, DATA_SIZE);
tagMs.Write(buffer, DATA_SIZE, TAG_SIZE);
}
else
{
temp = new byte[l];
stream.EnsureRead(temp, 0, temp.Length);
2020-02-29 18:03:35 +00:00
2025-08-17 06:11:22 +01:00
AaruLogging.Error(Localization.LZH_Compressed_images_not_yet_supported);
2020-02-29 18:03:35 +00:00
return ErrorNumber.NotImplemented;
2017-12-19 20:33:03 +00:00
}
2022-03-06 13:29:38 +00:00
}
2023-10-03 23:34:59 +01:00
}
2017-12-19 20:33:03 +00:00
2022-03-06 13:29:38 +00:00
_dataCache = dataMs.ToArray();
2020-02-29 18:03:35 +00:00
2022-03-16 11:47:00 +00:00
if(header.srcType is DISK_LISA or DISK_MAC or DISK_APPLE2)
2022-03-06 13:29:38 +00:00
{
_imageInfo.ReadableSectorTags.Add(SectorTagType.AppleSonyTag);
2022-03-06 13:29:38 +00:00
_tagCache = tagMs.ToArray();
}
2017-12-19 20:33:03 +00:00
2022-03-06 13:29:38 +00:00
try
{
if(imageFilter.HasResourceFork)
2017-12-19 20:33:03 +00:00
{
2022-03-06 13:29:38 +00:00
var rsrcFork = new ResourceFork(imageFilter.GetResourceForkStream());
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
// "vers"
if(rsrcFork.ContainsKey(0x76657273))
{
Resource versRsrc = rsrcFork.GetResource(0x76657273);
2022-03-06 13:29:38 +00:00
byte[] vers = versRsrc?.GetResource(versRsrc.GetIds()[0]);
2022-03-06 13:29:38 +00:00
if(vers != null)
{
var version = new Version(vers);
2022-03-06 13:29:38 +00:00
string release = null;
string pre = null;
2017-12-19 20:33:03 +00:00
var major = $"{version.MajorVersion}";
var minor = $".{version.MinorVersion / 10}";
2020-02-29 18:03:35 +00:00
2024-05-01 04:05:22 +01:00
if(version.MinorVersion % 10 > 0) release = $".{version.MinorVersion % 10}";
2020-02-29 18:03:35 +00:00
2022-11-15 01:35:06 +00:00
string dev = version.DevStage switch
2023-10-03 23:34:59 +01:00
{
Version.DevelopmentStage.Alpha => "a",
Version.DevelopmentStage.Beta => "b",
Version.DevelopmentStage.PreAlpha => "d",
_ => null
};
2024-05-01 04:05:22 +01:00
if(dev == null && version.PreReleaseVersion > 0) dev = "f";
2024-05-01 04:05:22 +01:00
if(dev != null) pre = $"{version.PreReleaseVersion}";
2022-03-06 13:29:38 +00:00
_imageInfo.ApplicationVersion = $"{major}{minor}{release}{dev}{pre}";
_imageInfo.Application = version.VersionString;
_imageInfo.Comments = version.VersionMessage;
2017-12-19 20:33:03 +00:00
}
2022-03-06 13:29:38 +00:00
}
2017-12-19 20:33:03 +00:00
2022-03-06 13:29:38 +00:00
// "dart"
if(rsrcFork.ContainsKey(0x44415254))
{
Resource dartRsrc = rsrcFork.GetResource(0x44415254);
if(dartRsrc != null)
2017-12-19 20:33:03 +00:00
{
2022-03-06 13:29:38 +00:00
string dArt = StringHandlers.PascalToString(dartRsrc.GetResource(dartRsrc.GetIds()[0]),
Encoding.GetEncoding("macintosh"));
var dArtEx = new Regex(DART_REGEX);
Match dArtMatch = dArtEx.Match(dArt);
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
if(dArtMatch.Success)
2017-12-19 20:33:03 +00:00
{
2022-03-06 13:29:38 +00:00
_imageInfo.Application = "DART";
_imageInfo.ApplicationVersion = dArtMatch.Groups["version"].Value;
_dataChecksum = Convert.ToUInt32(dArtMatch.Groups["datachk"].Value, 16);
2023-10-03 23:34:59 +01:00
_tagChecksum = Convert.ToUInt32(dArtMatch.Groups["tagchk"].Value, 16);
2017-12-19 20:33:03 +00:00
}
}
2022-03-06 13:29:38 +00:00
}
2017-12-19 20:33:03 +00:00
2022-03-06 13:29:38 +00:00
// "cksm"
if(rsrcFork.ContainsKey(0x434B534D))
{
Resource cksmRsrc = rsrcFork.GetResource(0x434B534D);
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
if(cksmRsrc?.ContainsId(1) == true)
{
byte[] tagChk = cksmRsrc.GetResource(1);
_tagChecksum = BigEndianBitConverter.ToUInt32(tagChk, 0);
}
2022-03-06 13:29:38 +00:00
if(cksmRsrc?.ContainsId(2) == true)
{
byte[] dataChk = cksmRsrc.GetResource(1);
_dataChecksum = BigEndianBitConverter.ToUInt32(dataChk, 0);
2017-12-19 20:33:03 +00:00
}
}
}
2022-03-06 13:29:38 +00:00
}
2025-08-20 18:51:05 +01:00
catch(InvalidCastException ex)
{
SentrySdk.CaptureException(ex);
}
2017-12-19 20:33:03 +00:00
2025-08-17 06:11:22 +01:00
AaruLogging.Debug(MODULE_NAME,
2025-08-20 18:51:05 +01:00
Localization.Image_application_0_version_1,
_imageInfo.Application,
_imageInfo.ApplicationVersion);
2017-12-19 20:33:03 +00:00
2022-03-06 13:29:38 +00:00
_imageInfo.Sectors = (ulong)(header.srcSize * 2);
_imageInfo.CreationTime = imageFilter.CreationTime;
_imageInfo.LastModificationTime = imageFilter.LastWriteTime;
_imageInfo.MediaTitle = Path.GetFileNameWithoutExtension(imageFilter.Filename);
_imageInfo.SectorSize = SECTOR_SIZE;
_imageInfo.MetadataMediaType = MetadataMediaType.BlockMedia;
2022-03-06 13:29:38 +00:00
_imageInfo.ImageSize = _imageInfo.Sectors * SECTOR_SIZE;
_imageInfo.Version = header.srcCmp == COMPRESS_NONE ? "1.4" : "1.5";
2017-12-19 20:33:03 +00:00
2022-03-06 13:29:38 +00:00
switch(header.srcSize)
{
case SIZE_MAC_SS:
_imageInfo.Cylinders = 80;
_imageInfo.Heads = 1;
_imageInfo.SectorsPerTrack = 10;
_imageInfo.MediaType = MediaType.AppleSonySS;
break;
case SIZE_MAC:
_imageInfo.Cylinders = 80;
_imageInfo.Heads = 2;
_imageInfo.SectorsPerTrack = 10;
_imageInfo.MediaType = MediaType.AppleSonyDS;
break;
case SIZE_DOS:
_imageInfo.Cylinders = 80;
_imageInfo.Heads = 2;
_imageInfo.SectorsPerTrack = 9;
_imageInfo.MediaType = MediaType.DOS_35_DS_DD_9;
break;
case SIZE_MAC_HD:
_imageInfo.Cylinders = 80;
_imageInfo.Heads = 2;
_imageInfo.SectorsPerTrack = 18;
_imageInfo.MediaType = MediaType.DOS_35_HD;
break;
2017-12-19 20:33:03 +00:00
}
2022-03-06 13:29:38 +00:00
return ErrorNumber.NoError;
}
2017-12-19 20:33:03 +00:00
2022-03-06 13:29:38 +00:00
/// <inheritdoc />
public ErrorNumber ReadSector(ulong sectorAddress, bool negative, out byte[] buffer, out SectorStatus sectorStatus)
{
sectorStatus = SectorStatus.Dumped;
return ReadSectors(sectorAddress, negative, 1, out buffer, out _);
}
2017-12-19 20:33:03 +00:00
2022-03-06 13:29:38 +00:00
/// <inheritdoc />
public ErrorNumber ReadSectorTag(ulong sectorAddress, bool negative, SectorTagType tag, out byte[] buffer) =>
ReadSectorsTag(sectorAddress, negative, 1, tag, out buffer);
2022-03-06 13:29:38 +00:00
/// <inheritdoc />
public ErrorNumber ReadSectors(ulong sectorAddress, bool negative, uint length, out byte[] buffer,
out SectorStatus[] sectorStatus)
2022-03-06 13:29:38 +00:00
{
buffer = null;
sectorStatus = null;
2017-12-19 20:33:03 +00:00
if(negative) return ErrorNumber.NotSupported;
2024-05-01 04:05:22 +01:00
if(sectorAddress > _imageInfo.Sectors - 1) return ErrorNumber.OutOfRange;
2017-12-19 20:33:03 +00:00
2024-05-01 04:05:22 +01:00
if(sectorAddress + length > _imageInfo.Sectors) return ErrorNumber.OutOfRange;
2017-12-19 20:33:03 +00:00
buffer = new byte[length * _imageInfo.SectorSize];
sectorStatus = new SectorStatus[length];
for(uint i = 0; i < length; i++) sectorStatus[i] = SectorStatus.Dumped;
2017-12-19 20:33:03 +00:00
2022-03-07 07:36:44 +00:00
Array.Copy(_dataCache, (int)sectorAddress * _imageInfo.SectorSize, buffer, 0, length * _imageInfo.SectorSize);
2017-12-19 20:33:03 +00:00
2022-03-06 13:29:38 +00:00
return ErrorNumber.NoError;
}
2022-03-06 13:29:38 +00:00
/// <inheritdoc />
public ErrorNumber ReadSectorsTag(ulong sectorAddress, bool negative, uint length, SectorTagType tag,
out byte[] buffer)
2022-03-06 13:29:38 +00:00
{
buffer = null;
2017-12-19 20:33:03 +00:00
if(negative) return ErrorNumber.NotSupported;
if(tag != SectorTagType.AppleSonyTag) return ErrorNumber.NotSupported;
2017-12-19 20:33:03 +00:00
2024-05-01 04:05:22 +01:00
if(_tagCache == null || _tagCache.Length == 0) return ErrorNumber.NoData;
2017-12-19 20:33:03 +00:00
2024-05-01 04:05:22 +01:00
if(sectorAddress > _imageInfo.Sectors - 1) return ErrorNumber.OutOfRange;
2017-12-19 20:33:03 +00:00
2024-05-01 04:05:22 +01:00
if(sectorAddress + length > _imageInfo.Sectors) return ErrorNumber.OutOfRange;
2017-12-19 20:33:03 +00:00
2022-03-06 13:29:38 +00:00
buffer = new byte[length * TAG_SECTOR_SIZE];
2017-12-19 20:33:03 +00:00
2022-03-06 13:29:38 +00:00
Array.Copy(_tagCache, (int)sectorAddress * TAG_SECTOR_SIZE, buffer, 0, length * TAG_SECTOR_SIZE);
2017-12-19 20:33:03 +00:00
2022-03-06 13:29:38 +00:00
return ErrorNumber.NoError;
}
2017-12-19 20:33:03 +00:00
2022-03-06 13:29:38 +00:00
/// <inheritdoc />
public ErrorNumber ReadSectorLong(ulong sectorAddress, bool negative, out byte[] buffer,
out SectorStatus sectorStatus)
{
sectorStatus = SectorStatus.Dumped;
return ReadSectorsLong(sectorAddress, negative, 1, out buffer, out _);
}
2022-03-06 13:29:38 +00:00
/// <inheritdoc />
public ErrorNumber ReadSectorsLong(ulong sectorAddress, bool negative, uint length, out byte[] buffer,
out SectorStatus[] sectorStatus)
2022-03-06 13:29:38 +00:00
{
buffer = null;
sectorStatus = null;
2017-12-19 20:33:03 +00:00
if(negative) return ErrorNumber.NotSupported;
2024-05-01 04:05:22 +01:00
if(sectorAddress > _imageInfo.Sectors - 1) return ErrorNumber.OutOfRange;
2017-12-19 20:33:03 +00:00
2024-05-01 04:05:22 +01:00
if(sectorAddress + length > _imageInfo.Sectors) return ErrorNumber.OutOfRange;
ErrorNumber errno = ReadSectors(sectorAddress, false, length, out byte[] data, out sectorStatus);
2024-05-01 04:05:22 +01:00
if(errno != ErrorNumber.NoError) return errno;
errno = ReadSectorsTag(sectorAddress, false, length, SectorTagType.AppleSonyTag, out byte[] tags);
2024-05-01 04:05:22 +01:00
if(errno != ErrorNumber.NoError) return errno;
2017-12-19 20:33:03 +00:00
2022-03-06 13:29:38 +00:00
buffer = new byte[data.Length + tags.Length];
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
for(uint i = 0; i < length; i++)
{
2024-05-01 04:05:22 +01:00
Array.Copy(data,
i * _imageInfo.SectorSize,
buffer,
i * (_imageInfo.SectorSize + TAG_SECTOR_SIZE),
2022-03-06 13:29:38 +00:00
_imageInfo.SectorSize);
2017-12-19 20:33:03 +00:00
2024-05-01 04:05:22 +01:00
Array.Copy(tags,
i * TAG_SECTOR_SIZE,
buffer,
i * (_imageInfo.SectorSize + TAG_SECTOR_SIZE) + _imageInfo.SectorSize,
TAG_SECTOR_SIZE);
2017-12-19 20:33:03 +00:00
}
2022-03-06 13:29:38 +00:00
return ErrorNumber.NoError;
2017-12-19 20:33:03 +00:00
}
2023-10-03 23:34:59 +01:00
#endregion
}