Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions examples/imphash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,11 @@ fn imphash(image: &[u8]) -> pelite::Result<u64> {
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) => {
Expand Down
4 changes: 2 additions & 2 deletions src/pe64/exports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,14 @@

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;

//----------------------------------------------------------------

Expand Down Expand Up @@ -449,7 +449,7 @@
//----------------------------------------------------------------

#[cfg(test)]
pub(crate) fn test<'a, P: Pe<'a>>(pe: P) -> Result<()> {

Check warning on line 452 in src/pe64/exports.rs

View workflow job for this annotation

GitHub Actions / ci-linux (stable)

function `test` is never used

Check warning on line 452 in src/pe64/exports.rs

View workflow job for this annotation

GitHub Actions / ci-linux (beta)

function `test` is never used

Check warning on line 452 in src/pe64/exports.rs

View workflow job for this annotation

GitHub Actions / ci-linux (nightly)

function `test` is never used

Check warning on line 452 in src/pe64/exports.rs

View workflow job for this annotation

GitHub Actions / ci-windows (stable, x86_64-pc-windows-msvc)

function `test` is never used

Check warning on line 452 in src/pe64/exports.rs

View workflow job for this annotation

GitHub Actions / ci-windows (beta, i686-pc-windows-msvc)

function `test` is never used

Check warning on line 452 in src/pe64/exports.rs

View workflow job for this annotation

GitHub Actions / ci-windows (nightly, x86_64-pc-windows-msvc)

function `test` is never used

Check warning on line 452 in src/pe64/exports.rs

View workflow job for this annotation

GitHub Actions / ci-windows (beta, x86_64-pc-windows-msvc)

function `test` is never used

Check warning on line 452 in src/pe64/exports.rs

View workflow job for this annotation

GitHub Actions / ci-windows (nightly, i686-pc-windows-msvc)

function `test` is never used

Check warning on line 452 in src/pe64/exports.rs

View workflow job for this annotation

GitHub Actions / ci-windows (stable, i686-pc-windows-msvc)

function `test` is never used
let by = pe.exports()?.by()?;
let _ = format!("{:?}", by);

Expand Down
4 changes: 3 additions & 1 deletion src/pe64/imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@

//----------------------------------------------------------------

pub use crate::wrap::imports::Import;
pub use crate::Import;

//----------------------------------------------------------------

// Gets the import from the import name table.
//
Expand Down Expand Up @@ -290,7 +292,7 @@
//----------------------------------------------------------------

#[cfg(test)]
pub(crate) fn test<'a, P: Pe<'a>>(pe: P) -> Result<()> {

Check warning on line 295 in src/pe64/imports.rs

View workflow job for this annotation

GitHub Actions / ci-linux (stable)

function `test` is never used

Check warning on line 295 in src/pe64/imports.rs

View workflow job for this annotation

GitHub Actions / ci-linux (beta)

function `test` is never used

Check warning on line 295 in src/pe64/imports.rs

View workflow job for this annotation

GitHub Actions / ci-linux (nightly)

function `test` is never used

Check warning on line 295 in src/pe64/imports.rs

View workflow job for this annotation

GitHub Actions / ci-windows (stable, x86_64-pc-windows-msvc)

function `test` is never used

Check warning on line 295 in src/pe64/imports.rs

View workflow job for this annotation

GitHub Actions / ci-windows (beta, i686-pc-windows-msvc)

function `test` is never used

Check warning on line 295 in src/pe64/imports.rs

View workflow job for this annotation

GitHub Actions / ci-windows (nightly, x86_64-pc-windows-msvc)

function `test` is never used

Check warning on line 295 in src/pe64/imports.rs

View workflow job for this annotation

GitHub Actions / ci-windows (beta, x86_64-pc-windows-msvc)

function `test` is never used

Check warning on line 295 in src/pe64/imports.rs

View workflow job for this annotation

GitHub Actions / ci-windows (nightly, i686-pc-windows-msvc)

function `test` is never used

Check warning on line 295 in src/pe64/imports.rs

View workflow job for this annotation

GitHub Actions / ci-windows (stable, i686-pc-windows-msvc)

function `test` is never used
let imports = pe.imports()?;
let _ = format!("{:?}", imports);

Expand Down
18 changes: 6 additions & 12 deletions src/wrap/debug.rs
Original file line number Diff line number Diff line change
@@ -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<pe32::debug::Debug<'a, Pe32>, pe64::debug::Debug<'a, Pe64>> {
Expand All @@ -26,7 +20,7 @@ impl<'a, Pe32: pe32::Pe<'a>, Pe64: pe64::Pe<'a>> Wrap<pe32::debug::Debug<'a, Pe3
}
/// Gets the CodeView PDB file name.
#[inline]
pub fn pdb_file_name(&self) -> 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(),
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
}
Expand Down
19 changes: 8 additions & 11 deletions src/wrap/exports.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
use crate::*;

use super::imports::Import;
use super::Wrap;
use super::*;

/// Exported symbol.
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
Expand All @@ -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.
Expand All @@ -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,
Expand Down Expand Up @@ -54,7 +51,7 @@ impl<'a, Pe32: pe32::Pe<'a>, Pe64: pe64::Pe<'a>> Wrap<pe32::exports::Exports<'a,
}
/// Gets the export directory's name for this library.
#[inline]
pub fn dll_name(&self) -> 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(),
Expand Down Expand Up @@ -122,7 +119,7 @@ impl<'a, Pe32: pe32::Pe<'a>, Pe64: pe64::Pe<'a>> Wrap<pe32::exports::By<'a, Pe32
}
/// Gets the export directory's name for this library.
#[inline]
pub fn dll_name(&self) -> 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(),
Expand Down Expand Up @@ -226,7 +223,7 @@ impl<'a, Pe32: pe32::Pe<'a>, Pe64: pe64::Pe<'a>> Wrap<pe32::exports::By<'a, Pe32
}
/// Looks up the name for a hint.
#[inline]
pub fn name_of_hint(&self, hint: usize) -> 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),
Expand Down Expand Up @@ -254,12 +251,12 @@ impl<'a, Pe32: pe32::Pe<'a>, Pe64: pe64::Pe<'a>> Wrap<pe32::exports::By<'a, Pe32
}
/// Iterate over functions exported by name.
#[inline]
pub fn iter_names<'s>(&'s self) -> impl 's + Clone + Iterator<Item = (Result<&'a util::CStr>, Result<Export<'a>>)> {
pub fn iter_names<'s>(&'s self) -> impl 's + Clone + Iterator<Item = (Result<&'a CStr>, Result<Export<'a>>)> {
(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<Item = (Result<&'a util::CStr>, usize)> {
pub fn iter_name_indices<'s>(&'s self) -> impl 's + Clone + Iterator<Item = (Result<&'a CStr>, 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))
}
}
Expand Down
4 changes: 1 addition & 3 deletions src/wrap/file.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use crate::*;

