Line data Source code
1 : //! PE export module
2 :
3 : use lief_ffi as ffi;
4 :
5 : use crate::common::FromFFI;
6 : use crate::declare_iterator;
7 : use crate::generic;
8 :
9 : use std::marker::PhantomData;
10 :
11 : pub struct Export<'a> {
12 : ptr: cxx::UniquePtr<ffi::PE_Export>,
13 : _owner: PhantomData<&'a ffi::PE_Binary>,
14 : }
15 :
16 : impl Export<'_> {
17 : /// According to the PE specifications this value is reserved and should be set to 0
18 40 : pub fn export_flags(&self) -> u32 {
19 40 : self.ptr.export_flags()
20 40 : }
21 :
22 : /// The time and date that the export data was created
23 40 : pub fn timestamp(&self) -> u32 {
24 40 : self.ptr.timestamp()
25 40 : }
26 :
27 : /// The major version number (can be user-defined)
28 40 : pub fn major_version(&self) -> u32 {
29 40 : self.ptr.major_version()
30 40 : }
31 :
32 : /// The minor version number (can be user-defined)
33 40 : pub fn minor_version(&self) -> u32 {
34 40 : self.ptr.minor_version()
35 40 : }
36 :
37 : /// The starting number for the exports. Usually this value is set to 1
38 40 : pub fn ordinal_base(&self) -> u32 {
39 40 : self.ptr.ordinal_base()
40 40 : }
41 :
42 : /// The name of the library exported (e.g. `KERNEL32.dll`)
43 40 : pub fn name(&self) -> String {
44 40 : self.ptr.name().to_string()
45 40 : }
46 :
47 : /// Iterator over the different [`Entry`] exported by this table
48 40 : pub fn entries(&self) -> ExportEntries {
49 40 : ExportEntries::new(self.ptr.entries())
50 40 : }
51 : }
52 :
53 : impl std::fmt::Debug for Export<'_> {
54 40 : fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
55 40 : f.debug_struct("Export")
56 40 : .field("export_flags", &self.export_flags())
57 40 : .field("timestamp", &self.timestamp())
58 40 : .field("major_version", &self.major_version())
59 40 : .field("minor_version", &self.minor_version())
60 40 : .field("ordinal_base", &self.ordinal_base())
61 40 : .field("name", &self.name())
62 40 : .finish()
63 40 : }
64 : }
65 :
66 : impl<'a> FromFFI<ffi::PE_Export> for Export<'a> {
67 40 : fn from_ffi(ptr: cxx::UniquePtr<ffi::PE_Export>) -> Self {
68 40 : Export {
69 40 : ptr,
70 40 : _owner: PhantomData,
71 40 : }
72 40 : }
73 : }
74 :
75 : /// Structure which represents an entry in the export table.
76 : ///
77 : /// It implements the [`generic::Symbol`] trait that exposes [`generic::Symbol::name`] and
78 : /// [`generic::Symbol::value`].
79 : pub struct Entry<'a> {
80 : ptr: cxx::UniquePtr<ffi::PE_ExportEntry>,
81 : _owner: PhantomData<&'a ffi::PE_Export>,
82 : }
83 :
84 : impl Entry<'_> {
85 59910 : pub fn ordinal(&self) -> u16 {
86 59910 : self.ptr.ordinal()
87 59910 : }
88 59910 : pub fn address(&self) -> u32 {
89 59910 : self.ptr.address()
90 59910 : }
91 0 : pub fn is_extern(&self) -> bool {
92 0 : self.ptr.is_extern()
93 0 : }
94 0 : pub fn is_forwarded(&self) -> bool {
95 0 : self.ptr.is_forwarded()
96 0 : }
97 :
98 : /// Demangled representation of the symbol or an empty string if it can't
99 : /// be demangled
100 0 : pub fn demangled_name(&self) -> String {
101 0 : self.ptr.demangled_name().to_string()
102 0 : }
103 : }
104 :
105 : impl generic::Symbol for Entry<'_> {
106 179730 : fn as_generic(&self) -> &ffi::AbstractSymbol {
107 179730 : self.ptr.as_ref().unwrap().as_ref()
108 179730 : }
109 : }
110 :
111 : impl std::fmt::Debug for Entry<'_> {
112 59910 : fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
113 59910 : let base = self as &dyn generic::Symbol;
114 59910 : f.debug_struct("ExportEntry")
115 59910 : .field("base", &base)
116 59910 : .field("ordinal", &self.ordinal())
117 59910 : .field("address", &self.address())
118 59910 : .finish()
119 59910 : }
120 : }
121 :
122 : impl<'a> FromFFI<ffi::PE_ExportEntry> for Entry<'a> {
123 59910 : fn from_ffi(ptr: cxx::UniquePtr<ffi::PE_ExportEntry>) -> Self {
124 59910 : Entry {
125 59910 : ptr,
126 59910 : _owner: PhantomData,
127 59910 : }
128 59910 : }
129 : }
130 :
131 59910 : declare_iterator!(
132 59910 : ExportEntries,
133 59910 : Entry<'a>,
134 59910 : ffi::PE_ExportEntry,
135 59910 : ffi::PE_Export,
136 59910 : ffi::PE_Export_it_entries
137 59910 : );
|