summaryrefslogtreecommitdiff
path: root/utils/zenutils/libraries/pelib-0.9/pelib/PeLibAux.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'utils/zenutils/libraries/pelib-0.9/pelib/PeLibAux.cpp')
-rwxr-xr-xutils/zenutils/libraries/pelib-0.9/pelib/PeLibAux.cpp275
1 files changed, 275 insertions, 0 deletions
diff --git a/utils/zenutils/libraries/pelib-0.9/pelib/PeLibAux.cpp b/utils/zenutils/libraries/pelib-0.9/pelib/PeLibAux.cpp
new file mode 100755
index 0000000000..1e06bae620
--- /dev/null
+++ b/utils/zenutils/libraries/pelib-0.9/pelib/PeLibAux.cpp
@@ -0,0 +1,275 @@
1/*
2* PeLibAux.cpp - Part of the PeLib library.
3*
4* Copyright (c) 2004 - 2005 Sebastian Porst (webmaster@the-interweb.com)
5* All rights reserved.
6*
7* This software is licensed under the zlib/libpng License.
8* For more details see http://www.opensource.org/licenses/zlib-license.php
9* or the license information file (license.htm) in the root directory
10* of PeLib.
11*/
12
13#include "PeLibInc.h"
14#include "PeLibAux.h"
15#include "PeFile.h"
16
17#ifdef _MSC_VER
18 #include <ctype.h>
19#endif
20
21namespace PeLib
22{
23 const qword PELIB_IMAGE_ORDINAL_FLAGS<64>::IMAGE_ORDINAL_FLAG = 0x8000000000000000ULL;
24
25 bool PELIB_IMAGE_SECTION_HEADER::biggerFileOffset(const PELIB_IMAGE_SECTION_HEADER& ish) const
26 {
27 return PointerToRawData < ish.PointerToRawData;
28 }
29
30 bool PELIB_IMAGE_SECTION_HEADER::biggerVirtualAddress(const PELIB_IMAGE_SECTION_HEADER& ish) const
31 {
32 return VirtualAddress < ish.VirtualAddress;
33 }
34
35 unsigned int alignOffset(unsigned int uiOffset, unsigned int uiAlignment)
36 {
37 if (!uiAlignment) return uiAlignment;
38 return (uiOffset % uiAlignment) ? uiOffset + (uiAlignment - uiOffset % uiAlignment) : uiOffset;
39 }
40
41 unsigned int fileSize(const std::string& filename)
42 {
43 std::fstream file(filename.c_str());
44 file.seekg(0, std::ios::end);
45 return file.tellg();
46 }
47
48 unsigned int fileSize(std::ifstream& file)
49 {
50 unsigned int oldpos = file.tellg();
51 file.seekg(0, std::ios::end);
52 unsigned int filesize = file.tellg();
53 file.seekg(oldpos, std::ios::beg);
54 return filesize;
55 }
56
57 unsigned int fileSize(std::fstream& file)
58 {
59 unsigned int oldpos = file.tellg();
60 file.seekg(0, std::ios::end);
61 unsigned int filesize = file.tellg();
62 file.seekg(oldpos, std::ios::beg);
63 return filesize;
64 }
65
66 unsigned int fileSize(std::ofstream& file)
67 {
68 unsigned int oldpos = file.tellp();
69 file.seekp(0, std::ios::end);
70 unsigned int filesize = file.tellp();
71 file.seekp(oldpos, std::ios::beg);
72 return filesize;
73 }
74
75 bool isEqualNc(const std::string& s1, const std::string& s2)
76 {
77 std::string t1 = s1;
78 std::string t2 = s2;
79
80 // No std:: to make VC++ happy
81 #ifdef _MSC_VER
82 std::transform(t1.begin(), t1.end(), t1.begin(), toupper);
83 std::transform(t2.begin(), t2.end(), t2.begin(), toupper);
84 #else
85 // Weird syntax to make Borland C++ happy
86 std::transform(t1.begin(), t1.end(), t1.begin(), (int(*)(int))std::toupper);
87 std::transform(t2.begin(), t2.end(), t2.begin(), (int(*)(int))std::toupper);
88 #endif
89 return t1 == t2;
90 }
91
92 PELIB_IMAGE_DOS_HEADER::PELIB_IMAGE_DOS_HEADER()
93 {
94 e_magic = 0;
95 e_cblp = 0;
96 e_cp = 0;
97 e_crlc = 0;
98 e_cparhdr = 0;
99 e_minalloc = 0;
100 e_maxalloc = 0;
101 e_ss = 0;
102 e_sp = 0;
103 e_csum = 0;
104 e_ip = 0;
105 e_cs = 0;
106 e_lfarlc = 0;
107 e_ovno = 0;
108
109 for (unsigned int i=0;i<sizeof(e_res)/sizeof(e_res[0]);i++)
110 {
111 e_res[i] = 0;
112 }
113
114 e_oemid = 0;
115 e_oeminfo = 0;
116
117 for (unsigned int i=0;i<sizeof(e_res2)/sizeof(e_res2[0]);i++)
118 {
119 e_res2[i] = 0;
120 }
121
122 e_lfanew = 0;
123 }
124
125 PELIB_EXP_FUNC_INFORMATION::PELIB_EXP_FUNC_INFORMATION()
126 {
127 addroffunc = 0;
128 addrofname = 0;
129 ordinal = 0;
130 }
131
132 PELIB_IMAGE_RESOURCE_DIRECTORY::PELIB_IMAGE_RESOURCE_DIRECTORY()
133 {
134 Characteristics = 0;
135 TimeDateStamp = 0;
136 MajorVersion = 0;
137 MinorVersion = 0;
138 NumberOfNamedEntries = 0;
139 NumberOfIdEntries = 0;
140 }
141
142 PELIB_IMAGE_RESOURCE_DIRECTORY_ENTRY::PELIB_IMAGE_RESOURCE_DIRECTORY_ENTRY()
143 {
144 Name = 0;
145 OffsetToData = 0;
146 }
147
148 bool PELIB_IMG_RES_DIR_ENTRY::operator<(const PELIB_IMG_RES_DIR_ENTRY& first) const
149 {
150 if (irde.Name & PELIB_IMAGE_RESOURCE_NAME_IS_STRING && first.irde.Name & PELIB_IMAGE_RESOURCE_NAME_IS_STRING)
151 {
152 return wstrName < first.wstrName;
153 }
154 else if (irde.Name & PELIB_IMAGE_RESOURCE_NAME_IS_STRING)
155 {
156 return true;
157 }
158 else if (first.irde.Name & PELIB_IMAGE_RESOURCE_NAME_IS_STRING)
159 {
160 return false;
161 }
162 else
163 {
164 return irde.Name < first.irde.Name;
165 }
166 }
167
168 PELIB_IMAGE_BASE_RELOCATION::PELIB_IMAGE_BASE_RELOCATION()
169 {
170 VirtualAddress = 0;
171 SizeOfBlock = 0;
172 }
173
174 PELIB_IMAGE_COR20_HEADER::PELIB_IMAGE_COR20_HEADER()
175 {
176 cb = 0;
177 MajorRuntimeVersion = 0;
178 MinorRuntimeVersion = 0;
179 MetaData.VirtualAddress = 0;
180 MetaData.Size = 0;
181 Flags = 0;
182 EntryPointToken = 0;
183 Resources.VirtualAddress = 0;
184 Resources.Size = 0;
185 StrongNameSignature.VirtualAddress = 0;
186 StrongNameSignature.Size = 0;
187 CodeManagerTable.VirtualAddress = 0;
188 CodeManagerTable.Size = 0;
189 VTableFixups.VirtualAddress = 0;
190 VTableFixups.Size = 0;
191 ExportAddressTableJumps.VirtualAddress = 0;
192 ExportAddressTableJumps.Size = 0;
193 ManagedNativeHeader.VirtualAddress = 0;
194 ManagedNativeHeader.Size = 0;
195 }
196
197 /** Compares the passed filename to the struct's filename.
198 * @param strModuleName A filename.
199 * @return True, if the passed filename equals the struct's filename. The comparison is case-sensitive.
200 **/
201 bool PELIB_IMAGE_BOUND_DIRECTORY::equal(const std::string strModuleName) const
202 {
203 return this->strModuleName == strModuleName;
204 }
205
206 bool PELIB_EXP_FUNC_INFORMATION::equal(const std::string strFunctionName) const
207 {
208 return isEqualNc(this->funcname, strFunctionName);
209 }
210
211 /**
212 * @param strFilename Name of a file.
213 * @return Either PEFILE32, PEFILE64 or PEFILE_UNKNOWN
214 **/
215 unsigned int getFileType(const std::string strFilename)
216 {
217 word machine, magic;
218
219 PeFile32 pef(strFilename);
220 if (pef.readMzHeader() != NO_ERROR) return PEFILE_UNKNOWN;
221 if (pef.readPeHeader() != NO_ERROR) return PEFILE_UNKNOWN;
222
223 machine = pef.peHeader().getMachine();
224 magic = pef.peHeader().getMagic();
225
226 if (machine == PELIB_IMAGE_FILE_MACHINE_I386 && magic == PELIB_IMAGE_NT_OPTIONAL_HDR32_MAGIC)
227 {
228 return PEFILE32;
229 }
230 // 0x8664 == AMD64; no named constant yet
231 else if ((machine == 0x8664 || machine == PELIB_IMAGE_FILE_MACHINE_IA64) && magic == PELIB_IMAGE_NT_OPTIONAL_HDR64_MAGIC)
232 {
233 return PEFILE64;
234 }
235 else
236 {
237 return PEFILE_UNKNOWN;
238 }
239 }
240
241 /**
242 * Opens a PE file. The return type is either PeFile32 or PeFile64 object. If an error occurs the return
243 * value is 0.
244 * @param strFilename Name of a file.
245 * @return Either a PeFile32 object, a PeFil64 object or 0.
246 **/
247 PeFile* openPeFile(const std::string& strFilename)
248 {
249 unsigned int type = getFileType(strFilename);
250
251 if (type == PEFILE32)
252 {
253 return new PeFile32(strFilename);
254 }
255 else if (type == PEFILE64)
256 {
257 return new PeFile64(strFilename);
258 }
259 else
260 {
261 return 0;
262 }
263 }
264
265 unsigned int PELIB_IMAGE_BOUND_DIRECTORY::size() const
266 {
267 unsigned int size = 0;
268 for (unsigned int i=0;i<moduleForwarders.size();i++)
269 {
270 size += moduleForwarders[i].size();
271 }
272
273 return size + PELIB_IMAGE_BOUND_IMPORT_DESCRIPTOR::size() + strModuleName.size() + 1;
274 }
275}