summaryrefslogtreecommitdiff
path: root/utils/zenutils/libraries/pelib-0.9/pelib/MzHeader.cpp
diff options
context:
space:
mode:
authorMaurus Cuelenaere <mcuelenaere@gmail.com>2008-07-11 15:50:46 +0000
committerMaurus Cuelenaere <mcuelenaere@gmail.com>2008-07-11 15:50:46 +0000
commit14c7f45cdae826f88dc539c8c38dd95caf305731 (patch)
tree832da054b7cfb2dc6fd63339af736625f31d21aa /utils/zenutils/libraries/pelib-0.9/pelib/MzHeader.cpp
parent7c84ede3781c27db73403bd6302f320c76a58c8c (diff)
downloadrockbox-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/MzHeader.cpp')
-rwxr-xr-xutils/zenutils/libraries/pelib-0.9/pelib/MzHeader.cpp584
1 files changed, 584 insertions, 0 deletions
diff --git a/utils/zenutils/libraries/pelib-0.9/pelib/MzHeader.cpp b/utils/zenutils/libraries/pelib-0.9/pelib/MzHeader.cpp
new file mode 100755
index 0000000000..39fe54d80d
--- /dev/null
+++ b/utils/zenutils/libraries/pelib-0.9/pelib/MzHeader.cpp
@@ -0,0 +1,584 @@
1/*
2* MzHeader.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 "MzHeader.h"
14#include <iostream>
15
16namespace PeLib
17{
18 /**
19 * Reads data from an InputBuffer into the struct that represents the MZ header.
20 * It's required that the size of the input buffer is at least as big as the
21 * size of a MZ header. Otherwise we get undefined behaviour.
22 * @param ibBuffer InputBuffer that holds the data.
23 * @return A non-zero value is returned if a problem occured.
24 **/
25 void MzHeader::read(InputBuffer& ibBuffer)
26 {
27 ibBuffer >> m_idhHeader.e_magic;
28 ibBuffer >> m_idhHeader.e_cblp;
29 ibBuffer >> m_idhHeader.e_cp;
30 ibBuffer >> m_idhHeader.e_crlc;
31 ibBuffer >> m_idhHeader.e_cparhdr;
32 ibBuffer >> m_idhHeader.e_minalloc;
33 ibBuffer >> m_idhHeader.e_maxalloc;
34 ibBuffer >> m_idhHeader.e_ss;
35 ibBuffer >> m_idhHeader.e_sp;
36 ibBuffer >> m_idhHeader.e_csum;
37 ibBuffer >> m_idhHeader.e_ip;
38 ibBuffer >> m_idhHeader.e_cs;
39 ibBuffer >> m_idhHeader.e_lfarlc;
40 ibBuffer >> m_idhHeader.e_ovno;
41
42 for (unsigned int i=0;i<sizeof(m_idhHeader.e_res)/sizeof(m_idhHeader.e_res[0]);i++)
43 {
44 ibBuffer >> m_idhHeader.e_res[i];
45 }
46
47 ibBuffer >> m_idhHeader.e_oemid;
48 ibBuffer >> m_idhHeader.e_oeminfo;
49
50 for (unsigned int i=0;i<sizeof(m_idhHeader.e_res2)/sizeof(m_idhHeader.e_res2[0]);i++)
51 {
52 ibBuffer >> m_idhHeader.e_res2[i];
53 }
54
55 ibBuffer >> m_idhHeader.e_lfanew;
56 }
57
58 /**
59 * Tests if the currently loaded MZ header is a valid MZ header.
60 * Note that this function does not check if the address to the PE header is valid as this is not possible.
61 * Actually, the only thing this function checks is if the e_magic value is set to 0x5A4D (IMAGE_DOS_SIGNATURE).
62 * Everything else is not relevant for Windows 2000 and that's the system PeLib is focusing on for now.
63 * @return A boolean value that indicates if the MZ header is correct or not.
64 **/
65 bool MzHeader::isValid() const
66 {
67 // The only thing that matters on Windows 2K is the e_magic value. The entire rest is for DOS compatibility.
68 return isValid(e_magic);
69 }
70
71 bool MzHeader::isValid(Field f) const
72 {
73 if (f == e_magic)
74 {
75 return m_idhHeader.e_magic == PELIB_IMAGE_DOS_SIGNATURE;
76 }
77 else
78 {
79 return true;
80 }
81 }
82
83 /**
84 * Corrects all erroneous values of the current MZ header. Note that this function does not correct the
85 * pointer to the PE header.
86 * Actually, the only thing this function corrects is the e_magic value.
87 * Everything else is not relevant for Windows 2000 and that's the system PeLib is focusing on for now.
88 **/
89 void MzHeader::makeValid()
90 {
91 // The only thing that matters on Windows is the e_magic value. The entire rest is for DOS compatibility.
92 setMagicNumber(PELIB_IMAGE_DOS_SIGNATURE);
93 }
94
95 void MzHeader::makeValid(Field f)
96 {
97 if (f == e_magic)
98 {
99 setMagicNumber(PELIB_IMAGE_DOS_SIGNATURE);
100 }
101 }
102
103 /**
104 * Reads the MZ header from a file. Note that this function does not verify if a file is actually a MZ file.
105 * For this purpose see #PeFile::MzHeader::isValid. The reason for this is simple: Otherwise it might not
106 * be possible to load damaged PE files to repair them.
107 * @param strFilename Name of the file which will be read.
108 * @return A non-zero value is returned if a problem occured.
109 **/
110 int MzHeader::read(const std::string& strFilename)
111 {
112 std::ifstream ifFile(strFilename.c_str(), std::ios::binary);
113
114 if (!ifFile)
115 {
116 return ERROR_OPENING_FILE;
117 }
118
119 if (fileSize(ifFile) < PELIB_IMAGE_DOS_HEADER::size())
120 {
121 return ERROR_INVALID_FILE;
122 }
123
124 ifFile.seekg(0, std::ios::beg);
125
126 originalOffset = 0;
127
128 std::vector<byte> vBuffer(PELIB_IMAGE_DOS_HEADER::size());
129 ifFile.read(reinterpret_cast<char*>(&vBuffer[0]), static_cast<unsigned int>(vBuffer.size()));
130 ifFile.close();
131
132 InputBuffer ibBuffer(vBuffer);
133 read(ibBuffer);
134 return NO_ERROR;
135 }
136
137 /**
138 * Reads the MZ header from memory. A pointer to a location in memory is passed and the data
139 * at this location is treated like a MZ header structure. The MZ header does not need to be valid.
140 * @param pcBuffer Pointer to a MZ header.
141 * @param uiSize Length of the buffer.
142 * @return A non-zero value is returned if a problem occured.
143 **/
144 int MzHeader::read(unsigned char* pcBuffer, unsigned int uiSize, unsigned int originalOffs)
145 {
146 if (uiSize < PELIB_IMAGE_DOS_HEADER::size())
147 {
148 return ERROR_INVALID_FILE;
149 }
150
151 std::vector<byte> vBuffer(pcBuffer, pcBuffer + uiSize);
152 for (int i=0;i<0x40;i++) std::cout << std::hex << (int)vBuffer[i] << " ";
153
154 originalOffset = originalOffs;
155
156 InputBuffer ibBuffer(vBuffer);
157 read(ibBuffer);
158 return NO_ERROR;
159 }
160
161 /**
162 * Rebuilds the MZ header so that it can be written to a file. It's not guaranteed that the
163 * MZ header will be valid. If you want to make sure that the MZ header will be valid you
164 * must call #PeLib::MzHeader::makeValid first.
165 * @param vBuffer Buffer where the rebuilt MZ header will be stored.
166 **/
167 void MzHeader::rebuild(std::vector<byte>& vBuffer) const
168 {
169 OutputBuffer obBuffer(vBuffer);
170
171 obBuffer << m_idhHeader.e_magic;
172 obBuffer << m_idhHeader.e_cblp;
173 obBuffer << m_idhHeader.e_cp;
174 obBuffer << m_idhHeader.e_crlc;
175 obBuffer << m_idhHeader.e_cparhdr;
176 obBuffer << m_idhHeader.e_minalloc;
177 obBuffer << m_idhHeader.e_maxalloc;
178 obBuffer << m_idhHeader.e_ss;
179 obBuffer << m_idhHeader.e_sp;
180 obBuffer << m_idhHeader.e_csum;
181 obBuffer << m_idhHeader.e_ip;
182 obBuffer << m_idhHeader.e_cs;
183 obBuffer << m_idhHeader.e_lfarlc;
184 obBuffer << m_idhHeader.e_ovno;
185
186 for (unsigned int i=0;i<sizeof(m_idhHeader.e_res)/sizeof(m_idhHeader.e_res[0]);i++)
187 {
188 obBuffer << m_idhHeader.e_res[i];
189 }
190
191 obBuffer << m_idhHeader.e_oemid;
192 obBuffer << m_idhHeader.e_oeminfo;
193
194 for (unsigned int i=0;i<sizeof(m_idhHeader.e_res2)/sizeof(m_idhHeader.e_res2[0]);i++)
195 {
196 obBuffer << m_idhHeader.e_res2[i];
197 }
198
199 obBuffer << m_idhHeader.e_lfanew;
200 }
201
202 /**
203 * Returns the size of the MZ header. This size is actually always sizeof(IMAGE_DOS_HEADER) (== 0x40)
204 * because the MZ header is a header of constant size if you disregard the dos stub. If you want to know the
205 * size of the MZ header + the size of the dos stub check #PeLib::MzHeader::getAddressOfPeHeader.
206 * @return Size of the MZ header.
207 **/
208 unsigned int MzHeader::size() const
209 {
210 return sizeof(m_idhHeader);
211 }
212
213 /**
214 * Writes the current MZ header to a file. The file does not have to exist. If it doesn't exist
215 * it will be created.
216 * @param strFilename Name of the file the header will be written to.
217 * @param dwOffset Offset the header will be written to (defaults to 0).
218 * @return A non-zero value is returned if a problem occured.
219 **/
220 int MzHeader::write(const std::string& strFilename, dword dwOffset = 0) const
221 {
222 std::fstream ofFile(strFilename.c_str(), std::ios_base::in);
223
224 if (!ofFile)
225 {
226 ofFile.clear();
227 ofFile.open(strFilename.c_str(), std::ios_base::out | std::ios_base::binary);
228 }
229 else
230 {
231 ofFile.close();
232 ofFile.open(strFilename.c_str(), std::ios_base::in | std::ios_base::out | std::ios_base::binary);
233 }
234
235 if (!ofFile)
236 {
237 return ERROR_OPENING_FILE;
238 }
239
240 ofFile.seekp(dwOffset, std::ios::beg);
241
242 std::vector<unsigned char> vBuffer;
243
244 rebuild(vBuffer);
245
246 ofFile.write(reinterpret_cast<const char*>(&vBuffer[0]), static_cast<unsigned int>(vBuffer.size()));
247
248 ofFile.close();
249
250 return NO_ERROR;
251 }
252
253 /**
254 * Returns the MZ header's e_magic value.
255 **/
256 word MzHeader::getMagicNumber() const
257 {
258 return m_idhHeader.e_magic;
259 }
260
261 /**
262 * Returns the MZ header's e_cblp value.
263 **/
264 word MzHeader::getBytesOnLastPage() const
265 {
266 return m_idhHeader.e_cblp;
267 }
268
269 /**
270 * Returns the MZ header's e_cp value.
271 **/
272 word MzHeader::getPagesInFile() const
273 {
274 return m_idhHeader.e_cp;
275 }
276
277 /**
278 * Returns the MZ header's e_crlc value.
279 **/
280 word MzHeader::getRelocations() const
281 {
282 return m_idhHeader.e_crlc;
283 }
284
285 /**
286 * Returns the MZ header's e_cparhdr value.
287 **/
288 word MzHeader::getSizeOfHeader() const
289 {
290 return m_idhHeader.e_cparhdr;
291 }
292
293 /**
294 * Returns the MZ header's e_minalloc value.
295 **/
296 word MzHeader::getMinExtraParagraphs() const
297 {
298 return m_idhHeader.e_minalloc;
299 }
300
301 /**
302 * Returns the MZ header's e_maxalloc value.
303 **/
304 word MzHeader::getMaxExtraParagraphs() const
305 {
306 return m_idhHeader.e_maxalloc;
307 }
308
309 /**
310 * Returns the MZ header's e_ss value.
311 **/
312 word MzHeader::getSsValue() const
313 {
314 return m_idhHeader.e_ss;
315 }
316
317 /**
318 * Returns the MZ header's e_sp value.
319 **/
320 word MzHeader::getSpValue() const
321 {
322 return m_idhHeader.e_sp;
323 }
324
325 /**
326 * Returns the MZ header's e_csum value.
327 **/
328 word MzHeader::getChecksum() const
329 {
330 return m_idhHeader.e_csum;
331 }
332
333 /**
334 * Returns the MZ header's e_ip value.
335 **/
336 word MzHeader::getIpValue() const
337 {
338 return m_idhHeader.e_ip;
339 }
340
341 /**
342 * Returns the MZ header's e_cs value.
343 **/
344 word MzHeader::getCsValue() const
345 {
346 return m_idhHeader.e_cs;
347 }
348
349 /**
350 * Returns the MZ header's e_lfarlc value.
351 **/
352 word MzHeader::getAddrOfRelocationTable() const
353 {
354 return m_idhHeader.e_lfarlc;
355 }
356
357 /**
358 * Returns the MZ header's e_ovno value.
359 **/
360 word MzHeader::getOverlayNumber() const
361 {
362 return m_idhHeader.e_ovno;
363 }
364
365 /**
366 * Returns the MZ header's e_oemid value.
367 **/
368 word MzHeader::getOemIdentifier() const
369 {
370 return m_idhHeader.e_oemid;
371 }
372
373 /**
374 * Returns the MZ header's e_oeminfo value.
375 **/
376 word MzHeader::getOemInformation() const
377 {
378 return m_idhHeader.e_oeminfo;
379 }
380
381 /**
382 * Returns the MZ header's e_lfanew value.
383 **/
384 dword MzHeader::getAddressOfPeHeader() const
385 {
386 return m_idhHeader.e_lfanew;
387 }
388
389 /**
390 * Returns the MZ header's e_res[uiNr] value. If the parameter uiNr is out of range
391 * you will get undefined behaviour.
392 * @param uiNr The index of the word in the e_res array (valid range: 0-3)
393 **/
394 word MzHeader::getReservedWords1(unsigned int uiNr) const
395 {
396 return m_idhHeader.e_res[uiNr];
397 }
398
399 /**
400 * Returns the MZ header's e_res2[uiNr] value. If the parameter uiNr is out of range
401 * you will get undefined behaviour.
402 * @param uiNr The index of the word in the e_res array (valid range: 0-9)
403 **/
404 word MzHeader::getReservedWords2(unsigned int uiNr) const
405 {
406 return m_idhHeader.e_res2[uiNr];
407 }
408
409 /**
410 * Sets the MZ header's e_magic value.
411 * @param wValue The new value of e_magic.
412 **/
413 void MzHeader::setMagicNumber(word wValue)
414 {
415 m_idhHeader.e_magic = wValue;
416 }
417
418 /**
419 * Sets the MZ header's e_cblp value.
420 * @param wValue The new value of e_cblp.
421 **/
422 void MzHeader::setBytesOnLastPage(word wValue)
423 {
424 m_idhHeader.e_cblp = wValue;
425 }
426
427 /**
428 * Sets the MZ header's e_cp value.
429 * @param wValue The new value of e_cp.
430 **/
431 void MzHeader::setPagesInFile(word wValue)
432 {
433 m_idhHeader.e_cp = wValue;
434 }
435
436 /**
437 * Sets the MZ header's e_crlc value.
438 * @param wValue The new value of e_crlc.
439 **/
440 void MzHeader::setRelocations(word wValue)
441 {
442 m_idhHeader.e_crlc = wValue;
443 }
444
445 /**
446 * Sets the MZ header's e_cparhdr value.
447 * @param wValue The new value of e_cparhdr.
448 **/
449 void MzHeader::setSizeOfHeader(word wValue)
450 {
451 m_idhHeader.e_cparhdr = wValue;
452 }
453
454 /**
455 * Sets the MZ header's e_minalloc value.
456 * @param wValue The new value of e_minalloc.
457 **/
458 void MzHeader::setMinExtraParagraphs(word wValue)
459 {
460 m_idhHeader.e_minalloc = wValue;
461 }
462
463 /**
464 * Sets the MZ header's e_maxalloc value.
465 * @param wValue The new value of e_maxalloc.
466 **/
467 void MzHeader::setMaxExtraParagraphs(word wValue)
468 {
469 m_idhHeader.e_maxalloc = wValue;
470 }
471
472 /**
473 * Sets the MZ header's e_ss value.
474 * @param wValue The new value of e_ss.
475 **/
476 void MzHeader::setSsValue(word wValue)
477 {
478 m_idhHeader.e_ss = wValue;
479 }
480
481 /**
482 * Sets the MZ header's e_sp value.
483 * @param wValue The new value of e_sp.
484 **/
485 void MzHeader::setSpValue(word wValue)
486 {
487 m_idhHeader.e_sp = wValue;
488 }
489
490 /**
491 * Sets the MZ header's e_csum value.
492 * @param wValue The new value of e_csum.
493 **/
494 void MzHeader::setChecksum(word wValue)
495 {
496 m_idhHeader.e_csum = wValue;
497 }
498
499 /**
500 * Sets the MZ header's e_ip value.
501 * @param wValue The new value of e_ip.
502 **/
503 void MzHeader::setIpValue(word wValue)
504 {
505 m_idhHeader.e_ip = wValue;
506 }
507
508 /**
509 * Sets the MZ header's e_cs value.
510 * @param wValue The new value of e_cs.
511 **/
512 void MzHeader::setCsValue(word wValue)
513 {
514 m_idhHeader.e_cs = wValue;
515 }
516
517 /**
518 * Sets the MZ header's e_lfarlc value.
519 * @param wValue The new value of e_lfarlc.
520 **/
521 void MzHeader::setAddrOfRelocationTable(word wValue)
522 {
523 m_idhHeader.e_lfarlc = wValue;
524 }
525
526 /**
527 * Sets the MZ header's e_ovno value.
528 * @param wValue The new value of e_ovno.
529 **/
530 void MzHeader::setOverlayNumber(word wValue)
531 {
532 m_idhHeader.e_ovno = wValue;
533 }
534
535 /**
536 * Sets the MZ header's e_oemid value.
537 * @param wValue The new value of e_oemid.
538 **/
539 void MzHeader::setOemIdentifier(word wValue)
540 {
541 m_idhHeader.e_oemid = wValue;
542 }
543
544 /**
545 * Sets the MZ header's e_oeminfo value.
546 * @param wValue The new value of e_oeminfo.
547 **/
548 void MzHeader::setOemInformation(word wValue)
549 {
550 m_idhHeader.e_oeminfo = wValue;
551 }
552
553 /**
554 * Sets the MZ header's e_lfanew value.
555 * @param lValue The new value of e_lfanew.
556 **/
557 void MzHeader::setAddressOfPeHeader(dword lValue)
558 {
559 m_idhHeader.e_lfanew = lValue;
560 }
561
562 /**
563 * Sets the MZ header's e_res[uiNr] value. If the parameter uiNr is out of range
564 * you will get undefined behaviour.
565 * @param uiNr The index of the word in the e_res array (valid range: 0-3)
566 * @param wValue The new value of e_res[nr].
567 **/
568 void MzHeader::setReservedWords1(unsigned int uiNr, word wValue)
569 {
570 m_idhHeader.e_res[uiNr] = wValue;
571 }
572
573 /**
574 * Sets the MZ header's e_res2[uiNr] value. If the parameter uiNr is out of range
575 * you will get undefined behaviour.
576 * @param uiNr The index of the word in the e_res2 array (valid range: 0-9)
577 * @param wValue The new value of e_res[nr].
578 **/
579 void MzHeader::setReservedWords2(unsigned int uiNr, word wValue)
580 {
581 m_idhHeader.e_res2[uiNr] = wValue;
582 }
583
584}