Skip to content

Commit ac97d4c

Browse files
committed
parse_options: added ParseMode
1 parent 872544b commit ac97d4c

File tree

2 files changed

+23
-6
lines changed

2 files changed

+23
-6
lines changed

src/pe/mod.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ use crate::container;
3333
use crate::error;
3434
use crate::pe::utils::pad;
3535
use crate::strtab;
36+
use options::ParseMode;
3637

3738
use scroll::{ctx, Pwrite};
3839

@@ -264,15 +265,19 @@ impl<'a> PE<'a> {
264265
if let Some(&certificate_table) =
265266
optional_header.data_directories.get_certificate_table()
266267
{
267-
certificates = certificate_table::enumerate_certificates(
268+
let certificates_result = certificate_table::enumerate_certificates(
268269
bytes,
269270
certificate_table.virtual_address,
270271
certificate_table.size,
271-
)
272-
.unwrap_or_else(|err| {
273-
warn!("Cannot parse CertificateTable: {:?}", err);
274-
Default::default()
275-
});
272+
);
273+
274+
certificates = match opts.parse_mode {
275+
ParseMode::Strict => certificates_result?,
276+
ParseMode::Permissive => certificates_result.unwrap_or_else(|err| {
277+
warn!("Cannot parse CertificateTable: {:?}", err);
278+
Default::default()
279+
}),
280+
};
276281

277282
certificate_table.size as usize
278283
} else {
@@ -529,6 +534,7 @@ impl<'a> TE<'a> {
529534
let opts = &options::ParseOptions {
530535
resolve_rva: false,
531536
parse_attribute_certificates: false,
537+
parse_mode: ParseMode::Strict,
532538
};
533539

534540
let mut offset = 0;

src/pe/options.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,16 @@ pub struct ParseOptions {
88
/// memory](https://learn.microsoft.com/en-us/windows/win32/debug/pe-format#other-contents-of-the-file).
99
/// For on-disk representations, leave as true. Default: true
1010
pub parse_attribute_certificates: bool,
11+
/// Whether or not to end with an error in case of incorrect data or continue parsing if able. Default: ParseMode::Strict
12+
pub parse_mode: ParseMode,
13+
}
14+
15+
#[derive(Debug, Copy, Clone)]
16+
pub enum ParseMode {
17+
/// Always end with error on incorrect data
18+
Strict,
19+
/// Incorrect data will not cause to end with error if possible
20+
Permissive,
1121
}
1222

1323
impl ParseOptions {
@@ -16,6 +26,7 @@ impl ParseOptions {
1626
ParseOptions {
1727
resolve_rva: true,
1828
parse_attribute_certificates: true,
29+
parse_mode: ParseMode::Strict,
1930
}
2031
}
2132
}

0 commit comments

Comments
 (0)