Files
Aaru/Aaru.Images/Apridisk/Read.cs

278 lines
11 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 Apridisk 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.IO;
2020-02-27 00:33:26 +00:00
using Aaru.CommonTypes;
using Aaru.CommonTypes.Enums;
using Aaru.CommonTypes.Interfaces;
using Aaru.Helpers;
using Aaru.Logging;
namespace Aaru.Images;
2022-03-06 13:29:38 +00:00
public sealed partial class Apridisk
{
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);
// Skip signature
stream.Seek(_signature.Length, SeekOrigin.Begin);
int totalCylinders = -1;
int totalHeads = -1;
int maxSector = -1;
int recordSize = Marshal.SizeOf<Record>();
2022-03-06 13:29:38 +00:00
// Count cylinders
while(stream.Position < stream.Length)
{
byte[] recB = new byte[recordSize];
stream.EnsureRead(recB, 0, recordSize);
2022-03-06 13:29:38 +00:00
Record record = Marshal.SpanToStructureLittleEndian<Record>(recB);
2022-03-06 13:29:38 +00:00
switch(record.type)
{
2022-03-06 13:29:38 +00:00
// Deleted record, just skip it
case RecordType.Deleted:
2025-08-17 06:11:22 +01:00
AaruLogging.Debug(MODULE_NAME, Localization.Found_deleted_record_at_0, stream.Position);
2022-03-06 13:29:38 +00:00
stream.Seek(record.headerSize - recordSize + record.dataSize, SeekOrigin.Current);
break;
case RecordType.Comment:
2025-08-17 06:11:22 +01:00
AaruLogging.Debug(MODULE_NAME, Localization.Found_comment_record_at_0, stream.Position);
2022-03-06 13:29:38 +00:00
stream.Seek(record.headerSize - recordSize, SeekOrigin.Current);
byte[] commentB = new byte[record.dataSize];
stream.EnsureRead(commentB, 0, commentB.Length);
2022-03-06 13:29:38 +00:00
_imageInfo.Comments = StringHandlers.CToString(commentB);
2025-08-17 06:11:22 +01:00
AaruLogging.Debug(MODULE_NAME, Localization.Comment_0, _imageInfo.Comments);
2022-03-06 13:29:38 +00:00
break;
case RecordType.Creator:
2025-08-17 06:11:22 +01:00
AaruLogging.Debug(MODULE_NAME, Localization.Found_creator_record_at_0, stream.Position);
2022-03-06 13:29:38 +00:00
stream.Seek(record.headerSize - recordSize, SeekOrigin.Current);
byte[] creatorB = new byte[record.dataSize];
stream.EnsureRead(creatorB, 0, creatorB.Length);
2022-03-06 13:29:38 +00:00
_imageInfo.Creator = StringHandlers.CToString(creatorB);
2025-08-17 06:11:22 +01:00
AaruLogging.Debug(MODULE_NAME, Localization.Creator_0, _imageInfo.Creator);
2022-03-06 13:29:38 +00:00
break;
case RecordType.Sector:
if(record.compression != CompressType.Compressed && record.compression != CompressType.Uncompresed)
2022-03-06 13:29:38 +00:00
return ErrorNumber.NotSupported;
2025-08-17 06:11:22 +01:00
AaruLogging.Debug(MODULE_NAME,
record.compression == CompressType.Compressed
2024-05-01 04:05:22 +01:00
? Localization
.Found_compressed_sector_record_at_0_for_cylinder_1_head_2_sector_3
: Localization
.Found_uncompressed_sector_record_at_0_for_cylinder_1_head_2_sector_3,
stream.Position,
record.cylinder,
record.head,
record.sector);
2022-03-06 13:29:38 +00:00
2024-05-01 04:05:22 +01:00
if(record.cylinder > totalCylinders) totalCylinders = record.cylinder;
2022-03-06 13:29:38 +00:00
2024-05-01 04:05:22 +01:00
if(record.head > totalHeads) totalHeads = record.head;
2022-03-06 13:29:38 +00:00
2024-05-01 04:05:22 +01:00
if(record.sector > maxSector) maxSector = record.sector;
2022-03-06 13:29:38 +00:00
stream.Seek(record.headerSize - recordSize + record.dataSize, SeekOrigin.Current);
break;
2023-10-03 23:34:59 +01:00
default:
return ErrorNumber.NotSupported;
}
2022-03-06 13:29:38 +00:00
}
2022-03-06 13:29:38 +00:00
totalCylinders++;
totalHeads++;
2024-05-01 04:05:22 +01:00
if(totalCylinders <= 0 || totalHeads <= 0) return ErrorNumber.NotSupported;
2022-03-06 13:29:38 +00:00
_sectorsData = new byte[totalCylinders][][][];
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
// Total sectors per track
uint[][] spts = new uint[totalCylinders][];
2022-03-06 13:29:38 +00:00
_imageInfo.Cylinders = (ushort)totalCylinders;
_imageInfo.Heads = (byte)totalHeads;
2025-08-17 06:11:22 +01:00
AaruLogging.Debug(MODULE_NAME,
Localization.Found_0_cylinders_and_1_heads_with_a_maximum_sector_number_of_2,
2024-05-01 04:05:22 +01:00
totalCylinders,
totalHeads,
maxSector);
2022-03-06 13:29:38 +00:00
// Create heads
for(int i = 0; i < totalCylinders; i++)
2022-03-06 13:29:38 +00:00
{
_sectorsData[i] = new byte[totalHeads][][];
spts[i] = new uint[totalHeads];
for(int j = 0; j < totalHeads; j++) _sectorsData[i][j] = new byte[maxSector + 1][];
2022-03-06 13:29:38 +00:00
}
2022-03-06 13:29:38 +00:00
_imageInfo.SectorSize = uint.MaxValue;
2022-03-06 13:29:38 +00:00
ulong headerSizes = 0;
2022-03-06 13:29:38 +00:00
// Read sectors
stream.Seek(_signature.Length, SeekOrigin.Begin);
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
while(stream.Position < stream.Length)
{
byte[] recB = new byte[recordSize];
stream.EnsureRead(recB, 0, recordSize);
2022-03-06 13:29:38 +00:00
Record record = Marshal.SpanToStructureLittleEndian<Record>(recB);
2022-03-06 13:29:38 +00:00
switch(record.type)
{
// Not sector record, just skip it
case RecordType.Deleted:
case RecordType.Comment:
case RecordType.Creator:
stream.Seek(record.headerSize - recordSize + record.dataSize, SeekOrigin.Current);
headerSizes += record.headerSize + record.dataSize;
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
break;
case RecordType.Sector:
stream.Seek(record.headerSize - recordSize, SeekOrigin.Current);
byte[] data = new byte[record.dataSize];
stream.EnsureRead(data, 0, data.Length);
2022-03-06 13:29:38 +00:00
spts[record.cylinder][record.head]++;
uint realLength = record.dataSize;
2022-03-06 13:29:38 +00:00
if(record.compression == CompressType.Compressed)
2022-03-07 07:36:44 +00:00
realLength = Decompress(data, out _sectorsData[record.cylinder][record.head][record.sector]);
2022-03-06 13:29:38 +00:00
else
_sectorsData[record.cylinder][record.head][record.sector] = data;
2024-05-01 04:05:22 +01:00
if(realLength < _imageInfo.SectorSize) _imageInfo.SectorSize = realLength;
2022-03-06 13:29:38 +00:00
headerSizes += record.headerSize + record.dataSize;
2022-03-06 13:29:38 +00:00
break;
}
2022-03-06 13:29:38 +00:00
}
2025-08-17 06:11:22 +01:00
AaruLogging.Debug(MODULE_NAME,
2024-05-01 04:05:22 +01:00
Localization.Found_a_minimum_of_0_bytes_per_sector,
_imageInfo.SectorSize);
2022-03-06 13:29:38 +00:00
// Count sectors per track
uint spt = uint.MaxValue;
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
for(ushort cyl = 0; cyl < _imageInfo.Cylinders; cyl++)
{
for(ushort head = 0; head < _imageInfo.Heads; head++)
{
if(spts[cyl][head] < spt) spt = spts[cyl][head];
}
2022-03-06 13:29:38 +00:00
}
2022-03-06 13:29:38 +00:00
_imageInfo.SectorsPerTrack = spt;
2025-08-17 06:11:22 +01:00
AaruLogging.Debug(MODULE_NAME,
2024-05-01 04:05:22 +01:00
Localization.Found_a_minimum_of_0_sectors_per_track,
2022-03-06 13:29:38 +00:00
_imageInfo.SectorsPerTrack);
2022-03-06 13:29:38 +00:00
_imageInfo.MediaType = Geometry.GetMediaType(((ushort)_imageInfo.Cylinders, (byte)_imageInfo.Heads,
(ushort)_imageInfo.SectorsPerTrack, 512, MediaEncoding.MFM,
false));
2022-03-06 13:29:38 +00:00
_imageInfo.ImageSize = (ulong)stream.Length - headerSizes;
_imageInfo.CreationTime = imageFilter.CreationTime;
_imageInfo.LastModificationTime = imageFilter.LastWriteTime;
_imageInfo.MediaTitle = Path.GetFileNameWithoutExtension(imageFilter.Filename);
_imageInfo.Sectors = _imageInfo.Cylinders * _imageInfo.Heads * _imageInfo.SectorsPerTrack;
_imageInfo.MetadataMediaType = MetadataMediaType.BlockMedia;
2022-03-06 13:29:38 +00:00
return ErrorNumber.NoError;
}
2022-03-06 13:29:38 +00:00
/// <inheritdoc />
public ErrorNumber ReadSector(ulong sectorAddress, out byte[] buffer)
{
buffer = null;
(ushort cylinder, byte head, byte sector) = LbaToChs(sectorAddress);
2024-05-01 04:05:22 +01:00
if(cylinder >= _sectorsData.Length) return ErrorNumber.SectorNotFound;
2024-05-01 04:05:22 +01:00
if(head >= _sectorsData[cylinder].Length) return ErrorNumber.SectorNotFound;
2024-05-01 04:05:22 +01:00
if(sector > _sectorsData[cylinder][head].Length) return ErrorNumber.SectorNotFound;
2022-03-06 13:29:38 +00:00
buffer = _sectorsData[cylinder][head][sector];
2022-03-06 13:29:38 +00:00
return ErrorNumber.NoError;
}
2022-03-06 13:29:38 +00:00
/// <inheritdoc />
public ErrorNumber ReadSectors(ulong sectorAddress, uint length, out byte[] buffer)
{
buffer = null;
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;
2022-03-06 13:29:38 +00:00
var ms = new MemoryStream();
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
for(uint i = 0; i < length; i++)
{
ErrorNumber errno = ReadSector(sectorAddress + i, out byte[] sector);
2024-05-01 04:05:22 +01:00
if(errno != ErrorNumber.NoError) return errno;
2022-03-06 13:29:38 +00:00
ms.Write(sector, 0, sector.Length);
}
2022-03-06 13:29:38 +00:00
buffer = ms.ToArray();
2022-03-06 13:29:38 +00:00
return ErrorNumber.NoError;
}
2023-10-03 23:34:59 +01:00
#endregion
}