linux_bootloader/
pe_section.rs1#![allow(clippy::needless_lifetimes)]
3#![allow(clippy::bind_instead_of_map)]
6
7use alloc::{borrow::ToOwned, string::String};
8use goblin::pe::section_table::SectionTable;
9
10pub fn pe_section_data<'a>(pe_data: &'a [u8], section: &SectionTable) -> Option<&'a [u8]> {
13 let section_start: usize = section.virtual_address.try_into().ok()?;
14
15 assert!(section.virtual_size <= section.size_of_raw_data);
16 let section_end: usize = section_start + usize::try_from(section.virtual_size).ok()?;
17
18 Some(&pe_data[section_start..section_end])
19}
20
21pub fn pe_section<'a>(pe_data: &'a [u8], section_name: &str) -> Option<&'a [u8]> {
24 let pe_binary = goblin::pe::PE::parse(pe_data).ok()?;
25
26 pe_binary
27 .sections
28 .iter()
29 .find(|s| s.name().map(|n| n == section_name).unwrap_or(false))
30 .and_then(|s| pe_section_data(pe_data, s))
31}
32
33pub fn pe_section_as_string<'a>(pe_data: &'a [u8], section_name: &str) -> Option<String> {
35 pe_section(pe_data, section_name).map(|data| core::str::from_utf8(data).unwrap().to_owned())
36}