Files
Aaru/Aaru.Images/HDCopy/Read.cs

164 lines
6.5 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 HD-Copy 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/>.
//
// ----------------------------------------------------------------------------
// Copyright © 2017 Michael Drüing
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.Interfaces;
using Aaru.Console;
2020-02-27 00:33:26 +00:00
namespace Aaru.DiscImages
{
2020-07-22 13:20:25 +01:00
public sealed partial class HdCopy
{
2021-08-17 13:56:05 +01:00
/// <inheritdoc />
public bool Open(IFilter imageFilter)
{
Stream stream = imageFilter.GetDataForkStream();
2021-08-17 21:23:10 +01:00
var fheader = new FileHeader();
// the start offset of the track data
long currentOffset = 0;
2021-08-17 21:23:10 +01:00
if(!TryReadHeader(stream, ref fheader, ref currentOffset))
{
return false;
}
2020-02-29 18:03:35 +00:00
2020-02-27 23:48:41 +00:00
AaruConsole.DebugWriteLine("HDCP plugin",
2020-02-29 18:03:35 +00:00
"Detected HD-Copy image with {0} tracks and {1} sectors per track.",
fheader.lastCylinder + 1, fheader.sectorsPerTrack);
2020-07-20 21:11:32 +01:00
_imageInfo.Cylinders = (uint)fheader.lastCylinder + 1;
_imageInfo.SectorsPerTrack = fheader.sectorsPerTrack;
_imageInfo.SectorSize = 512; // only 512 bytes per sector supported
_imageInfo.Heads = 2; // only 2-sided floppies are supported
_imageInfo.Sectors = 2 * _imageInfo.Cylinders * _imageInfo.SectorsPerTrack;
_imageInfo.ImageSize = _imageInfo.Sectors * _imageInfo.SectorSize;
2020-07-20 21:11:32 +01:00
_imageInfo.XmlMediaType = XmlMediaType.BlockMedia;
_imageInfo.CreationTime = imageFilter.CreationTime;
_imageInfo.LastModificationTime = imageFilter.LastWriteTime;
_imageInfo.MediaTitle = Path.GetFileNameWithoutExtension(imageFilter.Filename);
2020-02-29 18:03:35 +00:00
2020-07-20 21:11:32 +01:00
_imageInfo.MediaType = Geometry.GetMediaType(((ushort)_imageInfo.Cylinders, 2,
(ushort)_imageInfo.SectorsPerTrack, 512, MediaEncoding.MFM,
false));
// build table of track offsets
2020-07-20 21:11:32 +01:00
for(int i = 0; i < _imageInfo.Cylinders * 2; i++)
if(fheader.trackMap[i] == 0)
2020-07-20 21:11:32 +01:00
_trackOffset[i] = -1;
else
{
// track is present, read the block header
2020-02-29 18:03:35 +00:00
if(currentOffset + 3 >= stream.Length)
return false;
byte[] blkHeader = new byte[2];
stream.Read(blkHeader, 0, 2);
short blkLength = BitConverter.ToInt16(blkHeader, 0);
// assume block sizes are positive
2020-02-29 18:03:35 +00:00
if(blkLength < 0)
return false;
AaruConsole.DebugWriteLine("HDCP plugin", "Track {0} offset 0x{1:x8}, size={2:x4}", i,
currentOffset, blkLength);
2020-07-20 21:11:32 +01:00
_trackOffset[i] = currentOffset;
currentOffset += 2 + blkLength;
2020-02-29 18:03:35 +00:00
// skip the block data
stream.Seek(blkLength, SeekOrigin.Current);
}
// ensure that the last track is present completely
2020-02-29 18:03:35 +00:00
if(currentOffset > stream.Length)
return false;
// save some variables for later use
2020-07-20 21:11:32 +01:00
_fileHeader = fheader;
_hdcpImageFilter = imageFilter;
2020-02-29 18:03:35 +00:00
return true;
}
2021-08-17 13:56:05 +01:00
/// <inheritdoc />
public byte[] ReadSector(ulong sectorAddress)
{
2020-07-20 21:11:32 +01:00
int trackNum = (int)(sectorAddress / _imageInfo.SectorsPerTrack);
int sectorOffset = (int)(sectorAddress % _imageInfo.SectorsPerTrack);
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(trackNum > 2 * _imageInfo.Cylinders)
throw new ArgumentOutOfRangeException(nameof(sectorAddress), "Sector address not found");
2020-07-20 21:11:32 +01:00
byte[] result = new byte[_imageInfo.SectorSize];
2020-02-29 18:03:35 +00:00
2020-07-20 21:11:32 +01:00
if(_trackOffset[trackNum] == -1)
Array.Clear(result, 0, (int)_imageInfo.SectorSize);
else
{
// track is present in file, make sure it has been loaded
2020-07-20 21:11:32 +01:00
if(!_trackCache.ContainsKey(trackNum))
ReadTrackIntoCache(_hdcpImageFilter.GetDataForkStream(), trackNum);
2020-07-20 21:11:32 +01:00
Array.Copy(_trackCache[trackNum], sectorOffset * _imageInfo.SectorSize, result, 0,
_imageInfo.SectorSize);
}
return result;
}
2021-08-17 13:56:05 +01:00
/// <inheritdoc />
public byte[] ReadSectors(ulong sectorAddress, uint length)
{
2020-07-20 21:11:32 +01:00
byte[] result = new byte[length * _imageInfo.SectorSize];
2020-07-20 21:11:32 +01:00
if(sectorAddress + length > _imageInfo.Sectors)
throw new ArgumentOutOfRangeException(nameof(length), "Requested more sectors than available");
for(int i = 0; i < length; i++)
2020-07-20 21:11:32 +01:00
ReadSector(sectorAddress + (ulong)i).CopyTo(result, i * _imageInfo.SectorSize);
return result;
}
}
}