Files
Aaru/Aaru.Images/DriDiskCopy/Read.cs

149 lines
5.9 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 Digital Research's DISKCOPY 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-05-01 04:17:32 +01:00
// Copyright © 2011-2024 Natalia Portillo
// ****************************************************************************/
using System.IO;
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.Console;
using Aaru.Helpers;
namespace Aaru.Images;
2022-03-06 13:29:38 +00:00
public sealed partial class DriDiskCopy
{
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();
2024-05-01 04:05:22 +01:00
if((stream.Length - Marshal.SizeOf<Footer>()) % 512 != 0) return ErrorNumber.InvalidArgument;
2023-10-03 23:34:59 +01:00
var buffer = new byte[Marshal.SizeOf<Footer>()];
2022-03-06 13:29:38 +00:00
stream.Seek(-buffer.Length, SeekOrigin.End);
stream.EnsureRead(buffer, 0, buffer.Length);
2022-03-06 13:29:38 +00:00
_footer = Marshal.ByteArrayToStructureLittleEndian<Footer>(buffer);
2022-03-06 13:29:38 +00:00
string sig = StringHandlers.CToString(_footer.signature);
2022-03-06 13:29:38 +00:00
var regexSignature = new Regex(REGEX_DRI);
Match matchSignature = regexSignature.Match(sig);
2024-05-01 04:05:22 +01:00
if(!matchSignature.Success) return ErrorNumber.InvalidArgument;
2022-03-06 13:29:38 +00:00
if(_footer.bpb.sptrack * _footer.bpb.cylinders * _footer.bpb.heads != _footer.bpb.sectors)
return ErrorNumber.InvalidArgument;
2023-10-03 23:34:59 +01:00
if(_footer.bpb.sectors * _footer.bpb.bps + Marshal.SizeOf<Footer>() != stream.Length)
2022-03-06 13:29:38 +00:00
return ErrorNumber.InvalidArgument;
2022-03-06 13:29:38 +00:00
_imageInfo.Cylinders = _footer.bpb.cylinders;
_imageInfo.Heads = _footer.bpb.heads;
_imageInfo.SectorsPerTrack = _footer.bpb.sptrack;
_imageInfo.Sectors = _footer.bpb.sectors;
_imageInfo.SectorSize = _footer.bpb.bps;
_imageInfo.ApplicationVersion = matchSignature.Groups["version"].Value;
2022-03-06 13:29:38 +00:00
_driImageFilter = imageFilter;
2022-03-06 13:29:38 +00:00
_imageInfo.ImageSize = (ulong)(stream.Length - Marshal.SizeOf<Footer>());
_imageInfo.CreationTime = imageFilter.CreationTime;
_imageInfo.LastModificationTime = imageFilter.LastWriteTime;
2024-05-01 04:05:22 +01:00
AaruConsole.DebugWriteLine(MODULE_NAME,
Localization.Image_application_0_version_1,
_imageInfo.Application,
_imageInfo.ApplicationVersion);
2022-03-06 13:29:38 +00:00
// Correct some incorrect data in images of NEC 2HD disks
if(_imageInfo is { Cylinders: 77, Heads: 2 } and { SectorsPerTrack: 16, SectorSize: 512 } &&
2022-03-16 11:47:00 +00:00
_footer.bpb._driveCode is DriveCode.md2hd or DriveCode.mf2hd)
2022-03-06 13:29:38 +00:00
{
_imageInfo.SectorsPerTrack = 8;
_imageInfo.SectorSize = 1024;
}
2022-03-06 13:29:38 +00:00
_imageInfo.MediaType = Geometry.GetMediaType(((ushort)_imageInfo.Cylinders, (byte)_imageInfo.Heads,
(ushort)_imageInfo.SectorsPerTrack, _imageInfo.SectorSize,
MediaEncoding.MFM, false));
2022-03-06 13:29:38 +00:00
switch(_imageInfo.MediaType)
{
2022-03-16 11:47:00 +00:00
case MediaType.NEC_525_HD when _footer.bpb._driveCode is DriveCode.mf2hd or DriveCode.mf2ed:
2022-03-06 13:29:38 +00:00
_imageInfo.MediaType = MediaType.NEC_35_HD_8;
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
break;
2022-03-16 11:47:00 +00:00
case MediaType.DOS_525_HD when _footer.bpb._driveCode is DriveCode.mf2hd or DriveCode.mf2ed:
2022-03-06 13:29:38 +00:00
_imageInfo.MediaType = MediaType.NEC_35_HD_15;
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
break;
2022-03-16 11:47:00 +00:00
case MediaType.RX50 when _footer.bpb._driveCode is DriveCode.md2dd or DriveCode.md2hd:
2022-03-06 13:29:38 +00:00
_imageInfo.MediaType = MediaType.ATARI_35_SS_DD;
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
break;
}
_imageInfo.MetadataMediaType = MetadataMediaType.BlockMedia;
2020-02-29 18:03:35 +00:00
AaruConsole.VerboseWriteLine(Localization.Digital_Research_DiskCopy_image_contains_a_disk_of_type_0,
2022-03-06 13:29:38 +00:00
_imageInfo.MediaType);
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;
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
buffer = new byte[length * _imageInfo.SectorSize];
2022-03-06 13:29:38 +00:00
Stream stream = _driImageFilter.GetDataForkStream();
stream.Seek((long)(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
}