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
use alloc::string::String;
use crate::read::{ReadError, ReadRef, Result};
use crate::{pe, LittleEndian as LE, U16};
#[derive(Debug, Clone, Copy)]
pub struct ResourceDirectory<'data> {
data: &'data [u8],
}
impl<'data> ResourceDirectory<'data> {
pub fn new(data: &'data [u8]) -> Self {
ResourceDirectory { data }
}
pub fn root(&self) -> Result<ResourceDirectoryTable<'data>> {
ResourceDirectoryTable::parse(&self.data, 0)
}
}
#[derive(Debug, Clone)]
pub struct ResourceDirectoryTable<'data> {
pub header: &'data pe::ImageResourceDirectory,
pub entries: &'data [pe::ImageResourceDirectoryEntry],
}
impl<'data> ResourceDirectoryTable<'data> {
fn parse(data: &'data [u8], offset: u32) -> Result<Self> {
let mut offset = u64::from(offset);
let header = data
.read::<pe::ImageResourceDirectory>(&mut offset)
.read_error("Invalid resource table header")?;
let entries_count = header.number_of_id_entries.get(LE) as usize
+ header.number_of_named_entries.get(LE) as usize;
let entries = data
.read_slice::<pe::ImageResourceDirectoryEntry>(&mut offset, entries_count)
.read_error("Invalid resource table entries")?;
Ok(Self { header, entries })
}
}
impl pe::ImageResourceDirectoryEntry {
pub fn has_name(&self) -> bool {
self.name_or_id.get(LE) & pe::IMAGE_RESOURCE_NAME_IS_STRING != 0
}
fn name(&self) -> ResourceName {
let offset = self.name_or_id.get(LE) & !pe::IMAGE_RESOURCE_NAME_IS_STRING;
ResourceName { offset }
}
fn id(&self) -> u16 {
(self.name_or_id.get(LE) & 0x0000_FFFF) as u16
}
pub fn name_or_id(&self) -> ResourceNameOrId {
if self.has_name() {
ResourceNameOrId::Name(self.name())
} else {
ResourceNameOrId::Id(self.id())
}
}
pub fn is_table(&self) -> bool {
self.offset_to_data_or_directory.get(LE) & pe::IMAGE_RESOURCE_DATA_IS_DIRECTORY != 0
}
pub fn data_offset(&self) -> u32 {
self.offset_to_data_or_directory.get(LE) & !pe::IMAGE_RESOURCE_DATA_IS_DIRECTORY
}
pub fn data<'data>(
&self,
section: ResourceDirectory<'data>,
) -> Result<ResourceDirectoryEntryData<'data>> {
if self.is_table() {
ResourceDirectoryTable::parse(section.data, self.data_offset())
.map(|t| ResourceDirectoryEntryData::Table(t))
} else {
section
.data
.read_at::<pe::ImageResourceDataEntry>(self.data_offset().into())
.read_error("Invalid resource entry")
.map(|d| ResourceDirectoryEntryData::Data(d))
}
}
}
#[derive(Debug, Clone)]
pub enum ResourceDirectoryEntryData<'data> {
Table(ResourceDirectoryTable<'data>),
Data(&'data pe::ImageResourceDataEntry),
}
impl<'data> ResourceDirectoryEntryData<'data> {
pub fn table(self) -> Option<ResourceDirectoryTable<'data>> {
match self {
Self::Table(dir) => Some(dir),
_ => None,
}
}
pub fn data(self) -> Option<&'data pe::ImageResourceDataEntry> {
match self {
Self::Data(rsc) => Some(rsc),
_ => None,
}
}
}
#[derive(Debug, Clone, Copy)]
pub struct ResourceName {
offset: u32,
}
impl ResourceName {
pub fn to_string_lossy(&self, directory: ResourceDirectory) -> Result<String> {
let d = self.data(directory)?;
Ok(String::from_utf16_lossy(d))
}
pub fn data<'data>(&self, directory: ResourceDirectory<'data>) -> Result<&'data [u16]> {
let mut offset = u64::from(self.offset);
let len = directory
.data
.read::<U16<LE>>(&mut offset)
.read_error("Invalid resource name offset")?;
directory
.data
.read_slice::<u16>(&mut offset, len.get(LE).into())
.read_error("Invalid resource name length")
}
}
#[derive(Debug)]
pub enum ResourceNameOrId {
Name(ResourceName),
Id(u16),
}
impl ResourceNameOrId {
pub fn name(self) -> Option<ResourceName> {
match self {
Self::Name(name) => Some(name),
_ => None,
}
}
pub fn id(self) -> Option<u16> {
match self {
Self::Id(id) => Some(id),
_ => None,
}
}
}