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
use crate::*;
use super::Wrap;
use super::imports::Import;

/// Exported symbol.
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(::serde::Serialize))]
pub enum Export<'a> {
	/// Standard exported symbol.
	Symbol(&'a u32),
	/// This export is forwarded to another dll.
	///
	/// 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),
}
impl<'a> Export<'a> {
	/// Returns some if the symbol is exported.
	#[inline]
	pub fn symbol(self) -> Option<u32> {
		match self {
			Export::Symbol(&rva) => Some(rva),
			_ => None,
		}
	}
	/// Returns some if the symbol is forwarded.
	#[inline]
	pub fn forward(self) -> Option<&'a util::CStr> {
		match self {
			Export::Forward(name) => Some(name),
			_ => None,
		}
	}
}

/// Export directory.
impl<'a, Pe32: pe32::Pe<'a>, Pe64: pe64::Pe<'a>> Wrap<pe32::exports::Exports<'a, Pe32>, pe64::exports::Exports<'a, Pe64>> {
	/// Gets the PE instance.
	#[inline]
	pub fn pe(&self) -> Wrap<Pe32, Pe64> {
		match self {
			Wrap::T32(exports) => Wrap::T32(exports.pe()),
			Wrap::T64(exports) => Wrap::T64(exports.pe()),
		}
	}
	/// Returns the underlying export directory image.
	#[inline]
	pub fn image(&self) -> &'a image::IMAGE_EXPORT_DIRECTORY {
		match self {
			Wrap::T32(exports) => exports.image(),
			Wrap::T64(exports) => exports.image(),
		}
	}
	/// Gets the export directory's name for this library.
	#[inline]
	pub fn dll_name(&self) -> Result<&'a util::CStr> {
		match self {
			Wrap::T32(exports) => exports.dll_name(),
			Wrap::T64(exports) => exports.dll_name(),
		}
	}
	/// Gets the ordinal base for the exported functions.
	#[inline]
	pub fn ordinal_base(&self) -> u16 {
		match self {
			Wrap::T32(exports) => exports.ordinal_base(),
			Wrap::T64(exports) => exports.ordinal_base(),
		}
	}
	/// Gets the export address table.
	#[inline]
	pub fn functions(&self) -> Result<&'a [u32]> {
		match self {
			Wrap::T32(exports) => exports.functions(),
			Wrap::T64(exports) => exports.functions(),
		}
	}
	/// Gets the name address table.
	#[inline]
	pub fn names(&self) -> Result<&'a [u32]> {
		match self {
			Wrap::T32(exports) => exports.names(),
			Wrap::T64(exports) => exports.names(),
		}
	}
	/// Gets the name index table.
	#[inline]
	pub fn name_indices(&self) -> Result<&'a [u16]> {
		match self {
			Wrap::T32(exports) => exports.name_indices(),
			Wrap::T64(exports) => exports.name_indices(),
		}
	}
	/// Query the exports.
	#[inline]
	pub fn by(&self) -> Result<Wrap<pe32::exports::By<'a, Pe32>, pe64::exports::By<'a, Pe64>>> {
		match self {
			Wrap::T32(exports) => Wrap::T32(exports.by()).transpose(),
			Wrap::T64(exports) => Wrap::T64(exports.by()).transpose(),
		}
	}
}

