1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
/*!
Import Directory and the IAT.

The import directory lists all the module dependencies and their imported symbols by this module.

The Import Address Table (IAT) lists all the imported symbols for all the modules in one big list.
When the imports are resolved the IAT is overwritten with pointers to the resolved functions.

# Examples

```
# #![allow(unused_variables)]
use pelite::pe64::{Pe, PeFile};

# #[allow(dead_code)]
fn example(file: PeFile<'_>) -> pelite::Result<()> {
	// Access the import directory
	let imports = file.imports()?;

	// Iterate over the import descriptors
	for desc in imports {
		// DLL being imported from
		let dll_name = desc.dll_name()?;

		// Import Address Table and Import Name Table for this imported DLL
		let iat = desc.iat()?;
		let int = desc.int()?;

		// Iterate over the imported functions from this DLL
		for (va, import) in Iterator::zip(iat, int) {}
	}

	// Iterate over the IAT
	for (va, import) in file.iat()?.iter() {
		// The IAT may contains Null entries where the IAT of imported modules join
		if let Ok(import) = import {}
	}

	Ok(())
}
```
*/

use std::{fmt, iter, mem, slice};

use error::{Error, Result};
use util::CStr;

use super::image::*;
use super::Pe;

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

/// Imported symbol.
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize))]
pub enum Import<'a> {
	/// Imported by name.
	///
	/// 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 CStr },
	/// Imported by ordinal.
	ByOrdinal { ord: Ordinal }
}

// Gets the import from the import name table.
//
// These aren't actually virtual addresses.
// This function will decode them to get the import.
fn import_from_va<'a, P: Pe<'a> + Copy>(pe: P, &va: &'a Va) -> Result<Import<'a>> {
	if va & IMAGE_ORDINAL_FLAG == 0 {
		// TODO! Validate that this really is an Rva in PE32+?
		let rva = va as Rva;
		let hint = pe.derva::<u16>(rva)?;
		let name = pe.derva_c_str(rva + 2)?;
		Ok(Import::ByName { hint: *hint as usize, name })
	}
	else {
		Ok(Import::ByOrdinal { ord: va as Ordinal })
	}
}

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

/// Import directory.
///
/// For more information see the [module-level documentation](index.html).
#[derive(Copy, Clone)]
pub struct Imports<'a, P> {
	pe: P,
	image: &'a [IMAGE_IMPORT_DESCRIPTOR],
}
impl<'a, P: Pe<'a> + Copy> Imports<'a, P> {
	pub(crate) fn try_from(pe: P) -> Result<Imports<'a, P>> {
		let datadir = pe.data_directory().get(IMAGE_DIRECTORY_ENTRY_IMPORT).ok_or(Error::Bounds)?;
		let image = pe.derva_slice_f(datadir.VirtualAddress, |image: &IMAGE_IMPORT_DESCRIPTOR| image.is_null())?;
		Ok(Imports { pe, image })
	}
	/// Gets the PE instance.
	pub fn pe(&self) -> P {
		self.pe
	}
	/// Returns the underlying import directory image array.
	pub fn image(&self) -> &'a [IMAGE_IMPORT_DESCRIPTOR] {
		self.image
	}
}
impl<'a, P: Pe<'a> + Copy> IntoIterator for Imports<'a, P> {
	type Item = Desc<'a, P>;
	type IntoIter = Iter<'a, P>;
	fn into_iter(self) -> Iter<'a, P> {
		Iter {
			pe: self.pe,
			iter: self.image.iter(),
		}
	}
}
impl<'a, P: Pe<'a> + Copy> fmt::Debug for Imports<'a, P> {
	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
		f.debug_list()
			.entries(self.into_iter())
			.finish()
	}
}

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

/// Import Address Table.
///
/// For more information see the [module-level documentation](index.html).
#[derive(Copy, Clone)]
pub struct IAT<'a, P> {
	pe: P,
	image: &'a [Va],
}
impl<'a, P: Pe<'a> + Copy> IAT<'a, P> {
	pub(crate) fn try_from(pe: P) -> Result<IAT<'a, P>> {
		let datadir = pe.data_directory().get(IMAGE_DIRECTORY_ENTRY_IAT).ok_or(Error::Bounds)?;
		// Ignore datadir.Size not being a multiple of sizeof(Va), not that big of a deal...
		let image = pe.derva_slice(datadir.VirtualAddress, datadir.Size as usize / mem::size_of::<Va>())?;
		Ok(IAT { pe, image })
	}
	/// Gets the PE instance.
	pub fn pe(&self) -> P {
		self.pe
	}
	/// Returns the underlying iat array.
	pub fn image(&self) -> &'a [Va] {
		self.image
	}
	/// Iterate over the IAT.
	///
	/// When the imports aren't resolved yet the IAT is an alias for the import name table.
	pub fn iter(&self) -> iter::Map<slice::Iter<'a, Va>, impl Clone + FnMut(&'a Va) -> (&'a Va, Result<Import<'a>>)> {
		let pe = self.pe;
		self.image.iter().map(move |va| (va, import_from_va(pe, va)))
	}
}
impl<'a, P: Pe<'a> + Copy> fmt::Debug for IAT<'a, P> {
	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
		f.debug_struct("IAT")
			.field("iat.len", &self.image.len())
			.finish()
	}
}

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

