diff options
Diffstat (limited to 'utils/zenutils/libraries/pelib-0.9/pelib/IatDirectory.cpp')
-rwxr-xr-x | utils/zenutils/libraries/pelib-0.9/pelib/IatDirectory.cpp | 179 |
1 files changed, 179 insertions, 0 deletions
diff --git a/utils/zenutils/libraries/pelib-0.9/pelib/IatDirectory.cpp b/utils/zenutils/libraries/pelib-0.9/pelib/IatDirectory.cpp new file mode 100755 index 0000000000..36482fcbeb --- /dev/null +++ b/utils/zenutils/libraries/pelib-0.9/pelib/IatDirectory.cpp | |||
@@ -0,0 +1,179 @@ | |||
1 | /* | ||
2 | * IatDirectory.h - 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 "IatDirectory.h" | ||
14 | |||
15 | namespace PeLib | ||
16 | { | ||
17 | int IatDirectory::read(InputBuffer& inputBuffer, unsigned int size) | ||
18 | { | ||
19 | dword dwAddr; | ||
20 | |||
21 | std::vector<dword> vIat; | ||
22 | |||
23 | for (unsigned int i=0;i<size/sizeof(dword);i++) | ||
24 | { | ||
25 | inputBuffer >> dwAddr; | ||
26 | vIat.push_back(dwAddr); | ||
27 | } | ||
28 | |||
29 | std::swap(vIat, m_vIat); | ||
30 | |||
31 | return NO_ERROR; | ||
32 | } | ||
33 | |||
34 | /** | ||
35 | * Reads the Import Address table from a file. | ||
36 | * @param strFilename Name of the file. | ||
37 | * @param dwOffset File offset of the IAT (see #PeFile::PeHeader::getIDIatRVA). | ||
38 | * @param dwSize Size of the IAT (see #PeFile::PeHeader::getIDIatSize). | ||
39 | **/ | ||
40 | int IatDirectory::read(const std::string& strFilename, unsigned int dwOffset, unsigned int dwSize) | ||
41 | { | ||
42 | std::ifstream ifFile(strFilename.c_str(), std::ios::binary); | ||
43 | |||
44 | if (!ifFile) | ||
45 | { | ||
46 | return ERROR_OPENING_FILE; | ||
47 | } | ||
48 | |||
49 | if (fileSize(ifFile) < dwOffset + dwSize) | ||
50 | { | ||
51 | return ERROR_INVALID_FILE; | ||
52 | } | ||
53 | |||
54 | ifFile.seekg(dwOffset, std::ios::beg); | ||
55 | |||
56 | std::vector<byte> vBuffer(dwSize); | ||
57 | ifFile.read(reinterpret_cast<char*>(&vBuffer[0]), dwSize); | ||
58 | |||
59 | InputBuffer inpBuffer(vBuffer); | ||
60 | return read(inpBuffer, dwSize); | ||
61 | } | ||
62 | |||
63 | int IatDirectory::read(unsigned char* buffer, unsigned int buffersize) | ||
64 | { | ||
65 | std::vector<byte> vBuffer(buffer, buffer + buffersize); | ||
66 | InputBuffer inpBuffer(vBuffer); | ||
67 | return read(inpBuffer, buffersize); | ||
68 | } | ||
69 | |||
70 | /** | ||
71 | * Returns the number of fields in the IAT. This is equivalent to the number of | ||
72 | * imported functions. | ||
73 | * @return Number of fields in the IAT. | ||
74 | **/ | ||
75 | unsigned int IatDirectory::calcNumberOfAddresses() const | ||
76 | { | ||
77 | return static_cast<unsigned int>(m_vIat.size()); | ||
78 | } | ||
79 | |||
80 | /** | ||
81 | * Returns the dwValue of a field in the IAT. | ||
82 | * @param dwAddrnr Number identifying the field. | ||
83 | * @return dwValue of the field. | ||
84 | **/ | ||
85 | dword IatDirectory::getAddress(unsigned int index) const | ||
86 | { | ||
87 | return m_vIat[index]; | ||
88 | } | ||
89 | |||
90 | /** | ||
91 | * Updates the dwValue of a field in the IAT. | ||
92 | * @param dwAddrnr Number identifying the field. | ||
93 | * @param dwValue New dwValue of the field. | ||
94 | **/ | ||
95 | void IatDirectory::setAddress(dword dwAddrnr, dword dwValue) | ||
96 | { | ||
97 | m_vIat[dwAddrnr] = dwValue; | ||
98 | } | ||
99 | |||
100 | /** | ||
101 | * Adds another field to the IAT. | ||
102 | * @param dwValue dwValue of the new field. | ||
103 | **/ | ||
104 | void IatDirectory::addAddress(dword dwValue) | ||
105 | { | ||
106 | m_vIat.push_back(dwValue); | ||
107 | } | ||
108 | |||
109 | /** | ||
110 | * Removes an address from the IAT. | ||
111 | * @param dwAddrnr Number identifying the field. | ||
112 | **/ | ||
113 | void IatDirectory::removeAddress(unsigned int index) | ||
114 | { | ||
115 | std::vector<dword>::iterator pos = m_vIat.begin() + index; | ||
116 | m_vIat.erase(pos); | ||
117 | } | ||
118 | |||
119 | /** | ||
120 | * Delete all entries from the IAT. | ||
121 | **/ | ||
122 | void IatDirectory::clear() | ||
123 | { | ||
124 | m_vIat.clear(); | ||
125 | } | ||
126 | |||
127 | /** | ||
128 | * Rebuilds the complete Import Address Table. | ||
129 | * @param vBuffer Buffer where the rebuilt IAT will be stored. | ||
130 | **/ | ||
131 | void IatDirectory::rebuild(std::vector<byte>& vBuffer) const | ||
132 | { | ||
133 | vBuffer.reserve(size()); | ||
134 | OutputBuffer obBuffer(vBuffer); | ||
135 | |||
136 | for (unsigned int i=0;i<m_vIat.size();i++) | ||
137 | { | ||
138 | obBuffer << m_vIat[i]; | ||
139 | } | ||
140 | } | ||
141 | |||
142 | unsigned int IatDirectory::size() const | ||
143 | { | ||
144 | return static_cast<unsigned int>(m_vIat.size())* sizeof(dword); | ||
145 | } | ||
146 | |||
147 | /// Writes the current IAT to a file. | ||
148 | int IatDirectory::write(const std::string& strFilename, unsigned int uiOffset) const | ||
149 | { | ||
150 | std::fstream ofFile(strFilename.c_str(), std::ios_base::in); | ||
151 | |||
152 | if (!ofFile) | ||
153 | { | ||
154 | ofFile.clear(); | ||
155 | ofFile.open(strFilename.c_str(), std::ios_base::out | std::ios_base::binary); | ||
156 | } | ||
157 | else | ||
158 | { | ||
159 | ofFile.close(); | ||
160 | ofFile.open(strFilename.c_str(), std::ios_base::in | std::ios_base::out | std::ios_base::binary); | ||
161 | } | ||
162 | |||
163 | if (!ofFile) | ||
164 | { | ||
165 | return ERROR_OPENING_FILE; | ||
166 | } | ||
167 | |||
168 | ofFile.seekp(uiOffset, std::ios::beg); | ||
169 | |||
170 | std::vector<unsigned char> vBuffer; | ||
171 | rebuild(vBuffer); | ||
172 | |||
173 | ofFile.write(reinterpret_cast<const char*>(&vBuffer[0]), static_cast<unsigned int>(vBuffer.size())); | ||
174 | |||
175 | ofFile.close(); | ||
176 | |||
177 | return NO_ERROR; | ||
178 | } | ||
179 | } | ||