mirror of
https://github.com/SabreTools/SabreTools.Serialization.git
synced 2026-07-09 02:16:55 +00:00
Libraries
This change looks dramatic, but it's just separating out the already-split namespaces into separate top-level folders. In theory, every single one could be built into their own Nuget package. `SabreTools.Serialization` still builds the normal Nuget package that is used by all other projects and includes all namespaces.
This commit is contained in:
97
SabreTools.Data.Extensions/PortableExecutableExtensions.cs
Normal file
97
SabreTools.Data.Extensions/PortableExecutableExtensions.cs
Normal file
@@ -0,0 +1,97 @@
|
||||
using System;
|
||||
using SabreTools.Data.Models.COFF;
|
||||
|
||||
namespace SabreTools.Data.Extensions
|
||||
{
|
||||
public static class PortableExecutableExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Convert a relative virtual address to a physical one
|
||||
/// </summary>
|
||||
/// <param name="rva">Relative virtual address to convert</param>
|
||||
/// <param name="sections">Array of sections to check against</param>
|
||||
/// <returns>Physical address, 0 on error</returns>
|
||||
public static uint ConvertVirtualAddress(this uint rva, SectionHeader[] sections)
|
||||
{
|
||||
// If we have an invalid section table, we can't do anything
|
||||
if (sections.Length == 0)
|
||||
return 0;
|
||||
|
||||
// If the RVA is 0, we just return 0 because it's invalid
|
||||
if (rva == 0)
|
||||
return 0;
|
||||
|
||||
// If the RVA matches a section start exactly, use that
|
||||
var matchingSection = Array.Find(sections, s => s.VirtualAddress == rva);
|
||||
if (matchingSection is not null)
|
||||
return rva - matchingSection.VirtualAddress + matchingSection.PointerToRawData;
|
||||
|
||||
// Loop through all of the sections
|
||||
uint maxVirtualAddress = 0, maxRawPointer = 0;
|
||||
for (int i = 0; i < sections.Length; i++)
|
||||
{
|
||||
// If the section "starts" at 0, just skip it
|
||||
var section = sections[i];
|
||||
if (section.PointerToRawData == 0)
|
||||
continue;
|
||||
|
||||
// If the virtual address is greater than the RVA
|
||||
if (rva < section.VirtualAddress)
|
||||
continue;
|
||||
|
||||
// Cache the maximum matching section data, in case of a miss
|
||||
if (rva >= section.VirtualAddress)
|
||||
{
|
||||
maxVirtualAddress = section.VirtualAddress;
|
||||
maxRawPointer = section.PointerToRawData;
|
||||
}
|
||||
|
||||
// Attempt to derive the physical address from the current section
|
||||
if (section.VirtualSize != 0 && rva <= section.VirtualAddress + section.VirtualSize)
|
||||
return rva - section.VirtualAddress + section.PointerToRawData;
|
||||
else if (section.SizeOfRawData != 0 && rva <= section.VirtualAddress + section.SizeOfRawData)
|
||||
return rva - section.VirtualAddress + section.PointerToRawData;
|
||||
}
|
||||
|
||||
return maxRawPointer != 0 ? rva - maxVirtualAddress + maxRawPointer : 0;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Find the section a revlative virtual address lives in
|
||||
/// </summary>
|
||||
/// <param name="rva">Relative virtual address to convert</param>
|
||||
/// <param name="sections">Array of sections to check against</param>
|
||||
/// <returns>Section index, null on error</returns>
|
||||
public static int ContainingSectionIndex(this uint rva, SectionHeader[] sections)
|
||||
{
|
||||
// If we have an invalid section table, we can't do anything
|
||||
if (sections is null || sections.Length == 0)
|
||||
return -1;
|
||||
|
||||
// If the RVA is 0, we just return -1 because it's invalid
|
||||
if (rva == 0)
|
||||
return -1;
|
||||
|
||||
// Loop through all of the sections
|
||||
for (int i = 0; i < sections.Length; i++)
|
||||
{
|
||||
// If the section "starts" at 0, just skip it
|
||||
var section = sections[i];
|
||||
if (section.PointerToRawData == 0)
|
||||
continue;
|
||||
|
||||
// If the virtual address is greater than the RVA
|
||||
if (rva < section.VirtualAddress)
|
||||
continue;
|
||||
|
||||
// Attempt to derive the physical address from the current section
|
||||
if (section.VirtualSize != 0 && rva <= section.VirtualAddress + section.VirtualSize)
|
||||
return i;
|
||||
else if (section.SizeOfRawData != 0 && rva <= section.VirtualAddress + section.SizeOfRawData)
|
||||
return i;
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user