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

267 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/>.
//
// ----------------------------------------------------------------------------
2020-12-31 23:08:23 +00:00
// Copyright © 2011-2021 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.Exceptions;
using Aaru.CommonTypes.Interfaces;
using Aaru.Console;
using Aaru.Helpers;
2020-02-27 00:33:26 +00:00
namespace Aaru.DiscImages
{
2020-07-22 13:20:25 +01:00
public sealed partial class Apridisk
{
public bool Open(IFilter imageFilter)
{
Stream stream = imageFilter.GetDataForkStream();
stream.Seek(0, SeekOrigin.Begin);
// Skip signature
2020-07-20 21:11:32 +01:00
stream.Seek(_signature.Length, SeekOrigin.Begin);
int totalCylinders = -1;
int totalHeads = -1;
int maxSector = -1;
int recordSize = Marshal.SizeOf<Record>();
// Count cylinders
while(stream.Position < stream.Length)
{
byte[] recB = new byte[recordSize];
stream.Read(recB, 0, recordSize);
Record record = Marshal.SpanToStructureLittleEndian<Record>(recB);
switch(record.type)
{
// Deleted record, just skip it
case RecordType.Deleted:
2020-02-27 23:48:41 +00:00
AaruConsole.DebugWriteLine("Apridisk plugin", "Found deleted record at {0}", stream.Position);
2020-02-29 18:03:35 +00:00
stream.Seek((record.headerSize - recordSize) + record.dataSize, SeekOrigin.Current);
break;
case RecordType.Comment:
2020-02-27 23:48:41 +00:00
AaruConsole.DebugWriteLine("Apridisk plugin", "Found comment record at {0}", stream.Position);
stream.Seek(record.headerSize - recordSize, SeekOrigin.Current);
byte[] commentB = new byte[record.dataSize];
stream.Read(commentB, 0, commentB.Length);
2020-07-20 21:11:32 +01:00
_imageInfo.Comments = StringHandlers.CToString(commentB);
AaruConsole.DebugWriteLine("Apridisk plugin", "Comment: \"{0}\"", _imageInfo.Comments);
2020-02-29 18:03:35 +00:00
break;
case RecordType.Creator:
2020-02-27 23:48:41 +00:00
AaruConsole.DebugWriteLine("Apridisk plugin", "Found creator record at {0}", stream.Position);
stream.Seek(record.headerSize - recordSize, SeekOrigin.Current);
byte[] creatorB = new byte[record.dataSize];
stream.Read(creatorB, 0, creatorB.Length);
2020-07-20 21:11:32 +01:00
_imageInfo.Creator = StringHandlers.CToString(creatorB);
AaruConsole.DebugWriteLine("Apridisk plugin", "Creator: \"{0}\"", _imageInfo.Creator);
2020-02-29 18:03:35 +00:00
break;
case RecordType.Sector:
if(record.compression != CompressType.Compressed &&
record.compression != CompressType.Uncompresed)
throw new
ImageNotSupportedException($"Found record with unknown compression type 0x{(ushort)record.compression:X4} at {stream.Position}");
2020-02-27 23:48:41 +00:00
AaruConsole.DebugWriteLine("Apridisk plugin",
2020-02-29 18:03:35 +00:00
"Found {4} sector record at {0} for cylinder {1} head {2} sector {3}",
stream.Position, record.cylinder, record.head, record.sector,
record.compression == CompressType.Compressed ? "compressed"
: "uncompressed");
if(record.cylinder > totalCylinders)
totalCylinders = record.cylinder;
if(record.head > totalHeads)
totalHeads = record.head;
if(record.sector > maxSector)
maxSector = record.sector;
2020-02-29 18:03:35 +00:00
stream.Seek((record.headerSize - recordSize) + record.dataSize, SeekOrigin.Current);
break;
default:
throw new
ImageNotSupportedException($"Found record with unknown type 0x{(uint)record.type:X8} at {stream.Position}");
}
}
totalCylinders++;
totalHeads++;
2020-02-29 18:03:35 +00:00
if(totalCylinders <= 0 ||
totalHeads <= 0)
throw new ImageNotSupportedException("No cylinders or heads found");
2020-07-20 21:11:32 +01:00
_sectorsData = new byte[totalCylinders][][][];
2020-02-29 18:03:35 +00:00
// Total sectors per track
uint[][] spts = new uint[totalCylinders][];
2020-07-20 21:11:32 +01:00
_imageInfo.Cylinders = (ushort)totalCylinders;
_imageInfo.Heads = (byte)totalHeads;
2020-02-27 23:48:41 +00:00
AaruConsole.DebugWriteLine("Apridisk plugin",
2020-02-29 18:03:35 +00:00
"Found {0} cylinders and {1} heads with a maximum sector number of {2}",
totalCylinders, totalHeads, maxSector);
// Create heads
for(int i = 0; i < totalCylinders; i++)
{
2020-07-20 21:11:32 +01:00
_sectorsData[i] = new byte[totalHeads][][];
spts[i] = new uint[totalHeads];
2020-02-29 18:03:35 +00:00
for(int j = 0; j < totalHeads; j++)
2020-07-20 21:11:32 +01:00
_sectorsData[i][j] = new byte[maxSector + 1][];
}
2020-07-20 21:11:32 +01:00
_imageInfo.SectorSize = uint.MaxValue;
ulong headerSizes = 0;
// Read sectors
2020-07-20 21:11:32 +01:00
stream.Seek(_signature.Length, SeekOrigin.Begin);
2020-02-29 18:03:35 +00:00
while(stream.Position < stream.Length)
{
byte[] recB = new byte[recordSize];
stream.Read(recB, 0, recordSize);
Record record = Marshal.SpanToStructureLittleEndian<Record>(recB);
switch(record.type)
{
// Not sector record, just skip it
case RecordType.Deleted:
case RecordType.Comment:
case RecordType.Creator:
2020-02-29 18:03:35 +00:00
stream.Seek((record.headerSize - recordSize) + record.dataSize, SeekOrigin.Current);
headerSizes += record.headerSize + record.dataSize;
2020-02-29 18:03:35 +00:00
break;
case RecordType.Sector:
stream.Seek(record.headerSize - recordSize, SeekOrigin.Current);
byte[] data = new byte[record.dataSize];
stream.Read(data, 0, data.Length);
spts[record.cylinder][record.head]++;
uint realLength = record.dataSize;
if(record.compression == CompressType.Compressed)
2020-07-20 21:11:32 +01:00
realLength =
Decompress(data, out _sectorsData[record.cylinder][record.head][record.sector]);
2020-02-29 18:03:35 +00:00
else
2020-07-20 21:11:32 +01:00
_sectorsData[record.cylinder][record.head][record.sector] = data;
2020-07-20 21:11:32 +01:00
if(realLength < _imageInfo.SectorSize)
_imageInfo.SectorSize = realLength;
headerSizes += record.headerSize + record.dataSize;
break;
}
}
2020-02-27 23:48:41 +00:00
AaruConsole.DebugWriteLine("Apridisk plugin", "Found a minimum of {0} bytes per sector",
2020-07-20 21:11:32 +01:00
_imageInfo.SectorSize);
// Count sectors per track
uint spt = uint.MaxValue;
2020-02-29 18:03:35 +00:00
2020-07-20 21:11:32 +01:00
for(ushort cyl = 0; cyl < _imageInfo.Cylinders; cyl++)
{
2020-07-20 21:11:32 +01:00
for(ushort head = 0; head < _imageInfo.Heads; head++)
if(spts[cyl][head] < spt)
spt = spts[cyl][head];
}
2020-07-20 21:11:32 +01:00
_imageInfo.SectorsPerTrack = spt;
2020-02-27 23:48:41 +00:00
AaruConsole.DebugWriteLine("Apridisk plugin", "Found a minimum of {0} sectors per track",
2020-07-20 21:11:32 +01:00
_imageInfo.SectorsPerTrack);
2020-07-20 21:11:32 +01:00
_imageInfo.MediaType = Geometry.GetMediaType(((ushort)_imageInfo.Cylinders, (byte)_imageInfo.Heads,
(ushort)_imageInfo.SectorsPerTrack, 512, MediaEncoding.MFM,
false));
_imageInfo.ImageSize = (ulong)stream.Length - headerSizes;
2020-07-20 21:11:32 +01:00
_imageInfo.CreationTime = imageFilter.GetCreationTime();
_imageInfo.LastModificationTime = imageFilter.GetLastWriteTime();
_imageInfo.MediaTitle = Path.GetFileNameWithoutExtension(imageFilter.GetFilename());
_imageInfo.Sectors = _imageInfo.Cylinders * _imageInfo.Heads * _imageInfo.SectorsPerTrack;
_imageInfo.XmlMediaType = XmlMediaType.BlockMedia;
return true;
}
public byte[] ReadSector(ulong sectorAddress)
{
(ushort cylinder, byte head, byte sector) = LbaToChs(sectorAddress);
2020-07-20 21:11:32 +01:00
if(cylinder >= _sectorsData.Length)
throw new ArgumentOutOfRangeException(nameof(sectorAddress), "Sector address not found");
2020-07-20 21:11:32 +01:00
if(head >= _sectorsData[cylinder].Length)
throw new ArgumentOutOfRangeException(nameof(sectorAddress), "Sector address not found");
2020-07-20 21:11:32 +01:00
if(sector > _sectorsData[cylinder][head].Length)
throw new ArgumentOutOfRangeException(nameof(sectorAddress), "Sector address not found");
2020-07-20 21:11:32 +01:00
return _sectorsData[cylinder][head][sector];
}
public byte[] ReadSectors(ulong sectorAddress, uint length)
{
2020-07-20 21:11:32 +01:00
if(sectorAddress > _imageInfo.Sectors - 1)
throw new ArgumentOutOfRangeException(nameof(sectorAddress), "Sector address not found");
2020-07-20 21:11:32 +01:00
if(sectorAddress + length > _imageInfo.Sectors)
throw new ArgumentOutOfRangeException(nameof(length), "Requested more sectors than available");
2020-02-29 18:03:35 +00:00
var buffer = new MemoryStream();
for(uint i = 0; i < length; i++)
{
byte[] sector = ReadSector(sectorAddress + i);
buffer.Write(sector, 0, sector.Length);
}
return buffer.ToArray();
}
}
}