From 2a6a7b5e9a19b250fbab463bf9523328b66a0bb9 Mon Sep 17 00:00:00 2001 From: Matt Nadareski Date: Thu, 11 Sep 2025 08:14:24 -0400 Subject: [PATCH] Pass in the correct data --- .../Deserializers/PortableExecutable.cs | 25 ++++++------------- 1 file changed, 8 insertions(+), 17 deletions(-) diff --git a/SabreTools.Serialization/Deserializers/PortableExecutable.cs b/SabreTools.Serialization/Deserializers/PortableExecutable.cs index 2fea7d57..088d1ee1 100644 --- a/SabreTools.Serialization/Deserializers/PortableExecutable.cs +++ b/SabreTools.Serialization/Deserializers/PortableExecutable.cs @@ -135,11 +135,8 @@ namespace SabreTools.Serialization.Deserializers long paddingSize = optionalHeader.FileAlignment - ((offset + tableSize) % optionalHeader.FileAlignment); tableSize += (int)paddingSize; - // Seek to the export table - data.Seek(offset, SeekOrigin.Begin); - // Read the table data - byte[] tableData = data.ReadBytes(tableSize); + byte[]? tableData = data.ReadFrom(offset, tableSize, retainPosition: true); // Set the export table pex.ExportTable = ParseExportTable(tableData, data, initialOffset, pex.SectionTable); @@ -162,11 +159,8 @@ namespace SabreTools.Serialization.Deserializers long paddingSize = optionalHeader.FileAlignment - ((offset + tableSize) % optionalHeader.FileAlignment); tableSize += (int)paddingSize; - // Seek to the import table - data.Seek(offset, SeekOrigin.Begin); - // Read the table data - byte[] tableData = data.ReadBytes(tableSize); + byte[]? tableData = data.ReadFrom(offset, tableSize, retainPosition: true); // Set the import table pex.ImportTable = ParseImportTable(tableData, data, initialOffset, optionalHeader.Magic, pex.SectionTable); @@ -189,11 +183,8 @@ namespace SabreTools.Serialization.Deserializers long paddingSize = optionalHeader.FileAlignment - ((offset + tableSize) % optionalHeader.FileAlignment); tableSize += (int)paddingSize; - // Seek to the resource directory table - data.Seek(offset, SeekOrigin.Begin); - // Read the table data - byte[] tableData = data.ReadBytes(tableSize); + byte[]? tableData = data.ReadFrom(offset, tableSize, retainPosition: true); // Set the resource directory table long tableStart = data.Position; @@ -250,7 +241,7 @@ namespace SabreTools.Serialization.Deserializers tableSize += (int)paddingSize; // Read the table data - byte[] tableData = data.ReadBytes(tableSize); + byte[]? tableData = data.ReadFrom(offset, tableSize, retainPosition: true); // Set the attribute certificate table pex.AttributeCertificateTable = ParseAttributeCertificateTable(tableData); @@ -274,7 +265,7 @@ namespace SabreTools.Serialization.Deserializers tableSize += (int)paddingSize; // Read the table data - byte[] tableData = data.ReadBytes(tableSize); + byte[]? tableData = data.ReadFrom(offset, tableSize, retainPosition: true); // Set the base relocation table pex.BaseRelocationTable = ParseBaseRelocationTable(tableData); @@ -810,7 +801,7 @@ namespace SabreTools.Serialization.Deserializers /// Initial offset to use in address comparisons /// Section table to use for virtual address translation /// Filled ExportTable on success, null on error - public static ExportTable ParseExportTable(byte[] tableData, Stream data, long initialOffset, SectionHeader[] sections) + public static ExportTable ParseExportTable(byte[]? tableData, Stream data, long initialOffset, SectionHeader[] sections) { var obj = new ExportTable(); @@ -1359,7 +1350,7 @@ namespace SabreTools.Serialization.Deserializers /// Optional header magic number indicating PE32 or PE32+ /// Section table to use for virtual address translation /// Filled import table on success, null on error - public static ImportTable ParseImportTable(byte[] tableData, Stream data, long initialOffset, OptionalHeaderMagicNumber magic, SectionHeader[] sections) + public static ImportTable ParseImportTable(byte[]? tableData, Stream data, long initialOffset, OptionalHeaderMagicNumber magic, SectionHeader[] sections) { var obj = new ImportTable(); @@ -1602,7 +1593,7 @@ namespace SabreTools.Serialization.Deserializers /// Table start address for relative reads /// Section table to use for virtual address translation /// Filled ResourceDirectoryTable on success, null on error - public static ResourceDirectoryTable? ParseResourceDirectoryTable(byte[] tableData, + public static ResourceDirectoryTable? ParseResourceDirectoryTable(byte[]? tableData, Stream data, long initialOffset, long tableStart,