/// Export directory symbol lookup.
impl<'a, Pe32: pe32::Pe<'a>, Pe64: pe64::Pe<'a>> Wrap<pe32::exports::By<'a, Pe32>, pe64::exports::By<'a, Pe64>> {
	/// Gets the PE instance.
	#[inline]
	pub fn pe(&self) -> Wrap<Pe32, Pe64> {
		match self {
			Wrap::T32(by) => Wrap::T32(by.pe()),
			Wrap::T64(by) => Wrap::T64(by.pe()),
		}
	}
	/// Returns the underlying export directory image.
	#[inline]
	pub fn image(&self) -> &'a image::IMAGE_EXPORT_DIRECTORY {
		match self {
			Wrap::T32(by) => by.image(),
			Wrap::T64(by) => by.image(),
		}
	}
	/// Gets the export directory's name for this library.
	#[inline]
	pub fn dll_name(&self) -> Result<&'a util::CStr> {
		match self {
			Wrap::T32(by) => by.dll_name(),
			Wrap::T64(by) => by.dll_name(),
		}
	}
	/// Gets the ordinal base for the exported functions.
	#[inline]
	pub fn ordinal_base(&self) -> u16 {
		match self {
			Wrap::T32(by) => by.ordinal_base(),
			Wrap::T64(by) => by.ordinal_base(),
		}
	}
	/// Gets the export address table.
	#[inline]
	pub fn functions(&self) -> &'a [u32] {
		match self {
			Wrap::T32(by) => by.functions(),
			Wrap::T64(by) => by.functions(),
		}
	}
	/// Gets the name address table.
	#[inline]
	pub fn names(&self) -> &'a [u32] {
		match self {
			Wrap::T32(by) => by.names(),
			Wrap::T64(by) => by.names(),
		}
	}
	/// Gets the name index table.
	#[inline]
	pub fn name_indices(&self) -> &'a [u16] {
		match self {
			Wrap::T32(by) => by.name_indices(),
			Wrap::T64(by) => by.name_indices(),
		}
	}
	/// Validates and checks if the name table is sorted.
	#[inline]
	pub fn check_sorted(&self) -> Result<bool> {
		match self {
			Wrap::T32(by) => by.check_sorted(),
			Wrap::T64(by) => by.check_sorted(),
		}
	}
	/// Looks up an `Export` by its ordinal.
	#[inline]
	pub fn ordinal(&self, ordinal: u16) -> Result<Export<'a>> {
		match self {
			Wrap::T32(by) => by.ordinal(ordinal),
			Wrap::T64(by) => by.ordinal(ordinal),
		}
	}
	/// Looks up an `Export` by its name.
	#[inline]
	pub fn name_linear<S: AsRef<[u8]> + ?Sized>(&self, name: &S) -> Result<Export<'a>> {
		match self {
			Wrap::T32(by) => by.name_linear(name),
			Wrap::T64(by) => by.name_linear(name),
		}
	}
	/// Looks up an `Export` by its name.
	#[inline]
	pub fn name<S: AsRef<[u8]> + ?Sized>(&self, name: &S) -> Result<Export<'a>> {
		match self {
			Wrap::T32(by) => by.name(name),
			Wrap::T64(by) => by.name(name),
		}
	}
	/// Looks up an `Export` by its import.
	#[inline]
	pub fn import(&self, import: Import) -> Result<Export<'a>> {
		match self {
			Wrap::T32(by) => by.import(import),
			Wrap::T64(by) => by.import(import),
		}
	}
	/// Looks up an export by its index.
	#[inline]
	pub fn index(&self, index: usize) -> Result<Export<'a>> {
		match self {
			Wrap::T32(by) => by.index(index),
			Wrap::T64(by) => by.index(index),
		}
	}
	/// Looks up an export by its hint.
	#[inline]
	pub fn hint(&self, hint: usize) -> Result<Export<'a>> {
		match self {
			Wrap::T32(by) => by.hint(hint),
			Wrap::T64(by) => by.hint(hint),
		}
	}
	/// Looks up an export by its hint and falls back to the name if the hint is incorrect.
	#[inline]
	pub fn hint_name<S: AsRef<[u8]> + ?Sized>(&self, hint: usize, name: &S) -> Result<Export<'a>> {
		match self {
			Wrap::T32(by) => by.hint_name(hint, name),
			Wrap::T64(by) => by.hint_name(hint, name),
		}
	}
	/// Looks up the name for a hint.
	#[inline]
	pub fn name_of_hint(&self, hint: usize) -> Result<&'a util::CStr> {
		match self {
			Wrap::T32(by) => by.name_of_hint(hint),
			Wrap::T64(by) => by.name_of_hint(hint),
		}
	}
	/// Given an index in the functions array, gets the named export.
	#[inline]
	pub fn name_lookup(&self, index: usize) -> Result<Import<'a>> {
		match self {
			Wrap::T32(by) => by.name_lookup(index),
			Wrap::T64(by) => by.name_lookup(index),
		}
	}
	#[inline]
	fn symbol_from_rva(&self, rva: &'a u32) -> Result<Export<'a>> {
		match self {
			Wrap::T32(by) => by.symbol_from_rva(rva),
			Wrap::T64(by) => by.symbol_from_rva(rva),
		}
	}
	/// Iterate over exported functions.
	#[inline]
	pub fn iter<'s>(&'s self) -> impl 's + Clone + Iterator<Item = Result<Export<'a>>> {
		self.functions().iter().map(move |rva| self.symbol_from_rva(rva))
	}
	/// 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>>)> {
		(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)> {
		(0..self.names().len() as u32)
			.map(move |hint| (
				self.name_of_hint(hint as usize),
				self.name_indices()[hint as usize] as usize,
			))
	}
}

/// Convenient way to get an exported address.
impl<'a, Pe32: pe32::Pe<'a>, Pe64: pe64::Pe<'a>> Wrap<Pe32, Pe64> {
	/// Convenient method to get an exported function by ordinal.
	#[inline]
	pub fn get_export_by_ordinal(&self, ordinal: u16) -> Result<Export<'a>> {
		use pe32::exports::GetProcAddress as _;
		use pe64::exports::GetProcAddress as _;
		match self {
			Wrap::T32(pe32) => pe32.get_export(ordinal),
			Wrap::T64(pe64) => pe64.get_export(ordinal),
		}
	}
	/// Convenient method to get an exported function by import.
	#[inline]
	pub fn get_export_by_import(&self, import: Import<'a>) -> Result<Export<'a>> {
		use pe32::exports::GetProcAddress as _;
		use pe64::exports::GetProcAddress as _;
		match self {
			Wrap::T32(pe32) => pe32.get_export(import),
			Wrap::T64(pe64) => pe64.get_export(import),
		}
	}
	/// Convenient method to get an exported function by name.
	#[inline]
	pub fn get_export_by_name<S: ?Sized + AsRef<[u8]>>(&self, name: &S) -> Result<Export<'a>> {
		use pe32::exports::GetProcAddress as _;
		use pe64::exports::GetProcAddress as _;
		match self {
			Wrap::T32(pe32) => pe32.get_export(name),
			Wrap::T64(pe64) => pe64.get_export(name),
		}
	}
}