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
/*!
Base Relocations Directory.

# Examples

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

# #[allow(dead_code)]
fn example(file: PeFile) -> pelite::Result<()> {
	// Access the base relocations
	let base_relocs = file.base_relocs()?;

	// Iterate over the flattened list of relocations
	for rva in base_relocs.into_iter().flat_map(|relocs| relocs) {}

	Ok(())
}
```
*/

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

use error::{Error, Result};

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

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

#[derive(Copy, Clone)]
pub struct BaseRelocs<'a, P> {
	pe: P,
	relocs: &'a [u8],
}
impl<'a, P: Pe<'a> + Copy> BaseRelocs<'a, P> {
	pub(crate) fn new(pe: P) -> Result<BaseRelocs<'a, P>> {
		let datadir = pe.data_directory().get(IMAGE_DIRECTORY_ENTRY_BASERELOC).ok_or(Error::OOB)?;
		let relocs = pe.derva_slice(datadir.VirtualAddress, datadir.Size as usize)?;
		// Validate the relocations...
		// This better not contain any bugs.
		let mut it = relocs;
		while it.len() > 0 {
			if it.len() < mem::size_of::<IMAGE_BASE_RELOCATION>() {
				return Err(Error::Corrupt);
			}
			let image = unsafe { &*(it.as_ptr() as *const IMAGE_BASE_RELOCATION) };
			if image.SizeOfBlock % 4 != 0 || image.SizeOfBlock as usize > it.len() {
				return Err(Error::Corrupt);
			}
			it = &it[image.SizeOfBlock as usize..];
		}
		Ok(BaseRelocs { pe, relocs })
	}
	pub fn pe(&self) -> P {
		self.pe
	}
}
impl<'a, P: Pe<'a> + Copy> IntoIterator for BaseRelocs<'a, P> {
	type Item = Block<'a, P>;
	type IntoIter = Iter<'a, P>;
	fn into_iter(self) -> Iter<'a, P> {
		Iter { pe: self.pe, iter: self.relocs.iter() }
	}
}
impl<'a, P: Pe<'a> + Copy> fmt::Debug for BaseRelocs<'a, P> {
	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
		f.debug_struct("BaseRelocs").finish()
	}
}

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

/// Iterator over the base relocations.
#[derive(Clone)]
pub struct Iter<'a, P> {
	pe: P,
	iter: slice::Iter<'a, u8>,
}
impl<'a, P: Pe<'a> + Copy> Iterator for Iter<'a, P> {
	type Item = Block<'a, P>;
	fn next(&mut self) -> Option<Block<'a, P>> {
		if self.iter.len() != 0 {
			// This is safe as it should have been checked in `new`.
			unsafe {
				let slice = self.iter.as_slice();
				let image = &*(slice.as_ptr() as *const IMAGE_BASE_RELOCATION);
				self.iter = slice.get_unchecked(image.SizeOfBlock as usize..).iter();
				Some(Block { pe: self.pe, image })
			}
		}
		else {
			None
		}
	}
}

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

/// Base Relocations Block.
#[derive(Copy, Clone)]
pub struct Block<'a, P> {
	pe: P,
	image: &'a IMAGE_BASE_RELOCATION,
}
impl<'a, P: Pe<'a> + Copy> Block<'a, P> {
	/// Gets the PE instance.
	pub fn pe(&self) -> P {
		self.pe
	}
	/// Returns the underlying base relocations image.
	pub fn image(&self) -> &'a IMAGE_BASE_RELOCATION {
		self.image
	}
	/// Rva starting from.
	pub fn va(&self) -> Rva {
		self.image.VirtualAddress
	}
	/// Gets the types and offsets.
	pub fn words(&self) -> &'a [u16] {
		unsafe {
			let p = (self.image as *const IMAGE_BASE_RELOCATION).offset(1) as *const u16;
			let len = (self.image.SizeOfBlock as usize - mem::size_of::<IMAGE_BASE_RELOCATION>()) / 2;
			slice::from_raw_parts(p, len)
		}
	}
	/// Gets the final Rva of a typeoffset.
	pub fn rva_of(&self, word: &u16) -> Rva {
		let offset = (word & 0x0FFF) as Rva;
		self.image.VirtualAddress + offset
	}
	/// Gets the type of a typeoffset.
	pub fn type_of(&self, &word: &u16) -> u8 {
		((word >> 12) & 0xFF) as u8
	}
}
impl<'a, P: Pe<'a> + Copy> IntoIterator for Block<'a, P> {
	type Item = Rva;
	type IntoIter = BlockIter<'a>;
	fn into_iter(self) -> BlockIter<'a> {
		BlockIter {
			rva: self.image.VirtualAddress,
			iter: self.words().iter(),
		}
	}
}
impl<'a, P: Pe<'a> + Copy> fmt::Debug for Block<'a, P> {
	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
		f.debug_struct("Block")
			.field("words.len", &self.words().len())
			.finish()
	}
}


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

pub struct BlockIter<'a> {
	rva: Rva,
	iter: slice::Iter<'a, u16>,
}
impl<'a> Iterator for BlockIter<'a> {
	type Item = Rva;
	fn next(&mut self) -> Option<Rva> {
		self.iter.next().and_then(|&word| {
			let ty = ((word >> 12) & 0xFF) as u8;
			if ty != IMAGE_REL_BASED_ABSOLUTE {
				let offset = (word & 0x0FFF) as Rva;
				Some(self.rva + offset)
			}
			else {
				None
			}
		})
	}
}