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