#[derive(Clone)]
pub struct Iter<'a, P> {
	pe: P,
	iter: slice::Iter<'a, IMAGE_IMPORT_DESCRIPTOR>,
}
impl<'a, P: Pe<'a> + Copy> Iter<'a, P> {
	pub fn image(&self) -> &'a [IMAGE_IMPORT_DESCRIPTOR] {
		self.iter.as_slice()
	}
}
impl<'a, P: Pe<'a> + Copy> Iterator for Iter<'a, P> {
	type Item = Desc<'a, P>;
	fn next(&mut self) -> Option<Desc<'a, P>> {
		self.iter.next().map(|image| Desc { pe: self.pe, image })
	}
	fn size_hint(&self) -> (usize, Option<usize>) {
		self.iter.size_hint()
	}
	fn count(self) -> usize {
		self.iter.count()
	}
	fn nth(&mut self, n: usize) -> Option<Desc<'a, P>> {
		self.iter.nth(n).map(|image| Desc { pe: self.pe, image })
	}
}
impl<'a, P: Pe<'a> + Copy> DoubleEndedIterator for Iter<'a, P> {
	fn next_back(&mut self) -> Option<Desc<'a, P>> {
		self.iter.next_back().map(|image| Desc { pe: self.pe, image })
	}
}
impl<'a, P: Pe<'a> + Copy> ExactSizeIterator for Iter<'a, P> {}
impl<'a, P: Pe<'a> + Copy> iter::FusedIterator for Iter<'a, P> {}

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

/// Import library descriptor.
#[derive(Copy, Clone)]
pub struct Desc<'a, P> {
	pe: P,
	image: &'a IMAGE_IMPORT_DESCRIPTOR,
}
impl<'a, P: Pe<'a> + Copy> Desc<'a, P> {
	/// Gets the PE instance.
	pub fn pe(&self) -> P {
		self.pe
	}
	/// Returns the underlying import descriptor image.
	pub fn image(&self) -> &'a IMAGE_IMPORT_DESCRIPTOR {
		self.image
	}
	/// Gets the name of the DLL imported from.
	pub fn dll_name(&self) -> Result<&'a CStr> {
		self.pe.derva_c_str(self.image.Name)
	}
	/// Gets the import address table.
	///
	/// After being loaded as a library their values are resolved to the addresses of the imported functions.
	///
	/// Otherwise these contain references to the imported functions.
	/// See [`import_from_va`](struct.Desc.html#import_from_va) to get their names.
	pub fn iat(&self) -> Result<slice::Iter<'a, Va>> {
		let slice = self.pe.derva_slice_s(self.image.FirstThunk, 0)?;
		Ok(slice.iter())
	}
	/// Gets the import name table.
	pub fn int(&self) -> Result<iter::Map<slice::Iter<'a, Va>, impl Clone + FnMut(&'a Va) -> Result<Import<'a>>>> {
		let slice = self.pe.derva_slice_s(self.image.OriginalFirstThunk, 0)?;
		let pe = self.pe;
		Ok(slice.iter().map(move |va| import_from_va(pe, va)))
	}
}
impl<'a, P: Pe<'a> + Copy> fmt::Debug for Desc<'a, P> {
	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
		f.debug_struct("Imports")
			.field("dll_name", &format_args!("{:?}", self.dll_name()))
			.field("iat.len", &format_args!("{:?}", &self.iat().map(|iter| iter.len())))
			.field("int.len", &format_args!("{:?}", &self.int().map(|iter| iter.len())))
			.finish()
	}
}

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

/*
	imports: [
		{
			"dll_name": "KERNEL32.dll",
			"int": [
				{
					"ByName": { .. }
				},
				{
					"ByOrdinal": { .. }
				}
			]
		}
	]
*/

#[cfg(feature = "serde")]
mod serde {
	use util::serde_helper::*;
	use super::{Pe, Imports, IAT, Desc};

	impl<'a, P: Pe<'a> + Copy> Serialize for Imports<'a, P> {
		fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
			serializer.collect_seq(self.into_iter())
		}
	}
	impl<'a, P: Pe<'a> + Copy> Serialize for IAT<'a, P> {
		fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
			let iat = self.iter().filter_map(|(_va, import)| import.ok());
			serializer.collect_seq(iat)
		}
	}
	impl<'a, P: Pe<'a> + Copy> Serialize for Desc<'a, P> {
		fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
			let mut state = serializer.serialize_struct("Desc", 2)?;
			state.serialize_field("dll_name", &self.dll_name().ok())?;
			let int = self.int().map(|int| {
				SerdeIter(int.filter_map(|import| {
					import.ok()
				}))
			});
			state.serialize_field("int", &int.ok())?;
			state.end()
		}
	}
}

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

#[cfg(test)]
pub(crate) fn test<'a, P: Pe<'a> + Copy>(pe: P) -> Result<()> {
	let imports = pe.imports()?;
	let _ = format!("{:?}", imports);

	for desc in imports {
		let _ = format!("{:?}", desc);
		let _dll_name = desc.dll_name();
		for _ in desc.iat() {}
		for _ in desc.int() {}
	}

	let iat = pe.iat()?;
	for (va, import) in iat.iter() {
		let _ = format!("{:?}", import);
		if import.is_ok() {
			assert_eq!(import_from_va(pe, va), import);
		}
	}

	Ok(())
}