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

182 lines
6.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 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 ErrorNumber 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 ErrorNumber.InvalidArgument;
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 ErrorNumber.InvalidArgument;
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 ErrorNumber.InvalidArgument;
2020-02-29 18:03:35 +00:00
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 ErrorNumber.InvalidArgument;
// 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 ErrorNumber.NoError;
}
2021-08-17 13:56:05 +01:00
/// <inheritdoc />
public ErrorNumber ReadSector(ulong sectorAddress, out byte[] buffer)
{
buffer = null;
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)
return ErrorNumber.OutOfRange;
2020-07-20 21:11:32 +01:00
if(trackNum > 2 * _imageInfo.Cylinders)
return ErrorNumber.SectorNotFound;
buffer = 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(buffer, 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))
2021-09-21 04:55:28 +01:00
{
ErrorNumber errno = ReadTrackIntoCache(_hdcpImageFilter.GetDataForkStream(), trackNum);
if(errno != ErrorNumber.NoError)
return errno;
}
Array.Copy(_trackCache[trackNum], sectorOffset * _imageInfo.SectorSize, buffer, 0,
2020-07-20 21:11:32 +01:00
_imageInfo.SectorSize);
}
return ErrorNumber.NoError;
}
2021-08-17 13:56:05 +01:00
/// <inheritdoc />
public ErrorNumber ReadSectors(ulong sectorAddress, uint length, out byte[] buffer)
{
buffer = null;
if(sectorAddress > _imageInfo.Sectors - 1)
return ErrorNumber.OutOfRange;
2020-07-20 21:11:32 +01:00
if(sectorAddress + length > _imageInfo.Sectors)
return ErrorNumber.OutOfRange;
var ms = new MemoryStream();
for(uint i = 0; i < length; i++)
{
ErrorNumber errno = ReadSector(sectorAddress + i, out byte[] sector);
if(errno != ErrorNumber.NoError)
return errno;
ms.Write(sector, 0, sector.Length);
}
buffer = ms.ToArray();
return ErrorNumber.NoError;
}
}
}