[QCOW] Use new source generator based big endian marshaller

This commit is contained in:
2025-10-21 11:40:53 +01:00
parent 8c93002fdd
commit a1ad9aa6a0
3 changed files with 20 additions and 18 deletions

View File

@@ -50,7 +50,7 @@ public sealed partial class Qcow
var qHdrB = new byte[48];
stream.EnsureRead(qHdrB, 0, 48);
_qHdr = Marshal.SpanToStructureBigEndian<Header>(qHdrB);
_qHdr = Marshal.SpanToStructureBigEndianGenerated<Header>(qHdrB);
return _qHdr is { magic: QCOW_MAGIC, version: QCOW_VERSION };
}

View File

@@ -57,9 +57,9 @@ public sealed partial class Qcow
if(stream.Length < 512) return ErrorNumber.InvalidArgument;
byte[] qHdrB = new byte[48];
var qHdrB = new byte[48];
stream.EnsureRead(qHdrB, 0, 48);
_qHdr = Marshal.SpanToStructureBigEndian<Header>(qHdrB);
_qHdr = Marshal.SpanToStructureBigEndianGenerated<Header>(qHdrB);
AaruLogging.Debug(MODULE_NAME, "qHdr.magic = 0x{0:X8}", _qHdr.magic);
AaruLogging.Debug(MODULE_NAME, "qHdr.version = {0}", _qHdr.version);
@@ -133,7 +133,7 @@ public sealed partial class Qcow
AaruLogging.Debug(MODULE_NAME, "qHdr.l2Size = {0}", _l2Size);
AaruLogging.Debug(MODULE_NAME, "qHdr.sectors = {0}", _imageInfo.Sectors);
byte[] l1TableB = new byte[_l1Size * 8];
var l1TableB = new byte[_l1Size * 8];
stream.Seek((long)_qHdr.l1_table_offset, SeekOrigin.Begin);
stream.EnsureRead(l1TableB, 0, (int)_l1Size * 8);
_l1Table = MemoryMarshal.Cast<byte, ulong>(l1TableB).ToArray();
@@ -142,10 +142,10 @@ public sealed partial class Qcow
for(long i = 0; i < _l1Table.LongLength; i++) _l1Table[i] = Swapping.Swap(_l1Table[i]);
_l1Mask = 0;
int c = 0;
var c = 0;
_l1Shift = _qHdr.l2_bits + _qHdr.cluster_bits;
for(int i = 0; i < 64; i++)
for(var i = 0; i < 64; i++)
{
_l1Mask <<= 1;
@@ -157,13 +157,13 @@ public sealed partial class Qcow
_l2Mask = 0;
for(int i = 0; i < _qHdr.l2_bits; i++) _l2Mask = (_l2Mask << 1) + 1;
for(var i = 0; i < _qHdr.l2_bits; i++) _l2Mask = (_l2Mask << 1) + 1;
_l2Mask <<= _qHdr.cluster_bits;
_sectorMask = 0;
for(int i = 0; i < _qHdr.cluster_bits; i++) _sectorMask = (_sectorMask << 1) + 1;
for(var i = 0; i < _qHdr.cluster_bits; i++) _sectorMask = (_sectorMask << 1) + 1;
AaruLogging.Debug(MODULE_NAME, "qHdr.l1Mask = {0:X}", _l1Mask);
AaruLogging.Debug(MODULE_NAME, "qHdr.l1Shift = {0}", _l1Shift);
@@ -216,9 +216,9 @@ public sealed partial class Qcow
if((long)l1Off >= _l1Table.LongLength)
{
AaruLogging.Debug(MODULE_NAME,
string.Format(Localization.Trying_to_read_past_L1_table_position_0_of_a_max_1,
l1Off,
_l1Table.LongLength));
string.Format(Localization.Trying_to_read_past_L1_table_position_0_of_a_max_1,
l1Off,
_l1Table.LongLength));
return ErrorNumber.InvalidArgument;
}
@@ -234,7 +234,7 @@ public sealed partial class Qcow
if(!_l2TableCache.TryGetValue(l1Off, out ulong[] l2Table))
{
_imageStream.Seek((long)_l1Table[l1Off], SeekOrigin.Begin);
byte[] l2TableB = new byte[_l2Size * 8];
var l2TableB = new byte[_l2Size * 8];
_imageStream.EnsureRead(l2TableB, 0, _l2Size * 8);
AaruLogging.Debug(MODULE_NAME, Localization.Reading_L2_table_0, l1Off);
l2Table = MemoryMarshal.Cast<byte, ulong>(l2TableB).ToArray();
@@ -265,7 +265,7 @@ public sealed partial class Qcow
ulong realOff = offset & offMask;
ulong compSize = (offset & compSizeMask) >> 63 - _qHdr.cluster_bits;
byte[] zCluster = new byte[compSize];
var zCluster = new byte[compSize];
_imageStream.Seek((long)realOff, SeekOrigin.Begin);
_imageStream.EnsureRead(zCluster, 0, (int)compSize);

View File

@@ -31,6 +31,7 @@
// ****************************************************************************/
using System.Runtime.InteropServices;
using Aaru.CommonTypes.Attributes;
namespace Aaru.Images;
@@ -40,7 +41,8 @@ public sealed partial class Qcow
/// <summary>QCOW header, big-endian</summary>
[StructLayout(LayoutKind.Sequential, Pack = 1)]
struct Header
[SwapEndian]
partial struct Header
{
/// <summary>
/// <see cref="Qcow.QCOW_MAGIC" />
@@ -49,9 +51,9 @@ public sealed partial class Qcow
/// <summary>Must be 1</summary>
public uint version;
/// <summary>Offset inside file to string containing backing file</summary>
public readonly ulong backing_file_offset;
public ulong backing_file_offset;
/// <summary>Size of <see cref="backing_file_offset" /></summary>
public readonly uint backing_file_size;
public uint backing_file_size;
/// <summary>Modification time</summary>
public uint mtime;
/// <summary>Size in bytes</summary>
@@ -61,9 +63,9 @@ public sealed partial class Qcow
/// <summary>L2 table bits</summary>
public byte l2_bits;
/// <summary>Padding</summary>
public readonly ushort padding;
public ushort padding;
/// <summary>Encryption method</summary>
public readonly uint crypt_method;
public uint crypt_method;
/// <summary>Offset to L1 table</summary>
public ulong l1_table_offset;
}