From 8a77a8a0099636fc0214831f9b4e6b1636166d53 Mon Sep 17 00:00:00 2001 From: Matt Nadareski Date: Fri, 9 Dec 2022 14:59:31 -0800 Subject: [PATCH] Simplify PE section helper methods --- BurnOutSharp.Wrappers/PortableExecutable.cs | 136 +------------------- 1 file changed, 6 insertions(+), 130 deletions(-) diff --git a/BurnOutSharp.Wrappers/PortableExecutable.cs b/BurnOutSharp.Wrappers/PortableExecutable.cs index e15a45d9..56c7fd0d 100644 --- a/BurnOutSharp.Wrappers/PortableExecutable.cs +++ b/BurnOutSharp.Wrappers/PortableExecutable.cs @@ -3032,34 +3032,7 @@ namespace BurnOutSharp.Wrappers // Get the first index of the section int index = Array.IndexOf(SectionNames, name); - if (index == -1) - return null; - - // Get the section data from the table - var section = SectionTable[index]; - uint address = section.VirtualAddress.ConvertVirtualAddress(SectionTable); - if (address == 0) - return null; - - // Set the section size - uint size = section.SizeOfRawData; - lock (_sourceDataLock) - { - // Create the section data array if we have to - if (_sectionData == null) - _sectionData = new byte[SectionNames.Length][]; - - // If we already have cached data, just use that immediately - if (_sectionData[index] != null) - return _sectionData[index]; - - // Populate the raw section data based on the source - byte[] sectionData = ReadFromDataSource((int)address, (int)size); - - // Cache and return the section data, even if null - _sectionData[index] = sectionData; - return sectionData; - } + return GetSectionData(index); } /// @@ -3081,22 +3054,7 @@ namespace BurnOutSharp.Wrappers // Get the first index of the section int index = Array.IndexOf(SectionNames, name); - if (index == -1) - return null; - - // Get the section data from the table - var section = SectionTable[index]; - uint address = section.VirtualAddress.ConvertVirtualAddress(SectionTable); - if (address == 0) - return null; - - // Set the section size - uint size = section.SizeOfRawData; - lock (_sourceDataLock) - { - // Immediately return the data without caching - return ReadFromDataSource((int)address + offset, (int)size - offset); - } + return GetSectionData(index); } /// @@ -3117,34 +3075,7 @@ namespace BurnOutSharp.Wrappers // Get the last index of the section int index = Array.LastIndexOf(SectionNames, name); - if (index == -1) - return null; - - // Get the section data from the table - var section = SectionTable[index]; - uint address = section.VirtualAddress.ConvertVirtualAddress(SectionTable); - if (address == 0) - return null; - - // Set the section size - uint size = section.SizeOfRawData; - lock (_sourceDataLock) - { - // Create the section data array if we have to - if (_sectionData == null) - _sectionData = new byte[SectionNames.Length][]; - - // If we already have cached data, just use that immediately - if (_sectionData[index] != null) - return _sectionData[index]; - - // Populate the raw section data based on the source - byte[] sectionData = ReadFromDataSource((int)address, (int)size); - - // Cache and return the section data, even if null - _sectionData[index] = sectionData; - return sectionData; - } + return GetSectionData(index); } /// @@ -3207,34 +3138,7 @@ namespace BurnOutSharp.Wrappers // Get the first index of the section int index = Array.IndexOf(SectionNames, name); - if (index == -1) - return null; - - // Get the section data from the table - var section = SectionTable[index]; - uint address = section.VirtualAddress.ConvertVirtualAddress(SectionTable); - if (address == 0) - return null; - - // Set the section size - uint size = section.SizeOfRawData; - lock (_sourceDataLock) - { - // Create the section string array if we have to - if (_sectionStringData == null) - _sectionStringData = new List[SectionNames.Length]; - - // If we already have cached data, just use that immediately - if (_sectionStringData[index] != null) - return _sectionStringData[index]; - - // Populate the section string data based on the source - List sectionStringData = ReadStringsFromDataSource((int)address, (int)size); - - // Cache and return the section string data, even if null - _sectionStringData[index] = sectionStringData; - return sectionStringData; - } + return GetSectionStrings(index); } /// @@ -3255,41 +3159,13 @@ namespace BurnOutSharp.Wrappers // Get the last index of the section int index = Array.LastIndexOf(SectionNames, name); - if (index == -1) - return null; - - // Get the section data from the table - var section = SectionTable[index]; - uint address = section.VirtualAddress.ConvertVirtualAddress(SectionTable); - if (address == 0) - return null; - - // Set the section size - uint size = section.SizeOfRawData; - lock (_sourceDataLock) - { - // Create the section string array if we have to - if (_sectionStringData == null) - _sectionStringData = new List[SectionNames.Length]; - - // If we already have cached data, just use that immediately - if (_sectionStringData[index] != null) - return _sectionStringData[index]; - - // Populate the section string data based on the source - List sectionStringData = ReadStringsFromDataSource((int)address, (int)size); - - // Cache and return the section string data, even if null - _sectionStringData[index] = sectionStringData; - return sectionStringData; - } + return GetSectionStrings(index); } /// /// Get the section strings based on index, if possible /// - /// Name of the section to check for - /// True to enable exact matching of names, false for starts-with + /// Index of the section to check for /// Section strings on success, null on error public List GetSectionStrings(int index) {