Skip to content

Commit 6ece796

Browse files
retragerbradford
authored andcommitted
pe: Replace hard-coded magic numbers in PE parser
This commit replaces the hard-coded magic numbers in the PE parser with configurable constant values. This change makes supporting other architectures easier in the future. Signed-off-by: Akira Moroo <retrage01@gmail.com>
1 parent 396bc4e commit 6ece796

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

src/pe.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ struct Section {
3838
}
3939

4040
impl<'a> Loader<'a> {
41+
#[cfg(target_arch = "x86_64")]
42+
const MACHINE_TYPE: u16 = 0x8664;
43+
44+
#[cfg(target_arch = "x86_64")]
45+
const OPTIONAL_HEADER_MAGIC: u16 = 0x20b; // PE32+
46+
4147
pub fn new(file: &'a mut dyn crate::fat::Read) -> Loader {
4248
Loader {
4349
file,
@@ -84,7 +90,7 @@ impl<'a> Loader<'a> {
8490
}
8591

8692
// Check for supported machine
87-
if pe_region.read_u16(4) != 0x8664 {
93+
if pe_region.read_u16(4) != Self::MACHINE_TYPE {
8894
return Err(Error::InvalidExecutable);
8995
}
9096

@@ -94,8 +100,7 @@ impl<'a> Loader<'a> {
94100
let optional_region =
95101
MemoryRegion::from_bytes(&mut data[(24 + pe_header_offset) as usize..]);
96102

97-
// Only support x86-64 EFI
98-
if optional_region.read_u16(0) != 0x20b {
103+
if optional_region.read_u16(0) != Self::OPTIONAL_HEADER_MAGIC {
99104
return Err(Error::InvalidExecutable);
100105
}
101106

0 commit comments

Comments
 (0)