use crate::io::{ReadData, WriteData};
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum SectionName {
ExportTable = 0,
ImportTable = 1,
ResourceTable = 2,
ExceptionTable = 3,
CertificateTable = 4,
BaseRelocationTable = 5,
Debug = 6,
Architecture = 7,
GlobalPtr = 8,
TlsTable = 9,
LoadConfigTable = 10,
BoundImport = 11,
Ita = 12,
DelayImportDescriptor = 13,
ClrRuntimeHeader = 14,
Reserved = 15,
}
impl SectionName {
pub const ALL: [SectionName; 16] = [
SectionName::ExportTable,
SectionName::ImportTable,
SectionName::ResourceTable,
SectionName::ExceptionTable,
SectionName::CertificateTable,
SectionName::BaseRelocationTable,
SectionName::Debug,
SectionName::Architecture,
SectionName::GlobalPtr,
SectionName::TlsTable,
SectionName::LoadConfigTable,
SectionName::BoundImport,
SectionName::Ita,
SectionName::DelayImportDescriptor,
SectionName::ClrRuntimeHeader,
SectionName::Reserved,
];
}
#[derive(Default, Clone, Debug, PartialEq, Eq)]
pub struct DataDirectories {
pub export_table: ImageDataDirectory,
pub import_table: ImageDataDirectory,
pub resource_table: ImageDataDirectory,
pub exception_table: ImageDataDirectory,
pub certificate_table: ImageDataDirectory,
pub base_relocation_table: ImageDataDirectory,
pub debug: ImageDataDirectory,
pub architecture: ImageDataDirectory,
pub global_ptr: ImageDataDirectory,
pub tls_table: ImageDataDirectory,
pub load_config_table: ImageDataDirectory,
pub bound_import: ImageDataDirectory,
pub ita: ImageDataDirectory,
pub delay_import_descriptor: ImageDataDirectory,
pub clr_runtime_header: ImageDataDirectory,
pub reserved: ImageDataDirectory,
}
impl DataDirectories {
pub fn get_directory(&self, name: SectionName) -> ImageDataDirectory {
match name {
SectionName::ExportTable => self.export_table,
SectionName::ImportTable => self.import_table,
SectionName::ResourceTable => self.resource_table,
SectionName::ExceptionTable => self.exception_table,
SectionName::CertificateTable => self.certificate_table,
SectionName::BaseRelocationTable => self.base_relocation_table,
SectionName::Debug => self.debug,
SectionName::Architecture => self.architecture,
SectionName::GlobalPtr => self.global_ptr,
SectionName::TlsTable => self.tls_table,
SectionName::LoadConfigTable => self.load_config_table,
SectionName::BoundImport => self.bound_import,
SectionName::Ita => self.ita,
SectionName::DelayImportDescriptor => self.delay_import_descriptor,
SectionName::ClrRuntimeHeader => self.clr_runtime_header,
SectionName::Reserved => self.reserved,
}
}
pub fn set_directory(&mut self, name: SectionName, data: ImageDataDirectory) {
match name {
SectionName::ExportTable => self.export_table = data,
SectionName::ImportTable => self.import_table = data,
SectionName::ResourceTable => self.resource_table = data,
SectionName::ExceptionTable => self.exception_table = data,
SectionName::CertificateTable => self.certificate_table = data,
SectionName::BaseRelocationTable => self.base_relocation_table = data,
SectionName::Debug => self.debug = data,
SectionName::Architecture => self.architecture = data,
SectionName::GlobalPtr => self.global_ptr = data,
SectionName::TlsTable => self.tls_table = data,
SectionName::LoadConfigTable => self.load_config_table = data,
SectionName::BoundImport => self.bound_import = data,
SectionName::Ita => self.ita = data,
SectionName::DelayImportDescriptor => self.delay_import_descriptor = data,
SectionName::ClrRuntimeHeader => self.clr_runtime_header = data,
SectionName::Reserved => self.reserved = data,
}
}
}
#[derive(Default, Copy, Clone, Debug, PartialEq, Eq)]
pub struct ImageDataDirectory {
pub virtual_address: u32,
pub size: u32,
}
impl ImageDataDirectory {
pub const SIZE: usize = 8;
pub fn is_null(&self) -> bool {
self.virtual_address == 0 &&
self.size == 0
}
}
impl ReadData for ImageDataDirectory {
fn read(reader: &mut impl crate::io::Reader) -> crate::error::Result<Self> {
Ok(Self {
virtual_address: reader.read()?,
size: reader.read()?,
})
}
}
impl WriteData for ImageDataDirectory {
fn write_to(self, writer: &mut impl crate::io::Writer) -> crate::error::Result<()> {
writer.write(self.virtual_address)?;
writer.write(self.size)?;
Ok(())
}
}