Skip to content

Commit

Permalink
parse_options: added ParseMode
Browse files Browse the repository at this point in the history
  • Loading branch information
ideeockus committed Dec 8, 2024
1 parent 872544b commit ac97d4c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
18 changes: 12 additions & 6 deletions src/pe/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ use crate::container;
use crate::error;
use crate::pe::utils::pad;
use crate::strtab;
use options::ParseMode;

use scroll::{ctx, Pwrite};

Expand Down Expand Up @@ -264,15 +265,19 @@ impl<'a> PE<'a> {
if let Some(&certificate_table) =
optional_header.data_directories.get_certificate_table()
{
certificates = certificate_table::enumerate_certificates(
let certificates_result = certificate_table::enumerate_certificates(
bytes,
certificate_table.virtual_address,
certificate_table.size,
)
.unwrap_or_else(|err| {
warn!("Cannot parse CertificateTable: {:?}", err);
Default::default()
});
);

certificates = match opts.parse_mode {
ParseMode::Strict => certificates_result?,
ParseMode::Permissive => certificates_result.unwrap_or_else(|err| {
warn!("Cannot parse CertificateTable: {:?}", err);
Default::default()
}),
};

certificate_table.size as usize
} else {
Expand Down Expand Up @@ -529,6 +534,7 @@ impl<'a> TE<'a> {
let opts = &options::ParseOptions {
resolve_rva: false,
parse_attribute_certificates: false,
parse_mode: ParseMode::Strict,
};

let mut offset = 0;
Expand Down
11 changes: 11 additions & 0 deletions src/pe/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,16 @@ pub struct ParseOptions {
/// memory](https://learn.microsoft.com/en-us/windows/win32/debug/pe-format#other-contents-of-the-file).
/// For on-disk representations, leave as true. Default: true
pub parse_attribute_certificates: bool,
/// Whether or not to end with an error in case of incorrect data or continue parsing if able. Default: ParseMode::Strict
pub parse_mode: ParseMode,
}

#[derive(Debug, Copy, Clone)]
pub enum ParseMode {
/// Always end with error on incorrect data
Strict,
/// Incorrect data will not cause to end with error if possible
Permissive,
}

impl ParseOptions {
Expand All @@ -16,6 +26,7 @@ impl ParseOptions {
ParseOptions {
resolve_rva: true,
parse_attribute_certificates: true,
parse_mode: ParseMode::Strict,
}
}
}

0 comments on commit ac97d4c

Please sign in to comment.