1use core::slice;
23use crate::endian::LittleEndian as LE;
4use crate::pe;
5use crate::read::{Error, ReadError, ReadRef, Result};
67use super::{
8 DelayLoadImportTable, ExportTable, ImportTable, RelocationBlockIterator, ResourceDirectory,
9 SectionTable,
10};
1112/// The table of data directories in a PE file.
13///
14/// Returned by [`ImageNtHeaders::parse`](super::ImageNtHeaders::parse).
15#[derive(Debug, Clone, Copy)]
16pub struct DataDirectories<'data> {
17 entries: &'data [pe::ImageDataDirectory],
18}
1920impl<'data> DataDirectories<'data> {
21/// Parse the data directory table.
22 ///
23 /// `data` must be the remaining optional data following the
24 /// [optional header](pe::ImageOptionalHeader64). `number` must be from the
25 /// [`number_of_rva_and_sizes`](pe::ImageOptionalHeader64::number_of_rva_and_sizes)
26 /// field of the optional header.
27pub fn parse(data: &'data [u8], number: u32) -> Result<Self> {
28let entries = data
29 .read_slice_at(0, number as usize)
30 .read_error("Invalid PE number of RVA and sizes")?;
31Ok(DataDirectories { entries })
32 }
3334/// The number of data directories.
35#[allow(clippy::len_without_is_empty)]
36pub fn len(&self) -> usize {
37self.entries.len()
38 }
3940/// Iterator over the data directories.
41pub fn iter(&self) -> slice::Iter<'data, pe::ImageDataDirectory> {
42self.entries.iter()
43 }
4445/// Iterator which gives the directories as well as their index (one of the IMAGE_DIRECTORY_ENTRY_* constants).
46pub fn enumerate(&self) -> core::iter::Enumerate<slice::Iter<'data, pe::ImageDataDirectory>> {
47self.entries.iter().enumerate()
48 }
4950/// Returns the data directory at the given index.
51 ///
52 /// Index should be one of the `IMAGE_DIRECTORY_ENTRY_*` constants.
53 ///
54 /// Returns `None` if the index is larger than the table size,
55 /// or if the entry at the index has a zero virtual address.
56pub fn get(&self, index: usize) -> Option<&'data pe::ImageDataDirectory> {
57self.entries
58 .get(index)
59 .filter(|d| d.virtual_address.get(LE) != 0)
60 }
6162/// Returns the unparsed export directory.
63 ///
64 /// `data` must be the entire file data.
65pub fn export_directory<R: ReadRef<'data>>(
66&self,
67 data: R,
68 sections: &SectionTable<'data>,
69 ) -> Result<Option<&'data pe::ImageExportDirectory>> {
70let data_dir = match self.get(pe::IMAGE_DIRECTORY_ENTRY_EXPORT) {
71Some(data_dir) => data_dir,
72None => return Ok(None),
73 };
74let export_data = data_dir.data(data, sections)?;
75 ExportTable::parse_directory(export_data).map(Some)
76 }
7778/// Returns the partially parsed export directory.
79 ///
80 /// `data` must be the entire file data.
81pub fn export_table<R: ReadRef<'data>>(
82&self,
83 data: R,
84 sections: &SectionTable<'data>,
85 ) -> Result<Option<ExportTable<'data>>> {
86let data_dir = match self.get(pe::IMAGE_DIRECTORY_ENTRY_EXPORT) {
87Some(data_dir) => data_dir,
88None => return Ok(None),
89 };
90let export_va = data_dir.virtual_address.get(LE);
91let export_data = data_dir.data(data, sections)?;
92 ExportTable::parse(export_data, export_va).map(Some)
93 }
9495/// Returns the partially parsed import directory.
96 ///
97 /// `data` must be the entire file data.
98pub fn import_table<R: ReadRef<'data>>(
99&self,
100 data: R,
101 sections: &SectionTable<'data>,
102 ) -> Result<Option<ImportTable<'data>>> {
103let data_dir = match self.get(pe::IMAGE_DIRECTORY_ENTRY_IMPORT) {
104Some(data_dir) => data_dir,
105None => return Ok(None),
106 };
107let import_va = data_dir.virtual_address.get(LE);
108let (section_data, section_va) = sections
109 .pe_data_containing(data, import_va)
110 .read_error("Invalid import data dir virtual address")?;
111Ok(Some(ImportTable::new(section_data, section_va, import_va)))
112 }
113114/// Returns the partially parsed delay-load import directory.
115 ///
116 /// `data` must be the entire file data.
117pub fn delay_load_import_table<R: ReadRef<'data>>(
118&self,
119 data: R,
120 sections: &SectionTable<'data>,
121 ) -> Result<Option<DelayLoadImportTable<'data>>> {
122let data_dir = match self.get(pe::IMAGE_DIRECTORY_ENTRY_DELAY_IMPORT) {
123Some(data_dir) => data_dir,
124None => return Ok(None),
125 };
126let import_va = data_dir.virtual_address.get(LE);
127let (section_data, section_va) = sections
128 .pe_data_containing(data, import_va)
129 .read_error("Invalid import data dir virtual address")?;
130Ok(Some(DelayLoadImportTable::new(
131 section_data,
132 section_va,
133 import_va,
134 )))
135 }
136137/// Returns the blocks in the base relocation directory.
138 ///
139 /// `data` must be the entire file data.
140pub fn relocation_blocks<R: ReadRef<'data>>(
141&self,
142 data: R,
143 sections: &SectionTable<'data>,
144 ) -> Result<Option<RelocationBlockIterator<'data>>> {
145let data_dir = match self.get(pe::IMAGE_DIRECTORY_ENTRY_BASERELOC) {
146Some(data_dir) => data_dir,
147None => return Ok(None),
148 };
149let reloc_data = data_dir.data(data, sections)?;
150Ok(Some(RelocationBlockIterator::new(reloc_data)))
151 }
152153/// Returns the resource directory.
154 ///
155 /// `data` must be the entire file data.
156pub fn resource_directory<R: ReadRef<'data>>(
157&self,
158 data: R,
159 sections: &SectionTable<'data>,
160 ) -> Result<Option<ResourceDirectory<'data>>> {
161let data_dir = match self.get(pe::IMAGE_DIRECTORY_ENTRY_RESOURCE) {
162Some(data_dir) => data_dir,
163None => return Ok(None),
164 };
165let rsrc_data = data_dir.data(data, sections)?;
166Ok(Some(ResourceDirectory::new(rsrc_data)))
167 }
168}
169170impl pe::ImageDataDirectory {
171/// Return the virtual address range of this directory entry.
172pub fn address_range(&self) -> (u32, u32) {
173 (self.virtual_address.get(LE), self.size.get(LE))
174 }
175176/// Return the file offset and size of this directory entry.
177 ///
178 /// This function has some limitations:
179 /// - It requires that the data is contained in a single section.
180 /// - It uses the size field of the directory entry, which is
181 /// not desirable for all data directories.
182 /// - It uses the `virtual_address` of the directory entry as an address,
183 /// which is not valid for `IMAGE_DIRECTORY_ENTRY_SECURITY`.
184pub fn file_range(&self, sections: &SectionTable<'_>) -> Result<(u32, u32)> {
185let (offset, section_size) = sections
186 .pe_file_range_at(self.virtual_address.get(LE))
187 .read_error("Invalid data dir virtual address")?;
188let size = self.size.get(LE);
189if size > section_size {
190return Err(Error("Invalid data dir size"));
191 }
192Ok((offset, size))
193 }
194195/// Get the data referenced by this directory entry.
196 ///
197 /// This function has some limitations:
198 /// - It requires that the data is contained in a single section.
199 /// - It uses the size field of the directory entry, which is
200 /// not desirable for all data directories.
201 /// - It uses the `virtual_address` of the directory entry as an address,
202 /// which is not valid for `IMAGE_DIRECTORY_ENTRY_SECURITY`.
203pub fn data<'data, R: ReadRef<'data>>(
204&self,
205 data: R,
206 sections: &SectionTable<'data>,
207 ) -> Result<&'data [u8]> {
208 sections
209 .pe_data_at(data, self.virtual_address.get(LE))
210 .read_error("Invalid data dir virtual address")?
211.get(..self.size.get(LE) as usize)
212 .read_error("Invalid data dir size")
213 }
214}