2018-07-23 23:25:43 +01:00
|
|
|
|
// /***************************************************************************
|
2020-02-27 12:31:25 +00:00
|
|
|
|
// Aaru Data Preservation Suite
|
2018-07-23 23:25:43 +01:00
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
//
|
|
|
|
|
|
// 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/>.
|
|
|
|
|
|
//
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
2020-12-31 23:08:23 +00:00
|
|
|
|
// Copyright © 2011-2021 Natalia Portillo
|
2018-07-23 23:25:43 +01:00
|
|
|
|
// ****************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
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;
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
2020-02-27 00:33:26 +00:00
|
|
|
|
namespace Aaru.DiscImages
|
2018-07-23 23:25:43 +01:00
|
|
|
|
{
|
2020-07-22 13:20:25 +01:00
|
|
|
|
public sealed partial class DriDiskCopy
|
2018-07-23 23:25:43 +01:00
|
|
|
|
{
|
2021-08-17 13:56:05 +01:00
|
|
|
|
/// <inheritdoc />
|
2021-09-16 19:10:39 +01:00
|
|
|
|
public ErrorNumber Open(IFilter imageFilter)
|
2018-07-23 23:25:43 +01:00
|
|
|
|
{
|
|
|
|
|
|
Stream stream = imageFilter.GetDataForkStream();
|
|
|
|
|
|
|
2020-11-11 04:19:18 +00:00
|
|
|
|
if((stream.Length - Marshal.SizeOf<Footer>()) % 512 != 0)
|
2021-09-16 19:10:39 +01:00
|
|
|
|
return ErrorNumber.InvalidArgument;
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
2020-11-11 04:19:18 +00:00
|
|
|
|
byte[] buffer = new byte[Marshal.SizeOf<Footer>()];
|
2018-07-23 23:25:43 +01:00
|
|
|
|
stream.Seek(-buffer.Length, SeekOrigin.End);
|
|
|
|
|
|
stream.Read(buffer, 0, buffer.Length);
|
|
|
|
|
|
|
2020-11-11 04:19:18 +00:00
|
|
|
|
_footer = Marshal.ByteArrayToStructureLittleEndian<Footer>(buffer);
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
2020-07-20 21:11:32 +01:00
|
|
|
|
string sig = StringHandlers.CToString(_footer.signature);
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
2020-02-29 18:03:35 +00:00
|
|
|
|
var regexSignature = new Regex(REGEX_DRI);
|
2018-07-23 23:25:43 +01:00
|
|
|
|
Match matchSignature = regexSignature.Match(sig);
|
|
|
|
|
|
|
2020-02-29 18:03:35 +00:00
|
|
|
|
if(!matchSignature.Success)
|
2021-09-16 19:10:39 +01:00
|
|
|
|
return ErrorNumber.InvalidArgument;
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
2020-07-20 21:11:32 +01:00
|
|
|
|
if(_footer.bpb.sptrack * _footer.bpb.cylinders * _footer.bpb.heads != _footer.bpb.sectors)
|
2021-09-16 19:10:39 +01:00
|
|
|
|
return ErrorNumber.InvalidArgument;
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
2020-11-11 04:19:18 +00:00
|
|
|
|
if((_footer.bpb.sectors * _footer.bpb.bps) + Marshal.SizeOf<Footer>() != stream.Length)
|
2021-09-16 19:10:39 +01:00
|
|
|
|
return ErrorNumber.InvalidArgument;
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
2020-07-20 21:11:32 +01: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;
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
2020-07-20 21:11:32 +01:00
|
|
|
|
_driImageFilter = imageFilter;
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
2020-11-11 04:19:18 +00:00
|
|
|
|
_imageInfo.ImageSize = (ulong)(stream.Length - Marshal.SizeOf<Footer>());
|
2021-09-15 11:25:26 +01:00
|
|
|
|
_imageInfo.CreationTime = imageFilter.CreationTime;
|
|
|
|
|
|
_imageInfo.LastModificationTime = imageFilter.LastWriteTime;
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
2020-02-27 23:48:41 +00:00
|
|
|
|
AaruConsole.DebugWriteLine("DRI DiskCopy plugin", "Image application = {0} version {1}",
|
2020-07-20 21:11:32 +01:00
|
|
|
|
_imageInfo.Application, _imageInfo.ApplicationVersion);
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
|
|
|
|
|
// Correct some incorrect data in images of NEC 2HD disks
|
2020-07-20 21:11:32 +01:00
|
|
|
|
if(_imageInfo.Cylinders == 77 &&
|
|
|
|
|
|
_imageInfo.Heads == 2 &&
|
|
|
|
|
|
_imageInfo.SectorsPerTrack == 16 &&
|
|
|
|
|
|
_imageInfo.SectorSize == 512 &&
|
2020-11-11 04:19:18 +00:00
|
|
|
|
(_footer.bpb._driveCode == DriveCode.md2hd || _footer.bpb._driveCode == DriveCode.mf2hd))
|
2018-07-23 23:25:43 +01:00
|
|
|
|
{
|
2020-07-20 21:11:32 +01:00
|
|
|
|
_imageInfo.SectorsPerTrack = 8;
|
|
|
|
|
|
_imageInfo.SectorSize = 1024;
|
2018-07-23 23:25:43 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-07-20 21:11:32 +01:00
|
|
|
|
_imageInfo.MediaType = Geometry.GetMediaType(((ushort)_imageInfo.Cylinders, (byte)_imageInfo.Heads,
|
|
|
|
|
|
(ushort)_imageInfo.SectorsPerTrack, _imageInfo.SectorSize,
|
|
|
|
|
|
MediaEncoding.MFM, false));
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
2020-07-20 21:11:32 +01:00
|
|
|
|
switch(_imageInfo.MediaType)
|
2018-07-23 23:25:43 +01:00
|
|
|
|
{
|
2020-11-11 04:19:18 +00:00
|
|
|
|
case MediaType.NEC_525_HD when _footer.bpb._driveCode == DriveCode.mf2hd ||
|
|
|
|
|
|
_footer.bpb._driveCode == DriveCode.mf2ed:
|
2020-07-20 21:11:32 +01:00
|
|
|
|
_imageInfo.MediaType = MediaType.NEC_35_HD_8;
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
2018-07-23 23:25:43 +01:00
|
|
|
|
break;
|
2020-11-11 04:19:18 +00:00
|
|
|
|
case MediaType.DOS_525_HD when _footer.bpb._driveCode == DriveCode.mf2hd ||
|
|
|
|
|
|
_footer.bpb._driveCode == DriveCode.mf2ed:
|
2020-07-20 21:11:32 +01:00
|
|
|
|
_imageInfo.MediaType = MediaType.NEC_35_HD_15;
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
2018-07-23 23:25:43 +01:00
|
|
|
|
break;
|
2020-11-11 04:19:18 +00:00
|
|
|
|
case MediaType.RX50 when _footer.bpb._driveCode == DriveCode.md2dd ||
|
|
|
|
|
|
_footer.bpb._driveCode == DriveCode.md2hd:
|
2020-07-20 21:11:32 +01:00
|
|
|
|
_imageInfo.MediaType = MediaType.ATARI_35_SS_DD;
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
2018-07-23 23:25:43 +01:00
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-07-20 21:11:32 +01:00
|
|
|
|
_imageInfo.XmlMediaType = XmlMediaType.BlockMedia;
|
2020-02-29 18:03:35 +00:00
|
|
|
|
|
2020-02-27 23:48:41 +00:00
|
|
|
|
AaruConsole.VerboseWriteLine("Digital Research DiskCopy image contains a disk of type {0}",
|
2020-07-20 21:11:32 +01:00
|
|
|
|
_imageInfo.MediaType);
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
2021-09-16 19:10:39 +01:00
|
|
|
|
return ErrorNumber.NoError;
|
2018-07-23 23:25:43 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-08-17 13:56:05 +01:00
|
|
|
|
/// <inheritdoc />
|
2021-09-19 21:16:47 +01:00
|
|
|
|
public ErrorNumber ReadSector(ulong sectorAddress, out byte[] buffer) =>
|
|
|
|
|
|
ReadSectors(sectorAddress, 1, out buffer);
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
2021-08-17 13:56:05 +01:00
|
|
|
|
/// <inheritdoc />
|
2021-09-19 21:16:47 +01:00
|
|
|
|
public ErrorNumber ReadSectors(ulong sectorAddress, uint length, out byte[] buffer)
|
2018-07-23 23:25:43 +01:00
|
|
|
|
{
|
2021-09-19 21:16:47 +01:00
|
|
|
|
buffer = null;
|
|
|
|
|
|
|
2020-07-20 21:11:32 +01:00
|
|
|
|
if(sectorAddress > _imageInfo.Sectors - 1)
|
2021-09-19 21:16:47 +01:00
|
|
|
|
return ErrorNumber.OutOfRange;
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
2020-07-20 21:11:32 +01:00
|
|
|
|
if(sectorAddress + length > _imageInfo.Sectors)
|
2021-09-19 21:16:47 +01:00
|
|
|
|
return ErrorNumber.OutOfRange;
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
2021-09-19 21:16:47 +01:00
|
|
|
|
buffer = new byte[length * _imageInfo.SectorSize];
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
2020-07-20 21:11:32 +01:00
|
|
|
|
Stream stream = _driImageFilter.GetDataForkStream();
|
|
|
|
|
|
stream.Seek((long)(sectorAddress * _imageInfo.SectorSize), SeekOrigin.Begin);
|
|
|
|
|
|
stream.Read(buffer, 0, (int)(length * _imageInfo.SectorSize));
|
2018-07-23 23:25:43 +01:00
|
|
|
|
|
2021-09-19 21:16:47 +01:00
|
|
|
|
return ErrorNumber.NoError;
|
2018-07-23 23:25:43 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|