diff options
Diffstat (limited to 'utils/zenutils/source/shared/pe.cpp')
-rwxr-xr-x | utils/zenutils/source/shared/pe.cpp | 128 |
1 files changed, 128 insertions, 0 deletions
diff --git a/utils/zenutils/source/shared/pe.cpp b/utils/zenutils/source/shared/pe.cpp new file mode 100755 index 0000000000..c86ec6c8cc --- /dev/null +++ b/utils/zenutils/source/shared/pe.cpp | |||
@@ -0,0 +1,128 @@ | |||
1 | /* zenutils - Utilities for working with creative firmwares. | ||
2 | * Copyright 2007 (c) Rasmus Ry <rasmus.ry{at}gmail.com> | ||
3 | * | ||
4 | * This program is free software; you can redistribute it and/or modify | ||
5 | * it under the terms of the GNU General Public License as published by | ||
6 | * the Free Software Foundation; either version 2 of the License, or | ||
7 | * (at your option) any later version. | ||
8 | * | ||
9 | * This program is distributed in the hope that it will be useful, | ||
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
12 | * GNU General Public License for more details. | ||
13 | * | ||
14 | * You should have received a copy of the GNU General Public License | ||
15 | * along with this program; if not, write to the Free Software | ||
16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
17 | */ | ||
18 | |||
19 | #include "pe.h" | ||
20 | |||
21 | |||
22 | shared::pe_file::pe_file(PeLib::PeFile* pef) : _pef(pef) | ||
23 | { | ||
24 | } | ||
25 | shared::pe_file::~pe_file() | ||
26 | { | ||
27 | if (_pef != NULL) | ||
28 | delete _pef; | ||
29 | } | ||
30 | |||
31 | bool shared::pe_file::is_valid() const | ||
32 | { | ||
33 | if (_pef->getBits() == 32) | ||
34 | { | ||
35 | PeLib::PeHeader32& pef32 = static_cast<PeLib::PeFile32*>(_pef)->peHeader(); | ||
36 | if (!pef32.isValid()) | ||
37 | return false; | ||
38 | return true; | ||
39 | } | ||
40 | else if (_pef->getBits() == 64) | ||
41 | { | ||
42 | PeLib::PeHeader64& pef64 = static_cast<PeLib::PeFile64*>(_pef)->peHeader(); | ||
43 | if (!pef64.isValid()) | ||
44 | return false; | ||
45 | return true; | ||
46 | } | ||
47 | return false; | ||
48 | } | ||
49 | |||
50 | bool shared::pe_file::read(const std::string& filename) | ||
51 | { | ||
52 | if (_pef != NULL) | ||
53 | { | ||
54 | delete _pef; | ||
55 | _pef = NULL; | ||
56 | } | ||
57 | |||
58 | _pef = PeLib::openPeFile(filename); | ||
59 | if (!_pef) | ||
60 | { | ||
61 | return false; | ||
62 | } | ||
63 | if (_pef->readMzHeader()) | ||
64 | { | ||
65 | delete _pef; | ||
66 | return false; | ||
67 | } | ||
68 | if (!_pef->mzHeader().isValid()) | ||
69 | { | ||
70 | delete _pef; | ||
71 | return false; | ||
72 | } | ||
73 | if (_pef->readPeHeader()) | ||
74 | { | ||
75 | delete _pef; | ||
76 | return false; | ||
77 | } | ||
78 | if (!is_valid()) | ||
79 | { | ||
80 | delete _pef; | ||
81 | return false; | ||
82 | } | ||
83 | return true; | ||
84 | } | ||
85 | |||
86 | bool shared::pe_file::find_section(const std::string& name, section_info& info) const | ||
87 | { | ||
88 | if (_pef->getBits() == 32) | ||
89 | return find_section(static_cast<PeLib::PeFile32*>(_pef), | ||
90 | name, info); | ||
91 | else if (_pef->getBits() == 64) | ||
92 | return find_section(static_cast<PeLib::PeFile64*>(_pef), | ||
93 | name, info); | ||
94 | return false; | ||
95 | } | ||
96 | |||
97 | bool shared::pe_file::add_section(const std::string& name, | ||
98 | const bytes& buffer, section_info& info) | ||
99 | { | ||
100 | if (_pef->getBits() == 32) | ||
101 | { | ||
102 | return add_section(static_cast<PeLib::PeFile32*>(_pef), | ||
103 | name, buffer, info); | ||
104 | } | ||
105 | else if (_pef->getBits() == 64) | ||
106 | { | ||
107 | return add_section(static_cast<PeLib::PeFile64*>(_pef), | ||
108 | name, buffer, info); | ||
109 | } | ||
110 | return false; | ||
111 | } | ||
112 | |||
113 | dword shared::pe_file::get_image_base() const | ||
114 | { | ||
115 | if (_pef->getBits() == 32) | ||
116 | return static_cast<PeLib::PeFile32*>(_pef)->peHeader().getImageBase(); | ||
117 | else | ||
118 | return static_cast<PeLib::PeFile64*>(_pef)->peHeader().getImageBase(); | ||
119 | return 0; | ||
120 | } | ||
121 | dword shared::pe_file::pa_to_va(dword pa) const | ||
122 | { | ||
123 | if (_pef->getBits() == 32) | ||
124 | return static_cast<PeLib::PeFile32*>(_pef)->peHeader().offsetToVa(pa); | ||
125 | else | ||
126 | return static_cast<PeLib::PeFile64*>(_pef)->peHeader().offsetToVa(pa); | ||
127 | return 0; | ||
128 | } | ||