PageRenderTime 50ms CodeModel.GetById 14ms app.highlight 30ms RepoModel.GetById 1ms app.codeStats 0ms

/Rainman2/win32pe.h

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