diff options
Diffstat (limited to 'utils/zenutils/libraries/pelib-0.9/pelib/BoundImportDirectory.cpp')
-rw-r--r--[-rwxr-xr-x] | utils/zenutils/libraries/pelib-0.9/pelib/BoundImportDirectory.cpp | 1022 |
1 files changed, 511 insertions, 511 deletions
diff --git a/utils/zenutils/libraries/pelib-0.9/pelib/BoundImportDirectory.cpp b/utils/zenutils/libraries/pelib-0.9/pelib/BoundImportDirectory.cpp index 5b84931838..7c5ff74f0e 100755..100644 --- a/utils/zenutils/libraries/pelib-0.9/pelib/BoundImportDirectory.cpp +++ b/utils/zenutils/libraries/pelib-0.9/pelib/BoundImportDirectory.cpp | |||
@@ -1,511 +1,511 @@ | |||
1 | /* | 1 | /* |
2 | * BoundImportDirectory.cpp - Part of the PeLib library. | 2 | * BoundImportDirectory.cpp - Part of the PeLib library. |
3 | * | 3 | * |
4 | * Copyright (c) 2004 - 2005 Sebastian Porst (webmaster@the-interweb.com) | 4 | * Copyright (c) 2004 - 2005 Sebastian Porst (webmaster@the-interweb.com) |
5 | * All rights reserved. | 5 | * All rights reserved. |
6 | * | 6 | * |
7 | * This software is licensed under the zlib/libpng License. | 7 | * This software is licensed under the zlib/libpng License. |
8 | * For more details see http://www.opensource.org/licenses/zlib-license.php | 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 | 9 | * or the license information file (license.htm) in the root directory |
10 | * of PeLib. | 10 | * of PeLib. |
11 | */ | 11 | */ |
12 | 12 | ||
13 | #include "PeLibInc.h" | 13 | #include "PeLibInc.h" |
14 | #include "BoundImportDirectory.h" | 14 | #include "BoundImportDirectory.h" |
15 | #include <numeric> | 15 | #include <numeric> |
16 | #include <set> | 16 | #include <set> |
17 | #include <map> | 17 | #include <map> |
18 | 18 | ||
19 | namespace PeLib | 19 | namespace PeLib |
20 | { | 20 | { |
21 | /** | 21 | /** |
22 | * Adds another bound import to the BoundImport directory. | 22 | * Adds another bound import to the BoundImport directory. |
23 | * @param strModuleName Name of the PE file which will be imported. | 23 | * @param strModuleName Name of the PE file which will be imported. |
24 | * @param dwTds Value of the TimeDateStamp of the bound import field. | 24 | * @param dwTds Value of the TimeDateStamp of the bound import field. |
25 | * @param wOmn Value of the OffsetModuleName of the bound import field. | 25 | * @param wOmn Value of the OffsetModuleName of the bound import field. |
26 | * @param wWfr Value of the NumberOfModuleForwarderRefs of the bound import field. | 26 | * @param wWfr Value of the NumberOfModuleForwarderRefs of the bound import field. |
27 | **/ | 27 | **/ |
28 | int BoundImportDirectory::addBoundImport(const std::string& strModuleName, dword dwTds, word wOmn, word wWfr) | 28 | int BoundImportDirectory::addBoundImport(const std::string& strModuleName, dword dwTds, word wOmn, word wWfr) |
29 | { | 29 | { |
30 | for (unsigned int i=0;i<m_vIbd.size();i++) | 30 | for (unsigned int i=0;i<m_vIbd.size();i++) |
31 | { | 31 | { |
32 | if (isEqualNc(strModuleName, m_vIbd[i].strModuleName)) | 32 | if (isEqualNc(strModuleName, m_vIbd[i].strModuleName)) |
33 | { | 33 | { |
34 | return ERROR_DUPLICATE_ENTRY; | 34 | return ERROR_DUPLICATE_ENTRY; |
35 | } | 35 | } |
36 | } | 36 | } |
37 | 37 | ||
38 | PELIB_IMAGE_BOUND_IMPORT_DESCRIPTOR ibidCurrent; | 38 | PELIB_IMAGE_BOUND_IMPORT_DESCRIPTOR ibidCurrent; |
39 | ibidCurrent.TimeDateStamp = dwTds; | 39 | ibidCurrent.TimeDateStamp = dwTds; |
40 | ibidCurrent.OffsetModuleName = wOmn; | 40 | ibidCurrent.OffsetModuleName = wOmn; |
41 | ibidCurrent.NumberOfModuleForwarderRefs = wWfr; | 41 | ibidCurrent.NumberOfModuleForwarderRefs = wWfr; |
42 | PELIB_IMAGE_BOUND_DIRECTORY ibdCurrent; | 42 | PELIB_IMAGE_BOUND_DIRECTORY ibdCurrent; |
43 | ibdCurrent.ibdDescriptor = ibidCurrent; | 43 | ibdCurrent.ibdDescriptor = ibidCurrent; |
44 | ibdCurrent.strModuleName = strModuleName; | 44 | ibdCurrent.strModuleName = strModuleName; |
45 | m_vIbd.push_back(ibdCurrent); | 45 | m_vIbd.push_back(ibdCurrent); |
46 | 46 | ||
47 | return NO_ERROR; | 47 | return NO_ERROR; |
48 | } | 48 | } |
49 | 49 | ||
50 | /** | 50 | /** |
51 | * Searches for the first instance of a module with the given modulename. | 51 | * Searches for the first instance of a module with the given modulename. |
52 | * @param strModuleName The name of a module. | 52 | * @param strModuleName The name of a module. |
53 | * @return The id of the module. | 53 | * @return The id of the module. |
54 | **/ | 54 | **/ |
55 | int BoundImportDirectory::getModuleIndex(const std::string& strModuleName) const | 55 | int BoundImportDirectory::getModuleIndex(const std::string& strModuleName) const |
56 | { | 56 | { |
57 | std::vector<PELIB_IMAGE_BOUND_DIRECTORY>::const_iterator Iter = std::find_if(m_vIbd.begin(), m_vIbd.end(), std::bind2nd(std::mem_fun_ref(&PELIB_IMAGE_BOUND_DIRECTORY::equal), strModuleName)); | 57 | std::vector<PELIB_IMAGE_BOUND_DIRECTORY>::const_iterator Iter = std::find_if(m_vIbd.begin(), m_vIbd.end(), std::bind2nd(std::mem_fun_ref(&PELIB_IMAGE_BOUND_DIRECTORY::equal), strModuleName)); |
58 | 58 | ||
59 | if (Iter == m_vIbd.end()) | 59 | if (Iter == m_vIbd.end()) |
60 | { | 60 | { |
61 | return ERROR_ENTRY_NOT_FOUND; | 61 | return ERROR_ENTRY_NOT_FOUND; |
62 | } | 62 | } |
63 | 63 | ||
64 | return static_cast<int>(std::distance(m_vIbd.begin(), Iter)); | 64 | return static_cast<int>(std::distance(m_vIbd.begin(), Iter)); |
65 | } | 65 | } |
66 | 66 | ||
67 | /** | 67 | /** |
68 | * @return Number of files in the current BoundImport directory. | 68 | * @return Number of files in the current BoundImport directory. |
69 | **/ | 69 | **/ |
70 | unsigned int BoundImportDirectory::calcNumberOfModules() const | 70 | unsigned int BoundImportDirectory::calcNumberOfModules() const |
71 | { | 71 | { |
72 | return static_cast<unsigned int>(m_vIbd.size()); | 72 | return static_cast<unsigned int>(m_vIbd.size()); |
73 | } | 73 | } |
74 | 74 | ||
75 | int BoundImportDirectory::read(InputBuffer& inpBuffer, unsigned char* data, unsigned int dwSize) | 75 | int BoundImportDirectory::read(InputBuffer& inpBuffer, unsigned char* data, unsigned int dwSize) |
76 | { | 76 | { |
77 | std::vector<PELIB_IMAGE_BOUND_DIRECTORY> currentDirectory; | 77 | std::vector<PELIB_IMAGE_BOUND_DIRECTORY> currentDirectory; |
78 | 78 | ||
79 | do | 79 | do |
80 | { | 80 | { |
81 | PELIB_IMAGE_BOUND_DIRECTORY ibdCurrent; | 81 | PELIB_IMAGE_BOUND_DIRECTORY ibdCurrent; |
82 | 82 | ||
83 | inpBuffer >> ibdCurrent.ibdDescriptor.TimeDateStamp; | 83 | inpBuffer >> ibdCurrent.ibdDescriptor.TimeDateStamp; |
84 | inpBuffer >> ibdCurrent.ibdDescriptor.OffsetModuleName; | 84 | inpBuffer >> ibdCurrent.ibdDescriptor.OffsetModuleName; |
85 | inpBuffer >> ibdCurrent.ibdDescriptor.NumberOfModuleForwarderRefs; | 85 | inpBuffer >> ibdCurrent.ibdDescriptor.NumberOfModuleForwarderRefs; |
86 | 86 | ||
87 | if (ibdCurrent.ibdDescriptor.TimeDateStamp == 0 && ibdCurrent.ibdDescriptor.OffsetModuleName == 0 && ibdCurrent.ibdDescriptor.NumberOfModuleForwarderRefs == 0) break; | 87 | if (ibdCurrent.ibdDescriptor.TimeDateStamp == 0 && ibdCurrent.ibdDescriptor.OffsetModuleName == 0 && ibdCurrent.ibdDescriptor.NumberOfModuleForwarderRefs == 0) break; |
88 | 88 | ||
89 | for (int i=0;i<ibdCurrent.ibdDescriptor.NumberOfModuleForwarderRefs;i++) | 89 | for (int i=0;i<ibdCurrent.ibdDescriptor.NumberOfModuleForwarderRefs;i++) |
90 | { | 90 | { |
91 | PELIB_IMAGE_BOUND_DIRECTORY currentForwarder; | 91 | PELIB_IMAGE_BOUND_DIRECTORY currentForwarder; |
92 | 92 | ||
93 | inpBuffer >> currentForwarder.ibdDescriptor.TimeDateStamp; | 93 | inpBuffer >> currentForwarder.ibdDescriptor.TimeDateStamp; |
94 | inpBuffer >> currentForwarder.ibdDescriptor.OffsetModuleName; | 94 | inpBuffer >> currentForwarder.ibdDescriptor.OffsetModuleName; |
95 | inpBuffer >> currentForwarder.ibdDescriptor.NumberOfModuleForwarderRefs; | 95 | inpBuffer >> currentForwarder.ibdDescriptor.NumberOfModuleForwarderRefs; |
96 | 96 | ||
97 | ibdCurrent.moduleForwarders.push_back(currentForwarder); | 97 | ibdCurrent.moduleForwarders.push_back(currentForwarder); |
98 | } | 98 | } |
99 | 99 | ||
100 | currentDirectory.push_back(ibdCurrent); | 100 | currentDirectory.push_back(ibdCurrent); |
101 | ibdCurrent.moduleForwarders.clear(); | 101 | ibdCurrent.moduleForwarders.clear(); |
102 | } while (true); | 102 | } while (true); |
103 | 103 | ||
104 | for (unsigned int i=0;i<currentDirectory.size();i++) | 104 | for (unsigned int i=0;i<currentDirectory.size();i++) |
105 | { | 105 | { |
106 | dword wOmn = currentDirectory[i].ibdDescriptor.OffsetModuleName; | 106 | dword wOmn = currentDirectory[i].ibdDescriptor.OffsetModuleName; |
107 | if (wOmn > dwSize) | 107 | if (wOmn > dwSize) |
108 | { | 108 | { |
109 | return ERROR_INVALID_FILE; | 109 | return ERROR_INVALID_FILE; |
110 | } | 110 | } |
111 | 111 | ||
112 | currentDirectory[i].strModuleName = ""; | 112 | currentDirectory[i].strModuleName = ""; |
113 | for (int k=0;data[wOmn + k] != 0 && k + wOmn < dwSize;k++) | 113 | for (int k=0;data[wOmn + k] != 0 && k + wOmn < dwSize;k++) |
114 | { | 114 | { |
115 | currentDirectory[i].strModuleName += data[wOmn + k]; | 115 | currentDirectory[i].strModuleName += data[wOmn + k]; |
116 | } | 116 | } |
117 | 117 | ||
118 | for (unsigned int j=0;j<currentDirectory[i].moduleForwarders.size();j++) | 118 | for (unsigned int j=0;j<currentDirectory[i].moduleForwarders.size();j++) |
119 | { | 119 | { |
120 | dword wOmn = currentDirectory[i].moduleForwarders[j].ibdDescriptor.OffsetModuleName; | 120 | dword wOmn = currentDirectory[i].moduleForwarders[j].ibdDescriptor.OffsetModuleName; |
121 | 121 | ||
122 | if (wOmn > dwSize) | 122 | if (wOmn > dwSize) |
123 | { | 123 | { |
124 | return ERROR_INVALID_FILE; | 124 | return ERROR_INVALID_FILE; |
125 | } | 125 | } |
126 | 126 | ||
127 | // m_vIbd[i].moduleForwarders[j].strModuleName.assign((char*)(&vBimpDir[wOmn])); | 127 | // m_vIbd[i].moduleForwarders[j].strModuleName.assign((char*)(&vBimpDir[wOmn])); |
128 | currentDirectory[i].moduleForwarders[j].strModuleName = ""; | 128 | currentDirectory[i].moduleForwarders[j].strModuleName = ""; |
129 | for (int k=0;data[wOmn + k] != 0 && k + wOmn < dwSize;k++) | 129 | for (int k=0;data[wOmn + k] != 0 && k + wOmn < dwSize;k++) |
130 | { | 130 | { |
131 | currentDirectory[i].moduleForwarders[j].strModuleName += data[wOmn + k]; | 131 | currentDirectory[i].moduleForwarders[j].strModuleName += data[wOmn + k]; |
132 | } | 132 | } |
133 | } | 133 | } |
134 | } | 134 | } |
135 | 135 | ||
136 | std::swap(m_vIbd, currentDirectory); | 136 | std::swap(m_vIbd, currentDirectory); |
137 | 137 | ||
138 | return NO_ERROR; | 138 | return NO_ERROR; |
139 | } | 139 | } |
140 | 140 | ||
141 | /** | 141 | /** |
142 | * Reads the BoundImport directory from a PE file. | 142 | * Reads the BoundImport directory from a PE file. |
143 | * @param strModuleName The name of the PE file from which the BoundImport directory is read. | 143 | * @param strModuleName The name of the PE file from which the BoundImport directory is read. |
144 | * @param dwOffset The file offset where the BoundImport directory can be found (see #PeFile::PeHeader::getIDBoundImportRVA). | 144 | * @param dwOffset The file offset where the BoundImport directory can be found (see #PeFile::PeHeader::getIDBoundImportRVA). |
145 | * @param dwSize The size of the BoundImport directory (see #PeFile::PeHeader::getIDBoundImportSize). | 145 | * @param dwSize The size of the BoundImport directory (see #PeFile::PeHeader::getIDBoundImportSize). |
146 | **/ | 146 | **/ |
147 | int BoundImportDirectory::read(const std::string& strModuleName, dword dwOffset, unsigned int uiSize) | 147 | int BoundImportDirectory::read(const std::string& strModuleName, dword dwOffset, unsigned int uiSize) |
148 | { | 148 | { |
149 | std::ifstream ifFile(strModuleName.c_str(), std::ios::binary); | 149 | std::ifstream ifFile(strModuleName.c_str(), std::ios::binary); |
150 | 150 | ||
151 | if (!ifFile) | 151 | if (!ifFile) |
152 | { | 152 | { |
153 | return ERROR_OPENING_FILE; | 153 | return ERROR_OPENING_FILE; |
154 | } | 154 | } |
155 | 155 | ||
156 | if (fileSize(ifFile) < dwOffset + uiSize) | 156 | if (fileSize(ifFile) < dwOffset + uiSize) |
157 | { | 157 | { |
158 | return ERROR_INVALID_FILE; | 158 | return ERROR_INVALID_FILE; |
159 | } | 159 | } |
160 | 160 | ||
161 | ifFile.seekg(dwOffset, std::ios::beg); | 161 | ifFile.seekg(dwOffset, std::ios::beg); |
162 | 162 | ||
163 | std::vector<unsigned char> vBimpDir(uiSize); | 163 | std::vector<unsigned char> vBimpDir(uiSize); |
164 | ifFile.read(reinterpret_cast<char*>(&vBimpDir[0]), uiSize); | 164 | ifFile.read(reinterpret_cast<char*>(&vBimpDir[0]), uiSize); |
165 | 165 | ||
166 | InputBuffer inpBuffer(vBimpDir); | 166 | InputBuffer inpBuffer(vBimpDir); |
167 | 167 | ||
168 | return read(inpBuffer, &vBimpDir[0], uiSize); | 168 | return read(inpBuffer, &vBimpDir[0], uiSize); |
169 | } | 169 | } |
170 | 170 | ||
171 | int BoundImportDirectory::read(unsigned char* pcBuffer, unsigned int uiSize) | 171 | int BoundImportDirectory::read(unsigned char* pcBuffer, unsigned int uiSize) |
172 | { | 172 | { |
173 | std::vector<unsigned char> vBimpDir(pcBuffer, pcBuffer + uiSize); | 173 | std::vector<unsigned char> vBimpDir(pcBuffer, pcBuffer + uiSize); |
174 | InputBuffer inpBuffer(vBimpDir); | 174 | InputBuffer inpBuffer(vBimpDir); |
175 | 175 | ||
176 | return read(inpBuffer, &vBimpDir[0], uiSize); | 176 | return read(inpBuffer, &vBimpDir[0], uiSize); |
177 | } | 177 | } |
178 | 178 | ||
179 | unsigned int BoundImportDirectory::totalModules() const | 179 | unsigned int BoundImportDirectory::totalModules() const |
180 | { | 180 | { |
181 | unsigned int modules = static_cast<unsigned int>(m_vIbd.size()); | 181 | unsigned int modules = static_cast<unsigned int>(m_vIbd.size()); |
182 | 182 | ||
183 | for (unsigned int i=0;i<m_vIbd.size();i++) | 183 | for (unsigned int i=0;i<m_vIbd.size();i++) |
184 | { | 184 | { |
185 | modules += static_cast<unsigned int>(m_vIbd[i].moduleForwarders.size()); | 185 | modules += static_cast<unsigned int>(m_vIbd[i].moduleForwarders.size()); |
186 | } | 186 | } |
187 | 187 | ||
188 | return modules; | 188 | return modules; |
189 | } | 189 | } |
190 | 190 | ||
191 | /** | 191 | /** |
192 | * Rebuilds the BoundImport directory. The rebuilded BoundImport directory can then be | 192 | * Rebuilds the BoundImport directory. The rebuilded BoundImport directory can then be |
193 | * written back to a PE file. | 193 | * written back to a PE file. |
194 | * @param vBuffer Buffer where the rebuilt BoundImport directory will be stored. | 194 | * @param vBuffer Buffer where the rebuilt BoundImport directory will be stored. |
195 | * @param fMakeValid If this flag is true a valid directory will be produced. | 195 | * @param fMakeValid If this flag is true a valid directory will be produced. |
196 | **/ | 196 | **/ |
197 | void BoundImportDirectory::rebuild(std::vector<byte>& vBuffer, bool fMakeValid) const | 197 | void BoundImportDirectory::rebuild(std::vector<byte>& vBuffer, bool fMakeValid) const |
198 | { | 198 | { |
199 | std::map<std::string, word> filename_offsets; | 199 | std::map<std::string, word> filename_offsets; |
200 | 200 | ||
201 | OutputBuffer obBuffer(vBuffer); | 201 | OutputBuffer obBuffer(vBuffer); |
202 | 202 | ||
203 | word ulNameOffset = static_cast<word>((totalModules() + 1) * PELIB_IMAGE_BOUND_IMPORT_DESCRIPTOR::size()); | 203 | word ulNameOffset = static_cast<word>((totalModules() + 1) * PELIB_IMAGE_BOUND_IMPORT_DESCRIPTOR::size()); |
204 | 204 | ||
205 | for (unsigned int i=0;i<m_vIbd.size();i++) | 205 | for (unsigned int i=0;i<m_vIbd.size();i++) |
206 | { | 206 | { |
207 | obBuffer << m_vIbd[i].ibdDescriptor.TimeDateStamp; | 207 | obBuffer << m_vIbd[i].ibdDescriptor.TimeDateStamp; |
208 | 208 | ||
209 | // Recalculate the offsets if a valid directory is wanted. | 209 | // Recalculate the offsets if a valid directory is wanted. |
210 | if (fMakeValid) | 210 | if (fMakeValid) |
211 | { | 211 | { |
212 | if (filename_offsets.find(m_vIbd[i].strModuleName) == filename_offsets.end()) | 212 | if (filename_offsets.find(m_vIbd[i].strModuleName) == filename_offsets.end()) |
213 | { | 213 | { |
214 | filename_offsets[m_vIbd[i].strModuleName] = ulNameOffset; | 214 | filename_offsets[m_vIbd[i].strModuleName] = ulNameOffset; |
215 | obBuffer << ulNameOffset; | 215 | obBuffer << ulNameOffset; |
216 | ulNameOffset += static_cast<word>(m_vIbd[i].strModuleName.size() + 1); | 216 | ulNameOffset += static_cast<word>(m_vIbd[i].strModuleName.size() + 1); |
217 | } | 217 | } |
218 | else | 218 | else |
219 | { | 219 | { |
220 | obBuffer << filename_offsets[m_vIbd[i].strModuleName]; | 220 | obBuffer << filename_offsets[m_vIbd[i].strModuleName]; |
221 | } | 221 | } |
222 | } | 222 | } |
223 | else // Otherwise just copy the old values into the buffer. | 223 | else // Otherwise just copy the old values into the buffer. |
224 | { | 224 | { |
225 | obBuffer << m_vIbd[i].ibdDescriptor.OffsetModuleName; | 225 | obBuffer << m_vIbd[i].ibdDescriptor.OffsetModuleName; |
226 | } | 226 | } |
227 | 227 | ||
228 | obBuffer << m_vIbd[i].ibdDescriptor.NumberOfModuleForwarderRefs; | 228 | obBuffer << m_vIbd[i].ibdDescriptor.NumberOfModuleForwarderRefs; |
229 | 229 | ||
230 | for (int j=0;j<calcNumberOfModuleForwarderRefs(i);j++) | 230 | for (int j=0;j<calcNumberOfModuleForwarderRefs(i);j++) |
231 | { | 231 | { |
232 | obBuffer << m_vIbd[i].moduleForwarders[j].ibdDescriptor.TimeDateStamp; | 232 | obBuffer << m_vIbd[i].moduleForwarders[j].ibdDescriptor.TimeDateStamp; |
233 | 233 | ||
234 | if (fMakeValid) | 234 | if (fMakeValid) |
235 | { | 235 | { |
236 | if (filename_offsets.find(m_vIbd[i].strModuleName) == filename_offsets.end()) | 236 | if (filename_offsets.find(m_vIbd[i].strModuleName) == filename_offsets.end()) |
237 | { | 237 | { |
238 | filename_offsets[m_vIbd[i].moduleForwarders[j].strModuleName] = ulNameOffset; | 238 | filename_offsets[m_vIbd[i].moduleForwarders[j].strModuleName] = ulNameOffset; |
239 | obBuffer << ulNameOffset; | 239 | obBuffer << ulNameOffset; |
240 | ulNameOffset += static_cast<word>(m_vIbd[i].moduleForwarders[j].strModuleName.size() + 1); | 240 | ulNameOffset += static_cast<word>(m_vIbd[i].moduleForwarders[j].strModuleName.size() + 1); |
241 | } | 241 | } |
242 | else | 242 | else |
243 | { | 243 | { |
244 | obBuffer << filename_offsets[m_vIbd[i].moduleForwarders[j].strModuleName]; | 244 | obBuffer << filename_offsets[m_vIbd[i].moduleForwarders[j].strModuleName]; |
245 | } | 245 | } |
246 | } | 246 | } |
247 | else // Otherwise just copy the old values into the buffer. | 247 | else // Otherwise just copy the old values into the buffer. |
248 | { | 248 | { |
249 | obBuffer << m_vIbd[i].moduleForwarders[j].ibdDescriptor.OffsetModuleName; | 249 | obBuffer << m_vIbd[i].moduleForwarders[j].ibdDescriptor.OffsetModuleName; |
250 | } | 250 | } |
251 | 251 | ||
252 | obBuffer << m_vIbd[i].moduleForwarders[j].ibdDescriptor.NumberOfModuleForwarderRefs; | 252 | obBuffer << m_vIbd[i].moduleForwarders[j].ibdDescriptor.NumberOfModuleForwarderRefs; |
253 | } | 253 | } |
254 | } | 254 | } |
255 | 255 | ||
256 | obBuffer << static_cast<dword>(0); | 256 | obBuffer << static_cast<dword>(0); |
257 | obBuffer << static_cast<word>(0); | 257 | obBuffer << static_cast<word>(0); |
258 | obBuffer << static_cast<word>(0); | 258 | obBuffer << static_cast<word>(0); |
259 | 259 | ||
260 | for (unsigned int i=0;i<m_vIbd.size();i++) | 260 | for (unsigned int i=0;i<m_vIbd.size();i++) |
261 | { | 261 | { |
262 | if (filename_offsets.find(m_vIbd[i].strModuleName) != filename_offsets.end()) | 262 | if (filename_offsets.find(m_vIbd[i].strModuleName) != filename_offsets.end()) |
263 | { | 263 | { |
264 | obBuffer.add(getModuleName(i).c_str(), static_cast<unsigned long>(getModuleName(i).size() + 1)); | 264 | obBuffer.add(getModuleName(i).c_str(), static_cast<unsigned long>(getModuleName(i).size() + 1)); |
265 | filename_offsets.erase(m_vIbd[i].strModuleName); | 265 | filename_offsets.erase(m_vIbd[i].strModuleName); |
266 | } | 266 | } |
267 | 267 | ||
268 | for (int j=0;j<calcNumberOfModuleForwarderRefs(i);j++) | 268 | for (int j=0;j<calcNumberOfModuleForwarderRefs(i);j++) |
269 | { | 269 | { |
270 | if (filename_offsets.find(getModuleName(i, j)) != filename_offsets.end()) | 270 | if (filename_offsets.find(getModuleName(i, j)) != filename_offsets.end()) |
271 | { | 271 | { |
272 | obBuffer.add(getModuleName(i, j).c_str(), static_cast<unsigned long>(getModuleName(i, j).size() + 1)); | 272 | obBuffer.add(getModuleName(i, j).c_str(), static_cast<unsigned long>(getModuleName(i, j).size() + 1)); |
273 | filename_offsets.erase(getModuleName(i, j)); | 273 | filename_offsets.erase(getModuleName(i, j)); |
274 | } | 274 | } |
275 | } | 275 | } |
276 | } | 276 | } |
277 | } | 277 | } |
278 | 278 | ||
279 | /** | 279 | /** |
280 | * Removes all bound import files. | 280 | * Removes all bound import files. |
281 | **/ | 281 | **/ |
282 | void BoundImportDirectory::clear() | 282 | void BoundImportDirectory::clear() |
283 | { | 283 | { |
284 | m_vIbd.clear(); | 284 | m_vIbd.clear(); |
285 | } | 285 | } |
286 | 286 | ||
287 | /** | 287 | /** |
288 | * Removes a field specified by the parameter filename from the BoundImport directory. | 288 | * Removes a field specified by the parameter filename from the BoundImport directory. |
289 | * @param strModuleName Name of the file whose field will be removed from the BoundImport directory. | 289 | * @param strModuleName Name of the file whose field will be removed from the BoundImport directory. |
290 | **/ | 290 | **/ |
291 | void BoundImportDirectory::removeBoundImport(const std::string& strModuleName) | 291 | void BoundImportDirectory::removeBoundImport(const std::string& strModuleName) |
292 | { | 292 | { |
293 | m_vIbd.erase(std::remove_if(m_vIbd.begin(), m_vIbd.end(), std::bind2nd(std::mem_fun_ref(&PELIB_IMAGE_BOUND_DIRECTORY::equal), strModuleName)), m_vIbd.end()); | 293 | m_vIbd.erase(std::remove_if(m_vIbd.begin(), m_vIbd.end(), std::bind2nd(std::mem_fun_ref(&PELIB_IMAGE_BOUND_DIRECTORY::equal), strModuleName)), m_vIbd.end()); |
294 | } | 294 | } |
295 | 295 | ||
296 | /** | 296 | /** |
297 | * Returns the size of the rebuilt BoundImportDirectory. | 297 | * Returns the size of the rebuilt BoundImportDirectory. |
298 | * @return Size of the rebuilt BoundImportDirectory. | 298 | * @return Size of the rebuilt BoundImportDirectory. |
299 | **/ | 299 | **/ |
300 | unsigned int BoundImportDirectory::size() const | 300 | unsigned int BoundImportDirectory::size() const |
301 | { | 301 | { |
302 | unsigned int size = PELIB_IMAGE_BOUND_IMPORT_DESCRIPTOR::size(); | 302 | unsigned int size = PELIB_IMAGE_BOUND_IMPORT_DESCRIPTOR::size(); |
303 | 303 | ||
304 | std::set<std::string> filenames; | 304 | std::set<std::string> filenames; |
305 | 305 | ||
306 | for (unsigned int i = 0; i < m_vIbd.size(); i++) | 306 | for (unsigned int i = 0; i < m_vIbd.size(); i++) |
307 | { | 307 | { |
308 | filenames.insert(m_vIbd[i].strModuleName); | 308 | filenames.insert(m_vIbd[i].strModuleName); |
309 | 309 | ||
310 | size += PELIB_IMAGE_BOUND_IMPORT_DESCRIPTOR::size(); | 310 | size += PELIB_IMAGE_BOUND_IMPORT_DESCRIPTOR::size(); |
311 | 311 | ||
312 | for (unsigned int j = 0; j < m_vIbd[i].moduleForwarders.size(); j++) | 312 | for (unsigned int j = 0; j < m_vIbd[i].moduleForwarders.size(); j++) |
313 | { | 313 | { |
314 | filenames.insert(m_vIbd[i].moduleForwarders[j].strModuleName); | 314 | filenames.insert(m_vIbd[i].moduleForwarders[j].strModuleName); |
315 | 315 | ||
316 | size += PELIB_IMAGE_BOUND_IMPORT_DESCRIPTOR::size(); | 316 | size += PELIB_IMAGE_BOUND_IMPORT_DESCRIPTOR::size(); |
317 | } | 317 | } |
318 | } | 318 | } |
319 | 319 | ||
320 | for (std::set<std::string>::iterator iter = filenames.begin(); iter != filenames.end(); ++iter) | 320 | for (std::set<std::string>::iterator iter = filenames.begin(); iter != filenames.end(); ++iter) |
321 | { | 321 | { |
322 | size += static_cast<unsigned int>(iter->size()) + 1; | 322 | size += static_cast<unsigned int>(iter->size()) + 1; |
323 | } | 323 | } |
324 | 324 | ||
325 | return size; | 325 | return size; |
326 | } | 326 | } |
327 | 327 | ||
328 | /** | 328 | /** |
329 | * @param strFilename Name of the file. | 329 | * @param strFilename Name of the file. |
330 | * @param dwOffset File offset the bound importdirectory will be written to. | 330 | * @param dwOffset File offset the bound importdirectory will be written to. |
331 | * @param fMakeValid If this flag is true a valid directory will be produced. | 331 | * @param fMakeValid If this flag is true a valid directory will be produced. |
332 | **/ | 332 | **/ |
333 | int BoundImportDirectory::write(const std::string& strFilename, dword dwOffset, bool fMakeValid) const | 333 | int BoundImportDirectory::write(const std::string& strFilename, dword dwOffset, bool fMakeValid) const |
334 | { | 334 | { |
335 | std::fstream ofFile(strFilename.c_str(), std::ios_base::in); | 335 | std::fstream ofFile(strFilename.c_str(), std::ios_base::in); |
336 | 336 | ||
337 | if (!ofFile) | 337 | if (!ofFile) |
338 | { | 338 | { |
339 | ofFile.clear(); | 339 | ofFile.clear(); |
340 | ofFile.open(strFilename.c_str(), std::ios_base::out | std::ios_base::binary); | 340 | ofFile.open(strFilename.c_str(), std::ios_base::out | std::ios_base::binary); |
341 | } | 341 | } |
342 | else | 342 | else |
343 | { | 343 | { |
344 | ofFile.close(); | 344 | ofFile.close(); |
345 | ofFile.open(strFilename.c_str(), std::ios_base::in | std::ios_base::out | std::ios_base::binary); | 345 | ofFile.open(strFilename.c_str(), std::ios_base::in | std::ios_base::out | std::ios_base::binary); |
346 | } | 346 | } |
347 | 347 | ||
348 | if (!ofFile) | 348 | if (!ofFile) |
349 | { | 349 | { |
350 | return ERROR_OPENING_FILE; | 350 | return ERROR_OPENING_FILE; |
351 | } | 351 | } |
352 | 352 | ||
353 | ofFile.seekp(dwOffset, std::ios::beg); | 353 | ofFile.seekp(dwOffset, std::ios::beg); |
354 | 354 | ||
355 | std::vector<unsigned char> vBuffer; | 355 | std::vector<unsigned char> vBuffer; |
356 | rebuild(vBuffer, fMakeValid); | 356 | rebuild(vBuffer, fMakeValid); |
357 | 357 | ||
358 | ofFile.write(reinterpret_cast<const char*>(&vBuffer[0]), static_cast<std::streamsize>(vBuffer.size())); | 358 | ofFile.write(reinterpret_cast<const char*>(&vBuffer[0]), static_cast<std::streamsize>(vBuffer.size())); |
359 | 359 | ||
360 | ofFile.close(); | 360 | ofFile.close(); |
361 | 361 | ||
362 | return NO_ERROR; | 362 | return NO_ERROR; |
363 | } | 363 | } |
364 | 364 | ||
365 | /** | 365 | /** |
366 | * Retrieves the value of the TimeDateStamp value of a bound import field. | 366 | * Retrieves the value of the TimeDateStamp value of a bound import field. |
367 | * @param dwBidnr Number of the bound import field. | 367 | * @param dwBidnr Number of the bound import field. |
368 | * @return Value of the TimeDateStamp of the bound import field. | 368 | * @return Value of the TimeDateStamp of the bound import field. |
369 | **/ | 369 | **/ |
370 | dword BoundImportDirectory::getTimeDateStamp(dword dwBidnr) const | 370 | dword BoundImportDirectory::getTimeDateStamp(dword dwBidnr) const |
371 | { | 371 | { |
372 | return m_vIbd[dwBidnr].ibdDescriptor.TimeDateStamp; | 372 | return m_vIbd[dwBidnr].ibdDescriptor.TimeDateStamp; |
373 | } | 373 | } |
374 | 374 | ||
375 | /** | 375 | /** |
376 | * Retrieves the value of the OffsetModuleName value of a bound import field. | 376 | * Retrieves the value of the OffsetModuleName value of a bound import field. |
377 | * @param dwBidnr Number of the bound import field. | 377 | * @param dwBidnr Number of the bound import field. |
378 | * @return Value of the OffsetModuleName of the bound import field. | 378 | * @return Value of the OffsetModuleName of the bound import field. |
379 | **/ | 379 | **/ |
380 | word BoundImportDirectory::getOffsetModuleName(dword dwBidnr) const | 380 | word BoundImportDirectory::getOffsetModuleName(dword dwBidnr) const |
381 | { | 381 | { |
382 | return m_vIbd[dwBidnr].ibdDescriptor.OffsetModuleName; | 382 | return m_vIbd[dwBidnr].ibdDescriptor.OffsetModuleName; |
383 | } | 383 | } |
384 | 384 | ||
385 | /** | 385 | /** |
386 | * Retrieves the value of the NumberOfModuleForwarderRefs value of a bound import field. | 386 | * Retrieves the value of the NumberOfModuleForwarderRefs value of a bound import field. |
387 | * @param dwBidnr Number of the bound import field. | 387 | * @param dwBidnr Number of the bound import field. |
388 | * @return Value of the NumberOfModuleForwarderRefs of the bound import field. | 388 | * @return Value of the NumberOfModuleForwarderRefs of the bound import field. |
389 | **/ | 389 | **/ |
390 | word BoundImportDirectory::getNumberOfModuleForwarderRefs(dword dwBidnr) const | 390 | word BoundImportDirectory::getNumberOfModuleForwarderRefs(dword dwBidnr) const |
391 | { | 391 | { |
392 | return m_vIbd[dwBidnr].ibdDescriptor.NumberOfModuleForwarderRefs; | 392 | return m_vIbd[dwBidnr].ibdDescriptor.NumberOfModuleForwarderRefs; |
393 | } | 393 | } |
394 | 394 | ||
395 | /** | 395 | /** |
396 | * Retrieves the value of the ModuleName value of a bound import field. | 396 | * Retrieves the value of the ModuleName value of a bound import field. |
397 | * @param dwBidnr Number of the bound import field. | 397 | * @param dwBidnr Number of the bound import field. |
398 | * @return Value of the ModuleName of the bound import field. | 398 | * @return Value of the ModuleName of the bound import field. |
399 | **/ | 399 | **/ |
400 | std::string BoundImportDirectory::getModuleName(dword dwBidnr) const | 400 | std::string BoundImportDirectory::getModuleName(dword dwBidnr) const |
401 | { | 401 | { |
402 | return m_vIbd[dwBidnr].strModuleName; | 402 | return m_vIbd[dwBidnr].strModuleName; |
403 | } | 403 | } |
404 | 404 | ||
405 | /** | 405 | /** |
406 | * Changes the TimeDateStamp value of an existing bound import field. | 406 | * Changes the TimeDateStamp value of an existing bound import field. |
407 | * @param dwBidnr Number of the bound import field which will be changed. | 407 | * @param dwBidnr Number of the bound import field which will be changed. |
408 | * @param dwTds New value of the TimeDateStamp of the bound import field. | 408 | * @param dwTds New value of the TimeDateStamp of the bound import field. |
409 | **/ | 409 | **/ |
410 | void BoundImportDirectory::setTimeDateStamp(dword dwBidnr, dword dwTds) | 410 | void BoundImportDirectory::setTimeDateStamp(dword dwBidnr, dword dwTds) |
411 | { | 411 | { |
412 | m_vIbd[dwBidnr].ibdDescriptor.TimeDateStamp = dwTds; | 412 | m_vIbd[dwBidnr].ibdDescriptor.TimeDateStamp = dwTds; |
413 | } | 413 | } |
414 | 414 | ||
415 | /** | 415 | /** |
416 | * Changes the OffsetModuleName value of an existing bound import field. | 416 | * Changes the OffsetModuleName value of an existing bound import field. |
417 | * @param dwBidnr Number of the bound import field which will be changed. | 417 | * @param dwBidnr Number of the bound import field which will be changed. |
418 | * @param wOmn New value of the OffsetModuleName of the bound import field. | 418 | * @param wOmn New value of the OffsetModuleName of the bound import field. |
419 | **/ | 419 | **/ |
420 | void BoundImportDirectory::setOffsetModuleName(dword dwBidnr, word wOmn) | 420 | void BoundImportDirectory::setOffsetModuleName(dword dwBidnr, word wOmn) |
421 | { | 421 | { |
422 | m_vIbd[dwBidnr].ibdDescriptor.OffsetModuleName = wOmn; | 422 | m_vIbd[dwBidnr].ibdDescriptor.OffsetModuleName = wOmn; |
423 | } | 423 | } |
424 | 424 | ||
425 | /** | 425 | /** |
426 | * Changes the NumberOfModuleForwarderRefs value of an existing bound import field. | 426 | * Changes the NumberOfModuleForwarderRefs value of an existing bound import field. |
427 | * @param dwBidnr Number of the bound import field which will be changed. | 427 | * @param dwBidnr Number of the bound import field which will be changed. |
428 | * @param wMfr New value of the NumberOfModuleForwarderRefs of the bound import field. | 428 | * @param wMfr New value of the NumberOfModuleForwarderRefs of the bound import field. |
429 | **/ | 429 | **/ |
430 | void BoundImportDirectory::setNumberOfModuleForwarderRefs(dword dwBidnr, word wMfr) | 430 | void BoundImportDirectory::setNumberOfModuleForwarderRefs(dword dwBidnr, word wMfr) |
431 | { | 431 | { |
432 | m_vIbd[dwBidnr].ibdDescriptor.NumberOfModuleForwarderRefs = wMfr; | 432 | m_vIbd[dwBidnr].ibdDescriptor.NumberOfModuleForwarderRefs = wMfr; |
433 | } | 433 | } |
434 | 434 | ||
435 | /** | 435 | /** |
436 | * Changes the ModuleName value of an existing bound import field. | 436 | * Changes the ModuleName value of an existing bound import field. |
437 | * @param dwBidnr Number of the bound import field which will be changed. | 437 | * @param dwBidnr Number of the bound import field which will be changed. |
438 | * @param strModuleName New value of the ModuleName of the bound import field. | 438 | * @param strModuleName New value of the ModuleName of the bound import field. |
439 | **/ | 439 | **/ |
440 | void BoundImportDirectory::setModuleName(dword dwBidnr, const std::string& strModuleName) | 440 | void BoundImportDirectory::setModuleName(dword dwBidnr, const std::string& strModuleName) |
441 | { | 441 | { |
442 | m_vIbd[dwBidnr].strModuleName = strModuleName; | 442 | m_vIbd[dwBidnr].strModuleName = strModuleName; |
443 | } | 443 | } |
444 | 444 | ||
445 | dword BoundImportDirectory::getTimeDateStamp(dword dwBidnr, dword forwardedModule) const | 445 | dword BoundImportDirectory::getTimeDateStamp(dword dwBidnr, dword forwardedModule) const |
446 | { | 446 | { |
447 | return m_vIbd[dwBidnr].moduleForwarders[forwardedModule].ibdDescriptor.TimeDateStamp; | 447 | return m_vIbd[dwBidnr].moduleForwarders[forwardedModule].ibdDescriptor.TimeDateStamp; |
448 | } | 448 | } |
449 | 449 | ||
450 | word BoundImportDirectory::getOffsetModuleName(dword dwBidnr, dword forwardedModule) const | 450 | word BoundImportDirectory::getOffsetModuleName(dword dwBidnr, dword forwardedModule) const |
451 | { | 451 | { |
452 | return m_vIbd[dwBidnr].moduleForwarders[forwardedModule].ibdDescriptor.OffsetModuleName; | 452 | return m_vIbd[dwBidnr].moduleForwarders[forwardedModule].ibdDescriptor.OffsetModuleName; |
453 | } | 453 | } |
454 | 454 | ||
455 | word BoundImportDirectory::getNumberOfModuleForwarderRefs(dword dwBidnr, dword forwardedModule) const | 455 | word BoundImportDirectory::getNumberOfModuleForwarderRefs(dword dwBidnr, dword forwardedModule) const |
456 | { | 456 | { |
457 | return m_vIbd[dwBidnr].moduleForwarders[forwardedModule].ibdDescriptor.NumberOfModuleForwarderRefs; | 457 | return m_vIbd[dwBidnr].moduleForwarders[forwardedModule].ibdDescriptor.NumberOfModuleForwarderRefs; |
458 | } | 458 | } |
459 | 459 | ||
460 | std::string BoundImportDirectory::getModuleName(dword dwBidnr, dword forwardedModule) const | 460 | std::string BoundImportDirectory::getModuleName(dword dwBidnr, dword forwardedModule) const |
461 | { | 461 | { |
462 | return m_vIbd[dwBidnr].moduleForwarders[forwardedModule].strModuleName; | 462 | return m_vIbd[dwBidnr].moduleForwarders[forwardedModule].strModuleName; |
463 | } | 463 | } |
464 | 464 | ||
465 | void BoundImportDirectory::setTimeDateStamp(dword dwBidnr, dword forwardedModule, dword dwTds) | 465 | void BoundImportDirectory::setTimeDateStamp(dword dwBidnr, dword forwardedModule, dword dwTds) |
466 | { | 466 | { |
467 | m_vIbd[dwBidnr].moduleForwarders[forwardedModule].ibdDescriptor.TimeDateStamp = dwTds; | 467 | m_vIbd[dwBidnr].moduleForwarders[forwardedModule].ibdDescriptor.TimeDateStamp = dwTds; |
468 | } | 468 | } |
469 | 469 | ||
470 | void BoundImportDirectory::setOffsetModuleName(dword dwBidnr, dword forwardedModule, word wOmn) | 470 | void BoundImportDirectory::setOffsetModuleName(dword dwBidnr, dword forwardedModule, word wOmn) |
471 | { | 471 | { |
472 | m_vIbd[dwBidnr].moduleForwarders[forwardedModule].ibdDescriptor.OffsetModuleName = wOmn; | 472 | m_vIbd[dwBidnr].moduleForwarders[forwardedModule].ibdDescriptor.OffsetModuleName = wOmn; |
473 | } | 473 | } |
474 | 474 | ||
475 | void BoundImportDirectory::setNumberOfModuleForwarderRefs(dword dwBidnr, dword forwardedModule, word wMfr) | 475 | void BoundImportDirectory::setNumberOfModuleForwarderRefs(dword dwBidnr, dword forwardedModule, word wMfr) |
476 | { | 476 | { |
477 | m_vIbd[dwBidnr].moduleForwarders[forwardedModule].ibdDescriptor.NumberOfModuleForwarderRefs = wMfr; | 477 | m_vIbd[dwBidnr].moduleForwarders[forwardedModule].ibdDescriptor.NumberOfModuleForwarderRefs = wMfr; |
478 | } | 478 | } |
479 | 479 | ||
480 | void BoundImportDirectory::setModuleName(dword dwBidnr, dword forwardedModule, const std::string& strModuleName) | 480 | void BoundImportDirectory::setModuleName(dword dwBidnr, dword forwardedModule, const std::string& strModuleName) |
481 | { | 481 | { |
482 | m_vIbd[dwBidnr].moduleForwarders[forwardedModule].strModuleName = strModuleName; | 482 | m_vIbd[dwBidnr].moduleForwarders[forwardedModule].strModuleName = strModuleName; |
483 | } | 483 | } |
484 | 484 | ||
485 | word BoundImportDirectory::calcNumberOfModuleForwarderRefs(dword dwBidnr) const | 485 | word BoundImportDirectory::calcNumberOfModuleForwarderRefs(dword dwBidnr) const |
486 | { | 486 | { |
487 | return static_cast<word>(m_vIbd[dwBidnr].moduleForwarders.size()); | 487 | return static_cast<word>(m_vIbd[dwBidnr].moduleForwarders.size()); |
488 | } | 488 | } |
489 | 489 | ||
490 | void BoundImportDirectory::addForwardedModule(dword dwBidnr, const std::string& name, dword timeStamp, word offsetModuleName, word forwardedModules) | 490 | void BoundImportDirectory::addForwardedModule(dword dwBidnr, const std::string& name, dword timeStamp, word offsetModuleName, word forwardedModules) |
491 | { | 491 | { |
492 | // XXX: Maybe test if there are already 0xFFFF forwarded modules. | 492 | // XXX: Maybe test if there are already 0xFFFF forwarded modules. |
493 | // XXX: Check for duplicate entries. Is it also necessary to check | 493 | // XXX: Check for duplicate entries. Is it also necessary to check |
494 | // non-forwarded entries and forwarded entries in other non-forwarded | 494 | // non-forwarded entries and forwarded entries in other non-forwarded |
495 | // entries? | 495 | // entries? |
496 | // XXX: Can forwarders forward recursively? | 496 | // XXX: Can forwarders forward recursively? |
497 | 497 | ||
498 | PELIB_IMAGE_BOUND_DIRECTORY ibdCurrent; | 498 | PELIB_IMAGE_BOUND_DIRECTORY ibdCurrent; |
499 | ibdCurrent.strModuleName = name; | 499 | ibdCurrent.strModuleName = name; |
500 | ibdCurrent.ibdDescriptor.TimeDateStamp = timeStamp; | 500 | ibdCurrent.ibdDescriptor.TimeDateStamp = timeStamp; |
501 | ibdCurrent.ibdDescriptor.OffsetModuleName = offsetModuleName; | 501 | ibdCurrent.ibdDescriptor.OffsetModuleName = offsetModuleName; |
502 | ibdCurrent.ibdDescriptor.NumberOfModuleForwarderRefs = forwardedModules; | 502 | ibdCurrent.ibdDescriptor.NumberOfModuleForwarderRefs = forwardedModules; |
503 | 503 | ||
504 | m_vIbd[dwBidnr].moduleForwarders.push_back(ibdCurrent); | 504 | m_vIbd[dwBidnr].moduleForwarders.push_back(ibdCurrent); |
505 | } | 505 | } |
506 | 506 | ||
507 | void BoundImportDirectory::removeForwardedModule(dword dwBidnr, word forwardedModule) | 507 | void BoundImportDirectory::removeForwardedModule(dword dwBidnr, word forwardedModule) |
508 | { | 508 | { |
509 | m_vIbd[dwBidnr].moduleForwarders.erase(m_vIbd[dwBidnr].moduleForwarders.begin() + forwardedModule); | 509 | m_vIbd[dwBidnr].moduleForwarders.erase(m_vIbd[dwBidnr].moduleForwarders.begin() + forwardedModule); |
510 | } | 510 | } |
511 | } | 511 | } |