diff options
author | Maurus Cuelenaere <mcuelenaere@gmail.com> | 2008-07-11 15:50:46 +0000 |
---|---|---|
committer | Maurus Cuelenaere <mcuelenaere@gmail.com> | 2008-07-11 15:50:46 +0000 |
commit | 14c7f45cdae826f88dc539c8c38dd95caf305731 (patch) | |
tree | 832da054b7cfb2dc6fd63339af736625f31d21aa /utils/zenutils/libraries/pelib-0.9/pelib/ComHeaderDirectory.cpp | |
parent | 7c84ede3781c27db73403bd6302f320c76a58c8c (diff) | |
download | rockbox-14c7f45cdae826f88dc539c8c38dd95caf305731.tar.gz rockbox-14c7f45cdae826f88dc539c8c38dd95caf305731.zip |
Add zook's ZenUtils to SVN
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@18010 a1c6a512-1295-4272-9138-f99709370657
Diffstat (limited to 'utils/zenutils/libraries/pelib-0.9/pelib/ComHeaderDirectory.cpp')
-rwxr-xr-x | utils/zenutils/libraries/pelib-0.9/pelib/ComHeaderDirectory.cpp | 467 |
1 files changed, 467 insertions, 0 deletions
diff --git a/utils/zenutils/libraries/pelib-0.9/pelib/ComHeaderDirectory.cpp b/utils/zenutils/libraries/pelib-0.9/pelib/ComHeaderDirectory.cpp new file mode 100755 index 0000000000..886348994e --- /dev/null +++ b/utils/zenutils/libraries/pelib-0.9/pelib/ComHeaderDirectory.cpp | |||
@@ -0,0 +1,467 @@ | |||
1 | /* | ||
2 | * ComHeaderDirectory.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 "ComHeaderDirectory.h" | ||
15 | |||
16 | namespace PeLib | ||
17 | { | ||
18 | void ComHeaderDirectory::read(InputBuffer& inputbuffer) | ||
19 | { | ||
20 | PELIB_IMAGE_COR20_HEADER ichCurr; | ||
21 | |||
22 | inputbuffer >> ichCurr.cb; | ||
23 | inputbuffer >> ichCurr.MajorRuntimeVersion; | ||
24 | inputbuffer >> ichCurr.MinorRuntimeVersion; | ||
25 | inputbuffer >> ichCurr.MetaData.VirtualAddress; | ||
26 | inputbuffer >> ichCurr.MetaData.Size; | ||
27 | inputbuffer >> ichCurr.Flags; | ||
28 | inputbuffer >> ichCurr.EntryPointToken; | ||
29 | inputbuffer >> ichCurr.Resources.VirtualAddress; | ||
30 | inputbuffer >> ichCurr.Resources.Size; | ||
31 | inputbuffer >> ichCurr.StrongNameSignature.VirtualAddress; | ||
32 | inputbuffer >> ichCurr.StrongNameSignature.Size; | ||
33 | inputbuffer >> ichCurr.CodeManagerTable.VirtualAddress; | ||
34 | inputbuffer >> ichCurr.CodeManagerTable.Size; | ||
35 | inputbuffer >> ichCurr.VTableFixups.VirtualAddress; | ||
36 | inputbuffer >> ichCurr.VTableFixups.Size; | ||
37 | inputbuffer >> ichCurr.ExportAddressTableJumps.VirtualAddress; | ||
38 | inputbuffer >> ichCurr.ExportAddressTableJumps.Size; | ||
39 | inputbuffer >> ichCurr.ManagedNativeHeader.VirtualAddress; | ||
40 | inputbuffer >> ichCurr.ManagedNativeHeader.Size; | ||
41 | |||
42 | std::swap(ichCurr, m_ichComHeader); | ||
43 | } | ||
44 | |||
45 | int ComHeaderDirectory::read(unsigned char* buffer, unsigned int buffersize) | ||
46 | { | ||
47 | if (buffersize < PELIB_IMAGE_COR20_HEADER::size()) | ||
48 | { | ||
49 | return ERROR_INVALID_FILE; | ||
50 | } | ||
51 | |||
52 | std::vector<byte> vComDescDirectory(buffer, buffer + buffersize); | ||
53 | |||
54 | InputBuffer ibBuffer(vComDescDirectory); | ||
55 | read(ibBuffer); | ||
56 | return NO_ERROR; | ||
57 | } | ||
58 | |||
59 | /** | ||
60 | * Reads a file's COM+ descriptor. | ||
61 | * @param strFilename Name of the file. | ||
62 | * @param uiOffset File offset of the COM+ descriptor. | ||
63 | * @param uiSize Size of the COM+ descriptor. | ||
64 | **/ | ||
65 | int ComHeaderDirectory::read(const std::string& strFilename, unsigned int uiOffset, unsigned int uiSize) | ||
66 | { | ||
67 | std::ifstream ifFile(strFilename.c_str(), std::ios::binary); | ||
68 | unsigned int ulFileSize = fileSize(ifFile); | ||
69 | |||
70 | if (!ifFile) | ||
71 | { | ||
72 | return ERROR_OPENING_FILE; | ||
73 | } | ||
74 | |||
75 | if (ulFileSize < uiOffset + uiSize) | ||
76 | { | ||
77 | return ERROR_INVALID_FILE; | ||
78 | } | ||
79 | |||
80 | ifFile.seekg(uiOffset, std::ios::beg); | ||
81 | |||
82 | std::vector<byte> vComDescDirectory(uiSize); | ||
83 | ifFile.read(reinterpret_cast<char*>(&vComDescDirectory[0]), uiSize); | ||
84 | |||
85 | InputBuffer ibBuffer(vComDescDirectory); | ||
86 | read(ibBuffer); | ||
87 | return NO_ERROR; | ||
88 | } | ||
89 | |||
90 | /** | ||
91 | * Rebuilds the current COM+ descriptor. | ||
92 | * @param vBuffer Buffer where the COM+ descriptor will be written to. | ||
93 | **/ | ||
94 | void ComHeaderDirectory::rebuild(std::vector<byte>& vBuffer) const | ||
95 | { | ||
96 | OutputBuffer obBuffer(vBuffer); | ||
97 | |||
98 | obBuffer << m_ichComHeader.cb; | ||
99 | obBuffer << m_ichComHeader.MajorRuntimeVersion; | ||
100 | obBuffer << m_ichComHeader.MinorRuntimeVersion; | ||
101 | obBuffer << m_ichComHeader.MetaData.VirtualAddress; | ||
102 | obBuffer << m_ichComHeader.MetaData.Size; | ||
103 | obBuffer << m_ichComHeader.Flags; | ||
104 | obBuffer << m_ichComHeader.EntryPointToken; | ||
105 | obBuffer << m_ichComHeader.Resources.VirtualAddress; | ||
106 | obBuffer << m_ichComHeader.Resources.Size; | ||
107 | obBuffer << m_ichComHeader.StrongNameSignature.VirtualAddress; | ||
108 | obBuffer << m_ichComHeader.StrongNameSignature.Size; | ||
109 | obBuffer << m_ichComHeader.CodeManagerTable.VirtualAddress; | ||
110 | obBuffer << m_ichComHeader.CodeManagerTable.Size; | ||
111 | obBuffer << m_ichComHeader.VTableFixups.VirtualAddress; | ||
112 | obBuffer << m_ichComHeader.VTableFixups.Size; | ||
113 | obBuffer << m_ichComHeader.ExportAddressTableJumps.VirtualAddress; | ||
114 | obBuffer << m_ichComHeader.ExportAddressTableJumps.Size; | ||
115 | obBuffer << m_ichComHeader.ManagedNativeHeader.VirtualAddress; | ||
116 | obBuffer << m_ichComHeader.ManagedNativeHeader.Size; | ||
117 | } | ||
118 | |||
119 | /** | ||
120 | * @return Size in bytes. | ||
121 | **/ | ||
122 | unsigned int ComHeaderDirectory::size() const | ||
123 | { | ||
124 | return PELIB_IMAGE_COR20_HEADER::size(); | ||
125 | } | ||
126 | |||
127 | /** | ||
128 | * @param strFilename Name of the file. | ||
129 | * @param dwOffset File offset the COM+ descriptor will be written to. | ||
130 | **/ | ||
131 | int ComHeaderDirectory::write(const std::string& strFilename, unsigned int dwOffset) const | ||
132 | { | ||
133 | std::fstream ofFile(strFilename.c_str(), std::ios_base::in); | ||
134 | |||
135 | if (!ofFile) | ||
136 | { | ||
137 | ofFile.clear(); | ||
138 | ofFile.open(strFilename.c_str(), std::ios_base::out | std::ios_base::binary); | ||
139 | } | ||
140 | else | ||
141 | { | ||
142 | ofFile.close(); | ||
143 | ofFile.open(strFilename.c_str(), std::ios_base::in | std::ios_base::out | std::ios_base::binary); | ||
144 | } | ||
145 | |||
146 | if (!ofFile) | ||
147 | { | ||
148 | return ERROR_OPENING_FILE; | ||
149 | } | ||
150 | |||
151 | ofFile.seekp(dwOffset, std::ios::beg); | ||
152 | |||
153 | std::vector<unsigned char> vBuffer; | ||
154 | rebuild(vBuffer); | ||
155 | |||
156 | ofFile.write(reinterpret_cast<const char*>(&vBuffer[0]), static_cast<unsigned int>(vBuffer.size())); | ||
157 | |||
158 | ofFile.close(); | ||
159 | |||
160 | return NO_ERROR; | ||
161 | } | ||
162 | |||
163 | /** | ||
164 | * @return SizeOfHeader value of the current COM+ descriptor. | ||
165 | **/ | ||
166 | dword ComHeaderDirectory::getSizeOfHeader() const | ||
167 | { | ||
168 | return m_ichComHeader.cb; | ||
169 | } | ||
170 | |||
171 | /** | ||
172 | * @return MajorRuntimeVersion value of the current COM+ descriptor. | ||
173 | **/ | ||
174 | word ComHeaderDirectory::getMajorRuntimeVersion() const | ||
175 | { | ||
176 | return m_ichComHeader.MajorRuntimeVersion; | ||
177 | } | ||
178 | |||
179 | /** | ||
180 | * @return MinorRuntimeVersion value of the current COM+ descriptor. | ||
181 | **/ | ||
182 | word ComHeaderDirectory::getMinorRuntimeVersion() const | ||
183 | { | ||
184 | return m_ichComHeader.MinorRuntimeVersion; | ||
185 | } | ||
186 | |||
187 | /** | ||
188 | * @return MetaData (Virtual Address) value of the current COM+ descriptor. | ||
189 | **/ | ||
190 | dword ComHeaderDirectory::getMetaDataVa() const | ||
191 | { | ||
192 | return m_ichComHeader.MetaData.VirtualAddress; | ||
193 | } | ||
194 | |||
195 | /** | ||
196 | * @return MetaData (Size) value of the current COM+ descriptor. | ||
197 | **/ | ||
198 | dword ComHeaderDirectory::getMetaDataSize() const | ||
199 | { | ||
200 | return m_ichComHeader.MetaData.Size; | ||
201 | } | ||
202 | |||
203 | /** | ||
204 | * @return Flags value of the current COM+ descriptor. | ||
205 | **/ | ||
206 | dword ComHeaderDirectory::getFlags() const | ||
207 | { | ||
208 | return m_ichComHeader.Flags; | ||
209 | } | ||
210 | |||
211 | /** | ||
212 | * @return EntryPointToken value of the current COM+ descriptor. | ||
213 | **/ | ||
214 | dword ComHeaderDirectory::getEntryPointToken() const | ||
215 | { | ||
216 | return m_ichComHeader.EntryPointToken; | ||
217 | } | ||
218 | |||
219 | /** | ||
220 | * @return Resources (Virtual Address) value of the current COM+ descriptor. | ||
221 | **/ | ||
222 | dword ComHeaderDirectory::getResourcesVa() const | ||
223 | { | ||
224 | return m_ichComHeader.Resources.VirtualAddress; | ||
225 | } | ||
226 | |||
227 | /** | ||
228 | * @return Resources (Size) value of the current COM+ descriptor. | ||
229 | **/ | ||
230 | dword ComHeaderDirectory::getResourcesSize() | ||
231 | { | ||
232 | return m_ichComHeader.Resources.Size; | ||
233 | } | ||
234 | |||
235 | /** | ||
236 | * @return StrongNameSignature (Virtual Address) value of the current COM+ descriptor. | ||
237 | **/ | ||
238 | dword ComHeaderDirectory::getStrongNameSignatureVa() const | ||
239 | { | ||
240 | return m_ichComHeader.StrongNameSignature.VirtualAddress; | ||
241 | } | ||
242 | |||
243 | /** | ||
244 | * @return StrongNameSignature (Size) value of the current COM+ descriptor. | ||
245 | **/ | ||
246 | dword ComHeaderDirectory::getStrongNameSignagureSize() const | ||
247 | { | ||
248 | return m_ichComHeader.StrongNameSignature.Size; | ||
249 | } | ||
250 | |||
251 | /** | ||
252 | * @return CodeManagerTable (Virtual Address) value of the current COM+ descriptor. | ||
253 | **/ | ||
254 | dword ComHeaderDirectory::getCodeManagerTableVa() const | ||
255 | { | ||
256 | return m_ichComHeader.CodeManagerTable.VirtualAddress; | ||
257 | } | ||
258 | |||
259 | /** | ||
260 | * @return CodeManagerTable (Size) value of the current COM+ descriptor. | ||
261 | **/ | ||
262 | dword ComHeaderDirectory::getCodeManagerTableSize() const | ||
263 | { | ||
264 | return m_ichComHeader.CodeManagerTable.Size; | ||
265 | } | ||
266 | |||
267 | /** | ||
268 | * @return VTableFixups (Virtual Address) value of the current COM+ descriptor. | ||
269 | **/ | ||
270 | dword ComHeaderDirectory::getVTableFixupsVa() const | ||
271 | { | ||
272 | return m_ichComHeader.VTableFixups.VirtualAddress; | ||
273 | } | ||
274 | |||
275 | /** | ||
276 | * @return VTableFixups (Size) value of the current COM+ descriptor. | ||
277 | **/ | ||
278 | dword ComHeaderDirectory::getVTableFixupsSize() const | ||
279 | { | ||
280 | return m_ichComHeader.VTableFixups.Size; | ||
281 | } | ||
282 | |||
283 | /** | ||
284 | * @return ExportAddressTableJumps (Virtual Address) value of the current COM+ descriptor. | ||
285 | **/ | ||
286 | dword ComHeaderDirectory::getExportAddressTableJumpsVa() const | ||
287 | { | ||
288 | return m_ichComHeader.ExportAddressTableJumps.VirtualAddress; | ||
289 | } | ||
290 | |||
291 | /** | ||
292 | * @return ExportAddressTableJumps (Size) value of the current COM+ descriptor. | ||
293 | **/ | ||
294 | dword ComHeaderDirectory::getExportAddressTableJumpsSize() const | ||
295 | { | ||
296 | return m_ichComHeader.ExportAddressTableJumps.Size; | ||
297 | } | ||
298 | |||
299 | /** | ||
300 | * @return ManagedNativeHeader (Virtual Address) value of the current COM+ descriptor. | ||
301 | **/ | ||
302 | dword ComHeaderDirectory::getManagedNativeHeaderVa() const | ||
303 | { | ||
304 | return m_ichComHeader.ManagedNativeHeader.VirtualAddress; | ||
305 | } | ||
306 | |||
307 | /** | ||
308 | * @return ManagedNativeHeader (Size) value of the current COM+ descriptor. | ||
309 | **/ | ||
310 | dword ComHeaderDirectory::getManagedNativeHeaderSize() const | ||
311 | { | ||
312 | return m_ichComHeader.ManagedNativeHeader.Size; | ||
313 | } | ||
314 | |||
315 | /** | ||
316 | * @param dwValue New value for the current SizeOfHeader (cb) value. | ||
317 | **/ | ||
318 | void ComHeaderDirectory::setSizeOfHeader(dword dwValue) | ||
319 | { | ||
320 | m_ichComHeader.cb = dwValue; | ||
321 | } | ||
322 | |||
323 | /** | ||
324 | * @param wValue New value for the current MajorRuntimeVersion value. | ||
325 | **/ | ||
326 | void ComHeaderDirectory::setMajorRuntimeVersion(word wValue) | ||
327 | { | ||
328 | m_ichComHeader.MajorRuntimeVersion = wValue; | ||
329 | } | ||
330 | |||
331 | /** | ||
332 | * @param wValue New value for the current MinorRuntimeVersion value. | ||
333 | **/ | ||
334 | void ComHeaderDirectory::setMinorRuntimeVersion(word wValue) | ||
335 | { | ||
336 | m_ichComHeader.MinorRuntimeVersion = wValue; | ||
337 | } | ||
338 | |||
339 | /** | ||
340 | * @param dwValue New value for the current MetaData (VirtualAddress) value. | ||
341 | **/ | ||
342 | void ComHeaderDirectory::setMetaDataVa(dword dwValue) | ||
343 | { | ||
344 | m_ichComHeader.MetaData.VirtualAddress = dwValue; | ||
345 | } | ||
346 | |||
347 | /** | ||
348 | * @param dwValue New value for the current MetaData (Size) value. | ||
349 | **/ | ||
350 | void ComHeaderDirectory::setMetaDataSize(dword dwValue) | ||
351 | { | ||
352 | m_ichComHeader.MetaData.Size = dwValue; | ||
353 | } | ||
354 | |||
355 | /** | ||
356 | * @param dwValue New value for the current Flags value. | ||
357 | **/ | ||
358 | void ComHeaderDirectory::setFlags(dword dwValue) | ||
359 | { | ||
360 | m_ichComHeader.Flags = dwValue; | ||
361 | } | ||
362 | |||
363 | /** | ||
364 | * @param dwValue New value for the current EntryPointToken value. | ||
365 | **/ | ||
366 | void ComHeaderDirectory::setEntryPointToken(dword dwValue) | ||
367 | { | ||
368 | m_ichComHeader.EntryPointToken = dwValue; | ||
369 | } | ||
370 | |||
371 | /** | ||
372 | * @param dwValue New value for the current Resources (VirtualAddress) value. | ||
373 | **/ | ||
374 | void ComHeaderDirectory::setResourcesVa(dword dwValue) | ||
375 | { | ||
376 | m_ichComHeader.Resources.VirtualAddress = dwValue; | ||
377 | } | ||
378 | |||
379 | /** | ||
380 | * @param dwValue New value for the current Resources (Size) value. | ||
381 | **/ | ||
382 | void ComHeaderDirectory::setResourcesSize(dword dwValue) | ||
383 | { | ||
384 | m_ichComHeader.Resources.Size = dwValue; | ||
385 | } | ||
386 | |||
387 | /** | ||
388 | * @param dwValue New value for the current StrongNameSignature (VirtualAddress) value. | ||
389 | **/ | ||
390 | void ComHeaderDirectory::setStrongNameSignatureVa(dword dwValue) | ||
391 | { | ||
392 | m_ichComHeader.StrongNameSignature.VirtualAddress = dwValue; | ||
393 | } | ||
394 | |||
395 | /** | ||
396 | * @param dwValue New value for the current StrongNameSignature (Size) value. | ||
397 | **/ | ||
398 | void ComHeaderDirectory::setStrongNameSignagureSize(dword dwValue) | ||
399 | { | ||
400 | m_ichComHeader.StrongNameSignature.Size = dwValue; | ||
401 | } | ||
402 | |||
403 | /** | ||
404 | * @param dwValue New value for the current CodeManagerTable (VirtualAddress) value. | ||
405 | **/ | ||
406 | void ComHeaderDirectory::setCodeManagerTableVa(dword dwValue) | ||
407 | { | ||
408 | m_ichComHeader.CodeManagerTable.VirtualAddress = dwValue; | ||
409 | } | ||
410 | |||
411 | /** | ||
412 | * @param dwValue New value for the current CodeManagerTable (Size) value. | ||
413 | **/ | ||
414 | void ComHeaderDirectory::setCodeManagerTableSize(dword dwValue) | ||
415 | { | ||
416 | m_ichComHeader.CodeManagerTable.Size = dwValue; | ||
417 | } | ||
418 | |||
419 | /** | ||
420 | * @param dwValue New value for the current VTableFixups (VirtualAddress) value. | ||
421 | **/ | ||
422 | void ComHeaderDirectory::setVTableFixupsVa(dword dwValue) | ||
423 | { | ||
424 | m_ichComHeader.VTableFixups.VirtualAddress = dwValue; | ||
425 | } | ||
426 | |||
427 | /** | ||
428 | * @param dwValue New value for the current VTableFixups (Size) value. | ||
429 | **/ | ||
430 | void ComHeaderDirectory::setVTableFixupsSize(dword dwValue) | ||
431 | { | ||
432 | m_ichComHeader.VTableFixups.Size = dwValue; | ||
433 | } | ||
434 | |||
435 | /** | ||
436 | * @param dwValue New value for the current ExportAddressTableJumps (VirtualAddress) value. | ||
437 | **/ | ||
438 | void ComHeaderDirectory::setExportAddressTableJumpsVa(dword dwValue) | ||
439 | { | ||
440 | m_ichComHeader.ExportAddressTableJumps.VirtualAddress = dwValue; | ||
441 | } | ||
442 | |||
443 | /** | ||
444 | * @param dwValue New value for the current ExportAddressTableJumps (Size) value. | ||
445 | **/ | ||
446 | void ComHeaderDirectory::setExportAddressTableJumpsSize(dword dwValue) | ||
447 | { | ||
448 | m_ichComHeader.ExportAddressTableJumps.Size = dwValue; | ||
449 | } | ||
450 | |||
451 | /** | ||
452 | * @param dwValue New value for the current ManagedNativeHeader (VirtualAddress) value. | ||
453 | **/ | ||
454 | void ComHeaderDirectory::setManagedNativeHeaderVa(dword dwValue) | ||
455 | { | ||
456 | m_ichComHeader.ManagedNativeHeader.VirtualAddress = dwValue; | ||
457 | } | ||
458 | |||
459 | /** | ||
460 | * @param dwValue New value for the current ManagedNativeHeader (Size) value. | ||
461 | **/ | ||
462 | void ComHeaderDirectory::setManagedNativeHeaderSize(dword dwValue) | ||
463 | { | ||
464 | m_ichComHeader.ManagedNativeHeader.Size = dwValue; | ||
465 | } | ||
466 | |||
467 | } | ||