summaryrefslogtreecommitdiff
path: root/utils/zenutils/libraries/pelib-0.9/pelib/RelocationsDirectory.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'utils/zenutils/libraries/pelib-0.9/pelib/RelocationsDirectory.cpp')
-rwxr-xr-xutils/zenutils/libraries/pelib-0.9/pelib/RelocationsDirectory.cpp211
1 files changed, 211 insertions, 0 deletions
diff --git a/utils/zenutils/libraries/pelib-0.9/pelib/RelocationsDirectory.cpp b/utils/zenutils/libraries/pelib-0.9/pelib/RelocationsDirectory.cpp
new file mode 100755
index 0000000000..d8b5035abf
--- /dev/null
+++ b/utils/zenutils/libraries/pelib-0.9/pelib/RelocationsDirectory.cpp
@@ -0,0 +1,211 @@
1/*
2* Relocations.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 "RelocationsDirectory.h"
15
16namespace PeLib
17{
18 void RelocationsDirectory::setRelocationData(unsigned int ulRelocation, unsigned int ulDataNumber, word wData)
19 {
20 m_vRelocations[ulRelocation].vRelocData[ulDataNumber] = wData;
21 }
22
23 void RelocationsDirectory::read(InputBuffer& inputbuffer, unsigned int uiSize)
24 {
25 IMG_BASE_RELOC ibrCurr;
26
27 std::vector<IMG_BASE_RELOC> vCurrReloc;
28
29 do
30 {
31 inputbuffer >> ibrCurr.ibrRelocation.VirtualAddress;
32 inputbuffer >> ibrCurr.ibrRelocation.SizeOfBlock;
33
34 ibrCurr.vRelocData.clear();
35
36 // That's not how to check if there are relocations, some DLLs start at VA 0.
37 // if (!ibrCurr.ibrRelocation.VirtualAddress) break;
38
39 for (unsigned int i=0;i<(ibrCurr.ibrRelocation.SizeOfBlock - PELIB_IMAGE_SIZEOF_BASE_RELOCATION) / sizeof(word);i++)
40 {
41 word wData;
42 inputbuffer >> wData;
43 ibrCurr.vRelocData.push_back(wData);
44 }
45
46 vCurrReloc.push_back(ibrCurr);
47 } while (ibrCurr.ibrRelocation.VirtualAddress && inputbuffer.get() < uiSize);
48
49 std::swap(vCurrReloc, m_vRelocations);
50 }
51
52 // TODO: Return value is wrong if buffer was too small.
53 int RelocationsDirectory::read(const unsigned char* buffer, unsigned int buffersize)
54 {
55 std::vector<unsigned char> vRelocDirectory(buffer, buffer + buffersize);
56
57 InputBuffer ibBuffer(vRelocDirectory);
58 read(ibBuffer, buffersize);
59
60 return NO_ERROR;
61 }
62
63 int RelocationsDirectory::read(const std::string& strFilename, unsigned int uiOffset, unsigned int uiSize)
64 {
65 std::ifstream ifFile(strFilename.c_str(), std::ios::binary);
66 unsigned int ulFileSize = fileSize(ifFile);
67
68 if (!ifFile)
69 {
70 return ERROR_OPENING_FILE;
71 }
72
73 if (ulFileSize < uiOffset + uiSize)
74 {
75 return ERROR_INVALID_FILE;
76 }
77
78 ifFile.seekg(uiOffset, std::ios::beg);
79
80 std::vector<unsigned char> vRelocDirectory(uiSize);
81 ifFile.read(reinterpret_cast<char*>(&vRelocDirectory[0]), uiSize);
82
83 InputBuffer ibBuffer(vRelocDirectory);
84 read(ibBuffer, uiSize);
85
86 return NO_ERROR;
87 }
88
89 unsigned int RelocationsDirectory::size() const
90 {
91 unsigned int size = static_cast<unsigned int>(m_vRelocations.size()) * PELIB_IMAGE_BASE_RELOCATION::size();
92
93 for (unsigned int i=0;i<m_vRelocations.size();i++)
94 {
95 size += static_cast<unsigned int>(m_vRelocations[i].vRelocData.size()) * sizeof(word);
96 }
97
98 return size;
99 }
100
101 void RelocationsDirectory::rebuild(std::vector<byte>& vBuffer) const
102 {
103 OutputBuffer obBuffer(vBuffer);
104
105 for (unsigned int i=0;i<m_vRelocations.size();i++)
106 {
107 obBuffer << m_vRelocations[i].ibrRelocation.VirtualAddress;
108 obBuffer << m_vRelocations[i].ibrRelocation.SizeOfBlock;
109
110 for (unsigned int j=0;j<m_vRelocations[i].vRelocData.size();j++)
111 {
112 obBuffer << m_vRelocations[i].vRelocData[j];
113 }
114 }
115 }
116
117 unsigned int RelocationsDirectory::calcNumberOfRelocations() const
118 {
119 return static_cast<unsigned int>(m_vRelocations.size());
120 }
121
122 dword RelocationsDirectory::getVirtualAddress(unsigned int ulRelocation) const
123 {
124 return m_vRelocations[ulRelocation].ibrRelocation.VirtualAddress;
125 }
126
127 dword RelocationsDirectory::getSizeOfBlock(unsigned int ulRelocation) const
128 {
129 return m_vRelocations[ulRelocation].ibrRelocation.SizeOfBlock;
130 }
131
132 unsigned int RelocationsDirectory::calcNumberOfRelocationData(unsigned int ulRelocation) const
133 {
134 return static_cast<unsigned int>(m_vRelocations[ulRelocation].vRelocData.size());
135 }
136
137 word RelocationsDirectory::getRelocationData(unsigned int ulRelocation, unsigned int ulDataNumber) const
138 {
139 return m_vRelocations[ulRelocation].vRelocData[ulDataNumber];
140 }
141
142 void RelocationsDirectory::setVirtualAddress(unsigned int ulRelocation, dword dwValue)
143 {
144 m_vRelocations[ulRelocation].ibrRelocation.VirtualAddress = dwValue;
145 }
146
147 void RelocationsDirectory::setSizeOfBlock(unsigned int ulRelocation, dword dwValue)
148 {
149 m_vRelocations[ulRelocation].ibrRelocation.SizeOfBlock = dwValue;
150 }
151
152 void RelocationsDirectory::addRelocation()
153 {
154 IMG_BASE_RELOC newrelocation;
155 m_vRelocations.push_back(newrelocation);
156 }
157
158 void RelocationsDirectory::addRelocationData(unsigned int ulRelocation, word wValue)
159 {
160 m_vRelocations[ulRelocation].vRelocData.push_back(wValue);
161 }
162
163/* void RelocationsDirectory::removeRelocationData(unsigned int ulRelocation, word wValue)
164 {
165 // If you get an error with Borland C++ here you have two options: Upgrade your compiler
166 // or use the commented line instead of the line below.
167 m_vRelocations[ulRelocation].vRelocData.erase(std::remove(m_vRelocations[ulRelocation].vRelocData.begin(), m_vRelocations[ulRelocation].vRelocData.end(), wValue), m_vRelocations[ulRelocation].vRelocData.end());
168 }
169*/
170 void RelocationsDirectory::removeRelocation(unsigned int index)
171 {
172 m_vRelocations.erase(m_vRelocations.begin() + index);
173 }
174
175 void RelocationsDirectory::removeRelocationData(unsigned int relocindex, unsigned int dataindex)
176 {
177 m_vRelocations[relocindex].vRelocData.erase(m_vRelocations[relocindex].vRelocData.begin() + dataindex);
178 }
179
180 int RelocationsDirectory::write(const std::string& strFilename, unsigned int uiOffset) const
181 {
182 std::fstream ofFile(strFilename.c_str(), std::ios_base::in);
183
184 if (!ofFile)
185 {
186 ofFile.clear();
187 ofFile.open(strFilename.c_str(), std::ios_base::out | std::ios_base::binary);
188 }
189 else
190 {
191 ofFile.close();
192 ofFile.open(strFilename.c_str(), std::ios_base::in | std::ios_base::out | std::ios_base::binary);
193 }
194
195 if (!ofFile)
196 {
197 return ERROR_OPENING_FILE;
198 }
199
200 ofFile.seekp(uiOffset, std::ios::beg);
201
202 std::vector<unsigned char> vBuffer;
203 rebuild(vBuffer);
204
205 ofFile.write(reinterpret_cast<const char*>(&vBuffer[0]), static_cast<std::streamsize>(vBuffer.size()));
206
207 ofFile.close();
208
209 return NO_ERROR;
210 }
211}