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 32 : pub fn export_flags(&self) -> u32 {
19 32 : self.ptr.export_flags()
20 32 : }
21 :
22 : /// The time and date that the export data was created
23 32 : pub fn timestamp(&self) -> u32 {
24 32 : self.ptr.timestamp()
25 32 : }
26 :
27 : /// The major version number (can be user-defined)
28 32 : pub fn major_version(&self) -> u32 {
29 32 : self.ptr.major_version()
30 32 : }
31 :
32 : /// The minor version number (can be user-defined)
33 32 : pub fn minor_version(&self) -> u32 {
34 32 : self.ptr.minor_version()
35 32 : }
36 :
37 : /// The starting number for the exports. Usually this value is set to 1
38 32 : pub fn ordinal_base(&self) -> u32 {
39 32 : self.ptr.ordinal_base()
40 32 : }
41 :
42 : /// The name of the library exported (e.g. `KERNEL32.dll`)
43 32 : pub fn name(&self) -> String {
44 32 : self.ptr.name().to_string()
45 32 : }
46 :
47 : /// Iterator over the different [`Entry`] exported by this table
48 32 : pub fn entries(&self) -> ExportEntries {
49 32 : ExportEntries::new(self.ptr.entries())
50 32 : }
51 : }
52 :
53 : impl std::fmt::Debug for Export<'_> {
54 32 : fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
55 32 : f.debug_struct("Export")
56 32 : .field("export_flags", &self.export_flags())
57 32 : .field("timestamp", &self.timestamp())
58 32 : .field("major_version", &self.major_version())
59 32 : .field("minor_version", &self.minor_version())
60 32 : .field("ordinal_base", &self.ordinal_base())
61 32 : .field("name", &self.name())
62 32 : .finish()
63 32 : }
64 : }
65 :
66 : impl<'a> FromFFI<ffi::PE_Export> for Export<'a> {
67 32 : fn from_ffi(ptr: cxx::UniquePtr<ffi::PE_Export>) -> Self {
68 32 : Export {
69 32 : ptr,
70 32 : _owner: PhantomData,
71 32 : }
72 32 : }
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 47928 : pub fn ordinal(&self) -> u16 {
86 47928 : self.ptr.ordinal()
87 47928 : }
88 47928 : pub fn address(&self) -> u32 {
89 47928 : self.ptr.address()
90 47928 : }
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 143784 : fn as_generic(&self) -> &ffi::AbstractSymbol {
107 143784 : self.ptr.as_ref().unwrap().as_ref()
108 143784 : }
109 : }
110 :
111 : impl std::fmt::Debug for Entry<'_> {
112 47928 : fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
113 47928 : let base = self as &dyn generic::Symbol;
114 47928 : f.debug_struct("ExportEntry")
115 47928 : .field("base", &base)
116 47928 : .field("ordinal", &self.ordinal())
117 47928 : .field("address", &self.address())
118 47928 : .finish()
119 47928 : }
120 : }
121 :
122 : impl<'a> FromFFI<ffi::PE_ExportEntry> for Entry<'a> {
123 47928 : fn from_ffi(ptr: cxx::UniquePtr<ffi::PE_ExportEntry>) -> Self {
124 47928 : Entry {
125 47928 : ptr,
126 47928 : _owner: PhantomData,
127 47928 : }
128 47928 : }
129 : }
130 :
131 47928 : declare_iterator!(
132 47928 : ExportEntries,
133 47928 : Entry<'a>,
134 47928 : ffi::PE_ExportEntry,
135 47928 : ffi::PE_Export,
136 47928 : ffi::PE_Export_it_entries
137 47928 : );
|