use super::Wrap;
use super::*;

/// Format agnostic PE file.
pub type PeFile<'a> = Wrap<pe32::PeFile<'a>, pe64::PeFile<'a>>;
Expand Down
10 changes: 3 additions & 7 deletions src/wrap/headers.rs
Original file line number Diff line number Diff line change
@@ -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<pe32::headers::Headers<Pe32>, pe64::headers::Headers<Pe64>> {
Expand Down Expand Up @@ -32,15 +28,15 @@ impl<'a, Pe32: pe32::Pe<'a>, Pe64: pe64::Pe<'a>> Wrap<pe32::headers::Headers<Pe3
}
/// Gets the code range from the optional header.
#[inline]
pub fn code_range(&self) -> Range<u32> {
pub fn code_range(&self) -> ops::Range<u32> {
match self {
Wrap::T32(headers) => headers.code_range(),
Wrap::T64(headers) => headers.code_range(),
}
}
/// Gets the full image range excluding the PE headers.
#[inline]
pub fn image_range(&self) -> Range<u32> {
pub fn image_range(&self) -> ops::Range<u32> {
match self {
Wrap::T32(headers) => headers.image_range(),
Wrap::T64(headers) => headers.image_range(),
Expand Down
12 changes: 4 additions & 8 deletions src/wrap/imports.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
use std::slice;

use crate::*;

use super::Wrap;
use super::*;

/// Imported symbol.
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
Expand All @@ -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 },
}
Expand Down Expand Up @@ -76,7 +72,7 @@ impl<'a, Pe32: pe32::Pe<'a>, Pe64: pe64::Pe<'a>> Wrap<pe32::imports::IAT<'a, Pe3
}
/// Iterate over the IAT.
#[inline]
pub fn iter(&self) -> Wrap<impl Clone + Iterator<Item = (&'a u32, Result<pe32::imports::Import<'a>>)>, impl Clone + Iterator<Item = (&'a u64, Result<pe64::imports::Import<'a>>)>> {
pub fn iter(&self) -> Wrap<impl Clone + Iterator<Item = (&'a u32, Result<Import<'a>>)>, impl Clone + Iterator<Item = (&'a u64, Result<Import<'a>>)>> {
match self {
Wrap::T32(iat) => Wrap::T32(iat.iter()),
Wrap::T64(iat) => Wrap::T64(iat.iter()),
Expand Down Expand Up @@ -104,7 +100,7 @@ impl<'a, Pe32: pe32::Pe<'a>, Pe64: pe64::Pe<'a>> Wrap<pe32::imports::Desc<'a, Pe
}
/// Gets the name of the DLL imported from.
#[inline]
pub fn dll_name(&self) -> 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(),
Expand Down
9 changes: 8 additions & 1 deletion src/wrap/mod.rs
Original file line number Diff line number Diff line change
@@ -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)]
Expand Down Expand Up @@ -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;
8 changes: 3 additions & 5 deletions src/wrap/pe.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use crate::*;

use super::Wrap;
use super::*;

/// The specific alignment used by the view.
///
Expand Down Expand Up @@ -167,14 +165,14 @@ impl<'a, Pe32: pe32::Pe<'a>, Pe64: pe64::Pe<'a>> Wrap<Pe32, Pe64> {
}
}
#[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<T: ?Sized + util::FromBytes>(&self, rva: u32) -> Result<&'a T> {
pub fn derva_string<T: ?Sized + FromBytes>(&self, rva: u32) -> Result<&'a T> {
match self {
Wrap::T32(pe32) => pe32.derva_string(rva),
Wrap::T64(pe64) => pe64.derva_string(rva),
Expand Down
12 changes: 4 additions & 8 deletions src/wrap/scanner.rs
Original file line number Diff line number Diff line change
@@ -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<pe32::scanner::Scanner<Pe32>, pe64::scanner::Scanner<Pe64>> {
/// Finds the unique match for the pattern in the given range.
#[inline]
pub fn finds(&self, pat: &[pattern::Atom], range: Range<u32>, save: &mut [u32]) -> bool {
pub fn finds(&self, pat: &[pattern::Atom], range: ops::Range<u32>, save: &mut [u32]) -> bool {
match self {
Wrap::T32(scanner) => scanner.finds(pat, range, save),
Wrap::T64(scanner) => scanner.finds(pat, range, save),
Expand All @@ -24,7 +20,7 @@ impl<'a, Pe32: pe32::Pe<'a>, Pe64: pe64::Pe<'a>> Wrap<pe32::scanner::Scanner<Pe3
}
/// Returns an iterator over the matches of a pattern within the given range.
#[inline]
pub fn matches<'pat>(&self, pat: &'pat [pattern::Atom], range: Range<u32>) -> Wrap<pe32::scanner::Matches<'pat, Pe32>, pe64::scanner::Matches<'pat, Pe64>> {
pub fn matches<'pat>(&self, pat: &'pat [pattern::Atom], range: ops::Range<u32>) -> Wrap<pe32::scanner::Matches<'pat, Pe32>, 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)),
Expand Down Expand Up @@ -67,7 +63,7 @@ impl<'a, 'pat, Pe32: pe32::Pe<'a>, Pe64: pe64::Pe<'a>> Wrap<pe32::scanner::Match
}
/// Gets the remaining range to scan.
#[inline]
pub fn range(&self) -> Range<u32> {
pub fn range(&self) -> ops::Range<u32> {
match self {
Wrap::T32(matches) => matches.range(),
Wrap::T64(matches) => matches.range(),
Expand Down
25 changes: 11 additions & 14 deletions src/wrap/sections.rs
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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.
Expand All @@ -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
}
}
Expand Down Expand Up @@ -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`.
Expand All @@ -95,11 +92,11 @@ impl SectionHeaders {
pub fn by_name<S: ?Sized + AsRef<[u8]>>(&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];
}
Expand Down Expand Up @@ -142,7 +139,7 @@ impl fmt::Debug for SectionHeaders {
//----------------------------------------------------------------

#[cfg(feature = "serde")]
pub(crate) fn serialize_name<S: ::serde::ser::Serializer>(name: &[u8; IMAGE_SIZEOF_SHORT_NAME], serializer: S) -> Result<S::Ok, S::Error> {
pub(crate) fn serialize_name<S: ::serde::ser::Serializer>(name: &[u8; image::IMAGE_SIZEOF_SHORT_NAME], serializer: S) -> core::result::Result<S::Ok, S::Error> {
match crate::util::parsen(name) {
Ok(name) => serializer.serialize_str(name),
Err(name) => serializer.serialize_bytes(name),
Expand Down
4 changes: 1 addition & 3 deletions src/wrap/tls.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use crate::*;

use super::Wrap;
use super::*;

/// TLS Directory.
impl<'a, Pe32: pe32::Pe<'a>, Pe64: pe64::Pe<'a>> Wrap<pe32::tls::Tls<'a, Pe32>, pe64::tls::Tls<'a, Pe64>> {
Expand Down
Loading
Loading