diff options
Diffstat (limited to 'utils/zenutils/libraries/pelib-0.9/pelib/PeFile.h')
-rwxr-xr-x | utils/zenutils/libraries/pelib-0.9/pelib/PeFile.h | 451 |
1 files changed, 451 insertions, 0 deletions
diff --git a/utils/zenutils/libraries/pelib-0.9/pelib/PeFile.h b/utils/zenutils/libraries/pelib-0.9/pelib/PeFile.h new file mode 100755 index 0000000000..a2b02cdfa8 --- /dev/null +++ b/utils/zenutils/libraries/pelib-0.9/pelib/PeFile.h | |||
@@ -0,0 +1,451 @@ | |||
1 | /* | ||
2 | * PeFile.h - 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 | #ifndef PEFILE_H | ||
14 | #define PEFILE_H | ||
15 | |||
16 | #include "PeLibInc.h" | ||
17 | #include "MzHeader.h" | ||
18 | #include "PeHeader.h" | ||
19 | #include "ImportDirectory.h" | ||
20 | #include "ExportDirectory.h" | ||
21 | #include "BoundImportDirectory.h" | ||
22 | #include "ResourceDirectory.h" | ||
23 | #include "RelocationsDirectory.h" | ||
24 | #include "ComHeaderDirectory.h" | ||
25 | #include "IatDirectory.h" | ||
26 | #include "DebugDirectory.h" | ||
27 | #include "TlsDirectory.h" | ||
28 | |||
29 | namespace PeLib | ||
30 | { | ||
31 | class PeFile32; | ||
32 | class PeFile64; | ||
33 | |||
34 | /** | ||
35 | * Visitor base class for PeFiles. | ||
36 | **/ | ||
37 | class PeFileVisitor | ||
38 | { | ||
39 | public: | ||
40 | virtual void callback(PeFile32 &file){} | ||
41 | virtual void callback(PeFile64 &file){} | ||
42 | virtual ~PeFileVisitor(){} | ||
43 | }; | ||
44 | |||
45 | /** | ||
46 | * Traits class that's used to decide of what type the PeHeader in a PeFile is. | ||
47 | **/ | ||
48 | template<int> | ||
49 | struct PeFile_Traits; | ||
50 | |||
51 | template<> | ||
52 | struct PeFile_Traits<32> | ||
53 | { | ||
54 | typedef PeHeader32 PeHeader32_64; | ||
55 | }; | ||
56 | |||
57 | template<> | ||
58 | struct PeFile_Traits<64> | ||
59 | { | ||
60 | typedef PeHeader64 PeHeader32_64; | ||
61 | }; | ||
62 | |||
63 | /** | ||
64 | * This class represents the common structures of PE and PE+ files. | ||
65 | **/ | ||
66 | class PeFile | ||
67 | { | ||
68 | protected: | ||
69 | std::string m_filename; ///< Name of the current file. | ||
70 | MzHeader m_mzh; ///< MZ header of the current file. | ||
71 | ExportDirectory m_expdir; ///< Export directory of the current file. | ||
72 | BoundImportDirectory m_boundimpdir; ///< BoundImportDirectory of the current file. | ||
73 | ResourceDirectory m_resdir; ///< ResourceDirectory of the current file. | ||
74 | RelocationsDirectory m_relocs; ///< Relocations directory of the current file. | ||
75 | ComHeaderDirectory m_comdesc; ///< COM+ descriptor directory of the current file. | ||
76 | IatDirectory m_iat; ///< Import address table of the current file. | ||
77 | DebugDirectory m_debugdir; | ||
78 | public: | ||
79 | virtual ~PeFile(); | ||
80 | |||
81 | /// Returns the name of the current file. | ||
82 | virtual std::string getFileName() const = 0; // EXPORT | ||
83 | /// Changes the name of the current file. | ||
84 | virtual void setFileName(std::string strFilename) = 0; // EXPORT | ||
85 | |||
86 | virtual void visit(PeFileVisitor &v) = 0; | ||
87 | |||
88 | /// Reads the MZ header of the current file from disc. | ||
89 | virtual int readMzHeader() = 0; // EXPORT | ||
90 | /// Reads the export directory of the current file from disc. | ||
91 | virtual int readExportDirectory() = 0; // EXPORT | ||
92 | /// Reads the PE header of the current file from disc. | ||
93 | virtual int readPeHeader() = 0; // EXPORT | ||
94 | /// Reads the import directory of the current file from disc. | ||
95 | virtual int readImportDirectory() = 0; // EXPORT | ||
96 | /// Reads the bound import directory of the current file from disc. | ||
97 | virtual int readBoundImportDirectory() = 0; // EXPORT | ||
98 | /// Reads the resource directory of the current file from disc. | ||
99 | virtual int readResourceDirectory() = 0; // EXPORT | ||
100 | /// Reads the relocations directory of the current file from disc. | ||
101 | virtual int readRelocationsDirectory() = 0; // EXPORT | ||
102 | /// Reads the COM+ descriptor directory of the current file from disc. | ||
103 | virtual int readComHeaderDirectory() = 0; // EXPORT | ||
104 | /// Reads the IAT directory of the current file from disc. | ||
105 | virtual int readIatDirectory() = 0; // EXPORT | ||
106 | /// Reads the Debug directory of the current file. | ||
107 | virtual int readDebugDirectory() = 0; // EXPORT | ||
108 | virtual int readTlsDirectory() = 0; // EXPORT | ||
109 | |||
110 | virtual unsigned int getBits() const = 0; | ||
111 | |||
112 | /// Accessor function for the MZ header. | ||
113 | const MzHeader& mzHeader() const; | ||
114 | /// Accessor function for the MZ header. | ||
115 | MzHeader& mzHeader(); // EXPORT | ||
116 | |||
117 | /// Accessor function for the export directory. | ||
118 | const ExportDirectory& expDir() const; | ||
119 | /// Accessor function for the export directory. | ||
120 | ExportDirectory& expDir(); // EXPORT | ||
121 | |||
122 | /// Accessor function for the bound import directory. | ||
123 | const BoundImportDirectory& boundImpDir() const; | ||
124 | /// Accessor function for the bound import directory. | ||
125 | BoundImportDirectory& boundImpDir(); // EXPORT | ||
126 | |||
127 | /// Accessor function for the resource directory. | ||
128 | const ResourceDirectory& resDir() const; | ||
129 | /// Accessor function for the resource directory. | ||
130 | ResourceDirectory& resDir(); // EXPORT | ||
131 | |||
132 | /// Accessor function for the relocations directory. | ||
133 | const RelocationsDirectory& relocDir() const; | ||
134 | /// Accessor function for the relocations directory. | ||
135 | RelocationsDirectory& relocDir(); // EXPORT | ||
136 | |||
137 | /// Accessor function for the COM+ descriptor directory. | ||
138 | const ComHeaderDirectory& comDir() const; | ||
139 | /// Accessor function for the COM+ descriptor directory. | ||
140 | ComHeaderDirectory& comDir(); // EXPORT | ||
141 | |||
142 | /// Accessor function for the IAT directory. | ||
143 | const IatDirectory& iatDir() const; | ||
144 | /// Accessor function for the IAT directory. | ||
145 | IatDirectory& iatDir(); // EXPORT | ||
146 | |||
147 | /// Accessor function for the debug directory. | ||
148 | const DebugDirectory& debugDir() const; | ||
149 | /// Accessor function for the debug directory. | ||
150 | DebugDirectory& debugDir(); // EXPORT | ||
151 | |||
152 | }; | ||
153 | |||
154 | /** | ||
155 | * This class implements the common structures of PE and PE+ files. | ||
156 | **/ | ||
157 | template<int bits> | ||
158 | class PeFileT : public PeFile | ||
159 | { | ||
160 | typedef typename PeFile_Traits<bits>::PeHeader32_64 PeHeader32_64; | ||
161 | |||
162 | private: | ||
163 | PeHeader32_64 m_peh; ///< PE header of the current file. | ||
164 | ImportDirectory<bits> m_impdir; ///< Import directory of the current file. | ||
165 | TlsDirectory<bits> m_tlsdir; | ||
166 | |||
167 | public: | ||
168 | /// Default constructor which exists only for the sake of allowing to construct files without filenames. | ||
169 | PeFileT(); | ||
170 | |||
171 | virtual ~PeFileT() {} | ||
172 | |||
173 | /// Initializes a PeFile with a filename | ||
174 | explicit PeFileT(const std::string& strFilename); | ||
175 | |||
176 | /// Returns the name of the current file. | ||
177 | std::string getFileName() const; | ||
178 | /// Changes the name of the current file. | ||
179 | void setFileName(std::string strFilename); | ||
180 | |||
181 | /// Reads the MZ header of the current file from disc. | ||
182 | int readMzHeader() ; | ||
183 | /// Reads the export directory of the current file from disc. | ||
184 | int readExportDirectory() ; | ||
185 | /// Reads the PE header of the current file from disc. | ||
186 | int readPeHeader() ; | ||
187 | /// Reads the import directory of the current file from disc. | ||
188 | int readImportDirectory() ; | ||
189 | /// Reads the bound import directory of the current file from disc. | ||
190 | int readBoundImportDirectory() ; | ||
191 | /// Reads the resource directory of the current file from disc. | ||
192 | int readResourceDirectory() ; | ||
193 | /// Reads the relocations directory of the current file from disc. | ||
194 | int readRelocationsDirectory() ; | ||
195 | /// Reads the COM+ descriptor directory of the current file from disc. | ||
196 | int readComHeaderDirectory() ; | ||
197 | /// Reads the IAT directory of the current file from disc. | ||
198 | int readIatDirectory() ; | ||
199 | /// Reads the Debug directory of the current file. | ||
200 | int readDebugDirectory() ; | ||
201 | int readTlsDirectory() ; | ||
202 | |||
203 | unsigned int getBits() const | ||
204 | { | ||
205 | return bits; | ||
206 | } | ||
207 | |||
208 | /// Accessor function for the PE header. | ||
209 | const PeHeader32_64& peHeader() const; | ||
210 | /// Accessor function for the PE header. | ||
211 | PeHeader32_64& peHeader(); | ||
212 | |||
213 | /// Accessor function for the import directory. | ||
214 | const ImportDirectory<bits>& impDir() const; | ||
215 | /// Accessor function for the import directory. | ||
216 | ImportDirectory<bits>& impDir(); | ||
217 | |||
218 | const TlsDirectory<bits>& tlsDir() const; | ||
219 | TlsDirectory<bits>& tlsDir(); | ||
220 | }; | ||
221 | |||
222 | /** | ||
223 | * This class is the main class for handling PE files. | ||
224 | **/ | ||
225 | class PeFile32 : public PeFileT<32> | ||
226 | { | ||
227 | public: | ||
228 | /// Default constructor which exists only for the sake of allowing to construct files without filenames. | ||
229 | PeFile32(); | ||
230 | |||
231 | /// Initializes a PeFile with a filename | ||
232 | explicit PeFile32(const std::string& strFlename); | ||
233 | virtual void visit(PeFileVisitor &v) { v.callback( *this ); } | ||
234 | }; | ||
235 | |||
236 | /** | ||
237 | * This class is the main class for handling PE+ files. | ||
238 | **/ | ||
239 | class PeFile64 : public PeFileT<64> | ||
240 | { | ||
241 | public: | ||
242 | /// Default constructor which exists only for the sake of allowing to construct files without filenames. | ||
243 | PeFile64(); | ||
244 | |||
245 | /// Initializes a PeFile with a filename | ||
246 | explicit PeFile64(const std::string& strFlename); | ||
247 | virtual void visit(PeFileVisitor &v) { v.callback( *this ); } | ||
248 | }; | ||
249 | |||
250 | //typedef PeFileT<32> PeFile32; | ||
251 | //typedef PeFileT<64> PeFile64; | ||
252 | |||
253 | /** | ||
254 | * @param strFilename Name of the current file. | ||
255 | **/ | ||
256 | template<int bits> | ||
257 | PeFileT<bits>::PeFileT(const std::string& strFilename) | ||
258 | { | ||
259 | m_filename = strFilename; | ||
260 | } | ||
261 | |||
262 | template<int bits> | ||
263 | PeFileT<bits>::PeFileT() | ||
264 | { | ||
265 | } | ||
266 | |||
267 | template<int bits> | ||
268 | int PeFileT<bits>::readPeHeader() | ||
269 | { | ||
270 | return peHeader().read(getFileName(), mzHeader().getAddressOfPeHeader()); | ||
271 | } | ||
272 | |||
273 | |||
274 | template<int bits> | ||
275 | int PeFileT<bits>::readImportDirectory() | ||
276 | { | ||
277 | if (peHeader().calcNumberOfRvaAndSizes() >= 2 | ||
278 | && peHeader().getIddImportRva() | ||
279 | && peHeader().getIddImportSize()) | ||
280 | { | ||
281 | return impDir().read(getFileName(), static_cast<unsigned int>(peHeader().rvaToOffset(peHeader().getIddImportRva())), peHeader().getIddImportSize(), peHeader()); | ||
282 | } | ||
283 | return ERROR_DIRECTORY_DOES_NOT_EXIST; | ||
284 | } | ||
285 | |||
286 | /** | ||
287 | * @return A reference to the file's PE header. | ||
288 | **/ | ||
289 | template<int bits> | ||
290 | const typename PeFile_Traits<bits>::PeHeader32_64& PeFileT<bits>::peHeader() const | ||
291 | { | ||
292 | return m_peh; | ||
293 | } | ||
294 | |||
295 | /** | ||
296 | * @return A reference to the file's PE header. | ||
297 | **/ | ||
298 | template<int bits> | ||
299 | typename PeFile_Traits<bits>::PeHeader32_64& PeFileT<bits>::peHeader() | ||
300 | { | ||
301 | return m_peh; | ||
302 | } | ||
303 | |||
304 | /** | ||
305 | * @return A reference to the file's import directory. | ||
306 | **/ | ||
307 | template<int bits> | ||
308 | const ImportDirectory<bits>& PeFileT<bits>::impDir() const | ||
309 | { | ||
310 | return m_impdir; | ||
311 | } | ||
312 | |||
313 | /** | ||
314 | * @return A reference to the file's import directory. | ||
315 | **/ | ||
316 | template<int bits> | ||
317 | ImportDirectory<bits>& PeFileT<bits>::impDir() | ||
318 | { | ||
319 | return m_impdir; | ||
320 | } | ||
321 | |||
322 | template<int bits> | ||
323 | const TlsDirectory<bits>& PeFileT<bits>::tlsDir() const | ||
324 | { | ||
325 | return m_tlsdir; | ||
326 | } | ||
327 | |||
328 | template<int bits> | ||
329 | TlsDirectory<bits>& PeFileT<bits>::tlsDir() | ||
330 | { | ||
331 | return m_tlsdir; | ||
332 | } | ||
333 | |||
334 | /** | ||
335 | * @return Filename of the current file. | ||
336 | **/ | ||
337 | template<int bits> | ||
338 | std::string PeFileT<bits>::getFileName() const | ||
339 | { | ||
340 | return m_filename; | ||
341 | } | ||
342 | |||
343 | /** | ||
344 | * @param strFilename New filename. | ||
345 | **/ | ||
346 | template<int bits> | ||
347 | void PeFileT<bits>::setFileName(std::string strFilename) | ||
348 | { | ||
349 | m_filename = strFilename; | ||
350 | } | ||
351 | |||
352 | template<int bits> | ||
353 | int PeFileT<bits>::readMzHeader() | ||
354 | { | ||
355 | return mzHeader().read(getFileName()); | ||
356 | } | ||
357 | |||
358 | template<int bits> | ||
359 | int PeFileT<bits>::readExportDirectory() | ||
360 | { | ||
361 | if (peHeader().calcNumberOfRvaAndSizes() >= 1 | ||
362 | && peHeader().getIddExportRva() && peHeader().getIddExportSize()) | ||
363 | { | ||
364 | return expDir().read(getFileName(), static_cast<unsigned int>(peHeader().rvaToOffset(peHeader().getIddExportRva())), peHeader().getIddExportSize(), peHeader()); | ||
365 | } | ||
366 | return ERROR_DIRECTORY_DOES_NOT_EXIST; | ||
367 | } | ||
368 | |||
369 | |||
370 | template<int bits> | ||
371 | int PeFileT<bits>::readBoundImportDirectory() | ||
372 | { | ||
373 | if (peHeader().calcNumberOfRvaAndSizes() >= 12 | ||
374 | && peHeader().getIddBoundImportRva() && peHeader().getIddBoundImportSize()) | ||
375 | { | ||
376 | return boundImpDir().read(getFileName(), static_cast<unsigned int>(peHeader().rvaToOffset(peHeader().getIddBoundImportRva())), peHeader().getIddBoundImportSize()); | ||
377 | } | ||
378 | return ERROR_DIRECTORY_DOES_NOT_EXIST; | ||
379 | } | ||
380 | |||
381 | template<int bits> | ||
382 | int PeFileT<bits>::readResourceDirectory() | ||
383 | { | ||
384 | if (peHeader().calcNumberOfRvaAndSizes() >= 3 | ||
385 | && peHeader().getIddResourceRva() && peHeader().getIddResourceSize()) | ||
386 | { | ||
387 | return resDir().read(getFileName(), static_cast<unsigned int>(peHeader().rvaToOffset(peHeader().getIddResourceRva())), peHeader().getIddResourceSize(), peHeader().getIddResourceRva()); | ||
388 | } | ||
389 | return ERROR_DIRECTORY_DOES_NOT_EXIST; | ||
390 | } | ||
391 | |||
392 | template<int bits> | ||
393 | int PeFileT<bits>::readRelocationsDirectory() | ||
394 | { | ||
395 | if (peHeader().calcNumberOfRvaAndSizes() >= 6 | ||
396 | && peHeader().getIddBaseRelocRva() && peHeader().getIddBaseRelocSize()) | ||
397 | { | ||
398 | return relocDir().read(getFileName(), static_cast<unsigned int>(peHeader().rvaToOffset(peHeader().getIddBaseRelocRva())), peHeader().getIddBaseRelocSize()); | ||
399 | } | ||
400 | return ERROR_DIRECTORY_DOES_NOT_EXIST; | ||
401 | } | ||
402 | |||
403 | template<int bits> | ||
404 | int PeFileT<bits>::readComHeaderDirectory() | ||
405 | { | ||
406 | if (peHeader().calcNumberOfRvaAndSizes() >= 15 | ||
407 | && peHeader().getIddComHeaderRva() && peHeader().getIddComHeaderSize()) | ||
408 | { | ||
409 | return comDir().read(getFileName(), static_cast<unsigned int>(peHeader().rvaToOffset(peHeader().getIddComHeaderRva())), peHeader().getIddComHeaderSize()); | ||
410 | } | ||
411 | std::cout << peHeader().getIddComHeaderRva() << std::endl; | ||
412 | std::exit(0); | ||
413 | return ERROR_DIRECTORY_DOES_NOT_EXIST; | ||
414 | } | ||
415 | |||
416 | template<int bits> | ||
417 | int PeFileT<bits>::readIatDirectory() | ||
418 | { | ||
419 | if (peHeader().calcNumberOfRvaAndSizes() >= 13 | ||
420 | && peHeader().getIddIatRva() && peHeader().getIddIatSize()) | ||
421 | { | ||
422 | return iatDir().read(getFileName(), static_cast<unsigned int>(peHeader().rvaToOffset(peHeader().getIddIatRva())), peHeader().getIddIatSize()); | ||
423 | } | ||
424 | return ERROR_DIRECTORY_DOES_NOT_EXIST; | ||
425 | } | ||
426 | |||
427 | template<int bits> | ||
428 | int PeFileT<bits>::readDebugDirectory() | ||
429 | { | ||
430 | if (peHeader().calcNumberOfRvaAndSizes() >= 7 | ||
431 | && peHeader().getIddDebugRva() && peHeader().getIddDebugSize()) | ||
432 | { | ||
433 | return debugDir().read(getFileName(), static_cast<unsigned int>(peHeader().rvaToOffset(peHeader().getIddDebugRva())), peHeader().getIddDebugSize()); | ||
434 | } | ||
435 | return ERROR_DIRECTORY_DOES_NOT_EXIST; | ||
436 | } | ||
437 | |||
438 | template<int bits> | ||
439 | int PeFileT<bits>::readTlsDirectory() | ||
440 | { | ||
441 | if (peHeader().calcNumberOfRvaAndSizes() >= 10 | ||
442 | && peHeader().getIddTlsRva() && peHeader().getIddTlsSize()) | ||
443 | { | ||
444 | return tlsDir().read(getFileName(), static_cast<unsigned int>(peHeader().rvaToOffset(peHeader().getIddTlsRva())), peHeader().getIddTlsSize()); | ||
445 | } | ||
446 | return ERROR_DIRECTORY_DOES_NOT_EXIST; | ||
447 | } | ||
448 | |||
449 | } | ||
450 | |||
451 | #endif | ||