Files
Aaru/Aaru.Images/Partimage/Read.cs

472 lines
20 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 partimage 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;
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 Partimage
2017-12-19 20:33:03 +00:00
{
2021-08-17 13:56:05 +01:00
/// <inheritdoc />
public ErrorNumber Open(IFilter imageFilter)
2017-12-19 20:33:03 +00:00
{
Stream stream = imageFilter.GetDataForkStream();
stream.Seek(0, SeekOrigin.Begin);
2020-02-29 18:03:35 +00:00
if(stream.Length < 512)
return ErrorNumber.InvalidArgument;
2017-12-19 20:33:03 +00:00
byte[] hdrB = new byte[Marshal.SizeOf<Header>()];
stream.Read(hdrB, 0, Marshal.SizeOf<Header>());
_cVolumeHeader = Marshal.ByteArrayToStructureLittleEndian<Header>(hdrB);
2017-12-19 20:33:03 +00:00
2020-02-27 23:48:41 +00:00
AaruConsole.DebugWriteLine("Partimage plugin", "CVolumeHeader.magic = {0}",
2020-07-20 21:11:32 +01:00
StringHandlers.CToString(_cVolumeHeader.magic));
2020-02-29 18:03:35 +00:00
2020-02-27 23:48:41 +00:00
AaruConsole.DebugWriteLine("Partimage plugin", "CVolumeHeader.version = {0}",
2020-07-20 21:11:32 +01:00
StringHandlers.CToString(_cVolumeHeader.version));
2020-02-29 18:03:35 +00:00
2020-02-27 23:48:41 +00:00
AaruConsole.DebugWriteLine("Partimage plugin", "CVolumeHeader.volumeNumber = {0}",
2020-07-20 21:11:32 +01:00
_cVolumeHeader.volumeNumber);
2020-02-29 18:03:35 +00:00
2020-02-27 23:48:41 +00:00
AaruConsole.DebugWriteLine("Partimage plugin", "CVolumeHeader.identificator = {0:X16}",
2020-07-20 21:11:32 +01:00
_cVolumeHeader.identificator);
2017-12-19 20:33:03 +00:00
// TODO: Support multifile volumes
2020-07-20 21:11:32 +01:00
if(_cVolumeHeader.volumeNumber > 0)
2021-09-21 04:55:28 +01:00
{
AaruConsole.ErrorWriteLine("Support for multiple volumes not supported");
return ErrorNumber.NotImplemented;
}
2017-12-19 20:33:03 +00:00
hdrB = new byte[Marshal.SizeOf<MainHeader>()];
stream.Read(hdrB, 0, Marshal.SizeOf<MainHeader>());
_cMainHeader = Marshal.ByteArrayToStructureLittleEndian<MainHeader>(hdrB);
2017-12-19 20:33:03 +00:00
2020-02-27 23:48:41 +00:00
AaruConsole.DebugWriteLine("Partimage plugin", "CMainHeader.szFileSystem = {0}",
2020-07-20 21:11:32 +01:00
StringHandlers.CToString(_cMainHeader.szFileSystem));
2020-02-29 18:03:35 +00:00
2020-02-27 23:48:41 +00:00
AaruConsole.DebugWriteLine("Partimage plugin", "CMainHeader.szPartDescription = {0}",
2020-07-20 21:11:32 +01:00
StringHandlers.CToString(_cMainHeader.szPartDescription));
2020-02-29 18:03:35 +00:00
2020-02-27 23:48:41 +00:00
AaruConsole.DebugWriteLine("Partimage plugin", "CMainHeader.szOriginalDevice = {0}",
2020-07-20 21:11:32 +01:00
StringHandlers.CToString(_cMainHeader.szOriginalDevice));
2020-02-29 18:03:35 +00:00
2020-02-27 23:48:41 +00:00
AaruConsole.DebugWriteLine("Partimage plugin", "CMainHeader.szFirstImageFilepath = {0}",
2020-07-20 21:11:32 +01:00
StringHandlers.CToString(_cMainHeader.szFirstImageFilepath));
2020-02-29 18:03:35 +00:00
2020-02-27 23:48:41 +00:00
AaruConsole.DebugWriteLine("Partimage plugin", "CMainHeader.szUnameSysname = {0}",
2020-07-20 21:11:32 +01:00
StringHandlers.CToString(_cMainHeader.szUnameSysname));
2020-02-29 18:03:35 +00:00
2020-02-27 23:48:41 +00:00
AaruConsole.DebugWriteLine("Partimage plugin", "CMainHeader.szUnameNodename = {0}",
2020-07-20 21:11:32 +01:00
StringHandlers.CToString(_cMainHeader.szUnameNodename));
2020-02-29 18:03:35 +00:00
2020-02-27 23:48:41 +00:00
AaruConsole.DebugWriteLine("Partimage plugin", "CMainHeader.szUnameRelease = {0}",
2020-07-20 21:11:32 +01:00
StringHandlers.CToString(_cMainHeader.szUnameRelease));
2020-02-29 18:03:35 +00:00
2020-02-27 23:48:41 +00:00
AaruConsole.DebugWriteLine("Partimage plugin", "CMainHeader.szUnameVersion = {0}",
2020-07-20 21:11:32 +01:00
StringHandlers.CToString(_cMainHeader.szUnameVersion));
2020-02-29 18:03:35 +00:00
2020-02-27 23:48:41 +00:00
AaruConsole.DebugWriteLine("Partimage plugin", "CMainHeader.szUnameMachine = {0}",
2020-07-20 21:11:32 +01:00
StringHandlers.CToString(_cMainHeader.szUnameMachine));
2020-02-29 18:03:35 +00:00
2020-02-27 23:48:41 +00:00
AaruConsole.DebugWriteLine("Partimage plugin", "CMainHeader.dwCompression = {0} ({1})",
2020-07-20 21:11:32 +01:00
_cMainHeader.dwCompression, (uint)_cMainHeader.dwCompression);
2020-02-29 18:03:35 +00:00
2020-07-20 21:11:32 +01:00
AaruConsole.DebugWriteLine("Partimage plugin", "CMainHeader.dwMainFlags = {0}", _cMainHeader.dwMainFlags);
2020-02-29 18:03:35 +00:00
2020-02-27 23:48:41 +00:00
AaruConsole.DebugWriteLine("Partimage plugin", "CMainHeader.dateCreate.tm_sec = {0}",
2020-07-20 21:11:32 +01:00
_cMainHeader.dateCreate.Second);
2020-02-29 18:03:35 +00:00
2020-02-27 23:48:41 +00:00
AaruConsole.DebugWriteLine("Partimage plugin", "CMainHeader.dateCreate.tm_min = {0}",
2020-07-20 21:11:32 +01:00
_cMainHeader.dateCreate.Minute);
2020-02-29 18:03:35 +00:00
2020-02-27 23:48:41 +00:00
AaruConsole.DebugWriteLine("Partimage plugin", "CMainHeader.dateCreate.tm_hour = {0}",
2020-07-20 21:11:32 +01:00
_cMainHeader.dateCreate.Hour);
2020-02-29 18:03:35 +00:00
2020-02-27 23:48:41 +00:00
AaruConsole.DebugWriteLine("Partimage plugin", "CMainHeader.dateCreate.tm_mday = {0}",
2020-07-20 21:11:32 +01:00
_cMainHeader.dateCreate.DayOfMonth);
2020-02-29 18:03:35 +00:00
2020-02-27 23:48:41 +00:00
AaruConsole.DebugWriteLine("Partimage plugin", "CMainHeader.dateCreate.tm_mon = {0}",
2020-07-20 21:11:32 +01:00
_cMainHeader.dateCreate.Month);
2020-02-29 18:03:35 +00:00
2020-02-27 23:48:41 +00:00
AaruConsole.DebugWriteLine("Partimage plugin", "CMainHeader.dateCreate.tm_year = {0}",
2020-07-20 21:11:32 +01:00
_cMainHeader.dateCreate.Year);
2020-02-29 18:03:35 +00:00
2020-02-27 23:48:41 +00:00
AaruConsole.DebugWriteLine("Partimage plugin", "CMainHeader.dateCreate.tm_wday = {0}",
2020-07-20 21:11:32 +01:00
_cMainHeader.dateCreate.DayOfWeek);
2020-02-29 18:03:35 +00:00
2020-02-27 23:48:41 +00:00
AaruConsole.DebugWriteLine("Partimage plugin", "CMainHeader.dateCreate.tm_yday = {0}",
2020-07-20 21:11:32 +01:00
_cMainHeader.dateCreate.DayOfYear);
2020-02-29 18:03:35 +00:00
2020-02-27 23:48:41 +00:00
AaruConsole.DebugWriteLine("Partimage plugin", "CMainHeader.dateCreate.tm_isdst = {0}",
2020-07-20 21:11:32 +01:00
_cMainHeader.dateCreate.IsDst);
2020-02-29 18:03:35 +00:00
2020-02-27 23:48:41 +00:00
AaruConsole.DebugWriteLine("Partimage plugin", "CMainHeader.dateCreate.tm_gmtoffsec = {0}",
2020-07-20 21:11:32 +01:00
_cMainHeader.dateCreate.GmtOff);
2020-02-29 18:03:35 +00:00
2020-02-27 23:48:41 +00:00
AaruConsole.DebugWriteLine("Partimage plugin", "CMainHeader.dateCreate.tm_zone = {0}",
2020-07-20 21:11:32 +01:00
_cMainHeader.dateCreate.Timezone);
2020-02-29 18:03:35 +00:00
2020-07-20 21:11:32 +01:00
var dateCreate = new DateTime(1900 + (int)_cMainHeader.dateCreate.Year,
(int)_cMainHeader.dateCreate.Month + 1,
(int)_cMainHeader.dateCreate.DayOfMonth, (int)_cMainHeader.dateCreate.Hour,
(int)_cMainHeader.dateCreate.Minute, (int)_cMainHeader.dateCreate.Second);
2017-12-19 20:33:03 +00:00
2020-02-27 23:48:41 +00:00
AaruConsole.DebugWriteLine("Partimage plugin", "CMainHeader.dateCreate = {0}", dateCreate);
2017-12-19 20:33:03 +00:00
2020-07-20 21:11:32 +01:00
AaruConsole.DebugWriteLine("Partimage plugin", "CMainHeader.qwPartSize = {0}", _cMainHeader.qwPartSize);
2020-02-29 18:03:35 +00:00
2020-02-27 23:48:41 +00:00
AaruConsole.DebugWriteLine("Partimage plugin", "CMainHeader.szHostname = {0}",
2020-07-20 21:11:32 +01:00
StringHandlers.CToString(_cMainHeader.szHostname));
2020-02-29 18:03:35 +00:00
2020-02-27 23:48:41 +00:00
AaruConsole.DebugWriteLine("Partimage plugin", "CMainHeader.szVersion = {0}",
2020-07-20 21:11:32 +01:00
StringHandlers.CToString(_cMainHeader.szVersion));
2020-02-29 18:03:35 +00:00
2020-07-20 21:11:32 +01:00
AaruConsole.DebugWriteLine("Partimage plugin", "CMainHeader.dwMbrCount = {0}", _cMainHeader.dwMbrCount);
AaruConsole.DebugWriteLine("Partimage plugin", "CMainHeader.dwMbrSize = {0}", _cMainHeader.dwMbrSize);
2020-02-29 18:03:35 +00:00
2020-02-27 23:48:41 +00:00
AaruConsole.DebugWriteLine("Partimage plugin", "CMainHeader.dwEncryptAlgo = {0} ({1})",
2020-07-20 21:11:32 +01:00
_cMainHeader.dwEncryptAlgo, (uint)_cMainHeader.dwEncryptAlgo);
2020-02-29 18:03:35 +00:00
2020-02-27 23:48:41 +00:00
AaruConsole.DebugWriteLine("Partimage plugin", "ArrayIsNullOrEmpty(CMainHeader.cHashTestKey) = {0}",
2020-07-20 21:11:32 +01:00
ArrayHelpers.ArrayIsNullOrEmpty(_cMainHeader.cHashTestKey));
2020-02-29 18:03:35 +00:00
2020-02-27 23:48:41 +00:00
AaruConsole.DebugWriteLine("Partimage plugin", "CMainHeader.dwReservedFuture000 = {0}",
2020-07-20 21:11:32 +01:00
_cMainHeader.dwReservedFuture000);
2020-02-29 18:03:35 +00:00
2020-02-27 23:48:41 +00:00
AaruConsole.DebugWriteLine("Partimage plugin", "CMainHeader.dwReservedFuture001 = {0}",
2020-07-20 21:11:32 +01:00
_cMainHeader.dwReservedFuture001);
2020-02-29 18:03:35 +00:00
2020-02-27 23:48:41 +00:00
AaruConsole.DebugWriteLine("Partimage plugin", "CMainHeader.dwReservedFuture002 = {0}",
2020-07-20 21:11:32 +01:00
_cMainHeader.dwReservedFuture002);
2020-02-29 18:03:35 +00:00
2020-02-27 23:48:41 +00:00
AaruConsole.DebugWriteLine("Partimage plugin", "CMainHeader.dwReservedFuture003 = {0}",
2020-07-20 21:11:32 +01:00
_cMainHeader.dwReservedFuture003);
2020-02-29 18:03:35 +00:00
2020-02-27 23:48:41 +00:00
AaruConsole.DebugWriteLine("Partimage plugin", "CMainHeader.dwReservedFuture004 = {0}",
2020-07-20 21:11:32 +01:00
_cMainHeader.dwReservedFuture004);
2020-02-29 18:03:35 +00:00
2020-02-27 23:48:41 +00:00
AaruConsole.DebugWriteLine("Partimage plugin", "CMainHeader.dwReservedFuture005 = {0}",
2020-07-20 21:11:32 +01:00
_cMainHeader.dwReservedFuture005);
2020-02-29 18:03:35 +00:00
2020-02-27 23:48:41 +00:00
AaruConsole.DebugWriteLine("Partimage plugin", "CMainHeader.dwReservedFuture006 = {0}",
2020-07-20 21:11:32 +01:00
_cMainHeader.dwReservedFuture006);
2020-02-29 18:03:35 +00:00
2020-02-27 23:48:41 +00:00
AaruConsole.DebugWriteLine("Partimage plugin", "CMainHeader.dwReservedFuture007 = {0}",
2020-07-20 21:11:32 +01:00
_cMainHeader.dwReservedFuture007);
2020-02-29 18:03:35 +00:00
2020-02-27 23:48:41 +00:00
AaruConsole.DebugWriteLine("Partimage plugin", "CMainHeader.dwReservedFuture008 = {0}",
2020-07-20 21:11:32 +01:00
_cMainHeader.dwReservedFuture008);
2020-02-29 18:03:35 +00:00
2020-02-27 23:48:41 +00:00
AaruConsole.DebugWriteLine("Partimage plugin", "CMainHeader.dwReservedFuture009 = {0}",
2020-07-20 21:11:32 +01:00
_cMainHeader.dwReservedFuture009);
2020-02-29 18:03:35 +00:00
2020-02-27 23:48:41 +00:00
AaruConsole.DebugWriteLine("Partimage plugin", "ArrayIsNullOrEmpty(CMainHeader.cReserved) = {0}",
2020-07-20 21:11:32 +01:00
ArrayHelpers.ArrayIsNullOrEmpty(_cMainHeader.cReserved));
2020-02-29 18:03:35 +00:00
2020-07-20 21:11:32 +01:00
AaruConsole.DebugWriteLine("Partimage plugin", "CMainHeader.crc = 0x{0:X8}", _cMainHeader.crc);
2017-12-19 20:33:03 +00:00
// partimage 0.6.1 does not support them either
2020-07-20 21:11:32 +01:00
if(_cMainHeader.dwEncryptAlgo != PEncryption.None)
2021-09-21 04:55:28 +01:00
{
AaruConsole.ErrorWriteLine("Encrypted images are currently not supported.");
return ErrorNumber.NotImplemented;
}
2017-12-19 20:33:03 +00:00
string magic;
// Skip MBRs
2020-07-20 21:11:32 +01:00
if(_cMainHeader.dwMbrCount > 0)
2017-12-19 20:33:03 +00:00
{
hdrB = new byte[MAGIC_BEGIN_MBRBACKUP.Length];
stream.Read(hdrB, 0, MAGIC_BEGIN_MBRBACKUP.Length);
magic = StringHandlers.CToString(hdrB);
2020-02-29 18:03:35 +00:00
if(!magic.Equals(MAGIC_BEGIN_MBRBACKUP))
2021-09-21 04:55:28 +01:00
{
AaruConsole.ErrorWriteLine("Cannot find MBRs");
return ErrorNumber.InvalidArgument;
}
2017-12-19 20:33:03 +00:00
2020-07-20 21:11:32 +01:00
stream.Seek(_cMainHeader.dwMbrSize * _cMainHeader.dwMbrCount, SeekOrigin.Current);
2017-12-19 20:33:03 +00:00
}
// Skip extended headers and their CRC fields
stream.Seek((MAGIC_BEGIN_EXT000.Length + 4) * 10, SeekOrigin.Current);
hdrB = new byte[MAGIC_BEGIN_LOCALHEADER.Length];
stream.Read(hdrB, 0, MAGIC_BEGIN_LOCALHEADER.Length);
magic = StringHandlers.CToString(hdrB);
2020-02-29 18:03:35 +00:00
if(!magic.Equals(MAGIC_BEGIN_LOCALHEADER))
2021-09-21 04:55:28 +01:00
{
AaruConsole.ErrorWriteLine("Cannot find local header");
return ErrorNumber.InvalidArgument;
}
2017-12-19 20:33:03 +00:00
hdrB = new byte[Marshal.SizeOf<CLocalHeader>()];
stream.Read(hdrB, 0, Marshal.SizeOf<CLocalHeader>());
CLocalHeader localHeader = Marshal.ByteArrayToStructureLittleEndian<CLocalHeader>(hdrB);
2017-12-19 20:33:03 +00:00
2020-02-29 18:03:35 +00:00
AaruConsole.DebugWriteLine("Partimage plugin", "CLocalHeader.qwBlockSize = {0}", localHeader.qwBlockSize);
2020-02-27 23:48:41 +00:00
AaruConsole.DebugWriteLine("Partimage plugin", "CLocalHeader.qwUsedBlocks = {0}", localHeader.qwUsedBlocks);
2020-02-29 18:03:35 +00:00
2020-02-27 23:48:41 +00:00
AaruConsole.DebugWriteLine("Partimage plugin", "CLocalHeader.qwBlocksCount = {0}",
2020-02-29 18:03:35 +00:00
localHeader.qwBlocksCount);
2020-02-27 23:48:41 +00:00
AaruConsole.DebugWriteLine("Partimage plugin", "CLocalHeader.qwBitmapSize = {0}", localHeader.qwBitmapSize);
2020-02-29 18:03:35 +00:00
2020-02-27 23:48:41 +00:00
AaruConsole.DebugWriteLine("Partimage plugin", "CLocalHeader.qwBadBlocksCount = {0}",
2020-02-29 18:03:35 +00:00
localHeader.qwBadBlocksCount);
2020-02-27 23:48:41 +00:00
AaruConsole.DebugWriteLine("Partimage plugin", "CLocalHeader.szLabel = {0}",
2020-02-29 18:03:35 +00:00
StringHandlers.CToString(localHeader.szLabel));
2020-02-27 23:48:41 +00:00
AaruConsole.DebugWriteLine("Partimage plugin", "ArrayIsNullOrEmpty(CLocalHeader.cReserved) = {0}",
2020-02-29 18:03:35 +00:00
ArrayHelpers.ArrayIsNullOrEmpty(localHeader.cReserved));
2020-02-27 23:48:41 +00:00
AaruConsole.DebugWriteLine("Partimage plugin", "CLocalHeader.crc = 0x{0:X8}", localHeader.crc);
2017-12-19 20:33:03 +00:00
hdrB = new byte[MAGIC_BEGIN_BITMAP.Length];
stream.Read(hdrB, 0, MAGIC_BEGIN_BITMAP.Length);
magic = StringHandlers.CToString(hdrB);
2020-02-29 18:03:35 +00:00
if(!magic.Equals(MAGIC_BEGIN_BITMAP))
2021-09-21 04:55:28 +01:00
{
AaruConsole.ErrorWriteLine("Cannot find bitmap");
return ErrorNumber.InvalidArgument;
}
2017-12-19 20:33:03 +00:00
2020-07-20 21:11:32 +01:00
_bitmap = new byte[localHeader.qwBitmapSize];
stream.Read(_bitmap, 0, (int)localHeader.qwBitmapSize);
2017-12-19 20:33:03 +00:00
hdrB = new byte[MAGIC_BEGIN_INFO.Length];
stream.Read(hdrB, 0, MAGIC_BEGIN_INFO.Length);
magic = StringHandlers.CToString(hdrB);
2020-02-29 18:03:35 +00:00
if(!magic.Equals(MAGIC_BEGIN_INFO))
2021-09-21 04:55:28 +01:00
{
AaruConsole.ErrorWriteLine("Cannot find info block");
return ErrorNumber.InvalidArgument;
}
2017-12-19 20:33:03 +00:00
// Skip info block and its checksum
stream.Seek(16384 + 4, SeekOrigin.Current);
hdrB = new byte[MAGIC_BEGIN_DATABLOCKS.Length];
stream.Read(hdrB, 0, MAGIC_BEGIN_DATABLOCKS.Length);
magic = StringHandlers.CToString(hdrB);
2020-02-29 18:03:35 +00:00
if(!magic.Equals(MAGIC_BEGIN_DATABLOCKS))
2021-09-21 04:55:28 +01:00
{
AaruConsole.ErrorWriteLine("Cannot find data blocks");
return ErrorNumber.InvalidArgument;
}
2017-12-19 20:33:03 +00:00
2020-07-20 21:11:32 +01:00
_dataOff = stream.Position;
2017-12-19 20:33:03 +00:00
2020-07-20 21:11:32 +01:00
AaruConsole.DebugWriteLine("Partimage plugin", "dataOff = {0}", _dataOff);
2017-12-19 20:33:03 +00:00
// Seek to tail
stream.Seek(-(Marshal.SizeOf<CMainTail>() + MAGIC_BEGIN_TAIL.Length), SeekOrigin.End);
2017-12-19 20:33:03 +00:00
hdrB = new byte[MAGIC_BEGIN_TAIL.Length];
stream.Read(hdrB, 0, MAGIC_BEGIN_TAIL.Length);
magic = StringHandlers.CToString(hdrB);
2020-02-29 18:03:35 +00:00
2017-12-19 20:33:03 +00:00
if(!magic.Equals(MAGIC_BEGIN_TAIL))
2021-09-21 04:55:28 +01:00
{
AaruConsole.ErrorWriteLine("Cannot find tail. Multiple volumes are not supported or image is corrupt.");
return ErrorNumber.InvalidArgument;
}
2017-12-19 20:33:03 +00:00
2020-02-27 23:48:41 +00:00
AaruConsole.DebugWriteLine("Partimage plugin", "Filling extents");
2018-06-22 08:08:38 +01:00
DateTime start = DateTime.Now;
2020-07-20 21:11:32 +01:00
_extents = new ExtentsULong();
_extentsOff = new Dictionary<ulong, ulong>();
bool current = (_bitmap[0] & (1 << (0 % 8))) != 0;
ulong blockOff = 0;
2017-12-19 20:33:03 +00:00
ulong extentStart = 0;
for(ulong i = 1; i <= localHeader.qwBlocksCount; i++)
{
2020-07-20 21:11:32 +01:00
bool next = (_bitmap[i / 8] & (1 << (int)(i % 8))) != 0;
2017-12-19 20:33:03 +00:00
// Flux
if(next != current)
if(next)
{
extentStart = i;
2020-07-20 21:11:32 +01:00
_extentsOff.Add(i, ++blockOff);
2017-12-19 20:33:03 +00:00
}
else
{
2020-07-20 21:11:32 +01:00
_extents.Add(extentStart, i);
_extentsOff.TryGetValue(extentStart, out ulong _);
2017-12-19 20:33:03 +00:00
}
2020-02-29 18:03:35 +00:00
if(next && current)
blockOff++;
2017-12-19 20:33:03 +00:00
current = next;
}
DateTime end = DateTime.Now;
2020-02-29 18:03:35 +00:00
2020-02-27 23:48:41 +00:00
AaruConsole.DebugWriteLine("Partimage plugin", "Took {0} seconds to fill extents",
2020-02-29 18:03:35 +00:00
(end - start).TotalSeconds);
2017-12-19 20:33:03 +00:00
2020-07-20 21:11:32 +01:00
_sectorCache = new Dictionary<ulong, byte[]>();
2017-12-19 20:33:03 +00:00
2020-07-20 21:11:32 +01:00
_imageInfo.CreationTime = dateCreate;
_imageInfo.LastModificationTime = imageFilter.LastWriteTime;
_imageInfo.MediaTitle = Path.GetFileNameWithoutExtension(imageFilter.Filename);
2020-07-20 21:11:32 +01:00
_imageInfo.Sectors = localHeader.qwBlocksCount + 1;
_imageInfo.SectorSize = (uint)localHeader.qwBlockSize;
_imageInfo.XmlMediaType = XmlMediaType.BlockMedia;
_imageInfo.MediaType = MediaType.GENERIC_HDD;
_imageInfo.Version = StringHandlers.CToString(_cMainHeader.szVersion);
_imageInfo.Comments = StringHandlers.CToString(_cMainHeader.szPartDescription);
2020-02-29 18:03:35 +00:00
2020-07-20 21:11:32 +01:00
_imageInfo.ImageSize =
(ulong)(stream.Length - (_dataOff + Marshal.SizeOf<CMainTail>() + MAGIC_BEGIN_TAIL.Length));
2020-02-29 18:03:35 +00:00
2020-07-20 21:11:32 +01:00
_imageStream = stream;
2017-12-19 20:33:03 +00:00
return ErrorNumber.NoError;
2017-12-19 20:33:03 +00:00
}
2021-08-17 13:56:05 +01:00
/// <inheritdoc />
public ErrorNumber ReadSector(ulong sectorAddress, out byte[] buffer)
2017-12-19 20:33:03 +00:00
{
buffer = null;
2020-07-20 21:11:32 +01:00
if(sectorAddress > _imageInfo.Sectors - 1)
return ErrorNumber.OutOfRange;
2017-12-19 20:33:03 +00:00
2020-07-20 21:11:32 +01:00
if((_bitmap[sectorAddress / 8] & (1 << (int)(sectorAddress % 8))) == 0)
{
buffer = new byte[_imageInfo.SectorSize];
return ErrorNumber.NoError;
}
2017-12-19 20:33:03 +00:00
if(_sectorCache.TryGetValue(sectorAddress, out buffer))
return ErrorNumber.NoError;
2017-12-19 20:33:03 +00:00
ulong blockOff = BlockOffset(sectorAddress);
// Offset of requested sector is:
// Start of data +
2020-07-20 21:11:32 +01:00
long imageOff = _dataOff +
2020-02-29 18:03:35 +00:00
2017-12-19 20:33:03 +00:00
// How many stored bytes to skip
2020-07-20 21:11:32 +01:00
(long)(blockOff * _imageInfo.SectorSize) +
2020-02-29 18:03:35 +00:00
2017-12-19 20:33:03 +00:00
// How many bytes of CRC blocks to skip
2020-07-20 21:11:32 +01:00
((long)(blockOff / (CHECK_FREQUENCY / _imageInfo.SectorSize)) * Marshal.SizeOf<CCheck>());
2017-12-19 20:33:03 +00:00
buffer = new byte[_imageInfo.SectorSize];
2020-07-20 21:11:32 +01:00
_imageStream.Seek(imageOff, SeekOrigin.Begin);
_imageStream.Read(buffer, 0, (int)_imageInfo.SectorSize);
2017-12-19 20:33:03 +00:00
2020-07-20 21:11:32 +01:00
if(_sectorCache.Count > MAX_CACHED_SECTORS)
_sectorCache.Clear();
2017-12-19 20:33:03 +00:00
_sectorCache.Add(sectorAddress, buffer);
2017-12-19 20:33:03 +00:00
return ErrorNumber.NoError;
2017-12-19 20:33:03 +00:00
}
2021-08-17 13:56:05 +01:00
/// <inheritdoc />
public ErrorNumber ReadSectors(ulong sectorAddress, uint length, out byte[] buffer)
2017-12-19 20:33:03 +00:00
{
buffer = null;
2020-07-20 21:11:32 +01:00
if(sectorAddress > _imageInfo.Sectors - 1)
return ErrorNumber.OutOfRange;
2017-12-19 20:33:03 +00:00
2020-07-20 21:11:32 +01:00
if(sectorAddress + length > _imageInfo.Sectors)
return ErrorNumber.OutOfRange;
2017-12-19 20:33:03 +00:00
2020-02-29 18:03:35 +00:00
var ms = new MemoryStream();
2017-12-19 20:33:03 +00:00
bool allEmpty = true;
2020-02-29 18:03:35 +00:00
2018-06-22 08:08:38 +01:00
for(uint i = 0; i < length; i++)
2020-07-20 21:11:32 +01:00
if((_bitmap[sectorAddress / 8] & (1 << (int)(sectorAddress % 8))) != 0)
2017-12-19 20:33:03 +00:00
{
allEmpty = false;
2020-02-29 18:03:35 +00:00
2017-12-19 20:33:03 +00:00
break;
}
2020-02-29 18:03:35 +00:00
if(allEmpty)
{
buffer = new byte[_imageInfo.SectorSize * length];
return ErrorNumber.NoError;
}
2017-12-19 20:33:03 +00:00
for(uint i = 0; i < length; i++)
{
ErrorNumber errno = ReadSector(sectorAddress + i, out byte[] sector);
if(errno != ErrorNumber.NoError)
return errno;
2017-12-19 20:33:03 +00:00
ms.Write(sector, 0, sector.Length);
}
buffer = ms.ToArray();
return ErrorNumber.NoError;
2017-12-19 20:33:03 +00:00
}
}
}