mirror of
https://github.com/aaru-dps/Aaru.git
synced 2025-12-16 19:24:25 +00:00
General cleanup and refactor.
This commit is contained in:
@@ -30,6 +30,8 @@
|
||||
// Copyright © 2011-2022 Natalia Portillo
|
||||
// ****************************************************************************/
|
||||
|
||||
namespace Aaru.DiscImages;
|
||||
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Runtime.InteropServices;
|
||||
@@ -41,15 +43,13 @@ using Aaru.CommonTypes.Interfaces;
|
||||
using Aaru.Console;
|
||||
using Aaru.Helpers;
|
||||
|
||||
namespace Aaru.DiscImages;
|
||||
|
||||
public sealed partial class Vhd
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public ErrorNumber Open(IFilter imageFilter)
|
||||
{
|
||||
Stream imageStream = imageFilter.GetDataForkStream();
|
||||
byte[] header = new byte[512];
|
||||
var header = new byte[512];
|
||||
byte[] footer;
|
||||
|
||||
imageStream.Seek(0, SeekOrigin.Begin);
|
||||
@@ -68,10 +68,10 @@ public sealed partial class Vhd
|
||||
imageStream.Read(footer, 0, 511);
|
||||
}
|
||||
|
||||
uint headerChecksum = BigEndianBitConverter.ToUInt32(header, 0x40);
|
||||
uint footerChecksum = BigEndianBitConverter.ToUInt32(footer, 0x40);
|
||||
ulong headerCookie = BigEndianBitConverter.ToUInt64(header, 0);
|
||||
ulong footerCookie = BigEndianBitConverter.ToUInt64(footer, 0);
|
||||
var headerChecksum = BigEndianBitConverter.ToUInt32(header, 0x40);
|
||||
var footerChecksum = BigEndianBitConverter.ToUInt32(footer, 0x40);
|
||||
var headerCookie = BigEndianBitConverter.ToUInt64(header, 0);
|
||||
var footerCookie = BigEndianBitConverter.ToUInt64(footer, 0);
|
||||
|
||||
header[0x40] = 0;
|
||||
header[0x41] = 0;
|
||||
@@ -155,13 +155,11 @@ public sealed partial class Vhd
|
||||
Encoding.ASCII.GetString(BigEndianBitConverter.GetBytes(_thisFooter.
|
||||
CreatorApplication)));
|
||||
|
||||
AaruConsole.DebugWriteLine("VirtualPC plugin", "footer.creatorVersion = 0x{0:X8}",
|
||||
_thisFooter.CreatorVersion);
|
||||
AaruConsole.DebugWriteLine("VirtualPC plugin", "footer.creatorVersion = 0x{0:X8}", _thisFooter.CreatorVersion);
|
||||
|
||||
AaruConsole.DebugWriteLine("VirtualPC plugin", "footer.creatorHostOS = 0x{0:X8} (\"{1}\")",
|
||||
_thisFooter.CreatorHostOs,
|
||||
Encoding.ASCII.GetString(BigEndianBitConverter.
|
||||
GetBytes(_thisFooter.CreatorHostOs)));
|
||||
Encoding.ASCII.GetString(BigEndianBitConverter.GetBytes(_thisFooter.CreatorHostOs)));
|
||||
|
||||
AaruConsole.DebugWriteLine("VirtualPC plugin", "footer.originalSize = {0}", _thisFooter.OriginalSize);
|
||||
AaruConsole.DebugWriteLine("VirtualPC plugin", "footer.currentSize = {0}", _thisFooter.CurrentSize);
|
||||
@@ -258,8 +256,7 @@ public sealed partial class Vhd
|
||||
|
||||
break;
|
||||
default:
|
||||
_imageInfo.ApplicationVersion =
|
||||
$"Unknown version 0x{_thisFooter.CreatorVersion:X8}";
|
||||
_imageInfo.ApplicationVersion = $"Unknown version 0x{_thisFooter.CreatorVersion:X8}";
|
||||
|
||||
break;
|
||||
}
|
||||
@@ -284,8 +281,7 @@ public sealed partial class Vhd
|
||||
|
||||
break;
|
||||
default:
|
||||
_imageInfo.ApplicationVersion =
|
||||
$"Unknown version 0x{_thisFooter.CreatorVersion:X8}";
|
||||
_imageInfo.ApplicationVersion = $"Unknown version 0x{_thisFooter.CreatorVersion:X8}";
|
||||
|
||||
break;
|
||||
}
|
||||
@@ -350,10 +346,10 @@ public sealed partial class Vhd
|
||||
_thisFooter.DiskType == TYPE_DIFFERENCING)
|
||||
{
|
||||
imageStream.Seek((long)_thisFooter.Offset, SeekOrigin.Begin);
|
||||
byte[] dynamicBytes = new byte[1024];
|
||||
var dynamicBytes = new byte[1024];
|
||||
imageStream.Read(dynamicBytes, 0, 1024);
|
||||
|
||||
uint dynamicChecksum = BigEndianBitConverter.ToUInt32(dynamicBytes, 0x24);
|
||||
var dynamicChecksum = BigEndianBitConverter.ToUInt32(dynamicBytes, 0x24);
|
||||
|
||||
dynamicBytes[0x24] = 0;
|
||||
dynamicBytes[0x25] = 0;
|
||||
@@ -362,9 +358,8 @@ public sealed partial class Vhd
|
||||
|
||||
uint dynamicChecksumCalculated = VhdChecksum(dynamicBytes);
|
||||
|
||||
AaruConsole.DebugWriteLine("VirtualPC plugin",
|
||||
"Dynamic header checksum = 0x{0:X8}, calculated = 0x{1:X8}", dynamicChecksum,
|
||||
dynamicChecksumCalculated);
|
||||
AaruConsole.DebugWriteLine("VirtualPC plugin", "Dynamic header checksum = 0x{0:X8}, calculated = 0x{1:X8}",
|
||||
dynamicChecksum, dynamicChecksumCalculated);
|
||||
|
||||
if(dynamicChecksum != dynamicChecksumCalculated)
|
||||
{
|
||||
@@ -380,7 +375,7 @@ public sealed partial class Vhd
|
||||
Reserved2 = new byte[256]
|
||||
};
|
||||
|
||||
for(int i = 0; i < 8; i++)
|
||||
for(var i = 0; i < 8; i++)
|
||||
_thisDynamic.LocatorEntries[i] = new ParentLocatorEntry();
|
||||
|
||||
_thisDynamic.Cookie = BigEndianBitConverter.ToUInt64(dynamicBytes, 0x00);
|
||||
@@ -395,22 +390,22 @@ public sealed partial class Vhd
|
||||
_thisDynamic.Reserved = BigEndianBitConverter.ToUInt32(dynamicBytes, 0x3C);
|
||||
_thisDynamic.ParentName = Encoding.BigEndianUnicode.GetString(dynamicBytes, 0x40, 512);
|
||||
|
||||
for(int i = 0; i < 8; i++)
|
||||
for(var i = 0; i < 8; i++)
|
||||
{
|
||||
_thisDynamic.LocatorEntries[i].PlatformCode =
|
||||
BigEndianBitConverter.ToUInt32(dynamicBytes, 0x240 + 0x00 + (24 * i));
|
||||
BigEndianBitConverter.ToUInt32(dynamicBytes, 0x240 + 0x00 + 24 * i);
|
||||
|
||||
_thisDynamic.LocatorEntries[i].PlatformDataSpace =
|
||||
BigEndianBitConverter.ToUInt32(dynamicBytes, 0x240 + 0x04 + (24 * i));
|
||||
BigEndianBitConverter.ToUInt32(dynamicBytes, 0x240 + 0x04 + 24 * i);
|
||||
|
||||
_thisDynamic.LocatorEntries[i].PlatformDataLength =
|
||||
BigEndianBitConverter.ToUInt32(dynamicBytes, 0x240 + 0x08 + (24 * i));
|
||||
BigEndianBitConverter.ToUInt32(dynamicBytes, 0x240 + 0x08 + 24 * i);
|
||||
|
||||
_thisDynamic.LocatorEntries[i].Reserved =
|
||||
BigEndianBitConverter.ToUInt32(dynamicBytes, 0x240 + 0x0C + (24 * i));
|
||||
BigEndianBitConverter.ToUInt32(dynamicBytes, 0x240 + 0x0C + 24 * i);
|
||||
|
||||
_thisDynamic.LocatorEntries[i].PlatformDataOffset =
|
||||
BigEndianBitConverter.ToUInt64(dynamicBytes, 0x240 + 0x10 + (24 * i));
|
||||
BigEndianBitConverter.ToUInt64(dynamicBytes, 0x240 + 0x10 + 24 * i);
|
||||
}
|
||||
|
||||
Array.Copy(dynamicBytes, 0x300, _thisDynamic.Reserved2, 0, 256);
|
||||
@@ -440,7 +435,7 @@ public sealed partial class Vhd
|
||||
|
||||
AaruConsole.DebugWriteLine("VirtualPC plugin", "dynamic.reserved = 0x{0:X8}", _thisDynamic.Reserved);
|
||||
|
||||
for(int i = 0; i < 8; i++)
|
||||
for(var i = 0; i < 8; i++)
|
||||
{
|
||||
AaruConsole.DebugWriteLine("VirtualPC plugin",
|
||||
"dynamic.locatorEntries[{0}].platformCode = 0x{1:X8} (\"{2}\")", i,
|
||||
@@ -448,20 +443,17 @@ public sealed partial class Vhd
|
||||
Encoding.ASCII.GetString(BigEndianBitConverter.GetBytes(_thisDynamic.
|
||||
LocatorEntries[i].PlatformCode)));
|
||||
|
||||
AaruConsole.DebugWriteLine("VirtualPC plugin",
|
||||
"dynamic.locatorEntries[{0}].platformDataSpace = {1}", i,
|
||||
AaruConsole.DebugWriteLine("VirtualPC plugin", "dynamic.locatorEntries[{0}].platformDataSpace = {1}", i,
|
||||
_thisDynamic.LocatorEntries[i].PlatformDataSpace);
|
||||
|
||||
AaruConsole.DebugWriteLine("VirtualPC plugin",
|
||||
"dynamic.locatorEntries[{0}].platformDataLength = {1}", i,
|
||||
_thisDynamic.LocatorEntries[i].PlatformDataLength);
|
||||
AaruConsole.DebugWriteLine("VirtualPC plugin", "dynamic.locatorEntries[{0}].platformDataLength = {1}",
|
||||
i, _thisDynamic.LocatorEntries[i].PlatformDataLength);
|
||||
|
||||
AaruConsole.DebugWriteLine("VirtualPC plugin", "dynamic.locatorEntries[{0}].reserved = 0x{1:X8}", i,
|
||||
_thisDynamic.LocatorEntries[i].Reserved);
|
||||
|
||||
AaruConsole.DebugWriteLine("VirtualPC plugin",
|
||||
"dynamic.locatorEntries[{0}].platformDataOffset = {1}", i,
|
||||
_thisDynamic.LocatorEntries[i].PlatformDataOffset);
|
||||
AaruConsole.DebugWriteLine("VirtualPC plugin", "dynamic.locatorEntries[{0}].platformDataOffset = {1}",
|
||||
i, _thisDynamic.LocatorEntries[i].PlatformDataOffset);
|
||||
}
|
||||
|
||||
AaruConsole.DebugWriteLine("VirtualPC plugin", "dynamic.parentName = \"{0}\"", _thisDynamic.ParentName);
|
||||
@@ -479,16 +471,16 @@ public sealed partial class Vhd
|
||||
|
||||
_blockAllocationTable = new uint[_thisDynamic.MaxTableEntries];
|
||||
|
||||
byte[] bat = new byte[_thisDynamic.MaxTableEntries * 4];
|
||||
var bat = new byte[_thisDynamic.MaxTableEntries * 4];
|
||||
imageStream.Seek((long)_thisDynamic.TableOffset, SeekOrigin.Begin);
|
||||
imageStream.Read(bat, 0, bat.Length);
|
||||
|
||||
ReadOnlySpan<byte> span = bat;
|
||||
|
||||
_blockAllocationTable = MemoryMarshal.Cast<byte, uint>(span).
|
||||
Slice(0, (int)_thisDynamic.MaxTableEntries).ToArray();
|
||||
_blockAllocationTable = MemoryMarshal.Cast<byte, uint>(span).Slice(0, (int)_thisDynamic.MaxTableEntries).
|
||||
ToArray();
|
||||
|
||||
for(int i = 0; i < _blockAllocationTable.Length; i++)
|
||||
for(var i = 0; i < _blockAllocationTable.Length; i++)
|
||||
_blockAllocationTable[i] = Swapping.Swap(_blockAllocationTable[i]);
|
||||
|
||||
DateTime endTime = DateTime.UtcNow;
|
||||
@@ -522,7 +514,7 @@ public sealed partial class Vhd
|
||||
{
|
||||
_locatorEntriesData = new byte[8][];
|
||||
|
||||
for(int i = 0; i < 8; i++)
|
||||
for(var i = 0; i < 8; i++)
|
||||
if(_thisDynamic.LocatorEntries[i].PlatformCode != 0x00000000)
|
||||
{
|
||||
_locatorEntriesData[i] = new byte[_thisDynamic.LocatorEntries[i].PlatformDataLength];
|
||||
@@ -535,23 +527,20 @@ public sealed partial class Vhd
|
||||
{
|
||||
case PLATFORM_CODE_WINDOWS_ABSOLUTE:
|
||||
case PLATFORM_CODE_WINDOWS_RELATIVE:
|
||||
AaruConsole.DebugWriteLine("VirtualPC plugin",
|
||||
"dynamic.locatorEntries[{0}] = \"{1}\"", i,
|
||||
Encoding.ASCII.GetString(_locatorEntriesData[i]));
|
||||
AaruConsole.DebugWriteLine("VirtualPC plugin", "dynamic.locatorEntries[{0}] = \"{1}\"",
|
||||
i, Encoding.ASCII.GetString(_locatorEntriesData[i]));
|
||||
|
||||
break;
|
||||
case PLATFORM_CODE_WINDOWS_ABSOLUTE_U:
|
||||
case PLATFORM_CODE_WINDOWS_RELATIVE_U:
|
||||
AaruConsole.DebugWriteLine("VirtualPC plugin",
|
||||
"dynamic.locatorEntries[{0}] = \"{1}\"", i,
|
||||
Encoding.BigEndianUnicode.
|
||||
GetString(_locatorEntriesData[i]));
|
||||
AaruConsole.DebugWriteLine("VirtualPC plugin", "dynamic.locatorEntries[{0}] = \"{1}\"",
|
||||
i,
|
||||
Encoding.BigEndianUnicode.GetString(_locatorEntriesData[i]));
|
||||
|
||||
break;
|
||||
case PLATFORM_CODE_MACINTOSH_URI:
|
||||
AaruConsole.DebugWriteLine("VirtualPC plugin",
|
||||
"dynamic.locatorEntries[{0}] = \"{1}\"", i,
|
||||
Encoding.UTF8.GetString(_locatorEntriesData[i]));
|
||||
AaruConsole.DebugWriteLine("VirtualPC plugin", "dynamic.locatorEntries[{0}] = \"{1}\"",
|
||||
i, Encoding.UTF8.GetString(_locatorEntriesData[i]));
|
||||
|
||||
break;
|
||||
default:
|
||||
@@ -562,8 +551,8 @@ public sealed partial class Vhd
|
||||
}
|
||||
}
|
||||
|
||||
int currentLocator = 0;
|
||||
bool locatorFound = false;
|
||||
var currentLocator = 0;
|
||||
var locatorFound = false;
|
||||
string parentPath = null;
|
||||
|
||||
while(!locatorFound &&
|
||||
@@ -583,8 +572,7 @@ public sealed partial class Vhd
|
||||
break;
|
||||
case PLATFORM_CODE_MACINTOSH_URI:
|
||||
parentPath =
|
||||
Uri.UnescapeDataString(Encoding.UTF8.
|
||||
GetString(_locatorEntriesData[currentLocator]));
|
||||
Uri.UnescapeDataString(Encoding.UTF8.GetString(_locatorEntriesData[currentLocator]));
|
||||
|
||||
if(parentPath.StartsWith("file://localhost", StringComparison.InvariantCulture))
|
||||
parentPath = parentPath.Remove(0, 16);
|
||||
@@ -647,8 +635,7 @@ public sealed partial class Vhd
|
||||
|
||||
if(!_parentImage.Identify(parentFilter))
|
||||
{
|
||||
AaruConsole.
|
||||
ErrorWriteLine("(VirtualPC plugin): Parent image is not a Virtual PC disk image");
|
||||
AaruConsole.ErrorWriteLine("(VirtualPC plugin): Parent image is not a Virtual PC disk image");
|
||||
|
||||
return ErrorNumber.InvalidArgument;
|
||||
}
|
||||
@@ -703,10 +690,10 @@ public sealed partial class Vhd
|
||||
case TYPE_DIFFERENCING:
|
||||
{
|
||||
// Block number for BAT searching
|
||||
uint blockNumber = (uint)Math.Floor(sectorAddress / (_thisDynamic.BlockSize / 512.0));
|
||||
var blockNumber = (uint)Math.Floor(sectorAddress / (_thisDynamic.BlockSize / 512.0));
|
||||
|
||||
// Sector number inside of block
|
||||
uint sectorInBlock = (uint)(sectorAddress % (_thisDynamic.BlockSize / 512));
|
||||
var sectorInBlock = (uint)(sectorAddress % (_thisDynamic.BlockSize / 512));
|
||||
|
||||
if(_blockAllocationTable[blockNumber] == 0xFFFFFFFF)
|
||||
{
|
||||
@@ -715,20 +702,20 @@ public sealed partial class Vhd
|
||||
return ErrorNumber.NoError;
|
||||
}
|
||||
|
||||
byte[] bitmap = new byte[_bitmapSize * 512];
|
||||
var bitmap = new byte[_bitmapSize * 512];
|
||||
|
||||
// Offset of block in file
|
||||
long blockOffset = _blockAllocationTable[blockNumber] * 512L;
|
||||
|
||||
int bitmapByte = (int)Math.Floor((double)sectorInBlock / 8);
|
||||
int bitmapBit = (int)(sectorInBlock % 8);
|
||||
var bitmapByte = (int)Math.Floor((double)sectorInBlock / 8);
|
||||
var bitmapBit = (int)(sectorInBlock % 8);
|
||||
|
||||
Stream thisStream = _thisFilter.GetDataForkStream();
|
||||
|
||||
thisStream.Seek(blockOffset, SeekOrigin.Begin);
|
||||
thisStream.Read(bitmap, 0, (int)_bitmapSize * 512);
|
||||
|
||||
byte mask = (byte)(1 << (7 - bitmapBit));
|
||||
var mask = (byte)(1 << (7 - bitmapBit));
|
||||
bool dirty = (bitmap[bitmapByte] & mask) == mask;
|
||||
|
||||
/*
|
||||
@@ -796,13 +783,13 @@ public sealed partial class Vhd
|
||||
case TYPE_DYNAMIC:
|
||||
{
|
||||
// Block number for BAT searching
|
||||
uint blockNumber = (uint)Math.Floor(sectorAddress / (_thisDynamic.BlockSize / 512.0));
|
||||
var blockNumber = (uint)Math.Floor(sectorAddress / (_thisDynamic.BlockSize / 512.0));
|
||||
|
||||
// Sector number inside of block
|
||||
uint sectorInBlock = (uint)(sectorAddress % (_thisDynamic.BlockSize / 512));
|
||||
var sectorInBlock = (uint)(sectorAddress % (_thisDynamic.BlockSize / 512));
|
||||
|
||||
// How many sectors before reaching end of block
|
||||
uint remainingInBlock = (_thisDynamic.BlockSize / 512) - sectorInBlock;
|
||||
uint remainingInBlock = _thisDynamic.BlockSize / 512 - sectorInBlock;
|
||||
|
||||
// Data that needs to be read from another block
|
||||
byte[] suffix = null;
|
||||
@@ -828,7 +815,7 @@ public sealed partial class Vhd
|
||||
uint sectorOffset = _blockAllocationTable[blockNumber] + _bitmapSize + sectorInBlock;
|
||||
|
||||
// Data that can be read in this block
|
||||
byte[] prefix = new byte[sectorsToReadHere * 512];
|
||||
var prefix = new byte[sectorsToReadHere * 512];
|
||||
|
||||
// 0xFFFFFFFF means unallocated
|
||||
if(_blockAllocationTable[blockNumber] != 0xFFFFFFFF)
|
||||
|
||||
Reference in New Issue
Block a user