Files
Aaru/Aaru.Images/Anex86/Read.cs

111 lines
4.2 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 Anex86 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/>.
//
// ----------------------------------------------------------------------------
2022-12-03 16:07:10 +00:00
// Copyright © 2011-2023 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.Console;
using Aaru.Helpers;
namespace Aaru.Images;
2022-03-06 13:29:38 +00:00
public sealed partial class Anex86
{
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);
2022-03-06 13:29:38 +00:00
if(stream.Length < Marshal.SizeOf<Header>())
return ErrorNumber.InvalidArgument;
2023-10-03 23:34:59 +01:00
var hdrB = new byte[Marshal.SizeOf<Header>()];
stream.EnsureRead(hdrB, 0, hdrB.Length);
2022-03-06 13:29:38 +00:00
_header = Marshal.SpanToStructureLittleEndian<Header>(hdrB);
2022-03-06 13:29:38 +00:00
_imageInfo.MediaType = Geometry.GetMediaType(((ushort)_header.cylinders, (byte)_header.heads,
(ushort)_header.spt, (uint)_header.bps, MediaEncoding.MFM,
false));
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
if(_imageInfo.MediaType == MediaType.Unknown)
_imageInfo.MediaType = MediaType.GENERIC_HDD;
AaruConsole.DebugWriteLine(MODULE_NAME, Localization.MediaType_0, _imageInfo.MediaType);
2022-03-06 13:29:38 +00:00
_imageInfo.ImageSize = (ulong)_header.dskSize;
_imageInfo.CreationTime = imageFilter.CreationTime;
_imageInfo.LastModificationTime = imageFilter.LastWriteTime;
_imageInfo.MediaTitle = Path.GetFileNameWithoutExtension(imageFilter.Filename);
_imageInfo.Sectors = (ulong)(_header.cylinders * _header.heads * _header.spt);
_imageInfo.MetadataMediaType = MetadataMediaType.BlockMedia;
2022-03-06 13:29:38 +00:00
_imageInfo.SectorSize = (uint)_header.bps;
_imageInfo.Cylinders = (uint)_header.cylinders;
_imageInfo.Heads = (uint)_header.heads;
_imageInfo.SectorsPerTrack = (uint)_header.spt;
2022-03-06 13:29:38 +00:00
_anexImageFilter = imageFilter;
2022-03-06 13:29:38 +00:00
return ErrorNumber.NoError;
}
2022-03-06 13:29:38 +00:00
/// <inheritdoc />
2022-03-07 07:36:44 +00:00
public ErrorNumber ReadSector(ulong sectorAddress, out byte[] buffer) => ReadSectors(sectorAddress, 1, out buffer);
2022-03-06 13:29:38 +00:00
/// <inheritdoc />
public ErrorNumber ReadSectors(ulong sectorAddress, uint length, out byte[] buffer)
{
buffer = null;
2022-03-06 13:29:38 +00:00
if(sectorAddress > _imageInfo.Sectors - 1)
return ErrorNumber.OutOfRange;
2022-03-06 13:29:38 +00:00
if(sectorAddress + length > _imageInfo.Sectors)
return ErrorNumber.OutOfRange;
2022-03-06 13:29:38 +00:00
buffer = new byte[length * _imageInfo.SectorSize];
2022-03-06 13:29:38 +00:00
Stream stream = _anexImageFilter.GetDataForkStream();
2023-10-03 23:34:59 +01:00
stream.Seek((long)((ulong)_header.hdrSize + sectorAddress * _imageInfo.SectorSize), SeekOrigin.Begin);
stream.EnsureRead(buffer, 0, (int)(length * _imageInfo.SectorSize));
2022-03-06 13:29:38 +00:00
return ErrorNumber.NoError;
}
2023-10-03 23:34:59 +01:00
#endregion
}