pelite\wrap/
imports.rs

1use crate::*;
2use super::Wrap;
3
4use std::slice;
5
6/// Imported symbol.
7#[derive(Copy, Clone, Debug, Eq, PartialEq)]
8#[cfg_attr(feature = "serde", derive(::serde::Serialize))]
9pub enum Import<'a> {
10	/// Imported by name.
11	///
12	/// The hint is an index in the export names table that may contain the desired symbol.
13	/// For more information see this [blog post](https://blogs.msdn.microsoft.com/oldnewthing/20100317-00/?p=14573) by Raymond Chen.
14	ByName { hint: usize, name: &'a util::CStr },
15	/// Imported by ordinal.
16	ByOrdinal { ord: u16 },
17}
18
19/// Import directory.
20impl<'a, Pe32: pe32::Pe<'a>, Pe64: pe64::Pe<'a>> Wrap<pe32::imports::Imports<'a, Pe32>, pe64::imports::Imports<'a, Pe64>> {
21	/// Gets the PE instance.
22	#[inline]
23	pub fn pe(&self) -> Wrap<Pe32, Pe64> {
24		match self {
25			Wrap::T32(imports) => Wrap::T32(imports.pe()),
26			Wrap::T64(imports) => Wrap::T64(imports.pe()),
27		}
28	}
29	/// Returns the underlying import directory image array.
30	#[inline]
31	pub fn image(&self) -> &'a [image::IMAGE_IMPORT_DESCRIPTOR] {
32		match self {
33			Wrap::T32(imports) => imports.image(),
34			Wrap::T64(imports) => imports.image(),
35		}
36	}
37	/// Iterator over the import descriptors.
38	#[inline]
39	pub fn iter(&self) -> Wrap<pe32::imports::Iter<'a, Pe32>, pe64::imports::Iter<'a, Pe64>> {
40		match self {
41			Wrap::T32(imports) => Wrap::T32(imports.iter()),
42			Wrap::T64(imports) => Wrap::T64(imports.iter()),
43		}
44	}
45}
46impl<'a, Pe32: pe32::Pe<'a>, Pe64: pe64::Pe<'a>> IntoIterator for Wrap<pe32::imports::Imports<'a, Pe32>, pe64::imports::Imports<'a, Pe64>> {
47	type Item = Wrap<pe32::imports::Desc<'a, Pe32>, pe64::imports::Desc<'a, Pe64>>;
48	type IntoIter = Wrap<pe32::imports::Iter<'a, Pe32>, pe64::imports::Iter<'a, Pe64>>;
49	#[inline]
50	fn into_iter(self) -> Self::IntoIter {
51		match self {
52			Wrap::T32(imports) => Wrap::T32(imports.into_iter()),
53			Wrap::T64(imports) => Wrap::T64(imports.into_iter()),
54		}
55	}
56}
57
58/// Import Address Table.
59impl<'a, Pe32: pe32::Pe<'a>, Pe64: pe64::Pe<'a>> Wrap<pe32::imports::IAT<'a, Pe32>, pe64::imports::IAT<'a, Pe64>> {
60	/// Gets the PE instance.
61	#[inline]
62	pub fn pe(&self) -> Wrap<Pe32, Pe64> {
63		match self {
64			Wrap::T32(iat) => Wrap::T32(iat.pe()),
65			Wrap::T64(iat) => Wrap::T64(iat.pe()),
66		}
67	}
68	/// Returns the underlying iat array.
69	#[inline]
70	pub fn image(&self) -> Wrap<&'a [u32], &'a [u64]> {
71		match self {
72			Wrap::T32(iat) => Wrap::T32(iat.image()),
73			Wrap::T64(iat) => Wrap::T64(iat.image()),
74		}
75	}
76	/// Iterate over the IAT.
77	#[inline]
78	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>>)>> {
79		match self {
80			Wrap::T32(iat) => Wrap::T32(iat.iter()),
81			Wrap::T64(iat) => Wrap::T64(iat.iter()),
82		}
83	}
84}
85
86/// Import library descriptor.
87impl<'a, Pe32: pe32::Pe<'a>, Pe64: pe64::Pe<'a>> Wrap<pe32::imports::Desc<'a, Pe32>, pe64::imports::Desc<'a, Pe64>> {
88	/// Gets the PE instance.
89	#[inline]
90	pub fn pe(&self) -> Wrap<Pe32, Pe64> {
91		match self {
92			Wrap::T32(desc) => Wrap::T32(desc.pe()),
93			Wrap::T64(desc) => Wrap::T64(desc.pe()),
94		}
95	}
96	/// Returns the underlying import descriptor image.
97	#[inline]
98	pub fn image(&self) -> &'a image::IMAGE_IMPORT_DESCRIPTOR {
99		match self {
100			Wrap::T32(desc) => desc.image(),
101			Wrap::T64(desc) => desc.image(),
102		}
103	}
104	/// Gets the name of the DLL imported from.
105	#[inline]
106	pub fn dll_name(&self) -> Result<&'a util::CStr> {
107		match self {
108			Wrap::T32(desc) => desc.dll_name(),
109			Wrap::T64(desc) => desc.dll_name(),
110		}
111	}
112	/// Gets the import address table.
113	#[inline]
114	pub fn iat(&self) -> Result<Wrap<slice::Iter<'a, u32>, slice::Iter<'a, u64>>> {
115		match self {
116			Wrap::T32(desc) => Wrap::T32(desc.iat()).transpose(),
117			Wrap::T64(desc) => Wrap::T64(desc.iat()).transpose(),
118		}
119	}
120	/// Gets the import name table.
121	#[inline]
122	pub fn int(&self) -> Result<impl Clone + Iterator<Item = Result<Import<'a>>>> {
123		match self {
124			Wrap::T32(desc) => Ok(Wrap::T32(desc.int()?).map(Wrap::into)),
125			Wrap::T64(desc) => Ok(Wrap::T64(desc.int()?).map(Wrap::into)),
126		}
127	}
128}