diff options
Diffstat (limited to 'utils/zenutils/source/shared/pe.h')
-rw-r--r--[-rwxr-xr-x] | utils/zenutils/source/shared/pe.h | 284 |
1 files changed, 142 insertions, 142 deletions
diff --git a/utils/zenutils/source/shared/pe.h b/utils/zenutils/source/shared/pe.h index 92a272d3c7..f2f3aa48e8 100755..100644 --- a/utils/zenutils/source/shared/pe.h +++ b/utils/zenutils/source/shared/pe.h | |||
@@ -1,142 +1,142 @@ | |||
1 | /* zenutils - Utilities for working with creative firmwares. | 1 | /* zenutils - Utilities for working with creative firmwares. |
2 | * Copyright 2007 (c) Rasmus Ry <rasmus.ry{at}gmail.com> | 2 | * Copyright 2007 (c) Rasmus Ry <rasmus.ry{at}gmail.com> |
3 | * | 3 | * |
4 | * This program is free software; you can redistribute it and/or modify | 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 | 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 | 6 | * the Free Software Foundation; either version 2 of the License, or |
7 | * (at your option) any later version. | 7 | * (at your option) any later version. |
8 | * | 8 | * |
9 | * This program is distributed in the hope that it will be useful, | 9 | * This program is distributed in the hope that it will be useful, |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 | * GNU General Public License for more details. | 12 | * GNU General Public License for more details. |
13 | * | 13 | * |
14 | * You should have received a copy of the GNU General Public License | 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 | 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 | 16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
17 | */ | 17 | */ |
18 | 18 | ||
19 | #ifndef SHARED_PE_H_INCLUDED | 19 | #ifndef SHARED_PE_H_INCLUDED |
20 | #define SHARED_PE_H_INCLUDED | 20 | #define SHARED_PE_H_INCLUDED |
21 | 21 | ||
22 | #include <string> | 22 | #include <string> |
23 | #include <pelib/PeLib.h> | 23 | #include <pelib/PeLib.h> |
24 | #include <utils.h> | 24 | #include <utils.h> |
25 | 25 | ||
26 | namespace shared { | 26 | namespace shared { |
27 | struct section_info | 27 | struct section_info |
28 | { | 28 | { |
29 | word index; | 29 | word index; |
30 | dword virtual_address; | 30 | dword virtual_address; |
31 | dword virtual_size; | 31 | dword virtual_size; |
32 | dword raw_address; | 32 | dword raw_address; |
33 | dword raw_size; | 33 | dword raw_size; |
34 | dword characteristics; | 34 | dword characteristics; |
35 | }; //struct section_info | 35 | }; //struct section_info |
36 | 36 | ||
37 | class pe_file | 37 | class pe_file |
38 | { | 38 | { |
39 | public: | 39 | public: |
40 | pe_file(PeLib::PeFile* pef = NULL); | 40 | pe_file(PeLib::PeFile* pef = NULL); |
41 | ~pe_file(); | 41 | ~pe_file(); |
42 | 42 | ||
43 | bool is_valid() const; | 43 | bool is_valid() const; |
44 | bool read(const std::string& filename); | 44 | bool read(const std::string& filename); |
45 | bool find_section(const std::string& name, section_info& info) const; | 45 | bool find_section(const std::string& name, section_info& info) const; |
46 | bool add_section(const std::string& name, const bytes& buffer, section_info& info); | 46 | bool add_section(const std::string& name, const bytes& buffer, section_info& info); |
47 | dword get_image_base() const; | 47 | dword get_image_base() const; |
48 | dword pa_to_va(PeLib::dword pa) const; | 48 | dword pa_to_va(PeLib::dword pa) const; |
49 | 49 | ||
50 | protected: | 50 | protected: |
51 | template <int _Bits> | 51 | template <int _Bits> |
52 | static bool find_section(const PeLib::PeFileT<_Bits>* pef, | 52 | static bool find_section(const PeLib::PeFileT<_Bits>* pef, |
53 | const std::string& name, section_info& info); | 53 | const std::string& name, section_info& info); |
54 | template <int _Bits> | 54 | template <int _Bits> |
55 | static bool add_section(PeLib::PeFileT<_Bits>* pef, | 55 | static bool add_section(PeLib::PeFileT<_Bits>* pef, |
56 | const std::string& name, const bytes& buffer, | 56 | const std::string& name, const bytes& buffer, |
57 | section_info& info); | 57 | section_info& info); |
58 | 58 | ||
59 | private: | 59 | private: |
60 | PeLib::PeFile* _pef; | 60 | PeLib::PeFile* _pef; |
61 | }; //class pe_file | 61 | }; //class pe_file |
62 | 62 | ||
63 | 63 | ||
64 | template <int _Bits> | 64 | template <int _Bits> |
65 | bool pe_file::find_section(const PeLib::PeFileT<_Bits>* pef, | 65 | bool pe_file::find_section(const PeLib::PeFileT<_Bits>* pef, |
66 | const std::string& name, section_info& info) | 66 | const std::string& name, section_info& info) |
67 | { | 67 | { |
68 | for (PeLib::word i = 0; i < pef->peHeader().getNumberOfSections(); i++) | 68 | for (PeLib::word i = 0; i < pef->peHeader().getNumberOfSections(); i++) |
69 | { | 69 | { |
70 | if (pef->peHeader().getSectionName(i) == name) | 70 | if (pef->peHeader().getSectionName(i) == name) |
71 | { | 71 | { |
72 | info.index = i; | 72 | info.index = i; |
73 | info.virtual_address = pef->peHeader().getVirtualAddress(i); | 73 | info.virtual_address = pef->peHeader().getVirtualAddress(i); |
74 | info.virtual_size = pef->peHeader().getVirtualSize(i); | 74 | info.virtual_size = pef->peHeader().getVirtualSize(i); |
75 | info.raw_address = pef->peHeader().getPointerToRawData(i); | 75 | info.raw_address = pef->peHeader().getPointerToRawData(i); |
76 | info.raw_size = pef->peHeader().getSizeOfRawData(i); | 76 | info.raw_size = pef->peHeader().getSizeOfRawData(i); |
77 | info.characteristics = pef->peHeader().getCharacteristics(i); | 77 | info.characteristics = pef->peHeader().getCharacteristics(i); |
78 | return true; | 78 | return true; |
79 | } | 79 | } |
80 | } | 80 | } |
81 | return false; | 81 | return false; |
82 | } | 82 | } |
83 | 83 | ||
84 | template <int _Bits> | 84 | template <int _Bits> |
85 | bool pe_file::add_section(PeLib::PeFileT<_Bits>* pef, | 85 | bool pe_file::add_section(PeLib::PeFileT<_Bits>* pef, |
86 | const std::string& name, const bytes& buffer, | 86 | const std::string& name, const bytes& buffer, |
87 | section_info& info) | 87 | section_info& info) |
88 | { | 88 | { |
89 | using namespace PeLib; | 89 | using namespace PeLib; |
90 | 90 | ||
91 | // Check if the last section has the same name as the one being added. | 91 | // Check if the last section has the same name as the one being added. |
92 | PeLib::word secnum = pef->peHeader().getNumberOfSections(); | 92 | PeLib::word secnum = pef->peHeader().getNumberOfSections(); |
93 | if (pef->peHeader().getSectionName(secnum-1) == name) | 93 | if (pef->peHeader().getSectionName(secnum-1) == name) |
94 | { | 94 | { |
95 | // If it is, we change the attributes of the existing section. | 95 | // If it is, we change the attributes of the existing section. |
96 | secnum = secnum - 1; | 96 | secnum = secnum - 1; |
97 | pef->peHeader().setSizeOfRawData(secnum, | 97 | pef->peHeader().setSizeOfRawData(secnum, |
98 | alignOffset(buffer.size(), | 98 | alignOffset(buffer.size(), |
99 | pef->peHeader().getFileAlignment())); | 99 | pef->peHeader().getFileAlignment())); |
100 | pef->peHeader().setVirtualSize(secnum, | 100 | pef->peHeader().setVirtualSize(secnum, |
101 | alignOffset(buffer.size(), | 101 | alignOffset(buffer.size(), |
102 | pef->peHeader().getSectionAlignment())); | 102 | pef->peHeader().getSectionAlignment())); |
103 | PeLib::dword chars = pef->peHeader().getCharacteristics(secnum-1); | 103 | PeLib::dword chars = pef->peHeader().getCharacteristics(secnum-1); |
104 | pef->peHeader().setCharacteristics(secnum, | 104 | pef->peHeader().setCharacteristics(secnum, |
105 | chars | PELIB_IMAGE_SCN_MEM_WRITE | PELIB_IMAGE_SCN_MEM_READ); | 105 | chars | PELIB_IMAGE_SCN_MEM_WRITE | PELIB_IMAGE_SCN_MEM_READ); |
106 | } | 106 | } |
107 | else | 107 | else |
108 | { | 108 | { |
109 | // Otherwise we add a new section. | 109 | // Otherwise we add a new section. |
110 | if (pef->peHeader().addSection(name, buffer.size()) != NO_ERROR) | 110 | if (pef->peHeader().addSection(name, buffer.size()) != NO_ERROR) |
111 | { | 111 | { |
112 | return false; | 112 | return false; |
113 | } | 113 | } |
114 | pef->peHeader().makeValid(pef->mzHeader().getAddressOfPeHeader()); | 114 | pef->peHeader().makeValid(pef->mzHeader().getAddressOfPeHeader()); |
115 | pef->peHeader().write(pef->getFileName(), pef->mzHeader().getAddressOfPeHeader()); | 115 | pef->peHeader().write(pef->getFileName(), pef->mzHeader().getAddressOfPeHeader()); |
116 | } | 116 | } |
117 | 117 | ||
118 | // Save the section headers to the file. | 118 | // Save the section headers to the file. |
119 | if (pef->peHeader().writeSections(pef->getFileName()) != NO_ERROR) | 119 | if (pef->peHeader().writeSections(pef->getFileName()) != NO_ERROR) |
120 | { | 120 | { |
121 | return false; | 121 | return false; |
122 | } | 122 | } |
123 | 123 | ||
124 | // Save the section data to the file. | 124 | // Save the section data to the file. |
125 | if (pef->peHeader().writeSectionData(pef->getFileName(), secnum, buffer) != NO_ERROR) | 125 | if (pef->peHeader().writeSectionData(pef->getFileName(), secnum, buffer) != NO_ERROR) |
126 | { | 126 | { |
127 | return false; | 127 | return false; |
128 | } | 128 | } |
129 | 129 | ||
130 | // Fill out the section information. | 130 | // Fill out the section information. |
131 | info.index = secnum; | 131 | info.index = secnum; |
132 | info.virtual_address = pef->peHeader().getVirtualAddress(secnum); | 132 | info.virtual_address = pef->peHeader().getVirtualAddress(secnum); |
133 | info.virtual_size = pef->peHeader().getVirtualSize(secnum); | 133 | info.virtual_size = pef->peHeader().getVirtualSize(secnum); |
134 | info.raw_address = pef->peHeader().getPointerToRawData(secnum); | 134 | info.raw_address = pef->peHeader().getPointerToRawData(secnum); |
135 | info.raw_size = pef->peHeader().getSizeOfRawData(secnum); | 135 | info.raw_size = pef->peHeader().getSizeOfRawData(secnum); |
136 | info.characteristics = pef->peHeader().getCharacteristics(secnum); | 136 | info.characteristics = pef->peHeader().getCharacteristics(secnum); |
137 | 137 | ||
138 | return true; | 138 | return true; |
139 | } | 139 | } |
140 | }; //namespace shared | 140 | }; //namespace shared |
141 | 141 | ||
142 | #endif //SHARED_PE_H_INCLUDED | 142 | #endif //SHARED_PE_H_INCLUDED |