Main Page | Class Hierarchy | Class List | File List | Class Members | Related Pages

PeFile.h

00001 /* 00002 * PeFile.h - Part of the PeLib library. 00003 * 00004 * Copyright (c) 2004 Sebastian Porst (webmaster@the-interweb.com) 00005 * All rights reserved. 00006 * 00007 * This software is licensed under the zlib/libpng License. 00008 * For more details see http://www.opensource.org/licenses/zlib-license.php 00009 * or the license information file (license.htm) in the root directory 00010 * of PeLib. 00011 */ 00012 00013 #ifndef PEFILE_H 00014 #define PEFILE_H 00015 00016 #include "PeLibInc.h" 00017 #include "MzHeader.h" 00018 #include "PeHeader.h" 00019 #include "ImportDirectory.h" 00020 #include "ExportDirectory.h" 00021 #include "BoundImportDirectory.h" 00022 #include "ResourceDirectory.h" 00023 #include "RelocationsDirectory.h" 00024 #include "ComHeaderDirectory.h" 00025 #include "IatDirectory.h" 00026 #include "DebugDirectory.h" 00027 #include "TlsDirectory.h" 00028 00029 namespace PeLib 00030 { 00031 class PeFile32; 00032 class PeFile64; 00033 00037 class PeFileVisitor 00038 { 00039 public: 00040 virtual void callback(PeFile32 &file){} 00041 virtual void callback(PeFile64 &file){} 00042 virtual ~PeFileVisitor(){} 00043 }; 00044 00048 template<int> 00049 struct PeFile_Traits; 00050 00051 template<> 00052 struct PeFile_Traits<32> 00053 { 00054 typedef PeHeader32 PeHeader32_64; 00055 }; 00056 00057 template<> 00058 struct PeFile_Traits<64> 00059 { 00060 typedef PeHeader64 PeHeader32_64; 00061 }; 00062 00066 class PeFile 00067 { 00068 protected: 00069 std::string m_filename; 00070 MzHeader m_mzh; 00071 ExportDirectory m_expdir; 00072 BoundImportDirectory m_boundimpdir; 00073 ResourceDirectory m_resdir; 00074 RelocationsDirectory m_relocs; 00075 ComHeaderDirectory m_comdesc; 00076 IatDirectory m_iat; 00077 DebugDirectory m_debugdir; 00078 public: 00079 virtual ~PeFile(); 00080 00082 virtual std::string getFileName() const = 0; 00084 virtual void setFileName(std::string strFilename) = 0; 00085 00086 virtual void visit(PeFileVisitor &v) = 0; 00087 00089 virtual int readMzHeader() = 0; 00091 virtual int readExportDirectory() = 0; 00093 virtual int readPeHeader() = 0; 00095 virtual int readImportDirectory() = 0; 00097 virtual int readBoundImportDirectory() = 0; 00099 virtual int readResourceDirectory() = 0; 00101 virtual int readRelocationsDirectory() = 0; 00103 virtual int readComHeaderDirectory() = 0; 00105 virtual int readIatDirectory() = 0; 00107 virtual int readDebugDirectory() = 0; 00108 virtual int readTlsDirectory() = 0; 00109 00110 virtual unsigned int getBits() const = 0; 00111 00113 const MzHeader& mzHeader() const; 00115 MzHeader& mzHeader(); 00116 00118 const ExportDirectory& expDir() const; 00120 ExportDirectory& expDir(); 00121 00123 const BoundImportDirectory& boundImpDir() const; 00125 BoundImportDirectory& boundImpDir(); 00126 00128 const ResourceDirectory& resDir() const; 00130 ResourceDirectory& resDir(); 00131 00133 const RelocationsDirectory& relocDir() const; 00135 RelocationsDirectory& relocDir(); 00136 00138 const ComHeaderDirectory& comDir() const; 00140 ComHeaderDirectory& comDir(); 00141 00143 const IatDirectory& iatDir() const; 00145 IatDirectory& iatDir(); 00146 00148 const DebugDirectory& debugDir() const; 00150 DebugDirectory& debugDir(); 00151 00152 }; 00153 00157 template<int bits> 00158 class PeFileT : public PeFile 00159 { 00160 typedef typename PeFile_Traits<bits>::PeHeader32_64 PeHeader32_64; 00161 00162 private: 00163 PeHeader32_64 m_peh; 00164 ImportDirectory<bits> m_impdir; 00165 TlsDirectory<bits> m_tlsdir; 00166 00167 public: 00169 PeFileT(); 00170 00171 virtual ~PeFileT() {} 00172 00174 explicit PeFileT(const std::string& strFilename); 00175 00177 std::string getFileName() const; 00179 void setFileName(std::string strFilename); 00180 00182 int readMzHeader() ; 00184 int readExportDirectory() ; 00186 int readPeHeader() ; 00188 int readImportDirectory() ; 00190 int readBoundImportDirectory() ; 00192 int readResourceDirectory() ; 00194 int readRelocationsDirectory() ; 00196 int readComHeaderDirectory() ; 00198 int readIatDirectory() ; 00200 int readDebugDirectory() ; 00201 int readTlsDirectory() ; 00202 00203 unsigned int getBits() const 00204 { 00205 return bits; 00206 } 00207 00209 const PeHeader32_64& peHeader() const; 00211 PeHeader32_64& peHeader(); 00212 00214 const ImportDirectory<bits>& impDir() const; 00216 ImportDirectory<bits>& impDir(); 00217 00218 const TlsDirectory<bits>& tlsDir() const; 00219 TlsDirectory<bits>& tlsDir(); 00220 }; 00221 00225 class PeFile32 : public PeFileT<32> 00226 { 00227 public: 00229 PeFile32(); 00230 00232 explicit PeFile32(const std::string& strFlename); 00233 virtual void visit(PeFileVisitor &v) { v.callback( *this ); } 00234 }; 00235 00239 class PeFile64 : public PeFileT<64> 00240 { 00241 public: 00243 PeFile64(); 00244 00246 explicit PeFile64(const std::string& strFlename); 00247 virtual void visit(PeFileVisitor &v) { v.callback( *this ); } 00248 }; 00249 00250 //typedef PeFileT<32> PeFile32; 00251 //typedef PeFileT<64> PeFile64; 00252 00256 template<int bits> 00257 PeFileT<bits>::PeFileT(const std::string& strFilename) 00258 { 00259 m_filename = strFilename; 00260 } 00261 00262 template<int bits> 00263 PeFileT<bits>::PeFileT() 00264 { 00265 } 00266 00267 template<int bits> 00268 int PeFileT<bits>::readPeHeader() 00269 { 00270 return peHeader().read(getFileName(), mzHeader().getAddressOfPeHeader()); 00271 } 00272 00273 00274 template<int bits> 00275 int PeFileT<bits>::readImportDirectory() 00276 { 00277 if (peHeader().getIddImportRva() && peHeader().getIddImportSize()) 00278 { 00279 return impDir().read(getFileName(), static_cast<unsigned int>(peHeader().rvaToOffset(peHeader().getIddImportRva())), peHeader().getIddImportSize(), peHeader()); 00280 } 00281 return 1; 00282 } 00283 00287 template<int bits> 00288 const typename PeFile_Traits<bits>::PeHeader32_64& PeFileT<bits>::peHeader() const 00289 { 00290 return m_peh; 00291 } 00292 00296 template<int bits> 00297 typename PeFile_Traits<bits>::PeHeader32_64& PeFileT<bits>::peHeader() 00298 { 00299 return m_peh; 00300 } 00301 00305 template<int bits> 00306 const ImportDirectory<bits>& PeFileT<bits>::impDir() const 00307 { 00308 return m_impdir; 00309 } 00310 00314 template<int bits> 00315 ImportDirectory<bits>& PeFileT<bits>::impDir() 00316 { 00317 return m_impdir; 00318 } 00319 00320 template<int bits> 00321 const TlsDirectory<bits>& PeFileT<bits>::tlsDir() const 00322 { 00323 return m_tlsdir; 00324 } 00325 00326 template<int bits> 00327 TlsDirectory<bits>& PeFileT<bits>::tlsDir() 00328 { 00329 return m_tlsdir; 00330 } 00331 00335 template<int bits> 00336 std::string PeFileT<bits>::getFileName() const 00337 { 00338 return m_filename; 00339 } 00340 00344 template<int bits> 00345 void PeFileT<bits>::setFileName(std::string strFilename) 00346 { 00347 m_filename = strFilename; 00348 } 00349 00350 template<int bits> 00351 int PeFileT<bits>::readMzHeader() 00352 { 00353 return mzHeader().read(getFileName()); 00354 } 00355 00356 template<int bits> 00357 int PeFileT<bits>::readExportDirectory() 00358 { 00359 if (peHeader().getIddExportRva() && peHeader().getIddExportSize()) 00360 { 00361 return expDir().read(getFileName(), static_cast<unsigned int>(peHeader().rvaToOffset(peHeader().getIddExportRva())), peHeader().getIddExportSize(), peHeader()); 00362 } 00363 return 1; 00364 } 00365 00366 00367 template<int bits> 00368 int PeFileT<bits>::readBoundImportDirectory() 00369 { 00370 if (peHeader().getIddBoundImportRva() && peHeader().getIddBoundImportSize()) 00371 { 00372 return boundImpDir().read(getFileName(), static_cast<unsigned int>(peHeader().rvaToOffset(peHeader().getIddBoundImportRva())), peHeader().getIddBoundImportSize()); 00373 } 00374 return 1; 00375 } 00376 00377 template<int bits> 00378 int PeFileT<bits>::readResourceDirectory() 00379 { 00380 if (peHeader().getIddResourceRva() && peHeader().getIddResourceSize()) 00381 { 00382 return resDir().read(getFileName(), static_cast<unsigned int>(peHeader().rvaToOffset(peHeader().getIddResourceRva())), peHeader().getIddResourceSize(), peHeader().getIddResourceRva()); 00383 } 00384 return 1; 00385 } 00386 00387 template<int bits> 00388 int PeFileT<bits>::readRelocationsDirectory() 00389 { 00390 if (peHeader().getIddBaseRelocRva() && peHeader().getIddBaseRelocSize()) 00391 { 00392 return relocDir().read(getFileName(), static_cast<unsigned int>(peHeader().rvaToOffset(peHeader().getIddBaseRelocRva())), peHeader().getIddBaseRelocSize()); 00393 } 00394 return 1; 00395 } 00396 00397 template<int bits> 00398 int PeFileT<bits>::readComHeaderDirectory() 00399 { 00400 if (peHeader().getIddComHeaderRva() && peHeader().getIddComHeaderSize()) 00401 { 00402 return comDir().read(getFileName(), static_cast<unsigned int>(peHeader().rvaToOffset(peHeader().getIddComHeaderRva())), peHeader().getIddComHeaderSize()); 00403 } 00404 return 1; 00405 } 00406 00407 template<int bits> 00408 int PeFileT<bits>::readIatDirectory() 00409 { 00410 if (peHeader().getIddIatRva() && peHeader().getIddIatSize()) 00411 { 00412 return iatDir().read(getFileName(), static_cast<unsigned int>(peHeader().rvaToOffset(peHeader().getIddIatRva())), peHeader().getIddIatSize()); 00413 } 00414 return 1; 00415 } 00416 00417 template<int bits> 00418 int PeFileT<bits>::readDebugDirectory() 00419 { 00420 if (peHeader().getIddDebugRva() && peHeader().getIddDebugSize()) 00421 { 00422 return debugDir().read(getFileName(), static_cast<unsigned int>(peHeader().rvaToOffset(peHeader().getIddDebugRva())), peHeader().getIddDebugSize()); 00423 } 00424 return 1; 00425 } 00426 00427 template<int bits> 00428 int PeFileT<bits>::readTlsDirectory() 00429 { 00430 if (peHeader().getIddTlsRva() && peHeader().getIddTlsSize()) 00431 { 00432 return tlsDir().read(getFileName(), static_cast<unsigned int>(peHeader().rvaToOffset(peHeader().getIddTlsRva())), peHeader().getIddTlsSize()); 00433 } 00434 return 1; 00435 } 00436 00437 } 00438 00439 #endif

Generated on Mon Jan 17 20:50:08 2005 for PeLib by doxygen 1.3.7