Skip to content

Commit

Permalink
pe(optional header): support writing PE32 header
Browse files Browse the repository at this point in the history
Well, PE32 should be something of the past, but, corkami tests PE are mostly PE32,
so for testing reason, it helps to support that usecase.
  • Loading branch information
RaitoBezarius committed Oct 1, 2023
1 parent 4585d30 commit ff7b1ad
Showing 1 changed file with 36 additions and 2 deletions.
38 changes: 36 additions & 2 deletions src/pe/optional_header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,36 @@ impl From<WindowsFields32> for WindowsFields {
}
}

impl TryFrom<WindowsFields64> for WindowsFields32 {
type Error = crate::error::Error;

fn try_from(value: WindowsFields64) -> Result<Self, Self::Error> {
Ok(WindowsFields32 {
image_base: value.image_base.try_into()?,
section_alignment: value.section_alignment,
file_alignment: value.file_alignment,
major_operating_system_version: value.major_operating_system_version,
minor_operating_system_version: value.minor_operating_system_version,
major_image_version: value.major_image_version,
minor_image_version: value.minor_image_version,
major_subsystem_version: value.major_subsystem_version,
minor_subsystem_version: value.minor_subsystem_version,
win32_version_value: value.win32_version_value,
size_of_image: value.size_of_image,
size_of_headers: value.size_of_headers,
check_sum: value.check_sum,
subsystem: value.subsystem,
dll_characteristics: value.dll_characteristics,
size_of_stack_reserve: value.size_of_stack_reserve.try_into()?,
size_of_stack_commit: value.size_of_stack_commit.try_into()?,
size_of_heap_reserve: value.size_of_heap_reserve.try_into()?,
size_of_heap_commit: value.size_of_heap_commit.try_into()?,
loader_flags: value.loader_flags,
number_of_rva_and_sizes: value.number_of_rva_and_sizes
})
}
}

// impl From<WindowsFields32> for WindowsFields {
// fn from(windows: WindowsFields32) -> Self {
// WindowsFields {
Expand Down Expand Up @@ -328,8 +358,12 @@ impl ctx::TryIntoCtx<scroll::Endian> for OptionalHeader {
match self.standard_fields.magic {
MAGIC_32 => {
bytes.gwrite_with::<StandardFields32>(self.standard_fields.into(), offset, ctx)?;
// TODO: windowsfields64 -> windowsfields32
todo!();
bytes.gwrite_with(
WindowsFields32::try_from(self.windows_fields)?,
offset,
ctx
)?;
bytes.gwrite_with(self.data_directories, offset, ctx)?;
},
MAGIC_64 => {
bytes.gwrite_with::<StandardFields64>(self.standard_fields.into(), offset, ctx)?;
Expand Down

0 comments on commit ff7b1ad

Please sign in to comment.