Line data Source code
1 : use lief_ffi as ffi;
2 :
3 : use crate::common::FromFFI;
4 : use std::marker::PhantomData;
5 : use crate::pdb::types::PdbType;
6 :
7 : /// This class represents a primitive types (int, float, ...) which are
8 : /// also named *simple* types in the PDB format.
9 : pub struct Simple<'a> {
10 : ptr: cxx::UniquePtr<ffi::PDB_types_Simple>,
11 : _owner: PhantomData<&'a ()>,
12 : }
13 :
14 : impl FromFFI<ffi::PDB_types_Simple> for Simple<'_> {
15 0 : fn from_ffi(cmd: cxx::UniquePtr<ffi::PDB_types_Simple>) -> Self {
16 0 : Self {
17 0 : ptr: cmd,
18 0 : _owner: PhantomData,
19 0 : }
20 0 : }
21 : }
22 :
23 : #[allow(non_camel_case_types)]
24 0 : #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
25 : pub enum SimpleType {
26 : /// Void type (return type or void*)
27 : VOID,
28 :
29 : // --- Characters ---
30 : /// Signed Character
31 : SCHAR,
32 : /// Unsigned Character
33 : UCHAR,
34 : /// "Real" Character (char)
35 : RCHAR,
36 :
37 : // --- Unicode / Wide Characters ---
38 : /// Wide Character (wchar_t)
39 : WCHAR,
40 : /// 16-bit Character (char16_t)
41 : CHAR16,
42 : /// 32-bit Character (char32_t)
43 : CHAR32,
44 : /// 8-bit Character (char8_t)
45 : CHAR8,
46 :
47 : // --- Bytes ---
48 : /// Signed Byte
49 : SBYTE,
50 : /// Unsigned Byte
51 : UBYTE,
52 :
53 : // --- Short (16-bit) ---
54 : /// Signed Short
55 : SSHORT,
56 : /// Unsigned Short
57 : USHORT,
58 :
59 : /// Explicit Signed 16-bit Integer
60 : SINT16,
61 : /// Explicit Unsigned 16-bit Integer
62 : UINT16,
63 :
64 : // --- Long (32-bit) ---
65 : /// Signed Long
66 : SLONG,
67 : /// Unsigned Long
68 : ULONG,
69 :
70 : /// Explicit Signed 32-bit Integer
71 : SINT32,
72 : /// Explicit Unsigned 32-bit Integer
73 : UINT32,
74 :
75 : // --- Quad (64-bit) ---
76 : /// Signed Quadword
77 : SQUAD,
78 : /// Unsigned Quadword
79 : UQUAD,
80 :
81 : /// Explicit Signed 64-bit Integer
82 : SINT64,
83 : /// Explicit Unsigned 64-bit Integer
84 : UINT64,
85 :
86 : // --- Octa (128-bit) ---
87 : /// Signed Octaword
88 : SOCTA,
89 : /// Unsigned Octaword
90 : UOCTA,
91 :
92 : /// Explicit Signed 128-bit Integer
93 : SINT128,
94 : /// Explicit Unsigned 128-bit Integer
95 : UINT128,
96 :
97 : // --- Floating Point ---
98 : /// 16-bit Floating point
99 : FLOAT16,
100 : /// 32-bit Floating point (float)
101 : FLOAT32,
102 : FLOAT32_PARTIAL_PRECISION,
103 :
104 : /// 48-bit Floating point
105 : FLOAT48,
106 : /// 64-bit Floating point (double)
107 : FLOAT64,
108 : /// 80-bit Floating point
109 : FLOAT80,
110 : /// 128-bit Floating point
111 : FLOAT128,
112 :
113 : // --- Complex Numbers ---
114 : COMPLEX16,
115 : COMPLEX32,
116 : COMPLEX32_PARTIAL_PRECISION,
117 : COMPLEX48,
118 : COMPLEX64,
119 : COMPLEX80,
120 : COMPLEX128,
121 :
122 : // --- Booleans ---
123 : /// 8-bit Boolean
124 : BOOL8,
125 : /// 16-bit Boolean
126 : BOOL16,
127 : /// 32-bit Boolean
128 : BOOL32,
129 : /// 64-bit Boolean
130 : BOOL64,
131 : /// 128-bit Boolean
132 : BOOL128,
133 :
134 : UNKNOWN(u32),
135 : }
136 :
137 : impl From<u32> for SimpleType {
138 0 : fn from(value: u32) -> Self {
139 0 : match value {
140 0 : 0x0003 => SimpleType::VOID,
141 0 : 0x0010 => SimpleType::SCHAR,
142 0 : 0x0020 => SimpleType::UCHAR,
143 0 : 0x0070 => SimpleType::RCHAR,
144 0 : 0x0071 => SimpleType::WCHAR,
145 0 : 0x007a => SimpleType::CHAR16,
146 0 : 0x007b => SimpleType::CHAR32,
147 0 : 0x007c => SimpleType::CHAR8,
148 0 : 0x0068 => SimpleType::SBYTE,
149 0 : 0x0069 => SimpleType::UBYTE,
150 0 : 0x0011 => SimpleType::SSHORT,
151 0 : 0x0021 => SimpleType::USHORT,
152 0 : 0x0072 => SimpleType::SINT16,
153 0 : 0x0073 => SimpleType::UINT16,
154 0 : 0x0012 => SimpleType::SLONG,
155 0 : 0x0022 => SimpleType::ULONG,
156 0 : 0x0074 => SimpleType::SINT32,
157 0 : 0x0075 => SimpleType::UINT32,
158 0 : 0x0013 => SimpleType::SQUAD,
159 0 : 0x0023 => SimpleType::UQUAD,
160 0 : 0x0076 => SimpleType::SINT64,
161 0 : 0x0077 => SimpleType::UINT64,
162 0 : 0x0014 => SimpleType::SOCTA,
163 0 : 0x0024 => SimpleType::UOCTA,
164 0 : 0x0078 => SimpleType::SINT128,
165 0 : 0x0079 => SimpleType::UINT128,
166 0 : 0x0046 => SimpleType::FLOAT16,
167 0 : 0x0040 => SimpleType::FLOAT32,
168 0 : 0x0045 => SimpleType::FLOAT32_PARTIAL_PRECISION,
169 0 : 0x0044 => SimpleType::FLOAT48,
170 0 : 0x0041 => SimpleType::FLOAT64,
171 0 : 0x0042 => SimpleType::FLOAT80,
172 0 : 0x0043 => SimpleType::FLOAT128,
173 0 : 0x0056 => SimpleType::COMPLEX16,
174 0 : 0x0050 => SimpleType::COMPLEX32,
175 0 : 0x0055 => SimpleType::COMPLEX32_PARTIAL_PRECISION,
176 0 : 0x0054 => SimpleType::COMPLEX48,
177 0 : 0x0051 => SimpleType::COMPLEX64,
178 0 : 0x0052 => SimpleType::COMPLEX80,
179 0 : 0x0053 => SimpleType::COMPLEX128,
180 0 : 0x0030 => SimpleType::BOOL8,
181 0 : 0x0031 => SimpleType::BOOL16,
182 0 : 0x0032 => SimpleType::BOOL32,
183 0 : 0x0033 => SimpleType::BOOL64,
184 0 : 0x0034 => SimpleType::BOOL128,
185 0 : _ => SimpleType::UNKNOWN(value),
186 : }
187 0 : }
188 : }
189 :
190 : impl From<SimpleType> for u32 {
191 0 : fn from(value: SimpleType) -> u32 {
192 0 : match value {
193 0 : SimpleType::VOID => 0x0003,
194 0 : SimpleType::SCHAR => 0x0010,
195 0 : SimpleType::UCHAR => 0x0020,
196 0 : SimpleType::RCHAR => 0x0070,
197 0 : SimpleType::WCHAR => 0x0071,
198 0 : SimpleType::CHAR16 => 0x007a,
199 0 : SimpleType::CHAR32 => 0x007b,
200 0 : SimpleType::CHAR8 => 0x007c,
201 0 : SimpleType::SBYTE => 0x0068,
202 0 : SimpleType::UBYTE => 0x0069,
203 0 : SimpleType::SSHORT => 0x0011,
204 0 : SimpleType::USHORT => 0x0021,
205 0 : SimpleType::SINT16 => 0x0072,
206 0 : SimpleType::UINT16 => 0x0073,
207 0 : SimpleType::SLONG => 0x0012,
208 0 : SimpleType::ULONG => 0x0022,
209 0 : SimpleType::SINT32 => 0x0074,
210 0 : SimpleType::UINT32 => 0x0075,
211 0 : SimpleType::SQUAD => 0x0013,
212 0 : SimpleType::UQUAD => 0x0023,
213 0 : SimpleType::SINT64 => 0x0076,
214 0 : SimpleType::UINT64 => 0x0077,
215 0 : SimpleType::SOCTA => 0x0014,
216 0 : SimpleType::UOCTA => 0x0024,
217 0 : SimpleType::SINT128 => 0x0078,
218 0 : SimpleType::UINT128 => 0x0079,
219 0 : SimpleType::FLOAT16 => 0x0046,
220 0 : SimpleType::FLOAT32 => 0x0040,
221 0 : SimpleType::FLOAT32_PARTIAL_PRECISION => 0x0045,
222 0 : SimpleType::FLOAT48 => 0x0044,
223 0 : SimpleType::FLOAT64 => 0x0041,
224 0 : SimpleType::FLOAT80 => 0x0042,
225 0 : SimpleType::FLOAT128 => 0x0043,
226 0 : SimpleType::COMPLEX16 => 0x0056,
227 0 : SimpleType::COMPLEX32 => 0x0050,
228 0 : SimpleType::COMPLEX32_PARTIAL_PRECISION => 0x0055,
229 0 : SimpleType::COMPLEX48 => 0x0054,
230 0 : SimpleType::COMPLEX64 => 0x0051,
231 0 : SimpleType::COMPLEX80 => 0x0052,
232 0 : SimpleType::COMPLEX128 => 0x0053,
233 0 : SimpleType::BOOL8 => 0x0030,
234 0 : SimpleType::BOOL16 => 0x0031,
235 0 : SimpleType::BOOL32 => 0x0032,
236 0 : SimpleType::BOOL64 => 0x0033,
237 0 : SimpleType::BOOL128 => 0x0034,
238 0 : SimpleType::UNKNOWN(v) => v,
239 : }
240 0 : }
241 : }
242 :
243 : #[allow(non_camel_case_types)]
244 0 : #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
245 : pub enum SimpleMode {
246 : /// Not a pointer (direct access)
247 : DIRECT,
248 : /// Far pointer
249 : FAR_POINTER,
250 : /// Huge pointer
251 : HUGE_POINTER,
252 : /// 32-bit Near pointer
253 : NEAR_POINTER32,
254 : /// 32-bit Far pointer
255 : FAR_POINTER32,
256 : /// 64-bit Near pointer
257 : NEAR_POINTER64,
258 : /// 128-bit Near pointer
259 : NEAR_POINTER128,
260 :
261 : UNKNOWN(u32),
262 : }
263 :
264 : impl From<u32> for SimpleMode {
265 0 : fn from(value: u32) -> Self {
266 0 : match value {
267 0 : 0x00000000 => SimpleMode::DIRECT,
268 0 : 0x00000200 => SimpleMode::FAR_POINTER,
269 0 : 0x00000300 => SimpleMode::HUGE_POINTER,
270 0 : 0x00000400 => SimpleMode::NEAR_POINTER32,
271 0 : 0x00000500 => SimpleMode::FAR_POINTER32,
272 0 : 0x00000600 => SimpleMode::NEAR_POINTER64,
273 0 : 0x00000700 => SimpleMode::NEAR_POINTER128,
274 0 : _ => SimpleMode::UNKNOWN(value),
275 : }
276 0 : }
277 : }
278 :
279 : impl From<SimpleMode> for u32 {
280 0 : fn from(value: SimpleMode) -> u32 {
281 0 : match value {
282 0 : SimpleMode::DIRECT => 0x00000000,
283 0 : SimpleMode::FAR_POINTER => 0x00000200,
284 0 : SimpleMode::HUGE_POINTER => 0x00000300,
285 0 : SimpleMode::NEAR_POINTER32 => 0x00000400,
286 0 : SimpleMode::FAR_POINTER32 => 0x00000500,
287 0 : SimpleMode::NEAR_POINTER64 => 0x00000600,
288 0 : SimpleMode::NEAR_POINTER128 => 0x00000700,
289 0 : SimpleMode::UNKNOWN(v) => v,
290 : }
291 0 : }
292 : }
293 :
294 : impl Simple<'_> {
295 : /// Check if this simple type is a pointer.
296 0 : pub fn is_pointer(&self) -> bool {
297 0 : self.ptr.is_pointer()
298 0 : }
299 :
300 : /// Check if the underlying type is signed
301 0 : pub fn is_signed(&self) -> bool {
302 0 : self.ptr.is_signed()
303 0 : }
304 :
305 : /// Returns the underlying primitive type.
306 0 : pub fn get_type(&self) -> SimpleType {
307 0 : SimpleType::from(self.ptr.get_type())
308 0 : }
309 :
310 : /// Returns the mode (pointer type) of this Simple type.
311 0 : pub fn modes(&self) -> SimpleMode {
312 0 : SimpleMode::from(self.ptr.modes())
313 0 : }
314 : }
315 :
316 : impl PdbType for Simple<'_> {
317 0 : fn get_base(&self) -> &ffi::PDB_Type {
318 0 : self.ptr.as_ref().unwrap().as_ref()
319 0 : }
320 : }
|