Use Stream extension to ensure read operations return the requested number of bytes (unless EOF arrives first).

This commit is contained in:
2022-11-14 09:43:16 +00:00
parent f90cc6593f
commit 0eb589d785
178 changed files with 862 additions and 780 deletions

View File

@@ -238,7 +238,7 @@ public sealed partial class AaruFormat
if(_imageStream.Length > Marshal.SizeOf<AaruHeader>())
{
_structureBytes = new byte[Marshal.SizeOf<AaruHeader>()];
_imageStream.Read(_structureBytes, 0, _structureBytes.Length);
_imageStream.EnsureRead(_structureBytes, 0, _structureBytes.Length);
_header = Marshal.ByteArrayToStructureLittleEndian<AaruHeader>(_structureBytes);
if(_header.identifier != DIC_MAGIC &&
@@ -308,7 +308,7 @@ public sealed partial class AaruFormat
_imageStream.Position = (long)_header.indexOffset;
_structureBytes = new byte[Marshal.SizeOf<IndexHeader>()];
_imageStream.Read(_structureBytes, 0, _structureBytes.Length);
_imageStream.EnsureRead(_structureBytes, 0, _structureBytes.Length);
IndexHeader idxHeader = Marshal.SpanToStructureLittleEndian<IndexHeader>(_structureBytes);
if(idxHeader.identifier != BlockType.Index)
@@ -324,7 +324,7 @@ public sealed partial class AaruFormat
for(ushort i = 0; i < idxHeader.entries; i++)
{
_structureBytes = new byte[Marshal.SizeOf<IndexEntry>()];
_imageStream.Read(_structureBytes, 0, _structureBytes.Length);
_imageStream.EnsureRead(_structureBytes, 0, _structureBytes.Length);
IndexEntry entry = Marshal.SpanToStructureLittleEndian<IndexEntry>(_structureBytes);
AaruConsole.DebugWriteLine("Aaru Format plugin",
@@ -353,7 +353,7 @@ public sealed partial class AaruFormat
_imageStream.Position = (long)entry.offset;
_structureBytes = new byte[Marshal.SizeOf<BlockHeader>()];
_imageStream.Read(_structureBytes, 0, _structureBytes.Length);
_imageStream.EnsureRead(_structureBytes, 0, _structureBytes.Length);
BlockHeader blockHeader = Marshal.SpanToStructureLittleEndian<BlockHeader>(_structureBytes);
_imageInfo.ImageSize += blockHeader.cmpLength;
@@ -404,8 +404,8 @@ public sealed partial class AaruFormat
DateTime startDecompress = DateTime.Now;
var compressedTag = new byte[blockHeader.cmpLength - LZMA_PROPERTIES_LENGTH];
var lzmaProperties = new byte[LZMA_PROPERTIES_LENGTH];
_imageStream.Read(lzmaProperties, 0, LZMA_PROPERTIES_LENGTH);
_imageStream.Read(compressedTag, 0, compressedTag.Length);
_imageStream.EnsureRead(lzmaProperties, 0, LZMA_PROPERTIES_LENGTH);
_imageStream.EnsureRead(compressedTag, 0, compressedTag.Length);
data = new byte[blockHeader.length];
int decompressedLength = LZMA.DecodeBuffer(compressedTag, data, lzmaProperties);
@@ -431,7 +431,7 @@ public sealed partial class AaruFormat
else if(blockHeader.compression == CompressionType.None)
{
data = new byte[blockHeader.length];
_imageStream.Read(data, 0, (int)blockHeader.length);
_imageStream.EnsureRead(data, 0, (int)blockHeader.length);
AaruConsole.DebugWriteLine("Aaru Format plugin", "Memory snapshot: {0} bytes",
GC.GetTotalMemory(false));
@@ -588,7 +588,7 @@ public sealed partial class AaruFormat
if(entry.dataType == DataType.UserData)
{
_structureBytes = new byte[Marshal.SizeOf<DdtHeader>()];
_imageStream.Read(_structureBytes, 0, _structureBytes.Length);
_imageStream.EnsureRead(_structureBytes, 0, _structureBytes.Length);
DdtHeader ddtHeader = Marshal.ByteArrayToStructureLittleEndian<DdtHeader>(_structureBytes);
@@ -616,8 +616,8 @@ public sealed partial class AaruFormat
var compressedDdt = new byte[ddtHeader.cmpLength - LZMA_PROPERTIES_LENGTH];
var lzmaProperties = new byte[LZMA_PROPERTIES_LENGTH];
_imageStream.Read(lzmaProperties, 0, LZMA_PROPERTIES_LENGTH);
_imageStream.Read(compressedDdt, 0, compressedDdt.Length);
_imageStream.EnsureRead(lzmaProperties, 0, LZMA_PROPERTIES_LENGTH);
_imageStream.EnsureRead(compressedDdt, 0, compressedDdt.Length);
var decompressedDdt = new byte[ddtHeader.length];
var decompressedLength =
@@ -667,7 +667,7 @@ public sealed partial class AaruFormat
else if(entry.dataType is DataType.CdSectorPrefixCorrected or DataType.CdSectorSuffixCorrected)
{
_structureBytes = new byte[Marshal.SizeOf<DdtHeader>()];
_imageStream.Read(_structureBytes, 0, _structureBytes.Length);
_imageStream.EnsureRead(_structureBytes, 0, _structureBytes.Length);
DdtHeader ddtHeader = Marshal.ByteArrayToStructureLittleEndian<DdtHeader>(_structureBytes);
@@ -694,8 +694,8 @@ public sealed partial class AaruFormat
var compressedDdt = new byte[ddtHeader.cmpLength - LZMA_PROPERTIES_LENGTH];
var lzmaProperties = new byte[LZMA_PROPERTIES_LENGTH];
_imageStream.Read(lzmaProperties, 0, LZMA_PROPERTIES_LENGTH);
_imageStream.Read(compressedDdt, 0, compressedDdt.Length);
_imageStream.EnsureRead(lzmaProperties, 0, LZMA_PROPERTIES_LENGTH);
_imageStream.EnsureRead(compressedDdt, 0, compressedDdt.Length);
var decompressedLength =
(ulong)LZMA.DecodeBuffer(compressedDdt, decompressedDdt, lzmaProperties);
@@ -716,7 +716,7 @@ public sealed partial class AaruFormat
break;
case CompressionType.None:
_imageStream.Read(decompressedDdt, 0, decompressedDdt.Length);
_imageStream.EnsureRead(decompressedDdt, 0, decompressedDdt.Length);
break;
default:
@@ -746,7 +746,7 @@ public sealed partial class AaruFormat
// Logical geometry block. It doesn't have a CRC coz, well, it's not so important
case BlockType.GeometryBlock:
_structureBytes = new byte[Marshal.SizeOf<GeometryBlock>()];
_imageStream.Read(_structureBytes, 0, _structureBytes.Length);
_imageStream.EnsureRead(_structureBytes, 0, _structureBytes.Length);
_geometryBlock = Marshal.SpanToStructureLittleEndian<GeometryBlock>(_structureBytes);
if(_geometryBlock.identifier == BlockType.GeometryBlock)
@@ -769,7 +769,7 @@ public sealed partial class AaruFormat
// Metadata block
case BlockType.MetadataBlock:
_structureBytes = new byte[Marshal.SizeOf<MetadataBlock>()];
_imageStream.Read(_structureBytes, 0, _structureBytes.Length);
_imageStream.EnsureRead(_structureBytes, 0, _structureBytes.Length);
MetadataBlock metadataBlock =
Marshal.SpanToStructureLittleEndian<MetadataBlock>(_structureBytes);
@@ -788,7 +788,7 @@ public sealed partial class AaruFormat
var metadata = new byte[metadataBlock.blockSize];
_imageStream.Position = (long)entry.offset;
_imageStream.Read(metadata, 0, metadata.Length);
_imageStream.EnsureRead(metadata, 0, metadata.Length);
if(metadataBlock.mediaSequence > 0 &&
metadataBlock.lastMediaSequence > 0)
@@ -945,7 +945,7 @@ public sealed partial class AaruFormat
// Optical disc tracks block
case BlockType.TracksBlock:
_structureBytes = new byte[Marshal.SizeOf<TracksHeader>()];
_imageStream.Read(_structureBytes, 0, _structureBytes.Length);
_imageStream.EnsureRead(_structureBytes, 0, _structureBytes.Length);
TracksHeader tracksHeader = Marshal.SpanToStructureLittleEndian<TracksHeader>(_structureBytes);
@@ -959,7 +959,7 @@ public sealed partial class AaruFormat
}
_structureBytes = new byte[Marshal.SizeOf<TrackEntry>() * tracksHeader.entries];
_imageStream.Read(_structureBytes, 0, _structureBytes.Length);
_imageStream.EnsureRead(_structureBytes, 0, _structureBytes.Length);
Crc64Context.Data(_structureBytes, out byte[] trksCrc);
if(BitConverter.ToUInt64(trksCrc, 0) != tracksHeader.crc64)
@@ -983,7 +983,7 @@ public sealed partial class AaruFormat
for(ushort i = 0; i < tracksHeader.entries; i++)
{
_structureBytes = new byte[Marshal.SizeOf<TrackEntry>()];
_imageStream.Read(_structureBytes, 0, _structureBytes.Length);
_imageStream.EnsureRead(_structureBytes, 0, _structureBytes.Length);
TrackEntry trackEntry =
Marshal.ByteArrayToStructureLittleEndian<TrackEntry>(_structureBytes);
@@ -1027,7 +1027,7 @@ public sealed partial class AaruFormat
// CICM XML metadata block
case BlockType.CicmBlock:
_structureBytes = new byte[Marshal.SizeOf<CicmMetadataBlock>()];
_imageStream.Read(_structureBytes, 0, _structureBytes.Length);
_imageStream.EnsureRead(_structureBytes, 0, _structureBytes.Length);
CicmMetadataBlock cicmBlock =
Marshal.SpanToStructureLittleEndian<CicmMetadataBlock>(_structureBytes);
@@ -1039,7 +1039,7 @@ public sealed partial class AaruFormat
"Found CICM XML metadata block at position {0}", entry.offset);
var cicmBytes = new byte[cicmBlock.length];
_imageStream.Read(cicmBytes, 0, cicmBytes.Length);
_imageStream.EnsureRead(cicmBytes, 0, cicmBytes.Length);
var cicmMs = new MemoryStream(cicmBytes);
var cicmXs = new XmlSerializer(typeof(CICMMetadataType));
@@ -1062,7 +1062,7 @@ public sealed partial class AaruFormat
// Dump hardware block
case BlockType.DumpHardwareBlock:
_structureBytes = new byte[Marshal.SizeOf<DumpHardwareHeader>()];
_imageStream.Read(_structureBytes, 0, _structureBytes.Length);
_imageStream.EnsureRead(_structureBytes, 0, _structureBytes.Length);
DumpHardwareHeader dumpBlock =
Marshal.SpanToStructureLittleEndian<DumpHardwareHeader>(_structureBytes);
@@ -1074,7 +1074,7 @@ public sealed partial class AaruFormat
entry.offset);
_structureBytes = new byte[dumpBlock.length];
_imageStream.Read(_structureBytes, 0, _structureBytes.Length);
_imageStream.EnsureRead(_structureBytes, 0, _structureBytes.Length);
Crc64Context.Data(_structureBytes, out byte[] dumpCrc);
if(BitConverter.ToUInt64(dumpCrc, 0) != dumpBlock.crc64)
@@ -1093,7 +1093,7 @@ public sealed partial class AaruFormat
for(ushort i = 0; i < dumpBlock.entries; i++)
{
_structureBytes = new byte[Marshal.SizeOf<DumpHardwareEntry>()];
_imageStream.Read(_structureBytes, 0, _structureBytes.Length);
_imageStream.EnsureRead(_structureBytes, 0, _structureBytes.Length);
DumpHardwareEntry dumpEntry =
Marshal.SpanToStructureLittleEndian<DumpHardwareEntry>(_structureBytes);
@@ -1109,7 +1109,7 @@ public sealed partial class AaruFormat
if(dumpEntry.manufacturerLength > 0)
{
tmp = new byte[dumpEntry.manufacturerLength - 1];
_imageStream.Read(tmp, 0, tmp.Length);
_imageStream.EnsureRead(tmp, 0, tmp.Length);
_imageStream.Position += 1;
dump.Manufacturer = Encoding.UTF8.GetString(tmp);
}
@@ -1117,7 +1117,7 @@ public sealed partial class AaruFormat
if(dumpEntry.modelLength > 0)
{
tmp = new byte[dumpEntry.modelLength - 1];
_imageStream.Read(tmp, 0, tmp.Length);
_imageStream.EnsureRead(tmp, 0, tmp.Length);
_imageStream.Position += 1;
dump.Model = Encoding.UTF8.GetString(tmp);
}
@@ -1125,7 +1125,7 @@ public sealed partial class AaruFormat
if(dumpEntry.revisionLength > 0)
{
tmp = new byte[dumpEntry.revisionLength - 1];
_imageStream.Read(tmp, 0, tmp.Length);
_imageStream.EnsureRead(tmp, 0, tmp.Length);
_imageStream.Position += 1;
dump.Revision = Encoding.UTF8.GetString(tmp);
}
@@ -1133,7 +1133,7 @@ public sealed partial class AaruFormat
if(dumpEntry.firmwareLength > 0)
{
tmp = new byte[dumpEntry.firmwareLength - 1];
_imageStream.Read(tmp, 0, tmp.Length);
_imageStream.EnsureRead(tmp, 0, tmp.Length);
_imageStream.Position += 1;
dump.Firmware = Encoding.UTF8.GetString(tmp);
}
@@ -1141,7 +1141,7 @@ public sealed partial class AaruFormat
if(dumpEntry.serialLength > 0)
{
tmp = new byte[dumpEntry.serialLength - 1];
_imageStream.Read(tmp, 0, tmp.Length);
_imageStream.EnsureRead(tmp, 0, tmp.Length);
_imageStream.Position += 1;
dump.Serial = Encoding.UTF8.GetString(tmp);
}
@@ -1149,7 +1149,7 @@ public sealed partial class AaruFormat
if(dumpEntry.softwareNameLength > 0)
{
tmp = new byte[dumpEntry.softwareNameLength - 1];
_imageStream.Read(tmp, 0, tmp.Length);
_imageStream.EnsureRead(tmp, 0, tmp.Length);
_imageStream.Position += 1;
dump.Software.Name = Encoding.UTF8.GetString(tmp);
}
@@ -1157,7 +1157,7 @@ public sealed partial class AaruFormat
if(dumpEntry.softwareVersionLength > 0)
{
tmp = new byte[dumpEntry.softwareVersionLength - 1];
_imageStream.Read(tmp, 0, tmp.Length);
_imageStream.EnsureRead(tmp, 0, tmp.Length);
_imageStream.Position += 1;
dump.Software.Version = Encoding.UTF8.GetString(tmp);
}
@@ -1166,7 +1166,7 @@ public sealed partial class AaruFormat
{
tmp = new byte[dumpEntry.softwareOperatingSystemLength - 1];
_imageStream.Position += 1;
_imageStream.Read(tmp, 0, tmp.Length);
_imageStream.EnsureRead(tmp, 0, tmp.Length);
dump.Software.OperatingSystem = Encoding.UTF8.GetString(tmp);
}
@@ -1174,7 +1174,7 @@ public sealed partial class AaruFormat
for(uint j = 0; j < dumpEntry.extents; j++)
{
_imageStream.Read(tmp, 0, tmp.Length);
_imageStream.EnsureRead(tmp, 0, tmp.Length);
dump.Extents[j] = new ExtentType
{
@@ -1197,7 +1197,7 @@ public sealed partial class AaruFormat
// Tape partition block
case BlockType.TapePartitionBlock:
_structureBytes = new byte[Marshal.SizeOf<TapePartitionHeader>()];
_imageStream.Read(_structureBytes, 0, _structureBytes.Length);
_imageStream.EnsureRead(_structureBytes, 0, _structureBytes.Length);
TapePartitionHeader partitionHeader =
Marshal.SpanToStructureLittleEndian<TapePartitionHeader>(_structureBytes);
@@ -1209,7 +1209,7 @@ public sealed partial class AaruFormat
entry.offset);
var tapePartitionBytes = new byte[partitionHeader.length];
_imageStream.Read(tapePartitionBytes, 0, tapePartitionBytes.Length);
_imageStream.EnsureRead(tapePartitionBytes, 0, tapePartitionBytes.Length);
Span<TapePartitionEntry> tapePartitions =
MemoryMarshal.Cast<byte, TapePartitionEntry>(tapePartitionBytes);
@@ -1231,7 +1231,7 @@ public sealed partial class AaruFormat
// Tape file block
case BlockType.TapeFileBlock:
_structureBytes = new byte[Marshal.SizeOf<TapeFileHeader>()];
_imageStream.Read(_structureBytes, 0, _structureBytes.Length);
_imageStream.EnsureRead(_structureBytes, 0, _structureBytes.Length);
TapeFileHeader fileHeader =
Marshal.SpanToStructureLittleEndian<TapeFileHeader>(_structureBytes);
@@ -1243,7 +1243,7 @@ public sealed partial class AaruFormat
entry.offset);
var tapeFileBytes = new byte[fileHeader.length];
_imageStream.Read(tapeFileBytes, 0, tapeFileBytes.Length);
_imageStream.EnsureRead(tapeFileBytes, 0, tapeFileBytes.Length);
Span<TapeFileEntry> tapeFiles = MemoryMarshal.Cast<byte, TapeFileEntry>(tapeFileBytes);
Files = new List<TapeFile>();
@@ -1263,7 +1263,7 @@ public sealed partial class AaruFormat
// Optical disc tracks block
case BlockType.CompactDiscIndexesBlock:
_structureBytes = new byte[Marshal.SizeOf<CompactDiscIndexesHeader>()];
_imageStream.Read(_structureBytes, 0, _structureBytes.Length);
_imageStream.EnsureRead(_structureBytes, 0, _structureBytes.Length);
CompactDiscIndexesHeader indexesHeader =
Marshal.SpanToStructureLittleEndian<CompactDiscIndexesHeader>(_structureBytes);
@@ -1278,7 +1278,7 @@ public sealed partial class AaruFormat
}
_structureBytes = new byte[Marshal.SizeOf<CompactDiscIndexEntry>() * indexesHeader.entries];
_imageStream.Read(_structureBytes, 0, _structureBytes.Length);
_imageStream.EnsureRead(_structureBytes, 0, _structureBytes.Length);
Crc64Context.Data(_structureBytes, out byte[] idsxCrc);
if(BitConverter.ToUInt64(idsxCrc, 0) != indexesHeader.crc64)
@@ -1301,7 +1301,7 @@ public sealed partial class AaruFormat
for(ushort i = 0; i < indexesHeader.entries; i++)
{
_structureBytes = new byte[Marshal.SizeOf<CompactDiscIndexEntry>()];
_imageStream.Read(_structureBytes, 0, _structureBytes.Length);
_imageStream.EnsureRead(_structureBytes, 0, _structureBytes.Length);
compactDiscIndexes.Add(Marshal.
ByteArrayToStructureLittleEndian<