using System;
namespace QuickLook.Plugin.PEViewer.PEImageParser;
///
/// Represents the base class for PE image optional headers. This is an abstract class.
///
public abstract class ImageOptionalHeader
{
///
/// Gets the linker major version number.
///
public byte MajorLinkerVersion { get; internal set; }
///
/// Gets the linker minor version number.
///
public byte MinorLinkerVersion { get; internal set; }
///
/// Gets the size of the code (text) section, or the sum of all code sections if there are multiple sections.
///
public uint SizeOfCode { get; internal set; }
///
/// Gets the size of the initialized data section, or the sum of all such sections if there are multiple data sections.
///
public uint SizeOfInitializedData { get; internal set; }
///
/// Gets the size of the uninitialized data section (BSS), or the sum of all such sections if there are multiple BSS sections.
///
public uint SizeOfUninitializedData { get; internal set; }
///
/// Gets the address of the entry point relative to the image base when the executable file is loaded into memory. For program images, this is the starting address. For device drivers, this is the address of the initialization function. An entry point is optional for DLLs. When no entry point is present, this property must be zero.
///
public uint AddressOfEntryPoint { get; internal set; }
///
/// Gets the address that is relative to the image base of the beginning-of-code section when it is loaded into memory.
///
public uint BaseOfCode { get; internal set; }
///
/// Gets the alignment (in bytes) of sections when they are loaded into memory. It must be greater than or equal to . The default is the page size for the architecture.
///
public uint SectionAlignment { get; internal set; }
///
/// Gets the alignment factor (in bytes) that is used to align the raw data of sections in the image file. The value should be a power of 2 between 512 and 64 K, inclusive. The default is 512. If the is less than the architecture's page size, then must match .
///
public uint FileAlignment { get; internal set; }
///
/// Gets the major version number of the required operating system.
///
public ushort MajorOperatingSystemVersion { get; internal set; }
///
/// Gets the minor version number of the required operating system.
///
public ushort MinorOperatingSystemVersion { get; internal set; }
///
/// Gets the major version number of the image.
///
public ushort MajorImageVersion { get; internal set; }
///
/// Gets the minor version number of the image.
///
public ushort MinorImageVersion { get; internal set; }
///
/// Gets the major version number of the subsystem.
///
public ushort MajorSubsystemVersion { get; internal set; }
///
/// Gets the minor version number of the subsystem.
///
public ushort MinorSubsystemVersion { get; internal set; }
///
/// Reserved, must be zero.
///
public uint Win32VersionValue { get; internal set; }
///
/// Gets the size (in bytes) of the image, including all headers, as the image is loaded in memory. It must be a multiple of .
///
public uint SizeOfImage { get; internal set; }
///
/// Gets the combined size of an MS-DOS stub, PE header, and section headers rounded up to a multiple of .
///
public uint SizeOfHeaders { get; internal set; }
///
/// Gets the image file checksum. The algorithm for computing the checksum is incorporated into IMAGHELP.DLL. The following are checked for validation at load time: all drivers, any DLL loaded at boot time, and any DLL that is loaded into a critical Windows process.
///
public uint Checksum { get; internal set; }
///
/// Gets the subsystem that is required to run this image.
///
public ImageSubsystem Subsystem { get; internal set; }
///
/// Gets the "DllCharacteristics" attribute of the PE image optional header.
///
public ImageDllCharacteristics DllCharacteristics { get; internal set; }
///
/// Reserved, must be zero.
///
public uint LoaderFlags { get; internal set; }
///
/// Gets the number of data-directory entries in the remainder of the optional header. Each describes a location and size.
///
public uint NumberOfRvaAndSizes { get; internal set; }
///
/// Gets the collection data directories of the PE image optional header.
///
public ImageDataDirectory[] DataDirectories { get; internal set; }
internal ImageOptionalHeader()
{
DataDirectories = [];
}
///
/// Returns the or property of this instance.
///
///
/// The or property of this instance.
///
public ulong GetImageBase()
{
if (this is ImageOptionalHeader32 optionalHeader32)
{
return optionalHeader32.ImageBase;
}
else if (this is ImageOptionalHeader64 optionalHeader64)
{
return optionalHeader64.ImageBase;
}
else
{
throw new NotSupportedException(nameof(optionalHeader32.ImageBase));
}
}
}