PageRenderTime 48ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/contrib/PeLib/source/DebugDirectory.cpp

https://bitbucket.org/deeso/qfscanner
C++ | 383 lines | 190 code | 56 blank | 137 comment | 8 complexity | 1d3ebcb9715b11d41b7c57d58de77b19 MD5 | raw file
  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. #include "PeLibInc.h"
  13. #include "DebugDirectory.h"
  14. namespace PeLib
  15. {
  16. void DebugDirectory::clear()
  17. {
  18. m_vDebugInfo.clear();
  19. }
  20. std::vector<PELIB_IMG_DEBUG_DIRECTORY> DebugDirectory::read(InputBuffer& ibBuffer, unsigned int uiSize)
  21. {
  22. std::vector<PELIB_IMG_DEBUG_DIRECTORY> currDebugInfo;
  23. PELIB_IMG_DEBUG_DIRECTORY iddCurr;
  24. for (unsigned int i=0;i<uiSize/PELIB_IMAGE_DEBUG_DIRECTORY::size();i++)
  25. {
  26. ibBuffer >> iddCurr.idd.Characteristics;
  27. ibBuffer >> iddCurr.idd.TimeDateStamp;
  28. ibBuffer >> iddCurr.idd.MajorVersion;
  29. ibBuffer >> iddCurr.idd.MinorVersion;
  30. ibBuffer >> iddCurr.idd.Type;
  31. ibBuffer >> iddCurr.idd.SizeOfData;
  32. ibBuffer >> iddCurr.idd.AddressOfRawData;
  33. ibBuffer >> iddCurr.idd.PointerToRawData;
  34. currDebugInfo.push_back(iddCurr);
  35. }
  36. return currDebugInfo;
  37. }
  38. int DebugDirectory::read(unsigned char* buffer, unsigned int buffersize)
  39. {
  40. // XXX: Note, debug data is not read at all. This might or might not change
  41. // in the future.
  42. std::vector<byte> vDebugDirectory(buffer, buffer + buffersize);
  43. InputBuffer ibBuffer(vDebugDirectory);
  44. std::vector<PELIB_IMG_DEBUG_DIRECTORY> currDebugInfo = read(ibBuffer, buffersize);
  45. std::swap(currDebugInfo, m_vDebugInfo);
  46. return NO_ERROR;
  47. }
  48. /**
  49. * @param strFilename Name of the file which will be read.
  50. * @param uiOffset File offset of the Debug directory.
  51. * @param uiSize Size of the Debug directory.
  52. **/
  53. int DebugDirectory::read(const std::string& strFilename, unsigned int uiOffset, unsigned int uiSize)
  54. {
  55. std::ifstream ifFile(strFilename.c_str(), std::ios::binary);
  56. unsigned int ulFileSize = fileSize(ifFile);
  57. if (!ifFile)
  58. {
  59. return ERROR_OPENING_FILE;
  60. }
  61. if (ulFileSize < uiOffset + uiSize)
  62. {
  63. return ERROR_INVALID_FILE;
  64. }
  65. ifFile.seekg(uiOffset, std::ios::beg);
  66. std::vector<byte> vDebugDirectory(uiSize);
  67. ifFile.read(reinterpret_cast<char*>(&vDebugDirectory[0]), uiSize);
  68. InputBuffer ibBuffer(vDebugDirectory);
  69. std::vector<PELIB_IMG_DEBUG_DIRECTORY> currDebugInfo = read(ibBuffer, uiSize);
  70. for (unsigned int i=0;i<currDebugInfo.size();i++)
  71. {
  72. ifFile.seekg(currDebugInfo[i].idd.PointerToRawData, std::ios::beg);
  73. currDebugInfo[i].data.resize(currDebugInfo[i].idd.SizeOfData);
  74. ifFile.read(reinterpret_cast<char*>(&currDebugInfo[i].data[0]), currDebugInfo[i].idd.SizeOfData);
  75. if (!ifFile) return ERROR_INVALID_FILE;
  76. }
  77. std::swap(currDebugInfo, m_vDebugInfo);
  78. return NO_ERROR;
  79. }
  80. /**
  81. * Rebuilds the current debug directory.
  82. * @param vBuffer Buffer where the rebuilt directory is stored.
  83. **/
  84. void DebugDirectory::rebuild(std::vector<byte>& vBuffer) const
  85. {
  86. OutputBuffer obBuffer(vBuffer);
  87. for (unsigned int i=0;i<m_vDebugInfo.size();i++)
  88. {
  89. obBuffer << m_vDebugInfo[i].idd.Characteristics;
  90. obBuffer << m_vDebugInfo[i].idd.TimeDateStamp;
  91. obBuffer << m_vDebugInfo[i].idd.MajorVersion;
  92. obBuffer << m_vDebugInfo[i].idd.MinorVersion;
  93. obBuffer << m_vDebugInfo[i].idd.Type;
  94. obBuffer << m_vDebugInfo[i].idd.SizeOfData;
  95. obBuffer << m_vDebugInfo[i].idd.AddressOfRawData;
  96. obBuffer << m_vDebugInfo[i].idd.PointerToRawData;
  97. }
  98. }
  99. /**
  100. * @return Size of the debug directory.
  101. **/
  102. unsigned int DebugDirectory::size() const
  103. {
  104. return static_cast<unsigned int>(m_vDebugInfo.size()) * PELIB_IMAGE_DEBUG_DIRECTORY::size();
  105. }
  106. /**
  107. * @param strFilename Name of the file which will be written.
  108. * @param uiOffset File offset where the debug directory will be stored.
  109. **/
  110. int DebugDirectory::write(const std::string& strFilename, unsigned int uiOffset) const
  111. {
  112. std::fstream ofFile(strFilename.c_str(), std::ios_base::in);
  113. if (!ofFile)
  114. {
  115. ofFile.clear();
  116. ofFile.open(strFilename.c_str(), std::ios_base::out | std::ios_base::binary);
  117. }
  118. else
  119. {
  120. ofFile.close();
  121. ofFile.open(strFilename.c_str(), std::ios_base::in | std::ios_base::out | std::ios_base::binary);
  122. }
  123. if (!ofFile)
  124. {
  125. return ERROR_OPENING_FILE;
  126. }
  127. ofFile.seekp(uiOffset, std::ios::beg);
  128. std::vector<unsigned char> vBuffer;
  129. rebuild(vBuffer);
  130. ofFile.write(reinterpret_cast<const char*>(&vBuffer[0]), static_cast<unsigned int>(vBuffer.size()));
  131. ofFile.close();
  132. return NO_ERROR;
  133. }
  134. /**
  135. * @return Number of debug structures in the current Debug directory.
  136. **/
  137. unsigned int DebugDirectory::calcNumberOfEntries() const
  138. {
  139. return static_cast<unsigned int>(m_vDebugInfo.size());
  140. }
  141. /**
  142. * Adds a new debug structure to the debug directory. The initial values of all members of the structure
  143. * are undefined.
  144. **/
  145. void DebugDirectory::addEntry()
  146. {
  147. PELIB_IMG_DEBUG_DIRECTORY p;
  148. m_vDebugInfo.push_back(p);
  149. }
  150. /**
  151. * Removes a debug structure from the current debug directory. If an invalid structure is specified
  152. * by the parameter uiIndex the result will be undefined behaviour.
  153. * @param uiIndex Identifies the debug structure.
  154. **/
  155. void DebugDirectory::removeEntry(unsigned int uiIndex)
  156. {
  157. m_vDebugInfo.erase(m_vDebugInfo.begin() + uiIndex);
  158. }
  159. /**
  160. * Returns the Characteristics value of a debug structure. If an invalid structure is specified
  161. * by the parameter uiIndex the result will be undefined behaviour.
  162. * @param uiIndex Identifies the debug structure.
  163. * @return Characteristics value of the debug structure.
  164. **/
  165. dword DebugDirectory::getCharacteristics(unsigned int uiIndex) const
  166. {
  167. return m_vDebugInfo[uiIndex].idd.Characteristics;
  168. }
  169. /**
  170. * Returns the TimeDateStamp value of a debug structure. If an invalid structure is specified
  171. * by the parameter uiIndex the result will be undefined behaviour.
  172. * @param uiIndex Identifies the debug structure.
  173. * @return TimeDateStamp value of the debug structure.
  174. **/
  175. dword DebugDirectory::getTimeDateStamp(unsigned int uiIndex) const
  176. {
  177. return m_vDebugInfo[uiIndex].idd.TimeDateStamp;
  178. }
  179. /**
  180. * Returns the MajorVersion value of a debug structure. If an invalid structure is specified
  181. * by the parameter uiIndex the result will be undefined behaviour.
  182. * @param uiIndex Identifies the debug structure.
  183. * @return MajorVersion value of the debug structure.
  184. **/
  185. word DebugDirectory::getMajorVersion(unsigned int uiIndex) const
  186. {
  187. return m_vDebugInfo[uiIndex].idd.MajorVersion;
  188. }
  189. /**
  190. * Returns the MinorVersion value of a debug structure. If an invalid structure is specified
  191. * by the parameter uiIndex the result will be undefined behaviour.
  192. * @param uiIndex Identifies the debug structure.
  193. * @return MinorVersion value of the debug structure.
  194. **/
  195. word DebugDirectory::getMinorVersion(unsigned int uiIndex) const
  196. {
  197. return m_vDebugInfo[uiIndex].idd.MinorVersion;
  198. }
  199. /**
  200. * Returns the Type value of a debug structure. If an invalid structure is specified
  201. * by the parameter uiIndex the result will be undefined behaviour.
  202. * @param uiIndex Identifies the debug structure.
  203. * @return Type value of the debug structure.
  204. **/
  205. dword DebugDirectory::getType(unsigned int uiIndex) const
  206. {
  207. return m_vDebugInfo[uiIndex].idd.Type;
  208. }
  209. /**
  210. * Returns the SizeOfData 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 SizeOfData value of the debug structure.
  214. **/
  215. dword DebugDirectory::getSizeOfData(unsigned int uiIndex) const
  216. {
  217. return m_vDebugInfo[uiIndex].idd.SizeOfData;
  218. }
  219. /**
  220. * Returns the AddressOfRawData value of a debug structure. If an invalid structure is specified
  221. * by the parameter uiIndex the result will be undefined behaviour.
  222. * @param uiIndex Identifies the debug structure.
  223. * @return AddressOfRawData value of the debug structure.
  224. **/
  225. dword DebugDirectory::getAddressOfRawData(unsigned int uiIndex) const
  226. {
  227. return m_vDebugInfo[uiIndex].idd.AddressOfRawData;
  228. }
  229. /**
  230. * Returns the PointerToRawData value of a debug structure. If an invalid structure is specified
  231. * by the parameter uiIndex the result will be undefined behaviour.
  232. * @param uiIndex Identifies the debug structure.
  233. * @return PointerToRawData value of the debug structure.
  234. **/
  235. dword DebugDirectory::getPointerToRawData(unsigned int uiIndex) const
  236. {
  237. return m_vDebugInfo[uiIndex].idd.PointerToRawData;
  238. }
  239. std::vector<byte> DebugDirectory::getData(unsigned int index) const
  240. {
  241. return m_vDebugInfo[index].data;
  242. }
  243. /**
  244. * Changes the Characteristics value of a debug structure. If an invalid structure is specified
  245. * by the parameter uiIndex the result will be undefined behaviour.
  246. * @param uiIndex Identifies the debug structure.
  247. * @param dwValue New value of the Characteristics value of the debug structure.
  248. **/
  249. void DebugDirectory::setCharacteristics(unsigned int uiIndex, dword dwValue)
  250. {
  251. m_vDebugInfo[uiIndex].idd.Characteristics = dwValue;
  252. }
  253. /**
  254. * Changes the TimeDateStamp 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. * @param dwValue New value of the TimeDateStamp value of the debug structure.
  258. **/
  259. void DebugDirectory::setTimeDateStamp(unsigned int uiIndex, dword dwValue)
  260. {
  261. m_vDebugInfo[uiIndex].idd.TimeDateStamp = dwValue;
  262. }
  263. /**
  264. * Changes the MajorVersion value of a debug structure. If an invalid structure is specified
  265. * by the parameter uiIndex the result will be undefined behaviour.
  266. * @param uiIndex Identifies the debug structure.
  267. * @param wValue New value of the MajorVersion value of the debug structure.
  268. **/
  269. void DebugDirectory::setMajorVersion(unsigned int uiIndex, word wValue)
  270. {
  271. m_vDebugInfo[uiIndex].idd.MajorVersion = wValue;
  272. }
  273. /**
  274. * Changes the MinorVersion value of a debug structure. If an invalid structure is specified
  275. * by the parameter uiIndex the result will be undefined behaviour.
  276. * @param uiIndex Identifies the debug structure.
  277. * @param wValue New value of the MinorVersion value of the debug structure.
  278. **/
  279. void DebugDirectory::setMinorVersion(unsigned int uiIndex, word wValue)
  280. {
  281. m_vDebugInfo[uiIndex].idd.MinorVersion = wValue;
  282. }
  283. /**
  284. * Changes the Type value of a debug structure. If an invalid structure is specified
  285. * by the parameter uiIndex the result will be undefined behaviour.
  286. * @param uiIndex Identifies the debug structure.
  287. * @param dwValue New value of the Type value of the debug structure.
  288. **/
  289. void DebugDirectory::setType(unsigned int uiIndex, dword dwValue)
  290. {
  291. m_vDebugInfo[uiIndex].idd.Type = dwValue;
  292. }
  293. /**
  294. * Changes the SizeOfData value of a debug structure. If an invalid structure is specified
  295. * by the parameter uiIndex the result will be undefined behaviour.
  296. * @param uiIndex Identifies the debug structure.
  297. * @param dwValue New value of the SizeOfData value of the debug structure.
  298. **/
  299. void DebugDirectory::setSizeOfData(unsigned int uiIndex, dword dwValue)
  300. {
  301. m_vDebugInfo[uiIndex].idd.SizeOfData = dwValue;
  302. }
  303. /**
  304. * Changes the AddressOfRawData value of a debug structure. If an invalid structure is specified
  305. * by the parameter uiIndex the result will be undefined behaviour.
  306. * @param uiIndex Identifies the debug structure.
  307. * @param dwValue New value of the AddressOfRawData value of the debug structure.
  308. **/
  309. void DebugDirectory::setAddressOfRawData(unsigned int uiIndex, dword dwValue)
  310. {
  311. m_vDebugInfo[uiIndex].idd.AddressOfRawData = dwValue;
  312. }
  313. /**
  314. * Changes the PointerToRawData 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 dwValue New value of the PointerToRawData value of the debug structure.
  318. **/
  319. void DebugDirectory::setPointerToRawData(unsigned int uiIndex, dword dwValue)
  320. {
  321. m_vDebugInfo[uiIndex].idd.PointerToRawData = dwValue;
  322. }
  323. void DebugDirectory::setData(unsigned int index, const std::vector<byte>& data)
  324. {
  325. m_vDebugInfo[index].data = data;
  326. }
  327. }