Skip to content

Commit

Permalink
try impl ResourceEntryIterator::find_by_id
Browse files Browse the repository at this point in the history
  • Loading branch information
kkent030315 committed Dec 11, 2024
1 parent 9af1f1d commit e107d84
Showing 1 changed file with 19 additions and 8 deletions.
27 changes: 19 additions & 8 deletions src/pe/resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,23 @@ impl Iterator for ResourceEntryIterator<'_> {
}
}

impl<'a> ResourceEntryIterator<'a> {
/// Find the resource entry by its resource ID.
pub fn find_by_id(&self, id: u16) -> error::Result<Option<ResourceEntry>> {
self.map(|x| {
x.and_then(|x| {
if x.id() == Some(id) {
Ok(Some(x))
} else {
Ok(None)
}
})
})
.find_map(Result::transpose)
.transpose()
}
}

/// Represents an entry in a resource data entry structure.
///
/// This struct contains information about a specific resource, including
Expand Down Expand Up @@ -1148,10 +1165,7 @@ impl<'a> VersionInfo<'a> {
file_alignment: u32,
opts: &options::ParseOptions,
) -> error::Result<Option<Self>> {
if let Some(entry) = it
.filter_map(|x| x.ok())
.find(|x| x.id() == Some(RT_VERSION))
{
if let Some(entry) = it.find_by_id(RT_VERSION)? {
let offset_to_data =
match entry.recursive_next_depth(bytes, |e| e.offset_to_data().is_none())? {
Some(next) => match next.offset_to_data() {
Expand Down Expand Up @@ -1226,10 +1240,7 @@ impl<'a> ManifestData<'a> {
file_alignment: u32,
opts: &options::ParseOptions,
) -> error::Result<Option<Self>> {
if let Some(entry) = it
.filter_map(|x| x.ok())
.find(|x| x.id() == Some(RT_VERSION))
{
if let Some(entry) = it.find_by_id(RT_MANIFEST)? {
let offset_to_data =
match entry.recursive_next_depth(bytes, |e| e.offset_to_data().is_none())? {
Some(next) => match next.offset_to_data() {
Expand Down

0 comments on commit e107d84

Please sign in to comment.