/Rainman2/win32pe.h

http://modstudio2.googlecode.com/ · C Header · 169 lines · 115 code · 30 blank · 24 comment · 5 complexity · 8b57db5dd30ee26dc1411e7cd6ecb32b MD5 · raw file

  1. /*
  2. Copyright (c) 2008 Peter "Corsix" Cawley
  3. Permission is hereby granted, free of charge, to any person
  4. obtaining a copy of this software and associated documentation
  5. files (the "Software"), to deal in the Software without
  6. restriction, including without limitation the rights to use,
  7. copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the
  9. Software is furnished to do so, subject to the following
  10. conditions:
  11. The above copyright notice and this permission notice shall be
  12. included in all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  14. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  15. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  16. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  17. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  18. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. OTHER DEALINGS IN THE SOFTWARE.
  21. */
  22. #pragma once
  23. #include "api.h"
  24. #include "file.h"
  25. #include <vector>
  26. class RAINMAN2_API Win32PEFile
  27. {
  28. public:
  29. struct Section
  30. {
  31. RainString sName;
  32. unsigned long iVirtualSize;
  33. unsigned long iVirtualAddress;
  34. unsigned long iPhysicalSize;
  35. unsigned long iPhysicalAddress;
  36. unsigned long iFlags;
  37. };
  38. class ResourceNodeName
  39. {
  40. public:
  41. ResourceNodeName(const ResourceNodeName&);
  42. ResourceNodeName(RainString sName);
  43. ResourceNodeName(unsigned long iIdentifier);
  44. inline bool hasName() const throw() {return m_bUsesName;}
  45. inline const RainString& getName() const throw() {return m_sName;}
  46. inline bool hasIdentifier() const throw() {return !m_bUsesName;}
  47. inline unsigned long getIdentifier() const throw() {return m_iIdentifier;}
  48. protected:
  49. RainString m_sName;
  50. unsigned long m_iIdentifier;
  51. bool m_bUsesName;
  52. };
  53. class ResourceNode
  54. {
  55. public:
  56. typedef std::vector<ResourceNode*> Children;
  57. inline bool hasName() const throw() {return m_bHasName;}
  58. inline const RainString& getName() const throw() {return m_sName;}
  59. inline bool hasIdentifier() const throw() {return m_bHasIdentifer;}
  60. inline unsigned long getIdentifier() const throw() {return m_iIdentifier;}
  61. inline bool hasData () const throw() {return m_bHasData;}
  62. inline unsigned long getDataAddress () const throw() {return m_iDataOffset;}
  63. inline unsigned long getDataSize () const throw() {return m_iDataLength;}
  64. inline unsigned long getDataCodepage() const throw() {return m_iDataCodepage;}
  65. inline bool hasParent() const throw() {return m_pParent != 0;}
  66. inline ResourceNode* getParent() throw() {return m_pParent;}
  67. inline const ResourceNode* getParent() const throw() {return m_pParent;}
  68. inline bool hasChildren() const throw() {return m_bHasChildren;}
  69. inline Children& getChildren() throw() {return m_vChildren;}
  70. inline const Children& getChildren() const throw() {return m_vChildren;}
  71. const ResourceNode* findFirstChildWithData() const;
  72. const ResourceNode* findChild(ResourceNodeName oName) const;
  73. protected:
  74. friend class Win32PEFile;
  75. ResourceNode();
  76. ~ResourceNode();
  77. RainString m_sName;
  78. unsigned long m_iIdentifier,
  79. m_iDataOffset,
  80. m_iDataLength,
  81. m_iDataCodepage;
  82. ResourceNode* m_pParent;
  83. Children m_vChildren;
  84. bool m_bHasName : 1,
  85. m_bHasIdentifer : 1,
  86. m_bHasData : 1,
  87. m_bHasChildren : 1;
  88. };
  89. struct Icon
  90. {
  91. Icon();
  92. unsigned char iWidth,
  93. iHeight,
  94. iColourCount,
  95. iReserved;
  96. unsigned short iNumPlanes,
  97. iBitsPerPixel,
  98. iIndex;
  99. unsigned long iSize,
  100. iOffset;
  101. };
  102. typedef std::vector<Icon> IconGroup;
  103. Win32PEFile();
  104. ~Win32PEFile();
  105. void clear();
  106. void load(IFile *pFile) throw(...);
  107. void load(RainString sFile) throw(...);
  108. size_t getSectionCount() const throw();
  109. const Section& getSection(size_t iIndex) const throw(...);
  110. const std::vector<Section>& getSectionArray() const throw();
  111. size_t getImportedLibraryCount() const throw();
  112. const RainString& getImportedLibrary(size_t iIndex) const throw(...);
  113. const std::vector<RainString>& getImportedLibraryArray() const throw();
  114. const ResourceNode* getResources() const throw();
  115. const ResourceNode* findResource(ResourceNodeName oType) const throw();
  116. const ResourceNode* findResource(ResourceNodeName oType, ResourceNodeName oName) const throw();
  117. const ResourceNode* findResource(ResourceNodeName oType, ResourceNodeName oName, ResourceNodeName oLanguage) const throw();
  118. const std::vector<IconGroup*>& getIconGroupArray() const throw();
  119. protected:
  120. std::vector< std::pair<unsigned long, unsigned long> >m_vSpecialTables;
  121. std::vector<Section> m_vSections;
  122. std::vector<RainString> m_vImportedLibraries;
  123. std::vector<IconGroup*> m_vIconGroups;
  124. ResourceNode* m_pRootResourceNode;
  125. static const unsigned long NOT_MAPPED = static_cast<unsigned long>(-1);
  126. unsigned long _mapToPhysicalAddress(unsigned long iVirtualAddress) throw(...);
  127. unsigned long _mapToPhysicalAddressNoThrow(unsigned long iVirtualAddress) throw(...);
  128. void _loadImports(IFile *pFile);
  129. void _loadResources(IFile *pFile);
  130. void _loadResourceTable(IFile *pFile, ResourceNode::Children& vDestination);
  131. void _loadIconGroup(IFile *pFile, IconGroup *pDestination);
  132. };
  133. bool operator == (const Win32PEFile::ResourceNodeName&, const Win32PEFile::ResourceNode&);
  134. bool operator == (const Win32PEFile::ResourceNodeName&, const Win32PEFile::ResourceNode*);
  135. bool operator == (const Win32PEFile::ResourceNode&, const Win32PEFile::ResourceNodeName&);
  136. bool operator == (const Win32PEFile::ResourceNode*, const Win32PEFile::ResourceNodeName&);