Files
Aaru/Aaru.Images/AppleDOS/Read.cs

134 lines
4.9 KiB
C#
Raw Permalink 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 interleaved Apple ][ disk 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-12-19 10:45:18 +00:00
// Copyright © 2011-2025 Natalia Portillo
// ****************************************************************************/
using System;
using System.IO;
2020-02-27 00:33:26 +00:00
using Aaru.CommonTypes;
using Aaru.CommonTypes.Enums;
using Aaru.CommonTypes.Interfaces;
using Aaru.Helpers;
namespace Aaru.Images;
2022-03-06 13:29:38 +00:00
public sealed partial class AppleDos
{
2023-10-03 23:34:59 +01:00
#region IWritableImage Members
2022-03-06 13:29:38 +00:00
/// <inheritdoc />
public ErrorNumber Open(IFilter imageFilter)
{
2022-03-06 13:29:38 +00:00
Stream stream = imageFilter.GetDataForkStream();
stream.Seek(0, SeekOrigin.Begin);
2023-10-03 23:34:59 +01:00
var tmp = new byte[imageFilter.DataForkLength];
stream.EnsureRead(tmp, 0, tmp.Length);
2022-03-06 13:29:38 +00:00
_extension = Path.GetExtension(imageFilter.Filename)?.ToLower();
if(_extension is ".d13" or ".do" && tmp.Length == 116480)
{
2022-03-06 13:29:38 +00:00
_dos32 = true;
_deinterleaved = tmp;
}
else
{
bool isDos = tmp[0x11001] == 17 &&
tmp[0x11002] < 16 &&
tmp[0x11027] <= 122 &&
tmp[0x11034] == 35 &&
tmp[0x11035] == 16 &&
tmp[0x11036] == 0 &&
tmp[0x11037] == 1;
2022-03-06 13:29:38 +00:00
_deinterleaved = new byte[tmp.Length];
2024-05-01 04:05:22 +01:00
int[] offsets = _extension == ".do"
? isDos ? _deinterleave : _interleave
: isDos
? _interleave
: _deinterleave;
2023-10-03 23:34:59 +01:00
for(var t = 0; t < 35; t++)
{
2023-10-03 23:34:59 +01:00
for(var s = 0; s < 16; s++)
Array.Copy(tmp, t * 16 * 256 + s * 256, _deinterleaved, t * 16 * 256 + offsets[s] * 256, 256);
}
}
2022-03-06 13:29:38 +00:00
_imageInfo.SectorSize = 256;
_imageInfo.ImageSize = (ulong)imageFilter.DataForkLength;
_imageInfo.CreationTime = imageFilter.CreationTime;
_imageInfo.LastModificationTime = imageFilter.LastWriteTime;
_imageInfo.MediaTitle = Path.GetFileNameWithoutExtension(imageFilter.Filename);
_imageInfo.Sectors = _dos32 ? 455u : 560u;
_imageInfo.MediaType = _dos32 ? MediaType.Apple32SS : MediaType.Apple33SS;
_imageInfo.MetadataMediaType = MetadataMediaType.BlockMedia;
2022-03-06 13:29:38 +00:00
_imageInfo.Cylinders = 35;
_imageInfo.Heads = 1;
_imageInfo.SectorsPerTrack = _dos32 ? 13u : 16u;
return ErrorNumber.NoError;
}
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, false, 1, out buffer, out _);
}
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;
if(negative) return ErrorNumber.NotSupported;
2024-05-01 04:05:22 +01:00
if(sectorAddress > _imageInfo.Sectors - 1) return ErrorNumber.OutOfRange;
2024-05-01 04:05:22 +01:00
if(sectorAddress + length > _imageInfo.Sectors) return ErrorNumber.OutOfRange;
sectorStatus = new SectorStatus[length];
for(uint i = 0; i < length; i++) sectorStatus[i] = SectorStatus.Dumped;
2022-03-06 13:29:38 +00:00
buffer = new byte[length * _imageInfo.SectorSize];
2022-03-06 13:29:38 +00:00
Array.Copy(_deinterleaved, (int)(sectorAddress * _imageInfo.SectorSize), buffer, 0, buffer.Length);
return ErrorNumber.NoError;
}
2023-10-03 23:34:59 +01:00
#endregion
}