Files
Aaru/Aaru.Images/PartClone/Read.cs

221 lines
7.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 partclone 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
// ****************************************************************************/
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
2020-02-27 00:33:26 +00:00
using Aaru.CommonTypes;
using Aaru.CommonTypes.Enums;
using Aaru.CommonTypes.Extents;
using Aaru.CommonTypes.Interfaces;
using Aaru.Console;
using Aaru.Helpers;
2020-02-27 00:33:26 +00:00
namespace Aaru.DiscImages
{
2020-07-22 13:20:25 +01:00
public sealed partial class PartClone
{
2021-08-17 13:56:05 +01:00
/// <inheritdoc />
public ErrorNumber Open(IFilter imageFilter)
{
Stream stream = imageFilter.GetDataForkStream();
stream.Seek(0, SeekOrigin.Begin);
2020-02-29 18:03:35 +00:00
if(stream.Length < 512)
return ErrorNumber.InvalidArgument;
byte[] pHdrB = new byte[Marshal.SizeOf<Header>()];
stream.Read(pHdrB, 0, Marshal.SizeOf<Header>());
_pHdr = Marshal.ByteArrayToStructureLittleEndian<Header>(pHdrB);
2020-07-20 21:11:32 +01:00
AaruConsole.DebugWriteLine("PartClone plugin", "pHdr.magic = {0}", StringHandlers.CToString(_pHdr.magic));
2020-02-29 18:03:35 +00:00
2020-02-27 23:48:41 +00:00
AaruConsole.DebugWriteLine("PartClone plugin", "pHdr.filesystem = {0}",
2020-07-20 21:11:32 +01:00
StringHandlers.CToString(_pHdr.filesystem));
2020-02-29 18:03:35 +00:00
2020-02-27 23:48:41 +00:00
AaruConsole.DebugWriteLine("PartClone plugin", "pHdr.version = {0}",
2020-07-20 21:11:32 +01:00
StringHandlers.CToString(_pHdr.version));
2020-02-29 18:03:35 +00:00
2020-07-20 21:11:32 +01:00
AaruConsole.DebugWriteLine("PartClone plugin", "pHdr.blockSize = {0}", _pHdr.blockSize);
AaruConsole.DebugWriteLine("PartClone plugin", "pHdr.deviceSize = {0}", _pHdr.deviceSize);
AaruConsole.DebugWriteLine("PartClone plugin", "pHdr.totalBlocks = {0}", _pHdr.totalBlocks);
AaruConsole.DebugWriteLine("PartClone plugin", "pHdr.usedBlocks = {0}", _pHdr.usedBlocks);
2020-07-20 21:11:32 +01:00
_byteMap = new byte[_pHdr.totalBlocks];
AaruConsole.DebugWriteLine("PartClone plugin", "Reading bytemap {0} bytes", _byteMap.Length);
stream.Read(_byteMap, 0, _byteMap.Length);
byte[] bitmagic = new byte[8];
stream.Read(bitmagic, 0, 8);
2020-02-27 23:48:41 +00:00
AaruConsole.DebugWriteLine("PartClone plugin", "pHdr.bitmagic = {0}", StringHandlers.CToString(bitmagic));
2020-07-20 21:11:32 +01:00
if(!_biTmAgIc.SequenceEqual(bitmagic))
2021-09-21 04:55:28 +01:00
{
AaruConsole.ErrorWriteLine("Could not find partclone BiTmAgIc, not continuing...");
return ErrorNumber.InvalidArgument;
}
2020-07-20 21:11:32 +01:00
_dataOff = stream.Position;
AaruConsole.DebugWriteLine("PartClone plugin", "pHdr.dataOff = {0}", _dataOff);
2020-02-27 23:48:41 +00:00
AaruConsole.DebugWriteLine("PartClone plugin", "Filling extents");
DateTime start = DateTime.Now;
2020-07-20 21:11:32 +01:00
_extents = new ExtentsULong();
_extentsOff = new Dictionary<ulong, ulong>();
bool current = _byteMap[0] > 0;
ulong blockOff = 0;
ulong extentStart = 0;
2020-07-20 21:11:32 +01:00
for(ulong i = 1; i < _pHdr.totalBlocks; i++)
{
2020-07-20 21:11:32 +01:00
bool next = _byteMap[i] > 0;
// Flux
if(next != current)
if(next)
{
extentStart = i;
2020-07-20 21:11:32 +01:00
_extentsOff.Add(i, ++blockOff);
}
else
{
2020-07-20 21:11:32 +01:00
_extents.Add(extentStart, i);
_extentsOff.TryGetValue(extentStart, out _);
}
2020-02-29 18:03:35 +00:00
if(next && current)
blockOff++;
current = next;
}
DateTime end = DateTime.Now;
2020-02-29 18:03:35 +00:00
2020-02-27 23:48:41 +00:00
AaruConsole.DebugWriteLine("PartClone plugin", "Took {0} seconds to fill extents",
2020-02-29 18:03:35 +00:00
(end - start).TotalSeconds);
2020-07-20 21:11:32 +01:00
_sectorCache = new Dictionary<ulong, byte[]>();
_imageInfo.CreationTime = imageFilter.CreationTime;
_imageInfo.LastModificationTime = imageFilter.LastWriteTime;
_imageInfo.MediaTitle = Path.GetFileNameWithoutExtension(imageFilter.Filename);
2020-07-20 21:11:32 +01:00
_imageInfo.Sectors = _pHdr.totalBlocks;
_imageInfo.SectorSize = _pHdr.blockSize;
_imageInfo.XmlMediaType = XmlMediaType.BlockMedia;
_imageInfo.MediaType = MediaType.GENERIC_HDD;
_imageInfo.ImageSize = (ulong)(stream.Length - (4096 + 0x40 + (long)_pHdr.totalBlocks));
_imageStream = stream;
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
if(sectorAddress > _imageInfo.Sectors - 1)
return ErrorNumber.OutOfRange;
2020-07-20 21:11:32 +01:00
if(_byteMap[sectorAddress] == 0)
{
buffer = new byte[_pHdr.blockSize];
return ErrorNumber.NoError;
}
if(_sectorCache.TryGetValue(sectorAddress, out buffer))
return ErrorNumber.NoError;
2020-07-20 21:11:32 +01:00
long imageOff = _dataOff + (long)(BlockOffset(sectorAddress) * (_pHdr.blockSize + CRC_SIZE));
buffer = new byte[_pHdr.blockSize];
2020-07-20 21:11:32 +01:00
_imageStream.Seek(imageOff, SeekOrigin.Begin);
_imageStream.Read(buffer, 0, (int)_pHdr.blockSize);
2020-07-20 21:11:32 +01:00
if(_sectorCache.Count > MAX_CACHED_SECTORS)
_sectorCache.Clear();
_sectorCache.Add(sectorAddress, buffer);
return ErrorNumber.NoError;
}
2021-08-17 13:56:05 +01:00
/// <inheritdoc />
public ErrorNumber ReadSectors(ulong sectorAddress, uint length, out byte[] buffer)
{
buffer = null;
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(sectorAddress + length > _imageInfo.Sectors)
return ErrorNumber.OutOfRange;
2020-02-29 18:03:35 +00:00
var ms = new MemoryStream();
bool allEmpty = true;
2020-02-29 18:03:35 +00:00
for(uint i = 0; i < length; i++)
2020-07-20 21:11:32 +01:00
if(_byteMap[sectorAddress + i] != 0)
{
allEmpty = false;
2020-02-29 18:03:35 +00:00
break;
}
2020-02-29 18:03:35 +00:00
if(allEmpty)
{
buffer = new byte[_pHdr.blockSize * length];
return ErrorNumber.NoError;
}
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;
}
}
}