summaryrefslogtreecommitdiff
path: root/utils/zenutils/libraries/pelib-0.9/pelib/DebugDirectory.cpp
diff options
context:
space:
mode:
authorMaurus Cuelenaere <mcuelenaere@gmail.com>2008-07-11 15:50:46 +0000
committerMaurus Cuelenaere <mcuelenaere@gmail.com>2008-07-11 15:50:46 +0000
commit14c7f45cdae826f88dc539c8c38dd95caf305731 (patch)
tree832da054b7cfb2dc6fd63339af736625f31d21aa /utils/zenutils/libraries/pelib-0.9/pelib/DebugDirectory.cpp
parent7c84ede3781c27db73403bd6302f320c76a58c8c (diff)
downloadrockbox-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/DebugDirectory.cpp')
-rwxr-xr-xutils/zenutils/libraries/pelib-0.9/pelib/DebugDirectory.cpp383
1 files changed, 383 insertions, 0 deletions
diff --git a/utils/zenutils/libraries/pelib-0.9/pelib/DebugDirectory.cpp b/utils/zenutils/libraries/pelib-0.9/pelib/DebugDirectory.cpp
new file mode 100755
index 0000000000..eb3d5c5600
--- /dev/null
+++ b/utils/zenutils/libraries/pelib-0.9/pelib/DebugDirectory.cpp
@@ -0,0 +1,383 @@
1/*
2* DebugDirectory.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 "DebugDirectory.h"
15
16namespace PeLib
17{
18 void DebugDirectory::clear()
19 {
20 m_vDebugInfo.clear();
21 }
22
23 std::vector<PELIB_IMG_DEBUG_DIRECTORY> DebugDirectory::read(InputBuffer& ibBuffer, unsigned int uiSize)
24 {
25 std::vector<PELIB_IMG_DEBUG_DIRECTORY> currDebugInfo;
26
27 PELIB_IMG_DEBUG_DIRECTORY iddCurr;
28
29 for (unsigned int i=0;i<uiSize/PELIB_IMAGE_DEBUG_DIRECTORY::size();i++)
30 {
31
32 ibBuffer >> iddCurr.idd.Characteristics;
33 ibBuffer >> iddCurr.idd.TimeDateStamp;
34 ibBuffer >> iddCurr.idd.MajorVersion;
35 ibBuffer >> iddCurr.idd.MinorVersion;
36 ibBuffer >> iddCurr.idd.Type;
37 ibBuffer >> iddCurr.idd.SizeOfData;
38 ibBuffer >> iddCurr.idd.AddressOfRawData;
39 ibBuffer >> iddCurr.idd.PointerToRawData;
40
41 currDebugInfo.push_back(iddCurr);
42 }
43
44 return currDebugInfo;
45 }
46
47 int DebugDirectory::read(unsigned char* buffer, unsigned int buffersize)
48 {
49 // XXX: Note, debug data is not read at all. This might or might not change
50 // in the future.
51
52 std::vector<byte> vDebugDirectory(buffer, buffer + buffersize);
53
54 InputBuffer ibBuffer(vDebugDirectory);
55
56 std::vector<PELIB_IMG_DEBUG_DIRECTORY> currDebugInfo = read(ibBuffer, buffersize);
57
58 std::swap(currDebugInfo, m_vDebugInfo);
59
60 return NO_ERROR;
61 }
62
63 /**
64 * @param strFilename Name of the file which will be read.
65 * @param uiOffset File offset of the Debug directory.
66 * @param uiSize Size of the Debug directory.
67 **/
68 int DebugDirectory::read(const std::string& strFilename, unsigned int uiOffset, unsigned int uiSize)
69 {
70 std::ifstream ifFile(strFilename.c_str(), std::ios::binary);
71 unsigned int ulFileSize = fileSize(ifFile);
72
73 if (!ifFile)
74 {
75 return ERROR_OPENING_FILE;
76 }
77
78 if (ulFileSize < uiOffset + uiSize)
79 {
80 return ERROR_INVALID_FILE;
81 }
82
83 ifFile.seekg(uiOffset, std::ios::beg);
84
85 std::vector<byte> vDebugDirectory(uiSize);
86 ifFile.read(reinterpret_cast<char*>(&vDebugDirectory[0]), uiSize);
87
88 InputBuffer ibBuffer(vDebugDirectory);
89
90 std::vector<PELIB_IMG_DEBUG_DIRECTORY> currDebugInfo = read(ibBuffer, uiSize);
91
92 for (unsigned int i=0;i<currDebugInfo.size();i++)
93 {
94 ifFile.seekg(currDebugInfo[i].idd.PointerToRawData, std::ios::beg);
95 currDebugInfo[i].data.resize(currDebugInfo[i].idd.SizeOfData);
96 ifFile.read(reinterpret_cast<char*>(&currDebugInfo[i].data[0]), currDebugInfo[i].idd.SizeOfData);
97 if (!ifFile) return ERROR_INVALID_FILE;
98 }
99
100 std::swap(currDebugInfo, m_vDebugInfo);
101
102 return NO_ERROR;
103 }
104
105 /**
106 * Rebuilds the current debug directory.
107 * @param vBuffer Buffer where the rebuilt directory is stored.
108 **/
109 void DebugDirectory::rebuild(std::vector<byte>& vBuffer) const
110 {
111 OutputBuffer obBuffer(vBuffer);
112
113 for (unsigned int i=0;i<m_vDebugInfo.size();i++)
114 {
115 obBuffer << m_vDebugInfo[i].idd.Characteristics;
116 obBuffer << m_vDebugInfo[i].idd.TimeDateStamp;
117 obBuffer << m_vDebugInfo[i].idd.MajorVersion;
118 obBuffer << m_vDebugInfo[i].idd.MinorVersion;
119 obBuffer << m_vDebugInfo[i].idd.Type;
120 obBuffer << m_vDebugInfo[i].idd.SizeOfData;
121 obBuffer << m_vDebugInfo[i].idd.AddressOfRawData;
122 obBuffer << m_vDebugInfo[i].idd.PointerToRawData;
123 }
124 }
125
126 /**
127 * @return Size of the debug directory.
128 **/
129 unsigned int DebugDirectory::size() const
130 {
131 return static_cast<unsigned int>(m_vDebugInfo.size()) * PELIB_IMAGE_DEBUG_DIRECTORY::size();
132 }
133
134 /**
135 * @param strFilename Name of the file which will be written.
136 * @param uiOffset File offset where the debug directory will be stored.
137 **/
138 int DebugDirectory::write(const std::string& strFilename, unsigned int uiOffset) const
139 {
140 std::fstream ofFile(strFilename.c_str(), std::ios_base::in);
141
142 if (!ofFile)
143 {
144 ofFile.clear();
145 ofFile.open(strFilename.c_str(), std::ios_base::out | std::ios_base::binary);
146 }
147 else
148 {
149 ofFile.close();
150 ofFile.open(strFilename.c_str(), std::ios_base::in | std::ios_base::out | std::ios_base::binary);
151 }
152
153 if (!ofFile)
154 {
155 return ERROR_OPENING_FILE;
156 }
157
158 ofFile.seekp(uiOffset, std::ios::beg);
159
160 std::vector<unsigned char> vBuffer;
161 rebuild(vBuffer);
162
163 ofFile.write(reinterpret_cast<const char*>(&vBuffer[0]), static_cast<unsigned int>(vBuffer.size()));
164
165 ofFile.close();
166
167 return NO_ERROR;
168 }
169
170 /**
171 * @return Number of debug structures in the current Debug directory.
172 **/
173 unsigned int DebugDirectory::calcNumberOfEntries() const
174 {
175 return static_cast<unsigned int>(m_vDebugInfo.size());
176 }
177
178 /**
179 * Adds a new debug structure to the debug directory. The initial values of all members of the structure
180 * are undefined.
181 **/
182 void DebugDirectory::addEntry()
183 {
184 PELIB_IMG_DEBUG_DIRECTORY p;
185 m_vDebugInfo.push_back(p);
186 }
187
188 /**
189 * Removes a debug structure from the current debug directory. If an invalid structure is specified
190 * by the parameter uiIndex the result will be undefined behaviour.
191 * @param uiIndex Identifies the debug structure.
192 **/
193 void DebugDirectory::removeEntry(unsigned int uiIndex)
194 {
195 m_vDebugInfo.erase(m_vDebugInfo.begin() + uiIndex);
196 }
197
198 /**
199 * Returns the Characteristics value of a debug structure. If an invalid structure is specified
200 * by the parameter uiIndex the result will be undefined behaviour.
201 * @param uiIndex Identifies the debug structure.
202 * @return Characteristics value of the debug structure.
203 **/
204 dword DebugDirectory::getCharacteristics(unsigned int uiIndex) const
205 {
206 return m_vDebugInfo[uiIndex].idd.Characteristics;
207 }
208
209 /**
210 * Returns the TimeDateStamp value of a debug structure. If an invalid structure is specified
211 * by the parameter uiIndex the result will be undefined behaviour.
212 * @param uiIndex Identifies the debug structure.
213 * @return TimeDateStamp value of the debug structure.
214 **/
215 dword DebugDirectory::getTimeDateStamp(unsigned int uiIndex) const
216 {
217 return m_vDebugInfo[uiIndex].idd.TimeDateStamp;
218 }
219
220 /**
221 * Returns the MajorVersion value of a debug structure. If an invalid structure is specified
222 * by the parameter uiIndex the result will be undefined behaviour.
223 * @param uiIndex Identifies the debug structure.
224 * @return MajorVersion value of the debug structure.
225 **/
226 word DebugDirectory::getMajorVersion(unsigned int uiIndex) const
227 {
228 return m_vDebugInfo[uiIndex].idd.MajorVersion;
229 }
230
231 /**
232 * Returns the MinorVersion value of a debug structure. If an invalid structure is specified
233 * by the parameter uiIndex the result will be undefined behaviour.
234 * @param uiIndex Identifies the debug structure.
235 * @return MinorVersion value of the debug structure.
236 **/
237 word DebugDirectory::getMinorVersion(unsigned int uiIndex) const
238 {
239 return m_vDebugInfo[uiIndex].idd.MinorVersion;
240 }
241
242 /**
243 * Returns the Type value of a debug structure. If an invalid structure is specified
244 * by the parameter uiIndex the result will be undefined behaviour.
245 * @param uiIndex Identifies the debug structure.
246 * @return Type value of the debug structure.
247 **/
248 dword DebugDirectory::getType(unsigned int uiIndex) const
249 {
250 return m_vDebugInfo[uiIndex].idd.Type;
251 }
252
253 /**
254 * Returns the SizeOfData value of a debug structure. If an invalid structure is specified
255 * by the parameter uiIndex the result will be undefined behaviour.
256 * @param uiIndex Identifies the debug structure.
257 * @return SizeOfData value of the debug structure.
258 **/
259 dword DebugDirectory::getSizeOfData(unsigned int uiIndex) const
260 {
261 return m_vDebugInfo[uiIndex].idd.SizeOfData;
262 }
263
264 /**
265 * Returns the AddressOfRawData value of a debug structure. If an invalid structure is specified
266 * by the parameter uiIndex the result will be undefined behaviour.
267 * @param uiIndex Identifies the debug structure.
268 * @return AddressOfRawData value of the debug structure.
269 **/
270 dword DebugDirectory::getAddressOfRawData(unsigned int uiIndex) const
271 {
272 return m_vDebugInfo[uiIndex].idd.AddressOfRawData;
273 }
274
275 /**
276 * Returns the PointerToRawData value of a debug structure. If an invalid structure is specified
277 * by the parameter uiIndex the result will be undefined behaviour.
278 * @param uiIndex Identifies the debug structure.
279 * @return PointerToRawData value of the debug structure.
280 **/
281 dword DebugDirectory::getPointerToRawData(unsigned int uiIndex) const
282 {
283 return m_vDebugInfo[uiIndex].idd.PointerToRawData;
284 }
285
286 std::vector<byte> DebugDirectory::getData(unsigned int index) const
287 {
288 return m_vDebugInfo[index].data;
289 }
290
291 /**
292 * Changes the Characteristics value of a debug structure. If an invalid structure is specified
293 * by the parameter uiIndex the result will be undefined behaviour.
294 * @param uiIndex Identifies the debug structure.
295 * @param dwValue New value of the Characteristics value of the debug structure.
296 **/
297 void DebugDirectory::setCharacteristics(unsigned int uiIndex, dword dwValue)
298 {
299 m_vDebugInfo[uiIndex].idd.Characteristics = dwValue;
300 }
301
302 /**
303 * Changes the TimeDateStamp value of a debug structure. If an invalid structure is specified
304 * by the parameter uiIndex the result will be undefined behaviour.
305 * @param uiIndex Identifies the debug structure.
306 * @param dwValue New value of the TimeDateStamp value of the debug structure.
307 **/
308 void DebugDirectory::setTimeDateStamp(unsigned int uiIndex, dword dwValue)
309 {
310 m_vDebugInfo[uiIndex].idd.TimeDateStamp = dwValue;
311 }
312
313 /**
314 * Changes the MajorVersion value of a debug structure. If an invalid structure is specified
315 * by the parameter uiIndex the result will be undefined behaviour.
316 * @param uiIndex Identifies the debug structure.
317 * @param wValue New value of the MajorVersion value of the debug structure.
318 **/
319 void DebugDirectory::setMajorVersion(unsigned int uiIndex, word wValue)
320 {
321 m_vDebugInfo[uiIndex].idd.MajorVersion = wValue;
322 }
323
324 /**
325 * Changes the MinorVersion value of a debug structure. If an invalid structure is specified
326 * by the parameter uiIndex the result will be undefined behaviour.
327 * @param uiIndex Identifies the debug structure.
328 * @param wValue New value of the MinorVersion value of the debug structure.
329 **/
330 void DebugDirectory::setMinorVersion(unsigned int uiIndex, word wValue)
331 {
332 m_vDebugInfo[uiIndex].idd.MinorVersion = wValue;
333 }
334
335 /**
336 * Changes the Type value of a debug structure. If an invalid structure is specified
337 * by the parameter uiIndex the result will be undefined behaviour.
338 * @param uiIndex Identifies the debug structure.
339 * @param dwValue New value of the Type value of the debug structure.
340 **/
341 void DebugDirectory::setType(unsigned int uiIndex, dword dwValue)
342 {
343 m_vDebugInfo[uiIndex].idd.Type = dwValue;
344 }
345
346 /**
347 * Changes the SizeOfData value of a debug structure. If an invalid structure is specified
348 * by the parameter uiIndex the result will be undefined behaviour.
349 * @param uiIndex Identifies the debug structure.
350 * @param dwValue New value of the SizeOfData value of the debug structure.
351 **/
352 void DebugDirectory::setSizeOfData(unsigned int uiIndex, dword dwValue)
353 {
354 m_vDebugInfo[uiIndex].idd.SizeOfData = dwValue;
355 }
356
357 /**
358 * Changes the AddressOfRawData value of a debug structure. If an invalid structure is specified
359 * by the parameter uiIndex the result will be undefined behaviour.
360 * @param uiIndex Identifies the debug structure.
361 * @param dwValue New value of the AddressOfRawData value of the debug structure.
362 **/
363 void DebugDirectory::setAddressOfRawData(unsigned int uiIndex, dword dwValue)
364 {
365 m_vDebugInfo[uiIndex].idd.AddressOfRawData = dwValue;
366 }
367
368 /**
369 * Changes the PointerToRawData value of a debug structure. If an invalid structure is specified
370 * by the parameter uiIndex the result will be undefined behaviour.
371 * @param uiIndex Identifies the debug structure.
372 * @param dwValue New value of the PointerToRawData value of the debug structure.
373 **/
374 void DebugDirectory::setPointerToRawData(unsigned int uiIndex, dword dwValue)
375 {
376 m_vDebugInfo[uiIndex].idd.PointerToRawData = dwValue;
377 }
378
379 void DebugDirectory::setData(unsigned int index, const std::vector<byte>& data)
380 {
381 m_vDebugInfo[index].data = data;
382 }
383}