Skip to content

Commit

Permalink
Added ImageDataDirectory::file_offset_range
Browse files Browse the repository at this point in the history
  • Loading branch information
daladim committed Jan 10, 2022
1 parent f3a5821 commit aeca852
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/read/pe/data_directory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,38 @@ impl<'data> DataDirectories<'data> {

impl pe::ImageDataDirectory {
/// Return the virtual address range of this directory entry.
///
/// For correctly formatted PE files, this range does not overlap sections.
pub fn address_range(&self) -> (u32, u32) {
(self.virtual_address.get(LE), self.size.get(LE))
}

/// Return the file offset range of this directory entry.
///
/// For correctly formatted PE files, this range does not overlap sections.
pub fn file_offset_range<'data>(&self, sections: &SectionTable<'data>) -> Result<(u32, u32)> {
let start_section =
sections
.section_at(self.virtual_address.get(LE))
.ok_or(crate::read::Error(
"This directory does not point to a valid section",
))?;

let section_file_offset = start_section.pointer_to_raw_data.get(LE);
let section_va = start_section.virtual_address.get(LE);
let start = self
.virtual_address
.get(LE)
.checked_sub(section_va)
.and_then(|a| a.checked_add(section_file_offset))
.ok_or(crate::read::Error("Invalid directory addresses"))?;
let end = start
.checked_add(self.size.get(LE))
.ok_or(crate::read::Error("Invalid directory addresses"))?;

Ok((start, end))
}

/// Get the data referenced by this directory entry.
///
/// This function has some limitations:
Expand Down

0 comments on commit aeca852

Please sign in to comment.