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

160 lines
6.4 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-01-03 17:51:30 +00:00
// Copyright © 2011-2020 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;
using Aaru.Helpers;
2020-02-27 00:33:26 +00:00
namespace Aaru.DiscImages
{
public partial class HdCopy
{
public bool Open(IFilter imageFilter)
{
Stream stream = imageFilter.GetDataForkStream();
stream.Seek(0, SeekOrigin.Begin);
2020-02-29 18:03:35 +00:00
byte[] header = new byte[2 + (2 * 82)];
stream.Read(header, 0, 2 + (2 * 82));
HdcpFileHeader fheader = Marshal.ByteArrayToStructureLittleEndian<HdcpFileHeader>(header);
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);
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;
imageInfo.XmlMediaType = XmlMediaType.BlockMedia;
imageInfo.CreationTime = imageFilter.GetCreationTime();
imageInfo.LastModificationTime = imageFilter.GetLastWriteTime();
imageInfo.MediaTitle = Path.GetFileNameWithoutExtension(imageFilter.GetFilename());
2020-02-29 18:03:35 +00:00
imageInfo.MediaType = Geometry.GetMediaType(((ushort)imageInfo.Cylinders, 2,
(ushort)imageInfo.SectorsPerTrack, 512, MediaEncoding.MFM,
false));
// the start offset of the track data
2020-02-29 18:03:35 +00:00
long currentOffset = 2 + (2 * 82);
// build table of track offsets
for(int i = 0; i < imageInfo.Cylinders * 2; i++)
if(fheader.trackMap[i] == 0)
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);
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
fileHeader = fheader;
hdcpImageFilter = imageFilter;
2020-02-29 18:03:35 +00:00
return true;
}
public byte[] ReadSector(ulong sectorAddress)
{
int trackNum = (int)(sectorAddress / imageInfo.SectorsPerTrack);
int sectorOffset = (int)(sectorAddress % imageInfo.SectorsPerTrack);
if(sectorAddress > imageInfo.Sectors - 1)
throw new ArgumentOutOfRangeException(nameof(sectorAddress), "Sector address not found");
if(trackNum > 2 * imageInfo.Cylinders)
throw new ArgumentOutOfRangeException(nameof(sectorAddress), "Sector address not found");
byte[] result = new byte[imageInfo.SectorSize];
2020-02-29 18:03:35 +00: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-02-29 18:03:35 +00:00
if(!trackCache.ContainsKey(trackNum))
ReadTrackIntoCache(hdcpImageFilter.GetDataForkStream(), trackNum);
Array.Copy(trackCache[trackNum], sectorOffset * imageInfo.SectorSize, result, 0, imageInfo.SectorSize);
}
return result;
}
public byte[] ReadSectors(ulong sectorAddress, uint length)
{
byte[] result = new byte[length * imageInfo.SectorSize];
if(sectorAddress + length > imageInfo.Sectors)
throw new ArgumentOutOfRangeException(nameof(length), "Requested more sectors than available");
for(int i = 0; i < length; i++)
ReadSector(sectorAddress + (ulong)i).CopyTo(result, i * imageInfo.SectorSize);
return result;
}
}
}