retdec
PeFile.h
Go to the documentation of this file.
1 /*
2 * PeFile.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 RETDEC_PELIB_PEFILE_H
14 #define RETDEC_PELIB_PEFILE_H
15 
16 #include "retdec/pelib/PeLibInc.h"
31 
32 namespace PeLib
33 {
37  class PeFile
38  {
39  protected:
40  std::string m_filename;
45  public:
46  virtual ~PeFile();
47 
49  virtual std::string getFileName() const = 0; // EXPORT
51  virtual void setFileName(const std::string & strFilename) = 0; // EXPORT
52 
54  virtual int readExportDirectory() = 0; // EXPORT
56  virtual int readImportDirectory() = 0; // EXPORT
58  virtual int readBoundImportDirectory() = 0; // EXPORT
60  virtual int readResourceDirectory() = 0; // EXPORT
62  virtual int readRelocationsDirectory() = 0; // EXPORT
64  virtual int readComHeaderDirectory() = 0; // EXPORT
66  virtual int readIatDirectory() = 0; // EXPORT
68  virtual int readDebugDirectory() = 0; // EXPORT
70  virtual int readTlsDirectory() = 0; // EXPORT
72  virtual int readRichHeader(std::size_t offset, std::size_t size, bool ignoreInvalidKey = false) = 0; // EXPORT
74  virtual int readCoffSymbolTable(ByteBuffer & fileData) = 0; // EXPORT
76  virtual int readDelayImportDirectory() = 0; // EXPORT
78  virtual int readSecurityDirectory() = 0; // EXPORT
80  virtual LoaderError loaderError() const = 0;
81 
82  virtual unsigned int getBits() const = 0;
83 
85  const ImageLoader & imageLoader() const;
87  ImageLoader & imageLoader(); // EXPORT
88 
90  const RichHeader& richHeader() const;
92  RichHeader& richHeader(); // EXPORT
93 
95  const CoffSymbolTable& coffSymTab() const;
97  CoffSymbolTable& coffSymTab(); // EXPORT
98 
100  const SecurityDirectory& securityDir() const;
103  };
104 
108  class PeFileT : public PeFile
109  {
110  private:
111 
112  std::ifstream m_ifStream;
113  std::istream& m_iStream;
114 
125 
126  public:
127 
128  PeFileT(const std::string& strFileName);
129  PeFileT(std::istream& stream);
130  PeFileT();
131  virtual ~PeFileT() {}
132 
134  int loadPeHeaders(bool loadHeadersOnly = false);
135 
137  int loadPeHeaders(ByteBuffer & fileData, bool loadHeadersOnly = false);
138 
140  int getFileType() const;
141 
143  std::string getFileName() const;
145  void setFileName(const std::string & strFilename);
146 
148  int readExportDirectory() ;
150  int readImportDirectory() ;
154  int readResourceDirectory() ;
158  int readComHeaderDirectory() ;
160  int readIatDirectory() ;
162  int readDebugDirectory() ;
164  int readTlsDirectory() ;
166  int readRichHeader(std::size_t offset, std::size_t size, bool ignoreInvalidKey = false) ;
168  int readCoffSymbolTable(ByteBuffer & fileData);
172  int readSecurityDirectory() ;
173 
177 
179  LoaderError loaderError() const;
180 
181  unsigned int getBits() const
182  {
184  }
185 
187  const ExportDirectory & expDir() const;
189  ExportDirectory & expDir(); // EXPORT
190 
192  const ImportDirectory & impDir() const;
195 
197  const BoundImportDirectory & boundImpDir() const;
199  BoundImportDirectory & boundImpDir(); // EXPORT
200 
202  const ResourceDirectory & resDir() const;
204  ResourceDirectory & resDir(); // EXPORT
205 
207  const RelocationsDirectory & relocDir() const;
209  RelocationsDirectory & relocDir(); // EXPORT
210 
212  const ComHeaderDirectory & comDir() const;
214  ComHeaderDirectory & comDir(); // EXPORT
215 
217  const IatDirectory & iatDir() const;
219  IatDirectory & iatDir(); // EXPORT
220 
222  const DebugDirectory & debugDir() const;
224  DebugDirectory & debugDir(); // EXPORT
225 
227  const DelayImportDirectory & delayImports() const;
229  DelayImportDirectory & delayImports(); // EXPORT
230 
232  const TlsDirectory & tlsDir() const;
234  TlsDirectory & tlsDir();
235  };
236 }
237 
238 #endif
Class for COFF symbol table.
Class for delay import directory.
Class for rich header.
Class for certificate directory.
Class that handles the BoundImport directory.
Definition: BoundImportDirectory.h:27
Definition: CoffSymbolTable.h:18
Class that handles the COM+ descriptor directory.
Definition: ComHeaderDirectory.h:26
Class that handles the Debug directory.
Definition: DebugDirectory.h:22
Definition: DelayImportDirectory.h:20
Class that handles the export directory.
Definition: ExportDirectory.h:26
Class that handles the Import Address Table (IAT)
Definition: IatDirectory.h:26
Definition: ImageLoader.h:138
std::uint32_t getImageBitability() const
Definition: ImageLoader.cpp:432
Class that handles import directories.
Definition: ImportDirectory.h:42
Definition: PeFile.h:109
int readImportDirectory()
Reads the import directory of the current file from disc.
Definition: PeFile.cpp:297
std::ifstream m_ifStream
Valid if we opened the file stream ourself.
Definition: PeFile.h:112
ComHeaderDirectory m_comdesc
COM+ descriptor directory of the current file.
Definition: PeFile.h:120
PeFileT()
Definition: PeFile.cpp:25
const ExportDirectory & expDir() const
Accessor function for the export directory.
Definition: PeFile.cpp:153
int loadPeHeaders(bool loadHeadersOnly=false)
Load the PE file using the already-open stream.
Definition: PeFile.cpp:31
int readSecurityDirectory()
Reads the security directory of the current file.
Definition: PeFile.cpp:315
std::string getFileName() const
Returns the name of the current file.
Definition: PeFile.cpp:253
int getFileType() const
returns PEFILE64 or PEFILE32
Definition: PeFile.cpp:42
int readRelocationsDirectory()
Reads the relocations directory of the current file from disc.
Definition: PeFile.cpp:326
int readCoffSymbolTable(ByteBuffer &fileData)
Reads the COFF symbol table of the current file.
Definition: PeFile.cpp:276
ResourceDirectory m_resdir
ResourceDirectory of the current file.
Definition: PeFile.h:118
TlsDirectory m_tlsdir
TLS directory of the current file.
Definition: PeFile.h:124
const BoundImportDirectory & boundImpDir() const
Accessor function for the bound import directory.
Definition: PeFile.cpp:169
DebugDirectory m_debugdir
Debug directory of the current file.
Definition: PeFile.h:122
int readDebugDirectory()
Reads the Debug directory of the current file.
Definition: PeFile.cpp:335
BoundImportDirectory m_boundimpdir
BoundImportDirectory of the current file.
Definition: PeFile.h:117
const ResourceDirectory & resDir() const
Accessor function for the resource directory.
Definition: PeFile.cpp:185
int readExportDirectory()
Reads the export directory of the current file from disc.
Definition: PeFile.cpp:288
int readComHeaderDirectory()
Reads the COM+ descriptor directory of the current file from disc.
Definition: PeFile.cpp:381
const DebugDirectory & debugDir() const
Accessor function for the debug directory.
Definition: PeFile.cpp:240
int readBoundImportDirectory()
Reads the bound import directory of the current file from disc.
Definition: PeFile.cpp:353
DelayImportDirectory m_delayimpdir
Delay import directory of the current file.
Definition: PeFile.h:123
const DelayImportDirectory & delayImports() const
Accessor function for the delay import directory.
Definition: PeFile.cpp:137
RelocationsDirectory m_relocs
Relocations directory of the current file.
Definition: PeFile.h:119
unsigned int getBits() const
Definition: PeFile.h:181
const ImportDirectory & impDir() const
Accessor function for the import directory.
Definition: PeFile.cpp:111
ExportDirectory m_expdir
Export directory of the current file.
Definition: PeFile.h:115
void setFileName(const std::string &strFilename)
Changes the name of the current file.
Definition: PeFile.cpp:261
LoaderError loaderError() const
Returns a loader error, if there was any.
Definition: PeFile.cpp:468
int readDelayImportDirectory()
Reads delay import directory of the current file.
Definition: PeFile.cpp:371
LoaderError checkForInMemoryLayout(LoaderError ldrError) const
Definition: PeFile.cpp:430
const ComHeaderDirectory & comDir() const
Accessor function for the COM+ descriptor directory.
Definition: PeFile.cpp:217
std::istream & m_iStream
Can also reference m_ifStream.
Definition: PeFile.h:113
const IatDirectory & iatDir() const
Accessor function for the IAT directory.
Definition: PeFile.cpp:230
IatDirectory m_iat
Import address table of the current file.
Definition: PeFile.h:121
int readIatDirectory()
Reads the IAT directory of the current file from disc.
Definition: PeFile.cpp:362
const RelocationsDirectory & relocDir() const
Accessor function for the relocations directory.
Definition: PeFile.cpp:201
ImportDirectory m_impdir
Import directory of the current file.
Definition: PeFile.h:116
int readTlsDirectory()
Reads the TLS directory of the current file.
Definition: PeFile.cpp:344
const TlsDirectory & tlsDir() const
Accessor function for the TLS directory.
Definition: PeFile.cpp:124
int readRichHeader(std::size_t offset, std::size_t size, bool ignoreInvalidKey=false)
Reads rich header of the current file.
Definition: PeFile.cpp:271
virtual ~PeFileT()
Default constructor which exists only for the sake of allowing to construct files without filenames.
Definition: PeFile.h:131
LoaderError checkEntryPointErrors() const
Checks the entry point code.
Definition: PeFile.cpp:391
int readResourceDirectory()
Reads the resource directory of the current file from disc.
Definition: PeFile.cpp:306
Definition: PeFile.h:38
virtual LoaderError loaderError() const =0
Returns a loader error, if there was any.
virtual int readDebugDirectory()=0
Reads the Debug directory of the current file.
const SecurityDirectory & securityDir() const
Accessor function for the security directory.
Definition: PeFile.cpp:98
virtual int readExportDirectory()=0
Reads the export directory of the current file from disc.
virtual void setFileName(const std::string &strFilename)=0
Changes the name of the current file.
const ImageLoader & imageLoader() const
Accessor function for the image loader.
Definition: PeFile.cpp:64
virtual int readBoundImportDirectory()=0
Reads the bound import directory of the current file from disc.
virtual int readComHeaderDirectory()=0
Reads the COM+ descriptor directory of the current file from disc.
virtual unsigned int getBits() const =0
const RichHeader & richHeader() const
Accessor function for the Rich header.
Definition: PeFile.cpp:78
SecurityDirectory m_secdir
Security directory of the current file.
Definition: PeFile.h:44
virtual int readResourceDirectory()=0
Reads the resource directory of the current file from disc.
virtual int readIatDirectory()=0
Reads the IAT directory of the current file from disc.
virtual ~PeFile()
Definition: PeFile.cpp:28
RichHeader m_richheader
Rich header of the current file.
Definition: PeFile.h:42
virtual int readImportDirectory()=0
Reads the import directory of the current file from disc.
virtual int readCoffSymbolTable(ByteBuffer &fileData)=0
Reads the COFF symbol table of the current file.
virtual int readTlsDirectory()=0
Reads the TLS directory of the current file.
virtual std::string getFileName() const =0
Returns the name of the current file.
virtual int readRelocationsDirectory()=0
Reads the relocations directory of the current file from disc.
virtual int readSecurityDirectory()=0
Reads security directory of the current file.
CoffSymbolTable m_coffsymtab
Symbol table of the current file.
Definition: PeFile.h:43
std::string m_filename
Name of the current file.
Definition: PeFile.h:40
const CoffSymbolTable & coffSymTab() const
Accessor function for the COFF symbol table.
Definition: PeFile.cpp:88
virtual int readDelayImportDirectory()=0
Reads delay import directory of the current file.
ImageLoader m_imageLoader
Definition: PeFile.h:41
virtual int readRichHeader(std::size_t offset, std::size_t size, bool ignoreInvalidKey=false)=0
Reads rich header of the current file.
Class that handles the relocations directory.
Definition: RelocationsDirectory.h:25
Class that represents the resource directory of a PE file.
Definition: ResourceDirectory.h:342
Definition: RichHeader.h:20
Definition: SecurityDirectory.h:13
Class that handles the TLS directory.
Definition: TlsDirectory.h:25
Definition: BoundImportDirectory.h:21
LoaderError
Definition: PeLibAux.h:47
std::vector< std::uint8_t > ByteBuffer
Definition: PeLibAux.h:126