Files
Aaru/Aaru.Images/DiskDupe/Read.cs

141 lines
5.4 KiB
C#
Raw Normal View History

2021-03-07 18:09:30 +01:00
// /***************************************************************************
// Aaru Data Preservation Suite
// ----------------------------------------------------------------------------
//
// Filename : Read.cs
// Author(s) : Michael Drüing <michael@drueing.de>
//
// Component : Disk image plugins.
//
// --[ Description ] ----------------------------------------------------------
//
// Reads DiskDupe DDI 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 © 2021 Michael Drüing
// Copyright © 2011-2021 Natalia Portillo
// ****************************************************************************/
using System;
using System.IO;
using Aaru.CommonTypes;
using Aaru.CommonTypes.Enums;
using Aaru.CommonTypes.Interfaces;
using Aaru.Console;
namespace Aaru.DiscImages
{
public sealed partial class DiskDupe
{
public ErrorNumber Open(IFilter imageFilter)
2021-03-07 18:09:30 +01:00
{
Stream stream = imageFilter.GetDataForkStream();
2021-08-17 21:23:10 +01:00
var fHeader = new FileHeader();
TrackInfo[] trackMap = null;
long[] trackOffsets = null;
2021-03-07 18:09:30 +01:00
2021-08-17 21:23:10 +01:00
if(!TryReadHeader(stream, ref fHeader, ref trackMap, ref trackOffsets))
return ErrorNumber.InvalidArgument;
2021-03-07 18:09:30 +01:00
AaruConsole.DebugWriteLine("DiskDupe Plugin",
"Detected DiskDupe DDI image with {0} tracks and {1} sectors per track.",
2021-08-17 16:27:42 +01:00
_diskTypes[fHeader.diskType].cyl, _diskTypes[fHeader.diskType].spt);
2021-03-07 18:09:30 +01:00
2021-08-17 16:27:42 +01:00
_imageInfo.Cylinders = _diskTypes[fHeader.diskType].cyl;
_imageInfo.Heads = _diskTypes[fHeader.diskType].hd;
_imageInfo.SectorsPerTrack = _diskTypes[fHeader.diskType].spt;
2021-03-07 18:09:30 +01:00
_imageInfo.SectorSize = 512; // only 512 bytes per sector supported
2021-08-17 21:23:10 +01:00
_imageInfo.Sectors = _imageInfo.Heads * _imageInfo.Cylinders * _imageInfo.SectorsPerTrack;
2021-03-07 18:09:30 +01:00
_imageInfo.ImageSize = _imageInfo.Sectors * _imageInfo.SectorSize;
_imageInfo.XmlMediaType = XmlMediaType.BlockMedia;
_imageInfo.CreationTime = imageFilter.CreationTime;
_imageInfo.LastModificationTime = imageFilter.LastWriteTime;
_imageInfo.MediaTitle = Path.GetFileNameWithoutExtension(imageFilter.Filename);
2021-03-07 18:09:30 +01:00
_imageInfo.MediaType = Geometry.GetMediaType(((ushort)_imageInfo.Cylinders, (byte)_imageInfo.Heads,
(ushort)_imageInfo.SectorsPerTrack, 512, MediaEncoding.MFM,
false));
// save some variables for later use
_fileHeader = fHeader;
_ddiImageFilter = imageFilter;
_trackMap = trackMap;
_trackOffsets = trackOffsets;
return ErrorNumber.NoError;
2021-03-07 18:09:30 +01:00
}
public ErrorNumber ReadSector(ulong sectorAddress, out byte[] buffer)
2021-03-07 18:09:30 +01:00
{
buffer = null;
2021-03-07 18:09:30 +01:00
int trackNum = (int)(sectorAddress / _imageInfo.SectorsPerTrack);
int sectorOffset = (int)(sectorAddress % _imageInfo.SectorsPerTrack);
if(sectorAddress > _imageInfo.Sectors - 1)
return ErrorNumber.OutOfRange;
2021-03-07 18:09:30 +01:00
if(trackNum > 2 * _imageInfo.Cylinders)
return ErrorNumber.SectorNotFound;
2021-03-07 18:09:30 +01:00
buffer = new byte[_imageInfo.SectorSize];
2021-03-07 18:09:30 +01:00
if(_trackMap[trackNum].present != 1)
Array.Clear(buffer, 0, (int)_imageInfo.SectorSize);
2021-03-07 18:09:30 +01:00
else
{
Stream strm = _ddiImageFilter.GetDataForkStream();
2021-08-17 18:16:25 +01:00
strm.Seek(_trackOffsets[trackNum] + (sectorOffset * _imageInfo.SectorSize), SeekOrigin.Begin);
2021-03-07 18:09:30 +01:00
strm.Read(buffer, 0, (int)_imageInfo.SectorSize);
2021-03-07 18:09:30 +01:00
}
return ErrorNumber.NoError;
2021-03-07 18:09:30 +01:00
}
public ErrorNumber ReadSectors(ulong sectorAddress, uint length, out byte[] buffer)
2021-03-07 18:09:30 +01:00
{
buffer = null;
if(sectorAddress > _imageInfo.Sectors - 1)
return ErrorNumber.OutOfRange;
if(sectorAddress + length > _imageInfo.Sectors)
return ErrorNumber.OutOfRange;
var ms = new MemoryStream();
2021-03-07 18:09:30 +01:00
for(uint i = 0; i < length; i++)
{
ErrorNumber errno = ReadSector(sectorAddress + i, out byte[] sector);
if(errno != ErrorNumber.NoError)
return errno;
2021-03-07 18:09:30 +01:00
ms.Write(sector, 0, sector.Length);
}
2021-03-07 18:09:30 +01:00
buffer = ms.ToArray();
return ErrorNumber.NoError;
2021-03-07 18:09:30 +01:00
}
}
}