diff options
author | Maurus Cuelenaere <mcuelenaere@gmail.com> | 2008-07-11 15:50:46 +0000 |
---|---|---|
committer | Maurus Cuelenaere <mcuelenaere@gmail.com> | 2008-07-11 15:50:46 +0000 |
commit | 14c7f45cdae826f88dc539c8c38dd95caf305731 (patch) | |
tree | 832da054b7cfb2dc6fd63339af736625f31d21aa /utils/zenutils/libraries/pelib-0.9/pelib/TlsDirectory.h | |
parent | 7c84ede3781c27db73403bd6302f320c76a58c8c (diff) | |
download | rockbox-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/TlsDirectory.h')
-rwxr-xr-x | utils/zenutils/libraries/pelib-0.9/pelib/TlsDirectory.h | 304 |
1 files changed, 304 insertions, 0 deletions
diff --git a/utils/zenutils/libraries/pelib-0.9/pelib/TlsDirectory.h b/utils/zenutils/libraries/pelib-0.9/pelib/TlsDirectory.h new file mode 100755 index 0000000000..ebea929f94 --- /dev/null +++ b/utils/zenutils/libraries/pelib-0.9/pelib/TlsDirectory.h | |||
@@ -0,0 +1,304 @@ | |||
1 | /* | ||
2 | * TlsDirectory.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 | #ifndef TLSDIRECTORY_H | ||
14 | #define TLSDIRECTORY_H | ||
15 | |||
16 | namespace PeLib | ||
17 | { | ||
18 | /// Class that handles the TLS directory. | ||
19 | /** | ||
20 | * This class handles the TLS (Thread Local Storage) directory. | ||
21 | **/ | ||
22 | template<int bits> | ||
23 | class TlsDirectory | ||
24 | { | ||
25 | private: | ||
26 | PELIB_IMAGE_TLS_DIRECTORY<bits> m_tls; ///< Structure that holds all information about the directory. | ||
27 | |||
28 | void read(InputBuffer& inputbuffer); | ||
29 | |||
30 | public: | ||
31 | /// Reads a file's TLS directory. | ||
32 | int read(const std::string& strFilename, unsigned int uiOffset, unsigned int uiSize); // EXPORT | ||
33 | int read(unsigned char* buffer, unsigned int buffersize); // EXPORT | ||
34 | /// Rebuilds the TLS directory. | ||
35 | void rebuild(std::vector<byte>& vBuffer) const; // EXPORT | ||
36 | /// Returns the size of the TLS Directory. | ||
37 | unsigned int size() const; // EXPORT | ||
38 | /// Writes the TLS directory to a file. | ||
39 | int write(const std::string& strFilename, unsigned int dwOffset) const; // EXPORT | ||
40 | |||
41 | /// Returns the StartAddressOfRawData value of the TLS header. | ||
42 | dword getStartAddressOfRawData() const; // EXPORT | ||
43 | /// Returns the EndAddressOfRawData value of the TLS header. | ||
44 | dword getEndAddressOfRawData() const; // EXPORT | ||
45 | /// Returns the AddressOfIndex value of the TLS header. | ||
46 | dword getAddressOfIndex() const; // EXPORT | ||
47 | /// Returns the AddressOfCallBacks value of the TLS header. | ||
48 | dword getAddressOfCallBacks() const; // EXPORT | ||
49 | /// Returns the SizeOfZeroFill value of the TLS header. | ||
50 | dword getSizeOfZeroFill() const; // EXPORT | ||
51 | /// Returns the Characteristics value of the TLS header. | ||
52 | dword getCharacteristics() const; // EXPORT | ||
53 | |||
54 | /// Sets the StartAddressOfRawData value of the TLS header. | ||
55 | void setStartAddressOfRawData(dword dwValue); // EXPORT | ||
56 | /// Sets the EndAddressOfRawData value of the TLS header. | ||
57 | void setEndAddressOfRawData(dword dwValue); // EXPORT | ||
58 | /// Sets the AddressOfIndex value of the TLS header. | ||
59 | void setAddressOfIndex(dword dwValue); // EXPORT | ||
60 | /// Sets the AddressOfCallBacks value of the TLS header. | ||
61 | void setAddressOfCallBacks(dword dwValue); // EXPORT | ||
62 | /// Sets the SizeOfZeroFill value of the TLS header. | ||
63 | void setSizeOfZeroFill(dword dwValue); // EXPORT | ||
64 | /// Sets the Characteristics value of the TLS header. | ||
65 | void setCharacteristics(dword dwValue); // EXPORT | ||
66 | }; | ||
67 | |||
68 | template<int bits> | ||
69 | void TlsDirectory<bits>::read(InputBuffer& inputBuffer) | ||
70 | { | ||
71 | PELIB_IMAGE_TLS_DIRECTORY<bits> itdCurr; | ||
72 | |||
73 | inputBuffer >> itdCurr.StartAddressOfRawData; | ||
74 | inputBuffer >> itdCurr.EndAddressOfRawData; | ||
75 | inputBuffer >> itdCurr.AddressOfIndex; | ||
76 | inputBuffer >> itdCurr.AddressOfCallBacks; | ||
77 | inputBuffer >> itdCurr.SizeOfZeroFill; | ||
78 | inputBuffer >> itdCurr.Characteristics; | ||
79 | |||
80 | std::swap(itdCurr, m_tls); | ||
81 | } | ||
82 | |||
83 | template<int bits> | ||
84 | int TlsDirectory<bits>::read(unsigned char* buffer, unsigned int buffersize) | ||
85 | { | ||
86 | if (buffersize < PELIB_IMAGE_TLS_DIRECTORY<bits>::size()) | ||
87 | { | ||
88 | return ERROR_INVALID_FILE; | ||
89 | } | ||
90 | |||
91 | std::vector<byte> vTlsDirectory(buffer, buffer + buffersize); | ||
92 | |||
93 | InputBuffer ibBuffer(vTlsDirectory); | ||
94 | read(ibBuffer); | ||
95 | return NO_ERROR; | ||
96 | } | ||
97 | |||
98 | /** | ||
99 | * Reads a file's TLS directory. | ||
100 | * @param strFilename Name of the file. | ||
101 | * @param uiOffset File offset of the TLS directory. | ||
102 | * @param uiSize Size of the TLS directory. | ||
103 | **/ | ||
104 | template<int bits> | ||
105 | int TlsDirectory<bits>::read(const std::string& strFilename, unsigned int uiOffset, unsigned int uiSize) | ||
106 | { | ||
107 | std::ifstream ifFile(strFilename.c_str(), std::ios::binary); | ||
108 | unsigned int ulFileSize = fileSize(ifFile); | ||
109 | |||
110 | if (!ifFile) | ||
111 | { | ||
112 | return ERROR_OPENING_FILE; | ||
113 | } | ||
114 | |||
115 | if (ulFileSize < uiOffset + uiSize) | ||
116 | { | ||
117 | return ERROR_INVALID_FILE; | ||
118 | } | ||
119 | |||
120 | ifFile.seekg(uiOffset, std::ios::beg); | ||
121 | |||
122 | std::vector<byte> vTlsDirectory(uiSize); | ||
123 | ifFile.read(reinterpret_cast<char*>(&vTlsDirectory[0]), uiSize); | ||
124 | |||
125 | InputBuffer ibBuffer(vTlsDirectory); | ||
126 | read(ibBuffer); | ||
127 | return NO_ERROR; | ||
128 | } | ||
129 | |||
130 | /** | ||
131 | * Rebuilds the current TLS Directory. | ||
132 | * @param vBuffer Buffer where the TLS directory will be written to. | ||
133 | **/ | ||
134 | template<int bits> | ||
135 | void TlsDirectory<bits>::rebuild(std::vector<byte>& vBuffer) const | ||
136 | { | ||
137 | OutputBuffer obBuffer(vBuffer); | ||
138 | |||
139 | obBuffer << m_tls.StartAddressOfRawData; | ||
140 | obBuffer << m_tls.EndAddressOfRawData; | ||
141 | obBuffer << m_tls.AddressOfIndex; | ||
142 | obBuffer << m_tls.AddressOfCallBacks; | ||
143 | obBuffer << m_tls.SizeOfZeroFill; | ||
144 | obBuffer << m_tls.Characteristics; | ||
145 | } | ||
146 | |||
147 | /** | ||
148 | * Returns the size of the TLS directory. Due to the static nature of this structure the return value | ||
149 | * will always be 24. | ||
150 | * @return Size in bytes. | ||
151 | **/ | ||
152 | template<int bits> | ||
153 | unsigned int TlsDirectory<bits>::size() const | ||
154 | { | ||
155 | return PELIB_IMAGE_TLS_DIRECTORY<bits>::size(); | ||
156 | } | ||
157 | |||
158 | /** | ||
159 | * @param strFilename Name of the file. | ||
160 | * @param dwOffset File offset the TLS Directory will be written to. | ||
161 | **/ | ||
162 | template<int bits> | ||
163 | int TlsDirectory<bits>::write(const std::string& strFilename, unsigned int dwOffset) const | ||
164 | { | ||
165 | std::fstream ofFile(strFilename.c_str(), std::ios_base::in); | ||
166 | |||
167 | if (!ofFile) | ||
168 | { | ||
169 | ofFile.clear(); | ||
170 | ofFile.open(strFilename.c_str(), std::ios_base::out | std::ios_base::binary); | ||
171 | } | ||
172 | else | ||
173 | { | ||
174 | ofFile.close(); | ||
175 | ofFile.open(strFilename.c_str(), std::ios_base::in | std::ios_base::out | std::ios_base::binary); | ||
176 | } | ||
177 | |||
178 | if (!ofFile) | ||
179 | { | ||
180 | return ERROR_OPENING_FILE; | ||
181 | } | ||
182 | |||
183 | ofFile.seekp(dwOffset, std::ios::beg); | ||
184 | |||
185 | std::vector<unsigned char> vBuffer; | ||
186 | rebuild(vBuffer); | ||
187 | |||
188 | ofFile.write(reinterpret_cast<const char*>(&vBuffer[0]), vBuffer.size()); | ||
189 | |||
190 | ofFile.close(); | ||
191 | |||
192 | return NO_ERROR; | ||
193 | } | ||
194 | |||
195 | /** | ||
196 | * @return The StartAddressOfRawData value of the TLS directory. | ||
197 | **/ | ||
198 | template<int bits> | ||
199 | dword TlsDirectory<bits>::getStartAddressOfRawData() const | ||
200 | { | ||
201 | return m_tls.StartAddressOfRawData; | ||
202 | } | ||
203 | |||
204 | /** | ||
205 | * @return The EndAddressOfRawData value of the TLS directory. | ||
206 | **/ | ||
207 | template<int bits> | ||
208 | dword TlsDirectory<bits>::getEndAddressOfRawData() const | ||
209 | { | ||
210 | return m_tls.EndAddressOfRawData; | ||
211 | } | ||
212 | |||
213 | /** | ||
214 | * @return The AddressOfIndex value of the TLS directory. | ||
215 | **/ | ||
216 | template<int bits> | ||
217 | dword TlsDirectory<bits>::getAddressOfIndex() const | ||
218 | { | ||
219 | return m_tls.AddressOfIndex; | ||
220 | } | ||
221 | |||
222 | /** | ||
223 | * @return The AddressOfCallBacks value of the TLS directory. | ||
224 | **/ | ||
225 | template<int bits> | ||
226 | dword TlsDirectory<bits>::getAddressOfCallBacks() const | ||
227 | { | ||
228 | return m_tls.AddressOfCallBacks; | ||
229 | } | ||
230 | |||
231 | /** | ||
232 | * @return The SizeOfZeroFill value of the TLS directory. | ||
233 | **/ | ||
234 | template<int bits> | ||
235 | dword TlsDirectory<bits>::getSizeOfZeroFill() const | ||
236 | { | ||
237 | return m_tls.SizeOfZeroFill; | ||
238 | } | ||
239 | |||
240 | /** | ||
241 | * @return The Characteristics value of the TLS directory. | ||
242 | **/ | ||
243 | template<int bits> | ||
244 | dword TlsDirectory<bits>::getCharacteristics() const | ||
245 | { | ||
246 | return m_tls.Characteristics; | ||
247 | } | ||
248 | |||
249 | /** | ||
250 | * @param dwValue The new StartAddressOfRawData value of the TLS directory. | ||
251 | **/ | ||
252 | template<int bits> | ||
253 | void TlsDirectory<bits>::setStartAddressOfRawData(dword dwValue) | ||
254 | { | ||
255 | m_tls.StartAddressOfRawData = dwValue; | ||
256 | } | ||
257 | |||
258 | /** | ||
259 | * @param dwValue The new EndAddressOfRawData value of the TLS directory. | ||
260 | **/ | ||
261 | template<int bits> | ||
262 | void TlsDirectory<bits>::setEndAddressOfRawData(dword dwValue) | ||
263 | { | ||
264 | m_tls.EndAddressOfRawData = dwValue; | ||
265 | } | ||
266 | |||
267 | /** | ||
268 | * @param dwValue The new AddressOfIndex value of the TLS directory. | ||
269 | **/ | ||
270 | template<int bits> | ||
271 | void TlsDirectory<bits>::setAddressOfIndex(dword dwValue) | ||
272 | { | ||
273 | m_tls.AddressOfIndex = dwValue; | ||
274 | } | ||
275 | |||
276 | /** | ||
277 | * @param dwValue The new AddressOfCallBacks value of the TLS directory. | ||
278 | **/ | ||
279 | template<int bits> | ||
280 | void TlsDirectory<bits>::setAddressOfCallBacks(dword dwValue) | ||
281 | { | ||
282 | m_tls.AddressOfCallBacks = dwValue; | ||
283 | } | ||
284 | |||
285 | /** | ||
286 | * @param dwValue The new SizeOfZeroFill value of the TLS directory. | ||
287 | **/ | ||
288 | template<int bits> | ||
289 | void TlsDirectory<bits>::setSizeOfZeroFill(dword dwValue) | ||
290 | { | ||
291 | m_tls.SizeOfZeroFill = dwValue; | ||
292 | } | ||
293 | |||
294 | /** | ||
295 | * @param dwValue The new Characteristics value of the TLS directory. | ||
296 | **/ | ||
297 | template<int bits> | ||
298 | void TlsDirectory<bits>::setCharacteristics(dword dwValue) | ||
299 | { | ||
300 | m_tls.Characteristics = dwValue; | ||
301 | } | ||
302 | |||
303 | } | ||
304 | #endif | ||