summaryrefslogtreecommitdiffstats
path: root/utils/zenutils/libraries/pelib-0.9
diff options
context:
space:
mode:
authorNicolas Pennequin <nicolas.pennequin@free.fr>2008-07-11 16:51:25 +0000
committerNicolas Pennequin <nicolas.pennequin@free.fr>2008-07-11 16:51:25 +0000
commitca5bb76d2b8f65aa97e50b633f828c1deb241526 (patch)
tree453a1b2de3a0dc0d0b2f7080d10d033bf8fbcdf1 /utils/zenutils/libraries/pelib-0.9
parent141774be48940d56e3ad4dbf451d245b61d4f8b2 (diff)
downloadrockbox-ca5bb76d2b8f65aa97e50b633f828c1deb241526.tar.gz
rockbox-ca5bb76d2b8f65aa97e50b633f828c1deb241526.tar.bz2
rockbox-ca5bb76d2b8f65aa97e50b633f828c1deb241526.zip
Delete the svn:executable property and set svn:eol-style to native for all those text files.
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@18012 a1c6a512-1295-4272-9138-f99709370657
Diffstat (limited to 'utils/zenutils/libraries/pelib-0.9')
-rw-r--r--[-rwxr-xr-x]utils/zenutils/libraries/pelib-0.9/CMakeLists.txt40
-rw-r--r--[-rwxr-xr-x]utils/zenutils/libraries/pelib-0.9/pelib/BoundImportDirectory.cpp1022
-rw-r--r--[-rwxr-xr-x]utils/zenutils/libraries/pelib-0.9/pelib/BoundImportDirectory.h174
-rw-r--r--[-rwxr-xr-x]utils/zenutils/libraries/pelib-0.9/pelib/ComHeaderDirectory.cpp934
-rw-r--r--[-rwxr-xr-x]utils/zenutils/libraries/pelib-0.9/pelib/ComHeaderDirectory.h240
-rw-r--r--[-rwxr-xr-x]utils/zenutils/libraries/pelib-0.9/pelib/DebugDirectory.cpp766
-rw-r--r--[-rwxr-xr-x]utils/zenutils/libraries/pelib-0.9/pelib/DebugDirectory.h168
-rw-r--r--[-rwxr-xr-x]utils/zenutils/libraries/pelib-0.9/pelib/ExportDirectory.cpp1384
-rw-r--r--[-rwxr-xr-x]utils/zenutils/libraries/pelib-0.9/pelib/ExportDirectory.h266
-rw-r--r--[-rwxr-xr-x]utils/zenutils/libraries/pelib-0.9/pelib/IatDirectory.cpp358
-rw-r--r--[-rwxr-xr-x]utils/zenutils/libraries/pelib-0.9/pelib/IatDirectory.h116
-rw-r--r--[-rwxr-xr-x]utils/zenutils/libraries/pelib-0.9/pelib/ImportDirectory.h2278
-rw-r--r--[-rwxr-xr-x]utils/zenutils/libraries/pelib-0.9/pelib/MzHeader.cpp1168
-rw-r--r--[-rwxr-xr-x]utils/zenutils/libraries/pelib-0.9/pelib/MzHeader.h296
-rw-r--r--[-rwxr-xr-x]utils/zenutils/libraries/pelib-0.9/pelib/PeFile.cpp338
-rw-r--r--[-rwxr-xr-x]utils/zenutils/libraries/pelib-0.9/pelib/PeFile.h902
-rw-r--r--[-rwxr-xr-x]utils/zenutils/libraries/pelib-0.9/pelib/PeHeader.cpp180
-rw-r--r--[-rwxr-xr-x]utils/zenutils/libraries/pelib-0.9/pelib/PeHeader.h5370
-rw-r--r--[-rwxr-xr-x]utils/zenutils/libraries/pelib-0.9/pelib/PeLib.h54
-rw-r--r--[-rwxr-xr-x]utils/zenutils/libraries/pelib-0.9/pelib/PeLibAux.cpp550
-rw-r--r--[-rwxr-xr-x]utils/zenutils/libraries/pelib-0.9/pelib/PeLibAux.h1768
-rw-r--r--[-rwxr-xr-x]utils/zenutils/libraries/pelib-0.9/pelib/PeLibInc.h64
-rw-r--r--[-rwxr-xr-x]utils/zenutils/libraries/pelib-0.9/pelib/RelocationsDirectory.cpp422
-rw-r--r--[-rwxr-xr-x]utils/zenutils/libraries/pelib-0.9/pelib/RelocationsDirectory.h140
-rw-r--r--[-rwxr-xr-x]utils/zenutils/libraries/pelib-0.9/pelib/ResourceDirectory.cpp2994
-rw-r--r--[-rwxr-xr-x]utils/zenutils/libraries/pelib-0.9/pelib/ResourceDirectory.h1470
-rw-r--r--[-rwxr-xr-x]utils/zenutils/libraries/pelib-0.9/pelib/TlsDirectory.h608
-rw-r--r--[-rwxr-xr-x]utils/zenutils/libraries/pelib-0.9/pelib/buffer/InputBuffer.cpp116
-rw-r--r--[-rwxr-xr-x]utils/zenutils/libraries/pelib-0.9/pelib/buffer/InputBuffer.h104
-rw-r--r--[-rwxr-xr-x]utils/zenutils/libraries/pelib-0.9/pelib/buffer/OutputBuffer.cpp82
-rw-r--r--[-rwxr-xr-x]utils/zenutils/libraries/pelib-0.9/pelib/buffer/OutputBuffer.h102
-rw-r--r--[-rwxr-xr-x]utils/zenutils/libraries/pelib-0.9/pelib/changelog.txt640
-rw-r--r--[-rwxr-xr-x]utils/zenutils/libraries/pelib-0.9/pelib/license.htm68
-rw-r--r--[-rwxr-xr-x]utils/zenutils/libraries/pelib-0.9/pelib/readme.txt86
34 files changed, 12634 insertions, 12634 deletions
diff --git a/utils/zenutils/libraries/pelib-0.9/CMakeLists.txt b/utils/zenutils/libraries/pelib-0.9/CMakeLists.txt
index 540fa99c8c..3cdb6e71ff 100755..100644
--- a/utils/zenutils/libraries/pelib-0.9/CMakeLists.txt
+++ b/utils/zenutils/libraries/pelib-0.9/CMakeLists.txt
@@ -1,20 +1,20 @@
-PROJECT(pelib)
-
-# source files for pelib
-SET(pelib_srcs
- pelib/BoundImportDirectory.cpp
- pelib/ComHeaderDirectory.cpp
- pelib/DebugDirectory.cpp
- pelib/ExportDirectory.cpp
- pelib/IatDirectory.cpp
- pelib/MzHeader.cpp
- pelib/PeFile.cpp
- pelib/PeHeader.cpp
- pelib/PeLibAux.cpp
- pelib/RelocationsDirectory.cpp
- pelib/ResourceDirectory.cpp
- pelib/buffer/InputBuffer.cpp
- pelib/buffer/OutputBuffer.cpp
-)
-
-ADD_LIBRARY(pelib ${pelib_srcs})
+PROJECT(pelib)
+
+# source files for pelib
+SET(pelib_srcs
+ pelib/BoundImportDirectory.cpp
+ pelib/ComHeaderDirectory.cpp
+ pelib/DebugDirectory.cpp
+ pelib/ExportDirectory.cpp
+ pelib/IatDirectory.cpp
+ pelib/MzHeader.cpp
+ pelib/PeFile.cpp
+ pelib/PeHeader.cpp
+ pelib/PeLibAux.cpp
+ pelib/RelocationsDirectory.cpp
+ pelib/ResourceDirectory.cpp
+ pelib/buffer/InputBuffer.cpp
+ pelib/buffer/OutputBuffer.cpp
+)
+
+ADD_LIBRARY(pelib ${pelib_srcs})
diff --git a/utils/zenutils/libraries/pelib-0.9/pelib/BoundImportDirectory.cpp b/utils/zenutils/libraries/pelib-0.9/pelib/BoundImportDirectory.cpp
index 5b84931838..7c5ff74f0e 100755..100644
--- a/utils/zenutils/libraries/pelib-0.9/pelib/BoundImportDirectory.cpp
+++ b/utils/zenutils/libraries/pelib-0.9/pelib/BoundImportDirectory.cpp
@@ -1,511 +1,511 @@
-/*
-* BoundImportDirectory.cpp - Part of the PeLib library.
-*
-* Copyright (c) 2004 - 2005 Sebastian Porst (webmaster@the-interweb.com)
-* All rights reserved.
-*
-* This software is licensed under the zlib/libpng License.
-* For more details see http://www.opensource.org/licenses/zlib-license.php
-* or the license information file (license.htm) in the root directory
-* of PeLib.
-*/
-
-#include "PeLibInc.h"
-#include "BoundImportDirectory.h"
-#include <numeric>
-#include <set>
-#include <map>
-
-namespace PeLib
-{
- /**
- * Adds another bound import to the BoundImport directory.
- * @param strModuleName Name of the PE file which will be imported.
- * @param dwTds Value of the TimeDateStamp of the bound import field.
- * @param wOmn Value of the OffsetModuleName of the bound import field.
- * @param wWfr Value of the NumberOfModuleForwarderRefs of the bound import field.
- **/
- int BoundImportDirectory::addBoundImport(const std::string& strModuleName, dword dwTds, word wOmn, word wWfr)
- {
- for (unsigned int i=0;i<m_vIbd.size();i++)
- {
- if (isEqualNc(strModuleName, m_vIbd[i].strModuleName))
- {
- return ERROR_DUPLICATE_ENTRY;
- }
- }
-
- PELIB_IMAGE_BOUND_IMPORT_DESCRIPTOR ibidCurrent;
- ibidCurrent.TimeDateStamp = dwTds;
- ibidCurrent.OffsetModuleName = wOmn;
- ibidCurrent.NumberOfModuleForwarderRefs = wWfr;
- PELIB_IMAGE_BOUND_DIRECTORY ibdCurrent;
- ibdCurrent.ibdDescriptor = ibidCurrent;
- ibdCurrent.strModuleName = strModuleName;
- m_vIbd.push_back(ibdCurrent);
-
- return NO_ERROR;
- }
-
- /**
- * Searches for the first instance of a module with the given modulename.
- * @param strModuleName The name of a module.
- * @return The id of the module.
- **/
- int BoundImportDirectory::getModuleIndex(const std::string& strModuleName) const
- {
- std::vector<PELIB_IMAGE_BOUND_DIRECTORY>::const_iterator Iter = std::find_if(m_vIbd.begin(), m_vIbd.end(), std::bind2nd(std::mem_fun_ref(&PELIB_IMAGE_BOUND_DIRECTORY::equal), strModuleName));
-
- if (Iter == m_vIbd.end())
- {
- return ERROR_ENTRY_NOT_FOUND;
- }
-
- return static_cast<int>(std::distance(m_vIbd.begin(), Iter));
- }
-
- /**
- * @return Number of files in the current BoundImport directory.
- **/
- unsigned int BoundImportDirectory::calcNumberOfModules() const
- {
- return static_cast<unsigned int>(m_vIbd.size());
- }
-
- int BoundImportDirectory::read(InputBuffer& inpBuffer, unsigned char* data, unsigned int dwSize)
- {
- std::vector<PELIB_IMAGE_BOUND_DIRECTORY> currentDirectory;
-
- do
- {
- PELIB_IMAGE_BOUND_DIRECTORY ibdCurrent;
-
- inpBuffer >> ibdCurrent.ibdDescriptor.TimeDateStamp;
- inpBuffer >> ibdCurrent.ibdDescriptor.OffsetModuleName;
- inpBuffer >> ibdCurrent.ibdDescriptor.NumberOfModuleForwarderRefs;
-
- if (ibdCurrent.ibdDescriptor.TimeDateStamp == 0 && ibdCurrent.ibdDescriptor.OffsetModuleName == 0 && ibdCurrent.ibdDescriptor.NumberOfModuleForwarderRefs == 0) break;
-
- for (int i=0;i<ibdCurrent.ibdDescriptor.NumberOfModuleForwarderRefs;i++)
- {
- PELIB_IMAGE_BOUND_DIRECTORY currentForwarder;
-
- inpBuffer >> currentForwarder.ibdDescriptor.TimeDateStamp;
- inpBuffer >> currentForwarder.ibdDescriptor.OffsetModuleName;
- inpBuffer >> currentForwarder.ibdDescriptor.NumberOfModuleForwarderRefs;
-
- ibdCurrent.moduleForwarders.push_back(currentForwarder);
- }
-
- currentDirectory.push_back(ibdCurrent);
- ibdCurrent.moduleForwarders.clear();
- } while (true);
-
- for (unsigned int i=0;i<currentDirectory.size();i++)
- {
- dword wOmn = currentDirectory[i].ibdDescriptor.OffsetModuleName;
- if (wOmn > dwSize)
- {
- return ERROR_INVALID_FILE;
- }
-
- currentDirectory[i].strModuleName = "";
- for (int k=0;data[wOmn + k] != 0 && k + wOmn < dwSize;k++)
- {
- currentDirectory[i].strModuleName += data[wOmn + k];
- }
-
- for (unsigned int j=0;j<currentDirectory[i].moduleForwarders.size();j++)
- {
- dword wOmn = currentDirectory[i].moduleForwarders[j].ibdDescriptor.OffsetModuleName;
-
- if (wOmn > dwSize)
- {
- return ERROR_INVALID_FILE;
- }
-
-// m_vIbd[i].moduleForwarders[j].strModuleName.assign((char*)(&vBimpDir[wOmn]));
- currentDirectory[i].moduleForwarders[j].strModuleName = "";
- for (int k=0;data[wOmn + k] != 0 && k + wOmn < dwSize;k++)
- {
- currentDirectory[i].moduleForwarders[j].strModuleName += data[wOmn + k];
- }
- }
- }
-
- std::swap(m_vIbd, currentDirectory);
-
- return NO_ERROR;
- }
-
- /**
- * Reads the BoundImport directory from a PE file.
- * @param strModuleName The name of the PE file from which the BoundImport directory is read.
- * @param dwOffset The file offset where the BoundImport directory can be found (see #PeFile::PeHeader::getIDBoundImportRVA).
- * @param dwSize The size of the BoundImport directory (see #PeFile::PeHeader::getIDBoundImportSize).
- **/
- int BoundImportDirectory::read(const std::string& strModuleName, dword dwOffset, unsigned int uiSize)
- {
- std::ifstream ifFile(strModuleName.c_str(), std::ios::binary);
-
- if (!ifFile)
- {
- return ERROR_OPENING_FILE;
- }
-
- if (fileSize(ifFile) < dwOffset + uiSize)
- {
- return ERROR_INVALID_FILE;
- }
-
- ifFile.seekg(dwOffset, std::ios::beg);
-
- std::vector<unsigned char> vBimpDir(uiSize);
- ifFile.read(reinterpret_cast<char*>(&vBimpDir[0]), uiSize);
-
- InputBuffer inpBuffer(vBimpDir);
-
- return read(inpBuffer, &vBimpDir[0], uiSize);
- }
-
- int BoundImportDirectory::read(unsigned char* pcBuffer, unsigned int uiSize)
- {
- std::vector<unsigned char> vBimpDir(pcBuffer, pcBuffer + uiSize);
- InputBuffer inpBuffer(vBimpDir);
-
- return read(inpBuffer, &vBimpDir[0], uiSize);
- }
-
- unsigned int BoundImportDirectory::totalModules() const
- {
- unsigned int modules = static_cast<unsigned int>(m_vIbd.size());
-
- for (unsigned int i=0;i<m_vIbd.size();i++)
- {
- modules += static_cast<unsigned int>(m_vIbd[i].moduleForwarders.size());
- }
-
- return modules;
- }
-
- /**
- * Rebuilds the BoundImport directory. The rebuilded BoundImport directory can then be
- * written back to a PE file.
- * @param vBuffer Buffer where the rebuilt BoundImport directory will be stored.
- * @param fMakeValid If this flag is true a valid directory will be produced.
- **/
- void BoundImportDirectory::rebuild(std::vector<byte>& vBuffer, bool fMakeValid) const
- {
- std::map<std::string, word> filename_offsets;
-
- OutputBuffer obBuffer(vBuffer);
-
- word ulNameOffset = static_cast<word>((totalModules() + 1) * PELIB_IMAGE_BOUND_IMPORT_DESCRIPTOR::size());
-
- for (unsigned int i=0;i<m_vIbd.size();i++)
- {
- obBuffer << m_vIbd[i].ibdDescriptor.TimeDateStamp;
-
- // Recalculate the offsets if a valid directory is wanted.
- if (fMakeValid)
- {
- if (filename_offsets.find(m_vIbd[i].strModuleName) == filename_offsets.end())
- {
- filename_offsets[m_vIbd[i].strModuleName] = ulNameOffset;
- obBuffer << ulNameOffset;
- ulNameOffset += static_cast<word>(m_vIbd[i].strModuleName.size() + 1);
- }
- else
- {
- obBuffer << filename_offsets[m_vIbd[i].strModuleName];
- }
- }
- else // Otherwise just copy the old values into the buffer.
- {
- obBuffer << m_vIbd[i].ibdDescriptor.OffsetModuleName;
- }
-
- obBuffer << m_vIbd[i].ibdDescriptor.NumberOfModuleForwarderRefs;
-
- for (int j=0;j<calcNumberOfModuleForwarderRefs(i);j++)
- {
- obBuffer << m_vIbd[i].moduleForwarders[j].ibdDescriptor.TimeDateStamp;
-
- if (fMakeValid)
- {
- if (filename_offsets.find(m_vIbd[i].strModuleName) == filename_offsets.end())
- {
- filename_offsets[m_vIbd[i].moduleForwarders[j].strModuleName] = ulNameOffset;
- obBuffer << ulNameOffset;
- ulNameOffset += static_cast<word>(m_vIbd[i].moduleForwarders[j].strModuleName.size() + 1);
- }
- else
- {
- obBuffer << filename_offsets[m_vIbd[i].moduleForwarders[j].strModuleName];
- }
- }
- else // Otherwise just copy the old values into the buffer.
- {
- obBuffer << m_vIbd[i].moduleForwarders[j].ibdDescriptor.OffsetModuleName;
- }
-
- obBuffer << m_vIbd[i].moduleForwarders[j].ibdDescriptor.NumberOfModuleForwarderRefs;
- }
- }
-
- obBuffer << static_cast<dword>(0);
- obBuffer << static_cast<word>(0);
- obBuffer << static_cast<word>(0);
-
- for (unsigned int i=0;i<m_vIbd.size();i++)
- {
- if (filename_offsets.find(m_vIbd[i].strModuleName) != filename_offsets.end())
- {
- obBuffer.add(getModuleName(i).c_str(), static_cast<unsigned long>(getModuleName(i).size() + 1));
- filename_offsets.erase(m_vIbd[i].strModuleName);
- }
-
- for (int j=0;j<calcNumberOfModuleForwarderRefs(i);j++)
- {
- if (filename_offsets.find(getModuleName(i, j)) != filename_offsets.end())
- {
- obBuffer.add(getModuleName(i, j).c_str(), static_cast<unsigned long>(getModuleName(i, j).size() + 1));
- filename_offsets.erase(getModuleName(i, j));
- }
- }
- }
- }
-
- /**
- * Removes all bound import files.
- **/
- void BoundImportDirectory::clear()
- {
- m_vIbd.clear();
- }
-
- /**
- * Removes a field specified by the parameter filename from the BoundImport directory.
- * @param strModuleName Name of the file whose field will be removed from the BoundImport directory.
- **/
- void BoundImportDirectory::removeBoundImport(const std::string& strModuleName)
- {
- m_vIbd.erase(std::remove_if(m_vIbd.begin(), m_vIbd.end(), std::bind2nd(std::mem_fun_ref(&PELIB_IMAGE_BOUND_DIRECTORY::equal), strModuleName)), m_vIbd.end());
- }
-
- /**
- * Returns the size of the rebuilt BoundImportDirectory.
- * @return Size of the rebuilt BoundImportDirectory.
- **/
- unsigned int BoundImportDirectory::size() const
- {
- unsigned int size = PELIB_IMAGE_BOUND_IMPORT_DESCRIPTOR::size();
-
- std::set<std::string> filenames;
-
- for (unsigned int i = 0; i < m_vIbd.size(); i++)
- {
- filenames.insert(m_vIbd[i].strModuleName);
-
- size += PELIB_IMAGE_BOUND_IMPORT_DESCRIPTOR::size();
-
- for (unsigned int j = 0; j < m_vIbd[i].moduleForwarders.size(); j++)
- {
- filenames.insert(m_vIbd[i].moduleForwarders[j].strModuleName);
-
- size += PELIB_IMAGE_BOUND_IMPORT_DESCRIPTOR::size();
- }
- }
-
- for (std::set<std::string>::iterator iter = filenames.begin(); iter != filenames.end(); ++iter)
- {
- size += static_cast<unsigned int>(iter->size()) + 1;
- }
-
- return size;
- }
-
- /**
- * @param strFilename Name of the file.
- * @param dwOffset File offset the bound importdirectory will be written to.
- * @param fMakeValid If this flag is true a valid directory will be produced.
- **/
- int BoundImportDirectory::write(const std::string& strFilename, dword dwOffset, bool fMakeValid) const
- {
- std::fstream ofFile(strFilename.c_str(), std::ios_base::in);
-
- if (!ofFile)
- {
- ofFile.clear();
- ofFile.open(strFilename.c_str(), std::ios_base::out | std::ios_base::binary);
- }
- else
- {
- ofFile.close();
- ofFile.open(strFilename.c_str(), std::ios_base::in | std::ios_base::out | std::ios_base::binary);
- }
-
- if (!ofFile)
- {
- return ERROR_OPENING_FILE;
- }
-
- ofFile.seekp(dwOffset, std::ios::beg);
-
- std::vector<unsigned char> vBuffer;
- rebuild(vBuffer, fMakeValid);
-
- ofFile.write(reinterpret_cast<const char*>(&vBuffer[0]), static_cast<std::streamsize>(vBuffer.size()));
-
- ofFile.close();
-
- return NO_ERROR;
- }
-
- /**
- * Retrieves the value of the TimeDateStamp value of a bound import field.
- * @param dwBidnr Number of the bound import field.
- * @return Value of the TimeDateStamp of the bound import field.
- **/
- dword BoundImportDirectory::getTimeDateStamp(dword dwBidnr) const
- {
- return m_vIbd[dwBidnr].ibdDescriptor.TimeDateStamp;
- }
-
- /**
- * Retrieves the value of the OffsetModuleName value of a bound import field.
- * @param dwBidnr Number of the bound import field.
- * @return Value of the OffsetModuleName of the bound import field.
- **/
- word BoundImportDirectory::getOffsetModuleName(dword dwBidnr) const
- {
- return m_vIbd[dwBidnr].ibdDescriptor.OffsetModuleName;
- }
-
- /**
- * Retrieves the value of the NumberOfModuleForwarderRefs value of a bound import field.
- * @param dwBidnr Number of the bound import field.
- * @return Value of the NumberOfModuleForwarderRefs of the bound import field.
- **/
- word BoundImportDirectory::getNumberOfModuleForwarderRefs(dword dwBidnr) const
- {
- return m_vIbd[dwBidnr].ibdDescriptor.NumberOfModuleForwarderRefs;
- }
-
- /**
- * Retrieves the value of the ModuleName value of a bound import field.
- * @param dwBidnr Number of the bound import field.
- * @return Value of the ModuleName of the bound import field.
- **/
- std::string BoundImportDirectory::getModuleName(dword dwBidnr) const
- {
- return m_vIbd[dwBidnr].strModuleName;
- }
-
- /**
- * Changes the TimeDateStamp value of an existing bound import field.
- * @param dwBidnr Number of the bound import field which will be changed.
- * @param dwTds New value of the TimeDateStamp of the bound import field.
- **/
- void BoundImportDirectory::setTimeDateStamp(dword dwBidnr, dword dwTds)
- {
- m_vIbd[dwBidnr].ibdDescriptor.TimeDateStamp = dwTds;
- }
-
- /**
- * Changes the OffsetModuleName value of an existing bound import field.
- * @param dwBidnr Number of the bound import field which will be changed.
- * @param wOmn New value of the OffsetModuleName of the bound import field.
- **/
- void BoundImportDirectory::setOffsetModuleName(dword dwBidnr, word wOmn)
- {
- m_vIbd[dwBidnr].ibdDescriptor.OffsetModuleName = wOmn;
- }
-
- /**
- * Changes the NumberOfModuleForwarderRefs value of an existing bound import field.
- * @param dwBidnr Number of the bound import field which will be changed.
- * @param wMfr New value of the NumberOfModuleForwarderRefs of the bound import field.
- **/
- void BoundImportDirectory::setNumberOfModuleForwarderRefs(dword dwBidnr, word wMfr)
- {
- m_vIbd[dwBidnr].ibdDescriptor.NumberOfModuleForwarderRefs = wMfr;
- }
-
- /**
- * Changes the ModuleName value of an existing bound import field.
- * @param dwBidnr Number of the bound import field which will be changed.
- * @param strModuleName New value of the ModuleName of the bound import field.
- **/
- void BoundImportDirectory::setModuleName(dword dwBidnr, const std::string& strModuleName)
- {
- m_vIbd[dwBidnr].strModuleName = strModuleName;
- }
-
- dword BoundImportDirectory::getTimeDateStamp(dword dwBidnr, dword forwardedModule) const
- {
- return m_vIbd[dwBidnr].moduleForwarders[forwardedModule].ibdDescriptor.TimeDateStamp;
- }
-
- word BoundImportDirectory::getOffsetModuleName(dword dwBidnr, dword forwardedModule) const
- {
- return m_vIbd[dwBidnr].moduleForwarders[forwardedModule].ibdDescriptor.OffsetModuleName;
- }
-
- word BoundImportDirectory::getNumberOfModuleForwarderRefs(dword dwBidnr, dword forwardedModule) const
- {
- return m_vIbd[dwBidnr].moduleForwarders[forwardedModule].ibdDescriptor.NumberOfModuleForwarderRefs;
- }
-
- std::string BoundImportDirectory::getModuleName(dword dwBidnr, dword forwardedModule) const
- {
- return m_vIbd[dwBidnr].moduleForwarders[forwardedModule].strModuleName;
- }
-
- void BoundImportDirectory::setTimeDateStamp(dword dwBidnr, dword forwardedModule, dword dwTds)
- {
- m_vIbd[dwBidnr].moduleForwarders[forwardedModule].ibdDescriptor.TimeDateStamp = dwTds;
- }
-
- void BoundImportDirectory::setOffsetModuleName(dword dwBidnr, dword forwardedModule, word wOmn)
- {
- m_vIbd[dwBidnr].moduleForwarders[forwardedModule].ibdDescriptor.OffsetModuleName = wOmn;
- }
-
- void BoundImportDirectory::setNumberOfModuleForwarderRefs(dword dwBidnr, dword forwardedModule, word wMfr)
- {
- m_vIbd[dwBidnr].moduleForwarders[forwardedModule].ibdDescriptor.NumberOfModuleForwarderRefs = wMfr;
- }
-
- void BoundImportDirectory::setModuleName(dword dwBidnr, dword forwardedModule, const std::string& strModuleName)
- {
- m_vIbd[dwBidnr].moduleForwarders[forwardedModule].strModuleName = strModuleName;
- }
-
- word BoundImportDirectory::calcNumberOfModuleForwarderRefs(dword dwBidnr) const
- {
- return static_cast<word>(m_vIbd[dwBidnr].moduleForwarders.size());
- }
-
- void BoundImportDirectory::addForwardedModule(dword dwBidnr, const std::string& name, dword timeStamp, word offsetModuleName, word forwardedModules)
- {
- // XXX: Maybe test if there are already 0xFFFF forwarded modules.
- // XXX: Check for duplicate entries. Is it also necessary to check
- // non-forwarded entries and forwarded entries in other non-forwarded
- // entries?
- // XXX: Can forwarders forward recursively?
-
- PELIB_IMAGE_BOUND_DIRECTORY ibdCurrent;
- ibdCurrent.strModuleName = name;
- ibdCurrent.ibdDescriptor.TimeDateStamp = timeStamp;
- ibdCurrent.ibdDescriptor.OffsetModuleName = offsetModuleName;
- ibdCurrent.ibdDescriptor.NumberOfModuleForwarderRefs = forwardedModules;
-
- m_vIbd[dwBidnr].moduleForwarders.push_back(ibdCurrent);
- }
-
- void BoundImportDirectory::removeForwardedModule(dword dwBidnr, word forwardedModule)
- {
- m_vIbd[dwBidnr].moduleForwarders.erase(m_vIbd[dwBidnr].moduleForwarders.begin() + forwardedModule);
- }
-}
+/*
+* BoundImportDirectory.cpp - Part of the PeLib library.
+*
+* Copyright (c) 2004 - 2005 Sebastian Porst (webmaster@the-interweb.com)
+* All rights reserved.
+*
+* This software is licensed under the zlib/libpng License.
+* For more details see http://www.opensource.org/licenses/zlib-license.php
+* or the license information file (license.htm) in the root directory
+* of PeLib.
+*/
+
+#include "PeLibInc.h"
+#include "BoundImportDirectory.h"
+#include <numeric>
+#include <set>
+#include <map>
+
+namespace PeLib
+{
+ /**
+ * Adds another bound import to the BoundImport directory.
+ * @param strModuleName Name of the PE file which will be imported.
+ * @param dwTds Value of the TimeDateStamp of the bound import field.
+ * @param wOmn Value of the OffsetModuleName of the bound import field.
+ * @param wWfr Value of the NumberOfModuleForwarderRefs of the bound import field.
+ **/
+ int BoundImportDirectory::addBoundImport(const std::string& strModuleName, dword dwTds, word wOmn, word wWfr)
+ {
+ for (unsigned int i=0;i<m_vIbd.size();i++)
+ {
+ if (isEqualNc(strModuleName, m_vIbd[i].strModuleName))
+ {
+ return ERROR_DUPLICATE_ENTRY;
+ }
+ }
+
+ PELIB_IMAGE_BOUND_IMPORT_DESCRIPTOR ibidCurrent;
+ ibidCurrent.TimeDateStamp = dwTds;
+ ibidCurrent.OffsetModuleName = wOmn;
+ ibidCurrent.NumberOfModuleForwarderRefs = wWfr;
+ PELIB_IMAGE_BOUND_DIRECTORY ibdCurrent;
+ ibdCurrent.ibdDescriptor = ibidCurrent;
+ ibdCurrent.strModuleName = strModuleName;
+ m_vIbd.push_back(ibdCurrent);
+
+ return NO_ERROR;
+ }
+
+ /**
+ * Searches for the first instance of a module with the given modulename.
+ * @param strModuleName The name of a module.
+ * @return The id of the module.
+ **/
+ int BoundImportDirectory::getModuleIndex(const std::string& strModuleName) const
+ {
+ std::vector<PELIB_IMAGE_BOUND_DIRECTORY>::const_iterator Iter = std::find_if(m_vIbd.begin(), m_vIbd.end(), std::bind2nd(std::mem_fun_ref(&PELIB_IMAGE_BOUND_DIRECTORY::equal), strModuleName));
+
+ if (Iter == m_vIbd.end())
+ {
+ return ERROR_ENTRY_NOT_FOUND;
+ }
+
+ return static_cast<int>(std::distance(m_vIbd.begin(), Iter));
+ }
+
+ /**
+ * @return Number of files in the current BoundImport directory.
+ **/
+ unsigned int BoundImportDirectory::calcNumberOfModules() const
+ {
+ return static_cast<unsigned int>(m_vIbd.size());
+ }
+
+ int BoundImportDirectory::read(InputBuffer& inpBuffer, unsigned char* data, unsigned int dwSize)
+ {
+ std::vector<PELIB_IMAGE_BOUND_DIRECTORY> currentDirectory;
+
+ do
+ {
+ PELIB_IMAGE_BOUND_DIRECTORY ibdCurrent;
+
+ inpBuffer >> ibdCurrent.ibdDescriptor.TimeDateStamp;
+ inpBuffer >> ibdCurrent.ibdDescriptor.OffsetModuleName;
+ inpBuffer >> ibdCurrent.ibdDescriptor.NumberOfModuleForwarderRefs;
+
+ if (ibdCurrent.ibdDescriptor.TimeDateStamp == 0 && ibdCurrent.ibdDescriptor.OffsetModuleName == 0 && ibdCurrent.ibdDescriptor.NumberOfModuleForwarderRefs == 0) break;
+
+ for (int i=0;i<ibdCurrent.ibdDescriptor.NumberOfModuleForwarderRefs;i++)
+ {
+ PELIB_IMAGE_BOUND_DIRECTORY currentForwarder;
+
+ inpBuffer >> currentForwarder.ibdDescriptor.TimeDateStamp;
+ inpBuffer >> currentForwarder.ibdDescriptor.OffsetModuleName;
+ inpBuffer >> currentForwarder.ibdDescriptor.NumberOfModuleForwarderRefs;
+
+ ibdCurrent.moduleForwarders.push_back(currentForwarder);
+ }
+
+ currentDirectory.push_back(ibdCurrent);
+ ibdCurrent.moduleForwarders.clear();
+ } while (true);
+
+ for (unsigned int i=0;i<currentDirectory.size();i++)
+ {
+ dword wOmn = currentDirectory[i].ibdDescriptor.OffsetModuleName;
+ if (wOmn > dwSize)
+ {
+ return ERROR_INVALID_FILE;
+ }
+
+ currentDirectory[i].strModuleName = "";
+ for (int k=0;data[wOmn + k] != 0 && k + wOmn < dwSize;k++)
+ {
+ currentDirectory[i].strModuleName += data[wOmn + k];
+ }
+
+ for (unsigned int j=0;j<currentDirectory[i].moduleForwarders.size();j++)
+ {
+ dword wOmn = currentDirectory[i].moduleForwarders[j].ibdDescriptor.OffsetModuleName;
+
+ if (wOmn > dwSize)
+ {
+ return ERROR_INVALID_FILE;
+ }
+
+// m_vIbd[i].moduleForwarders[j].strModuleName.assign((char*)(&vBimpDir[wOmn]));
+ currentDirectory[i].moduleForwarders[j].strModuleName = "";
+ for (int k=0;data[wOmn + k] != 0 && k + wOmn < dwSize;k++)
+ {
+ currentDirectory[i].moduleForwarders[j].strModuleName += data[wOmn + k];
+ }
+ }
+ }
+
+ std::swap(m_vIbd, currentDirectory);
+
+ return NO_ERROR;
+ }
+
+ /**
+ * Reads the BoundImport directory from a PE file.
+ * @param strModuleName The name of the PE file from which the BoundImport directory is read.
+ * @param dwOffset The file offset where the BoundImport directory can be found (see #PeFile::PeHeader::getIDBoundImportRVA).
+ * @param dwSize The size of the BoundImport directory (see #PeFile::PeHeader::getIDBoundImportSize).
+ **/
+ int BoundImportDirectory::read(const std::string& strModuleName, dword dwOffset, unsigned int uiSize)
+ {
+ std::ifstream ifFile(strModuleName.c_str(), std::ios::binary);
+
+ if (!ifFile)
+ {
+ return ERROR_OPENING_FILE;
+ }
+
+ if (fileSize(ifFile) < dwOffset + uiSize)
+ {
+ return ERROR_INVALID_FILE;
+ }
+
+ ifFile.seekg(dwOffset, std::ios::beg);
+
+ std::vector<unsigned char> vBimpDir(uiSize);
+ ifFile.read(reinterpret_cast<char*>(&vBimpDir[0]), uiSize);
+
+ InputBuffer inpBuffer(vBimpDir);
+
+ return read(inpBuffer, &vBimpDir[0], uiSize);
+ }
+
+ int BoundImportDirectory::read(unsigned char* pcBuffer, unsigned int uiSize)
+ {
+ std::vector<unsigned char> vBimpDir(pcBuffer, pcBuffer + uiSize);
+ InputBuffer inpBuffer(vBimpDir);
+
+ return read(inpBuffer, &vBimpDir[0], uiSize);
+ }
+
+ unsigned int BoundImportDirectory::totalModules() const
+ {
+ unsigned int modules = static_cast<unsigned int>(m_vIbd.size());
+
+ for (unsigned int i=0;i<m_vIbd.size();i++)
+ {
+ modules += static_cast<unsigned int>(m_vIbd[i].moduleForwarders.size());
+ }
+
+ return modules;
+ }
+
+ /**
+ * Rebuilds the BoundImport directory. The rebuilded BoundImport directory can then be
+ * written back to a PE file.
+ * @param vBuffer Buffer where the rebuilt BoundImport directory will be stored.
+ * @param fMakeValid If this flag is true a valid directory will be produced.
+ **/
+ void BoundImportDirectory::rebuild(std::vector<byte>& vBuffer, bool fMakeValid) const
+ {
+ std::map<std::string, word> filename_offsets;
+
+ OutputBuffer obBuffer(vBuffer);
+
+ word ulNameOffset = static_cast<word>((totalModules() + 1) * PELIB_IMAGE_BOUND_IMPORT_DESCRIPTOR::size());
+
+ for (unsigned int i=0;i<m_vIbd.size();i++)
+ {
+ obBuffer << m_vIbd[i].ibdDescriptor.TimeDateStamp;
+
+ // Recalculate the offsets if a valid directory is wanted.
+ if (fMakeValid)
+ {
+ if (filename_offsets.find(m_vIbd[i].strModuleName) == filename_offsets.end())
+ {
+ filename_offsets[m_vIbd[i].strModuleName] = ulNameOffset;
+ obBuffer << ulNameOffset;
+ ulNameOffset += static_cast<word>(m_vIbd[i].strModuleName.size() + 1);
+ }
+ else
+ {
+ obBuffer << filename_offsets[m_vIbd[i].strModuleName];
+ }
+ }
+ else // Otherwise just copy the old values into the buffer.
+ {
+ obBuffer << m_vIbd[i].ibdDescriptor.OffsetModuleName;
+ }
+
+ obBuffer << m_vIbd[i].ibdDescriptor.NumberOfModuleForwarderRefs;
+
+ for (int j=0;j<calcNumberOfModuleForwarderRefs(i);j++)
+ {
+ obBuffer << m_vIbd[i].moduleForwarders[j].ibdDescriptor.TimeDateStamp;
+
+ if (fMakeValid)
+ {
+ if (filename_offsets.find(m_vIbd[i].strModuleName) == filename_offsets.end())
+ {
+ filename_offsets[m_vIbd[i].moduleForwarders[j].strModuleName] = ulNameOffset;
+ obBuffer << ulNameOffset;
+ ulNameOffset += static_cast<word>(m_vIbd[i].moduleForwarders[j].strModuleName.size() + 1);
+ }
+ else
+ {
+ obBuffer << filename_offsets[m_vIbd[i].moduleForwarders[j].strModuleName];
+ }
+ }
+ else // Otherwise just copy the old values into the buffer.
+ {
+ obBuffer << m_vIbd[i].moduleForwarders[j].ibdDescriptor.OffsetModuleName;
+ }
+
+ obBuffer << m_vIbd[i].moduleForwarders[j].ibdDescriptor.NumberOfModuleForwarderRefs;
+ }
+ }
+
+ obBuffer << static_cast<dword>(0);
+ obBuffer << static_cast<word>(0);
+ obBuffer << static_cast<word>(0);
+
+ for (unsigned int i=0;i<m_vIbd.size();i++)
+ {
+ if (filename_offsets.find(m_vIbd[i].strModuleName) != filename_offsets.end())
+ {
+ obBuffer.add(getModuleName(i).c_str(), static_cast<unsigned long>(getModuleName(i).size() + 1));
+ filename_offsets.erase(m_vIbd[i].strModuleName);
+ }
+
+ for (int j=0;j<calcNumberOfModuleForwarderRefs(i);j++)
+ {
+ if (filename_offsets.find(getModuleName(i, j)) != filename_offsets.end())
+ {
+ obBuffer.add(getModuleName(i, j).c_str(), static_cast<unsigned long>(getModuleName(i, j).size() + 1));
+ filename_offsets.erase(getModuleName(i, j));
+ }
+ }
+ }
+ }
+
+ /**
+ * Removes all bound import files.
+ **/
+ void BoundImportDirectory::clear()
+ {
+ m_vIbd.clear();
+ }
+
+ /**
+ * Removes a field specified by the parameter filename from the BoundImport directory.
+ * @param strModuleName Name of the file whose field will be removed from the BoundImport directory.
+ **/
+ void BoundImportDirectory::removeBoundImport(const std::string& strModuleName)
+ {
+ m_vIbd.erase(std::remove_if(m_vIbd.begin(), m_vIbd.end(), std::bind2nd(std::mem_fun_ref(&PELIB_IMAGE_BOUND_DIRECTORY::equal), strModuleName)), m_vIbd.end());
+ }
+
+ /**
+ * Returns the size of the rebuilt BoundImportDirectory.
+ * @return Size of the rebuilt BoundImportDirectory.
+ **/
+ unsigned int BoundImportDirectory::size() const
+ {
+ unsigned int size = PELIB_IMAGE_BOUND_IMPORT_DESCRIPTOR::size();
+
+ std::set<std::string> filenames;
+
+ for (unsigned int i = 0; i < m_vIbd.size(); i++)
+ {
+ filenames.insert(m_vIbd[i].strModuleName);
+
+ size += PELIB_IMAGE_BOUND_IMPORT_DESCRIPTOR::size();
+
+ for (unsigned int j = 0; j < m_vIbd[i].moduleForwarders.size(); j++)
+ {
+ filenames.insert(m_vIbd[i].moduleForwarders[j].strModuleName);
+
+ size += PELIB_IMAGE_BOUND_IMPORT_DESCRIPTOR::size();
+ }
+ }
+
+ for (std::set<std::string>::iterator iter = filenames.begin(); iter != filenames.end(); ++iter)
+ {
+ size += static_cast<unsigned int>(iter->size()) + 1;
+ }
+
+ return size;
+ }
+
+ /**
+ * @param strFilename Name of the file.
+ * @param dwOffset File offset the bound importdirectory will be written to.
+ * @param fMakeValid If this flag is true a valid directory will be produced.
+ **/
+ int BoundImportDirectory::write(const std::string& strFilename, dword dwOffset, bool fMakeValid) const
+ {
+ std::fstream ofFile(strFilename.c_str(), std::ios_base::in);
+
+ if (!ofFile)
+ {
+ ofFile.clear();
+ ofFile.open(strFilename.c_str(), std::ios_base::out | std::ios_base::binary);
+ }
+ else
+ {
+ ofFile.close();
+ ofFile.open(strFilename.c_str(), std::ios_base::in | std::ios_base::out | std::ios_base::binary);
+ }
+
+ if (!ofFile)
+ {
+ return ERROR_OPENING_FILE;
+ }
+
+ ofFile.seekp(dwOffset, std::ios::beg);
+
+ std::vector<unsigned char> vBuffer;
+ rebuild(vBuffer, fMakeValid);
+
+ ofFile.write(reinterpret_cast<const char*>(&vBuffer[0]), static_cast<std::streamsize>(vBuffer.size()));
+
+ ofFile.close();
+
+ return NO_ERROR;
+ }
+
+ /**
+ * Retrieves the value of the TimeDateStamp value of a bound import field.
+ * @param dwBidnr Number of the bound import field.
+ * @return Value of the TimeDateStamp of the bound import field.
+ **/
+ dword BoundImportDirectory::getTimeDateStamp(dword dwBidnr) const
+ {
+ return m_vIbd[dwBidnr].ibdDescriptor.TimeDateStamp;
+ }
+
+ /**
+ * Retrieves the value of the OffsetModuleName value of a bound import field.
+ * @param dwBidnr Number of the bound import field.
+ * @return Value of the OffsetModuleName of the bound import field.
+ **/
+ word BoundImportDirectory::getOffsetModuleName(dword dwBidnr) const
+ {
+ return m_vIbd[dwBidnr].ibdDescriptor.OffsetModuleName;
+ }
+
+ /**
+ * Retrieves the value of the NumberOfModuleForwarderRefs value of a bound import field.
+ * @param dwBidnr Number of the bound import field.
+ * @return Value of the NumberOfModuleForwarderRefs of the bound import field.
+ **/
+ word BoundImportDirectory::getNumberOfModuleForwarderRefs(dword dwBidnr) const
+ {
+ return m_vIbd[dwBidnr].ibdDescriptor.NumberOfModuleForwarderRefs;
+ }
+
+ /**
+ * Retrieves the value of the ModuleName value of a bound import field.
+ * @param dwBidnr Number of the bound import field.
+ * @return Value of the ModuleName of the bound import field.
+ **/
+ std::string BoundImportDirectory::getModuleName(dword dwBidnr) const
+ {
+ return m_vIbd[dwBidnr].strModuleName;
+ }
+
+ /**
+ * Changes the TimeDateStamp value of an existing bound import field.
+ * @param dwBidnr Number of the bound import field which will be changed.
+ * @param dwTds New value of the TimeDateStamp of the bound import field.
+ **/
+ void BoundImportDirectory::setTimeDateStamp(dword dwBidnr, dword dwTds)
+ {
+ m_vIbd[dwBidnr].ibdDescriptor.TimeDateStamp = dwTds;
+ }
+
+ /**
+ * Changes the OffsetModuleName value of an existing bound import field.
+ * @param dwBidnr Number of the bound import field which will be changed.
+ * @param wOmn New value of the OffsetModuleName of the bound import field.
+ **/
+ void BoundImportDirectory::setOffsetModuleName(dword dwBidnr, word wOmn)
+ {
+ m_vIbd[dwBidnr].ibdDescriptor.OffsetModuleName = wOmn;
+ }
+
+ /**
+ * Changes the NumberOfModuleForwarderRefs value of an existing bound import field.
+ * @param dwBidnr Number of the bound import field which will be changed.
+ * @param wMfr New value of the NumberOfModuleForwarderRefs of the bound import field.
+ **/
+ void BoundImportDirectory::setNumberOfModuleForwarderRefs(dword dwBidnr, word wMfr)
+ {
+ m_vIbd[dwBidnr].ibdDescriptor.NumberOfModuleForwarderRefs = wMfr;
+ }
+
+ /**
+ * Changes the ModuleName value of an existing bound import field.
+ * @param dwBidnr Number of the bound import field which will be changed.
+ * @param strModuleName New value of the ModuleName of the bound import field.
+ **/
+ void BoundImportDirectory::setModuleName(dword dwBidnr, const std::string& strModuleName)
+ {
+ m_vIbd[dwBidnr].strModuleName = strModuleName;
+ }
+
+ dword BoundImportDirectory::getTimeDateStamp(dword dwBidnr, dword forwardedModule) const
+ {
+ return m_vIbd[dwBidnr].moduleForwarders[forwardedModule].ibdDescriptor.TimeDateStamp;
+ }
+
+ word BoundImportDirectory::getOffsetModuleName(dword dwBidnr, dword forwardedModule) const
+ {
+ return m_vIbd[dwBidnr].moduleForwarders[forwardedModule].ibdDescriptor.OffsetModuleName;
+ }
+
+ word BoundImportDirectory::getNumberOfModuleForwarderRefs(dword dwBidnr, dword forwardedModule) const
+ {
+ return m_vIbd[dwBidnr].moduleForwarders[forwardedModule].ibdDescriptor.NumberOfModuleForwarderRefs;
+ }
+
+ std::string BoundImportDirectory::getModuleName(dword dwBidnr, dword forwardedModule) const
+ {
+ return m_vIbd[dwBidnr].moduleForwarders[forwardedModule].strModuleName;
+ }
+
+ void BoundImportDirectory::setTimeDateStamp(dword dwBidnr, dword forwardedModule, dword dwTds)
+ {
+ m_vIbd[dwBidnr].moduleForwarders[forwardedModule].ibdDescriptor.TimeDateStamp = dwTds;
+ }
+
+ void BoundImportDirectory::setOffsetModuleName(dword dwBidnr, dword forwardedModule, word wOmn)
+ {
+ m_vIbd[dwBidnr].moduleForwarders[forwardedModule].ibdDescriptor.OffsetModuleName = wOmn;
+ }
+
+ void BoundImportDirectory::setNumberOfModuleForwarderRefs(dword dwBidnr, dword forwardedModule, word wMfr)
+ {
+ m_vIbd[dwBidnr].moduleForwarders[forwardedModule].ibdDescriptor.NumberOfModuleForwarderRefs = wMfr;
+ }
+
+ void BoundImportDirectory::setModuleName(dword dwBidnr, dword forwardedModule, const std::string& strModuleName)
+ {
+ m_vIbd[dwBidnr].moduleForwarders[forwardedModule].strModuleName = strModuleName;
+ }
+
+ word BoundImportDirectory::calcNumberOfModuleForwarderRefs(dword dwBidnr) const
+ {
+ return static_cast<word>(m_vIbd[dwBidnr].moduleForwarders.size());
+ }
+
+ void BoundImportDirectory::addForwardedModule(dword dwBidnr, const std::string& name, dword timeStamp, word offsetModuleName, word forwardedModules)
+ {
+ // XXX: Maybe test if there are already 0xFFFF forwarded modules.
+ // XXX: Check for duplicate entries. Is it also necessary to check
+ // non-forwarded entries and forwarded entries in other non-forwarded
+ // entries?
+ // XXX: Can forwarders forward recursively?
+
+ PELIB_IMAGE_BOUND_DIRECTORY ibdCurrent;
+ ibdCurrent.strModuleName = name;
+ ibdCurrent.ibdDescriptor.TimeDateStamp = timeStamp;
+ ibdCurrent.ibdDescriptor.OffsetModuleName = offsetModuleName;
+ ibdCurrent.ibdDescriptor.NumberOfModuleForwarderRefs = forwardedModules;
+
+ m_vIbd[dwBidnr].moduleForwarders.push_back(ibdCurrent);
+ }
+
+ void BoundImportDirectory::removeForwardedModule(dword dwBidnr, word forwardedModule)
+ {
+ m_vIbd[dwBidnr].moduleForwarders.erase(m_vIbd[dwBidnr].moduleForwarders.begin() + forwardedModule);
+ }
+}
diff --git a/utils/zenutils/libraries/pelib-0.9/pelib/BoundImportDirectory.h b/utils/zenutils/libraries/pelib-0.9/pelib/BoundImportDirectory.h
index fd2bab56fd..142d78f710 100755..100644
--- a/utils/zenutils/libraries/pelib-0.9/pelib/BoundImportDirectory.h
+++ b/utils/zenutils/libraries/pelib-0.9/pelib/BoundImportDirectory.h
@@ -1,87 +1,87 @@
-/*
-* BoundImportDirectory.h - Part of the PeLib library.
-*
-* Copyright (c) 2004 - 2005 Sebastian Porst (webmaster@the-interweb.com)
-* All rights reserved.
-*
-* This software is licensed under the zlib/libpng License.
-* For more details see http://www.opensource.org/licenses/zlib-license.php
-* or the license information file (license.htm) in the root directory
-* of PeLib.
-*/
-
-#ifndef BOUNDIMPORTDIRECTORY_H
-#define BOUNDIMPORTDIRECTORY_H
-
-#include "PeLibAux.h"
-
-namespace PeLib
-{
- /// Class that handles the BoundImport directory.
- /**
- * This class can read and modify the BoundImport directory table of a PE file.
- **/
- class BoundImportDirectory
- {
- private:
- std::vector<PELIB_IMAGE_BOUND_DIRECTORY> m_vIbd; ///< Stores the individual BoundImport fields.
-
- int read(InputBuffer& inpBuffer, unsigned char* data, unsigned int dwSize);
- unsigned int totalModules() const;
- public:
- /// Adds another bound import.
- int addBoundImport(const std::string& strModuleName, dword dwTds, word dwOmn, word wWfr); // EXPORT
- /// Identifies a module through it's name.
- int getModuleIndex(const std::string& strModuleName) const; // EXPORT
- /// Returns the number of files in the BoundImport directory.
- unsigned int calcNumberOfModules() const; // EXPORT
- /// Reads the BoundImport directory table from a PE file.
- int read(const std::string& strFileName, dword dwOffset, unsigned int uiSize); // EXPORT
- int read(unsigned char* pcBuffer, unsigned int uiSize); // EXPORT
- /// Rebuilds the BoundImport directory.
- void rebuild(std::vector<byte>& vBuffer, bool fMakeValid = true) const; // EXPORT
- /// Empties the BoundImport directory.
- void clear(); // EXPORT
- /// Removes a bound import.
- void removeBoundImport(const std::string& strModuleName); // EXPORT
- /// Returns the size of the BoundImport directory.
- unsigned int size() const; // EXPORT
- /// Writes the current bound import directory to a file.
- int write(const std::string& strFilename, dword dwOffset, bool fMakeValid = true) const; // EXPORT
-
- /// Retrieves the TimeDateStamp value of a bound import.
- dword getTimeDateStamp(dword dwBidnr) const; // EXPORT
- /// Retrieves the OffsetModuleName value of a bound import.
- word getOffsetModuleName(dword dwBidnr) const; // EXPORT
- /// Retrieves the NumberOfModuleForwarderRefs value of a bound import.
- word getNumberOfModuleForwarderRefs(dword dwBidnr) const; // EXPORT
- /// Retrieves the ModuleName value of a bound import.
- std::string getModuleName(dword dwBidnr) const; // EXPORT
-
- /// Updates the TimeDateStamp value of a bound import.
- void setTimeDateStamp(dword dwBidnr, dword dwTds); // EXPORT
- /// Updates the OffsetModuleName value of a bound import.
- void setOffsetModuleName(dword dwBidnr, word wOmn); // EXPORT
- /// Updates the NumberOfModuleForwarderRefs value of a bound import.
- void setNumberOfModuleForwarderRefs(dword dwBidnr, word wMfr); // EXPORT
- /// Updates the ModuleName value of a bound import.
- void setModuleName(dword dwBidnr, const std::string& strModuleName); // EXPORT
-
- dword getTimeDateStamp(dword dwBidnr, dword forwardedModule) const; // EXPORT _module
- word getOffsetModuleName(dword dwBidnr, dword forwardedModule) const; // EXPORT _module
- word getNumberOfModuleForwarderRefs(dword dwBidnr, dword forwardedModule) const; // EXPORT _module
- std::string getModuleName(dword dwBidnr, dword forwardedModule) const; // EXPORT _module
-
- void setTimeDateStamp(dword dwBidnr, dword forwardedModule, dword dwTds); // EXPORT _module
- void setOffsetModuleName(dword dwBidnr, dword forwardedModule, word wOmn); // EXPORT _module
- void setNumberOfModuleForwarderRefs(dword dwBidnr, dword forwardedModule, word wMfr); // EXPORT _module
- void setModuleName(dword dwBidnr, dword forwardedModule, const std::string& strModuleName); // EXPORT _module
-
- word calcNumberOfModuleForwarderRefs(dword dwBidnr) const; // EXPORT
- void addForwardedModule(dword dwBidnr, const std::string& name, dword timeStamp = 0, word offsetModuleName = 0, word forwardedModules = 0); // EXPORT
- void removeForwardedModule(dword dwBidnr, word forwardedModule); // EXPORT
- };
-}
-
-
-#endif
+/*
+* BoundImportDirectory.h - Part of the PeLib library.
+*
+* Copyright (c) 2004 - 2005 Sebastian Porst (webmaster@the-interweb.com)
+* All rights reserved.
+*
+* This software is licensed under the zlib/libpng License.
+* For more details see http://www.opensource.org/licenses/zlib-license.php
+* or the license information file (license.htm) in the root directory
+* of PeLib.
+*/
+
+#ifndef BOUNDIMPORTDIRECTORY_H
+#define BOUNDIMPORTDIRECTORY_H
+
+#include "PeLibAux.h"
+
+namespace PeLib
+{
+ /// Class that handles the BoundImport directory.
+ /**
+ * This class can read and modify the BoundImport directory table of a PE file.
+ **/
+ class BoundImportDirectory
+ {
+ private:
+ std::vector<PELIB_IMAGE_BOUND_DIRECTORY> m_vIbd; ///< Stores the individual BoundImport fields.
+
+ int read(InputBuffer& inpBuffer, unsigned char* data, unsigned int dwSize);
+ unsigned int totalModules() const;
+ public:
+ /// Adds another bound import.
+ int addBoundImport(const std::string& strModuleName, dword dwTds, word dwOmn, word wWfr); // EXPORT
+ /// Identifies a module through it's name.
+ int getModuleIndex(const std::string& strModuleName) const; // EXPORT
+ /// Returns the number of files in the BoundImport directory.
+ unsigned int calcNumberOfModules() const; // EXPORT
+ /// Reads the BoundImport directory table from a PE file.
+ int read(const std::string& strFileName, dword dwOffset, unsigned int uiSize); // EXPORT
+ int read(unsigned char* pcBuffer, unsigned int uiSize); // EXPORT
+ /// Rebuilds the BoundImport directory.
+ void rebuild(std::vector<byte>& vBuffer, bool fMakeValid = true) const; // EXPORT
+ /// Empties the BoundImport directory.
+ void clear(); // EXPORT
+ /// Removes a bound import.
+ void removeBoundImport(const std::string& strModuleName); // EXPORT
+ /// Returns the size of the BoundImport directory.
+ unsigned int size() const; // EXPORT
+ /// Writes the current bound import directory to a file.
+ int write(const std::string& strFilename, dword dwOffset, bool fMakeValid = true) const; // EXPORT
+
+ /// Retrieves the TimeDateStamp value of a bound import.
+ dword getTimeDateStamp(dword dwBidnr) const; // EXPORT
+ /// Retrieves the OffsetModuleName value of a bound import.
+ word getOffsetModuleName(dword dwBidnr) const; // EXPORT
+ /// Retrieves the NumberOfModuleForwarderRefs value of a bound import.
+ word getNumberOfModuleForwarderRefs(dword dwBidnr) const; // EXPORT
+ /// Retrieves the ModuleName value of a bound import.
+ std::string getModuleName(dword dwBidnr) const; // EXPORT
+
+ /// Updates the TimeDateStamp value of a bound import.
+ void setTimeDateStamp(dword dwBidnr, dword dwTds); // EXPORT
+ /// Updates the OffsetModuleName value of a bound import.
+ void setOffsetModuleName(dword dwBidnr, word wOmn); // EXPORT
+ /// Updates the NumberOfModuleForwarderRefs value of a bound import.
+ void setNumberOfModuleForwarderRefs(dword dwBidnr, word wMfr); // EXPORT
+ /// Updates the ModuleName value of a bound import.
+ void setModuleName(dword dwBidnr, const std::string& strModuleName); // EXPORT
+
+ dword getTimeDateStamp(dword dwBidnr, dword forwardedModule) const; // EXPORT _module
+ word getOffsetModuleName(dword dwBidnr, dword forwardedModule) const; // EXPORT _module
+ word getNumberOfModuleForwarderRefs(dword dwBidnr, dword forwardedModule) const; // EXPORT _module
+ std::string getModuleName(dword dwBidnr, dword forwardedModule) const; // EXPORT _module
+
+ void setTimeDateStamp(dword dwBidnr, dword forwardedModule, dword dwTds); // EXPORT _module
+ void setOffsetModuleName(dword dwBidnr, dword forwardedModule, word wOmn); // EXPORT _module
+ void setNumberOfModuleForwarderRefs(dword dwBidnr, dword forwardedModule, word wMfr); // EXPORT _module
+ void setModuleName(dword dwBidnr, dword forwardedModule, const std::string& strModuleName); // EXPORT _module
+
+ word calcNumberOfModuleForwarderRefs(dword dwBidnr) const; // EXPORT
+ void addForwardedModule(dword dwBidnr, const std::string& name, dword timeStamp = 0, word offsetModuleName = 0, word forwardedModules = 0); // EXPORT
+ void removeForwardedModule(dword dwBidnr, word forwardedModule); // EXPORT
+ };
+}
+
+
+#endif
diff --git a/utils/zenutils/libraries/pelib-0.9/pelib/ComHeaderDirectory.cpp b/utils/zenutils/libraries/pelib-0.9/pelib/ComHeaderDirectory.cpp
index 886348994e..fef12f4d8b 100755..100644
--- a/utils/zenutils/libraries/pelib-0.9/pelib/ComHeaderDirectory.cpp
+++ b/utils/zenutils/libraries/pelib-0.9/pelib/ComHeaderDirectory.cpp
@@ -1,467 +1,467 @@
-/*
-* ComHeaderDirectory.cpp - Part of the PeLib library.
-*
-* Copyright (c) 2004 - 2005 Sebastian Porst (webmaster@the-interweb.com)
-* All rights reserved.
-*
-* This software is licensed under the zlib/libpng License.
-* For more details see http://www.opensource.org/licenses/zlib-license.php
-* or the license information file (license.htm) in the root directory
-* of PeLib.
-*/
-
-#include "PeLibInc.h"
-#include "ComHeaderDirectory.h"
-
-namespace PeLib
-{
- void ComHeaderDirectory::read(InputBuffer& inputbuffer)
- {
- PELIB_IMAGE_COR20_HEADER ichCurr;
-
- inputbuffer >> ichCurr.cb;
- inputbuffer >> ichCurr.MajorRuntimeVersion;
- inputbuffer >> ichCurr.MinorRuntimeVersion;
- inputbuffer >> ichCurr.MetaData.VirtualAddress;
- inputbuffer >> ichCurr.MetaData.Size;
- inputbuffer >> ichCurr.Flags;
- inputbuffer >> ichCurr.EntryPointToken;
- inputbuffer >> ichCurr.Resources.VirtualAddress;
- inputbuffer >> ichCurr.Resources.Size;
- inputbuffer >> ichCurr.StrongNameSignature.VirtualAddress;
- inputbuffer >> ichCurr.StrongNameSignature.Size;
- inputbuffer >> ichCurr.CodeManagerTable.VirtualAddress;
- inputbuffer >> ichCurr.CodeManagerTable.Size;
- inputbuffer >> ichCurr.VTableFixups.VirtualAddress;
- inputbuffer >> ichCurr.VTableFixups.Size;
- inputbuffer >> ichCurr.ExportAddressTableJumps.VirtualAddress;
- inputbuffer >> ichCurr.ExportAddressTableJumps.Size;
- inputbuffer >> ichCurr.ManagedNativeHeader.VirtualAddress;
- inputbuffer >> ichCurr.ManagedNativeHeader.Size;
-
- std::swap(ichCurr, m_ichComHeader);
- }
-
- int ComHeaderDirectory::read(unsigned char* buffer, unsigned int buffersize)
- {
- if (buffersize < PELIB_IMAGE_COR20_HEADER::size())
- {
- return ERROR_INVALID_FILE;
- }
-
- std::vector<byte> vComDescDirectory(buffer, buffer + buffersize);
-
- InputBuffer ibBuffer(vComDescDirectory);
- read(ibBuffer);
- return NO_ERROR;
- }
-
- /**
- * Reads a file's COM+ descriptor.
- * @param strFilename Name of the file.
- * @param uiOffset File offset of the COM+ descriptor.
- * @param uiSize Size of the COM+ descriptor.
- **/
- int ComHeaderDirectory::read(const std::string& strFilename, unsigned int uiOffset, unsigned int uiSize)
- {
- std::ifstream ifFile(strFilename.c_str(), std::ios::binary);
- unsigned int ulFileSize = fileSize(ifFile);
-
- if (!ifFile)
- {
- return ERROR_OPENING_FILE;
- }
-
- if (ulFileSize < uiOffset + uiSize)
- {
- return ERROR_INVALID_FILE;
- }
-
- ifFile.seekg(uiOffset, std::ios::beg);
-
- std::vector<byte> vComDescDirectory(uiSize);
- ifFile.read(reinterpret_cast<char*>(&vComDescDirectory[0]), uiSize);
-
- InputBuffer ibBuffer(vComDescDirectory);
- read(ibBuffer);
- return NO_ERROR;
- }
-
- /**
- * Rebuilds the current COM+ descriptor.
- * @param vBuffer Buffer where the COM+ descriptor will be written to.
- **/
- void ComHeaderDirectory::rebuild(std::vector<byte>& vBuffer) const
- {
- OutputBuffer obBuffer(vBuffer);
-
- obBuffer << m_ichComHeader.cb;
- obBuffer << m_ichComHeader.MajorRuntimeVersion;
- obBuffer << m_ichComHeader.MinorRuntimeVersion;
- obBuffer << m_ichComHeader.MetaData.VirtualAddress;
- obBuffer << m_ichComHeader.MetaData.Size;
- obBuffer << m_ichComHeader.Flags;
- obBuffer << m_ichComHeader.EntryPointToken;
- obBuffer << m_ichComHeader.Resources.VirtualAddress;
- obBuffer << m_ichComHeader.Resources.Size;
- obBuffer << m_ichComHeader.StrongNameSignature.VirtualAddress;
- obBuffer << m_ichComHeader.StrongNameSignature.Size;
- obBuffer << m_ichComHeader.CodeManagerTable.VirtualAddress;
- obBuffer << m_ichComHeader.CodeManagerTable.Size;
- obBuffer << m_ichComHeader.VTableFixups.VirtualAddress;
- obBuffer << m_ichComHeader.VTableFixups.Size;
- obBuffer << m_ichComHeader.ExportAddressTableJumps.VirtualAddress;
- obBuffer << m_ichComHeader.ExportAddressTableJumps.Size;
- obBuffer << m_ichComHeader.ManagedNativeHeader.VirtualAddress;
- obBuffer << m_ichComHeader.ManagedNativeHeader.Size;
- }
-
- /**
- * @return Size in bytes.
- **/
- unsigned int ComHeaderDirectory::size() const
- {
- return PELIB_IMAGE_COR20_HEADER::size();
- }
-
- /**
- * @param strFilename Name of the file.
- * @param dwOffset File offset the COM+ descriptor will be written to.
- **/
- int ComHeaderDirectory::write(const std::string& strFilename, unsigned int dwOffset) const
- {
- std::fstream ofFile(strFilename.c_str(), std::ios_base::in);
-
- if (!ofFile)
- {
- ofFile.clear();
- ofFile.open(strFilename.c_str(), std::ios_base::out | std::ios_base::binary);
- }
- else
- {
- ofFile.close();
- ofFile.open(strFilename.c_str(), std::ios_base::in | std::ios_base::out | std::ios_base::binary);
- }
-
- if (!ofFile)
- {
- return ERROR_OPENING_FILE;
- }
-
- ofFile.seekp(dwOffset, std::ios::beg);
-
- std::vector<unsigned char> vBuffer;
- rebuild(vBuffer);
-
- ofFile.write(reinterpret_cast<const char*>(&vBuffer[0]), static_cast<unsigned int>(vBuffer.size()));
-
- ofFile.close();
-
- return NO_ERROR;
- }
-
- /**
- * @return SizeOfHeader value of the current COM+ descriptor.
- **/
- dword ComHeaderDirectory::getSizeOfHeader() const
- {
- return m_ichComHeader.cb;
- }
-
- /**
- * @return MajorRuntimeVersion value of the current COM+ descriptor.
- **/
- word ComHeaderDirectory::getMajorRuntimeVersion() const
- {
- return m_ichComHeader.MajorRuntimeVersion;
- }
-
- /**
- * @return MinorRuntimeVersion value of the current COM+ descriptor.
- **/
- word ComHeaderDirectory::getMinorRuntimeVersion() const
- {
- return m_ichComHeader.MinorRuntimeVersion;
- }
-
- /**
- * @return MetaData (Virtual Address) value of the current COM+ descriptor.
- **/
- dword ComHeaderDirectory::getMetaDataVa() const
- {
- return m_ichComHeader.MetaData.VirtualAddress;
- }
-
- /**
- * @return MetaData (Size) value of the current COM+ descriptor.
- **/
- dword ComHeaderDirectory::getMetaDataSize() const
- {
- return m_ichComHeader.MetaData.Size;
- }
-
- /**
- * @return Flags value of the current COM+ descriptor.
- **/
- dword ComHeaderDirectory::getFlags() const
- {
- return m_ichComHeader.Flags;
- }
-
- /**
- * @return EntryPointToken value of the current COM+ descriptor.
- **/
- dword ComHeaderDirectory::getEntryPointToken() const
- {
- return m_ichComHeader.EntryPointToken;
- }
-
- /**
- * @return Resources (Virtual Address) value of the current COM+ descriptor.
- **/
- dword ComHeaderDirectory::getResourcesVa() const
- {
- return m_ichComHeader.Resources.VirtualAddress;
- }
-
- /**
- * @return Resources (Size) value of the current COM+ descriptor.
- **/
- dword ComHeaderDirectory::getResourcesSize()
- {
- return m_ichComHeader.Resources.Size;
- }
-
- /**
- * @return StrongNameSignature (Virtual Address) value of the current COM+ descriptor.
- **/
- dword ComHeaderDirectory::getStrongNameSignatureVa() const
- {
- return m_ichComHeader.StrongNameSignature.VirtualAddress;
- }
-
- /**
- * @return StrongNameSignature (Size) value of the current COM+ descriptor.
- **/
- dword ComHeaderDirectory::getStrongNameSignagureSize() const
- {
- return m_ichComHeader.StrongNameSignature.Size;
- }
-
- /**
- * @return CodeManagerTable (Virtual Address) value of the current COM+ descriptor.
- **/
- dword ComHeaderDirectory::getCodeManagerTableVa() const
- {
- return m_ichComHeader.CodeManagerTable.VirtualAddress;
- }
-
- /**
- * @return CodeManagerTable (Size) value of the current COM+ descriptor.
- **/
- dword ComHeaderDirectory::getCodeManagerTableSize() const
- {
- return m_ichComHeader.CodeManagerTable.Size;
- }
-
- /**
- * @return VTableFixups (Virtual Address) value of the current COM+ descriptor.
- **/
- dword ComHeaderDirectory::getVTableFixupsVa() const
- {
- return m_ichComHeader.VTableFixups.VirtualAddress;
- }
-
- /**
- * @return VTableFixups (Size) value of the current COM+ descriptor.
- **/
- dword ComHeaderDirectory::getVTableFixupsSize() const
- {
- return m_ichComHeader.VTableFixups.Size;
- }
-
- /**
- * @return ExportAddressTableJumps (Virtual Address) value of the current COM+ descriptor.
- **/
- dword ComHeaderDirectory::getExportAddressTableJumpsVa() const
- {
- return m_ichComHeader.ExportAddressTableJumps.VirtualAddress;
- }
-
- /**
- * @return ExportAddressTableJumps (Size) value of the current COM+ descriptor.
- **/
- dword ComHeaderDirectory::getExportAddressTableJumpsSize() const
- {
- return m_ichComHeader.ExportAddressTableJumps.Size;
- }
-
- /**
- * @return ManagedNativeHeader (Virtual Address) value of the current COM+ descriptor.
- **/
- dword ComHeaderDirectory::getManagedNativeHeaderVa() const
- {
- return m_ichComHeader.ManagedNativeHeader.VirtualAddress;
- }
-
- /**
- * @return ManagedNativeHeader (Size) value of the current COM+ descriptor.
- **/
- dword ComHeaderDirectory::getManagedNativeHeaderSize() const
- {
- return m_ichComHeader.ManagedNativeHeader.Size;
- }
-
- /**
- * @param dwValue New value for the current SizeOfHeader (cb) value.
- **/
- void ComHeaderDirectory::setSizeOfHeader(dword dwValue)
- {
- m_ichComHeader.cb = dwValue;
- }
-
- /**
- * @param wValue New value for the current MajorRuntimeVersion value.
- **/
- void ComHeaderDirectory::setMajorRuntimeVersion(word wValue)
- {
- m_ichComHeader.MajorRuntimeVersion = wValue;
- }
-
- /**
- * @param wValue New value for the current MinorRuntimeVersion value.
- **/
- void ComHeaderDirectory::setMinorRuntimeVersion(word wValue)
- {
- m_ichComHeader.MinorRuntimeVersion = wValue;
- }
-
- /**
- * @param dwValue New value for the current MetaData (VirtualAddress) value.
- **/
- void ComHeaderDirectory::setMetaDataVa(dword dwValue)
- {
- m_ichComHeader.MetaData.VirtualAddress = dwValue;
- }
-
- /**
- * @param dwValue New value for the current MetaData (Size) value.
- **/
- void ComHeaderDirectory::setMetaDataSize(dword dwValue)
- {
- m_ichComHeader.MetaData.Size = dwValue;
- }
-
- /**
- * @param dwValue New value for the current Flags value.
- **/
- void ComHeaderDirectory::setFlags(dword dwValue)
- {
- m_ichComHeader.Flags = dwValue;
- }
-
- /**
- * @param dwValue New value for the current EntryPointToken value.
- **/
- void ComHeaderDirectory::setEntryPointToken(dword dwValue)
- {
- m_ichComHeader.EntryPointToken = dwValue;
- }
-
- /**
- * @param dwValue New value for the current Resources (VirtualAddress) value.
- **/
- void ComHeaderDirectory::setResourcesVa(dword dwValue)
- {
- m_ichComHeader.Resources.VirtualAddress = dwValue;
- }
-
- /**
- * @param dwValue New value for the current Resources (Size) value.
- **/
- void ComHeaderDirectory::setResourcesSize(dword dwValue)
- {
- m_ichComHeader.Resources.Size = dwValue;
- }
-
- /**
- * @param dwValue New value for the current StrongNameSignature (VirtualAddress) value.
- **/
- void ComHeaderDirectory::setStrongNameSignatureVa(dword dwValue)
- {
- m_ichComHeader.StrongNameSignature.VirtualAddress = dwValue;
- }
-
- /**
- * @param dwValue New value for the current StrongNameSignature (Size) value.
- **/
- void ComHeaderDirectory::setStrongNameSignagureSize(dword dwValue)
- {
- m_ichComHeader.StrongNameSignature.Size = dwValue;
- }
-
- /**
- * @param dwValue New value for the current CodeManagerTable (VirtualAddress) value.
- **/
- void ComHeaderDirectory::setCodeManagerTableVa(dword dwValue)
- {
- m_ichComHeader.CodeManagerTable.VirtualAddress = dwValue;
- }
-
- /**
- * @param dwValue New value for the current CodeManagerTable (Size) value.
- **/
- void ComHeaderDirectory::setCodeManagerTableSize(dword dwValue)
- {
- m_ichComHeader.CodeManagerTable.Size = dwValue;
- }
-
- /**
- * @param dwValue New value for the current VTableFixups (VirtualAddress) value.
- **/
- void ComHeaderDirectory::setVTableFixupsVa(dword dwValue)
- {
- m_ichComHeader.VTableFixups.VirtualAddress = dwValue;
- }
-
- /**
- * @param dwValue New value for the current VTableFixups (Size) value.
- **/
- void ComHeaderDirectory::setVTableFixupsSize(dword dwValue)
- {
- m_ichComHeader.VTableFixups.Size = dwValue;
- }
-
- /**
- * @param dwValue New value for the current ExportAddressTableJumps (VirtualAddress) value.
- **/
- void ComHeaderDirectory::setExportAddressTableJumpsVa(dword dwValue)
- {
- m_ichComHeader.ExportAddressTableJumps.VirtualAddress = dwValue;
- }
-
- /**
- * @param dwValue New value for the current ExportAddressTableJumps (Size) value.
- **/
- void ComHeaderDirectory::setExportAddressTableJumpsSize(dword dwValue)
- {
- m_ichComHeader.ExportAddressTableJumps.Size = dwValue;
- }
-
- /**
- * @param dwValue New value for the current ManagedNativeHeader (VirtualAddress) value.
- **/
- void ComHeaderDirectory::setManagedNativeHeaderVa(dword dwValue)
- {
- m_ichComHeader.ManagedNativeHeader.VirtualAddress = dwValue;
- }
-
- /**
- * @param dwValue New value for the current ManagedNativeHeader (Size) value.
- **/
- void ComHeaderDirectory::setManagedNativeHeaderSize(dword dwValue)
- {
- m_ichComHeader.ManagedNativeHeader.Size = dwValue;
- }
-
-}
+/*
+* ComHeaderDirectory.cpp - Part of the PeLib library.
+*
+* Copyright (c) 2004 - 2005 Sebastian Porst (webmaster@the-interweb.com)
+* All rights reserved.
+*
+* This software is licensed under the zlib/libpng License.
+* For more details see http://www.opensource.org/licenses/zlib-license.php
+* or the license information file (license.htm) in the root directory
+* of PeLib.
+*/
+
+#include "PeLibInc.h"
+#include "ComHeaderDirectory.h"
+
+namespace PeLib
+{
+ void ComHeaderDirectory::read(InputBuffer& inputbuffer)
+ {
+ PELIB_IMAGE_COR20_HEADER ichCurr;
+
+ inputbuffer >> ichCurr.cb;
+ inputbuffer >> ichCurr.MajorRuntimeVersion;
+ inputbuffer >> ichCurr.MinorRuntimeVersion;
+ inputbuffer >> ichCurr.MetaData.VirtualAddress;
+ inputbuffer >> ichCurr.MetaData.Size;
+ inputbuffer >> ichCurr.Flags;
+ inputbuffer >> ichCurr.EntryPointToken;
+ inputbuffer >> ichCurr.Resources.VirtualAddress;
+ inputbuffer >> ichCurr.Resources.Size;
+ inputbuffer >> ichCurr.StrongNameSignature.VirtualAddress;
+ inputbuffer >> ichCurr.StrongNameSignature.Size;
+ inputbuffer >> ichCurr.CodeManagerTable.VirtualAddress;
+ inputbuffer >> ichCurr.CodeManagerTable.Size;
+ inputbuffer >> ichCurr.VTableFixups.VirtualAddress;
+ inputbuffer >> ichCurr.VTableFixups.Size;
+ inputbuffer >> ichCurr.ExportAddressTableJumps.VirtualAddress;
+ inputbuffer >> ichCurr.ExportAddressTableJumps.Size;
+ inputbuffer >> ichCurr.ManagedNativeHeader.VirtualAddress;
+ inputbuffer >> ichCurr.ManagedNativeHeader.Size;
+
+ std::swap(ichCurr, m_ichComHeader);
+ }
+
+ int ComHeaderDirectory::read(unsigned char* buffer, unsigned int buffersize)
+ {
+ if (buffersize < PELIB_IMAGE_COR20_HEADER::size())
+ {
+ return ERROR_INVALID_FILE;
+ }
+
+ std::vector<byte> vComDescDirectory(buffer, buffer + buffersize);
+
+ InputBuffer ibBuffer(vComDescDirectory);
+ read(ibBuffer);
+ return NO_ERROR;
+ }
+
+ /**
+ * Reads a file's COM+ descriptor.
+ * @param strFilename Name of the file.
+ * @param uiOffset File offset of the COM+ descriptor.
+ * @param uiSize Size of the COM+ descriptor.
+ **/
+ int ComHeaderDirectory::read(const std::string& strFilename, unsigned int uiOffset, unsigned int uiSize)
+ {
+ std::ifstream ifFile(strFilename.c_str(), std::ios::binary);
+ unsigned int ulFileSize = fileSize(ifFile);
+
+ if (!ifFile)
+ {
+ return ERROR_OPENING_FILE;
+ }
+
+ if (ulFileSize < uiOffset + uiSize)
+ {
+ return ERROR_INVALID_FILE;
+ }
+
+ ifFile.seekg(uiOffset, std::ios::beg);
+
+ std::vector<byte> vComDescDirectory(uiSize);
+ ifFile.read(reinterpret_cast<char*>(&vComDescDirectory[0]), uiSize);
+
+ InputBuffer ibBuffer(vComDescDirectory);
+ read(ibBuffer);
+ return NO_ERROR;
+ }
+
+ /**
+ * Rebuilds the current COM+ descriptor.
+ * @param vBuffer Buffer where the COM+ descriptor will be written to.
+ **/
+ void ComHeaderDirectory::rebuild(std::vector<byte>& vBuffer) const
+ {
+ OutputBuffer obBuffer(vBuffer);
+
+ obBuffer << m_ichComHeader.cb;
+ obBuffer << m_ichComHeader.MajorRuntimeVersion;
+ obBuffer << m_ichComHeader.MinorRuntimeVersion;
+ obBuffer << m_ichComHeader.MetaData.VirtualAddress;
+ obBuffer << m_ichComHeader.MetaData.Size;
+ obBuffer << m_ichComHeader.Flags;
+ obBuffer << m_ichComHeader.EntryPointToken;
+ obBuffer << m_ichComHeader.Resources.VirtualAddress;
+ obBuffer << m_ichComHeader.Resources.Size;
+ obBuffer << m_ichComHeader.StrongNameSignature.VirtualAddress;
+ obBuffer << m_ichComHeader.StrongNameSignature.Size;
+ obBuffer << m_ichComHeader.CodeManagerTable.VirtualAddress;
+ obBuffer << m_ichComHeader.CodeManagerTable.Size;
+ obBuffer << m_ichComHeader.VTableFixups.VirtualAddress;
+ obBuffer << m_ichComHeader.VTableFixups.Size;
+ obBuffer << m_ichComHeader.ExportAddressTableJumps.VirtualAddress;
+ obBuffer << m_ichComHeader.ExportAddressTableJumps.Size;
+ obBuffer << m_ichComHeader.ManagedNativeHeader.VirtualAddress;
+ obBuffer << m_ichComHeader.ManagedNativeHeader.Size;
+ }
+
+ /**
+ * @return Size in bytes.
+ **/
+ unsigned int ComHeaderDirectory::size() const
+ {
+ return PELIB_IMAGE_COR20_HEADER::size();
+ }
+
+ /**
+ * @param strFilename Name of the file.
+ * @param dwOffset File offset the COM+ descriptor will be written to.
+ **/
+ int ComHeaderDirectory::write(const std::string& strFilename, unsigned int dwOffset) const
+ {
+ std::fstream ofFile(strFilename.c_str(), std::ios_base::in);
+
+ if (!ofFile)
+ {
+ ofFile.clear();
+ ofFile.open(strFilename.c_str(), std::ios_base::out | std::ios_base::binary);
+ }
+ else
+ {
+ ofFile.close();
+ ofFile.open(strFilename.c_str(), std::ios_base::in | std::ios_base::out | std::ios_base::binary);
+ }
+
+ if (!ofFile)
+ {
+ return ERROR_OPENING_FILE;
+ }
+
+ ofFile.seekp(dwOffset, std::ios::beg);
+
+ std::vector<unsigned char> vBuffer;
+ rebuild(vBuffer);
+
+ ofFile.write(reinterpret_cast<const char*>(&vBuffer[0]), static_cast<unsigned int>(vBuffer.size()));
+
+ ofFile.close();
+
+ return NO_ERROR;
+ }
+
+ /**
+ * @return SizeOfHeader value of the current COM+ descriptor.
+ **/
+ dword ComHeaderDirectory::getSizeOfHeader() const
+ {
+ return m_ichComHeader.cb;
+ }
+
+ /**
+ * @return MajorRuntimeVersion value of the current COM+ descriptor.
+ **/
+ word ComHeaderDirectory::getMajorRuntimeVersion() const
+ {
+ return m_ichComHeader.MajorRuntimeVersion;
+ }
+
+ /**
+ * @return MinorRuntimeVersion value of the current COM+ descriptor.
+ **/
+ word ComHeaderDirectory::getMinorRuntimeVersion() const
+ {
+ return m_ichComHeader.MinorRuntimeVersion;
+ }
+
+ /**
+ * @return MetaData (Virtual Address) value of the current COM+ descriptor.
+ **/
+ dword ComHeaderDirectory::getMetaDataVa() const
+ {
+ return m_ichComHeader.MetaData.VirtualAddress;
+ }
+
+ /**
+ * @return MetaData (Size) value of the current COM+ descriptor.
+ **/
+ dword ComHeaderDirectory::getMetaDataSize() const
+ {
+ return m_ichComHeader.MetaData.Size;
+ }
+
+ /**
+ * @return Flags value of the current COM+ descriptor.
+ **/
+ dword ComHeaderDirectory::getFlags() const
+ {
+ return m_ichComHeader.Flags;
+ }
+
+ /**
+ * @return EntryPointToken value of the current COM+ descriptor.
+ **/
+ dword ComHeaderDirectory::getEntryPointToken() const
+ {
+ return m_ichComHeader.EntryPointToken;
+ }
+
+ /**
+ * @return Resources (Virtual Address) value of the current COM+ descriptor.
+ **/
+ dword ComHeaderDirectory::getResourcesVa() const
+ {
+ return m_ichComHeader.Resources.VirtualAddress;
+ }
+
+ /**
+ * @return Resources (Size) value of the current COM+ descriptor.
+ **/
+ dword ComHeaderDirectory::getResourcesSize()
+ {
+ return m_ichComHeader.Resources.Size;
+ }
+
+ /**
+ * @return StrongNameSignature (Virtual Address) value of the current COM+ descriptor.
+ **/
+ dword ComHeaderDirectory::getStrongNameSignatureVa() const
+ {
+ return m_ichComHeader.StrongNameSignature.VirtualAddress;
+ }
+
+ /**
+ * @return StrongNameSignature (Size) value of the current COM+ descriptor.
+ **/
+ dword ComHeaderDirectory::getStrongNameSignagureSize() const
+ {
+ return m_ichComHeader.StrongNameSignature.Size;
+ }
+
+ /**
+ * @return CodeManagerTable (Virtual Address) value of the current COM+ descriptor.
+ **/
+ dword ComHeaderDirectory::getCodeManagerTableVa() const
+ {
+ return m_ichComHeader.CodeManagerTable.VirtualAddress;
+ }
+
+ /**
+ * @return CodeManagerTable (Size) value of the current COM+ descriptor.
+ **/
+ dword ComHeaderDirectory::getCodeManagerTableSize() const
+ {
+ return m_ichComHeader.CodeManagerTable.Size;
+ }
+
+ /**
+ * @return VTableFixups (Virtual Address) value of the current COM+ descriptor.
+ **/
+ dword ComHeaderDirectory::getVTableFixupsVa() const
+ {
+ return m_ichComHeader.VTableFixups.VirtualAddress;
+ }
+
+ /**
+ * @return VTableFixups (Size) value of the current COM+ descriptor.
+ **/
+ dword ComHeaderDirectory::getVTableFixupsSize() const
+ {
+ return m_ichComHeader.VTableFixups.Size;
+ }
+
+ /**
+ * @return ExportAddressTableJumps (Virtual Address) value of the current COM+ descriptor.
+ **/
+ dword ComHeaderDirectory::getExportAddressTableJumpsVa() const
+ {
+ return m_ichComHeader.ExportAddressTableJumps.VirtualAddress;
+ }
+
+ /**
+ * @return ExportAddressTableJumps (Size) value of the current COM+ descriptor.
+ **/
+ dword ComHeaderDirectory::getExportAddressTableJumpsSize() const
+ {
+ return m_ichComHeader.ExportAddressTableJumps.Size;
+ }
+
+ /**
+ * @return ManagedNativeHeader (Virtual Address) value of the current COM+ descriptor.
+ **/
+ dword ComHeaderDirectory::getManagedNativeHeaderVa() const
+ {
+ return m_ichComHeader.ManagedNativeHeader.VirtualAddress;
+ }
+
+ /**
+ * @return ManagedNativeHeader (Size) value of the current COM+ descriptor.
+ **/
+ dword ComHeaderDirectory::getManagedNativeHeaderSize() const
+ {
+ return m_ichComHeader.ManagedNativeHeader.Size;
+ }
+
+ /**
+ * @param dwValue New value for the current SizeOfHeader (cb) value.
+ **/
+ void ComHeaderDirectory::setSizeOfHeader(dword dwValue)
+ {
+ m_ichComHeader.cb = dwValue;
+ }
+
+ /**
+ * @param wValue New value for the current MajorRuntimeVersion value.
+ **/
+ void ComHeaderDirectory::setMajorRuntimeVersion(word wValue)
+ {
+ m_ichComHeader.MajorRuntimeVersion = wValue;
+ }
+
+ /**
+ * @param wValue New value for the current MinorRuntimeVersion value.
+ **/
+ void ComHeaderDirectory::setMinorRuntimeVersion(word wValue)
+ {
+ m_ichComHeader.MinorRuntimeVersion = wValue;
+ }
+
+ /**
+ * @param dwValue New value for the current MetaData (VirtualAddress) value.
+ **/
+ void ComHeaderDirectory::setMetaDataVa(dword dwValue)
+ {
+ m_ichComHeader.MetaData.VirtualAddress = dwValue;
+ }
+
+ /**
+ * @param dwValue New value for the current MetaData (Size) value.
+ **/
+ void ComHeaderDirectory::setMetaDataSize(dword dwValue)
+ {
+ m_ichComHeader.MetaData.Size = dwValue;
+ }
+
+ /**
+ * @param dwValue New value for the current Flags value.
+ **/
+ void ComHeaderDirectory::setFlags(dword dwValue)
+ {
+ m_ichComHeader.Flags = dwValue;
+ }
+
+ /**
+ * @param dwValue New value for the current EntryPointToken value.
+ **/
+ void ComHeaderDirectory::setEntryPointToken(dword dwValue)
+ {
+ m_ichComHeader.EntryPointToken = dwValue;
+ }
+
+ /**
+ * @param dwValue New value for the current Resources (VirtualAddress) value.
+ **/
+ void ComHeaderDirectory::setResourcesVa(dword dwValue)
+ {
+ m_ichComHeader.Resources.VirtualAddress = dwValue;
+ }
+
+ /**
+ * @param dwValue New value for the current Resources (Size) value.
+ **/
+ void ComHeaderDirectory::setResourcesSize(dword dwValue)
+ {
+ m_ichComHeader.Resources.Size = dwValue;
+ }
+
+ /**
+ * @param dwValue New value for the current StrongNameSignature (VirtualAddress) value.
+ **/
+ void ComHeaderDirectory::setStrongNameSignatureVa(dword dwValue)
+ {
+ m_ichComHeader.StrongNameSignature.VirtualAddress = dwValue;
+ }
+
+ /**
+ * @param dwValue New value for the current StrongNameSignature (Size) value.
+ **/
+ void ComHeaderDirectory::setStrongNameSignagureSize(dword dwValue)
+ {
+ m_ichComHeader.StrongNameSignature.Size = dwValue;
+ }
+
+ /**
+ * @param dwValue New value for the current CodeManagerTable (VirtualAddress) value.
+ **/
+ void ComHeaderDirectory::setCodeManagerTableVa(dword dwValue)
+ {
+ m_ichComHeader.CodeManagerTable.VirtualAddress = dwValue;
+ }
+
+ /**
+ * @param dwValue New value for the current CodeManagerTable (Size) value.
+ **/
+ void ComHeaderDirectory::setCodeManagerTableSize(dword dwValue)
+ {
+ m_ichComHeader.CodeManagerTable.Size = dwValue;
+ }
+
+ /**
+ * @param dwValue New value for the current VTableFixups (VirtualAddress) value.
+ **/
+ void ComHeaderDirectory::setVTableFixupsVa(dword dwValue)
+ {
+ m_ichComHeader.VTableFixups.VirtualAddress = dwValue;
+ }
+
+ /**
+ * @param dwValue New value for the current VTableFixups (Size) value.
+ **/
+ void ComHeaderDirectory::setVTableFixupsSize(dword dwValue)
+ {
+ m_ichComHeader.VTableFixups.Size = dwValue;
+ }
+
+ /**
+ * @param dwValue New value for the current ExportAddressTableJumps (VirtualAddress) value.
+ **/
+ void ComHeaderDirectory::setExportAddressTableJumpsVa(dword dwValue)
+ {
+ m_ichComHeader.ExportAddressTableJumps.VirtualAddress = dwValue;
+ }
+
+ /**
+ * @param dwValue New value for the current ExportAddressTableJumps (Size) value.
+ **/
+ void ComHeaderDirectory::setExportAddressTableJumpsSize(dword dwValue)
+ {
+ m_ichComHeader.ExportAddressTableJumps.Size = dwValue;
+ }
+
+ /**
+ * @param dwValue New value for the current ManagedNativeHeader (VirtualAddress) value.
+ **/
+ void ComHeaderDirectory::setManagedNativeHeaderVa(dword dwValue)
+ {
+ m_ichComHeader.ManagedNativeHeader.VirtualAddress = dwValue;
+ }
+
+ /**
+ * @param dwValue New value for the current ManagedNativeHeader (Size) value.
+ **/
+ void ComHeaderDirectory::setManagedNativeHeaderSize(dword dwValue)
+ {
+ m_ichComHeader.ManagedNativeHeader.Size = dwValue;
+ }
+
+}
diff --git a/utils/zenutils/libraries/pelib-0.9/pelib/ComHeaderDirectory.h b/utils/zenutils/libraries/pelib-0.9/pelib/ComHeaderDirectory.h
index 915813c1cc..cdc993df1f 100755..100644
--- a/utils/zenutils/libraries/pelib-0.9/pelib/ComHeaderDirectory.h
+++ b/utils/zenutils/libraries/pelib-0.9/pelib/ComHeaderDirectory.h
@@ -1,120 +1,120 @@
-/*
-* ComHeaderDirectory.h - Part of the PeLib library.
-*
-* Copyright (c) 2004 - 2005 Sebastian Porst (webmaster@the-interweb.com)
-* All rights reserved.
-*
-* This software is licensed under the zlib/libpng License.
-* For more details see http://www.opensource.org/licenses/zlib-license.php
-* or the license information file (license.htm) in the root directory
-* of PeLib.
-*/
-
-#ifndef COMHEADERDIRECTORY_H
-#define COMHEADERDIRECTORY_H
-
-namespace PeLib
-{
- /// Class that handles the COM+ descriptor directory.
- /**
- * This class handles the COM+ Descriptor directory which was added to PE files
- * which work with the .NET runtime modules.
- **/
- class ComHeaderDirectory
- {
- private:
- PELIB_IMAGE_COR20_HEADER m_ichComHeader; ///< The COM+ descriptor.
-
- void read(InputBuffer& inputbuffer);
-
- public:
- /// Read a file's COM+ runtime descriptor directory.
- int read(const std::string& strFilename, unsigned int uiOffset, unsigned int uiSize); // EXPORT
- int read(unsigned char* buffer, unsigned int buffersize); // EXPORT
- /// Rebuild the COM+ descriptor.
- void rebuild(std::vector<byte>& vBuffer) const; // EXPORT
- /// Returns the size of the current COM+ descriptor.
- unsigned int size() const; // EXPORT
- /// Writes the current COM+ descriptor directory to a file.
- int write(const std::string& strFilename, unsigned int dwOffset) const; // EXPORT
-
- /// Get the COM+ descriptor's SizeOfHeader (cb) value.
- dword getSizeOfHeader() const; // EXPORT
- /// Get the COM+ descriptor's MajorRuntimeVersion value.
- word getMajorRuntimeVersion() const; // EXPORT
- /// Get the COM+ descriptor's MinorRuntimeVersion value.
- word getMinorRuntimeVersion() const; // EXPORT
- /// Get the COM+ descriptor's MetaData (Virtual Address) value.
- dword getMetaDataVa() const; // EXPORT
- /// Get the COM+ descriptor's MetaData (Size) value.
- dword getMetaDataSize() const; // EXPORT
- /// Get the COM+ descriptor's Flags value.
- dword getFlags() const; // EXPORT
- /// Get the COM+ descriptor's EntryPointToken value.
- dword getEntryPointToken() const; // EXPORT
- /// Get the COM+ descriptor's Resources (Virtual Address) value.
- dword getResourcesVa() const; // EXPORT
- /// Get the COM+ descriptor's Resources (Size) value.
- dword getResourcesSize(); // EXPORT
- /// Get the COM+ descriptor's StrongNameSignature (Virtual Address) value.
- dword getStrongNameSignatureVa() const; // EXPORT
- /// Get the COM+ descriptor's StrongNameSignature (Size) value.
- dword getStrongNameSignagureSize() const; // EXPORT
- /// Get the COM+ descriptor's CodeManagerTable (Virtual Address) value.
- dword getCodeManagerTableVa() const; // EXPORT
- /// Get the COM+ descriptor's CodeManagerTable (Size) value.
- dword getCodeManagerTableSize() const; // EXPORT
- /// Get the COM+ descriptor's VTableFixup (Virtual Address) value.
- dword getVTableFixupsVa() const; // EXPORT
- /// Get the COM+ descriptor's VTableFixup (Size) value.
- dword getVTableFixupsSize() const; // EXPORT
- /// Get the COM+ descriptor's ExportAddressTable (Virtual Address) value.
- dword getExportAddressTableJumpsVa() const; // EXPORT
- /// Get the COM+ descriptor's ExportAddressTable (Size) value.
- dword getExportAddressTableJumpsSize() const; // EXPORT
- /// Get the COM+ descriptor's ManagedNativeHeader (Virtual Address) value.
- dword getManagedNativeHeaderVa() const; // EXPORT
- /// Get the COM+ descriptor's ManagedNativeHeader (Size) value.
- dword getManagedNativeHeaderSize() const; // EXPORT
-
- /// Change the COM+ descriptor's SizeOfHeader (cb) value.
- void setSizeOfHeader(dword dwValue); // EXPORT
- /// Change the COM+ descriptor's MajorRuntimeVersion value.
- void setMajorRuntimeVersion(word wValue); // EXPORT
- /// Change the COM+ descriptor's MinorRuntimeVersion value.
- void setMinorRuntimeVersion(word wValue); // EXPORT
- /// Change the COM+ descriptor's MetaData (VirtualAddress) value.
- void setMetaDataVa(dword dwValue); // EXPORT
- /// Change the COM+ descriptor's MetaData (Size) value.
- void setMetaDataSize(dword dwValue); // EXPORT
- /// Change the COM+ descriptor's Flags value.
- void setFlags(dword dwValue); // EXPORT
- /// Change the COM+ descriptor's EntryPointToken value.
- void setEntryPointToken(dword dwValue); // EXPORT
- /// Change the COM+ descriptor's Resources (VirtualAddress) value.
- void setResourcesVa(dword dwValue); // EXPORT
- /// Change the COM+ descriptor's Resources (Size) value.
- void setResourcesSize(dword dwValue); // EXPORT
- /// Change the COM+ descriptor's StrongNameSignatureVa (VirtualAddress) value.
- void setStrongNameSignatureVa(dword dwValue); // EXPORT
- /// Change the COM+ descriptor's StrongNameSignatureVa (Size) value.
- void setStrongNameSignagureSize(dword dwValue); // EXPORT
- /// Change the COM+ descriptor's CodeManagerTable (VirtualAddress) value.
- void setCodeManagerTableVa(dword dwValue); // EXPORT
- /// Change the COM+ descriptor's CodeManagerTable (Size) value.
- void setCodeManagerTableSize(dword dwValue); // EXPORT
- /// Change the COM+ descriptor's VTableFixups (VirtualAddress) value.
- void setVTableFixupsVa(dword dwValue); // EXPORT
- /// Change the COM+ descriptor's VTableFixups (Size) value.
- void setVTableFixupsSize(dword dwValue); // EXPORT
- /// Change the COM+ descriptor's ExportAddressTableJumps (VirtualAddress) value.
- void setExportAddressTableJumpsVa(dword dwValue); // EXPORT
- /// Change the COM+ descriptor's ExportAddressTableJumps (Size) value.
- void setExportAddressTableJumpsSize(dword dwValue); // EXPORT
- /// Change the COM+ descriptor's ManagedNativeHeader (VirtualAddress) value.
- void setManagedNativeHeaderVa(dword dwValue); // EXPORT
- /// Change the COM+ descriptor's ManagedNativeHeader (Size) value.
- void setManagedNativeHeaderSize(dword dwValue); // EXPORT
- };
-}
-#endif
+/*
+* ComHeaderDirectory.h - Part of the PeLib library.
+*
+* Copyright (c) 2004 - 2005 Sebastian Porst (webmaster@the-interweb.com)
+* All rights reserved.
+*
+* This software is licensed under the zlib/libpng License.
+* For more details see http://www.opensource.org/licenses/zlib-license.php
+* or the license information file (license.htm) in the root directory
+* of PeLib.
+*/
+
+#ifndef COMHEADERDIRECTORY_H
+#define COMHEADERDIRECTORY_H
+
+namespace PeLib
+{
+ /// Class that handles the COM+ descriptor directory.
+ /**
+ * This class handles the COM+ Descriptor directory which was added to PE files
+ * which work with the .NET runtime modules.
+ **/
+ class ComHeaderDirectory
+ {
+ private:
+ PELIB_IMAGE_COR20_HEADER m_ichComHeader; ///< The COM+ descriptor.
+
+ void read(InputBuffer& inputbuffer);
+
+ public:
+ /// Read a file's COM+ runtime descriptor directory.
+ int read(const std::string& strFilename, unsigned int uiOffset, unsigned int uiSize); // EXPORT
+ int read(unsigned char* buffer, unsigned int buffersize); // EXPORT
+ /// Rebuild the COM+ descriptor.
+ void rebuild(std::vector<byte>& vBuffer) const; // EXPORT
+ /// Returns the size of the current COM+ descriptor.
+ unsigned int size() const; // EXPORT
+ /// Writes the current COM+ descriptor directory to a file.
+ int write(const std::string& strFilename, unsigned int dwOffset) const; // EXPORT
+
+ /// Get the COM+ descriptor's SizeOfHeader (cb) value.
+ dword getSizeOfHeader() const; // EXPORT
+ /// Get the COM+ descriptor's MajorRuntimeVersion value.
+ word getMajorRuntimeVersion() const; // EXPORT
+ /// Get the COM+ descriptor's MinorRuntimeVersion value.
+ word getMinorRuntimeVersion() const; // EXPORT
+ /// Get the COM+ descriptor's MetaData (Virtual Address) value.
+ dword getMetaDataVa() const; // EXPORT
+ /// Get the COM+ descriptor's MetaData (Size) value.
+ dword getMetaDataSize() const; // EXPORT
+ /// Get the COM+ descriptor's Flags value.
+ dword getFlags() const; // EXPORT
+ /// Get the COM+ descriptor's EntryPointToken value.
+ dword getEntryPointToken() const; // EXPORT
+ /// Get the COM+ descriptor's Resources (Virtual Address) value.
+ dword getResourcesVa() const; // EXPORT
+ /// Get the COM+ descriptor's Resources (Size) value.
+ dword getResourcesSize(); // EXPORT
+ /// Get the COM+ descriptor's StrongNameSignature (Virtual Address) value.
+ dword getStrongNameSignatureVa() const; // EXPORT
+ /// Get the COM+ descriptor's StrongNameSignature (Size) value.
+ dword getStrongNameSignagureSize() const; // EXPORT
+ /// Get the COM+ descriptor's CodeManagerTable (Virtual Address) value.
+ dword getCodeManagerTableVa() const; // EXPORT
+ /// Get the COM+ descriptor's CodeManagerTable (Size) value.
+ dword getCodeManagerTableSize() const; // EXPORT
+ /// Get the COM+ descriptor's VTableFixup (Virtual Address) value.
+ dword getVTableFixupsVa() const; // EXPORT
+ /// Get the COM+ descriptor's VTableFixup (Size) value.
+ dword getVTableFixupsSize() const; // EXPORT
+ /// Get the COM+ descriptor's ExportAddressTable (Virtual Address) value.
+ dword getExportAddressTableJumpsVa() const; // EXPORT
+ /// Get the COM+ descriptor's ExportAddressTable (Size) value.
+ dword getExportAddressTableJumpsSize() const; // EXPORT
+ /// Get the COM+ descriptor's ManagedNativeHeader (Virtual Address) value.
+ dword getManagedNativeHeaderVa() const; // EXPORT
+ /// Get the COM+ descriptor's ManagedNativeHeader (Size) value.
+ dword getManagedNativeHeaderSize() const; // EXPORT
+
+ /// Change the COM+ descriptor's SizeOfHeader (cb) value.
+ void setSizeOfHeader(dword dwValue); // EXPORT
+ /// Change the COM+ descriptor's MajorRuntimeVersion value.
+ void setMajorRuntimeVersion(word wValue); // EXPORT
+ /// Change the COM+ descriptor's MinorRuntimeVersion value.
+ void setMinorRuntimeVersion(word wValue); // EXPORT
+ /// Change the COM+ descriptor's MetaData (VirtualAddress) value.
+ void setMetaDataVa(dword dwValue); // EXPORT
+ /// Change the COM+ descriptor's MetaData (Size) value.
+ void setMetaDataSize(dword dwValue); // EXPORT
+ /// Change the COM+ descriptor's Flags value.
+ void setFlags(dword dwValue); // EXPORT
+ /// Change the COM+ descriptor's EntryPointToken value.
+ void setEntryPointToken(dword dwValue); // EXPORT
+ /// Change the COM+ descriptor's Resources (VirtualAddress) value.
+ void setResourcesVa(dword dwValue); // EXPORT
+ /// Change the COM+ descriptor's Resources (Size) value.
+ void setResourcesSize(dword dwValue); // EXPORT
+ /// Change the COM+ descriptor's StrongNameSignatureVa (VirtualAddress) value.
+ void setStrongNameSignatureVa(dword dwValue); // EXPORT
+ /// Change the COM+ descriptor's StrongNameSignatureVa (Size) value.
+ void setStrongNameSignagureSize(dword dwValue); // EXPORT
+ /// Change the COM+ descriptor's CodeManagerTable (VirtualAddress) value.
+ void setCodeManagerTableVa(dword dwValue); // EXPORT
+ /// Change the COM+ descriptor's CodeManagerTable (Size) value.
+ void setCodeManagerTableSize(dword dwValue); // EXPORT
+ /// Change the COM+ descriptor's VTableFixups (VirtualAddress) value.
+ void setVTableFixupsVa(dword dwValue); // EXPORT
+ /// Change the COM+ descriptor's VTableFixups (Size) value.
+ void setVTableFixupsSize(dword dwValue); // EXPORT
+ /// Change the COM+ descriptor's ExportAddressTableJumps (VirtualAddress) value.
+ void setExportAddressTableJumpsVa(dword dwValue); // EXPORT
+ /// Change the COM+ descriptor's ExportAddressTableJumps (Size) value.
+ void setExportAddressTableJumpsSize(dword dwValue); // EXPORT
+ /// Change the COM+ descriptor's ManagedNativeHeader (VirtualAddress) value.
+ void setManagedNativeHeaderVa(dword dwValue); // EXPORT
+ /// Change the COM+ descriptor's ManagedNativeHeader (Size) value.
+ void setManagedNativeHeaderSize(dword dwValue); // EXPORT
+ };
+}
+#endif
diff --git a/utils/zenutils/libraries/pelib-0.9/pelib/DebugDirectory.cpp b/utils/zenutils/libraries/pelib-0.9/pelib/DebugDirectory.cpp
index eb3d5c5600..9a91c7b18c 100755..100644
--- a/utils/zenutils/libraries/pelib-0.9/pelib/DebugDirectory.cpp
+++ b/utils/zenutils/libraries/pelib-0.9/pelib/DebugDirectory.cpp
@@ -1,383 +1,383 @@
-/*
-* DebugDirectory.cpp - Part of the PeLib library.
-*
-* Copyright (c) 2004 - 2005 Sebastian Porst (webmaster@the-interweb.com)
-* All rights reserved.
-*
-* This software is licensed under the zlib/libpng License.
-* For more details see http://www.opensource.org/licenses/zlib-license.php
-* or the license information file (license.htm) in the root directory
-* of PeLib.
-*/
-
-#include "PeLibInc.h"
-#include "DebugDirectory.h"
-
-namespace PeLib
-{
- void DebugDirectory::clear()
- {
- m_vDebugInfo.clear();
- }
-
- std::vector<PELIB_IMG_DEBUG_DIRECTORY> DebugDirectory::read(InputBuffer& ibBuffer, unsigned int uiSize)
- {
- std::vector<PELIB_IMG_DEBUG_DIRECTORY> currDebugInfo;
-
- PELIB_IMG_DEBUG_DIRECTORY iddCurr;
-
- for (unsigned int i=0;i<uiSize/PELIB_IMAGE_DEBUG_DIRECTORY::size();i++)
- {
-
- ibBuffer >> iddCurr.idd.Characteristics;
- ibBuffer >> iddCurr.idd.TimeDateStamp;
- ibBuffer >> iddCurr.idd.MajorVersion;
- ibBuffer >> iddCurr.idd.MinorVersion;
- ibBuffer >> iddCurr.idd.Type;
- ibBuffer >> iddCurr.idd.SizeOfData;
- ibBuffer >> iddCurr.idd.AddressOfRawData;
- ibBuffer >> iddCurr.idd.PointerToRawData;
-
- currDebugInfo.push_back(iddCurr);
- }
-
- return currDebugInfo;
- }
-
- int DebugDirectory::read(unsigned char* buffer, unsigned int buffersize)
- {
- // XXX: Note, debug data is not read at all. This might or might not change
- // in the future.
-
- std::vector<byte> vDebugDirectory(buffer, buffer + buffersize);
-
- InputBuffer ibBuffer(vDebugDirectory);
-
- std::vector<PELIB_IMG_DEBUG_DIRECTORY> currDebugInfo = read(ibBuffer, buffersize);
-
- std::swap(currDebugInfo, m_vDebugInfo);
-
- return NO_ERROR;
- }
-
- /**
- * @param strFilename Name of the file which will be read.
- * @param uiOffset File offset of the Debug directory.
- * @param uiSize Size of the Debug directory.
- **/
- int DebugDirectory::read(const std::string& strFilename, unsigned int uiOffset, unsigned int uiSize)
- {
- std::ifstream ifFile(strFilename.c_str(), std::ios::binary);
- unsigned int ulFileSize = fileSize(ifFile);
-
- if (!ifFile)
- {
- return ERROR_OPENING_FILE;
- }
-
- if (ulFileSize < uiOffset + uiSize)
- {
- return ERROR_INVALID_FILE;
- }
-
- ifFile.seekg(uiOffset, std::ios::beg);
-
- std::vector<byte> vDebugDirectory(uiSize);
- ifFile.read(reinterpret_cast<char*>(&vDebugDirectory[0]), uiSize);
-
- InputBuffer ibBuffer(vDebugDirectory);
-
- std::vector<PELIB_IMG_DEBUG_DIRECTORY> currDebugInfo = read(ibBuffer, uiSize);
-
- for (unsigned int i=0;i<currDebugInfo.size();i++)
- {
- ifFile.seekg(currDebugInfo[i].idd.PointerToRawData, std::ios::beg);
- currDebugInfo[i].data.resize(currDebugInfo[i].idd.SizeOfData);
- ifFile.read(reinterpret_cast<char*>(&currDebugInfo[i].data[0]), currDebugInfo[i].idd.SizeOfData);
- if (!ifFile) return ERROR_INVALID_FILE;
- }
-
- std::swap(currDebugInfo, m_vDebugInfo);
-
- return NO_ERROR;
- }
-
- /**
- * Rebuilds the current debug directory.
- * @param vBuffer Buffer where the rebuilt directory is stored.
- **/
- void DebugDirectory::rebuild(std::vector<byte>& vBuffer) const
- {
- OutputBuffer obBuffer(vBuffer);
-
- for (unsigned int i=0;i<m_vDebugInfo.size();i++)
- {
- obBuffer << m_vDebugInfo[i].idd.Characteristics;
- obBuffer << m_vDebugInfo[i].idd.TimeDateStamp;
- obBuffer << m_vDebugInfo[i].idd.MajorVersion;
- obBuffer << m_vDebugInfo[i].idd.MinorVersion;
- obBuffer << m_vDebugInfo[i].idd.Type;
- obBuffer << m_vDebugInfo[i].idd.SizeOfData;
- obBuffer << m_vDebugInfo[i].idd.AddressOfRawData;
- obBuffer << m_vDebugInfo[i].idd.PointerToRawData;
- }
- }
-
- /**
- * @return Size of the debug directory.
- **/
- unsigned int DebugDirectory::size() const
- {
- return static_cast<unsigned int>(m_vDebugInfo.size()) * PELIB_IMAGE_DEBUG_DIRECTORY::size();
- }
-
- /**
- * @param strFilename Name of the file which will be written.
- * @param uiOffset File offset where the debug directory will be stored.
- **/
- int DebugDirectory::write(const std::string& strFilename, unsigned int uiOffset) const
- {
- std::fstream ofFile(strFilename.c_str(), std::ios_base::in);
-
- if (!ofFile)
- {
- ofFile.clear();
- ofFile.open(strFilename.c_str(), std::ios_base::out | std::ios_base::binary);
- }
- else
- {
- ofFile.close();
- ofFile.open(strFilename.c_str(), std::ios_base::in | std::ios_base::out | std::ios_base::binary);
- }
-
- if (!ofFile)
- {
- return ERROR_OPENING_FILE;
- }
-
- ofFile.seekp(uiOffset, std::ios::beg);
-
- std::vector<unsigned char> vBuffer;
- rebuild(vBuffer);
-
- ofFile.write(reinterpret_cast<const char*>(&vBuffer[0]), static_cast<unsigned int>(vBuffer.size()));
-
- ofFile.close();
-
- return NO_ERROR;
- }
-
- /**
- * @return Number of debug structures in the current Debug directory.
- **/
- unsigned int DebugDirectory::calcNumberOfEntries() const
- {
- return static_cast<unsigned int>(m_vDebugInfo.size());
- }
-
- /**
- * Adds a new debug structure to the debug directory. The initial values of all members of the structure
- * are undefined.
- **/
- void DebugDirectory::addEntry()
- {
- PELIB_IMG_DEBUG_DIRECTORY p;
- m_vDebugInfo.push_back(p);
- }
-
- /**
- * Removes a debug structure from the current debug directory. If an invalid structure is specified
- * by the parameter uiIndex the result will be undefined behaviour.
- * @param uiIndex Identifies the debug structure.
- **/
- void DebugDirectory::removeEntry(unsigned int uiIndex)
- {
- m_vDebugInfo.erase(m_vDebugInfo.begin() + uiIndex);
- }
-
- /**
- * Returns the Characteristics value of a debug structure. If an invalid structure is specified
- * by the parameter uiIndex the result will be undefined behaviour.
- * @param uiIndex Identifies the debug structure.
- * @return Characteristics value of the debug structure.
- **/
- dword DebugDirectory::getCharacteristics(unsigned int uiIndex) const
- {
- return m_vDebugInfo[uiIndex].idd.Characteristics;
- }
-
- /**
- * Returns the TimeDateStamp value of a debug structure. If an invalid structure is specified
- * by the parameter uiIndex the result will be undefined behaviour.
- * @param uiIndex Identifies the debug structure.
- * @return TimeDateStamp value of the debug structure.
- **/
- dword DebugDirectory::getTimeDateStamp(unsigned int uiIndex) const
- {
- return m_vDebugInfo[uiIndex].idd.TimeDateStamp;
- }
-
- /**
- * Returns the MajorVersion value of a debug structure. If an invalid structure is specified
- * by the parameter uiIndex the result will be undefined behaviour.
- * @param uiIndex Identifies the debug structure.
- * @return MajorVersion value of the debug structure.
- **/
- word DebugDirectory::getMajorVersion(unsigned int uiIndex) const
- {
- return m_vDebugInfo[uiIndex].idd.MajorVersion;
- }
-
- /**
- * Returns the MinorVersion value of a debug structure. If an invalid structure is specified
- * by the parameter uiIndex the result will be undefined behaviour.
- * @param uiIndex Identifies the debug structure.
- * @return MinorVersion value of the debug structure.
- **/
- word DebugDirectory::getMinorVersion(unsigned int uiIndex) const
- {
- return m_vDebugInfo[uiIndex].idd.MinorVersion;
- }
-
- /**
- * Returns the Type value of a debug structure. If an invalid structure is specified
- * by the parameter uiIndex the result will be undefined behaviour.
- * @param uiIndex Identifies the debug structure.
- * @return Type value of the debug structure.
- **/
- dword DebugDirectory::getType(unsigned int uiIndex) const
- {
- return m_vDebugInfo[uiIndex].idd.Type;
- }
-
- /**
- * Returns the SizeOfData value of a debug structure. If an invalid structure is specified
- * by the parameter uiIndex the result will be undefined behaviour.
- * @param uiIndex Identifies the debug structure.
- * @return SizeOfData value of the debug structure.
- **/
- dword DebugDirectory::getSizeOfData(unsigned int uiIndex) const
- {
- return m_vDebugInfo[uiIndex].idd.SizeOfData;
- }
-
- /**
- * Returns the AddressOfRawData value of a debug structure. If an invalid structure is specified
- * by the parameter uiIndex the result will be undefined behaviour.
- * @param uiIndex Identifies the debug structure.
- * @return AddressOfRawData value of the debug structure.
- **/
- dword DebugDirectory::getAddressOfRawData(unsigned int uiIndex) const
- {
- return m_vDebugInfo[uiIndex].idd.AddressOfRawData;
- }
-
- /**
- * Returns the PointerToRawData value of a debug structure. If an invalid structure is specified
- * by the parameter uiIndex the result will be undefined behaviour.
- * @param uiIndex Identifies the debug structure.
- * @return PointerToRawData value of the debug structure.
- **/
- dword DebugDirectory::getPointerToRawData(unsigned int uiIndex) const
- {
- return m_vDebugInfo[uiIndex].idd.PointerToRawData;
- }
-
- std::vector<byte> DebugDirectory::getData(unsigned int index) const
- {
- return m_vDebugInfo[index].data;
- }
-
- /**
- * Changes the Characteristics value of a debug structure. If an invalid structure is specified
- * by the parameter uiIndex the result will be undefined behaviour.
- * @param uiIndex Identifies the debug structure.
- * @param dwValue New value of the Characteristics value of the debug structure.
- **/
- void DebugDirectory::setCharacteristics(unsigned int uiIndex, dword dwValue)
- {
- m_vDebugInfo[uiIndex].idd.Characteristics = dwValue;
- }
-
- /**
- * Changes the TimeDateStamp value of a debug structure. If an invalid structure is specified
- * by the parameter uiIndex the result will be undefined behaviour.
- * @param uiIndex Identifies the debug structure.
- * @param dwValue New value of the TimeDateStamp value of the debug structure.
- **/
- void DebugDirectory::setTimeDateStamp(unsigned int uiIndex, dword dwValue)
- {
- m_vDebugInfo[uiIndex].idd.TimeDateStamp = dwValue;
- }
-
- /**
- * Changes the MajorVersion value of a debug structure. If an invalid structure is specified
- * by the parameter uiIndex the result will be undefined behaviour.
- * @param uiIndex Identifies the debug structure.
- * @param wValue New value of the MajorVersion value of the debug structure.
- **/
- void DebugDirectory::setMajorVersion(unsigned int uiIndex, word wValue)
- {
- m_vDebugInfo[uiIndex].idd.MajorVersion = wValue;
- }
-
- /**
- * Changes the MinorVersion value of a debug structure. If an invalid structure is specified
- * by the parameter uiIndex the result will be undefined behaviour.
- * @param uiIndex Identifies the debug structure.
- * @param wValue New value of the MinorVersion value of the debug structure.
- **/
- void DebugDirectory::setMinorVersion(unsigned int uiIndex, word wValue)
- {
- m_vDebugInfo[uiIndex].idd.MinorVersion = wValue;
- }
-
- /**
- * Changes the Type value of a debug structure. If an invalid structure is specified
- * by the parameter uiIndex the result will be undefined behaviour.
- * @param uiIndex Identifies the debug structure.
- * @param dwValue New value of the Type value of the debug structure.
- **/
- void DebugDirectory::setType(unsigned int uiIndex, dword dwValue)
- {
- m_vDebugInfo[uiIndex].idd.Type = dwValue;
- }
-
- /**
- * Changes the SizeOfData value of a debug structure. If an invalid structure is specified
- * by the parameter uiIndex the result will be undefined behaviour.
- * @param uiIndex Identifies the debug structure.
- * @param dwValue New value of the SizeOfData value of the debug structure.
- **/
- void DebugDirectory::setSizeOfData(unsigned int uiIndex, dword dwValue)
- {
- m_vDebugInfo[uiIndex].idd.SizeOfData = dwValue;
- }
-
- /**
- * Changes the AddressOfRawData value of a debug structure. If an invalid structure is specified
- * by the parameter uiIndex the result will be undefined behaviour.
- * @param uiIndex Identifies the debug structure.
- * @param dwValue New value of the AddressOfRawData value of the debug structure.
- **/
- void DebugDirectory::setAddressOfRawData(unsigned int uiIndex, dword dwValue)
- {
- m_vDebugInfo[uiIndex].idd.AddressOfRawData = dwValue;
- }
-
- /**
- * Changes the PointerToRawData value of a debug structure. If an invalid structure is specified
- * by the parameter uiIndex the result will be undefined behaviour.
- * @param uiIndex Identifies the debug structure.
- * @param dwValue New value of the PointerToRawData value of the debug structure.
- **/
- void DebugDirectory::setPointerToRawData(unsigned int uiIndex, dword dwValue)
- {
- m_vDebugInfo[uiIndex].idd.PointerToRawData = dwValue;
- }
-
- void DebugDirectory::setData(unsigned int index, const std::vector<byte>& data)
- {
- m_vDebugInfo[index].data = data;
- }
-}
+/*
+* DebugDirectory.cpp - Part of the PeLib library.
+*
+* Copyright (c) 2004 - 2005 Sebastian Porst (webmaster@the-interweb.com)
+* All rights reserved.
+*
+* This software is licensed under the zlib/libpng License.
+* For more details see http://www.opensource.org/licenses/zlib-license.php
+* or the license information file (license.htm) in the root directory
+* of PeLib.
+*/
+
+#include "PeLibInc.h"
+#include "DebugDirectory.h"
+
+namespace PeLib
+{
+ void DebugDirectory::clear()
+ {
+ m_vDebugInfo.clear();
+ }
+
+ std::vector<PELIB_IMG_DEBUG_DIRECTORY> DebugDirectory::read(InputBuffer& ibBuffer, unsigned int uiSize)
+ {
+ std::vector<PELIB_IMG_DEBUG_DIRECTORY> currDebugInfo;
+
+ PELIB_IMG_DEBUG_DIRECTORY iddCurr;
+
+ for (unsigned int i=0;i<uiSize/PELIB_IMAGE_DEBUG_DIRECTORY::size();i++)
+ {
+
+ ibBuffer >> iddCurr.idd.Characteristics;
+ ibBuffer >> iddCurr.idd.TimeDateStamp;
+ ibBuffer >> iddCurr.idd.MajorVersion;
+ ibBuffer >> iddCurr.idd.MinorVersion;
+ ibBuffer >> iddCurr.idd.Type;
+ ibBuffer >> iddCurr.idd.SizeOfData;
+ ibBuffer >> iddCurr.idd.AddressOfRawData;
+ ibBuffer >> iddCurr.idd.PointerToRawData;
+
+ currDebugInfo.push_back(iddCurr);
+ }
+
+ return currDebugInfo;
+ }
+
+ int DebugDirectory::read(unsigned char* buffer, unsigned int buffersize)
+ {
+ // XXX: Note, debug data is not read at all. This might or might not change
+ // in the future.
+
+ std::vector<byte> vDebugDirectory(buffer, buffer + buffersize);
+
+ InputBuffer ibBuffer(vDebugDirectory);
+
+ std::vector<PELIB_IMG_DEBUG_DIRECTORY> currDebugInfo = read(ibBuffer, buffersize);
+
+ std::swap(currDebugInfo, m_vDebugInfo);
+
+ return NO_ERROR;
+ }
+
+ /**
+ * @param strFilename Name of the file which will be read.
+ * @param uiOffset File offset of the Debug directory.
+ * @param uiSize Size of the Debug directory.
+ **/
+ int DebugDirectory::read(const std::string& strFilename, unsigned int uiOffset, unsigned int uiSize)
+ {
+ std::ifstream ifFile(strFilename.c_str(), std::ios::binary);
+ unsigned int ulFileSize = fileSize(ifFile);
+
+ if (!ifFile)
+ {
+ return ERROR_OPENING_FILE;
+ }
+
+ if (ulFileSize < uiOffset + uiSize)
+ {
+ return ERROR_INVALID_FILE;
+ }
+
+ ifFile.seekg(uiOffset, std::ios::beg);
+
+ std::vector<byte> vDebugDirectory(uiSize);
+ ifFile.read(reinterpret_cast<char*>(&vDebugDirectory[0]), uiSize);
+
+ InputBuffer ibBuffer(vDebugDirectory);
+
+ std::vector<PELIB_IMG_DEBUG_DIRECTORY> currDebugInfo = read(ibBuffer, uiSize);
+
+ for (unsigned int i=0;i<currDebugInfo.size();i++)
+ {
+ ifFile.seekg(currDebugInfo[i].idd.PointerToRawData, std::ios::beg);
+ currDebugInfo[i].data.resize(currDebugInfo[i].idd.SizeOfData);
+ ifFile.read(reinterpret_cast<char*>(&currDebugInfo[i].data[0]), currDebugInfo[i].idd.SizeOfData);
+ if (!ifFile) return ERROR_INVALID_FILE;
+ }
+
+ std::swap(currDebugInfo, m_vDebugInfo);
+
+ return NO_ERROR;
+ }
+
+ /**
+ * Rebuilds the current debug directory.
+ * @param vBuffer Buffer where the rebuilt directory is stored.
+ **/
+ void DebugDirectory::rebuild(std::vector<byte>& vBuffer) const
+ {
+ OutputBuffer obBuffer(vBuffer);
+
+ for (unsigned int i=0;i<m_vDebugInfo.size();i++)
+ {
+ obBuffer << m_vDebugInfo[i].idd.Characteristics;
+ obBuffer << m_vDebugInfo[i].idd.TimeDateStamp;
+ obBuffer << m_vDebugInfo[i].idd.MajorVersion;
+ obBuffer << m_vDebugInfo[i].idd.MinorVersion;
+ obBuffer << m_vDebugInfo[i].idd.Type;
+ obBuffer << m_vDebugInfo[i].idd.SizeOfData;
+ obBuffer << m_vDebugInfo[i].idd.AddressOfRawData;
+ obBuffer << m_vDebugInfo[i].idd.PointerToRawData;
+ }
+ }
+
+ /**
+ * @return Size of the debug directory.
+ **/
+ unsigned int DebugDirectory::size() const
+ {
+ return static_cast<unsigned int>(m_vDebugInfo.size()) * PELIB_IMAGE_DEBUG_DIRECTORY::size();
+ }
+
+ /**
+ * @param strFilename Name of the file which will be written.
+ * @param uiOffset File offset where the debug directory will be stored.
+ **/
+ int DebugDirectory::write(const std::string& strFilename, unsigned int uiOffset) const
+ {
+ std::fstream ofFile(strFilename.c_str(), std::ios_base::in);
+
+ if (!ofFile)
+ {
+ ofFile.clear();
+ ofFile.open(strFilename.c_str(), std::ios_base::out | std::ios_base::binary);
+ }
+ else
+ {
+ ofFile.close();
+ ofFile.open(strFilename.c_str(), std::ios_base::in | std::ios_base::out | std::ios_base::binary);
+ }
+
+ if (!ofFile)
+ {
+ return ERROR_OPENING_FILE;
+ }
+
+ ofFile.seekp(uiOffset, std::ios::beg);
+
+ std::vector<unsigned char> vBuffer;
+ rebuild(vBuffer);
+
+ ofFile.write(reinterpret_cast<const char*>(&vBuffer[0]), static_cast<unsigned int>(vBuffer.size()));
+
+ ofFile.close();
+
+ return NO_ERROR;
+ }
+
+ /**
+ * @return Number of debug structures in the current Debug directory.
+ **/
+ unsigned int DebugDirectory::calcNumberOfEntries() const
+ {
+ return static_cast<unsigned int>(m_vDebugInfo.size());
+ }
+
+ /**
+ * Adds a new debug structure to the debug directory. The initial values of all members of the structure
+ * are undefined.
+ **/
+ void DebugDirectory::addEntry()
+ {
+ PELIB_IMG_DEBUG_DIRECTORY p;
+ m_vDebugInfo.push_back(p);
+ }
+
+ /**
+ * Removes a debug structure from the current debug directory. If an invalid structure is specified
+ * by the parameter uiIndex the result will be undefined behaviour.
+ * @param uiIndex Identifies the debug structure.
+ **/
+ void DebugDirectory::removeEntry(unsigned int uiIndex)
+ {
+ m_vDebugInfo.erase(m_vDebugInfo.begin() + uiIndex);
+ }
+
+ /**
+ * Returns the Characteristics value of a debug structure. If an invalid structure is specified
+ * by the parameter uiIndex the result will be undefined behaviour.
+ * @param uiIndex Identifies the debug structure.
+ * @return Characteristics value of the debug structure.
+ **/
+ dword DebugDirectory::getCharacteristics(unsigned int uiIndex) const
+ {
+ return m_vDebugInfo[uiIndex].idd.Characteristics;
+ }
+
+ /**
+ * Returns the TimeDateStamp value of a debug structure. If an invalid structure is specified
+ * by the parameter uiIndex the result will be undefined behaviour.
+ * @param uiIndex Identifies the debug structure.
+ * @return TimeDateStamp value of the debug structure.
+ **/
+ dword DebugDirectory::getTimeDateStamp(unsigned int uiIndex) const
+ {
+ return m_vDebugInfo[uiIndex].idd.TimeDateStamp;
+ }
+
+ /**
+ * Returns the MajorVersion value of a debug structure. If an invalid structure is specified
+ * by the parameter uiIndex the result will be undefined behaviour.
+ * @param uiIndex Identifies the debug structure.
+ * @return MajorVersion value of the debug structure.
+ **/
+ word DebugDirectory::getMajorVersion(unsigned int uiIndex) const
+ {
+ return m_vDebugInfo[uiIndex].idd.MajorVersion;
+ }
+
+ /**
+ * Returns the MinorVersion value of a debug structure. If an invalid structure is specified
+ * by the parameter uiIndex the result will be undefined behaviour.
+ * @param uiIndex Identifies the debug structure.
+ * @return MinorVersion value of the debug structure.
+ **/
+ word DebugDirectory::getMinorVersion(unsigned int uiIndex) const
+ {
+ return m_vDebugInfo[uiIndex].idd.MinorVersion;
+ }
+
+ /**
+ * Returns the Type value of a debug structure. If an invalid structure is specified
+ * by the parameter uiIndex the result will be undefined behaviour.
+ * @param uiIndex Identifies the debug structure.
+ * @return Type value of the debug structure.
+ **/
+ dword DebugDirectory::getType(unsigned int uiIndex) const
+ {
+ return m_vDebugInfo[uiIndex].idd.Type;
+ }
+
+ /**
+ * Returns the SizeOfData value of a debug structure. If an invalid structure is specified
+ * by the parameter uiIndex the result will be undefined behaviour.
+ * @param uiIndex Identifies the debug structure.
+ * @return SizeOfData value of the debug structure.
+ **/
+ dword DebugDirectory::getSizeOfData(unsigned int uiIndex) const
+ {
+ return m_vDebugInfo[uiIndex].idd.SizeOfData;
+ }
+
+ /**
+ * Returns the AddressOfRawData value of a debug structure. If an invalid structure is specified
+ * by the parameter uiIndex the result will be undefined behaviour.
+ * @param uiIndex Identifies the debug structure.
+ * @return AddressOfRawData value of the debug structure.
+ **/
+ dword DebugDirectory::getAddressOfRawData(unsigned int uiIndex) const
+ {
+ return m_vDebugInfo[uiIndex].idd.AddressOfRawData;
+ }
+
+ /**
+ * Returns the PointerToRawData value of a debug structure. If an invalid structure is specified
+ * by the parameter uiIndex the result will be undefined behaviour.
+ * @param uiIndex Identifies the debug structure.
+ * @return PointerToRawData value of the debug structure.
+ **/
+ dword DebugDirectory::getPointerToRawData(unsigned int uiIndex) const
+ {
+ return m_vDebugInfo[uiIndex].idd.PointerToRawData;
+ }
+
+ std::vector<byte> DebugDirectory::getData(unsigned int index) const
+ {
+ return m_vDebugInfo[index].data;
+ }
+
+ /**
+ * Changes the Characteristics value of a debug structure. If an invalid structure is specified
+ * by the parameter uiIndex the result will be undefined behaviour.
+ * @param uiIndex Identifies the debug structure.
+ * @param dwValue New value of the Characteristics value of the debug structure.
+ **/
+ void DebugDirectory::setCharacteristics(unsigned int uiIndex, dword dwValue)
+ {
+ m_vDebugInfo[uiIndex].idd.Characteristics = dwValue;
+ }
+
+ /**
+ * Changes the TimeDateStamp value of a debug structure. If an invalid structure is specified
+ * by the parameter uiIndex the result will be undefined behaviour.
+ * @param uiIndex Identifies the debug structure.
+ * @param dwValue New value of the TimeDateStamp value of the debug structure.
+ **/
+ void DebugDirectory::setTimeDateStamp(unsigned int uiIndex, dword dwValue)
+ {
+ m_vDebugInfo[uiIndex].idd.TimeDateStamp = dwValue;
+ }
+
+ /**
+ * Changes the MajorVersion value of a debug structure. If an invalid structure is specified
+ * by the parameter uiIndex the result will be undefined behaviour.
+ * @param uiIndex Identifies the debug structure.
+ * @param wValue New value of the MajorVersion value of the debug structure.
+ **/
+ void DebugDirectory::setMajorVersion(unsigned int uiIndex, word wValue)
+ {
+ m_vDebugInfo[uiIndex].idd.MajorVersion = wValue;
+ }
+
+ /**
+ * Changes the MinorVersion value of a debug structure. If an invalid structure is specified
+ * by the parameter uiIndex the result will be undefined behaviour.
+ * @param uiIndex Identifies the debug structure.
+ * @param wValue New value of the MinorVersion value of the debug structure.
+ **/
+ void DebugDirectory::setMinorVersion(unsigned int uiIndex, word wValue)
+ {
+ m_vDebugInfo[uiIndex].idd.MinorVersion = wValue;
+ }
+
+ /**
+ * Changes the Type value of a debug structure. If an invalid structure is specified
+ * by the parameter uiIndex the result will be undefined behaviour.
+ * @param uiIndex Identifies the debug structure.
+ * @param dwValue New value of the Type value of the debug structure.
+ **/
+ void DebugDirectory::setType(unsigned int uiIndex, dword dwValue)
+ {
+ m_vDebugInfo[uiIndex].idd.Type = dwValue;
+ }
+
+ /**
+ * Changes the SizeOfData value of a debug structure. If an invalid structure is specified
+ * by the parameter uiIndex the result will be undefined behaviour.
+ * @param uiIndex Identifies the debug structure.
+ * @param dwValue New value of the SizeOfData value of the debug structure.
+ **/
+ void DebugDirectory::setSizeOfData(unsigned int uiIndex, dword dwValue)
+ {
+ m_vDebugInfo[uiIndex].idd.SizeOfData = dwValue;
+ }
+
+ /**
+ * Changes the AddressOfRawData value of a debug structure. If an invalid structure is specified
+ * by the parameter uiIndex the result will be undefined behaviour.
+ * @param uiIndex Identifies the debug structure.
+ * @param dwValue New value of the AddressOfRawData value of the debug structure.
+ **/
+ void DebugDirectory::setAddressOfRawData(unsigned int uiIndex, dword dwValue)
+ {
+ m_vDebugInfo[uiIndex].idd.AddressOfRawData = dwValue;
+ }
+
+ /**
+ * Changes the PointerToRawData value of a debug structure. If an invalid structure is specified
+ * by the parameter uiIndex the result will be undefined behaviour.
+ * @param uiIndex Identifies the debug structure.
+ * @param dwValue New value of the PointerToRawData value of the debug structure.
+ **/
+ void DebugDirectory::setPointerToRawData(unsigned int uiIndex, dword dwValue)
+ {
+ m_vDebugInfo[uiIndex].idd.PointerToRawData = dwValue;
+ }
+
+ void DebugDirectory::setData(unsigned int index, const std::vector<byte>& data)
+ {
+ m_vDebugInfo[index].data = data;
+ }
+}
diff --git a/utils/zenutils/libraries/pelib-0.9/pelib/DebugDirectory.h b/utils/zenutils/libraries/pelib-0.9/pelib/DebugDirectory.h
index 0ffda38815..adfef6b52e 100755..100644
--- a/utils/zenutils/libraries/pelib-0.9/pelib/DebugDirectory.h
+++ b/utils/zenutils/libraries/pelib-0.9/pelib/DebugDirectory.h
@@ -1,84 +1,84 @@
-/*
-* DebugDirectory.h - Part of the PeLib library.
-*
-* Copyright (c) 2004 - 2005 Sebastian Porst (webmaster@the-interweb.com)
-* All rights reserved.
-*
-* This software is licensed under the zlib/libpng License.
-* For more details see http://www.opensource.org/licenses/zlib-license.php
-* or the license information file (license.htm) in the root directory
-* of PeLib.
-*/
-
-#ifndef DEBUGDIRECTORY_H
-#define DEBUGDIRECTORY_H
-
-namespace PeLib
-{
- /// Class that handles the Debug directory.
- class DebugDirectory
- {
- private:
- /// Stores the various DebugDirectory structures.
- std::vector<PELIB_IMG_DEBUG_DIRECTORY> m_vDebugInfo;
-
- std::vector<PELIB_IMG_DEBUG_DIRECTORY> read(InputBuffer& ibBuffer, unsigned int uiSize);
-
- public:
- void clear(); // EXPORT
- /// Reads the Debug directory from a file.
- int read(const std::string& strFilename, unsigned int uiOffset, unsigned int uiSize); // EXPORT
- int read(unsigned char* buffer, unsigned int buffersize);
- /// Rebuilds the current Debug directory.
- void rebuild(std::vector<byte>& obBuffer) const; // EXPORT
- /// Returns the size the current Debug directory needs after rebuilding.
- unsigned int size() const;
- /// Writes the current Debug directory back to a file.
- int write(const std::string& strFilename, unsigned int uiOffset) const; // EXPORT
-
- /// Returns the number of DebugDirectory image structures in the current DebugDirectory.
- unsigned int calcNumberOfEntries() const; // EXPORT
-
- /// Adds a new debug structure.
- void addEntry(); // EXPORT
- /// Removes a debug structure.
- void removeEntry(unsigned int uiIndex); // EXPORT
-
- /// Returns the Characteristics value of a debug structure.
- dword getCharacteristics(unsigned int uiIndex) const; // EXPORT
- /// Returns the TimeDateStamp value of a debug structure.
- dword getTimeDateStamp(unsigned int uiIndex) const; // EXPORT
- /// Returns the MajorVersion value of a debug structure.
- word getMajorVersion(unsigned int uiIndex) const; // EXPORT
- /// Returns the MinorVersion value of a debug structure.
- word getMinorVersion(unsigned int uiIndex) const; // EXPORT
- /// Returns the Type value of a debug structure.
- dword getType(unsigned int uiIndex) const; // EXPORT
- /// Returns the SizeOfData value of a debug structure.
- dword getSizeOfData(unsigned int uiIndex) const; // EXPORT
- /// Returns the AddressOfRawData value of a debug structure.
- dword getAddressOfRawData(unsigned int uiIndex) const; // EXPORT
- /// Returns the PointerToRawData value of a debug structure.
- dword getPointerToRawData(unsigned int uiIndex) const; // EXPORT
- std::vector<byte> getData(unsigned int index) const; // EXPORT
-
- /// Sets the Characteristics value of a debug structure.
- void setCharacteristics(unsigned int uiIndex, dword dwValue); // EXPORT
- /// Sets the TimeDateStamp value of a debug structure.
- void setTimeDateStamp(unsigned int uiIndex, dword dwValue); // EXPORT
- /// Sets the MajorVersion value of a debug structure.
- void setMajorVersion(unsigned int uiIndex, word wValue); // EXPORT
- /// Sets the MinorVersion value of a debug structure.
- void setMinorVersion(unsigned int uiIndex, word wValue); // EXPORT
- /// Sets the Type value of a debug structure.
- void setType(unsigned int uiIndex, dword dwValue); // EXPORT
- /// Sets the SizeOfData value of a debug structure.
- void setSizeOfData(unsigned int uiIndex, dword dwValue); // EXPORT
- /// Sets the AddressOfRawData value of a debug structure.
- void setAddressOfRawData(unsigned int uiIndex, dword dwValue); // EXPORT
- /// Sets the PointerToRawData value of a debug structure.
- void setPointerToRawData(unsigned int uiIndex, dword dwValue); // EXPORT
- void setData(unsigned int index, const std::vector<byte>& data); // EXPORT
- };
-}
-#endif
+/*
+* DebugDirectory.h - Part of the PeLib library.
+*
+* Copyright (c) 2004 - 2005 Sebastian Porst (webmaster@the-interweb.com)
+* All rights reserved.
+*
+* This software is licensed under the zlib/libpng License.
+* For more details see http://www.opensource.org/licenses/zlib-license.php
+* or the license information file (license.htm) in the root directory
+* of PeLib.
+*/
+
+#ifndef DEBUGDIRECTORY_H
+#define DEBUGDIRECTORY_H
+
+namespace PeLib
+{
+ /// Class that handles the Debug directory.
+ class DebugDirectory
+ {
+ private:
+ /// Stores the various DebugDirectory structures.
+ std::vector<PELIB_IMG_DEBUG_DIRECTORY> m_vDebugInfo;
+
+ std::vector<PELIB_IMG_DEBUG_DIRECTORY> read(InputBuffer& ibBuffer, unsigned int uiSize);
+
+ public:
+ void clear(); // EXPORT
+ /// Reads the Debug directory from a file.
+ int read(const std::string& strFilename, unsigned int uiOffset, unsigned int uiSize); // EXPORT
+ int read(unsigned char* buffer, unsigned int buffersize);
+ /// Rebuilds the current Debug directory.
+ void rebuild(std::vector<byte>& obBuffer) const; // EXPORT
+ /// Returns the size the current Debug directory needs after rebuilding.
+ unsigned int size() const;
+ /// Writes the current Debug directory back to a file.
+ int write(const std::string& strFilename, unsigned int uiOffset) const; // EXPORT
+
+ /// Returns the number of DebugDirectory image structures in the current DebugDirectory.
+ unsigned int calcNumberOfEntries() const; // EXPORT
+
+ /// Adds a new debug structure.
+ void addEntry(); // EXPORT
+ /// Removes a debug structure.
+ void removeEntry(unsigned int uiIndex); // EXPORT
+
+ /// Returns the Characteristics value of a debug structure.
+ dword getCharacteristics(unsigned int uiIndex) const; // EXPORT
+ /// Returns the TimeDateStamp value of a debug structure.
+ dword getTimeDateStamp(unsigned int uiIndex) const; // EXPORT
+ /// Returns the MajorVersion value of a debug structure.
+ word getMajorVersion(unsigned int uiIndex) const; // EXPORT
+ /// Returns the MinorVersion value of a debug structure.
+ word getMinorVersion(unsigned int uiIndex) const; // EXPORT
+ /// Returns the Type value of a debug structure.
+ dword getType(unsigned int uiIndex) const; // EXPORT
+ /// Returns the SizeOfData value of a debug structure.
+ dword getSizeOfData(unsigned int uiIndex) const; // EXPORT
+ /// Returns the AddressOfRawData value of a debug structure.
+ dword getAddressOfRawData(unsigned int uiIndex) const; // EXPORT
+ /// Returns the PointerToRawData value of a debug structure.
+ dword getPointerToRawData(unsigned int uiIndex) const; // EXPORT
+ std::vector<byte> getData(unsigned int index) const; // EXPORT
+
+ /// Sets the Characteristics value of a debug structure.
+ void setCharacteristics(unsigned int uiIndex, dword dwValue); // EXPORT
+ /// Sets the TimeDateStamp value of a debug structure.
+ void setTimeDateStamp(unsigned int uiIndex, dword dwValue); // EXPORT
+ /// Sets the MajorVersion value of a debug structure.
+ void setMajorVersion(unsigned int uiIndex, word wValue); // EXPORT
+ /// Sets the MinorVersion value of a debug structure.
+ void setMinorVersion(unsigned int uiIndex, word wValue); // EXPORT
+ /// Sets the Type value of a debug structure.
+ void setType(unsigned int uiIndex, dword dwValue); // EXPORT
+ /// Sets the SizeOfData value of a debug structure.
+ void setSizeOfData(unsigned int uiIndex, dword dwValue); // EXPORT
+ /// Sets the AddressOfRawData value of a debug structure.
+ void setAddressOfRawData(unsigned int uiIndex, dword dwValue); // EXPORT
+ /// Sets the PointerToRawData value of a debug structure.
+ void setPointerToRawData(unsigned int uiIndex, dword dwValue); // EXPORT
+ void setData(unsigned int index, const std::vector<byte>& data); // EXPORT
+ };
+}
+#endif
diff --git a/utils/zenutils/libraries/pelib-0.9/pelib/ExportDirectory.cpp b/utils/zenutils/libraries/pelib-0.9/pelib/ExportDirectory.cpp
index aa9c28a50f..83c4b4e69d 100755..100644
--- a/utils/zenutils/libraries/pelib-0.9/pelib/ExportDirectory.cpp
+++ b/utils/zenutils/libraries/pelib-0.9/pelib/ExportDirectory.cpp
@@ -1,692 +1,692 @@
-/*
-* ExportDirectory.cpp - Part of the PeLib library.
-*
-* Copyright (c) 2004 - 2005 Sebastian Porst (webmaster@the-interweb.com)
-* All rights reserved.
-*
-* This software is licensed under the zlib/libpng License.
-* For more details see http://www.opensource.org/licenses/zlib-license.php
-* or the license information file (license.htm) in the root directory
-* of PeLib.
-*/
-
-//#ifdef false
-
-#include "PeLibInc.h"
-#include "ExportDirectory.h"
-
-namespace PeLib
-{
- /**
- * @param strFuncname Name of the function.
- * @param dwFuncAddr RVA of the function.
- **/
- void ExportDirectory::addFunction(const std::string& strFuncname, dword dwFuncAddr)
- {
- PELIB_EXP_FUNC_INFORMATION efiCurr;
- efiCurr.funcname = strFuncname;
- efiCurr.addroffunc = dwFuncAddr;
- m_ied.functions.push_back(efiCurr);
- }
-
- void ExportDirectory::removeFunction(unsigned int index)
- {
- m_ied.functions.erase(m_ied.functions.begin() + index);
- }
-
- void ExportDirectory::clear()
- {
- m_ied.functions.clear();
- }
-
- unsigned int ExportDirectory::calcNumberOfFunctions() const
- {
- return static_cast<unsigned int>(m_ied.functions.size());
- }
-
- /**
- * Identifies an exported function through it's name.
- * @param strFunctionName Name of the function
- * @return Number which identifies the functions.
- **/
- int ExportDirectory::getFunctionIndex(const std::string& strFunctionName) const
- {
- std::vector<PELIB_EXP_FUNC_INFORMATION>::const_iterator Iter = std::find_if(m_ied.functions.begin(), m_ied.functions.end(), std::bind2nd(std::mem_fun_ref(&PELIB_EXP_FUNC_INFORMATION::equal), strFunctionName));
-
- if (Iter == m_ied.functions.end())
- {
-// throw Exceptions::InvalidName(ExportDirectoryId, __LINE__);
- return -1;
- }
-
- return static_cast<int>(std::distance(m_ied.functions.begin(), Iter));
- }
-
- /**
- * @param strFilename Name of the file.
- * @param uiOffset File offset of the export directory.
- * @param uiSize Size of the export directory.
- * @param pehHeader A valid PE header which is necessary because some RVA calculations need to be done.
- * \todo: Proper use of InputBuffer
- **/
- int ExportDirectory::read(const std::string& strFilename, unsigned int uiOffset, unsigned int uiSize, const PeHeader& pehHeader)
- {
- std::ifstream ifFile(strFilename.c_str(), std::ios::binary);
-
- if (!ifFile)
- {
- return ERROR_OPENING_FILE;
- }
-
- unsigned int filesize = fileSize(ifFile);
-
- if (filesize < uiOffset + uiSize)
- {
- return ERROR_INVALID_FILE;
- }
-
- ifFile.seekg(uiOffset, std::ios::beg);
-
- PELIB_IMAGE_EXP_DIRECTORY iedCurr;
-
- std::vector<unsigned char> vExportdirectory(uiSize);
- ifFile.read(reinterpret_cast<char*>(&vExportdirectory[0]), uiSize);
-
- InputBuffer inpBuffer(vExportdirectory);
-
- inpBuffer >> iedCurr.ied.Characteristics;
- inpBuffer >> iedCurr.ied.TimeDateStamp;
- inpBuffer >> iedCurr.ied.MajorVersion;
- inpBuffer >> iedCurr.ied.MinorVersion;
- inpBuffer >> iedCurr.ied.Name;
- inpBuffer >> iedCurr.ied.Base;
- inpBuffer >> iedCurr.ied.NumberOfFunctions;
- inpBuffer >> iedCurr.ied.NumberOfNames;
- inpBuffer >> iedCurr.ied.AddressOfFunctions;
- inpBuffer >> iedCurr.ied.AddressOfNames;
- inpBuffer >> iedCurr.ied.AddressOfNameOrdinals;
-
- if (const PeHeader32* p32 = dynamic_cast<const PeHeader32*>(&pehHeader))
- {
- unsigned int offset = p32->rvaToOffset(iedCurr.ied.Name);
-
- if (offset >= filesize)
- return ERROR_INVALID_FILE;
-
- ifFile.seekg(offset, std::ios::beg);
- }
- else if (const PeHeader64* p64 = dynamic_cast<const PeHeader64*>(&pehHeader))
- {
- // XXX: Files might be > 4 GB
- unsigned int offset = static_cast<unsigned int>(p64->rvaToOffset(iedCurr.ied.Name));
-
- if (offset >= filesize)
- return ERROR_INVALID_FILE;
-
- ifFile.seekg(offset, std::ios::beg);
- }
-
- char c = 0;
- std::string strFname = "";
- do
- {
- ifFile.read(reinterpret_cast<char*>(&c), sizeof(c));
- if (!ifFile) return ERROR_INVALID_FILE;
- if (c) strFname += c;
- }
- while (c != 0);
- iedCurr.name = strFname;
-
- if (const PeHeader32* p32 = dynamic_cast<const PeHeader32*>(&pehHeader))
- {
- unsigned int offset = p32->rvaToOffset(iedCurr.ied.AddressOfFunctions);
-
- if (offset >= filesize)
- return ERROR_INVALID_FILE;
-
- ifFile.seekg(offset, std::ios::beg);
- }
- else if (const PeHeader64* p64 = dynamic_cast<const PeHeader64*>(&pehHeader))
- {
- // XXX: File might be > 4 GB
- unsigned int offset = static_cast<unsigned int>(p64->rvaToOffset(iedCurr.ied.AddressOfFunctions));
-
- if (offset >= filesize)
- return ERROR_INVALID_FILE;
-
- ifFile.seekg(offset, std::ios::beg);
- }
-
- PELIB_EXP_FUNC_INFORMATION efiCurr;
- efiCurr.ordinal = 0; efiCurr.addroffunc = 0; efiCurr.addrofname = 0;
-
- for (unsigned int i=0;i<iedCurr.ied.NumberOfFunctions;i++)
- {
- if (const PeHeader32* p32 = dynamic_cast<const PeHeader32*>(&pehHeader))
- {
- unsigned int offset = p32->rvaToOffset(iedCurr.ied.AddressOfFunctions) + i*sizeof(efiCurr.addroffunc);
-
- if (offset >= filesize)
- return ERROR_INVALID_FILE;
-
- ifFile.seekg(offset, std::ios::beg);
- }
- else if (const PeHeader64* p64 = dynamic_cast<const PeHeader64*>(&pehHeader))
- {
- // XXX: File might be > 4GB
- unsigned int offset = static_cast<unsigned int>(p64->rvaToOffset(iedCurr.ied.AddressOfFunctions)) + i*sizeof(efiCurr.addroffunc);
-
- if (offset >= filesize)
- return ERROR_INVALID_FILE;
-
- ifFile.seekg(offset, std::ios::beg);
- }
-
- ifFile.read(reinterpret_cast<char*>(&efiCurr.addroffunc), sizeof(efiCurr.addroffunc));
-
- if (!ifFile)
- return ERROR_INVALID_FILE;
-
- efiCurr.ordinal = i;
- iedCurr.functions.push_back(efiCurr);
- }
-
- for (unsigned int i=0;i<iedCurr.ied.NumberOfNames;i++)
- {
- if (const PeHeader32* p32 = dynamic_cast<const PeHeader32*>(&pehHeader))
- {
- unsigned int offset = p32->rvaToOffset(iedCurr.ied.AddressOfNameOrdinals) + i*sizeof(efiCurr.ordinal);
-
- if (offset >= filesize)
- return ERROR_INVALID_FILE;
-
- ifFile.seekg(offset, std::ios::beg);
- }
- else if (const PeHeader64* p64 = dynamic_cast<const PeHeader64*>(&pehHeader))
- {
- // XXX: File might be > 4 GB
- unsigned int offset = static_cast<unsigned int>(p64->rvaToOffset(iedCurr.ied.AddressOfNameOrdinals)) + i*sizeof(efiCurr.ordinal);
-
- if (offset >= filesize)
- return ERROR_INVALID_FILE;
-
- ifFile.seekg(offset, std::ios::beg);
- }
-
- word ordinal;
- ifFile.read(reinterpret_cast<char*>(&ordinal), sizeof(ordinal));
-
- if (!ifFile)
- return ERROR_INVALID_FILE;
-
- iedCurr.functions[ordinal].ordinal = ordinal;
-
- if (const PeHeader32* p32 = dynamic_cast<const PeHeader32*>(&pehHeader))
- {
- unsigned int offset = p32->rvaToOffset(iedCurr.ied.AddressOfNames) + i*sizeof(efiCurr.addrofname);
-
- if (offset >= filesize)
- return ERROR_INVALID_FILE;
-
- ifFile.seekg(offset, std::ios::beg);
- }
- else if (const PeHeader64* p64 = dynamic_cast<const PeHeader64*>(&pehHeader))
- {
- // XXX: File might be > 4 GB.
- unsigned int offset = static_cast<unsigned int>(p64->rvaToOffset(iedCurr.ied.AddressOfNames)) + i*sizeof(efiCurr.addrofname);
-
- if (offset >= filesize)
- return ERROR_INVALID_FILE;
-
- ifFile.seekg(offset, std::ios::beg);
- }
-
- ifFile.read(reinterpret_cast<char*>(&iedCurr.functions[ordinal].addrofname), sizeof(iedCurr.functions[ordinal].addrofname));
-
- if (!ifFile)
- return ERROR_INVALID_FILE;
-
- if (const PeHeader32* p32 = dynamic_cast<const PeHeader32*>(&pehHeader))
- {
- unsigned int offset = p32->rvaToOffset(iedCurr.functions[ordinal].addrofname);
-
- if (offset >= filesize)
- return ERROR_INVALID_FILE;
-
- ifFile.seekg(offset, std::ios::beg);
- }
- else if (const PeHeader64* p64 = dynamic_cast<const PeHeader64*>(&pehHeader))
- {
- // XXX: File might be > 4 GB.
- unsigned int offset = static_cast<unsigned int>(p64->rvaToOffset(iedCurr.functions[ordinal].addrofname));
-
- if (offset >= filesize)
- return ERROR_INVALID_FILE;
-
- ifFile.seekg(static_cast<unsigned int>(p64->rvaToOffset(iedCurr.functions[ordinal].addrofname)), std::ios::beg);
- }
-
- char c = 0;
- std::string strFname = "";
- do
- {
- ifFile.read(reinterpret_cast<char*>(&c), sizeof(c));
-
- if (!ifFile)
- return ERROR_INVALID_FILE;
-
- if (c) strFname += c;
- }
- while (c != 0);
-
- iedCurr.functions[ordinal].funcname = strFname;
- }
-
- std::swap(m_ied, iedCurr);
-
- return NO_ERROR;
- }
-
- /**
- * @param vBuffer Buffer where the rebuilt export directory is written to.
- * @param dwRva RVA of the export directory.
- * \todo fValid flag
- **/
- void ExportDirectory::rebuild(std::vector<byte>& vBuffer, dword dwRva) const
- {
- unsigned int uiSizeDirectory = sizeof(PELIB_IMAGE_EXPORT_DIRECTORY);
-
- unsigned int uiSizeNames = 0;
- unsigned int uiSizeAddrFuncs = 0;
- unsigned int uiSizeAddrNames = 0;
- unsigned int uiSizeOrdinals = 0;
-
- for (unsigned int i=0;i<m_ied.functions.size();i++)
- {
- uiSizeNames += (m_ied.functions[i].funcname.empty()) ? 0 : static_cast<unsigned int>(m_ied.functions[i].funcname.size()) + 1;
- uiSizeAddrFuncs += sizeof(m_ied.functions[i].addroffunc);
- uiSizeAddrNames += (m_ied.functions[i].funcname.empty()) ? 0 : sizeof(m_ied.functions[i].addrofname);
- uiSizeOrdinals += (m_ied.functions[i].funcname.empty()) ? 0 : sizeof(m_ied.functions[i].ordinal);
- }
-
- unsigned int uiFilenameSize = static_cast<unsigned int>(m_ied.name.size()) + 1;
-
- OutputBuffer obBuffer(vBuffer);
-
- obBuffer << m_ied.ied.Characteristics;
- obBuffer << m_ied.ied.TimeDateStamp;
- obBuffer << m_ied.ied.MajorVersion;
- obBuffer << m_ied.ied.MinorVersion;
- obBuffer << dwRva + uiSizeDirectory;
- obBuffer << m_ied.ied.Base;
- obBuffer << static_cast<unsigned int>(m_ied.functions.size());
-
- // TODO: Not correct but sufficient for now. (Update: I forgot what this comment refers to, but I'll leave it in)
- obBuffer << static_cast<unsigned int>(m_ied.functions.size());
- obBuffer << dwRva + uiSizeDirectory + uiFilenameSize;
- obBuffer << dwRva + uiSizeDirectory + uiFilenameSize + uiSizeAddrFuncs;
- obBuffer << dwRva + uiSizeDirectory + uiFilenameSize + uiSizeAddrFuncs + uiSizeAddrNames;
-
- obBuffer.add(m_ied.name.c_str(), static_cast<unsigned int>(m_ied.name.size())+1);
-
- for (unsigned int i=0;i<m_ied.functions.size();i++)
- {
- obBuffer << m_ied.functions[i].addroffunc;
- }
-
- unsigned int ulFuncCounter = dwRva + uiSizeDirectory + uiFilenameSize + uiSizeAddrFuncs + uiSizeAddrNames + uiSizeOrdinals;
-
- for (unsigned int i=0;i<m_ied.functions.size();i++)
- {
- if (!m_ied.functions[i].funcname.empty())
- {
- obBuffer << ulFuncCounter;
- ulFuncCounter += static_cast<unsigned int>(m_ied.functions[i].funcname.size()) + 1;
- }
- }
-
- for (unsigned int i=0;i<m_ied.functions.size();i++)
- {
- if (!m_ied.functions[i].funcname.empty())
- {
- obBuffer << m_ied.functions[i].ordinal;
- }
- }
-
- for (unsigned int i=0;i<m_ied.functions.size();i++)
- {
- if (m_ied.functions[i].funcname.empty() && m_ied.functions[i].addroffunc)
- {
- obBuffer << m_ied.functions[i].ordinal;
- }
- }
-
- for (unsigned int i=0;i<m_ied.functions.size();i++)
- {
- if (!m_ied.functions[i].funcname.empty())
- {
- obBuffer.add(m_ied.functions[i].funcname.c_str(), static_cast<unsigned int>(m_ied.functions[i].funcname.size()) + 1);
- }
- }
- }
-
- /**
- * @return Size of the current export directory.
- **/
- unsigned int ExportDirectory::size() const
- {
- return m_ied.size();
- }
-
- /**
- * @param strFilename Name of the file.
- * @param uiOffset File offset the export directory will be written to.
- * @param uiRva RVA of the export directory.
- * \todo Check if ofFile.write succeeded.
- **/
- int ExportDirectory::write(const std::string& strFilename, unsigned int uiOffset, unsigned int uiRva) const
- {
- std::fstream ofFile(strFilename.c_str(), std::ios_base::in);
-
- if (!ofFile)
- {
- ofFile.clear();
- ofFile.open(strFilename.c_str(), std::ios_base::out | std::ios_base::binary);
- }
- else
- {
- ofFile.close();
- ofFile.open(strFilename.c_str(), std::ios_base::in | std::ios_base::out | std::ios_base::binary);
- }
-
- if (!ofFile)
- {
- return ERROR_OPENING_FILE;
- }
-
- ofFile.seekp(uiOffset, std::ios::beg);
-
- std::vector<unsigned char> vBuffer;
- rebuild(vBuffer, uiRva);
-
- ofFile.write(reinterpret_cast<const char*>(&vBuffer[0]), static_cast<unsigned int>(vBuffer.size()));
-
- ofFile.close();
-
- return NO_ERROR;
- }
-
- /**
- * Changes the filename according to the export directory.
- * @param strFilename New filename.
- **/
- void ExportDirectory::setNameString(const std::string& strFilename)
- {
- m_ied.name = strFilename;
- }
-
- std::string ExportDirectory::getNameString() const
- {
- return m_ied.name;
- }
-
- /**
- * @param dwIndex Number which identifies an exported function.
- * @return The name of that function.
- **/
- std::string ExportDirectory::getFunctionName(dword dwIndex) const
- {
- return m_ied.functions[dwIndex].funcname;
- }
-
- /**
- * @param dwIndex Number which identifies an exported function.
- * @return The ordinal of that function.
- **/
- word ExportDirectory::getFunctionOrdinal(dword dwIndex) const
- {
- return m_ied.functions[dwIndex].ordinal;
- }
-
- /**
- * @param dwIndex Number which identifies an exported function.
- * @return The RVA of the name string of that function.
- **/
- dword ExportDirectory::getAddressOfName(dword dwIndex) const
- {
- return m_ied.functions[dwIndex].addrofname;
- }
-
- /**
- * @param dwIndex Number which identifies an exported function.
- * @return The RVA of that function.
- **/
- dword ExportDirectory::getAddressOfFunction(dword dwIndex) const
- {
- return m_ied.functions[dwIndex].addroffunc;
- }
-
- /**
- * @param dwIndex Number which identifies an exported function.
- * @param strName The name of that function.
- **/
- void ExportDirectory::setFunctionName(dword dwIndex, const std::string& strName)
- {
- m_ied.functions[dwIndex].funcname = strName;
- }
-
- /**
- * @param dwIndex Number which identifies an exported function.
- * @param wValue The ordinal of that function.
- **/
- void ExportDirectory::setFunctionOrdinal(dword dwIndex, word wValue)
- {
- m_ied.functions[dwIndex].ordinal = wValue;
- }
-
- /**
- * @param dwIndex Number which identifies an exported function.
- * @param dwValue The RVA of the name string of that function.
- **/
- void ExportDirectory::setAddressOfName(dword dwIndex, dword dwValue)
- {
- m_ied.functions[dwIndex].addrofname = dwValue;
- }
-
- /**
- * @param dwIndex Number which identifies an exported function.
- * @param dwValue The RVA of that function.
- **/
- void ExportDirectory::setAddressOfFunction(dword dwIndex, dword dwValue)
- {
- m_ied.functions[dwIndex].addroffunc = dwValue;
- }
-
- /**
- * @return The ordinal base of the export directory.
- **/
- dword ExportDirectory::getBase() const
- {
- return m_ied.ied.Base;
- }
-
- /**
- * @return The characteristics of the export directory.
- **/
- dword ExportDirectory::getCharacteristics() const
- {
- return m_ied.ied.Characteristics;
- }
-
- /**
- * @return The time/date stamp of the export directory.
- **/
- dword ExportDirectory::getTimeDateStamp() const
- {
- return m_ied.ied.TimeDateStamp;
- }
-
- /**
- * @return The MajorVersion of the export directory.
- **/
- word ExportDirectory::getMajorVersion() const
- {
- return m_ied.ied.MajorVersion;
- }
-
- /**
- * @return The MinorVersion of the export directory.
- **/
- word ExportDirectory::getMinorVersion() const
- {
- return m_ied.ied.MinorVersion;
- }
-
- /**
- * @return The RVA of the name of the file.
- **/
- dword ExportDirectory::getName() const
- {
- return m_ied.ied.Name;
- }
-
- /**
- * @return The NumberOfFunctions of the export directory.
- **/
- dword ExportDirectory::getNumberOfFunctions() const
- {
- return m_ied.ied.NumberOfFunctions;
- }
-
- /**
- * @return The NumberOfNames of the export directory.
- **/
- dword ExportDirectory::getNumberOfNames() const
- {
- return m_ied.ied.NumberOfNames;
- }
-
- /**
- * @return The AddressOfFunctions of the export directory.
- **/
- dword ExportDirectory::getAddressOfFunctions() const
- {
- return m_ied.ied.AddressOfFunctions;
- }
-
- /**
- * @return The AddressOfNames of the export directory.
- **/
- dword ExportDirectory::getAddressOfNames() const
- {
- return m_ied.ied.AddressOfNames;
- }
-
-/* dword ExportDirectory::getNumberOfNameOrdinals() const
- {
- return static_cast<dword>(m_ied.functions.size());
- }
-
- dword ExportDirectory::getNumberOfAddressOfFunctionNames() const
- {
- return static_cast<dword>(m_ied.functions.size());
- }
-
- dword ExportDirectory::getNumberOfAddressOfFunctions() const
- {
- return static_cast<dword>(m_ied.functions.size());
- }
-*/
- /**
- * @return The AddressOfNameOrdinals of the export directory.
- **/
- dword ExportDirectory::getAddressOfNameOrdinals() const
- {
- return m_ied.ied.AddressOfNameOrdinals;
- }
-
- /**
- * @param dwValue The ordinal base of the export directory.
- **/
- void ExportDirectory::setBase(dword dwValue)
- {
- m_ied.ied.Base = dwValue;
- }
-
- /**
- * @param dwValue The Characteristics of the export directory.
- **/
- void ExportDirectory::setCharacteristics(dword dwValue)
- {
- m_ied.ied.Characteristics = dwValue;
- }
-
- /**
- * @param dwValue The TimeDateStamp of the export directory.
- **/
- void ExportDirectory::setTimeDateStamp(dword dwValue)
- {
- m_ied.ied.TimeDateStamp = dwValue;
- }
-
- /**
- * @param wValue The MajorVersion of the export directory.
- **/
- void ExportDirectory::setMajorVersion(word wValue)
- {
- m_ied.ied.MajorVersion = wValue;
- }
-
- /**
- * @param wValue The MinorVersion of the export directory.
- **/
- void ExportDirectory::setMinorVersion(word wValue)
- {
- m_ied.ied.MinorVersion = wValue;
- }
-
- /**
- * @param dwValue The Name of the export directory.
- **/
- void ExportDirectory::setName(dword dwValue)
- {
- m_ied.ied.Name = dwValue;
- }
-
- /**
- * @param dwValue The NumberOfFunctions of the export directory.
- **/
- void ExportDirectory::setNumberOfFunctions(dword dwValue)
- {
- m_ied.ied.NumberOfFunctions = dwValue;
- }
-
- /**
- * @param dwValue The NumberOfNames of the export directory.
- **/
- void ExportDirectory::setNumberOfNames(dword dwValue)
- {
- m_ied.ied.NumberOfNames = dwValue;
- }
-
- /**
- * @param dwValue The AddressOfFunctions of the export directory.
- **/
- void ExportDirectory::setAddressOfFunctions(dword dwValue)
- {
- m_ied.ied.AddressOfFunctions = dwValue;
- }
-
- /**
- * @param dwValue The AddressOfNames of the export directory.
- **/
- void ExportDirectory::setAddressOfNames(dword dwValue)
- {
- m_ied.ied.AddressOfNames = dwValue;
- }
-
- void ExportDirectory::setAddressOfNameOrdinals(dword value)
- {
- m_ied.ied.AddressOfNameOrdinals = value;
- }
-}
+/*
+* ExportDirectory.cpp - Part of the PeLib library.
+*
+* Copyright (c) 2004 - 2005 Sebastian Porst (webmaster@the-interweb.com)
+* All rights reserved.
+*
+* This software is licensed under the zlib/libpng License.
+* For more details see http://www.opensource.org/licenses/zlib-license.php
+* or the license information file (license.htm) in the root directory
+* of PeLib.
+*/
+
+//#ifdef false
+
+#include "PeLibInc.h"
+#include "ExportDirectory.h"
+
+namespace PeLib
+{
+ /**
+ * @param strFuncname Name of the function.
+ * @param dwFuncAddr RVA of the function.
+ **/
+ void ExportDirectory::addFunction(const std::string& strFuncname, dword dwFuncAddr)
+ {
+ PELIB_EXP_FUNC_INFORMATION efiCurr;
+ efiCurr.funcname = strFuncname;
+ efiCurr.addroffunc = dwFuncAddr;
+ m_ied.functions.push_back(efiCurr);
+ }
+
+ void ExportDirectory::removeFunction(unsigned int index)
+ {
+ m_ied.functions.erase(m_ied.functions.begin() + index);
+ }
+
+ void ExportDirectory::clear()
+ {
+ m_ied.functions.clear();
+ }
+
+ unsigned int ExportDirectory::calcNumberOfFunctions() const
+ {
+ return static_cast<unsigned int>(m_ied.functions.size());
+ }
+
+ /**
+ * Identifies an exported function through it's name.
+ * @param strFunctionName Name of the function
+ * @return Number which identifies the functions.
+ **/
+ int ExportDirectory::getFunctionIndex(const std::string& strFunctionName) const
+ {
+ std::vector<PELIB_EXP_FUNC_INFORMATION>::const_iterator Iter = std::find_if(m_ied.functions.begin(), m_ied.functions.end(), std::bind2nd(std::mem_fun_ref(&PELIB_EXP_FUNC_INFORMATION::equal), strFunctionName));
+
+ if (Iter == m_ied.functions.end())
+ {
+// throw Exceptions::InvalidName(ExportDirectoryId, __LINE__);
+ return -1;
+ }
+
+ return static_cast<int>(std::distance(m_ied.functions.begin(), Iter));
+ }
+
+ /**
+ * @param strFilename Name of the file.
+ * @param uiOffset File offset of the export directory.
+ * @param uiSize Size of the export directory.
+ * @param pehHeader A valid PE header which is necessary because some RVA calculations need to be done.
+ * \todo: Proper use of InputBuffer
+ **/
+ int ExportDirectory::read(const std::string& strFilename, unsigned int uiOffset, unsigned int uiSize, const PeHeader& pehHeader)
+ {
+ std::ifstream ifFile(strFilename.c_str(), std::ios::binary);
+
+ if (!ifFile)
+ {
+ return ERROR_OPENING_FILE;
+ }
+
+ unsigned int filesize = fileSize(ifFile);
+
+ if (filesize < uiOffset + uiSize)
+ {
+ return ERROR_INVALID_FILE;
+ }
+
+ ifFile.seekg(uiOffset, std::ios::beg);
+
+ PELIB_IMAGE_EXP_DIRECTORY iedCurr;
+
+ std::vector<unsigned char> vExportdirectory(uiSize);
+ ifFile.read(reinterpret_cast<char*>(&vExportdirectory[0]), uiSize);
+
+ InputBuffer inpBuffer(vExportdirectory);
+
+ inpBuffer >> iedCurr.ied.Characteristics;
+ inpBuffer >> iedCurr.ied.TimeDateStamp;
+ inpBuffer >> iedCurr.ied.MajorVersion;
+ inpBuffer >> iedCurr.ied.MinorVersion;
+ inpBuffer >> iedCurr.ied.Name;
+ inpBuffer >> iedCurr.ied.Base;
+ inpBuffer >> iedCurr.ied.NumberOfFunctions;
+ inpBuffer >> iedCurr.ied.NumberOfNames;
+ inpBuffer >> iedCurr.ied.AddressOfFunctions;
+ inpBuffer >> iedCurr.ied.AddressOfNames;
+ inpBuffer >> iedCurr.ied.AddressOfNameOrdinals;
+
+ if (const PeHeader32* p32 = dynamic_cast<const PeHeader32*>(&pehHeader))
+ {
+ unsigned int offset = p32->rvaToOffset(iedCurr.ied.Name);
+
+ if (offset >= filesize)
+ return ERROR_INVALID_FILE;
+
+ ifFile.seekg(offset, std::ios::beg);
+ }
+ else if (const PeHeader64* p64 = dynamic_cast<const PeHeader64*>(&pehHeader))
+ {
+ // XXX: Files might be > 4 GB
+ unsigned int offset = static_cast<unsigned int>(p64->rvaToOffset(iedCurr.ied.Name));
+
+ if (offset >= filesize)
+ return ERROR_INVALID_FILE;
+
+ ifFile.seekg(offset, std::ios::beg);
+ }
+
+ char c = 0;
+ std::string strFname = "";
+ do
+ {
+ ifFile.read(reinterpret_cast<char*>(&c), sizeof(c));
+ if (!ifFile) return ERROR_INVALID_FILE;
+ if (c) strFname += c;
+ }
+ while (c != 0);
+ iedCurr.name = strFname;
+
+ if (const PeHeader32* p32 = dynamic_cast<const PeHeader32*>(&pehHeader))
+ {
+ unsigned int offset = p32->rvaToOffset(iedCurr.ied.AddressOfFunctions);
+
+ if (offset >= filesize)
+ return ERROR_INVALID_FILE;
+
+ ifFile.seekg(offset, std::ios::beg);
+ }
+ else if (const PeHeader64* p64 = dynamic_cast<const PeHeader64*>(&pehHeader))
+ {
+ // XXX: File might be > 4 GB
+ unsigned int offset = static_cast<unsigned int>(p64->rvaToOffset(iedCurr.ied.AddressOfFunctions));
+
+ if (offset >= filesize)
+ return ERROR_INVALID_FILE;
+
+ ifFile.seekg(offset, std::ios::beg);
+ }
+
+ PELIB_EXP_FUNC_INFORMATION efiCurr;
+ efiCurr.ordinal = 0; efiCurr.addroffunc = 0; efiCurr.addrofname = 0;
+
+ for (unsigned int i=0;i<iedCurr.ied.NumberOfFunctions;i++)
+ {
+ if (const PeHeader32* p32 = dynamic_cast<const PeHeader32*>(&pehHeader))
+ {
+ unsigned int offset = p32->rvaToOffset(iedCurr.ied.AddressOfFunctions) + i*sizeof(efiCurr.addroffunc);
+
+ if (offset >= filesize)
+ return ERROR_INVALID_FILE;
+
+ ifFile.seekg(offset, std::ios::beg);
+ }
+ else if (const PeHeader64* p64 = dynamic_cast<const PeHeader64*>(&pehHeader))
+ {
+ // XXX: File might be > 4GB
+ unsigned int offset = static_cast<unsigned int>(p64->rvaToOffset(iedCurr.ied.AddressOfFunctions)) + i*sizeof(efiCurr.addroffunc);
+
+ if (offset >= filesize)
+ return ERROR_INVALID_FILE;
+
+ ifFile.seekg(offset, std::ios::beg);
+ }
+
+ ifFile.read(reinterpret_cast<char*>(&efiCurr.addroffunc), sizeof(efiCurr.addroffunc));
+
+ if (!ifFile)
+ return ERROR_INVALID_FILE;
+
+ efiCurr.ordinal = i;
+ iedCurr.functions.push_back(efiCurr);
+ }
+
+ for (unsigned int i=0;i<iedCurr.ied.NumberOfNames;i++)
+ {
+ if (const PeHeader32* p32 = dynamic_cast<const PeHeader32*>(&pehHeader))
+ {
+ unsigned int offset = p32->rvaToOffset(iedCurr.ied.AddressOfNameOrdinals) + i*sizeof(efiCurr.ordinal);
+
+ if (offset >= filesize)
+ return ERROR_INVALID_FILE;
+
+ ifFile.seekg(offset, std::ios::beg);
+ }
+ else if (const PeHeader64* p64 = dynamic_cast<const PeHeader64*>(&pehHeader))
+ {
+ // XXX: File might be > 4 GB
+ unsigned int offset = static_cast<unsigned int>(p64->rvaToOffset(iedCurr.ied.AddressOfNameOrdinals)) + i*sizeof(efiCurr.ordinal);
+
+ if (offset >= filesize)
+ return ERROR_INVALID_FILE;
+
+ ifFile.seekg(offset, std::ios::beg);
+ }
+
+ word ordinal;
+ ifFile.read(reinterpret_cast<char*>(&ordinal), sizeof(ordinal));
+
+ if (!ifFile)
+ return ERROR_INVALID_FILE;
+
+ iedCurr.functions[ordinal].ordinal = ordinal;
+
+ if (const PeHeader32* p32 = dynamic_cast<const PeHeader32*>(&pehHeader))
+ {
+ unsigned int offset = p32->rvaToOffset(iedCurr.ied.AddressOfNames) + i*sizeof(efiCurr.addrofname);
+
+ if (offset >= filesize)
+ return ERROR_INVALID_FILE;
+
+ ifFile.seekg(offset, std::ios::beg);
+ }
+ else if (const PeHeader64* p64 = dynamic_cast<const PeHeader64*>(&pehHeader))
+ {
+ // XXX: File might be > 4 GB.
+ unsigned int offset = static_cast<unsigned int>(p64->rvaToOffset(iedCurr.ied.AddressOfNames)) + i*sizeof(efiCurr.addrofname);
+
+ if (offset >= filesize)
+ return ERROR_INVALID_FILE;
+
+ ifFile.seekg(offset, std::ios::beg);
+ }
+
+ ifFile.read(reinterpret_cast<char*>(&iedCurr.functions[ordinal].addrofname), sizeof(iedCurr.functions[ordinal].addrofname));
+
+ if (!ifFile)
+ return ERROR_INVALID_FILE;
+
+ if (const PeHeader32* p32 = dynamic_cast<const PeHeader32*>(&pehHeader))
+ {
+ unsigned int offset = p32->rvaToOffset(iedCurr.functions[ordinal].addrofname);
+
+ if (offset >= filesize)
+ return ERROR_INVALID_FILE;
+
+ ifFile.seekg(offset, std::ios::beg);
+ }
+ else if (const PeHeader64* p64 = dynamic_cast<const PeHeader64*>(&pehHeader))
+ {
+ // XXX: File might be > 4 GB.
+ unsigned int offset = static_cast<unsigned int>(p64->rvaToOffset(iedCurr.functions[ordinal].addrofname));
+
+ if (offset >= filesize)
+ return ERROR_INVALID_FILE;
+
+ ifFile.seekg(static_cast<unsigned int>(p64->rvaToOffset(iedCurr.functions[ordinal].addrofname)), std::ios::beg);
+ }
+
+ char c = 0;
+ std::string strFname = "";
+ do
+ {
+ ifFile.read(reinterpret_cast<char*>(&c), sizeof(c));
+
+ if (!ifFile)
+ return ERROR_INVALID_FILE;
+
+ if (c) strFname += c;
+ }
+ while (c != 0);
+
+ iedCurr.functions[ordinal].funcname = strFname;
+ }
+
+ std::swap(m_ied, iedCurr);
+
+ return NO_ERROR;
+ }
+
+ /**
+ * @param vBuffer Buffer where the rebuilt export directory is written to.
+ * @param dwRva RVA of the export directory.
+ * \todo fValid flag
+ **/
+ void ExportDirectory::rebuild(std::vector<byte>& vBuffer, dword dwRva) const
+ {
+ unsigned int uiSizeDirectory = sizeof(PELIB_IMAGE_EXPORT_DIRECTORY);
+
+ unsigned int uiSizeNames = 0;
+ unsigned int uiSizeAddrFuncs = 0;
+ unsigned int uiSizeAddrNames = 0;
+ unsigned int uiSizeOrdinals = 0;
+
+ for (unsigned int i=0;i<m_ied.functions.size();i++)
+ {
+ uiSizeNames += (m_ied.functions[i].funcname.empty()) ? 0 : static_cast<unsigned int>(m_ied.functions[i].funcname.size()) + 1;
+ uiSizeAddrFuncs += sizeof(m_ied.functions[i].addroffunc);
+ uiSizeAddrNames += (m_ied.functions[i].funcname.empty()) ? 0 : sizeof(m_ied.functions[i].addrofname);
+ uiSizeOrdinals += (m_ied.functions[i].funcname.empty()) ? 0 : sizeof(m_ied.functions[i].ordinal);
+ }
+
+ unsigned int uiFilenameSize = static_cast<unsigned int>(m_ied.name.size()) + 1;
+
+ OutputBuffer obBuffer(vBuffer);
+
+ obBuffer << m_ied.ied.Characteristics;
+ obBuffer << m_ied.ied.TimeDateStamp;
+ obBuffer << m_ied.ied.MajorVersion;
+ obBuffer << m_ied.ied.MinorVersion;
+ obBuffer << dwRva + uiSizeDirectory;
+ obBuffer << m_ied.ied.Base;
+ obBuffer << static_cast<unsigned int>(m_ied.functions.size());
+
+ // TODO: Not correct but sufficient for now. (Update: I forgot what this comment refers to, but I'll leave it in)
+ obBuffer << static_cast<unsigned int>(m_ied.functions.size());
+ obBuffer << dwRva + uiSizeDirectory + uiFilenameSize;
+ obBuffer << dwRva + uiSizeDirectory + uiFilenameSize + uiSizeAddrFuncs;
+ obBuffer << dwRva + uiSizeDirectory + uiFilenameSize + uiSizeAddrFuncs + uiSizeAddrNames;
+
+ obBuffer.add(m_ied.name.c_str(), static_cast<unsigned int>(m_ied.name.size())+1);
+
+ for (unsigned int i=0;i<m_ied.functions.size();i++)
+ {
+ obBuffer << m_ied.functions[i].addroffunc;
+ }
+
+ unsigned int ulFuncCounter = dwRva + uiSizeDirectory + uiFilenameSize + uiSizeAddrFuncs + uiSizeAddrNames + uiSizeOrdinals;
+
+ for (unsigned int i=0;i<m_ied.functions.size();i++)
+ {
+ if (!m_ied.functions[i].funcname.empty())
+ {
+ obBuffer << ulFuncCounter;
+ ulFuncCounter += static_cast<unsigned int>(m_ied.functions[i].funcname.size()) + 1;
+ }
+ }
+
+ for (unsigned int i=0;i<m_ied.functions.size();i++)
+ {
+ if (!m_ied.functions[i].funcname.empty())
+ {
+ obBuffer << m_ied.functions[i].ordinal;
+ }
+ }
+
+ for (unsigned int i=0;i<m_ied.functions.size();i++)
+ {
+ if (m_ied.functions[i].funcname.empty() && m_ied.functions[i].addroffunc)
+ {
+ obBuffer << m_ied.functions[i].ordinal;
+ }
+ }
+
+ for (unsigned int i=0;i<m_ied.functions.size();i++)
+ {
+ if (!m_ied.functions[i].funcname.empty())
+ {
+ obBuffer.add(m_ied.functions[i].funcname.c_str(), static_cast<unsigned int>(m_ied.functions[i].funcname.size()) + 1);
+ }
+ }
+ }
+
+ /**
+ * @return Size of the current export directory.
+ **/
+ unsigned int ExportDirectory::size() const
+ {
+ return m_ied.size();
+ }
+
+ /**
+ * @param strFilename Name of the file.
+ * @param uiOffset File offset the export directory will be written to.
+ * @param uiRva RVA of the export directory.
+ * \todo Check if ofFile.write succeeded.
+ **/
+ int ExportDirectory::write(const std::string& strFilename, unsigned int uiOffset, unsigned int uiRva) const
+ {
+ std::fstream ofFile(strFilename.c_str(), std::ios_base::in);
+
+ if (!ofFile)
+ {
+ ofFile.clear();
+ ofFile.open(strFilename.c_str(), std::ios_base::out | std::ios_base::binary);
+ }
+ else
+ {
+ ofFile.close();
+ ofFile.open(strFilename.c_str(), std::ios_base::in | std::ios_base::out | std::ios_base::binary);
+ }
+
+ if (!ofFile)
+ {
+ return ERROR_OPENING_FILE;
+ }
+
+ ofFile.seekp(uiOffset, std::ios::beg);
+
+ std::vector<unsigned char> vBuffer;
+ rebuild(vBuffer, uiRva);
+
+ ofFile.write(reinterpret_cast<const char*>(&vBuffer[0]), static_cast<unsigned int>(vBuffer.size()));
+
+ ofFile.close();
+
+ return NO_ERROR;
+ }
+
+ /**
+ * Changes the filename according to the export directory.
+ * @param strFilename New filename.
+ **/
+ void ExportDirectory::setNameString(const std::string& strFilename)
+ {
+ m_ied.name = strFilename;
+ }
+
+ std::string ExportDirectory::getNameString() const
+ {
+ return m_ied.name;
+ }
+
+ /**
+ * @param dwIndex Number which identifies an exported function.
+ * @return The name of that function.
+ **/
+ std::string ExportDirectory::getFunctionName(dword dwIndex) const
+ {
+ return m_ied.functions[dwIndex].funcname;
+ }
+
+ /**
+ * @param dwIndex Number which identifies an exported function.
+ * @return The ordinal of that function.
+ **/
+ word ExportDirectory::getFunctionOrdinal(dword dwIndex) const
+ {
+ return m_ied.functions[dwIndex].ordinal;
+ }
+
+ /**
+ * @param dwIndex Number which identifies an exported function.
+ * @return The RVA of the name string of that function.
+ **/
+ dword ExportDirectory::getAddressOfName(dword dwIndex) const
+ {
+ return m_ied.functions[dwIndex].addrofname;
+ }
+
+ /**
+ * @param dwIndex Number which identifies an exported function.
+ * @return The RVA of that function.
+ **/
+ dword ExportDirectory::getAddressOfFunction(dword dwIndex) const
+ {
+ return m_ied.functions[dwIndex].addroffunc;
+ }
+
+ /**
+ * @param dwIndex Number which identifies an exported function.
+ * @param strName The name of that function.
+ **/
+ void ExportDirectory::setFunctionName(dword dwIndex, const std::string& strName)
+ {
+ m_ied.functions[dwIndex].funcname = strName;
+ }
+
+ /**
+ * @param dwIndex Number which identifies an exported function.
+ * @param wValue The ordinal of that function.
+ **/
+ void ExportDirectory::setFunctionOrdinal(dword dwIndex, word wValue)
+ {
+ m_ied.functions[dwIndex].ordinal = wValue;
+ }
+
+ /**
+ * @param dwIndex Number which identifies an exported function.
+ * @param dwValue The RVA of the name string of that function.
+ **/
+ void ExportDirectory::setAddressOfName(dword dwIndex, dword dwValue)
+ {
+ m_ied.functions[dwIndex].addrofname = dwValue;
+ }
+
+ /**
+ * @param dwIndex Number which identifies an exported function.
+ * @param dwValue The RVA of that function.
+ **/
+ void ExportDirectory::setAddressOfFunction(dword dwIndex, dword dwValue)
+ {
+ m_ied.functions[dwIndex].addroffunc = dwValue;
+ }
+
+ /**
+ * @return The ordinal base of the export directory.
+ **/
+ dword ExportDirectory::getBase() const
+ {
+ return m_ied.ied.Base;
+ }
+
+ /**
+ * @return The characteristics of the export directory.
+ **/
+ dword ExportDirectory::getCharacteristics() const
+ {
+ return m_ied.ied.Characteristics;
+ }
+
+ /**
+ * @return The time/date stamp of the export directory.
+ **/
+ dword ExportDirectory::getTimeDateStamp() const
+ {
+ return m_ied.ied.TimeDateStamp;
+ }
+
+ /**
+ * @return The MajorVersion of the export directory.
+ **/
+ word ExportDirectory::getMajorVersion() const
+ {
+ return m_ied.ied.MajorVersion;
+ }
+
+ /**
+ * @return The MinorVersion of the export directory.
+ **/
+ word ExportDirectory::getMinorVersion() const
+ {
+ return m_ied.ied.MinorVersion;
+ }
+
+ /**
+ * @return The RVA of the name of the file.
+ **/
+ dword ExportDirectory::getName() const
+ {
+ return m_ied.ied.Name;
+ }
+
+ /**
+ * @return The NumberOfFunctions of the export directory.
+ **/
+ dword ExportDirectory::getNumberOfFunctions() const
+ {
+ return m_ied.ied.NumberOfFunctions;
+ }
+
+ /**
+ * @return The NumberOfNames of the export directory.
+ **/
+ dword ExportDirectory::getNumberOfNames() const
+ {
+ return m_ied.ied.NumberOfNames;
+ }
+
+ /**
+ * @return The AddressOfFunctions of the export directory.
+ **/
+ dword ExportDirectory::getAddressOfFunctions() const
+ {
+ return m_ied.ied.AddressOfFunctions;
+ }
+
+ /**
+ * @return The AddressOfNames of the export directory.
+ **/
+ dword ExportDirectory::getAddressOfNames() const
+ {
+ return m_ied.ied.AddressOfNames;
+ }
+
+/* dword ExportDirectory::getNumberOfNameOrdinals() const
+ {
+ return static_cast<dword>(m_ied.functions.size());
+ }
+
+ dword ExportDirectory::getNumberOfAddressOfFunctionNames() const
+ {
+ return static_cast<dword>(m_ied.functions.size());
+ }
+
+ dword ExportDirectory::getNumberOfAddressOfFunctions() const
+ {
+ return static_cast<dword>(m_ied.functions.size());
+ }
+*/
+ /**
+ * @return The AddressOfNameOrdinals of the export directory.
+ **/
+ dword ExportDirectory::getAddressOfNameOrdinals() const
+ {
+ return m_ied.ied.AddressOfNameOrdinals;
+ }
+
+ /**
+ * @param dwValue The ordinal base of the export directory.
+ **/
+ void ExportDirectory::setBase(dword dwValue)
+ {
+ m_ied.ied.Base = dwValue;
+ }
+
+ /**
+ * @param dwValue The Characteristics of the export directory.
+ **/
+ void ExportDirectory::setCharacteristics(dword dwValue)
+ {
+ m_ied.ied.Characteristics = dwValue;
+ }
+
+ /**
+ * @param dwValue The TimeDateStamp of the export directory.
+ **/
+ void ExportDirectory::setTimeDateStamp(dword dwValue)
+ {
+ m_ied.ied.TimeDateStamp = dwValue;
+ }
+
+ /**
+ * @param wValue The MajorVersion of the export directory.
+ **/
+ void ExportDirectory::setMajorVersion(word wValue)
+ {
+ m_ied.ied.MajorVersion = wValue;
+ }
+
+ /**
+ * @param wValue The MinorVersion of the export directory.
+ **/
+ void ExportDirectory::setMinorVersion(word wValue)
+ {
+ m_ied.ied.MinorVersion = wValue;
+ }
+
+ /**
+ * @param dwValue The Name of the export directory.
+ **/
+ void ExportDirectory::setName(dword dwValue)
+ {
+ m_ied.ied.Name = dwValue;
+ }
+
+ /**
+ * @param dwValue The NumberOfFunctions of the export directory.
+ **/
+ void ExportDirectory::setNumberOfFunctions(dword dwValue)
+ {
+ m_ied.ied.NumberOfFunctions = dwValue;
+ }
+
+ /**
+ * @param dwValue The NumberOfNames of the export directory.
+ **/
+ void ExportDirectory::setNumberOfNames(dword dwValue)
+ {
+ m_ied.ied.NumberOfNames = dwValue;
+ }
+
+ /**
+ * @param dwValue The AddressOfFunctions of the export directory.
+ **/
+ void ExportDirectory::setAddressOfFunctions(dword dwValue)
+ {
+ m_ied.ied.AddressOfFunctions = dwValue;
+ }
+
+ /**
+ * @param dwValue The AddressOfNames of the export directory.
+ **/
+ void ExportDirectory::setAddressOfNames(dword dwValue)
+ {
+ m_ied.ied.AddressOfNames = dwValue;
+ }
+
+ void ExportDirectory::setAddressOfNameOrdinals(dword value)
+ {
+ m_ied.ied.AddressOfNameOrdinals = value;
+ }
+}
diff --git a/utils/zenutils/libraries/pelib-0.9/pelib/ExportDirectory.h b/utils/zenutils/libraries/pelib-0.9/pelib/ExportDirectory.h
index 17a7e57020..19609b65a7 100755..100644
--- a/utils/zenutils/libraries/pelib-0.9/pelib/ExportDirectory.h
+++ b/utils/zenutils/libraries/pelib-0.9/pelib/ExportDirectory.h
@@ -1,133 +1,133 @@
-/*
-* ExportDirectory.h - Part of the PeLib library.
-*
-* Copyright (c) 2004 - 2005 Sebastian Porst (webmaster@the-interweb.com)
-* All rights reserved.
-*
-* This software is licensed under the zlib/libpng License.
-* For more details see http://www.opensource.org/licenses/zlib-license.php
-* or the license information file (license.htm) in the root directory
-* of PeLib.
-*/
-
-#ifndef EXPORTDIRECTORY_H
-#define EXPORTDIRECTORY_H
-#include "PeHeader.h"
-
-namespace PeLib
-{
- /// Class that handles the export directory.
- /**
- * This class handles the export directory.
- * \todo getNameString
- **/
-// template<int bits>
- class ExportDirectory
- {
- private:
- /// Used to store all necessary information about a file's exported functions.
- PELIB_IMAGE_EXP_DIRECTORY m_ied;
-
- public:
- /// Add another function to be exported.
- void addFunction(const std::string& strFuncname, dword dwFuncAddr); // EXPORT
- unsigned int calcNumberOfFunctions() const; // EXPORT
- void clear(); // EXPORT
- /// Identifies a function through it's name.
- int getFunctionIndex(const std::string& strFunctionName) const; // EXPORT
- /// Read a file's export directory.
- int read(const std::string& strFilename, unsigned int uiOffset, unsigned int uiSize, const PeHeader& pehHeader); // EXPORT
- /// Rebuild the current export directory.
- void rebuild(std::vector<byte>& vBuffer, dword dwRva) const; // EXPORT
- void removeFunction(unsigned int index); // EXPORT
- /// Returns the size of the current export directory.
- unsigned int size() const; // EXPORT
- /// Writes the current export directory to a file.
- int write(const std::string& strFilename, unsigned int uiOffset, unsigned int uiRva) const; // EXPORT
-
- /// Changes the name of the file (according to the export directory).
- void setNameString(const std::string& strFilename); // EXPORT
- std::string getNameString() const; // EXPORT
-
- /// Get the name of an exported function.
- std::string getFunctionName(unsigned int index) const; // EXPORT
- /// Get the ordinal of an exported function.
- word getFunctionOrdinal(unsigned int index) const; // EXPORT
- /// Get the address of the name of an exported function.
- dword getAddressOfName(unsigned int index) const; // EXPORT
- /// Get the address of an exported function.
- dword getAddressOfFunction(unsigned int index) const; // EXPORT
-
- /// Change the name of an exported function.
- void setFunctionName(unsigned int index, const std::string& strName); // EXPORT
- /// Change the ordinal of an exported function.
- void setFunctionOrdinal(unsigned int index, word wValue); // EXPORT
- /// Change the address of the name of an exported function.
- void setAddressOfName(unsigned int index, dword dwValue); // EXPORT
- /// Change the address of an exported function.
- void setAddressOfFunction(unsigned int index, dword dwValue); // EXPORT
-
- /*
- word getFunctionOrdinal(std::string strFuncname) const;
- dword getAddressOfName(std::string strFuncname) const;
- dword getAddressOfFunction(std::string strFuncname) const;
-
- void setFunctionOrdinal(std::string strFuncname, word wValue);
- void setAddressOfName(std::string strFuncname, dword dwValue);
- void setAddressOfFunction(std::string strFuncname, dword dwValue);
- */
-
- /// Return the Base value of the export directory.
- dword getBase() const; // EXPORT
- /// Return the Characteristics value of the export directory.
- dword getCharacteristics() const; // EXPORT
- /// Return the TimeDateStamp value of the export directory.
- dword getTimeDateStamp() const; // EXPORT
- /// Return the MajorVersion value of the export directory.
- word getMajorVersion() const; // EXPORT
- /// Return the MinorVersion value of the export directory.
- word getMinorVersion() const; // EXPORT
- /// Return the Name value of the export directory.
- dword getName() const; // EXPORT
- /// Return the NumberOfFunctions value of the export directory.
- dword getNumberOfFunctions() const; // EXPORT
- /// Return the NumberOfNames value of the export directory.
- dword getNumberOfNames() const; // EXPORT
- /// Return the AddressOfFunctions value of the export directory.
- dword getAddressOfFunctions() const; // EXPORT
- /// Return the AddressOfNames value of the export directory.
- dword getAddressOfNames() const; // EXPORT
- /// Returns the AddressOfNameOrdinals value.
- dword getAddressOfNameOrdinals() const; // EXPORT
-
-/* /// Returns the number of NameOrdinals.
- dword getNumberOfNameOrdinals() const; // EXPORT
- /// Returns the number of AddressOfFunctionNames values.
- dword getNumberOfAddressOfFunctionNames() const; // EXPORT
- /// Returns the number of AddressOfFunction values.
- dword getNumberOfAddressOfFunctions() const; // EXPORT
-*/
- /// Set the Base value of the export directory.
- void setBase(dword dwValue); // EXPORT
- /// Set the Characteristics value of the export directory.
- void setCharacteristics(dword dwValue); // EXPORT
- /// Set the TimeDateStamp value of the export directory.
- void setTimeDateStamp(dword dwValue); // EXPORT
- /// Set the MajorVersion value of the export directory.
- void setMajorVersion(word wValue); // EXPORT
- /// Set the MinorVersion value of the export directory.
- void setMinorVersion(word wValue); // EXPORT
- /// Set the Name value of the export directory.
- void setName(dword dwValue); // EXPORT
- /// Set the NumberOfFunctions value of the export directory.
- void setNumberOfFunctions(dword dwValue); // EXPORT
- /// Set the NumberOfNames value of the export directory.
- void setNumberOfNames(dword dwValue); // EXPORT
- /// Set the AddressOfFunctions value of the export directory.
- void setAddressOfFunctions(dword dwValue); // EXPORT
- /// Set the AddressOfNames value of the export directory.
- void setAddressOfNames(dword dwValue); // EXPORT
- void setAddressOfNameOrdinals(dword value); // EXPORT
- };
-}
-#endif
+/*
+* ExportDirectory.h - Part of the PeLib library.
+*
+* Copyright (c) 2004 - 2005 Sebastian Porst (webmaster@the-interweb.com)
+* All rights reserved.
+*
+* This software is licensed under the zlib/libpng License.
+* For more details see http://www.opensource.org/licenses/zlib-license.php
+* or the license information file (license.htm) in the root directory
+* of PeLib.
+*/
+
+#ifndef EXPORTDIRECTORY_H
+#define EXPORTDIRECTORY_H
+#include "PeHeader.h"
+
+namespace PeLib
+{
+ /// Class that handles the export directory.
+ /**
+ * This class handles the export directory.
+ * \todo getNameString
+ **/
+// template<int bits>
+ class ExportDirectory
+ {
+ private:
+ /// Used to store all necessary information about a file's exported functions.
+ PELIB_IMAGE_EXP_DIRECTORY m_ied;
+
+ public:
+ /// Add another function to be exported.
+ void addFunction(const std::string& strFuncname, dword dwFuncAddr); // EXPORT
+ unsigned int calcNumberOfFunctions() const; // EXPORT
+ void clear(); // EXPORT
+ /// Identifies a function through it's name.
+ int getFunctionIndex(const std::string& strFunctionName) const; // EXPORT
+ /// Read a file's export directory.
+ int read(const std::string& strFilename, unsigned int uiOffset, unsigned int uiSize, const PeHeader& pehHeader); // EXPORT
+ /// Rebuild the current export directory.
+ void rebuild(std::vector<byte>& vBuffer, dword dwRva) const; // EXPORT
+ void removeFunction(unsigned int index); // EXPORT
+ /// Returns the size of the current export directory.
+ unsigned int size() const; // EXPORT
+ /// Writes the current export directory to a file.
+ int write(const std::string& strFilename, unsigned int uiOffset, unsigned int uiRva) const; // EXPORT
+
+ /// Changes the name of the file (according to the export directory).
+ void setNameString(const std::string& strFilename); // EXPORT
+ std::string getNameString() const; // EXPORT
+
+ /// Get the name of an exported function.
+ std::string getFunctionName(unsigned int index) const; // EXPORT
+ /// Get the ordinal of an exported function.
+ word getFunctionOrdinal(unsigned int index) const; // EXPORT
+ /// Get the address of the name of an exported function.
+ dword getAddressOfName(unsigned int index) const; // EXPORT
+ /// Get the address of an exported function.
+ dword getAddressOfFunction(unsigned int index) const; // EXPORT
+
+ /// Change the name of an exported function.
+ void setFunctionName(unsigned int index, const std::string& strName); // EXPORT
+ /// Change the ordinal of an exported function.
+ void setFunctionOrdinal(unsigned int index, word wValue); // EXPORT
+ /// Change the address of the name of an exported function.
+ void setAddressOfName(unsigned int index, dword dwValue); // EXPORT
+ /// Change the address of an exported function.
+ void setAddressOfFunction(unsigned int index, dword dwValue); // EXPORT
+
+ /*
+ word getFunctionOrdinal(std::string strFuncname) const;
+ dword getAddressOfName(std::string strFuncname) const;
+ dword getAddressOfFunction(std::string strFuncname) const;
+
+ void setFunctionOrdinal(std::string strFuncname, word wValue);
+ void setAddressOfName(std::string strFuncname, dword dwValue);
+ void setAddressOfFunction(std::string strFuncname, dword dwValue);
+ */
+
+ /// Return the Base value of the export directory.
+ dword getBase() const; // EXPORT
+ /// Return the Characteristics value of the export directory.
+ dword getCharacteristics() const; // EXPORT
+ /// Return the TimeDateStamp value of the export directory.
+ dword getTimeDateStamp() const; // EXPORT
+ /// Return the MajorVersion value of the export directory.
+ word getMajorVersion() const; // EXPORT
+ /// Return the MinorVersion value of the export directory.
+ word getMinorVersion() const; // EXPORT
+ /// Return the Name value of the export directory.
+ dword getName() const; // EXPORT
+ /// Return the NumberOfFunctions value of the export directory.
+ dword getNumberOfFunctions() const; // EXPORT
+ /// Return the NumberOfNames value of the export directory.
+ dword getNumberOfNames() const; // EXPORT
+ /// Return the AddressOfFunctions value of the export directory.
+ dword getAddressOfFunctions() const; // EXPORT
+ /// Return the AddressOfNames value of the export directory.
+ dword getAddressOfNames() const; // EXPORT
+ /// Returns the AddressOfNameOrdinals value.
+ dword getAddressOfNameOrdinals() const; // EXPORT
+
+/* /// Returns the number of NameOrdinals.
+ dword getNumberOfNameOrdinals() const; // EXPORT
+ /// Returns the number of AddressOfFunctionNames values.
+ dword getNumberOfAddressOfFunctionNames() const; // EXPORT
+ /// Returns the number of AddressOfFunction values.
+ dword getNumberOfAddressOfFunctions() const; // EXPORT
+*/
+ /// Set the Base value of the export directory.
+ void setBase(dword dwValue); // EXPORT
+ /// Set the Characteristics value of the export directory.
+ void setCharacteristics(dword dwValue); // EXPORT
+ /// Set the TimeDateStamp value of the export directory.
+ void setTimeDateStamp(dword dwValue); // EXPORT
+ /// Set the MajorVersion value of the export directory.
+ void setMajorVersion(word wValue); // EXPORT
+ /// Set the MinorVersion value of the export directory.
+ void setMinorVersion(word wValue); // EXPORT
+ /// Set the Name value of the export directory.
+ void setName(dword dwValue); // EXPORT
+ /// Set the NumberOfFunctions value of the export directory.
+ void setNumberOfFunctions(dword dwValue); // EXPORT
+ /// Set the NumberOfNames value of the export directory.
+ void setNumberOfNames(dword dwValue); // EXPORT
+ /// Set the AddressOfFunctions value of the export directory.
+ void setAddressOfFunctions(dword dwValue); // EXPORT
+ /// Set the AddressOfNames value of the export directory.
+ void setAddressOfNames(dword dwValue); // EXPORT
+ void setAddressOfNameOrdinals(dword value); // EXPORT
+ };
+}
+#endif
diff --git a/utils/zenutils/libraries/pelib-0.9/pelib/IatDirectory.cpp b/utils/zenutils/libraries/pelib-0.9/pelib/IatDirectory.cpp
index 36482fcbeb..b32cfb2a8e 100755..100644
--- a/utils/zenutils/libraries/pelib-0.9/pelib/IatDirectory.cpp
+++ b/utils/zenutils/libraries/pelib-0.9/pelib/IatDirectory.cpp
@@ -1,179 +1,179 @@
-/*
-* IatDirectory.h - Part of the PeLib library.
-*
-* Copyright (c) 2004 - 2005 Sebastian Porst (webmaster@the-interweb.com)
-* All rights reserved.
-*
-* This software is licensed under the zlib/libpng License.
-* For more details see http://www.opensource.org/licenses/zlib-license.php
-* or the license information file (license.htm) in the root directory
-* of PeLib.
-*/
-
-#include "IatDirectory.h"
-
-namespace PeLib
-{
- int IatDirectory::read(InputBuffer& inputBuffer, unsigned int size)
- {
- dword dwAddr;
-
- std::vector<dword> vIat;
-
- for (unsigned int i=0;i<size/sizeof(dword);i++)
- {
- inputBuffer >> dwAddr;
- vIat.push_back(dwAddr);
- }
-
- std::swap(vIat, m_vIat);
-
- return NO_ERROR;
- }
-
- /**
- * Reads the Import Address table from a file.
- * @param strFilename Name of the file.
- * @param dwOffset File offset of the IAT (see #PeFile::PeHeader::getIDIatRVA).
- * @param dwSize Size of the IAT (see #PeFile::PeHeader::getIDIatSize).
- **/
- int IatDirectory::read(const std::string& strFilename, unsigned int dwOffset, unsigned int dwSize)
- {
- std::ifstream ifFile(strFilename.c_str(), std::ios::binary);
-
- if (!ifFile)
- {
- return ERROR_OPENING_FILE;
- }
-
- if (fileSize(ifFile) < dwOffset + dwSize)
- {
- return ERROR_INVALID_FILE;
- }
-
- ifFile.seekg(dwOffset, std::ios::beg);
-
- std::vector<byte> vBuffer(dwSize);
- ifFile.read(reinterpret_cast<char*>(&vBuffer[0]), dwSize);
-
- InputBuffer inpBuffer(vBuffer);
- return read(inpBuffer, dwSize);
- }
-
- int IatDirectory::read(unsigned char* buffer, unsigned int buffersize)
- {
- std::vector<byte> vBuffer(buffer, buffer + buffersize);
- InputBuffer inpBuffer(vBuffer);
- return read(inpBuffer, buffersize);
- }
-
- /**
- * Returns the number of fields in the IAT. This is equivalent to the number of
- * imported functions.
- * @return Number of fields in the IAT.
- **/
- unsigned int IatDirectory::calcNumberOfAddresses() const
- {
- return static_cast<unsigned int>(m_vIat.size());
- }
-
- /**
- * Returns the dwValue of a field in the IAT.
- * @param dwAddrnr Number identifying the field.
- * @return dwValue of the field.
- **/
- dword IatDirectory::getAddress(unsigned int index) const
- {
- return m_vIat[index];
- }
-
- /**
- * Updates the dwValue of a field in the IAT.
- * @param dwAddrnr Number identifying the field.
- * @param dwValue New dwValue of the field.
- **/
- void IatDirectory::setAddress(dword dwAddrnr, dword dwValue)
- {
- m_vIat[dwAddrnr] = dwValue;
- }
-
- /**
- * Adds another field to the IAT.
- * @param dwValue dwValue of the new field.
- **/
- void IatDirectory::addAddress(dword dwValue)
- {
- m_vIat.push_back(dwValue);
- }
-
- /**
- * Removes an address from the IAT.
- * @param dwAddrnr Number identifying the field.
- **/
- void IatDirectory::removeAddress(unsigned int index)
- {
- std::vector<dword>::iterator pos = m_vIat.begin() + index;
- m_vIat.erase(pos);
- }
-
- /**
- * Delete all entries from the IAT.
- **/
- void IatDirectory::clear()
- {
- m_vIat.clear();
- }
-
- /**
- * Rebuilds the complete Import Address Table.
- * @param vBuffer Buffer where the rebuilt IAT will be stored.
- **/
- void IatDirectory::rebuild(std::vector<byte>& vBuffer) const
- {
- vBuffer.reserve(size());
- OutputBuffer obBuffer(vBuffer);
-
- for (unsigned int i=0;i<m_vIat.size();i++)
- {
- obBuffer << m_vIat[i];
- }
- }
-
- unsigned int IatDirectory::size() const
- {
- return static_cast<unsigned int>(m_vIat.size())* sizeof(dword);
- }
-
- /// Writes the current IAT to a file.
- int IatDirectory::write(const std::string& strFilename, unsigned int uiOffset) const
- {
- std::fstream ofFile(strFilename.c_str(), std::ios_base::in);
-
- if (!ofFile)
- {
- ofFile.clear();
- ofFile.open(strFilename.c_str(), std::ios_base::out | std::ios_base::binary);
- }
- else
- {
- ofFile.close();
- ofFile.open(strFilename.c_str(), std::ios_base::in | std::ios_base::out | std::ios_base::binary);
- }
-
- if (!ofFile)
- {
- return ERROR_OPENING_FILE;
- }
-
- ofFile.seekp(uiOffset, std::ios::beg);
-
- std::vector<unsigned char> vBuffer;
- rebuild(vBuffer);
-
- ofFile.write(reinterpret_cast<const char*>(&vBuffer[0]), static_cast<unsigned int>(vBuffer.size()));
-
- ofFile.close();
-
- return NO_ERROR;
- }
-}
+/*
+* IatDirectory.h - Part of the PeLib library.
+*
+* Copyright (c) 2004 - 2005 Sebastian Porst (webmaster@the-interweb.com)
+* All rights reserved.
+*
+* This software is licensed under the zlib/libpng License.
+* For more details see http://www.opensource.org/licenses/zlib-license.php
+* or the license information file (license.htm) in the root directory
+* of PeLib.
+*/
+
+#include "IatDirectory.h"
+
+namespace PeLib
+{
+ int IatDirectory::read(InputBuffer& inputBuffer, unsigned int size)
+ {
+ dword dwAddr;
+
+ std::vector<dword> vIat;
+
+ for (unsigned int i=0;i<size/sizeof(dword);i++)
+ {
+ inputBuffer >> dwAddr;
+ vIat.push_back(dwAddr);
+ }
+
+ std::swap(vIat, m_vIat);
+
+ return NO_ERROR;
+ }
+
+ /**
+ * Reads the Import Address table from a file.
+ * @param strFilename Name of the file.
+ * @param dwOffset File offset of the IAT (see #PeFile::PeHeader::getIDIatRVA).
+ * @param dwSize Size of the IAT (see #PeFile::PeHeader::getIDIatSize).
+ **/
+ int IatDirectory::read(const std::string& strFilename, unsigned int dwOffset, unsigned int dwSize)
+ {
+ std::ifstream ifFile(strFilename.c_str(), std::ios::binary);
+
+ if (!ifFile)
+ {
+ return ERROR_OPENING_FILE;
+ }
+
+ if (fileSize(ifFile) < dwOffset + dwSize)
+ {
+ return ERROR_INVALID_FILE;
+ }
+
+ ifFile.seekg(dwOffset, std::ios::beg);
+
+ std::vector<byte> vBuffer(dwSize);
+ ifFile.read(reinterpret_cast<char*>(&vBuffer[0]), dwSize);
+
+ InputBuffer inpBuffer(vBuffer);
+ return read(inpBuffer, dwSize);
+ }
+
+ int IatDirectory::read(unsigned char* buffer, unsigned int buffersize)
+ {
+ std::vector<byte> vBuffer(buffer, buffer + buffersize);
+ InputBuffer inpBuffer(vBuffer);
+ return read(inpBuffer, buffersize);
+ }
+
+ /**
+ * Returns the number of fields in the IAT. This is equivalent to the number of
+ * imported functions.
+ * @return Number of fields in the IAT.
+ **/
+ unsigned int IatDirectory::calcNumberOfAddresses() const
+ {
+ return static_cast<unsigned int>(m_vIat.size());
+ }
+
+ /**
+ * Returns the dwValue of a field in the IAT.
+ * @param dwAddrnr Number identifying the field.
+ * @return dwValue of the field.
+ **/
+ dword IatDirectory::getAddress(unsigned int index) const
+ {
+ return m_vIat[index];
+ }
+
+ /**
+ * Updates the dwValue of a field in the IAT.
+ * @param dwAddrnr Number identifying the field.
+ * @param dwValue New dwValue of the field.
+ **/
+ void IatDirectory::setAddress(dword dwAddrnr, dword dwValue)
+ {
+ m_vIat[dwAddrnr] = dwValue;
+ }
+
+ /**
+ * Adds another field to the IAT.
+ * @param dwValue dwValue of the new field.
+ **/
+ void IatDirectory::addAddress(dword dwValue)
+ {
+ m_vIat.push_back(dwValue);
+ }
+
+ /**
+ * Removes an address from the IAT.
+ * @param dwAddrnr Number identifying the field.
+ **/
+ void IatDirectory::removeAddress(unsigned int index)
+ {
+ std::vector<dword>::iterator pos = m_vIat.begin() + index;
+ m_vIat.erase(pos);
+ }
+
+ /**
+ * Delete all entries from the IAT.
+ **/
+ void IatDirectory::clear()
+ {
+ m_vIat.clear();
+ }
+
+ /**
+ * Rebuilds the complete Import Address Table.
+ * @param vBuffer Buffer where the rebuilt IAT will be stored.
+ **/
+ void IatDirectory::rebuild(std::vector<byte>& vBuffer) const
+ {
+ vBuffer.reserve(size());
+ OutputBuffer obBuffer(vBuffer);
+
+ for (unsigned int i=0;i<m_vIat.size();i++)
+ {
+ obBuffer << m_vIat[i];
+ }
+ }
+
+ unsigned int IatDirectory::size() const
+ {
+ return static_cast<unsigned int>(m_vIat.size())* sizeof(dword);
+ }
+
+ /// Writes the current IAT to a file.
+ int IatDirectory::write(const std::string& strFilename, unsigned int uiOffset) const
+ {
+ std::fstream ofFile(strFilename.c_str(), std::ios_base::in);
+
+ if (!ofFile)
+ {
+ ofFile.clear();
+ ofFile.open(strFilename.c_str(), std::ios_base::out | std::ios_base::binary);
+ }
+ else
+ {
+ ofFile.close();
+ ofFile.open(strFilename.c_str(), std::ios_base::in | std::ios_base::out | std::ios_base::binary);
+ }
+
+ if (!ofFile)
+ {
+ return ERROR_OPENING_FILE;
+ }
+
+ ofFile.seekp(uiOffset, std::ios::beg);
+
+ std::vector<unsigned char> vBuffer;
+ rebuild(vBuffer);
+
+ ofFile.write(reinterpret_cast<const char*>(&vBuffer[0]), static_cast<unsigned int>(vBuffer.size()));
+
+ ofFile.close();
+
+ return NO_ERROR;
+ }
+}
diff --git a/utils/zenutils/libraries/pelib-0.9/pelib/IatDirectory.h b/utils/zenutils/libraries/pelib-0.9/pelib/IatDirectory.h
index 81ef77ed6a..26c5d4009c 100755..100644
--- a/utils/zenutils/libraries/pelib-0.9/pelib/IatDirectory.h
+++ b/utils/zenutils/libraries/pelib-0.9/pelib/IatDirectory.h
@@ -1,58 +1,58 @@
-/*
-* IatDirectory.h - Part of the PeLib library.
-*
-* Copyright (c) 2004 - 2005 Sebastian Porst (webmaster@the-interweb.com)
-* All rights reserved.
-*
-* This software is licensed under the zlib/libpng License.
-* For more details see http://www.opensource.org/licenses/zlib-license.php
-* or the license information file (license.htm) in the root directory
-* of PeLib.
-*/
-
-#ifndef IATDIRECTORY_H
-#define IATDIRECTORY_H
-
-#include "PeLibInc.h"
-
-namespace PeLib
-{
- /// Class that handles the Import Address Table (IAT)
- /**
- * This class can read and modify the Import Address Table of a PE file.
- **/
- class IatDirectory
- {
- private:
- std::vector<dword> m_vIat; ///< Stores the individual IAT fields.
-
- int read(InputBuffer& inputBuffer, unsigned int size);
-
- public:
- /// Reads the Import Address Table from a PE file.
- int read(const std::string& strFilename, unsigned int dwOffset, unsigned int dwSize); // EXPORT
- int read(unsigned char* buffer, unsigned int buffersize); // EXPORT
- /// Returns the number of fields in the IAT.
- unsigned int calcNumberOfAddresses() const; // EXPORT
- /// Adds another address to the IAT.
- void addAddress(dword dwValue); // EXPORT
- /// Removes an address from the IAT.
- void removeAddress(unsigned int index); // EXPORT
- /// Empties the IAT.
- void clear(); // EXPORT
- // Rebuilds the IAT.
- void rebuild(std::vector<byte>& vBuffer) const; // EXPORT
- /// Returns the size of the current IAT.
- unsigned int size() const; // EXPORT
- /// Writes the current IAT to a file.
- int write(const std::string& strFilename, unsigned int uiOffset) const; // EXPORT
-
- /// Retrieve the value of a field in the IAT.
- dword getAddress(unsigned int index) const; // EXPORT
- /// Change the value of a field in the IAT.
- void setAddress(dword dwAddrnr, dword dwValue); // EXPORT
- };
-}
-
-#endif
-
+/*
+* IatDirectory.h - Part of the PeLib library.
+*
+* Copyright (c) 2004 - 2005 Sebastian Porst (webmaster@the-interweb.com)
+* All rights reserved.
+*
+* This software is licensed under the zlib/libpng License.
+* For more details see http://www.opensource.org/licenses/zlib-license.php
+* or the license information file (license.htm) in the root directory
+* of PeLib.
+*/
+
+#ifndef IATDIRECTORY_H
+#define IATDIRECTORY_H
+
+#include "PeLibInc.h"
+
+namespace PeLib
+{
+ /// Class that handles the Import Address Table (IAT)
+ /**
+ * This class can read and modify the Import Address Table of a PE file.
+ **/
+ class IatDirectory
+ {
+ private:
+ std::vector<dword> m_vIat; ///< Stores the individual IAT fields.
+
+ int read(InputBuffer& inputBuffer, unsigned int size);
+
+ public:
+ /// Reads the Import Address Table from a PE file.
+ int read(const std::string& strFilename, unsigned int dwOffset, unsigned int dwSize); // EXPORT
+ int read(unsigned char* buffer, unsigned int buffersize); // EXPORT
+ /// Returns the number of fields in the IAT.
+ unsigned int calcNumberOfAddresses() const; // EXPORT
+ /// Adds another address to the IAT.
+ void addAddress(dword dwValue); // EXPORT
+ /// Removes an address from the IAT.
+ void removeAddress(unsigned int index); // EXPORT
+ /// Empties the IAT.
+ void clear(); // EXPORT
+ // Rebuilds the IAT.
+ void rebuild(std::vector<byte>& vBuffer) const; // EXPORT
+ /// Returns the size of the current IAT.
+ unsigned int size() const; // EXPORT
+ /// Writes the current IAT to a file.
+ int write(const std::string& strFilename, unsigned int uiOffset) const; // EXPORT
+
+ /// Retrieve the value of a field in the IAT.
+ dword getAddress(unsigned int index) const; // EXPORT
+ /// Change the value of a field in the IAT.
+ void setAddress(dword dwAddrnr, dword dwValue); // EXPORT
+ };
+}
+
+#endif
+
diff --git a/utils/zenutils/libraries/pelib-0.9/pelib/ImportDirectory.h b/utils/zenutils/libraries/pelib-0.9/pelib/ImportDirectory.h
index 6578f0712a..42526f0c3c 100755..100644
--- a/utils/zenutils/libraries/pelib-0.9/pelib/ImportDirectory.h
+++ b/utils/zenutils/libraries/pelib-0.9/pelib/ImportDirectory.h
@@ -1,1139 +1,1139 @@
-/*
-* ImportDirectory.h - Part of the PeLib library.
-*
-* Copyright (c) 2004 - 2005 Sebastian Porst (webmaster@the-interweb.com)
-* All rights reserved.
-*
-* This software is licensed under the zlib/libpng License.
-* For more details see http://www.opensource.org/licenses/zlib-license.php
-* or the license information file (license.htm) in the root directory
-* of PeLib.
-*/
-
-#ifndef IMPORTDIRECTORY_H
-#define IMPORTDIRECTORY_H
-
-#include "PeLibAux.h"
-#include "PeHeader.h"
-
-namespace PeLib
-{
- /// Parameter for functions that can operate on the OLDDIR or new import directory.
- enum currdir {OLDDIR = 1, NEWDIR};
-
- class PeLibException;
-
- /// Class that handles import directories.
- /**
- * This class can read import directories from existing PE files or start completely from scratch.
- * Modifying import directories and writing them to files is also possible.
- * It's worthy to note that many functions require an extra parameter of type currdir
- * because the structure of import directories make it necessary that the OLDDIR import directory
- * must be preserved. That's why some functions (like adding and removing) imported functions
- * only exist for the new import directory, not for the one which is already written to the file.
- * \todo Adding functions by ordinal doesn't work yet (rebuild needs to be changed).
- * \todo Somehow store the rvas of the chunks in the file.
- **/
- template<int bits>
- class ImportDirectory
- {
- typedef typename std::vector<PELIB_IMAGE_IMPORT_DIRECTORY<bits> >::iterator ImpDirFileIterator;
- typedef typename std::vector<PELIB_IMAGE_IMPORT_DIRECTORY<bits> >::const_iterator ConstImpDirFileIterator;
-
- private:
- /// Stores information about already imported DLLs.
- std::vector<PELIB_IMAGE_IMPORT_DIRECTORY<bits> > m_vOldiid;
- /// Stores information about imported DLLs which will be added.
- std::vector<PELIB_IMAGE_IMPORT_DIRECTORY<bits> > m_vNewiid;
-
- // I can't convince Borland C++ to compile the function outside of the class declaration.
- // That's why the function definition is here.
- /// Tests if a certain function is imported.
- template<typename T> bool hasFunction(std::string strFilename, T value, bool(PELIB_THUNK_DATA<bits>::* comp)(T) const) const
- {
- ConstImpDirFileIterator FileIter = m_vOldiid.begin();
- ConstImpDirFileIterator EndIter = m_vOldiid.end();
-
- for (int i=0;i<=1;i++) // Loop once for m_vOldiid and once for m_vNewiid
- {
- do
- {
- FileIter = std::find_if(FileIter, EndIter, std::bind2nd(std::mem_fun_ref(&PELIB_IMAGE_IMPORT_DIRECTORY<bits>::operator==), strFilename));
-
- if (FileIter != EndIter)
- {
- typename std::vector<PELIB_THUNK_DATA<bits> >::const_iterator Iter = std::find_if(FileIter->originalfirstthunk.begin(), FileIter->originalfirstthunk.end(), std::bind2nd(std::mem_fun_ref(comp), value));
- if (Iter != FileIter->originalfirstthunk.end())
- {
- return true;
- }
- ++FileIter;
- }
- }
- while (FileIter != EndIter);
-
- FileIter = m_vNewiid.begin();
- EndIter = m_vNewiid.end();
- }
-
- return false;
- }
-
-
- public:
-
- /// Add a function to the import directory.
- int addFunction(const std::string& strFilename, word wHint); // EXPORT _byHint
- /// Add a function to the import directory.
- int addFunction(const std::string& strFilename, const std::string& strFuncname); // EXPORT _byName
-
- /// Get the ID of a file through it's name.
- unsigned int getFileIndex(const std::string& strFilename, currdir cdDir) const; // EXPORT
- /// Get the ID of a function through it's name.
- unsigned int getFunctionIndex(const std::string& strFilename, const std::string& strFuncname, currdir cdDir) const; // EXPORT
-
- /// Get the name of an imported file.
- std::string getFileName(dword dwFilenr, currdir cdDir) const; // EXPORT
-
- void setFileName(dword filenr, currdir dir, const std::string& name); // EXPORT
-
- /// Get the hint of an imported function.
- word getFunctionHint(dword dwFilenr, dword dwFuncnr, currdir cdDir) const; // EXPORT
- void setFunctionHint(dword dwFilenr, dword dwFuncnr, currdir cdDir, word value); // EXPORT
- /// Get the name of an imported function.
- std::string getFunctionName(dword dwFilenr, dword dwFuncnr, currdir cdDir) const; // EXPORT
- void setFunctionName(dword dwFilenr, dword dwFuncnr, currdir cdDir, const std::string& functionName); // EXPORT
- /// Get the number of files which are imported.
- dword getNumberOfFiles(currdir cdDir) const; // EXPORT
- /// Get the number of fucntions which are imported by a specific file.
- dword getNumberOfFunctions(dword dwFilenr, currdir cdDir) const; // EXPORT
- /// Read a file's import directory.
- int read(const std::string& strFilename, unsigned int uiOffset, unsigned int uiSize, const PeHeaderT<bits>& pehHeader); // EXPORT
- /// Rebuild the import directory.
- void rebuild(std::vector<byte>& vBuffer, dword dwRva, bool fixEntries = true) const; // EXPORT
- /// Remove a file from the import directory.
- int removeFile(const std::string& strFilename); // EXPORT
- /// Remove a function from the import directory.
- int removeFunction(const std::string& strFilename, const std::string& strFuncname); // EXPORT _byName
- /// Remove a function from the import directory.
- int removeFunction(const std::string& strFilename, word wHint); // EXPORT _byHint
- /// Returns the size of the current import directory.
- unsigned int size() const; // EXPORT
- /// Writes the import directory to a file.
- int write(const std::string& strFilename, unsigned int uiOffset, unsigned int uiRva); // EXPORT
-
- /// Returns the FirstThunk value of a function.
- dword getFirstThunk(dword dwFilenr, dword dwFuncnr, currdir cdDir) const; // EXPORT _byNumber
- void setFirstThunk(dword dwFilenr, dword dwFuncnr, currdir cdDir, dword value); // EXPORT _byNumber
- /// Returns the OriginalFirstThunk value of a function.
- dword getOriginalFirstThunk(dword dwFilenr, dword dwFuncnr, currdir cdDir) const; // EXPORT _byNumber
- void setOriginalFirstThunk(dword dwFilenr, dword dwFuncnr, currdir cdDir, dword value); // EXPORT
-
-// dword getFirstThunk(const std::string& strFilename, const std::string& strFuncname, currdir cdDir) const throw (PeLibException);
-// dword getOriginalFirstThunk(const std::string& strFilename, const std::string& strFuncname, currdir cdDir) const throw (PeLibException);
-
- /// Returns the FirstThunk value of a file.
- dword getFirstThunk(const std::string& strFilename, currdir cdDir) const; // EXPORT _byName
- /// Returns the OriginalFirstThunk value of a file.
- dword getOriginalFirstThunk(const std::string& strFilename, currdir cdDir) const; // EXPORT _byName
- /// Returns the ForwarderChain value of a file.
- dword getForwarderChain(const std::string& strFilename, currdir cdDir) const; // EXPORT _byName
- dword getRvaOfName(const std::string& strFilename, currdir cdDir) const; // EXPORT _byName
- /// Returns the TimeDateStamp value of a file.
- dword getTimeDateStamp(const std::string& strFilename, currdir cdDir) const; // EXPORT _byName
-
- /// Returns the FirstThunk value of a file.
- dword getFirstThunk(dword dwFilenr, currdir cdDir) const; // EXPORT
- void setFirstThunk(dword dwFilenr, currdir cdDir, dword value); // EXPORT _byNumber_function
- /// Returns the OriginalFirstThunk value of a file.
- dword getOriginalFirstThunk(dword dwFilenr, currdir cdDir) const; // EXPORT
- void setOriginalFirstThunk(dword dwFilenr, currdir cdDir, dword value); // EXPORT _byNumber_function
- /// Returns the ForwarderChain value of a file.
- dword getForwarderChain(dword dwFilenr, currdir cdDir) const; // EXPORT _byNumber
- void setForwarderChain(dword dwFilenr, currdir cdDir, dword value); // EXPORT _byNumber_function
- dword getRvaOfName(dword dwFilenr, currdir cdDir) const; // EXPORT _byNumber
- void setRvaOfName(dword dwFilenr, currdir cdDir, dword value); // EXPORT
- /// Returns the TimeDateStamp value of a file.
- dword getTimeDateStamp(dword dwFilenr, currdir cdDir) const; // EXPORT
- void setTimeDateStamp(dword dwFilenr, currdir cdDir, dword value); // EXPORT _byNumber
-
-// word getFunctionHint(const std::string& strFilename, const std::string& strFuncname, currdir cdDir) const throw (PeLibException);
- };
-
- /**
- * Add another import (by Ordinal) to the current file. Note that the import table is not automatically updated.
- * The new imported functions will be added when you recalculate the import table as it's necessary
- * to specify the address the import table will have in the file.
- * @param strFilename The name of a DLL.
- * @param wHint The ordinal of the function in the DLL.
- **/
- template<int bits>
- int ImportDirectory<bits>::addFunction(const std::string& strFilename, word wHint)
- {
- if (hasFunction(strFilename, wHint, &PELIB_THUNK_DATA<bits>::equalHint))
- {
- return ERROR_DUPLICATE_ENTRY;
- }
-
- // Find the imported file.
- ImpDirFileIterator FileIter = std::find_if(m_vNewiid.begin(), m_vNewiid.end(), std::bind2nd(std::mem_fun_ref(&PELIB_IMAGE_IMPORT_DIRECTORY<bits>::operator==), strFilename));
-
- PELIB_IMAGE_IMPORT_DIRECTORY<bits> iid;
- PELIB_THUNK_DATA<bits> td;
- td.hint = wHint;
- td.itd.Ordinal = wHint | PELIB_IMAGE_ORDINAL_FLAGS<bits>::IMAGE_ORDINAL_FLAG;
- iid.name = strFilename;
- if (FileIter == m_vNewiid.end())
- {
- iid.originalfirstthunk.push_back(td);
- iid.firstthunk.push_back(td);
- m_vNewiid.push_back(iid);
- }
- else
- {
- FileIter->originalfirstthunk.push_back(td);
- FileIter->firstthunk.push_back(td);
- }
-
- return NO_ERROR;
- }
-
- /**
- * Add a function to the Import Directory.
- * @param strFilename Name of the file which will be imported
- * @param strFuncname Name of the function which will be imported.
- **/
- template<int bits>
- int ImportDirectory<bits>::addFunction(const std::string& strFilename, const std::string& strFuncname)
- {
- if (hasFunction(strFilename, strFuncname, &PELIB_THUNK_DATA<bits>::equalFunctionName))
- {
- return ERROR_DUPLICATE_ENTRY;
- }
-
- // Find the imported file.
- ImpDirFileIterator FileIter = std::find_if(m_vNewiid.begin(), m_vNewiid.end(), std::bind2nd(std::mem_fun_ref(&PELIB_IMAGE_IMPORT_DIRECTORY<bits>::operator==), strFilename));
-
- PELIB_IMAGE_IMPORT_DIRECTORY<bits> iid;
- PELIB_THUNK_DATA<bits> td;
- td.fname = strFuncname;
- iid.name = strFilename;
- if (FileIter == m_vNewiid.end())
- {
- iid.originalfirstthunk.push_back(td);
- iid.firstthunk.push_back(td);
- m_vNewiid.push_back(iid);
- }
- else
- {
- FileIter->originalfirstthunk.push_back(td);
- FileIter->firstthunk.push_back(td);
- }
-
- return NO_ERROR;
- }
-
- /**
- * Searches through the import directory and returns the number of the import
- * directory entry which belongs to the given filename.
- * @param strFilename Name of the imported file.
- * @param cdDir Flag to decide if the OLDDIR or new import directory is used.
- * @return The ID of an imported file.
- **/
- template<int bits>
- unsigned int ImportDirectory<bits>::getFileIndex(const std::string& strFilename, currdir cdDir) const
- {
- const std::vector<PELIB_IMAGE_IMPORT_DIRECTORY<bits> >* currDir;
-
- if (cdDir == OLDDIR)
- {
- currDir = &m_vOldiid;
- }
- else
- {
- currDir = &m_vNewiid;
- }
-
- ConstImpDirFileIterator FileIter = std::find_if(currDir->begin(), currDir->end(), std::bind2nd(std::mem_fun_ref(&PELIB_IMAGE_IMPORT_DIRECTORY<bits>::operator==), strFilename));
-
- if (FileIter != currDir->end())
- {
- return static_cast<unsigned int>(std::distance(currDir->begin(), FileIter));
- }
- else
- {
- return -1;
- // throw Exceptions::InvalidName(ImportDirectoryId, __LINE__);
- }
-
- return NO_ERROR;
- }
-
- /**
- * Searches through an imported file for a specific function.
- * @param strFilename Name of the imported file.
- * @param strFuncname Name of the imported function.
- * @param cdDir Flag to decide if the OLDDIR or new import directory is used.
- * @return ID of the imported function.
- **/
- template<int bits>
- unsigned int ImportDirectory<bits>::getFunctionIndex(const std::string& strFilename, const std::string& strFuncname, currdir cdDir) const
- {
- unsigned int uiFile = getFileIndex(strFilename, cdDir);
-
- for (unsigned int i=0;i<getNumberOfFunctions(uiFile, cdDir);i++)
- {
- if (getFunctionName(uiFile, i, cdDir) == strFuncname) return i;
- }
-
- return -1;
- }
-
- /**
- * Get the name of an imported file.
- * @param dwFilenr Identifies which file should be checked.
- * @param cdDir Flag to decide if the OLDDIR or new import directory is used.
- * @return Name of an imported file.
- **/
- template<int bits>
- std::string ImportDirectory<bits>::getFileName(dword dwFilenr, currdir cdDir) const
- {
- if (cdDir == OLDDIR) return m_vOldiid[dwFilenr].name;
- else return m_vNewiid[dwFilenr].name;
- }
-
- template<int bits>
- void ImportDirectory<bits>::setFileName(dword filenr, currdir dir, const std::string& name)
- {
- if (dir == OLDDIR) m_vOldiid[filenr].name = name;
- else m_vNewiid[filenr].name = name;
- }
-
- /**
- * Get the name of an imported function.
- * @param dwFilenr Identifies which file should be checked.
- * @param dwFuncnr Identifies which function should be checked.
- * @param cdDir Flag to decide if the OLDDIR or new import directory is used.
- * @return Name of an imported function.
- * \todo Marked line is unsafe (function should be rewritten).
- **/
- template<int bits>
- std::string ImportDirectory<bits>::getFunctionName(dword dwFilenr, dword dwFuncnr, currdir cdDir) const
- {
- if (cdDir == OLDDIR)
- {
- // Unsafe
- if (m_vOldiid[dwFilenr].impdesc.OriginalFirstThunk)
- {
- return m_vOldiid[dwFilenr].originalfirstthunk[dwFuncnr].fname;
- }
- else
- {
- return m_vOldiid[dwFilenr].firstthunk[dwFuncnr].fname;
- }
- }
- else
- {
- if (m_vNewiid[dwFilenr].impdesc.OriginalFirstThunk)
- {
- return m_vNewiid[dwFilenr].originalfirstthunk[dwFuncnr].fname;
- }
- else
- {
- return m_vNewiid[dwFilenr].firstthunk[dwFuncnr].fname;
- }
- }
- }
-
- template<int bits>
- void ImportDirectory<bits>::setFunctionName(dword dwFilenr, dword dwFuncnr, currdir cdDir, const std::string& functionName)
- {
- if (cdDir == OLDDIR)
- {
- // Unsafe
- if (m_vOldiid[dwFilenr].impdesc.OriginalFirstThunk)
- {
- m_vOldiid[dwFilenr].originalfirstthunk[dwFuncnr].fname = functionName;
- }
- else
- {
- m_vOldiid[dwFilenr].firstthunk[dwFuncnr].fname = functionName;
- }
- }
- else
- {
- if (m_vNewiid[dwFilenr].impdesc.OriginalFirstThunk)
- {
- m_vNewiid[dwFilenr].originalfirstthunk[dwFuncnr].fname = functionName;
- }
- else
- {
- m_vNewiid[dwFilenr].firstthunk[dwFuncnr].fname = functionName;
- }
- }
- }
-
- /**
- * Get the hint of an imported function.
- * @param dwFilenr Identifies which file should be checked.
- * @param dwFuncnr Identifies which function should be checked.
- * @param cdDir Flag to decide if the OLDDIR or new import directory is used.
- * @return Hint of an imported function.
- **/
- template<int bits>
- word ImportDirectory<bits>::getFunctionHint(dword dwFilenr, dword dwFuncnr, currdir cdDir) const
- {
- if (cdDir == OLDDIR)
- {
- if (m_vOldiid[dwFilenr].impdesc.OriginalFirstThunk)
- {
- return m_vOldiid[dwFilenr].originalfirstthunk[dwFuncnr].hint;
- }
- else
- {
- return m_vOldiid[dwFilenr].firstthunk[dwFuncnr].hint;
- }
- }
- else return m_vNewiid[dwFilenr].originalfirstthunk[dwFuncnr].hint;
- }
-
- template<int bits>
- void ImportDirectory<bits>::setFunctionHint(dword dwFilenr, dword dwFuncnr, currdir cdDir, word value)
- {
- if (cdDir == OLDDIR)
- {
- if (m_vOldiid[dwFilenr].impdesc.OriginalFirstThunk)
- {
- m_vOldiid[dwFilenr].originalfirstthunk[dwFuncnr].hint = value;
- }
- else
- {
- m_vOldiid[dwFilenr].firstthunk[dwFuncnr].hint = value;
- }
- }
- else m_vNewiid[dwFilenr].originalfirstthunk[dwFuncnr].hint = value;
- }
-
- /**
- * Get the number of files which are currently being imported.
- * @param cdDir Flag to decide if the OLDDIR or new import directory is used.
- * @return Number of files which are currently being imported.
- **/
- template<int bits>
- dword ImportDirectory<bits>::getNumberOfFiles(currdir cdDir) const
- {
- if (cdDir == OLDDIR) return static_cast<dword>(m_vOldiid.size());
- else return static_cast<dword>(m_vNewiid.size());
- }
-
- /**
- * Get the number of functions which are currently being imported from a specific file.
- * @param dwFilenr Identifies which file should be checked.
- * @param cdDir Flag to decide if the OLDDIR or new import directory is used.
- * @return Number of functions which are currently being imported from a specific file.
- **/
- template<int bits>
- dword ImportDirectory<bits>::getNumberOfFunctions(dword dwFilenr, currdir cdDir) const
- {
- if (cdDir == OLDDIR) return static_cast<unsigned int>(m_vOldiid[dwFilenr].firstthunk.size());
- else return static_cast<unsigned int>(m_vNewiid[dwFilenr].firstthunk.size());
- }
-
- /**
- * Read an import directory from a file.
- * \todo Check if streams failed.
- * @param strFilename Name of the file which will be read.
- * @param uiOffset Offset of the import directory (see #PeLib::PeHeader::getIDImportRVA).
- * @param uiSize Size of the import directory (see #PeLib::PeHeader::getIDImportSize).
- * @param pehHeader A valid PE header.
- **/
- template<int bits>
- int ImportDirectory<bits>::read(const std::string& strFilename, unsigned int uiOffset, unsigned int uiSize, const PeHeaderT<bits>& pehHeader)
- {
- std::ifstream ifFile(strFilename.c_str(), std::ios_base::binary);
-
- if (!ifFile)
- {
- return ERROR_OPENING_FILE;
- }
-
- unsigned int uiFileSize = fileSize(ifFile);
-
- if (uiFileSize < uiOffset + uiSize)
- {
- return ERROR_INVALID_FILE;
- }
-
- ifFile.seekg(uiOffset, std::ios_base::beg);
-
- std::vector<unsigned char> vImportdirectory(uiSize);
- ifFile.read(reinterpret_cast<char*>(&vImportdirectory[0]), uiSize);
-
- PELIB_IMAGE_IMPORT_DIRECTORY<bits> iidCurr;
- unsigned int uiDesccounter = 0;
-
- InputBuffer inpBuffer(vImportdirectory);
-
- std::vector<PELIB_IMAGE_IMPORT_DIRECTORY<bits> > vOldIidCurr;
-
- do // Read and store all descriptors
- {
- inpBuffer >> iidCurr.impdesc.OriginalFirstThunk;
- inpBuffer >> iidCurr.impdesc.TimeDateStamp;
- inpBuffer >> iidCurr.impdesc.ForwarderChain;
- inpBuffer >> iidCurr.impdesc.Name;
- inpBuffer >> iidCurr.impdesc.FirstThunk;
-
- if (iidCurr.impdesc.OriginalFirstThunk != 0 || iidCurr.impdesc.TimeDateStamp != 0 || iidCurr.impdesc.ForwarderChain != 0 ||
- iidCurr.impdesc.Name != 0 || iidCurr.impdesc.FirstThunk != 0)
- {
- vOldIidCurr.push_back(iidCurr);
- }
-
- uiDesccounter++;
-
- if (uiSize < (uiDesccounter + 1) * PELIB_IMAGE_IMPORT_DESCRIPTOR::size()) break;
- } while (iidCurr.impdesc.OriginalFirstThunk != 0 || iidCurr.impdesc.TimeDateStamp != 0 || iidCurr.impdesc.ForwarderChain != 0 ||
- iidCurr.impdesc.Name != 0 || iidCurr.impdesc.FirstThunk != 0);
-
- char namebuffer[2] = {0};
-
- // Name
- for (unsigned int i=0;i<vOldIidCurr.size();i++)
- {
- ifFile.seekg(static_cast<unsigned int>(pehHeader.rvaToOffset(vOldIidCurr[i].impdesc.Name)), std::ios_base::beg);
-
- std::string dllname = "";
-
- do
- {
- ifFile.read(namebuffer, 1);
- if (!ifFile || !namebuffer[0]) break; // reached end of file or 0-byte
- dllname += namebuffer;
- } while (true);
-
- vOldIidCurr[i].name = dllname;
-
- }
-
- // OriginalFirstThunk
- for (unsigned int i=0;i<vOldIidCurr.size();i++)
- {
- PELIB_THUNK_DATA<bits> tdCurr;
- dword uiVaoft = vOldIidCurr[i].impdesc.OriginalFirstThunk;
-
- if (!uiVaoft)
- {
- continue;
- }
-
- ifFile.seekg(static_cast<unsigned int>(pehHeader.rvaToOffset(uiVaoft)), std::ios_base::beg);
-
- do
- {
- if (uiFileSize < pehHeader.rvaToOffset(uiVaoft) + sizeof(tdCurr.itd.Ordinal))
- {
- return ERROR_INVALID_FILE;
- }
- uiVaoft += sizeof(tdCurr.itd.Ordinal);
-
- ifFile.read(reinterpret_cast<char*>(&tdCurr.itd.Ordinal), sizeof(tdCurr.itd.Ordinal));
- if (tdCurr.itd.Ordinal) vOldIidCurr[i].originalfirstthunk.push_back(tdCurr);
- } while (tdCurr.itd.Ordinal);
- }
-
- // FirstThunk
- for (unsigned int i=0;i<vOldIidCurr.size();i++)
- {
- dword uiVaoft = vOldIidCurr[i].impdesc.FirstThunk;
- PELIB_THUNK_DATA<bits> tdCurr;
-
- ifFile.seekg(static_cast<unsigned int>(pehHeader.rvaToOffset(uiVaoft)), std::ios_base::beg);
-
- do
- {
- if (uiFileSize < pehHeader.rvaToOffset(uiVaoft) + sizeof(tdCurr.itd.Ordinal))
- {
- return ERROR_INVALID_FILE;
- }
-
- uiVaoft += sizeof(tdCurr.itd.Ordinal);
-
- ifFile.read(reinterpret_cast<char*>(&tdCurr.itd.Ordinal), sizeof(tdCurr.itd.Ordinal));
- if (tdCurr.itd.Ordinal) vOldIidCurr[i].firstthunk.push_back(tdCurr);
- } while (tdCurr.itd.Ordinal);
- }
-
- // Names
- for (unsigned int i=0;i<vOldIidCurr.size();i++)
- {
- if (vOldIidCurr[i].impdesc.OriginalFirstThunk)
- {
- for (unsigned int j=0;j<vOldIidCurr[i].originalfirstthunk.size();j++)
- {
- if (vOldIidCurr[i].originalfirstthunk[j].itd.Ordinal & PELIB_IMAGE_ORDINAL_FLAGS<bits>::IMAGE_ORDINAL_FLAG)
- {
- vOldIidCurr[i].originalfirstthunk[j].hint = 0;
- continue;
- }
-
- ifFile.seekg(static_cast<unsigned int>(pehHeader.rvaToOffset(vOldIidCurr[i].originalfirstthunk[j].itd.Ordinal)), std::ios_base::beg);
-
- ifFile.read(reinterpret_cast<char*>(&vOldIidCurr[i].originalfirstthunk[j].hint), sizeof(vOldIidCurr[i].originalfirstthunk[j].hint));
-
- if (!ifFile)
- return ERROR_INVALID_FILE;
-
- std::string funcname = "";
- do
- {
- ifFile.read(namebuffer, 1);
- if (!ifFile || !namebuffer[0]) break; // reached end of file or 0-byte
- funcname += namebuffer;
- } while (true);
-
- vOldIidCurr[i].originalfirstthunk[j].fname = funcname;
- }
- }
- else
- {
- for (unsigned int j=0;j<vOldIidCurr[i].firstthunk.size();j++)
- {
- if (vOldIidCurr[i].firstthunk[j].itd.Ordinal & PELIB_IMAGE_ORDINAL_FLAGS<bits>::IMAGE_ORDINAL_FLAG)
- {
- continue;
- }
-
- ifFile.seekg(static_cast<unsigned int>(pehHeader.rvaToOffset(vOldIidCurr[i].firstthunk[j].itd.Ordinal)), std::ios_base::beg);
-
- ifFile.read(reinterpret_cast<char*>(&vOldIidCurr[i].firstthunk[j].hint), sizeof(vOldIidCurr[i].firstthunk[j].hint));
-
- if (!ifFile)
- return ERROR_INVALID_FILE;
-
- std::string funcname = "";
- do
- {
- ifFile.read(namebuffer, 1);
- if (!ifFile || !namebuffer[0]) break; // reached end of file or 0-byte
- funcname += namebuffer;
- } while (true);
-
- vOldIidCurr[i].firstthunk[j].fname = funcname;
- }
- }
- }
- std::swap(vOldIidCurr, m_vOldiid);
- return NO_ERROR;
- }
-
- /**
- * Rebuilds the import directory.
- * @param vBuffer Buffer the rebuilt import directory will be written to.
- * @param dwRva The RVA of the ImportDirectory in the file.
- * \todo uiSizeoffuncnames is not used.
- **/
- template<int bits>
- void ImportDirectory<bits>::rebuild(std::vector<byte>& vBuffer, dword dwRva, bool fixEntries) const
- {
- unsigned int uiImprva = dwRva;
- unsigned int uiSizeofdescriptors = (static_cast<unsigned int>(m_vNewiid.size() + m_vOldiid.size()) + 1) * PELIB_IMAGE_IMPORT_DESCRIPTOR::size();
-
- unsigned int uiSizeofdllnames = 0, uiSizeoffuncnames = 0;
- unsigned int uiSizeofoft = 0;
-
- for (unsigned int i=0;i<m_vNewiid.size();i++)
- {
- uiSizeofdllnames += static_cast<unsigned int>(m_vNewiid[i].name.size()) + 1;
- uiSizeofoft += (static_cast<unsigned int>(m_vNewiid[i].originalfirstthunk.size())+1) * PELIB_IMAGE_THUNK_DATA<bits>::size();
-
- for(unsigned int j=0;j<m_vNewiid[i].originalfirstthunk.size();j++)
- {
- // +3 for hint (word) and 00-byte
- uiSizeoffuncnames += (static_cast<unsigned int>(m_vNewiid[i].originalfirstthunk[j].fname.size()) + 3);
- }
- }
-
-// for (unsigned int i=0;i<m_vNewiid.size();i++)
-// {
-// uiSizeofoft += (static_cast<unsigned int>(m_vNewiid[i].originalfirstthunk.size())+1) * PELIB_IMAGE_THUNK_DATA<bits>::size();
-// }
-
- OutputBuffer obBuffer(vBuffer);
-
- // Rebuild IMAGE_IMPORT_DESCRIPTORS
- for (unsigned int i=0;i<m_vOldiid.size();i++)
- {
- obBuffer << m_vOldiid[i].impdesc.OriginalFirstThunk;
- obBuffer << m_vOldiid[i].impdesc.TimeDateStamp;
- obBuffer << m_vOldiid[i].impdesc.ForwarderChain;
- obBuffer << m_vOldiid[i].impdesc.Name;
- obBuffer << m_vOldiid[i].impdesc.FirstThunk;
- }
-
- unsigned int dllsize = 0;
-
- for (unsigned int i=0;i<m_vNewiid.size();i++)
- {
- dword dwPoft = uiSizeofdescriptors + uiImprva;
-
- for (unsigned int j=1;j<=i;j++)
- {
- dwPoft += (static_cast<unsigned int>(m_vNewiid[j-1].originalfirstthunk.size()) + 1) * PELIB_IMAGE_THUNK_DATA<bits>::size();
- }
-
- obBuffer << (fixEntries ? dwPoft : m_vNewiid[i].impdesc.OriginalFirstThunk);
- obBuffer << m_vNewiid[i].impdesc.TimeDateStamp;
- obBuffer << m_vNewiid[i].impdesc.ForwarderChain;
- dword dwPdll = uiSizeofdescriptors + uiSizeofoft + uiImprva + dllsize;
- obBuffer << (fixEntries ? dwPdll : m_vNewiid[i].impdesc.Name);
- obBuffer << (fixEntries ? dwPoft : m_vNewiid[i].impdesc.FirstThunk);
-
- dllsize += static_cast<unsigned int>(m_vNewiid[i].name.size()) + 1;
- }
-
- obBuffer << (dword)0;
- obBuffer << (dword)0;
- obBuffer << (dword)0;
- obBuffer << (dword)0;
- obBuffer << (dword)0;
-
- unsigned int uiPfunc = uiSizeofdescriptors + uiSizeofoft + uiSizeofdllnames + uiImprva;
-
- // Rebuild original first thunk
- for (unsigned int i=0;i<m_vNewiid.size();i++)
- {
- for (unsigned int j=0;j<m_vNewiid[i].originalfirstthunk.size();j++)
- {
- if (m_vNewiid[i].originalfirstthunk[j].itd.Ordinal & PELIB_IMAGE_ORDINAL_FLAGS<bits>::IMAGE_ORDINAL_FLAG
- || fixEntries == false)
- {
- obBuffer << m_vNewiid[i].originalfirstthunk[j].itd.Ordinal;
- }
- else
- {
- obBuffer << uiPfunc;
- }
- uiPfunc += static_cast<unsigned int>(m_vNewiid[i].originalfirstthunk[j].fname.size()) + 3;
- }
- obBuffer << (dword)0;
- }
-
- // Write dllnames into import directory
- for (unsigned int i=0;i<m_vNewiid.size();i++)
- {
- obBuffer.add(m_vNewiid[i].name.c_str(), static_cast<unsigned int>(m_vNewiid[i].name.size())+1);
- }
-
- // Write function names into directory
- for (unsigned int i=0;i<m_vNewiid.size();i++)
- {
- for (unsigned int j=0;j<m_vNewiid[i].originalfirstthunk.size();j++)
- {
- obBuffer << m_vNewiid[i].originalfirstthunk[j].hint;
- obBuffer.add(m_vNewiid[i].originalfirstthunk[j].fname.c_str(), static_cast<unsigned int>(m_vNewiid[i].originalfirstthunk[j].fname.size()) + 1);
- }
- }
- }
-
- /**
- * Removes a specific file and all functions of it from the import directory.
- * @param strFilename Name of the file which will be removed.
- **/
- template<int bits>
- int ImportDirectory<bits>::removeFile(const std::string& strFilename)
- {
- unsigned int oldSize = static_cast<unsigned int>(m_vNewiid.size());
-
- m_vNewiid.erase(std::remove_if(m_vNewiid.begin(), m_vNewiid.end(), std::bind2nd(std::mem_fun_ref(&PELIB_IMAGE_IMPORT_DIRECTORY<bits>::operator==), strFilename)), m_vNewiid.end());
-
- return oldSize == m_vNewiid.size() ? 1 : 0;
- }
-
- /**
- * Removes a specific function from the import directory.
- * @param strFilename Name of the file which exports the function.
- * @param strFuncname Name of the imported function.
- **/
- template<int bits>
- int ImportDirectory<bits>::removeFunction(const std::string& strFilename, const std::string& strFuncname)
- {
- ImpDirFileIterator viPos = m_vNewiid.begin();
-
- int notFound = 1;
-
- while (viPos != m_vNewiid.end())
- {
- if (isEqualNc(viPos->name, strFilename))
- {
- unsigned int oldSize = static_cast<unsigned int>(viPos->originalfirstthunk.size());
- viPos->originalfirstthunk.erase(std::remove_if(viPos->originalfirstthunk.begin(), viPos->originalfirstthunk.end(), std::bind2nd(std::mem_fun_ref(&PELIB_THUNK_DATA<bits>::equalFunctionName), strFuncname)), viPos->originalfirstthunk.end());
- //viPos->originalfirstthunk.erase(std::remove_if(viPos->originalfirstthunk.begin(), viPos->originalfirstthunk.end(), std::bind2nd(CompPolicy<PELIB_THUNK_DATA, std::string>(), strFuncname)));
- if (viPos->originalfirstthunk.size() != oldSize) notFound = 0;
- }
- ++viPos;
- }
-
- return notFound;
- }
-
- /**
- * Removes a specific function from the import directory.
- * @param strFilename Name of the file which exports the function.
- * @param wHint The hint of the function.
- **/
- template<int bits>
- int ImportDirectory<bits>::removeFunction(const std::string& strFilename, word wHint)
- {
- ImpDirFileIterator viPos = m_vNewiid.begin();
- int notFound = 1;
-
- while (viPos != m_vNewiid.end())
- {
- if (isEqualNc(viPos->name, strFilename))
- {
- unsigned int oldSize = static_cast<unsigned int>(viPos->originalfirstthunk.size());
- viPos->originalfirstthunk.erase(std::remove_if(viPos->originalfirstthunk.begin(), viPos->originalfirstthunk.end(), std::bind2nd(std::mem_fun_ref(&PELIB_THUNK_DATA<bits>::equalHint), wHint)), viPos->originalfirstthunk.end());
- unsigned int newPos = static_cast<unsigned int>(viPos->originalfirstthunk.size());
- if (viPos->originalfirstthunk.size() != oldSize) notFound = 0;
- }
- ++viPos;
- }
-
- return notFound;
- }
-
- /**
- * Writes the current import directory to a file.
- * @param strFilename Name of the file.
- * @param uiOffset File Offset of the new import directory.
- * @param uiRva RVA which belongs to that file offset.
- **/
- template<int bits>
- int ImportDirectory<bits>::write(const std::string& strFilename, unsigned int uiOffset, unsigned int uiRva)
- {
- std::fstream ofFile(strFilename.c_str(), std::ios_base::in);
-
- if (!ofFile)
- {
- ofFile.clear();
- ofFile.open(strFilename.c_str(), std::ios_base::out | std::ios_base::binary);
- }
- else
- {
- ofFile.close();
- ofFile.open(strFilename.c_str(), std::ios_base::in | std::ios_base::out | std::ios_base::binary);
- }
-
- if (!ofFile)
- {
- return ERROR_OPENING_FILE;
- }
-
- ofFile.seekp(uiOffset, std::ios_base::beg);
-
- std::vector<byte> vBuffer;
-
- rebuild(vBuffer, uiRva);
-
- ofFile.write(reinterpret_cast<const char*>(&vBuffer[0]), vBuffer.size());
- ofFile.close();
-
- std::copy(m_vNewiid.begin(), m_vNewiid.end(), std::back_inserter(m_vOldiid));
- m_vNewiid.clear();
-
- return NO_ERROR;
- }
-
- /**
- * Returns the size of the import directory.
- * @return Size of the import directory.
- **/
- template<int bits>
- unsigned int ImportDirectory<bits>::size() const
- {
- // Only the descriptors of m_vOldiid must be rebuilt, not the data they point to.
- return std::accumulate(m_vNewiid.begin(), m_vNewiid.end(), 0, accumulate<PELIB_IMAGE_IMPORT_DIRECTORY<bits> >)
- + (m_vOldiid.size() + 1) * PELIB_IMAGE_IMPORT_DESCRIPTOR::size();
- }
-
- /**
- * @param strFilename Name of the imported file.
- * @param cdDir Flag to decide if the OLDDIR or new import directory is used.
- * @return FirstThunk value of an imported file.
- **/
- template<int bits>
- dword ImportDirectory<bits>::getFirstThunk(const std::string& strFilename, currdir cdDir) const
- {
- if (cdDir == OLDDIR)
- {
- return m_vOldiid[getFileIndex(strFilename, cdDir)].impdesc.FirstThunk;
- }
- else
- {
- return m_vNewiid[getFileIndex(strFilename, cdDir)].impdesc.FirstThunk;
- }
- }
-
- /**
- * @param strFilename Name of the imported file.
- * @param cdDir Flag to decide if the OLDDIR or new import directory is used.
- * @return OriginalFirstThunk value of an imported file.
- **/
- template<int bits>
- dword ImportDirectory<bits>::getOriginalFirstThunk(const std::string& strFilename, currdir cdDir) const
- {
- if (cdDir == OLDDIR)
- {
- return m_vOldiid[getFileIndex(strFilename, cdDir)].impdesc.OriginalFirstThunk;
- }
- else
- {
- return m_vNewiid[getFileIndex(strFilename, cdDir)].impdesc.OriginalFirstThunk;
- }
- }
-
- /**
- * @param strFilename Name of the imported file.
- * @param cdDir Flag to decide if the OLDDIR or new import directory is used.
- * @return ForwarderChain value of an imported file.
- **/
- template<int bits>
- dword ImportDirectory<bits>::getForwarderChain(const std::string& strFilename, currdir cdDir) const
- {
- if (cdDir == OLDDIR)
- {
- return m_vOldiid[getFileIndex(strFilename, cdDir)].impdesc.ForwarderChain;
- }
- else
- {
- return m_vNewiid[getFileIndex(strFilename, cdDir)].impdesc.ForwarderChain;
- }
- }
-
- /**
- * @param strFilename Name of the imported file.
- * @param cdDir Flag to decide if the OLDDIR or new import directory is used.
- * @return TimeDateStamp value of an imported file.
- **/
- template<int bits>
- dword ImportDirectory<bits>::getTimeDateStamp(const std::string& strFilename, currdir cdDir) const
- {
- if (cdDir == OLDDIR)
- {
- return m_vOldiid[getFileIndex(strFilename, cdDir)].impdesc.TimeDateStamp;
- }
- else
- {
- return m_vNewiid[getFileIndex(strFilename, cdDir)].impdesc.TimeDateStamp;
- }
- }
-
- template<int bits>
- dword ImportDirectory<bits>::getRvaOfName(const std::string& strFilename, currdir cdDir) const
- {
- if (cdDir == OLDDIR)
- {
- return m_vOldiid[getFileIndex(strFilename, cdDir)].impdesc.Name;
- }
- else
- {
- return m_vNewiid[getFileIndex(strFilename, cdDir)].impdesc.Name;
- }
- }
-
- /**
- * @param strFilename Name of the imported file.
- * @param cdDir Flag to decide if the OLDDIR or new import directory is used.
- * @return FirstThunk value of an imported file.
- **/
- template<int bits>
- dword ImportDirectory<bits>::getFirstThunk(dword dwFilenr, currdir cdDir) const
- {
- if (cdDir == OLDDIR)
- {
- return m_vOldiid[dwFilenr].impdesc.FirstThunk;
- }
- else
- {
- return m_vNewiid[dwFilenr].impdesc.FirstThunk;
- }
- }
-
- template<int bits>
- void ImportDirectory<bits>::setFirstThunk(dword dwFilenr, currdir cdDir, dword value)
- {
- if (cdDir == OLDDIR)
- {
- m_vOldiid[dwFilenr].impdesc.FirstThunk = value;
- }
- else
- {
- m_vNewiid[dwFilenr].impdesc.FirstThunk = value;
- }
- }
-
- /**
- * @param strFilename Name of the imported file.
- * @param cdDir Flag to decide if the OLDDIR or new import directory is used.
- * @return OriginalFirstThunk value of an imported file.
- **/
- template<int bits>
- dword ImportDirectory<bits>::getOriginalFirstThunk(dword dwFilenr, currdir cdDir) const
- {
- if (cdDir == OLDDIR)
- {
- return m_vOldiid[dwFilenr].impdesc.OriginalFirstThunk;
- }
- else
- {
- return m_vNewiid[dwFilenr].impdesc.OriginalFirstThunk;
- }
- }
-
- template<int bits>
- void ImportDirectory<bits>::setOriginalFirstThunk(dword dwFilenr, currdir cdDir, dword value)
- {
- if (cdDir == OLDDIR)
- {
- m_vOldiid[dwFilenr].impdesc.OriginalFirstThunk = value;
- }
- else
- {
- m_vNewiid[dwFilenr].impdesc.OriginalFirstThunk = value;
- }
- }
-
- /**
- * @param strFilename Name of the imported file.
- * @param cdDir Flag to decide if the OLDDIR or new import directory is used.
- * @return ForwarderChain value of an imported file.
- **/
- template<int bits>
- dword ImportDirectory<bits>::getForwarderChain(dword dwFilenr, currdir cdDir) const
- {
- if (cdDir == OLDDIR)
- {
- return m_vOldiid[dwFilenr].impdesc.ForwarderChain;
- }
- else
- {
- return m_vNewiid[dwFilenr].impdesc.ForwarderChain;
- }
- }
-
- template<int bits>
- void ImportDirectory<bits>::setForwarderChain(dword dwFilenr, currdir cdDir, dword value)
- {
- if (cdDir == OLDDIR)
- {
- m_vOldiid[dwFilenr].impdesc.ForwarderChain = value;
- }
- else
- {
- m_vNewiid[dwFilenr].impdesc.ForwarderChain = value;
- }
- }
-
- /**
- * @param strFilename Name of the imported file.
- * @param cdDir Flag to decide if the OLDDIR or new import directory is used.
- * @return TimeDateStamp value of an imported file.
- **/
- template<int bits>
- dword ImportDirectory<bits>::getTimeDateStamp(dword dwFilenr, currdir cdDir) const
- {
- if (cdDir == OLDDIR)
- {
- return m_vOldiid[dwFilenr].impdesc.TimeDateStamp;
- }
- else
- {
- return m_vNewiid[dwFilenr].impdesc.TimeDateStamp;
- }
- }
-
- template<int bits>
- void ImportDirectory<bits>::setTimeDateStamp(dword dwFilenr, currdir cdDir, dword value)
- {
- if (cdDir == OLDDIR)
- {
- m_vOldiid[dwFilenr].impdesc.TimeDateStamp = value;
- }
- else
- {
- m_vNewiid[dwFilenr].impdesc.TimeDateStamp = value;
- }
- }
-
- template<int bits>
- dword ImportDirectory<bits>::getRvaOfName(dword dwFilenr, currdir cdDir) const
- {
- if (cdDir == OLDDIR)
- {
- return m_vOldiid[dwFilenr].impdesc.Name;
- }
- else
- {
- return m_vNewiid[dwFilenr].impdesc.Name;
- }
- }
-
- template<int bits>
- void ImportDirectory<bits>::setRvaOfName(dword dwFilenr, currdir cdDir, dword value)
- {
- if (cdDir == OLDDIR)
- {
- m_vOldiid[dwFilenr].impdesc.Name = value;
- }
- else
- {
- m_vNewiid[dwFilenr].impdesc.Name = value;
- }
- }
-
- /**
- * @param dwFilenr ID of the imported file.
- * @param dwFuncnr ID of the imported function.
- * @param cdDir Flag to decide if the OLDDIR or new import directory is used.
- * @return FirstThunk value of an imported function.
- **/
- template<int bits>
- dword ImportDirectory<bits>::getFirstThunk(dword dwFilenr, dword dwFuncnr, currdir cdDir) const
- {
- if (cdDir == OLDDIR) return m_vOldiid[dwFilenr].firstthunk[dwFuncnr].itd.Ordinal;
- else return m_vNewiid[dwFilenr].firstthunk[dwFuncnr].itd.Ordinal;
- }
-
- template<int bits>
- void ImportDirectory<bits>::setFirstThunk(dword dwFilenr, dword dwFuncnr, currdir cdDir, dword value)
- {
- if (cdDir == OLDDIR) m_vOldiid[dwFilenr].firstthunk[dwFuncnr].itd.Ordinal = value;
- else m_vNewiid[dwFilenr].firstthunk[dwFuncnr].itd.Ordinal = value;
- }
-
- /**
- * @param dwFilenr ID of the imported file.
- * @param dwFuncnr ID of the imported function.
- * @param cdDir Flag to decide if the OLDDIR or new import directory is used.
- * @return OriginalFirstThunk value of an imported function.
- **/
- template<int bits>
- dword ImportDirectory<bits>::getOriginalFirstThunk(dword dwFilenr, dword dwFuncnr, currdir cdDir) const
- {
- if (cdDir == OLDDIR) return m_vOldiid[dwFilenr].originalfirstthunk[dwFuncnr].itd.Ordinal;
- else return m_vNewiid[dwFilenr].originalfirstthunk[dwFuncnr].itd.Ordinal;
- }
-
- template<int bits>
- void ImportDirectory<bits>::setOriginalFirstThunk(dword dwFilenr, dword dwFuncnr, currdir cdDir, dword value)
- {
- if (cdDir == OLDDIR) m_vOldiid[dwFilenr].originalfirstthunk[dwFuncnr].itd.Ordinal = value;
- else m_vNewiid[dwFilenr].originalfirstthunk[dwFuncnr].itd.Ordinal = value;
- }
-
- typedef ImportDirectory<32> ImportDirectory32;
- typedef ImportDirectory<64> ImportDirectory64;
-}
-
-#endif
+/*
+* ImportDirectory.h - Part of the PeLib library.
+*
+* Copyright (c) 2004 - 2005 Sebastian Porst (webmaster@the-interweb.com)
+* All rights reserved.
+*
+* This software is licensed under the zlib/libpng License.
+* For more details see http://www.opensource.org/licenses/zlib-license.php
+* or the license information file (license.htm) in the root directory
+* of PeLib.
+*/
+
+#ifndef IMPORTDIRECTORY_H
+#define IMPORTDIRECTORY_H
+
+#include "PeLibAux.h"
+#include "PeHeader.h"
+
+namespace PeLib
+{
+ /// Parameter for functions that can operate on the OLDDIR or new import directory.
+ enum currdir {OLDDIR = 1, NEWDIR};
+
+ class PeLibException;
+
+ /// Class that handles import directories.
+ /**
+ * This class can read import directories from existing PE files or start completely from scratch.
+ * Modifying import directories and writing them to files is also possible.
+ * It's worthy to note that many functions require an extra parameter of type currdir
+ * because the structure of import directories make it necessary that the OLDDIR import directory
+ * must be preserved. That's why some functions (like adding and removing) imported functions
+ * only exist for the new import directory, not for the one which is already written to the file.
+ * \todo Adding functions by ordinal doesn't work yet (rebuild needs to be changed).
+ * \todo Somehow store the rvas of the chunks in the file.
+ **/
+ template<int bits>
+ class ImportDirectory
+ {
+ typedef typename std::vector<PELIB_IMAGE_IMPORT_DIRECTORY<bits> >::iterator ImpDirFileIterator;
+ typedef typename std::vector<PELIB_IMAGE_IMPORT_DIRECTORY<bits> >::const_iterator ConstImpDirFileIterator;
+
+ private:
+ /// Stores information about already imported DLLs.
+ std::vector<PELIB_IMAGE_IMPORT_DIRECTORY<bits> > m_vOldiid;
+ /// Stores information about imported DLLs which will be added.
+ std::vector<PELIB_IMAGE_IMPORT_DIRECTORY<bits> > m_vNewiid;
+
+ // I can't convince Borland C++ to compile the function outside of the class declaration.
+ // That's why the function definition is here.
+ /// Tests if a certain function is imported.
+ template<typename T> bool hasFunction(std::string strFilename, T value, bool(PELIB_THUNK_DATA<bits>::* comp)(T) const) const
+ {
+ ConstImpDirFileIterator FileIter = m_vOldiid.begin();
+ ConstImpDirFileIterator EndIter = m_vOldiid.end();
+
+ for (int i=0;i<=1;i++) // Loop once for m_vOldiid and once for m_vNewiid
+ {
+ do
+ {
+ FileIter = std::find_if(FileIter, EndIter, std::bind2nd(std::mem_fun_ref(&PELIB_IMAGE_IMPORT_DIRECTORY<bits>::operator==), strFilename));
+
+ if (FileIter != EndIter)
+ {
+ typename std::vector<PELIB_THUNK_DATA<bits> >::const_iterator Iter = std::find_if(FileIter->originalfirstthunk.begin(), FileIter->originalfirstthunk.end(), std::bind2nd(std::mem_fun_ref(comp), value));
+ if (Iter != FileIter->originalfirstthunk.end())
+ {
+ return true;
+ }
+ ++FileIter;
+ }
+ }
+ while (FileIter != EndIter);
+
+ FileIter = m_vNewiid.begin();
+ EndIter = m_vNewiid.end();
+ }
+
+ return false;
+ }
+
+
+ public:
+
+ /// Add a function to the import directory.
+ int addFunction(const std::string& strFilename, word wHint); // EXPORT _byHint
+ /// Add a function to the import directory.
+ int addFunction(const std::string& strFilename, const std::string& strFuncname); // EXPORT _byName
+
+ /// Get the ID of a file through it's name.
+ unsigned int getFileIndex(const std::string& strFilename, currdir cdDir) const; // EXPORT
+ /// Get the ID of a function through it's name.
+ unsigned int getFunctionIndex(const std::string& strFilename, const std::string& strFuncname, currdir cdDir) const; // EXPORT
+
+ /// Get the name of an imported file.
+ std::string getFileName(dword dwFilenr, currdir cdDir) const; // EXPORT
+
+ void setFileName(dword filenr, currdir dir, const std::string& name); // EXPORT
+
+ /// Get the hint of an imported function.
+ word getFunctionHint(dword dwFilenr, dword dwFuncnr, currdir cdDir) const; // EXPORT
+ void setFunctionHint(dword dwFilenr, dword dwFuncnr, currdir cdDir, word value); // EXPORT
+ /// Get the name of an imported function.
+ std::string getFunctionName(dword dwFilenr, dword dwFuncnr, currdir cdDir) const; // EXPORT
+ void setFunctionName(dword dwFilenr, dword dwFuncnr, currdir cdDir, const std::string& functionName); // EXPORT
+ /// Get the number of files which are imported.
+ dword getNumberOfFiles(currdir cdDir) const; // EXPORT
+ /// Get the number of fucntions which are imported by a specific file.
+ dword getNumberOfFunctions(dword dwFilenr, currdir cdDir) const; // EXPORT
+ /// Read a file's import directory.
+ int read(const std::string& strFilename, unsigned int uiOffset, unsigned int uiSize, const PeHeaderT<bits>& pehHeader); // EXPORT
+ /// Rebuild the import directory.
+ void rebuild(std::vector<byte>& vBuffer, dword dwRva, bool fixEntries = true) const; // EXPORT
+ /// Remove a file from the import directory.
+ int removeFile(const std::string& strFilename); // EXPORT
+ /// Remove a function from the import directory.
+ int removeFunction(const std::string& strFilename, const std::string& strFuncname); // EXPORT _byName
+ /// Remove a function from the import directory.
+ int removeFunction(const std::string& strFilename, word wHint); // EXPORT _byHint
+ /// Returns the size of the current import directory.
+ unsigned int size() const; // EXPORT
+ /// Writes the import directory to a file.
+ int write(const std::string& strFilename, unsigned int uiOffset, unsigned int uiRva); // EXPORT
+
+ /// Returns the FirstThunk value of a function.
+ dword getFirstThunk(dword dwFilenr, dword dwFuncnr, currdir cdDir) const; // EXPORT _byNumber
+ void setFirstThunk(dword dwFilenr, dword dwFuncnr, currdir cdDir, dword value); // EXPORT _byNumber
+ /// Returns the OriginalFirstThunk value of a function.
+ dword getOriginalFirstThunk(dword dwFilenr, dword dwFuncnr, currdir cdDir) const; // EXPORT _byNumber
+ void setOriginalFirstThunk(dword dwFilenr, dword dwFuncnr, currdir cdDir, dword value); // EXPORT
+
+// dword getFirstThunk(const std::string& strFilename, const std::string& strFuncname, currdir cdDir) const throw (PeLibException);
+// dword getOriginalFirstThunk(const std::string& strFilename, const std::string& strFuncname, currdir cdDir) const throw (PeLibException);
+
+ /// Returns the FirstThunk value of a file.
+ dword getFirstThunk(const std::string& strFilename, currdir cdDir) const; // EXPORT _byName
+ /// Returns the OriginalFirstThunk value of a file.
+ dword getOriginalFirstThunk(const std::string& strFilename, currdir cdDir) const; // EXPORT _byName
+ /// Returns the ForwarderChain value of a file.
+ dword getForwarderChain(const std::string& strFilename, currdir cdDir) const; // EXPORT _byName
+ dword getRvaOfName(const std::string& strFilename, currdir cdDir) const; // EXPORT _byName
+ /// Returns the TimeDateStamp value of a file.
+ dword getTimeDateStamp(const std::string& strFilename, currdir cdDir) const; // EXPORT _byName
+
+ /// Returns the FirstThunk value of a file.
+ dword getFirstThunk(dword dwFilenr, currdir cdDir) const; // EXPORT
+ void setFirstThunk(dword dwFilenr, currdir cdDir, dword value); // EXPORT _byNumber_function
+ /// Returns the OriginalFirstThunk value of a file.
+ dword getOriginalFirstThunk(dword dwFilenr, currdir cdDir) const; // EXPORT
+ void setOriginalFirstThunk(dword dwFilenr, currdir cdDir, dword value); // EXPORT _byNumber_function
+ /// Returns the ForwarderChain value of a file.
+ dword getForwarderChain(dword dwFilenr, currdir cdDir) const; // EXPORT _byNumber
+ void setForwarderChain(dword dwFilenr, currdir cdDir, dword value); // EXPORT _byNumber_function
+ dword getRvaOfName(dword dwFilenr, currdir cdDir) const; // EXPORT _byNumber
+ void setRvaOfName(dword dwFilenr, currdir cdDir, dword value); // EXPORT
+ /// Returns the TimeDateStamp value of a file.
+ dword getTimeDateStamp(dword dwFilenr, currdir cdDir) const; // EXPORT
+ void setTimeDateStamp(dword dwFilenr, currdir cdDir, dword value); // EXPORT _byNumber
+
+// word getFunctionHint(const std::string& strFilename, const std::string& strFuncname, currdir cdDir) const throw (PeLibException);
+ };
+
+ /**
+ * Add another import (by Ordinal) to the current file. Note that the import table is not automatically updated.
+ * The new imported functions will be added when you recalculate the import table as it's necessary
+ * to specify the address the import table will have in the file.
+ * @param strFilename The name of a DLL.
+ * @param wHint The ordinal of the function in the DLL.
+ **/
+ template<int bits>
+ int ImportDirectory<bits>::addFunction(const std::string& strFilename, word wHint)
+ {
+ if (hasFunction(strFilename, wHint, &PELIB_THUNK_DATA<bits>::equalHint))
+ {
+ return ERROR_DUPLICATE_ENTRY;
+ }
+
+ // Find the imported file.
+ ImpDirFileIterator FileIter = std::find_if(m_vNewiid.begin(), m_vNewiid.end(), std::bind2nd(std::mem_fun_ref(&PELIB_IMAGE_IMPORT_DIRECTORY<bits>::operator==), strFilename));
+
+ PELIB_IMAGE_IMPORT_DIRECTORY<bits> iid;
+ PELIB_THUNK_DATA<bits> td;
+ td.hint = wHint;
+ td.itd.Ordinal = wHint | PELIB_IMAGE_ORDINAL_FLAGS<bits>::IMAGE_ORDINAL_FLAG;
+ iid.name = strFilename;
+ if (FileIter == m_vNewiid.end())
+ {
+ iid.originalfirstthunk.push_back(td);
+ iid.firstthunk.push_back(td);
+ m_vNewiid.push_back(iid);
+ }
+ else
+ {
+ FileIter->originalfirstthunk.push_back(td);
+ FileIter->firstthunk.push_back(td);
+ }
+
+ return NO_ERROR;
+ }
+
+ /**
+ * Add a function to the Import Directory.
+ * @param strFilename Name of the file which will be imported
+ * @param strFuncname Name of the function which will be imported.
+ **/
+ template<int bits>
+ int ImportDirectory<bits>::addFunction(const std::string& strFilename, const std::string& strFuncname)
+ {
+ if (hasFunction(strFilename, strFuncname, &PELIB_THUNK_DATA<bits>::equalFunctionName))
+ {
+ return ERROR_DUPLICATE_ENTRY;
+ }
+
+ // Find the imported file.
+ ImpDirFileIterator FileIter = std::find_if(m_vNewiid.begin(), m_vNewiid.end(), std::bind2nd(std::mem_fun_ref(&PELIB_IMAGE_IMPORT_DIRECTORY<bits>::operator==), strFilename));
+
+ PELIB_IMAGE_IMPORT_DIRECTORY<bits> iid;
+ PELIB_THUNK_DATA<bits> td;
+ td.fname = strFuncname;
+ iid.name = strFilename;
+ if (FileIter == m_vNewiid.end())
+ {
+ iid.originalfirstthunk.push_back(td);
+ iid.firstthunk.push_back(td);
+ m_vNewiid.push_back(iid);
+ }
+ else
+ {
+ FileIter->originalfirstthunk.push_back(td);
+ FileIter->firstthunk.push_back(td);
+ }
+
+ return NO_ERROR;
+ }
+
+ /**
+ * Searches through the import directory and returns the number of the import
+ * directory entry which belongs to the given filename.
+ * @param strFilename Name of the imported file.
+ * @param cdDir Flag to decide if the OLDDIR or new import directory is used.
+ * @return The ID of an imported file.
+ **/
+ template<int bits>
+ unsigned int ImportDirectory<bits>::getFileIndex(const std::string& strFilename, currdir cdDir) const
+ {
+ const std::vector<PELIB_IMAGE_IMPORT_DIRECTORY<bits> >* currDir;
+
+ if (cdDir == OLDDIR)
+ {
+ currDir = &m_vOldiid;
+ }
+ else
+ {
+ currDir = &m_vNewiid;
+ }
+
+ ConstImpDirFileIterator FileIter = std::find_if(currDir->begin(), currDir->end(), std::bind2nd(std::mem_fun_ref(&PELIB_IMAGE_IMPORT_DIRECTORY<bits>::operator==), strFilename));
+
+ if (FileIter != currDir->end())
+ {
+ return static_cast<unsigned int>(std::distance(currDir->begin(), FileIter));
+ }
+ else
+ {
+ return -1;
+ // throw Exceptions::InvalidName(ImportDirectoryId, __LINE__);
+ }
+
+ return NO_ERROR;
+ }
+
+ /**
+ * Searches through an imported file for a specific function.
+ * @param strFilename Name of the imported file.
+ * @param strFuncname Name of the imported function.
+ * @param cdDir Flag to decide if the OLDDIR or new import directory is used.
+ * @return ID of the imported function.
+ **/
+ template<int bits>
+ unsigned int ImportDirectory<bits>::getFunctionIndex(const std::string& strFilename, const std::string& strFuncname, currdir cdDir) const
+ {
+ unsigned int uiFile = getFileIndex(strFilename, cdDir);
+
+ for (unsigned int i=0;i<getNumberOfFunctions(uiFile, cdDir);i++)
+ {
+ if (getFunctionName(uiFile, i, cdDir) == strFuncname) return i;
+ }
+
+ return -1;
+ }
+
+ /**
+ * Get the name of an imported file.
+ * @param dwFilenr Identifies which file should be checked.
+ * @param cdDir Flag to decide if the OLDDIR or new import directory is used.
+ * @return Name of an imported file.
+ **/
+ template<int bits>
+ std::string ImportDirectory<bits>::getFileName(dword dwFilenr, currdir cdDir) const
+ {
+ if (cdDir == OLDDIR) return m_vOldiid[dwFilenr].name;
+ else return m_vNewiid[dwFilenr].name;
+ }
+
+ template<int bits>
+ void ImportDirectory<bits>::setFileName(dword filenr, currdir dir, const std::string& name)
+ {
+ if (dir == OLDDIR) m_vOldiid[filenr].name = name;
+ else m_vNewiid[filenr].name = name;
+ }
+
+ /**
+ * Get the name of an imported function.
+ * @param dwFilenr Identifies which file should be checked.
+ * @param dwFuncnr Identifies which function should be checked.
+ * @param cdDir Flag to decide if the OLDDIR or new import directory is used.
+ * @return Name of an imported function.
+ * \todo Marked line is unsafe (function should be rewritten).
+ **/
+ template<int bits>
+ std::string ImportDirectory<bits>::getFunctionName(dword dwFilenr, dword dwFuncnr, currdir cdDir) const
+ {
+ if (cdDir == OLDDIR)
+ {
+ // Unsafe
+ if (m_vOldiid[dwFilenr].impdesc.OriginalFirstThunk)
+ {
+ return m_vOldiid[dwFilenr].originalfirstthunk[dwFuncnr].fname;
+ }
+ else
+ {
+ return m_vOldiid[dwFilenr].firstthunk[dwFuncnr].fname;
+ }
+ }
+ else
+ {
+ if (m_vNewiid[dwFilenr].impdesc.OriginalFirstThunk)
+ {
+ return m_vNewiid[dwFilenr].originalfirstthunk[dwFuncnr].fname;
+ }
+ else
+ {
+ return m_vNewiid[dwFilenr].firstthunk[dwFuncnr].fname;
+ }
+ }
+ }
+
+ template<int bits>
+ void ImportDirectory<bits>::setFunctionName(dword dwFilenr, dword dwFuncnr, currdir cdDir, const std::string& functionName)
+ {
+ if (cdDir == OLDDIR)
+ {
+ // Unsafe
+ if (m_vOldiid[dwFilenr].impdesc.OriginalFirstThunk)
+ {
+ m_vOldiid[dwFilenr].originalfirstthunk[dwFuncnr].fname = functionName;
+ }
+ else
+ {
+ m_vOldiid[dwFilenr].firstthunk[dwFuncnr].fname = functionName;
+ }
+ }
+ else
+ {
+ if (m_vNewiid[dwFilenr].impdesc.OriginalFirstThunk)
+ {
+ m_vNewiid[dwFilenr].originalfirstthunk[dwFuncnr].fname = functionName;
+ }
+ else
+ {
+ m_vNewiid[dwFilenr].firstthunk[dwFuncnr].fname = functionName;
+ }
+ }
+ }
+
+ /**
+ * Get the hint of an imported function.
+ * @param dwFilenr Identifies which file should be checked.
+ * @param dwFuncnr Identifies which function should be checked.
+ * @param cdDir Flag to decide if the OLDDIR or new import directory is used.
+ * @return Hint of an imported function.
+ **/
+ template<int bits>
+ word ImportDirectory<bits>::getFunctionHint(dword dwFilenr, dword dwFuncnr, currdir cdDir) const
+ {
+ if (cdDir == OLDDIR)
+ {
+ if (m_vOldiid[dwFilenr].impdesc.OriginalFirstThunk)
+ {
+ return m_vOldiid[dwFilenr].originalfirstthunk[dwFuncnr].hint;
+ }
+ else
+ {
+ return m_vOldiid[dwFilenr].firstthunk[dwFuncnr].hint;
+ }
+ }
+ else return m_vNewiid[dwFilenr].originalfirstthunk[dwFuncnr].hint;
+ }
+
+ template<int bits>
+ void ImportDirectory<bits>::setFunctionHint(dword dwFilenr, dword dwFuncnr, currdir cdDir, word value)
+ {
+ if (cdDir == OLDDIR)
+ {
+ if (m_vOldiid[dwFilenr].impdesc.OriginalFirstThunk)
+ {
+ m_vOldiid[dwFilenr].originalfirstthunk[dwFuncnr].hint = value;
+ }
+ else
+ {
+ m_vOldiid[dwFilenr].firstthunk[dwFuncnr].hint = value;
+ }
+ }
+ else m_vNewiid[dwFilenr].originalfirstthunk[dwFuncnr].hint = value;
+ }
+
+ /**
+ * Get the number of files which are currently being imported.
+ * @param cdDir Flag to decide if the OLDDIR or new import directory is used.
+ * @return Number of files which are currently being imported.
+ **/
+ template<int bits>
+ dword ImportDirectory<bits>::getNumberOfFiles(currdir cdDir) const
+ {
+ if (cdDir == OLDDIR) return static_cast<dword>(m_vOldiid.size());
+ else return static_cast<dword>(m_vNewiid.size());
+ }
+
+ /**
+ * Get the number of functions which are currently being imported from a specific file.
+ * @param dwFilenr Identifies which file should be checked.
+ * @param cdDir Flag to decide if the OLDDIR or new import directory is used.
+ * @return Number of functions which are currently being imported from a specific file.
+ **/
+ template<int bits>
+ dword ImportDirectory<bits>::getNumberOfFunctions(dword dwFilenr, currdir cdDir) const
+ {
+ if (cdDir == OLDDIR) return static_cast<unsigned int>(m_vOldiid[dwFilenr].firstthunk.size());
+ else return static_cast<unsigned int>(m_vNewiid[dwFilenr].firstthunk.size());
+ }
+
+ /**
+ * Read an import directory from a file.
+ * \todo Check if streams failed.
+ * @param strFilename Name of the file which will be read.
+ * @param uiOffset Offset of the import directory (see #PeLib::PeHeader::getIDImportRVA).
+ * @param uiSize Size of the import directory (see #PeLib::PeHeader::getIDImportSize).
+ * @param pehHeader A valid PE header.
+ **/
+ template<int bits>
+ int ImportDirectory<bits>::read(const std::string& strFilename, unsigned int uiOffset, unsigned int uiSize, const PeHeaderT<bits>& pehHeader)
+ {
+ std::ifstream ifFile(strFilename.c_str(), std::ios_base::binary);
+
+ if (!ifFile)
+ {
+ return ERROR_OPENING_FILE;
+ }
+
+ unsigned int uiFileSize = fileSize(ifFile);
+
+ if (uiFileSize < uiOffset + uiSize)
+ {
+ return ERROR_INVALID_FILE;
+ }
+
+ ifFile.seekg(uiOffset, std::ios_base::beg);
+
+ std::vector<unsigned char> vImportdirectory(uiSize);
+ ifFile.read(reinterpret_cast<char*>(&vImportdirectory[0]), uiSize);
+
+ PELIB_IMAGE_IMPORT_DIRECTORY<bits> iidCurr;
+ unsigned int uiDesccounter = 0;
+
+ InputBuffer inpBuffer(vImportdirectory);
+
+ std::vector<PELIB_IMAGE_IMPORT_DIRECTORY<bits> > vOldIidCurr;
+
+ do // Read and store all descriptors
+ {
+ inpBuffer >> iidCurr.impdesc.OriginalFirstThunk;
+ inpBuffer >> iidCurr.impdesc.TimeDateStamp;
+ inpBuffer >> iidCurr.impdesc.ForwarderChain;
+ inpBuffer >> iidCurr.impdesc.Name;
+ inpBuffer >> iidCurr.impdesc.FirstThunk;
+
+ if (iidCurr.impdesc.OriginalFirstThunk != 0 || iidCurr.impdesc.TimeDateStamp != 0 || iidCurr.impdesc.ForwarderChain != 0 ||
+ iidCurr.impdesc.Name != 0 || iidCurr.impdesc.FirstThunk != 0)
+ {
+ vOldIidCurr.push_back(iidCurr);
+ }
+
+ uiDesccounter++;
+
+ if (uiSize < (uiDesccounter + 1) * PELIB_IMAGE_IMPORT_DESCRIPTOR::size()) break;
+ } while (iidCurr.impdesc.OriginalFirstThunk != 0 || iidCurr.impdesc.TimeDateStamp != 0 || iidCurr.impdesc.ForwarderChain != 0 ||
+ iidCurr.impdesc.Name != 0 || iidCurr.impdesc.FirstThunk != 0);
+
+ char namebuffer[2] = {0};
+
+ // Name
+ for (unsigned int i=0;i<vOldIidCurr.size();i++)
+ {
+ ifFile.seekg(static_cast<unsigned int>(pehHeader.rvaToOffset(vOldIidCurr[i].impdesc.Name)), std::ios_base::beg);
+
+ std::string dllname = "";
+
+ do
+ {
+ ifFile.read(namebuffer, 1);
+ if (!ifFile || !namebuffer[0]) break; // reached end of file or 0-byte
+ dllname += namebuffer;
+ } while (true);
+
+ vOldIidCurr[i].name = dllname;
+
+ }
+
+ // OriginalFirstThunk
+ for (unsigned int i=0;i<vOldIidCurr.size();i++)
+ {
+ PELIB_THUNK_DATA<bits> tdCurr;
+ dword uiVaoft = vOldIidCurr[i].impdesc.OriginalFirstThunk;
+
+ if (!uiVaoft)
+ {
+ continue;
+ }
+
+ ifFile.seekg(static_cast<unsigned int>(pehHeader.rvaToOffset(uiVaoft)), std::ios_base::beg);
+
+ do
+ {
+ if (uiFileSize < pehHeader.rvaToOffset(uiVaoft) + sizeof(tdCurr.itd.Ordinal))
+ {
+ return ERROR_INVALID_FILE;
+ }
+ uiVaoft += sizeof(tdCurr.itd.Ordinal);
+
+ ifFile.read(reinterpret_cast<char*>(&tdCurr.itd.Ordinal), sizeof(tdCurr.itd.Ordinal));
+ if (tdCurr.itd.Ordinal) vOldIidCurr[i].originalfirstthunk.push_back(tdCurr);
+ } while (tdCurr.itd.Ordinal);
+ }
+
+ // FirstThunk
+ for (unsigned int i=0;i<vOldIidCurr.size();i++)
+ {
+ dword uiVaoft = vOldIidCurr[i].impdesc.FirstThunk;
+ PELIB_THUNK_DATA<bits> tdCurr;
+
+ ifFile.seekg(static_cast<unsigned int>(pehHeader.rvaToOffset(uiVaoft)), std::ios_base::beg);
+
+ do
+ {
+ if (uiFileSize < pehHeader.rvaToOffset(uiVaoft) + sizeof(tdCurr.itd.Ordinal))
+ {
+ return ERROR_INVALID_FILE;
+ }
+
+ uiVaoft += sizeof(tdCurr.itd.Ordinal);
+
+ ifFile.read(reinterpret_cast<char*>(&tdCurr.itd.Ordinal), sizeof(tdCurr.itd.Ordinal));
+ if (tdCurr.itd.Ordinal) vOldIidCurr[i].firstthunk.push_back(tdCurr);
+ } while (tdCurr.itd.Ordinal);
+ }
+
+ // Names
+ for (unsigned int i=0;i<vOldIidCurr.size();i++)
+ {
+ if (vOldIidCurr[i].impdesc.OriginalFirstThunk)
+ {
+ for (unsigned int j=0;j<vOldIidCurr[i].originalfirstthunk.size();j++)
+ {
+ if (vOldIidCurr[i].originalfirstthunk[j].itd.Ordinal & PELIB_IMAGE_ORDINAL_FLAGS<bits>::IMAGE_ORDINAL_FLAG)
+ {
+ vOldIidCurr[i].originalfirstthunk[j].hint = 0;
+ continue;
+ }
+
+ ifFile.seekg(static_cast<unsigned int>(pehHeader.rvaToOffset(vOldIidCurr[i].originalfirstthunk[j].itd.Ordinal)), std::ios_base::beg);
+
+ ifFile.read(reinterpret_cast<char*>(&vOldIidCurr[i].originalfirstthunk[j].hint), sizeof(vOldIidCurr[i].originalfirstthunk[j].hint));
+
+ if (!ifFile)
+ return ERROR_INVALID_FILE;
+
+ std::string funcname = "";
+ do
+ {
+ ifFile.read(namebuffer, 1);
+ if (!ifFile || !namebuffer[0]) break; // reached end of file or 0-byte
+ funcname += namebuffer;
+ } while (true);
+
+ vOldIidCurr[i].originalfirstthunk[j].fname = funcname;
+ }
+ }
+ else
+ {
+ for (unsigned int j=0;j<vOldIidCurr[i].firstthunk.size();j++)
+ {
+ if (vOldIidCurr[i].firstthunk[j].itd.Ordinal & PELIB_IMAGE_ORDINAL_FLAGS<bits>::IMAGE_ORDINAL_FLAG)
+ {
+ continue;
+ }
+
+ ifFile.seekg(static_cast<unsigned int>(pehHeader.rvaToOffset(vOldIidCurr[i].firstthunk[j].itd.Ordinal)), std::ios_base::beg);
+
+ ifFile.read(reinterpret_cast<char*>(&vOldIidCurr[i].firstthunk[j].hint), sizeof(vOldIidCurr[i].firstthunk[j].hint));
+
+ if (!ifFile)
+ return ERROR_INVALID_FILE;
+
+ std::string funcname = "";
+ do
+ {
+ ifFile.read(namebuffer, 1);
+ if (!ifFile || !namebuffer[0]) break; // reached end of file or 0-byte
+ funcname += namebuffer;
+ } while (true);
+
+ vOldIidCurr[i].firstthunk[j].fname = funcname;
+ }
+ }
+ }
+ std::swap(vOldIidCurr, m_vOldiid);
+ return NO_ERROR;
+ }
+
+ /**
+ * Rebuilds the import directory.
+ * @param vBuffer Buffer the rebuilt import directory will be written to.
+ * @param dwRva The RVA of the ImportDirectory in the file.
+ * \todo uiSizeoffuncnames is not used.
+ **/
+ template<int bits>
+ void ImportDirectory<bits>::rebuild(std::vector<byte>& vBuffer, dword dwRva, bool fixEntries) const
+ {
+ unsigned int uiImprva = dwRva;
+ unsigned int uiSizeofdescriptors = (static_cast<unsigned int>(m_vNewiid.size() + m_vOldiid.size()) + 1) * PELIB_IMAGE_IMPORT_DESCRIPTOR::size();
+
+ unsigned int uiSizeofdllnames = 0, uiSizeoffuncnames = 0;
+ unsigned int uiSizeofoft = 0;
+
+ for (unsigned int i=0;i<m_vNewiid.size();i++)
+ {
+ uiSizeofdllnames += static_cast<unsigned int>(m_vNewiid[i].name.size()) + 1;
+ uiSizeofoft += (static_cast<unsigned int>(m_vNewiid[i].originalfirstthunk.size())+1) * PELIB_IMAGE_THUNK_DATA<bits>::size();
+
+ for(unsigned int j=0;j<m_vNewiid[i].originalfirstthunk.size();j++)
+ {
+ // +3 for hint (word) and 00-byte
+ uiSizeoffuncnames += (static_cast<unsigned int>(m_vNewiid[i].originalfirstthunk[j].fname.size()) + 3);
+ }
+ }
+
+// for (unsigned int i=0;i<m_vNewiid.size();i++)
+// {
+// uiSizeofoft += (static_cast<unsigned int>(m_vNewiid[i].originalfirstthunk.size())+1) * PELIB_IMAGE_THUNK_DATA<bits>::size();
+// }
+
+ OutputBuffer obBuffer(vBuffer);
+
+ // Rebuild IMAGE_IMPORT_DESCRIPTORS
+ for (unsigned int i=0;i<m_vOldiid.size();i++)
+ {
+ obBuffer << m_vOldiid[i].impdesc.OriginalFirstThunk;
+ obBuffer << m_vOldiid[i].impdesc.TimeDateStamp;
+ obBuffer << m_vOldiid[i].impdesc.ForwarderChain;
+ obBuffer << m_vOldiid[i].impdesc.Name;
+ obBuffer << m_vOldiid[i].impdesc.FirstThunk;
+ }
+
+ unsigned int dllsize = 0;
+
+ for (unsigned int i=0;i<m_vNewiid.size();i++)
+ {
+ dword dwPoft = uiSizeofdescriptors + uiImprva;
+
+ for (unsigned int j=1;j<=i;j++)
+ {
+ dwPoft += (static_cast<unsigned int>(m_vNewiid[j-1].originalfirstthunk.size()) + 1) * PELIB_IMAGE_THUNK_DATA<bits>::size();
+ }
+
+ obBuffer << (fixEntries ? dwPoft : m_vNewiid[i].impdesc.OriginalFirstThunk);
+ obBuffer << m_vNewiid[i].impdesc.TimeDateStamp;
+ obBuffer << m_vNewiid[i].impdesc.ForwarderChain;
+ dword dwPdll = uiSizeofdescriptors + uiSizeofoft + uiImprva + dllsize;
+ obBuffer << (fixEntries ? dwPdll : m_vNewiid[i].impdesc.Name);
+ obBuffer << (fixEntries ? dwPoft : m_vNewiid[i].impdesc.FirstThunk);
+
+ dllsize += static_cast<unsigned int>(m_vNewiid[i].name.size()) + 1;
+ }
+
+ obBuffer << (dword)0;
+ obBuffer << (dword)0;
+ obBuffer << (dword)0;
+ obBuffer << (dword)0;
+ obBuffer << (dword)0;
+
+ unsigned int uiPfunc = uiSizeofdescriptors + uiSizeofoft + uiSizeofdllnames + uiImprva;
+
+ // Rebuild original first thunk
+ for (unsigned int i=0;i<m_vNewiid.size();i++)
+ {
+ for (unsigned int j=0;j<m_vNewiid[i].originalfirstthunk.size();j++)
+ {
+ if (m_vNewiid[i].originalfirstthunk[j].itd.Ordinal & PELIB_IMAGE_ORDINAL_FLAGS<bits>::IMAGE_ORDINAL_FLAG
+ || fixEntries == false)
+ {
+ obBuffer << m_vNewiid[i].originalfirstthunk[j].itd.Ordinal;
+ }
+ else
+ {
+ obBuffer << uiPfunc;
+ }
+ uiPfunc += static_cast<unsigned int>(m_vNewiid[i].originalfirstthunk[j].fname.size()) + 3;
+ }
+ obBuffer << (dword)0;
+ }
+
+ // Write dllnames into import directory
+ for (unsigned int i=0;i<m_vNewiid.size();i++)
+ {
+ obBuffer.add(m_vNewiid[i].name.c_str(), static_cast<unsigned int>(m_vNewiid[i].name.size())+1);
+ }
+
+ // Write function names into directory
+ for (unsigned int i=0;i<m_vNewiid.size();i++)
+ {
+ for (unsigned int j=0;j<m_vNewiid[i].originalfirstthunk.size();j++)
+ {
+ obBuffer << m_vNewiid[i].originalfirstthunk[j].hint;
+ obBuffer.add(m_vNewiid[i].originalfirstthunk[j].fname.c_str(), static_cast<unsigned int>(m_vNewiid[i].originalfirstthunk[j].fname.size()) + 1);
+ }
+ }
+ }
+
+ /**
+ * Removes a specific file and all functions of it from the import directory.
+ * @param strFilename Name of the file which will be removed.
+ **/
+ template<int bits>
+ int ImportDirectory<bits>::removeFile(const std::string& strFilename)
+ {
+ unsigned int oldSize = static_cast<unsigned int>(m_vNewiid.size());
+
+ m_vNewiid.erase(std::remove_if(m_vNewiid.begin(), m_vNewiid.end(), std::bind2nd(std::mem_fun_ref(&PELIB_IMAGE_IMPORT_DIRECTORY<bits>::operator==), strFilename)), m_vNewiid.end());
+
+ return oldSize == m_vNewiid.size() ? 1 : 0;
+ }
+
+ /**
+ * Removes a specific function from the import directory.
+ * @param strFilename Name of the file which exports the function.
+ * @param strFuncname Name of the imported function.
+ **/
+ template<int bits>
+ int ImportDirectory<bits>::removeFunction(const std::string& strFilename, const std::string& strFuncname)
+ {
+ ImpDirFileIterator viPos = m_vNewiid.begin();
+
+ int notFound = 1;
+
+ while (viPos != m_vNewiid.end())
+ {
+ if (isEqualNc(viPos->name, strFilename))
+ {
+ unsigned int oldSize = static_cast<unsigned int>(viPos->originalfirstthunk.size());
+ viPos->originalfirstthunk.erase(std::remove_if(viPos->originalfirstthunk.begin(), viPos->originalfirstthunk.end(), std::bind2nd(std::mem_fun_ref(&PELIB_THUNK_DATA<bits>::equalFunctionName), strFuncname)), viPos->originalfirstthunk.end());
+ //viPos->originalfirstthunk.erase(std::remove_if(viPos->originalfirstthunk.begin(), viPos->originalfirstthunk.end(), std::bind2nd(CompPolicy<PELIB_THUNK_DATA, std::string>(), strFuncname)));
+ if (viPos->originalfirstthunk.size() != oldSize) notFound = 0;
+ }
+ ++viPos;
+ }
+
+ return notFound;
+ }
+
+ /**
+ * Removes a specific function from the import directory.
+ * @param strFilename Name of the file which exports the function.
+ * @param wHint The hint of the function.
+ **/
+ template<int bits>
+ int ImportDirectory<bits>::removeFunction(const std::string& strFilename, word wHint)
+ {
+ ImpDirFileIterator viPos = m_vNewiid.begin();
+ int notFound = 1;
+
+ while (viPos != m_vNewiid.end())
+ {
+ if (isEqualNc(viPos->name, strFilename))
+ {
+ unsigned int oldSize = static_cast<unsigned int>(viPos->originalfirstthunk.size());
+ viPos->originalfirstthunk.erase(std::remove_if(viPos->originalfirstthunk.begin(), viPos->originalfirstthunk.end(), std::bind2nd(std::mem_fun_ref(&PELIB_THUNK_DATA<bits>::equalHint), wHint)), viPos->originalfirstthunk.end());
+ unsigned int newPos = static_cast<unsigned int>(viPos->originalfirstthunk.size());
+ if (viPos->originalfirstthunk.size() != oldSize) notFound = 0;
+ }
+ ++viPos;
+ }
+
+ return notFound;
+ }
+
+ /**
+ * Writes the current import directory to a file.
+ * @param strFilename Name of the file.
+ * @param uiOffset File Offset of the new import directory.
+ * @param uiRva RVA which belongs to that file offset.
+ **/
+ template<int bits>
+ int ImportDirectory<bits>::write(const std::string& strFilename, unsigned int uiOffset, unsigned int uiRva)
+ {
+ std::fstream ofFile(strFilename.c_str(), std::ios_base::in);
+
+ if (!ofFile)
+ {
+ ofFile.clear();
+ ofFile.open(strFilename.c_str(), std::ios_base::out | std::ios_base::binary);
+ }
+ else
+ {
+ ofFile.close();
+ ofFile.open(strFilename.c_str(), std::ios_base::in | std::ios_base::out | std::ios_base::binary);
+ }
+
+ if (!ofFile)
+ {
+ return ERROR_OPENING_FILE;
+ }
+
+ ofFile.seekp(uiOffset, std::ios_base::beg);
+
+ std::vector<byte> vBuffer;
+
+ rebuild(vBuffer, uiRva);
+
+ ofFile.write(reinterpret_cast<const char*>(&vBuffer[0]), vBuffer.size());
+ ofFile.close();
+
+ std::copy(m_vNewiid.begin(), m_vNewiid.end(), std::back_inserter(m_vOldiid));
+ m_vNewiid.clear();
+
+ return NO_ERROR;
+ }
+
+ /**
+ * Returns the size of the import directory.
+ * @return Size of the import directory.
+ **/
+ template<int bits>
+ unsigned int ImportDirectory<bits>::size() const
+ {
+ // Only the descriptors of m_vOldiid must be rebuilt, not the data they point to.
+ return std::accumulate(m_vNewiid.begin(), m_vNewiid.end(), 0, accumulate<PELIB_IMAGE_IMPORT_DIRECTORY<bits> >)
+ + (m_vOldiid.size() + 1) * PELIB_IMAGE_IMPORT_DESCRIPTOR::size();
+ }
+
+ /**
+ * @param strFilename Name of the imported file.
+ * @param cdDir Flag to decide if the OLDDIR or new import directory is used.
+ * @return FirstThunk value of an imported file.
+ **/
+ template<int bits>
+ dword ImportDirectory<bits>::getFirstThunk(const std::string& strFilename, currdir cdDir) const
+ {
+ if (cdDir == OLDDIR)
+ {
+ return m_vOldiid[getFileIndex(strFilename, cdDir)].impdesc.FirstThunk;
+ }
+ else
+ {
+ return m_vNewiid[getFileIndex(strFilename, cdDir)].impdesc.FirstThunk;
+ }
+ }
+
+ /**
+ * @param strFilename Name of the imported file.
+ * @param cdDir Flag to decide if the OLDDIR or new import directory is used.
+ * @return OriginalFirstThunk value of an imported file.
+ **/
+ template<int bits>
+ dword ImportDirectory<bits>::getOriginalFirstThunk(const std::string& strFilename, currdir cdDir) const
+ {
+ if (cdDir == OLDDIR)
+ {
+ return m_vOldiid[getFileIndex(strFilename, cdDir)].impdesc.OriginalFirstThunk;
+ }
+ else
+ {
+ return m_vNewiid[getFileIndex(strFilename, cdDir)].impdesc.OriginalFirstThunk;
+ }
+ }
+
+ /**
+ * @param strFilename Name of the imported file.
+ * @param cdDir Flag to decide if the OLDDIR or new import directory is used.
+ * @return ForwarderChain value of an imported file.
+ **/
+ template<int bits>
+ dword ImportDirectory<bits>::getForwarderChain(const std::string& strFilename, currdir cdDir) const
+ {
+ if (cdDir == OLDDIR)
+ {
+ return m_vOldiid[getFileIndex(strFilename, cdDir)].impdesc.ForwarderChain;
+ }
+ else
+ {
+ return m_vNewiid[getFileIndex(strFilename, cdDir)].impdesc.ForwarderChain;
+ }
+ }
+
+ /**
+ * @param strFilename Name of the imported file.
+ * @param cdDir Flag to decide if the OLDDIR or new import directory is used.
+ * @return TimeDateStamp value of an imported file.
+ **/
+ template<int bits>
+ dword ImportDirectory<bits>::getTimeDateStamp(const std::string& strFilename, currdir cdDir) const
+ {
+ if (cdDir == OLDDIR)
+ {
+ return m_vOldiid[getFileIndex(strFilename, cdDir)].impdesc.TimeDateStamp;
+ }
+ else
+ {
+ return m_vNewiid[getFileIndex(strFilename, cdDir)].impdesc.TimeDateStamp;
+ }
+ }
+
+ template<int bits>
+ dword ImportDirectory<bits>::getRvaOfName(const std::string& strFilename, currdir cdDir) const
+ {
+ if (cdDir == OLDDIR)
+ {
+ return m_vOldiid[getFileIndex(strFilename, cdDir)].impdesc.Name;
+ }
+ else
+ {
+ return m_vNewiid[getFileIndex(strFilename, cdDir)].impdesc.Name;
+ }
+ }
+
+ /**
+ * @param strFilename Name of the imported file.
+ * @param cdDir Flag to decide if the OLDDIR or new import directory is used.
+ * @return FirstThunk value of an imported file.
+ **/
+ template<int bits>
+ dword ImportDirectory<bits>::getFirstThunk(dword dwFilenr, currdir cdDir) const
+ {
+ if (cdDir == OLDDIR)
+ {
+ return m_vOldiid[dwFilenr].impdesc.FirstThunk;
+ }
+ else
+ {
+ return m_vNewiid[dwFilenr].impdesc.FirstThunk;
+ }
+ }
+
+ template<int bits>
+ void ImportDirectory<bits>::setFirstThunk(dword dwFilenr, currdir cdDir, dword value)
+ {
+ if (cdDir == OLDDIR)
+ {
+ m_vOldiid[dwFilenr].impdesc.FirstThunk = value;
+ }
+ else
+ {
+ m_vNewiid[dwFilenr].impdesc.FirstThunk = value;
+ }
+ }
+
+ /**
+ * @param strFilename Name of the imported file.
+ * @param cdDir Flag to decide if the OLDDIR or new import directory is used.
+ * @return OriginalFirstThunk value of an imported file.
+ **/
+ template<int bits>
+ dword ImportDirectory<bits>::getOriginalFirstThunk(dword dwFilenr, currdir cdDir) const
+ {
+ if (cdDir == OLDDIR)
+ {
+ return m_vOldiid[dwFilenr].impdesc.OriginalFirstThunk;
+ }
+ else
+ {
+ return m_vNewiid[dwFilenr].impdesc.OriginalFirstThunk;
+ }
+ }
+
+ template<int bits>
+ void ImportDirectory<bits>::setOriginalFirstThunk(dword dwFilenr, currdir cdDir, dword value)
+ {
+ if (cdDir == OLDDIR)
+ {
+ m_vOldiid[dwFilenr].impdesc.OriginalFirstThunk = value;
+ }
+ else
+ {
+ m_vNewiid[dwFilenr].impdesc.OriginalFirstThunk = value;
+ }
+ }
+
+ /**
+ * @param strFilename Name of the imported file.
+ * @param cdDir Flag to decide if the OLDDIR or new import directory is used.
+ * @return ForwarderChain value of an imported file.
+ **/
+ template<int bits>
+ dword ImportDirectory<bits>::getForwarderChain(dword dwFilenr, currdir cdDir) const
+ {
+ if (cdDir == OLDDIR)
+ {
+ return m_vOldiid[dwFilenr].impdesc.ForwarderChain;
+ }
+ else
+ {
+ return m_vNewiid[dwFilenr].impdesc.ForwarderChain;
+ }
+ }
+
+ template<int bits>
+ void ImportDirectory<bits>::setForwarderChain(dword dwFilenr, currdir cdDir, dword value)
+ {
+ if (cdDir == OLDDIR)
+ {
+ m_vOldiid[dwFilenr].impdesc.ForwarderChain = value;
+ }
+ else
+ {
+ m_vNewiid[dwFilenr].impdesc.ForwarderChain = value;
+ }
+ }
+
+ /**
+ * @param strFilename Name of the imported file.
+ * @param cdDir Flag to decide if the OLDDIR or new import directory is used.
+ * @return TimeDateStamp value of an imported file.
+ **/
+ template<int bits>
+ dword ImportDirectory<bits>::getTimeDateStamp(dword dwFilenr, currdir cdDir) const
+ {
+ if (cdDir == OLDDIR)
+ {
+ return m_vOldiid[dwFilenr].impdesc.TimeDateStamp;
+ }
+ else
+ {
+ return m_vNewiid[dwFilenr].impdesc.TimeDateStamp;
+ }
+ }
+
+ template<int bits>
+ void ImportDirectory<bits>::setTimeDateStamp(dword dwFilenr, currdir cdDir, dword value)
+ {
+ if (cdDir == OLDDIR)
+ {
+ m_vOldiid[dwFilenr].impdesc.TimeDateStamp = value;
+ }
+ else
+ {
+ m_vNewiid[dwFilenr].impdesc.TimeDateStamp = value;
+ }
+ }
+
+ template<int bits>
+ dword ImportDirectory<bits>::getRvaOfName(dword dwFilenr, currdir cdDir) const
+ {
+ if (cdDir == OLDDIR)
+ {
+ return m_vOldiid[dwFilenr].impdesc.Name;
+ }
+ else
+ {
+ return m_vNewiid[dwFilenr].impdesc.Name;
+ }
+ }
+
+ template<int bits>
+ void ImportDirectory<bits>::setRvaOfName(dword dwFilenr, currdir cdDir, dword value)
+ {
+ if (cdDir == OLDDIR)
+ {
+ m_vOldiid[dwFilenr].impdesc.Name = value;
+ }
+ else
+ {
+ m_vNewiid[dwFilenr].impdesc.Name = value;
+ }
+ }
+
+ /**
+ * @param dwFilenr ID of the imported file.
+ * @param dwFuncnr ID of the imported function.
+ * @param cdDir Flag to decide if the OLDDIR or new import directory is used.
+ * @return FirstThunk value of an imported function.
+ **/
+ template<int bits>
+ dword ImportDirectory<bits>::getFirstThunk(dword dwFilenr, dword dwFuncnr, currdir cdDir) const
+ {
+ if (cdDir == OLDDIR) return m_vOldiid[dwFilenr].firstthunk[dwFuncnr].itd.Ordinal;
+ else return m_vNewiid[dwFilenr].firstthunk[dwFuncnr].itd.Ordinal;
+ }
+
+ template<int bits>
+ void ImportDirectory<bits>::setFirstThunk(dword dwFilenr, dword dwFuncnr, currdir cdDir, dword value)
+ {
+ if (cdDir == OLDDIR) m_vOldiid[dwFilenr].firstthunk[dwFuncnr].itd.Ordinal = value;
+ else m_vNewiid[dwFilenr].firstthunk[dwFuncnr].itd.Ordinal = value;
+ }
+
+ /**
+ * @param dwFilenr ID of the imported file.
+ * @param dwFuncnr ID of the imported function.
+ * @param cdDir Flag to decide if the OLDDIR or new import directory is used.
+ * @return OriginalFirstThunk value of an imported function.
+ **/
+ template<int bits>
+ dword ImportDirectory<bits>::getOriginalFirstThunk(dword dwFilenr, dword dwFuncnr, currdir cdDir) const
+ {
+ if (cdDir == OLDDIR) return m_vOldiid[dwFilenr].originalfirstthunk[dwFuncnr].itd.Ordinal;
+ else return m_vNewiid[dwFilenr].originalfirstthunk[dwFuncnr].itd.Ordinal;
+ }
+
+ template<int bits>
+ void ImportDirectory<bits>::setOriginalFirstThunk(dword dwFilenr, dword dwFuncnr, currdir cdDir, dword value)
+ {
+ if (cdDir == OLDDIR) m_vOldiid[dwFilenr].originalfirstthunk[dwFuncnr].itd.Ordinal = value;
+ else m_vNewiid[dwFilenr].originalfirstthunk[dwFuncnr].itd.Ordinal = value;
+ }
+
+ typedef ImportDirectory<32> ImportDirectory32;
+ typedef ImportDirectory<64> ImportDirectory64;
+}
+
+#endif
diff --git a/utils/zenutils/libraries/pelib-0.9/pelib/MzHeader.cpp b/utils/zenutils/libraries/pelib-0.9/pelib/MzHeader.cpp
index 39fe54d80d..3a2119b7ac 100755..100644
--- a/utils/zenutils/libraries/pelib-0.9/pelib/MzHeader.cpp
+++ b/utils/zenutils/libraries/pelib-0.9/pelib/MzHeader.cpp
@@ -1,584 +1,584 @@
-/*
-* MzHeader.cpp - Part of the PeLib library.
-*
-* Copyright (c) 2004 - 2005 Sebastian Porst (webmaster@the-interweb.com)
-* All rights reserved.
-*
-* This software is licensed under the zlib/libpng License.
-* For more details see http://www.opensource.org/licenses/zlib-license.php
-* or the license information file (license.htm) in the root directory
-* of PeLib.
-*/
-
-#include "MzHeader.h"
-#include <iostream>
-
-namespace PeLib
-{
- /**
- * Reads data from an InputBuffer into the struct that represents the MZ header.
- * It's required that the size of the input buffer is at least as big as the
- * size of a MZ header. Otherwise we get undefined behaviour.
- * @param ibBuffer InputBuffer that holds the data.
- * @return A non-zero value is returned if a problem occured.
- **/
- void MzHeader::read(InputBuffer& ibBuffer)
- {
- ibBuffer >> m_idhHeader.e_magic;
- ibBuffer >> m_idhHeader.e_cblp;
- ibBuffer >> m_idhHeader.e_cp;
- ibBuffer >> m_idhHeader.e_crlc;
- ibBuffer >> m_idhHeader.e_cparhdr;
- ibBuffer >> m_idhHeader.e_minalloc;
- ibBuffer >> m_idhHeader.e_maxalloc;
- ibBuffer >> m_idhHeader.e_ss;
- ibBuffer >> m_idhHeader.e_sp;
- ibBuffer >> m_idhHeader.e_csum;
- ibBuffer >> m_idhHeader.e_ip;
- ibBuffer >> m_idhHeader.e_cs;
- ibBuffer >> m_idhHeader.e_lfarlc;
- ibBuffer >> m_idhHeader.e_ovno;
-
- for (unsigned int i=0;i<sizeof(m_idhHeader.e_res)/sizeof(m_idhHeader.e_res[0]);i++)
- {
- ibBuffer >> m_idhHeader.e_res[i];
- }
-
- ibBuffer >> m_idhHeader.e_oemid;
- ibBuffer >> m_idhHeader.e_oeminfo;
-
- for (unsigned int i=0;i<sizeof(m_idhHeader.e_res2)/sizeof(m_idhHeader.e_res2[0]);i++)
- {
- ibBuffer >> m_idhHeader.e_res2[i];
- }
-
- ibBuffer >> m_idhHeader.e_lfanew;
- }
-
- /**
- * Tests if the currently loaded MZ header is a valid MZ header.
- * Note that this function does not check if the address to the PE header is valid as this is not possible.
- * Actually, the only thing this function checks is if the e_magic value is set to 0x5A4D (IMAGE_DOS_SIGNATURE).
- * Everything else is not relevant for Windows 2000 and that's the system PeLib is focusing on for now.
- * @return A boolean value that indicates if the MZ header is correct or not.
- **/
- bool MzHeader::isValid() const
- {
- // The only thing that matters on Windows 2K is the e_magic value. The entire rest is for DOS compatibility.
- return isValid(e_magic);
- }
-
- bool MzHeader::isValid(Field f) const
- {
- if (f == e_magic)
- {
- return m_idhHeader.e_magic == PELIB_IMAGE_DOS_SIGNATURE;
- }
- else
- {
- return true;
- }
- }
-
- /**
- * Corrects all erroneous values of the current MZ header. Note that this function does not correct the
- * pointer to the PE header.
- * Actually, the only thing this function corrects is the e_magic value.
- * Everything else is not relevant for Windows 2000 and that's the system PeLib is focusing on for now.
- **/
- void MzHeader::makeValid()
- {
- // The only thing that matters on Windows is the e_magic value. The entire rest is for DOS compatibility.
- setMagicNumber(PELIB_IMAGE_DOS_SIGNATURE);
- }
-
- void MzHeader::makeValid(Field f)
- {
- if (f == e_magic)
- {
- setMagicNumber(PELIB_IMAGE_DOS_SIGNATURE);
- }
- }
-
- /**
- * Reads the MZ header from a file. Note that this function does not verify if a file is actually a MZ file.
- * For this purpose see #PeFile::MzHeader::isValid. The reason for this is simple: Otherwise it might not
- * be possible to load damaged PE files to repair them.
- * @param strFilename Name of the file which will be read.
- * @return A non-zero value is returned if a problem occured.
- **/
- int MzHeader::read(const std::string& strFilename)
- {
- std::ifstream ifFile(strFilename.c_str(), std::ios::binary);
-
- if (!ifFile)
- {
- return ERROR_OPENING_FILE;
- }
-
- if (fileSize(ifFile) < PELIB_IMAGE_DOS_HEADER::size())
- {
- return ERROR_INVALID_FILE;
- }
-
- ifFile.seekg(0, std::ios::beg);
-
- originalOffset = 0;
-
- std::vector<byte> vBuffer(PELIB_IMAGE_DOS_HEADER::size());
- ifFile.read(reinterpret_cast<char*>(&vBuffer[0]), static_cast<unsigned int>(vBuffer.size()));
- ifFile.close();
-
- InputBuffer ibBuffer(vBuffer);
- read(ibBuffer);
- return NO_ERROR;
- }
-
- /**
- * Reads the MZ header from memory. A pointer to a location in memory is passed and the data
- * at this location is treated like a MZ header structure. The MZ header does not need to be valid.
- * @param pcBuffer Pointer to a MZ header.
- * @param uiSize Length of the buffer.
- * @return A non-zero value is returned if a problem occured.
- **/
- int MzHeader::read(unsigned char* pcBuffer, unsigned int uiSize, unsigned int originalOffs)
- {
- if (uiSize < PELIB_IMAGE_DOS_HEADER::size())
- {
- return ERROR_INVALID_FILE;
- }
-
- std::vector<byte> vBuffer(pcBuffer, pcBuffer + uiSize);
- for (int i=0;i<0x40;i++) std::cout << std::hex << (int)vBuffer[i] << " ";
-
- originalOffset = originalOffs;
-
- InputBuffer ibBuffer(vBuffer);
- read(ibBuffer);
- return NO_ERROR;
- }
-
- /**
- * Rebuilds the MZ header so that it can be written to a file. It's not guaranteed that the
- * MZ header will be valid. If you want to make sure that the MZ header will be valid you
- * must call #PeLib::MzHeader::makeValid first.
- * @param vBuffer Buffer where the rebuilt MZ header will be stored.
- **/
- void MzHeader::rebuild(std::vector<byte>& vBuffer) const
- {
- OutputBuffer obBuffer(vBuffer);
-
- obBuffer << m_idhHeader.e_magic;
- obBuffer << m_idhHeader.e_cblp;
- obBuffer << m_idhHeader.e_cp;
- obBuffer << m_idhHeader.e_crlc;
- obBuffer << m_idhHeader.e_cparhdr;
- obBuffer << m_idhHeader.e_minalloc;
- obBuffer << m_idhHeader.e_maxalloc;
- obBuffer << m_idhHeader.e_ss;
- obBuffer << m_idhHeader.e_sp;
- obBuffer << m_idhHeader.e_csum;
- obBuffer << m_idhHeader.e_ip;
- obBuffer << m_idhHeader.e_cs;
- obBuffer << m_idhHeader.e_lfarlc;
- obBuffer << m_idhHeader.e_ovno;
-
- for (unsigned int i=0;i<sizeof(m_idhHeader.e_res)/sizeof(m_idhHeader.e_res[0]);i++)
- {
- obBuffer << m_idhHeader.e_res[i];
- }
-
- obBuffer << m_idhHeader.e_oemid;
- obBuffer << m_idhHeader.e_oeminfo;
-
- for (unsigned int i=0;i<sizeof(m_idhHeader.e_res2)/sizeof(m_idhHeader.e_res2[0]);i++)
- {
- obBuffer << m_idhHeader.e_res2[i];
- }
-
- obBuffer << m_idhHeader.e_lfanew;
- }
-
- /**
- * Returns the size of the MZ header. This size is actually always sizeof(IMAGE_DOS_HEADER) (== 0x40)
- * because the MZ header is a header of constant size if you disregard the dos stub. If you want to know the
- * size of the MZ header + the size of the dos stub check #PeLib::MzHeader::getAddressOfPeHeader.
- * @return Size of the MZ header.
- **/
- unsigned int MzHeader::size() const
- {
- return sizeof(m_idhHeader);
- }
-
- /**
- * Writes the current MZ header to a file. The file does not have to exist. If it doesn't exist
- * it will be created.
- * @param strFilename Name of the file the header will be written to.
- * @param dwOffset Offset the header will be written to (defaults to 0).
- * @return A non-zero value is returned if a problem occured.
- **/
- int MzHeader::write(const std::string& strFilename, dword dwOffset = 0) const
- {
- std::fstream ofFile(strFilename.c_str(), std::ios_base::in);
-
- if (!ofFile)
- {
- ofFile.clear();
- ofFile.open(strFilename.c_str(), std::ios_base::out | std::ios_base::binary);
- }
- else
- {
- ofFile.close();
- ofFile.open(strFilename.c_str(), std::ios_base::in | std::ios_base::out | std::ios_base::binary);
- }
-
- if (!ofFile)
- {
- return ERROR_OPENING_FILE;
- }
-
- ofFile.seekp(dwOffset, std::ios::beg);
-
- std::vector<unsigned char> vBuffer;
-
- rebuild(vBuffer);
-
- ofFile.write(reinterpret_cast<const char*>(&vBuffer[0]), static_cast<unsigned int>(vBuffer.size()));
-
- ofFile.close();
-
- return NO_ERROR;
- }
-
- /**
- * Returns the MZ header's e_magic value.
- **/
- word MzHeader::getMagicNumber() const
- {
- return m_idhHeader.e_magic;
- }
-
- /**
- * Returns the MZ header's e_cblp value.
- **/
- word MzHeader::getBytesOnLastPage() const
- {
- return m_idhHeader.e_cblp;
- }
-
- /**
- * Returns the MZ header's e_cp value.
- **/
- word MzHeader::getPagesInFile() const
- {
- return m_idhHeader.e_cp;
- }
-
- /**
- * Returns the MZ header's e_crlc value.
- **/
- word MzHeader::getRelocations() const
- {
- return m_idhHeader.e_crlc;
- }
-
- /**
- * Returns the MZ header's e_cparhdr value.
- **/
- word MzHeader::getSizeOfHeader() const
- {
- return m_idhHeader.e_cparhdr;
- }
-
- /**
- * Returns the MZ header's e_minalloc value.
- **/
- word MzHeader::getMinExtraParagraphs() const
- {
- return m_idhHeader.e_minalloc;
- }
-
- /**
- * Returns the MZ header's e_maxalloc value.
- **/
- word MzHeader::getMaxExtraParagraphs() const
- {
- return m_idhHeader.e_maxalloc;
- }
-
- /**
- * Returns the MZ header's e_ss value.
- **/
- word MzHeader::getSsValue() const
- {
- return m_idhHeader.e_ss;
- }
-
- /**
- * Returns the MZ header's e_sp value.
- **/
- word MzHeader::getSpValue() const
- {
- return m_idhHeader.e_sp;
- }
-
- /**
- * Returns the MZ header's e_csum value.
- **/
- word MzHeader::getChecksum() const
- {
- return m_idhHeader.e_csum;
- }
-
- /**
- * Returns the MZ header's e_ip value.
- **/
- word MzHeader::getIpValue() const
- {
- return m_idhHeader.e_ip;
- }
-
- /**
- * Returns the MZ header's e_cs value.
- **/
- word MzHeader::getCsValue() const
- {
- return m_idhHeader.e_cs;
- }
-
- /**
- * Returns the MZ header's e_lfarlc value.
- **/
- word MzHeader::getAddrOfRelocationTable() const
- {
- return m_idhHeader.e_lfarlc;
- }
-
- /**
- * Returns the MZ header's e_ovno value.
- **/
- word MzHeader::getOverlayNumber() const
- {
- return m_idhHeader.e_ovno;
- }
-
- /**
- * Returns the MZ header's e_oemid value.
- **/
- word MzHeader::getOemIdentifier() const
- {
- return m_idhHeader.e_oemid;
- }
-
- /**
- * Returns the MZ header's e_oeminfo value.
- **/
- word MzHeader::getOemInformation() const
- {
- return m_idhHeader.e_oeminfo;
- }
-
- /**
- * Returns the MZ header's e_lfanew value.
- **/
- dword MzHeader::getAddressOfPeHeader() const
- {
- return m_idhHeader.e_lfanew;
- }
-
- /**
- * Returns the MZ header's e_res[uiNr] value. If the parameter uiNr is out of range
- * you will get undefined behaviour.
- * @param uiNr The index of the word in the e_res array (valid range: 0-3)
- **/
- word MzHeader::getReservedWords1(unsigned int uiNr) const
- {
- return m_idhHeader.e_res[uiNr];
- }
-
- /**
- * Returns the MZ header's e_res2[uiNr] value. If the parameter uiNr is out of range
- * you will get undefined behaviour.
- * @param uiNr The index of the word in the e_res array (valid range: 0-9)
- **/
- word MzHeader::getReservedWords2(unsigned int uiNr) const
- {
- return m_idhHeader.e_res2[uiNr];
- }
-
- /**
- * Sets the MZ header's e_magic value.
- * @param wValue The new value of e_magic.
- **/
- void MzHeader::setMagicNumber(word wValue)
- {
- m_idhHeader.e_magic = wValue;
- }
-
- /**
- * Sets the MZ header's e_cblp value.
- * @param wValue The new value of e_cblp.
- **/
- void MzHeader::setBytesOnLastPage(word wValue)
- {
- m_idhHeader.e_cblp = wValue;
- }
-
- /**
- * Sets the MZ header's e_cp value.
- * @param wValue The new value of e_cp.
- **/
- void MzHeader::setPagesInFile(word wValue)
- {
- m_idhHeader.e_cp = wValue;
- }
-
- /**
- * Sets the MZ header's e_crlc value.
- * @param wValue The new value of e_crlc.
- **/
- void MzHeader::setRelocations(word wValue)
- {
- m_idhHeader.e_crlc = wValue;
- }
-
- /**
- * Sets the MZ header's e_cparhdr value.
- * @param wValue The new value of e_cparhdr.
- **/
- void MzHeader::setSizeOfHeader(word wValue)
- {
- m_idhHeader.e_cparhdr = wValue;
- }
-
- /**
- * Sets the MZ header's e_minalloc value.
- * @param wValue The new value of e_minalloc.
- **/
- void MzHeader::setMinExtraParagraphs(word wValue)
- {
- m_idhHeader.e_minalloc = wValue;
- }
-
- /**
- * Sets the MZ header's e_maxalloc value.
- * @param wValue The new value of e_maxalloc.
- **/
- void MzHeader::setMaxExtraParagraphs(word wValue)
- {
- m_idhHeader.e_maxalloc = wValue;
- }
-
- /**
- * Sets the MZ header's e_ss value.
- * @param wValue The new value of e_ss.
- **/
- void MzHeader::setSsValue(word wValue)
- {
- m_idhHeader.e_ss = wValue;
- }
-
- /**
- * Sets the MZ header's e_sp value.
- * @param wValue The new value of e_sp.
- **/
- void MzHeader::setSpValue(word wValue)
- {
- m_idhHeader.e_sp = wValue;
- }
-
- /**
- * Sets the MZ header's e_csum value.
- * @param wValue The new value of e_csum.
- **/
- void MzHeader::setChecksum(word wValue)
- {
- m_idhHeader.e_csum = wValue;
- }
-
- /**
- * Sets the MZ header's e_ip value.
- * @param wValue The new value of e_ip.
- **/
- void MzHeader::setIpValue(word wValue)
- {
- m_idhHeader.e_ip = wValue;
- }
-
- /**
- * Sets the MZ header's e_cs value.
- * @param wValue The new value of e_cs.
- **/
- void MzHeader::setCsValue(word wValue)
- {
- m_idhHeader.e_cs = wValue;
- }
-
- /**
- * Sets the MZ header's e_lfarlc value.
- * @param wValue The new value of e_lfarlc.
- **/
- void MzHeader::setAddrOfRelocationTable(word wValue)
- {
- m_idhHeader.e_lfarlc = wValue;
- }
-
- /**
- * Sets the MZ header's e_ovno value.
- * @param wValue The new value of e_ovno.
- **/
- void MzHeader::setOverlayNumber(word wValue)
- {
- m_idhHeader.e_ovno = wValue;
- }
-
- /**
- * Sets the MZ header's e_oemid value.
- * @param wValue The new value of e_oemid.
- **/
- void MzHeader::setOemIdentifier(word wValue)
- {
- m_idhHeader.e_oemid = wValue;
- }
-
- /**
- * Sets the MZ header's e_oeminfo value.
- * @param wValue The new value of e_oeminfo.
- **/
- void MzHeader::setOemInformation(word wValue)
- {
- m_idhHeader.e_oeminfo = wValue;
- }
-
- /**
- * Sets the MZ header's e_lfanew value.
- * @param lValue The new value of e_lfanew.
- **/
- void MzHeader::setAddressOfPeHeader(dword lValue)
- {
- m_idhHeader.e_lfanew = lValue;
- }
-
- /**
- * Sets the MZ header's e_res[uiNr] value. If the parameter uiNr is out of range
- * you will get undefined behaviour.
- * @param uiNr The index of the word in the e_res array (valid range: 0-3)
- * @param wValue The new value of e_res[nr].
- **/
- void MzHeader::setReservedWords1(unsigned int uiNr, word wValue)
- {
- m_idhHeader.e_res[uiNr] = wValue;
- }
-
- /**
- * Sets the MZ header's e_res2[uiNr] value. If the parameter uiNr is out of range
- * you will get undefined behaviour.
- * @param uiNr The index of the word in the e_res2 array (valid range: 0-9)
- * @param wValue The new value of e_res[nr].
- **/
- void MzHeader::setReservedWords2(unsigned int uiNr, word wValue)
- {
- m_idhHeader.e_res2[uiNr] = wValue;
- }
-
-}
+/*
+* MzHeader.cpp - Part of the PeLib library.
+*
+* Copyright (c) 2004 - 2005 Sebastian Porst (webmaster@the-interweb.com)
+* All rights reserved.
+*
+* This software is licensed under the zlib/libpng License.
+* For more details see http://www.opensource.org/licenses/zlib-license.php
+* or the license information file (license.htm) in the root directory
+* of PeLib.
+*/
+
+#include "MzHeader.h"
+#include <iostream>
+
+namespace PeLib
+{
+ /**
+ * Reads data from an InputBuffer into the struct that represents the MZ header.
+ * It's required that the size of the input buffer is at least as big as the
+ * size of a MZ header. Otherwise we get undefined behaviour.
+ * @param ibBuffer InputBuffer that holds the data.
+ * @return A non-zero value is returned if a problem occured.
+ **/
+ void MzHeader::read(InputBuffer& ibBuffer)
+ {
+ ibBuffer >> m_idhHeader.e_magic;
+ ibBuffer >> m_idhHeader.e_cblp;
+ ibBuffer >> m_idhHeader.e_cp;
+ ibBuffer >> m_idhHeader.e_crlc;
+ ibBuffer >> m_idhHeader.e_cparhdr;
+ ibBuffer >> m_idhHeader.e_minalloc;
+ ibBuffer >> m_idhHeader.e_maxalloc;
+ ibBuffer >> m_idhHeader.e_ss;
+ ibBuffer >> m_idhHeader.e_sp;
+ ibBuffer >> m_idhHeader.e_csum;
+ ibBuffer >> m_idhHeader.e_ip;
+ ibBuffer >> m_idhHeader.e_cs;
+ ibBuffer >> m_idhHeader.e_lfarlc;
+ ibBuffer >> m_idhHeader.e_ovno;
+
+ for (unsigned int i=0;i<sizeof(m_idhHeader.e_res)/sizeof(m_idhHeader.e_res[0]);i++)
+ {
+ ibBuffer >> m_idhHeader.e_res[i];
+ }
+
+ ibBuffer >> m_idhHeader.e_oemid;
+ ibBuffer >> m_idhHeader.e_oeminfo;
+
+ for (unsigned int i=0;i<sizeof(m_idhHeader.e_res2)/sizeof(m_idhHeader.e_res2[0]);i++)
+ {
+ ibBuffer >> m_idhHeader.e_res2[i];
+ }
+
+ ibBuffer >> m_idhHeader.e_lfanew;
+ }
+
+ /**
+ * Tests if the currently loaded MZ header is a valid MZ header.
+ * Note that this function does not check if the address to the PE header is valid as this is not possible.
+ * Actually, the only thing this function checks is if the e_magic value is set to 0x5A4D (IMAGE_DOS_SIGNATURE).
+ * Everything else is not relevant for Windows 2000 and that's the system PeLib is focusing on for now.
+ * @return A boolean value that indicates if the MZ header is correct or not.
+ **/
+ bool MzHeader::isValid() const
+ {
+ // The only thing that matters on Windows 2K is the e_magic value. The entire rest is for DOS compatibility.
+ return isValid(e_magic);
+ }
+
+ bool MzHeader::isValid(Field f) const
+ {
+ if (f == e_magic)
+ {
+ return m_idhHeader.e_magic == PELIB_IMAGE_DOS_SIGNATURE;
+ }
+ else
+ {
+ return true;
+ }
+ }
+
+ /**
+ * Corrects all erroneous values of the current MZ header. Note that this function does not correct the
+ * pointer to the PE header.
+ * Actually, the only thing this function corrects is the e_magic value.
+ * Everything else is not relevant for Windows 2000 and that's the system PeLib is focusing on for now.
+ **/
+ void MzHeader::makeValid()
+ {
+ // The only thing that matters on Windows is the e_magic value. The entire rest is for DOS compatibility.
+ setMagicNumber(PELIB_IMAGE_DOS_SIGNATURE);
+ }
+
+ void MzHeader::makeValid(Field f)
+ {
+ if (f == e_magic)
+ {
+ setMagicNumber(PELIB_IMAGE_DOS_SIGNATURE);
+ }
+ }
+
+ /**
+ * Reads the MZ header from a file. Note that this function does not verify if a file is actually a MZ file.
+ * For this purpose see #PeFile::MzHeader::isValid. The reason for this is simple: Otherwise it might not
+ * be possible to load damaged PE files to repair them.
+ * @param strFilename Name of the file which will be read.
+ * @return A non-zero value is returned if a problem occured.
+ **/
+ int MzHeader::read(const std::string& strFilename)
+ {
+ std::ifstream ifFile(strFilename.c_str(), std::ios::binary);
+
+ if (!ifFile)
+ {
+ return ERROR_OPENING_FILE;
+ }
+
+ if (fileSize(ifFile) < PELIB_IMAGE_DOS_HEADER::size())
+ {
+ return ERROR_INVALID_FILE;
+ }
+
+ ifFile.seekg(0, std::ios::beg);
+
+ originalOffset = 0;
+
+ std::vector<byte> vBuffer(PELIB_IMAGE_DOS_HEADER::size());
+ ifFile.read(reinterpret_cast<char*>(&vBuffer[0]), static_cast<unsigned int>(vBuffer.size()));
+ ifFile.close();
+
+ InputBuffer ibBuffer(vBuffer);
+ read(ibBuffer);
+ return NO_ERROR;
+ }
+
+ /**
+ * Reads the MZ header from memory. A pointer to a location in memory is passed and the data
+ * at this location is treated like a MZ header structure. The MZ header does not need to be valid.
+ * @param pcBuffer Pointer to a MZ header.
+ * @param uiSize Length of the buffer.
+ * @return A non-zero value is returned if a problem occured.
+ **/
+ int MzHeader::read(unsigned char* pcBuffer, unsigned int uiSize, unsigned int originalOffs)
+ {
+ if (uiSize < PELIB_IMAGE_DOS_HEADER::size())
+ {
+ return ERROR_INVALID_FILE;
+ }
+
+ std::vector<byte> vBuffer(pcBuffer, pcBuffer + uiSize);
+ for (int i=0;i<0x40;i++) std::cout << std::hex << (int)vBuffer[i] << " ";
+
+ originalOffset = originalOffs;
+
+ InputBuffer ibBuffer(vBuffer);
+ read(ibBuffer);
+ return NO_ERROR;
+ }
+
+ /**
+ * Rebuilds the MZ header so that it can be written to a file. It's not guaranteed that the
+ * MZ header will be valid. If you want to make sure that the MZ header will be valid you
+ * must call #PeLib::MzHeader::makeValid first.
+ * @param vBuffer Buffer where the rebuilt MZ header will be stored.
+ **/
+ void MzHeader::rebuild(std::vector<byte>& vBuffer) const
+ {
+ OutputBuffer obBuffer(vBuffer);
+
+ obBuffer << m_idhHeader.e_magic;
+ obBuffer << m_idhHeader.e_cblp;
+ obBuffer << m_idhHeader.e_cp;
+ obBuffer << m_idhHeader.e_crlc;
+ obBuffer << m_idhHeader.e_cparhdr;
+ obBuffer << m_idhHeader.e_minalloc;
+ obBuffer << m_idhHeader.e_maxalloc;
+ obBuffer << m_idhHeader.e_ss;
+ obBuffer << m_idhHeader.e_sp;
+ obBuffer << m_idhHeader.e_csum;
+ obBuffer << m_idhHeader.e_ip;
+ obBuffer << m_idhHeader.e_cs;
+ obBuffer << m_idhHeader.e_lfarlc;
+ obBuffer << m_idhHeader.e_ovno;
+
+ for (unsigned int i=0;i<sizeof(m_idhHeader.e_res)/sizeof(m_idhHeader.e_res[0]);i++)
+ {
+ obBuffer << m_idhHeader.e_res[i];
+ }
+
+ obBuffer << m_idhHeader.e_oemid;
+ obBuffer << m_idhHeader.e_oeminfo;
+
+ for (unsigned int i=0;i<sizeof(m_idhHeader.e_res2)/sizeof(m_idhHeader.e_res2[0]);i++)
+ {
+ obBuffer << m_idhHeader.e_res2[i];
+ }
+
+ obBuffer << m_idhHeader.e_lfanew;
+ }
+
+ /**
+ * Returns the size of the MZ header. This size is actually always sizeof(IMAGE_DOS_HEADER) (== 0x40)
+ * because the MZ header is a header of constant size if you disregard the dos stub. If you want to know the
+ * size of the MZ header + the size of the dos stub check #PeLib::MzHeader::getAddressOfPeHeader.
+ * @return Size of the MZ header.
+ **/
+ unsigned int MzHeader::size() const
+ {
+ return sizeof(m_idhHeader);
+ }
+
+ /**
+ * Writes the current MZ header to a file. The file does not have to exist. If it doesn't exist
+ * it will be created.
+ * @param strFilename Name of the file the header will be written to.
+ * @param dwOffset Offset the header will be written to (defaults to 0).
+ * @return A non-zero value is returned if a problem occured.
+ **/
+ int MzHeader::write(const std::string& strFilename, dword dwOffset = 0) const
+ {
+ std::fstream ofFile(strFilename.c_str(), std::ios_base::in);
+
+ if (!ofFile)
+ {
+ ofFile.clear();
+ ofFile.open(strFilename.c_str(), std::ios_base::out | std::ios_base::binary);
+ }
+ else
+ {
+ ofFile.close();
+ ofFile.open(strFilename.c_str(), std::ios_base::in | std::ios_base::out | std::ios_base::binary);
+ }
+
+ if (!ofFile)
+ {
+ return ERROR_OPENING_FILE;
+ }
+
+ ofFile.seekp(dwOffset, std::ios::beg);
+
+ std::vector<unsigned char> vBuffer;
+
+ rebuild(vBuffer);
+
+ ofFile.write(reinterpret_cast<const char*>(&vBuffer[0]), static_cast<unsigned int>(vBuffer.size()));
+
+ ofFile.close();
+
+ return NO_ERROR;
+ }
+
+ /**
+ * Returns the MZ header's e_magic value.
+ **/
+ word MzHeader::getMagicNumber() const
+ {
+ return m_idhHeader.e_magic;
+ }
+
+ /**
+ * Returns the MZ header's e_cblp value.
+ **/
+ word MzHeader::getBytesOnLastPage() const
+ {
+ return m_idhHeader.e_cblp;
+ }
+
+ /**
+ * Returns the MZ header's e_cp value.
+ **/
+ word MzHeader::getPagesInFile() const
+ {
+ return m_idhHeader.e_cp;
+ }
+
+ /**
+ * Returns the MZ header's e_crlc value.
+ **/
+ word MzHeader::getRelocations() const
+ {
+ return m_idhHeader.e_crlc;
+ }
+
+ /**
+ * Returns the MZ header's e_cparhdr value.
+ **/
+ word MzHeader::getSizeOfHeader() const
+ {
+ return m_idhHeader.e_cparhdr;
+ }
+
+ /**
+ * Returns the MZ header's e_minalloc value.
+ **/
+ word MzHeader::getMinExtraParagraphs() const
+ {
+ return m_idhHeader.e_minalloc;
+ }
+
+ /**
+ * Returns the MZ header's e_maxalloc value.
+ **/
+ word MzHeader::getMaxExtraParagraphs() const
+ {
+ return m_idhHeader.e_maxalloc;
+ }
+
+ /**
+ * Returns the MZ header's e_ss value.
+ **/
+ word MzHeader::getSsValue() const
+ {
+ return m_idhHeader.e_ss;
+ }
+
+ /**
+ * Returns the MZ header's e_sp value.
+ **/
+ word MzHeader::getSpValue() const
+ {
+ return m_idhHeader.e_sp;
+ }
+
+ /**
+ * Returns the MZ header's e_csum value.
+ **/
+ word MzHeader::getChecksum() const
+ {
+ return m_idhHeader.e_csum;
+ }
+
+ /**
+ * Returns the MZ header's e_ip value.
+ **/
+ word MzHeader::getIpValue() const
+ {
+ return m_idhHeader.e_ip;
+ }
+
+ /**
+ * Returns the MZ header's e_cs value.
+ **/
+ word MzHeader::getCsValue() const
+ {
+ return m_idhHeader.e_cs;
+ }
+
+ /**
+ * Returns the MZ header's e_lfarlc value.
+ **/
+ word MzHeader::getAddrOfRelocationTable() const
+ {
+ return m_idhHeader.e_lfarlc;
+ }
+
+ /**
+ * Returns the MZ header's e_ovno value.
+ **/
+ word MzHeader::getOverlayNumber() const
+ {
+ return m_idhHeader.e_ovno;
+ }
+
+ /**
+ * Returns the MZ header's e_oemid value.
+ **/
+ word MzHeader::getOemIdentifier() const
+ {
+ return m_idhHeader.e_oemid;
+ }
+
+ /**
+ * Returns the MZ header's e_oeminfo value.
+ **/
+ word MzHeader::getOemInformation() const
+ {
+ return m_idhHeader.e_oeminfo;
+ }
+
+ /**
+ * Returns the MZ header's e_lfanew value.
+ **/
+ dword MzHeader::getAddressOfPeHeader() const
+ {
+ return m_idhHeader.e_lfanew;
+ }
+
+ /**
+ * Returns the MZ header's e_res[uiNr] value. If the parameter uiNr is out of range
+ * you will get undefined behaviour.
+ * @param uiNr The index of the word in the e_res array (valid range: 0-3)
+ **/
+ word MzHeader::getReservedWords1(unsigned int uiNr) const
+ {
+ return m_idhHeader.e_res[uiNr];
+ }
+
+ /**
+ * Returns the MZ header's e_res2[uiNr] value. If the parameter uiNr is out of range
+ * you will get undefined behaviour.
+ * @param uiNr The index of the word in the e_res array (valid range: 0-9)
+ **/
+ word MzHeader::getReservedWords2(unsigned int uiNr) const
+ {
+ return m_idhHeader.e_res2[uiNr];
+ }
+
+ /**
+ * Sets the MZ header's e_magic value.
+ * @param wValue The new value of e_magic.
+ **/
+ void MzHeader::setMagicNumber(word wValue)
+ {
+ m_idhHeader.e_magic = wValue;
+ }
+
+ /**
+ * Sets the MZ header's e_cblp value.
+ * @param wValue The new value of e_cblp.
+ **/
+ void MzHeader::setBytesOnLastPage(word wValue)
+ {
+ m_idhHeader.e_cblp = wValue;
+ }
+
+ /**
+ * Sets the MZ header's e_cp value.
+ * @param wValue The new value of e_cp.
+ **/
+ void MzHeader::setPagesInFile(word wValue)
+ {
+ m_idhHeader.e_cp = wValue;
+ }
+
+ /**
+ * Sets the MZ header's e_crlc value.
+ * @param wValue The new value of e_crlc.
+ **/
+ void MzHeader::setRelocations(word wValue)
+ {
+ m_idhHeader.e_crlc = wValue;
+ }
+
+ /**
+ * Sets the MZ header's e_cparhdr value.
+ * @param wValue The new value of e_cparhdr.
+ **/
+ void MzHeader::setSizeOfHeader(word wValue)
+ {
+ m_idhHeader.e_cparhdr = wValue;
+ }
+
+ /**
+ * Sets the MZ header's e_minalloc value.
+ * @param wValue The new value of e_minalloc.
+ **/
+ void MzHeader::setMinExtraParagraphs(word wValue)
+ {
+ m_idhHeader.e_minalloc = wValue;
+ }
+
+ /**
+ * Sets the MZ header's e_maxalloc value.
+ * @param wValue The new value of e_maxalloc.
+ **/
+ void MzHeader::setMaxExtraParagraphs(word wValue)
+ {
+ m_idhHeader.e_maxalloc = wValue;
+ }
+
+ /**
+ * Sets the MZ header's e_ss value.
+ * @param wValue The new value of e_ss.
+ **/
+ void MzHeader::setSsValue(word wValue)
+ {
+ m_idhHeader.e_ss = wValue;
+ }
+
+ /**
+ * Sets the MZ header's e_sp value.
+ * @param wValue The new value of e_sp.
+ **/
+ void MzHeader::setSpValue(word wValue)
+ {
+ m_idhHeader.e_sp = wValue;
+ }
+
+ /**
+ * Sets the MZ header's e_csum value.
+ * @param wValue The new value of e_csum.
+ **/
+ void MzHeader::setChecksum(word wValue)
+ {
+ m_idhHeader.e_csum = wValue;
+ }
+
+ /**
+ * Sets the MZ header's e_ip value.
+ * @param wValue The new value of e_ip.
+ **/
+ void MzHeader::setIpValue(word wValue)
+ {
+ m_idhHeader.e_ip = wValue;
+ }
+
+ /**
+ * Sets the MZ header's e_cs value.
+ * @param wValue The new value of e_cs.
+ **/
+ void MzHeader::setCsValue(word wValue)
+ {
+ m_idhHeader.e_cs = wValue;
+ }
+
+ /**
+ * Sets the MZ header's e_lfarlc value.
+ * @param wValue The new value of e_lfarlc.
+ **/
+ void MzHeader::setAddrOfRelocationTable(word wValue)
+ {
+ m_idhHeader.e_lfarlc = wValue;
+ }
+
+ /**
+ * Sets the MZ header's e_ovno value.
+ * @param wValue The new value of e_ovno.
+ **/
+ void MzHeader::setOverlayNumber(word wValue)
+ {
+ m_idhHeader.e_ovno = wValue;
+ }
+
+ /**
+ * Sets the MZ header's e_oemid value.
+ * @param wValue The new value of e_oemid.
+ **/
+ void MzHeader::setOemIdentifier(word wValue)
+ {
+ m_idhHeader.e_oemid = wValue;
+ }
+
+ /**
+ * Sets the MZ header's e_oeminfo value.
+ * @param wValue The new value of e_oeminfo.
+ **/
+ void MzHeader::setOemInformation(word wValue)
+ {
+ m_idhHeader.e_oeminfo = wValue;
+ }
+
+ /**
+ * Sets the MZ header's e_lfanew value.
+ * @param lValue The new value of e_lfanew.
+ **/
+ void MzHeader::setAddressOfPeHeader(dword lValue)
+ {
+ m_idhHeader.e_lfanew = lValue;
+ }
+
+ /**
+ * Sets the MZ header's e_res[uiNr] value. If the parameter uiNr is out of range
+ * you will get undefined behaviour.
+ * @param uiNr The index of the word in the e_res array (valid range: 0-3)
+ * @param wValue The new value of e_res[nr].
+ **/
+ void MzHeader::setReservedWords1(unsigned int uiNr, word wValue)
+ {
+ m_idhHeader.e_res[uiNr] = wValue;
+ }
+
+ /**
+ * Sets the MZ header's e_res2[uiNr] value. If the parameter uiNr is out of range
+ * you will get undefined behaviour.
+ * @param uiNr The index of the word in the e_res2 array (valid range: 0-9)
+ * @param wValue The new value of e_res[nr].
+ **/
+ void MzHeader::setReservedWords2(unsigned int uiNr, word wValue)
+ {
+ m_idhHeader.e_res2[uiNr] = wValue;
+ }
+
+}
diff --git a/utils/zenutils/libraries/pelib-0.9/pelib/MzHeader.h b/utils/zenutils/libraries/pelib-0.9/pelib/MzHeader.h
index 5aca6bfe59..4acb819727 100755..100644
--- a/utils/zenutils/libraries/pelib-0.9/pelib/MzHeader.h
+++ b/utils/zenutils/libraries/pelib-0.9/pelib/MzHeader.h
@@ -1,148 +1,148 @@
-/*
-* MzHeader.h - Part of the PeLib library.
-*
-* Copyright (c) 2004 - 2005 Sebastian Porst (webmaster@the-interweb.com)
-* All rights reserved.
-*
-* This software is licensed under the zlib/libpng License.
-* For more details see http://www.opensource.org/licenses/zlib-license.php
-* or the license information file (license.htm) in the root directory
-* of PeLib.
-*/
-
-#ifndef MZHEADER_H
-#define MZHEADER_H
-
-#include "PeLibInc.h"
-
-namespace PeLib
-{
- /// Class that handles the MZ header of files.
- /**
- * This class can read and modify MZ headers. It provides set- and get functions to access
- * all individual members of a MZ header. Furthermore it's possible to validate and rebuild
- * MZ headers.
- **/
- class MzHeader
- {
- private:
- PELIB_IMAGE_DOS_HEADER m_idhHeader; ///< Stores all MZ header information.
-
- /// Reads data from an InputBuffer into a MZ header struct.
- void read(InputBuffer& ibBuffer);
-
- /// Offset of the MZ header in the original file.
- unsigned int originalOffset;
-
- public:
-
- enum Field {e_magic, e_cblp, e_cp, e_crlc, e_cparhdr, e_minalloc, e_maxalloc,
- e_ss, e_sp, e_csum, e_ip, e_cs, e_lfarlc, e_ovno, e_res, e_oemid,
- e_oeminfo, e_res2, e_lfanew};
-
- /// Checks if the current MZ header is valid.
- bool isValid() const; // EXPORT
-
- bool isValid(Field field) const; // EXPORT _field
-
- /// Corrects the current MZ header.
- void makeValid(); // EXPORT
-
- void makeValid(Field field); // EXPORT _field
-
- /// Reads the MZ header of a file.
- int read(const std::string& strFilename); // EXPORT
-
- /// Reads the MZ header from a memory location.
- int read(unsigned char* pcBuffer, unsigned int uiSize, unsigned int originalOffs = 0); // EXPORT _fromMemory
-
- /// Rebuild the MZ header.
- void rebuild(std::vector<byte>& vBuffer) const; // EXPORT
-
- /// Returns the size of the current MZ header.
- unsigned int size() const; // EXPORT
-
- /// Writes the current MZ header to offset 0 of a file.
- int write(const std::string& strFilename, dword dwOffset) const; // EXPORT
-
- /// Gets the e_magic value of the MZ header.
- word getMagicNumber() const; // EXPORT
- /// Gets the e_cblp value of the MZ header.
- word getBytesOnLastPage() const; // EXPORT
- /// Gets the e_cp value of the MZ header.
- word getPagesInFile() const; // EXPORT
- /// Gets the e_crlc value of the MZ header.
- word getRelocations() const; // EXPORT
- /// Gets the e_cparhdr value of the MZ header.
- word getSizeOfHeader() const; // EXPORT
- /// Gets the e_minalloc value of the MZ header.
- word getMinExtraParagraphs() const; // EXPORT
- /// Gets the e_maxalloc value of the MZ header.
- word getMaxExtraParagraphs() const; // EXPORT
- /// Gets the e_ss value of the MZ header.
- word getSsValue() const; // EXPORT
- /// Gets the e_sp value of the MZ header.
- word getSpValue() const; // EXPORT
- /// Gets the e_csum value of the MZ header.
- word getChecksum() const; // EXPORT
- /// Gets the e_ip value of the MZ header.
- word getIpValue() const; // EXPORT
- /// Gets the e_cs value of the MZ header.
- word getCsValue() const; // EXPORT
- /// Gets the e_lfarlc value of the MZ header.
- word getAddrOfRelocationTable() const; // EXPORT
- /// Gets the e_ovnovalue of the MZ header.
- word getOverlayNumber() const; // EXPORT
- /// Gets the e_oemid value of the MZ header.
- word getOemIdentifier() const; // EXPORT
- /// Gets the e_oeminfo value of the MZ header.
- word getOemInformation() const; // EXPORT
- /// Gets the e_lfanew value of the MZ header.
- dword getAddressOfPeHeader() const; // EXPORT
- /// Gets the e_res of the MZ header.
- word getReservedWords1(unsigned int uiNr) const; // EXPORT
- /// Gets the e_res2 of the MZ header.
- word getReservedWords2(unsigned int uiNr) const; // EXPORT
-
- /// Sets the e_magic value of the MZ header.
- void setMagicNumber(word wValue); // EXPORT
- /// Sets the e_cblp value of the MZ header.
- void setBytesOnLastPage(word wValue); // EXPORT
- /// Sets the e_cp value of the MZ header.
- void setPagesInFile(word wValue); // EXPORT
- /// Sets the e_crlc value of the MZ header.
- void setRelocations(word wValue); // EXPORT
- /// Sets the e_cparhdr value of the MZ header.
- void setSizeOfHeader(word wValue); // EXPORT
- /// Sets the e_minalloc value of the MZ header.
- void setMinExtraParagraphs(word wValue); // EXPORT
- /// Sets the e_maxalloc value of the MZ header.
- void setMaxExtraParagraphs(word wValue); // EXPORT
- /// Sets the e_ss value of the MZ header.
- void setSsValue(word wValue); // EXPORT
- /// Sets the e_sp value of the MZ header.
- void setSpValue(word wValue); // EXPORT
- /// Sets the e_csum value of the MZ header.
- void setChecksum(word wValue); // EXPORT
- /// Sets the e_ip value of the MZ header.
- void setIpValue(word wValue); // EXPORT
- /// Sets the e_cs value of the MZ header.
- void setCsValue(word wValue); // EXPORT
- /// Sets the e_lfarlc value of the MZ header.
- void setAddrOfRelocationTable(word wValue); // EXPORT
- /// Sets the e_ovno value of the MZ header.
- void setOverlayNumber(word wValue); // EXPORT
- /// Sets the e_oemid value of the MZ header.
- void setOemIdentifier(word wValue); // EXPORT
- /// Sets the e_oeminfo value of the MZ header.
- void setOemInformation(word wValue); // EXPORT
- /// Sets the e_lfanew value of the MZ header.
- void setAddressOfPeHeader(dword dwValue); // EXPORT
- /// Sets the e_res value of the MZ header.
- void setReservedWords1(unsigned int uiNr, word wValue); // EXPORT
- /// Sets the e_res2 value of the MZ header.
- void setReservedWords2(unsigned int uiNr, word wValue); // EXPORT
- };
-}
-
-#endif
+/*
+* MzHeader.h - Part of the PeLib library.
+*
+* Copyright (c) 2004 - 2005 Sebastian Porst (webmaster@the-interweb.com)
+* All rights reserved.
+*
+* This software is licensed under the zlib/libpng License.
+* For more details see http://www.opensource.org/licenses/zlib-license.php
+* or the license information file (license.htm) in the root directory
+* of PeLib.
+*/
+
+#ifndef MZHEADER_H
+#define MZHEADER_H
+
+#include "PeLibInc.h"
+
+namespace PeLib
+{
+ /// Class that handles the MZ header of files.
+ /**
+ * This class can read and modify MZ headers. It provides set- and get functions to access
+ * all individual members of a MZ header. Furthermore it's possible to validate and rebuild
+ * MZ headers.
+ **/
+ class MzHeader
+ {
+ private:
+ PELIB_IMAGE_DOS_HEADER m_idhHeader; ///< Stores all MZ header information.
+
+ /// Reads data from an InputBuffer into a MZ header struct.
+ void read(InputBuffer& ibBuffer);
+
+ /// Offset of the MZ header in the original file.
+ unsigned int originalOffset;
+
+ public:
+
+ enum Field {e_magic, e_cblp, e_cp, e_crlc, e_cparhdr, e_minalloc, e_maxalloc,
+ e_ss, e_sp, e_csum, e_ip, e_cs, e_lfarlc, e_ovno, e_res, e_oemid,
+ e_oeminfo, e_res2, e_lfanew};
+
+ /// Checks if the current MZ header is valid.
+ bool isValid() const; // EXPORT
+
+ bool isValid(Field field) const; // EXPORT _field
+
+ /// Corrects the current MZ header.
+ void makeValid(); // EXPORT
+
+ void makeValid(Field field); // EXPORT _field
+
+ /// Reads the MZ header of a file.
+ int read(const std::string& strFilename); // EXPORT
+
+ /// Reads the MZ header from a memory location.
+ int read(unsigned char* pcBuffer, unsigned int uiSize, unsigned int originalOffs = 0); // EXPORT _fromMemory
+
+ /// Rebuild the MZ header.
+ void rebuild(std::vector<byte>& vBuffer) const; // EXPORT
+
+ /// Returns the size of the current MZ header.
+ unsigned int size() const; // EXPORT
+
+ /// Writes the current MZ header to offset 0 of a file.
+ int write(const std::string& strFilename, dword dwOffset) const; // EXPORT
+
+ /// Gets the e_magic value of the MZ header.
+ word getMagicNumber() const; // EXPORT
+ /// Gets the e_cblp value of the MZ header.
+ word getBytesOnLastPage() const; // EXPORT
+ /// Gets the e_cp value of the MZ header.
+ word getPagesInFile() const; // EXPORT
+ /// Gets the e_crlc value of the MZ header.
+ word getRelocations() const; // EXPORT
+ /// Gets the e_cparhdr value of the MZ header.
+ word getSizeOfHeader() const; // EXPORT
+ /// Gets the e_minalloc value of the MZ header.
+ word getMinExtraParagraphs() const; // EXPORT
+ /// Gets the e_maxalloc value of the MZ header.
+ word getMaxExtraParagraphs() const; // EXPORT
+ /// Gets the e_ss value of the MZ header.
+ word getSsValue() const; // EXPORT
+ /// Gets the e_sp value of the MZ header.
+ word getSpValue() const; // EXPORT
+ /// Gets the e_csum value of the MZ header.
+ word getChecksum() const; // EXPORT
+ /// Gets the e_ip value of the MZ header.
+ word getIpValue() const; // EXPORT
+ /// Gets the e_cs value of the MZ header.
+ word getCsValue() const; // EXPORT
+ /// Gets the e_lfarlc value of the MZ header.
+ word getAddrOfRelocationTable() const; // EXPORT
+ /// Gets the e_ovnovalue of the MZ header.
+ word getOverlayNumber() const; // EXPORT
+ /// Gets the e_oemid value of the MZ header.
+ word getOemIdentifier() const; // EXPORT
+ /// Gets the e_oeminfo value of the MZ header.
+ word getOemInformation() const; // EXPORT
+ /// Gets the e_lfanew value of the MZ header.
+ dword getAddressOfPeHeader() const; // EXPORT
+ /// Gets the e_res of the MZ header.
+ word getReservedWords1(unsigned int uiNr) const; // EXPORT
+ /// Gets the e_res2 of the MZ header.
+ word getReservedWords2(unsigned int uiNr) const; // EXPORT
+
+ /// Sets the e_magic value of the MZ header.
+ void setMagicNumber(word wValue); // EXPORT
+ /// Sets the e_cblp value of the MZ header.
+ void setBytesOnLastPage(word wValue); // EXPORT
+ /// Sets the e_cp value of the MZ header.
+ void setPagesInFile(word wValue); // EXPORT
+ /// Sets the e_crlc value of the MZ header.
+ void setRelocations(word wValue); // EXPORT
+ /// Sets the e_cparhdr value of the MZ header.
+ void setSizeOfHeader(word wValue); // EXPORT
+ /// Sets the e_minalloc value of the MZ header.
+ void setMinExtraParagraphs(word wValue); // EXPORT
+ /// Sets the e_maxalloc value of the MZ header.
+ void setMaxExtraParagraphs(word wValue); // EXPORT
+ /// Sets the e_ss value of the MZ header.
+ void setSsValue(word wValue); // EXPORT
+ /// Sets the e_sp value of the MZ header.
+ void setSpValue(word wValue); // EXPORT
+ /// Sets the e_csum value of the MZ header.
+ void setChecksum(word wValue); // EXPORT
+ /// Sets the e_ip value of the MZ header.
+ void setIpValue(word wValue); // EXPORT
+ /// Sets the e_cs value of the MZ header.
+ void setCsValue(word wValue); // EXPORT
+ /// Sets the e_lfarlc value of the MZ header.
+ void setAddrOfRelocationTable(word wValue); // EXPORT
+ /// Sets the e_ovno value of the MZ header.
+ void setOverlayNumber(word wValue); // EXPORT
+ /// Sets the e_oemid value of the MZ header.
+ void setOemIdentifier(word wValue); // EXPORT
+ /// Sets the e_oeminfo value of the MZ header.
+ void setOemInformation(word wValue); // EXPORT
+ /// Sets the e_lfanew value of the MZ header.
+ void setAddressOfPeHeader(dword dwValue); // EXPORT
+ /// Sets the e_res value of the MZ header.
+ void setReservedWords1(unsigned int uiNr, word wValue); // EXPORT
+ /// Sets the e_res2 value of the MZ header.
+ void setReservedWords2(unsigned int uiNr, word wValue); // EXPORT
+ };
+}
+
+#endif
diff --git a/utils/zenutils/libraries/pelib-0.9/pelib/PeFile.cpp b/utils/zenutils/libraries/pelib-0.9/pelib/PeFile.cpp
index 39f2488b81..9e9f2d8691 100755..100644
--- a/utils/zenutils/libraries/pelib-0.9/pelib/PeFile.cpp
+++ b/utils/zenutils/libraries/pelib-0.9/pelib/PeFile.cpp
@@ -1,169 +1,169 @@
-/*
-* PeLib.cpp - Part of the PeLib library.
-*
-* Copyright (c) 2004 - 2005 Sebastian Porst (webmaster@the-interweb.com)
-* All rights reserved.
-*
-* This software is licensed under the zlib/libpng License.
-* For more details see http://www.opensource.org/licenses/zlib-license.php
-* or the license information file (license.htm) in the root directory
-* of PeLib.
-*/
-
-#include "PeFile.h"
-
-namespace PeLib
-{
- PeFile::~PeFile()
- {
- }
-
- PeFile32::PeFile32() : PeFileT<32>()
- {
- }
-
- PeFile32::PeFile32(const std::string& strFlename) : PeFileT<32>(strFlename)
- {
- }
-
- PeFile64::PeFile64() : PeFileT<64>()
- {
- }
-
- PeFile64::PeFile64(const std::string& strFlename) : PeFileT<64>(strFlename)
- {
- }
-
- /**
- * @return A reference to the file's MZ header.
- **/
-
- const MzHeader& PeFile::mzHeader() const
- {
- return m_mzh;
- }
-
- /**
- * @return A reference to the file's MZ header.
- **/
-
- MzHeader& PeFile::mzHeader()
- {
- return m_mzh;
- }
-
- /**
- * @return A reference to the file's export directory.
- **/
-
- const ExportDirectory& PeFile::expDir() const
- {
- return m_expdir;
- }
-
- /**
- * @return A reference to the file's export directory.
- **/
-
- ExportDirectory& PeFile::expDir()
- {
- return m_expdir;
- }
-
- /**
- * @return A reference to the file's bound import directory.
- **/
-
- const BoundImportDirectory& PeFile::boundImpDir() const
- {
- return m_boundimpdir;
- }
-
- /**
- * @return A reference to the file's bound import directory.
- **/
-
- BoundImportDirectory& PeFile::boundImpDir()
- {
- return m_boundimpdir;
- }
-
- /**
- * @return A reference to the file's resource directory.
- **/
-
- const ResourceDirectory& PeFile::resDir() const
- {
- return m_resdir;
- }
-
- /**
- * @return A reference to the file's resource directory.
- **/
-
- ResourceDirectory& PeFile::resDir()
- {
- return m_resdir;
- }
-
- /**
- * @return A reference to the file's relocations directory.
- **/
-
- const RelocationsDirectory& PeFile::relocDir() const
- {
- return m_relocs;
- }
-
- /**
- * @return A reference to the file's relocations directory.
- **/
-
- RelocationsDirectory& PeFile::relocDir()
- {
- return m_relocs;
- }
-
- /**
- * @return A reference to the file's COM+ descriptor directory.
- **/
-
- const ComHeaderDirectory& PeFile::comDir() const
- {
- return m_comdesc;
- }
-
- /**
- * @return A reference to the file's COM+ descriptor directory.
- **/
-
- ComHeaderDirectory & PeFile::comDir()
- {
- return m_comdesc;
- }
-
-
- const IatDirectory& PeFile::iatDir() const
- {
- return m_iat;
- }
-
-
- IatDirectory& PeFile::iatDir()
- {
- return m_iat;
- }
-
-
- const DebugDirectory& PeFile::debugDir() const
- {
- return m_debugdir;
- }
-
-
- DebugDirectory& PeFile::debugDir()
- {
- return m_debugdir;
- }
-
-}
+/*
+* PeLib.cpp - Part of the PeLib library.
+*
+* Copyright (c) 2004 - 2005 Sebastian Porst (webmaster@the-interweb.com)
+* All rights reserved.
+*
+* This software is licensed under the zlib/libpng License.
+* For more details see http://www.opensource.org/licenses/zlib-license.php
+* or the license information file (license.htm) in the root directory
+* of PeLib.
+*/
+
+#include "PeFile.h"
+
+namespace PeLib
+{
+ PeFile::~PeFile()
+ {
+ }
+
+ PeFile32::PeFile32() : PeFileT<32>()
+ {
+ }
+
+ PeFile32::PeFile32(const std::string& strFlename) : PeFileT<32>(strFlename)
+ {
+ }
+
+ PeFile64::PeFile64() : PeFileT<64>()
+ {
+ }
+
+ PeFile64::PeFile64(const std::string& strFlename) : PeFileT<64>(strFlename)
+ {
+ }
+
+ /**
+ * @return A reference to the file's MZ header.
+ **/
+
+ const MzHeader& PeFile::mzHeader() const
+ {
+ return m_mzh;
+ }
+
+ /**
+ * @return A reference to the file's MZ header.
+ **/
+
+ MzHeader& PeFile::mzHeader()
+ {
+ return m_mzh;
+ }
+
+ /**
+ * @return A reference to the file's export directory.
+ **/
+
+ const ExportDirectory& PeFile::expDir() const
+ {
+ return m_expdir;
+ }
+
+ /**
+ * @return A reference to the file's export directory.
+ **/
+
+ ExportDirectory& PeFile::expDir()
+ {
+ return m_expdir;
+ }
+
+ /**
+ * @return A reference to the file's bound import directory.
+ **/
+
+ const BoundImportDirectory& PeFile::boundImpDir() const
+ {
+ return m_boundimpdir;
+ }
+
+ /**
+ * @return A reference to the file's bound import directory.
+ **/
+
+ BoundImportDirectory& PeFile::boundImpDir()
+ {
+ return m_boundimpdir;
+ }
+
+ /**
+ * @return A reference to the file's resource directory.
+ **/
+
+ const ResourceDirectory& PeFile::resDir() const
+ {
+ return m_resdir;
+ }
+
+ /**
+ * @return A reference to the file's resource directory.
+ **/
+
+ ResourceDirectory& PeFile::resDir()
+ {
+ return m_resdir;
+ }
+
+ /**
+ * @return A reference to the file's relocations directory.
+ **/
+
+ const RelocationsDirectory& PeFile::relocDir() const
+ {
+ return m_relocs;
+ }
+
+ /**
+ * @return A reference to the file's relocations directory.
+ **/
+
+ RelocationsDirectory& PeFile::relocDir()
+ {
+ return m_relocs;
+ }
+
+ /**
+ * @return A reference to the file's COM+ descriptor directory.
+ **/
+
+ const ComHeaderDirectory& PeFile::comDir() const
+ {
+ return m_comdesc;
+ }
+
+ /**
+ * @return A reference to the file's COM+ descriptor directory.
+ **/
+
+ ComHeaderDirectory & PeFile::comDir()
+ {
+ return m_comdesc;
+ }
+
+
+ const IatDirectory& PeFile::iatDir() const
+ {
+ return m_iat;
+ }
+
+
+ IatDirectory& PeFile::iatDir()
+ {
+ return m_iat;
+ }
+
+
+ const DebugDirectory& PeFile::debugDir() const
+ {
+ return m_debugdir;
+ }
+
+
+ DebugDirectory& PeFile::debugDir()
+ {
+ return m_debugdir;
+ }
+
+}
diff --git a/utils/zenutils/libraries/pelib-0.9/pelib/PeFile.h b/utils/zenutils/libraries/pelib-0.9/pelib/PeFile.h
index a2b02cdfa8..e08d8da309 100755..100644
--- a/utils/zenutils/libraries/pelib-0.9/pelib/PeFile.h
+++ b/utils/zenutils/libraries/pelib-0.9/pelib/PeFile.h
@@ -1,451 +1,451 @@
-/*
-* PeFile.h - Part of the PeLib library.
-*
-* Copyright (c) 2004 - 2005 Sebastian Porst (webmaster@the-interweb.com)
-* All rights reserved.
-*
-* This software is licensed under the zlib/libpng License.
-* For more details see http://www.opensource.org/licenses/zlib-license.php
-* or the license information file (license.htm) in the root directory
-* of PeLib.
-*/
-
-#ifndef PEFILE_H
-#define PEFILE_H
-
-#include "PeLibInc.h"
-#include "MzHeader.h"
-#include "PeHeader.h"
-#include "ImportDirectory.h"
-#include "ExportDirectory.h"
-#include "BoundImportDirectory.h"
-#include "ResourceDirectory.h"
-#include "RelocationsDirectory.h"
-#include "ComHeaderDirectory.h"
-#include "IatDirectory.h"
-#include "DebugDirectory.h"
-#include "TlsDirectory.h"
-
-namespace PeLib
-{
- class PeFile32;
- class PeFile64;
-
- /**
- * Visitor base class for PeFiles.
- **/
- class PeFileVisitor
- {
- public:
- virtual void callback(PeFile32 &file){}
- virtual void callback(PeFile64 &file){}
- virtual ~PeFileVisitor(){}
- };
-
- /**
- * Traits class that's used to decide of what type the PeHeader in a PeFile is.
- **/
- template<int>
- struct PeFile_Traits;
-
- template<>
- struct PeFile_Traits<32>
- {
- typedef PeHeader32 PeHeader32_64;
- };
-
- template<>
- struct PeFile_Traits<64>
- {
- typedef PeHeader64 PeHeader32_64;
- };
-
- /**
- * This class represents the common structures of PE and PE+ files.
- **/
- class PeFile
- {
- protected:
- std::string m_filename; ///< Name of the current file.
- MzHeader m_mzh; ///< MZ header of the current file.
- ExportDirectory m_expdir; ///< Export directory of the current file.
- BoundImportDirectory m_boundimpdir; ///< BoundImportDirectory of the current file.
- ResourceDirectory m_resdir; ///< ResourceDirectory of the current file.
- RelocationsDirectory m_relocs; ///< Relocations directory of the current file.
- ComHeaderDirectory m_comdesc; ///< COM+ descriptor directory of the current file.
- IatDirectory m_iat; ///< Import address table of the current file.
- DebugDirectory m_debugdir;
- public:
- virtual ~PeFile();
-
- /// Returns the name of the current file.
- virtual std::string getFileName() const = 0; // EXPORT
- /// Changes the name of the current file.
- virtual void setFileName(std::string strFilename) = 0; // EXPORT
-
- virtual void visit(PeFileVisitor &v) = 0;
-
- /// Reads the MZ header of the current file from disc.
- virtual int readMzHeader() = 0; // EXPORT
- /// Reads the export directory of the current file from disc.
- virtual int readExportDirectory() = 0; // EXPORT
- /// Reads the PE header of the current file from disc.
- virtual int readPeHeader() = 0; // EXPORT
- /// Reads the import directory of the current file from disc.
- virtual int readImportDirectory() = 0; // EXPORT
- /// Reads the bound import directory of the current file from disc.
- virtual int readBoundImportDirectory() = 0; // EXPORT
- /// Reads the resource directory of the current file from disc.
- virtual int readResourceDirectory() = 0; // EXPORT
- /// Reads the relocations directory of the current file from disc.
- virtual int readRelocationsDirectory() = 0; // EXPORT
- /// Reads the COM+ descriptor directory of the current file from disc.
- virtual int readComHeaderDirectory() = 0; // EXPORT
- /// Reads the IAT directory of the current file from disc.
- virtual int readIatDirectory() = 0; // EXPORT
- /// Reads the Debug directory of the current file.
- virtual int readDebugDirectory() = 0; // EXPORT
- virtual int readTlsDirectory() = 0; // EXPORT
-
- virtual unsigned int getBits() const = 0;
-
- /// Accessor function for the MZ header.
- const MzHeader& mzHeader() const;
- /// Accessor function for the MZ header.
- MzHeader& mzHeader(); // EXPORT
-
- /// Accessor function for the export directory.
- const ExportDirectory& expDir() const;
- /// Accessor function for the export directory.
- ExportDirectory& expDir(); // EXPORT
-
- /// Accessor function for the bound import directory.
- const BoundImportDirectory& boundImpDir() const;
- /// Accessor function for the bound import directory.
- BoundImportDirectory& boundImpDir(); // EXPORT
-
- /// Accessor function for the resource directory.
- const ResourceDirectory& resDir() const;
- /// Accessor function for the resource directory.
- ResourceDirectory& resDir(); // EXPORT
-
- /// Accessor function for the relocations directory.
- const RelocationsDirectory& relocDir() const;
- /// Accessor function for the relocations directory.
- RelocationsDirectory& relocDir(); // EXPORT
-
- /// Accessor function for the COM+ descriptor directory.
- const ComHeaderDirectory& comDir() const;
- /// Accessor function for the COM+ descriptor directory.
- ComHeaderDirectory& comDir(); // EXPORT
-
- /// Accessor function for the IAT directory.
- const IatDirectory& iatDir() const;
- /// Accessor function for the IAT directory.
- IatDirectory& iatDir(); // EXPORT
-
- /// Accessor function for the debug directory.
- const DebugDirectory& debugDir() const;
- /// Accessor function for the debug directory.
- DebugDirectory& debugDir(); // EXPORT
-
- };
-
- /**
- * This class implements the common structures of PE and PE+ files.
- **/
- template<int bits>
- class PeFileT : public PeFile
- {
- typedef typename PeFile_Traits<bits>::PeHeader32_64 PeHeader32_64;
-
- private:
- PeHeader32_64 m_peh; ///< PE header of the current file.
- ImportDirectory<bits> m_impdir; ///< Import directory of the current file.
- TlsDirectory<bits> m_tlsdir;
-
- public:
- /// Default constructor which exists only for the sake of allowing to construct files without filenames.
- PeFileT();
-
- virtual ~PeFileT() {}
-
- /// Initializes a PeFile with a filename
- explicit PeFileT(const std::string& strFilename);
-
- /// Returns the name of the current file.
- std::string getFileName() const;
- /// Changes the name of the current file.
- void setFileName(std::string strFilename);
-
- /// Reads the MZ header of the current file from disc.
- int readMzHeader() ;
- /// Reads the export directory of the current file from disc.
- int readExportDirectory() ;
- /// Reads the PE header of the current file from disc.
- int readPeHeader() ;
- /// Reads the import directory of the current file from disc.
- int readImportDirectory() ;
- /// Reads the bound import directory of the current file from disc.
- int readBoundImportDirectory() ;
- /// Reads the resource directory of the current file from disc.
- int readResourceDirectory() ;
- /// Reads the relocations directory of the current file from disc.
- int readRelocationsDirectory() ;
- /// Reads the COM+ descriptor directory of the current file from disc.
- int readComHeaderDirectory() ;
- /// Reads the IAT directory of the current file from disc.
- int readIatDirectory() ;
- /// Reads the Debug directory of the current file.
- int readDebugDirectory() ;
- int readTlsDirectory() ;
-
- unsigned int getBits() const
- {
- return bits;
- }
-
- /// Accessor function for the PE header.
- const PeHeader32_64& peHeader() const;
- /// Accessor function for the PE header.
- PeHeader32_64& peHeader();
-
- /// Accessor function for the import directory.
- const ImportDirectory<bits>& impDir() const;
- /// Accessor function for the import directory.
- ImportDirectory<bits>& impDir();
-
- const TlsDirectory<bits>& tlsDir() const;
- TlsDirectory<bits>& tlsDir();
- };
-
- /**
- * This class is the main class for handling PE files.
- **/
- class PeFile32 : public PeFileT<32>
- {
- public:
- /// Default constructor which exists only for the sake of allowing to construct files without filenames.
- PeFile32();
-
- /// Initializes a PeFile with a filename
- explicit PeFile32(const std::string& strFlename);
- virtual void visit(PeFileVisitor &v) { v.callback( *this ); }
- };
-
- /**
- * This class is the main class for handling PE+ files.
- **/
- class PeFile64 : public PeFileT<64>
- {
- public:
- /// Default constructor which exists only for the sake of allowing to construct files without filenames.
- PeFile64();
-
- /// Initializes a PeFile with a filename
- explicit PeFile64(const std::string& strFlename);
- virtual void visit(PeFileVisitor &v) { v.callback( *this ); }
- };
-
- //typedef PeFileT<32> PeFile32;
- //typedef PeFileT<64> PeFile64;
-
- /**
- * @param strFilename Name of the current file.
- **/
- template<int bits>
- PeFileT<bits>::PeFileT(const std::string& strFilename)
- {
- m_filename = strFilename;
- }
-
- template<int bits>
- PeFileT<bits>::PeFileT()
- {
- }
-
- template<int bits>
- int PeFileT<bits>::readPeHeader()
- {
- return peHeader().read(getFileName(), mzHeader().getAddressOfPeHeader());
- }
-
-
- template<int bits>
- int PeFileT<bits>::readImportDirectory()
- {
- if (peHeader().calcNumberOfRvaAndSizes() >= 2
- && peHeader().getIddImportRva()
- && peHeader().getIddImportSize())
- {
- return impDir().read(getFileName(), static_cast<unsigned int>(peHeader().rvaToOffset(peHeader().getIddImportRva())), peHeader().getIddImportSize(), peHeader());
- }
- return ERROR_DIRECTORY_DOES_NOT_EXIST;
- }
-
- /**
- * @return A reference to the file's PE header.
- **/
- template<int bits>
- const typename PeFile_Traits<bits>::PeHeader32_64& PeFileT<bits>::peHeader() const
- {
- return m_peh;
- }
-
- /**
- * @return A reference to the file's PE header.
- **/
- template<int bits>
- typename PeFile_Traits<bits>::PeHeader32_64& PeFileT<bits>::peHeader()
- {
- return m_peh;
- }
-
- /**
- * @return A reference to the file's import directory.
- **/
- template<int bits>
- const ImportDirectory<bits>& PeFileT<bits>::impDir() const
- {
- return m_impdir;
- }
-
- /**
- * @return A reference to the file's import directory.
- **/
- template<int bits>
- ImportDirectory<bits>& PeFileT<bits>::impDir()
- {
- return m_impdir;
- }
-
- template<int bits>
- const TlsDirectory<bits>& PeFileT<bits>::tlsDir() const
- {
- return m_tlsdir;
- }
-
- template<int bits>
- TlsDirectory<bits>& PeFileT<bits>::tlsDir()
- {
- return m_tlsdir;
- }
-
- /**
- * @return Filename of the current file.
- **/
- template<int bits>
- std::string PeFileT<bits>::getFileName() const
- {
- return m_filename;
- }
-
- /**
- * @param strFilename New filename.
- **/
- template<int bits>
- void PeFileT<bits>::setFileName(std::string strFilename)
- {
- m_filename = strFilename;
- }
-
- template<int bits>
- int PeFileT<bits>::readMzHeader()
- {
- return mzHeader().read(getFileName());
- }
-
- template<int bits>
- int PeFileT<bits>::readExportDirectory()
- {
- if (peHeader().calcNumberOfRvaAndSizes() >= 1
- && peHeader().getIddExportRva() && peHeader().getIddExportSize())
- {
- return expDir().read(getFileName(), static_cast<unsigned int>(peHeader().rvaToOffset(peHeader().getIddExportRva())), peHeader().getIddExportSize(), peHeader());
- }
- return ERROR_DIRECTORY_DOES_NOT_EXIST;
- }
-
-
- template<int bits>
- int PeFileT<bits>::readBoundImportDirectory()
- {
- if (peHeader().calcNumberOfRvaAndSizes() >= 12
- && peHeader().getIddBoundImportRva() && peHeader().getIddBoundImportSize())
- {
- return boundImpDir().read(getFileName(), static_cast<unsigned int>(peHeader().rvaToOffset(peHeader().getIddBoundImportRva())), peHeader().getIddBoundImportSize());
- }
- return ERROR_DIRECTORY_DOES_NOT_EXIST;
- }
-
- template<int bits>
- int PeFileT<bits>::readResourceDirectory()
- {
- if (peHeader().calcNumberOfRvaAndSizes() >= 3
- && peHeader().getIddResourceRva() && peHeader().getIddResourceSize())
- {
- return resDir().read(getFileName(), static_cast<unsigned int>(peHeader().rvaToOffset(peHeader().getIddResourceRva())), peHeader().getIddResourceSize(), peHeader().getIddResourceRva());
- }
- return ERROR_DIRECTORY_DOES_NOT_EXIST;
- }
-
- template<int bits>
- int PeFileT<bits>::readRelocationsDirectory()
- {
- if (peHeader().calcNumberOfRvaAndSizes() >= 6
- && peHeader().getIddBaseRelocRva() && peHeader().getIddBaseRelocSize())
- {
- return relocDir().read(getFileName(), static_cast<unsigned int>(peHeader().rvaToOffset(peHeader().getIddBaseRelocRva())), peHeader().getIddBaseRelocSize());
- }
- return ERROR_DIRECTORY_DOES_NOT_EXIST;
- }
-
- template<int bits>
- int PeFileT<bits>::readComHeaderDirectory()
- {
- if (peHeader().calcNumberOfRvaAndSizes() >= 15
- && peHeader().getIddComHeaderRva() && peHeader().getIddComHeaderSize())
- {
- return comDir().read(getFileName(), static_cast<unsigned int>(peHeader().rvaToOffset(peHeader().getIddComHeaderRva())), peHeader().getIddComHeaderSize());
- }
- std::cout << peHeader().getIddComHeaderRva() << std::endl;
- std::exit(0);
- return ERROR_DIRECTORY_DOES_NOT_EXIST;
- }
-
- template<int bits>
- int PeFileT<bits>::readIatDirectory()
- {
- if (peHeader().calcNumberOfRvaAndSizes() >= 13
- && peHeader().getIddIatRva() && peHeader().getIddIatSize())
- {
- return iatDir().read(getFileName(), static_cast<unsigned int>(peHeader().rvaToOffset(peHeader().getIddIatRva())), peHeader().getIddIatSize());
- }
- return ERROR_DIRECTORY_DOES_NOT_EXIST;
- }
-
- template<int bits>
- int PeFileT<bits>::readDebugDirectory()
- {
- if (peHeader().calcNumberOfRvaAndSizes() >= 7
- && peHeader().getIddDebugRva() && peHeader().getIddDebugSize())
- {
- return debugDir().read(getFileName(), static_cast<unsigned int>(peHeader().rvaToOffset(peHeader().getIddDebugRva())), peHeader().getIddDebugSize());
- }
- return ERROR_DIRECTORY_DOES_NOT_EXIST;
- }
-
- template<int bits>
- int PeFileT<bits>::readTlsDirectory()
- {
- if (peHeader().calcNumberOfRvaAndSizes() >= 10
- && peHeader().getIddTlsRva() && peHeader().getIddTlsSize())
- {
- return tlsDir().read(getFileName(), static_cast<unsigned int>(peHeader().rvaToOffset(peHeader().getIddTlsRva())), peHeader().getIddTlsSize());
- }
- return ERROR_DIRECTORY_DOES_NOT_EXIST;
- }
-
-}
-
-#endif
+/*
+* PeFile.h - Part of the PeLib library.
+*
+* Copyright (c) 2004 - 2005 Sebastian Porst (webmaster@the-interweb.com)
+* All rights reserved.
+*
+* This software is licensed under the zlib/libpng License.
+* For more details see http://www.opensource.org/licenses/zlib-license.php
+* or the license information file (license.htm) in the root directory
+* of PeLib.
+*/
+
+#ifndef PEFILE_H
+#define PEFILE_H
+
+#include "PeLibInc.h"
+#include "MzHeader.h"
+#include "PeHeader.h"
+#include "ImportDirectory.h"
+#include "ExportDirectory.h"
+#include "BoundImportDirectory.h"
+#include "ResourceDirectory.h"
+#include "RelocationsDirectory.h"
+#include "ComHeaderDirectory.h"
+#include "IatDirectory.h"
+#include "DebugDirectory.h"
+#include "TlsDirectory.h"
+
+namespace PeLib
+{
+ class PeFile32;
+ class PeFile64;
+
+ /**
+ * Visitor base class for PeFiles.
+ **/
+ class PeFileVisitor
+ {
+ public:
+ virtual void callback(PeFile32 &file){}
+ virtual void callback(PeFile64 &file){}
+ virtual ~PeFileVisitor(){}
+ };
+
+ /**
+ * Traits class that's used to decide of what type the PeHeader in a PeFile is.
+ **/
+ template<int>
+ struct PeFile_Traits;
+
+ template<>
+ struct PeFile_Traits<32>
+ {
+ typedef PeHeader32 PeHeader32_64;
+ };
+
+ template<>
+ struct PeFile_Traits<64>
+ {
+ typedef PeHeader64 PeHeader32_64;
+ };
+
+ /**
+ * This class represents the common structures of PE and PE+ files.
+ **/
+ class PeFile
+ {
+ protected:
+ std::string m_filename; ///< Name of the current file.
+ MzHeader m_mzh; ///< MZ header of the current file.
+ ExportDirectory m_expdir; ///< Export directory of the current file.
+ BoundImportDirectory m_boundimpdir; ///< BoundImportDirectory of the current file.
+ ResourceDirectory m_resdir; ///< ResourceDirectory of the current file.
+ RelocationsDirectory m_relocs; ///< Relocations directory of the current file.
+ ComHeaderDirectory m_comdesc; ///< COM+ descriptor directory of the current file.
+ IatDirectory m_iat; ///< Import address table of the current file.
+ DebugDirectory m_debugdir;
+ public:
+ virtual ~PeFile();
+
+ /// Returns the name of the current file.
+ virtual std::string getFileName() const = 0; // EXPORT
+ /// Changes the name of the current file.
+ virtual void setFileName(std::string strFilename) = 0; // EXPORT
+
+ virtual void visit(PeFileVisitor &v) = 0;
+
+ /// Reads the MZ header of the current file from disc.
+ virtual int readMzHeader() = 0; // EXPORT
+ /// Reads the export directory of the current file from disc.
+ virtual int readExportDirectory() = 0; // EXPORT
+ /// Reads the PE header of the current file from disc.
+ virtual int readPeHeader() = 0; // EXPORT
+ /// Reads the import directory of the current file from disc.
+ virtual int readImportDirectory() = 0; // EXPORT
+ /// Reads the bound import directory of the current file from disc.
+ virtual int readBoundImportDirectory() = 0; // EXPORT
+ /// Reads the resource directory of the current file from disc.
+ virtual int readResourceDirectory() = 0; // EXPORT
+ /// Reads the relocations directory of the current file from disc.
+ virtual int readRelocationsDirectory() = 0; // EXPORT
+ /// Reads the COM+ descriptor directory of the current file from disc.
+ virtual int readComHeaderDirectory() = 0; // EXPORT
+ /// Reads the IAT directory of the current file from disc.
+ virtual int readIatDirectory() = 0; // EXPORT
+ /// Reads the Debug directory of the current file.
+ virtual int readDebugDirectory() = 0; // EXPORT
+ virtual int readTlsDirectory() = 0; // EXPORT
+
+ virtual unsigned int getBits() const = 0;
+
+ /// Accessor function for the MZ header.
+ const MzHeader& mzHeader() const;
+ /// Accessor function for the MZ header.
+ MzHeader& mzHeader(); // EXPORT
+
+ /// Accessor function for the export directory.
+ const ExportDirectory& expDir() const;
+ /// Accessor function for the export directory.
+ ExportDirectory& expDir(); // EXPORT
+
+ /// Accessor function for the bound import directory.
+ const BoundImportDirectory& boundImpDir() const;
+ /// Accessor function for the bound import directory.
+ BoundImportDirectory& boundImpDir(); // EXPORT
+
+ /// Accessor function for the resource directory.
+ const ResourceDirectory& resDir() const;
+ /// Accessor function for the resource directory.
+ ResourceDirectory& resDir(); // EXPORT
+
+ /// Accessor function for the relocations directory.
+ const RelocationsDirectory& relocDir() const;
+ /// Accessor function for the relocations directory.
+ RelocationsDirectory& relocDir(); // EXPORT
+
+ /// Accessor function for the COM+ descriptor directory.
+ const ComHeaderDirectory& comDir() const;
+ /// Accessor function for the COM+ descriptor directory.
+ ComHeaderDirectory& comDir(); // EXPORT
+
+ /// Accessor function for the IAT directory.
+ const IatDirectory& iatDir() const;
+ /// Accessor function for the IAT directory.
+ IatDirectory& iatDir(); // EXPORT
+
+ /// Accessor function for the debug directory.
+ const DebugDirectory& debugDir() const;
+ /// Accessor function for the debug directory.
+ DebugDirectory& debugDir(); // EXPORT
+
+ };
+
+ /**
+ * This class implements the common structures of PE and PE+ files.
+ **/
+ template<int bits>
+ class PeFileT : public PeFile
+ {
+ typedef typename PeFile_Traits<bits>::PeHeader32_64 PeHeader32_64;
+
+ private:
+ PeHeader32_64 m_peh; ///< PE header of the current file.
+ ImportDirectory<bits> m_impdir; ///< Import directory of the current file.
+ TlsDirectory<bits> m_tlsdir;
+
+ public:
+ /// Default constructor which exists only for the sake of allowing to construct files without filenames.
+ PeFileT();
+
+ virtual ~PeFileT() {}
+
+ /// Initializes a PeFile with a filename
+ explicit PeFileT(const std::string& strFilename);
+
+ /// Returns the name of the current file.
+ std::string getFileName() const;
+ /// Changes the name of the current file.
+ void setFileName(std::string strFilename);
+
+ /// Reads the MZ header of the current file from disc.
+ int readMzHeader() ;
+ /// Reads the export directory of the current file from disc.
+ int readExportDirectory() ;
+ /// Reads the PE header of the current file from disc.
+ int readPeHeader() ;
+ /// Reads the import directory of the current file from disc.
+ int readImportDirectory() ;
+ /// Reads the bound import directory of the current file from disc.
+ int readBoundImportDirectory() ;
+ /// Reads the resource directory of the current file from disc.
+ int readResourceDirectory() ;
+ /// Reads the relocations directory of the current file from disc.
+ int readRelocationsDirectory() ;
+ /// Reads the COM+ descriptor directory of the current file from disc.
+ int readComHeaderDirectory() ;
+ /// Reads the IAT directory of the current file from disc.
+ int readIatDirectory() ;
+ /// Reads the Debug directory of the current file.
+ int readDebugDirectory() ;
+ int readTlsDirectory() ;
+
+ unsigned int getBits() const
+ {
+ return bits;
+ }
+
+ /// Accessor function for the PE header.
+ const PeHeader32_64& peHeader() const;
+ /// Accessor function for the PE header.
+ PeHeader32_64& peHeader();
+
+ /// Accessor function for the import directory.
+ const ImportDirectory<bits>& impDir() const;
+ /// Accessor function for the import directory.
+ ImportDirectory<bits>& impDir();
+
+ const TlsDirectory<bits>& tlsDir() const;
+ TlsDirectory<bits>& tlsDir();
+ };
+
+ /**
+ * This class is the main class for handling PE files.
+ **/
+ class PeFile32 : public PeFileT<32>
+ {
+ public:
+ /// Default constructor which exists only for the sake of allowing to construct files without filenames.
+ PeFile32();
+
+ /// Initializes a PeFile with a filename
+ explicit PeFile32(const std::string& strFlename);
+ virtual void visit(PeFileVisitor &v) { v.callback( *this ); }
+ };
+
+ /**
+ * This class is the main class for handling PE+ files.
+ **/
+ class PeFile64 : public PeFileT<64>
+ {
+ public:
+ /// Default constructor which exists only for the sake of allowing to construct files without filenames.
+ PeFile64();
+
+ /// Initializes a PeFile with a filename
+ explicit PeFile64(const std::string& strFlename);
+ virtual void visit(PeFileVisitor &v) { v.callback( *this ); }
+ };
+
+ //typedef PeFileT<32> PeFile32;
+ //typedef PeFileT<64> PeFile64;
+
+ /**
+ * @param strFilename Name of the current file.
+ **/
+ template<int bits>
+ PeFileT<bits>::PeFileT(const std::string& strFilename)
+ {
+ m_filename = strFilename;
+ }
+
+ template<int bits>
+ PeFileT<bits>::PeFileT()
+ {
+ }
+
+ template<int bits>
+ int PeFileT<bits>::readPeHeader()
+ {
+ return peHeader().read(getFileName(), mzHeader().getAddressOfPeHeader());
+ }
+
+
+ template<int bits>
+ int PeFileT<bits>::readImportDirectory()
+ {
+ if (peHeader().calcNumberOfRvaAndSizes() >= 2
+ && peHeader().getIddImportRva()
+ && peHeader().getIddImportSize())
+ {
+ return impDir().read(getFileName(), static_cast<unsigned int>(peHeader().rvaToOffset(peHeader().getIddImportRva())), peHeader().getIddImportSize(), peHeader());
+ }
+ return ERROR_DIRECTORY_DOES_NOT_EXIST;
+ }
+
+ /**
+ * @return A reference to the file's PE header.
+ **/
+ template<int bits>
+ const typename PeFile_Traits<bits>::PeHeader32_64& PeFileT<bits>::peHeader() const
+ {
+ return m_peh;
+ }
+
+ /**
+ * @return A reference to the file's PE header.
+ **/
+ template<int bits>
+ typename PeFile_Traits<bits>::PeHeader32_64& PeFileT<bits>::peHeader()
+ {
+ return m_peh;
+ }
+
+ /**
+ * @return A reference to the file's import directory.
+ **/
+ template<int bits>
+ const ImportDirectory<bits>& PeFileT<bits>::impDir() const
+ {
+ return m_impdir;
+ }
+
+ /**
+ * @return A reference to the file's import directory.
+ **/
+ template<int bits>
+ ImportDirectory<bits>& PeFileT<bits>::impDir()
+ {
+ return m_impdir;
+ }
+
+ template<int bits>
+ const TlsDirectory<bits>& PeFileT<bits>::tlsDir() const
+ {
+ return m_tlsdir;
+ }
+
+ template<int bits>
+ TlsDirectory<bits>& PeFileT<bits>::tlsDir()
+ {
+ return m_tlsdir;
+ }
+
+ /**
+ * @return Filename of the current file.
+ **/
+ template<int bits>
+ std::string PeFileT<bits>::getFileName() const
+ {
+ return m_filename;
+ }
+
+ /**
+ * @param strFilename New filename.
+ **/
+ template<int bits>
+ void PeFileT<bits>::setFileName(std::string strFilename)
+ {
+ m_filename = strFilename;
+ }
+
+ template<int bits>
+ int PeFileT<bits>::readMzHeader()
+ {
+ return mzHeader().read(getFileName());
+ }
+
+ template<int bits>
+ int PeFileT<bits>::readExportDirectory()
+ {
+ if (peHeader().calcNumberOfRvaAndSizes() >= 1
+ && peHeader().getIddExportRva() && peHeader().getIddExportSize())
+ {
+ return expDir().read(getFileName(), static_cast<unsigned int>(peHeader().rvaToOffset(peHeader().getIddExportRva())), peHeader().getIddExportSize(), peHeader());
+ }
+ return ERROR_DIRECTORY_DOES_NOT_EXIST;
+ }
+
+
+ template<int bits>
+ int PeFileT<bits>::readBoundImportDirectory()
+ {
+ if (peHeader().calcNumberOfRvaAndSizes() >= 12
+ && peHeader().getIddBoundImportRva() && peHeader().getIddBoundImportSize())
+ {
+ return boundImpDir().read(getFileName(), static_cast<unsigned int>(peHeader().rvaToOffset(peHeader().getIddBoundImportRva())), peHeader().getIddBoundImportSize());
+ }
+ return ERROR_DIRECTORY_DOES_NOT_EXIST;
+ }
+
+ template<int bits>
+ int PeFileT<bits>::readResourceDirectory()
+ {
+ if (peHeader().calcNumberOfRvaAndSizes() >= 3
+ && peHeader().getIddResourceRva() && peHeader().getIddResourceSize())
+ {
+ return resDir().read(getFileName(), static_cast<unsigned int>(peHeader().rvaToOffset(peHeader().getIddResourceRva())), peHeader().getIddResourceSize(), peHeader().getIddResourceRva());
+ }
+ return ERROR_DIRECTORY_DOES_NOT_EXIST;
+ }
+
+ template<int bits>
+ int PeFileT<bits>::readRelocationsDirectory()
+ {
+ if (peHeader().calcNumberOfRvaAndSizes() >= 6
+ && peHeader().getIddBaseRelocRva() && peHeader().getIddBaseRelocSize())
+ {
+ return relocDir().read(getFileName(), static_cast<unsigned int>(peHeader().rvaToOffset(peHeader().getIddBaseRelocRva())), peHeader().getIddBaseRelocSize());
+ }
+ return ERROR_DIRECTORY_DOES_NOT_EXIST;
+ }
+
+ template<int bits>
+ int PeFileT<bits>::readComHeaderDirectory()
+ {
+ if (peHeader().calcNumberOfRvaAndSizes() >= 15
+ && peHeader().getIddComHeaderRva() && peHeader().getIddComHeaderSize())
+ {
+ return comDir().read(getFileName(), static_cast<unsigned int>(peHeader().rvaToOffset(peHeader().getIddComHeaderRva())), peHeader().getIddComHeaderSize());
+ }
+ std::cout << peHeader().getIddComHeaderRva() << std::endl;
+ std::exit(0);
+ return ERROR_DIRECTORY_DOES_NOT_EXIST;
+ }
+
+ template<int bits>
+ int PeFileT<bits>::readIatDirectory()
+ {
+ if (peHeader().calcNumberOfRvaAndSizes() >= 13
+ && peHeader().getIddIatRva() && peHeader().getIddIatSize())
+ {
+ return iatDir().read(getFileName(), static_cast<unsigned int>(peHeader().rvaToOffset(peHeader().getIddIatRva())), peHeader().getIddIatSize());
+ }
+ return ERROR_DIRECTORY_DOES_NOT_EXIST;
+ }
+
+ template<int bits>
+ int PeFileT<bits>::readDebugDirectory()
+ {
+ if (peHeader().calcNumberOfRvaAndSizes() >= 7
+ && peHeader().getIddDebugRva() && peHeader().getIddDebugSize())
+ {
+ return debugDir().read(getFileName(), static_cast<unsigned int>(peHeader().rvaToOffset(peHeader().getIddDebugRva())), peHeader().getIddDebugSize());
+ }
+ return ERROR_DIRECTORY_DOES_NOT_EXIST;
+ }
+
+ template<int bits>
+ int PeFileT<bits>::readTlsDirectory()
+ {
+ if (peHeader().calcNumberOfRvaAndSizes() >= 10
+ && peHeader().getIddTlsRva() && peHeader().getIddTlsSize())
+ {
+ return tlsDir().read(getFileName(), static_cast<unsigned int>(peHeader().rvaToOffset(peHeader().getIddTlsRva())), peHeader().getIddTlsSize());
+ }
+ return ERROR_DIRECTORY_DOES_NOT_EXIST;
+ }
+
+}
+
+#endif
diff --git a/utils/zenutils/libraries/pelib-0.9/pelib/PeHeader.cpp b/utils/zenutils/libraries/pelib-0.9/pelib/PeHeader.cpp
index fe7011072c..1199e6bdd1 100755..100644
--- a/utils/zenutils/libraries/pelib-0.9/pelib/PeHeader.cpp
+++ b/utils/zenutils/libraries/pelib-0.9/pelib/PeHeader.cpp
@@ -1,90 +1,90 @@
-/*
-* PeHeader.cpp - Part of the PeLib library.
-*
-* Copyright (c) 2004 - 2005 Sebastian Porst (webmaster@the-interweb.com)
-* All rights reserved.
-*
-* This software is licensed under the zlib/libpng License.
-* For more details see http://www.opensource.org/licenses/zlib-license.php
-* or the license information file (license.htm) in the root directory
-* of PeLib.
-*/
-
-#include "PeLibInc.h"
-#include "PeHeader.h"
-
-namespace PeLib
-{
- template<>
- void PeHeaderT<32>::readBaseOfData(InputBuffer& ibBuffer, PELIB_IMAGE_NT_HEADERS<32>& header) const
- {
- ibBuffer >> header.OptionalHeader.BaseOfData;
- }
-
- template<>
- void PeHeaderT<64>::readBaseOfData(InputBuffer&, PELIB_IMAGE_NT_HEADERS<64>&) const
- {
- }
-
- template<>
- void PeHeaderT<32>::rebuildBaseOfData(OutputBuffer& obBuffer) const
- {
- obBuffer << m_inthHeader.OptionalHeader.BaseOfData;
- }
-
- template<>
- void PeHeaderT<64>::rebuildBaseOfData(OutputBuffer&) const
- {
- }
-
- template<>
- bool PeHeaderT<32>::isValid() const
- {
- return true;
- }
-
- template<>
- bool PeHeaderT<64>::isValid() const
- {
- return true;
- }
-
- template<>
- bool PeHeaderT<32>::isValid(unsigned int pehf) const
- {
- /*
- if (pehf == NtSignature)
- {
- return m_inthHeader.Signature == IMAGE_NT_SIGNATURE;
- }
- else if (pehf == NumberOfSections)
- {
- return getNumberOfSections() == calcNumberOfSections();
- } */
- return false;
- }
-
- template<>
- bool PeHeaderT<64>::isValid(unsigned int pehf) const
- {
- return false;
- }
-
- /**
- * @return The BaseOfData value from the PE header.
- **/
- dword PeHeader32::getBaseOfData() const
- {
- return m_inthHeader.OptionalHeader.BaseOfData;
- }
-
- /**
- * Changes the file's BaseOfData.
- * @param dwValue New value.
- **/
- void PeHeader32::setBaseOfData(dword dwValue)
- {
- m_inthHeader.OptionalHeader.BaseOfData = dwValue;
- }
-
-}
+/*
+* PeHeader.cpp - Part of the PeLib library.
+*
+* Copyright (c) 2004 - 2005 Sebastian Porst (webmaster@the-interweb.com)
+* All rights reserved.
+*
+* This software is licensed under the zlib/libpng License.
+* For more details see http://www.opensource.org/licenses/zlib-license.php
+* or the license information file (license.htm) in the root directory
+* of PeLib.
+*/
+
+#include "PeLibInc.h"
+#include "PeHeader.h"
+
+namespace PeLib
+{
+ template<>
+ void PeHeaderT<32>::readBaseOfData(InputBuffer& ibBuffer, PELIB_IMAGE_NT_HEADERS<32>& header) const
+ {
+ ibBuffer >> header.OptionalHeader.BaseOfData;
+ }
+
+ template<>
+ void PeHeaderT<64>::readBaseOfData(InputBuffer&, PELIB_IMAGE_NT_HEADERS<64>&) const
+ {
+ }
+
+ template<>
+ void PeHeaderT<32>::rebuildBaseOfData(OutputBuffer& obBuffer) const
+ {
+ obBuffer << m_inthHeader.OptionalHeader.BaseOfData;
+ }
+
+ template<>
+ void PeHeaderT<64>::rebuildBaseOfData(OutputBuffer&) const
+ {
+ }
+
+ template<>
+ bool PeHeaderT<32>::isValid() const
+ {
+ return true;
+ }
+
+ template<>
+ bool PeHeaderT<64>::isValid() const
+ {
+ return true;
+ }
+
+ template<>
+ bool PeHeaderT<32>::isValid(unsigned int pehf) const
+ {
+ /*
+ if (pehf == NtSignature)
+ {
+ return m_inthHeader.Signature == IMAGE_NT_SIGNATURE;
+ }
+ else if (pehf == NumberOfSections)
+ {
+ return getNumberOfSections() == calcNumberOfSections();
+ } */
+ return false;
+ }
+
+ template<>
+ bool PeHeaderT<64>::isValid(unsigned int pehf) const
+ {
+ return false;
+ }
+
+ /**
+ * @return The BaseOfData value from the PE header.
+ **/
+ dword PeHeader32::getBaseOfData() const
+ {
+ return m_inthHeader.OptionalHeader.BaseOfData;
+ }
+
+ /**
+ * Changes the file's BaseOfData.
+ * @param dwValue New value.
+ **/
+ void PeHeader32::setBaseOfData(dword dwValue)
+ {
+ m_inthHeader.OptionalHeader.BaseOfData = dwValue;
+ }
+
+}
diff --git a/utils/zenutils/libraries/pelib-0.9/pelib/PeHeader.h b/utils/zenutils/libraries/pelib-0.9/pelib/PeHeader.h
index 08eaca4072..d5c1748233 100755..100644
--- a/utils/zenutils/libraries/pelib-0.9/pelib/PeHeader.h
+++ b/utils/zenutils/libraries/pelib-0.9/pelib/PeHeader.h
@@ -1,2685 +1,2685 @@
-/*
-* PeHeader.h - Part of the PeLib library.
-*
-* Copyright (c) 2004 - 2005 Sebastian Porst (webmaster@the-interweb.com)
-* All rights reserved.
-*
-* This software is licensed under the zlib/libpng License.
-* For more details see http://www.opensource.org/licenses/zlib-license.php
-* or the license information file (license.htm) in the root directory
-* of PeLib.
-*/
-
-#ifndef PEHEADER_H
-#define PEHEADER_H
-
-#include "PeLibAux.h"
-
-namespace PeLib
-{
- class PeHeader
- {
-// protected:
-// virtual void readBaseOfData(InputBuffer& ibBuffer) = 0;
-// virtual void rebuildBaseOfData(OutputBuffer& obBuffer) const = 0;
-
- public:
- virtual ~PeHeader(){};
- };
-
- /// Class that handles the PE header of files.
- /**
- * This class can read and modify PE headers. It provides set- and get functions to access
- * all individual members of a PE header. Furthermore it's possible to validate and rebuild
- * PE headers. A PE header includes the IMAGE_Nt_HEADERS and the section definitions of a PE file.
- * \todo getIdReservedRva
- **/
- template<int x>
- class PeHeaderT : public PeHeader
- {
- private:
- void readBaseOfData(InputBuffer& ibBuffer, PELIB_IMAGE_NT_HEADERS<x>& header) const;
- void rebuildBaseOfData(OutputBuffer& obBuffer) const;
-
- protected:
- std::vector<PELIB_IMAGE_SECTION_HEADER> m_vIsh; ///< Stores section header information.
- PELIB_IMAGE_NT_HEADERS<x> m_inthHeader; ///< Stores Nt header information.
- dword m_uiOffset; ///< Equivalent to the value returned by #PeLib::MzHeader::getAddressOfPeFile
-
- public:
- typedef typename FieldSizes<x>::VAR4_8 VAR4_8;
-
- PeHeaderT() : m_uiOffset(0)
- {
- }
-
- /// Add a section to the header.
- int addSection(const std::string& strName, dword dwSize); // EXPORT
-
- unsigned int calcSizeOfImage() const; // EXPORT
-
- /// Returns the unused space after the header.
- unsigned int calcSpaceAfterHeader() const; // EXPORT
-
- /// Returns the address of the physically first section (not the first defined section).
- unsigned int calcStartOfCode() const; // EXPORT
-
- /// Calculates the offset for a new section of size uiSize.
- unsigned int calcOffset() const; // EXPORT
-
- /// Calculates the Rva for a new section of size uiSize.
- unsigned int calcRva() const; // EXPORT
-
- /// Returns the number of sections in the current file.
- word calcNumberOfSections() const; // EXPORT
-
- void enlargeLastSection(unsigned int uiSize); // EXPORT
-
- /// Returns the section Id of the section that contains the offset.
- word getSectionWithOffset(VAR4_8 dwOffset) const; // EXPORT
-
- /// Returns the number of the section which the given relative address points to.
- word getSectionWithRva(VAR4_8 rva) const; // EXPORT
-
- bool isValid() const; // EXPORT
- bool isValid(unsigned int foo) const; // EXPORT
-
- /// Corrects the current PE header.
- void makeValid(dword dwOffset); // EXPORT
-
- /// Converts a file offset to a relative virtual offset.
- unsigned int offsetToRva(VAR4_8 dwOffset) const; // EXPORT
-
- /// Converts a file offset to a virtual address.
- unsigned int offsetToVa(VAR4_8 dwOffset) const; // EXPORT
-
- /// Reads the PE header of a file.
- int read(std::string strFilename, unsigned int uiOffset); // EXPORT
-
- int read(const unsigned char* pcBuffer, unsigned int uiSize, unsigned int uiOffset); // EXPORT
-
- void readHeader(InputBuffer& ibBuffer, PELIB_IMAGE_NT_HEADERS<x>& header) const;
- void readDataDirectories(InputBuffer& ibBuffer, PELIB_IMAGE_NT_HEADERS<x>& header) const;
- std::vector<PELIB_IMAGE_SECTION_HEADER> readSections(InputBuffer& ibBuffer, PELIB_IMAGE_NT_HEADERS<x>& header) const;
-
- /// Rebuilds the current PE header.
- void rebuild(std::vector<byte>& vBuffer) const; // EXPORT
-
- /// Converts a relative virtual address to a file offset.
- VAR4_8 rvaToOffset(VAR4_8 dwRva) const; // EXPORT
-
- /// Converts a relative virtual address to a virtual address.
- VAR4_8 rvaToVa(VAR4_8 dwRva) const; // EXPORT
-
- /// Calculates the size for the current PE header including all section definitions.
- unsigned int size() const;
-
- VAR4_8 vaToRva(VAR4_8 dwRva) const; // EXPORT
- VAR4_8 vaToOffset(VAR4_8 dwRva) const; // EXPORT
-
- /// Save the PE header to a file.
- int write(std::string strFilename, unsigned int uiOffset) const; // EXPORT
-
- /// Writes sections to a file.
- int writeSections(const std::string& strFilename) const; // EXPORT
- /// Overwrites a section with new data.
- int writeSectionData(const std::string& strFilename, word wSecnr, const std::vector<byte>& vBuffer) const; // EXPORT
-
-// header getters
- /// Returns the Signature value of the header.
- dword getNtSignature() const; // EXPORT
- /// Returns the Machine value of the header.
- word getMachine() const; // EXPORT
- /// Returns the Sections value of the header.
- word getNumberOfSections() const; // EXPORT
- /// Returns the TimeDateStamp value of the header.
- dword getTimeDateStamp() const; // EXPORT
- /// Returns the PointerToSymbolTable value of the header.
- dword getPointerToSymbolTable() const; // EXPORT
- /// Returns the NumberOfSymbols value of the header.
- dword getNumberOfSymbols() const; // EXPORT
- /// Returns the SizeOfOptionalHeader value of the header.
- word getSizeOfOptionalHeader() const; // EXPORT
- /// Returns the Characteristics value of the header.
- word getCharacteristics() const; // EXPORT
-
- /// Returns the Magic value of the header.
- word getMagic() const; // EXPORT
- /// Returns the MajorLinkerVersion value of the header.
- byte getMajorLinkerVersion() const; // EXPORT
- /// Returns the MinorLinkerVersion value of the header.
- byte getMinorLinkerVersion() const; // EXPORT
- /// Returns the SizeOfCode value of the header.
- dword getSizeOfCode() const; // EXPORT
- /// Returns the SizeOfInitializedData value of the header.
- dword getSizeOfInitializedData() const; // EXPORT
- /// Returns the SizeOfUninitializedData value of the header.
- dword getSizeOfUninitializedData() const; // EXPORT
- /// Returns the AddressOfEntryPoint value of the header.
- dword getAddressOfEntryPoint() const; // EXPORT
- /// Returns the BaseOfCode value of the header.
- dword getBaseOfCode() const; // EXPORT
- /// Returns the ImageBase value of the header.
- VAR4_8 getImageBase() const; // EXPORT
- /// Returns the SectionAlignment value of the header.
- dword getSectionAlignment() const; // EXPORT
- /// Returns the FileAlignment value of the header.
- dword getFileAlignment() const; // EXPORT
- /// Returns the MajorOperatingSystemVersion value of the header.
- word getMajorOperatingSystemVersion() const; // EXPORT
- /// Returns the MinorOperatingSystemVersion value of the header.
- word getMinorOperatingSystemVersion() const; // EXPORT
- /// Returns the MajorImageVersion value of the header.
- word getMajorImageVersion() const; // EXPORT
- /// Returns the MinorImageVersion value of the header.
- word getMinorImageVersion() const; // EXPORT
- /// Returns the MajorSubsystemVersion value of the header.
- word getMajorSubsystemVersion() const; // EXPORT
- /// Returns the MinorSubsystemVersion value of the header.
- word getMinorSubsystemVersion() const; // EXPORT
- /// Returns the Reserved1 value of the header.
- dword getWin32VersionValue() const; // EXPORT
- /// Returns the SizeOfImage value of the header.
- dword getSizeOfImage() const; // EXPORT
- /// Returns the SizeOfHeaders value of the header.
- dword getSizeOfHeaders() const; // EXPORT
- /// Returns the CheckSum value of the header.
- dword getCheckSum() const; // EXPORT
- /// Returns the Subsystem value of the header.
- word getSubsystem() const; // EXPORT
- /// Returns the DllCharacteristics value of the header.
- word getDllCharacteristics() const; // EXPORT
- /// Returns the SizeOfStackReserve value of the header.
- VAR4_8 getSizeOfStackReserve() const; // EXPORT
- /// Returns the SizeOfStackCommit value of the header.
- VAR4_8 getSizeOfStackCommit() const; // EXPORT
- /// Returns the SizeOfHeapReserve value of the header.
- VAR4_8 getSizeOfHeapReserve() const; // EXPORT
- /// Returns the SizeOfHeapCommit value of the header.
- VAR4_8 getSizeOfHeapCommit() const; // EXPORT
- /// Returns the LoaderFlags value of the header.
- dword getLoaderFlags() const; // EXPORT
- /// Returns the NumberOfRvaAndSizes value of the header.
- dword getNumberOfRvaAndSizes() const; // EXPORT
- dword calcNumberOfRvaAndSizes() const; // EXPORT
-
- void addDataDirectory(); // EXPORT
- void removeDataDirectory(dword index); // EXPORT
-
-// image directory getters
- /// Returns the relative virtual address of the image directory Export.
- dword getIddExportRva() const; // EXPORT
- /// Returns the size of the image directory Export.
- dword getIddExportSize() const; // EXPORT
- /// Returns the relative virtual address of the image directory Import.
- dword getIddImportRva() const; // EXPORT
- /// Returns the size of the image directory Import.
- dword getIddImportSize() const; // EXPORT
- /// Returns the relative virtual address of the image directory Resource.
- dword getIddResourceRva() const; // EXPORT
- /// Returns the size of the image directory Resource.
- dword getIddResourceSize() const; // EXPORT
- /// Returns the relative virtual address of the image directory Exception.
- dword getIddExceptionRva() const; // EXPORT
- /// Returns the size of the image directory Exception.
- dword getIddExceptionSize() const; // EXPORT
- /// Returns the relative virtual address of the image directory Security.
- dword getIddSecurityRva() const; // EXPORT
- /// Returns the size of the image directory Security.
- dword getIddSecuritySize() const; // EXPORT
- /// Returns the relative virtual address of the image directory Base Reloc.
- dword getIddBaseRelocRva() const; // EXPORT
- /// Returns the size of the image directory Base Reloc.
- dword getIddBaseRelocSize() const; // EXPORT
- /// Returns the relative virtual address of the image directory Debug.
- dword getIddDebugRva() const; // EXPORT
- /// Returns the size of the image directory Debug.
- dword getIddDebugSize() const; // EXPORT
- /// Returns the relative virtual address of the image directory Architecture.
- dword getIddArchitectureRva() const; // EXPORT
- /// Returns the size of the image directory Architecture.
- dword getIddArchitectureSize() const; // EXPORT
- /// Returns the relative virtual address of the image directory GlobalPtr.
- dword getIddGlobalPtrRva() const; // EXPORT
- /// Returns the size of the image directory GlobalPtr.
- dword getIddGlobalPtrSize() const; // EXPORT
- /// Returns the relative virtual address of the image directory Tls.
- dword getIddTlsRva() const; // EXPORT
- /// Returns the size of the image directory Tls.
- dword getIddTlsSize() const; // EXPORT
- /// Returns the relative virtual address of the image directory LoadConfig.
- dword getIddLoadConfigRva() const; // EXPORT
- /// Returns the size of the image directory LoadConfig.
- dword getIddLoadConfigSize() const; // EXPORT
- /// Returns the relative virtual address of the image directory BoundImport.
- dword getIddBoundImportRva() const; // EXPORT
- /// Returns the size of the image directory BoundImport.
- dword getIddBoundImportSize() const; // EXPORT
- /// Returns the relative virtual address of the image directory Iat.
- dword getIddIatRva() const; // EXPORT
- /// Returns the size of the image directory Iat.
- dword getIddIatSize() const; // EXPORT
- /// Returns the relative virtual address of the image directory DelayImport.
- dword getIddDelayImportRva() const; // EXPORT
- /// Returns the size of the image directory DelayImport.
- dword getIddDelayImportSize() const; // EXPORT
- /// Returns the relative virtual address of the image directory COM Descriptor.
- dword getIddComHeaderRva() const; // EXPORT
- /// Returns the size of the image directory COM Descriptor.
- dword getIddComHeaderSize() const; // EXPORT
-
- /// Returns the relative virtual address of an image directory.
- dword getImageDataDirectoryRva(dword dwDirectory) const; // EXPORT
- /// Returns the size of an image directory.
- dword getImageDataDirectorySize(dword dwDirectory) const; // EXPORT
-
- void setImageDataDirectoryRva(dword dwDirectory, dword value); // EXPORT
- void setImageDataDirectorySize(dword dwDirectory, dword value); // EXPORT
-
-// section getters
- /// Returns the name of a section.
- std::string getSectionName(word uiSectionnr) const; // EXPORT
- /// Returns the virtual size of a section.
- dword getVirtualSize(word uiSectionnr) const; // EXPORT
- /// Returns the virtual address of a section.
- dword getVirtualAddress(word uiSectionnr) const; // EXPORT
- /// Returns the size of a section's raw data.
- dword getSizeOfRawData(word uiSectionnr) const; // EXPORT
- /// Returns file offset of the data of a section.
- dword getPointerToRawData(word uiSectionnr) const; // EXPORT
- /// Returns the rva of the relocations of a section.
- dword getPointerToRelocations(word uiSectionnr) const; // EXPORT
- /// Returns the rva of the line numbers of a section.
- dword getPointerToLinenumbers(word uiSectionnr) const; // EXPORT
- /// Returns the number of relocations of a section.
- dword getNumberOfRelocations(word uiSectionnr) const; // EXPORT
- /// Returns the number of line numbers of a section.
- dword getNumberOfLinenumbers(word uiSectionnr) const; // EXPORT
- /// Returns the characteristics of a section.
- dword getCharacteristics(word uiSectionnr) const; // EXPORT _section
-
-// header setters
- /// Sets the Signature value of the header.
- void setNtSignature(dword value); // EXPORT
- /// Sets the Machine value of the header.
- void setMachine(word value); // EXPORT
- /// Sets the Sections value of the header.
- void setNumberOfSections(word value); // EXPORT
- /// Sets the TimeDateStamp value of the header.
- void setTimeDateStamp(dword value); // EXPORT
- /// Sets the PointerToSymbolTable value of the header.
- void setPointerToSymbolTable(dword value); // EXPORT
- /// Sets the NumberOfSymbols value of the header.
- void setNumberOfSymbols(dword value); // EXPORT
- /// Sets the SizeOfOptionalHeader value of the header.
- void setSizeOfOptionalHeader(word value); // EXPORT
- /// Sets the Characteristics value of the header.
- void setCharacteristics(word value); // EXPORT _section
-
- /// Sets the Magic value of the header.
- void setMagic(word value); // EXPORT
- /// Sets the MajorLinkerVersion value of the header.
- void setMajorLinkerVersion(byte value); // EXPORT
- /// Sets the MinorLinkerVersion value of the header.
- void setMinorLinkerVersion(byte value); // EXPORT
- /// Sets the SizeOfCode value of the header.
- void setSizeOfCode(dword value); // EXPORT
- /// Sets the SizeOfInitializedData value of the header.
- void setSizeOfInitializedData(dword value); // EXPORT
- /// Sets the SizeOfUninitializedData value of the header.
- void setSizeOfUninitializedData(dword value); // EXPORT
- /// Sets the AddressOfEntryPoint value of the header.
- void setAddressOfEntryPoint(dword value); // EXPORT
- /// Sets the BaseOfCode value of the header.
- void setBaseOfCode(dword value); // EXPORT
- /// Sets the ImageBase value of the header.
- void setImageBase(VAR4_8 value); // EXPORT
- /// Sets the SectionAlignment value of the header.
- void setSectionAlignment(dword value); // EXPORT
- /// Sets the FileAlignment value of the header.
- void setFileAlignment(dword value); // EXPORT
- /// Sets the MajorOperatingSystemVersion value of the header.
- void setMajorOperatingSystemVersion(word value); // EXPORT
- /// Sets the MinorOperatingSystemVersion value of the header.
- void setMinorOperatingSystemVersion(word value); // EXPORT
- /// Sets the MajorImageVersion value of the header.
- void setMajorImageVersion(word value); // EXPORT
- /// Sets the MinorImageVersion value of the header.
- void setMinorImageVersion(word value); // EXPORT
- /// Sets the MajorSubsystemVersion value of the header.
- void setMajorSubsystemVersion(word value); // EXPORT
- /// Sets the MinorSubsystemVersion value of the header.
- void setMinorSubsystemVersion(word value); // EXPORT
- /// Sets the Reserved1 value of the header.
- void setWin32VersionValue(dword value); // EXPORT
- /// Sets the SizeOfImage value of the header.
- void setSizeOfImage(dword value); // EXPORT
- /// Sets the SizeOfHeaders value of the header.
- void setSizeOfHeaders(dword value); // EXPORT
- /// Sets the CheckSum value of the header.
- void setCheckSum(dword value); // EXPORT
- /// Sets the Subsystem value of the header.
- void setSubsystem(word value); // EXPORT
- /// Sets the DllCharacteristics value of the header.
- void setDllCharacteristics(word value); // EXPORT
- /// Sets the SizeOfStackReserve value of the header.
- void setSizeOfStackReserve(VAR4_8 value); // EXPORT
- /// Sets the SizeOfStackCommit value of the header.
- void setSizeOfStackCommit(VAR4_8 value); // EXPORT
- /// Sets the SizeOfHeapReserve value of the header.
- void setSizeOfHeapReserve(VAR4_8 value); // EXPORT
- /// Sets the SizeOfHeapCommit value of the header.
- void setSizeOfHeapCommit(VAR4_8 value); // EXPORT
- /// Sets the LoaderFlags value of the header.
- void setLoaderFlags(dword value); // EXPORT
- /// Sets the NumberOfRvaAndSizes value of the header.
- void setNumberOfRvaAndSizes(dword value); // EXPORT
-
-// image directory getters
- void setIddDebugRva(dword dwValue); // EXPORT
- void setIddDebugSize(dword dwValue); // EXPORT
- void setIddDelayImportRva(dword dwValue); // EXPORT
- void setIddDelayImportSize(dword dwValue); // EXPORT
- void setIddExceptionRva(dword dwValue); // EXPORT
- void setIddExceptionSize(dword dwValue); // EXPORT
- void setIddGlobalPtrRva(dword dwValue); // EXPORT
- void setIddGlobalPtrSize(dword dwValue); // EXPORT
- void setIddIatRva(dword dwValue); // EXPORT
- void setIddIatSize(dword dwValue); // EXPORT
- void setIddLoadConfigRva(dword dwValue); // EXPORT
- void setIddLoadConfigSize(dword dwValue); // EXPORT
- void setIddResourceRva(dword dwValue); // EXPORT
- void setIddResourceSize(dword dwValue); // EXPORT
- void setIddSecurityRva(dword dwValue); // EXPORT
- void setIddSecuritySize(dword dwValue); // EXPORT
- void setIddTlsRva(dword dwValue); // EXPORT
- void setIddTlsSize(dword dwValue); // EXPORT
-
- void setIddImportRva(dword dwValue); // EXPORT
- void setIddImportSize(dword dwValue); // EXPORT
- void setIddExportRva(dword dwValue); // EXPORT
- void setIddExportSize(dword dwValue); // EXPORT
-
- void setIddBaseRelocRva(dword value); // EXPORT
- void setIddBaseRelocSize(dword value); // EXPORT
- void setIddArchitectureRva(dword value); // EXPORT
- void setIddArchitectureSize(dword value); // EXPORT
- void setIddComHeaderRva(dword value); // EXPORT
- void setIddComHeaderSize(dword value); // EXPORT
-
- /// Set the name of a section.
- void setSectionName(word uiSectionnr, std::string strName); // EXPORT
- /// Set the virtual size of a section.
- void setVirtualSize(word uiSectionnr, dword dwValue); // EXPORT
- /// Set the virtual address of a section.
- void setVirtualAddress(word uiSectionnr, dword dwValue); // EXPORT
- /// Set the size of raw data of a section.
- void setSizeOfRawData(word uiSectionnr, dword dwValue); // EXPORT
- /// Set the file offset of a section.
- void setPointerToRawData(word uiSectionnr, dword dwValue); // EXPORT
- /// Set the pointer to relocations of a section.
- void setPointerToRelocations(word uiSectionnr, dword dwValue); // EXPORT
- /// Set the pointer to linenumbers of a section.
- void setPointerToLinenumbers(word uiSectionnr, dword dwValue); // EXPORT
- /// Set the number of relocations a section.
- void setNumberOfRelocations(word uiSectionnr, dword dwValue); // EXPORT
- /// Set the number of linenumbers section.
- void setNumberOfLinenumbers(word uiSectionnr, dword dwValue); // EXPORT
- /// Set the characteristics of a section.
- void setCharacteristics(word uiSectionnr, dword dwValue); // EXPORT
- };
-
- class PeHeader32 : public PeHeaderT<32>
- {
- public:
- /// Returns the BaseOfData value of the header.
- dword getBaseOfData() const; // EXPORT
- /// Sets the BaseOfData value of the header.
- void setBaseOfData(dword value); // EXPORT
- };
-
- class PeHeader64 : public PeHeaderT<64>
- {
- };
-
- template<int x>
- void PeHeaderT<x>::addDataDirectory()
- {
- m_inthHeader.dataDirectories.push_back(PELIB_IMAGE_DATA_DIRECTORY());
- }
-
- template<int x>
- void PeHeaderT<x>::removeDataDirectory(dword index)
- {
- m_inthHeader.dataDirectories.erase(m_inthHeader.dataDirectories.begin() + index);
- }
-
- /**
- * Adds a new section to the header. The physical and virtual address as well as the virtual
- * size of the section will be determined automatically from the raw size. The section
- * characteristics will be set to IMAGE_SCN_MEM_WRITE | IMAGE_SCN_MEM_READ |
- * IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_CNT_CODE. All other values will be set to 0.
- * Note: It's important that if the current header's FileAlignment and/or SectionAlignment values are
- * 0 this function will fail.
- * @param strName Name of the new section. If this name is longer than 8 bytes only the first 8 bytes will be used.
- * @param dwSize Physical size of the new section in bytes.
- * \todo Better code that handles files with 0 sections.
- **/
- template<int x>
- int PeHeaderT<x>::addSection(const std::string& strName, dword dwSize)
- {
- unsigned int uiSecnr = calcNumberOfSections();
-
- if (!getFileAlignment())
- {
- return ERROR_NO_FILE_ALIGNMENT;
- }
- else if (!getSectionAlignment())
- {
- return ERROR_NO_SECTION_ALIGNMENT;
- }
-
- if (uiSecnr) // Always allow 1 section.
- {
- if (uiSecnr == 0xFFFF)
- {
- return ERROR_TOO_MANY_SECTIONS;
- }
- else if (calcSpaceAfterHeader() < PELIB_IMAGE_SECTION_HEADER::size())
- {
- return ERROR_NOT_ENOUGH_SPACE;
- }
- }
-
- dword dwOffset = calcOffset(/*dwSize*/);
- dword dwRva = calcRva(/*dwSize*/);
-
- PELIB_IMAGE_SECTION_HEADER ishdCurr;
- m_vIsh.push_back(ishdCurr);
-
- setSectionName(uiSecnr, strName);
- setSizeOfRawData(uiSecnr, alignOffset(dwSize, getFileAlignment()));
- setPointerToRawData(uiSecnr, dwOffset);
- setVirtualSize(uiSecnr, alignOffset(dwSize, getSectionAlignment()));
- setVirtualAddress(uiSecnr, dwRva);
- setCharacteristics(uiSecnr, PELIB_IMAGE_SCN_MEM_WRITE | PELIB_IMAGE_SCN_MEM_READ | PELIB_IMAGE_SCN_CNT_INITIALIZED_DATA | PELIB_IMAGE_SCN_CNT_CODE);
-
- return NO_ERROR;
- }
-
- /**
- * Calculates a valid SizeOfImage value given the information from the current PE header.
- * Note that this calculation works in Win2K but probably does not work in Win9X. I didn't test that though.
- * @return Valid SizeOfImage value.
- **/
- template<int x>
- unsigned int PeHeaderT<x>::calcSizeOfImage() const
- {
- // Major note here: It's possible for sections to exist with a Virtual Size of 0.
- // That's why it's necessary to use std::max(Vsize, RawSize) here.
- // An example for such a file is dbeng6.exe (made by Sybase).
- // In this file each and every section has a VSize of 0 but it still runs.
-
- std::vector<PELIB_IMAGE_SECTION_HEADER>::const_iterator ishLastSection = std::max_element(m_vIsh.begin(), m_vIsh.end(), std::mem_fun_ref(&PELIB_IMAGE_SECTION_HEADER::biggerVirtualAddress));
- if (ishLastSection->VirtualSize != 0) return ishLastSection->VirtualAddress + ishLastSection->VirtualSize;
- return ishLastSection->VirtualAddress + std::max(ishLastSection->VirtualSize, ishLastSection->SizeOfRawData);
- }
-
- /**
- * Calculates the space between the last byte of the header and the first byte that's used for something
- * else (that's either the first section or an image directory).
- * @return Unused space after the header.
- * \todo There are PE files with sections beginning at offset 0. They
- * need to be considered.
- **/
- template<int x>
- unsigned int PeHeaderT<x>::calcSpaceAfterHeader() const
- {
- return (calcStartOfCode() > size() + m_uiOffset) ? calcStartOfCode() - (size() + m_uiOffset) : 0;
- }
-
- /**
- * Returns the first offset of the file that's actually used for something different than the header.
- * That something is not necessarily code, it can be a data directory too.
- * This offset can be the beginning of a section or the beginning of a directory.
- * \todo Some optimizization is surely possible here.
- * \todo There are PE files with sections beginning at offset 0. They
- * need to be considered. Returning 0 for these files doesn't really make sense.
- * So far these sections are disregarded.
- **/
- template<int x>
- unsigned int PeHeaderT<x>::calcStartOfCode() const
- {
- unsigned int directories = calcNumberOfRvaAndSizes();
- dword dwMinOffset = 0xFFFFFFFF;
- if (directories >= 1 && getIddExportRva() && rvaToOffset(getIddExportRva()) < dwMinOffset) dwMinOffset = rvaToOffset(getIddExportRva());
- if (directories >= 2 && getIddImportRva() && rvaToOffset(getIddImportRva()) < dwMinOffset) dwMinOffset = rvaToOffset(getIddImportRva());
- if (directories >= 3 && getIddResourceRva() && rvaToOffset(getIddResourceRva()) < dwMinOffset) dwMinOffset = rvaToOffset(getIddResourceRva());
- if (directories >= 4 && getIddExceptionRva() && rvaToOffset(getIddExceptionRva()) < dwMinOffset) dwMinOffset = rvaToOffset(getIddExceptionRva());
- if (directories >= 5 && getIddSecurityRva() && rvaToOffset(getIddSecurityRva()) < dwMinOffset) dwMinOffset = rvaToOffset(getIddSecurityRva());
- if (directories >= 6 && getIddBaseRelocRva() && rvaToOffset(getIddBaseRelocRva()) < dwMinOffset) dwMinOffset = rvaToOffset(getIddBaseRelocRva());
- if (directories >= 7 && getIddDebugRva() && rvaToOffset(getIddDebugRva()) < dwMinOffset) dwMinOffset = rvaToOffset(getIddDebugRva());
- if (directories >= 8 && getIddArchitectureRva() && rvaToOffset(getIddArchitectureRva()) < dwMinOffset) dwMinOffset = rvaToOffset(getIddArchitectureRva());
- if (directories >= 9 && getIddGlobalPtrRva() && rvaToOffset(getIddGlobalPtrRva()) < dwMinOffset) dwMinOffset = rvaToOffset(getIddGlobalPtrRva());
- if (directories >= 10 && getIddTlsRva() && rvaToOffset(getIddTlsRva()) < dwMinOffset) dwMinOffset = rvaToOffset(getIddTlsRva());
- if (directories >= 11 && getIddLoadConfigRva() && rvaToOffset(getIddLoadConfigRva()) < dwMinOffset) dwMinOffset = rvaToOffset(getIddLoadConfigRva());
- if (directories >= 12 && getIddBoundImportRva() && rvaToOffset(getIddBoundImportRva()) < dwMinOffset) dwMinOffset = rvaToOffset(getIddBoundImportRva());
- if (directories >= 13 && getIddIatRva() && rvaToOffset(getIddIatRva()) < dwMinOffset) dwMinOffset = rvaToOffset(getIddIatRva());
- if (directories >= 14 && getIddDelayImportRva() && rvaToOffset(getIddDelayImportRva()) < dwMinOffset) dwMinOffset = rvaToOffset(getIddDelayImportRva());
- if (directories >= 15 && getIddComHeaderRva() && rvaToOffset(getIddComHeaderRva()) < dwMinOffset) dwMinOffset = rvaToOffset(getIddComHeaderRva());
-
- for (word i=0;i<calcNumberOfSections();i++)
- {
- if ((getPointerToRawData(i) < dwMinOffset || dwMinOffset == 0xFFFFFFFF) && getSizeOfRawData(i))
- {
- if (getPointerToRawData(i)) dwMinOffset = getPointerToRawData(i);
- }
- }
- return dwMinOffset;
- }
-
- /**
- * Calculates the file offset for a new section. The file offset will already be aligned to the file's FileAlignment.
- * @return Aligned file offset.
- * \todo uiSize isn't used yet. Will be used later on to search for caves.
- **/
- template<int x>
- unsigned int PeHeaderT<x>::calcOffset(/*unsigned int uiSize*/) const
- {
- unsigned int maxoffset = size();
-
- for (word i=0;i<calcNumberOfSections();i++)
- {
- if (getPointerToRawData(i) + getSizeOfRawData(i) > maxoffset) maxoffset = getPointerToRawData(i) + getSizeOfRawData(i);
- }
-
- return alignOffset(maxoffset, getFileAlignment());
- }
-
- /**
- * Calculates the Rva for a new section. The Rva will already be aligned to the file's SectionAlignment.
- * \todo uiSize isn't used yet. Will be used later on to search for caves.
- * @return Aligned Rva.
- **/
- template<int x>
- unsigned int PeHeaderT<x>::calcRva(/*unsigned int uiSize*/) const
- {
- // Major note here: It's possible for sections to exist with a Virtual Size of 0.
- // That's why it's necessary to use std::max(Vsize, RawSize) here.
- // An example for such a file is dbeng6.exe (made by Sybase).
- // In this file each and every section has a VSize of 0 but it still runs.
-
- unsigned int maxoffset = size();
- for (word i=0;i<calcNumberOfSections();i++)
- {
- if (getVirtualAddress(i) + std::max(getVirtualSize(i), getSizeOfRawData(i)) > maxoffset) maxoffset = getVirtualAddress(i) + std::max(getVirtualSize(i), getSizeOfRawData(i));
- }
-
- return alignOffset(maxoffset, getSectionAlignment());
- }
-
- /**
- * Returns the number of currently defined sections. Note that this value can be different from the number
- * of sections according to the header (see #PeLib::PeHeaderT<x>::getNumberOfSections).
- * @return Number of currently defined sections.
- **/
- template<int x>
- word PeHeaderT<x>::calcNumberOfSections() const
- {
- return static_cast<PeLib::word>(m_vIsh.size());
- }
-
- /**
- * Enlarges the physically last section in the file.
- * @param uiSize Additional size that's added to the section's size.
- **/
- template<int x>
- void PeHeaderT<x>::enlargeLastSection(unsigned int uiSize)
- {
- std::vector<PELIB_IMAGE_SECTION_HEADER>::iterator ishLastSection = std::max_element(m_vIsh.begin(), m_vIsh.end(), std::mem_fun_ref(&PELIB_IMAGE_SECTION_HEADER::biggerFileOffset));
- unsigned int uiRawDataSize = alignOffset(ishLastSection->SizeOfRawData + uiSize, getFileAlignment());
-
- ishLastSection->SizeOfRawData = uiRawDataSize;
- ishLastSection->VirtualSize = ishLastSection->SizeOfRawData;
-
- setSizeOfImage(calcSizeOfImage());
- }
-
- /**
- * Determines the section which contains the file offset.
- * @param dwOffset File offset.
- * @return Section Id of the section which contains the offset.
- **/
- template<int x>
- word PeHeaderT<x>::getSectionWithOffset(VAR4_8 dwOffset) const
- {
- // Offset = 0 must be handled explicitly as there are files
- // with sections that begin at offset 0, that means the section
- // only exists in memory.
-
- if (!dwOffset) return std::numeric_limits<word>::max();
-
- for (word i=0;i<calcNumberOfSections();i++)
- {
- // Explicity exclude sections with raw pointer = 0.
- dword rawptr = getPointerToRawData(i);
- if (rawptr && rawptr <= dwOffset && rawptr + getSizeOfRawData(i) > dwOffset) return i;
- }
-
- return std::numeric_limits<word>::max();
- }
-
- /**
- * Determines the section which contains the Rva.
- * @param dwRva A relative virtual address.
- * @return Section Id of the section which contains the Rva.
- **/
- template<int x>
- word PeHeaderT<x>::getSectionWithRva(VAR4_8 dwRva) const
- {
- // Major note here: It's possible for sections to exist with a Virtual Size of 0.
- // That's why it's necessary to use std::max(Vsize, RawSize) here.
- // An example for such a file is dbeng6.exe (made by Sybase).
- // In this file each and every section has a VSize of 0 but it still runs.
-
- for (word i=0;i<calcNumberOfSections();i++)
- {
- // Weird VC++7 error doesn't allow me to use std::max here.
- dword max = getVirtualSize(i) >= getSizeOfRawData(i) ? getVirtualSize(i) : getSizeOfRawData(i);
- if (getVirtualAddress(i) <= dwRva && getVirtualAddress(i) + max > dwRva) return i;
- }
-
- return -1;
- }
-
- /**
- * Corrects all faulty values of the current PE header. The following values will be corrected: NtSignature,
- * NumberOfSections, SizeOfOptionalHeader, FileAlignment (will be aligned to n*0x200),
- * SectionAlignment (will be aligned to n*0x1000), NumberOfRvaAndSizes, SizeOfHeaders, SizeOfImage,
- * Magic, Characteristics.
- * @param dwOffset Beginning of PeHeader (see #PeLib::MzHeader::getAddressOfPeHeader).
- * \todo 32bit and 64bit versions.
- **/
- template<int x>
- void PeHeaderT<x>::makeValid(dword dwOffset)
- {
- setNtSignature(PELIB_IMAGE_NT_SIGNATURE); // 'PE'
- setMachine(PELIB_IMAGE_FILE_MACHINE_I386);
- setNumberOfSections(calcNumberOfSections());
-
- // Check if 64 bits.
- setSizeOfOptionalHeader(PELIB_IMAGE_OPTIONAL_HEADER<x>::size() + calcNumberOfRvaAndSizes() * 8);
-
- // Check if 64 bits.
- dword dwCharacteristics = PELIB_IMAGE_FILE_EXECUTABLE_IMAGE | PELIB_IMAGE_FILE_32BIT_MACHINE;
- setCharacteristics(dwCharacteristics);
-
- // Check if 64 bits.
- setMagic(PELIB_IMAGE_NT_OPTIONAL_HDR32_MAGIC);
-
- // setImageBase(0x01000000);
-
- // Align file and section alignment values
- unsigned int dwAlignedOffset = alignOffset(getSectionAlignment(), 0x1000);
- setSectionAlignment(dwAlignedOffset ? dwAlignedOffset : 0x1000);
-
- dwAlignedOffset = alignOffset(getFileAlignment(), 0x200);
- setFileAlignment(dwAlignedOffset ? dwAlignedOffset : 0x200);
-
-// setMajorSubsystemVersion(4);
-// setSubsystem(IMAGE_SUBSYSTEM_WINDOWS_GUI);
- setNumberOfRvaAndSizes(calcNumberOfRvaAndSizes());
-
- // Code below depends on code above. Don't change the order.
- dword dwSizeOfHeaders = alignOffset(dwOffset + size(), getFileAlignment());
- setSizeOfHeaders(dwSizeOfHeaders);
-
- dword dwSizeOfImage = alignOffset(dwSizeOfHeaders, getSectionAlignment());
-
- for (int i=0;i<calcNumberOfSections();i++)
- {
- dwSizeOfImage += alignOffset(getVirtualSize(i), getSectionAlignment());
- }
-
- dwSizeOfImage = alignOffset(dwSizeOfImage, getSectionAlignment());
- setSizeOfImage(dwSizeOfImage);
- }
-
- template<int x>
- unsigned int PeHeaderT<x>::offsetToRva(VAR4_8 dwOffset) const
- {
- if (dwOffset < calcStartOfCode()) return dwOffset;
-
- PeLib::word uiSecnr = getSectionWithOffset(dwOffset);
-
- if (uiSecnr == 0xFFFF) return -1;
-
- return getVirtualAddress(uiSecnr) + dwOffset - getPointerToRawData(uiSecnr);
- }
-
- /**
- * Converts a file offset to a virtual address.
- * @param dwOffset File offset.
- * @return Virtual Address.
- **/
- template<int x>
- unsigned int PeHeaderT<x>::offsetToVa(VAR4_8 dwOffset) const
- {
- if (dwOffset < calcStartOfCode()) return getImageBase() + dwOffset;
-
- PeLib::word uiSecnr = getSectionWithOffset(dwOffset);
-
- if (uiSecnr == 0xFFFF) return -1;
-
- return getImageBase() + getVirtualAddress(uiSecnr) + dwOffset - getPointerToRawData(uiSecnr);
- }
-
- template<int x>
- void PeHeaderT<x>::readHeader(InputBuffer& ibBuffer, PELIB_IMAGE_NT_HEADERS<x>& header) const
- {
- ibBuffer >> header.Signature;
-
- ibBuffer >> header.FileHeader.Machine;
- ibBuffer >> header.FileHeader.NumberOfSections;
- ibBuffer >> header.FileHeader.TimeDateStamp;
- ibBuffer >> header.FileHeader.PointerToSymbolTable;
- ibBuffer >> header.FileHeader.NumberOfSymbols;
- ibBuffer >> header.FileHeader.SizeOfOptionalHeader;
- ibBuffer >> header.FileHeader.Characteristics;
- ibBuffer >> header.OptionalHeader.Magic;
-
- ibBuffer >> header.OptionalHeader.MajorLinkerVersion;
- ibBuffer >> header.OptionalHeader.MinorLinkerVersion;
- ibBuffer >> header.OptionalHeader.SizeOfCode;
- ibBuffer >> header.OptionalHeader.SizeOfInitializedData;
- ibBuffer >> header.OptionalHeader.SizeOfUninitializedData;
- ibBuffer >> header.OptionalHeader.AddressOfEntryPoint;
- ibBuffer >> header.OptionalHeader.BaseOfCode;
- readBaseOfData(ibBuffer, header);
- ibBuffer >> header.OptionalHeader.ImageBase;
- ibBuffer >> header.OptionalHeader.SectionAlignment;
- ibBuffer >> header.OptionalHeader.FileAlignment;
- ibBuffer >> header.OptionalHeader.MajorOperatingSystemVersion;
- ibBuffer >> header.OptionalHeader.MinorOperatingSystemVersion;
- ibBuffer >> header.OptionalHeader.MajorImageVersion;
- ibBuffer >> header.OptionalHeader.MinorImageVersion;
- ibBuffer >> header.OptionalHeader.MajorSubsystemVersion;
- ibBuffer >> header.OptionalHeader.MinorSubsystemVersion;
- ibBuffer >> header.OptionalHeader.Win32VersionValue;
- ibBuffer >> header.OptionalHeader.SizeOfImage;
- ibBuffer >> header.OptionalHeader.SizeOfHeaders;
- ibBuffer >> header.OptionalHeader.CheckSum;
- ibBuffer >> header.OptionalHeader.Subsystem;
- ibBuffer >> header.OptionalHeader.DllCharacteristics;
- ibBuffer >> header.OptionalHeader.SizeOfStackReserve;
- ibBuffer >> header.OptionalHeader.SizeOfStackCommit;
- ibBuffer >> header.OptionalHeader.SizeOfHeapReserve;
- ibBuffer >> header.OptionalHeader.SizeOfHeapCommit;
- ibBuffer >> header.OptionalHeader.LoaderFlags;
- ibBuffer >> header.OptionalHeader.NumberOfRvaAndSizes;
- }
-
- template<int x>
- void PeHeaderT<x>::readDataDirectories(InputBuffer& ibBuffer, PELIB_IMAGE_NT_HEADERS<x>& header) const
- {
- PELIB_IMAGE_DATA_DIRECTORY idd;
-
- for (unsigned int i=0;i<header.OptionalHeader.NumberOfRvaAndSizes;i++)
- {
- ibBuffer >> idd.VirtualAddress;
- ibBuffer >> idd.Size;
- header.dataDirectories.push_back(idd);
- }
- }
-
- template<int x>
- std::vector<PELIB_IMAGE_SECTION_HEADER> PeHeaderT<x>::readSections(InputBuffer& ibBuffer, PELIB_IMAGE_NT_HEADERS<x>& header) const
- {
- const unsigned int nrSections = header.FileHeader.NumberOfSections;
- PELIB_IMAGE_SECTION_HEADER ishCurr;
-
- std::vector<PELIB_IMAGE_SECTION_HEADER> vIshdCurr;
-
- for (unsigned int i=0;i<nrSections;i++)
- {
- ibBuffer.read(reinterpret_cast<char*>(ishCurr.Name), 8);
- ibBuffer >> ishCurr.VirtualSize;
- ibBuffer >> ishCurr.VirtualAddress;
- ibBuffer >> ishCurr.SizeOfRawData;
- ibBuffer >> ishCurr.PointerToRawData;
- ibBuffer >> ishCurr.PointerToRelocations;
- ibBuffer >> ishCurr.PointerToLinenumbers;
- ibBuffer >> ishCurr.NumberOfRelocations;
- ibBuffer >> ishCurr.NumberOfLinenumbers;
- ibBuffer >> ishCurr.Characteristics;
- vIshdCurr.push_back(ishCurr);
- }
-
- return vIshdCurr;
- }
-
- template<int x>
- int PeHeaderT<x>::read(const unsigned char* pcBuffer, unsigned int uiSize, unsigned int uiOffset)
- {
- if (uiSize < m_inthHeader.size())
- {
- return ERROR_INVALID_FILE;
- }
-
- std::vector<unsigned char> vBuffer(pcBuffer, pcBuffer + m_inthHeader.size());
-
- InputBuffer ibBuffer(vBuffer);
- PELIB_IMAGE_NT_HEADERS<x> header;
-
- readHeader(ibBuffer, header);
-
- if (uiSize < m_inthHeader.size() + header.OptionalHeader.NumberOfRvaAndSizes * 8 + header.FileHeader.NumberOfSections * 0x28)
- {
- return ERROR_INVALID_FILE;
- }
-
- vBuffer.resize(header.OptionalHeader.NumberOfRvaAndSizes * 8 + header.FileHeader.NumberOfSections * 0x28);
- vBuffer.assign(pcBuffer + m_inthHeader.size(), pcBuffer + m_inthHeader.size() + header.OptionalHeader.NumberOfRvaAndSizes * 8 + header.FileHeader.NumberOfSections * 0x28);
-
- ibBuffer.setBuffer(vBuffer);
-
- readDataDirectories(ibBuffer, header);
-
- m_vIsh = readSections(ibBuffer, header);
-
- std::swap(m_inthHeader, header);
-
- m_uiOffset = uiOffset;
-
- return NO_ERROR;
- }
-
- /**
- * Reads the PE header from a file Note that this function does not verify if a file is actually a MZ file.
- * For this purpose see #PeLib::PeHeaderT<x>::isValid. The only check this function makes is a check to see if
- * the file is large enough to be a PE header. If the data is valid doesn't matter.
- * @param strFilename Name of the file which will be read.
- * @param uiOffset File offset of PE header (see #PeLib::MzHeader::getAddressOfPeHeader).
- **/
- template<int x>
- int PeHeaderT<x>::read(std::string strFilename, unsigned int uiOffset)
- {
- std::ifstream ifFile(strFilename.c_str(), std::ios::binary);
-
- if (!ifFile)
- {
- return ERROR_OPENING_FILE;
- }
-
- // File too small
- if (fileSize(ifFile) < uiOffset + m_inthHeader.size())
- {
- return ERROR_INVALID_FILE;
- }
-
- std::vector<unsigned char> vBuffer(m_inthHeader.size());
-
- ifFile.seekg(uiOffset, std::ios::beg);
- ifFile.read(reinterpret_cast<char*>(&vBuffer[0]), static_cast<std::streamsize>(vBuffer.size()));
-
- InputBuffer ibBuffer(vBuffer);
- PELIB_IMAGE_NT_HEADERS<x> header;
-
- readHeader(ibBuffer, header);
-
- vBuffer.resize(header.OptionalHeader.NumberOfRvaAndSizes * 8 + header.FileHeader.NumberOfSections * 0x28);
-
- ifFile.read(reinterpret_cast<char*>(&vBuffer[0]), static_cast<std::streamsize>(vBuffer.size()));
- if (!ifFile)
- {
- return ERROR_INVALID_FILE;
- }
-
- ibBuffer.setBuffer(vBuffer);
-
- readDataDirectories(ibBuffer, header);
-
- // Sections
-// const unsigned int nrSections = header.FileHeader.NumberOfSections;
-// if (fileSize(ifFile) < uiOffset + m_inthHeader.size() + nrSections * PELIB_IMAGE_SECTION_HEADER::size())
-// {
-// return ERROR_INVALID_FILE;
-// }
-
- m_vIsh = readSections(ibBuffer, header);
-
- std::swap(m_inthHeader, header);
-
- m_uiOffset = uiOffset;
-
- ifFile.close();
-
- return NO_ERROR;
- }
-
- /**
- * Rebuilds the PE header so that it can be written to a file. It's not guaranteed that the
- * header will be valid. If you want to make sure that the header will be valid you
- * must call #PeLib::PeHeaderT<x>::makeValid first.
- * @param vBuffer Buffer where the rebuilt header will be stored.
- **/
- template<int x>
- void PeHeaderT<x>::rebuild(std::vector<byte>& vBuffer) const
- {
- OutputBuffer obBuffer(vBuffer);
-
- obBuffer << m_inthHeader.Signature;
-
- obBuffer << m_inthHeader.FileHeader.Machine;
- obBuffer << m_inthHeader.FileHeader.NumberOfSections;
- obBuffer << m_inthHeader.FileHeader.TimeDateStamp;
- obBuffer << m_inthHeader.FileHeader.PointerToSymbolTable;
- obBuffer << m_inthHeader.FileHeader.NumberOfSymbols;
- obBuffer << m_inthHeader.FileHeader.SizeOfOptionalHeader;
- obBuffer << m_inthHeader.FileHeader.Characteristics;
- obBuffer << m_inthHeader.OptionalHeader.Magic;
- obBuffer << m_inthHeader.OptionalHeader.MajorLinkerVersion;
- obBuffer << m_inthHeader.OptionalHeader.MinorLinkerVersion;
- obBuffer << m_inthHeader.OptionalHeader.SizeOfCode;
- obBuffer << m_inthHeader.OptionalHeader.SizeOfInitializedData;
- obBuffer << m_inthHeader.OptionalHeader.SizeOfUninitializedData;
- obBuffer << m_inthHeader.OptionalHeader.AddressOfEntryPoint;
- obBuffer << m_inthHeader.OptionalHeader.BaseOfCode;
- rebuildBaseOfData(obBuffer);
-// obBuffer << m_inthHeader.OptionalHeader.BaseOfData;
- obBuffer << m_inthHeader.OptionalHeader.ImageBase;
- obBuffer << m_inthHeader.OptionalHeader.SectionAlignment;
- obBuffer << m_inthHeader.OptionalHeader.FileAlignment;
- obBuffer << m_inthHeader.OptionalHeader.MajorOperatingSystemVersion;
- obBuffer << m_inthHeader.OptionalHeader.MinorOperatingSystemVersion;
- obBuffer << m_inthHeader.OptionalHeader.MajorImageVersion;
- obBuffer << m_inthHeader.OptionalHeader.MinorImageVersion;
- obBuffer << m_inthHeader.OptionalHeader.MajorSubsystemVersion;
- obBuffer << m_inthHeader.OptionalHeader.MinorSubsystemVersion;
- obBuffer << m_inthHeader.OptionalHeader.Win32VersionValue;
- obBuffer << m_inthHeader.OptionalHeader.SizeOfImage;
- obBuffer << m_inthHeader.OptionalHeader.SizeOfHeaders;
- obBuffer << m_inthHeader.OptionalHeader.CheckSum;
- obBuffer << m_inthHeader.OptionalHeader.Subsystem;
- obBuffer << m_inthHeader.OptionalHeader.DllCharacteristics;
- obBuffer << m_inthHeader.OptionalHeader.SizeOfStackReserve;
- obBuffer << m_inthHeader.OptionalHeader.SizeOfStackCommit;
- obBuffer << m_inthHeader.OptionalHeader.SizeOfHeapReserve;
- obBuffer << m_inthHeader.OptionalHeader.SizeOfHeapCommit;
- obBuffer << m_inthHeader.OptionalHeader.LoaderFlags;
- obBuffer << m_inthHeader.OptionalHeader.NumberOfRvaAndSizes;
-
- // The 0x10 data directories
- for (unsigned int i=0;i<calcNumberOfRvaAndSizes();i++)
- {
- obBuffer << m_inthHeader.dataDirectories[i].VirtualAddress;
- obBuffer << m_inthHeader.dataDirectories[i].Size;
- }
-
- // The section definitions
- const unsigned int nrSections = calcNumberOfSections();
- for (unsigned int i=0;i<nrSections;i++)
- {
- char temp[9] = {0};
- strcpy(temp, getSectionName(i).c_str());
- obBuffer.add(temp, 8);
- obBuffer << m_vIsh[i].VirtualSize;
- obBuffer << m_vIsh[i].VirtualAddress;
- obBuffer << m_vIsh[i].SizeOfRawData;
- obBuffer << m_vIsh[i].PointerToRawData;
- obBuffer << m_vIsh[i].PointerToRelocations;
- obBuffer << m_vIsh[i].PointerToLinenumbers;
- obBuffer << m_vIsh[i].NumberOfRelocations;
- obBuffer << m_vIsh[i].NumberOfLinenumbers;
- obBuffer << m_vIsh[i].Characteristics;
- }
- }
-
- /**
- * Converts a relative virtual offset to a file offset.
- * @param dwRva A relative virtual offset.
- * @return A file offset.
- * \todo It's not always 0x1000.
- **/
- template<int x>
- typename FieldSizes<x>::VAR4_8 PeHeaderT<x>::rvaToOffset(VAR4_8 dwRva) const
- {
- // XXX: Not correct
- if (dwRva < 0x1000) return dwRva;
-
- PeLib::word uiSecnr = getSectionWithRva(dwRva);
- if (uiSecnr == 0xFFFF || dwRva > getVirtualAddress(uiSecnr) + getSizeOfRawData(uiSecnr))
- {
- return std::numeric_limits<VAR4_8>::max();
- }
-
- return getPointerToRawData(uiSecnr) + dwRva - getVirtualAddress(uiSecnr);
- }
-
- /**
- * Converts a relative virtual offset to a virtual offset.
- * @param dwRva A relative virtual offset.
- * @return A virtual offset.
- **/
- template<int x>
- typename FieldSizes<x>::VAR4_8 PeHeaderT<x>::rvaToVa(VAR4_8 dwRva) const
- {
- return getImageBase() + dwRva;
- }
-
- /**
- * Calculates the size of the current PE header. This includes the actual header and the section definitions.
- * @return Size of the current PE header.
- * \todo Better handling of files with less than 0x10 directories.
- **/
- template<int x>
- unsigned int PeHeaderT<x>::size() const
- {
- return m_inthHeader.size() + getNumberOfSections() * PELIB_IMAGE_SECTION_HEADER::size();
- }
-
- // \todo Not sure if this works.
- template<int x>
- typename FieldSizes<x>::VAR4_8 PeHeaderT<x>::vaToRva(VAR4_8 dwRva) const
- {
- if (dwRva - getImageBase() < calcStartOfCode()) return dwRva - getImageBase();
-
- if (getSectionWithRva(dwRva - getImageBase()) == 0xFFFF) return -1;
-
- return dwRva - getImageBase();
- }
-
- template<int x>
- typename FieldSizes<x>::VAR4_8 PeHeaderT<x>::vaToOffset(VAR4_8 dwRva) const
- {
- return rvaToOffset(dwRva - getImageBase());
- }
-
- /**
- * Saves the PE header to a file. Note that this only saves the header information, if you have added sections
- * and want to save these to the file you have to call #PeLib::PeHeaderT<x>::saveSections too. This function also
- * does not verify if the PE header is correct. If you want to make sure that the current PE header is valid,
- * call #PeLib::PeHeaderT<x>::isValid and #PeLib::PeHeaderT<x>::makeValid first.
- * @param strFilename Filename of the file the header will be written to.
- * @param uiOffset File offset the header will be written to.
- **/
- template<int x>
- int PeHeaderT<x>::write(std::string strFilename, unsigned int uiOffset) const
- {
- std::fstream ofFile(strFilename.c_str(), std::ios_base::in);
-
- if (!ofFile)
- {
- ofFile.clear();
- ofFile.open(strFilename.c_str(), std::ios_base::out | std::ios_base::binary);
- }
- else
- {
- ofFile.close();
- ofFile.open(strFilename.c_str(), std::ios_base::in | std::ios_base::out | std::ios_base::binary);
- }
-
- if (!ofFile)
- {
- return ERROR_OPENING_FILE;
- }
-
- ofFile.seekp(uiOffset, std::ios::beg);
-
- std::vector<unsigned char> vBuffer;
-
- rebuild(vBuffer);
-
- ofFile.write(reinterpret_cast<const char*>(&vBuffer[0]), static_cast<unsigned int>(vBuffer.size()));
-
- ofFile.close();
-
- return NO_ERROR;
- }
-
-
- /**
- * Overwrites a section's data.
- * @param wSecnr Number of the section which will be overwritten.
- * @param strFilename Name of the file where the section will be written to.
- * @param wSecnr Number of the section that will be written.
- * @param vBuffer New data of the section.
- **/
- template<int x>
- int PeHeaderT<x>::writeSectionData(const std::string& strFilename, word wSecnr, const std::vector<byte>& vBuffer) const
- {
- std::fstream ofFile(strFilename.c_str(), std::ios_base::in);
-
- if (!ofFile)
- {
- ofFile.clear();
- ofFile.open(strFilename.c_str(), std::ios_base::out | std::ios_base::binary);
- }
- else
- {
- ofFile.close();
- ofFile.open(strFilename.c_str(), std::ios_base::in | std::ios_base::out | std::ios_base::binary);
- }
-
- if (!ofFile)
- {
- ofFile.clear();
-
- return ERROR_OPENING_FILE;
- }
-
- ofFile.seekp(getPointerToRawData(wSecnr), std::ios::beg);
-
- ofFile.write(reinterpret_cast<const char*>(&vBuffer[0]), std::min(static_cast<unsigned int>(vBuffer.size()), getSizeOfRawData(wSecnr)));
-
- ofFile.close();
-
- return NO_ERROR;
- }
-
- template<int x>
- int PeHeaderT<x>::writeSections(const std::string& strFilename) const
- {
- std::fstream ofFile(strFilename.c_str(), std::ios_base::in);
-
- if (!ofFile)
- {
- ofFile.clear();
- ofFile.open(strFilename.c_str(), std::ios_base::out | std::ios_base::binary);
- }
- else
- {
- ofFile.close();
- ofFile.open(strFilename.c_str(), std::ios_base::in | std::ios_base::out | std::ios_base::binary);
- }
-
- if (!ofFile)
- {
- return ERROR_OPENING_FILE;
- }
-
- unsigned int uiFilesize = fileSize(ofFile);
-
- for (int i=0;i<calcNumberOfSections();i++)
- {
- if (uiFilesize < getPointerToRawData(i) + getSizeOfRawData(i))
- {
- unsigned int uiToWrite = getPointerToRawData(i) + getSizeOfRawData(i) - uiFilesize;
- std::vector<char> vBuffer(uiToWrite);
- ofFile.seekp(0, std::ios::end);
- ofFile.write(&vBuffer[0], static_cast<unsigned int>(vBuffer.size()));
- uiFilesize = getPointerToRawData(i) + getSizeOfRawData(i);
- }
- }
-
- ofFile.close();
-
- return NO_ERROR;
- }
-
- /**
- * Returns the file's Nt signature.
- * @return The Nt signature value from the PE header.
- **/
- template<int x>
- dword PeHeaderT<x>::getNtSignature() const
- {
- return m_inthHeader.Signature;
- }
-
- /**
- * Returns the file's machine.
- * @return The Machine value from the PE header.
- **/
- template<int x>
- word PeHeaderT<x>::getMachine() const
- {
- return m_inthHeader.FileHeader.Machine;
- }
-
-
- /**
- * Returns the file's number of sections as defined in the header. Note that this value can be different
- * from the number of defined sections (#see PeLib::PeHeaderT<x>::getNumberOfSections).
- * @return The NumberOfSections value from the PE header.
- **/
- template<int x>
- word PeHeaderT<x>::getNumberOfSections() const
- {
- return m_inthHeader.FileHeader.NumberOfSections;
- }
-
- /**
- * Returns the file's TimeDateStamp.
- * @return The TimeDateStamp value from the PE header.
- **/
- template<int x>
- dword PeHeaderT<x>::getTimeDateStamp() const
- {
- return m_inthHeader.FileHeader.TimeDateStamp;
- }
-
- /**
- * Returns the relative virtual address of the file's symbol table.
- * @return The PointerToSymbolTable value from the PE header.
- **/
- template<int x>
- dword PeHeaderT<x>::getPointerToSymbolTable() const
- {
- return m_inthHeader.FileHeader.PointerToSymbolTable;
- }
-
- /**
- * Returns the number of symbols of the file's symbol table.
- * @return The NumberOfSymbols value from the PE header.
- **/
- template<int x>
- dword PeHeaderT<x>::getNumberOfSymbols() const
- {
- return m_inthHeader.FileHeader.NumberOfSymbols;
- }
-
- /**
- * Returns the size of optional header of the file.
- * @return The SizeOfOptionalHeader value from the PE header.
- **/
- template<int x>
- word PeHeaderT<x>::getSizeOfOptionalHeader() const
- {
- return m_inthHeader.FileHeader.SizeOfOptionalHeader;
- }
-
- /**
- * @return The Characteristics value from the PE header.
- **/
- template<int x>
- word PeHeaderT<x>::getCharacteristics() const
- {
- return m_inthHeader.FileHeader.Characteristics;
- }
-
- /**
- * @return The Magic value from the PE header.
- **/
- template<int x>
- word PeHeaderT<x>::getMagic() const
- {
- return m_inthHeader.OptionalHeader.Magic;
- }
-
- /**
- * @return The MajorLinkerVersion value from the PE header.
- **/
- template<int x>
- byte PeHeaderT<x>::getMajorLinkerVersion() const
- {
- return m_inthHeader.OptionalHeader.MajorLinkerVersion;
- }
-
- /**
- * @return The MinorLinkerVersion value from the PE header.
- **/
- template<int x>
- byte PeHeaderT<x>::getMinorLinkerVersion() const
- {
- return m_inthHeader.OptionalHeader.MinorLinkerVersion;
- }
-
- /**
- * @return The SizeOfCode value from the PE header.
- **/
- template<int x>
- dword PeHeaderT<x>::getSizeOfCode() const
- {
- return m_inthHeader.OptionalHeader.SizeOfCode;
- }
-
- /**
- * @return The SizeOfInitializedData value from the PE header.
- **/
- template<int x>
- dword PeHeaderT<x>::getSizeOfInitializedData() const
- {
- return m_inthHeader.OptionalHeader.SizeOfInitializedData;
- }
-
- /**
- * @return The SizeOfUninitializedData value from the PE header.
- **/
- template<int x>
- dword PeHeaderT<x>::getSizeOfUninitializedData() const
- {
- return m_inthHeader.OptionalHeader.SizeOfUninitializedData;
- }
-
- /**
- * @return The AddressOfEntryPoint value from the PE header.
- **/
- template<int x>
- dword PeHeaderT<x>::getAddressOfEntryPoint() const
- {
- return m_inthHeader.OptionalHeader.AddressOfEntryPoint;
- }
-
- /**
- * @return The BaseOfCode value from the PE header.
- **/
- template<int x>
- dword PeHeaderT<x>::getBaseOfCode() const
- {
- return m_inthHeader.OptionalHeader.BaseOfCode;
- }
-
- /**
- * @return The ImageBase value from the PE header.
- **/
- template<int x>
- typename FieldSizes<x>::VAR4_8 PeHeaderT<x>::getImageBase() const
- {
- return m_inthHeader.OptionalHeader.ImageBase;
- }
-
- /**
- * @return The SectionAlignment value from the PE header.
- **/
- template<int x>
- dword PeHeaderT<x>::getSectionAlignment() const
- {
- return m_inthHeader.OptionalHeader.SectionAlignment;
- }
-
- /**
- * @return The FileAlignment value from the PE header.
- **/
- template<int x>
- dword PeHeaderT<x>::getFileAlignment() const
- {
- return m_inthHeader.OptionalHeader.FileAlignment;
- }
-
- /**
- * @return The MajorOperatingSystemVersion value from the PE header.
- **/
- template<int x>
- word PeHeaderT<x>::getMajorOperatingSystemVersion() const
- {
- return m_inthHeader.OptionalHeader.MajorOperatingSystemVersion;
- }
-
- /**
- * @return The MinorOperatingSystemVersion value from the PE header.
- **/
- template<int x>
- word PeHeaderT<x>::getMinorOperatingSystemVersion() const
- {
- return m_inthHeader.OptionalHeader.MinorOperatingSystemVersion;
- }
-
- /**
- * @return The MajorImageVersion value from the PE header.
- **/
- template<int x>
- word PeHeaderT<x>::getMajorImageVersion() const
- {
- return m_inthHeader.OptionalHeader.MajorImageVersion;
- }
-
- /**
- * @return The MinorImageVersion value from the PE header.
- **/
- template<int x>
- word PeHeaderT<x>::getMinorImageVersion() const
- {
- return m_inthHeader.OptionalHeader.MinorImageVersion;
- }
-
- /**
- * @return The MajorSubsystemVersion value from the PE header.
- **/
- template<int x>
- word PeHeaderT<x>::getMajorSubsystemVersion() const
- {
- return m_inthHeader.OptionalHeader.MajorSubsystemVersion;
- }
-
- /**
- * @return The MinorSubsystemVersion value from the PE header.
- **/
- template<int x>
- word PeHeaderT<x>::getMinorSubsystemVersion() const
- {
- return m_inthHeader.OptionalHeader.MinorSubsystemVersion;
- }
-
- /**
- * @return The WinVersionValue value from the PE header.
- **/
- template<int x>
- dword PeHeaderT<x>::getWin32VersionValue() const
- {
- return m_inthHeader.OptionalHeader.Win32VersionValue;
- }
-
- /**
- * @return The SizeOfImage value from the PE header.
- **/
- template<int x>
- dword PeHeaderT<x>::getSizeOfImage() const
- {
- return m_inthHeader.OptionalHeader.SizeOfImage;
- }
-
- /**
- * @return The SizeOfHeaders value from the PE header.
- **/
- template<int x>
- dword PeHeaderT<x>::getSizeOfHeaders() const
- {
- return m_inthHeader.OptionalHeader.SizeOfHeaders;
- }
-
- /**
- * @return The CheckSums value from the PE header.
- **/
- template<int x>
- dword PeHeaderT<x>::getCheckSum() const
- {
- return m_inthHeader.OptionalHeader.CheckSum;
- }
-
- /**
- * @return The Subsystem value from the PE header.
- **/
- template<int x>
- word PeHeaderT<x>::getSubsystem() const
- {
- return m_inthHeader.OptionalHeader.Subsystem;
- }
-
- /**
- * @return The DllCharacteristics value from the PE header.
- **/
- template<int x>
- word PeHeaderT<x>::getDllCharacteristics() const
- {
- return m_inthHeader.OptionalHeader.DllCharacteristics;
- }
-
- /**
- * @return The SizeOfStackReserve value from the PE header.
- **/
- template<int x>
- typename FieldSizes<x>::VAR4_8 PeHeaderT<x>::getSizeOfStackReserve() const
- {
- return m_inthHeader.OptionalHeader.SizeOfStackReserve;
- }
-
- /**
- * @return The SizeOfStackCommit value from the PE header.
- **/
- template<int x>
- typename FieldSizes<x>::VAR4_8 PeHeaderT<x>::getSizeOfStackCommit() const
- {
- return m_inthHeader.OptionalHeader.SizeOfStackCommit;
- }
-
- /**
- * @return The SizeOfHeapReserve value from the PE header.
- **/
- template<int x>
- typename FieldSizes<x>::VAR4_8 PeHeaderT<x>::getSizeOfHeapReserve() const
- {
- return m_inthHeader.OptionalHeader.SizeOfHeapReserve;
- }
-
- /**
- * @return The SizeOfHeapCommit value from the PE header.
- **/
- template<int x>
- typename FieldSizes<x>::VAR4_8 PeHeaderT<x>::getSizeOfHeapCommit() const
- {
- return m_inthHeader.OptionalHeader.SizeOfHeapCommit;
- }
-
- /**
- * @return The LoaderFlags value from the PE header.
- **/
- template<int x>
- dword PeHeaderT<x>::getLoaderFlags() const
- {
- return m_inthHeader.OptionalHeader.LoaderFlags;
- }
-
- /**
- * @return The NumberOfRvaAndSizes value from the PE header.
- **/
- template<int x>
- dword PeHeaderT<x>::getNumberOfRvaAndSizes() const
- {
- return m_inthHeader.OptionalHeader.NumberOfRvaAndSizes;
- }
-
- template<int x>
- dword PeHeaderT<x>::calcNumberOfRvaAndSizes() const
- {
- return static_cast<dword>(m_inthHeader.dataDirectories.size());
- }
-
- /**
- * Returns the relative virtual address of the current file's export directory.
- * @return The Rva of the Export directory.
- **/
- template<int x>
- dword PeHeaderT<x>::getIddExportRva() const
- {
- return m_inthHeader.dataDirectories[PELIB_IMAGE_DIRECTORY_ENTRY_EXPORT].VirtualAddress;
- }
-
- /**
- * Returns the size of the current file's export directory.
- * @return The sizeof the Export directory.
- **/
- template<int x>
- dword PeHeaderT<x>::getIddExportSize() const
- {
- return m_inthHeader.dataDirectories[PELIB_IMAGE_DIRECTORY_ENTRY_EXPORT].Size;
- }
-
- /**
- * Returns the relative virtual address of the current file's import directory.
- * @return The Rva of the Import directory.
- **/
- template<int x>
- dword PeHeaderT<x>::getIddImportRva() const
- {
- return m_inthHeader.dataDirectories[PELIB_IMAGE_DIRECTORY_ENTRY_IMPORT].VirtualAddress;
- }
-
- /**
- * Returns the size of the current file's import directory.
- * @return The size of the Import directory.
- **/
- template<int x>
- dword PeHeaderT<x>::getIddImportSize() const
- {
- return m_inthHeader.dataDirectories[PELIB_IMAGE_DIRECTORY_ENTRY_IMPORT].Size;
- }
-
- /**
- * Returns the relative virtual address of the current file's resource directory.
- * @return The Rva of the Resource directory.
- **/
- template<int x>
- dword PeHeaderT<x>::getIddResourceRva() const
- {
- return m_inthHeader.dataDirectories[PELIB_IMAGE_DIRECTORY_ENTRY_RESOURCE].VirtualAddress;
- }
-
- /**
- * Returns the size of the current file'resource resource directory.
- * @return The size of the Resource directory.
- **/
- template<int x>
- dword PeHeaderT<x>::getIddResourceSize() const
- {
- return m_inthHeader.dataDirectories[PELIB_IMAGE_DIRECTORY_ENTRY_RESOURCE].Size;
- }
-
- /**
- * Returns the relative virtual address of the current file's exception directory.
- * @return The Rva of the Exception directory.
- **/
- template<int x>
- dword PeHeaderT<x>::getIddExceptionRva() const
- {
- return m_inthHeader.dataDirectories[PELIB_IMAGE_DIRECTORY_ENTRY_EXCEPTION].VirtualAddress;
- }
-
- /**
- * Returns the size of the current file's exception directory.
- * @return The size of the Exception directory.
- **/
- template<int x>
- dword PeHeaderT<x>::getIddExceptionSize() const
- {
- return m_inthHeader.dataDirectories[PELIB_IMAGE_DIRECTORY_ENTRY_EXCEPTION].Size;
- }
-
- /**
- * Returns the relative virtual address of the current file's security directory.
- * @return The Rva of the Security directory.
- **/
- template<int x>
- dword PeHeaderT<x>::getIddSecurityRva() const
- {
- return m_inthHeader.dataDirectories[PELIB_IMAGE_DIRECTORY_ENTRY_SECURITY].VirtualAddress;
- }
-
- /**
- * Returns the size of the current file's security directory.
- * @return The size of the Security directory.
- **/
- template<int x>
- dword PeHeaderT<x>::getIddSecuritySize() const
- {
- return m_inthHeader.dataDirectories[PELIB_IMAGE_DIRECTORY_ENTRY_SECURITY].Size;
- }
-
- /**
- * Returns the relative virtual address of the current file's base reloc directory.
- * @return The Rva of the Base Reloc directory.
- **/
- template<int x>
- dword PeHeaderT<x>::getIddBaseRelocRva() const
- {
- return m_inthHeader.dataDirectories[PELIB_IMAGE_DIRECTORY_ENTRY_BASERELOC].VirtualAddress;
- }
-
- /**
- * Returns the size of the current file's base reloc directory.
- * @return The size of the Base Reloc directory.
- **/
- template<int x>
- dword PeHeaderT<x>::getIddBaseRelocSize() const
- {
- return m_inthHeader.dataDirectories[PELIB_IMAGE_DIRECTORY_ENTRY_BASERELOC].Size;
- }
-
- /**
- * Returns the relative virtual address of the current file's debug directory.
- * @return The Rva of the Debug directory.
- **/
- template<int x>
- dword PeHeaderT<x>::getIddDebugRva() const
- {
- return m_inthHeader.dataDirectories[PELIB_IMAGE_DIRECTORY_ENTRY_DEBUG].VirtualAddress;
- }
-
- /**
- * Returns the size of the current file's debug directory.
- * @return The size of the Debug directory.
- **/
- template<int x>
- dword PeHeaderT<x>::getIddDebugSize() const
- {
- return m_inthHeader.dataDirectories[PELIB_IMAGE_DIRECTORY_ENTRY_DEBUG].Size;
- }
-
- /**
- * Returns the relative virtual address of the current file's Architecture directory.
- * @return The Rva of the Architecture directory.
- **/
- template<int x>
- dword PeHeaderT<x>::getIddArchitectureRva() const
- {
- return m_inthHeader.dataDirectories[PELIB_IMAGE_DIRECTORY_ENTRY_ARCHITECTURE].VirtualAddress;
- }
-
- /**
- * Returns the size of the current file's Architecture directory.
- * @return The size of the Architecture directory.
- **/
- template<int x>
- dword PeHeaderT<x>::getIddArchitectureSize() const
- {
- return m_inthHeader.dataDirectories[PELIB_IMAGE_DIRECTORY_ENTRY_ARCHITECTURE].Size;
- }
-
- /**
- * Returns the relative virtual address of the current file's global ptr directory.
- * @return The Rva of the GlobalPtr directory.
- **/
- template<int x>
- dword PeHeaderT<x>::getIddGlobalPtrRva() const
- {
- return m_inthHeader.dataDirectories[PELIB_IMAGE_DIRECTORY_ENTRY_GLOBALPTR].VirtualAddress;
- }
-
- /**
- * Returns the size of the current file's global ptr directory.
- * @return The size of the GlobalPtr directory.
- **/
- template<int x>
- dword PeHeaderT<x>::getIddGlobalPtrSize() const
- {
- return m_inthHeader.dataDirectories[PELIB_IMAGE_DIRECTORY_ENTRY_GLOBALPTR].Size;
- }
-
- /**
- * Returns the relative virtual address of the current file's TLS directory.
- * @return The Rva of the Tls directory.
- **/
- template<int x>
- dword PeHeaderT<x>::getIddTlsRva() const
- {
- return m_inthHeader.dataDirectories[PELIB_IMAGE_DIRECTORY_ENTRY_TLS].VirtualAddress;
- }
-
- /**
- * Returns the size of the current file's TLS directory.
- * @return The size of the Tls directory.
- **/
- template<int x>
- dword PeHeaderT<x>::getIddTlsSize() const
- {
- return m_inthHeader.dataDirectories[PELIB_IMAGE_DIRECTORY_ENTRY_TLS].Size;
- }
-
- /**
- * Returns the relative virtual address of the current file's load config directory.
- * @return The Rva of the LoadConfig directory.
- **/
- template<int x>
- dword PeHeaderT<x>::getIddLoadConfigRva() const
- {
- return m_inthHeader.dataDirectories[PELIB_IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG].VirtualAddress;
- }
-
- /**
- * Returns the size of the current file's load config directory.
- * @return The size of the LoadConfig directory.
- **/
- template<int x>
- dword PeHeaderT<x>::getIddLoadConfigSize() const
- {
- return m_inthHeader.dataDirectories[PELIB_IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG].Size;
- }
-
- /**
- * Returns the relative virtual address of the current file's bound import directory.
- * @return The Rva of the BoundImport directory.
- **/
- template<int x>
- dword PeHeaderT<x>::getIddBoundImportRva() const
- {
- return m_inthHeader.dataDirectories[PELIB_IMAGE_DIRECTORY_ENTRY_BOUND_IMPORT].VirtualAddress;
- }
-
- /**
- * Returns the size of the current file's bound import directory.
- * @return The size of the BoundImport directory.
- **/
- template<int x>
- dword PeHeaderT<x>::getIddBoundImportSize() const
- {
- return m_inthHeader.dataDirectories[PELIB_IMAGE_DIRECTORY_ENTRY_BOUND_IMPORT].Size;
- }
-
- /**
- * Returns the relative virtual address of the current file's IAT directory.
- * @return The Rva of the IAT directory.
- **/
- template<int x>
- dword PeHeaderT<x>::getIddIatRva() const
- {
- return m_inthHeader.dataDirectories[PELIB_IMAGE_DIRECTORY_ENTRY_IAT].VirtualAddress;
- }
-
- /**
- * Returns the size of the current file's IAT directory.
- * @return The size of the IAT directory.
- **/
- template<int x>
- dword PeHeaderT<x>::getIddIatSize() const
- {
- return m_inthHeader.dataDirectories[PELIB_IMAGE_DIRECTORY_ENTRY_IAT].Size;
- }
-
- /**
- * Returns the relative virtual address of the current file's Delay Import directory.
- * @return The Rva of the DelayImport directory.
- **/
- template<int x>
- dword PeHeaderT<x>::getIddDelayImportRva() const
- {
- return m_inthHeader.dataDirectories[PELIB_IMAGE_DIRECTORY_ENTRY_DELAY_IMPORT].VirtualAddress;
- }
-
- /**
- * Returns the size of the current file's Delay Import directory.
- * @return The size of the DelayImport directory.
- **/
- template<int x>
- dword PeHeaderT<x>::getIddDelayImportSize() const
- {
- return m_inthHeader.dataDirectories[PELIB_IMAGE_DIRECTORY_ENTRY_DELAY_IMPORT].Size;
- }
-
- /**
- * Returns the relative virtual address of the current file's COM Descriptor directory.
- * @return The Rva of the COM Descriptor directory.
- **/
- template<int x>
- dword PeHeaderT<x>::getIddComHeaderRva() const
- {
- return m_inthHeader.dataDirectories[PELIB_IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR].VirtualAddress;
- }
-
- /**
- * Returns the size of the current file's COM Descriptor directory.
- * @return The Rva of the COM Descriptor directory.
- **/
- template<int x>
- dword PeHeaderT<x>::getIddComHeaderSize() const
- {
- return m_inthHeader.dataDirectories[PELIB_IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR].Size;
- }
-
- /**
- * Returns the relative virtual address of an image directory.
- * @param dwDirectory The identifier of an image directory.
- * @return The Rva of the image directory.
- **/
- template<int x>
- dword PeHeaderT<x>::getImageDataDirectoryRva(dword dwDirectory) const
- {
- return m_inthHeader.dataDirectories[dwDirectory].VirtualAddress;
- }
-
- template<int x>
- void PeHeaderT<x>::setImageDataDirectoryRva(dword dwDirectory, dword value)
- {
- m_inthHeader.dataDirectories[dwDirectory].VirtualAddress = value;
- }
-
- /**
- * Returns the size of an image directory.
- * @param dwDirectory The identifier of an image directory.
- * @return The size of the image directory.
- **/
- template<int x>
- dword PeHeaderT<x>::getImageDataDirectorySize(dword dwDirectory) const
- {
- return m_inthHeader.dataDirectories[dwDirectory].Size;
- }
-
- template<int x>
- void PeHeaderT<x>::setImageDataDirectorySize(dword dwDirectory, dword value)
- {
- m_inthHeader.dataDirectories[dwDirectory].Size = value;
- }
-
- /**
- * Returns the name of the section which is specified by the parameter wSectionnr.
- * @param wSectionnr Index of the section.
- * @return The name of the section.
- **/
- template<int x>
- std::string PeHeaderT<x>::getSectionName(word wSectionnr) const
- {
- std::string sectionName = "";
-
- for (unsigned int i=0;i<sizeof(m_vIsh[wSectionnr].Name);i++)
- {
- if (m_vIsh[wSectionnr].Name[i]) sectionName += m_vIsh[wSectionnr].Name[i];
- }
-
- return sectionName;
- }
-
- /**
- * Returns the virtual size of the section which is specified by the parameter wSectionnr.
- * @param wSectionnr Index of the section.
- * @return The virtual size of the section.
- **/
- template<int x>
- dword PeHeaderT<x>::getVirtualSize(word wSectionnr) const
- {
- return m_vIsh[wSectionnr].VirtualSize;
- }
-
- /**
- * Returns the relative virtual address of the section which is specified by the parameter wSectionnr.
- * @param wSectionnr Index of the section.
- * @return The Rva of the section.
- **/
- template<int x>
- dword PeHeaderT<x>::getVirtualAddress(word wSectionnr) const
- {
- return m_vIsh[wSectionnr].VirtualAddress;
- }
-
- /**
- * Returns the size of raw data of the section which is specified by the parameter wSectionnr.
- * @param wSectionnr Index of the section.
- * @return The size of raw data of the section.
- **/
- template<int x>
- dword PeHeaderT<x>::getSizeOfRawData(word wSectionnr) const
- {
- return m_vIsh[wSectionnr].SizeOfRawData;
- }
-
- /**
- * Returns the file offset of the section which is specified by the parameter wSectionnr.
- * @param wSectionnr Index of the section.
- * @return The file offset of the section.
- **/
- template<int x>
- dword PeHeaderT<x>::getPointerToRawData(word wSectionnr) const
- {
- return m_vIsh[wSectionnr].PointerToRawData;
- }
-
- /**
- * Returns the pointer to relocations of the section which is specified by the parameter wSectionnr.
- * @param wSectionnr Index of the section.
- * @return The pointer to relocations of the section.
- **/
- template<int x>
- dword PeHeaderT<x>::getPointerToRelocations(word wSectionnr) const
- {
- return m_vIsh[wSectionnr].PointerToRelocations;
- }
-
- /**
- * Returns the poiner to line numbers of the section which is specified by the parameter wSectionnr.
- * @param wSectionnr Index of the section.
- * @return The pointer to line numbers of the section.
- **/
- template<int x>
- dword PeHeaderT<x>::getPointerToLinenumbers(word wSectionnr) const
- {
- return m_vIsh[wSectionnr].PointerToLinenumbers;
- }
-
- /**
- * Returns the number of relocations of the section which is specified by the parameter wSectionnr.
- * @param wSectionnr Index of the section.
- * @return The number of relocations of the section.
- **/
- template<int x>
- dword PeHeaderT<x>::getNumberOfRelocations(word wSectionnr) const
- {
- return m_vIsh[wSectionnr].NumberOfRelocations;
- }
-
- /**
- * Returns the number of line numbers of the section which is specified by the parameter wSectionnr.
- * @param wSectionnr Index of the section.
- * @return The number of line numbers of the section.
- **/
- template<int x>
- dword PeHeaderT<x>::getNumberOfLinenumbers(word wSectionnr) const
- {
- return m_vIsh[wSectionnr].NumberOfLinenumbers;
- }
-
- /**
- * Returns the characteristics of the section which is specified by the parameter wSectionnr.
- * @param wSectionnr Index of the section.
- * @return The characteristics of the section.
- **/
- template<int x>
- dword PeHeaderT<x>::getCharacteristics(word wSectionnr) const
- {
- return m_vIsh[wSectionnr].Characteristics;
- }
-
- /**
- * Changes the file's Nt signature.
- * @param dwValue New value.
- **/
- template<int x>
- void PeHeaderT<x>::setNtSignature(dword dwValue)
- {
- m_inthHeader.Signature = dwValue;
- }
-
- /**
- * Changes the file's Machine.
- * @param wValue New value.
- **/
- template<int x>
- void PeHeaderT<x>::setMachine(word wValue)
- {
- m_inthHeader.FileHeader.Machine = wValue;
- }
-
- /**
- * Changes the number of sections.
- * @param wValue New value.
- **/
- template<int x>
- void PeHeaderT<x>::setNumberOfSections(word wValue)
- {
- m_inthHeader.FileHeader.NumberOfSections = wValue;
- }
-
- /**
- * Changes the file's TimeDateStamp.
- * @param dwValue New value.
- **/
- template<int x>
- void PeHeaderT<x>::setTimeDateStamp(dword dwValue)
- {
- m_inthHeader.FileHeader.TimeDateStamp = dwValue;
- }
-
- /**
- * Changes the file's PointerToSymbolTable.
- * @param dwValue New value.
- **/
- template<int x>
- void PeHeaderT<x>::setPointerToSymbolTable(dword dwValue)
- {
- m_inthHeader.FileHeader.PointerToSymbolTable = dwValue;
- }
-
- /**
- * Changes the file's NumberOfSymbols.
- * @param dwValue New value.
- **/
- template<int x>
- void PeHeaderT<x>::setNumberOfSymbols(dword dwValue)
- {
- m_inthHeader.FileHeader.NumberOfSymbols = dwValue;
- }
-
- /**
- * Changes the file's SizeOfOptionalHeader.
- * @param wValue New value.
- **/
- template<int x>
- void PeHeaderT<x>::setSizeOfOptionalHeader(word wValue)
- {
- m_inthHeader.FileHeader.SizeOfOptionalHeader = wValue;
- }
-
- /**
- * Changes the file's Characteristics.
- * @param wValue New value.
- **/
- template<int x>
- void PeHeaderT<x>::setCharacteristics(word wValue)
- {
- m_inthHeader.FileHeader.Characteristics = wValue;
- }
-
- /**
- * Changes the file's Magic.
- * @param wValue New value.
- **/
- template<int x>
- void PeHeaderT<x>::setMagic(word wValue)
- {
- m_inthHeader.OptionalHeader.Magic = wValue;
- }
-
- /**
- * Changes the file's MajorLinkerVersion.
- * @param bValue New value.
- **/
- template<int x>
- void PeHeaderT<x>::setMajorLinkerVersion(byte bValue)
- {
- m_inthHeader.OptionalHeader.MajorLinkerVersion = bValue;
- }
-
- /**
- * Changes the file's MinorLinkerVersion.
- * @param bValue New value.
- **/
- template<int x>
- void PeHeaderT<x>::setMinorLinkerVersion(byte bValue)
- {
- m_inthHeader.OptionalHeader.MinorLinkerVersion = bValue;
- }
-
- /**
- * Changes the file's SizeOfCode.
- * @param dwValue New value.
- **/
- template<int x>
- void PeHeaderT<x>::setSizeOfCode(dword dwValue)
- {
- m_inthHeader.OptionalHeader.SizeOfCode = dwValue;
- }
-
- /**
- * Changes the file's SizeOfInitializedData.
- * @param dwValue New value.
- **/
- template<int x>
- void PeHeaderT<x>::setSizeOfInitializedData(dword dwValue)
- {
- m_inthHeader.OptionalHeader.SizeOfInitializedData = dwValue;
- }
-
- /**
- * Changes the file's SizeOfUninitializedData.
- * @param dwValue New value.
- **/
- template<int x>
- void PeHeaderT<x>::setSizeOfUninitializedData(dword dwValue)
- {
- m_inthHeader.OptionalHeader.SizeOfUninitializedData = dwValue;
- }
-
- /**
- * Changes the file's AddressOfEntryPoint.
- * @param dwValue New value.
- **/
- template<int x>
- void PeHeaderT<x>::setAddressOfEntryPoint(dword dwValue)
- {
- m_inthHeader.OptionalHeader.AddressOfEntryPoint = dwValue;
- }
-
- /**
- * Changes the file's BaseOfCode.
- * @param dwValue New value.
- **/
- template<int x>
- void PeHeaderT<x>::setBaseOfCode(dword dwValue)
- {
- m_inthHeader.OptionalHeader.BaseOfCode = dwValue;
- }
-
- /**
- * Changes the file's ImageBase.
- * @param dwValue New value.
- **/
- template<int x>
- void PeHeaderT<x>::setImageBase(typename FieldSizes<x>::VAR4_8 dwValue)
- {
- m_inthHeader.OptionalHeader.ImageBase = dwValue;
- }
-
- /**
- * Changes the file's SectionAlignment.
- * @param dwValue New value.
- **/
- template<int x>
- void PeHeaderT<x>::setSectionAlignment(dword dwValue)
- {
- m_inthHeader.OptionalHeader.SectionAlignment = dwValue;
- }
-
- /**
- * Changes the file's FileAlignment.
- * @param dwValue New value.
- **/
- template<int x>
- void PeHeaderT<x>::setFileAlignment(dword dwValue)
- {
- m_inthHeader.OptionalHeader.FileAlignment = dwValue;
- }
-
- /**
- * Changes the file's MajorOperatingSystemVersion.
- * @param wValue New value.
- **/
- template<int x>
- void PeHeaderT<x>::setMajorOperatingSystemVersion(word wValue)
- {
- m_inthHeader.OptionalHeader.MajorOperatingSystemVersion = wValue;
- }
-
- /**
- * Changes the file's MinorOperatingSystemVersion.
- * @param wValue New value.
- **/
- template<int x>
- void PeHeaderT<x>::setMinorOperatingSystemVersion(word wValue)
- {
- m_inthHeader.OptionalHeader.MinorOperatingSystemVersion = wValue;
- }
-
- /**
- * Changes the file's MajorImageVersion.
- * @param wValue New value.
- **/
- template<int x>
- void PeHeaderT<x>::setMajorImageVersion(word wValue)
- {
- m_inthHeader.OptionalHeader.MajorImageVersion = wValue;
- }
-
- /**
- * Changes the file's MinorImageVersion.
- * @param wValue New value.
- **/
- template<int x>
- void PeHeaderT<x>::setMinorImageVersion(word wValue)
- {
- m_inthHeader.OptionalHeader.MinorImageVersion = wValue;
- }
-
- /**
- * Changes the file's MajorSubsystemVersion.
- * @param wValue New value.
- **/
- template<int x>
- void PeHeaderT<x>::setMajorSubsystemVersion(word wValue)
- {
- m_inthHeader.OptionalHeader.MajorSubsystemVersion = wValue;
- }
-
- /**
- * Changes the file's MinorSubsystemVersion.
- * @param wValue New value.
- **/
- template<int x>
- void PeHeaderT<x>::setMinorSubsystemVersion(word wValue)
- {
- m_inthHeader.OptionalHeader.MinorSubsystemVersion = wValue;
- }
-
- /**
- * Changes the file's Win32VersionValue.
- * @param dwValue New value.
- **/
- template<int x>
- void PeHeaderT<x>::setWin32VersionValue(dword dwValue)
- {
- m_inthHeader.OptionalHeader.Win32VersionValue = dwValue;
- }
-
- /**
- * Changes the file's SizeOfImage.
- * @param dwValue New value.
- **/
- template<int x>
- void PeHeaderT<x>::setSizeOfImage(dword dwValue)
- {
- m_inthHeader.OptionalHeader.SizeOfImage = dwValue;
- }
-
- /**
- * Changes the file's SizeOfHeaders.
- * @param dwValue New value.
- **/
- template<int x>
- void PeHeaderT<x>::setSizeOfHeaders(dword dwValue)
- {
- m_inthHeader.OptionalHeader.SizeOfHeaders = dwValue;
- }
-
- /**
- * Changes the file's CheckSum.
- * @param dwValue New value.
- **/
- template<int x>
- void PeHeaderT<x>::setCheckSum(dword dwValue)
- {
- m_inthHeader.OptionalHeader.CheckSum = dwValue;
- }
-
- /**
- * Changes the file's Subsystem.
- * @param wValue New value.
- **/
- template<int x>
- void PeHeaderT<x>::setSubsystem(word wValue)
- {
- m_inthHeader.OptionalHeader.Subsystem = wValue;
- }
-
- /**
- * Changes the file's DllCharacteristics.
- * @param wValue New value.
- **/
- template<int x>
- void PeHeaderT<x>::setDllCharacteristics(word wValue)
- {
- m_inthHeader.OptionalHeader.DllCharacteristics = wValue;
- }
-
- /**
- * Changes the file's SizeOfStackReserve.
- * @param dwValue New value.
- **/
- template<int x>
- void PeHeaderT<x>::setSizeOfStackReserve(typename FieldSizes<x>::VAR4_8 dwValue)
- {
- m_inthHeader.OptionalHeader.SizeOfStackReserve = dwValue;
- }
-
- /**
- * Changes the file's SizeOfStackCommit.
- * @param dwValue New value.
- **/
- template<int x>
- void PeHeaderT<x>::setSizeOfStackCommit(typename FieldSizes<x>::VAR4_8 dwValue)
- {
- m_inthHeader.OptionalHeader.SizeOfStackCommit = dwValue;
- }
-
- /**
- * Changes the file's SizeOfHeapReserve.
- * @param dwValue New value.
- **/
- template<int x>
- void PeHeaderT<x>::setSizeOfHeapReserve(typename FieldSizes<x>::VAR4_8 dwValue)
- {
- m_inthHeader.OptionalHeader.SizeOfHeapReserve = dwValue;
- }
-
- /**
- * Changes the file's SizeOfHeapCommit.
- * @param dwValue New value.
- **/
- template<int x>
- void PeHeaderT<x>::setSizeOfHeapCommit(typename FieldSizes<x>::VAR4_8 dwValue)
- {
- m_inthHeader.OptionalHeader.SizeOfHeapCommit = dwValue;
- }
-
- /**
- * Changes the file's LoaderFlags.
- * @param dwValue New value.
- **/
- template<int x>
- void PeHeaderT<x>::setLoaderFlags(dword dwValue)
- {
- m_inthHeader.OptionalHeader.LoaderFlags = dwValue;
- }
-
- /**
- * Changes the file's NumberOfRvaAndSizes.
- * @param dwValue New value.
- **/
- template<int x>
- void PeHeaderT<x>::setNumberOfRvaAndSizes(dword dwValue)
- {
- m_inthHeader.OptionalHeader.NumberOfRvaAndSizes = dwValue;
- }
-
- template<int x>
- void PeHeaderT<x>::setIddDebugRva(dword dwValue)
- {
- m_inthHeader.dataDirectories[PELIB_IMAGE_DIRECTORY_ENTRY_DEBUG].VirtualAddress = dwValue;
- }
-
- template<int x>
- void PeHeaderT<x>::setIddDebugSize(dword dwValue)
- {
- m_inthHeader.dataDirectories[PELIB_IMAGE_DIRECTORY_ENTRY_DEBUG].Size = dwValue;
- }
-
- template<int x>
- void PeHeaderT<x>::setIddDelayImportRva(dword dwValue)
- {
- m_inthHeader.dataDirectories[PELIB_IMAGE_DIRECTORY_ENTRY_DELAY_IMPORT].VirtualAddress = dwValue;
- }
-
- template<int x>
- void PeHeaderT<x>::setIddDelayImportSize(dword dwValue)
- {
- m_inthHeader.dataDirectories[PELIB_IMAGE_DIRECTORY_ENTRY_DELAY_IMPORT].Size = dwValue;
- }
-
- template<int x>
- void PeHeaderT<x>::setIddExceptionRva(dword dwValue)
- {
- m_inthHeader.dataDirectories[PELIB_IMAGE_DIRECTORY_ENTRY_EXCEPTION].VirtualAddress = dwValue;
- }
-
- template<int x>
- void PeHeaderT<x>::setIddExceptionSize(dword dwValue)
- {
- m_inthHeader.dataDirectories[PELIB_IMAGE_DIRECTORY_ENTRY_EXCEPTION].Size = dwValue;
- }
-
- template<int x>
- void PeHeaderT<x>::setIddGlobalPtrRva(dword dwValue)
- {
- m_inthHeader.dataDirectories[PELIB_IMAGE_DIRECTORY_ENTRY_GLOBALPTR].VirtualAddress = dwValue;
- }
-
- template<int x>
- void PeHeaderT<x>::setIddGlobalPtrSize(dword dwValue)
- {
- m_inthHeader.dataDirectories[PELIB_IMAGE_DIRECTORY_ENTRY_GLOBALPTR].Size = dwValue;
- }
-
- template<int x>
- void PeHeaderT<x>::setIddIatRva(dword dwValue)
- {
- m_inthHeader.dataDirectories[PELIB_IMAGE_DIRECTORY_ENTRY_IAT].VirtualAddress = dwValue;
- }
-
- template<int x>
- void PeHeaderT<x>::setIddIatSize(dword dwValue)
- {
- m_inthHeader.dataDirectories[PELIB_IMAGE_DIRECTORY_ENTRY_IAT].Size = dwValue;
- }
-
- template<int x>
- void PeHeaderT<x>::setIddLoadConfigRva(dword dwValue)
- {
- m_inthHeader.dataDirectories[PELIB_IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG].VirtualAddress = dwValue;
- }
-
- template<int x>
- void PeHeaderT<x>::setIddLoadConfigSize(dword dwValue)
- {
- m_inthHeader.dataDirectories[PELIB_IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG].Size = dwValue;
- }
-
- template<int x>
- void PeHeaderT<x>::setIddResourceRva(dword dwValue)
- {
- m_inthHeader.dataDirectories[PELIB_IMAGE_DIRECTORY_ENTRY_RESOURCE].VirtualAddress = dwValue;
- }
-
- template<int x>
- void PeHeaderT<x>::setIddResourceSize(dword dwValue)
- {
- m_inthHeader.dataDirectories[PELIB_IMAGE_DIRECTORY_ENTRY_RESOURCE].Size = dwValue;
- }
-
- template<int x>
- void PeHeaderT<x>::setIddSecurityRva(dword dwValue)
- {
- m_inthHeader.dataDirectories[PELIB_IMAGE_DIRECTORY_ENTRY_SECURITY].VirtualAddress = dwValue;
- }
-
- template<int x>
- void PeHeaderT<x>::setIddSecuritySize(dword dwValue)
- {
- m_inthHeader.dataDirectories[PELIB_IMAGE_DIRECTORY_ENTRY_SECURITY].Size = dwValue;
- }
-
- template<int x>
- void PeHeaderT<x>::setIddTlsRva(dword dwValue)
- {
- m_inthHeader.dataDirectories[PELIB_IMAGE_DIRECTORY_ENTRY_TLS].VirtualAddress = dwValue;
- }
-
- template<int x>
- void PeHeaderT<x>::setIddTlsSize(dword dwValue)
- {
- m_inthHeader.dataDirectories[PELIB_IMAGE_DIRECTORY_ENTRY_TLS].Size = dwValue;
- }
-
- /**
- * Changes the rva of the file's export directory.
- * @param dwValue New value.
- **/
- template<int x>
- void PeHeaderT<x>::setIddExportRva(dword dwValue)
- {
- m_inthHeader.dataDirectories[PELIB_IMAGE_DIRECTORY_ENTRY_EXPORT].VirtualAddress = dwValue;
- }
-
- /**
- * Changes the size of the file's export directory.
- * @param dwValue New value.
- **/
- template<int x>
- void PeHeaderT<x>::setIddExportSize(dword dwValue)
- {
- m_inthHeader.dataDirectories[PELIB_IMAGE_DIRECTORY_ENTRY_EXPORT].Size = dwValue;
- }
-
- template<int x>
- void PeHeaderT<x>::setIddBaseRelocRva(dword value)
- {
- m_inthHeader.dataDirectories[PELIB_IMAGE_DIRECTORY_ENTRY_BASERELOC].VirtualAddress = value;
- }
-
- template<int x>
- void PeHeaderT<x>::setIddBaseRelocSize(dword value)
- {
- m_inthHeader.dataDirectories[PELIB_IMAGE_DIRECTORY_ENTRY_BASERELOC].Size = value;
- }
-
- template<int x>
- void PeHeaderT<x>::setIddArchitectureRva(dword value)
- {
- m_inthHeader.dataDirectories[PELIB_IMAGE_DIRECTORY_ENTRY_ARCHITECTURE].VirtualAddress = value;
- }
-
- template<int x>
- void PeHeaderT<x>::setIddArchitectureSize(dword value)
- {
- m_inthHeader.dataDirectories[PELIB_IMAGE_DIRECTORY_ENTRY_ARCHITECTURE].Size = value;
- }
-
- template<int x>
- void PeHeaderT<x>::setIddComHeaderRva(dword value)
- {
- m_inthHeader.dataDirectories[PELIB_IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR].VirtualAddress = value;
- }
-
- template<int x>
- void PeHeaderT<x>::setIddComHeaderSize(dword value)
- {
- m_inthHeader.dataDirectories[PELIB_IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR].Size = value;
- }
-
- /**
- * Changes the rva of the file's import directory.
- * @param dwValue New value.
- **/
- template<int x>
- void PeHeaderT<x>::setIddImportRva(dword dwValue)
- {
- m_inthHeader.dataDirectories[PELIB_IMAGE_DIRECTORY_ENTRY_IMPORT].VirtualAddress = dwValue;
- }
-
- /**
- * Changes the size of the file's import directory.
- * @param dwValue New value.
- **/
- template<int x>
- void PeHeaderT<x>::setIddImportSize(dword dwValue)
- {
- m_inthHeader.dataDirectories[PELIB_IMAGE_DIRECTORY_ENTRY_IMPORT].Size = dwValue;
- }
-
- /**
- * Changes the name of a section.
- * @param wSectionnr Identifier of the section
- * @param strName New name.
- **/
- template<int x>
- void PeHeaderT<x>::setSectionName(word wSectionnr, std::string strName)
- {
- strncpy(reinterpret_cast<char*>(m_vIsh[wSectionnr].Name), strName.c_str(), sizeof(m_vIsh[wSectionnr].Name));
- }
-
- /**
- * Changes the virtual size of a section.
- * @param wSectionnr Identifier of the section
- * @param dwValue New value.
- **/
- template<int x>
- void PeHeaderT<x>::setVirtualSize(word wSectionnr, dword dwValue)
- {
- m_vIsh[wSectionnr].VirtualSize = dwValue;
- }
-
- /**
- * Changes the virtual address of a section.
- * @param wSectionnr Identifier of the section
- * @param dwValue New value.
- **/
- template<int x>
- void PeHeaderT<x>::setVirtualAddress(word wSectionnr, dword dwValue)
- {
- m_vIsh[wSectionnr].VirtualAddress = dwValue;
- }
-
- /**
- * Changes the size of raw data of a section.
- * @param wSectionnr Identifier of the section
- * @param dwValue New value.
- **/
- template<int x>
- void PeHeaderT<x>::setSizeOfRawData(word wSectionnr, dword dwValue)
- {
- m_vIsh[wSectionnr].SizeOfRawData = dwValue;
- }
-
- /**
- * Changes the size of raw data of a section.
- * @param wSectionnr Identifier of the section
- * @param dwValue New value.
- **/
- template<int x>
- void PeHeaderT<x>::setPointerToRawData(word wSectionnr, dword dwValue)
- {
- m_vIsh[wSectionnr].PointerToRawData = dwValue;
- }
-
- /**
- * Changes the pointer to relocations of a section.
- * @param wSectionnr Identifier of the section
- * @param dwValue New value.
- **/
- template<int x>
- void PeHeaderT<x>::setPointerToRelocations(word wSectionnr, dword dwValue)
- {
- m_vIsh[wSectionnr].PointerToRelocations = dwValue;
- }
-
- /**
- * Changes the pointer to line numbers of a section.
- * @param wSectionnr Identifier of the section
- * @param dwValue New value.
- **/
- template<int x>
- void PeHeaderT<x>::setPointerToLinenumbers(word wSectionnr, dword dwValue)
- {
- m_vIsh[wSectionnr].PointerToLinenumbers = dwValue;
- }
-
- /**
- * Changes the number of relocations of a section.
- * @param wSectionnr Identifier of the section
- * @param dwValue New value.
- **/
- template<int x>
- void PeHeaderT<x>::setNumberOfRelocations(word wSectionnr, dword dwValue)
- {
- m_vIsh[wSectionnr].NumberOfRelocations = dwValue;
- }
-
- /**
- * Changes the number of line numbers of a section.
- * @param wSectionnr Identifier of the section
- * @param dwValue New value.
- **/
- template<int x>
- void PeHeaderT<x>::setNumberOfLinenumbers(word wSectionnr, dword dwValue)
- {
- m_vIsh[wSectionnr].NumberOfLinenumbers = dwValue;
- }
-
- /**
- * Changes the characteristics of a section.
- * @param wSectionnr Identifier of the section
- * @param dwValue New value.
- **/
- template<int x>
- void PeHeaderT<x>::setCharacteristics(word wSectionnr, dword dwValue)
- {
- m_vIsh[wSectionnr].Characteristics = dwValue;
- }
-
-}
-
-#endif
+/*
+* PeHeader.h - Part of the PeLib library.
+*
+* Copyright (c) 2004 - 2005 Sebastian Porst (webmaster@the-interweb.com)
+* All rights reserved.
+*
+* This software is licensed under the zlib/libpng License.
+* For more details see http://www.opensource.org/licenses/zlib-license.php
+* or the license information file (license.htm) in the root directory
+* of PeLib.
+*/
+
+#ifndef PEHEADER_H
+#define PEHEADER_H
+
+#include "PeLibAux.h"
+
+namespace PeLib
+{
+ class PeHeader
+ {
+// protected:
+// virtual void readBaseOfData(InputBuffer& ibBuffer) = 0;
+// virtual void rebuildBaseOfData(OutputBuffer& obBuffer) const = 0;
+
+ public:
+ virtual ~PeHeader(){};
+ };
+
+ /// Class that handles the PE header of files.
+ /**
+ * This class can read and modify PE headers. It provides set- and get functions to access
+ * all individual members of a PE header. Furthermore it's possible to validate and rebuild
+ * PE headers. A PE header includes the IMAGE_Nt_HEADERS and the section definitions of a PE file.
+ * \todo getIdReservedRva
+ **/
+ template<int x>
+ class PeHeaderT : public PeHeader
+ {
+ private:
+ void readBaseOfData(InputBuffer& ibBuffer, PELIB_IMAGE_NT_HEADERS<x>& header) const;
+ void rebuildBaseOfData(OutputBuffer& obBuffer) const;
+
+ protected:
+ std::vector<PELIB_IMAGE_SECTION_HEADER> m_vIsh; ///< Stores section header information.
+ PELIB_IMAGE_NT_HEADERS<x> m_inthHeader; ///< Stores Nt header information.
+ dword m_uiOffset; ///< Equivalent to the value returned by #PeLib::MzHeader::getAddressOfPeFile
+
+ public:
+ typedef typename FieldSizes<x>::VAR4_8 VAR4_8;
+
+ PeHeaderT() : m_uiOffset(0)
+ {
+ }
+
+ /// Add a section to the header.
+ int addSection(const std::string& strName, dword dwSize); // EXPORT
+
+ unsigned int calcSizeOfImage() const; // EXPORT
+
+ /// Returns the unused space after the header.
+ unsigned int calcSpaceAfterHeader() const; // EXPORT
+
+ /// Returns the address of the physically first section (not the first defined section).
+ unsigned int calcStartOfCode() const; // EXPORT
+
+ /// Calculates the offset for a new section of size uiSize.
+ unsigned int calcOffset() const; // EXPORT
+
+ /// Calculates the Rva for a new section of size uiSize.
+ unsigned int calcRva() const; // EXPORT
+
+ /// Returns the number of sections in the current file.
+ word calcNumberOfSections() const; // EXPORT
+
+ void enlargeLastSection(unsigned int uiSize); // EXPORT
+
+ /// Returns the section Id of the section that contains the offset.
+ word getSectionWithOffset(VAR4_8 dwOffset) const; // EXPORT
+
+ /// Returns the number of the section which the given relative address points to.
+ word getSectionWithRva(VAR4_8 rva) const; // EXPORT
+
+ bool isValid() const; // EXPORT
+ bool isValid(unsigned int foo) const; // EXPORT
+
+ /// Corrects the current PE header.
+ void makeValid(dword dwOffset); // EXPORT
+
+ /// Converts a file offset to a relative virtual offset.
+ unsigned int offsetToRva(VAR4_8 dwOffset) const; // EXPORT
+
+ /// Converts a file offset to a virtual address.
+ unsigned int offsetToVa(VAR4_8 dwOffset) const; // EXPORT
+
+ /// Reads the PE header of a file.
+ int read(std::string strFilename, unsigned int uiOffset); // EXPORT
+
+ int read(const unsigned char* pcBuffer, unsigned int uiSize, unsigned int uiOffset); // EXPORT
+
+ void readHeader(InputBuffer& ibBuffer, PELIB_IMAGE_NT_HEADERS<x>& header) const;
+ void readDataDirectories(InputBuffer& ibBuffer, PELIB_IMAGE_NT_HEADERS<x>& header) const;
+ std::vector<PELIB_IMAGE_SECTION_HEADER> readSections(InputBuffer& ibBuffer, PELIB_IMAGE_NT_HEADERS<x>& header) const;
+
+ /// Rebuilds the current PE header.
+ void rebuild(std::vector<byte>& vBuffer) const; // EXPORT
+
+ /// Converts a relative virtual address to a file offset.
+ VAR4_8 rvaToOffset(VAR4_8 dwRva) const; // EXPORT
+
+ /// Converts a relative virtual address to a virtual address.
+ VAR4_8 rvaToVa(VAR4_8 dwRva) const; // EXPORT
+
+ /// Calculates the size for the current PE header including all section definitions.
+ unsigned int size() const;
+
+ VAR4_8 vaToRva(VAR4_8 dwRva) const; // EXPORT
+ VAR4_8 vaToOffset(VAR4_8 dwRva) const; // EXPORT
+
+ /// Save the PE header to a file.
+ int write(std::string strFilename, unsigned int uiOffset) const; // EXPORT
+
+ /// Writes sections to a file.
+ int writeSections(const std::string& strFilename) const; // EXPORT
+ /// Overwrites a section with new data.
+ int writeSectionData(const std::string& strFilename, word wSecnr, const std::vector<byte>& vBuffer) const; // EXPORT
+
+// header getters
+ /// Returns the Signature value of the header.
+ dword getNtSignature() const; // EXPORT
+ /// Returns the Machine value of the header.
+ word getMachine() const; // EXPORT
+ /// Returns the Sections value of the header.
+ word getNumberOfSections() const; // EXPORT
+ /// Returns the TimeDateStamp value of the header.
+ dword getTimeDateStamp() const; // EXPORT
+ /// Returns the PointerToSymbolTable value of the header.
+ dword getPointerToSymbolTable() const; // EXPORT
+ /// Returns the NumberOfSymbols value of the header.
+ dword getNumberOfSymbols() const; // EXPORT
+ /// Returns the SizeOfOptionalHeader value of the header.
+ word getSizeOfOptionalHeader() const; // EXPORT
+ /// Returns the Characteristics value of the header.
+ word getCharacteristics() const; // EXPORT
+
+ /// Returns the Magic value of the header.
+ word getMagic() const; // EXPORT
+ /// Returns the MajorLinkerVersion value of the header.
+ byte getMajorLinkerVersion() const; // EXPORT
+ /// Returns the MinorLinkerVersion value of the header.
+ byte getMinorLinkerVersion() const; // EXPORT
+ /// Returns the SizeOfCode value of the header.
+ dword getSizeOfCode() const; // EXPORT
+ /// Returns the SizeOfInitializedData value of the header.
+ dword getSizeOfInitializedData() const; // EXPORT
+ /// Returns the SizeOfUninitializedData value of the header.
+ dword getSizeOfUninitializedData() const; // EXPORT
+ /// Returns the AddressOfEntryPoint value of the header.
+ dword getAddressOfEntryPoint() const; // EXPORT
+ /// Returns the BaseOfCode value of the header.
+ dword getBaseOfCode() const; // EXPORT
+ /// Returns the ImageBase value of the header.
+ VAR4_8 getImageBase() const; // EXPORT
+ /// Returns the SectionAlignment value of the header.
+ dword getSectionAlignment() const; // EXPORT
+ /// Returns the FileAlignment value of the header.
+ dword getFileAlignment() const; // EXPORT
+ /// Returns the MajorOperatingSystemVersion value of the header.
+ word getMajorOperatingSystemVersion() const; // EXPORT
+ /// Returns the MinorOperatingSystemVersion value of the header.
+ word getMinorOperatingSystemVersion() const; // EXPORT
+ /// Returns the MajorImageVersion value of the header.
+ word getMajorImageVersion() const; // EXPORT
+ /// Returns the MinorImageVersion value of the header.
+ word getMinorImageVersion() const; // EXPORT
+ /// Returns the MajorSubsystemVersion value of the header.
+ word getMajorSubsystemVersion() const; // EXPORT
+ /// Returns the MinorSubsystemVersion value of the header.
+ word getMinorSubsystemVersion() const; // EXPORT
+ /// Returns the Reserved1 value of the header.
+ dword getWin32VersionValue() const; // EXPORT
+ /// Returns the SizeOfImage value of the header.
+ dword getSizeOfImage() const; // EXPORT
+ /// Returns the SizeOfHeaders value of the header.
+ dword getSizeOfHeaders() const; // EXPORT
+ /// Returns the CheckSum value of the header.
+ dword getCheckSum() const; // EXPORT
+ /// Returns the Subsystem value of the header.
+ word getSubsystem() const; // EXPORT
+ /// Returns the DllCharacteristics value of the header.
+ word getDllCharacteristics() const; // EXPORT
+ /// Returns the SizeOfStackReserve value of the header.
+ VAR4_8 getSizeOfStackReserve() const; // EXPORT
+ /// Returns the SizeOfStackCommit value of the header.
+ VAR4_8 getSizeOfStackCommit() const; // EXPORT
+ /// Returns the SizeOfHeapReserve value of the header.
+ VAR4_8 getSizeOfHeapReserve() const; // EXPORT
+ /// Returns the SizeOfHeapCommit value of the header.
+ VAR4_8 getSizeOfHeapCommit() const; // EXPORT
+ /// Returns the LoaderFlags value of the header.
+ dword getLoaderFlags() const; // EXPORT
+ /// Returns the NumberOfRvaAndSizes value of the header.
+ dword getNumberOfRvaAndSizes() const; // EXPORT
+ dword calcNumberOfRvaAndSizes() const; // EXPORT
+
+ void addDataDirectory(); // EXPORT
+ void removeDataDirectory(dword index); // EXPORT
+
+// image directory getters
+ /// Returns the relative virtual address of the image directory Export.
+ dword getIddExportRva() const; // EXPORT
+ /// Returns the size of the image directory Export.
+ dword getIddExportSize() const; // EXPORT
+ /// Returns the relative virtual address of the image directory Import.
+ dword getIddImportRva() const; // EXPORT
+ /// Returns the size of the image directory Import.
+ dword getIddImportSize() const; // EXPORT
+ /// Returns the relative virtual address of the image directory Resource.
+ dword getIddResourceRva() const; // EXPORT
+ /// Returns the size of the image directory Resource.
+ dword getIddResourceSize() const; // EXPORT
+ /// Returns the relative virtual address of the image directory Exception.
+ dword getIddExceptionRva() const; // EXPORT
+ /// Returns the size of the image directory Exception.
+ dword getIddExceptionSize() const; // EXPORT
+ /// Returns the relative virtual address of the image directory Security.
+ dword getIddSecurityRva() const; // EXPORT
+ /// Returns the size of the image directory Security.
+ dword getIddSecuritySize() const; // EXPORT
+ /// Returns the relative virtual address of the image directory Base Reloc.
+ dword getIddBaseRelocRva() const; // EXPORT
+ /// Returns the size of the image directory Base Reloc.
+ dword getIddBaseRelocSize() const; // EXPORT
+ /// Returns the relative virtual address of the image directory Debug.
+ dword getIddDebugRva() const; // EXPORT
+ /// Returns the size of the image directory Debug.
+ dword getIddDebugSize() const; // EXPORT
+ /// Returns the relative virtual address of the image directory Architecture.
+ dword getIddArchitectureRva() const; // EXPORT
+ /// Returns the size of the image directory Architecture.
+ dword getIddArchitectureSize() const; // EXPORT
+ /// Returns the relative virtual address of the image directory GlobalPtr.
+ dword getIddGlobalPtrRva() const; // EXPORT
+ /// Returns the size of the image directory GlobalPtr.
+ dword getIddGlobalPtrSize() const; // EXPORT
+ /// Returns the relative virtual address of the image directory Tls.
+ dword getIddTlsRva() const; // EXPORT
+ /// Returns the size of the image directory Tls.
+ dword getIddTlsSize() const; // EXPORT
+ /// Returns the relative virtual address of the image directory LoadConfig.
+ dword getIddLoadConfigRva() const; // EXPORT
+ /// Returns the size of the image directory LoadConfig.
+ dword getIddLoadConfigSize() const; // EXPORT
+ /// Returns the relative virtual address of the image directory BoundImport.
+ dword getIddBoundImportRva() const; // EXPORT
+ /// Returns the size of the image directory BoundImport.
+ dword getIddBoundImportSize() const; // EXPORT
+ /// Returns the relative virtual address of the image directory Iat.
+ dword getIddIatRva() const; // EXPORT
+ /// Returns the size of the image directory Iat.
+ dword getIddIatSize() const; // EXPORT
+ /// Returns the relative virtual address of the image directory DelayImport.
+ dword getIddDelayImportRva() const; // EXPORT
+ /// Returns the size of the image directory DelayImport.
+ dword getIddDelayImportSize() const; // EXPORT
+ /// Returns the relative virtual address of the image directory COM Descriptor.
+ dword getIddComHeaderRva() const; // EXPORT
+ /// Returns the size of the image directory COM Descriptor.
+ dword getIddComHeaderSize() const; // EXPORT
+
+ /// Returns the relative virtual address of an image directory.
+ dword getImageDataDirectoryRva(dword dwDirectory) const; // EXPORT
+ /// Returns the size of an image directory.
+ dword getImageDataDirectorySize(dword dwDirectory) const; // EXPORT
+
+ void setImageDataDirectoryRva(dword dwDirectory, dword value); // EXPORT
+ void setImageDataDirectorySize(dword dwDirectory, dword value); // EXPORT
+
+// section getters
+ /// Returns the name of a section.
+ std::string getSectionName(word uiSectionnr) const; // EXPORT
+ /// Returns the virtual size of a section.
+ dword getVirtualSize(word uiSectionnr) const; // EXPORT
+ /// Returns the virtual address of a section.
+ dword getVirtualAddress(word uiSectionnr) const; // EXPORT
+ /// Returns the size of a section's raw data.
+ dword getSizeOfRawData(word uiSectionnr) const; // EXPORT
+ /// Returns file offset of the data of a section.
+ dword getPointerToRawData(word uiSectionnr) const; // EXPORT
+ /// Returns the rva of the relocations of a section.
+ dword getPointerToRelocations(word uiSectionnr) const; // EXPORT
+ /// Returns the rva of the line numbers of a section.
+ dword getPointerToLinenumbers(word uiSectionnr) const; // EXPORT
+ /// Returns the number of relocations of a section.
+ dword getNumberOfRelocations(word uiSectionnr) const; // EXPORT
+ /// Returns the number of line numbers of a section.
+ dword getNumberOfLinenumbers(word uiSectionnr) const; // EXPORT
+ /// Returns the characteristics of a section.
+ dword getCharacteristics(word uiSectionnr) const; // EXPORT _section
+
+// header setters
+ /// Sets the Signature value of the header.
+ void setNtSignature(dword value); // EXPORT
+ /// Sets the Machine value of the header.
+ void setMachine(word value); // EXPORT
+ /// Sets the Sections value of the header.
+ void setNumberOfSections(word value); // EXPORT
+ /// Sets the TimeDateStamp value of the header.
+ void setTimeDateStamp(dword value); // EXPORT
+ /// Sets the PointerToSymbolTable value of the header.
+ void setPointerToSymbolTable(dword value); // EXPORT
+ /// Sets the NumberOfSymbols value of the header.
+ void setNumberOfSymbols(dword value); // EXPORT
+ /// Sets the SizeOfOptionalHeader value of the header.
+ void setSizeOfOptionalHeader(word value); // EXPORT
+ /// Sets the Characteristics value of the header.
+ void setCharacteristics(word value); // EXPORT _section
+
+ /// Sets the Magic value of the header.
+ void setMagic(word value); // EXPORT
+ /// Sets the MajorLinkerVersion value of the header.
+ void setMajorLinkerVersion(byte value); // EXPORT
+ /// Sets the MinorLinkerVersion value of the header.
+ void setMinorLinkerVersion(byte value); // EXPORT
+ /// Sets the SizeOfCode value of the header.
+ void setSizeOfCode(dword value); // EXPORT
+ /// Sets the SizeOfInitializedData value of the header.
+ void setSizeOfInitializedData(dword value); // EXPORT
+ /// Sets the SizeOfUninitializedData value of the header.
+ void setSizeOfUninitializedData(dword value); // EXPORT
+ /// Sets the AddressOfEntryPoint value of the header.
+ void setAddressOfEntryPoint(dword value); // EXPORT
+ /// Sets the BaseOfCode value of the header.
+ void setBaseOfCode(dword value); // EXPORT
+ /// Sets the ImageBase value of the header.
+ void setImageBase(VAR4_8 value); // EXPORT
+ /// Sets the SectionAlignment value of the header.
+ void setSectionAlignment(dword value); // EXPORT
+ /// Sets the FileAlignment value of the header.
+ void setFileAlignment(dword value); // EXPORT
+ /// Sets the MajorOperatingSystemVersion value of the header.
+ void setMajorOperatingSystemVersion(word value); // EXPORT
+ /// Sets the MinorOperatingSystemVersion value of the header.
+ void setMinorOperatingSystemVersion(word value); // EXPORT
+ /// Sets the MajorImageVersion value of the header.
+ void setMajorImageVersion(word value); // EXPORT
+ /// Sets the MinorImageVersion value of the header.
+ void setMinorImageVersion(word value); // EXPORT
+ /// Sets the MajorSubsystemVersion value of the header.
+ void setMajorSubsystemVersion(word value); // EXPORT
+ /// Sets the MinorSubsystemVersion value of the header.
+ void setMinorSubsystemVersion(word value); // EXPORT
+ /// Sets the Reserved1 value of the header.
+ void setWin32VersionValue(dword value); // EXPORT
+ /// Sets the SizeOfImage value of the header.
+ void setSizeOfImage(dword value); // EXPORT
+ /// Sets the SizeOfHeaders value of the header.
+ void setSizeOfHeaders(dword value); // EXPORT
+ /// Sets the CheckSum value of the header.
+ void setCheckSum(dword value); // EXPORT
+ /// Sets the Subsystem value of the header.
+ void setSubsystem(word value); // EXPORT
+ /// Sets the DllCharacteristics value of the header.
+ void setDllCharacteristics(word value); // EXPORT
+ /// Sets the SizeOfStackReserve value of the header.
+ void setSizeOfStackReserve(VAR4_8 value); // EXPORT
+ /// Sets the SizeOfStackCommit value of the header.
+ void setSizeOfStackCommit(VAR4_8 value); // EXPORT
+ /// Sets the SizeOfHeapReserve value of the header.
+ void setSizeOfHeapReserve(VAR4_8 value); // EXPORT
+ /// Sets the SizeOfHeapCommit value of the header.
+ void setSizeOfHeapCommit(VAR4_8 value); // EXPORT
+ /// Sets the LoaderFlags value of the header.
+ void setLoaderFlags(dword value); // EXPORT
+ /// Sets the NumberOfRvaAndSizes value of the header.
+ void setNumberOfRvaAndSizes(dword value); // EXPORT
+
+// image directory getters
+ void setIddDebugRva(dword dwValue); // EXPORT
+ void setIddDebugSize(dword dwValue); // EXPORT
+ void setIddDelayImportRva(dword dwValue); // EXPORT
+ void setIddDelayImportSize(dword dwValue); // EXPORT
+ void setIddExceptionRva(dword dwValue); // EXPORT
+ void setIddExceptionSize(dword dwValue); // EXPORT
+ void setIddGlobalPtrRva(dword dwValue); // EXPORT
+ void setIddGlobalPtrSize(dword dwValue); // EXPORT
+ void setIddIatRva(dword dwValue); // EXPORT
+ void setIddIatSize(dword dwValue); // EXPORT
+ void setIddLoadConfigRva(dword dwValue); // EXPORT
+ void setIddLoadConfigSize(dword dwValue); // EXPORT
+ void setIddResourceRva(dword dwValue); // EXPORT
+ void setIddResourceSize(dword dwValue); // EXPORT
+ void setIddSecurityRva(dword dwValue); // EXPORT
+ void setIddSecuritySize(dword dwValue); // EXPORT
+ void setIddTlsRva(dword dwValue); // EXPORT
+ void setIddTlsSize(dword dwValue); // EXPORT
+
+ void setIddImportRva(dword dwValue); // EXPORT
+ void setIddImportSize(dword dwValue); // EXPORT
+ void setIddExportRva(dword dwValue); // EXPORT
+ void setIddExportSize(dword dwValue); // EXPORT
+
+ void setIddBaseRelocRva(dword value); // EXPORT
+ void setIddBaseRelocSize(dword value); // EXPORT
+ void setIddArchitectureRva(dword value); // EXPORT
+ void setIddArchitectureSize(dword value); // EXPORT
+ void setIddComHeaderRva(dword value); // EXPORT
+ void setIddComHeaderSize(dword value); // EXPORT
+
+ /// Set the name of a section.
+ void setSectionName(word uiSectionnr, std::string strName); // EXPORT
+ /// Set the virtual size of a section.
+ void setVirtualSize(word uiSectionnr, dword dwValue); // EXPORT
+ /// Set the virtual address of a section.
+ void setVirtualAddress(word uiSectionnr, dword dwValue); // EXPORT
+ /// Set the size of raw data of a section.
+ void setSizeOfRawData(word uiSectionnr, dword dwValue); // EXPORT
+ /// Set the file offset of a section.
+ void setPointerToRawData(word uiSectionnr, dword dwValue); // EXPORT
+ /// Set the pointer to relocations of a section.
+ void setPointerToRelocations(word uiSectionnr, dword dwValue); // EXPORT
+ /// Set the pointer to linenumbers of a section.
+ void setPointerToLinenumbers(word uiSectionnr, dword dwValue); // EXPORT
+ /// Set the number of relocations a section.
+ void setNumberOfRelocations(word uiSectionnr, dword dwValue); // EXPORT
+ /// Set the number of linenumbers section.
+ void setNumberOfLinenumbers(word uiSectionnr, dword dwValue); // EXPORT
+ /// Set the characteristics of a section.
+ void setCharacteristics(word uiSectionnr, dword dwValue); // EXPORT
+ };
+
+ class PeHeader32 : public PeHeaderT<32>
+ {
+ public:
+ /// Returns the BaseOfData value of the header.
+ dword getBaseOfData() const; // EXPORT
+ /// Sets the BaseOfData value of the header.
+ void setBaseOfData(dword value); // EXPORT
+ };
+
+ class PeHeader64 : public PeHeaderT<64>
+ {
+ };
+
+ template<int x>
+ void PeHeaderT<x>::addDataDirectory()
+ {
+ m_inthHeader.dataDirectories.push_back(PELIB_IMAGE_DATA_DIRECTORY());
+ }
+
+ template<int x>
+ void PeHeaderT<x>::removeDataDirectory(dword index)
+ {
+ m_inthHeader.dataDirectories.erase(m_inthHeader.dataDirectories.begin() + index);
+ }
+
+ /**
+ * Adds a new section to the header. The physical and virtual address as well as the virtual
+ * size of the section will be determined automatically from the raw size. The section
+ * characteristics will be set to IMAGE_SCN_MEM_WRITE | IMAGE_SCN_MEM_READ |
+ * IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_CNT_CODE. All other values will be set to 0.
+ * Note: It's important that if the current header's FileAlignment and/or SectionAlignment values are
+ * 0 this function will fail.
+ * @param strName Name of the new section. If this name is longer than 8 bytes only the first 8 bytes will be used.
+ * @param dwSize Physical size of the new section in bytes.
+ * \todo Better code that handles files with 0 sections.
+ **/
+ template<int x>
+ int PeHeaderT<x>::addSection(const std::string& strName, dword dwSize)
+ {
+ unsigned int uiSecnr = calcNumberOfSections();
+
+ if (!getFileAlignment())
+ {
+ return ERROR_NO_FILE_ALIGNMENT;
+ }
+ else if (!getSectionAlignment())
+ {
+ return ERROR_NO_SECTION_ALIGNMENT;
+ }
+
+ if (uiSecnr) // Always allow 1 section.
+ {
+ if (uiSecnr == 0xFFFF)
+ {
+ return ERROR_TOO_MANY_SECTIONS;
+ }
+ else if (calcSpaceAfterHeader() < PELIB_IMAGE_SECTION_HEADER::size())
+ {
+ return ERROR_NOT_ENOUGH_SPACE;
+ }
+ }
+
+ dword dwOffset = calcOffset(/*dwSize*/);
+ dword dwRva = calcRva(/*dwSize*/);
+
+ PELIB_IMAGE_SECTION_HEADER ishdCurr;
+ m_vIsh.push_back(ishdCurr);
+
+ setSectionName(uiSecnr, strName);
+ setSizeOfRawData(uiSecnr, alignOffset(dwSize, getFileAlignment()));
+ setPointerToRawData(uiSecnr, dwOffset);
+ setVirtualSize(uiSecnr, alignOffset(dwSize, getSectionAlignment()));
+ setVirtualAddress(uiSecnr, dwRva);
+ setCharacteristics(uiSecnr, PELIB_IMAGE_SCN_MEM_WRITE | PELIB_IMAGE_SCN_MEM_READ | PELIB_IMAGE_SCN_CNT_INITIALIZED_DATA | PELIB_IMAGE_SCN_CNT_CODE);
+
+ return NO_ERROR;
+ }
+
+ /**
+ * Calculates a valid SizeOfImage value given the information from the current PE header.
+ * Note that this calculation works in Win2K but probably does not work in Win9X. I didn't test that though.
+ * @return Valid SizeOfImage value.
+ **/
+ template<int x>
+ unsigned int PeHeaderT<x>::calcSizeOfImage() const
+ {
+ // Major note here: It's possible for sections to exist with a Virtual Size of 0.
+ // That's why it's necessary to use std::max(Vsize, RawSize) here.
+ // An example for such a file is dbeng6.exe (made by Sybase).
+ // In this file each and every section has a VSize of 0 but it still runs.
+
+ std::vector<PELIB_IMAGE_SECTION_HEADER>::const_iterator ishLastSection = std::max_element(m_vIsh.begin(), m_vIsh.end(), std::mem_fun_ref(&PELIB_IMAGE_SECTION_HEADER::biggerVirtualAddress));
+ if (ishLastSection->VirtualSize != 0) return ishLastSection->VirtualAddress + ishLastSection->VirtualSize;
+ return ishLastSection->VirtualAddress + std::max(ishLastSection->VirtualSize, ishLastSection->SizeOfRawData);
+ }
+
+ /**
+ * Calculates the space between the last byte of the header and the first byte that's used for something
+ * else (that's either the first section or an image directory).
+ * @return Unused space after the header.
+ * \todo There are PE files with sections beginning at offset 0. They
+ * need to be considered.
+ **/
+ template<int x>
+ unsigned int PeHeaderT<x>::calcSpaceAfterHeader() const
+ {
+ return (calcStartOfCode() > size() + m_uiOffset) ? calcStartOfCode() - (size() + m_uiOffset) : 0;
+ }
+
+ /**
+ * Returns the first offset of the file that's actually used for something different than the header.
+ * That something is not necessarily code, it can be a data directory too.
+ * This offset can be the beginning of a section or the beginning of a directory.
+ * \todo Some optimizization is surely possible here.
+ * \todo There are PE files with sections beginning at offset 0. They
+ * need to be considered. Returning 0 for these files doesn't really make sense.
+ * So far these sections are disregarded.
+ **/
+ template<int x>
+ unsigned int PeHeaderT<x>::calcStartOfCode() const
+ {
+ unsigned int directories = calcNumberOfRvaAndSizes();
+ dword dwMinOffset = 0xFFFFFFFF;
+ if (directories >= 1 && getIddExportRva() && rvaToOffset(getIddExportRva()) < dwMinOffset) dwMinOffset = rvaToOffset(getIddExportRva());
+ if (directories >= 2 && getIddImportRva() && rvaToOffset(getIddImportRva()) < dwMinOffset) dwMinOffset = rvaToOffset(getIddImportRva());
+ if (directories >= 3 && getIddResourceRva() && rvaToOffset(getIddResourceRva()) < dwMinOffset) dwMinOffset = rvaToOffset(getIddResourceRva());
+ if (directories >= 4 && getIddExceptionRva() && rvaToOffset(getIddExceptionRva()) < dwMinOffset) dwMinOffset = rvaToOffset(getIddExceptionRva());
+ if (directories >= 5 && getIddSecurityRva() && rvaToOffset(getIddSecurityRva()) < dwMinOffset) dwMinOffset = rvaToOffset(getIddSecurityRva());
+ if (directories >= 6 && getIddBaseRelocRva() && rvaToOffset(getIddBaseRelocRva()) < dwMinOffset) dwMinOffset = rvaToOffset(getIddBaseRelocRva());
+ if (directories >= 7 && getIddDebugRva() && rvaToOffset(getIddDebugRva()) < dwMinOffset) dwMinOffset = rvaToOffset(getIddDebugRva());
+ if (directories >= 8 && getIddArchitectureRva() && rvaToOffset(getIddArchitectureRva()) < dwMinOffset) dwMinOffset = rvaToOffset(getIddArchitectureRva());
+ if (directories >= 9 && getIddGlobalPtrRva() && rvaToOffset(getIddGlobalPtrRva()) < dwMinOffset) dwMinOffset = rvaToOffset(getIddGlobalPtrRva());
+ if (directories >= 10 && getIddTlsRva() && rvaToOffset(getIddTlsRva()) < dwMinOffset) dwMinOffset = rvaToOffset(getIddTlsRva());
+ if (directories >= 11 && getIddLoadConfigRva() && rvaToOffset(getIddLoadConfigRva()) < dwMinOffset) dwMinOffset = rvaToOffset(getIddLoadConfigRva());
+ if (directories >= 12 && getIddBoundImportRva() && rvaToOffset(getIddBoundImportRva()) < dwMinOffset) dwMinOffset = rvaToOffset(getIddBoundImportRva());
+ if (directories >= 13 && getIddIatRva() && rvaToOffset(getIddIatRva()) < dwMinOffset) dwMinOffset = rvaToOffset(getIddIatRva());
+ if (directories >= 14 && getIddDelayImportRva() && rvaToOffset(getIddDelayImportRva()) < dwMinOffset) dwMinOffset = rvaToOffset(getIddDelayImportRva());
+ if (directories >= 15 && getIddComHeaderRva() && rvaToOffset(getIddComHeaderRva()) < dwMinOffset) dwMinOffset = rvaToOffset(getIddComHeaderRva());
+
+ for (word i=0;i<calcNumberOfSections();i++)
+ {
+ if ((getPointerToRawData(i) < dwMinOffset || dwMinOffset == 0xFFFFFFFF) && getSizeOfRawData(i))
+ {
+ if (getPointerToRawData(i)) dwMinOffset = getPointerToRawData(i);
+ }
+ }
+ return dwMinOffset;
+ }
+
+ /**
+ * Calculates the file offset for a new section. The file offset will already be aligned to the file's FileAlignment.
+ * @return Aligned file offset.
+ * \todo uiSize isn't used yet. Will be used later on to search for caves.
+ **/
+ template<int x>
+ unsigned int PeHeaderT<x>::calcOffset(/*unsigned int uiSize*/) const
+ {
+ unsigned int maxoffset = size();
+
+ for (word i=0;i<calcNumberOfSections();i++)
+ {
+ if (getPointerToRawData(i) + getSizeOfRawData(i) > maxoffset) maxoffset = getPointerToRawData(i) + getSizeOfRawData(i);
+ }
+
+ return alignOffset(maxoffset, getFileAlignment());
+ }
+
+ /**
+ * Calculates the Rva for a new section. The Rva will already be aligned to the file's SectionAlignment.
+ * \todo uiSize isn't used yet. Will be used later on to search for caves.
+ * @return Aligned Rva.
+ **/
+ template<int x>
+ unsigned int PeHeaderT<x>::calcRva(/*unsigned int uiSize*/) const
+ {
+ // Major note here: It's possible for sections to exist with a Virtual Size of 0.
+ // That's why it's necessary to use std::max(Vsize, RawSize) here.
+ // An example for such a file is dbeng6.exe (made by Sybase).
+ // In this file each and every section has a VSize of 0 but it still runs.
+
+ unsigned int maxoffset = size();
+ for (word i=0;i<calcNumberOfSections();i++)
+ {
+ if (getVirtualAddress(i) + std::max(getVirtualSize(i), getSizeOfRawData(i)) > maxoffset) maxoffset = getVirtualAddress(i) + std::max(getVirtualSize(i), getSizeOfRawData(i));
+ }
+
+ return alignOffset(maxoffset, getSectionAlignment());
+ }
+
+ /**
+ * Returns the number of currently defined sections. Note that this value can be different from the number
+ * of sections according to the header (see #PeLib::PeHeaderT<x>::getNumberOfSections).
+ * @return Number of currently defined sections.
+ **/
+ template<int x>
+ word PeHeaderT<x>::calcNumberOfSections() const
+ {
+ return static_cast<PeLib::word>(m_vIsh.size());
+ }
+
+ /**
+ * Enlarges the physically last section in the file.
+ * @param uiSize Additional size that's added to the section's size.
+ **/
+ template<int x>
+ void PeHeaderT<x>::enlargeLastSection(unsigned int uiSize)
+ {
+ std::vector<PELIB_IMAGE_SECTION_HEADER>::iterator ishLastSection = std::max_element(m_vIsh.begin(), m_vIsh.end(), std::mem_fun_ref(&PELIB_IMAGE_SECTION_HEADER::biggerFileOffset));
+ unsigned int uiRawDataSize = alignOffset(ishLastSection->SizeOfRawData + uiSize, getFileAlignment());
+
+ ishLastSection->SizeOfRawData = uiRawDataSize;
+ ishLastSection->VirtualSize = ishLastSection->SizeOfRawData;
+
+ setSizeOfImage(calcSizeOfImage());
+ }
+
+ /**
+ * Determines the section which contains the file offset.
+ * @param dwOffset File offset.
+ * @return Section Id of the section which contains the offset.
+ **/
+ template<int x>
+ word PeHeaderT<x>::getSectionWithOffset(VAR4_8 dwOffset) const
+ {
+ // Offset = 0 must be handled explicitly as there are files
+ // with sections that begin at offset 0, that means the section
+ // only exists in memory.
+
+ if (!dwOffset) return std::numeric_limits<word>::max();
+
+ for (word i=0;i<calcNumberOfSections();i++)
+ {
+ // Explicity exclude sections with raw pointer = 0.
+ dword rawptr = getPointerToRawData(i);
+ if (rawptr && rawptr <= dwOffset && rawptr + getSizeOfRawData(i) > dwOffset) return i;
+ }
+
+ return std::numeric_limits<word>::max();
+ }
+
+ /**
+ * Determines the section which contains the Rva.
+ * @param dwRva A relative virtual address.
+ * @return Section Id of the section which contains the Rva.
+ **/
+ template<int x>
+ word PeHeaderT<x>::getSectionWithRva(VAR4_8 dwRva) const
+ {
+ // Major note here: It's possible for sections to exist with a Virtual Size of 0.
+ // That's why it's necessary to use std::max(Vsize, RawSize) here.
+ // An example for such a file is dbeng6.exe (made by Sybase).
+ // In this file each and every section has a VSize of 0 but it still runs.
+
+ for (word i=0;i<calcNumberOfSections();i++)
+ {
+ // Weird VC++7 error doesn't allow me to use std::max here.
+ dword max = getVirtualSize(i) >= getSizeOfRawData(i) ? getVirtualSize(i) : getSizeOfRawData(i);
+ if (getVirtualAddress(i) <= dwRva && getVirtualAddress(i) + max > dwRva) return i;
+ }
+
+ return -1;
+ }
+
+ /**
+ * Corrects all faulty values of the current PE header. The following values will be corrected: NtSignature,
+ * NumberOfSections, SizeOfOptionalHeader, FileAlignment (will be aligned to n*0x200),
+ * SectionAlignment (will be aligned to n*0x1000), NumberOfRvaAndSizes, SizeOfHeaders, SizeOfImage,
+ * Magic, Characteristics.
+ * @param dwOffset Beginning of PeHeader (see #PeLib::MzHeader::getAddressOfPeHeader).
+ * \todo 32bit and 64bit versions.
+ **/
+ template<int x>
+ void PeHeaderT<x>::makeValid(dword dwOffset)
+ {
+ setNtSignature(PELIB_IMAGE_NT_SIGNATURE); // 'PE'
+ setMachine(PELIB_IMAGE_FILE_MACHINE_I386);
+ setNumberOfSections(calcNumberOfSections());
+
+ // Check if 64 bits.
+ setSizeOfOptionalHeader(PELIB_IMAGE_OPTIONAL_HEADER<x>::size() + calcNumberOfRvaAndSizes() * 8);
+
+ // Check if 64 bits.
+ dword dwCharacteristics = PELIB_IMAGE_FILE_EXECUTABLE_IMAGE | PELIB_IMAGE_FILE_32BIT_MACHINE;
+ setCharacteristics(dwCharacteristics);
+
+ // Check if 64 bits.
+ setMagic(PELIB_IMAGE_NT_OPTIONAL_HDR32_MAGIC);
+
+ // setImageBase(0x01000000);
+
+ // Align file and section alignment values
+ unsigned int dwAlignedOffset = alignOffset(getSectionAlignment(), 0x1000);
+ setSectionAlignment(dwAlignedOffset ? dwAlignedOffset : 0x1000);
+
+ dwAlignedOffset = alignOffset(getFileAlignment(), 0x200);
+ setFileAlignment(dwAlignedOffset ? dwAlignedOffset : 0x200);
+
+// setMajorSubsystemVersion(4);
+// setSubsystem(IMAGE_SUBSYSTEM_WINDOWS_GUI);
+ setNumberOfRvaAndSizes(calcNumberOfRvaAndSizes());
+
+ // Code below depends on code above. Don't change the order.
+ dword dwSizeOfHeaders = alignOffset(dwOffset + size(), getFileAlignment());
+ setSizeOfHeaders(dwSizeOfHeaders);
+
+ dword dwSizeOfImage = alignOffset(dwSizeOfHeaders, getSectionAlignment());
+
+ for (int i=0;i<calcNumberOfSections();i++)
+ {
+ dwSizeOfImage += alignOffset(getVirtualSize(i), getSectionAlignment());
+ }
+
+ dwSizeOfImage = alignOffset(dwSizeOfImage, getSectionAlignment());
+ setSizeOfImage(dwSizeOfImage);
+ }
+
+ template<int x>
+ unsigned int PeHeaderT<x>::offsetToRva(VAR4_8 dwOffset) const
+ {
+ if (dwOffset < calcStartOfCode()) return dwOffset;
+
+ PeLib::word uiSecnr = getSectionWithOffset(dwOffset);
+
+ if (uiSecnr == 0xFFFF) return -1;
+
+ return getVirtualAddress(uiSecnr) + dwOffset - getPointerToRawData(uiSecnr);
+ }
+
+ /**
+ * Converts a file offset to a virtual address.
+ * @param dwOffset File offset.
+ * @return Virtual Address.
+ **/
+ template<int x>
+ unsigned int PeHeaderT<x>::offsetToVa(VAR4_8 dwOffset) const
+ {
+ if (dwOffset < calcStartOfCode()) return getImageBase() + dwOffset;
+
+ PeLib::word uiSecnr = getSectionWithOffset(dwOffset);
+
+ if (uiSecnr == 0xFFFF) return -1;
+
+ return getImageBase() + getVirtualAddress(uiSecnr) + dwOffset - getPointerToRawData(uiSecnr);
+ }
+
+ template<int x>
+ void PeHeaderT<x>::readHeader(InputBuffer& ibBuffer, PELIB_IMAGE_NT_HEADERS<x>& header) const
+ {
+ ibBuffer >> header.Signature;
+
+ ibBuffer >> header.FileHeader.Machine;
+ ibBuffer >> header.FileHeader.NumberOfSections;
+ ibBuffer >> header.FileHeader.TimeDateStamp;
+ ibBuffer >> header.FileHeader.PointerToSymbolTable;
+ ibBuffer >> header.FileHeader.NumberOfSymbols;
+ ibBuffer >> header.FileHeader.SizeOfOptionalHeader;
+ ibBuffer >> header.FileHeader.Characteristics;
+ ibBuffer >> header.OptionalHeader.Magic;
+
+ ibBuffer >> header.OptionalHeader.MajorLinkerVersion;
+ ibBuffer >> header.OptionalHeader.MinorLinkerVersion;
+ ibBuffer >> header.OptionalHeader.SizeOfCode;
+ ibBuffer >> header.OptionalHeader.SizeOfInitializedData;
+ ibBuffer >> header.OptionalHeader.SizeOfUninitializedData;
+ ibBuffer >> header.OptionalHeader.AddressOfEntryPoint;
+ ibBuffer >> header.OptionalHeader.BaseOfCode;
+ readBaseOfData(ibBuffer, header);
+ ibBuffer >> header.OptionalHeader.ImageBase;
+ ibBuffer >> header.OptionalHeader.SectionAlignment;
+ ibBuffer >> header.OptionalHeader.FileAlignment;
+ ibBuffer >> header.OptionalHeader.MajorOperatingSystemVersion;
+ ibBuffer >> header.OptionalHeader.MinorOperatingSystemVersion;
+ ibBuffer >> header.OptionalHeader.MajorImageVersion;
+ ibBuffer >> header.OptionalHeader.MinorImageVersion;
+ ibBuffer >> header.OptionalHeader.MajorSubsystemVersion;
+ ibBuffer >> header.OptionalHeader.MinorSubsystemVersion;
+ ibBuffer >> header.OptionalHeader.Win32VersionValue;
+ ibBuffer >> header.OptionalHeader.SizeOfImage;
+ ibBuffer >> header.OptionalHeader.SizeOfHeaders;
+ ibBuffer >> header.OptionalHeader.CheckSum;
+ ibBuffer >> header.OptionalHeader.Subsystem;
+ ibBuffer >> header.OptionalHeader.DllCharacteristics;
+ ibBuffer >> header.OptionalHeader.SizeOfStackReserve;
+ ibBuffer >> header.OptionalHeader.SizeOfStackCommit;
+ ibBuffer >> header.OptionalHeader.SizeOfHeapReserve;
+ ibBuffer >> header.OptionalHeader.SizeOfHeapCommit;
+ ibBuffer >> header.OptionalHeader.LoaderFlags;
+ ibBuffer >> header.OptionalHeader.NumberOfRvaAndSizes;
+ }
+
+ template<int x>
+ void PeHeaderT<x>::readDataDirectories(InputBuffer& ibBuffer, PELIB_IMAGE_NT_HEADERS<x>& header) const
+ {
+ PELIB_IMAGE_DATA_DIRECTORY idd;
+
+ for (unsigned int i=0;i<header.OptionalHeader.NumberOfRvaAndSizes;i++)
+ {
+ ibBuffer >> idd.VirtualAddress;
+ ibBuffer >> idd.Size;
+ header.dataDirectories.push_back(idd);
+ }
+ }
+
+ template<int x>
+ std::vector<PELIB_IMAGE_SECTION_HEADER> PeHeaderT<x>::readSections(InputBuffer& ibBuffer, PELIB_IMAGE_NT_HEADERS<x>& header) const
+ {
+ const unsigned int nrSections = header.FileHeader.NumberOfSections;
+ PELIB_IMAGE_SECTION_HEADER ishCurr;
+
+ std::vector<PELIB_IMAGE_SECTION_HEADER> vIshdCurr;
+
+ for (unsigned int i=0;i<nrSections;i++)
+ {
+ ibBuffer.read(reinterpret_cast<char*>(ishCurr.Name), 8);
+ ibBuffer >> ishCurr.VirtualSize;
+ ibBuffer >> ishCurr.VirtualAddress;
+ ibBuffer >> ishCurr.SizeOfRawData;
+ ibBuffer >> ishCurr.PointerToRawData;
+ ibBuffer >> ishCurr.PointerToRelocations;
+ ibBuffer >> ishCurr.PointerToLinenumbers;
+ ibBuffer >> ishCurr.NumberOfRelocations;
+ ibBuffer >> ishCurr.NumberOfLinenumbers;
+ ibBuffer >> ishCurr.Characteristics;
+ vIshdCurr.push_back(ishCurr);
+ }
+
+ return vIshdCurr;
+ }
+
+ template<int x>
+ int PeHeaderT<x>::read(const unsigned char* pcBuffer, unsigned int uiSize, unsigned int uiOffset)
+ {
+ if (uiSize < m_inthHeader.size())
+ {
+ return ERROR_INVALID_FILE;
+ }
+
+ std::vector<unsigned char> vBuffer(pcBuffer, pcBuffer + m_inthHeader.size());
+
+ InputBuffer ibBuffer(vBuffer);
+ PELIB_IMAGE_NT_HEADERS<x> header;
+
+ readHeader(ibBuffer, header);
+
+ if (uiSize < m_inthHeader.size() + header.OptionalHeader.NumberOfRvaAndSizes * 8 + header.FileHeader.NumberOfSections * 0x28)
+ {
+ return ERROR_INVALID_FILE;
+ }
+
+ vBuffer.resize(header.OptionalHeader.NumberOfRvaAndSizes * 8 + header.FileHeader.NumberOfSections * 0x28);
+ vBuffer.assign(pcBuffer + m_inthHeader.size(), pcBuffer + m_inthHeader.size() + header.OptionalHeader.NumberOfRvaAndSizes * 8 + header.FileHeader.NumberOfSections * 0x28);
+
+ ibBuffer.setBuffer(vBuffer);
+
+ readDataDirectories(ibBuffer, header);
+
+ m_vIsh = readSections(ibBuffer, header);
+
+ std::swap(m_inthHeader, header);
+
+ m_uiOffset = uiOffset;
+
+ return NO_ERROR;
+ }
+
+ /**
+ * Reads the PE header from a file Note that this function does not verify if a file is actually a MZ file.
+ * For this purpose see #PeLib::PeHeaderT<x>::isValid. The only check this function makes is a check to see if
+ * the file is large enough to be a PE header. If the data is valid doesn't matter.
+ * @param strFilename Name of the file which will be read.
+ * @param uiOffset File offset of PE header (see #PeLib::MzHeader::getAddressOfPeHeader).
+ **/
+ template<int x>
+ int PeHeaderT<x>::read(std::string strFilename, unsigned int uiOffset)
+ {
+ std::ifstream ifFile(strFilename.c_str(), std::ios::binary);
+
+ if (!ifFile)
+ {
+ return ERROR_OPENING_FILE;
+ }
+
+ // File too small
+ if (fileSize(ifFile) < uiOffset + m_inthHeader.size())
+ {
+ return ERROR_INVALID_FILE;
+ }
+
+ std::vector<unsigned char> vBuffer(m_inthHeader.size());
+
+ ifFile.seekg(uiOffset, std::ios::beg);
+ ifFile.read(reinterpret_cast<char*>(&vBuffer[0]), static_cast<std::streamsize>(vBuffer.size()));
+
+ InputBuffer ibBuffer(vBuffer);
+ PELIB_IMAGE_NT_HEADERS<x> header;
+
+ readHeader(ibBuffer, header);
+
+ vBuffer.resize(header.OptionalHeader.NumberOfRvaAndSizes * 8 + header.FileHeader.NumberOfSections * 0x28);
+
+ ifFile.read(reinterpret_cast<char*>(&vBuffer[0]), static_cast<std::streamsize>(vBuffer.size()));
+ if (!ifFile)
+ {
+ return ERROR_INVALID_FILE;
+ }
+
+ ibBuffer.setBuffer(vBuffer);
+
+ readDataDirectories(ibBuffer, header);
+
+ // Sections
+// const unsigned int nrSections = header.FileHeader.NumberOfSections;
+// if (fileSize(ifFile) < uiOffset + m_inthHeader.size() + nrSections * PELIB_IMAGE_SECTION_HEADER::size())
+// {
+// return ERROR_INVALID_FILE;
+// }
+
+ m_vIsh = readSections(ibBuffer, header);
+
+ std::swap(m_inthHeader, header);
+
+ m_uiOffset = uiOffset;
+
+ ifFile.close();
+
+ return NO_ERROR;
+ }
+
+ /**
+ * Rebuilds the PE header so that it can be written to a file. It's not guaranteed that the
+ * header will be valid. If you want to make sure that the header will be valid you
+ * must call #PeLib::PeHeaderT<x>::makeValid first.
+ * @param vBuffer Buffer where the rebuilt header will be stored.
+ **/
+ template<int x>
+ void PeHeaderT<x>::rebuild(std::vector<byte>& vBuffer) const
+ {
+ OutputBuffer obBuffer(vBuffer);
+
+ obBuffer << m_inthHeader.Signature;
+
+ obBuffer << m_inthHeader.FileHeader.Machine;
+ obBuffer << m_inthHeader.FileHeader.NumberOfSections;
+ obBuffer << m_inthHeader.FileHeader.TimeDateStamp;
+ obBuffer << m_inthHeader.FileHeader.PointerToSymbolTable;
+ obBuffer << m_inthHeader.FileHeader.NumberOfSymbols;
+ obBuffer << m_inthHeader.FileHeader.SizeOfOptionalHeader;
+ obBuffer << m_inthHeader.FileHeader.Characteristics;
+ obBuffer << m_inthHeader.OptionalHeader.Magic;
+ obBuffer << m_inthHeader.OptionalHeader.MajorLinkerVersion;
+ obBuffer << m_inthHeader.OptionalHeader.MinorLinkerVersion;
+ obBuffer << m_inthHeader.OptionalHeader.SizeOfCode;
+ obBuffer << m_inthHeader.OptionalHeader.SizeOfInitializedData;
+ obBuffer << m_inthHeader.OptionalHeader.SizeOfUninitializedData;
+ obBuffer << m_inthHeader.OptionalHeader.AddressOfEntryPoint;
+ obBuffer << m_inthHeader.OptionalHeader.BaseOfCode;
+ rebuildBaseOfData(obBuffer);
+// obBuffer << m_inthHeader.OptionalHeader.BaseOfData;
+ obBuffer << m_inthHeader.OptionalHeader.ImageBase;
+ obBuffer << m_inthHeader.OptionalHeader.SectionAlignment;
+ obBuffer << m_inthHeader.OptionalHeader.FileAlignment;
+ obBuffer << m_inthHeader.OptionalHeader.MajorOperatingSystemVersion;
+ obBuffer << m_inthHeader.OptionalHeader.MinorOperatingSystemVersion;
+ obBuffer << m_inthHeader.OptionalHeader.MajorImageVersion;
+ obBuffer << m_inthHeader.OptionalHeader.MinorImageVersion;
+ obBuffer << m_inthHeader.OptionalHeader.MajorSubsystemVersion;
+ obBuffer << m_inthHeader.OptionalHeader.MinorSubsystemVersion;
+ obBuffer << m_inthHeader.OptionalHeader.Win32VersionValue;
+ obBuffer << m_inthHeader.OptionalHeader.SizeOfImage;
+ obBuffer << m_inthHeader.OptionalHeader.SizeOfHeaders;
+ obBuffer << m_inthHeader.OptionalHeader.CheckSum;
+ obBuffer << m_inthHeader.OptionalHeader.Subsystem;
+ obBuffer << m_inthHeader.OptionalHeader.DllCharacteristics;
+ obBuffer << m_inthHeader.OptionalHeader.SizeOfStackReserve;
+ obBuffer << m_inthHeader.OptionalHeader.SizeOfStackCommit;
+ obBuffer << m_inthHeader.OptionalHeader.SizeOfHeapReserve;
+ obBuffer << m_inthHeader.OptionalHeader.SizeOfHeapCommit;
+ obBuffer << m_inthHeader.OptionalHeader.LoaderFlags;
+ obBuffer << m_inthHeader.OptionalHeader.NumberOfRvaAndSizes;
+
+ // The 0x10 data directories
+ for (unsigned int i=0;i<calcNumberOfRvaAndSizes();i++)
+ {
+ obBuffer << m_inthHeader.dataDirectories[i].VirtualAddress;
+ obBuffer << m_inthHeader.dataDirectories[i].Size;
+ }
+
+ // The section definitions
+ const unsigned int nrSections = calcNumberOfSections();
+ for (unsigned int i=0;i<nrSections;i++)
+ {
+ char temp[9] = {0};
+ strcpy(temp, getSectionName(i).c_str());
+ obBuffer.add(temp, 8);
+ obBuffer << m_vIsh[i].VirtualSize;
+ obBuffer << m_vIsh[i].VirtualAddress;
+ obBuffer << m_vIsh[i].SizeOfRawData;
+ obBuffer << m_vIsh[i].PointerToRawData;
+ obBuffer << m_vIsh[i].PointerToRelocations;
+ obBuffer << m_vIsh[i].PointerToLinenumbers;
+ obBuffer << m_vIsh[i].NumberOfRelocations;
+ obBuffer << m_vIsh[i].NumberOfLinenumbers;
+ obBuffer << m_vIsh[i].Characteristics;
+ }
+ }
+
+ /**
+ * Converts a relative virtual offset to a file offset.
+ * @param dwRva A relative virtual offset.
+ * @return A file offset.
+ * \todo It's not always 0x1000.
+ **/
+ template<int x>
+ typename FieldSizes<x>::VAR4_8 PeHeaderT<x>::rvaToOffset(VAR4_8 dwRva) const
+ {
+ // XXX: Not correct
+ if (dwRva < 0x1000) return dwRva;
+
+ PeLib::word uiSecnr = getSectionWithRva(dwRva);
+ if (uiSecnr == 0xFFFF || dwRva > getVirtualAddress(uiSecnr) + getSizeOfRawData(uiSecnr))
+ {
+ return std::numeric_limits<VAR4_8>::max();
+ }
+
+ return getPointerToRawData(uiSecnr) + dwRva - getVirtualAddress(uiSecnr);
+ }
+
+ /**
+ * Converts a relative virtual offset to a virtual offset.
+ * @param dwRva A relative virtual offset.
+ * @return A virtual offset.
+ **/
+ template<int x>
+ typename FieldSizes<x>::VAR4_8 PeHeaderT<x>::rvaToVa(VAR4_8 dwRva) const
+ {
+ return getImageBase() + dwRva;
+ }
+
+ /**
+ * Calculates the size of the current PE header. This includes the actual header and the section definitions.
+ * @return Size of the current PE header.
+ * \todo Better handling of files with less than 0x10 directories.
+ **/
+ template<int x>
+ unsigned int PeHeaderT<x>::size() const
+ {
+ return m_inthHeader.size() + getNumberOfSections() * PELIB_IMAGE_SECTION_HEADER::size();
+ }
+
+ // \todo Not sure if this works.
+ template<int x>
+ typename FieldSizes<x>::VAR4_8 PeHeaderT<x>::vaToRva(VAR4_8 dwRva) const
+ {
+ if (dwRva - getImageBase() < calcStartOfCode()) return dwRva - getImageBase();
+
+ if (getSectionWithRva(dwRva - getImageBase()) == 0xFFFF) return -1;
+
+ return dwRva - getImageBase();
+ }
+
+ template<int x>
+ typename FieldSizes<x>::VAR4_8 PeHeaderT<x>::vaToOffset(VAR4_8 dwRva) const
+ {
+ return rvaToOffset(dwRva - getImageBase());
+ }
+
+ /**
+ * Saves the PE header to a file. Note that this only saves the header information, if you have added sections
+ * and want to save these to the file you have to call #PeLib::PeHeaderT<x>::saveSections too. This function also
+ * does not verify if the PE header is correct. If you want to make sure that the current PE header is valid,
+ * call #PeLib::PeHeaderT<x>::isValid and #PeLib::PeHeaderT<x>::makeValid first.
+ * @param strFilename Filename of the file the header will be written to.
+ * @param uiOffset File offset the header will be written to.
+ **/
+ template<int x>
+ int PeHeaderT<x>::write(std::string strFilename, unsigned int uiOffset) const
+ {
+ std::fstream ofFile(strFilename.c_str(), std::ios_base::in);
+
+ if (!ofFile)
+ {
+ ofFile.clear();
+ ofFile.open(strFilename.c_str(), std::ios_base::out | std::ios_base::binary);
+ }
+ else
+ {
+ ofFile.close();
+ ofFile.open(strFilename.c_str(), std::ios_base::in | std::ios_base::out | std::ios_base::binary);
+ }
+
+ if (!ofFile)
+ {
+ return ERROR_OPENING_FILE;
+ }
+
+ ofFile.seekp(uiOffset, std::ios::beg);
+
+ std::vector<unsigned char> vBuffer;
+
+ rebuild(vBuffer);
+
+ ofFile.write(reinterpret_cast<const char*>(&vBuffer[0]), static_cast<unsigned int>(vBuffer.size()));
+
+ ofFile.close();
+
+ return NO_ERROR;
+ }
+
+
+ /**
+ * Overwrites a section's data.
+ * @param wSecnr Number of the section which will be overwritten.
+ * @param strFilename Name of the file where the section will be written to.
+ * @param wSecnr Number of the section that will be written.
+ * @param vBuffer New data of the section.
+ **/
+ template<int x>
+ int PeHeaderT<x>::writeSectionData(const std::string& strFilename, word wSecnr, const std::vector<byte>& vBuffer) const
+ {
+ std::fstream ofFile(strFilename.c_str(), std::ios_base::in);
+
+ if (!ofFile)
+ {
+ ofFile.clear();
+ ofFile.open(strFilename.c_str(), std::ios_base::out | std::ios_base::binary);
+ }
+ else
+ {
+ ofFile.close();
+ ofFile.open(strFilename.c_str(), std::ios_base::in | std::ios_base::out | std::ios_base::binary);
+ }
+
+ if (!ofFile)
+ {
+ ofFile.clear();
+
+ return ERROR_OPENING_FILE;
+ }
+
+ ofFile.seekp(getPointerToRawData(wSecnr), std::ios::beg);
+
+ ofFile.write(reinterpret_cast<const char*>(&vBuffer[0]), std::min(static_cast<unsigned int>(vBuffer.size()), getSizeOfRawData(wSecnr)));
+
+ ofFile.close();
+
+ return NO_ERROR;
+ }
+
+ template<int x>
+ int PeHeaderT<x>::writeSections(const std::string& strFilename) const
+ {
+ std::fstream ofFile(strFilename.c_str(), std::ios_base::in);
+
+ if (!ofFile)
+ {
+ ofFile.clear();
+ ofFile.open(strFilename.c_str(), std::ios_base::out | std::ios_base::binary);
+ }
+ else
+ {
+ ofFile.close();
+ ofFile.open(strFilename.c_str(), std::ios_base::in | std::ios_base::out | std::ios_base::binary);
+ }
+
+ if (!ofFile)
+ {
+ return ERROR_OPENING_FILE;
+ }
+
+ unsigned int uiFilesize = fileSize(ofFile);
+
+ for (int i=0;i<calcNumberOfSections();i++)
+ {
+ if (uiFilesize < getPointerToRawData(i) + getSizeOfRawData(i))
+ {
+ unsigned int uiToWrite = getPointerToRawData(i) + getSizeOfRawData(i) - uiFilesize;
+ std::vector<char> vBuffer(uiToWrite);
+ ofFile.seekp(0, std::ios::end);
+ ofFile.write(&vBuffer[0], static_cast<unsigned int>(vBuffer.size()));
+ uiFilesize = getPointerToRawData(i) + getSizeOfRawData(i);
+ }
+ }
+
+ ofFile.close();
+
+ return NO_ERROR;
+ }
+
+ /**
+ * Returns the file's Nt signature.
+ * @return The Nt signature value from the PE header.
+ **/
+ template<int x>
+ dword PeHeaderT<x>::getNtSignature() const
+ {
+ return m_inthHeader.Signature;
+ }
+
+ /**
+ * Returns the file's machine.
+ * @return The Machine value from the PE header.
+ **/
+ template<int x>
+ word PeHeaderT<x>::getMachine() const
+ {
+ return m_inthHeader.FileHeader.Machine;
+ }
+
+
+ /**
+ * Returns the file's number of sections as defined in the header. Note that this value can be different
+ * from the number of defined sections (#see PeLib::PeHeaderT<x>::getNumberOfSections).
+ * @return The NumberOfSections value from the PE header.
+ **/
+ template<int x>
+ word PeHeaderT<x>::getNumberOfSections() const
+ {
+ return m_inthHeader.FileHeader.NumberOfSections;
+ }
+
+ /**
+ * Returns the file's TimeDateStamp.
+ * @return The TimeDateStamp value from the PE header.
+ **/
+ template<int x>
+ dword PeHeaderT<x>::getTimeDateStamp() const
+ {
+ return m_inthHeader.FileHeader.TimeDateStamp;
+ }
+
+ /**
+ * Returns the relative virtual address of the file's symbol table.
+ * @return The PointerToSymbolTable value from the PE header.
+ **/
+ template<int x>
+ dword PeHeaderT<x>::getPointerToSymbolTable() const
+ {
+ return m_inthHeader.FileHeader.PointerToSymbolTable;
+ }
+
+ /**
+ * Returns the number of symbols of the file's symbol table.
+ * @return The NumberOfSymbols value from the PE header.
+ **/
+ template<int x>
+ dword PeHeaderT<x>::getNumberOfSymbols() const
+ {
+ return m_inthHeader.FileHeader.NumberOfSymbols;
+ }
+
+ /**
+ * Returns the size of optional header of the file.
+ * @return The SizeOfOptionalHeader value from the PE header.
+ **/
+ template<int x>
+ word PeHeaderT<x>::getSizeOfOptionalHeader() const
+ {
+ return m_inthHeader.FileHeader.SizeOfOptionalHeader;
+ }
+
+ /**
+ * @return The Characteristics value from the PE header.
+ **/
+ template<int x>
+ word PeHeaderT<x>::getCharacteristics() const
+ {
+ return m_inthHeader.FileHeader.Characteristics;
+ }
+
+ /**
+ * @return The Magic value from the PE header.
+ **/
+ template<int x>
+ word PeHeaderT<x>::getMagic() const
+ {
+ return m_inthHeader.OptionalHeader.Magic;
+ }
+
+ /**
+ * @return The MajorLinkerVersion value from the PE header.
+ **/
+ template<int x>
+ byte PeHeaderT<x>::getMajorLinkerVersion() const
+ {
+ return m_inthHeader.OptionalHeader.MajorLinkerVersion;
+ }
+
+ /**
+ * @return The MinorLinkerVersion value from the PE header.
+ **/
+ template<int x>
+ byte PeHeaderT<x>::getMinorLinkerVersion() const
+ {
+ return m_inthHeader.OptionalHeader.MinorLinkerVersion;
+ }
+
+ /**
+ * @return The SizeOfCode value from the PE header.
+ **/
+ template<int x>
+ dword PeHeaderT<x>::getSizeOfCode() const
+ {
+ return m_inthHeader.OptionalHeader.SizeOfCode;
+ }
+
+ /**
+ * @return The SizeOfInitializedData value from the PE header.
+ **/
+ template<int x>
+ dword PeHeaderT<x>::getSizeOfInitializedData() const
+ {
+ return m_inthHeader.OptionalHeader.SizeOfInitializedData;
+ }
+
+ /**
+ * @return The SizeOfUninitializedData value from the PE header.
+ **/
+ template<int x>
+ dword PeHeaderT<x>::getSizeOfUninitializedData() const
+ {
+ return m_inthHeader.OptionalHeader.SizeOfUninitializedData;
+ }
+
+ /**
+ * @return The AddressOfEntryPoint value from the PE header.
+ **/
+ template<int x>
+ dword PeHeaderT<x>::getAddressOfEntryPoint() const
+ {
+ return m_inthHeader.OptionalHeader.AddressOfEntryPoint;
+ }
+
+ /**
+ * @return The BaseOfCode value from the PE header.
+ **/
+ template<int x>
+ dword PeHeaderT<x>::getBaseOfCode() const
+ {
+ return m_inthHeader.OptionalHeader.BaseOfCode;
+ }
+
+ /**
+ * @return The ImageBase value from the PE header.
+ **/
+ template<int x>
+ typename FieldSizes<x>::VAR4_8 PeHeaderT<x>::getImageBase() const
+ {
+ return m_inthHeader.OptionalHeader.ImageBase;
+ }
+
+ /**
+ * @return The SectionAlignment value from the PE header.
+ **/
+ template<int x>
+ dword PeHeaderT<x>::getSectionAlignment() const
+ {
+ return m_inthHeader.OptionalHeader.SectionAlignment;
+ }
+
+ /**
+ * @return The FileAlignment value from the PE header.
+ **/
+ template<int x>
+ dword PeHeaderT<x>::getFileAlignment() const
+ {
+ return m_inthHeader.OptionalHeader.FileAlignment;
+ }
+
+ /**
+ * @return The MajorOperatingSystemVersion value from the PE header.
+ **/
+ template<int x>
+ word PeHeaderT<x>::getMajorOperatingSystemVersion() const
+ {
+ return m_inthHeader.OptionalHeader.MajorOperatingSystemVersion;
+ }
+
+ /**
+ * @return The MinorOperatingSystemVersion value from the PE header.
+ **/
+ template<int x>
+ word PeHeaderT<x>::getMinorOperatingSystemVersion() const
+ {
+ return m_inthHeader.OptionalHeader.MinorOperatingSystemVersion;
+ }
+
+ /**
+ * @return The MajorImageVersion value from the PE header.
+ **/
+ template<int x>
+ word PeHeaderT<x>::getMajorImageVersion() const
+ {
+ return m_inthHeader.OptionalHeader.MajorImageVersion;
+ }
+
+ /**
+ * @return The MinorImageVersion value from the PE header.
+ **/
+ template<int x>
+ word PeHeaderT<x>::getMinorImageVersion() const
+ {
+ return m_inthHeader.OptionalHeader.MinorImageVersion;
+ }
+
+ /**
+ * @return The MajorSubsystemVersion value from the PE header.
+ **/
+ template<int x>
+ word PeHeaderT<x>::getMajorSubsystemVersion() const
+ {
+ return m_inthHeader.OptionalHeader.MajorSubsystemVersion;
+ }
+
+ /**
+ * @return The MinorSubsystemVersion value from the PE header.
+ **/
+ template<int x>
+ word PeHeaderT<x>::getMinorSubsystemVersion() const
+ {
+ return m_inthHeader.OptionalHeader.MinorSubsystemVersion;
+ }
+
+ /**
+ * @return The WinVersionValue value from the PE header.
+ **/
+ template<int x>
+ dword PeHeaderT<x>::getWin32VersionValue() const
+ {
+ return m_inthHeader.OptionalHeader.Win32VersionValue;
+ }
+
+ /**
+ * @return The SizeOfImage value from the PE header.
+ **/
+ template<int x>
+ dword PeHeaderT<x>::getSizeOfImage() const
+ {
+ return m_inthHeader.OptionalHeader.SizeOfImage;
+ }
+
+ /**
+ * @return The SizeOfHeaders value from the PE header.
+ **/
+ template<int x>
+ dword PeHeaderT<x>::getSizeOfHeaders() const
+ {
+ return m_inthHeader.OptionalHeader.SizeOfHeaders;
+ }
+
+ /**
+ * @return The CheckSums value from the PE header.
+ **/
+ template<int x>
+ dword PeHeaderT<x>::getCheckSum() const
+ {
+ return m_inthHeader.OptionalHeader.CheckSum;
+ }
+
+ /**
+ * @return The Subsystem value from the PE header.
+ **/
+ template<int x>
+ word PeHeaderT<x>::getSubsystem() const
+ {
+ return m_inthHeader.OptionalHeader.Subsystem;
+ }
+
+ /**
+ * @return The DllCharacteristics value from the PE header.
+ **/
+ template<int x>
+ word PeHeaderT<x>::getDllCharacteristics() const
+ {
+ return m_inthHeader.OptionalHeader.DllCharacteristics;
+ }
+
+ /**
+ * @return The SizeOfStackReserve value from the PE header.
+ **/
+ template<int x>
+ typename FieldSizes<x>::VAR4_8 PeHeaderT<x>::getSizeOfStackReserve() const
+ {
+ return m_inthHeader.OptionalHeader.SizeOfStackReserve;
+ }
+
+ /**
+ * @return The SizeOfStackCommit value from the PE header.
+ **/
+ template<int x>
+ typename FieldSizes<x>::VAR4_8 PeHeaderT<x>::getSizeOfStackCommit() const
+ {
+ return m_inthHeader.OptionalHeader.SizeOfStackCommit;
+ }
+
+ /**
+ * @return The SizeOfHeapReserve value from the PE header.
+ **/
+ template<int x>
+ typename FieldSizes<x>::VAR4_8 PeHeaderT<x>::getSizeOfHeapReserve() const
+ {
+ return m_inthHeader.OptionalHeader.SizeOfHeapReserve;
+ }
+
+ /**
+ * @return The SizeOfHeapCommit value from the PE header.
+ **/
+ template<int x>
+ typename FieldSizes<x>::VAR4_8 PeHeaderT<x>::getSizeOfHeapCommit() const
+ {
+ return m_inthHeader.OptionalHeader.SizeOfHeapCommit;
+ }
+
+ /**
+ * @return The LoaderFlags value from the PE header.
+ **/
+ template<int x>
+ dword PeHeaderT<x>::getLoaderFlags() const
+ {
+ return m_inthHeader.OptionalHeader.LoaderFlags;
+ }
+
+ /**
+ * @return The NumberOfRvaAndSizes value from the PE header.
+ **/
+ template<int x>
+ dword PeHeaderT<x>::getNumberOfRvaAndSizes() const
+ {
+ return m_inthHeader.OptionalHeader.NumberOfRvaAndSizes;
+ }
+
+ template<int x>
+ dword PeHeaderT<x>::calcNumberOfRvaAndSizes() const
+ {
+ return static_cast<dword>(m_inthHeader.dataDirectories.size());
+ }
+
+ /**
+ * Returns the relative virtual address of the current file's export directory.
+ * @return The Rva of the Export directory.
+ **/
+ template<int x>
+ dword PeHeaderT<x>::getIddExportRva() const
+ {
+ return m_inthHeader.dataDirectories[PELIB_IMAGE_DIRECTORY_ENTRY_EXPORT].VirtualAddress;
+ }
+
+ /**
+ * Returns the size of the current file's export directory.
+ * @return The sizeof the Export directory.
+ **/
+ template<int x>
+ dword PeHeaderT<x>::getIddExportSize() const
+ {
+ return m_inthHeader.dataDirectories[PELIB_IMAGE_DIRECTORY_ENTRY_EXPORT].Size;
+ }
+
+ /**
+ * Returns the relative virtual address of the current file's import directory.
+ * @return The Rva of the Import directory.
+ **/
+ template<int x>
+ dword PeHeaderT<x>::getIddImportRva() const
+ {
+ return m_inthHeader.dataDirectories[PELIB_IMAGE_DIRECTORY_ENTRY_IMPORT].VirtualAddress;
+ }
+
+ /**
+ * Returns the size of the current file's import directory.
+ * @return The size of the Import directory.
+ **/
+ template<int x>
+ dword PeHeaderT<x>::getIddImportSize() const
+ {
+ return m_inthHeader.dataDirectories[PELIB_IMAGE_DIRECTORY_ENTRY_IMPORT].Size;
+ }
+
+ /**
+ * Returns the relative virtual address of the current file's resource directory.
+ * @return The Rva of the Resource directory.
+ **/
+ template<int x>
+ dword PeHeaderT<x>::getIddResourceRva() const
+ {
+ return m_inthHeader.dataDirectories[PELIB_IMAGE_DIRECTORY_ENTRY_RESOURCE].VirtualAddress;
+ }
+
+ /**
+ * Returns the size of the current file'resource resource directory.
+ * @return The size of the Resource directory.
+ **/
+ template<int x>
+ dword PeHeaderT<x>::getIddResourceSize() const
+ {
+ return m_inthHeader.dataDirectories[PELIB_IMAGE_DIRECTORY_ENTRY_RESOURCE].Size;
+ }
+
+ /**
+ * Returns the relative virtual address of the current file's exception directory.
+ * @return The Rva of the Exception directory.
+ **/
+ template<int x>
+ dword PeHeaderT<x>::getIddExceptionRva() const
+ {
+ return m_inthHeader.dataDirectories[PELIB_IMAGE_DIRECTORY_ENTRY_EXCEPTION].VirtualAddress;
+ }
+
+ /**
+ * Returns the size of the current file's exception directory.
+ * @return The size of the Exception directory.
+ **/
+ template<int x>
+ dword PeHeaderT<x>::getIddExceptionSize() const
+ {
+ return m_inthHeader.dataDirectories[PELIB_IMAGE_DIRECTORY_ENTRY_EXCEPTION].Size;
+ }
+
+ /**
+ * Returns the relative virtual address of the current file's security directory.
+ * @return The Rva of the Security directory.
+ **/
+ template<int x>
+ dword PeHeaderT<x>::getIddSecurityRva() const
+ {
+ return m_inthHeader.dataDirectories[PELIB_IMAGE_DIRECTORY_ENTRY_SECURITY].VirtualAddress;
+ }
+
+ /**
+ * Returns the size of the current file's security directory.
+ * @return The size of the Security directory.
+ **/
+ template<int x>
+ dword PeHeaderT<x>::getIddSecuritySize() const
+ {
+ return m_inthHeader.dataDirectories[PELIB_IMAGE_DIRECTORY_ENTRY_SECURITY].Size;
+ }
+
+ /**
+ * Returns the relative virtual address of the current file's base reloc directory.
+ * @return The Rva of the Base Reloc directory.
+ **/
+ template<int x>
+ dword PeHeaderT<x>::getIddBaseRelocRva() const
+ {
+ return m_inthHeader.dataDirectories[PELIB_IMAGE_DIRECTORY_ENTRY_BASERELOC].VirtualAddress;
+ }
+
+ /**
+ * Returns the size of the current file's base reloc directory.
+ * @return The size of the Base Reloc directory.
+ **/
+ template<int x>
+ dword PeHeaderT<x>::getIddBaseRelocSize() const
+ {
+ return m_inthHeader.dataDirectories[PELIB_IMAGE_DIRECTORY_ENTRY_BASERELOC].Size;
+ }
+
+ /**
+ * Returns the relative virtual address of the current file's debug directory.
+ * @return The Rva of the Debug directory.
+ **/
+ template<int x>
+ dword PeHeaderT<x>::getIddDebugRva() const
+ {
+ return m_inthHeader.dataDirectories[PELIB_IMAGE_DIRECTORY_ENTRY_DEBUG].VirtualAddress;
+ }
+
+ /**
+ * Returns the size of the current file's debug directory.
+ * @return The size of the Debug directory.
+ **/
+ template<int x>
+ dword PeHeaderT<x>::getIddDebugSize() const
+ {
+ return m_inthHeader.dataDirectories[PELIB_IMAGE_DIRECTORY_ENTRY_DEBUG].Size;
+ }
+
+ /**
+ * Returns the relative virtual address of the current file's Architecture directory.
+ * @return The Rva of the Architecture directory.
+ **/
+ template<int x>
+ dword PeHeaderT<x>::getIddArchitectureRva() const
+ {
+ return m_inthHeader.dataDirectories[PELIB_IMAGE_DIRECTORY_ENTRY_ARCHITECTURE].VirtualAddress;
+ }
+
+ /**
+ * Returns the size of the current file's Architecture directory.
+ * @return The size of the Architecture directory.
+ **/
+ template<int x>
+ dword PeHeaderT<x>::getIddArchitectureSize() const
+ {
+ return m_inthHeader.dataDirectories[PELIB_IMAGE_DIRECTORY_ENTRY_ARCHITECTURE].Size;
+ }
+
+ /**
+ * Returns the relative virtual address of the current file's global ptr directory.
+ * @return The Rva of the GlobalPtr directory.
+ **/
+ template<int x>
+ dword PeHeaderT<x>::getIddGlobalPtrRva() const
+ {
+ return m_inthHeader.dataDirectories[PELIB_IMAGE_DIRECTORY_ENTRY_GLOBALPTR].VirtualAddress;
+ }
+
+ /**
+ * Returns the size of the current file's global ptr directory.
+ * @return The size of the GlobalPtr directory.
+ **/
+ template<int x>
+ dword PeHeaderT<x>::getIddGlobalPtrSize() const
+ {
+ return m_inthHeader.dataDirectories[PELIB_IMAGE_DIRECTORY_ENTRY_GLOBALPTR].Size;
+ }
+
+ /**
+ * Returns the relative virtual address of the current file's TLS directory.
+ * @return The Rva of the Tls directory.
+ **/
+ template<int x>
+ dword PeHeaderT<x>::getIddTlsRva() const
+ {
+ return m_inthHeader.dataDirectories[PELIB_IMAGE_DIRECTORY_ENTRY_TLS].VirtualAddress;
+ }
+
+ /**
+ * Returns the size of the current file's TLS directory.
+ * @return The size of the Tls directory.
+ **/
+ template<int x>
+ dword PeHeaderT<x>::getIddTlsSize() const
+ {
+ return m_inthHeader.dataDirectories[PELIB_IMAGE_DIRECTORY_ENTRY_TLS].Size;
+ }
+
+ /**
+ * Returns the relative virtual address of the current file's load config directory.
+ * @return The Rva of the LoadConfig directory.
+ **/
+ template<int x>
+ dword PeHeaderT<x>::getIddLoadConfigRva() const
+ {
+ return m_inthHeader.dataDirectories[PELIB_IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG].VirtualAddress;
+ }
+
+ /**
+ * Returns the size of the current file's load config directory.
+ * @return The size of the LoadConfig directory.
+ **/
+ template<int x>
+ dword PeHeaderT<x>::getIddLoadConfigSize() const
+ {
+ return m_inthHeader.dataDirectories[PELIB_IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG].Size;
+ }
+
+ /**
+ * Returns the relative virtual address of the current file's bound import directory.
+ * @return The Rva of the BoundImport directory.
+ **/
+ template<int x>
+ dword PeHeaderT<x>::getIddBoundImportRva() const
+ {
+ return m_inthHeader.dataDirectories[PELIB_IMAGE_DIRECTORY_ENTRY_BOUND_IMPORT].VirtualAddress;
+ }
+
+ /**
+ * Returns the size of the current file's bound import directory.
+ * @return The size of the BoundImport directory.
+ **/
+ template<int x>
+ dword PeHeaderT<x>::getIddBoundImportSize() const
+ {
+ return m_inthHeader.dataDirectories[PELIB_IMAGE_DIRECTORY_ENTRY_BOUND_IMPORT].Size;
+ }
+
+ /**
+ * Returns the relative virtual address of the current file's IAT directory.
+ * @return The Rva of the IAT directory.
+ **/
+ template<int x>
+ dword PeHeaderT<x>::getIddIatRva() const
+ {
+ return m_inthHeader.dataDirectories[PELIB_IMAGE_DIRECTORY_ENTRY_IAT].VirtualAddress;
+ }
+
+ /**
+ * Returns the size of the current file's IAT directory.
+ * @return The size of the IAT directory.
+ **/
+ template<int x>
+ dword PeHeaderT<x>::getIddIatSize() const
+ {
+ return m_inthHeader.dataDirectories[PELIB_IMAGE_DIRECTORY_ENTRY_IAT].Size;
+ }
+
+ /**
+ * Returns the relative virtual address of the current file's Delay Import directory.
+ * @return The Rva of the DelayImport directory.
+ **/
+ template<int x>
+ dword PeHeaderT<x>::getIddDelayImportRva() const
+ {
+ return m_inthHeader.dataDirectories[PELIB_IMAGE_DIRECTORY_ENTRY_DELAY_IMPORT].VirtualAddress;
+ }
+
+ /**
+ * Returns the size of the current file's Delay Import directory.
+ * @return The size of the DelayImport directory.
+ **/
+ template<int x>
+ dword PeHeaderT<x>::getIddDelayImportSize() const
+ {
+ return m_inthHeader.dataDirectories[PELIB_IMAGE_DIRECTORY_ENTRY_DELAY_IMPORT].Size;
+ }
+
+ /**
+ * Returns the relative virtual address of the current file's COM Descriptor directory.
+ * @return The Rva of the COM Descriptor directory.
+ **/
+ template<int x>
+ dword PeHeaderT<x>::getIddComHeaderRva() const
+ {
+ return m_inthHeader.dataDirectories[PELIB_IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR].VirtualAddress;
+ }
+
+ /**
+ * Returns the size of the current file's COM Descriptor directory.
+ * @return The Rva of the COM Descriptor directory.
+ **/
+ template<int x>
+ dword PeHeaderT<x>::getIddComHeaderSize() const
+ {
+ return m_inthHeader.dataDirectories[PELIB_IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR].Size;
+ }
+
+ /**
+ * Returns the relative virtual address of an image directory.
+ * @param dwDirectory The identifier of an image directory.
+ * @return The Rva of the image directory.
+ **/
+ template<int x>
+ dword PeHeaderT<x>::getImageDataDirectoryRva(dword dwDirectory) const
+ {
+ return m_inthHeader.dataDirectories[dwDirectory].VirtualAddress;
+ }
+
+ template<int x>
+ void PeHeaderT<x>::setImageDataDirectoryRva(dword dwDirectory, dword value)
+ {
+ m_inthHeader.dataDirectories[dwDirectory].VirtualAddress = value;
+ }
+
+ /**
+ * Returns the size of an image directory.
+ * @param dwDirectory The identifier of an image directory.
+ * @return The size of the image directory.
+ **/
+ template<int x>
+ dword PeHeaderT<x>::getImageDataDirectorySize(dword dwDirectory) const
+ {
+ return m_inthHeader.dataDirectories[dwDirectory].Size;
+ }
+
+ template<int x>
+ void PeHeaderT<x>::setImageDataDirectorySize(dword dwDirectory, dword value)
+ {
+ m_inthHeader.dataDirectories[dwDirectory].Size = value;
+ }
+
+ /**
+ * Returns the name of the section which is specified by the parameter wSectionnr.
+ * @param wSectionnr Index of the section.
+ * @return The name of the section.
+ **/
+ template<int x>
+ std::string PeHeaderT<x>::getSectionName(word wSectionnr) const
+ {
+ std::string sectionName = "";
+
+ for (unsigned int i=0;i<sizeof(m_vIsh[wSectionnr].Name);i++)
+ {
+ if (m_vIsh[wSectionnr].Name[i]) sectionName += m_vIsh[wSectionnr].Name[i];
+ }
+
+ return sectionName;
+ }
+
+ /**
+ * Returns the virtual size of the section which is specified by the parameter wSectionnr.
+ * @param wSectionnr Index of the section.
+ * @return The virtual size of the section.
+ **/
+ template<int x>
+ dword PeHeaderT<x>::getVirtualSize(word wSectionnr) const
+ {
+ return m_vIsh[wSectionnr].VirtualSize;
+ }
+
+ /**
+ * Returns the relative virtual address of the section which is specified by the parameter wSectionnr.
+ * @param wSectionnr Index of the section.
+ * @return The Rva of the section.
+ **/
+ template<int x>
+ dword PeHeaderT<x>::getVirtualAddress(word wSectionnr) const
+ {
+ return m_vIsh[wSectionnr].VirtualAddress;
+ }
+
+ /**
+ * Returns the size of raw data of the section which is specified by the parameter wSectionnr.
+ * @param wSectionnr Index of the section.
+ * @return The size of raw data of the section.
+ **/
+ template<int x>
+ dword PeHeaderT<x>::getSizeOfRawData(word wSectionnr) const
+ {
+ return m_vIsh[wSectionnr].SizeOfRawData;
+ }
+
+ /**
+ * Returns the file offset of the section which is specified by the parameter wSectionnr.
+ * @param wSectionnr Index of the section.
+ * @return The file offset of the section.
+ **/
+ template<int x>
+ dword PeHeaderT<x>::getPointerToRawData(word wSectionnr) const
+ {
+ return m_vIsh[wSectionnr].PointerToRawData;
+ }
+
+ /**
+ * Returns the pointer to relocations of the section which is specified by the parameter wSectionnr.
+ * @param wSectionnr Index of the section.
+ * @return The pointer to relocations of the section.
+ **/
+ template<int x>
+ dword PeHeaderT<x>::getPointerToRelocations(word wSectionnr) const
+ {
+ return m_vIsh[wSectionnr].PointerToRelocations;
+ }
+
+ /**
+ * Returns the poiner to line numbers of the section which is specified by the parameter wSectionnr.
+ * @param wSectionnr Index of the section.
+ * @return The pointer to line numbers of the section.
+ **/
+ template<int x>
+ dword PeHeaderT<x>::getPointerToLinenumbers(word wSectionnr) const
+ {
+ return m_vIsh[wSectionnr].PointerToLinenumbers;
+ }
+
+ /**
+ * Returns the number of relocations of the section which is specified by the parameter wSectionnr.
+ * @param wSectionnr Index of the section.
+ * @return The number of relocations of the section.
+ **/
+ template<int x>
+ dword PeHeaderT<x>::getNumberOfRelocations(word wSectionnr) const
+ {
+ return m_vIsh[wSectionnr].NumberOfRelocations;
+ }
+
+ /**
+ * Returns the number of line numbers of the section which is specified by the parameter wSectionnr.
+ * @param wSectionnr Index of the section.
+ * @return The number of line numbers of the section.
+ **/
+ template<int x>
+ dword PeHeaderT<x>::getNumberOfLinenumbers(word wSectionnr) const
+ {
+ return m_vIsh[wSectionnr].NumberOfLinenumbers;
+ }
+
+ /**
+ * Returns the characteristics of the section which is specified by the parameter wSectionnr.
+ * @param wSectionnr Index of the section.
+ * @return The characteristics of the section.
+ **/
+ template<int x>
+ dword PeHeaderT<x>::getCharacteristics(word wSectionnr) const
+ {
+ return m_vIsh[wSectionnr].Characteristics;
+ }
+
+ /**
+ * Changes the file's Nt signature.
+ * @param dwValue New value.
+ **/
+ template<int x>
+ void PeHeaderT<x>::setNtSignature(dword dwValue)
+ {
+ m_inthHeader.Signature = dwValue;
+ }
+
+ /**
+ * Changes the file's Machine.
+ * @param wValue New value.
+ **/
+ template<int x>
+ void PeHeaderT<x>::setMachine(word wValue)
+ {
+ m_inthHeader.FileHeader.Machine = wValue;
+ }
+
+ /**
+ * Changes the number of sections.
+ * @param wValue New value.
+ **/
+ template<int x>
+ void PeHeaderT<x>::setNumberOfSections(word wValue)
+ {
+ m_inthHeader.FileHeader.NumberOfSections = wValue;
+ }
+
+ /**
+ * Changes the file's TimeDateStamp.
+ * @param dwValue New value.
+ **/
+ template<int x>
+ void PeHeaderT<x>::setTimeDateStamp(dword dwValue)
+ {
+ m_inthHeader.FileHeader.TimeDateStamp = dwValue;
+ }
+
+ /**
+ * Changes the file's PointerToSymbolTable.
+ * @param dwValue New value.
+ **/
+ template<int x>
+ void PeHeaderT<x>::setPointerToSymbolTable(dword dwValue)
+ {
+ m_inthHeader.FileHeader.PointerToSymbolTable = dwValue;
+ }
+
+ /**
+ * Changes the file's NumberOfSymbols.
+ * @param dwValue New value.
+ **/
+ template<int x>
+ void PeHeaderT<x>::setNumberOfSymbols(dword dwValue)
+ {
+ m_inthHeader.FileHeader.NumberOfSymbols = dwValue;
+ }
+
+ /**
+ * Changes the file's SizeOfOptionalHeader.
+ * @param wValue New value.
+ **/
+ template<int x>
+ void PeHeaderT<x>::setSizeOfOptionalHeader(word wValue)
+ {
+ m_inthHeader.FileHeader.SizeOfOptionalHeader = wValue;
+ }
+
+ /**
+ * Changes the file's Characteristics.
+ * @param wValue New value.
+ **/
+ template<int x>
+ void PeHeaderT<x>::setCharacteristics(word wValue)
+ {
+ m_inthHeader.FileHeader.Characteristics = wValue;
+ }
+
+ /**
+ * Changes the file's Magic.
+ * @param wValue New value.
+ **/
+ template<int x>
+ void PeHeaderT<x>::setMagic(word wValue)
+ {
+ m_inthHeader.OptionalHeader.Magic = wValue;
+ }
+
+ /**
+ * Changes the file's MajorLinkerVersion.
+ * @param bValue New value.
+ **/
+ template<int x>
+ void PeHeaderT<x>::setMajorLinkerVersion(byte bValue)
+ {
+ m_inthHeader.OptionalHeader.MajorLinkerVersion = bValue;
+ }
+
+ /**
+ * Changes the file's MinorLinkerVersion.
+ * @param bValue New value.
+ **/
+ template<int x>
+ void PeHeaderT<x>::setMinorLinkerVersion(byte bValue)
+ {
+ m_inthHeader.OptionalHeader.MinorLinkerVersion = bValue;
+ }
+
+ /**
+ * Changes the file's SizeOfCode.
+ * @param dwValue New value.
+ **/
+ template<int x>
+ void PeHeaderT<x>::setSizeOfCode(dword dwValue)
+ {
+ m_inthHeader.OptionalHeader.SizeOfCode = dwValue;
+ }
+
+ /**
+ * Changes the file's SizeOfInitializedData.
+ * @param dwValue New value.
+ **/
+ template<int x>
+ void PeHeaderT<x>::setSizeOfInitializedData(dword dwValue)
+ {
+ m_inthHeader.OptionalHeader.SizeOfInitializedData = dwValue;
+ }
+
+ /**
+ * Changes the file's SizeOfUninitializedData.
+ * @param dwValue New value.
+ **/
+ template<int x>
+ void PeHeaderT<x>::setSizeOfUninitializedData(dword dwValue)
+ {
+ m_inthHeader.OptionalHeader.SizeOfUninitializedData = dwValue;
+ }
+
+ /**
+ * Changes the file's AddressOfEntryPoint.
+ * @param dwValue New value.
+ **/
+ template<int x>
+ void PeHeaderT<x>::setAddressOfEntryPoint(dword dwValue)
+ {
+ m_inthHeader.OptionalHeader.AddressOfEntryPoint = dwValue;
+ }
+
+ /**
+ * Changes the file's BaseOfCode.
+ * @param dwValue New value.
+ **/
+ template<int x>
+ void PeHeaderT<x>::setBaseOfCode(dword dwValue)
+ {
+ m_inthHeader.OptionalHeader.BaseOfCode = dwValue;
+ }
+
+ /**
+ * Changes the file's ImageBase.
+ * @param dwValue New value.
+ **/
+ template<int x>
+ void PeHeaderT<x>::setImageBase(typename FieldSizes<x>::VAR4_8 dwValue)
+ {
+ m_inthHeader.OptionalHeader.ImageBase = dwValue;
+ }
+
+ /**
+ * Changes the file's SectionAlignment.
+ * @param dwValue New value.
+ **/
+ template<int x>
+ void PeHeaderT<x>::setSectionAlignment(dword dwValue)
+ {
+ m_inthHeader.OptionalHeader.SectionAlignment = dwValue;
+ }
+
+ /**
+ * Changes the file's FileAlignment.
+ * @param dwValue New value.
+ **/
+ template<int x>
+ void PeHeaderT<x>::setFileAlignment(dword dwValue)
+ {
+ m_inthHeader.OptionalHeader.FileAlignment = dwValue;
+ }
+
+ /**
+ * Changes the file's MajorOperatingSystemVersion.
+ * @param wValue New value.
+ **/
+ template<int x>
+ void PeHeaderT<x>::setMajorOperatingSystemVersion(word wValue)
+ {
+ m_inthHeader.OptionalHeader.MajorOperatingSystemVersion = wValue;
+ }
+
+ /**
+ * Changes the file's MinorOperatingSystemVersion.
+ * @param wValue New value.
+ **/
+ template<int x>
+ void PeHeaderT<x>::setMinorOperatingSystemVersion(word wValue)
+ {
+ m_inthHeader.OptionalHeader.MinorOperatingSystemVersion = wValue;
+ }
+
+ /**
+ * Changes the file's MajorImageVersion.
+ * @param wValue New value.
+ **/
+ template<int x>
+ void PeHeaderT<x>::setMajorImageVersion(word wValue)
+ {
+ m_inthHeader.OptionalHeader.MajorImageVersion = wValue;
+ }
+
+ /**
+ * Changes the file's MinorImageVersion.
+ * @param wValue New value.
+ **/
+ template<int x>
+ void PeHeaderT<x>::setMinorImageVersion(word wValue)
+ {
+ m_inthHeader.OptionalHeader.MinorImageVersion = wValue;
+ }
+
+ /**
+ * Changes the file's MajorSubsystemVersion.
+ * @param wValue New value.
+ **/
+ template<int x>
+ void PeHeaderT<x>::setMajorSubsystemVersion(word wValue)
+ {
+ m_inthHeader.OptionalHeader.MajorSubsystemVersion = wValue;
+ }
+
+ /**
+ * Changes the file's MinorSubsystemVersion.
+ * @param wValue New value.
+ **/
+ template<int x>
+ void PeHeaderT<x>::setMinorSubsystemVersion(word wValue)
+ {
+ m_inthHeader.OptionalHeader.MinorSubsystemVersion = wValue;
+ }
+
+ /**
+ * Changes the file's Win32VersionValue.
+ * @param dwValue New value.
+ **/
+ template<int x>
+ void PeHeaderT<x>::setWin32VersionValue(dword dwValue)
+ {
+ m_inthHeader.OptionalHeader.Win32VersionValue = dwValue;
+ }
+
+ /**
+ * Changes the file's SizeOfImage.
+ * @param dwValue New value.
+ **/
+ template<int x>
+ void PeHeaderT<x>::setSizeOfImage(dword dwValue)
+ {
+ m_inthHeader.OptionalHeader.SizeOfImage = dwValue;
+ }
+
+ /**
+ * Changes the file's SizeOfHeaders.
+ * @param dwValue New value.
+ **/
+ template<int x>
+ void PeHeaderT<x>::setSizeOfHeaders(dword dwValue)
+ {
+ m_inthHeader.OptionalHeader.SizeOfHeaders = dwValue;
+ }
+
+ /**
+ * Changes the file's CheckSum.
+ * @param dwValue New value.
+ **/
+ template<int x>
+ void PeHeaderT<x>::setCheckSum(dword dwValue)
+ {
+ m_inthHeader.OptionalHeader.CheckSum = dwValue;
+ }
+
+ /**
+ * Changes the file's Subsystem.
+ * @param wValue New value.
+ **/
+ template<int x>
+ void PeHeaderT<x>::setSubsystem(word wValue)
+ {
+ m_inthHeader.OptionalHeader.Subsystem = wValue;
+ }
+
+ /**
+ * Changes the file's DllCharacteristics.
+ * @param wValue New value.
+ **/
+ template<int x>
+ void PeHeaderT<x>::setDllCharacteristics(word wValue)
+ {
+ m_inthHeader.OptionalHeader.DllCharacteristics = wValue;
+ }
+
+ /**
+ * Changes the file's SizeOfStackReserve.
+ * @param dwValue New value.
+ **/
+ template<int x>
+ void PeHeaderT<x>::setSizeOfStackReserve(typename FieldSizes<x>::VAR4_8 dwValue)
+ {
+ m_inthHeader.OptionalHeader.SizeOfStackReserve = dwValue;
+ }
+
+ /**
+ * Changes the file's SizeOfStackCommit.
+ * @param dwValue New value.
+ **/
+ template<int x>
+ void PeHeaderT<x>::setSizeOfStackCommit(typename FieldSizes<x>::VAR4_8 dwValue)
+ {
+ m_inthHeader.OptionalHeader.SizeOfStackCommit = dwValue;
+ }
+
+ /**
+ * Changes the file's SizeOfHeapReserve.
+ * @param dwValue New value.
+ **/
+ template<int x>
+ void PeHeaderT<x>::setSizeOfHeapReserve(typename FieldSizes<x>::VAR4_8 dwValue)
+ {
+ m_inthHeader.OptionalHeader.SizeOfHeapReserve = dwValue;
+ }
+
+ /**
+ * Changes the file's SizeOfHeapCommit.
+ * @param dwValue New value.
+ **/
+ template<int x>
+ void PeHeaderT<x>::setSizeOfHeapCommit(typename FieldSizes<x>::VAR4_8 dwValue)
+ {
+ m_inthHeader.OptionalHeader.SizeOfHeapCommit = dwValue;
+ }
+
+ /**
+ * Changes the file's LoaderFlags.
+ * @param dwValue New value.
+ **/
+ template<int x>
+ void PeHeaderT<x>::setLoaderFlags(dword dwValue)
+ {
+ m_inthHeader.OptionalHeader.LoaderFlags = dwValue;
+ }
+
+ /**
+ * Changes the file's NumberOfRvaAndSizes.
+ * @param dwValue New value.
+ **/
+ template<int x>
+ void PeHeaderT<x>::setNumberOfRvaAndSizes(dword dwValue)
+ {
+ m_inthHeader.OptionalHeader.NumberOfRvaAndSizes = dwValue;
+ }
+
+ template<int x>
+ void PeHeaderT<x>::setIddDebugRva(dword dwValue)
+ {
+ m_inthHeader.dataDirectories[PELIB_IMAGE_DIRECTORY_ENTRY_DEBUG].VirtualAddress = dwValue;
+ }
+
+ template<int x>
+ void PeHeaderT<x>::setIddDebugSize(dword dwValue)
+ {
+ m_inthHeader.dataDirectories[PELIB_IMAGE_DIRECTORY_ENTRY_DEBUG].Size = dwValue;
+ }
+
+ template<int x>
+ void PeHeaderT<x>::setIddDelayImportRva(dword dwValue)
+ {
+ m_inthHeader.dataDirectories[PELIB_IMAGE_DIRECTORY_ENTRY_DELAY_IMPORT].VirtualAddress = dwValue;
+ }
+
+ template<int x>
+ void PeHeaderT<x>::setIddDelayImportSize(dword dwValue)
+ {
+ m_inthHeader.dataDirectories[PELIB_IMAGE_DIRECTORY_ENTRY_DELAY_IMPORT].Size = dwValue;
+ }
+
+ template<int x>
+ void PeHeaderT<x>::setIddExceptionRva(dword dwValue)
+ {
+ m_inthHeader.dataDirectories[PELIB_IMAGE_DIRECTORY_ENTRY_EXCEPTION].VirtualAddress = dwValue;
+ }
+
+ template<int x>
+ void PeHeaderT<x>::setIddExceptionSize(dword dwValue)
+ {
+ m_inthHeader.dataDirectories[PELIB_IMAGE_DIRECTORY_ENTRY_EXCEPTION].Size = dwValue;
+ }
+
+ template<int x>
+ void PeHeaderT<x>::setIddGlobalPtrRva(dword dwValue)
+ {
+ m_inthHeader.dataDirectories[PELIB_IMAGE_DIRECTORY_ENTRY_GLOBALPTR].VirtualAddress = dwValue;
+ }
+
+ template<int x>
+ void PeHeaderT<x>::setIddGlobalPtrSize(dword dwValue)
+ {
+ m_inthHeader.dataDirectories[PELIB_IMAGE_DIRECTORY_ENTRY_GLOBALPTR].Size = dwValue;
+ }
+
+ template<int x>
+ void PeHeaderT<x>::setIddIatRva(dword dwValue)
+ {
+ m_inthHeader.dataDirectories[PELIB_IMAGE_DIRECTORY_ENTRY_IAT].VirtualAddress = dwValue;
+ }
+
+ template<int x>
+ void PeHeaderT<x>::setIddIatSize(dword dwValue)
+ {
+ m_inthHeader.dataDirectories[PELIB_IMAGE_DIRECTORY_ENTRY_IAT].Size = dwValue;
+ }
+
+ template<int x>
+ void PeHeaderT<x>::setIddLoadConfigRva(dword dwValue)
+ {
+ m_inthHeader.dataDirectories[PELIB_IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG].VirtualAddress = dwValue;
+ }
+
+ template<int x>
+ void PeHeaderT<x>::setIddLoadConfigSize(dword dwValue)
+ {
+ m_inthHeader.dataDirectories[PELIB_IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG].Size = dwValue;
+ }
+
+ template<int x>
+ void PeHeaderT<x>::setIddResourceRva(dword dwValue)
+ {
+ m_inthHeader.dataDirectories[PELIB_IMAGE_DIRECTORY_ENTRY_RESOURCE].VirtualAddress = dwValue;
+ }
+
+ template<int x>
+ void PeHeaderT<x>::setIddResourceSize(dword dwValue)
+ {
+ m_inthHeader.dataDirectories[PELIB_IMAGE_DIRECTORY_ENTRY_RESOURCE].Size = dwValue;
+ }
+
+ template<int x>
+ void PeHeaderT<x>::setIddSecurityRva(dword dwValue)
+ {
+ m_inthHeader.dataDirectories[PELIB_IMAGE_DIRECTORY_ENTRY_SECURITY].VirtualAddress = dwValue;
+ }
+
+ template<int x>
+ void PeHeaderT<x>::setIddSecuritySize(dword dwValue)
+ {
+ m_inthHeader.dataDirectories[PELIB_IMAGE_DIRECTORY_ENTRY_SECURITY].Size = dwValue;
+ }
+
+ template<int x>
+ void PeHeaderT<x>::setIddTlsRva(dword dwValue)
+ {
+ m_inthHeader.dataDirectories[PELIB_IMAGE_DIRECTORY_ENTRY_TLS].VirtualAddress = dwValue;
+ }
+
+ template<int x>
+ void PeHeaderT<x>::setIddTlsSize(dword dwValue)
+ {
+ m_inthHeader.dataDirectories[PELIB_IMAGE_DIRECTORY_ENTRY_TLS].Size = dwValue;
+ }
+
+ /**
+ * Changes the rva of the file's export directory.
+ * @param dwValue New value.
+ **/
+ template<int x>
+ void PeHeaderT<x>::setIddExportRva(dword dwValue)
+ {
+ m_inthHeader.dataDirectories[PELIB_IMAGE_DIRECTORY_ENTRY_EXPORT].VirtualAddress = dwValue;
+ }
+
+ /**
+ * Changes the size of the file's export directory.
+ * @param dwValue New value.
+ **/
+ template<int x>
+ void PeHeaderT<x>::setIddExportSize(dword dwValue)
+ {
+ m_inthHeader.dataDirectories[PELIB_IMAGE_DIRECTORY_ENTRY_EXPORT].Size = dwValue;
+ }
+
+ template<int x>
+ void PeHeaderT<x>::setIddBaseRelocRva(dword value)
+ {
+ m_inthHeader.dataDirectories[PELIB_IMAGE_DIRECTORY_ENTRY_BASERELOC].VirtualAddress = value;
+ }
+
+ template<int x>
+ void PeHeaderT<x>::setIddBaseRelocSize(dword value)
+ {
+ m_inthHeader.dataDirectories[PELIB_IMAGE_DIRECTORY_ENTRY_BASERELOC].Size = value;
+ }
+
+ template<int x>
+ void PeHeaderT<x>::setIddArchitectureRva(dword value)
+ {
+ m_inthHeader.dataDirectories[PELIB_IMAGE_DIRECTORY_ENTRY_ARCHITECTURE].VirtualAddress = value;
+ }
+
+ template<int x>
+ void PeHeaderT<x>::setIddArchitectureSize(dword value)
+ {
+ m_inthHeader.dataDirectories[PELIB_IMAGE_DIRECTORY_ENTRY_ARCHITECTURE].Size = value;
+ }
+
+ template<int x>
+ void PeHeaderT<x>::setIddComHeaderRva(dword value)
+ {
+ m_inthHeader.dataDirectories[PELIB_IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR].VirtualAddress = value;
+ }
+
+ template<int x>
+ void PeHeaderT<x>::setIddComHeaderSize(dword value)
+ {
+ m_inthHeader.dataDirectories[PELIB_IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR].Size = value;
+ }
+
+ /**
+ * Changes the rva of the file's import directory.
+ * @param dwValue New value.
+ **/
+ template<int x>
+ void PeHeaderT<x>::setIddImportRva(dword dwValue)
+ {
+ m_inthHeader.dataDirectories[PELIB_IMAGE_DIRECTORY_ENTRY_IMPORT].VirtualAddress = dwValue;
+ }
+
+ /**
+ * Changes the size of the file's import directory.
+ * @param dwValue New value.
+ **/
+ template<int x>
+ void PeHeaderT<x>::setIddImportSize(dword dwValue)
+ {
+ m_inthHeader.dataDirectories[PELIB_IMAGE_DIRECTORY_ENTRY_IMPORT].Size = dwValue;
+ }
+
+ /**
+ * Changes the name of a section.
+ * @param wSectionnr Identifier of the section
+ * @param strName New name.
+ **/
+ template<int x>
+ void PeHeaderT<x>::setSectionName(word wSectionnr, std::string strName)
+ {
+ strncpy(reinterpret_cast<char*>(m_vIsh[wSectionnr].Name), strName.c_str(), sizeof(m_vIsh[wSectionnr].Name));
+ }
+
+ /**
+ * Changes the virtual size of a section.
+ * @param wSectionnr Identifier of the section
+ * @param dwValue New value.
+ **/
+ template<int x>
+ void PeHeaderT<x>::setVirtualSize(word wSectionnr, dword dwValue)
+ {
+ m_vIsh[wSectionnr].VirtualSize = dwValue;
+ }
+
+ /**
+ * Changes the virtual address of a section.
+ * @param wSectionnr Identifier of the section
+ * @param dwValue New value.
+ **/
+ template<int x>
+ void PeHeaderT<x>::setVirtualAddress(word wSectionnr, dword dwValue)
+ {
+ m_vIsh[wSectionnr].VirtualAddress = dwValue;
+ }
+
+ /**
+ * Changes the size of raw data of a section.
+ * @param wSectionnr Identifier of the section
+ * @param dwValue New value.
+ **/
+ template<int x>
+ void PeHeaderT<x>::setSizeOfRawData(word wSectionnr, dword dwValue)
+ {
+ m_vIsh[wSectionnr].SizeOfRawData = dwValue;
+ }
+
+ /**
+ * Changes the size of raw data of a section.
+ * @param wSectionnr Identifier of the section
+ * @param dwValue New value.
+ **/
+ template<int x>
+ void PeHeaderT<x>::setPointerToRawData(word wSectionnr, dword dwValue)
+ {
+ m_vIsh[wSectionnr].PointerToRawData = dwValue;
+ }
+
+ /**
+ * Changes the pointer to relocations of a section.
+ * @param wSectionnr Identifier of the section
+ * @param dwValue New value.
+ **/
+ template<int x>
+ void PeHeaderT<x>::setPointerToRelocations(word wSectionnr, dword dwValue)
+ {
+ m_vIsh[wSectionnr].PointerToRelocations = dwValue;
+ }
+
+ /**
+ * Changes the pointer to line numbers of a section.
+ * @param wSectionnr Identifier of the section
+ * @param dwValue New value.
+ **/
+ template<int x>
+ void PeHeaderT<x>::setPointerToLinenumbers(word wSectionnr, dword dwValue)
+ {
+ m_vIsh[wSectionnr].PointerToLinenumbers = dwValue;
+ }
+
+ /**
+ * Changes the number of relocations of a section.
+ * @param wSectionnr Identifier of the section
+ * @param dwValue New value.
+ **/
+ template<int x>
+ void PeHeaderT<x>::setNumberOfRelocations(word wSectionnr, dword dwValue)
+ {
+ m_vIsh[wSectionnr].NumberOfRelocations = dwValue;
+ }
+
+ /**
+ * Changes the number of line numbers of a section.
+ * @param wSectionnr Identifier of the section
+ * @param dwValue New value.
+ **/
+ template<int x>
+ void PeHeaderT<x>::setNumberOfLinenumbers(word wSectionnr, dword dwValue)
+ {
+ m_vIsh[wSectionnr].NumberOfLinenumbers = dwValue;
+ }
+
+ /**
+ * Changes the characteristics of a section.
+ * @param wSectionnr Identifier of the section
+ * @param dwValue New value.
+ **/
+ template<int x>
+ void PeHeaderT<x>::setCharacteristics(word wSectionnr, dword dwValue)
+ {
+ m_vIsh[wSectionnr].Characteristics = dwValue;
+ }
+
+}
+
+#endif
diff --git a/utils/zenutils/libraries/pelib-0.9/pelib/PeLib.h b/utils/zenutils/libraries/pelib-0.9/pelib/PeLib.h
index 870c873606..12f9693dda 100755..100644
--- a/utils/zenutils/libraries/pelib-0.9/pelib/PeLib.h
+++ b/utils/zenutils/libraries/pelib-0.9/pelib/PeLib.h
@@ -1,27 +1,27 @@
-/*
-* PeLib.h - Part of the PeLib library.
-*
-* Copyright (c) 2004 - 2005 Sebastian Porst (webmaster@the-interweb.com)
-* All rights reserved.
-*
-* This software is licensed under the zlib/libpng License.
-* For more details see http://www.opensource.org/licenses/zlib-license.php
-* or the license information file (license.htm) in the root directory
-* of PeLib.
-*/
-
-
-/** \mainpage PeLib - An open-source C++ library to modify PE files.
-*
-* \section intro Introduction
-*
-* <b>What is PeLib?</b><br>
-* PeLib is an open source C++ library with the purpose to ease the access and modification of PE (Portable executable) files. This is done through a multitude of classes which represent all important PE header and directory structures and which provide the necessary functions to read, modify and write these structures. As this library is fully ISO C++ compliant it should compile with all popular compilers for the Windows platform. For people who don't use C++ in their projects a dynamic link library is provided which encapsulates all of PeLib's functionality so that projects using programming languages as diverse as Win32 assembler, C# or Delphi can still benefit from PeLib's power
-*/
-
-#ifndef PELIB_H
-#define PELIB_H
-
-#include "PeFile.h"
-
-#endif
+/*
+* PeLib.h - Part of the PeLib library.
+*
+* Copyright (c) 2004 - 2005 Sebastian Porst (webmaster@the-interweb.com)
+* All rights reserved.
+*
+* This software is licensed under the zlib/libpng License.
+* For more details see http://www.opensource.org/licenses/zlib-license.php
+* or the license information file (license.htm) in the root directory
+* of PeLib.
+*/
+
+
+/** \mainpage PeLib - An open-source C++ library to modify PE files.
+*
+* \section intro Introduction
+*
+* <b>What is PeLib?</b><br>
+* PeLib is an open source C++ library with the purpose to ease the access and modification of PE (Portable executable) files. This is done through a multitude of classes which represent all important PE header and directory structures and which provide the necessary functions to read, modify and write these structures. As this library is fully ISO C++ compliant it should compile with all popular compilers for the Windows platform. For people who don't use C++ in their projects a dynamic link library is provided which encapsulates all of PeLib's functionality so that projects using programming languages as diverse as Win32 assembler, C# or Delphi can still benefit from PeLib's power
+*/
+
+#ifndef PELIB_H
+#define PELIB_H
+
+#include "PeFile.h"
+
+#endif
diff --git a/utils/zenutils/libraries/pelib-0.9/pelib/PeLibAux.cpp b/utils/zenutils/libraries/pelib-0.9/pelib/PeLibAux.cpp
index 1e06bae620..0a420ebecf 100755..100644
--- a/utils/zenutils/libraries/pelib-0.9/pelib/PeLibAux.cpp
+++ b/utils/zenutils/libraries/pelib-0.9/pelib/PeLibAux.cpp
@@ -1,275 +1,275 @@
-/*
-* PeLibAux.cpp - Part of the PeLib library.
-*
-* Copyright (c) 2004 - 2005 Sebastian Porst (webmaster@the-interweb.com)
-* All rights reserved.
-*
-* This software is licensed under the zlib/libpng License.
-* For more details see http://www.opensource.org/licenses/zlib-license.php
-* or the license information file (license.htm) in the root directory
-* of PeLib.
-*/
-
-#include "PeLibInc.h"
-#include "PeLibAux.h"
-#include "PeFile.h"
-
-#ifdef _MSC_VER
- #include <ctype.h>
-#endif
-
-namespace PeLib
-{
- const qword PELIB_IMAGE_ORDINAL_FLAGS<64>::IMAGE_ORDINAL_FLAG = 0x8000000000000000ULL;
-
- bool PELIB_IMAGE_SECTION_HEADER::biggerFileOffset(const PELIB_IMAGE_SECTION_HEADER& ish) const
- {
- return PointerToRawData < ish.PointerToRawData;
- }
-
- bool PELIB_IMAGE_SECTION_HEADER::biggerVirtualAddress(const PELIB_IMAGE_SECTION_HEADER& ish) const
- {
- return VirtualAddress < ish.VirtualAddress;
- }
-
- unsigned int alignOffset(unsigned int uiOffset, unsigned int uiAlignment)
- {
- if (!uiAlignment) return uiAlignment;
- return (uiOffset % uiAlignment) ? uiOffset + (uiAlignment - uiOffset % uiAlignment) : uiOffset;
- }
-
- unsigned int fileSize(const std::string& filename)
- {
- std::fstream file(filename.c_str());
- file.seekg(0, std::ios::end);
- return file.tellg();
- }
-
- unsigned int fileSize(std::ifstream& file)
- {
- unsigned int oldpos = file.tellg();
- file.seekg(0, std::ios::end);
- unsigned int filesize = file.tellg();
- file.seekg(oldpos, std::ios::beg);
- return filesize;
- }
-
- unsigned int fileSize(std::fstream& file)
- {
- unsigned int oldpos = file.tellg();
- file.seekg(0, std::ios::end);
- unsigned int filesize = file.tellg();
- file.seekg(oldpos, std::ios::beg);
- return filesize;
- }
-
- unsigned int fileSize(std::ofstream& file)
- {
- unsigned int oldpos = file.tellp();
- file.seekp(0, std::ios::end);
- unsigned int filesize = file.tellp();
- file.seekp(oldpos, std::ios::beg);
- return filesize;
- }
-
- bool isEqualNc(const std::string& s1, const std::string& s2)
- {
- std::string t1 = s1;
- std::string t2 = s2;
-
- // No std:: to make VC++ happy
- #ifdef _MSC_VER
- std::transform(t1.begin(), t1.end(), t1.begin(), toupper);
- std::transform(t2.begin(), t2.end(), t2.begin(), toupper);
- #else
- // Weird syntax to make Borland C++ happy
- std::transform(t1.begin(), t1.end(), t1.begin(), (int(*)(int))std::toupper);
- std::transform(t2.begin(), t2.end(), t2.begin(), (int(*)(int))std::toupper);
- #endif
- return t1 == t2;
- }
-
- PELIB_IMAGE_DOS_HEADER::PELIB_IMAGE_DOS_HEADER()
- {
- e_magic = 0;
- e_cblp = 0;
- e_cp = 0;
- e_crlc = 0;
- e_cparhdr = 0;
- e_minalloc = 0;
- e_maxalloc = 0;
- e_ss = 0;
- e_sp = 0;
- e_csum = 0;
- e_ip = 0;
- e_cs = 0;
- e_lfarlc = 0;
- e_ovno = 0;
-
- for (unsigned int i=0;i<sizeof(e_res)/sizeof(e_res[0]);i++)
- {
- e_res[i] = 0;
- }
-
- e_oemid = 0;
- e_oeminfo = 0;
-
- for (unsigned int i=0;i<sizeof(e_res2)/sizeof(e_res2[0]);i++)
- {
- e_res2[i] = 0;
- }
-
- e_lfanew = 0;
- }
-
- PELIB_EXP_FUNC_INFORMATION::PELIB_EXP_FUNC_INFORMATION()
- {
- addroffunc = 0;
- addrofname = 0;
- ordinal = 0;
- }
-
- PELIB_IMAGE_RESOURCE_DIRECTORY::PELIB_IMAGE_RESOURCE_DIRECTORY()
- {
- Characteristics = 0;
- TimeDateStamp = 0;
- MajorVersion = 0;
- MinorVersion = 0;
- NumberOfNamedEntries = 0;
- NumberOfIdEntries = 0;
- }
-
- PELIB_IMAGE_RESOURCE_DIRECTORY_ENTRY::PELIB_IMAGE_RESOURCE_DIRECTORY_ENTRY()
- {
- Name = 0;
- OffsetToData = 0;
- }
-
- bool PELIB_IMG_RES_DIR_ENTRY::operator<(const PELIB_IMG_RES_DIR_ENTRY& first) const
- {
- if (irde.Name & PELIB_IMAGE_RESOURCE_NAME_IS_STRING && first.irde.Name & PELIB_IMAGE_RESOURCE_NAME_IS_STRING)
- {
- return wstrName < first.wstrName;
- }
- else if (irde.Name & PELIB_IMAGE_RESOURCE_NAME_IS_STRING)
- {
- return true;
- }
- else if (first.irde.Name & PELIB_IMAGE_RESOURCE_NAME_IS_STRING)
- {
- return false;
- }
- else
- {
- return irde.Name < first.irde.Name;
- }
- }
-
- PELIB_IMAGE_BASE_RELOCATION::PELIB_IMAGE_BASE_RELOCATION()
- {
- VirtualAddress = 0;
- SizeOfBlock = 0;
- }
-
- PELIB_IMAGE_COR20_HEADER::PELIB_IMAGE_COR20_HEADER()
- {
- cb = 0;
- MajorRuntimeVersion = 0;
- MinorRuntimeVersion = 0;
- MetaData.VirtualAddress = 0;
- MetaData.Size = 0;
- Flags = 0;
- EntryPointToken = 0;
- Resources.VirtualAddress = 0;
- Resources.Size = 0;
- StrongNameSignature.VirtualAddress = 0;
- StrongNameSignature.Size = 0;
- CodeManagerTable.VirtualAddress = 0;
- CodeManagerTable.Size = 0;
- VTableFixups.VirtualAddress = 0;
- VTableFixups.Size = 0;
- ExportAddressTableJumps.VirtualAddress = 0;
- ExportAddressTableJumps.Size = 0;
- ManagedNativeHeader.VirtualAddress = 0;
- ManagedNativeHeader.Size = 0;
- }
-
- /** Compares the passed filename to the struct's filename.
- * @param strModuleName A filename.
- * @return True, if the passed filename equals the struct's filename. The comparison is case-sensitive.
- **/
- bool PELIB_IMAGE_BOUND_DIRECTORY::equal(const std::string strModuleName) const
- {
- return this->strModuleName == strModuleName;
- }
-
- bool PELIB_EXP_FUNC_INFORMATION::equal(const std::string strFunctionName) const
- {
- return isEqualNc(this->funcname, strFunctionName);
- }
-
- /**
- * @param strFilename Name of a file.
- * @return Either PEFILE32, PEFILE64 or PEFILE_UNKNOWN
- **/
- unsigned int getFileType(const std::string strFilename)
- {
- word machine, magic;
-
- PeFile32 pef(strFilename);
- if (pef.readMzHeader() != NO_ERROR) return PEFILE_UNKNOWN;
- if (pef.readPeHeader() != NO_ERROR) return PEFILE_UNKNOWN;
-
- machine = pef.peHeader().getMachine();
- magic = pef.peHeader().getMagic();
-
- if (machine == PELIB_IMAGE_FILE_MACHINE_I386 && magic == PELIB_IMAGE_NT_OPTIONAL_HDR32_MAGIC)
- {
- return PEFILE32;
- }
- // 0x8664 == AMD64; no named constant yet
- else if ((machine == 0x8664 || machine == PELIB_IMAGE_FILE_MACHINE_IA64) && magic == PELIB_IMAGE_NT_OPTIONAL_HDR64_MAGIC)
- {
- return PEFILE64;
- }
- else
- {
- return PEFILE_UNKNOWN;
- }
- }
-
- /**
- * Opens a PE file. The return type is either PeFile32 or PeFile64 object. If an error occurs the return
- * value is 0.
- * @param strFilename Name of a file.
- * @return Either a PeFile32 object, a PeFil64 object or 0.
- **/
- PeFile* openPeFile(const std::string& strFilename)
- {
- unsigned int type = getFileType(strFilename);
-
- if (type == PEFILE32)
- {
- return new PeFile32(strFilename);
- }
- else if (type == PEFILE64)
- {
- return new PeFile64(strFilename);
- }
- else
- {
- return 0;
- }
- }
-
- unsigned int PELIB_IMAGE_BOUND_DIRECTORY::size() const
- {
- unsigned int size = 0;
- for (unsigned int i=0;i<moduleForwarders.size();i++)
- {
- size += moduleForwarders[i].size();
- }
-
- return size + PELIB_IMAGE_BOUND_IMPORT_DESCRIPTOR::size() + strModuleName.size() + 1;
- }
-}
+/*
+* PeLibAux.cpp - Part of the PeLib library.
+*
+* Copyright (c) 2004 - 2005 Sebastian Porst (webmaster@the-interweb.com)
+* All rights reserved.
+*
+* This software is licensed under the zlib/libpng License.
+* For more details see http://www.opensource.org/licenses/zlib-license.php
+* or the license information file (license.htm) in the root directory
+* of PeLib.
+*/
+
+#include "PeLibInc.h"
+#include "PeLibAux.h"
+#include "PeFile.h"
+
+#ifdef _MSC_VER
+ #include <ctype.h>
+#endif
+
+namespace PeLib
+{
+ const qword PELIB_IMAGE_ORDINAL_FLAGS<64>::IMAGE_ORDINAL_FLAG = 0x8000000000000000ULL;
+
+ bool PELIB_IMAGE_SECTION_HEADER::biggerFileOffset(const PELIB_IMAGE_SECTION_HEADER& ish) const
+ {
+ return PointerToRawData < ish.PointerToRawData;
+ }
+
+ bool PELIB_IMAGE_SECTION_HEADER::biggerVirtualAddress(const PELIB_IMAGE_SECTION_HEADER& ish) const
+ {
+ return VirtualAddress < ish.VirtualAddress;
+ }
+
+ unsigned int alignOffset(unsigned int uiOffset, unsigned int uiAlignment)
+ {
+ if (!uiAlignment) return uiAlignment;
+ return (uiOffset % uiAlignment) ? uiOffset + (uiAlignment - uiOffset % uiAlignment) : uiOffset;
+ }
+
+ unsigned int fileSize(const std::string& filename)
+ {
+ std::fstream file(filename.c_str());
+ file.seekg(0, std::ios::end);
+ return file.tellg();
+ }
+
+ unsigned int fileSize(std::ifstream& file)
+ {
+ unsigned int oldpos = file.tellg();
+ file.seekg(0, std::ios::end);
+ unsigned int filesize = file.tellg();
+ file.seekg(oldpos, std::ios::beg);
+ return filesize;
+ }
+
+ unsigned int fileSize(std::fstream& file)
+ {
+ unsigned int oldpos = file.tellg();
+ file.seekg(0, std::ios::end);
+ unsigned int filesize = file.tellg();
+ file.seekg(oldpos, std::ios::beg);
+ return filesize;
+ }
+
+ unsigned int fileSize(std::ofstream& file)
+ {
+ unsigned int oldpos = file.tellp();
+ file.seekp(0, std::ios::end);
+ unsigned int filesize = file.tellp();
+ file.seekp(oldpos, std::ios::beg);
+ return filesize;
+ }
+
+ bool isEqualNc(const std::string& s1, const std::string& s2)
+ {
+ std::string t1 = s1;
+ std::string t2 = s2;
+
+ // No std:: to make VC++ happy
+ #ifdef _MSC_VER
+ std::transform(t1.begin(), t1.end(), t1.begin(), toupper);
+ std::transform(t2.begin(), t2.end(), t2.begin(), toupper);
+ #else
+ // Weird syntax to make Borland C++ happy
+ std::transform(t1.begin(), t1.end(), t1.begin(), (int(*)(int))std::toupper);
+ std::transform(t2.begin(), t2.end(), t2.begin(), (int(*)(int))std::toupper);
+ #endif
+ return t1 == t2;
+ }
+
+ PELIB_IMAGE_DOS_HEADER::PELIB_IMAGE_DOS_HEADER()
+ {
+ e_magic = 0;
+ e_cblp = 0;
+ e_cp = 0;
+ e_crlc = 0;
+ e_cparhdr = 0;
+ e_minalloc = 0;
+ e_maxalloc = 0;
+ e_ss = 0;
+ e_sp = 0;
+ e_csum = 0;
+ e_ip = 0;
+ e_cs = 0;
+ e_lfarlc = 0;
+ e_ovno = 0;
+
+ for (unsigned int i=0;i<sizeof(e_res)/sizeof(e_res[0]);i++)
+ {
+ e_res[i] = 0;
+ }
+
+ e_oemid = 0;
+ e_oeminfo = 0;
+
+ for (unsigned int i=0;i<sizeof(e_res2)/sizeof(e_res2[0]);i++)
+ {
+ e_res2[i] = 0;
+ }
+
+ e_lfanew = 0;
+ }
+
+ PELIB_EXP_FUNC_INFORMATION::PELIB_EXP_FUNC_INFORMATION()
+ {
+ addroffunc = 0;
+ addrofname = 0;
+ ordinal = 0;
+ }
+
+ PELIB_IMAGE_RESOURCE_DIRECTORY::PELIB_IMAGE_RESOURCE_DIRECTORY()
+ {
+ Characteristics = 0;
+ TimeDateStamp = 0;
+ MajorVersion = 0;
+ MinorVersion = 0;
+ NumberOfNamedEntries = 0;
+ NumberOfIdEntries = 0;
+ }
+
+ PELIB_IMAGE_RESOURCE_DIRECTORY_ENTRY::PELIB_IMAGE_RESOURCE_DIRECTORY_ENTRY()
+ {
+ Name = 0;
+ OffsetToData = 0;
+ }
+
+ bool PELIB_IMG_RES_DIR_ENTRY::operator<(const PELIB_IMG_RES_DIR_ENTRY& first) const
+ {
+ if (irde.Name & PELIB_IMAGE_RESOURCE_NAME_IS_STRING && first.irde.Name & PELIB_IMAGE_RESOURCE_NAME_IS_STRING)
+ {
+ return wstrName < first.wstrName;
+ }
+ else if (irde.Name & PELIB_IMAGE_RESOURCE_NAME_IS_STRING)
+ {
+ return true;
+ }
+ else if (first.irde.Name & PELIB_IMAGE_RESOURCE_NAME_IS_STRING)
+ {
+ return false;
+ }
+ else
+ {
+ return irde.Name < first.irde.Name;
+ }
+ }
+
+ PELIB_IMAGE_BASE_RELOCATION::PELIB_IMAGE_BASE_RELOCATION()
+ {
+ VirtualAddress = 0;
+ SizeOfBlock = 0;
+ }
+
+ PELIB_IMAGE_COR20_HEADER::PELIB_IMAGE_COR20_HEADER()
+ {
+ cb = 0;
+ MajorRuntimeVersion = 0;
+ MinorRuntimeVersion = 0;
+ MetaData.VirtualAddress = 0;
+ MetaData.Size = 0;
+ Flags = 0;
+ EntryPointToken = 0;
+ Resources.VirtualAddress = 0;
+ Resources.Size = 0;
+ StrongNameSignature.VirtualAddress = 0;
+ StrongNameSignature.Size = 0;
+ CodeManagerTable.VirtualAddress = 0;
+ CodeManagerTable.Size = 0;
+ VTableFixups.VirtualAddress = 0;
+ VTableFixups.Size = 0;
+ ExportAddressTableJumps.VirtualAddress = 0;
+ ExportAddressTableJumps.Size = 0;
+ ManagedNativeHeader.VirtualAddress = 0;
+ ManagedNativeHeader.Size = 0;
+ }
+
+ /** Compares the passed filename to the struct's filename.
+ * @param strModuleName A filename.
+ * @return True, if the passed filename equals the struct's filename. The comparison is case-sensitive.
+ **/
+ bool PELIB_IMAGE_BOUND_DIRECTORY::equal(const std::string strModuleName) const
+ {
+ return this->strModuleName == strModuleName;
+ }
+
+ bool PELIB_EXP_FUNC_INFORMATION::equal(const std::string strFunctionName) const
+ {
+ return isEqualNc(this->funcname, strFunctionName);
+ }
+
+ /**
+ * @param strFilename Name of a file.
+ * @return Either PEFILE32, PEFILE64 or PEFILE_UNKNOWN
+ **/
+ unsigned int getFileType(const std::string strFilename)
+ {
+ word machine, magic;
+
+ PeFile32 pef(strFilename);
+ if (pef.readMzHeader() != NO_ERROR) return PEFILE_UNKNOWN;
+ if (pef.readPeHeader() != NO_ERROR) return PEFILE_UNKNOWN;
+
+ machine = pef.peHeader().getMachine();
+ magic = pef.peHeader().getMagic();
+
+ if (machine == PELIB_IMAGE_FILE_MACHINE_I386 && magic == PELIB_IMAGE_NT_OPTIONAL_HDR32_MAGIC)
+ {
+ return PEFILE32;
+ }
+ // 0x8664 == AMD64; no named constant yet
+ else if ((machine == 0x8664 || machine == PELIB_IMAGE_FILE_MACHINE_IA64) && magic == PELIB_IMAGE_NT_OPTIONAL_HDR64_MAGIC)
+ {
+ return PEFILE64;
+ }
+ else
+ {
+ return PEFILE_UNKNOWN;
+ }
+ }
+
+ /**
+ * Opens a PE file. The return type is either PeFile32 or PeFile64 object. If an error occurs the return
+ * value is 0.
+ * @param strFilename Name of a file.
+ * @return Either a PeFile32 object, a PeFil64 object or 0.
+ **/
+ PeFile* openPeFile(const std::string& strFilename)
+ {
+ unsigned int type = getFileType(strFilename);
+
+ if (type == PEFILE32)
+ {
+ return new PeFile32(strFilename);
+ }
+ else if (type == PEFILE64)
+ {
+ return new PeFile64(strFilename);
+ }
+ else
+ {
+ return 0;
+ }
+ }
+
+ unsigned int PELIB_IMAGE_BOUND_DIRECTORY::size() const
+ {
+ unsigned int size = 0;
+ for (unsigned int i=0;i<moduleForwarders.size();i++)
+ {
+ size += moduleForwarders[i].size();
+ }
+
+ return size + PELIB_IMAGE_BOUND_IMPORT_DESCRIPTOR::size() + strModuleName.size() + 1;
+ }
+}
diff --git a/utils/zenutils/libraries/pelib-0.9/pelib/PeLibAux.h b/utils/zenutils/libraries/pelib-0.9/pelib/PeLibAux.h
index 4aed866a7d..cfce60796f 100755..100644
--- a/utils/zenutils/libraries/pelib-0.9/pelib/PeLibAux.h
+++ b/utils/zenutils/libraries/pelib-0.9/pelib/PeLibAux.h
@@ -1,884 +1,884 @@
-/*
-* PeLibAux.cpp - Part of the PeLib library.
-*
-* Copyright (c) 2004 - 2005 Sebastian Porst (webmaster@the-interweb.com)
-* All rights reserved.
-*
-* This software is licensed under the zlib/libpng License.
-* For more details see http://www.opensource.org/licenses/zlib-license.php
-* or the license information file (license.htm) in the root directory
-* of PeLib.
-*/
-
-#ifndef PELIBAUX_H
-#define PELIBAUX_H
-
-//#include "PeLibInc.h"
-//#include "PeHeader.h"
-#include "buffer/OutputBuffer.h"
-#include "buffer/InputBuffer.h"
-//#include "buffer/ResTree.h"
-#include <numeric>
-#include <limits>
-
-namespace PeLib
-{
- enum errorCodes
- {
- NO_ERROR = 0,
- ERROR_OPENING_FILE = -1,
- ERROR_INVALID_FILE = -2,
- ERROR_TOO_MANY_SECTIONS = -3,
- ERROR_NOT_ENOUGH_SPACE = -4,
- ERROR_NO_FILE_ALIGNMENT = -5,
- ERROR_NO_SECTION_ALIGNMENT = -6,
- ERROR_ENTRY_NOT_FOUND = -7,
- ERROR_DUPLICATE_ENTRY = -8,
- ERROR_DIRECTORY_DOES_NOT_EXIST = -9
- };
-
- class PeFile;
- bool isEqualNc(const std::string& s1, const std::string& s2);
-
-// It's necessary to make sure that a byte has 8 bits and that the platform has a 8 bit type,
-// a 16bit type and a bit type. That's because binary PE files are pretty picky about their
-// structure.
-
- #if CHAR_BIT == 8
- #if UCHAR_MAX == 255
- typedef unsigned char byte;
- // typedef std::bitset<8> byte;
- #else
- #error You need to change some typedefs (Code: 8). Please read the PeLib documentation.
- #endif
-
- #if USHRT_MAX == 65535U
- typedef unsigned short word;
- // typedef std::bitset<16> word;
- #else
- #error You need to change some typedefs (Code: 16). Please read the PeLib documentation.
- #endif
-
- #if UINT_MAX == 4294967295UL
- typedef unsigned int dword;
- // typedef std::bitset<32> dword;
- #else
- #error You need to change some typedefs (Code: 32). Please read the PeLib documentation.
- #endif
-
- typedef unsigned long long qword;
-
-// #if ULLONG_MAX == 18446744073709551615
-// typedef unsigned long long qword;
-// #else
-// #error You need to change some typedefs (Code: 32). Please read the PeLib documentation.
-// #endif
- #else
- #error You need to change some typedefs. Please read the PeLib documentation.
- #endif
-
-
-/* enum bits {BITS_BYTE = 8, BITS_WORD = 16, BITS_DWORD = 32};
-
- template<bits value>
- class DataType
- {
- private:
- std::bitset<value> bsValue;
- unsigned long ulValue;
-
- public:
- void operator=(unsigned long ulValue)
- {
- bsValue = ulValue;
- }
-
- operator unsigned long() const
- {
- return bsValue.to_ulong();
- }
-
- const int operator&()
- {
- ulValue = bsValue;
- return ulValue;
- }
-
- };
-
- typedef DataType<BITS_BYTE> byte;
- typedef DataType<BITS_WORD> word;
- typedef DataType<BITS_DWORD> dword;
-*/
-
- enum {PEFILE32 = 32,
- PEFILE64 = 64,
- PEFILE_UNKNOWN = 0};
-
- enum {BoundImportDirectoryId = 1,
- ComHeaderDirectoryId,
- ExportDirectoryId,
- IatDirectoryId,
- ImportDirectoryId,
- MzHeaderId,
- PeHeaderId,
- RelocationsId,
- PeFileId,
- ResourceDirectoryId,
- DebugDirectoryId,
- TlsDirectoryId
- };
-
- const word PELIB_IMAGE_DOS_SIGNATURE = 0x5A4D;
-
- const dword PELIB_IMAGE_NT_SIGNATURE = 0x00004550;
-
- template<int bits>
- struct PELIB_IMAGE_ORDINAL_FLAGS;
-
- template<>
- struct PELIB_IMAGE_ORDINAL_FLAGS<32>
- {
- static const dword IMAGE_ORDINAL_FLAG = 0x80000000;
- };
-
- template<>
- struct PELIB_IMAGE_ORDINAL_FLAGS<64>
- {
- static const qword IMAGE_ORDINAL_FLAG;
- };
-
- const unsigned long PELIB_IMAGE_NUMBEROF_DIRECTORY_ENTRIES = 16;
-
- const unsigned long PELIB_IMAGE_RESOURCE_NAME_IS_STRING = 0x80000000;
-
- const unsigned long PELIB_IMAGE_RESOURCE_DATA_IS_DIRECTORY = 0x80000000;
-
- enum
- {
- PELIB_IMAGE_DIRECTORY_ENTRY_EXPORT, // OK
- PELIB_IMAGE_DIRECTORY_ENTRY_IMPORT, // OK
- PELIB_IMAGE_DIRECTORY_ENTRY_RESOURCE, // OK
- PELIB_IMAGE_DIRECTORY_ENTRY_EXCEPTION,
- PELIB_IMAGE_DIRECTORY_ENTRY_SECURITY,
- PELIB_IMAGE_DIRECTORY_ENTRY_BASERELOC, // OK
- PELIB_IMAGE_DIRECTORY_ENTRY_DEBUG,
- PELIB_IMAGE_DIRECTORY_ENTRY_ARCHITECTURE,
- PELIB_IMAGE_DIRECTORY_ENTRY_GLOBALPTR,
- PELIB_IMAGE_DIRECTORY_ENTRY_TLS,
- PELIB_IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG,
- PELIB_IMAGE_DIRECTORY_ENTRY_BOUND_IMPORT, // OK
- PELIB_IMAGE_DIRECTORY_ENTRY_IAT, // OK
- PELIB_IMAGE_DIRECTORY_ENTRY_DELAY_IMPORT,
- PELIB_IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR
- };
-
- enum
- {
- PELIB_IMAGE_SCN_TYPE_NO_PAD = 0x00000008,
- PELIB_IMAGE_SCN_CNT_CODE = 0x00000020,
- PELIB_IMAGE_SCN_CNT_INITIALIZED_DATA = 0x00000040,
- PELIB_IMAGE_SCN_CNT_UNINITIALIZED_DATA = 0x00000080,
- PELIB_IMAGE_SCN_LNK_OTHER = 0x00000100,
- PELIB_IMAGE_SCN_LNK_INFO = 0x00000200,
- PELIB_IMAGE_SCN_LNK_REMOVE = 0x00000800,
- PELIB_IMAGE_SCN_LNK_COMDAT = 0x00001000,
- PELIB_IMAGE_SCN_NO_DEFER_SPEC_EXC = 0x00004000,
- PELIB_IMAGE_SCN_GPREL = 0x00008000,
- PELIB_IMAGE_SCN_MEM_FARDATA = 0x00008000,
- PELIB_IMAGE_SCN_MEM_PURGEABLE = 0x00020000,
- PELIB_IMAGE_SCN_MEM_16BIT = 0x00020000,
- PELIB_IMAGE_SCN_MEM_LOCKED = 0x00040000,
- PELIB_IMAGE_SCN_MEM_PRELOAD = 0x00080000,
- PELIB_IMAGE_SCN_ALIGN_1BYTES = 0x00100000,
- PELIB_IMAGE_SCN_ALIGN_2BYTES = 0x00200000,
- PELIB_IMAGE_SCN_ALIGN_4BYTES = 0x00300000,
- PELIB_IMAGE_SCN_ALIGN_8BYTES = 0x00400000,
- PELIB_IMAGE_SCN_ALIGN_16BYTES = 0x00500000,
- PELIB_IMAGE_SCN_ALIGN_BYTES = 0x00600000,
- PELIB_IMAGE_SCN_ALIGN_64BYTES = 0x00700000,
- PELIB_IMAGE_SCN_ALIGN_128BYTES = 0x00800000,
- PELIB_IMAGE_SCN_ALIGN_256BYTES = 0x00900000,
- PELIB_IMAGE_SCN_ALIGN_512BYTES = 0x00A00000,
- PELIB_IMAGE_SCN_ALIGN_1024BYTES = 0x00B00000,
- PELIB_IMAGE_SCN_ALIGN_2048BYTES = 0x00C00000,
- PELIB_IMAGE_SCN_ALIGN_4096BYTES = 0x00D00000,
- PELIB_IMAGE_SCN_ALIGN_8192BYTES = 0x00E00000,
- PELIB_IMAGE_SCN_LNK_NRELOC_OVFL = 0x01000000,
- PELIB_IMAGE_SCN_MEM_DISCARDABLE = 0x02000000,
- PELIB_IMAGE_SCN_MEM_NOT_CACHED = 0x04000000,
- PELIB_IMAGE_SCN_MEM_NOT_PAGED = 0x08000000,
- PELIB_IMAGE_SCN_MEM_SHARED = 0x10000000,
- PELIB_IMAGE_SCN_MEM_EXECUTE = 0x20000000,
- PELIB_IMAGE_SCN_MEM_READ = 0x40000000,
- PELIB_IMAGE_SCN_MEM_WRITE = 0x80000000
- };
-
- enum
- {
- PELIB_IMAGE_FILE_MACHINE_UNKNOWN = 0,
- PELIB_IMAGE_FILE_MACHINE_I386 = 0x014c,
- PELIB_IMAGE_FILE_MACHINE_R3000 = 0x0162,
- PELIB_IMAGE_FILE_MACHINE_R4000 = 0x0166,
- PELIB_IMAGE_FILE_MACHINE_R10000 = 0x0168,
- PELIB_IMAGE_FILE_MACHINE_WCEMIPSV2 = 0x0169,
- PELIB_IMAGE_FILE_MACHINE_ALPHA = 0x0184,
- PELIB_IMAGE_FILE_MACHINE_POWERPC = 0x01F0,
- PELIB_IMAGE_FILE_MACHINE_SH3 = 0x01a2,
- PELIB_IMAGE_FILE_MACHINE_SH3E = 0x01a4,
- PELIB_IMAGE_FILE_MACHINE_SH4 = 0x01a6,
- PELIB_IMAGE_FILE_MACHINE_ARM = 0x01c0,
- PELIB_IMAGE_FILE_MACHINE_THUMB = 0x01c2,
- PELIB_IMAGE_FILE_MACHINE_IA64 = 0x0200,
- PELIB_IMAGE_FILE_MACHINE_MIPS16 = 0x0266,
- PELIB_IMAGE_FILE_MACHINE_MIPSFPU = 0x0366,
- PELIB_IMAGE_FILE_MACHINE_MIPSFPU16 = 0x0466,
- PELIB_IMAGE_FILE_MACHINE_ALPHA64 = 0x0284,
- PELIB_IMAGE_FILE_MACHINE_AXP64 = PELIB_IMAGE_FILE_MACHINE_ALPHA64
- };
-
- enum
- {
- PELIB_IMAGE_FILE_RELOCS_STRIPPED = 0x0001,
- PELIB_IMAGE_FILE_EXECUTABLE_IMAGE = 0x0002,
- PELIB_IMAGE_FILE_LINE_NUMS_STRIPPED = 0x0004,
- PELIB_IMAGE_FILE_LOCAL_SYMS_STRIPPED = 0x0008,
- PELIB_IMAGE_FILE_AGGRESIVE_WS_TRIM = 0x0010,
- PELIB_IMAGE_FILE_LARGE_ADDRESS_AWARE = 0x0020,
- PELIB_IMAGE_FILE_BYTES_REVERSED_LO = 0x0080,
- PELIB_IMAGE_FILE_32BIT_MACHINE = 0x0100,
- PELIB_IMAGE_FILE_DEBUG_STRIPPED = 0x0200,
- PELIB_IMAGE_FILE_REMOVABLE_RUN_FROM_SWAP = 0x0400,
- PELIB_IMAGE_FILE_NET_RUN_FROM_SWAP = 0x0800,
- PELIB_IMAGE_FILE_SYSTEM = 0x1000,
- PELIB_IMAGE_FILE_DLL = 0x2000,
- PELIB_IMAGE_FILE_UP_SYSTEM_ONLY = 0x4000,
- PELIB_IMAGE_FILE_BYTES_REVERSED_HI = 0x8000
- };
-
- enum
- {
- PELIB_IMAGE_NT_OPTIONAL_HDR32_MAGIC = 0x10b,
- PELIB_IMAGE_NT_OPTIONAL_HDR64_MAGIC = 0x20b,
- PELIB_IMAGE_ROM_OPTIONAL_HDR_MAGIC = 0x107
- };
-
- enum
- {
- PELIB_IMAGE_SUBSYSTEM_UNKNOWN = 0,
- PELIB_IMAGE_SUBSYSTEM_NATIVE = 1,
- PELIB_IMAGE_SUBSYSTEM_WINDOWS_GUI = 2,
- PELIB_IMAGE_SUBSYSTEM_WINDOWS_CUI = 3,
- PELIB_IMAGE_SUBSYSTEM_OS2_CUI = 5,
- PELIB_IMAGE_SUBSYSTEM_POSIX_CUI = 7,
- PELIB_IMAGE_SUBSYSTEM_NATIVE_WINDOWS = 8,
- PELIB_IMAGE_SUBSYSTEM_WINDOWS_CE_GUI = 9
- };
-
- enum
- {
- PELIB_RT_CURSOR = 1, // 1
- PELIB_RT_BITMAP, // 2
- PELIB_RT_ICON, // 3
- PELIB_RT_MENU, // 4
- PELIB_RT_DIALOG, // 5
- PELIB_RT_STRING, // 6
- PELIB_RT_FONTDIR, // 7
- PELIB_RT_FONT, // 8
- PELIB_RT_ACCELERATOR, // 9
- PELIB_RT_RCDATA, // 10
- PELIB_RT_MESSAGETABLE, // 11
- PELIB_RT_GROUP_CURSOR, // 12
- PELIB_RT_GROUP_ICON = 14, // 14
- PELIB_RT_VERSION = 16,
- PELIB_RT_DLGINCLUDE,
- PELIB_RT_PLUGPLAY = 19,
- PELIB_RT_VXD,
- PELIB_RT_ANICURSOR,
- PELIB_RT_ANIICON,
- PELIB_RT_HTML,
- PELIB_RT_MANIFEST
- };
-
- template<typename T>
- unsigned int accumulate(unsigned int size, const T& v)
- {
- return size + v.size();
- }
-
-
- struct PELIB_IMAGE_DOS_HEADER
- {
- word e_magic;
- word e_cblp;
- word e_cp;
- word e_crlc;
- word e_cparhdr;
- word e_minalloc;
- word e_maxalloc;
- word e_ss;
- word e_sp;
- word e_csum;
- word e_ip;
- word e_cs;
- word e_lfarlc;
- word e_ovno;
- word e_res[4];
- word e_oemid;
- word e_oeminfo;
- word e_res2[10];
- dword e_lfanew;
-
- PELIB_IMAGE_DOS_HEADER();
-
- static inline unsigned int size() {return 64;}
- };
-
- struct PELIB_IMAGE_FILE_HEADER
- {
- word Machine;
- word NumberOfSections;
- dword TimeDateStamp;
- dword PointerToSymbolTable;
- dword NumberOfSymbols;
- word SizeOfOptionalHeader;
- word Characteristics;
-
- PELIB_IMAGE_FILE_HEADER()
- {
- Machine = 0;
- NumberOfSections = 0;
- TimeDateStamp = 0;
- PointerToSymbolTable = 0;
- NumberOfSymbols = 0;
- SizeOfOptionalHeader = 0;
- Characteristics = 0;
- }
-
- static inline unsigned int size() {return 20;}
- };
-
- struct PELIB_IMAGE_DATA_DIRECTORY
- {
- dword VirtualAddress;
- dword Size;
-
- PELIB_IMAGE_DATA_DIRECTORY()
- {
- VirtualAddress = 0;
- Size = 0;
- }
-
- static inline unsigned int size() {return 8;}
- };
-
- template<int>
- struct FieldSizes;
-
- template<>
- struct FieldSizes<32>
- {
- typedef dword VAR4_8;
- };
-
- template<>
- struct FieldSizes<64>
- {
- typedef qword VAR4_8;
- };
-
- template<int x>
- struct PELIB_IMAGE_OPTIONAL_HEADER_BASE
- {
- typedef typename FieldSizes<x>::VAR4_8 VAR4_8;
-
- word Magic;
- byte MajorLinkerVersion;
- byte MinorLinkerVersion;
- dword SizeOfCode;
- dword SizeOfInitializedData;
- dword SizeOfUninitializedData;
- dword AddressOfEntryPoint;
- dword BaseOfCode;
- dword BaseOfData;
- VAR4_8 ImageBase;
- dword SectionAlignment;
- dword FileAlignment;
- word MajorOperatingSystemVersion;
- word MinorOperatingSystemVersion;
- word MajorImageVersion;
- word MinorImageVersion;
- word MajorSubsystemVersion;
- word MinorSubsystemVersion;
- dword Win32VersionValue;
- dword SizeOfImage;
- dword SizeOfHeaders;
- dword CheckSum;
- word Subsystem;
- word DllCharacteristics;
- VAR4_8 SizeOfStackReserve;
- VAR4_8 SizeOfStackCommit;
- VAR4_8 SizeOfHeapReserve;
- VAR4_8 SizeOfHeapCommit;
- dword LoaderFlags;
- dword NumberOfRvaAndSizes;
-// PELIB_IMAGE_DATA_DIRECTORY DataDirectory[IMAGE_NUMBEROF_DIRECTORY_ENTRIES];
-
- PELIB_IMAGE_OPTIONAL_HEADER_BASE();
- };
-
- template<int x>
- PELIB_IMAGE_OPTIONAL_HEADER_BASE<x>::PELIB_IMAGE_OPTIONAL_HEADER_BASE()
- {
- Magic = 0;
- MajorLinkerVersion = 0;
- MinorLinkerVersion = 0;
- SizeOfCode = 0;
- SizeOfInitializedData = 0;
- SizeOfUninitializedData = 0;
- AddressOfEntryPoint = 0;
- BaseOfCode = 0;
-// BaseOfData = 0;
- ImageBase = 0;
- SectionAlignment = 0;
- FileAlignment = 0;
- MajorOperatingSystemVersion = 0;
- MinorOperatingSystemVersion = 0;
- MajorImageVersion = 0;
- MinorImageVersion = 0;
- MajorSubsystemVersion = 0;
- MinorSubsystemVersion = 0;
- Win32VersionValue = 0;
- SizeOfImage = 0;
- SizeOfHeaders = 0;
- CheckSum = 0;
- Subsystem = 0;
- DllCharacteristics = 0;
- SizeOfStackReserve = 0;
- SizeOfStackCommit = 0;
- SizeOfHeapReserve = 0;
- SizeOfHeapCommit = 0;
- LoaderFlags = 0;
- NumberOfRvaAndSizes = 0;
- }
-
- template<int>
- struct PELIB_IMAGE_OPTIONAL_HEADER;
-
- template<>
- struct PELIB_IMAGE_OPTIONAL_HEADER<32> : public PELIB_IMAGE_OPTIONAL_HEADER_BASE<32>
- {
- dword BaseOfData;
-
- static inline unsigned int size() {return 224 - 0x10 * 8;}
- };
-
- template<>
- struct PELIB_IMAGE_OPTIONAL_HEADER<64> : public PELIB_IMAGE_OPTIONAL_HEADER_BASE<64>
- {
- static inline unsigned int size() {return 240 - 0x10 * 8;}
- };
-
- template<int x>
- struct PELIB_IMAGE_NT_HEADERS
- {
- dword Signature;
- PELIB_IMAGE_FILE_HEADER FileHeader;
- PELIB_IMAGE_OPTIONAL_HEADER<x> OptionalHeader;
- std::vector<PELIB_IMAGE_DATA_DIRECTORY> dataDirectories;
-
- unsigned int size() const
- {
- return sizeof(dword)
- + PELIB_IMAGE_FILE_HEADER::size()
- + PELIB_IMAGE_OPTIONAL_HEADER<x>::size()
- + static_cast<unsigned int>(dataDirectories.size()) * PELIB_IMAGE_DATA_DIRECTORY::size();
- }
-
- PELIB_IMAGE_NT_HEADERS()
- {
- Signature = 0;
- }
- };
-
- const unsigned int PELIB_IMAGE_SIZEOF_SHORT_NAME = 8;
-
- struct PELIB_IMAGE_SECTION_HEADER
- {
- byte Name[PELIB_IMAGE_SIZEOF_SHORT_NAME];
- dword VirtualSize;
- dword VirtualAddress;
- dword SizeOfRawData;
- dword PointerToRawData;
- dword PointerToRelocations;
- dword PointerToLinenumbers;
- word NumberOfRelocations;
- word NumberOfLinenumbers;
- dword Characteristics;
-
- PELIB_IMAGE_SECTION_HEADER()
- {
- VirtualSize = 0;
- VirtualAddress = 0;
- SizeOfRawData = 0;
- PointerToRawData = 0;
- PointerToRelocations = 0;
- PointerToLinenumbers = 0;
- NumberOfRelocations = 0;
- NumberOfLinenumbers = 0;
- Characteristics = 0;
- }
-
- static inline unsigned int size() {return 40;}
- bool biggerFileOffset(const PELIB_IMAGE_SECTION_HEADER& ish) const;
- bool biggerVirtualAddress(const PELIB_IMAGE_SECTION_HEADER& ish) const;
- };
-
- template<int bits>
- struct PELIB_IMAGE_THUNK_DATA
- {
- typename FieldSizes<bits>::VAR4_8 Ordinal;
-
- PELIB_IMAGE_THUNK_DATA()
- {
- Ordinal = 0;
- }
-
- static inline unsigned int size() {return 4;}
- };
-
- struct PELIB_IMAGE_IMPORT_DESCRIPTOR
- {
- dword OriginalFirstThunk;
- dword TimeDateStamp;
- dword ForwarderChain;
- dword Name;
- dword FirstThunk;
-
- PELIB_IMAGE_IMPORT_DESCRIPTOR()
- {
- OriginalFirstThunk = 0;
- TimeDateStamp = 0;
- ForwarderChain = 0;
- Name = 0;
- FirstThunk = 0;
- }
-
- static inline unsigned int size() {return 20;}
- };
-
- struct PELIB_IMAGE_EXPORT_DIRECTORY
- {
- dword Characteristics;
- dword TimeDateStamp;
- word MajorVersion;
- word MinorVersion;
- dword Name;
- dword Base;
- dword NumberOfFunctions;
- dword NumberOfNames;
- dword AddressOfFunctions;
- dword AddressOfNames;
- dword AddressOfNameOrdinals;
-
- PELIB_IMAGE_EXPORT_DIRECTORY()
- {
- Characteristics = 0;
- TimeDateStamp = 0;
- MajorVersion = 0;
- MinorVersion = 0;
- Name = 0;
- Base = 0;
- NumberOfFunctions = 0;
- NumberOfNames = 0;
- AddressOfFunctions = 0;
- NumberOfNames = 0;
- AddressOfNameOrdinals = 0;
- }
-
- static inline unsigned int size() {return 40;}
- };
-
- struct PELIB_IMAGE_BOUND_IMPORT_DESCRIPTOR
- {
- dword TimeDateStamp;
- word OffsetModuleName;
- word NumberOfModuleForwarderRefs;
-
- PELIB_IMAGE_BOUND_IMPORT_DESCRIPTOR()
- {
- TimeDateStamp = 0;
- OffsetModuleName = 0;
- NumberOfModuleForwarderRefs = 0;
- }
-
- static unsigned int size()
- {
- return 8;
- }
- };
-
- // Stores all necessary information about a BoundImport field.
- struct PELIB_IMAGE_BOUND_DIRECTORY
- {
- PELIB_IMAGE_BOUND_IMPORT_DESCRIPTOR ibdDescriptor; ///< Information about the imported file.
- std::string strModuleName; ///< Name of the imported file.
- std::vector<PELIB_IMAGE_BOUND_DIRECTORY> moduleForwarders;
-
- // Will be used in std::find_if
- // Passing by-reference not possible (see C++ Standard Core Language Defect Reports, Revision 29, Issue 106)
- /// Compares the passed filename to the struct's filename.
- bool equal(const std::string strModuleName) const;
-
- unsigned int size() const;
- };
-
- struct PELIB_EXP_FUNC_INFORMATION
- {
- dword addroffunc;
- dword addrofname;
- word ordinal;
- std::string funcname;
-
- PELIB_EXP_FUNC_INFORMATION();
-
- bool equal(const std::string strFunctionName) const;
- inline unsigned int size() const
- {
- unsigned int uiSize = 4;
- if (addroffunc) uiSize += 2;// + 4;
- if (!funcname.empty()) uiSize += 4 + (unsigned int)funcname.size() + 1;
- return uiSize;
- }
- };
-
- struct PELIB_IMAGE_RESOURCE_DIRECTORY
- {
- dword Characteristics;
- dword TimeDateStamp;
- word MajorVersion;
- word MinorVersion;
- word NumberOfNamedEntries;
- word NumberOfIdEntries;
-
- PELIB_IMAGE_RESOURCE_DIRECTORY();
-
- static inline unsigned int size() {return 16;}
- };
-
- struct PELIB_IMAGE_RESOURCE_DIRECTORY_ENTRY
- {
- dword Name;
- dword OffsetToData;
- PELIB_IMAGE_RESOURCE_DIRECTORY_ENTRY();
- static inline unsigned int size() {return 8;}
- };
-
- const unsigned int PELIB_IMAGE_SIZEOF_BASE_RELOCATION = 8;
-
- struct PELIB_IMG_RES_DIR_ENTRY
- {
- PELIB_IMAGE_RESOURCE_DIRECTORY_ENTRY irde;
- std::string wstrName;
-
- bool operator<(const PELIB_IMG_RES_DIR_ENTRY& first) const;
-
- };
-
- struct PELIB_IMAGE_BASE_RELOCATION
- {
- dword VirtualAddress;
- dword SizeOfBlock;
-
- PELIB_IMAGE_BASE_RELOCATION();
- static inline unsigned int size() {return 8;}
- };
-
- struct PELIB_IMAGE_COR20_HEADER
- {
- dword cb;
- word MajorRuntimeVersion;
- word MinorRuntimeVersion;
- PELIB_IMAGE_DATA_DIRECTORY MetaData;
- dword Flags;
- dword EntryPointToken;
- PELIB_IMAGE_DATA_DIRECTORY Resources;
- PELIB_IMAGE_DATA_DIRECTORY StrongNameSignature;
- PELIB_IMAGE_DATA_DIRECTORY CodeManagerTable;
- PELIB_IMAGE_DATA_DIRECTORY VTableFixups;
- PELIB_IMAGE_DATA_DIRECTORY ExportAddressTableJumps;
- PELIB_IMAGE_DATA_DIRECTORY ManagedNativeHeader;
-
- PELIB_IMAGE_COR20_HEADER();
- static inline unsigned int size() {return 72;}
- };
-
- // Used to store a file's export table.
- struct PELIB_IMAGE_EXP_DIRECTORY
- {
- /// The IMAGE_EXPORTED_DIRECTORY of a file's export table.
- PELIB_IMAGE_EXPORT_DIRECTORY ied;
- /// The original filename of current file.
- std::string name;
- std::vector<PELIB_EXP_FUNC_INFORMATION> functions;
- inline unsigned int size() const
- {
- return PELIB_IMAGE_EXPORT_DIRECTORY::size() + name.size() + 1 +
- std::accumulate(functions.begin(), functions.end(), 0, accumulate<PELIB_EXP_FUNC_INFORMATION>);
- }
- };
-
- // Used for parsing a file's import table. It combines the function name, the hint
- // and the IMAGE_THUNK_DATA of an imported function.
- template<int bits>
- struct PELIB_THUNK_DATA
- {
- /// The IMAGE_THUNK_DATA struct of an imported function.
- PELIB_IMAGE_THUNK_DATA<bits> itd;
- /// The hint of an imported function.
- word hint;
- /// The function name of an imported function.
- std::string fname;
-
- bool equalHint(word wHint) const
- {
- return hint == wHint;
-// return itd.Ordinal == (wHint | IMAGE_ORDINAL_FLAGS<bits>::IMAGE_ORDINAL_FLAG);
- }
-
- bool equalFunctionName(std::string strFunctionName) const
- {
- return isEqualNc(fname, strFunctionName);
- }
-
- unsigned int size() const {return PELIB_IMAGE_THUNK_DATA<bits>::size() + fname.size() + 1 + sizeof(hint);}
- };
-
- // Used to store a file's import table. Every struct of this sort
- // can store import information of one DLL.
- template<int bits>
- struct PELIB_IMAGE_IMPORT_DIRECTORY
- {
- /// The IMAGE_IMPORT_DESCRIPTOR of an imported DLL.
- PELIB_IMAGE_IMPORT_DESCRIPTOR impdesc;
- /// The name of an imported DLL.
- std::string name;
- /// All original first thunk values of an imported DLL.
- std::vector<PELIB_THUNK_DATA<bits> > originalfirstthunk;
- /// All first thunk value of an imported DLL.
- std::vector<PELIB_THUNK_DATA<bits> > firstthunk;
-
-// bool operator==(std::string strFilename) const;
- inline unsigned int size() const
- {
- return PELIB_IMAGE_IMPORT_DESCRIPTOR::size() + name.size() + 1 + // descriptor + dllname
- std::accumulate(originalfirstthunk.begin(), originalfirstthunk.end(), 0, accumulate<PELIB_THUNK_DATA<bits> >) + // thunks (PeLib uses only one thunk)
- PELIB_IMAGE_THUNK_DATA<bits>::size(); // zero-termination
- }
-
- bool operator==(std::string strFilename) const
- {
- return isEqualNc(this->name, strFilename);
- }
- };
-
- struct PELIB_IMAGE_RESOURCE_DATA_ENTRY
- {
- dword OffsetToData;
- dword Size;
- dword CodePage;
- dword Reserved;
-
- static inline unsigned int size() {return 16;}
- };
-
- struct PELIB_IMAGE_RESOURCE_DATA
- {
- PELIB_IMAGE_RESOURCE_DATA_ENTRY irdEntry;
- std::vector<byte> vData;
- };
-
- struct IMG_BASE_RELOC
- {
- PELIB_IMAGE_BASE_RELOCATION ibrRelocation;
- std::vector<word> vRelocData;
- };
-
- struct PELIB_IMAGE_DEBUG_DIRECTORY
- {
- dword Characteristics;
- dword TimeDateStamp;
- word MajorVersion;
- word MinorVersion;
- dword Type;
- dword SizeOfData;
- dword AddressOfRawData;
- dword PointerToRawData;
-
- static unsigned int size() {return 28;}
- };
-
- struct PELIB_IMG_DEBUG_DIRECTORY
- {
- PELIB_IMAGE_DEBUG_DIRECTORY idd;
- std::vector<byte> data;
- };
-
- template<int bits>
- struct PELIB_IMAGE_TLS_DIRECTORY_BASE
- {
- typename FieldSizes<bits>::VAR4_8 StartAddressOfRawData;
- typename FieldSizes<bits>::VAR4_8 EndAddressOfRawData;
- typename FieldSizes<bits>::VAR4_8 AddressOfIndex;
- typename FieldSizes<bits>::VAR4_8 AddressOfCallBacks;
- dword SizeOfZeroFill;
- dword Characteristics;
- };
-
- template<int bits>
- struct PELIB_IMAGE_TLS_DIRECTORY;// : public PELIB_IMAGE_TLS_DIRECTORY_BASE<bits>
-
- template<>
- struct PELIB_IMAGE_TLS_DIRECTORY<32> : public PELIB_IMAGE_TLS_DIRECTORY_BASE<32>
- {
-// enum {size = 24};
- static unsigned int size(){return 24;}
- };
-
- template<>
- struct PELIB_IMAGE_TLS_DIRECTORY<64> : public PELIB_IMAGE_TLS_DIRECTORY_BASE<64>
- {
-// enum {size = 40};
- static unsigned int size(){return 40;}
- };
-
- unsigned int fileSize(const std::string& filename);
- unsigned int fileSize(std::ifstream& file);
- unsigned int fileSize(std::ofstream& file);
- unsigned int fileSize(std::fstream& file);
- unsigned int alignOffset(unsigned int uiOffset, unsigned int uiAlignment);
-
- /// Determines if a file is a 32bit or 64bit PE file.
- unsigned int getFileType(const std::string strFilename);
-
- /// Opens a PE file.
- PeFile* openPeFile(const std::string& strFilename);
-
- /* enum MzHeader_Field {e_magic, e_cblp, e_cp, e_crlc, e_cparhdr, e_minalloc, e_maxalloc,
- e_ss, e_sp, e_csum, e_ip, e_cs, e_lfarlc, e_ovno, e_res, e_oemid,
- e_oeminfo, e_res2, e_lfanew};
- enum PeHeader_Field {NtSignature, Machine, NumberOfSections, TimeDateStamp, PointerToSymbolTable,
- NumberOfSymbols, SizeOfOptionalHeader, Characteristics, Magic,
- MajorLinkerVersion, MinorLinkerVersion, SizeOfCode, SizeOfInitializedData,
- SizeOfUninitializedData, AddressOfEntryPoint, BaseOfCode, BaseOfData, ImageBase,
- SectionAlignment, FileAlignment, MajorOperatingSystemVersion, MinorOperatingSystemVersion,
- MajorImageVersion, MinorImageVersion, MajorSubsystemVersion, MinorSubsystemVersion,
- Win32VersionValue, SizeOfImage, SizeOfHeaders, CheckSum, Subsystem, DllCharacteristics,
- SizeOfStackReserve, SizeOfStackCommit, SizeOfHeapReserve, SizeOfHeapCommit,
- LoaderFlags, NumberOfRvaAndSizes, DataDirectoryRva, DataDirectorySize};
- enum Section_Field {SectionName, VirtualSize, VirtualAddress, SizeOfRawData, PointerToRawData, PointerToRelocations,
- PointerToLinenumbers, NumberOfRelocations, NumberOfLinenumbers, SectionCharacteristics};
-*/
-}
-
-#endif
+/*
+* PeLibAux.cpp - Part of the PeLib library.
+*
+* Copyright (c) 2004 - 2005 Sebastian Porst (webmaster@the-interweb.com)
+* All rights reserved.
+*
+* This software is licensed under the zlib/libpng License.
+* For more details see http://www.opensource.org/licenses/zlib-license.php
+* or the license information file (license.htm) in the root directory
+* of PeLib.
+*/
+
+#ifndef PELIBAUX_H
+#define PELIBAUX_H
+
+//#include "PeLibInc.h"
+//#include "PeHeader.h"
+#include "buffer/OutputBuffer.h"
+#include "buffer/InputBuffer.h"
+//#include "buffer/ResTree.h"
+#include <numeric>
+#include <limits>
+
+namespace PeLib
+{
+ enum errorCodes
+ {
+ NO_ERROR = 0,
+ ERROR_OPENING_FILE = -1,
+ ERROR_INVALID_FILE = -2,
+ ERROR_TOO_MANY_SECTIONS = -3,
+ ERROR_NOT_ENOUGH_SPACE = -4,
+ ERROR_NO_FILE_ALIGNMENT = -5,
+ ERROR_NO_SECTION_ALIGNMENT = -6,
+ ERROR_ENTRY_NOT_FOUND = -7,
+ ERROR_DUPLICATE_ENTRY = -8,
+ ERROR_DIRECTORY_DOES_NOT_EXIST = -9
+ };
+
+ class PeFile;
+ bool isEqualNc(const std::string& s1, const std::string& s2);
+
+// It's necessary to make sure that a byte has 8 bits and that the platform has a 8 bit type,
+// a 16bit type and a bit type. That's because binary PE files are pretty picky about their
+// structure.
+
+ #if CHAR_BIT == 8
+ #if UCHAR_MAX == 255
+ typedef unsigned char byte;
+ // typedef std::bitset<8> byte;
+ #else
+ #error You need to change some typedefs (Code: 8). Please read the PeLib documentation.
+ #endif
+
+ #if USHRT_MAX == 65535U
+ typedef unsigned short word;
+ // typedef std::bitset<16> word;
+ #else
+ #error You need to change some typedefs (Code: 16). Please read the PeLib documentation.
+ #endif
+
+ #if UINT_MAX == 4294967295UL
+ typedef unsigned int dword;
+ // typedef std::bitset<32> dword;
+ #else
+ #error You need to change some typedefs (Code: 32). Please read the PeLib documentation.
+ #endif
+
+ typedef unsigned long long qword;
+
+// #if ULLONG_MAX == 18446744073709551615
+// typedef unsigned long long qword;
+// #else
+// #error You need to change some typedefs (Code: 32). Please read the PeLib documentation.
+// #endif
+ #else
+ #error You need to change some typedefs. Please read the PeLib documentation.
+ #endif
+
+
+/* enum bits {BITS_BYTE = 8, BITS_WORD = 16, BITS_DWORD = 32};
+
+ template<bits value>
+ class DataType
+ {
+ private:
+ std::bitset<value> bsValue;
+ unsigned long ulValue;
+
+ public:
+ void operator=(unsigned long ulValue)
+ {
+ bsValue = ulValue;
+ }
+
+ operator unsigned long() const
+ {
+ return bsValue.to_ulong();
+ }
+
+ const int operator&()
+ {
+ ulValue = bsValue;
+ return ulValue;
+ }
+
+ };
+
+ typedef DataType<BITS_BYTE> byte;
+ typedef DataType<BITS_WORD> word;
+ typedef DataType<BITS_DWORD> dword;
+*/
+
+ enum {PEFILE32 = 32,
+ PEFILE64 = 64,
+ PEFILE_UNKNOWN = 0};
+
+ enum {BoundImportDirectoryId = 1,
+ ComHeaderDirectoryId,
+ ExportDirectoryId,
+ IatDirectoryId,
+ ImportDirectoryId,
+ MzHeaderId,
+ PeHeaderId,
+ RelocationsId,
+ PeFileId,
+ ResourceDirectoryId,
+ DebugDirectoryId,
+ TlsDirectoryId
+ };
+
+ const word PELIB_IMAGE_DOS_SIGNATURE = 0x5A4D;
+
+ const dword PELIB_IMAGE_NT_SIGNATURE = 0x00004550;
+
+ template<int bits>
+ struct PELIB_IMAGE_ORDINAL_FLAGS;
+
+ template<>
+ struct PELIB_IMAGE_ORDINAL_FLAGS<32>
+ {
+ static const dword IMAGE_ORDINAL_FLAG = 0x80000000;
+ };
+
+ template<>
+ struct PELIB_IMAGE_ORDINAL_FLAGS<64>
+ {
+ static const qword IMAGE_ORDINAL_FLAG;
+ };
+
+ const unsigned long PELIB_IMAGE_NUMBEROF_DIRECTORY_ENTRIES = 16;
+
+ const unsigned long PELIB_IMAGE_RESOURCE_NAME_IS_STRING = 0x80000000;
+
+ const unsigned long PELIB_IMAGE_RESOURCE_DATA_IS_DIRECTORY = 0x80000000;
+
+ enum
+ {
+ PELIB_IMAGE_DIRECTORY_ENTRY_EXPORT, // OK
+ PELIB_IMAGE_DIRECTORY_ENTRY_IMPORT, // OK
+ PELIB_IMAGE_DIRECTORY_ENTRY_RESOURCE, // OK
+ PELIB_IMAGE_DIRECTORY_ENTRY_EXCEPTION,
+ PELIB_IMAGE_DIRECTORY_ENTRY_SECURITY,
+ PELIB_IMAGE_DIRECTORY_ENTRY_BASERELOC, // OK
+ PELIB_IMAGE_DIRECTORY_ENTRY_DEBUG,
+ PELIB_IMAGE_DIRECTORY_ENTRY_ARCHITECTURE,
+ PELIB_IMAGE_DIRECTORY_ENTRY_GLOBALPTR,
+ PELIB_IMAGE_DIRECTORY_ENTRY_TLS,
+ PELIB_IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG,
+ PELIB_IMAGE_DIRECTORY_ENTRY_BOUND_IMPORT, // OK
+ PELIB_IMAGE_DIRECTORY_ENTRY_IAT, // OK
+ PELIB_IMAGE_DIRECTORY_ENTRY_DELAY_IMPORT,
+ PELIB_IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR
+ };
+
+ enum
+ {
+ PELIB_IMAGE_SCN_TYPE_NO_PAD = 0x00000008,
+ PELIB_IMAGE_SCN_CNT_CODE = 0x00000020,
+ PELIB_IMAGE_SCN_CNT_INITIALIZED_DATA = 0x00000040,
+ PELIB_IMAGE_SCN_CNT_UNINITIALIZED_DATA = 0x00000080,
+ PELIB_IMAGE_SCN_LNK_OTHER = 0x00000100,
+ PELIB_IMAGE_SCN_LNK_INFO = 0x00000200,
+ PELIB_IMAGE_SCN_LNK_REMOVE = 0x00000800,
+ PELIB_IMAGE_SCN_LNK_COMDAT = 0x00001000,
+ PELIB_IMAGE_SCN_NO_DEFER_SPEC_EXC = 0x00004000,
+ PELIB_IMAGE_SCN_GPREL = 0x00008000,
+ PELIB_IMAGE_SCN_MEM_FARDATA = 0x00008000,
+ PELIB_IMAGE_SCN_MEM_PURGEABLE = 0x00020000,
+ PELIB_IMAGE_SCN_MEM_16BIT = 0x00020000,
+ PELIB_IMAGE_SCN_MEM_LOCKED = 0x00040000,
+ PELIB_IMAGE_SCN_MEM_PRELOAD = 0x00080000,
+ PELIB_IMAGE_SCN_ALIGN_1BYTES = 0x00100000,
+ PELIB_IMAGE_SCN_ALIGN_2BYTES = 0x00200000,
+ PELIB_IMAGE_SCN_ALIGN_4BYTES = 0x00300000,
+ PELIB_IMAGE_SCN_ALIGN_8BYTES = 0x00400000,
+ PELIB_IMAGE_SCN_ALIGN_16BYTES = 0x00500000,
+ PELIB_IMAGE_SCN_ALIGN_BYTES = 0x00600000,
+ PELIB_IMAGE_SCN_ALIGN_64BYTES = 0x00700000,
+ PELIB_IMAGE_SCN_ALIGN_128BYTES = 0x00800000,
+ PELIB_IMAGE_SCN_ALIGN_256BYTES = 0x00900000,
+ PELIB_IMAGE_SCN_ALIGN_512BYTES = 0x00A00000,
+ PELIB_IMAGE_SCN_ALIGN_1024BYTES = 0x00B00000,
+ PELIB_IMAGE_SCN_ALIGN_2048BYTES = 0x00C00000,
+ PELIB_IMAGE_SCN_ALIGN_4096BYTES = 0x00D00000,
+ PELIB_IMAGE_SCN_ALIGN_8192BYTES = 0x00E00000,
+ PELIB_IMAGE_SCN_LNK_NRELOC_OVFL = 0x01000000,
+ PELIB_IMAGE_SCN_MEM_DISCARDABLE = 0x02000000,
+ PELIB_IMAGE_SCN_MEM_NOT_CACHED = 0x04000000,
+ PELIB_IMAGE_SCN_MEM_NOT_PAGED = 0x08000000,
+ PELIB_IMAGE_SCN_MEM_SHARED = 0x10000000,
+ PELIB_IMAGE_SCN_MEM_EXECUTE = 0x20000000,
+ PELIB_IMAGE_SCN_MEM_READ = 0x40000000,
+ PELIB_IMAGE_SCN_MEM_WRITE = 0x80000000
+ };
+
+ enum
+ {
+ PELIB_IMAGE_FILE_MACHINE_UNKNOWN = 0,
+ PELIB_IMAGE_FILE_MACHINE_I386 = 0x014c,
+ PELIB_IMAGE_FILE_MACHINE_R3000 = 0x0162,
+ PELIB_IMAGE_FILE_MACHINE_R4000 = 0x0166,
+ PELIB_IMAGE_FILE_MACHINE_R10000 = 0x0168,
+ PELIB_IMAGE_FILE_MACHINE_WCEMIPSV2 = 0x0169,
+ PELIB_IMAGE_FILE_MACHINE_ALPHA = 0x0184,
+ PELIB_IMAGE_FILE_MACHINE_POWERPC = 0x01F0,
+ PELIB_IMAGE_FILE_MACHINE_SH3 = 0x01a2,
+ PELIB_IMAGE_FILE_MACHINE_SH3E = 0x01a4,
+ PELIB_IMAGE_FILE_MACHINE_SH4 = 0x01a6,
+ PELIB_IMAGE_FILE_MACHINE_ARM = 0x01c0,
+ PELIB_IMAGE_FILE_MACHINE_THUMB = 0x01c2,
+ PELIB_IMAGE_FILE_MACHINE_IA64 = 0x0200,
+ PELIB_IMAGE_FILE_MACHINE_MIPS16 = 0x0266,
+ PELIB_IMAGE_FILE_MACHINE_MIPSFPU = 0x0366,
+ PELIB_IMAGE_FILE_MACHINE_MIPSFPU16 = 0x0466,
+ PELIB_IMAGE_FILE_MACHINE_ALPHA64 = 0x0284,
+ PELIB_IMAGE_FILE_MACHINE_AXP64 = PELIB_IMAGE_FILE_MACHINE_ALPHA64
+ };
+
+ enum
+ {
+ PELIB_IMAGE_FILE_RELOCS_STRIPPED = 0x0001,
+ PELIB_IMAGE_FILE_EXECUTABLE_IMAGE = 0x0002,
+ PELIB_IMAGE_FILE_LINE_NUMS_STRIPPED = 0x0004,
+ PELIB_IMAGE_FILE_LOCAL_SYMS_STRIPPED = 0x0008,
+ PELIB_IMAGE_FILE_AGGRESIVE_WS_TRIM = 0x0010,
+ PELIB_IMAGE_FILE_LARGE_ADDRESS_AWARE = 0x0020,
+ PELIB_IMAGE_FILE_BYTES_REVERSED_LO = 0x0080,
+ PELIB_IMAGE_FILE_32BIT_MACHINE = 0x0100,
+ PELIB_IMAGE_FILE_DEBUG_STRIPPED = 0x0200,
+ PELIB_IMAGE_FILE_REMOVABLE_RUN_FROM_SWAP = 0x0400,
+ PELIB_IMAGE_FILE_NET_RUN_FROM_SWAP = 0x0800,
+ PELIB_IMAGE_FILE_SYSTEM = 0x1000,
+ PELIB_IMAGE_FILE_DLL = 0x2000,
+ PELIB_IMAGE_FILE_UP_SYSTEM_ONLY = 0x4000,
+ PELIB_IMAGE_FILE_BYTES_REVERSED_HI = 0x8000
+ };
+
+ enum
+ {
+ PELIB_IMAGE_NT_OPTIONAL_HDR32_MAGIC = 0x10b,
+ PELIB_IMAGE_NT_OPTIONAL_HDR64_MAGIC = 0x20b,
+ PELIB_IMAGE_ROM_OPTIONAL_HDR_MAGIC = 0x107
+ };
+
+ enum
+ {
+ PELIB_IMAGE_SUBSYSTEM_UNKNOWN = 0,
+ PELIB_IMAGE_SUBSYSTEM_NATIVE = 1,
+ PELIB_IMAGE_SUBSYSTEM_WINDOWS_GUI = 2,
+ PELIB_IMAGE_SUBSYSTEM_WINDOWS_CUI = 3,
+ PELIB_IMAGE_SUBSYSTEM_OS2_CUI = 5,
+ PELIB_IMAGE_SUBSYSTEM_POSIX_CUI = 7,
+ PELIB_IMAGE_SUBSYSTEM_NATIVE_WINDOWS = 8,
+ PELIB_IMAGE_SUBSYSTEM_WINDOWS_CE_GUI = 9
+ };
+
+ enum
+ {
+ PELIB_RT_CURSOR = 1, // 1
+ PELIB_RT_BITMAP, // 2
+ PELIB_RT_ICON, // 3
+ PELIB_RT_MENU, // 4
+ PELIB_RT_DIALOG, // 5
+ PELIB_RT_STRING, // 6
+ PELIB_RT_FONTDIR, // 7
+ PELIB_RT_FONT, // 8
+ PELIB_RT_ACCELERATOR, // 9
+ PELIB_RT_RCDATA, // 10
+ PELIB_RT_MESSAGETABLE, // 11
+ PELIB_RT_GROUP_CURSOR, // 12
+ PELIB_RT_GROUP_ICON = 14, // 14
+ PELIB_RT_VERSION = 16,
+ PELIB_RT_DLGINCLUDE,
+ PELIB_RT_PLUGPLAY = 19,
+ PELIB_RT_VXD,
+ PELIB_RT_ANICURSOR,
+ PELIB_RT_ANIICON,
+ PELIB_RT_HTML,
+ PELIB_RT_MANIFEST
+ };
+
+ template<typename T>
+ unsigned int accumulate(unsigned int size, const T& v)
+ {
+ return size + v.size();
+ }
+
+
+ struct PELIB_IMAGE_DOS_HEADER
+ {
+ word e_magic;
+ word e_cblp;
+ word e_cp;
+ word e_crlc;
+ word e_cparhdr;
+ word e_minalloc;
+ word e_maxalloc;
+ word e_ss;
+ word e_sp;
+ word e_csum;
+ word e_ip;
+ word e_cs;
+ word e_lfarlc;
+ word e_ovno;
+ word e_res[4];
+ word e_oemid;
+ word e_oeminfo;
+ word e_res2[10];
+ dword e_lfanew;
+
+ PELIB_IMAGE_DOS_HEADER();
+
+ static inline unsigned int size() {return 64;}
+ };
+
+ struct PELIB_IMAGE_FILE_HEADER
+ {
+ word Machine;
+ word NumberOfSections;
+ dword TimeDateStamp;
+ dword PointerToSymbolTable;
+ dword NumberOfSymbols;
+ word SizeOfOptionalHeader;
+ word Characteristics;
+
+ PELIB_IMAGE_FILE_HEADER()
+ {
+ Machine = 0;
+ NumberOfSections = 0;
+ TimeDateStamp = 0;
+ PointerToSymbolTable = 0;
+ NumberOfSymbols = 0;
+ SizeOfOptionalHeader = 0;
+ Characteristics = 0;
+ }
+
+ static inline unsigned int size() {return 20;}
+ };
+
+ struct PELIB_IMAGE_DATA_DIRECTORY
+ {
+ dword VirtualAddress;
+ dword Size;
+
+ PELIB_IMAGE_DATA_DIRECTORY()
+ {
+ VirtualAddress = 0;
+ Size = 0;
+ }
+
+ static inline unsigned int size() {return 8;}
+ };
+
+ template<int>
+ struct FieldSizes;
+
+ template<>
+ struct FieldSizes<32>
+ {
+ typedef dword VAR4_8;
+ };
+
+ template<>
+ struct FieldSizes<64>
+ {
+ typedef qword VAR4_8;
+ };
+
+ template<int x>
+ struct PELIB_IMAGE_OPTIONAL_HEADER_BASE
+ {
+ typedef typename FieldSizes<x>::VAR4_8 VAR4_8;
+
+ word Magic;
+ byte MajorLinkerVersion;
+ byte MinorLinkerVersion;
+ dword SizeOfCode;
+ dword SizeOfInitializedData;
+ dword SizeOfUninitializedData;
+ dword AddressOfEntryPoint;
+ dword BaseOfCode;
+ dword BaseOfData;
+ VAR4_8 ImageBase;
+ dword SectionAlignment;
+ dword FileAlignment;
+ word MajorOperatingSystemVersion;
+ word MinorOperatingSystemVersion;
+ word MajorImageVersion;
+ word MinorImageVersion;
+ word MajorSubsystemVersion;
+ word MinorSubsystemVersion;
+ dword Win32VersionValue;
+ dword SizeOfImage;
+ dword SizeOfHeaders;
+ dword CheckSum;
+ word Subsystem;
+ word DllCharacteristics;
+ VAR4_8 SizeOfStackReserve;
+ VAR4_8 SizeOfStackCommit;
+ VAR4_8 SizeOfHeapReserve;
+ VAR4_8 SizeOfHeapCommit;
+ dword LoaderFlags;
+ dword NumberOfRvaAndSizes;
+// PELIB_IMAGE_DATA_DIRECTORY DataDirectory[IMAGE_NUMBEROF_DIRECTORY_ENTRIES];
+
+ PELIB_IMAGE_OPTIONAL_HEADER_BASE();
+ };
+
+ template<int x>
+ PELIB_IMAGE_OPTIONAL_HEADER_BASE<x>::PELIB_IMAGE_OPTIONAL_HEADER_BASE()
+ {
+ Magic = 0;
+ MajorLinkerVersion = 0;
+ MinorLinkerVersion = 0;
+ SizeOfCode = 0;
+ SizeOfInitializedData = 0;
+ SizeOfUninitializedData = 0;
+ AddressOfEntryPoint = 0;
+ BaseOfCode = 0;
+// BaseOfData = 0;
+ ImageBase = 0;
+ SectionAlignment = 0;
+ FileAlignment = 0;
+ MajorOperatingSystemVersion = 0;
+ MinorOperatingSystemVersion = 0;
+ MajorImageVersion = 0;
+ MinorImageVersion = 0;
+ MajorSubsystemVersion = 0;
+ MinorSubsystemVersion = 0;
+ Win32VersionValue = 0;
+ SizeOfImage = 0;
+ SizeOfHeaders = 0;
+ CheckSum = 0;
+ Subsystem = 0;
+ DllCharacteristics = 0;
+ SizeOfStackReserve = 0;
+ SizeOfStackCommit = 0;
+ SizeOfHeapReserve = 0;
+ SizeOfHeapCommit = 0;
+ LoaderFlags = 0;
+ NumberOfRvaAndSizes = 0;
+ }
+
+ template<int>
+ struct PELIB_IMAGE_OPTIONAL_HEADER;
+
+ template<>
+ struct PELIB_IMAGE_OPTIONAL_HEADER<32> : public PELIB_IMAGE_OPTIONAL_HEADER_BASE<32>
+ {
+ dword BaseOfData;
+
+ static inline unsigned int size() {return 224 - 0x10 * 8;}
+ };
+
+ template<>
+ struct PELIB_IMAGE_OPTIONAL_HEADER<64> : public PELIB_IMAGE_OPTIONAL_HEADER_BASE<64>
+ {
+ static inline unsigned int size() {return 240 - 0x10 * 8;}
+ };
+
+ template<int x>
+ struct PELIB_IMAGE_NT_HEADERS
+ {
+ dword Signature;
+ PELIB_IMAGE_FILE_HEADER FileHeader;
+ PELIB_IMAGE_OPTIONAL_HEADER<x> OptionalHeader;
+ std::vector<PELIB_IMAGE_DATA_DIRECTORY> dataDirectories;
+
+ unsigned int size() const
+ {
+ return sizeof(dword)
+ + PELIB_IMAGE_FILE_HEADER::size()
+ + PELIB_IMAGE_OPTIONAL_HEADER<x>::size()
+ + static_cast<unsigned int>(dataDirectories.size()) * PELIB_IMAGE_DATA_DIRECTORY::size();
+ }
+
+ PELIB_IMAGE_NT_HEADERS()
+ {
+ Signature = 0;
+ }
+ };
+
+ const unsigned int PELIB_IMAGE_SIZEOF_SHORT_NAME = 8;
+
+ struct PELIB_IMAGE_SECTION_HEADER
+ {
+ byte Name[PELIB_IMAGE_SIZEOF_SHORT_NAME];
+ dword VirtualSize;
+ dword VirtualAddress;
+ dword SizeOfRawData;
+ dword PointerToRawData;
+ dword PointerToRelocations;
+ dword PointerToLinenumbers;
+ word NumberOfRelocations;
+ word NumberOfLinenumbers;
+ dword Characteristics;
+
+ PELIB_IMAGE_SECTION_HEADER()
+ {
+ VirtualSize = 0;
+ VirtualAddress = 0;
+ SizeOfRawData = 0;
+ PointerToRawData = 0;
+ PointerToRelocations = 0;
+ PointerToLinenumbers = 0;
+ NumberOfRelocations = 0;
+ NumberOfLinenumbers = 0;
+ Characteristics = 0;
+ }
+
+ static inline unsigned int size() {return 40;}
+ bool biggerFileOffset(const PELIB_IMAGE_SECTION_HEADER& ish) const;
+ bool biggerVirtualAddress(const PELIB_IMAGE_SECTION_HEADER& ish) const;
+ };
+
+ template<int bits>
+ struct PELIB_IMAGE_THUNK_DATA
+ {
+ typename FieldSizes<bits>::VAR4_8 Ordinal;
+
+ PELIB_IMAGE_THUNK_DATA()
+ {
+ Ordinal = 0;
+ }
+
+ static inline unsigned int size() {return 4;}
+ };
+
+ struct PELIB_IMAGE_IMPORT_DESCRIPTOR
+ {
+ dword OriginalFirstThunk;
+ dword TimeDateStamp;
+ dword ForwarderChain;
+ dword Name;
+ dword FirstThunk;
+
+ PELIB_IMAGE_IMPORT_DESCRIPTOR()
+ {
+ OriginalFirstThunk = 0;
+ TimeDateStamp = 0;
+ ForwarderChain = 0;
+ Name = 0;
+ FirstThunk = 0;
+ }
+
+ static inline unsigned int size() {return 20;}
+ };
+
+ struct PELIB_IMAGE_EXPORT_DIRECTORY
+ {
+ dword Characteristics;
+ dword TimeDateStamp;
+ word MajorVersion;
+ word MinorVersion;
+ dword Name;
+ dword Base;
+ dword NumberOfFunctions;
+ dword NumberOfNames;
+ dword AddressOfFunctions;
+ dword AddressOfNames;
+ dword AddressOfNameOrdinals;
+
+ PELIB_IMAGE_EXPORT_DIRECTORY()
+ {
+ Characteristics = 0;
+ TimeDateStamp = 0;
+ MajorVersion = 0;
+ MinorVersion = 0;
+ Name = 0;
+ Base = 0;
+ NumberOfFunctions = 0;
+ NumberOfNames = 0;
+ AddressOfFunctions = 0;
+ NumberOfNames = 0;
+ AddressOfNameOrdinals = 0;
+ }
+
+ static inline unsigned int size() {return 40;}
+ };
+
+ struct PELIB_IMAGE_BOUND_IMPORT_DESCRIPTOR
+ {
+ dword TimeDateStamp;
+ word OffsetModuleName;
+ word NumberOfModuleForwarderRefs;
+
+ PELIB_IMAGE_BOUND_IMPORT_DESCRIPTOR()
+ {
+ TimeDateStamp = 0;
+ OffsetModuleName = 0;
+ NumberOfModuleForwarderRefs = 0;
+ }
+
+ static unsigned int size()
+ {
+ return 8;
+ }
+ };
+
+ // Stores all necessary information about a BoundImport field.
+ struct PELIB_IMAGE_BOUND_DIRECTORY
+ {
+ PELIB_IMAGE_BOUND_IMPORT_DESCRIPTOR ibdDescriptor; ///< Information about the imported file.
+ std::string strModuleName; ///< Name of the imported file.
+ std::vector<PELIB_IMAGE_BOUND_DIRECTORY> moduleForwarders;
+
+ // Will be used in std::find_if
+ // Passing by-reference not possible (see C++ Standard Core Language Defect Reports, Revision 29, Issue 106)
+ /// Compares the passed filename to the struct's filename.
+ bool equal(const std::string strModuleName) const;
+
+ unsigned int size() const;
+ };
+
+ struct PELIB_EXP_FUNC_INFORMATION
+ {
+ dword addroffunc;
+ dword addrofname;
+ word ordinal;
+ std::string funcname;
+
+ PELIB_EXP_FUNC_INFORMATION();
+
+ bool equal(const std::string strFunctionName) const;
+ inline unsigned int size() const
+ {
+ unsigned int uiSize = 4;
+ if (addroffunc) uiSize += 2;// + 4;
+ if (!funcname.empty()) uiSize += 4 + (unsigned int)funcname.size() + 1;
+ return uiSize;
+ }
+ };
+
+ struct PELIB_IMAGE_RESOURCE_DIRECTORY
+ {
+ dword Characteristics;
+ dword TimeDateStamp;
+ word MajorVersion;
+ word MinorVersion;
+ word NumberOfNamedEntries;
+ word NumberOfIdEntries;
+
+ PELIB_IMAGE_RESOURCE_DIRECTORY();
+
+ static inline unsigned int size() {return 16;}
+ };
+
+ struct PELIB_IMAGE_RESOURCE_DIRECTORY_ENTRY
+ {
+ dword Name;
+ dword OffsetToData;
+ PELIB_IMAGE_RESOURCE_DIRECTORY_ENTRY();
+ static inline unsigned int size() {return 8;}
+ };
+
+ const unsigned int PELIB_IMAGE_SIZEOF_BASE_RELOCATION = 8;
+
+ struct PELIB_IMG_RES_DIR_ENTRY
+ {
+ PELIB_IMAGE_RESOURCE_DIRECTORY_ENTRY irde;
+ std::string wstrName;
+
+ bool operator<(const PELIB_IMG_RES_DIR_ENTRY& first) const;
+
+ };
+
+ struct PELIB_IMAGE_BASE_RELOCATION
+ {
+ dword VirtualAddress;
+ dword SizeOfBlock;
+
+ PELIB_IMAGE_BASE_RELOCATION();
+ static inline unsigned int size() {return 8;}
+ };
+
+ struct PELIB_IMAGE_COR20_HEADER
+ {
+ dword cb;
+ word MajorRuntimeVersion;
+ word MinorRuntimeVersion;
+ PELIB_IMAGE_DATA_DIRECTORY MetaData;
+ dword Flags;
+ dword EntryPointToken;
+ PELIB_IMAGE_DATA_DIRECTORY Resources;
+ PELIB_IMAGE_DATA_DIRECTORY StrongNameSignature;
+ PELIB_IMAGE_DATA_DIRECTORY CodeManagerTable;
+ PELIB_IMAGE_DATA_DIRECTORY VTableFixups;
+ PELIB_IMAGE_DATA_DIRECTORY ExportAddressTableJumps;
+ PELIB_IMAGE_DATA_DIRECTORY ManagedNativeHeader;
+
+ PELIB_IMAGE_COR20_HEADER();
+ static inline unsigned int size() {return 72;}
+ };
+
+ // Used to store a file's export table.
+ struct PELIB_IMAGE_EXP_DIRECTORY
+ {
+ /// The IMAGE_EXPORTED_DIRECTORY of a file's export table.
+ PELIB_IMAGE_EXPORT_DIRECTORY ied;
+ /// The original filename of current file.
+ std::string name;
+ std::vector<PELIB_EXP_FUNC_INFORMATION> functions;
+ inline unsigned int size() const
+ {
+ return PELIB_IMAGE_EXPORT_DIRECTORY::size() + name.size() + 1 +
+ std::accumulate(functions.begin(), functions.end(), 0, accumulate<PELIB_EXP_FUNC_INFORMATION>);
+ }
+ };
+
+ // Used for parsing a file's import table. It combines the function name, the hint
+ // and the IMAGE_THUNK_DATA of an imported function.
+ template<int bits>
+ struct PELIB_THUNK_DATA
+ {
+ /// The IMAGE_THUNK_DATA struct of an imported function.
+ PELIB_IMAGE_THUNK_DATA<bits> itd;
+ /// The hint of an imported function.
+ word hint;
+ /// The function name of an imported function.
+ std::string fname;
+
+ bool equalHint(word wHint) const
+ {
+ return hint == wHint;
+// return itd.Ordinal == (wHint | IMAGE_ORDINAL_FLAGS<bits>::IMAGE_ORDINAL_FLAG);
+ }
+
+ bool equalFunctionName(std::string strFunctionName) const
+ {
+ return isEqualNc(fname, strFunctionName);
+ }
+
+ unsigned int size() const {return PELIB_IMAGE_THUNK_DATA<bits>::size() + fname.size() + 1 + sizeof(hint);}
+ };
+
+ // Used to store a file's import table. Every struct of this sort
+ // can store import information of one DLL.
+ template<int bits>
+ struct PELIB_IMAGE_IMPORT_DIRECTORY
+ {
+ /// The IMAGE_IMPORT_DESCRIPTOR of an imported DLL.
+ PELIB_IMAGE_IMPORT_DESCRIPTOR impdesc;
+ /// The name of an imported DLL.
+ std::string name;
+ /// All original first thunk values of an imported DLL.
+ std::vector<PELIB_THUNK_DATA<bits> > originalfirstthunk;
+ /// All first thunk value of an imported DLL.
+ std::vector<PELIB_THUNK_DATA<bits> > firstthunk;
+
+// bool operator==(std::string strFilename) const;
+ inline unsigned int size() const
+ {
+ return PELIB_IMAGE_IMPORT_DESCRIPTOR::size() + name.size() + 1 + // descriptor + dllname
+ std::accumulate(originalfirstthunk.begin(), originalfirstthunk.end(), 0, accumulate<PELIB_THUNK_DATA<bits> >) + // thunks (PeLib uses only one thunk)
+ PELIB_IMAGE_THUNK_DATA<bits>::size(); // zero-termination
+ }
+
+ bool operator==(std::string strFilename) const
+ {
+ return isEqualNc(this->name, strFilename);
+ }
+ };
+
+ struct PELIB_IMAGE_RESOURCE_DATA_ENTRY
+ {
+ dword OffsetToData;
+ dword Size;
+ dword CodePage;
+ dword Reserved;
+
+ static inline unsigned int size() {return 16;}
+ };
+
+ struct PELIB_IMAGE_RESOURCE_DATA
+ {
+ PELIB_IMAGE_RESOURCE_DATA_ENTRY irdEntry;
+ std::vector<byte> vData;
+ };
+
+ struct IMG_BASE_RELOC
+ {
+ PELIB_IMAGE_BASE_RELOCATION ibrRelocation;
+ std::vector<word> vRelocData;
+ };
+
+ struct PELIB_IMAGE_DEBUG_DIRECTORY
+ {
+ dword Characteristics;
+ dword TimeDateStamp;
+ word MajorVersion;
+ word MinorVersion;
+ dword Type;
+ dword SizeOfData;
+ dword AddressOfRawData;
+ dword PointerToRawData;
+
+ static unsigned int size() {return 28;}
+ };
+
+ struct PELIB_IMG_DEBUG_DIRECTORY
+ {
+ PELIB_IMAGE_DEBUG_DIRECTORY idd;
+ std::vector<byte> data;
+ };
+
+ template<int bits>
+ struct PELIB_IMAGE_TLS_DIRECTORY_BASE
+ {
+ typename FieldSizes<bits>::VAR4_8 StartAddressOfRawData;
+ typename FieldSizes<bits>::VAR4_8 EndAddressOfRawData;
+ typename FieldSizes<bits>::VAR4_8 AddressOfIndex