Skip to content

Commit

Permalink
Ran rustfmt
Browse files Browse the repository at this point in the history
  • Loading branch information
ko1N committed Jan 31, 2021
1 parent 1b0ea3a commit f3c9f0d
Show file tree
Hide file tree
Showing 7 changed files with 133 additions and 45 deletions.
26 changes: 21 additions & 5 deletions src/pe/debug.rs
Expand Up @@ -2,9 +2,9 @@ use crate::error;
use scroll::{Pread, Pwrite, SizeWith};

use crate::pe::data_directories;
use crate::pe::options;
use crate::pe::section_table;
use crate::pe::utils;
use crate::pe::options;

#[derive(Debug, PartialEq, Copy, Clone, Default)]
pub struct DebugData<'a> {
Expand All @@ -19,7 +19,13 @@ impl<'a> DebugData<'a> {
sections: &[section_table::SectionTable],
file_alignment: u32,
) -> error::Result<Self> {
Self::parse_with_opts(bytes, dd, sections, file_alignment, &options::ParseOptions::default())
Self::parse_with_opts(
bytes,
dd,
sections,
file_alignment,
&options::ParseOptions::default(),
)
}

pub fn parse_with_opts(
Expand Down Expand Up @@ -77,15 +83,21 @@ impl ImageDebugDirectory {
sections: &[section_table::SectionTable],
file_alignment: u32,
) -> error::Result<Self> {
Self::parse_with_opts(bytes, dd, sections, file_alignment, &options::ParseOptions::default())
Self::parse_with_opts(
bytes,
dd,
sections,
file_alignment,
&options::ParseOptions::default(),
)
}

fn parse_with_opts(
bytes: &[u8],
dd: data_directories::DataDirectory,
sections: &[section_table::SectionTable],
file_alignment: u32,
opts: &options::ParseOptions
opts: &options::ParseOptions,
) -> error::Result<Self> {
let rva = dd.virtual_address as usize;
let offset = utils::find_offset(rva, sections, file_alignment, opts).ok_or_else(|| {
Expand Down Expand Up @@ -119,7 +131,11 @@ impl<'a> CodeviewPDB70DebugInfo<'a> {
Self::parse_with_opts(bytes, idd, &options::ParseOptions::default())
}

pub fn parse_with_opts(bytes: &'a [u8], idd: &ImageDebugDirectory, opts: &options::ParseOptions) -> error::Result<Option<Self>> {
pub fn parse_with_opts(
bytes: &'a [u8],
idd: &ImageDebugDirectory,
opts: &options::ParseOptions,
) -> error::Result<Option<Self>> {
if idd.data_type != IMAGE_DEBUG_TYPE_CODEVIEW {
// not a codeview debug directory
// that's not an error, but it's not a CodeviewPDB70DebugInfo either
Expand Down
39 changes: 25 additions & 14 deletions src/pe/exception.rs
Expand Up @@ -49,8 +49,8 @@ use scroll::{self, Pread, Pwrite};
use crate::error;

use crate::pe::data_directories;
use crate::pe::section_table;
use crate::pe::options;
use crate::pe::section_table;
use crate::pe::utils;

/// The function has an exception handler that should be called when looking for functions that need
Expand Down Expand Up @@ -666,9 +666,15 @@ impl<'a> ExceptionData<'a> {
bytes: &'a [u8],
directory: data_directories::DataDirectory,
sections: &[section_table::SectionTable],
file_alignment: u32
file_alignment: u32,
) -> error::Result<Self> {
Self::parse_with_opts(bytes, directory, sections, file_alignment, &options::ParseOptions::default())
Self::parse_with_opts(
bytes,
directory,
sections,
file_alignment,
&options::ParseOptions::default(),
)
}

/// Parses exception data from the image at the given offset.
Expand All @@ -677,7 +683,7 @@ impl<'a> ExceptionData<'a> {
directory: data_directories::DataDirectory,
sections: &[section_table::SectionTable],
file_alignment: u32,
opts: &options::ParseOptions
opts: &options::ParseOptions,
) -> error::Result<Self> {
let size = directory.size as usize;

Expand Down Expand Up @@ -775,7 +781,7 @@ impl<'a> ExceptionData<'a> {
pub fn get_unwind_info(
&self,
function: RuntimeFunction,
sections: &[section_table::SectionTable]
sections: &[section_table::SectionTable],
) -> error::Result<UnwindInfo<'a>> {
self.get_unwind_info_with_opts(function, sections, &options::ParseOptions::default())
}
Expand All @@ -785,17 +791,18 @@ impl<'a> ExceptionData<'a> {
&self,
mut function: RuntimeFunction,
sections: &[section_table::SectionTable],
opts: &options::ParseOptions
opts: &options::ParseOptions,
) -> error::Result<UnwindInfo<'a>> {
while function.unwind_info_address % 2 != 0 {
let rva = (function.unwind_info_address & !1) as usize;
function = self.get_function_by_rva_with_opts(rva, sections, opts)?;
}

let rva = function.unwind_info_address as usize;
let offset = utils::find_offset(rva, sections, self.file_alignment, opts).ok_or_else(|| {
error::Error::Malformed(format!("cannot map unwind rva ({:#x}) into offset", rva))
})?;
let offset =
utils::find_offset(rva, sections, self.file_alignment, opts).ok_or_else(|| {
error::Error::Malformed(format!("cannot map unwind rva ({:#x}) into offset", rva))
})?;

UnwindInfo::parse(self.bytes, offset)
}
Expand All @@ -804,7 +811,7 @@ impl<'a> ExceptionData<'a> {
fn get_function_by_rva(
&self,
rva: usize,
sections: &[section_table::SectionTable]
sections: &[section_table::SectionTable],
) -> error::Result<RuntimeFunction> {
self.get_function_by_rva_with_opts(rva, sections, &options::ParseOptions::default())
}
Expand All @@ -813,11 +820,15 @@ impl<'a> ExceptionData<'a> {
&self,
rva: usize,
sections: &[section_table::SectionTable],
opts: &options::ParseOptions
opts: &options::ParseOptions,
) -> error::Result<RuntimeFunction> {
let offset = utils::find_offset(rva, sections, self.file_alignment, opts).ok_or_else(|| {
error::Error::Malformed(format!("cannot map exception rva ({:#x}) into offset", rva))
})?;
let offset =
utils::find_offset(rva, sections, self.file_alignment, opts).ok_or_else(|| {
error::Error::Malformed(format!(
"cannot map exception rva ({:#x}) into offset",
rva
))
})?;

self.get_function_by_offset(offset)
}
Expand Down
18 changes: 15 additions & 3 deletions src/pe/export.rs
Expand Up @@ -5,8 +5,8 @@ use log::debug;

use crate::error;

use crate::pe::options;
use crate::pe::data_directories;
use crate::pe::options;
use crate::pe::section_table;
use crate::pe::utils;

Expand Down Expand Up @@ -72,7 +72,13 @@ impl<'a> ExportData<'a> {
sections: &[section_table::SectionTable],
file_alignment: u32,
) -> error::Result<ExportData<'a>> {
Self::parse_with_opts(bytes, dd, sections, file_alignment, &options::ParseOptions::default())
Self::parse_with_opts(
bytes,
dd,
sections,
file_alignment,
&options::ParseOptions::default(),
)
}

pub fn parse_with_opts(
Expand Down Expand Up @@ -364,7 +370,13 @@ impl<'a> Export<'a> {
sections: &[section_table::SectionTable],
file_alignment: u32,
) -> error::Result<Vec<Export<'a>>> {
Self::parse_with_opts(bytes, export_data, sections, file_alignment, &options::ParseOptions::default())
Self::parse_with_opts(
bytes,
export_data,
sections,
file_alignment,
&options::ParseOptions::default(),
)
}

pub fn parse_with_opts(
Expand Down
57 changes: 46 additions & 11 deletions src/pe/import.rs
Expand Up @@ -7,8 +7,8 @@ use scroll::ctx::TryFromCtx;
use scroll::{Pread, Pwrite, SizeWith};

use crate::pe::data_directories;
use crate::pe::section_table;
use crate::pe::options;
use crate::pe::section_table;
use crate::pe::utils;

use log::{debug, warn};
Expand Down Expand Up @@ -99,7 +99,13 @@ impl<'a> SyntheticImportLookupTableEntry<'a> {
sections: &[section_table::SectionTable],
file_alignment: u32,
) -> error::Result<ImportLookupTable<'a>> {
Self::parse_with_opts::<T>(bytes, offset, sections, file_alignment, &options::ParseOptions::default())
Self::parse_with_opts::<T>(
bytes,
offset,
sections,
file_alignment,
&options::ParseOptions::default(),
)
}

pub fn parse_with_opts<T: Bitfield<'a>>(
Expand Down Expand Up @@ -192,7 +198,13 @@ impl<'a> SyntheticImportDirectoryEntry<'a> {
sections: &[section_table::SectionTable],
file_alignment: u32,
) -> error::Result<SyntheticImportDirectoryEntry<'a>> {
Self::parse_with_opts::<T>(bytes, import_directory_entry, sections, file_alignment, &options::ParseOptions::default())
Self::parse_with_opts::<T>(
bytes,
import_directory_entry,
sections,
file_alignment,
&options::ParseOptions::default(),
)
}

pub fn parse_with_opts<T: Bitfield<'a>>(
Expand All @@ -208,9 +220,12 @@ impl<'a> SyntheticImportDirectoryEntry<'a> {
let import_lookup_table = {
let import_lookup_table_rva = import_directory_entry.import_lookup_table_rva;
let import_address_table_rva = import_directory_entry.import_address_table_rva;
if let Some(import_lookup_table_offset) =
utils::find_offset(import_lookup_table_rva as usize, sections, file_alignment, opts)
{
if let Some(import_lookup_table_offset) = utils::find_offset(
import_lookup_table_rva as usize,
sections,
file_alignment,
opts,
) {
debug!("Synthesizing lookup table imports for {} lib, with import lookup table rva: {:#x}", name, import_lookup_table_rva);
let import_lookup_table = SyntheticImportLookupTableEntry::parse::<T>(
bytes,
Expand All @@ -223,9 +238,12 @@ impl<'a> SyntheticImportDirectoryEntry<'a> {
import_lookup_table
);
Some(import_lookup_table)
} else if let Some(import_address_table_offset) =
utils::find_offset(import_address_table_rva as usize, sections, file_alignment, opts)
{
} else if let Some(import_address_table_offset) = utils::find_offset(
import_address_table_rva as usize,
sections,
file_alignment,
opts,
) {
debug!("Synthesizing lookup table imports for {} lib, with import address table rva: {:#x}", name, import_lookup_table_rva);
let import_address_table = SyntheticImportLookupTableEntry::parse::<T>(
bytes,
Expand Down Expand Up @@ -288,7 +306,13 @@ impl<'a> ImportData<'a> {
sections: &[section_table::SectionTable],
file_alignment: u32,
) -> error::Result<ImportData<'a>> {
Self::parse_with_opts::<T>(bytes, dd, sections, file_alignment, &options::ParseOptions::default())
Self::parse_with_opts::<T>(
bytes,
dd,
sections,
file_alignment,
&options::ParseOptions::default(),
)
}

pub fn parse_with_opts<T: Bitfield<'a>>(
Expand All @@ -303,7 +327,18 @@ impl<'a> ImportData<'a> {
"import_directory_table_rva {:#x}",
import_directory_table_rva
);
let offset = &mut utils::find_offset(import_directory_table_rva, sections, file_alignment, opts).ok_or_else(|| error::Error::Malformed(format!("Cannot create ImportData; cannot map import_directory_table_rva {:#x} into offset", import_directory_table_rva)))?;
let offset = &mut utils::find_offset(
import_directory_table_rva,
sections,
file_alignment,
opts,
)
.ok_or_else(|| {
error::Error::Malformed(format!(
"Cannot create ImportData; cannot map import_directory_table_rva {:#x} into offset",
import_directory_table_rva
))
})?;
debug!("import data offset {:#x}", offset);
let mut import_data = Vec::new();
loop {
Expand Down
22 changes: 16 additions & 6 deletions src/pe/mod.rs
Expand Up @@ -5,7 +5,6 @@

use alloc::vec::Vec;

pub mod options;
pub mod characteristic;
pub mod data_directories;
pub mod debug;
Expand All @@ -14,6 +13,7 @@ pub mod export;
pub mod header;
pub mod import;
pub mod optional_header;
pub mod options;
pub mod relocation;
pub mod section_table;
pub mod symbol;
Expand Down Expand Up @@ -97,11 +97,21 @@ impl<'a> PE<'a> {
);
let file_alignment = optional_header.windows_fields.file_alignment;
if let Some(export_table) = *optional_header.data_directories.get_export_table() {
if let Ok(ed) =
export::ExportData::parse_with_opts(bytes, export_table, &sections, file_alignment, opts)
{
if let Ok(ed) = export::ExportData::parse_with_opts(
bytes,
export_table,
&sections,
file_alignment,
opts,
) {
debug!("export data {:#?}", ed);
exports = export::Export::parse_with_opts(bytes, &ed, &sections, file_alignment, opts)?;
exports = export::Export::parse_with_opts(
bytes,
&ed,
&sections,
file_alignment,
opts,
)?;
name = ed.name;
debug!("name: {:#?}", name);
export_data = Some(ed);
Expand Down Expand Up @@ -148,7 +158,7 @@ impl<'a> PE<'a> {
debug_table,
&sections,
file_alignment,
opts
opts,
)?);
}

Expand Down
6 changes: 2 additions & 4 deletions src/pe/options.rs
Expand Up @@ -8,8 +8,6 @@ pub struct ParseOptions {
impl ParseOptions {
/// Returns a parse options structure with default values
pub fn default() -> Self {
ParseOptions {
resolve_rva: true,
}
ParseOptions { resolve_rva: true }
}
}
}
10 changes: 8 additions & 2 deletions src/pe/utils.rs
Expand Up @@ -2,8 +2,8 @@ use crate::error;
use alloc::string::ToString;
use scroll::Pread;

use super::section_table;
use super::options;
use super::section_table;

use crate::pe::data_directories::DataDirectory;
use core::cmp;
Expand Down Expand Up @@ -127,7 +127,13 @@ pub fn get_data<'a, T>(
where
T: scroll::ctx::TryFromCtx<'a, scroll::Endian, Error = scroll::Error>,
{
get_data_with_opts(bytes, sections, directory, file_alignment, &options::ParseOptions::default())
get_data_with_opts(
bytes,
sections,
directory,
file_alignment,
&options::ParseOptions::default(),
)
}

pub fn get_data_with_opts<'a, T>(
Expand Down

0 comments on commit f3c9f0d

Please sign in to comment.