BearParser
Portable Executable parsing library (from PE-bear)
Loading...
Searching...
No Matches
FileHdrWrapper.cpp
Go to the documentation of this file.
1#include "pe/FileHdrWrapper.h"
2#include "pe/PEFile.h"
3
4#include <time.h>
5#include <QDateTime>
6
7namespace util {
9 {
10 const time_t rawtime = (const time_t)timestamp;
11 QString format = "dddd, dd.MM.yyyy hh:mm:ss";
12 QDateTime date1(QDateTime(QDateTime::fromTime_t(rawtime)));
13 return date1.toUTC().toString(format) + " UTC";
14 }
15};
16
17
18std::map<DWORD, QString> FileHdrWrapper::s_fHdrCharact;
19std::map<DWORD, QString> FileHdrWrapper::s_machine;
20
22{
23 if (s_fHdrCharact.size() != 0) {
24 return; //already initialized
25 }
26 s_fHdrCharact[F_RELOCS_STRIPPED] = "Relocation info stripped from file.";
27 s_fHdrCharact[F_EXECUTABLE_IMAGE] = "File is executable (i.e. no unresolved external references).";
28 s_fHdrCharact[F_LINE_NUMS_STRIPPED] = "Line numbers stripped from file.";
29 s_fHdrCharact[F_LOCAL_SYMS_STRIPPED] = "Local symbols stripped from file.";
30 s_fHdrCharact[F_AGGRESIVE_WS_TRIM] = "Aggressively trim working set";
31 s_fHdrCharact[F_LARGE_ADDRESS_AWARE] = "App can handle >2gb addresses";
32 s_fHdrCharact[F_BYTES_REVERSED_LO] = "Bytes of machine word are reversed.";
33 s_fHdrCharact[F_MACHINE_32BIT] = "32 bit word machine.";
34 s_fHdrCharact[F_DEBUG_STRIPPED] = "Debugging info stripped from file in .DBG file";
35 s_fHdrCharact[F_REMOVABLE_RUN_FROM_SWAP] = "If Image is on removable media, copy and run from the swap file.";
36 s_fHdrCharact[F_NET_RUN_FROM_SWAP] = "If Image is on Net, copy and run from the swap file.";
37 s_fHdrCharact[F_SYSTEM] = "System File.";
38 s_fHdrCharact[F_DLL] = "File is a DLL.";
39 s_fHdrCharact[F_UP_SYSTEM_ONLY] = "File should only be run on a UP machine";
40 s_fHdrCharact[F_BYTES_REVERSED_HI] = "Bytes of machine word are reversed.";
41}
42
44{
45 if (s_fHdrCharact.size() == 0) initCharact();
46
47 std::vector<DWORD> chSet;
48 for (std::map<DWORD, QString>::iterator iter = s_fHdrCharact.begin(); iter != s_fHdrCharact.end(); ++iter) {
49 if (characteristics & iter->first) {
50 chSet.push_back(iter->first);
51 }
52 }
53 return chSet;
54}
55
57{
58 if (s_fHdrCharact.size() == 0)
60
61 if (s_fHdrCharact.find(charact) == s_fHdrCharact.end()) return "";
62 return s_fHdrCharact[charact];
63}
64
66{
67 s_machine[M_UNKNOWN] = "s_machine unknown";
68
69 s_machine[M_I386] = "Intel 386";
70 s_machine[M_R3000] = "MIPS little-endian, 0x160 big-endian";
71 s_machine[M_R4000] = "MIPS little-endian";
72 s_machine[M_R10000] = "MIPS little-endian";
73 s_machine[M_WCEMIPSV2] = " MIPS little-endian WCE v2";
74 s_machine[M_ALPHA] = "Alpha_AXP";
75 s_machine[M_SH3] = "SH3 little-endian";
76 s_machine[M_SH3DSP] = "SH3DSP";
77 s_machine[M_SH3E] = "SH3E little-endian";
78 s_machine[M_SH4] = "SH4 little-endian";
79 s_machine[M_SH5] = "SH5";
80 s_machine[M_ARM] = "ARM Little-Endian";
81 s_machine[M_THUMB] = "Thumb";
82 s_machine[M_THUMB2] = "Thumb2";
83 s_machine[M_AM33] = "AM33";
84 s_machine[M_POWERPC] = "IBM PowerPC Little-Endian";
85 s_machine[M_POWERPCFP] = "PowerRPCFP";
86 s_machine[M_IA64] = "Intel 64";
87 s_machine[M_MIPS16] = "MIPS";
88 s_machine[M_ALPHA64] = "ALPHA64";
89 s_machine[M_MIPSFPU] = "MIPS";
90 s_machine[M_MIPSFPU16] = "MIPS";
91 s_machine[M_AXP64] = "M_ALPHA64";
92 s_machine[M_TRICORE] = " Infineon";
93 s_machine[M_CEF] = "CEF";
94 s_machine[M_EBC] = "EFI Byte Code";
95 s_machine[M_AMD64] = "AMD64 (K8)";
96 s_machine[M_M32R] = "M32R little-endian";
97 s_machine[M_CEE] = "CEE";
98 s_machine[M_RISCV32] = "RISC-V 32-bit Address Space";
99 s_machine[M_RISCV64] = "RISC-V 64-bit Address Space";
100 s_machine[M_RISCV128] = "RISC-V 128-bit Address Space";
101 s_machine[M_ARM64LE] = "ARM64 Little Endian";
102 s_machine[M_LOONGARCH32] = "LoongArch 32-bit Processor Family";
103 s_machine[M_LOONGARCH64] = "LoongArch 64-bit Processor Family";
104 s_machine[M_LINUXDOTNET64] = "AMD64 .Net For Linux";
105 s_machine[M_OSXDOTNET64] = "AMD64 .Net For Mac OS";
106 s_machine[M_FREEBSDDOTNET64] = "AMD64 .Net For Free BSD";
107 s_machine[M_NETBSDDOTNET64] = "AMD64 .Net For Net BSD";
108 s_machine[M_SUNDOTNET64] = "AMD64 .Net For Sun (Oracle Solaris)";
109 s_machine[M_LINUXDOTNET32] = "Intel 386 .Net For Linux";
110 s_machine[M_OSXDOTNET32] = "Intel 386 .Net For Mac OS";
111 s_machine[M_FREEBSDDOTNET32] = "Intel 386 .Net For Free BSD";
112 s_machine[M_NETBSDDOTNET32] = "Intel 386 .Net For Net BSD";
113 s_machine[M_SUNDOTNET32] = "Intel 386 .Net For Sun (Oracle Solaris)";
114}
115
117{
118 if (s_machine.size() == 0)
119 initMachine();
120
121 if (s_machine.find(val) == s_machine.end()) return "";
122 return s_machine[val];
123}
124
126{
127 if (this->hdr) {
128 // use cached if exists
129 return (void*) hdr;
130 }
131 if (m_PE == NULL) return NULL;
132
135 if (!hdr) return NULL; //error
136
137 return (void*) hdr;
138}
139
141{
142 IMAGE_FILE_HEADER * hdr = reinterpret_cast<IMAGE_FILE_HEADER*>(getPtr());
143 if (!hdr) return NULL;
144
146 switch (fieldId) {
147 case MACHINE: return (void*) &fileHeader.Machine;
148 case SEC_NUM: return (void*) &fileHeader.NumberOfSections;
149 case TIMESTAMP: return (void*) &fileHeader.TimeDateStamp;
150 case SYMBOL_PTR: return (void*) &fileHeader.PointerToSymbolTable;
151 case SYMBOL_NUM: return (void*) &fileHeader.NumberOfSymbols;
152 case OPTHDR_SIZE: return (void*) &fileHeader.SizeOfOptionalHeader;
153 case CHARACT: return (void*) &fileHeader.Characteristics;
154 }
155 return (void*) &fileHeader;
156}
157
159{
160 switch (fieldId) {
161 case MACHINE: return("Machine");
162 case SEC_NUM: return ("Sections Count");
163 case TIMESTAMP: {
164 PEFile* myPe = dynamic_cast<PEFile*>(this->m_Exe);
165 if (myPe && myPe->isReproBuild()) {
166 return "ReproChecksum";
167 }
168 return("Time Date Stamp");
169 }
170 case SYMBOL_PTR: return("Ptr to Symbol Table");
171 case SYMBOL_NUM: return("Num. of Symbols");
172 case OPTHDR_SIZE: return("Size of OptionalHeader");
173 case CHARACT: return("Characteristics");
174 }
175 return "";
176}
177
182
184{
185 IMAGE_FILE_HEADER * hdr = reinterpret_cast<IMAGE_FILE_HEADER*>(getPtr());
186 if (!hdr) return "";
187
189 switch (fieldId) {
191 case TIMESTAMP: {
192 PEFile* myPe = dynamic_cast<PEFile*>(this->m_Exe);
193 if (myPe && myPe->isReproBuild()) {
194 return ""; // This is not a real timestamp
195 }
196 return util::getDateString(fileHeader.TimeDateStamp);
197 }
198 }
199 return "";
200}
201
INT_TYPE _getNumValue(void *ptr)
uint64_t offset_t
BYTE * getContentAt(offset_t offset, bufsize_t size, bool allowExceptions=false)
Definition Executable.h:57
static QString translateMachine(DWORD val)
virtual QString translateFieldContent(size_t fieldId)
virtual void * getFieldPtr(size_t fieldId, size_t subField=FIELD_NONE)
virtual Executable::addr_type containsAddrType(size_t fieldId, size_t subField=FIELD_NONE)
static std::vector< DWORD > splitCharact(DWORD characteristics)
virtual void * getPtr()
virtual QString getFieldName(size_t fieldId)
static void initCharact()
static std::map< DWORD, QString > s_fHdrCharact
static std::map< DWORD, QString > s_machine
static void initMachine()
static QString translateCharacteristics(DWORD charact)
offset_t peFileHdrOffset() const
Definition PEFile.h:75
bool isReproBuild()
Definition PEFile.h:287
QString getDateString(const quint64 timestamp)