1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
|
/*
* MzHeader.h - Part of the PeLib library.
*
* Copyright (c) 2004 - 2005 Sebastian Porst (webmaster@the-interweb.com)
* All rights reserved.
*
* This software is licensed under the zlib/libpng License.
* For more details see http://www.opensource.org/licenses/zlib-license.php
* or the license information file (license.htm) in the root directory
* of PeLib.
*/
#ifndef MZHEADER_H
#define MZHEADER_H
#include "PeLibInc.h"
namespace PeLib
{
/// Class that handles the MZ header of files.
/**
* This class can read and modify MZ headers. It provides set- and get functions to access
* all individual members of a MZ header. Furthermore it's possible to validate and rebuild
* MZ headers.
**/
class MzHeader
{
private:
PELIB_IMAGE_DOS_HEADER m_idhHeader; ///< Stores all MZ header information.
/// Reads data from an InputBuffer into a MZ header struct.
void read(InputBuffer& ibBuffer);
/// Offset of the MZ header in the original file.
unsigned int originalOffset;
public:
enum Field {e_magic, e_cblp, e_cp, e_crlc, e_cparhdr, e_minalloc, e_maxalloc,
e_ss, e_sp, e_csum, e_ip, e_cs, e_lfarlc, e_ovno, e_res, e_oemid,
e_oeminfo, e_res2, e_lfanew};
/// Checks if the current MZ header is valid.
bool isValid() const; // EXPORT
bool isValid(Field field) const; // EXPORT _field
/// Corrects the current MZ header.
void makeValid(); // EXPORT
void makeValid(Field field); // EXPORT _field
/// Reads the MZ header of a file.
int read(const std::string& strFilename); // EXPORT
/// Reads the MZ header from a memory location.
int read(unsigned char* pcBuffer, unsigned int uiSize, unsigned int originalOffs = 0); // EXPORT _fromMemory
/// Rebuild the MZ header.
void rebuild(std::vector<byte>& vBuffer) const; // EXPORT
/// Returns the size of the current MZ header.
unsigned int size() const; // EXPORT
/// Writes the current MZ header to offset 0 of a file.
int write(const std::string& strFilename, dword dwOffset) const; // EXPORT
/// Gets the e_magic value of the MZ header.
word getMagicNumber() const; // EXPORT
/// Gets the e_cblp value of the MZ header.
word getBytesOnLastPage() const; // EXPORT
/// Gets the e_cp value of the MZ header.
word getPagesInFile() const; // EXPORT
/// Gets the e_crlc value of the MZ header.
word getRelocations() const; // EXPORT
/// Gets the e_cparhdr value of the MZ header.
word getSizeOfHeader() const; // EXPORT
/// Gets the e_minalloc value of the MZ header.
word getMinExtraParagraphs() const; // EXPORT
/// Gets the e_maxalloc value of the MZ header.
word getMaxExtraParagraphs() const; // EXPORT
/// Gets the e_ss value of the MZ header.
word getSsValue() const; // EXPORT
/// Gets the e_sp value of the MZ header.
word getSpValue() const; // EXPORT
/// Gets the e_csum value of the MZ header.
word getChecksum() const; // EXPORT
/// Gets the e_ip value of the MZ header.
word getIpValue() const; // EXPORT
/// Gets the e_cs value of the MZ header.
word getCsValue() const; // EXPORT
/// Gets the e_lfarlc value of the MZ header.
word getAddrOfRelocationTable() const; // EXPORT
/// Gets the e_ovnovalue of the MZ header.
word getOverlayNumber() const; // EXPORT
/// Gets the e_oemid value of the MZ header.
word getOemIdentifier() const; // EXPORT
/// Gets the e_oeminfo value of the MZ header.
word getOemInformation() const; // EXPORT
/// Gets the e_lfanew value of the MZ header.
dword getAddressOfPeHeader() const; // EXPORT
/// Gets the e_res of the MZ header.
word getReservedWords1(unsigned int uiNr) const; // EXPORT
/// Gets the e_res2 of the MZ header.
word getReservedWords2(unsigned int uiNr) const; // EXPORT
/// Sets the e_magic value of the MZ header.
void setMagicNumber(word wValue); // EXPORT
/// Sets the e_cblp value of the MZ header.
void setBytesOnLastPage(word wValue); // EXPORT
/// Sets the e_cp value of the MZ header.
void setPagesInFile(word wValue); // EXPORT
/// Sets the e_crlc value of the MZ header.
void setRelocations(word wValue); // EXPORT
/// Sets the e_cparhdr value of the MZ header.
void setSizeOfHeader(word wValue); // EXPORT
/// Sets the e_minalloc value of the MZ header.
void setMinExtraParagraphs(word wValue); // EXPORT
/// Sets the e_maxalloc value of the MZ header.
void setMaxExtraParagraphs(word wValue); // EXPORT
/// Sets the e_ss value of the MZ header.
void setSsValue(word wValue); // EXPORT
/// Sets the e_sp value of the MZ header.
void setSpValue(word wValue); // EXPORT
/// Sets the e_csum value of the MZ header.
void setChecksum(word wValue); // EXPORT
/// Sets the e_ip value of the MZ header.
void setIpValue(word wValue); // EXPORT
/// Sets the e_cs value of the MZ header.
void setCsValue(word wValue); // EXPORT
/// Sets the e_lfarlc value of the MZ header.
void setAddrOfRelocationTable(word wValue); // EXPORT
/// Sets the e_ovno value of the MZ header.
void setOverlayNumber(word wValue); // EXPORT
/// Sets the e_oemid value of the MZ header.
void setOemIdentifier(word wValue); // EXPORT
/// Sets the e_oeminfo value of the MZ header.
void setOemInformation(word wValue); // EXPORT
/// Sets the e_lfanew value of the MZ header.
void setAddressOfPeHeader(dword dwValue); // EXPORT
/// Sets the e_res value of the MZ header.
void setReservedWords1(unsigned int uiNr, word wValue); // EXPORT
/// Sets the e_res2 value of the MZ header.
void setReservedWords2(unsigned int uiNr, word wValue); // EXPORT
};
}
#endif
|