diff --git a/examples/imphash.rs b/examples/imphash.rs index 1e7fd5e..1e068d6 100644 --- a/examples/imphash.rs +++ b/examples/imphash.rs @@ -48,12 +48,11 @@ fn imphash(image: &[u8]) -> pelite::Result { let dll_name = desc.dll_name()?; dll_name.hash(&mut h); for imp in desc.int()? { - use pelite::pe32::imports::Import; match imp { - Ok(Import::ByName { hint: _, name }) => { + Ok(pelite::Import::ByName { hint: _, name }) => { name.hash(&mut h); }, - Ok(Import::ByOrdinal { ord }) => { + Ok(pelite::Import::ByOrdinal { ord }) => { ord.hash(&mut h); }, Err(err) => { diff --git a/src/pe64/exports.rs b/src/pe64/exports.rs index 4ef3569..a1b35e1 100644 --- a/src/pe64/exports.rs +++ b/src/pe64/exports.rs @@ -57,14 +57,14 @@ use std::{fmt, ops}; use crate::util::CStr; use crate::{Error, Result}; +use crate::Import; use super::image::*; -use super::imports::Import; use super::Pe; //---------------------------------------------------------------- -pub use crate::wrap::exports::Export; +pub use crate::Export; //---------------------------------------------------------------- diff --git a/src/pe64/imports.rs b/src/pe64/imports.rs index 4b45560..329aeaf 100644 --- a/src/pe64/imports.rs +++ b/src/pe64/imports.rs @@ -51,7 +51,9 @@ use super::Pe; //---------------------------------------------------------------- -pub use crate::wrap::imports::Import; +pub use crate::Import; + +//---------------------------------------------------------------- // Gets the import from the import name table. // diff --git a/src/wrap/debug.rs b/src/wrap/debug.rs index 5fe6b35..186eb8b 100644 --- a/src/wrap/debug.rs +++ b/src/wrap/debug.rs @@ -1,10 +1,4 @@ -use std::{fmt, str}; - -use crate::image::*; -use crate::util::CStr; -use crate::*; - -use super::Wrap; +use super::*; /// Debug directory. impl<'a, Pe32: pe32::Pe<'a>, Pe64: pe64::Pe<'a>> Wrap, pe64::debug::Debug<'a, Pe64>> { @@ -26,7 +20,7 @@ impl<'a, Pe32: pe32::Pe<'a>, Pe64: pe64::Pe<'a>> Wrap Option<&'a util::CStr> { + pub fn pdb_file_name(&self) -> Option<&'a CStr> { match self { Wrap::T32(debug) => debug.pdb_file_name(), Wrap::T64(debug) => debug.pdb_file_name(), @@ -134,9 +128,9 @@ impl<'a> Entry<'a> { #[derive(Copy, Clone)] pub enum CodeView<'a> { /// CodeView 2.0 debug information. - Cv20 { image: &'a IMAGE_DEBUG_CV_INFO_PDB20, pdb_file_name: &'a CStr }, + Cv20 { image: &'a image::IMAGE_DEBUG_CV_INFO_PDB20, pdb_file_name: &'a CStr }, /// CodeView 7.0 debug information. - Cv70 { image: &'a IMAGE_DEBUG_CV_INFO_PDB70, pdb_file_name: &'a CStr }, + Cv70 { image: &'a image::IMAGE_DEBUG_CV_INFO_PDB70, pdb_file_name: &'a CStr }, } impl<'a> CodeView<'a> { pub fn format(&self) -> &'a str { @@ -183,11 +177,11 @@ impl<'a> fmt::Debug for CodeView<'a> { /// Debug information. #[derive(Copy, Clone)] pub struct Dbg<'a> { - pub image: &'a IMAGE_DEBUG_MISC, + pub image: &'a image::IMAGE_DEBUG_MISC, } impl<'a> Dbg<'a> { /// Gets the underlying information image. - pub fn image(&self) -> &'a IMAGE_DEBUG_MISC { + pub fn image(&self) -> &'a image::IMAGE_DEBUG_MISC { self.image } } diff --git a/src/wrap/exports.rs b/src/wrap/exports.rs index 2628c3d..ee1e8a4 100644 --- a/src/wrap/exports.rs +++ b/src/wrap/exports.rs @@ -1,7 +1,4 @@ -use crate::*; - -use super::imports::Import; -use super::Wrap; +use super::*; /// Exported symbol. #[derive(Copy, Clone, Debug, Eq, PartialEq)] @@ -13,7 +10,7 @@ pub enum Export<'a> { /// /// Format of the string is `"DllName.ExportName"`. /// For more information see this [blog post](https://blogs.msdn.microsoft.com/oldnewthing/20060719-24/?p=30473) by Raymond Chen. - Forward(&'a util::CStr), + Forward(&'a CStr), } impl<'a> Export<'a> { /// Returns some if the symbol is exported. @@ -26,7 +23,7 @@ impl<'a> Export<'a> { } /// Returns some if the symbol is forwarded. #[inline] - pub fn forward(self) -> Option<&'a util::CStr> { + pub fn forward(self) -> Option<&'a CStr> { match self { Export::Forward(name) => Some(name), _ => None, @@ -54,7 +51,7 @@ impl<'a, Pe32: pe32::Pe<'a>, Pe64: pe64::Pe<'a>> Wrap Result<&'a util::CStr> { + pub fn dll_name(&self) -> Result<&'a CStr> { match self { Wrap::T32(exports) => exports.dll_name(), Wrap::T64(exports) => exports.dll_name(), @@ -122,7 +119,7 @@ impl<'a, Pe32: pe32::Pe<'a>, Pe64: pe64::Pe<'a>> Wrap Result<&'a util::CStr> { + pub fn dll_name(&self) -> Result<&'a CStr> { match self { Wrap::T32(by) => by.dll_name(), Wrap::T64(by) => by.dll_name(), @@ -226,7 +223,7 @@ impl<'a, Pe32: pe32::Pe<'a>, Pe64: pe64::Pe<'a>> Wrap Result<&'a util::CStr> { + pub fn name_of_hint(&self, hint: usize) -> Result<&'a CStr> { match self { Wrap::T32(by) => by.name_of_hint(hint), Wrap::T64(by) => by.name_of_hint(hint), @@ -254,12 +251,12 @@ impl<'a, Pe32: pe32::Pe<'a>, Pe64: pe64::Pe<'a>> Wrap(&'s self) -> impl 's + Clone + Iterator, Result>)> { + pub fn iter_names<'s>(&'s self) -> impl 's + Clone + Iterator, Result>)> { (0..self.names().len() as u32).map(move |hint| (self.name_of_hint(hint as usize), self.hint(hint as usize))) } /// Iterate over functions exported by name, returning their name and index in the functions table. #[inline] - pub fn iter_name_indices<'s>(&'s self) -> impl 's + Clone + Iterator, usize)> { + pub fn iter_name_indices<'s>(&'s self) -> impl 's + Clone + Iterator, usize)> { (0..self.names().len() as u32).map(move |hint| (self.name_of_hint(hint as usize), self.name_indices()[hint as usize] as usize)) } } diff --git a/src/wrap/file.rs b/src/wrap/file.rs index a5af535..c69910a 100644 --- a/src/wrap/file.rs +++ b/src/wrap/file.rs @@ -1,6 +1,4 @@ -use crate::*; - -use super::Wrap; +use super::*; /// Format agnostic PE file. pub type PeFile<'a> = Wrap, pe64::PeFile<'a>>; diff --git a/src/wrap/headers.rs b/src/wrap/headers.rs index a7e68a9..4faec2b 100644 --- a/src/wrap/headers.rs +++ b/src/wrap/headers.rs @@ -1,8 +1,4 @@ -use std::ops::Range; - -use crate::*; - -use super::Wrap; +use super::*; /// Describes the PE headers. impl<'a, Pe32: pe32::Pe<'a>, Pe64: pe64::Pe<'a>> Wrap, pe64::headers::Headers> { @@ -32,7 +28,7 @@ impl<'a, Pe32: pe32::Pe<'a>, Pe64: pe64::Pe<'a>> Wrap Range { + pub fn code_range(&self) -> ops::Range { match self { Wrap::T32(headers) => headers.code_range(), Wrap::T64(headers) => headers.code_range(), @@ -40,7 +36,7 @@ impl<'a, Pe32: pe32::Pe<'a>, Pe64: pe64::Pe<'a>> Wrap Range { + pub fn image_range(&self) -> ops::Range { match self { Wrap::T32(headers) => headers.image_range(), Wrap::T64(headers) => headers.image_range(), diff --git a/src/wrap/imports.rs b/src/wrap/imports.rs index b981cba..1c99d01 100644 --- a/src/wrap/imports.rs +++ b/src/wrap/imports.rs @@ -1,8 +1,4 @@ -use std::slice; - -use crate::*; - -use super::Wrap; +use super::*; /// Imported symbol. #[derive(Copy, Clone, Debug, Eq, PartialEq)] @@ -12,7 +8,7 @@ pub enum Import<'a> { /// /// The hint is an index in the export names table that may contain the desired symbol. /// For more information see this [blog post](https://blogs.msdn.microsoft.com/oldnewthing/20100317-00/?p=14573) by Raymond Chen. - ByName { hint: usize, name: &'a util::CStr }, + ByName { hint: usize, name: &'a CStr }, /// Imported by ordinal. ByOrdinal { ord: u16 }, } @@ -76,7 +72,7 @@ impl<'a, Pe32: pe32::Pe<'a>, Pe64: pe64::Pe<'a>> Wrap Wrap>)>, impl Clone + Iterator>)>> { + pub fn iter(&self) -> Wrap>)>, impl Clone + Iterator>)>> { match self { Wrap::T32(iat) => Wrap::T32(iat.iter()), Wrap::T64(iat) => Wrap::T64(iat.iter()), @@ -104,7 +100,7 @@ impl<'a, Pe32: pe32::Pe<'a>, Pe64: pe64::Pe<'a>> Wrap Result<&'a util::CStr> { + pub fn dll_name(&self) -> Result<&'a CStr> { match self { Wrap::T32(desc) => desc.dll_name(), Wrap::T64(desc) => desc.dll_name(), diff --git a/src/wrap/mod.rs b/src/wrap/mod.rs index 4d29a11..dbaff6f 100644 --- a/src/wrap/mod.rs +++ b/src/wrap/mod.rs @@ -1,4 +1,8 @@ -use crate::Result; +use std::{fmt, mem, ops, slice, str}; + +use crate::{image, pattern, pe32, pe64, rich_structure, Result, Error}; +use crate::util::{CStr, FromBytes}; +use crate::Pod; /// Wraps 32-bit and 64-bit variants. #[derive(Copy, Clone, Debug, Eq, PartialEq)] @@ -70,3 +74,6 @@ pub use self::file::PeFile; pub(crate) use self::pe::get_section_bytes; pub use self::pe::Align; pub use self::view::PeView; + +pub use self::imports::Import; +pub use self::exports::Export; diff --git a/src/wrap/pe.rs b/src/wrap/pe.rs index f2d8b14..63c62a7 100644 --- a/src/wrap/pe.rs +++ b/src/wrap/pe.rs @@ -1,6 +1,4 @@ -use crate::*; - -use super::Wrap; +use super::*; /// The specific alignment used by the view. /// @@ -167,14 +165,14 @@ impl<'a, Pe32: pe32::Pe<'a>, Pe64: pe64::Pe<'a>> Wrap { } } #[inline] - pub fn derva_c_str(&self, rva: u32) -> Result<&'a util::CStr> { + pub fn derva_c_str(&self, rva: u32) -> Result<&'a CStr> { match self { Wrap::T32(pe32) => pe32.derva_c_str(rva), Wrap::T64(pe64) => pe64.derva_c_str(rva), } } #[inline] - pub fn derva_string(&self, rva: u32) -> Result<&'a T> { + pub fn derva_string(&self, rva: u32) -> Result<&'a T> { match self { Wrap::T32(pe32) => pe32.derva_string(rva), Wrap::T64(pe64) => pe64.derva_string(rva), diff --git a/src/wrap/scanner.rs b/src/wrap/scanner.rs index d3196de..d801a53 100644 --- a/src/wrap/scanner.rs +++ b/src/wrap/scanner.rs @@ -1,14 +1,10 @@ -use std::ops::Range; - -use crate::*; - -use super::Wrap; +use super::*; /// Pattern scanner. impl<'a, Pe32: pe32::Pe<'a>, Pe64: pe64::Pe<'a>> Wrap, pe64::scanner::Scanner> { /// Finds the unique match for the pattern in the given range. #[inline] - pub fn finds(&self, pat: &[pattern::Atom], range: Range, save: &mut [u32]) -> bool { + pub fn finds(&self, pat: &[pattern::Atom], range: ops::Range, save: &mut [u32]) -> bool { match self { Wrap::T32(scanner) => scanner.finds(pat, range, save), Wrap::T64(scanner) => scanner.finds(pat, range, save), @@ -24,7 +20,7 @@ impl<'a, Pe32: pe32::Pe<'a>, Pe64: pe64::Pe<'a>> Wrap(&self, pat: &'pat [pattern::Atom], range: Range) -> Wrap, pe64::scanner::Matches<'pat, Pe64>> { + pub fn matches<'pat>(&self, pat: &'pat [pattern::Atom], range: ops::Range) -> Wrap, pe64::scanner::Matches<'pat, Pe64>> { match self { Wrap::T32(scanner) => Wrap::T32(scanner.matches(pat, range)), Wrap::T64(scanner) => Wrap::T64(scanner.matches(pat, range)), @@ -67,7 +63,7 @@ impl<'a, 'pat, Pe32: pe32::Pe<'a>, Pe64: pe64::Pe<'a>> Wrap Range { + pub fn range(&self) -> ops::Range { match self { Wrap::T32(matches) => matches.range(), Wrap::T64(matches) => matches.range(), diff --git a/src/wrap/sections.rs b/src/wrap/sections.rs index a4da0bb..cf0ff8e 100644 --- a/src/wrap/sections.rs +++ b/src/wrap/sections.rs @@ -1,14 +1,11 @@ -use std::{fmt, mem, ops, slice}; - -use crate::image::*; -use crate::Pod; +use super::*; //---------------------------------------------------------------- /// Section header. #[derive(Copy, Clone)] #[repr(transparent)] -pub struct SectionHeader(IMAGE_SECTION_HEADER); +pub struct SectionHeader(image::IMAGE_SECTION_HEADER); impl SectionHeader { /// Returns the name as a byte slice. @@ -17,7 +14,7 @@ impl SectionHeader { crate::util::trimn(&self.0.Name) } /// Returns the name. - pub fn name(&self) -> Result<&str, &[u8]> { + pub fn name(&self) -> core::result::Result<&str, &[u8]> { crate::util::parsen(&self.0.Name) } /// Returns the virtual range. @@ -39,9 +36,9 @@ impl SectionHeader { unsafe impl Pod for SectionHeader {} impl ops::Deref for SectionHeader { - type Target = IMAGE_SECTION_HEADER; + type Target = image::IMAGE_SECTION_HEADER; #[inline] - fn deref(&self) -> &IMAGE_SECTION_HEADER { + fn deref(&self) -> &image::IMAGE_SECTION_HEADER { &self.0 } } @@ -69,15 +66,15 @@ impl fmt::Debug for SectionHeader { /// Section headers. #[repr(transparent)] -pub struct SectionHeaders([IMAGE_SECTION_HEADER]); +pub struct SectionHeaders([image::IMAGE_SECTION_HEADER]); impl SectionHeaders { - pub(crate) fn new(image: &[IMAGE_SECTION_HEADER]) -> &SectionHeaders { + pub(crate) fn new(image: &[image::IMAGE_SECTION_HEADER]) -> &SectionHeaders { unsafe { mem::transmute(image) } } /// Returns the underlying slice of section headers. #[inline] - pub fn image(&self) -> &[IMAGE_SECTION_HEADER] { + pub fn image(&self) -> &[image::IMAGE_SECTION_HEADER] { &self.0 } /// Gets the section headers as a slice of `SectionHeader`. @@ -95,11 +92,11 @@ impl SectionHeaders { pub fn by_name>(&self, name: &S) -> Option<&SectionHeader> { // Names have a max length, if larger they will never match let name = name.as_ref(); - if name.len() > IMAGE_SIZEOF_SHORT_NAME { + if name.len() > image::IMAGE_SIZEOF_SHORT_NAME { return None; } // Copy the prefix into a new buffer for easy comparison - let mut name_buf = [0u8; IMAGE_SIZEOF_SHORT_NAME]; + let mut name_buf = [0u8; image::IMAGE_SIZEOF_SHORT_NAME]; for i in 0..name.len() { name_buf[i] = name[i]; } @@ -142,7 +139,7 @@ impl fmt::Debug for SectionHeaders { //---------------------------------------------------------------- #[cfg(feature = "serde")] -pub(crate) fn serialize_name(name: &[u8; IMAGE_SIZEOF_SHORT_NAME], serializer: S) -> Result { +pub(crate) fn serialize_name(name: &[u8; image::IMAGE_SIZEOF_SHORT_NAME], serializer: S) -> core::result::Result { match crate::util::parsen(name) { Ok(name) => serializer.serialize_str(name), Err(name) => serializer.serialize_bytes(name), diff --git a/src/wrap/tls.rs b/src/wrap/tls.rs index c5a667c..06f83d7 100644 --- a/src/wrap/tls.rs +++ b/src/wrap/tls.rs @@ -1,6 +1,4 @@ -use crate::*; - -use super::Wrap; +use super::*; /// TLS Directory. impl<'a, Pe32: pe32::Pe<'a>, Pe64: pe64::Pe<'a>> Wrap, pe64::tls::Tls<'a, Pe64>> { diff --git a/src/wrap/view.rs b/src/wrap/view.rs index 0148ee8..8a076a8 100644 --- a/src/wrap/view.rs +++ b/src/wrap/view.rs @@ -1,6 +1,4 @@ -use crate::*; - -use super::Wrap; +use super::*; /// Format agnostic PE view. pub type PeView<'a> = Wrap, pe64::PeView<'a>>; diff --git a/tests/demo64.rs b/tests/demo64.rs index de4f409..1fcd9ef 100644 --- a/tests/demo64.rs +++ b/tests/demo64.rs @@ -1,7 +1,7 @@ -use pelite::pe64::exports::{Export, GetProcAddress}; -use pelite::pe64::imports::Import; +use pelite::pe64::exports::GetProcAddress; use pelite::pe64::{Pe, PeFile, Rva}; use pelite::util::CStr; +use pelite::{Export, Import}; use pelite::{Error, FileMap}; const FILE_NAME: &str = "demo/Demo64.dll";