From ca5bb76d2b8f65aa97e50b633f828c1deb241526 Mon Sep 17 00:00:00 2001 From: Nicolas Pennequin Date: Fri, 11 Jul 2008 16:51:25 +0000 Subject: 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 --- utils/zenutils/libraries/pelib-0.9/CMakeLists.txt | 40 +- .../pelib-0.9/pelib/BoundImportDirectory.cpp | 1022 ++-- .../pelib-0.9/pelib/BoundImportDirectory.h | 174 +- .../pelib-0.9/pelib/ComHeaderDirectory.cpp | 934 ++-- .../libraries/pelib-0.9/pelib/ComHeaderDirectory.h | 240 +- .../libraries/pelib-0.9/pelib/DebugDirectory.cpp | 766 +-- .../libraries/pelib-0.9/pelib/DebugDirectory.h | 168 +- .../libraries/pelib-0.9/pelib/ExportDirectory.cpp | 1384 ++--- .../libraries/pelib-0.9/pelib/ExportDirectory.h | 266 +- .../libraries/pelib-0.9/pelib/IatDirectory.cpp | 358 +- .../libraries/pelib-0.9/pelib/IatDirectory.h | 116 +- .../libraries/pelib-0.9/pelib/ImportDirectory.h | 2278 ++++----- .../libraries/pelib-0.9/pelib/MzHeader.cpp | 1168 ++--- .../zenutils/libraries/pelib-0.9/pelib/MzHeader.h | 296 +- .../zenutils/libraries/pelib-0.9/pelib/PeFile.cpp | 338 +- utils/zenutils/libraries/pelib-0.9/pelib/PeFile.h | 902 ++-- .../libraries/pelib-0.9/pelib/PeHeader.cpp | 180 +- .../zenutils/libraries/pelib-0.9/pelib/PeHeader.h | 5370 ++++++++++---------- utils/zenutils/libraries/pelib-0.9/pelib/PeLib.h | 54 +- .../libraries/pelib-0.9/pelib/PeLibAux.cpp | 550 +- .../zenutils/libraries/pelib-0.9/pelib/PeLibAux.h | 1768 +++---- .../zenutils/libraries/pelib-0.9/pelib/PeLibInc.h | 64 +- .../pelib-0.9/pelib/RelocationsDirectory.cpp | 422 +- .../pelib-0.9/pelib/RelocationsDirectory.h | 140 +- .../pelib-0.9/pelib/ResourceDirectory.cpp | 2994 +++++------ .../libraries/pelib-0.9/pelib/ResourceDirectory.h | 1470 +++--- .../libraries/pelib-0.9/pelib/TlsDirectory.h | 608 +-- .../pelib-0.9/pelib/buffer/InputBuffer.cpp | 116 +- .../libraries/pelib-0.9/pelib/buffer/InputBuffer.h | 104 +- .../pelib-0.9/pelib/buffer/OutputBuffer.cpp | 82 +- .../pelib-0.9/pelib/buffer/OutputBuffer.h | 102 +- .../libraries/pelib-0.9/pelib/changelog.txt | 640 +-- .../zenutils/libraries/pelib-0.9/pelib/license.htm | 68 +- .../zenutils/libraries/pelib-0.9/pelib/readme.txt | 86 +- 34 files changed, 12634 insertions(+), 12634 deletions(-) mode change 100755 => 100644 utils/zenutils/libraries/pelib-0.9/CMakeLists.txt mode change 100755 => 100644 utils/zenutils/libraries/pelib-0.9/pelib/BoundImportDirectory.cpp mode change 100755 => 100644 utils/zenutils/libraries/pelib-0.9/pelib/BoundImportDirectory.h mode change 100755 => 100644 utils/zenutils/libraries/pelib-0.9/pelib/ComHeaderDirectory.cpp mode change 100755 => 100644 utils/zenutils/libraries/pelib-0.9/pelib/ComHeaderDirectory.h mode change 100755 => 100644 utils/zenutils/libraries/pelib-0.9/pelib/DebugDirectory.cpp mode change 100755 => 100644 utils/zenutils/libraries/pelib-0.9/pelib/DebugDirectory.h mode change 100755 => 100644 utils/zenutils/libraries/pelib-0.9/pelib/ExportDirectory.cpp mode change 100755 => 100644 utils/zenutils/libraries/pelib-0.9/pelib/ExportDirectory.h mode change 100755 => 100644 utils/zenutils/libraries/pelib-0.9/pelib/IatDirectory.cpp mode change 100755 => 100644 utils/zenutils/libraries/pelib-0.9/pelib/IatDirectory.h mode change 100755 => 100644 utils/zenutils/libraries/pelib-0.9/pelib/ImportDirectory.h mode change 100755 => 100644 utils/zenutils/libraries/pelib-0.9/pelib/MzHeader.cpp mode change 100755 => 100644 utils/zenutils/libraries/pelib-0.9/pelib/MzHeader.h mode change 100755 => 100644 utils/zenutils/libraries/pelib-0.9/pelib/PeFile.cpp mode change 100755 => 100644 utils/zenutils/libraries/pelib-0.9/pelib/PeFile.h mode change 100755 => 100644 utils/zenutils/libraries/pelib-0.9/pelib/PeHeader.cpp mode change 100755 => 100644 utils/zenutils/libraries/pelib-0.9/pelib/PeHeader.h mode change 100755 => 100644 utils/zenutils/libraries/pelib-0.9/pelib/PeLib.h mode change 100755 => 100644 utils/zenutils/libraries/pelib-0.9/pelib/PeLibAux.cpp mode change 100755 => 100644 utils/zenutils/libraries/pelib-0.9/pelib/PeLibAux.h mode change 100755 => 100644 utils/zenutils/libraries/pelib-0.9/pelib/PeLibInc.h mode change 100755 => 100644 utils/zenutils/libraries/pelib-0.9/pelib/RelocationsDirectory.cpp mode change 100755 => 100644 utils/zenutils/libraries/pelib-0.9/pelib/RelocationsDirectory.h mode change 100755 => 100644 utils/zenutils/libraries/pelib-0.9/pelib/ResourceDirectory.cpp mode change 100755 => 100644 utils/zenutils/libraries/pelib-0.9/pelib/ResourceDirectory.h mode change 100755 => 100644 utils/zenutils/libraries/pelib-0.9/pelib/TlsDirectory.h mode change 100755 => 100644 utils/zenutils/libraries/pelib-0.9/pelib/buffer/InputBuffer.cpp mode change 100755 => 100644 utils/zenutils/libraries/pelib-0.9/pelib/buffer/InputBuffer.h mode change 100755 => 100644 utils/zenutils/libraries/pelib-0.9/pelib/buffer/OutputBuffer.cpp mode change 100755 => 100644 utils/zenutils/libraries/pelib-0.9/pelib/buffer/OutputBuffer.h mode change 100755 => 100644 utils/zenutils/libraries/pelib-0.9/pelib/changelog.txt mode change 100755 => 100644 utils/zenutils/libraries/pelib-0.9/pelib/license.htm mode change 100755 => 100644 utils/zenutils/libraries/pelib-0.9/pelib/readme.txt (limited to 'utils/zenutils/libraries/pelib-0.9') diff --git a/utils/zenutils/libraries/pelib-0.9/CMakeLists.txt b/utils/zenutils/libraries/pelib-0.9/CMakeLists.txt old mode 100755 new mode 100644 index 540fa99c8c..3cdb6e71ff --- 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 old mode 100755 new mode 100644 index 5b84931838..7c5ff74f0e --- 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 -#include -#include - -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::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(std::distance(m_vIbd.begin(), Iter)); - } - - /** - * @return Number of files in the current BoundImport directory. - **/ - unsigned int BoundImportDirectory::calcNumberOfModules() const - { - return static_cast(m_vIbd.size()); - } - - int BoundImportDirectory::read(InputBuffer& inpBuffer, unsigned char* data, unsigned int dwSize) - { - std::vector 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> 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 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 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 vBimpDir(uiSize); - ifFile.read(reinterpret_cast(&vBimpDir[0]), uiSize); - - InputBuffer inpBuffer(vBimpDir); - - return read(inpBuffer, &vBimpDir[0], uiSize); - } - - int BoundImportDirectory::read(unsigned char* pcBuffer, unsigned int uiSize) - { - std::vector vBimpDir(pcBuffer, pcBuffer + uiSize); - InputBuffer inpBuffer(vBimpDir); - - return read(inpBuffer, &vBimpDir[0], uiSize); - } - - unsigned int BoundImportDirectory::totalModules() const - { - unsigned int modules = static_cast(m_vIbd.size()); - - for (unsigned int i=0;i(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& vBuffer, bool fMakeValid) const - { - std::map filename_offsets; - - OutputBuffer obBuffer(vBuffer); - - word ulNameOffset = static_cast((totalModules() + 1) * PELIB_IMAGE_BOUND_IMPORT_DESCRIPTOR::size()); - - for (unsigned int i=0;i(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(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(0); - obBuffer << static_cast(0); - obBuffer << static_cast(0); - - for (unsigned int i=0;i(getModuleName(i).size() + 1)); - filename_offsets.erase(m_vIbd[i].strModuleName); - } - - for (int j=0;j(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 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::iterator iter = filenames.begin(); iter != filenames.end(); ++iter) - { - size += static_cast(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 vBuffer; - rebuild(vBuffer, fMakeValid); - - ofFile.write(reinterpret_cast(&vBuffer[0]), static_cast(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(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 +#include +#include + +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::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(std::distance(m_vIbd.begin(), Iter)); + } + + /** + * @return Number of files in the current BoundImport directory. + **/ + unsigned int BoundImportDirectory::calcNumberOfModules() const + { + return static_cast(m_vIbd.size()); + } + + int BoundImportDirectory::read(InputBuffer& inpBuffer, unsigned char* data, unsigned int dwSize) + { + std::vector 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> 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 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 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 vBimpDir(uiSize); + ifFile.read(reinterpret_cast(&vBimpDir[0]), uiSize); + + InputBuffer inpBuffer(vBimpDir); + + return read(inpBuffer, &vBimpDir[0], uiSize); + } + + int BoundImportDirectory::read(unsigned char* pcBuffer, unsigned int uiSize) + { + std::vector vBimpDir(pcBuffer, pcBuffer + uiSize); + InputBuffer inpBuffer(vBimpDir); + + return read(inpBuffer, &vBimpDir[0], uiSize); + } + + unsigned int BoundImportDirectory::totalModules() const + { + unsigned int modules = static_cast(m_vIbd.size()); + + for (unsigned int i=0;i(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& vBuffer, bool fMakeValid) const + { + std::map filename_offsets; + + OutputBuffer obBuffer(vBuffer); + + word ulNameOffset = static_cast((totalModules() + 1) * PELIB_IMAGE_BOUND_IMPORT_DESCRIPTOR::size()); + + for (unsigned int i=0;i(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(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(0); + obBuffer << static_cast(0); + obBuffer << static_cast(0); + + for (unsigned int i=0;i(getModuleName(i).size() + 1)); + filename_offsets.erase(m_vIbd[i].strModuleName); + } + + for (int j=0;j(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 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::iterator iter = filenames.begin(); iter != filenames.end(); ++iter) + { + size += static_cast(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 vBuffer; + rebuild(vBuffer, fMakeValid); + + ofFile.write(reinterpret_cast(&vBuffer[0]), static_cast(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(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 old mode 100755 new mode 100644 index fd2bab56fd..142d78f710 --- 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 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& 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 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& 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 old mode 100755 new mode 100644 index 886348994e..fef12f4d8b --- 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 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 vComDescDirectory(uiSize); - ifFile.read(reinterpret_cast(&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& 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 vBuffer; - rebuild(vBuffer); - - ofFile.write(reinterpret_cast(&vBuffer[0]), static_cast(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 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 vComDescDirectory(uiSize); + ifFile.read(reinterpret_cast(&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& 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 vBuffer; + rebuild(vBuffer); + + ofFile.write(reinterpret_cast(&vBuffer[0]), static_cast(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 old mode 100755 new mode 100644 index 915813c1cc..cdc993df1f --- 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& 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& 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 old mode 100755 new mode 100644 index eb3d5c5600..9a91c7b18c --- 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 DebugDirectory::read(InputBuffer& ibBuffer, unsigned int uiSize) - { - std::vector currDebugInfo; - - PELIB_IMG_DEBUG_DIRECTORY iddCurr; - - for (unsigned int i=0;i> 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 vDebugDirectory(buffer, buffer + buffersize); - - InputBuffer ibBuffer(vDebugDirectory); - - std::vector 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 vDebugDirectory(uiSize); - ifFile.read(reinterpret_cast(&vDebugDirectory[0]), uiSize); - - InputBuffer ibBuffer(vDebugDirectory); - - std::vector currDebugInfo = read(ibBuffer, uiSize); - - for (unsigned int i=0;i(&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& vBuffer) const - { - OutputBuffer obBuffer(vBuffer); - - for (unsigned int i=0;i(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 vBuffer; - rebuild(vBuffer); - - ofFile.write(reinterpret_cast(&vBuffer[0]), static_cast(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(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 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& 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 DebugDirectory::read(InputBuffer& ibBuffer, unsigned int uiSize) + { + std::vector currDebugInfo; + + PELIB_IMG_DEBUG_DIRECTORY iddCurr; + + for (unsigned int i=0;i> 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 vDebugDirectory(buffer, buffer + buffersize); + + InputBuffer ibBuffer(vDebugDirectory); + + std::vector 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 vDebugDirectory(uiSize); + ifFile.read(reinterpret_cast(&vDebugDirectory[0]), uiSize); + + InputBuffer ibBuffer(vDebugDirectory); + + std::vector currDebugInfo = read(ibBuffer, uiSize); + + for (unsigned int i=0;i(&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& vBuffer) const + { + OutputBuffer obBuffer(vBuffer); + + for (unsigned int i=0;i(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 vBuffer; + rebuild(vBuffer); + + ofFile.write(reinterpret_cast(&vBuffer[0]), static_cast(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(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 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& 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 old mode 100755 new mode 100644 index 0ffda38815..adfef6b52e --- 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 m_vDebugInfo; - - std::vector 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& 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 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& 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 m_vDebugInfo; + + std::vector 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& 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 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& 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 old mode 100755 new mode 100644 index aa9c28a50f..83c4b4e69d --- 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(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::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(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 vExportdirectory(uiSize); - ifFile.read(reinterpret_cast(&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(&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(&pehHeader)) - { - // XXX: Files might be > 4 GB - unsigned int offset = static_cast(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(&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(&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(&pehHeader)) - { - // XXX: File might be > 4 GB - unsigned int offset = static_cast(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(&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(&pehHeader)) - { - // XXX: File might be > 4GB - unsigned int offset = static_cast(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(&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(&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(&pehHeader)) - { - // XXX: File might be > 4 GB - unsigned int offset = static_cast(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(&ordinal), sizeof(ordinal)); - - if (!ifFile) - return ERROR_INVALID_FILE; - - iedCurr.functions[ordinal].ordinal = ordinal; - - if (const PeHeader32* p32 = dynamic_cast(&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(&pehHeader)) - { - // XXX: File might be > 4 GB. - unsigned int offset = static_cast(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(&iedCurr.functions[ordinal].addrofname), sizeof(iedCurr.functions[ordinal].addrofname)); - - if (!ifFile) - return ERROR_INVALID_FILE; - - if (const PeHeader32* p32 = dynamic_cast(&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(&pehHeader)) - { - // XXX: File might be > 4 GB. - unsigned int offset = static_cast(p64->rvaToOffset(iedCurr.functions[ordinal].addrofname)); - - if (offset >= filesize) - return ERROR_INVALID_FILE; - - ifFile.seekg(static_cast(p64->rvaToOffset(iedCurr.functions[ordinal].addrofname)), std::ios::beg); - } - - char c = 0; - std::string strFname = ""; - do - { - ifFile.read(reinterpret_cast(&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& 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[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(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(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(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(m_ied.name.size())+1); - - for (unsigned int i=0;i(m_ied.functions[i].funcname.size()) + 1; - } - } - - for (unsigned int i=0;i(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 vBuffer; - rebuild(vBuffer, uiRva); - - ofFile.write(reinterpret_cast(&vBuffer[0]), static_cast(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(m_ied.functions.size()); - } - - dword ExportDirectory::getNumberOfAddressOfFunctionNames() const - { - return static_cast(m_ied.functions.size()); - } - - dword ExportDirectory::getNumberOfAddressOfFunctions() const - { - return static_cast(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(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::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(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 vExportdirectory(uiSize); + ifFile.read(reinterpret_cast(&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(&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(&pehHeader)) + { + // XXX: Files might be > 4 GB + unsigned int offset = static_cast(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(&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(&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(&pehHeader)) + { + // XXX: File might be > 4 GB + unsigned int offset = static_cast(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(&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(&pehHeader)) + { + // XXX: File might be > 4GB + unsigned int offset = static_cast(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(&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(&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(&pehHeader)) + { + // XXX: File might be > 4 GB + unsigned int offset = static_cast(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(&ordinal), sizeof(ordinal)); + + if (!ifFile) + return ERROR_INVALID_FILE; + + iedCurr.functions[ordinal].ordinal = ordinal; + + if (const PeHeader32* p32 = dynamic_cast(&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(&pehHeader)) + { + // XXX: File might be > 4 GB. + unsigned int offset = static_cast(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(&iedCurr.functions[ordinal].addrofname), sizeof(iedCurr.functions[ordinal].addrofname)); + + if (!ifFile) + return ERROR_INVALID_FILE; + + if (const PeHeader32* p32 = dynamic_cast(&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(&pehHeader)) + { + // XXX: File might be > 4 GB. + unsigned int offset = static_cast(p64->rvaToOffset(iedCurr.functions[ordinal].addrofname)); + + if (offset >= filesize) + return ERROR_INVALID_FILE; + + ifFile.seekg(static_cast(p64->rvaToOffset(iedCurr.functions[ordinal].addrofname)), std::ios::beg); + } + + char c = 0; + std::string strFname = ""; + do + { + ifFile.read(reinterpret_cast(&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& 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[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(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(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(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(m_ied.name.size())+1); + + for (unsigned int i=0;i(m_ied.functions[i].funcname.size()) + 1; + } + } + + for (unsigned int i=0;i(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 vBuffer; + rebuild(vBuffer, uiRva); + + ofFile.write(reinterpret_cast(&vBuffer[0]), static_cast(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(m_ied.functions.size()); + } + + dword ExportDirectory::getNumberOfAddressOfFunctionNames() const + { + return static_cast(m_ied.functions.size()); + } + + dword ExportDirectory::getNumberOfAddressOfFunctions() const + { + return static_cast(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 old mode 100755 new mode 100644 index 17a7e57020..19609b65a7 --- 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 - 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& 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 + 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& 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 old mode 100755 new mode 100644 index 36482fcbeb..b32cfb2a8e --- 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 vIat; - - for (unsigned int i=0;i> 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 vBuffer(dwSize); - ifFile.read(reinterpret_cast(&vBuffer[0]), dwSize); - - InputBuffer inpBuffer(vBuffer); - return read(inpBuffer, dwSize); - } - - int IatDirectory::read(unsigned char* buffer, unsigned int buffersize) - { - std::vector 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(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::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& vBuffer) const - { - vBuffer.reserve(size()); - OutputBuffer obBuffer(vBuffer); - - for (unsigned int i=0;i(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 vBuffer; - rebuild(vBuffer); - - ofFile.write(reinterpret_cast(&vBuffer[0]), static_cast(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 vIat; + + for (unsigned int i=0;i> 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 vBuffer(dwSize); + ifFile.read(reinterpret_cast(&vBuffer[0]), dwSize); + + InputBuffer inpBuffer(vBuffer); + return read(inpBuffer, dwSize); + } + + int IatDirectory::read(unsigned char* buffer, unsigned int buffersize) + { + std::vector 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(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::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& vBuffer) const + { + vBuffer.reserve(size()); + OutputBuffer obBuffer(vBuffer); + + for (unsigned int i=0;i(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 vBuffer; + rebuild(vBuffer); + + ofFile.write(reinterpret_cast(&vBuffer[0]), static_cast(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 old mode 100755 new mode 100644 index 81ef77ed6a..26c5d4009c --- 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 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& 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 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& 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 old mode 100755 new mode 100644 index 6578f0712a..42526f0c3c --- 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 - class ImportDirectory - { - typedef typename std::vector >::iterator ImpDirFileIterator; - typedef typename std::vector >::const_iterator ConstImpDirFileIterator; - - private: - /// Stores information about already imported DLLs. - std::vector > m_vOldiid; - /// Stores information about imported DLLs which will be added. - std::vector > 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 bool hasFunction(std::string strFilename, T value, bool(PELIB_THUNK_DATA::* 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::operator==), strFilename)); - - if (FileIter != EndIter) - { - typename std::vector >::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& pehHeader); // EXPORT - /// Rebuild the import directory. - void rebuild(std::vector& 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 ImportDirectory::addFunction(const std::string& strFilename, word wHint) - { - if (hasFunction(strFilename, wHint, &PELIB_THUNK_DATA::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::operator==), strFilename)); - - PELIB_IMAGE_IMPORT_DIRECTORY iid; - PELIB_THUNK_DATA td; - td.hint = wHint; - td.itd.Ordinal = wHint | PELIB_IMAGE_ORDINAL_FLAGS::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 ImportDirectory::addFunction(const std::string& strFilename, const std::string& strFuncname) - { - if (hasFunction(strFilename, strFuncname, &PELIB_THUNK_DATA::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::operator==), strFilename)); - - PELIB_IMAGE_IMPORT_DIRECTORY iid; - PELIB_THUNK_DATA 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 - unsigned int ImportDirectory::getFileIndex(const std::string& strFilename, currdir cdDir) const - { - const std::vector >* 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::operator==), strFilename)); - - if (FileIter != currDir->end()) - { - return static_cast(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 - unsigned int ImportDirectory::getFunctionIndex(const std::string& strFilename, const std::string& strFuncname, currdir cdDir) const - { - unsigned int uiFile = getFileIndex(strFilename, cdDir); - - for (unsigned int i=0;i - std::string ImportDirectory::getFileName(dword dwFilenr, currdir cdDir) const - { - if (cdDir == OLDDIR) return m_vOldiid[dwFilenr].name; - else return m_vNewiid[dwFilenr].name; - } - - template - void ImportDirectory::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 - std::string ImportDirectory::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 - void ImportDirectory::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 - word ImportDirectory::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 - void ImportDirectory::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 - dword ImportDirectory::getNumberOfFiles(currdir cdDir) const - { - if (cdDir == OLDDIR) return static_cast(m_vOldiid.size()); - else return static_cast(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 - dword ImportDirectory::getNumberOfFunctions(dword dwFilenr, currdir cdDir) const - { - if (cdDir == OLDDIR) return static_cast(m_vOldiid[dwFilenr].firstthunk.size()); - else return static_cast(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 ImportDirectory::read(const std::string& strFilename, unsigned int uiOffset, unsigned int uiSize, const PeHeaderT& 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 vImportdirectory(uiSize); - ifFile.read(reinterpret_cast(&vImportdirectory[0]), uiSize); - - PELIB_IMAGE_IMPORT_DIRECTORY iidCurr; - unsigned int uiDesccounter = 0; - - InputBuffer inpBuffer(vImportdirectory); - - std::vector > 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(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 tdCurr; - dword uiVaoft = vOldIidCurr[i].impdesc.OriginalFirstThunk; - - if (!uiVaoft) - { - continue; - } - - ifFile.seekg(static_cast(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(&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 tdCurr; - - ifFile.seekg(static_cast(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(&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::IMAGE_ORDINAL_FLAG) - { - vOldIidCurr[i].originalfirstthunk[j].hint = 0; - continue; - } - - ifFile.seekg(static_cast(pehHeader.rvaToOffset(vOldIidCurr[i].originalfirstthunk[j].itd.Ordinal)), std::ios_base::beg); - - ifFile.read(reinterpret_cast(&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::IMAGE_ORDINAL_FLAG) - { - continue; - } - - ifFile.seekg(static_cast(pehHeader.rvaToOffset(vOldIidCurr[i].firstthunk[j].itd.Ordinal)), std::ios_base::beg); - - ifFile.read(reinterpret_cast(&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 - void ImportDirectory::rebuild(std::vector& vBuffer, dword dwRva, bool fixEntries) const - { - unsigned int uiImprva = dwRva; - unsigned int uiSizeofdescriptors = (static_cast(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[i].name.size()) + 1; - uiSizeofoft += (static_cast(m_vNewiid[i].originalfirstthunk.size())+1) * PELIB_IMAGE_THUNK_DATA::size(); - - for(unsigned int j=0;j(m_vNewiid[i].originalfirstthunk[j].fname.size()) + 3); - } - } - -// for (unsigned int i=0;i(m_vNewiid[i].originalfirstthunk.size())+1) * PELIB_IMAGE_THUNK_DATA::size(); -// } - - OutputBuffer obBuffer(vBuffer); - - // Rebuild IMAGE_IMPORT_DESCRIPTORS - for (unsigned int i=0;i(m_vNewiid[j-1].originalfirstthunk.size()) + 1) * PELIB_IMAGE_THUNK_DATA::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(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::IMAGE_ORDINAL_FLAG - || fixEntries == false) - { - obBuffer << m_vNewiid[i].originalfirstthunk[j].itd.Ordinal; - } - else - { - obBuffer << uiPfunc; - } - uiPfunc += static_cast(m_vNewiid[i].originalfirstthunk[j].fname.size()) + 3; - } - obBuffer << (dword)0; - } - - // Write dllnames into import directory - for (unsigned int i=0;i(m_vNewiid[i].name.size())+1); - } - - // Write function names into directory - for (unsigned int i=0;i(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 ImportDirectory::removeFile(const std::string& strFilename) - { - unsigned int oldSize = static_cast(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::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 ImportDirectory::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(viPos->originalfirstthunk.size()); - viPos->originalfirstthunk.erase(std::remove_if(viPos->originalfirstthunk.begin(), viPos->originalfirstthunk.end(), std::bind2nd(std::mem_fun_ref(&PELIB_THUNK_DATA::equalFunctionName), strFuncname)), viPos->originalfirstthunk.end()); - //viPos->originalfirstthunk.erase(std::remove_if(viPos->originalfirstthunk.begin(), viPos->originalfirstthunk.end(), std::bind2nd(CompPolicy(), 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 ImportDirectory::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(viPos->originalfirstthunk.size()); - viPos->originalfirstthunk.erase(std::remove_if(viPos->originalfirstthunk.begin(), viPos->originalfirstthunk.end(), std::bind2nd(std::mem_fun_ref(&PELIB_THUNK_DATA::equalHint), wHint)), viPos->originalfirstthunk.end()); - unsigned int newPos = static_cast(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 ImportDirectory::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 vBuffer; - - rebuild(vBuffer, uiRva); - - ofFile.write(reinterpret_cast(&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 - unsigned int ImportDirectory::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 >) - + (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 - dword ImportDirectory::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 - dword ImportDirectory::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 - dword ImportDirectory::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 - dword ImportDirectory::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 - dword ImportDirectory::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 - dword ImportDirectory::getFirstThunk(dword dwFilenr, currdir cdDir) const - { - if (cdDir == OLDDIR) - { - return m_vOldiid[dwFilenr].impdesc.FirstThunk; - } - else - { - return m_vNewiid[dwFilenr].impdesc.FirstThunk; - } - } - - template - void ImportDirectory::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 - dword ImportDirectory::getOriginalFirstThunk(dword dwFilenr, currdir cdDir) const - { - if (cdDir == OLDDIR) - { - return m_vOldiid[dwFilenr].impdesc.OriginalFirstThunk; - } - else - { - return m_vNewiid[dwFilenr].impdesc.OriginalFirstThunk; - } - } - - template - void ImportDirectory::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 - dword ImportDirectory::getForwarderChain(dword dwFilenr, currdir cdDir) const - { - if (cdDir == OLDDIR) - { - return m_vOldiid[dwFilenr].impdesc.ForwarderChain; - } - else - { - return m_vNewiid[dwFilenr].impdesc.ForwarderChain; - } - } - - template - void ImportDirectory::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 - dword ImportDirectory::getTimeDateStamp(dword dwFilenr, currdir cdDir) const - { - if (cdDir == OLDDIR) - { - return m_vOldiid[dwFilenr].impdesc.TimeDateStamp; - } - else - { - return m_vNewiid[dwFilenr].impdesc.TimeDateStamp; - } - } - - template - void ImportDirectory::setTimeDateStamp(dword dwFilenr, currdir cdDir, dword value) - { - if (cdDir == OLDDIR) - { - m_vOldiid[dwFilenr].impdesc.TimeDateStamp = value; - } - else - { - m_vNewiid[dwFilenr].impdesc.TimeDateStamp = value; - } - } - - template - dword ImportDirectory::getRvaOfName(dword dwFilenr, currdir cdDir) const - { - if (cdDir == OLDDIR) - { - return m_vOldiid[dwFilenr].impdesc.Name; - } - else - { - return m_vNewiid[dwFilenr].impdesc.Name; - } - } - - template - void ImportDirectory::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 - dword ImportDirectory::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 - void ImportDirectory::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 - dword ImportDirectory::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 - void ImportDirectory::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 + class ImportDirectory + { + typedef typename std::vector >::iterator ImpDirFileIterator; + typedef typename std::vector >::const_iterator ConstImpDirFileIterator; + + private: + /// Stores information about already imported DLLs. + std::vector > m_vOldiid; + /// Stores information about imported DLLs which will be added. + std::vector > 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 bool hasFunction(std::string strFilename, T value, bool(PELIB_THUNK_DATA::* 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::operator==), strFilename)); + + if (FileIter != EndIter) + { + typename std::vector >::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& pehHeader); // EXPORT + /// Rebuild the import directory. + void rebuild(std::vector& 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 ImportDirectory::addFunction(const std::string& strFilename, word wHint) + { + if (hasFunction(strFilename, wHint, &PELIB_THUNK_DATA::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::operator==), strFilename)); + + PELIB_IMAGE_IMPORT_DIRECTORY iid; + PELIB_THUNK_DATA td; + td.hint = wHint; + td.itd.Ordinal = wHint | PELIB_IMAGE_ORDINAL_FLAGS::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 ImportDirectory::addFunction(const std::string& strFilename, const std::string& strFuncname) + { + if (hasFunction(strFilename, strFuncname, &PELIB_THUNK_DATA::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::operator==), strFilename)); + + PELIB_IMAGE_IMPORT_DIRECTORY iid; + PELIB_THUNK_DATA 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 + unsigned int ImportDirectory::getFileIndex(const std::string& strFilename, currdir cdDir) const + { + const std::vector >* 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::operator==), strFilename)); + + if (FileIter != currDir->end()) + { + return static_cast(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 + unsigned int ImportDirectory::getFunctionIndex(const std::string& strFilename, const std::string& strFuncname, currdir cdDir) const + { + unsigned int uiFile = getFileIndex(strFilename, cdDir); + + for (unsigned int i=0;i + std::string ImportDirectory::getFileName(dword dwFilenr, currdir cdDir) const + { + if (cdDir == OLDDIR) return m_vOldiid[dwFilenr].name; + else return m_vNewiid[dwFilenr].name; + } + + template + void ImportDirectory::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 + std::string ImportDirectory::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 + void ImportDirectory::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 + word ImportDirectory::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 + void ImportDirectory::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 + dword ImportDirectory::getNumberOfFiles(currdir cdDir) const + { + if (cdDir == OLDDIR) return static_cast(m_vOldiid.size()); + else return static_cast(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 + dword ImportDirectory::getNumberOfFunctions(dword dwFilenr, currdir cdDir) const + { + if (cdDir == OLDDIR) return static_cast(m_vOldiid[dwFilenr].firstthunk.size()); + else return static_cast(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 ImportDirectory::read(const std::string& strFilename, unsigned int uiOffset, unsigned int uiSize, const PeHeaderT& 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 vImportdirectory(uiSize); + ifFile.read(reinterpret_cast(&vImportdirectory[0]), uiSize); + + PELIB_IMAGE_IMPORT_DIRECTORY iidCurr; + unsigned int uiDesccounter = 0; + + InputBuffer inpBuffer(vImportdirectory); + + std::vector > 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(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 tdCurr; + dword uiVaoft = vOldIidCurr[i].impdesc.OriginalFirstThunk; + + if (!uiVaoft) + { + continue; + } + + ifFile.seekg(static_cast(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(&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 tdCurr; + + ifFile.seekg(static_cast(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(&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::IMAGE_ORDINAL_FLAG) + { + vOldIidCurr[i].originalfirstthunk[j].hint = 0; + continue; + } + + ifFile.seekg(static_cast(pehHeader.rvaToOffset(vOldIidCurr[i].originalfirstthunk[j].itd.Ordinal)), std::ios_base::beg); + + ifFile.read(reinterpret_cast(&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::IMAGE_ORDINAL_FLAG) + { + continue; + } + + ifFile.seekg(static_cast(pehHeader.rvaToOffset(vOldIidCurr[i].firstthunk[j].itd.Ordinal)), std::ios_base::beg); + + ifFile.read(reinterpret_cast(&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 + void ImportDirectory::rebuild(std::vector& vBuffer, dword dwRva, bool fixEntries) const + { + unsigned int uiImprva = dwRva; + unsigned int uiSizeofdescriptors = (static_cast(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[i].name.size()) + 1; + uiSizeofoft += (static_cast(m_vNewiid[i].originalfirstthunk.size())+1) * PELIB_IMAGE_THUNK_DATA::size(); + + for(unsigned int j=0;j(m_vNewiid[i].originalfirstthunk[j].fname.size()) + 3); + } + } + +// for (unsigned int i=0;i(m_vNewiid[i].originalfirstthunk.size())+1) * PELIB_IMAGE_THUNK_DATA::size(); +// } + + OutputBuffer obBuffer(vBuffer); + + // Rebuild IMAGE_IMPORT_DESCRIPTORS + for (unsigned int i=0;i(m_vNewiid[j-1].originalfirstthunk.size()) + 1) * PELIB_IMAGE_THUNK_DATA::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(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::IMAGE_ORDINAL_FLAG + || fixEntries == false) + { + obBuffer << m_vNewiid[i].originalfirstthunk[j].itd.Ordinal; + } + else + { + obBuffer << uiPfunc; + } + uiPfunc += static_cast(m_vNewiid[i].originalfirstthunk[j].fname.size()) + 3; + } + obBuffer << (dword)0; + } + + // Write dllnames into import directory + for (unsigned int i=0;i(m_vNewiid[i].name.size())+1); + } + + // Write function names into directory + for (unsigned int i=0;i(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 ImportDirectory::removeFile(const std::string& strFilename) + { + unsigned int oldSize = static_cast(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::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 ImportDirectory::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(viPos->originalfirstthunk.size()); + viPos->originalfirstthunk.erase(std::remove_if(viPos->originalfirstthunk.begin(), viPos->originalfirstthunk.end(), std::bind2nd(std::mem_fun_ref(&PELIB_THUNK_DATA::equalFunctionName), strFuncname)), viPos->originalfirstthunk.end()); + //viPos->originalfirstthunk.erase(std::remove_if(viPos->originalfirstthunk.begin(), viPos->originalfirstthunk.end(), std::bind2nd(CompPolicy(), 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 ImportDirectory::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(viPos->originalfirstthunk.size()); + viPos->originalfirstthunk.erase(std::remove_if(viPos->originalfirstthunk.begin(), viPos->originalfirstthunk.end(), std::bind2nd(std::mem_fun_ref(&PELIB_THUNK_DATA::equalHint), wHint)), viPos->originalfirstthunk.end()); + unsigned int newPos = static_cast(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 ImportDirectory::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 vBuffer; + + rebuild(vBuffer, uiRva); + + ofFile.write(reinterpret_cast(&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 + unsigned int ImportDirectory::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 >) + + (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 + dword ImportDirectory::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 + dword ImportDirectory::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 + dword ImportDirectory::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 + dword ImportDirectory::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 + dword ImportDirectory::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 + dword ImportDirectory::getFirstThunk(dword dwFilenr, currdir cdDir) const + { + if (cdDir == OLDDIR) + { + return m_vOldiid[dwFilenr].impdesc.FirstThunk; + } + else + { + return m_vNewiid[dwFilenr].impdesc.FirstThunk; + } + } + + template + void ImportDirectory::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 + dword ImportDirectory::getOriginalFirstThunk(dword dwFilenr, currdir cdDir) const + { + if (cdDir == OLDDIR) + { + return m_vOldiid[dwFilenr].impdesc.OriginalFirstThunk; + } + else + { + return m_vNewiid[dwFilenr].impdesc.OriginalFirstThunk; + } + } + + template + void ImportDirectory::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 + dword ImportDirectory::getForwarderChain(dword dwFilenr, currdir cdDir) const + { + if (cdDir == OLDDIR) + { + return m_vOldiid[dwFilenr].impdesc.ForwarderChain; + } + else + { + return m_vNewiid[dwFilenr].impdesc.ForwarderChain; + } + } + + template + void ImportDirectory::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 + dword ImportDirectory::getTimeDateStamp(dword dwFilenr, currdir cdDir) const + { + if (cdDir == OLDDIR) + { + return m_vOldiid[dwFilenr].impdesc.TimeDateStamp; + } + else + { + return m_vNewiid[dwFilenr].impdesc.TimeDateStamp; + } + } + + template + void ImportDirectory::setTimeDateStamp(dword dwFilenr, currdir cdDir, dword value) + { + if (cdDir == OLDDIR) + { + m_vOldiid[dwFilenr].impdesc.TimeDateStamp = value; + } + else + { + m_vNewiid[dwFilenr].impdesc.TimeDateStamp = value; + } + } + + template + dword ImportDirectory::getRvaOfName(dword dwFilenr, currdir cdDir) const + { + if (cdDir == OLDDIR) + { + return m_vOldiid[dwFilenr].impdesc.Name; + } + else + { + return m_vNewiid[dwFilenr].impdesc.Name; + } + } + + template + void ImportDirectory::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 + dword ImportDirectory::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 + void ImportDirectory::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 + dword ImportDirectory::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 + void ImportDirectory::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 old mode 100755 new mode 100644 index 39fe54d80d..3a2119b7ac --- 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 - -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> m_idhHeader.e_res[i]; - } - - ibBuffer >> m_idhHeader.e_oemid; - ibBuffer >> m_idhHeader.e_oeminfo; - - for (unsigned int i=0;i> 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 vBuffer(PELIB_IMAGE_DOS_HEADER::size()); - ifFile.read(reinterpret_cast(&vBuffer[0]), static_cast(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 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& 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 vBuffer; - - rebuild(vBuffer); - - ofFile.write(reinterpret_cast(&vBuffer[0]), static_cast(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 + +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> m_idhHeader.e_res[i]; + } + + ibBuffer >> m_idhHeader.e_oemid; + ibBuffer >> m_idhHeader.e_oeminfo; + + for (unsigned int i=0;i> 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 vBuffer(PELIB_IMAGE_DOS_HEADER::size()); + ifFile.read(reinterpret_cast(&vBuffer[0]), static_cast(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 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& 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 vBuffer; + + rebuild(vBuffer); + + ofFile.write(reinterpret_cast(&vBuffer[0]), static_cast(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 old mode 100755 new mode 100644 index 5aca6bfe59..4acb819727 --- 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& 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& 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 old mode 100755 new mode 100644 index 39f2488b81..9e9f2d8691 --- 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 old mode 100755 new mode 100644 index a2b02cdfa8..e08d8da309 --- 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 - 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 - class PeFileT : public PeFile - { - typedef typename PeFile_Traits::PeHeader32_64 PeHeader32_64; - - private: - PeHeader32_64 m_peh; ///< PE header of the current file. - ImportDirectory m_impdir; ///< Import directory of the current file. - TlsDirectory 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& impDir() const; - /// Accessor function for the import directory. - ImportDirectory& impDir(); - - const TlsDirectory& tlsDir() const; - TlsDirectory& 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 - PeFileT::PeFileT(const std::string& strFilename) - { - m_filename = strFilename; - } - - template - PeFileT::PeFileT() - { - } - - template - int PeFileT::readPeHeader() - { - return peHeader().read(getFileName(), mzHeader().getAddressOfPeHeader()); - } - - - template - int PeFileT::readImportDirectory() - { - if (peHeader().calcNumberOfRvaAndSizes() >= 2 - && peHeader().getIddImportRva() - && peHeader().getIddImportSize()) - { - return impDir().read(getFileName(), static_cast(peHeader().rvaToOffset(peHeader().getIddImportRva())), peHeader().getIddImportSize(), peHeader()); - } - return ERROR_DIRECTORY_DOES_NOT_EXIST; - } - - /** - * @return A reference to the file's PE header. - **/ - template - const typename PeFile_Traits::PeHeader32_64& PeFileT::peHeader() const - { - return m_peh; - } - - /** - * @return A reference to the file's PE header. - **/ - template - typename PeFile_Traits::PeHeader32_64& PeFileT::peHeader() - { - return m_peh; - } - - /** - * @return A reference to the file's import directory. - **/ - template - const ImportDirectory& PeFileT::impDir() const - { - return m_impdir; - } - - /** - * @return A reference to the file's import directory. - **/ - template - ImportDirectory& PeFileT::impDir() - { - return m_impdir; - } - - template - const TlsDirectory& PeFileT::tlsDir() const - { - return m_tlsdir; - } - - template - TlsDirectory& PeFileT::tlsDir() - { - return m_tlsdir; - } - - /** - * @return Filename of the current file. - **/ - template - std::string PeFileT::getFileName() const - { - return m_filename; - } - - /** - * @param strFilename New filename. - **/ - template - void PeFileT::setFileName(std::string strFilename) - { - m_filename = strFilename; - } - - template - int PeFileT::readMzHeader() - { - return mzHeader().read(getFileName()); - } - - template - int PeFileT::readExportDirectory() - { - if (peHeader().calcNumberOfRvaAndSizes() >= 1 - && peHeader().getIddExportRva() && peHeader().getIddExportSize()) - { - return expDir().read(getFileName(), static_cast(peHeader().rvaToOffset(peHeader().getIddExportRva())), peHeader().getIddExportSize(), peHeader()); - } - return ERROR_DIRECTORY_DOES_NOT_EXIST; - } - - - template - int PeFileT::readBoundImportDirectory() - { - if (peHeader().calcNumberOfRvaAndSizes() >= 12 - && peHeader().getIddBoundImportRva() && peHeader().getIddBoundImportSize()) - { - return boundImpDir().read(getFileName(), static_cast(peHeader().rvaToOffset(peHeader().getIddBoundImportRva())), peHeader().getIddBoundImportSize()); - } - return ERROR_DIRECTORY_DOES_NOT_EXIST; - } - - template - int PeFileT::readResourceDirectory() - { - if (peHeader().calcNumberOfRvaAndSizes() >= 3 - && peHeader().getIddResourceRva() && peHeader().getIddResourceSize()) - { - return resDir().read(getFileName(), static_cast(peHeader().rvaToOffset(peHeader().getIddResourceRva())), peHeader().getIddResourceSize(), peHeader().getIddResourceRva()); - } - return ERROR_DIRECTORY_DOES_NOT_EXIST; - } - - template - int PeFileT::readRelocationsDirectory() - { - if (peHeader().calcNumberOfRvaAndSizes() >= 6 - && peHeader().getIddBaseRelocRva() && peHeader().getIddBaseRelocSize()) - { - return relocDir().read(getFileName(), static_cast(peHeader().rvaToOffset(peHeader().getIddBaseRelocRva())), peHeader().getIddBaseRelocSize()); - } - return ERROR_DIRECTORY_DOES_NOT_EXIST; - } - - template - int PeFileT::readComHeaderDirectory() - { - if (peHeader().calcNumberOfRvaAndSizes() >= 15 - && peHeader().getIddComHeaderRva() && peHeader().getIddComHeaderSize()) - { - return comDir().read(getFileName(), static_cast(peHeader().rvaToOffset(peHeader().getIddComHeaderRva())), peHeader().getIddComHeaderSize()); - } - std::cout << peHeader().getIddComHeaderRva() << std::endl; - std::exit(0); - return ERROR_DIRECTORY_DOES_NOT_EXIST; - } - - template - int PeFileT::readIatDirectory() - { - if (peHeader().calcNumberOfRvaAndSizes() >= 13 - && peHeader().getIddIatRva() && peHeader().getIddIatSize()) - { - return iatDir().read(getFileName(), static_cast(peHeader().rvaToOffset(peHeader().getIddIatRva())), peHeader().getIddIatSize()); - } - return ERROR_DIRECTORY_DOES_NOT_EXIST; - } - - template - int PeFileT::readDebugDirectory() - { - if (peHeader().calcNumberOfRvaAndSizes() >= 7 - && peHeader().getIddDebugRva() && peHeader().getIddDebugSize()) - { - return debugDir().read(getFileName(), static_cast(peHeader().rvaToOffset(peHeader().getIddDebugRva())), peHeader().getIddDebugSize()); - } - return ERROR_DIRECTORY_DOES_NOT_EXIST; - } - - template - int PeFileT::readTlsDirectory() - { - if (peHeader().calcNumberOfRvaAndSizes() >= 10 - && peHeader().getIddTlsRva() && peHeader().getIddTlsSize()) - { - return tlsDir().read(getFileName(), static_cast(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 + 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 + class PeFileT : public PeFile + { + typedef typename PeFile_Traits::PeHeader32_64 PeHeader32_64; + + private: + PeHeader32_64 m_peh; ///< PE header of the current file. + ImportDirectory m_impdir; ///< Import directory of the current file. + TlsDirectory 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& impDir() const; + /// Accessor function for the import directory. + ImportDirectory& impDir(); + + const TlsDirectory& tlsDir() const; + TlsDirectory& 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 + PeFileT::PeFileT(const std::string& strFilename) + { + m_filename = strFilename; + } + + template + PeFileT::PeFileT() + { + } + + template + int PeFileT::readPeHeader() + { + return peHeader().read(getFileName(), mzHeader().getAddressOfPeHeader()); + } + + + template + int PeFileT::readImportDirectory() + { + if (peHeader().calcNumberOfRvaAndSizes() >= 2 + && peHeader().getIddImportRva() + && peHeader().getIddImportSize()) + { + return impDir().read(getFileName(), static_cast(peHeader().rvaToOffset(peHeader().getIddImportRva())), peHeader().getIddImportSize(), peHeader()); + } + return ERROR_DIRECTORY_DOES_NOT_EXIST; + } + + /** + * @return A reference to the file's PE header. + **/ + template + const typename PeFile_Traits::PeHeader32_64& PeFileT::peHeader() const + { + return m_peh; + } + + /** + * @return A reference to the file's PE header. + **/ + template + typename PeFile_Traits::PeHeader32_64& PeFileT::peHeader() + { + return m_peh; + } + + /** + * @return A reference to the file's import directory. + **/ + template + const ImportDirectory& PeFileT::impDir() const + { + return m_impdir; + } + + /** + * @return A reference to the file's import directory. + **/ + template + ImportDirectory& PeFileT::impDir() + { + return m_impdir; + } + + template + const TlsDirectory& PeFileT::tlsDir() const + { + return m_tlsdir; + } + + template + TlsDirectory& PeFileT::tlsDir() + { + return m_tlsdir; + } + + /** + * @return Filename of the current file. + **/ + template + std::string PeFileT::getFileName() const + { + return m_filename; + } + + /** + * @param strFilename New filename. + **/ + template + void PeFileT::setFileName(std::string strFilename) + { + m_filename = strFilename; + } + + template + int PeFileT::readMzHeader() + { + return mzHeader().read(getFileName()); + } + + template + int PeFileT::readExportDirectory() + { + if (peHeader().calcNumberOfRvaAndSizes() >= 1 + && peHeader().getIddExportRva() && peHeader().getIddExportSize()) + { + return expDir().read(getFileName(), static_cast(peHeader().rvaToOffset(peHeader().getIddExportRva())), peHeader().getIddExportSize(), peHeader()); + } + return ERROR_DIRECTORY_DOES_NOT_EXIST; + } + + + template + int PeFileT::readBoundImportDirectory() + { + if (peHeader().calcNumberOfRvaAndSizes() >= 12 + && peHeader().getIddBoundImportRva() && peHeader().getIddBoundImportSize()) + { + return boundImpDir().read(getFileName(), static_cast(peHeader().rvaToOffset(peHeader().getIddBoundImportRva())), peHeader().getIddBoundImportSize()); + } + return ERROR_DIRECTORY_DOES_NOT_EXIST; + } + + template + int PeFileT::readResourceDirectory() + { + if (peHeader().calcNumberOfRvaAndSizes() >= 3 + && peHeader().getIddResourceRva() && peHeader().getIddResourceSize()) + { + return resDir().read(getFileName(), static_cast(peHeader().rvaToOffset(peHeader().getIddResourceRva())), peHeader().getIddResourceSize(), peHeader().getIddResourceRva()); + } + return ERROR_DIRECTORY_DOES_NOT_EXIST; + } + + template + int PeFileT::readRelocationsDirectory() + { + if (peHeader().calcNumberOfRvaAndSizes() >= 6 + && peHeader().getIddBaseRelocRva() && peHeader().getIddBaseRelocSize()) + { + return relocDir().read(getFileName(), static_cast(peHeader().rvaToOffset(peHeader().getIddBaseRelocRva())), peHeader().getIddBaseRelocSize()); + } + return ERROR_DIRECTORY_DOES_NOT_EXIST; + } + + template + int PeFileT::readComHeaderDirectory() + { + if (peHeader().calcNumberOfRvaAndSizes() >= 15 + && peHeader().getIddComHeaderRva() && peHeader().getIddComHeaderSize()) + { + return comDir().read(getFileName(), static_cast(peHeader().rvaToOffset(peHeader().getIddComHeaderRva())), peHeader().getIddComHeaderSize()); + } + std::cout << peHeader().getIddComHeaderRva() << std::endl; + std::exit(0); + return ERROR_DIRECTORY_DOES_NOT_EXIST; + } + + template + int PeFileT::readIatDirectory() + { + if (peHeader().calcNumberOfRvaAndSizes() >= 13 + && peHeader().getIddIatRva() && peHeader().getIddIatSize()) + { + return iatDir().read(getFileName(), static_cast(peHeader().rvaToOffset(peHeader().getIddIatRva())), peHeader().getIddIatSize()); + } + return ERROR_DIRECTORY_DOES_NOT_EXIST; + } + + template + int PeFileT::readDebugDirectory() + { + if (peHeader().calcNumberOfRvaAndSizes() >= 7 + && peHeader().getIddDebugRva() && peHeader().getIddDebugSize()) + { + return debugDir().read(getFileName(), static_cast(peHeader().rvaToOffset(peHeader().getIddDebugRva())), peHeader().getIddDebugSize()); + } + return ERROR_DIRECTORY_DOES_NOT_EXIST; + } + + template + int PeFileT::readTlsDirectory() + { + if (peHeader().calcNumberOfRvaAndSizes() >= 10 + && peHeader().getIddTlsRva() && peHeader().getIddTlsSize()) + { + return tlsDir().read(getFileName(), static_cast(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 old mode 100755 new mode 100644 index fe7011072c..1199e6bdd1 --- 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 old mode 100755 new mode 100644 index 08eaca4072..d5c1748233 --- 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 - class PeHeaderT : public PeHeader - { - private: - void readBaseOfData(InputBuffer& ibBuffer, PELIB_IMAGE_NT_HEADERS& header) const; - void rebuildBaseOfData(OutputBuffer& obBuffer) const; - - protected: - std::vector m_vIsh; ///< Stores section header information. - PELIB_IMAGE_NT_HEADERS m_inthHeader; ///< Stores Nt header information. - dword m_uiOffset; ///< Equivalent to the value returned by #PeLib::MzHeader::getAddressOfPeFile - - public: - typedef typename FieldSizes::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& header) const; - void readDataDirectories(InputBuffer& ibBuffer, PELIB_IMAGE_NT_HEADERS& header) const; - std::vector readSections(InputBuffer& ibBuffer, PELIB_IMAGE_NT_HEADERS& header) const; - - /// Rebuilds the current PE header. - void rebuild(std::vector& 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& 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 - void PeHeaderT::addDataDirectory() - { - m_inthHeader.dataDirectories.push_back(PELIB_IMAGE_DATA_DIRECTORY()); - } - - template - void PeHeaderT::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 PeHeaderT::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 - unsigned int PeHeaderT::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::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 - unsigned int PeHeaderT::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 - unsigned int PeHeaderT::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 - unsigned int PeHeaderT::calcOffset(/*unsigned int uiSize*/) const - { - unsigned int maxoffset = size(); - - for (word i=0;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 - unsigned int PeHeaderT::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 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::getNumberOfSections). - * @return Number of currently defined sections. - **/ - template - word PeHeaderT::calcNumberOfSections() const - { - return static_cast(m_vIsh.size()); - } - - /** - * Enlarges the physically last section in the file. - * @param uiSize Additional size that's added to the section's size. - **/ - template - void PeHeaderT::enlargeLastSection(unsigned int uiSize) - { - std::vector::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 - word PeHeaderT::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::max(); - - for (word i=0;i dwOffset) return i; - } - - return std::numeric_limits::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 - word PeHeaderT::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= 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 - void PeHeaderT::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::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 - unsigned int PeHeaderT::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 - unsigned int PeHeaderT::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 - void PeHeaderT::readHeader(InputBuffer& ibBuffer, PELIB_IMAGE_NT_HEADERS& 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 - void PeHeaderT::readDataDirectories(InputBuffer& ibBuffer, PELIB_IMAGE_NT_HEADERS& header) const - { - PELIB_IMAGE_DATA_DIRECTORY idd; - - for (unsigned int i=0;i> idd.VirtualAddress; - ibBuffer >> idd.Size; - header.dataDirectories.push_back(idd); - } - } - - template - std::vector PeHeaderT::readSections(InputBuffer& ibBuffer, PELIB_IMAGE_NT_HEADERS& header) const - { - const unsigned int nrSections = header.FileHeader.NumberOfSections; - PELIB_IMAGE_SECTION_HEADER ishCurr; - - std::vector vIshdCurr; - - for (unsigned int i=0;i(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 PeHeaderT::read(const unsigned char* pcBuffer, unsigned int uiSize, unsigned int uiOffset) - { - if (uiSize < m_inthHeader.size()) - { - return ERROR_INVALID_FILE; - } - - std::vector vBuffer(pcBuffer, pcBuffer + m_inthHeader.size()); - - InputBuffer ibBuffer(vBuffer); - PELIB_IMAGE_NT_HEADERS 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::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 PeHeaderT::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 vBuffer(m_inthHeader.size()); - - ifFile.seekg(uiOffset, std::ios::beg); - ifFile.read(reinterpret_cast(&vBuffer[0]), static_cast(vBuffer.size())); - - InputBuffer ibBuffer(vBuffer); - PELIB_IMAGE_NT_HEADERS header; - - readHeader(ibBuffer, header); - - vBuffer.resize(header.OptionalHeader.NumberOfRvaAndSizes * 8 + header.FileHeader.NumberOfSections * 0x28); - - ifFile.read(reinterpret_cast(&vBuffer[0]), static_cast(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::makeValid first. - * @param vBuffer Buffer where the rebuilt header will be stored. - **/ - template - void PeHeaderT::rebuild(std::vector& 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 - typename FieldSizes::VAR4_8 PeHeaderT::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::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 - typename FieldSizes::VAR4_8 PeHeaderT::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 - unsigned int PeHeaderT::size() const - { - return m_inthHeader.size() + getNumberOfSections() * PELIB_IMAGE_SECTION_HEADER::size(); - } - - // \todo Not sure if this works. - template - typename FieldSizes::VAR4_8 PeHeaderT::vaToRva(VAR4_8 dwRva) const - { - if (dwRva - getImageBase() < calcStartOfCode()) return dwRva - getImageBase(); - - if (getSectionWithRva(dwRva - getImageBase()) == 0xFFFF) return -1; - - return dwRva - getImageBase(); - } - - template - typename FieldSizes::VAR4_8 PeHeaderT::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::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::isValid and #PeLib::PeHeaderT::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 PeHeaderT::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 vBuffer; - - rebuild(vBuffer); - - ofFile.write(reinterpret_cast(&vBuffer[0]), static_cast(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 PeHeaderT::writeSectionData(const std::string& strFilename, word wSecnr, const std::vector& 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(&vBuffer[0]), std::min(static_cast(vBuffer.size()), getSizeOfRawData(wSecnr))); - - ofFile.close(); - - return NO_ERROR; - } - - template - int PeHeaderT::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 vBuffer(uiToWrite); - ofFile.seekp(0, std::ios::end); - ofFile.write(&vBuffer[0], static_cast(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 - dword PeHeaderT::getNtSignature() const - { - return m_inthHeader.Signature; - } - - /** - * Returns the file's machine. - * @return The Machine value from the PE header. - **/ - template - word PeHeaderT::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::getNumberOfSections). - * @return The NumberOfSections value from the PE header. - **/ - template - word PeHeaderT::getNumberOfSections() const - { - return m_inthHeader.FileHeader.NumberOfSections; - } - - /** - * Returns the file's TimeDateStamp. - * @return The TimeDateStamp value from the PE header. - **/ - template - dword PeHeaderT::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 - dword PeHeaderT::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 - dword PeHeaderT::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 - word PeHeaderT::getSizeOfOptionalHeader() const - { - return m_inthHeader.FileHeader.SizeOfOptionalHeader; - } - - /** - * @return The Characteristics value from the PE header. - **/ - template - word PeHeaderT::getCharacteristics() const - { - return m_inthHeader.FileHeader.Characteristics; - } - - /** - * @return The Magic value from the PE header. - **/ - template - word PeHeaderT::getMagic() const - { - return m_inthHeader.OptionalHeader.Magic; - } - - /** - * @return The MajorLinkerVersion value from the PE header. - **/ - template - byte PeHeaderT::getMajorLinkerVersion() const - { - return m_inthHeader.OptionalHeader.MajorLinkerVersion; - } - - /** - * @return The MinorLinkerVersion value from the PE header. - **/ - template - byte PeHeaderT::getMinorLinkerVersion() const - { - return m_inthHeader.OptionalHeader.MinorLinkerVersion; - } - - /** - * @return The SizeOfCode value from the PE header. - **/ - template - dword PeHeaderT::getSizeOfCode() const - { - return m_inthHeader.OptionalHeader.SizeOfCode; - } - - /** - * @return The SizeOfInitializedData value from the PE header. - **/ - template - dword PeHeaderT::getSizeOfInitializedData() const - { - return m_inthHeader.OptionalHeader.SizeOfInitializedData; - } - - /** - * @return The SizeOfUninitializedData value from the PE header. - **/ - template - dword PeHeaderT::getSizeOfUninitializedData() const - { - return m_inthHeader.OptionalHeader.SizeOfUninitializedData; - } - - /** - * @return The AddressOfEntryPoint value from the PE header. - **/ - template - dword PeHeaderT::getAddressOfEntryPoint() const - { - return m_inthHeader.OptionalHeader.AddressOfEntryPoint; - } - - /** - * @return The BaseOfCode value from the PE header. - **/ - template - dword PeHeaderT::getBaseOfCode() const - { - return m_inthHeader.OptionalHeader.BaseOfCode; - } - - /** - * @return The ImageBase value from the PE header. - **/ - template - typename FieldSizes::VAR4_8 PeHeaderT::getImageBase() const - { - return m_inthHeader.OptionalHeader.ImageBase; - } - - /** - * @return The SectionAlignment value from the PE header. - **/ - template - dword PeHeaderT::getSectionAlignment() const - { - return m_inthHeader.OptionalHeader.SectionAlignment; - } - - /** - * @return The FileAlignment value from the PE header. - **/ - template - dword PeHeaderT::getFileAlignment() const - { - return m_inthHeader.OptionalHeader.FileAlignment; - } - - /** - * @return The MajorOperatingSystemVersion value from the PE header. - **/ - template - word PeHeaderT::getMajorOperatingSystemVersion() const - { - return m_inthHeader.OptionalHeader.MajorOperatingSystemVersion; - } - - /** - * @return The MinorOperatingSystemVersion value from the PE header. - **/ - template - word PeHeaderT::getMinorOperatingSystemVersion() const - { - return m_inthHeader.OptionalHeader.MinorOperatingSystemVersion; - } - - /** - * @return The MajorImageVersion value from the PE header. - **/ - template - word PeHeaderT::getMajorImageVersion() const - { - return m_inthHeader.OptionalHeader.MajorImageVersion; - } - - /** - * @return The MinorImageVersion value from the PE header. - **/ - template - word PeHeaderT::getMinorImageVersion() const - { - return m_inthHeader.OptionalHeader.MinorImageVersion; - } - - /** - * @return The MajorSubsystemVersion value from the PE header. - **/ - template - word PeHeaderT::getMajorSubsystemVersion() const - { - return m_inthHeader.OptionalHeader.MajorSubsystemVersion; - } - - /** - * @return The MinorSubsystemVersion value from the PE header. - **/ - template - word PeHeaderT::getMinorSubsystemVersion() const - { - return m_inthHeader.OptionalHeader.MinorSubsystemVersion; - } - - /** - * @return The WinVersionValue value from the PE header. - **/ - template - dword PeHeaderT::getWin32VersionValue() const - { - return m_inthHeader.OptionalHeader.Win32VersionValue; - } - - /** - * @return The SizeOfImage value from the PE header. - **/ - template - dword PeHeaderT::getSizeOfImage() const - { - return m_inthHeader.OptionalHeader.SizeOfImage; - } - - /** - * @return The SizeOfHeaders value from the PE header. - **/ - template - dword PeHeaderT::getSizeOfHeaders() const - { - return m_inthHeader.OptionalHeader.SizeOfHeaders; - } - - /** - * @return The CheckSums value from the PE header. - **/ - template - dword PeHeaderT::getCheckSum() const - { - return m_inthHeader.OptionalHeader.CheckSum; - } - - /** - * @return The Subsystem value from the PE header. - **/ - template - word PeHeaderT::getSubsystem() const - { - return m_inthHeader.OptionalHeader.Subsystem; - } - - /** - * @return The DllCharacteristics value from the PE header. - **/ - template - word PeHeaderT::getDllCharacteristics() const - { - return m_inthHeader.OptionalHeader.DllCharacteristics; - } - - /** - * @return The SizeOfStackReserve value from the PE header. - **/ - template - typename FieldSizes::VAR4_8 PeHeaderT::getSizeOfStackReserve() const - { - return m_inthHeader.OptionalHeader.SizeOfStackReserve; - } - - /** - * @return The SizeOfStackCommit value from the PE header. - **/ - template - typename FieldSizes::VAR4_8 PeHeaderT::getSizeOfStackCommit() const - { - return m_inthHeader.OptionalHeader.SizeOfStackCommit; - } - - /** - * @return The SizeOfHeapReserve value from the PE header. - **/ - template - typename FieldSizes::VAR4_8 PeHeaderT::getSizeOfHeapReserve() const - { - return m_inthHeader.OptionalHeader.SizeOfHeapReserve; - } - - /** - * @return The SizeOfHeapCommit value from the PE header. - **/ - template - typename FieldSizes::VAR4_8 PeHeaderT::getSizeOfHeapCommit() const - { - return m_inthHeader.OptionalHeader.SizeOfHeapCommit; - } - - /** - * @return The LoaderFlags value from the PE header. - **/ - template - dword PeHeaderT::getLoaderFlags() const - { - return m_inthHeader.OptionalHeader.LoaderFlags; - } - - /** - * @return The NumberOfRvaAndSizes value from the PE header. - **/ - template - dword PeHeaderT::getNumberOfRvaAndSizes() const - { - return m_inthHeader.OptionalHeader.NumberOfRvaAndSizes; - } - - template - dword PeHeaderT::calcNumberOfRvaAndSizes() const - { - return static_cast(m_inthHeader.dataDirectories.size()); - } - - /** - * Returns the relative virtual address of the current file's export directory. - * @return The Rva of the Export directory. - **/ - template - dword PeHeaderT::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 - dword PeHeaderT::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 - dword PeHeaderT::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 - dword PeHeaderT::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 - dword PeHeaderT::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 - dword PeHeaderT::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 - dword PeHeaderT::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 - dword PeHeaderT::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 - dword PeHeaderT::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 - dword PeHeaderT::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 - dword PeHeaderT::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 - dword PeHeaderT::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 - dword PeHeaderT::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 - dword PeHeaderT::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 - dword PeHeaderT::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 - dword PeHeaderT::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 - dword PeHeaderT::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 - dword PeHeaderT::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 - dword PeHeaderT::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 - dword PeHeaderT::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 - dword PeHeaderT::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 - dword PeHeaderT::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 - dword PeHeaderT::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 - dword PeHeaderT::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 - dword PeHeaderT::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 - dword PeHeaderT::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 - dword PeHeaderT::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 - dword PeHeaderT::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 - dword PeHeaderT::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 - dword PeHeaderT::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 - dword PeHeaderT::getImageDataDirectoryRva(dword dwDirectory) const - { - return m_inthHeader.dataDirectories[dwDirectory].VirtualAddress; - } - - template - void PeHeaderT::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 - dword PeHeaderT::getImageDataDirectorySize(dword dwDirectory) const - { - return m_inthHeader.dataDirectories[dwDirectory].Size; - } - - template - void PeHeaderT::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 - std::string PeHeaderT::getSectionName(word wSectionnr) const - { - std::string sectionName = ""; - - for (unsigned int i=0;i - dword PeHeaderT::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 - dword PeHeaderT::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 - dword PeHeaderT::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 - dword PeHeaderT::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 - dword PeHeaderT::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 - dword PeHeaderT::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 - dword PeHeaderT::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 - dword PeHeaderT::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 - dword PeHeaderT::getCharacteristics(word wSectionnr) const - { - return m_vIsh[wSectionnr].Characteristics; - } - - /** - * Changes the file's Nt signature. - * @param dwValue New value. - **/ - template - void PeHeaderT::setNtSignature(dword dwValue) - { - m_inthHeader.Signature = dwValue; - } - - /** - * Changes the file's Machine. - * @param wValue New value. - **/ - template - void PeHeaderT::setMachine(word wValue) - { - m_inthHeader.FileHeader.Machine = wValue; - } - - /** - * Changes the number of sections. - * @param wValue New value. - **/ - template - void PeHeaderT::setNumberOfSections(word wValue) - { - m_inthHeader.FileHeader.NumberOfSections = wValue; - } - - /** - * Changes the file's TimeDateStamp. - * @param dwValue New value. - **/ - template - void PeHeaderT::setTimeDateStamp(dword dwValue) - { - m_inthHeader.FileHeader.TimeDateStamp = dwValue; - } - - /** - * Changes the file's PointerToSymbolTable. - * @param dwValue New value. - **/ - template - void PeHeaderT::setPointerToSymbolTable(dword dwValue) - { - m_inthHeader.FileHeader.PointerToSymbolTable = dwValue; - } - - /** - * Changes the file's NumberOfSymbols. - * @param dwValue New value. - **/ - template - void PeHeaderT::setNumberOfSymbols(dword dwValue) - { - m_inthHeader.FileHeader.NumberOfSymbols = dwValue; - } - - /** - * Changes the file's SizeOfOptionalHeader. - * @param wValue New value. - **/ - template - void PeHeaderT::setSizeOfOptionalHeader(word wValue) - { - m_inthHeader.FileHeader.SizeOfOptionalHeader = wValue; - } - - /** - * Changes the file's Characteristics. - * @param wValue New value. - **/ - template - void PeHeaderT::setCharacteristics(word wValue) - { - m_inthHeader.FileHeader.Characteristics = wValue; - } - - /** - * Changes the file's Magic. - * @param wValue New value. - **/ - template - void PeHeaderT::setMagic(word wValue) - { - m_inthHeader.OptionalHeader.Magic = wValue; - } - - /** - * Changes the file's MajorLinkerVersion. - * @param bValue New value. - **/ - template - void PeHeaderT::setMajorLinkerVersion(byte bValue) - { - m_inthHeader.OptionalHeader.MajorLinkerVersion = bValue; - } - - /** - * Changes the file's MinorLinkerVersion. - * @param bValue New value. - **/ - template - void PeHeaderT::setMinorLinkerVersion(byte bValue) - { - m_inthHeader.OptionalHeader.MinorLinkerVersion = bValue; - } - - /** - * Changes the file's SizeOfCode. - * @param dwValue New value. - **/ - template - void PeHeaderT::setSizeOfCode(dword dwValue) - { - m_inthHeader.OptionalHeader.SizeOfCode = dwValue; - } - - /** - * Changes the file's SizeOfInitializedData. - * @param dwValue New value. - **/ - template - void PeHeaderT::setSizeOfInitializedData(dword dwValue) - { - m_inthHeader.OptionalHeader.SizeOfInitializedData = dwValue; - } - - /** - * Changes the file's SizeOfUninitializedData. - * @param dwValue New value. - **/ - template - void PeHeaderT::setSizeOfUninitializedData(dword dwValue) - { - m_inthHeader.OptionalHeader.SizeOfUninitializedData = dwValue; - } - - /** - * Changes the file's AddressOfEntryPoint. - * @param dwValue New value. - **/ - template - void PeHeaderT::setAddressOfEntryPoint(dword dwValue) - { - m_inthHeader.OptionalHeader.AddressOfEntryPoint = dwValue; - } - - /** - * Changes the file's BaseOfCode. - * @param dwValue New value. - **/ - template - void PeHeaderT::setBaseOfCode(dword dwValue) - { - m_inthHeader.OptionalHeader.BaseOfCode = dwValue; - } - - /** - * Changes the file's ImageBase. - * @param dwValue New value. - **/ - template - void PeHeaderT::setImageBase(typename FieldSizes::VAR4_8 dwValue) - { - m_inthHeader.OptionalHeader.ImageBase = dwValue; - } - - /** - * Changes the file's SectionAlignment. - * @param dwValue New value. - **/ - template - void PeHeaderT::setSectionAlignment(dword dwValue) - { - m_inthHeader.OptionalHeader.SectionAlignment = dwValue; - } - - /** - * Changes the file's FileAlignment. - * @param dwValue New value. - **/ - template - void PeHeaderT::setFileAlignment(dword dwValue) - { - m_inthHeader.OptionalHeader.FileAlignment = dwValue; - } - - /** - * Changes the file's MajorOperatingSystemVersion. - * @param wValue New value. - **/ - template - void PeHeaderT::setMajorOperatingSystemVersion(word wValue) - { - m_inthHeader.OptionalHeader.MajorOperatingSystemVersion = wValue; - } - - /** - * Changes the file's MinorOperatingSystemVersion. - * @param wValue New value. - **/ - template - void PeHeaderT::setMinorOperatingSystemVersion(word wValue) - { - m_inthHeader.OptionalHeader.MinorOperatingSystemVersion = wValue; - } - - /** - * Changes the file's MajorImageVersion. - * @param wValue New value. - **/ - template - void PeHeaderT::setMajorImageVersion(word wValue) - { - m_inthHeader.OptionalHeader.MajorImageVersion = wValue; - } - - /** - * Changes the file's MinorImageVersion. - * @param wValue New value. - **/ - template - void PeHeaderT::setMinorImageVersion(word wValue) - { - m_inthHeader.OptionalHeader.MinorImageVersion = wValue; - } - - /** - * Changes the file's MajorSubsystemVersion. - * @param wValue New value. - **/ - template - void PeHeaderT::setMajorSubsystemVersion(word wValue) - { - m_inthHeader.OptionalHeader.MajorSubsystemVersion = wValue; - } - - /** - * Changes the file's MinorSubsystemVersion. - * @param wValue New value. - **/ - template - void PeHeaderT::setMinorSubsystemVersion(word wValue) - { - m_inthHeader.OptionalHeader.MinorSubsystemVersion = wValue; - } - - /** - * Changes the file's Win32VersionValue. - * @param dwValue New value. - **/ - template - void PeHeaderT::setWin32VersionValue(dword dwValue) - { - m_inthHeader.OptionalHeader.Win32VersionValue = dwValue; - } - - /** - * Changes the file's SizeOfImage. - * @param dwValue New value. - **/ - template - void PeHeaderT::setSizeOfImage(dword dwValue) - { - m_inthHeader.OptionalHeader.SizeOfImage = dwValue; - } - - /** - * Changes the file's SizeOfHeaders. - * @param dwValue New value. - **/ - template - void PeHeaderT::setSizeOfHeaders(dword dwValue) - { - m_inthHeader.OptionalHeader.SizeOfHeaders = dwValue; - } - - /** - * Changes the file's CheckSum. - * @param dwValue New value. - **/ - template - void PeHeaderT::setCheckSum(dword dwValue) - { - m_inthHeader.OptionalHeader.CheckSum = dwValue; - } - - /** - * Changes the file's Subsystem. - * @param wValue New value. - **/ - template - void PeHeaderT::setSubsystem(word wValue) - { - m_inthHeader.OptionalHeader.Subsystem = wValue; - } - - /** - * Changes the file's DllCharacteristics. - * @param wValue New value. - **/ - template - void PeHeaderT::setDllCharacteristics(word wValue) - { - m_inthHeader.OptionalHeader.DllCharacteristics = wValue; - } - - /** - * Changes the file's SizeOfStackReserve. - * @param dwValue New value. - **/ - template - void PeHeaderT::setSizeOfStackReserve(typename FieldSizes::VAR4_8 dwValue) - { - m_inthHeader.OptionalHeader.SizeOfStackReserve = dwValue; - } - - /** - * Changes the file's SizeOfStackCommit. - * @param dwValue New value. - **/ - template - void PeHeaderT::setSizeOfStackCommit(typename FieldSizes::VAR4_8 dwValue) - { - m_inthHeader.OptionalHeader.SizeOfStackCommit = dwValue; - } - - /** - * Changes the file's SizeOfHeapReserve. - * @param dwValue New value. - **/ - template - void PeHeaderT::setSizeOfHeapReserve(typename FieldSizes::VAR4_8 dwValue) - { - m_inthHeader.OptionalHeader.SizeOfHeapReserve = dwValue; - } - - /** - * Changes the file's SizeOfHeapCommit. - * @param dwValue New value. - **/ - template - void PeHeaderT::setSizeOfHeapCommit(typename FieldSizes::VAR4_8 dwValue) - { - m_inthHeader.OptionalHeader.SizeOfHeapCommit = dwValue; - } - - /** - * Changes the file's LoaderFlags. - * @param dwValue New value. - **/ - template - void PeHeaderT::setLoaderFlags(dword dwValue) - { - m_inthHeader.OptionalHeader.LoaderFlags = dwValue; - } - - /** - * Changes the file's NumberOfRvaAndSizes. - * @param dwValue New value. - **/ - template - void PeHeaderT::setNumberOfRvaAndSizes(dword dwValue) - { - m_inthHeader.OptionalHeader.NumberOfRvaAndSizes = dwValue; - } - - template - void PeHeaderT::setIddDebugRva(dword dwValue) - { - m_inthHeader.dataDirectories[PELIB_IMAGE_DIRECTORY_ENTRY_DEBUG].VirtualAddress = dwValue; - } - - template - void PeHeaderT::setIddDebugSize(dword dwValue) - { - m_inthHeader.dataDirectories[PELIB_IMAGE_DIRECTORY_ENTRY_DEBUG].Size = dwValue; - } - - template - void PeHeaderT::setIddDelayImportRva(dword dwValue) - { - m_inthHeader.dataDirectories[PELIB_IMAGE_DIRECTORY_ENTRY_DELAY_IMPORT].VirtualAddress = dwValue; - } - - template - void PeHeaderT::setIddDelayImportSize(dword dwValue) - { - m_inthHeader.dataDirectories[PELIB_IMAGE_DIRECTORY_ENTRY_DELAY_IMPORT].Size = dwValue; - } - - template - void PeHeaderT::setIddExceptionRva(dword dwValue) - { - m_inthHeader.dataDirectories[PELIB_IMAGE_DIRECTORY_ENTRY_EXCEPTION].VirtualAddress = dwValue; - } - - template - void PeHeaderT::setIddExceptionSize(dword dwValue) - { - m_inthHeader.dataDirectories[PELIB_IMAGE_DIRECTORY_ENTRY_EXCEPTION].Size = dwValue; - } - - template - void PeHeaderT::setIddGlobalPtrRva(dword dwValue) - { - m_inthHeader.dataDirectories[PELIB_IMAGE_DIRECTORY_ENTRY_GLOBALPTR].VirtualAddress = dwValue; - } - - template - void PeHeaderT::setIddGlobalPtrSize(dword dwValue) - { - m_inthHeader.dataDirectories[PELIB_IMAGE_DIRECTORY_ENTRY_GLOBALPTR].Size = dwValue; - } - - template - void PeHeaderT::setIddIatRva(dword dwValue) - { - m_inthHeader.dataDirectories[PELIB_IMAGE_DIRECTORY_ENTRY_IAT].VirtualAddress = dwValue; - } - - template - void PeHeaderT::setIddIatSize(dword dwValue) - { - m_inthHeader.dataDirectories[PELIB_IMAGE_DIRECTORY_ENTRY_IAT].Size = dwValue; - } - - template - void PeHeaderT::setIddLoadConfigRva(dword dwValue) - { - m_inthHeader.dataDirectories[PELIB_IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG].VirtualAddress = dwValue; - } - - template - void PeHeaderT::setIddLoadConfigSize(dword dwValue) - { - m_inthHeader.dataDirectories[PELIB_IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG].Size = dwValue; - } - - template - void PeHeaderT::setIddResourceRva(dword dwValue) - { - m_inthHeader.dataDirectories[PELIB_IMAGE_DIRECTORY_ENTRY_RESOURCE].VirtualAddress = dwValue; - } - - template - void PeHeaderT::setIddResourceSize(dword dwValue) - { - m_inthHeader.dataDirectories[PELIB_IMAGE_DIRECTORY_ENTRY_RESOURCE].Size = dwValue; - } - - template - void PeHeaderT::setIddSecurityRva(dword dwValue) - { - m_inthHeader.dataDirectories[PELIB_IMAGE_DIRECTORY_ENTRY_SECURITY].VirtualAddress = dwValue; - } - - template - void PeHeaderT::setIddSecuritySize(dword dwValue) - { - m_inthHeader.dataDirectories[PELIB_IMAGE_DIRECTORY_ENTRY_SECURITY].Size = dwValue; - } - - template - void PeHeaderT::setIddTlsRva(dword dwValue) - { - m_inthHeader.dataDirectories[PELIB_IMAGE_DIRECTORY_ENTRY_TLS].VirtualAddress = dwValue; - } - - template - void PeHeaderT::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 - void PeHeaderT::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 - void PeHeaderT::setIddExportSize(dword dwValue) - { - m_inthHeader.dataDirectories[PELIB_IMAGE_DIRECTORY_ENTRY_EXPORT].Size = dwValue; - } - - template - void PeHeaderT::setIddBaseRelocRva(dword value) - { - m_inthHeader.dataDirectories[PELIB_IMAGE_DIRECTORY_ENTRY_BASERELOC].VirtualAddress = value; - } - - template - void PeHeaderT::setIddBaseRelocSize(dword value) - { - m_inthHeader.dataDirectories[PELIB_IMAGE_DIRECTORY_ENTRY_BASERELOC].Size = value; - } - - template - void PeHeaderT::setIddArchitectureRva(dword value) - { - m_inthHeader.dataDirectories[PELIB_IMAGE_DIRECTORY_ENTRY_ARCHITECTURE].VirtualAddress = value; - } - - template - void PeHeaderT::setIddArchitectureSize(dword value) - { - m_inthHeader.dataDirectories[PELIB_IMAGE_DIRECTORY_ENTRY_ARCHITECTURE].Size = value; - } - - template - void PeHeaderT::setIddComHeaderRva(dword value) - { - m_inthHeader.dataDirectories[PELIB_IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR].VirtualAddress = value; - } - - template - void PeHeaderT::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 - void PeHeaderT::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 - void PeHeaderT::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 - void PeHeaderT::setSectionName(word wSectionnr, std::string strName) - { - strncpy(reinterpret_cast(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 - void PeHeaderT::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 - void PeHeaderT::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 - void PeHeaderT::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 - void PeHeaderT::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 - void PeHeaderT::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 - void PeHeaderT::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 - void PeHeaderT::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 - void PeHeaderT::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 - void PeHeaderT::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 + class PeHeaderT : public PeHeader + { + private: + void readBaseOfData(InputBuffer& ibBuffer, PELIB_IMAGE_NT_HEADERS& header) const; + void rebuildBaseOfData(OutputBuffer& obBuffer) const; + + protected: + std::vector m_vIsh; ///< Stores section header information. + PELIB_IMAGE_NT_HEADERS m_inthHeader; ///< Stores Nt header information. + dword m_uiOffset; ///< Equivalent to the value returned by #PeLib::MzHeader::getAddressOfPeFile + + public: + typedef typename FieldSizes::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& header) const; + void readDataDirectories(InputBuffer& ibBuffer, PELIB_IMAGE_NT_HEADERS& header) const; + std::vector readSections(InputBuffer& ibBuffer, PELIB_IMAGE_NT_HEADERS& header) const; + + /// Rebuilds the current PE header. + void rebuild(std::vector& 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& 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 + void PeHeaderT::addDataDirectory() + { + m_inthHeader.dataDirectories.push_back(PELIB_IMAGE_DATA_DIRECTORY()); + } + + template + void PeHeaderT::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 PeHeaderT::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 + unsigned int PeHeaderT::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::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 + unsigned int PeHeaderT::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 + unsigned int PeHeaderT::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 + unsigned int PeHeaderT::calcOffset(/*unsigned int uiSize*/) const + { + unsigned int maxoffset = size(); + + for (word i=0;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 + unsigned int PeHeaderT::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 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::getNumberOfSections). + * @return Number of currently defined sections. + **/ + template + word PeHeaderT::calcNumberOfSections() const + { + return static_cast(m_vIsh.size()); + } + + /** + * Enlarges the physically last section in the file. + * @param uiSize Additional size that's added to the section's size. + **/ + template + void PeHeaderT::enlargeLastSection(unsigned int uiSize) + { + std::vector::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 + word PeHeaderT::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::max(); + + for (word i=0;i dwOffset) return i; + } + + return std::numeric_limits::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 + word PeHeaderT::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= 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 + void PeHeaderT::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::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 + unsigned int PeHeaderT::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 + unsigned int PeHeaderT::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 + void PeHeaderT::readHeader(InputBuffer& ibBuffer, PELIB_IMAGE_NT_HEADERS& 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 + void PeHeaderT::readDataDirectories(InputBuffer& ibBuffer, PELIB_IMAGE_NT_HEADERS& header) const + { + PELIB_IMAGE_DATA_DIRECTORY idd; + + for (unsigned int i=0;i> idd.VirtualAddress; + ibBuffer >> idd.Size; + header.dataDirectories.push_back(idd); + } + } + + template + std::vector PeHeaderT::readSections(InputBuffer& ibBuffer, PELIB_IMAGE_NT_HEADERS& header) const + { + const unsigned int nrSections = header.FileHeader.NumberOfSections; + PELIB_IMAGE_SECTION_HEADER ishCurr; + + std::vector vIshdCurr; + + for (unsigned int i=0;i(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 PeHeaderT::read(const unsigned char* pcBuffer, unsigned int uiSize, unsigned int uiOffset) + { + if (uiSize < m_inthHeader.size()) + { + return ERROR_INVALID_FILE; + } + + std::vector vBuffer(pcBuffer, pcBuffer + m_inthHeader.size()); + + InputBuffer ibBuffer(vBuffer); + PELIB_IMAGE_NT_HEADERS 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::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 PeHeaderT::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 vBuffer(m_inthHeader.size()); + + ifFile.seekg(uiOffset, std::ios::beg); + ifFile.read(reinterpret_cast(&vBuffer[0]), static_cast(vBuffer.size())); + + InputBuffer ibBuffer(vBuffer); + PELIB_IMAGE_NT_HEADERS header; + + readHeader(ibBuffer, header); + + vBuffer.resize(header.OptionalHeader.NumberOfRvaAndSizes * 8 + header.FileHeader.NumberOfSections * 0x28); + + ifFile.read(reinterpret_cast(&vBuffer[0]), static_cast(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::makeValid first. + * @param vBuffer Buffer where the rebuilt header will be stored. + **/ + template + void PeHeaderT::rebuild(std::vector& 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 + typename FieldSizes::VAR4_8 PeHeaderT::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::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 + typename FieldSizes::VAR4_8 PeHeaderT::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 + unsigned int PeHeaderT::size() const + { + return m_inthHeader.size() + getNumberOfSections() * PELIB_IMAGE_SECTION_HEADER::size(); + } + + // \todo Not sure if this works. + template + typename FieldSizes::VAR4_8 PeHeaderT::vaToRva(VAR4_8 dwRva) const + { + if (dwRva - getImageBase() < calcStartOfCode()) return dwRva - getImageBase(); + + if (getSectionWithRva(dwRva - getImageBase()) == 0xFFFF) return -1; + + return dwRva - getImageBase(); + } + + template + typename FieldSizes::VAR4_8 PeHeaderT::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::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::isValid and #PeLib::PeHeaderT::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 PeHeaderT::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 vBuffer; + + rebuild(vBuffer); + + ofFile.write(reinterpret_cast(&vBuffer[0]), static_cast(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 PeHeaderT::writeSectionData(const std::string& strFilename, word wSecnr, const std::vector& 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(&vBuffer[0]), std::min(static_cast(vBuffer.size()), getSizeOfRawData(wSecnr))); + + ofFile.close(); + + return NO_ERROR; + } + + template + int PeHeaderT::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 vBuffer(uiToWrite); + ofFile.seekp(0, std::ios::end); + ofFile.write(&vBuffer[0], static_cast(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 + dword PeHeaderT::getNtSignature() const + { + return m_inthHeader.Signature; + } + + /** + * Returns the file's machine. + * @return The Machine value from the PE header. + **/ + template + word PeHeaderT::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::getNumberOfSections). + * @return The NumberOfSections value from the PE header. + **/ + template + word PeHeaderT::getNumberOfSections() const + { + return m_inthHeader.FileHeader.NumberOfSections; + } + + /** + * Returns the file's TimeDateStamp. + * @return The TimeDateStamp value from the PE header. + **/ + template + dword PeHeaderT::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 + dword PeHeaderT::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 + dword PeHeaderT::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 + word PeHeaderT::getSizeOfOptionalHeader() const + { + return m_inthHeader.FileHeader.SizeOfOptionalHeader; + } + + /** + * @return The Characteristics value from the PE header. + **/ + template + word PeHeaderT::getCharacteristics() const + { + return m_inthHeader.FileHeader.Characteristics; + } + + /** + * @return The Magic value from the PE header. + **/ + template + word PeHeaderT::getMagic() const + { + return m_inthHeader.OptionalHeader.Magic; + } + + /** + * @return The MajorLinkerVersion value from the PE header. + **/ + template + byte PeHeaderT::getMajorLinkerVersion() const + { + return m_inthHeader.OptionalHeader.MajorLinkerVersion; + } + + /** + * @return The MinorLinkerVersion value from the PE header. + **/ + template + byte PeHeaderT::getMinorLinkerVersion() const + { + return m_inthHeader.OptionalHeader.MinorLinkerVersion; + } + + /** + * @return The SizeOfCode value from the PE header. + **/ + template + dword PeHeaderT::getSizeOfCode() const + { + return m_inthHeader.OptionalHeader.SizeOfCode; + } + + /** + * @return The SizeOfInitializedData value from the PE header. + **/ + template + dword PeHeaderT::getSizeOfInitializedData() const + { + return m_inthHeader.OptionalHeader.SizeOfInitializedData; + } + + /** + * @return The SizeOfUninitializedData value from the PE header. + **/ + template + dword PeHeaderT::getSizeOfUninitializedData() const + { + return m_inthHeader.OptionalHeader.SizeOfUninitializedData; + } + + /** + * @return The AddressOfEntryPoint value from the PE header. + **/ + template + dword PeHeaderT::getAddressOfEntryPoint() const + { + return m_inthHeader.OptionalHeader.AddressOfEntryPoint; + } + + /** + * @return The BaseOfCode value from the PE header. + **/ + template + dword PeHeaderT::getBaseOfCode() const + { + return m_inthHeader.OptionalHeader.BaseOfCode; + } + + /** + * @return The ImageBase value from the PE header. + **/ + template + typename FieldSizes::VAR4_8 PeHeaderT::getImageBase() const + { + return m_inthHeader.OptionalHeader.ImageBase; + } + + /** + * @return The SectionAlignment value from the PE header. + **/ + template + dword PeHeaderT::getSectionAlignment() const + { + return m_inthHeader.OptionalHeader.SectionAlignment; + } + + /** + * @return The FileAlignment value from the PE header. + **/ + template + dword PeHeaderT::getFileAlignment() const + { + return m_inthHeader.OptionalHeader.FileAlignment; + } + + /** + * @return The MajorOperatingSystemVersion value from the PE header. + **/ + template + word PeHeaderT::getMajorOperatingSystemVersion() const + { + return m_inthHeader.OptionalHeader.MajorOperatingSystemVersion; + } + + /** + * @return The MinorOperatingSystemVersion value from the PE header. + **/ + template + word PeHeaderT::getMinorOperatingSystemVersion() const + { + return m_inthHeader.OptionalHeader.MinorOperatingSystemVersion; + } + + /** + * @return The MajorImageVersion value from the PE header. + **/ + template + word PeHeaderT::getMajorImageVersion() const + { + return m_inthHeader.OptionalHeader.MajorImageVersion; + } + + /** + * @return The MinorImageVersion value from the PE header. + **/ + template + word PeHeaderT::getMinorImageVersion() const + { + return m_inthHeader.OptionalHeader.MinorImageVersion; + } + + /** + * @return The MajorSubsystemVersion value from the PE header. + **/ + template + word PeHeaderT::getMajorSubsystemVersion() const + { + return m_inthHeader.OptionalHeader.MajorSubsystemVersion; + } + + /** + * @return The MinorSubsystemVersion value from the PE header. + **/ + template + word PeHeaderT::getMinorSubsystemVersion() const + { + return m_inthHeader.OptionalHeader.MinorSubsystemVersion; + } + + /** + * @return The WinVersionValue value from the PE header. + **/ + template + dword PeHeaderT::getWin32VersionValue() const + { + return m_inthHeader.OptionalHeader.Win32VersionValue; + } + + /** + * @return The SizeOfImage value from the PE header. + **/ + template + dword PeHeaderT::getSizeOfImage() const + { + return m_inthHeader.OptionalHeader.SizeOfImage; + } + + /** + * @return The SizeOfHeaders value from the PE header. + **/ + template + dword PeHeaderT::getSizeOfHeaders() const + { + return m_inthHeader.OptionalHeader.SizeOfHeaders; + } + + /** + * @return The CheckSums value from the PE header. + **/ + template + dword PeHeaderT::getCheckSum() const + { + return m_inthHeader.OptionalHeader.CheckSum; + } + + /** + * @return The Subsystem value from the PE header. + **/ + template + word PeHeaderT::getSubsystem() const + { + return m_inthHeader.OptionalHeader.Subsystem; + } + + /** + * @return The DllCharacteristics value from the PE header. + **/ + template + word PeHeaderT::getDllCharacteristics() const + { + return m_inthHeader.OptionalHeader.DllCharacteristics; + } + + /** + * @return The SizeOfStackReserve value from the PE header. + **/ + template + typename FieldSizes::VAR4_8 PeHeaderT::getSizeOfStackReserve() const + { + return m_inthHeader.OptionalHeader.SizeOfStackReserve; + } + + /** + * @return The SizeOfStackCommit value from the PE header. + **/ + template + typename FieldSizes::VAR4_8 PeHeaderT::getSizeOfStackCommit() const + { + return m_inthHeader.OptionalHeader.SizeOfStackCommit; + } + + /** + * @return The SizeOfHeapReserve value from the PE header. + **/ + template + typename FieldSizes::VAR4_8 PeHeaderT::getSizeOfHeapReserve() const + { + return m_inthHeader.OptionalHeader.SizeOfHeapReserve; + } + + /** + * @return The SizeOfHeapCommit value from the PE header. + **/ + template + typename FieldSizes::VAR4_8 PeHeaderT::getSizeOfHeapCommit() const + { + return m_inthHeader.OptionalHeader.SizeOfHeapCommit; + } + + /** + * @return The LoaderFlags value from the PE header. + **/ + template + dword PeHeaderT::getLoaderFlags() const + { + return m_inthHeader.OptionalHeader.LoaderFlags; + } + + /** + * @return The NumberOfRvaAndSizes value from the PE header. + **/ + template + dword PeHeaderT::getNumberOfRvaAndSizes() const + { + return m_inthHeader.OptionalHeader.NumberOfRvaAndSizes; + } + + template + dword PeHeaderT::calcNumberOfRvaAndSizes() const + { + return static_cast(m_inthHeader.dataDirectories.size()); + } + + /** + * Returns the relative virtual address of the current file's export directory. + * @return The Rva of the Export directory. + **/ + template + dword PeHeaderT::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 + dword PeHeaderT::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 + dword PeHeaderT::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 + dword PeHeaderT::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 + dword PeHeaderT::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 + dword PeHeaderT::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 + dword PeHeaderT::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 + dword PeHeaderT::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 + dword PeHeaderT::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 + dword PeHeaderT::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 + dword PeHeaderT::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 + dword PeHeaderT::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 + dword PeHeaderT::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 + dword PeHeaderT::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 + dword PeHeaderT::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 + dword PeHeaderT::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 + dword PeHeaderT::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 + dword PeHeaderT::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 + dword PeHeaderT::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 + dword PeHeaderT::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 + dword PeHeaderT::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 + dword PeHeaderT::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 + dword PeHeaderT::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 + dword PeHeaderT::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 + dword PeHeaderT::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 + dword PeHeaderT::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 + dword PeHeaderT::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 + dword PeHeaderT::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 + dword PeHeaderT::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 + dword PeHeaderT::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 + dword PeHeaderT::getImageDataDirectoryRva(dword dwDirectory) const + { + return m_inthHeader.dataDirectories[dwDirectory].VirtualAddress; + } + + template + void PeHeaderT::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 + dword PeHeaderT::getImageDataDirectorySize(dword dwDirectory) const + { + return m_inthHeader.dataDirectories[dwDirectory].Size; + } + + template + void PeHeaderT::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 + std::string PeHeaderT::getSectionName(word wSectionnr) const + { + std::string sectionName = ""; + + for (unsigned int i=0;i + dword PeHeaderT::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 + dword PeHeaderT::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 + dword PeHeaderT::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 + dword PeHeaderT::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 + dword PeHeaderT::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 + dword PeHeaderT::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 + dword PeHeaderT::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 + dword PeHeaderT::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 + dword PeHeaderT::getCharacteristics(word wSectionnr) const + { + return m_vIsh[wSectionnr].Characteristics; + } + + /** + * Changes the file's Nt signature. + * @param dwValue New value. + **/ + template + void PeHeaderT::setNtSignature(dword dwValue) + { + m_inthHeader.Signature = dwValue; + } + + /** + * Changes the file's Machine. + * @param wValue New value. + **/ + template + void PeHeaderT::setMachine(word wValue) + { + m_inthHeader.FileHeader.Machine = wValue; + } + + /** + * Changes the number of sections. + * @param wValue New value. + **/ + template + void PeHeaderT::setNumberOfSections(word wValue) + { + m_inthHeader.FileHeader.NumberOfSections = wValue; + } + + /** + * Changes the file's TimeDateStamp. + * @param dwValue New value. + **/ + template + void PeHeaderT::setTimeDateStamp(dword dwValue) + { + m_inthHeader.FileHeader.TimeDateStamp = dwValue; + } + + /** + * Changes the file's PointerToSymbolTable. + * @param dwValue New value. + **/ + template + void PeHeaderT::setPointerToSymbolTable(dword dwValue) + { + m_inthHeader.FileHeader.PointerToSymbolTable = dwValue; + } + + /** + * Changes the file's NumberOfSymbols. + * @param dwValue New value. + **/ + template + void PeHeaderT::setNumberOfSymbols(dword dwValue) + { + m_inthHeader.FileHeader.NumberOfSymbols = dwValue; + } + + /** + * Changes the file's SizeOfOptionalHeader. + * @param wValue New value. + **/ + template + void PeHeaderT::setSizeOfOptionalHeader(word wValue) + { + m_inthHeader.FileHeader.SizeOfOptionalHeader = wValue; + } + + /** + * Changes the file's Characteristics. + * @param wValue New value. + **/ + template + void PeHeaderT::setCharacteristics(word wValue) + { + m_inthHeader.FileHeader.Characteristics = wValue; + } + + /** + * Changes the file's Magic. + * @param wValue New value. + **/ + template + void PeHeaderT::setMagic(word wValue) + { + m_inthHeader.OptionalHeader.Magic = wValue; + } + + /** + * Changes the file's MajorLinkerVersion. + * @param bValue New value. + **/ + template + void PeHeaderT::setMajorLinkerVersion(byte bValue) + { + m_inthHeader.OptionalHeader.MajorLinkerVersion = bValue; + } + + /** + * Changes the file's MinorLinkerVersion. + * @param bValue New value. + **/ + template + void PeHeaderT::setMinorLinkerVersion(byte bValue) + { + m_inthHeader.OptionalHeader.MinorLinkerVersion = bValue; + } + + /** + * Changes the file's SizeOfCode. + * @param dwValue New value. + **/ + template + void PeHeaderT::setSizeOfCode(dword dwValue) + { + m_inthHeader.OptionalHeader.SizeOfCode = dwValue; + } + + /** + * Changes the file's SizeOfInitializedData. + * @param dwValue New value. + **/ + template + void PeHeaderT::setSizeOfInitializedData(dword dwValue) + { + m_inthHeader.OptionalHeader.SizeOfInitializedData = dwValue; + } + + /** + * Changes the file's SizeOfUninitializedData. + * @param dwValue New value. + **/ + template + void PeHeaderT::setSizeOfUninitializedData(dword dwValue) + { + m_inthHeader.OptionalHeader.SizeOfUninitializedData = dwValue; + } + + /** + * Changes the file's AddressOfEntryPoint. + * @param dwValue New value. + **/ + template + void PeHeaderT::setAddressOfEntryPoint(dword dwValue) + { + m_inthHeader.OptionalHeader.AddressOfEntryPoint = dwValue; + } + + /** + * Changes the file's BaseOfCode. + * @param dwValue New value. + **/ + template + void PeHeaderT::setBaseOfCode(dword dwValue) + { + m_inthHeader.OptionalHeader.BaseOfCode = dwValue; + } + + /** + * Changes the file's ImageBase. + * @param dwValue New value. + **/ + template + void PeHeaderT::setImageBase(typename FieldSizes::VAR4_8 dwValue) + { + m_inthHeader.OptionalHeader.ImageBase = dwValue; + } + + /** + * Changes the file's SectionAlignment. + * @param dwValue New value. + **/ + template + void PeHeaderT::setSectionAlignment(dword dwValue) + { + m_inthHeader.OptionalHeader.SectionAlignment = dwValue; + } + + /** + * Changes the file's FileAlignment. + * @param dwValue New value. + **/ + template + void PeHeaderT::setFileAlignment(dword dwValue) + { + m_inthHeader.OptionalHeader.FileAlignment = dwValue; + } + + /** + * Changes the file's MajorOperatingSystemVersion. + * @param wValue New value. + **/ + template + void PeHeaderT::setMajorOperatingSystemVersion(word wValue) + { + m_inthHeader.OptionalHeader.MajorOperatingSystemVersion = wValue; + } + + /** + * Changes the file's MinorOperatingSystemVersion. + * @param wValue New value. + **/ + template + void PeHeaderT::setMinorOperatingSystemVersion(word wValue) + { + m_inthHeader.OptionalHeader.MinorOperatingSystemVersion = wValue; + } + + /** + * Changes the file's MajorImageVersion. + * @param wValue New value. + **/ + template + void PeHeaderT::setMajorImageVersion(word wValue) + { + m_inthHeader.OptionalHeader.MajorImageVersion = wValue; + } + + /** + * Changes the file's MinorImageVersion. + * @param wValue New value. + **/ + template + void PeHeaderT::setMinorImageVersion(word wValue) + { + m_inthHeader.OptionalHeader.MinorImageVersion = wValue; + } + + /** + * Changes the file's MajorSubsystemVersion. + * @param wValue New value. + **/ + template + void PeHeaderT::setMajorSubsystemVersion(word wValue) + { + m_inthHeader.OptionalHeader.MajorSubsystemVersion = wValue; + } + + /** + * Changes the file's MinorSubsystemVersion. + * @param wValue New value. + **/ + template + void PeHeaderT::setMinorSubsystemVersion(word wValue) + { + m_inthHeader.OptionalHeader.MinorSubsystemVersion = wValue; + } + + /** + * Changes the file's Win32VersionValue. + * @param dwValue New value. + **/ + template + void PeHeaderT::setWin32VersionValue(dword dwValue) + { + m_inthHeader.OptionalHeader.Win32VersionValue = dwValue; + } + + /** + * Changes the file's SizeOfImage. + * @param dwValue New value. + **/ + template + void PeHeaderT::setSizeOfImage(dword dwValue) + { + m_inthHeader.OptionalHeader.SizeOfImage = dwValue; + } + + /** + * Changes the file's SizeOfHeaders. + * @param dwValue New value. + **/ + template + void PeHeaderT::setSizeOfHeaders(dword dwValue) + { + m_inthHeader.OptionalHeader.SizeOfHeaders = dwValue; + } + + /** + * Changes the file's CheckSum. + * @param dwValue New value. + **/ + template + void PeHeaderT::setCheckSum(dword dwValue) + { + m_inthHeader.OptionalHeader.CheckSum = dwValue; + } + + /** + * Changes the file's Subsystem. + * @param wValue New value. + **/ + template + void PeHeaderT::setSubsystem(word wValue) + { + m_inthHeader.OptionalHeader.Subsystem = wValue; + } + + /** + * Changes the file's DllCharacteristics. + * @param wValue New value. + **/ + template + void PeHeaderT::setDllCharacteristics(word wValue) + { + m_inthHeader.OptionalHeader.DllCharacteristics = wValue; + } + + /** + * Changes the file's SizeOfStackReserve. + * @param dwValue New value. + **/ + template + void PeHeaderT::setSizeOfStackReserve(typename FieldSizes::VAR4_8 dwValue) + { + m_inthHeader.OptionalHeader.SizeOfStackReserve = dwValue; + } + + /** + * Changes the file's SizeOfStackCommit. + * @param dwValue New value. + **/ + template + void PeHeaderT::setSizeOfStackCommit(typename FieldSizes::VAR4_8 dwValue) + { + m_inthHeader.OptionalHeader.SizeOfStackCommit = dwValue; + } + + /** + * Changes the file's SizeOfHeapReserve. + * @param dwValue New value. + **/ + template + void PeHeaderT::setSizeOfHeapReserve(typename FieldSizes::VAR4_8 dwValue) + { + m_inthHeader.OptionalHeader.SizeOfHeapReserve = dwValue; + } + + /** + * Changes the file's SizeOfHeapCommit. + * @param dwValue New value. + **/ + template + void PeHeaderT::setSizeOfHeapCommit(typename FieldSizes::VAR4_8 dwValue) + { + m_inthHeader.OptionalHeader.SizeOfHeapCommit = dwValue; + } + + /** + * Changes the file's LoaderFlags. + * @param dwValue New value. + **/ + template + void PeHeaderT::setLoaderFlags(dword dwValue) + { + m_inthHeader.OptionalHeader.LoaderFlags = dwValue; + } + + /** + * Changes the file's NumberOfRvaAndSizes. + * @param dwValue New value. + **/ + template + void PeHeaderT::setNumberOfRvaAndSizes(dword dwValue) + { + m_inthHeader.OptionalHeader.NumberOfRvaAndSizes = dwValue; + } + + template + void PeHeaderT::setIddDebugRva(dword dwValue) + { + m_inthHeader.dataDirectories[PELIB_IMAGE_DIRECTORY_ENTRY_DEBUG].VirtualAddress = dwValue; + } + + template + void PeHeaderT::setIddDebugSize(dword dwValue) + { + m_inthHeader.dataDirectories[PELIB_IMAGE_DIRECTORY_ENTRY_DEBUG].Size = dwValue; + } + + template + void PeHeaderT::setIddDelayImportRva(dword dwValue) + { + m_inthHeader.dataDirectories[PELIB_IMAGE_DIRECTORY_ENTRY_DELAY_IMPORT].VirtualAddress = dwValue; + } + + template + void PeHeaderT::setIddDelayImportSize(dword dwValue) + { + m_inthHeader.dataDirectories[PELIB_IMAGE_DIRECTORY_ENTRY_DELAY_IMPORT].Size = dwValue; + } + + template + void PeHeaderT::setIddExceptionRva(dword dwValue) + { + m_inthHeader.dataDirectories[PELIB_IMAGE_DIRECTORY_ENTRY_EXCEPTION].VirtualAddress = dwValue; + } + + template + void PeHeaderT::setIddExceptionSize(dword dwValue) + { + m_inthHeader.dataDirectories[PELIB_IMAGE_DIRECTORY_ENTRY_EXCEPTION].Size = dwValue; + } + + template + void PeHeaderT::setIddGlobalPtrRva(dword dwValue) + { + m_inthHeader.dataDirectories[PELIB_IMAGE_DIRECTORY_ENTRY_GLOBALPTR].VirtualAddress = dwValue; + } + + template + void PeHeaderT::setIddGlobalPtrSize(dword dwValue) + { + m_inthHeader.dataDirectories[PELIB_IMAGE_DIRECTORY_ENTRY_GLOBALPTR].Size = dwValue; + } + + template + void PeHeaderT::setIddIatRva(dword dwValue) + { + m_inthHeader.dataDirectories[PELIB_IMAGE_DIRECTORY_ENTRY_IAT].VirtualAddress = dwValue; + } + + template + void PeHeaderT::setIddIatSize(dword dwValue) + { + m_inthHeader.dataDirectories[PELIB_IMAGE_DIRECTORY_ENTRY_IAT].Size = dwValue; + } + + template + void PeHeaderT::setIddLoadConfigRva(dword dwValue) + { + m_inthHeader.dataDirectories[PELIB_IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG].VirtualAddress = dwValue; + } + + template + void PeHeaderT::setIddLoadConfigSize(dword dwValue) + { + m_inthHeader.dataDirectories[PELIB_IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG].Size = dwValue; + } + + template + void PeHeaderT::setIddResourceRva(dword dwValue) + { + m_inthHeader.dataDirectories[PELIB_IMAGE_DIRECTORY_ENTRY_RESOURCE].VirtualAddress = dwValue; + } + + template + void PeHeaderT::setIddResourceSize(dword dwValue) + { + m_inthHeader.dataDirectories[PELIB_IMAGE_DIRECTORY_ENTRY_RESOURCE].Size = dwValue; + } + + template + void PeHeaderT::setIddSecurityRva(dword dwValue) + { + m_inthHeader.dataDirectories[PELIB_IMAGE_DIRECTORY_ENTRY_SECURITY].VirtualAddress = dwValue; + } + + template + void PeHeaderT::setIddSecuritySize(dword dwValue) + { + m_inthHeader.dataDirectories[PELIB_IMAGE_DIRECTORY_ENTRY_SECURITY].Size = dwValue; + } + + template + void PeHeaderT::setIddTlsRva(dword dwValue) + { + m_inthHeader.dataDirectories[PELIB_IMAGE_DIRECTORY_ENTRY_TLS].VirtualAddress = dwValue; + } + + template + void PeHeaderT::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 + void PeHeaderT::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 + void PeHeaderT::setIddExportSize(dword dwValue) + { + m_inthHeader.dataDirectories[PELIB_IMAGE_DIRECTORY_ENTRY_EXPORT].Size = dwValue; + } + + template + void PeHeaderT::setIddBaseRelocRva(dword value) + { + m_inthHeader.dataDirectories[PELIB_IMAGE_DIRECTORY_ENTRY_BASERELOC].VirtualAddress = value; + } + + template + void PeHeaderT::setIddBaseRelocSize(dword value) + { + m_inthHeader.dataDirectories[PELIB_IMAGE_DIRECTORY_ENTRY_BASERELOC].Size = value; + } + + template + void PeHeaderT::setIddArchitectureRva(dword value) + { + m_inthHeader.dataDirectories[PELIB_IMAGE_DIRECTORY_ENTRY_ARCHITECTURE].VirtualAddress = value; + } + + template + void PeHeaderT::setIddArchitectureSize(dword value) + { + m_inthHeader.dataDirectories[PELIB_IMAGE_DIRECTORY_ENTRY_ARCHITECTURE].Size = value; + } + + template + void PeHeaderT::setIddComHeaderRva(dword value) + { + m_inthHeader.dataDirectories[PELIB_IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR].VirtualAddress = value; + } + + template + void PeHeaderT::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 + void PeHeaderT::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 + void PeHeaderT::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 + void PeHeaderT::setSectionName(word wSectionnr, std::string strName) + { + strncpy(reinterpret_cast(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 + void PeHeaderT::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 + void PeHeaderT::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 + void PeHeaderT::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 + void PeHeaderT::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 + void PeHeaderT::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 + void PeHeaderT::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 + void PeHeaderT::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 + void PeHeaderT::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 + void PeHeaderT::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 old mode 100755 new mode 100644 index 870c873606..12f9693dda --- 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 -* -* What is PeLib?
-* 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 +* +* What is PeLib?
+* 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 old mode 100755 new mode 100644 index 1e06bae620..0a420ebecf --- 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 -#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;istrModuleName == 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 +#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;istrModuleName == 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 -#include - -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 - class DataType - { - private: - std::bitset 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 byte; - typedef DataType word; - typedef DataType 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 - 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 - 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 - struct FieldSizes; - - template<> - struct FieldSizes<32> - { - typedef dword VAR4_8; - }; - - template<> - struct FieldSizes<64> - { - typedef qword VAR4_8; - }; - - template - struct PELIB_IMAGE_OPTIONAL_HEADER_BASE - { - typedef typename FieldSizes::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 - PELIB_IMAGE_OPTIONAL_HEADER_BASE::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 - 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 - struct PELIB_IMAGE_NT_HEADERS - { - dword Signature; - PELIB_IMAGE_FILE_HEADER FileHeader; - PELIB_IMAGE_OPTIONAL_HEADER OptionalHeader; - std::vector dataDirectories; - - unsigned int size() const - { - return sizeof(dword) - + PELIB_IMAGE_FILE_HEADER::size() - + PELIB_IMAGE_OPTIONAL_HEADER::size() - + static_cast(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 - struct PELIB_IMAGE_THUNK_DATA - { - typename FieldSizes::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 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 functions; - inline unsigned int size() const - { - return PELIB_IMAGE_EXPORT_DIRECTORY::size() + name.size() + 1 + - std::accumulate(functions.begin(), functions.end(), 0, accumulate); - } - }; - - // 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 - struct PELIB_THUNK_DATA - { - /// The IMAGE_THUNK_DATA struct of an imported function. - PELIB_IMAGE_THUNK_DATA 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::IMAGE_ORDINAL_FLAG); - } - - bool equalFunctionName(std::string strFunctionName) const - { - return isEqualNc(fname, strFunctionName); - } - - unsigned int size() const {return PELIB_IMAGE_THUNK_DATA::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 - 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 > originalfirstthunk; - /// All first thunk value of an imported DLL. - std::vector > 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 >) + // thunks (PeLib uses only one thunk) - PELIB_IMAGE_THUNK_DATA::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 vData; - }; - - struct IMG_BASE_RELOC - { - PELIB_IMAGE_BASE_RELOCATION ibrRelocation; - std::vector 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 data; - }; - - template - struct PELIB_IMAGE_TLS_DIRECTORY_BASE - { - typename FieldSizes::VAR4_8 StartAddressOfRawData; - typename FieldSizes::VAR4_8 EndAddressOfRawData; - typename FieldSizes::VAR4_8 AddressOfIndex; - typename FieldSizes::VAR4_8 AddressOfCallBacks; - dword SizeOfZeroFill; - dword Characteristics; - }; - - template - struct PELIB_IMAGE_TLS_DIRECTORY;// : public PELIB_IMAGE_TLS_DIRECTORY_BASE - - 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 +#include + +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 + class DataType + { + private: + std::bitset 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 byte; + typedef DataType word; + typedef DataType 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 + 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 + 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 + struct FieldSizes; + + template<> + struct FieldSizes<32> + { + typedef dword VAR4_8; + }; + + template<> + struct FieldSizes<64> + { + typedef qword VAR4_8; + }; + + template + struct PELIB_IMAGE_OPTIONAL_HEADER_BASE + { + typedef typename FieldSizes::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 + PELIB_IMAGE_OPTIONAL_HEADER_BASE::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 + 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 + struct PELIB_IMAGE_NT_HEADERS + { + dword Signature; + PELIB_IMAGE_FILE_HEADER FileHeader; + PELIB_IMAGE_OPTIONAL_HEADER OptionalHeader; + std::vector dataDirectories; + + unsigned int size() const + { + return sizeof(dword) + + PELIB_IMAGE_FILE_HEADER::size() + + PELIB_IMAGE_OPTIONAL_HEADER::size() + + static_cast(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 + struct PELIB_IMAGE_THUNK_DATA + { + typename FieldSizes::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 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 functions; + inline unsigned int size() const + { + return PELIB_IMAGE_EXPORT_DIRECTORY::size() + name.size() + 1 + + std::accumulate(functions.begin(), functions.end(), 0, accumulate); + } + }; + + // 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 + struct PELIB_THUNK_DATA + { + /// The IMAGE_THUNK_DATA struct of an imported function. + PELIB_IMAGE_THUNK_DATA 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::IMAGE_ORDINAL_FLAG); + } + + bool equalFunctionName(std::string strFunctionName) const + { + return isEqualNc(fname, strFunctionName); + } + + unsigned int size() const {return PELIB_IMAGE_THUNK_DATA::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 + 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 > originalfirstthunk; + /// All first thunk value of an imported DLL. + std::vector > 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 >) + // thunks (PeLib uses only one thunk) + PELIB_IMAGE_THUNK_DATA::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 vData; + }; + + struct IMG_BASE_RELOC + { + PELIB_IMAGE_BASE_RELOCATION ibrRelocation; + std::vector 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 data; + }; + + template + struct PELIB_IMAGE_TLS_DIRECTORY_BASE + { + typename FieldSizes::VAR4_8 StartAddressOfRawData; + typename FieldSizes::VAR4_8 EndAddressOfRawData; + typename FieldSizes::VAR4_8 AddressOfIndex; + typename FieldSizes::VAR4_8 AddressOfCallBacks; + dword SizeOfZeroFill; + dword Characteristics; + }; + + template + struct PELIB_IMAGE_TLS_DIRECTORY;// : public PELIB_IMAGE_TLS_DIRECTORY_BASE + + 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 diff --git a/utils/zenutils/libraries/pelib-0.9/pelib/PeLibInc.h b/utils/zenutils/libraries/pelib-0.9/pelib/PeLibInc.h old mode 100755 new mode 100644 index 84538bd2dd..7d3cabaa6b --- a/utils/zenutils/libraries/pelib-0.9/pelib/PeLibInc.h +++ b/utils/zenutils/libraries/pelib-0.9/pelib/PeLibInc.h @@ -1,32 +1,32 @@ -/* -* PeLibInc.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 STD_H -#define STD_H - -#ifdef _MSC_VER - #ifndef for - #define for if (0) {} else for - #endif -#endif - -#include -#include -#include -#include -#include -#include -#include -// #include "PeLib.h" -#include "PeLibAux.h" - -#endif +/* +* PeLibInc.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 STD_H +#define STD_H + +#ifdef _MSC_VER + #ifndef for + #define for if (0) {} else for + #endif +#endif + +#include +#include +#include +#include +#include +#include +#include +// #include "PeLib.h" +#include "PeLibAux.h" + +#endif diff --git a/utils/zenutils/libraries/pelib-0.9/pelib/RelocationsDirectory.cpp b/utils/zenutils/libraries/pelib-0.9/pelib/RelocationsDirectory.cpp old mode 100755 new mode 100644 index d8b5035abf..392261d242 --- a/utils/zenutils/libraries/pelib-0.9/pelib/RelocationsDirectory.cpp +++ b/utils/zenutils/libraries/pelib-0.9/pelib/RelocationsDirectory.cpp @@ -1,211 +1,211 @@ -/* -* Relocations.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 "RelocationsDirectory.h" - -namespace PeLib -{ - void RelocationsDirectory::setRelocationData(unsigned int ulRelocation, unsigned int ulDataNumber, word wData) - { - m_vRelocations[ulRelocation].vRelocData[ulDataNumber] = wData; - } - - void RelocationsDirectory::read(InputBuffer& inputbuffer, unsigned int uiSize) - { - IMG_BASE_RELOC ibrCurr; - - std::vector vCurrReloc; - - do - { - inputbuffer >> ibrCurr.ibrRelocation.VirtualAddress; - inputbuffer >> ibrCurr.ibrRelocation.SizeOfBlock; - - ibrCurr.vRelocData.clear(); - - // That's not how to check if there are relocations, some DLLs start at VA 0. - // if (!ibrCurr.ibrRelocation.VirtualAddress) break; - - for (unsigned int i=0;i<(ibrCurr.ibrRelocation.SizeOfBlock - PELIB_IMAGE_SIZEOF_BASE_RELOCATION) / sizeof(word);i++) - { - word wData; - inputbuffer >> wData; - ibrCurr.vRelocData.push_back(wData); - } - - vCurrReloc.push_back(ibrCurr); - } while (ibrCurr.ibrRelocation.VirtualAddress && inputbuffer.get() < uiSize); - - std::swap(vCurrReloc, m_vRelocations); - } - - // TODO: Return value is wrong if buffer was too small. - int RelocationsDirectory::read(const unsigned char* buffer, unsigned int buffersize) - { - std::vector vRelocDirectory(buffer, buffer + buffersize); - - InputBuffer ibBuffer(vRelocDirectory); - read(ibBuffer, buffersize); - - return NO_ERROR; - } - - int RelocationsDirectory::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 vRelocDirectory(uiSize); - ifFile.read(reinterpret_cast(&vRelocDirectory[0]), uiSize); - - InputBuffer ibBuffer(vRelocDirectory); - read(ibBuffer, uiSize); - - return NO_ERROR; - } - - unsigned int RelocationsDirectory::size() const - { - unsigned int size = static_cast(m_vRelocations.size()) * PELIB_IMAGE_BASE_RELOCATION::size(); - - for (unsigned int i=0;i(m_vRelocations[i].vRelocData.size()) * sizeof(word); - } - - return size; - } - - void RelocationsDirectory::rebuild(std::vector& vBuffer) const - { - OutputBuffer obBuffer(vBuffer); - - for (unsigned int i=0;i(m_vRelocations.size()); - } - - dword RelocationsDirectory::getVirtualAddress(unsigned int ulRelocation) const - { - return m_vRelocations[ulRelocation].ibrRelocation.VirtualAddress; - } - - dword RelocationsDirectory::getSizeOfBlock(unsigned int ulRelocation) const - { - return m_vRelocations[ulRelocation].ibrRelocation.SizeOfBlock; - } - - unsigned int RelocationsDirectory::calcNumberOfRelocationData(unsigned int ulRelocation) const - { - return static_cast(m_vRelocations[ulRelocation].vRelocData.size()); - } - - word RelocationsDirectory::getRelocationData(unsigned int ulRelocation, unsigned int ulDataNumber) const - { - return m_vRelocations[ulRelocation].vRelocData[ulDataNumber]; - } - - void RelocationsDirectory::setVirtualAddress(unsigned int ulRelocation, dword dwValue) - { - m_vRelocations[ulRelocation].ibrRelocation.VirtualAddress = dwValue; - } - - void RelocationsDirectory::setSizeOfBlock(unsigned int ulRelocation, dword dwValue) - { - m_vRelocations[ulRelocation].ibrRelocation.SizeOfBlock = dwValue; - } - - void RelocationsDirectory::addRelocation() - { - IMG_BASE_RELOC newrelocation; - m_vRelocations.push_back(newrelocation); - } - - void RelocationsDirectory::addRelocationData(unsigned int ulRelocation, word wValue) - { - m_vRelocations[ulRelocation].vRelocData.push_back(wValue); - } - -/* void RelocationsDirectory::removeRelocationData(unsigned int ulRelocation, word wValue) - { - // If you get an error with Borland C++ here you have two options: Upgrade your compiler - // or use the commented line instead of the line below. - m_vRelocations[ulRelocation].vRelocData.erase(std::remove(m_vRelocations[ulRelocation].vRelocData.begin(), m_vRelocations[ulRelocation].vRelocData.end(), wValue), m_vRelocations[ulRelocation].vRelocData.end()); - } -*/ - void RelocationsDirectory::removeRelocation(unsigned int index) - { - m_vRelocations.erase(m_vRelocations.begin() + index); - } - - void RelocationsDirectory::removeRelocationData(unsigned int relocindex, unsigned int dataindex) - { - m_vRelocations[relocindex].vRelocData.erase(m_vRelocations[relocindex].vRelocData.begin() + dataindex); - } - - int RelocationsDirectory::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 vBuffer; - rebuild(vBuffer); - - ofFile.write(reinterpret_cast(&vBuffer[0]), static_cast(vBuffer.size())); - - ofFile.close(); - - return NO_ERROR; - } -} +/* +* Relocations.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 "RelocationsDirectory.h" + +namespace PeLib +{ + void RelocationsDirectory::setRelocationData(unsigned int ulRelocation, unsigned int ulDataNumber, word wData) + { + m_vRelocations[ulRelocation].vRelocData[ulDataNumber] = wData; + } + + void RelocationsDirectory::read(InputBuffer& inputbuffer, unsigned int uiSize) + { + IMG_BASE_RELOC ibrCurr; + + std::vector vCurrReloc; + + do + { + inputbuffer >> ibrCurr.ibrRelocation.VirtualAddress; + inputbuffer >> ibrCurr.ibrRelocation.SizeOfBlock; + + ibrCurr.vRelocData.clear(); + + // That's not how to check if there are relocations, some DLLs start at VA 0. + // if (!ibrCurr.ibrRelocation.VirtualAddress) break; + + for (unsigned int i=0;i<(ibrCurr.ibrRelocation.SizeOfBlock - PELIB_IMAGE_SIZEOF_BASE_RELOCATION) / sizeof(word);i++) + { + word wData; + inputbuffer >> wData; + ibrCurr.vRelocData.push_back(wData); + } + + vCurrReloc.push_back(ibrCurr); + } while (ibrCurr.ibrRelocation.VirtualAddress && inputbuffer.get() < uiSize); + + std::swap(vCurrReloc, m_vRelocations); + } + + // TODO: Return value is wrong if buffer was too small. + int RelocationsDirectory::read(const unsigned char* buffer, unsigned int buffersize) + { + std::vector vRelocDirectory(buffer, buffer + buffersize); + + InputBuffer ibBuffer(vRelocDirectory); + read(ibBuffer, buffersize); + + return NO_ERROR; + } + + int RelocationsDirectory::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 vRelocDirectory(uiSize); + ifFile.read(reinterpret_cast(&vRelocDirectory[0]), uiSize); + + InputBuffer ibBuffer(vRelocDirectory); + read(ibBuffer, uiSize); + + return NO_ERROR; + } + + unsigned int RelocationsDirectory::size() const + { + unsigned int size = static_cast(m_vRelocations.size()) * PELIB_IMAGE_BASE_RELOCATION::size(); + + for (unsigned int i=0;i(m_vRelocations[i].vRelocData.size()) * sizeof(word); + } + + return size; + } + + void RelocationsDirectory::rebuild(std::vector& vBuffer) const + { + OutputBuffer obBuffer(vBuffer); + + for (unsigned int i=0;i(m_vRelocations.size()); + } + + dword RelocationsDirectory::getVirtualAddress(unsigned int ulRelocation) const + { + return m_vRelocations[ulRelocation].ibrRelocation.VirtualAddress; + } + + dword RelocationsDirectory::getSizeOfBlock(unsigned int ulRelocation) const + { + return m_vRelocations[ulRelocation].ibrRelocation.SizeOfBlock; + } + + unsigned int RelocationsDirectory::calcNumberOfRelocationData(unsigned int ulRelocation) const + { + return static_cast(m_vRelocations[ulRelocation].vRelocData.size()); + } + + word RelocationsDirectory::getRelocationData(unsigned int ulRelocation, unsigned int ulDataNumber) const + { + return m_vRelocations[ulRelocation].vRelocData[ulDataNumber]; + } + + void RelocationsDirectory::setVirtualAddress(unsigned int ulRelocation, dword dwValue) + { + m_vRelocations[ulRelocation].ibrRelocation.VirtualAddress = dwValue; + } + + void RelocationsDirectory::setSizeOfBlock(unsigned int ulRelocation, dword dwValue) + { + m_vRelocations[ulRelocation].ibrRelocation.SizeOfBlock = dwValue; + } + + void RelocationsDirectory::addRelocation() + { + IMG_BASE_RELOC newrelocation; + m_vRelocations.push_back(newrelocation); + } + + void RelocationsDirectory::addRelocationData(unsigned int ulRelocation, word wValue) + { + m_vRelocations[ulRelocation].vRelocData.push_back(wValue); + } + +/* void RelocationsDirectory::removeRelocationData(unsigned int ulRelocation, word wValue) + { + // If you get an error with Borland C++ here you have two options: Upgrade your compiler + // or use the commented line instead of the line below. + m_vRelocations[ulRelocation].vRelocData.erase(std::remove(m_vRelocations[ulRelocation].vRelocData.begin(), m_vRelocations[ulRelocation].vRelocData.end(), wValue), m_vRelocations[ulRelocation].vRelocData.end()); + } +*/ + void RelocationsDirectory::removeRelocation(unsigned int index) + { + m_vRelocations.erase(m_vRelocations.begin() + index); + } + + void RelocationsDirectory::removeRelocationData(unsigned int relocindex, unsigned int dataindex) + { + m_vRelocations[relocindex].vRelocData.erase(m_vRelocations[relocindex].vRelocData.begin() + dataindex); + } + + int RelocationsDirectory::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 vBuffer; + rebuild(vBuffer); + + ofFile.write(reinterpret_cast(&vBuffer[0]), static_cast(vBuffer.size())); + + ofFile.close(); + + return NO_ERROR; + } +} diff --git a/utils/zenutils/libraries/pelib-0.9/pelib/RelocationsDirectory.h b/utils/zenutils/libraries/pelib-0.9/pelib/RelocationsDirectory.h old mode 100755 new mode 100644 index 60383381a3..0fc10500e0 --- a/utils/zenutils/libraries/pelib-0.9/pelib/RelocationsDirectory.h +++ b/utils/zenutils/libraries/pelib-0.9/pelib/RelocationsDirectory.h @@ -1,70 +1,70 @@ -/* -* Relocations.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 RELOCATIONSDIRECTORY_H -#define RELOCATIONSDIRECTORY_H - -namespace PeLib -{ - /// Class that handles the relocations directory. - /** - * This class handles the relocations directory. - **/ - class RelocationsDirectory - { - private: - std::vector m_vRelocations; ///< Used to store the relocation data. - - void read(InputBuffer& inputbuffer, unsigned int uiSize); - - public: - /// Returns the number of relocations in the relocations directory. - unsigned int calcNumberOfRelocations() const; // EXPORT - /// Returns the number of relocation data entries of a specific relocation. - unsigned int calcNumberOfRelocationData(unsigned int ulRelocation) const; // EXPORT - - /// Read a file's relocations directory. - int read(const std::string& strFilename, unsigned int uiOffset, unsigned int uiSize); // EXPORT - int read(const unsigned char* buffer, unsigned int buffersize); // EXPORT - /// Rebuilds the relocations directory. - void rebuild(std::vector& vBuffer) const; // EXPORT - /// Returns the size of the relocations directory. - unsigned int size() const; // EXPORT - /// Writes the relocations directory to a file. - int write(const std::string& strFilename, unsigned int dwOffset) const; // EXPORT - - /// Returns the VA of a relocation. - dword getVirtualAddress(unsigned int ulRelocation) const; // EXPORT - /// Returns the SizeOfBlock value of a relocation. - dword getSizeOfBlock(unsigned int ulRelocation) const; // EXPORT - /// Returns the RelocationData of a relocation. - word getRelocationData(unsigned int ulRelocation, unsigned int ulDataNumber) const; // EXPORT - - /// Changes the relocation data of a relocation. - void setRelocationData(unsigned int ulRelocation, unsigned int ulDataNumber, word wData); // EXPORT - - /// Changes the VirtualAddress of a relocation. - void setVirtualAddress(unsigned int ulRelocation, dword dwValue); // EXPORT - /// Changes the SizeOfBlock of a relocation. - void setSizeOfBlock(unsigned int ulRelocation, dword dwValue); // EXPORT - - void addRelocation(); // EXPORT - /// Adds new data to a relocation. - void addRelocationData(unsigned int ulRelocation, word wValue); // EXPORT - /// Removes data from a relocation. -// void removeRelocationData(unsigned int ulRelocation, word wValue); // EXPORT - void removeRelocation(unsigned int index); // EXPORT - void removeRelocationData(unsigned int relocindex, unsigned int dataindex); // EXPORT - }; -} - -#endif +/* +* Relocations.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 RELOCATIONSDIRECTORY_H +#define RELOCATIONSDIRECTORY_H + +namespace PeLib +{ + /// Class that handles the relocations directory. + /** + * This class handles the relocations directory. + **/ + class RelocationsDirectory + { + private: + std::vector m_vRelocations; ///< Used to store the relocation data. + + void read(InputBuffer& inputbuffer, unsigned int uiSize); + + public: + /// Returns the number of relocations in the relocations directory. + unsigned int calcNumberOfRelocations() const; // EXPORT + /// Returns the number of relocation data entries of a specific relocation. + unsigned int calcNumberOfRelocationData(unsigned int ulRelocation) const; // EXPORT + + /// Read a file's relocations directory. + int read(const std::string& strFilename, unsigned int uiOffset, unsigned int uiSize); // EXPORT + int read(const unsigned char* buffer, unsigned int buffersize); // EXPORT + /// Rebuilds the relocations directory. + void rebuild(std::vector& vBuffer) const; // EXPORT + /// Returns the size of the relocations directory. + unsigned int size() const; // EXPORT + /// Writes the relocations directory to a file. + int write(const std::string& strFilename, unsigned int dwOffset) const; // EXPORT + + /// Returns the VA of a relocation. + dword getVirtualAddress(unsigned int ulRelocation) const; // EXPORT + /// Returns the SizeOfBlock value of a relocation. + dword getSizeOfBlock(unsigned int ulRelocation) const; // EXPORT + /// Returns the RelocationData of a relocation. + word getRelocationData(unsigned int ulRelocation, unsigned int ulDataNumber) const; // EXPORT + + /// Changes the relocation data of a relocation. + void setRelocationData(unsigned int ulRelocation, unsigned int ulDataNumber, word wData); // EXPORT + + /// Changes the VirtualAddress of a relocation. + void setVirtualAddress(unsigned int ulRelocation, dword dwValue); // EXPORT + /// Changes the SizeOfBlock of a relocation. + void setSizeOfBlock(unsigned int ulRelocation, dword dwValue); // EXPORT + + void addRelocation(); // EXPORT + /// Adds new data to a relocation. + void addRelocationData(unsigned int ulRelocation, word wValue); // EXPORT + /// Removes data from a relocation. +// void removeRelocationData(unsigned int ulRelocation, word wValue); // EXPORT + void removeRelocation(unsigned int index); // EXPORT + void removeRelocationData(unsigned int relocindex, unsigned int dataindex); // EXPORT + }; +} + +#endif diff --git a/utils/zenutils/libraries/pelib-0.9/pelib/ResourceDirectory.cpp b/utils/zenutils/libraries/pelib-0.9/pelib/ResourceDirectory.cpp old mode 100755 new mode 100644 index 5e4d3010bf..d1e4c59e5a --- a/utils/zenutils/libraries/pelib-0.9/pelib/ResourceDirectory.cpp +++ b/utils/zenutils/libraries/pelib-0.9/pelib/ResourceDirectory.cpp @@ -1,1497 +1,1497 @@ -/* -* ResourceDirectory.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 "ResourceDirectory.h" - -namespace PeLib -{ -// -------------------------------------------------- ResourceChild ------------------------------------------- - - ResourceChild::ResourceChild() - { - } - - ResourceChild::ResourceChild(const ResourceChild& rhs) - { - if (dynamic_cast(rhs.child)) - { - ResourceNode* oldnode = static_cast(rhs.child); - - entry = rhs.entry; - - child = new ResourceNode; - child->uiElementRva = rhs.child->getElementRva(); - static_cast(child)->header = oldnode->header; - static_cast(child)->children = oldnode->children; - } - else - { - ResourceLeaf* oldnode = static_cast(rhs.child); - - child = new ResourceLeaf; - child->uiElementRva = rhs.child->getElementRva(); - static_cast(child)->m_data = oldnode->m_data; - static_cast(child)->entry = oldnode->entry; - } - } - - ResourceChild& ResourceChild::operator=(const ResourceChild& rhs) - { - if (this != &rhs) - { - if (dynamic_cast(rhs.child)) - { - ResourceNode* oldnode = static_cast(rhs.child); - - entry = rhs.entry; - - child = new ResourceNode; - child->uiElementRva = rhs.child->getElementRva(); - static_cast(child)->header = oldnode->header; - static_cast(child)->children = oldnode->children; - } - else - { - ResourceLeaf* oldnode = static_cast(rhs.child); - - child = new ResourceLeaf; - child->uiElementRva = rhs.child->getElementRva(); - static_cast(child)->m_data = oldnode->m_data; - static_cast(child)->entry = oldnode->entry; - } - } - - return *this; - } - - ResourceChild::~ResourceChild() - { - delete child; - } - - /** - * Compares the resource child's id to the parameter dwId. - * @param dwId ID of a resource. - * @return True, if the resource child's id equals the parameter. - **/ - bool ResourceChild::equalId(dword dwId) const - { - return entry.irde.Name == dwId; - } - - /** - * Compares the resource child's name to the parameter strName. - * @param strName ID of a resource. - * @return True, if the resource child's name equals the parameter. - **/ - bool ResourceChild::equalName(std::string strName) const - { - return entry.wstrName == strName; - } - - /** - * Returns true if the resource was given a name. - **/ - bool ResourceChild::isNamedResource() const - { - return entry.wstrName.size() != 0; - } - - /** - * The children of a resource must be ordered in a certain way. First come the named resources - * in sorted order, afterwards followed the unnamed resources in sorted order. - **/ - bool ResourceChild::operator<(const ResourceChild& rc) const - { - if (this->isNamedResource() && !rc.isNamedResource()) - { - return true; - } - else if (!this->isNamedResource() && rc.isNamedResource()) - { - return false; - } - else if (this->isNamedResource() && rc.isNamedResource()) - { - return this->entry.wstrName < rc.entry.wstrName; - } - else - { - return this->entry.irde.Name < rc.entry.irde.Name; - } - } - -/* unsigned int ResourceChild::size() const - { - return PELIB_IMAGE_RESOURCE_DIRECTORY_ENTRY::size() - + child->size() - + (entry.wstrName.size() ? entry.wstrName.size() + 2 : 0); - } -*/ -// -------------------------------------------------- ResourceElement ------------------------------------------- - - /** - * Returns the RVA of a ResourceElement. This is the RVA where the ResourceElement can be - * found in the file. - * @return RVA of the ResourceElement. - **/ - unsigned int ResourceElement::getElementRva() const - { - return uiElementRva; - } - -// -------------------------------------------------- ResourceLeaf ------------------------------------------- - - /** - * Checks if a ResourceElement is a leaf or not. - * @return Always returns true. - **/ - bool ResourceLeaf::isLeaf() const - { - return true; - } - - /** - * Reads the next resource leaf from the InputBuffer. - * @param inpBuffer An InputBuffer that holds the complete resource directory. - * @param uiOffset Offset of the resource leaf that's to be read. - * @param uiRva RVA of the beginning of the resource directory. - * @param pad Used for debugging purposes. - **/ - int ResourceLeaf::read(InputBuffer& inpBuffer, unsigned int uiOffset, unsigned int uiRva/*, const std::string& pad*/) - { -// std::cout << pad << "Leaf:" << std::endl; - - // Invalid leaf. - if (uiOffset + PELIB_IMAGE_RESOURCE_DATA_ENTRY::size() > inpBuffer.size()) - { - return 1; - } - - uiElementRva = uiOffset + uiRva; - - inpBuffer.set(uiOffset); - - inpBuffer >> entry.OffsetToData; - inpBuffer >> entry.Size; - inpBuffer >> entry.CodePage; - inpBuffer >> entry.Reserved; - -/* std::cout << pad << std::hex << "Offset: " << entry.OffsetToData << std::endl; - std::cout << pad << std::hex << "Size: " << entry.Size << std::endl; - std::cout << pad << std::hex << "CodePage: " << entry.CodePage << std::endl; - std::cout << pad << std::hex << "Reserved: " << entry.Reserved << std::endl; -*/ - // Invalid leaf. - if (entry.OffsetToData - uiRva + entry.Size > inpBuffer.size()) - { -// std::cout << entry.OffsetToData << " XXX " << uiRva << " - " << entry.Size << " - " << inpBuffer.size() << std::endl; - return 1; - } - -// std::cout << entry.OffsetToData << " - " << uiRva << " - " << entry.Size << " - " << inpBuffer.size() << std::endl; - inpBuffer.set(entry.OffsetToData - uiRva); - m_data.assign(inpBuffer.data() + inpBuffer.get(), inpBuffer.data() + inpBuffer.get() + entry.Size); -// std::cout << pad << std::hex << "Vector: " << m_data.size() << std::endl; -// std::copy(m_data.begin(), m_data.end(), std::ostream_iterator(std::cout << std::hex, " ")); - return 0; - } - - /** - * Rebuilds the current resource leaf. - * @param obBuffer OutputBuffer where the rebuilt resource leaf is stored. - * @param uiOffset Offset of the resource leaf inside the resource directory. - * @param uiRva RVA of the resource directory. - **/ - void ResourceLeaf::rebuild(OutputBuffer& obBuffer, unsigned int& uiOffset, unsigned int uiRva, const std::string&) const - { -// std::cout << std::hex << pad << "Leaf: " << uiOffset << std::endl; - uiOffset += PELIB_IMAGE_RESOURCE_DATA_ENTRY::size(); - -// obBuffer << entry.OffsetToData; -// obBuffer << uiOffset; - obBuffer << uiRva + uiOffset + (uiOffset % 4); - obBuffer << entry.Size; - obBuffer << entry.CodePage; - obBuffer << entry.Reserved; - - while (uiOffset % 4) - { - uiOffset++; - obBuffer << (byte)0; - } - - uiOffset += static_cast(m_data.size()); - - for (unsigned int i=0;i(m_data.size()); - } - -/* /// Returns the size of a resource leaf. - unsigned int ResourceLeaf::size() const - { - return PELIB_IMAGE_RESOURCE_DATA_ENTRY::size() + m_data.size(); - } -*/ - - /** - * Returns a vector that contains the raw data of a resource leaf. - * @return Raw data of the resource. - **/ - std::vector ResourceLeaf::getData() const - { - return m_data; - } - - /** - * Overwrites the raw data of a resource. - * @param vData New data of the resource. - **/ - void ResourceLeaf::setData(const std::vector& vData) - { - m_data = vData; - } - - /** - * Returns the leaf's OffsetToData value. That's the RVA where the raw data of the resource - * can be found. - * @return The leaf's OffsetToData value. - **/ - dword ResourceLeaf::getOffsetToData() const - { - return entry.OffsetToData; - } - - /** - * Returns the leaf's Size value. That's the size of the raw data of the resource. - * @return The leaf's Size value. - **/ - dword ResourceLeaf::getSize() const - { - return entry.Size; - } - - /** - * Returns the leaf's CodePage value. - * @return The leaf's CodePage value. - **/ - dword ResourceLeaf::getCodePage() const - { - return entry.CodePage; - } - - /** - * Returns the leaf's Reserved value. - * @return The leaf's Reserved value. - **/ - dword ResourceLeaf::getReserved() const - { - return entry.Reserved; - } - - /** - * Sets the leaf's OffsetToData value. - * @param dwValue The leaf's new OffsetToData value. - **/ - void ResourceLeaf::setOffsetToData(dword dwValue) - { - entry.OffsetToData = dwValue; - } - - /** - * Sets the leaf's Size value. - * @param dwValue The leaf's new Size value. - **/ - void ResourceLeaf::setSize(dword dwValue) - { - entry.Size = dwValue; - } - - /** - * Sets the leaf's CodePage value. - * @param dwValue The leaf's new CodePage value. - **/ - void ResourceLeaf::setCodePage(dword dwValue) - { - entry.CodePage = dwValue; - } - - /** - * Sets the leaf's Reserved value. - * @param dwValue The leaf's new Reserved value. - **/ - void ResourceLeaf::setReserved(dword dwValue) - { - entry.Reserved = dwValue; - } - - -// -------------------------------------------------- ResourceNode ------------------------------------------- - - /** - * Checks if a ResourceElement is a leaf or not. - * @return Always returns false. - **/ - bool ResourceNode::isLeaf() const - { - return false; - } - - /** - * Sorts the node's children and corrects the node's header. - **/ - void ResourceNode::makeValid() - { - std::sort(children.begin(), children.end()); - header.NumberOfNamedEntries = static_cast(std::count_if(children.begin(), children.end(), std::mem_fun_ref(&ResourceChild::isNamedResource))); - header.NumberOfIdEntries = static_cast(children.size()) - header.NumberOfNamedEntries; - } - - /** - * Rebuilds the current resource node. - * @param obBuffer OutputBuffer where the rebuilt resource node is stored. - * @param uiOffset Offset of the resource node inside the resource directory. - * @param uiRva RVA of the resource directory. - * @param pad Used for debugging. - **/ - void ResourceNode::rebuild(OutputBuffer& obBuffer, unsigned int& uiOffset, unsigned int uiRva, const std::string& pad) const - { -/* std::cout << std::hex << pad << uiOffset << std::endl; - - std::cout << std::hex << pad << "header.Characteristics: " << header.Characteristics << std::endl; - std::cout << std::hex << pad << "header.TimeDateStamp: " << header.TimeDateStamp << std::endl; - std::cout << std::hex << pad << "header.MajorVersion: " << header.MajorVersion << std::endl; - std::cout << std::hex << pad << "header.MinorVersion: " << header.MinorVersion << std::endl; - std::cout << std::hex << pad << "header.NumberOfNamedEntries: " << header.NumberOfNamedEntries << std::endl; - std::cout << std::hex << pad << "header.NumberOfIdEntries: " << header.NumberOfIdEntries << std::endl; -*/ - obBuffer << header.Characteristics; - obBuffer << header.TimeDateStamp; - obBuffer << header.MajorVersion; - obBuffer << header.MinorVersion; - //std::cout << pad << "Children: " << children.size() << std::endl; - //std::cout << pad << std::count_if(children.begin(), children.end(), std::mem_fun_ref(&ResourceChild::isNamedResource)) << std::endl; - //std::cout << pad << children.size() - std::count_if(children.begin(), children.end(), std::mem_fun_ref(&ResourceChild::isNamedResource)) << std::endl; -// obBuffer << std::count_if(children.begin(), children.end(), std::mem_fun_ref(&ResourceChild::isNamedResource)); -// obBuffer << children.size() - std::count_if(children.begin(), children.end(), std::mem_fun_ref(&ResourceChild::isNamedResource)); - obBuffer << header.NumberOfNamedEntries; - obBuffer << header.NumberOfIdEntries; - - uiOffset += PELIB_IMAGE_RESOURCE_DIRECTORY::size(); - - for (unsigned int i=0;irebuild(obBuffer, newoffs, uiRva, pad + " "); - } - uiOffset = newoffs; - } - - /** - * Reads the next resource node from the InputBuffer. - * @param inpBuffer An InputBuffer that holds the complete resource directory. - * @param uiOffset Offset of the resource node that's to be read. - * @param uiRva RVA of the beginning of the resource directory. - * @param pad Something I need for debugging. Will be removed soon. - **/ - int ResourceNode::read(InputBuffer& inpBuffer, unsigned int uiOffset, unsigned int uiRva/*, const std::string& pad*/) - { - // Not enough space to be a valid node. - if (uiOffset + PELIB_IMAGE_RESOURCE_DIRECTORY::size() > inpBuffer.size()) - { - return 1; - } - - uiElementRva = uiOffset + uiRva; - - inpBuffer.set(uiOffset); - - inpBuffer >> header.Characteristics; - inpBuffer >> header.TimeDateStamp; - inpBuffer >> header.MajorVersion; - inpBuffer >> header.MinorVersion; - inpBuffer >> header.NumberOfNamedEntries; - inpBuffer >> header.NumberOfIdEntries; - - // Not enough space to be a valid node. - if (uiOffset + PELIB_IMAGE_RESOURCE_DIRECTORY::size() - + (header.NumberOfNamedEntries + header.NumberOfIdEntries) * PELIB_IMAGE_RESOURCE_DIRECTORY_ENTRY::size() - > inpBuffer.size()) - { - return 1; - } - -// std::cout << std::hex << pad << "Characteristics: " << header.Characteristics << std::endl; -// std::cout << std::hex << pad << "TimeDateStamp: " << header.TimeDateStamp << std::endl; -// std::cout << std::hex << pad << "MajorVersion: " << header.MajorVersion << std::endl; -// std::cout << std::hex << pad << "MinorVersion: " << header.MinorVersion << std::endl; -// std::cout << std::hex << pad << "NumberOfNamedEntries: " << header.NumberOfNamedEntries << std::endl; -// std::cout << std::hex << pad << "NumberOfIdEntries: " << header.NumberOfIdEntries << std::endl; - - for (int i=0;i> rc.entry.irde.Name; - inpBuffer >> rc.entry.irde.OffsetToData; - - unsigned int lastPos = inpBuffer.get(); - - if (rc.entry.irde.Name & PELIB_IMAGE_RESOURCE_NAME_IS_STRING) - { - // Enough space to read string length? - if ((rc.entry.irde.Name & ~PELIB_IMAGE_RESOURCE_NAME_IS_STRING) + 2 < inpBuffer.size()) - { - inpBuffer.set(rc.entry.irde.Name & ~PELIB_IMAGE_RESOURCE_NAME_IS_STRING); - word strlen; - inpBuffer >> strlen; - - // Enough space to read string? - if ((rc.entry.irde.Name & ~PELIB_IMAGE_RESOURCE_NAME_IS_STRING) + 2 * strlen < inpBuffer.size()) - { - wchar_t c; - for (word i=0;i> c; - rc.entry.wstrName += c; - } - } - } - -// std::wcout << rc.entry.wstrName << std::endl; - -// std::cout << strlen << std::endl; - inpBuffer.set(lastPos); - } - - if (rc.entry.irde.OffsetToData & PELIB_IMAGE_RESOURCE_DATA_IS_DIRECTORY) - { - rc.child = new ResourceNode; - rc.child->read(inpBuffer, rc.entry.irde.OffsetToData & ~PELIB_IMAGE_RESOURCE_DATA_IS_DIRECTORY, uiRva/*, pad + " "*/); - } - else - { - rc.child = new ResourceLeaf; - rc.child->read(inpBuffer, rc.entry.irde.OffsetToData, uiRva/*, pad + " "*/); - } -// std::cout << std::hex << pad << "Entry " << i << "(Name): " << rc.entry.irde.Name << std::endl; -// std::cout << std::hex << pad << "Entry " << i << "(Offset): " << rc.entry.irde.OffsetToData << std::endl; - - children.push_back(rc); - inpBuffer.set(lastPos); - } - - return 0; - } - - /** - * Returns the number of children of the current node. Note that this number is the number - * of defined children, not the value from the header. - * @return Number of node's children. - **/ - unsigned int ResourceNode::getNumberOfChildren() const - { - return static_cast(children.size()); - } - - /** - * Adds another child to the current node. - **/ - void ResourceNode::addChild() - { - ResourceChild c; - c.child = 0; - children.push_back(c); - } - - /** - * Returns a node's child. - * @param uiIndex Index of the child. - * @return The child identified by uiIndex. This child can be either a ResourceNode or a ResourceLeaf. - **/ - ResourceElement* ResourceNode::getChild(unsigned int uiIndex) - { - return children[uiIndex].child; - } - - /** - * Removes a child from the current node. - * @param uiIndex Index of the child. - **/ - void ResourceNode::removeChild(unsigned int uiIndex) - { - children.erase(children.begin() + uiIndex); - } - - /** - * Returns the name of a child. - * @param uiIndex Index of the child. - * @return Either the name of the specified child or an empty string. - **/ - std::string ResourceNode::getChildName(unsigned int uiIndex) const - { - return children[uiIndex].entry.wstrName; - } - - /** - * Returns the Name value of a child. - * @param uiIndex Index of the child. - * @return Name value of a child. - **/ - dword ResourceNode::getOffsetToChildName(unsigned int uiIndex) const - { - return children[uiIndex].entry.irde.Name; - } - - /** - * Returns the OffsetToData value of a child. - * @param uiIndex Index of the child. - * @return OffsetToData value of a child. - **/ - dword ResourceNode::getOffsetToChildData(unsigned int uiIndex) const - { - return children[uiIndex].entry.irde.OffsetToData; - } - - - /** - * Sets the name of a child. - * @param uiIndex Index of the child. - * @param strNewName New name of the resource. - **/ - void ResourceNode::setChildName(unsigned int uiIndex, const std::string& strNewName) - { - children[uiIndex].entry.wstrName = strNewName; - } - - /** - * Sets the Name value of a child. - * @param uiIndex Index of the child. - * @param dwNewOffset New Name value of the resource. - **/ - void ResourceNode::setOffsetToChildName(unsigned int uiIndex, dword dwNewOffset) - { - children[uiIndex].entry.irde.Name = dwNewOffset; - } - - /** - * Sets the OffsetToData value of a child. - * @param uiIndex Index of the child. - * @param dwNewOffset New OffsetToData value of the resource. - **/ - void ResourceNode::setOffsetToChildData(unsigned int uiIndex, dword dwNewOffset) - { - children[uiIndex].entry.irde.OffsetToData = dwNewOffset; - } - - /** - * Returns the Characteristics value of the node. - * @return Characteristics value of the node. - **/ - dword ResourceNode::getCharacteristics() const - { - return header.Characteristics; - } - - /** - * Returns the TimeDateStamp value of the node. - * @return TimeDateStamp value of the node. - **/ - dword ResourceNode::getTimeDateStamp() const - { - return header.TimeDateStamp; - } - - /** - * Returns the MajorVersion value of the node. - * @return MajorVersion value of the node. - **/ - word ResourceNode::getMajorVersion() const - { - return header.MajorVersion; - } - - /** - * Returns the MinorVersion value of the node. - * @return MinorVersion value of the node. - **/ - word ResourceNode::getMinorVersion() const - { - return header.MinorVersion; - } - - /** - * Returns the NumberOfNamedEntries value of the node. - * @return NumberOfNamedEntries value of the node. - **/ - word ResourceNode::getNumberOfNamedEntries() const - { - return header.NumberOfNamedEntries; - } - - /** - * Returns the NumberOfIdEntries value of the node. - * @return NumberOfIdEntries value of the node. - **/ - word ResourceNode::getNumberOfIdEntries() const - { - return header.NumberOfIdEntries; - } - - /** - * Sets the Characteristics value of the node. - * @param value New Characteristics value of the node. - **/ - void ResourceNode::setCharacteristics(dword value) - { - header.Characteristics = value; - } - - /** - * Sets the TimeDateStamp value of the node. - * @param value New TimeDateStamp value of the node. - **/ - void ResourceNode::setTimeDateStamp(dword value) - { - header.TimeDateStamp = value; - } - - /** - * Sets the MajorVersion value of the node. - * @param value New MajorVersion value of the node. - **/ - void ResourceNode::setMajorVersion(word value) - { - header.MajorVersion = value; - } - - /** - * Sets the MinorVersion value of the node. - * @param value New MinorVersion value of the node. - **/ - void ResourceNode::setMinorVersion(word value) - { - header.MinorVersion = value; - } - - /** - * Sets the NumberOfNamedEntries value of the node. - * @param value New NumberOfNamedEntries value of the node. - **/ - void ResourceNode::setNumberOfNamedEntries(word value) - { - header.NumberOfNamedEntries = value; - } - - /** - * Sets the NumberOfIdEntries value of the node. - * @param value New NumberOfIdEntries value of the node. - **/ - void ResourceNode::setNumberOfIdEntries(word value) - { - header.NumberOfIdEntries = value; - } - - -/* /// Returns the size of a resource node. - unsigned int ResourceNode::size() const - { - if (children.size()) - { - std::cout << std::accumulate(children.begin(), children.end(), 0, accumulate) << std::endl; - return PELIB_IMAGE_RESOURCE_DIRECTORY::size() - + std::accumulate(children.begin(), children.end(), 0, accumulate); - } - else - { - return 0; - } - } -*/ -// -------------------------------------------------- ResourceDirectory ------------------------------------------- - - /** - * Returns the root node of the resource directory. - * @return Root node of the resource directory. - **/ - ResourceNode* ResourceDirectory::getRoot() - { - return &m_rnRoot; - } - - /** - * Correctly sorts the resource nodes of the resource tree. This function should be called - * before calling rebuild. - **/ - void ResourceDirectory::makeValid() - { - m_rnRoot.makeValid(); - } - - /** - * Reads the resource directory from a file. - * @param strFilename Name of the file. - * @param uiOffset File offset of the resource directory. - * @param uiSize Raw size of the resource directory. - * @param uiResDirRva RVA of the beginning of the resource directory. - **/ - int ResourceDirectory::read(const std::string& strFilename, unsigned int uiOffset, unsigned int uiSize, unsigned int uiResDirRva) - { - if (!uiSize || !uiOffset) - { - return 1; - } - - std::ifstream ifFile(strFilename.c_str(), std::ios::binary); - - if (!ifFile) - { -// throw Exceptions::CannotOpenFile(ResourceDirectoryId, __LINE__); - return 1; - } - - if (fileSize(ifFile) < uiOffset + uiSize) - { -// throw Exceptions::InvalidFormat(ResourceDirectoryId, __LINE__); - return 1; - } - - ifFile.seekg(uiOffset, std::ios::beg); - - PELIB_IMAGE_RESOURCE_DIRECTORY irdCurrRoot; - - std::vector vResourceDirectory(uiSize); - ifFile.read(reinterpret_cast(&vResourceDirectory[0]), uiSize); - - InputBuffer inpBuffer(vResourceDirectory); - -// ResourceNode currNode; - return m_rnRoot.read(inpBuffer, 0, uiResDirRva/*, ""*/); -// std::swap(currNode, m_rnRoot); - } - - /** - * Rebuilds the resource directory. - * @param vBuffer Buffer the source directory will be written to. - * @param uiRva RVA of the resource directory. - **/ - void ResourceDirectory::rebuild(std::vector& vBuffer, unsigned int uiRva) const - { - OutputBuffer obBuffer(vBuffer); - unsigned int offs = 0; -// std::cout << "Root: " << m_rnRoot.children.size() << std::endl; - m_rnRoot.rebuild(obBuffer, offs, uiRva, ""); - } - - /** - * Returns the size of the entire rebuilt resource directory. That's the size of the entire - * structure as it's written back to a file. - **/ -/* unsigned int ResourceDirectory::size() const - { - return m_rnRoot.size(); - } -*/ - /** - * Writes the current resource directory back into a file. - * @param strFilename Name of the output file. - * @param uiOffset File offset where the resource directory will be written to. - * @param uiRva RVA of the file offset. - **/ - int ResourceDirectory::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 vBuffer; - rebuild(vBuffer, uiRva); - - ofFile.write(reinterpret_cast(&vBuffer[0]), static_cast(vBuffer.size())); - - ofFile.close(); - - return 0; - } - - /** - * Adds another resource type. The new resource type is identified by the ID dwResTypeId. - * @param dwResTypeId ID which identifies the resource type. - **/ - int ResourceDirectory::addResourceType(dword dwResTypeId) - { - std::vector::iterator Iter = std::find_if(m_rnRoot.children.begin(), m_rnRoot.children.end(), std::bind2nd(std::mem_fun_ref(&ResourceChild::equalId), dwResTypeId)); - if (Iter != m_rnRoot.children.end()) - { - return 1; - // throw Exceptions::EntryAlreadyExists(ResourceDirectoryId, __LINE__); - } - - ResourceChild rcCurr; - rcCurr.child = new ResourceNode; - rcCurr.entry.irde.Name = dwResTypeId; - m_rnRoot.children.push_back(rcCurr); - - return 0; - } - - /** - * Adds another resource type. The new resource type is identified by the name strResTypeName. - * @param strResTypeName Name which identifies the resource type. - **/ - int ResourceDirectory::addResourceType(const std::string& strResTypeName) - { - std::vector::iterator Iter = std::find_if(m_rnRoot.children.begin(), m_rnRoot.children.end(), std::bind2nd(std::mem_fun_ref(&ResourceChild::equalName), strResTypeName)); - if (Iter != m_rnRoot.children.end()) - { - return 1; -// throw Exceptions::EntryAlreadyExists(ResourceDirectoryId, __LINE__); - } - - ResourceChild rcCurr; - rcCurr.entry.wstrName = strResTypeName; - rcCurr.child = new ResourceNode; - m_rnRoot.children.push_back(rcCurr); - - return 0; - } - - /** - * Removes the resource type identified by the ID dwResTypeId. - * @param dwResTypeId ID which identifies the resource type. - **/ - int ResourceDirectory::removeResourceType(dword dwResTypeId) - { - std::vector::iterator Iter = std::find_if(m_rnRoot.children.begin(), m_rnRoot.children.end(), std::bind2nd(std::mem_fun_ref(&ResourceChild::equalId), dwResTypeId)); - if (Iter == m_rnRoot.children.end()) - { - return 1; -// throw Exceptions::ResourceTypeDoesNotExist(ResourceDirectoryId, __LINE__); - } - - bool isNamed = false; - if (Iter->isNamedResource()) isNamed = true; - - m_rnRoot.children.erase(Iter); - - if (isNamed) m_rnRoot.header.NumberOfNamedEntries = static_cast(m_rnRoot.children.size()); - else m_rnRoot.header.NumberOfIdEntries = static_cast(m_rnRoot.children.size()); - - return 0; - } - - /** - * Removes the resource type identified by the name strResTypeName. - * @param strResTypeName Name which identifies the resource type. - **/ - int ResourceDirectory::removeResourceType(const std::string& strResTypeName) - { - std::vector::iterator Iter = std::find_if(m_rnRoot.children.begin(), m_rnRoot.children.end(), std::bind2nd(std::mem_fun_ref(&ResourceChild::equalName), strResTypeName)); - if (Iter == m_rnRoot.children.end()) - { - return 1; - // throw Exceptions::ResourceTypeDoesNotExist(ResourceDirectoryId, __LINE__); - } - - bool isNamed = false; - if (Iter->isNamedResource()) isNamed = true; - - m_rnRoot.children.erase(Iter); - - if (isNamed) m_rnRoot.header.NumberOfNamedEntries = static_cast(m_rnRoot.children.size()); - else m_rnRoot.header.NumberOfIdEntries = static_cast(m_rnRoot.children.size()); - - return 0; - } - - /** - * Removes the resource type identified by the index uiIndex. - * @param uiIndex Index which identifies the resource type. - **/ - int ResourceDirectory::removeResourceTypeByIndex(unsigned int uiIndex) - { - bool isNamed = false; - if (m_rnRoot.children[uiIndex].isNamedResource()) isNamed = true; - - m_rnRoot.children.erase(m_rnRoot.children.begin() + uiIndex); - - if (isNamed) m_rnRoot.header.NumberOfNamedEntries = static_cast(m_rnRoot.children.size()); - else m_rnRoot.header.NumberOfIdEntries = static_cast(m_rnRoot.children.size()); - - return 0; - } - - /** - * Adds another resource to the resource tree. The first parameter identifies the resource type - * of the new resource, the second parameter identifies the resource itself. - * @param dwResTypeId ID of the resource type. - * @param dwResId ID of the resource. - **/ - int ResourceDirectory::addResource(dword dwResTypeId, dword dwResId) - { - ResourceChild rcCurr; - rcCurr.entry.irde.Name = dwResId; - return addResourceT(dwResTypeId, dwResId, rcCurr); - } - - /** - * Adds another resource to the resource tree. The first parameter identifies the resource type - * of the new resource, the second parameter identifies the resource itself. - * @param dwResTypeId ID of the resource type. - * @param strResName Name of the resource. - **/ - int ResourceDirectory::addResource(dword dwResTypeId, const std::string& strResName) - { - ResourceChild rcCurr; - rcCurr.entry.wstrName = strResName; - return addResourceT(dwResTypeId, strResName, rcCurr); - } - - /** - * Adds another resource to the resource tree. The first parameter identifies the resource type - * of the new resource, the second parameter identifies the resource itself. - * @param strResTypeName Name of the resource type. - * @param dwResId ID of the resource. - **/ - int ResourceDirectory::addResource(const std::string& strResTypeName, dword dwResId) - { - ResourceChild rcCurr; - rcCurr.entry.irde.Name = dwResId; - return addResourceT(strResTypeName, dwResId, rcCurr); - } - - /** - * Adds another resource to the resource tree. The first parameter identifies the resource type - * of the new resource, the second parameter identifies the resource itself. - * @param strResTypeName Name of the resource type. - * @param strResName Name of the resource. - **/ - int ResourceDirectory::addResource(const std::string& strResTypeName, const std::string& strResName) - { - ResourceChild rcCurr; - rcCurr.entry.wstrName = strResName; - return addResourceT(strResTypeName, strResName, rcCurr); - } - - /** - * Removes a resource from the resource tree. The first parameter identifies the resource type - * of the new resource, the second parameter identifies the resource itself. - * @param dwResTypeIndex ID of the resource type. - * @param dwResId ID of the resource. - **/ - int ResourceDirectory::removeResource(dword dwResTypeIndex, dword dwResId) - { - return removeResourceT(dwResTypeIndex, dwResId); - } - - /** - * Removes a resource from the resource tree. The first parameter identifies the resource type - * of the new resource, the second parameter identifies the resource itself. - * @param dwResTypeIndex ID of the resource type. - * @param strResName Name of the resource. - **/ - int ResourceDirectory::removeResource(dword dwResTypeIndex, const std::string& strResName) - { - return removeResourceT(dwResTypeIndex, strResName); - } - - /** - * Removes a resource from the resource tree. The first parameter identifies the resource type - * of the new resource, the second parameter identifies the resource itself. - * @param strResTypeName Name of the resource type. - * @param dwResId ID of the resource. - **/ - int ResourceDirectory::removeResource(const std::string& strResTypeName, dword dwResId) - { - return removeResourceT(strResTypeName, dwResId); - } - - /** - * Removes a resource from the resource tree. The first parameter identifies the resource type - * of the new resource, the second parameter identifies the resource itself. - * @param strResTypeName Name of the resource type. - * @param strResName Name of the resource. - **/ - int ResourceDirectory::removeResource(const std::string& strResTypeName, const std::string& strResName) - { - return removeResourceT(strResTypeName, strResName); - } - - /** - * Returns the number of resource types. - **/ - unsigned int ResourceDirectory::getNumberOfResourceTypes() const - { - return static_cast(m_rnRoot.children.size()); - } - - /** - * Returns the ID of a resource type which was specified through an index. - * The valid range of the parameter uiIndex is 0...getNumberOfResourceTypes() - 1. - * Leaving the invalid range leads to undefined behaviour. - * @param uiIndex Index which identifies a resource type. - * @return The ID of the specified resource type. - **/ - dword ResourceDirectory::getResourceTypeIdByIndex(unsigned int uiIndex) const - { - return m_rnRoot.children[uiIndex].entry.irde.Name; - } - - /** - * Returns the name of a resource type which was specified through an index. - * The valid range of the parameter uiIndex is 0...getNumberOfResourceTypes() - 1. - * Leaving the invalid range leads to undefined behaviour. - * @param uiIndex Index which identifies a resource type. - * @return The name of the specified resource type. - **/ - std::string ResourceDirectory::getResourceTypeNameByIndex(unsigned int uiIndex) const - { - return m_rnRoot.children[uiIndex].entry.wstrName; - } - - /** - * Converts the ID of a resource type to an index. - * @param dwResTypeId ID of the resource type. - * @return Index of that resource type. - **/ - int ResourceDirectory::resourceTypeIdToIndex(dword dwResTypeId) const - { - std::vector::const_iterator Iter = std::find_if(m_rnRoot.children.begin(), m_rnRoot.children.end(), std::bind2nd(std::mem_fun_ref(&ResourceChild::equalId), dwResTypeId)); - if (Iter == m_rnRoot.children.end()) return -1; - return static_cast(std::distance(m_rnRoot.children.begin(), Iter)); - } - - /** - * Converts the name of a resource type to an index. - * @param strResTypeName ID of the resource type. - * @return Index of that resource type. - **/ - int ResourceDirectory::resourceTypeNameToIndex(const std::string& strResTypeName) const - { - std::vector::const_iterator Iter = std::find_if(m_rnRoot.children.begin(), m_rnRoot.children.end(), std::bind2nd(std::mem_fun_ref(&ResourceChild::equalName), strResTypeName)); - if (Iter == m_rnRoot.children.end()) return -1; - return static_cast(std::distance(m_rnRoot.children.begin(), Iter)); - } - - /** - * Returns the number of resources of a specific resource type. - * @param dwId ID of the resource type. - * @return Number of resources of resource type dwId. - **/ - unsigned int ResourceDirectory::getNumberOfResources(dword dwId) const - { -// std::vector::const_iterator IterD = m_rnRoot.children.begin(); -// std::cout << dwId << std::endl; -// while (IterD != m_rnRoot.children.end()) -// { -// std::cout << IterD->entry.irde.Name << std::endl; -// ++IterD; -// } - - std::vector::const_iterator Iter = std::find_if(m_rnRoot.children.begin(), m_rnRoot.children.end(), std::bind2nd(std::mem_fun_ref(&ResourceChild::equalId), dwId)); - if (Iter == m_rnRoot.children.end()) - { - return 0xFFFFFFFF; - } - else - { - ResourceNode* currNode = static_cast(Iter->child); - return static_cast(currNode->children.size()); - } - } - - /** - * Returns the number of resources of a specific resource type. - * @param strResTypeName Name of the resource type. - * @return Number of resources of resource type strResTypeName. - **/ - unsigned int ResourceDirectory::getNumberOfResources(const std::string& strResTypeName) const - { - std::vector::const_iterator Iter = std::find_if(m_rnRoot.children.begin(), m_rnRoot.children.end(), std::bind2nd(std::mem_fun_ref(&ResourceChild::equalName), strResTypeName)); - if (Iter == m_rnRoot.children.end()) - { - return 0xFFFFFFFF; - } - else - { - ResourceNode* currNode = static_cast(Iter->child); - return static_cast(currNode->children.size()); - } - } - - /** - * Returns the number of resources of a resource type which was specified through an index. - * The valid range of the parameter uiIndex is 0...getNumberOfResourceTypes() - 1. - * Leaving the invalid range leads to undefined behaviour. - * @param uiIndex Index which identifies a resource type. - * @return The number of resources of the specified resource type. - **/ - unsigned int ResourceDirectory::getNumberOfResourcesByIndex(unsigned int uiIndex) const - { - ResourceNode* currNode = static_cast(m_rnRoot.children[uiIndex].child); - return static_cast(currNode->children.size()); - } - - /** - * Gets the resource data of a specific resource. - * @param dwResTypeId Identifies the resource type of the resource. - * @param dwResId Identifies the resource. - * @param data Vector where the data is stored. - **/ - void ResourceDirectory::getResourceData(dword dwResTypeId, dword dwResId, std::vector& data) const - { - getResourceDataT(dwResTypeId, dwResId, data); - } - - /** - * Gets the resource data of a specific resource. - * @param dwResTypeId Identifies the resource type of the resource. - * @param strResName Identifies the resource. - * @param data Vector where the data is stored. - **/ - void ResourceDirectory::getResourceData(dword dwResTypeId, const std::string& strResName, std::vector& data) const - { - getResourceDataT(dwResTypeId, strResName, data); - } - - /** - * Gets the resource data of a specific resource. - * @param strResTypeName Identifies the resource type of the resource. - * @param dwResId Identifies the resource. - * @param data Vector where the data is stored. - **/ - void ResourceDirectory::getResourceData(const std::string& strResTypeName, dword dwResId, std::vector& data) const - { - getResourceDataT(strResTypeName, dwResId, data); - } - - /** - * Gets the resource data of a specific resource. - * @param strResTypeName Identifies the resource type of the resource. - * @param strResName Identifies the resource. - * @param data Vector where the data is stored. - **/ - void ResourceDirectory::getResourceData(const std::string& strResTypeName, const std::string& strResName, std::vector& data) const - { - getResourceDataT(strResTypeName, strResName, data); - } - - /** - * Gets the resource data of a specific resource by index. - * The valid range of the parameter uiResTypeIndex is 0...getNumberOfResourceTypes() - 1. - * The valid range of the parameter uiResIndex is 0...getNumberOfResources() - 1. - * Leaving the invalid range leads to undefined behaviour. - * @param uiResTypeIndex Identifies the resource type of the resource. - * @param uiResIndex Identifies the resource. - * @param data Vector where the data is stored. - **/ - void ResourceDirectory::getResourceDataByIndex(unsigned int uiResTypeIndex, unsigned int uiResIndex, std::vector& data) const - { - ResourceNode* currNode = static_cast(m_rnRoot.children[uiResTypeIndex].child); - currNode = static_cast(currNode->children[uiResIndex].child); - ResourceLeaf* currLeaf = static_cast(currNode->children[0].child); - - data.assign(currLeaf->m_data.begin(), currLeaf->m_data.end()); - } - - /** - * Sets the resource data of a specific resource. - * @param dwResTypeId Identifies the resource type of the resource. - * @param dwResId Identifies the resource. - * @param data The new resource data. - **/ - void ResourceDirectory::setResourceData(dword dwResTypeId, dword dwResId, std::vector& data) - { - setResourceDataT(dwResTypeId, dwResId, data); - } - - /** - * Sets the resource data of a specific resource. - * @param dwResTypeId Identifies the resource type of the resource. - * @param strResName Identifies the resource. - * @param data The new resource data. - **/ - void ResourceDirectory::setResourceData(dword dwResTypeId, const std::string& strResName, std::vector& data) - { - setResourceDataT(dwResTypeId, strResName, data); - } - - /** - * Sets the resource data of a specific resource. - * @param strResTypeName Identifies the resource type of the resource. - * @param dwResId Identifies the resource. - * @param data The new resource data. - **/ - void ResourceDirectory::setResourceData(const std::string& strResTypeName, dword dwResId, std::vector& data) - { - setResourceDataT(strResTypeName, dwResId, data); - } - - /** - * Sets the resource data of a specific resource. - * @param strResTypeName Identifies the resource type of the resource. - * @param strResName Identifies the resource. - * @param data The new resource data. - **/ - void ResourceDirectory::setResourceData(const std::string& strResTypeName, const std::string& strResName, std::vector& data) - { - setResourceDataT(strResTypeName, strResName, data); - } - - /** - * Sets the resource data of a specific resource by index. - * The valid range of the parameter uiResTypeIndex is 0...getNumberOfResourceTypes() - 1. - * The valid range of the parameter uiResIndex is 0...getNumberOfResources() - 1. - * Leaving the invalid range leads to undefined behaviour. - * @param uiResTypeIndex Identifies the resource type of the resource. - * @param uiResIndex Identifies the resource. - * @param data The new resource data. - **/ - void ResourceDirectory::setResourceDataByIndex(unsigned int uiResTypeIndex, unsigned int uiResIndex, std::vector& data) - { - ResourceNode* currNode = static_cast(m_rnRoot.children[uiResTypeIndex].child); - currNode = static_cast(currNode->children[uiResIndex].child); - ResourceLeaf* currLeaf = static_cast(currNode->children[0].child); - currLeaf->m_data.assign(data.begin(), data.end()); - } - - /** - * Gets the ID of a specific resource. - * @param dwResTypeId Identifies the resource type of the resource. - * @param strResName Identifies the resource. - * @return ID of the specified resource. - **/ - dword ResourceDirectory::getResourceId(dword dwResTypeId, const std::string& strResName) const - { - return getResourceIdT(dwResTypeId, strResName); - } - - /** - * Gets the ID of a specific resource. - * @param strResTypeName Identifies the resource type of the resource. - * @param strResName Identifies the resource. - * @return ID of the specified resource. - **/ - dword ResourceDirectory::getResourceId(const std::string& strResTypeName, const std::string& strResName) const - { - return getResourceIdT(strResTypeName, strResName); - } - - /** - * Gets the ID of a specific resource by index. - * @param uiResTypeIndex Identifies the resource type of the resource. - * @param uiResIndex Identifies the resource. - * @return ID of the specified resource. - **/ - dword ResourceDirectory::getResourceIdByIndex(unsigned int uiResTypeIndex, unsigned int uiResIndex) const - { - ResourceNode* currNode = static_cast(m_rnRoot.children[uiResTypeIndex].child); - return currNode->children[uiResIndex].entry.irde.Name; - } - - /** - * Sets the ID of a specific resource. - * @param dwResTypeId Identifies the resource type of the resource. - * @param dwResId Identifies the resource. - * @param dwNewResId New ID of the resource. - **/ - void ResourceDirectory::setResourceId(dword dwResTypeId, dword dwResId, dword dwNewResId) - { - setResourceIdT(dwResTypeId, dwResId, dwNewResId); - } - - /** - * Sets the ID of a specific resource. - * @param dwResTypeId Identifies the resource type of the resource. - * @param strResName Identifies the resource. - * @param dwNewResId New ID of the resource. - **/ - void ResourceDirectory::setResourceId(dword dwResTypeId, const std::string& strResName, dword dwNewResId) - { - setResourceIdT(dwResTypeId, strResName, dwNewResId); - } - - /** - * Sets the ID of a specific resource. - * @param strResTypeName Identifies the resource type of the resource. - * @param dwResId Identifies the resource. - * @param dwNewResId New ID of the resource. - **/ - void ResourceDirectory::setResourceId(const std::string& strResTypeName, dword dwResId, dword dwNewResId) - { - setResourceIdT(strResTypeName, dwResId, dwNewResId); - } - - /** - * Sets the ID of a specific resource. - * @param strResTypeName Identifies the resource type of the resource. - * @param strResName Identifies the resource. - * @param dwNewResId New ID of the resource. - **/ - void ResourceDirectory::setResourceId(const std::string& strResTypeName, const std::string& strResName, dword dwNewResId) - { - setResourceIdT(strResTypeName, strResName, dwNewResId); - } - - /** - * Sets the ID of a specific resource by index. - * @param uiResTypeIndex Identifies the resource type of the resource. - * @param uiResIndex Identifies the resource. - * @param dwNewResId New ID of the specified resource. - **/ - void ResourceDirectory::setResourceIdByIndex(unsigned int uiResTypeIndex, unsigned int uiResIndex, dword dwNewResId) - { - ResourceNode* currNode = static_cast(m_rnRoot.children[uiResTypeIndex].child); - currNode->children[uiResIndex].entry.irde.Name = dwNewResId; - } - - /** - * Gets the Name of a specific resource. - * @param dwResTypeId Identifies the resource type of the resource. - * @param dwResId Identifies the resource. - * @return Name of the specified resource. - **/ - std::string ResourceDirectory::getResourceName(dword dwResTypeId, dword dwResId) const - { - return getResourceNameT(dwResTypeId, dwResId); - } - - /** - * Gets the Name of a specific resource. - * @param strResTypeName Identifies the resource type of the resource. - * @param dwResId Identifies the resource. - * @return Name of the specified resource. - **/ - std::string ResourceDirectory::getResourceName(const std::string& strResTypeName, dword dwResId) const - { - return getResourceNameT(strResTypeName, dwResId); - } - - /** - * Gets the name of a specific resource by index. - * @param uiResTypeIndex Identifies the resource type of the resource. - * @param uiResIndex Identifies the resource. - * @return Name of the specified resource. - **/ - std::string ResourceDirectory::getResourceNameByIndex(unsigned int uiResTypeIndex, unsigned int uiResIndex) const - { - ResourceNode* currNode = static_cast(m_rnRoot.children[uiResTypeIndex].child); - return currNode->children[uiResIndex].entry.wstrName; - } - - /** - * Sets the name of a specific resource. - * @param dwResTypeId Identifies the resource type of the resource. - * @param dwResId Identifies the resource. - * @param strNewResName New name of the specified resource. - **/ - void ResourceDirectory::setResourceName(dword dwResTypeId, dword dwResId, const std::string& strNewResName) - { - setResourceNameT(dwResTypeId, dwResId, strNewResName); - } - - /** - * Sets the name of a specific resource. - * @param dwResTypeId Identifies the resource type of the resource. - * @param strResName Identifies the resource. - * @param strNewResName New name of the specified resource. - **/ - void ResourceDirectory::setResourceName(dword dwResTypeId, const std::string& strResName, const std::string& strNewResName) - { - setResourceNameT(dwResTypeId, strResName, strNewResName); - } - - /** - * Sets the name of a specific resource. - * @param strResTypeName Identifies the resource type of the resource. - * @param dwResId Identifies the resource. - * @param strNewResName New name of the specified resource. - **/ - void ResourceDirectory::setResourceName(const std::string& strResTypeName, dword dwResId, const std::string& strNewResName) - { - setResourceNameT(strResTypeName, dwResId, strNewResName); - } - - /** - * Sets the name of a specific resource. - * @param strResTypeName Identifies the resource type of the resource. - * @param strResName Identifies the resource. - * @param strNewResName New name of the specified resource. - **/ - void ResourceDirectory::setResourceName(const std::string& strResTypeName, const std::string& strResName, const std::string& strNewResName) - { - setResourceNameT(strResTypeName, strResName, strNewResName); - } - - /** - * Sets the name of a specific resource by index. - * @param uiResTypeIndex Identifies the resource type of the resource. - * @param uiResIndex Identifies the resource. - * @param strNewResName New name of the specified resource. - **/ - void ResourceDirectory::setResourceNameByIndex(unsigned int uiResTypeIndex, unsigned int uiResIndex, const std::string& strNewResName) - { - ResourceNode* currNode = static_cast(m_rnRoot.children[uiResTypeIndex].child); - currNode->children[uiResIndex].entry.wstrName = strNewResName; - } - -} +/* +* ResourceDirectory.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 "ResourceDirectory.h" + +namespace PeLib +{ +// -------------------------------------------------- ResourceChild ------------------------------------------- + + ResourceChild::ResourceChild() + { + } + + ResourceChild::ResourceChild(const ResourceChild& rhs) + { + if (dynamic_cast(rhs.child)) + { + ResourceNode* oldnode = static_cast(rhs.child); + + entry = rhs.entry; + + child = new ResourceNode; + child->uiElementRva = rhs.child->getElementRva(); + static_cast(child)->header = oldnode->header; + static_cast(child)->children = oldnode->children; + } + else + { + ResourceLeaf* oldnode = static_cast(rhs.child); + + child = new ResourceLeaf; + child->uiElementRva = rhs.child->getElementRva(); + static_cast(child)->m_data = oldnode->m_data; + static_cast(child)->entry = oldnode->entry; + } + } + + ResourceChild& ResourceChild::operator=(const ResourceChild& rhs) + { + if (this != &rhs) + { + if (dynamic_cast(rhs.child)) + { + ResourceNode* oldnode = static_cast(rhs.child); + + entry = rhs.entry; + + child = new ResourceNode; + child->uiElementRva = rhs.child->getElementRva(); + static_cast(child)->header = oldnode->header; + static_cast(child)->children = oldnode->children; + } + else + { + ResourceLeaf* oldnode = static_cast(rhs.child); + + child = new ResourceLeaf; + child->uiElementRva = rhs.child->getElementRva(); + static_cast(child)->m_data = oldnode->m_data; + static_cast(child)->entry = oldnode->entry; + } + } + + return *this; + } + + ResourceChild::~ResourceChild() + { + delete child; + } + + /** + * Compares the resource child's id to the parameter dwId. + * @param dwId ID of a resource. + * @return True, if the resource child's id equals the parameter. + **/ + bool ResourceChild::equalId(dword dwId) const + { + return entry.irde.Name == dwId; + } + + /** + * Compares the resource child's name to the parameter strName. + * @param strName ID of a resource. + * @return True, if the resource child's name equals the parameter. + **/ + bool ResourceChild::equalName(std::string strName) const + { + return entry.wstrName == strName; + } + + /** + * Returns true if the resource was given a name. + **/ + bool ResourceChild::isNamedResource() const + { + return entry.wstrName.size() != 0; + } + + /** + * The children of a resource must be ordered in a certain way. First come the named resources + * in sorted order, afterwards followed the unnamed resources in sorted order. + **/ + bool ResourceChild::operator<(const ResourceChild& rc) const + { + if (this->isNamedResource() && !rc.isNamedResource()) + { + return true; + } + else if (!this->isNamedResource() && rc.isNamedResource()) + { + return false; + } + else if (this->isNamedResource() && rc.isNamedResource()) + { + return this->entry.wstrName < rc.entry.wstrName; + } + else + { + return this->entry.irde.Name < rc.entry.irde.Name; + } + } + +/* unsigned int ResourceChild::size() const + { + return PELIB_IMAGE_RESOURCE_DIRECTORY_ENTRY::size() + + child->size() + + (entry.wstrName.size() ? entry.wstrName.size() + 2 : 0); + } +*/ +// -------------------------------------------------- ResourceElement ------------------------------------------- + + /** + * Returns the RVA of a ResourceElement. This is the RVA where the ResourceElement can be + * found in the file. + * @return RVA of the ResourceElement. + **/ + unsigned int ResourceElement::getElementRva() const + { + return uiElementRva; + } + +// -------------------------------------------------- ResourceLeaf ------------------------------------------- + + /** + * Checks if a ResourceElement is a leaf or not. + * @return Always returns true. + **/ + bool ResourceLeaf::isLeaf() const + { + return true; + } + + /** + * Reads the next resource leaf from the InputBuffer. + * @param inpBuffer An InputBuffer that holds the complete resource directory. + * @param uiOffset Offset of the resource leaf that's to be read. + * @param uiRva RVA of the beginning of the resource directory. + * @param pad Used for debugging purposes. + **/ + int ResourceLeaf::read(InputBuffer& inpBuffer, unsigned int uiOffset, unsigned int uiRva/*, const std::string& pad*/) + { +// std::cout << pad << "Leaf:" << std::endl; + + // Invalid leaf. + if (uiOffset + PELIB_IMAGE_RESOURCE_DATA_ENTRY::size() > inpBuffer.size()) + { + return 1; + } + + uiElementRva = uiOffset + uiRva; + + inpBuffer.set(uiOffset); + + inpBuffer >> entry.OffsetToData; + inpBuffer >> entry.Size; + inpBuffer >> entry.CodePage; + inpBuffer >> entry.Reserved; + +/* std::cout << pad << std::hex << "Offset: " << entry.OffsetToData << std::endl; + std::cout << pad << std::hex << "Size: " << entry.Size << std::endl; + std::cout << pad << std::hex << "CodePage: " << entry.CodePage << std::endl; + std::cout << pad << std::hex << "Reserved: " << entry.Reserved << std::endl; +*/ + // Invalid leaf. + if (entry.OffsetToData - uiRva + entry.Size > inpBuffer.size()) + { +// std::cout << entry.OffsetToData << " XXX " << uiRva << " - " << entry.Size << " - " << inpBuffer.size() << std::endl; + return 1; + } + +// std::cout << entry.OffsetToData << " - " << uiRva << " - " << entry.Size << " - " << inpBuffer.size() << std::endl; + inpBuffer.set(entry.OffsetToData - uiRva); + m_data.assign(inpBuffer.data() + inpBuffer.get(), inpBuffer.data() + inpBuffer.get() + entry.Size); +// std::cout << pad << std::hex << "Vector: " << m_data.size() << std::endl; +// std::copy(m_data.begin(), m_data.end(), std::ostream_iterator(std::cout << std::hex, " ")); + return 0; + } + + /** + * Rebuilds the current resource leaf. + * @param obBuffer OutputBuffer where the rebuilt resource leaf is stored. + * @param uiOffset Offset of the resource leaf inside the resource directory. + * @param uiRva RVA of the resource directory. + **/ + void ResourceLeaf::rebuild(OutputBuffer& obBuffer, unsigned int& uiOffset, unsigned int uiRva, const std::string&) const + { +// std::cout << std::hex << pad << "Leaf: " << uiOffset << std::endl; + uiOffset += PELIB_IMAGE_RESOURCE_DATA_ENTRY::size(); + +// obBuffer << entry.OffsetToData; +// obBuffer << uiOffset; + obBuffer << uiRva + uiOffset + (uiOffset % 4); + obBuffer << entry.Size; + obBuffer << entry.CodePage; + obBuffer << entry.Reserved; + + while (uiOffset % 4) + { + uiOffset++; + obBuffer << (byte)0; + } + + uiOffset += static_cast(m_data.size()); + + for (unsigned int i=0;i(m_data.size()); + } + +/* /// Returns the size of a resource leaf. + unsigned int ResourceLeaf::size() const + { + return PELIB_IMAGE_RESOURCE_DATA_ENTRY::size() + m_data.size(); + } +*/ + + /** + * Returns a vector that contains the raw data of a resource leaf. + * @return Raw data of the resource. + **/ + std::vector ResourceLeaf::getData() const + { + return m_data; + } + + /** + * Overwrites the raw data of a resource. + * @param vData New data of the resource. + **/ + void ResourceLeaf::setData(const std::vector& vData) + { + m_data = vData; + } + + /** + * Returns the leaf's OffsetToData value. That's the RVA where the raw data of the resource + * can be found. + * @return The leaf's OffsetToData value. + **/ + dword ResourceLeaf::getOffsetToData() const + { + return entry.OffsetToData; + } + + /** + * Returns the leaf's Size value. That's the size of the raw data of the resource. + * @return The leaf's Size value. + **/ + dword ResourceLeaf::getSize() const + { + return entry.Size; + } + + /** + * Returns the leaf's CodePage value. + * @return The leaf's CodePage value. + **/ + dword ResourceLeaf::getCodePage() const + { + return entry.CodePage; + } + + /** + * Returns the leaf's Reserved value. + * @return The leaf's Reserved value. + **/ + dword ResourceLeaf::getReserved() const + { + return entry.Reserved; + } + + /** + * Sets the leaf's OffsetToData value. + * @param dwValue The leaf's new OffsetToData value. + **/ + void ResourceLeaf::setOffsetToData(dword dwValue) + { + entry.OffsetToData = dwValue; + } + + /** + * Sets the leaf's Size value. + * @param dwValue The leaf's new Size value. + **/ + void ResourceLeaf::setSize(dword dwValue) + { + entry.Size = dwValue; + } + + /** + * Sets the leaf's CodePage value. + * @param dwValue The leaf's new CodePage value. + **/ + void ResourceLeaf::setCodePage(dword dwValue) + { + entry.CodePage = dwValue; + } + + /** + * Sets the leaf's Reserved value. + * @param dwValue The leaf's new Reserved value. + **/ + void ResourceLeaf::setReserved(dword dwValue) + { + entry.Reserved = dwValue; + } + + +// -------------------------------------------------- ResourceNode ------------------------------------------- + + /** + * Checks if a ResourceElement is a leaf or not. + * @return Always returns false. + **/ + bool ResourceNode::isLeaf() const + { + return false; + } + + /** + * Sorts the node's children and corrects the node's header. + **/ + void ResourceNode::makeValid() + { + std::sort(children.begin(), children.end()); + header.NumberOfNamedEntries = static_cast(std::count_if(children.begin(), children.end(), std::mem_fun_ref(&ResourceChild::isNamedResource))); + header.NumberOfIdEntries = static_cast(children.size()) - header.NumberOfNamedEntries; + } + + /** + * Rebuilds the current resource node. + * @param obBuffer OutputBuffer where the rebuilt resource node is stored. + * @param uiOffset Offset of the resource node inside the resource directory. + * @param uiRva RVA of the resource directory. + * @param pad Used for debugging. + **/ + void ResourceNode::rebuild(OutputBuffer& obBuffer, unsigned int& uiOffset, unsigned int uiRva, const std::string& pad) const + { +/* std::cout << std::hex << pad << uiOffset << std::endl; + + std::cout << std::hex << pad << "header.Characteristics: " << header.Characteristics << std::endl; + std::cout << std::hex << pad << "header.TimeDateStamp: " << header.TimeDateStamp << std::endl; + std::cout << std::hex << pad << "header.MajorVersion: " << header.MajorVersion << std::endl; + std::cout << std::hex << pad << "header.MinorVersion: " << header.MinorVersion << std::endl; + std::cout << std::hex << pad << "header.NumberOfNamedEntries: " << header.NumberOfNamedEntries << std::endl; + std::cout << std::hex << pad << "header.NumberOfIdEntries: " << header.NumberOfIdEntries << std::endl; +*/ + obBuffer << header.Characteristics; + obBuffer << header.TimeDateStamp; + obBuffer << header.MajorVersion; + obBuffer << header.MinorVersion; + //std::cout << pad << "Children: " << children.size() << std::endl; + //std::cout << pad << std::count_if(children.begin(), children.end(), std::mem_fun_ref(&ResourceChild::isNamedResource)) << std::endl; + //std::cout << pad << children.size() - std::count_if(children.begin(), children.end(), std::mem_fun_ref(&ResourceChild::isNamedResource)) << std::endl; +// obBuffer << std::count_if(children.begin(), children.end(), std::mem_fun_ref(&ResourceChild::isNamedResource)); +// obBuffer << children.size() - std::count_if(children.begin(), children.end(), std::mem_fun_ref(&ResourceChild::isNamedResource)); + obBuffer << header.NumberOfNamedEntries; + obBuffer << header.NumberOfIdEntries; + + uiOffset += PELIB_IMAGE_RESOURCE_DIRECTORY::size(); + + for (unsigned int i=0;irebuild(obBuffer, newoffs, uiRva, pad + " "); + } + uiOffset = newoffs; + } + + /** + * Reads the next resource node from the InputBuffer. + * @param inpBuffer An InputBuffer that holds the complete resource directory. + * @param uiOffset Offset of the resource node that's to be read. + * @param uiRva RVA of the beginning of the resource directory. + * @param pad Something I need for debugging. Will be removed soon. + **/ + int ResourceNode::read(InputBuffer& inpBuffer, unsigned int uiOffset, unsigned int uiRva/*, const std::string& pad*/) + { + // Not enough space to be a valid node. + if (uiOffset + PELIB_IMAGE_RESOURCE_DIRECTORY::size() > inpBuffer.size()) + { + return 1; + } + + uiElementRva = uiOffset + uiRva; + + inpBuffer.set(uiOffset); + + inpBuffer >> header.Characteristics; + inpBuffer >> header.TimeDateStamp; + inpBuffer >> header.MajorVersion; + inpBuffer >> header.MinorVersion; + inpBuffer >> header.NumberOfNamedEntries; + inpBuffer >> header.NumberOfIdEntries; + + // Not enough space to be a valid node. + if (uiOffset + PELIB_IMAGE_RESOURCE_DIRECTORY::size() + + (header.NumberOfNamedEntries + header.NumberOfIdEntries) * PELIB_IMAGE_RESOURCE_DIRECTORY_ENTRY::size() + > inpBuffer.size()) + { + return 1; + } + +// std::cout << std::hex << pad << "Characteristics: " << header.Characteristics << std::endl; +// std::cout << std::hex << pad << "TimeDateStamp: " << header.TimeDateStamp << std::endl; +// std::cout << std::hex << pad << "MajorVersion: " << header.MajorVersion << std::endl; +// std::cout << std::hex << pad << "MinorVersion: " << header.MinorVersion << std::endl; +// std::cout << std::hex << pad << "NumberOfNamedEntries: " << header.NumberOfNamedEntries << std::endl; +// std::cout << std::hex << pad << "NumberOfIdEntries: " << header.NumberOfIdEntries << std::endl; + + for (int i=0;i> rc.entry.irde.Name; + inpBuffer >> rc.entry.irde.OffsetToData; + + unsigned int lastPos = inpBuffer.get(); + + if (rc.entry.irde.Name & PELIB_IMAGE_RESOURCE_NAME_IS_STRING) + { + // Enough space to read string length? + if ((rc.entry.irde.Name & ~PELIB_IMAGE_RESOURCE_NAME_IS_STRING) + 2 < inpBuffer.size()) + { + inpBuffer.set(rc.entry.irde.Name & ~PELIB_IMAGE_RESOURCE_NAME_IS_STRING); + word strlen; + inpBuffer >> strlen; + + // Enough space to read string? + if ((rc.entry.irde.Name & ~PELIB_IMAGE_RESOURCE_NAME_IS_STRING) + 2 * strlen < inpBuffer.size()) + { + wchar_t c; + for (word i=0;i> c; + rc.entry.wstrName += c; + } + } + } + +// std::wcout << rc.entry.wstrName << std::endl; + +// std::cout << strlen << std::endl; + inpBuffer.set(lastPos); + } + + if (rc.entry.irde.OffsetToData & PELIB_IMAGE_RESOURCE_DATA_IS_DIRECTORY) + { + rc.child = new ResourceNode; + rc.child->read(inpBuffer, rc.entry.irde.OffsetToData & ~PELIB_IMAGE_RESOURCE_DATA_IS_DIRECTORY, uiRva/*, pad + " "*/); + } + else + { + rc.child = new ResourceLeaf; + rc.child->read(inpBuffer, rc.entry.irde.OffsetToData, uiRva/*, pad + " "*/); + } +// std::cout << std::hex << pad << "Entry " << i << "(Name): " << rc.entry.irde.Name << std::endl; +// std::cout << std::hex << pad << "Entry " << i << "(Offset): " << rc.entry.irde.OffsetToData << std::endl; + + children.push_back(rc); + inpBuffer.set(lastPos); + } + + return 0; + } + + /** + * Returns the number of children of the current node. Note that this number is the number + * of defined children, not the value from the header. + * @return Number of node's children. + **/ + unsigned int ResourceNode::getNumberOfChildren() const + { + return static_cast(children.size()); + } + + /** + * Adds another child to the current node. + **/ + void ResourceNode::addChild() + { + ResourceChild c; + c.child = 0; + children.push_back(c); + } + + /** + * Returns a node's child. + * @param uiIndex Index of the child. + * @return The child identified by uiIndex. This child can be either a ResourceNode or a ResourceLeaf. + **/ + ResourceElement* ResourceNode::getChild(unsigned int uiIndex) + { + return children[uiIndex].child; + } + + /** + * Removes a child from the current node. + * @param uiIndex Index of the child. + **/ + void ResourceNode::removeChild(unsigned int uiIndex) + { + children.erase(children.begin() + uiIndex); + } + + /** + * Returns the name of a child. + * @param uiIndex Index of the child. + * @return Either the name of the specified child or an empty string. + **/ + std::string ResourceNode::getChildName(unsigned int uiIndex) const + { + return children[uiIndex].entry.wstrName; + } + + /** + * Returns the Name value of a child. + * @param uiIndex Index of the child. + * @return Name value of a child. + **/ + dword ResourceNode::getOffsetToChildName(unsigned int uiIndex) const + { + return children[uiIndex].entry.irde.Name; + } + + /** + * Returns the OffsetToData value of a child. + * @param uiIndex Index of the child. + * @return OffsetToData value of a child. + **/ + dword ResourceNode::getOffsetToChildData(unsigned int uiIndex) const + { + return children[uiIndex].entry.irde.OffsetToData; + } + + + /** + * Sets the name of a child. + * @param uiIndex Index of the child. + * @param strNewName New name of the resource. + **/ + void ResourceNode::setChildName(unsigned int uiIndex, const std::string& strNewName) + { + children[uiIndex].entry.wstrName = strNewName; + } + + /** + * Sets the Name value of a child. + * @param uiIndex Index of the child. + * @param dwNewOffset New Name value of the resource. + **/ + void ResourceNode::setOffsetToChildName(unsigned int uiIndex, dword dwNewOffset) + { + children[uiIndex].entry.irde.Name = dwNewOffset; + } + + /** + * Sets the OffsetToData value of a child. + * @param uiIndex Index of the child. + * @param dwNewOffset New OffsetToData value of the resource. + **/ + void ResourceNode::setOffsetToChildData(unsigned int uiIndex, dword dwNewOffset) + { + children[uiIndex].entry.irde.OffsetToData = dwNewOffset; + } + + /** + * Returns the Characteristics value of the node. + * @return Characteristics value of the node. + **/ + dword ResourceNode::getCharacteristics() const + { + return header.Characteristics; + } + + /** + * Returns the TimeDateStamp value of the node. + * @return TimeDateStamp value of the node. + **/ + dword ResourceNode::getTimeDateStamp() const + { + return header.TimeDateStamp; + } + + /** + * Returns the MajorVersion value of the node. + * @return MajorVersion value of the node. + **/ + word ResourceNode::getMajorVersion() const + { + return header.MajorVersion; + } + + /** + * Returns the MinorVersion value of the node. + * @return MinorVersion value of the node. + **/ + word ResourceNode::getMinorVersion() const + { + return header.MinorVersion; + } + + /** + * Returns the NumberOfNamedEntries value of the node. + * @return NumberOfNamedEntries value of the node. + **/ + word ResourceNode::getNumberOfNamedEntries() const + { + return header.NumberOfNamedEntries; + } + + /** + * Returns the NumberOfIdEntries value of the node. + * @return NumberOfIdEntries value of the node. + **/ + word ResourceNode::getNumberOfIdEntries() const + { + return header.NumberOfIdEntries; + } + + /** + * Sets the Characteristics value of the node. + * @param value New Characteristics value of the node. + **/ + void ResourceNode::setCharacteristics(dword value) + { + header.Characteristics = value; + } + + /** + * Sets the TimeDateStamp value of the node. + * @param value New TimeDateStamp value of the node. + **/ + void ResourceNode::setTimeDateStamp(dword value) + { + header.TimeDateStamp = value; + } + + /** + * Sets the MajorVersion value of the node. + * @param value New MajorVersion value of the node. + **/ + void ResourceNode::setMajorVersion(word value) + { + header.MajorVersion = value; + } + + /** + * Sets the MinorVersion value of the node. + * @param value New MinorVersion value of the node. + **/ + void ResourceNode::setMinorVersion(word value) + { + header.MinorVersion = value; + } + + /** + * Sets the NumberOfNamedEntries value of the node. + * @param value New NumberOfNamedEntries value of the node. + **/ + void ResourceNode::setNumberOfNamedEntries(word value) + { + header.NumberOfNamedEntries = value; + } + + /** + * Sets the NumberOfIdEntries value of the node. + * @param value New NumberOfIdEntries value of the node. + **/ + void ResourceNode::setNumberOfIdEntries(word value) + { + header.NumberOfIdEntries = value; + } + + +/* /// Returns the size of a resource node. + unsigned int ResourceNode::size() const + { + if (children.size()) + { + std::cout << std::accumulate(children.begin(), children.end(), 0, accumulate) << std::endl; + return PELIB_IMAGE_RESOURCE_DIRECTORY::size() + + std::accumulate(children.begin(), children.end(), 0, accumulate); + } + else + { + return 0; + } + } +*/ +// -------------------------------------------------- ResourceDirectory ------------------------------------------- + + /** + * Returns the root node of the resource directory. + * @return Root node of the resource directory. + **/ + ResourceNode* ResourceDirectory::getRoot() + { + return &m_rnRoot; + } + + /** + * Correctly sorts the resource nodes of the resource tree. This function should be called + * before calling rebuild. + **/ + void ResourceDirectory::makeValid() + { + m_rnRoot.makeValid(); + } + + /** + * Reads the resource directory from a file. + * @param strFilename Name of the file. + * @param uiOffset File offset of the resource directory. + * @param uiSize Raw size of the resource directory. + * @param uiResDirRva RVA of the beginning of the resource directory. + **/ + int ResourceDirectory::read(const std::string& strFilename, unsigned int uiOffset, unsigned int uiSize, unsigned int uiResDirRva) + { + if (!uiSize || !uiOffset) + { + return 1; + } + + std::ifstream ifFile(strFilename.c_str(), std::ios::binary); + + if (!ifFile) + { +// throw Exceptions::CannotOpenFile(ResourceDirectoryId, __LINE__); + return 1; + } + + if (fileSize(ifFile) < uiOffset + uiSize) + { +// throw Exceptions::InvalidFormat(ResourceDirectoryId, __LINE__); + return 1; + } + + ifFile.seekg(uiOffset, std::ios::beg); + + PELIB_IMAGE_RESOURCE_DIRECTORY irdCurrRoot; + + std::vector vResourceDirectory(uiSize); + ifFile.read(reinterpret_cast(&vResourceDirectory[0]), uiSize); + + InputBuffer inpBuffer(vResourceDirectory); + +// ResourceNode currNode; + return m_rnRoot.read(inpBuffer, 0, uiResDirRva/*, ""*/); +// std::swap(currNode, m_rnRoot); + } + + /** + * Rebuilds the resource directory. + * @param vBuffer Buffer the source directory will be written to. + * @param uiRva RVA of the resource directory. + **/ + void ResourceDirectory::rebuild(std::vector& vBuffer, unsigned int uiRva) const + { + OutputBuffer obBuffer(vBuffer); + unsigned int offs = 0; +// std::cout << "Root: " << m_rnRoot.children.size() << std::endl; + m_rnRoot.rebuild(obBuffer, offs, uiRva, ""); + } + + /** + * Returns the size of the entire rebuilt resource directory. That's the size of the entire + * structure as it's written back to a file. + **/ +/* unsigned int ResourceDirectory::size() const + { + return m_rnRoot.size(); + } +*/ + /** + * Writes the current resource directory back into a file. + * @param strFilename Name of the output file. + * @param uiOffset File offset where the resource directory will be written to. + * @param uiRva RVA of the file offset. + **/ + int ResourceDirectory::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 vBuffer; + rebuild(vBuffer, uiRva); + + ofFile.write(reinterpret_cast(&vBuffer[0]), static_cast(vBuffer.size())); + + ofFile.close(); + + return 0; + } + + /** + * Adds another resource type. The new resource type is identified by the ID dwResTypeId. + * @param dwResTypeId ID which identifies the resource type. + **/ + int ResourceDirectory::addResourceType(dword dwResTypeId) + { + std::vector::iterator Iter = std::find_if(m_rnRoot.children.begin(), m_rnRoot.children.end(), std::bind2nd(std::mem_fun_ref(&ResourceChild::equalId), dwResTypeId)); + if (Iter != m_rnRoot.children.end()) + { + return 1; + // throw Exceptions::EntryAlreadyExists(ResourceDirectoryId, __LINE__); + } + + ResourceChild rcCurr; + rcCurr.child = new ResourceNode; + rcCurr.entry.irde.Name = dwResTypeId; + m_rnRoot.children.push_back(rcCurr); + + return 0; + } + + /** + * Adds another resource type. The new resource type is identified by the name strResTypeName. + * @param strResTypeName Name which identifies the resource type. + **/ + int ResourceDirectory::addResourceType(const std::string& strResTypeName) + { + std::vector::iterator Iter = std::find_if(m_rnRoot.children.begin(), m_rnRoot.children.end(), std::bind2nd(std::mem_fun_ref(&ResourceChild::equalName), strResTypeName)); + if (Iter != m_rnRoot.children.end()) + { + return 1; +// throw Exceptions::EntryAlreadyExists(ResourceDirectoryId, __LINE__); + } + + ResourceChild rcCurr; + rcCurr.entry.wstrName = strResTypeName; + rcCurr.child = new ResourceNode; + m_rnRoot.children.push_back(rcCurr); + + return 0; + } + + /** + * Removes the resource type identified by the ID dwResTypeId. + * @param dwResTypeId ID which identifies the resource type. + **/ + int ResourceDirectory::removeResourceType(dword dwResTypeId) + { + std::vector::iterator Iter = std::find_if(m_rnRoot.children.begin(), m_rnRoot.children.end(), std::bind2nd(std::mem_fun_ref(&ResourceChild::equalId), dwResTypeId)); + if (Iter == m_rnRoot.children.end()) + { + return 1; +// throw Exceptions::ResourceTypeDoesNotExist(ResourceDirectoryId, __LINE__); + } + + bool isNamed = false; + if (Iter->isNamedResource()) isNamed = true; + + m_rnRoot.children.erase(Iter); + + if (isNamed) m_rnRoot.header.NumberOfNamedEntries = static_cast(m_rnRoot.children.size()); + else m_rnRoot.header.NumberOfIdEntries = static_cast(m_rnRoot.children.size()); + + return 0; + } + + /** + * Removes the resource type identified by the name strResTypeName. + * @param strResTypeName Name which identifies the resource type. + **/ + int ResourceDirectory::removeResourceType(const std::string& strResTypeName) + { + std::vector::iterator Iter = std::find_if(m_rnRoot.children.begin(), m_rnRoot.children.end(), std::bind2nd(std::mem_fun_ref(&ResourceChild::equalName), strResTypeName)); + if (Iter == m_rnRoot.children.end()) + { + return 1; + // throw Exceptions::ResourceTypeDoesNotExist(ResourceDirectoryId, __LINE__); + } + + bool isNamed = false; + if (Iter->isNamedResource()) isNamed = true; + + m_rnRoot.children.erase(Iter); + + if (isNamed) m_rnRoot.header.NumberOfNamedEntries = static_cast(m_rnRoot.children.size()); + else m_rnRoot.header.NumberOfIdEntries = static_cast(m_rnRoot.children.size()); + + return 0; + } + + /** + * Removes the resource type identified by the index uiIndex. + * @param uiIndex Index which identifies the resource type. + **/ + int ResourceDirectory::removeResourceTypeByIndex(unsigned int uiIndex) + { + bool isNamed = false; + if (m_rnRoot.children[uiIndex].isNamedResource()) isNamed = true; + + m_rnRoot.children.erase(m_rnRoot.children.begin() + uiIndex); + + if (isNamed) m_rnRoot.header.NumberOfNamedEntries = static_cast(m_rnRoot.children.size()); + else m_rnRoot.header.NumberOfIdEntries = static_cast(m_rnRoot.children.size()); + + return 0; + } + + /** + * Adds another resource to the resource tree. The first parameter identifies the resource type + * of the new resource, the second parameter identifies the resource itself. + * @param dwResTypeId ID of the resource type. + * @param dwResId ID of the resource. + **/ + int ResourceDirectory::addResource(dword dwResTypeId, dword dwResId) + { + ResourceChild rcCurr; + rcCurr.entry.irde.Name = dwResId; + return addResourceT(dwResTypeId, dwResId, rcCurr); + } + + /** + * Adds another resource to the resource tree. The first parameter identifies the resource type + * of the new resource, the second parameter identifies the resource itself. + * @param dwResTypeId ID of the resource type. + * @param strResName Name of the resource. + **/ + int ResourceDirectory::addResource(dword dwResTypeId, const std::string& strResName) + { + ResourceChild rcCurr; + rcCurr.entry.wstrName = strResName; + return addResourceT(dwResTypeId, strResName, rcCurr); + } + + /** + * Adds another resource to the resource tree. The first parameter identifies the resource type + * of the new resource, the second parameter identifies the resource itself. + * @param strResTypeName Name of the resource type. + * @param dwResId ID of the resource. + **/ + int ResourceDirectory::addResource(const std::string& strResTypeName, dword dwResId) + { + ResourceChild rcCurr; + rcCurr.entry.irde.Name = dwResId; + return addResourceT(strResTypeName, dwResId, rcCurr); + } + + /** + * Adds another resource to the resource tree. The first parameter identifies the resource type + * of the new resource, the second parameter identifies the resource itself. + * @param strResTypeName Name of the resource type. + * @param strResName Name of the resource. + **/ + int ResourceDirectory::addResource(const std::string& strResTypeName, const std::string& strResName) + { + ResourceChild rcCurr; + rcCurr.entry.wstrName = strResName; + return addResourceT(strResTypeName, strResName, rcCurr); + } + + /** + * Removes a resource from the resource tree. The first parameter identifies the resource type + * of the new resource, the second parameter identifies the resource itself. + * @param dwResTypeIndex ID of the resource type. + * @param dwResId ID of the resource. + **/ + int ResourceDirectory::removeResource(dword dwResTypeIndex, dword dwResId) + { + return removeResourceT(dwResTypeIndex, dwResId); + } + + /** + * Removes a resource from the resource tree. The first parameter identifies the resource type + * of the new resource, the second parameter identifies the resource itself. + * @param dwResTypeIndex ID of the resource type. + * @param strResName Name of the resource. + **/ + int ResourceDirectory::removeResource(dword dwResTypeIndex, const std::string& strResName) + { + return removeResourceT(dwResTypeIndex, strResName); + } + + /** + * Removes a resource from the resource tree. The first parameter identifies the resource type + * of the new resource, the second parameter identifies the resource itself. + * @param strResTypeName Name of the resource type. + * @param dwResId ID of the resource. + **/ + int ResourceDirectory::removeResource(const std::string& strResTypeName, dword dwResId) + { + return removeResourceT(strResTypeName, dwResId); + } + + /** + * Removes a resource from the resource tree. The first parameter identifies the resource type + * of the new resource, the second parameter identifies the resource itself. + * @param strResTypeName Name of the resource type. + * @param strResName Name of the resource. + **/ + int ResourceDirectory::removeResource(const std::string& strResTypeName, const std::string& strResName) + { + return removeResourceT(strResTypeName, strResName); + } + + /** + * Returns the number of resource types. + **/ + unsigned int ResourceDirectory::getNumberOfResourceTypes() const + { + return static_cast(m_rnRoot.children.size()); + } + + /** + * Returns the ID of a resource type which was specified through an index. + * The valid range of the parameter uiIndex is 0...getNumberOfResourceTypes() - 1. + * Leaving the invalid range leads to undefined behaviour. + * @param uiIndex Index which identifies a resource type. + * @return The ID of the specified resource type. + **/ + dword ResourceDirectory::getResourceTypeIdByIndex(unsigned int uiIndex) const + { + return m_rnRoot.children[uiIndex].entry.irde.Name; + } + + /** + * Returns the name of a resource type which was specified through an index. + * The valid range of the parameter uiIndex is 0...getNumberOfResourceTypes() - 1. + * Leaving the invalid range leads to undefined behaviour. + * @param uiIndex Index which identifies a resource type. + * @return The name of the specified resource type. + **/ + std::string ResourceDirectory::getResourceTypeNameByIndex(unsigned int uiIndex) const + { + return m_rnRoot.children[uiIndex].entry.wstrName; + } + + /** + * Converts the ID of a resource type to an index. + * @param dwResTypeId ID of the resource type. + * @return Index of that resource type. + **/ + int ResourceDirectory::resourceTypeIdToIndex(dword dwResTypeId) const + { + std::vector::const_iterator Iter = std::find_if(m_rnRoot.children.begin(), m_rnRoot.children.end(), std::bind2nd(std::mem_fun_ref(&ResourceChild::equalId), dwResTypeId)); + if (Iter == m_rnRoot.children.end()) return -1; + return static_cast(std::distance(m_rnRoot.children.begin(), Iter)); + } + + /** + * Converts the name of a resource type to an index. + * @param strResTypeName ID of the resource type. + * @return Index of that resource type. + **/ + int ResourceDirectory::resourceTypeNameToIndex(const std::string& strResTypeName) const + { + std::vector::const_iterator Iter = std::find_if(m_rnRoot.children.begin(), m_rnRoot.children.end(), std::bind2nd(std::mem_fun_ref(&ResourceChild::equalName), strResTypeName)); + if (Iter == m_rnRoot.children.end()) return -1; + return static_cast(std::distance(m_rnRoot.children.begin(), Iter)); + } + + /** + * Returns the number of resources of a specific resource type. + * @param dwId ID of the resource type. + * @return Number of resources of resource type dwId. + **/ + unsigned int ResourceDirectory::getNumberOfResources(dword dwId) const + { +// std::vector::const_iterator IterD = m_rnRoot.children.begin(); +// std::cout << dwId << std::endl; +// while (IterD != m_rnRoot.children.end()) +// { +// std::cout << IterD->entry.irde.Name << std::endl; +// ++IterD; +// } + + std::vector::const_iterator Iter = std::find_if(m_rnRoot.children.begin(), m_rnRoot.children.end(), std::bind2nd(std::mem_fun_ref(&ResourceChild::equalId), dwId)); + if (Iter == m_rnRoot.children.end()) + { + return 0xFFFFFFFF; + } + else + { + ResourceNode* currNode = static_cast(Iter->child); + return static_cast(currNode->children.size()); + } + } + + /** + * Returns the number of resources of a specific resource type. + * @param strResTypeName Name of the resource type. + * @return Number of resources of resource type strResTypeName. + **/ + unsigned int ResourceDirectory::getNumberOfResources(const std::string& strResTypeName) const + { + std::vector::const_iterator Iter = std::find_if(m_rnRoot.children.begin(), m_rnRoot.children.end(), std::bind2nd(std::mem_fun_ref(&ResourceChild::equalName), strResTypeName)); + if (Iter == m_rnRoot.children.end()) + { + return 0xFFFFFFFF; + } + else + { + ResourceNode* currNode = static_cast(Iter->child); + return static_cast(currNode->children.size()); + } + } + + /** + * Returns the number of resources of a resource type which was specified through an index. + * The valid range of the parameter uiIndex is 0...getNumberOfResourceTypes() - 1. + * Leaving the invalid range leads to undefined behaviour. + * @param uiIndex Index which identifies a resource type. + * @return The number of resources of the specified resource type. + **/ + unsigned int ResourceDirectory::getNumberOfResourcesByIndex(unsigned int uiIndex) const + { + ResourceNode* currNode = static_cast(m_rnRoot.children[uiIndex].child); + return static_cast(currNode->children.size()); + } + + /** + * Gets the resource data of a specific resource. + * @param dwResTypeId Identifies the resource type of the resource. + * @param dwResId Identifies the resource. + * @param data Vector where the data is stored. + **/ + void ResourceDirectory::getResourceData(dword dwResTypeId, dword dwResId, std::vector& data) const + { + getResourceDataT(dwResTypeId, dwResId, data); + } + + /** + * Gets the resource data of a specific resource. + * @param dwResTypeId Identifies the resource type of the resource. + * @param strResName Identifies the resource. + * @param data Vector where the data is stored. + **/ + void ResourceDirectory::getResourceData(dword dwResTypeId, const std::string& strResName, std::vector& data) const + { + getResourceDataT(dwResTypeId, strResName, data); + } + + /** + * Gets the resource data of a specific resource. + * @param strResTypeName Identifies the resource type of the resource. + * @param dwResId Identifies the resource. + * @param data Vector where the data is stored. + **/ + void ResourceDirectory::getResourceData(const std::string& strResTypeName, dword dwResId, std::vector& data) const + { + getResourceDataT(strResTypeName, dwResId, data); + } + + /** + * Gets the resource data of a specific resource. + * @param strResTypeName Identifies the resource type of the resource. + * @param strResName Identifies the resource. + * @param data Vector where the data is stored. + **/ + void ResourceDirectory::getResourceData(const std::string& strResTypeName, const std::string& strResName, std::vector& data) const + { + getResourceDataT(strResTypeName, strResName, data); + } + + /** + * Gets the resource data of a specific resource by index. + * The valid range of the parameter uiResTypeIndex is 0...getNumberOfResourceTypes() - 1. + * The valid range of the parameter uiResIndex is 0...getNumberOfResources() - 1. + * Leaving the invalid range leads to undefined behaviour. + * @param uiResTypeIndex Identifies the resource type of the resource. + * @param uiResIndex Identifies the resource. + * @param data Vector where the data is stored. + **/ + void ResourceDirectory::getResourceDataByIndex(unsigned int uiResTypeIndex, unsigned int uiResIndex, std::vector& data) const + { + ResourceNode* currNode = static_cast(m_rnRoot.children[uiResTypeIndex].child); + currNode = static_cast(currNode->children[uiResIndex].child); + ResourceLeaf* currLeaf = static_cast(currNode->children[0].child); + + data.assign(currLeaf->m_data.begin(), currLeaf->m_data.end()); + } + + /** + * Sets the resource data of a specific resource. + * @param dwResTypeId Identifies the resource type of the resource. + * @param dwResId Identifies the resource. + * @param data The new resource data. + **/ + void ResourceDirectory::setResourceData(dword dwResTypeId, dword dwResId, std::vector& data) + { + setResourceDataT(dwResTypeId, dwResId, data); + } + + /** + * Sets the resource data of a specific resource. + * @param dwResTypeId Identifies the resource type of the resource. + * @param strResName Identifies the resource. + * @param data The new resource data. + **/ + void ResourceDirectory::setResourceData(dword dwResTypeId, const std::string& strResName, std::vector& data) + { + setResourceDataT(dwResTypeId, strResName, data); + } + + /** + * Sets the resource data of a specific resource. + * @param strResTypeName Identifies the resource type of the resource. + * @param dwResId Identifies the resource. + * @param data The new resource data. + **/ + void ResourceDirectory::setResourceData(const std::string& strResTypeName, dword dwResId, std::vector& data) + { + setResourceDataT(strResTypeName, dwResId, data); + } + + /** + * Sets the resource data of a specific resource. + * @param strResTypeName Identifies the resource type of the resource. + * @param strResName Identifies the resource. + * @param data The new resource data. + **/ + void ResourceDirectory::setResourceData(const std::string& strResTypeName, const std::string& strResName, std::vector& data) + { + setResourceDataT(strResTypeName, strResName, data); + } + + /** + * Sets the resource data of a specific resource by index. + * The valid range of the parameter uiResTypeIndex is 0...getNumberOfResourceTypes() - 1. + * The valid range of the parameter uiResIndex is 0...getNumberOfResources() - 1. + * Leaving the invalid range leads to undefined behaviour. + * @param uiResTypeIndex Identifies the resource type of the resource. + * @param uiResIndex Identifies the resource. + * @param data The new resource data. + **/ + void ResourceDirectory::setResourceDataByIndex(unsigned int uiResTypeIndex, unsigned int uiResIndex, std::vector& data) + { + ResourceNode* currNode = static_cast(m_rnRoot.children[uiResTypeIndex].child); + currNode = static_cast(currNode->children[uiResIndex].child); + ResourceLeaf* currLeaf = static_cast(currNode->children[0].child); + currLeaf->m_data.assign(data.begin(), data.end()); + } + + /** + * Gets the ID of a specific resource. + * @param dwResTypeId Identifies the resource type of the resource. + * @param strResName Identifies the resource. + * @return ID of the specified resource. + **/ + dword ResourceDirectory::getResourceId(dword dwResTypeId, const std::string& strResName) const + { + return getResourceIdT(dwResTypeId, strResName); + } + + /** + * Gets the ID of a specific resource. + * @param strResTypeName Identifies the resource type of the resource. + * @param strResName Identifies the resource. + * @return ID of the specified resource. + **/ + dword ResourceDirectory::getResourceId(const std::string& strResTypeName, const std::string& strResName) const + { + return getResourceIdT(strResTypeName, strResName); + } + + /** + * Gets the ID of a specific resource by index. + * @param uiResTypeIndex Identifies the resource type of the resource. + * @param uiResIndex Identifies the resource. + * @return ID of the specified resource. + **/ + dword ResourceDirectory::getResourceIdByIndex(unsigned int uiResTypeIndex, unsigned int uiResIndex) const + { + ResourceNode* currNode = static_cast(m_rnRoot.children[uiResTypeIndex].child); + return currNode->children[uiResIndex].entry.irde.Name; + } + + /** + * Sets the ID of a specific resource. + * @param dwResTypeId Identifies the resource type of the resource. + * @param dwResId Identifies the resource. + * @param dwNewResId New ID of the resource. + **/ + void ResourceDirectory::setResourceId(dword dwResTypeId, dword dwResId, dword dwNewResId) + { + setResourceIdT(dwResTypeId, dwResId, dwNewResId); + } + + /** + * Sets the ID of a specific resource. + * @param dwResTypeId Identifies the resource type of the resource. + * @param strResName Identifies the resource. + * @param dwNewResId New ID of the resource. + **/ + void ResourceDirectory::setResourceId(dword dwResTypeId, const std::string& strResName, dword dwNewResId) + { + setResourceIdT(dwResTypeId, strResName, dwNewResId); + } + + /** + * Sets the ID of a specific resource. + * @param strResTypeName Identifies the resource type of the resource. + * @param dwResId Identifies the resource. + * @param dwNewResId New ID of the resource. + **/ + void ResourceDirectory::setResourceId(const std::string& strResTypeName, dword dwResId, dword dwNewResId) + { + setResourceIdT(strResTypeName, dwResId, dwNewResId); + } + + /** + * Sets the ID of a specific resource. + * @param strResTypeName Identifies the resource type of the resource. + * @param strResName Identifies the resource. + * @param dwNewResId New ID of the resource. + **/ + void ResourceDirectory::setResourceId(const std::string& strResTypeName, const std::string& strResName, dword dwNewResId) + { + setResourceIdT(strResTypeName, strResName, dwNewResId); + } + + /** + * Sets the ID of a specific resource by index. + * @param uiResTypeIndex Identifies the resource type of the resource. + * @param uiResIndex Identifies the resource. + * @param dwNewResId New ID of the specified resource. + **/ + void ResourceDirectory::setResourceIdByIndex(unsigned int uiResTypeIndex, unsigned int uiResIndex, dword dwNewResId) + { + ResourceNode* currNode = static_cast(m_rnRoot.children[uiResTypeIndex].child); + currNode->children[uiResIndex].entry.irde.Name = dwNewResId; + } + + /** + * Gets the Name of a specific resource. + * @param dwResTypeId Identifies the resource type of the resource. + * @param dwResId Identifies the resource. + * @return Name of the specified resource. + **/ + std::string ResourceDirectory::getResourceName(dword dwResTypeId, dword dwResId) const + { + return getResourceNameT(dwResTypeId, dwResId); + } + + /** + * Gets the Name of a specific resource. + * @param strResTypeName Identifies the resource type of the resource. + * @param dwResId Identifies the resource. + * @return Name of the specified resource. + **/ + std::string ResourceDirectory::getResourceName(const std::string& strResTypeName, dword dwResId) const + { + return getResourceNameT(strResTypeName, dwResId); + } + + /** + * Gets the name of a specific resource by index. + * @param uiResTypeIndex Identifies the resource type of the resource. + * @param uiResIndex Identifies the resource. + * @return Name of the specified resource. + **/ + std::string ResourceDirectory::getResourceNameByIndex(unsigned int uiResTypeIndex, unsigned int uiResIndex) const + { + ResourceNode* currNode = static_cast(m_rnRoot.children[uiResTypeIndex].child); + return currNode->children[uiResIndex].entry.wstrName; + } + + /** + * Sets the name of a specific resource. + * @param dwResTypeId Identifies the resource type of the resource. + * @param dwResId Identifies the resource. + * @param strNewResName New name of the specified resource. + **/ + void ResourceDirectory::setResourceName(dword dwResTypeId, dword dwResId, const std::string& strNewResName) + { + setResourceNameT(dwResTypeId, dwResId, strNewResName); + } + + /** + * Sets the name of a specific resource. + * @param dwResTypeId Identifies the resource type of the resource. + * @param strResName Identifies the resource. + * @param strNewResName New name of the specified resource. + **/ + void ResourceDirectory::setResourceName(dword dwResTypeId, const std::string& strResName, const std::string& strNewResName) + { + setResourceNameT(dwResTypeId, strResName, strNewResName); + } + + /** + * Sets the name of a specific resource. + * @param strResTypeName Identifies the resource type of the resource. + * @param dwResId Identifies the resource. + * @param strNewResName New name of the specified resource. + **/ + void ResourceDirectory::setResourceName(const std::string& strResTypeName, dword dwResId, const std::string& strNewResName) + { + setResourceNameT(strResTypeName, dwResId, strNewResName); + } + + /** + * Sets the name of a specific resource. + * @param strResTypeName Identifies the resource type of the resource. + * @param strResName Identifies the resource. + * @param strNewResName New name of the specified resource. + **/ + void ResourceDirectory::setResourceName(const std::string& strResTypeName, const std::string& strResName, const std::string& strNewResName) + { + setResourceNameT(strResTypeName, strResName, strNewResName); + } + + /** + * Sets the name of a specific resource by index. + * @param uiResTypeIndex Identifies the resource type of the resource. + * @param uiResIndex Identifies the resource. + * @param strNewResName New name of the specified resource. + **/ + void ResourceDirectory::setResourceNameByIndex(unsigned int uiResTypeIndex, unsigned int uiResIndex, const std::string& strNewResName) + { + ResourceNode* currNode = static_cast(m_rnRoot.children[uiResTypeIndex].child); + currNode->children[uiResIndex].entry.wstrName = strNewResName; + } + +} diff --git a/utils/zenutils/libraries/pelib-0.9/pelib/ResourceDirectory.h b/utils/zenutils/libraries/pelib-0.9/pelib/ResourceDirectory.h old mode 100755 new mode 100644 index a0bba4c9b3..6a3dc3caef --- a/utils/zenutils/libraries/pelib-0.9/pelib/ResourceDirectory.h +++ b/utils/zenutils/libraries/pelib-0.9/pelib/ResourceDirectory.h @@ -1,735 +1,735 @@ -/* -* ResourceDirectory.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 RESOURCEDIRECTORY_H -#define RESOURCEDIRECTORY_H - -#include "PeLibInc.h" - -namespace PeLib -{ - class ResourceElement; - - /// The class ResourceChild is used to store information about a resource node. - class ResourceChild - { - friend class ResourceElement; - friend class ResourceDirectory; - friend class ResourceNode; - friend class ResourceLeaf; - - /// Stores name and offset of a resource node. - PELIB_IMG_RES_DIR_ENTRY entry; - /// A pointer to one of the node's child nodes. - ResourceElement* child; - - public: - /// Function which compares a resource ID to the node's resource ID. - bool equalId(dword wId) const; // EXPORT - /// Function which compares a string to the node's resource name. - bool equalName(std::string strName) const; // EXPORT - /// Predicate that determines if a child is identified by name or by ID. - bool isNamedResource() const; // EXPORT - /// Used for sorting a node's children. - bool operator<(const ResourceChild& rc) const; // EXPORT - /// Returns the size of a resource child. -// unsigned int size() const; - - /// Standard constructor. Does absolutely nothing. - ResourceChild(); - /// Makes a deep copy of a ResourceChild object. - ResourceChild(const ResourceChild& rhs); - /// Makes a deep copy of a ResourceChild object. - ResourceChild& operator=(const ResourceChild& rhs); - /// Deletes a ResourceChild object. - ~ResourceChild(); - }; - - /// Base class for ResourceNode and ResourceLeaf, the elements of the resource tree. - /// \todo write - class ResourceElement - { - friend class ResourceChild; - friend class ResourceNode; - friend class ResourceLeaf; - - protected: - /// Stores RVA of the resource element in the file. - unsigned int uiElementRva; - - /// Reads the next resource element from the InputBuffer. - virtual int read(InputBuffer&, unsigned int, unsigned int/*, const std::string&*/) = 0; - /// Writes the next resource element into the OutputBuffer. - virtual void rebuild(OutputBuffer&, unsigned int&, unsigned int, const std::string&) const = 0; - - public: - /// Returns the RVA of the element in the file. - unsigned int getElementRva() const; // EXPORT - /// Indicates if the resource element is a leaf or a node. - virtual bool isLeaf() const = 0; // EXPORT - /// Corrects erroneous valeus in the ResourceElement. - virtual void makeValid() = 0; // EXPORT - /// Returns the size of a resource element. -// virtual unsigned int size() const = 0; - /// Necessary virtual destructor. - virtual ~ResourceElement() {} - }; - - /// ResourceLeafs represent the leafs of the resource tree: The actual resources. - class ResourceLeaf : public ResourceElement - { - friend class ResourceChild; - friend class ResourceDirectory; - template friend struct fixNumberOfEntries; - - private: - /// The resource data. - std::vector m_data; - /// PeLib equivalent of the Win32 structure IMAGE_RESOURCE_DATA_ENTRY - PELIB_IMAGE_RESOURCE_DATA_ENTRY entry; - - protected: - int read(InputBuffer& inpBuffer, unsigned int uiOffset, unsigned int rva/*, const std::string&*/); - /// Writes the next resource leaf into the OutputBuffer. - void rebuild(OutputBuffer&, unsigned int& uiOffset, unsigned int uiRva, const std::string&) const; - - public: - /// Indicates if the resource element is a leaf or a node. - bool isLeaf() const; // EXPORT - /// Corrects erroneous valeus in the ResourceLeaf. - void makeValid(); // EXPORT - /// Reads the next resource leaf from the InputBuffer. - /// Returns the size of a resource lead. -// unsigned int size() const; - - /// Returns the resource data of this resource leaf. - std::vector getData() const; // EXPORT - /// Sets the resource data of this resource leaf. - void setData(const std::vector& vData); // EXPORT - - /// Returns the OffsetToData value of this resource leaf. - dword getOffsetToData() const; // EXPORT - /// Returns the Size value of this resource leaf. - dword getSize() const; // EXPORT - /// Returns the CodePage value of this resource leaf. - dword getCodePage() const; // EXPORT - /// Returns the Reserved value of this resource leaf. - dword getReserved() const; // EXPORT - - /// Sets the OffsetToData value of this resource leaf. - void setOffsetToData(dword dwValue); // EXPORT - /// Sets the Size value of this resource leaf. - void setSize(dword dwValue); // EXPORT - /// Sets the CodePage value of this resource leaf. - void setCodePage(dword dwValue); // EXPORT - /// Sets the Reserved value of this resource leaf. - void setReserved(dword dwValue); // EXPORT - }; - - /// ResourceNodes represent the nodes in the resource tree. - class ResourceNode : public ResourceElement - { - friend class ResourceChild; - friend class ResourceDirectory; - template friend struct fixNumberOfEntries; - - /// The node's children. - std::vector children; - /// The node's header. Equivalent to IMAGE_RESOURCE_DIRECTORY from the Win32 API. - PELIB_IMAGE_RESOURCE_DIRECTORY header; - - protected: - /// Reads the next resource node. - int read(InputBuffer& inpBuffer, unsigned int uiOffset, unsigned int rva/*, const std::string&*/); - /// Writes the next resource node into the OutputBuffer. - void rebuild(OutputBuffer&, unsigned int& uiOffset, unsigned int uiRva, const std::string&) const; - - public: - /// Indicates if the resource element is a leaf or a node. - bool isLeaf() const; // EXPORT - /// Corrects erroneous valeus in the ResourceNode. - void makeValid(); // EXPORT - - /// Returns the node's number of children. - unsigned int getNumberOfChildren() const; // EXPORT - /// Adds another child to node. - void addChild(); // EXPORT - /// Returns a node's child. - ResourceElement* getChild(unsigned int uiIndex); // EXPORT - /// Removes a node's child. - void removeChild(unsigned int uiIndex); // EXPORT - - /// Returns the name of one of the node's children. - std::string getChildName(unsigned int uiIndex) const; // EXPORT - /// Returns the Name value of one of the node's children. - dword getOffsetToChildName(unsigned int uiIndex) const; // EXPORT - /// Returns the OffsetToData value of one of the node's children. - dword getOffsetToChildData(unsigned int uiIndex) const; // EXPORT - - /// Sets the name of one of the node's children. - void setChildName(unsigned int uiIndex, const std::string& strNewName); // EXPORT - /// Sets the Name value of one of the node's children. - void setOffsetToChildName(unsigned int uiIndex, dword dwNewOffset); // EXPORT - /// Sets the OffsetToData value of one of the node's children. - void setOffsetToChildData(unsigned int uiIndex, dword dwNewOffset); // EXPORT - - /// Returns the node's Characteristics value. - dword getCharacteristics() const; // EXPORT - /// Returns the node's TimeDateStamp value. - dword getTimeDateStamp() const; // EXPORT - /// Returns the node's MajorVersion value. - word getMajorVersion() const; // EXPORT - /// Returns the node's MinorVersion value. - word getMinorVersion() const; // EXPORT - /// Returns the node's NumberOfNamedEntries value. - word getNumberOfNamedEntries() const; // EXPORT - /// Returns the node's NumberOfIdEntries value. - word getNumberOfIdEntries() const; // EXPORT - - /// Sets the node's Characteristics value. - void setCharacteristics(dword value); // EXPORT - /// Sets the node's TimeDateStamp value. - void setTimeDateStamp(dword value); // EXPORT - /// Sets the node's MajorVersion value. - void setMajorVersion(word value); // EXPORT - /// Sets the node's MinorVersion value. - void setMinorVersion(word value); // EXPORT - /// Sets the node's NumberOfNamedEntries value. - void setNumberOfNamedEntries(word value); // EXPORT - /// Sets the node's NumberOfIdEntries value. - void setNumberOfIdEntries(word value); // EXPORT - - /// Returns the size of a resource node. -// unsigned int size() const; - }; - - /// Auxiliary functor which is used to search through the resource tree. - /** - * Traits class for the template functions of ResourceDirectory. - * It's used to find out which function to use when searching for resource nodes or resource leafs - * in a node's children vector. - **/ - template - struct ResComparer - { - /// Pointer to a member function of ResourceChild - typedef bool(ResourceChild::*CompFunc)(T) const; - - /// Return 0 for all unspecialized versions of ResComparer. - static CompFunc comp(); - }; - - /// Auxiliary functor which is used to search through the resource tree. - /** - * ResComparer is used when a resource element is searched for by ID. - **/ - template<> - struct ResComparer - { - /// Pointer to a member function of ResourceChild - typedef bool(ResourceChild::*CompFunc)(dword) const; - - /// Return the address of the ResourceChild member function that compares the ids of resource elements. - static CompFunc comp() - { - return &ResourceChild::equalId; - } - }; - - /// Auxiliary functor which is used to search through the resource tree. - /** - * This specializd version of ResComparer is used when a resource element is searched for by name. - **/ - template<> - struct ResComparer - { - /// Pointer to a member function of ResourceChild - typedef bool(ResourceChild::*CompFunc)(std::string) const; - - /// Return the address of the ResourceChild member function that compares the names of resource elements. - static CompFunc comp() - { - return &ResourceChild::equalName; - } - }; - - /// Unspecialized function that's used as base template for the specialized versions below. - template - struct fixNumberOfEntries - { - /// Fixes a resource node's header. - static void fix(ResourceNode*); - }; - - /// Fixes NumberOfIdEntries value of a node. - template<> - struct fixNumberOfEntries - { - /// Fixes a resource node's NumberOfIdEntries value. - static void fix(ResourceNode* node) - { - node->header.NumberOfIdEntries = (unsigned int)node->children.size() - std::count_if(node->children.begin(), node->children.end(), std::mem_fun_ref(&ResourceChild::isNamedResource)); - } - }; - - /// Fixes NumberOfNamedEntries value of a node. - template<> - struct fixNumberOfEntries - { - /// Fixes a resource node's NumberOfNamedEntries value. - static void fix(ResourceNode* node) - { - node->header.NumberOfNamedEntries = static_cast(std::count_if(node->children.begin(), node->children.end(), std::mem_fun_ref(&ResourceChild::isNamedResource))); - } - }; - - /// Class that represents the resource directory of a PE file. - /** - * The class ResourceDirectory represents the resource directory of a PE file. This class is fundamentally - * different from the other classes of the PeLib library due to the structure of the ResourceDirectory. - * For once, it's possible to manipulate the ResourceDirectory through a set of "high level" functions and - * and through a set of "low level" functions. The "high level" functions are the functions inside the - * ResourceDirectory class with the exception of getRoot.

- * getRoot on the other hand is the first "low level" function. Use it to retrieve the root node of the - * resource tree. Then you can traverse through the tree and manipulate individual nodes and leafs - * directly using the functions provided by the classes ResourceNode and ResourceLeaf.

- * There's another difference between the ResourceDirectory class and the other PeLib classes, which is - * once again caused by the special structure of the PE resource directory. The nodes of the resource - * tree must be in a certain order. Manipulating the resource tree does not directly sort the nodes - * correctly as this would cause more trouble than it fixes. That means it's your responsibility to - * fix the resource tree after manipulating it. PeLib makes the job easy for you, just call the - * ResourceDirectory::makeValid function.

- * You might also wonder why there's no size() function in this class. I did not forget it. It's just - * that it's impossible to calculate the size of the resource directory without rebuilding it. So why - * should PeLib do this if you can do it just as easily by calling rebuild() and then checking the length - * of the returned vector.

- * There are also different ways to serialize (rebuild) the resource tree as it's not a fixed structure - * that can easily be minimized like most other PE directories.

- * This means it's entirely possible that the resource tree you read from a file differs from the one - * PeLib creates. This might cause a minor issue. The original resource tree might be smaller (due to - * different padding) so it's crucial that you check if there's enough space in the original resource - * directory before you write the rebuilt resource directory back to the file. - **/ - class ResourceDirectory - { - private: - /// The root node of the resource directory. - ResourceNode m_rnRoot; - - // Prepare for some crazy syntax below to make Digital Mars happy. - - /// Retrieves an iterator to a specified resource child. - template - std::vector::const_iterator locateResourceT(S restypeid, T resid) const; - - /// Retrieves an iterator to a specified resource child. - template - std::vector::iterator locateResourceT(S restypeid, T resid); - - /// Adds a new resource. - template - int addResourceT(S restypeid, T resid, ResourceChild& rc); - - /// Removes new resource. - template - int removeResourceT(S restypeid, T resid); - - /// Returns the data of a resource. - template - int getResourceDataT(S restypeid, T resid, std::vector& data) const; - - /// Sets the data of a resource. - template - int setResourceDataT(S restypeid, T resid, std::vector& data); - - /// Returns the ID of a resource. - template - dword getResourceIdT(S restypeid, T resid) const; - - /// Sets the ID of a resource. - template - int setResourceIdT(S restypeid, T resid, dword dwNewResId); - - /// Returns the name of a resource. - template - std::string getResourceNameT(S restypeid, T resid) const; - - /// Sets the name of a resource. - template - int setResourceNameT(S restypeid, T resid, std::string strNewResName); - - public: - ResourceNode* getRoot(); - /// Corrects a erroneous resource directory. - void makeValid(); - /// Reads the resource directory from a file. - int read(const std::string& strFilename, unsigned int uiOffset, unsigned int uiSize, unsigned int uiResDirRva); - /// Rebuilds the resource directory. - void rebuild(std::vector& vBuffer, unsigned int uiRva) const; - /// Returns the size of the rebuilt resource directory. -// unsigned int size() const; - /// Writes the resource directory to a file. - int write(const std::string& strFilename, unsigned int uiOffset, unsigned int uiRva) const; - - /// Adds a new resource type. - int addResourceType(dword dwResTypeId); - /// Adds a new resource type. - int addResourceType(const std::string& strResTypeName); - - /// Removes a resource type and all of it's resources. - int removeResourceType(dword dwResTypeId); - /// Removes a resource type and all of it's resources. - int removeResourceType(const std::string& strResTypeName); - - /// Removes a resource type and all of it's resources. - int removeResourceTypeByIndex(unsigned int uiIndex); - - /// Adds a new resource. - int addResource(dword dwResTypeId, dword dwResId); - /// Adds a new resource. - int addResource(dword dwResTypeId, const std::string& strResName); - /// Adds a new resource. - int addResource(const std::string& strResTypeName, dword dwResId); - /// Adds a new resource. - int addResource(const std::string& strResTypeName, const std::string& strResName); - - /// Removes a resource. - int removeResource(dword dwResTypeId, dword dwResId); - /// Removes a resource. - int removeResource(dword dwResTypeId, const std::string& strResName); - /// Removes a resource. - int removeResource(const std::string& strResTypeName, dword dwResId); - /// Removes a resource. - int removeResource(const std::string& strResTypeName, const std::string& strResName); - - /// Returns the number of resource types. - unsigned int getNumberOfResourceTypes() const; - - /// Returns the ID of a resource type. - dword getResourceTypeIdByIndex(unsigned int uiIndex) const; - /// Returns the name of a resource type. - std::string getResourceTypeNameByIndex(unsigned int uiIndex) const; - - /// Converts a resource type ID to an index. - int resourceTypeIdToIndex(dword dwResTypeId) const; - /// Converts a resource type name to an index. - int resourceTypeNameToIndex(const std::string& strResTypeName) const; - - /// Returns the number of resources of a certain resource type. - unsigned int getNumberOfResources(dword dwId) const; - /// Returns the number of resources of a certain resource type. - unsigned int getNumberOfResources(const std::string& strResTypeName) const; - - /// Returns the number of resources of a certain resource type. - unsigned int getNumberOfResourcesByIndex(unsigned int uiIndex) const; - - /// Returns the data of a certain resource. - void getResourceData(dword dwResTypeId, dword dwResId, std::vector& data) const; - /// Returns the data of a certain resource. - void getResourceData(dword dwResTypeId, const std::string& strResName, std::vector& data) const; - /// Returns the data of a certain resource. - void getResourceData(const std::string& strResTypeName, dword dwResId, std::vector& data) const; - /// Returns the data of a certain resource. - void getResourceData(const std::string& strResTypeName, const std::string& strResName, std::vector& data) const; - - /// Returns the data of a certain resource. - void getResourceDataByIndex(unsigned int uiResTypeIndex, unsigned int uiResIndex, std::vector& data) const; - - /// Sets the data of a certain resource. - void setResourceData(dword dwResTypeId, dword dwResId, std::vector& data); - /// Sets the data of a certain resource. - void setResourceData(dword dwResTypeId, const std::string& strResName, std::vector& data); - /// Sets the data of a certain resource. - void setResourceData(const std::string& strResTypeName, dword dwResId, std::vector& data); - /// Sets the data of a certain resource. - void setResourceData(const std::string& strResTypeName, const std::string& strResName, std::vector& data); - - /// Sets the data of a certain resource. - void setResourceDataByIndex(unsigned int uiResTypeIndex, unsigned int uiResIndex, std::vector& data); - - /// Returns the ID of a certain resource. - dword getResourceId(dword dwResTypeId, const std::string& strResName) const; - /// Returns the ID of a certain resource. - dword getResourceId(const std::string& strResTypeName, const std::string& strResName) const; - - /// Returns the ID of a certain resource. - dword getResourceIdByIndex(unsigned int uiResTypeIndex, unsigned int uiResIndex) const; - - /// Sets the ID of a certain resource. - void setResourceId(dword dwResTypeId, dword dwResId, dword dwNewResId); - /// Sets the ID of a certain resource. - void setResourceId(dword dwResTypeId, const std::string& strResName, dword dwNewResId); - /// Sets the ID of a certain resource. - void setResourceId(const std::string& strResTypeName, dword dwResId, dword dwNewResId); - /// Sets the ID of a certain resource. - void setResourceId(const std::string& strResTypeName, const std::string& strResName, dword dwNewResId); - - /// Sets the ID of a certain resource. - void setResourceIdByIndex(unsigned int uiResTypeIndex, unsigned int uiResIndex, dword dwNewResId); - - /// Returns the name of a certain resource. - std::string getResourceName(dword dwResTypeId, dword dwResId) const; - /// Returns the name of a certain resource. - std::string getResourceName(const std::string& strResTypeName, dword dwResId) const; - - /// Returns the name of a certain resource. - std::string getResourceNameByIndex(unsigned int uiResTypeIndex, unsigned int uiResIndex) const; - - /// Sets the name of a certain resource. - void setResourceName(dword dwResTypeId, dword dwResId, const std::string& strNewResName); - /// Sets the name of a certain resource. - void setResourceName(dword dwResTypeId, const std::string& strResName, const std::string& strNewResName); - /// Sets the name of a certain resource. - void setResourceName(const std::string& strResTypeName, dword dwResId, const std::string& strNewResName); - /// Sets the name of a certain resource. - void setResourceName(const std::string& strResTypeName, const std::string& strResName, const std::string& strNewResName); - - /// Sets the name of a certain resource. - void setResourceNameByIndex(unsigned int uiResTypeIndex, unsigned int uiResIndex, const std::string& strNewResName); - }; - - /** - * Looks through the entire resource tree and returns a const_iterator to the resource specified - * by the parameters. - * @param restypeid Identifier of the resource type (either ID or name). - * @param resid Identifier of the resource (either ID or name). - * @return A const_iterator to the specified resource. - **/ - template - std::vector::const_iterator ResourceDirectory::locateResourceT(S restypeid, T resid) const - { - typedef bool(ResourceChild::*CompFunc1)(S) const; - typedef bool(ResourceChild::*CompFunc2)(T) const; - - CompFunc1 comp1 = ResComparer::comp(); - CompFunc2 comp2 = ResComparer::comp(); - - std::vector::const_iterator Iter = std::find_if(m_rnRoot.children.begin(), m_rnRoot.children.end(), std::bind2nd(std::mem_fun_ref(comp1), restypeid)); - if (Iter == m_rnRoot.children.end()) - { - return Iter; - } - - ResourceNode* currNode = static_cast(Iter->child); - std::vector::const_iterator ResIter = std::find_if(currNode->children.begin(), currNode->children.end(), std::bind2nd(std::mem_fun_ref(comp2), resid)); - if (ResIter == currNode->children.end()) - { - return ResIter; - } - - return ResIter; - } - - /** - * Looks through the entire resource tree and returns an iterator to the resource specified - * by the parameters. - * @param restypeid Identifier of the resource type (either ID or name). - * @param resid Identifier of the resource (either ID or name). - * @return An iterator to the specified resource. - **/ - template - std::vector::iterator ResourceDirectory::locateResourceT(S restypeid, T resid) - { - typedef bool(ResourceChild::*CompFunc1)(S) const; - typedef bool(ResourceChild::*CompFunc2)(T) const; - - CompFunc1 comp1 = ResComparer::comp(); - CompFunc2 comp2 = ResComparer::comp(); - - std::vector::iterator Iter = std::find_if(m_rnRoot.children.begin(), m_rnRoot.children.end(), std::bind2nd(std::mem_fun_ref(comp1), restypeid)); - if (Iter == m_rnRoot.children.end()) - { - return Iter; - } - - ResourceNode* currNode = static_cast(Iter->child); - std::vector::iterator ResIter = std::find_if(currNode->children.begin(), currNode->children.end(), std::bind2nd(std::mem_fun_ref(comp2), resid)); - if (ResIter == currNode->children.end()) - { - return ResIter; - } - - return ResIter; - } - - /** - * Adds a new resource, resource type and ID are specified by the parameters. - * @param restypeid Identifier of the resource type (either ID or name). - * @param resid Identifier of the resource (either ID or name). - * @param rc ResourceChild that will be added. - **/ - template - int ResourceDirectory::addResourceT(S restypeid, T resid, ResourceChild& rc) - { - typedef bool(ResourceChild::*CompFunc1)(S) const; - typedef bool(ResourceChild::*CompFunc2)(T) const; - - CompFunc1 comp1 = ResComparer::comp(); - CompFunc2 comp2 = ResComparer::comp(); - - std::vector::iterator Iter = std::find_if(m_rnRoot.children.begin(), m_rnRoot.children.end(), std::bind2nd(std::mem_fun_ref(comp1), restypeid)); - if (Iter == m_rnRoot.children.end()) - { - return 1; - // throw Exceptions::ResourceTypeDoesNotExist(ResourceDirectoryId, __LINE__); - } - - ResourceNode* currNode = static_cast(Iter->child); - std::vector::iterator ResIter = std::find_if(currNode->children.begin(), currNode->children.end(), std::bind2nd(std::mem_fun_ref(comp2), resid)); - if (ResIter != currNode->children.end()) - { - return 1; -// throw Exceptions::EntryAlreadyExists(ResourceDirectoryId, __LINE__); - } - - rc.child = new ResourceNode; - ResourceChild rlnew; - rlnew.child = new ResourceLeaf; - ResourceNode* currNode2 = static_cast(rc.child); - currNode2->children.push_back(rlnew); - currNode->children.push_back(rc); - - fixNumberOfEntries::fix(currNode); - fixNumberOfEntries::fix(currNode2); - - return 0; - } - - /** - * Removes a resource, resource type and ID are specified by the parameters. - * @param restypeid Identifier of the resource type (either ID or name). - * @param resid Identifier of the resource (either ID or name). - **/ - template - int ResourceDirectory::removeResourceT(S restypeid, T resid) - { - typedef bool(ResourceChild::*CompFunc1)(S) const; - typedef bool(ResourceChild::*CompFunc2)(T) const; - - CompFunc1 comp1 = ResComparer::comp(); - CompFunc2 comp2 = ResComparer::comp(); - - std::vector::iterator Iter = std::find_if(m_rnRoot.children.begin(), m_rnRoot.children.end(), std::bind2nd(std::mem_fun_ref(comp1), restypeid)); - if (Iter == m_rnRoot.children.end()) - { - return 1; - //throw Exceptions::ResourceTypeDoesNotExist(ResourceDirectoryId, __LINE__); - } - - ResourceNode* currNode = static_cast(Iter->child); - std::vector::iterator ResIter = std::find_if(currNode->children.begin(), currNode->children.end(), std::bind2nd(std::mem_fun_ref(comp2), resid)); - if (ResIter == currNode->children.end()) - { - return 1; - // throw Exceptions::InvalidName(ResourceDirectoryId, __LINE__); - } - - currNode->children.erase(ResIter); - - fixNumberOfEntries::fix(currNode); - - return 0; - } - - /** - * Returns the data of a resource, resource type and ID are specified by the parameters. - * @param restypeid Identifier of the resource type (either ID or name). - * @param resid Identifier of the resource (either ID or name). - * @param data The data of the resource will be written into this vector. - **/ - template - int ResourceDirectory::getResourceDataT(S restypeid, T resid, std::vector& data) const - { - std::vector::const_iterator ResIter = locateResourceT(restypeid, resid); - ResourceNode* currNode = static_cast(ResIter->child); - ResourceLeaf* currLeaf = static_cast(currNode->children[0].child); - data.assign(currLeaf->m_data.begin(), currLeaf->m_data.end()); - - return 0; - } - - /** - * Sets the data of a resource, resource type and ID are specified by the parameters. - * @param restypeid Identifier of the resource type (either ID or name). - * @param resid Identifier of the resource (either ID or name). - * @param data The new data of the resource is taken from this vector. - **/ - template - int ResourceDirectory::setResourceDataT(S restypeid, T resid, std::vector& data) - { - std::vector::iterator ResIter = locateResourceT(restypeid, resid); - ResourceNode* currNode = static_cast(ResIter->child); - ResourceLeaf* currLeaf = static_cast(currNode->children[0].child); - currLeaf->m_data.assign(data.begin(), data.end()); - - return 0; - } - - /** - * Returns the id of a resource, resource type and ID are specified by the parameters. - * Note: Calling this function with resid == the ID of the resource makes no sense at all. - * @param restypeid Identifier of the resource type (either ID or name). - * @param resid Identifier of the resource (either ID or name). - * @return The ID of the specified resource. - **/ - template - dword ResourceDirectory::getResourceIdT(S restypeid, T resid) const - { - std::vector::const_iterator ResIter = locateResourceT(restypeid, resid); - return ResIter->entry.irde.Name; - } - - /** - * Sets the id of a resource, resource type and ID are specified by the parameters. - * @param restypeid Identifier of the resource type (either ID or name). - * @param resid Identifier of the resource (either ID or name). - * @param dwNewResId New ID of the resource. - **/ - template - int ResourceDirectory::setResourceIdT(S restypeid, T resid, dword dwNewResId) - { - std::vector::iterator ResIter = locateResourceT(restypeid, resid); - ResIter->entry.irde.Name = dwNewResId; - return 0; - } - - /** - * Returns the name of a resource, resource type and ID are specified by the parameters. - * Note: Calling this function with resid == the name of the resource makes no sense at all. - * @param restypeid Identifier of the resource type (either ID or name). - * @param resid Identifier of the resource (either ID or name). - * @return The name of the specified resource. - **/ - template - std::string ResourceDirectory::getResourceNameT(S restypeid, T resid) const - { - std::vector::const_iterator ResIter = locateResourceT(restypeid, resid); - return ResIter->entry.wstrName; - } - - /** - * Sets the name of a resource, resource type and ID are specified by the parameters. - * @param restypeid Identifier of the resource type (either ID or name). - * @param resid Identifier of the resource (either ID or name). - * @param strNewResName The new name of the resource. - **/ - template - int ResourceDirectory::setResourceNameT(S restypeid, T resid, std::string strNewResName) - { - std::vector::iterator ResIter = locateResourceT(restypeid, resid); - ResIter->entry.wstrName = strNewResName; - - return 0; - } -} - -#endif +/* +* ResourceDirectory.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 RESOURCEDIRECTORY_H +#define RESOURCEDIRECTORY_H + +#include "PeLibInc.h" + +namespace PeLib +{ + class ResourceElement; + + /// The class ResourceChild is used to store information about a resource node. + class ResourceChild + { + friend class ResourceElement; + friend class ResourceDirectory; + friend class ResourceNode; + friend class ResourceLeaf; + + /// Stores name and offset of a resource node. + PELIB_IMG_RES_DIR_ENTRY entry; + /// A pointer to one of the node's child nodes. + ResourceElement* child; + + public: + /// Function which compares a resource ID to the node's resource ID. + bool equalId(dword wId) const; // EXPORT + /// Function which compares a string to the node's resource name. + bool equalName(std::string strName) const; // EXPORT + /// Predicate that determines if a child is identified by name or by ID. + bool isNamedResource() const; // EXPORT + /// Used for sorting a node's children. + bool operator<(const ResourceChild& rc) const; // EXPORT + /// Returns the size of a resource child. +// unsigned int size() const; + + /// Standard constructor. Does absolutely nothing. + ResourceChild(); + /// Makes a deep copy of a ResourceChild object. + ResourceChild(const ResourceChild& rhs); + /// Makes a deep copy of a ResourceChild object. + ResourceChild& operator=(const ResourceChild& rhs); + /// Deletes a ResourceChild object. + ~ResourceChild(); + }; + + /// Base class for ResourceNode and ResourceLeaf, the elements of the resource tree. + /// \todo write + class ResourceElement + { + friend class ResourceChild; + friend class ResourceNode; + friend class ResourceLeaf; + + protected: + /// Stores RVA of the resource element in the file. + unsigned int uiElementRva; + + /// Reads the next resource element from the InputBuffer. + virtual int read(InputBuffer&, unsigned int, unsigned int/*, const std::string&*/) = 0; + /// Writes the next resource element into the OutputBuffer. + virtual void rebuild(OutputBuffer&, unsigned int&, unsigned int, const std::string&) const = 0; + + public: + /// Returns the RVA of the element in the file. + unsigned int getElementRva() const; // EXPORT + /// Indicates if the resource element is a leaf or a node. + virtual bool isLeaf() const = 0; // EXPORT + /// Corrects erroneous valeus in the ResourceElement. + virtual void makeValid() = 0; // EXPORT + /// Returns the size of a resource element. +// virtual unsigned int size() const = 0; + /// Necessary virtual destructor. + virtual ~ResourceElement() {} + }; + + /// ResourceLeafs represent the leafs of the resource tree: The actual resources. + class ResourceLeaf : public ResourceElement + { + friend class ResourceChild; + friend class ResourceDirectory; + template friend struct fixNumberOfEntries; + + private: + /// The resource data. + std::vector m_data; + /// PeLib equivalent of the Win32 structure IMAGE_RESOURCE_DATA_ENTRY + PELIB_IMAGE_RESOURCE_DATA_ENTRY entry; + + protected: + int read(InputBuffer& inpBuffer, unsigned int uiOffset, unsigned int rva/*, const std::string&*/); + /// Writes the next resource leaf into the OutputBuffer. + void rebuild(OutputBuffer&, unsigned int& uiOffset, unsigned int uiRva, const std::string&) const; + + public: + /// Indicates if the resource element is a leaf or a node. + bool isLeaf() const; // EXPORT + /// Corrects erroneous valeus in the ResourceLeaf. + void makeValid(); // EXPORT + /// Reads the next resource leaf from the InputBuffer. + /// Returns the size of a resource lead. +// unsigned int size() const; + + /// Returns the resource data of this resource leaf. + std::vector getData() const; // EXPORT + /// Sets the resource data of this resource leaf. + void setData(const std::vector& vData); // EXPORT + + /// Returns the OffsetToData value of this resource leaf. + dword getOffsetToData() const; // EXPORT + /// Returns the Size value of this resource leaf. + dword getSize() const; // EXPORT + /// Returns the CodePage value of this resource leaf. + dword getCodePage() const; // EXPORT + /// Returns the Reserved value of this resource leaf. + dword getReserved() const; // EXPORT + + /// Sets the OffsetToData value of this resource leaf. + void setOffsetToData(dword dwValue); // EXPORT + /// Sets the Size value of this resource leaf. + void setSize(dword dwValue); // EXPORT + /// Sets the CodePage value of this resource leaf. + void setCodePage(dword dwValue); // EXPORT + /// Sets the Reserved value of this resource leaf. + void setReserved(dword dwValue); // EXPORT + }; + + /// ResourceNodes represent the nodes in the resource tree. + class ResourceNode : public ResourceElement + { + friend class ResourceChild; + friend class ResourceDirectory; + template friend struct fixNumberOfEntries; + + /// The node's children. + std::vector children; + /// The node's header. Equivalent to IMAGE_RESOURCE_DIRECTORY from the Win32 API. + PELIB_IMAGE_RESOURCE_DIRECTORY header; + + protected: + /// Reads the next resource node. + int read(InputBuffer& inpBuffer, unsigned int uiOffset, unsigned int rva/*, const std::string&*/); + /// Writes the next resource node into the OutputBuffer. + void rebuild(OutputBuffer&, unsigned int& uiOffset, unsigned int uiRva, const std::string&) const; + + public: + /// Indicates if the resource element is a leaf or a node. + bool isLeaf() const; // EXPORT + /// Corrects erroneous valeus in the ResourceNode. + void makeValid(); // EXPORT + + /// Returns the node's number of children. + unsigned int getNumberOfChildren() const; // EXPORT + /// Adds another child to node. + void addChild(); // EXPORT + /// Returns a node's child. + ResourceElement* getChild(unsigned int uiIndex); // EXPORT + /// Removes a node's child. + void removeChild(unsigned int uiIndex); // EXPORT + + /// Returns the name of one of the node's children. + std::string getChildName(unsigned int uiIndex) const; // EXPORT + /// Returns the Name value of one of the node's children. + dword getOffsetToChildName(unsigned int uiIndex) const; // EXPORT + /// Returns the OffsetToData value of one of the node's children. + dword getOffsetToChildData(unsigned int uiIndex) const; // EXPORT + + /// Sets the name of one of the node's children. + void setChildName(unsigned int uiIndex, const std::string& strNewName); // EXPORT + /// Sets the Name value of one of the node's children. + void setOffsetToChildName(unsigned int uiIndex, dword dwNewOffset); // EXPORT + /// Sets the OffsetToData value of one of the node's children. + void setOffsetToChildData(unsigned int uiIndex, dword dwNewOffset); // EXPORT + + /// Returns the node's Characteristics value. + dword getCharacteristics() const; // EXPORT + /// Returns the node's TimeDateStamp value. + dword getTimeDateStamp() const; // EXPORT + /// Returns the node's MajorVersion value. + word getMajorVersion() const; // EXPORT + /// Returns the node's MinorVersion value. + word getMinorVersion() const; // EXPORT + /// Returns the node's NumberOfNamedEntries value. + word getNumberOfNamedEntries() const; // EXPORT + /// Returns the node's NumberOfIdEntries value. + word getNumberOfIdEntries() const; // EXPORT + + /// Sets the node's Characteristics value. + void setCharacteristics(dword value); // EXPORT + /// Sets the node's TimeDateStamp value. + void setTimeDateStamp(dword value); // EXPORT + /// Sets the node's MajorVersion value. + void setMajorVersion(word value); // EXPORT + /// Sets the node's MinorVersion value. + void setMinorVersion(word value); // EXPORT + /// Sets the node's NumberOfNamedEntries value. + void setNumberOfNamedEntries(word value); // EXPORT + /// Sets the node's NumberOfIdEntries value. + void setNumberOfIdEntries(word value); // EXPORT + + /// Returns the size of a resource node. +// unsigned int size() const; + }; + + /// Auxiliary functor which is used to search through the resource tree. + /** + * Traits class for the template functions of ResourceDirectory. + * It's used to find out which function to use when searching for resource nodes or resource leafs + * in a node's children vector. + **/ + template + struct ResComparer + { + /// Pointer to a member function of ResourceChild + typedef bool(ResourceChild::*CompFunc)(T) const; + + /// Return 0 for all unspecialized versions of ResComparer. + static CompFunc comp(); + }; + + /// Auxiliary functor which is used to search through the resource tree. + /** + * ResComparer is used when a resource element is searched for by ID. + **/ + template<> + struct ResComparer + { + /// Pointer to a member function of ResourceChild + typedef bool(ResourceChild::*CompFunc)(dword) const; + + /// Return the address of the ResourceChild member function that compares the ids of resource elements. + static CompFunc comp() + { + return &ResourceChild::equalId; + } + }; + + /// Auxiliary functor which is used to search through the resource tree. + /** + * This specializd version of ResComparer is used when a resource element is searched for by name. + **/ + template<> + struct ResComparer + { + /// Pointer to a member function of ResourceChild + typedef bool(ResourceChild::*CompFunc)(std::string) const; + + /// Return the address of the ResourceChild member function that compares the names of resource elements. + static CompFunc comp() + { + return &ResourceChild::equalName; + } + }; + + /// Unspecialized function that's used as base template for the specialized versions below. + template + struct fixNumberOfEntries + { + /// Fixes a resource node's header. + static void fix(ResourceNode*); + }; + + /// Fixes NumberOfIdEntries value of a node. + template<> + struct fixNumberOfEntries + { + /// Fixes a resource node's NumberOfIdEntries value. + static void fix(ResourceNode* node) + { + node->header.NumberOfIdEntries = (unsigned int)node->children.size() - std::count_if(node->children.begin(), node->children.end(), std::mem_fun_ref(&ResourceChild::isNamedResource)); + } + }; + + /// Fixes NumberOfNamedEntries value of a node. + template<> + struct fixNumberOfEntries + { + /// Fixes a resource node's NumberOfNamedEntries value. + static void fix(ResourceNode* node) + { + node->header.NumberOfNamedEntries = static_cast(std::count_if(node->children.begin(), node->children.end(), std::mem_fun_ref(&ResourceChild::isNamedResource))); + } + }; + + /// Class that represents the resource directory of a PE file. + /** + * The class ResourceDirectory represents the resource directory of a PE file. This class is fundamentally + * different from the other classes of the PeLib library due to the structure of the ResourceDirectory. + * For once, it's possible to manipulate the ResourceDirectory through a set of "high level" functions and + * and through a set of "low level" functions. The "high level" functions are the functions inside the + * ResourceDirectory class with the exception of getRoot.

+ * getRoot on the other hand is the first "low level" function. Use it to retrieve the root node of the + * resource tree. Then you can traverse through the tree and manipulate individual nodes and leafs + * directly using the functions provided by the classes ResourceNode and ResourceLeaf.

+ * There's another difference between the ResourceDirectory class and the other PeLib classes, which is + * once again caused by the special structure of the PE resource directory. The nodes of the resource + * tree must be in a certain order. Manipulating the resource tree does not directly sort the nodes + * correctly as this would cause more trouble than it fixes. That means it's your responsibility to + * fix the resource tree after manipulating it. PeLib makes the job easy for you, just call the + * ResourceDirectory::makeValid function.

+ * You might also wonder why there's no size() function in this class. I did not forget it. It's just + * that it's impossible to calculate the size of the resource directory without rebuilding it. So why + * should PeLib do this if you can do it just as easily by calling rebuild() and then checking the length + * of the returned vector.

+ * There are also different ways to serialize (rebuild) the resource tree as it's not a fixed structure + * that can easily be minimized like most other PE directories.

+ * This means it's entirely possible that the resource tree you read from a file differs from the one + * PeLib creates. This might cause a minor issue. The original resource tree might be smaller (due to + * different padding) so it's crucial that you check if there's enough space in the original resource + * directory before you write the rebuilt resource directory back to the file. + **/ + class ResourceDirectory + { + private: + /// The root node of the resource directory. + ResourceNode m_rnRoot; + + // Prepare for some crazy syntax below to make Digital Mars happy. + + /// Retrieves an iterator to a specified resource child. + template + std::vector::const_iterator locateResourceT(S restypeid, T resid) const; + + /// Retrieves an iterator to a specified resource child. + template + std::vector::iterator locateResourceT(S restypeid, T resid); + + /// Adds a new resource. + template + int addResourceT(S restypeid, T resid, ResourceChild& rc); + + /// Removes new resource. + template + int removeResourceT(S restypeid, T resid); + + /// Returns the data of a resource. + template + int getResourceDataT(S restypeid, T resid, std::vector& data) const; + + /// Sets the data of a resource. + template + int setResourceDataT(S restypeid, T resid, std::vector& data); + + /// Returns the ID of a resource. + template + dword getResourceIdT(S restypeid, T resid) const; + + /// Sets the ID of a resource. + template + int setResourceIdT(S restypeid, T resid, dword dwNewResId); + + /// Returns the name of a resource. + template + std::string getResourceNameT(S restypeid, T resid) const; + + /// Sets the name of a resource. + template + int setResourceNameT(S restypeid, T resid, std::string strNewResName); + + public: + ResourceNode* getRoot(); + /// Corrects a erroneous resource directory. + void makeValid(); + /// Reads the resource directory from a file. + int read(const std::string& strFilename, unsigned int uiOffset, unsigned int uiSize, unsigned int uiResDirRva); + /// Rebuilds the resource directory. + void rebuild(std::vector& vBuffer, unsigned int uiRva) const; + /// Returns the size of the rebuilt resource directory. +// unsigned int size() const; + /// Writes the resource directory to a file. + int write(const std::string& strFilename, unsigned int uiOffset, unsigned int uiRva) const; + + /// Adds a new resource type. + int addResourceType(dword dwResTypeId); + /// Adds a new resource type. + int addResourceType(const std::string& strResTypeName); + + /// Removes a resource type and all of it's resources. + int removeResourceType(dword dwResTypeId); + /// Removes a resource type and all of it's resources. + int removeResourceType(const std::string& strResTypeName); + + /// Removes a resource type and all of it's resources. + int removeResourceTypeByIndex(unsigned int uiIndex); + + /// Adds a new resource. + int addResource(dword dwResTypeId, dword dwResId); + /// Adds a new resource. + int addResource(dword dwResTypeId, const std::string& strResName); + /// Adds a new resource. + int addResource(const std::string& strResTypeName, dword dwResId); + /// Adds a new resource. + int addResource(const std::string& strResTypeName, const std::string& strResName); + + /// Removes a resource. + int removeResource(dword dwResTypeId, dword dwResId); + /// Removes a resource. + int removeResource(dword dwResTypeId, const std::string& strResName); + /// Removes a resource. + int removeResource(const std::string& strResTypeName, dword dwResId); + /// Removes a resource. + int removeResource(const std::string& strResTypeName, const std::string& strResName); + + /// Returns the number of resource types. + unsigned int getNumberOfResourceTypes() const; + + /// Returns the ID of a resource type. + dword getResourceTypeIdByIndex(unsigned int uiIndex) const; + /// Returns the name of a resource type. + std::string getResourceTypeNameByIndex(unsigned int uiIndex) const; + + /// Converts a resource type ID to an index. + int resourceTypeIdToIndex(dword dwResTypeId) const; + /// Converts a resource type name to an index. + int resourceTypeNameToIndex(const std::string& strResTypeName) const; + + /// Returns the number of resources of a certain resource type. + unsigned int getNumberOfResources(dword dwId) const; + /// Returns the number of resources of a certain resource type. + unsigned int getNumberOfResources(const std::string& strResTypeName) const; + + /// Returns the number of resources of a certain resource type. + unsigned int getNumberOfResourcesByIndex(unsigned int uiIndex) const; + + /// Returns the data of a certain resource. + void getResourceData(dword dwResTypeId, dword dwResId, std::vector& data) const; + /// Returns the data of a certain resource. + void getResourceData(dword dwResTypeId, const std::string& strResName, std::vector& data) const; + /// Returns the data of a certain resource. + void getResourceData(const std::string& strResTypeName, dword dwResId, std::vector& data) const; + /// Returns the data of a certain resource. + void getResourceData(const std::string& strResTypeName, const std::string& strResName, std::vector& data) const; + + /// Returns the data of a certain resource. + void getResourceDataByIndex(unsigned int uiResTypeIndex, unsigned int uiResIndex, std::vector& data) const; + + /// Sets the data of a certain resource. + void setResourceData(dword dwResTypeId, dword dwResId, std::vector& data); + /// Sets the data of a certain resource. + void setResourceData(dword dwResTypeId, const std::string& strResName, std::vector& data); + /// Sets the data of a certain resource. + void setResourceData(const std::string& strResTypeName, dword dwResId, std::vector& data); + /// Sets the data of a certain resource. + void setResourceData(const std::string& strResTypeName, const std::string& strResName, std::vector& data); + + /// Sets the data of a certain resource. + void setResourceDataByIndex(unsigned int uiResTypeIndex, unsigned int uiResIndex, std::vector& data); + + /// Returns the ID of a certain resource. + dword getResourceId(dword dwResTypeId, const std::string& strResName) const; + /// Returns the ID of a certain resource. + dword getResourceId(const std::string& strResTypeName, const std::string& strResName) const; + + /// Returns the ID of a certain resource. + dword getResourceIdByIndex(unsigned int uiResTypeIndex, unsigned int uiResIndex) const; + + /// Sets the ID of a certain resource. + void setResourceId(dword dwResTypeId, dword dwResId, dword dwNewResId); + /// Sets the ID of a certain resource. + void setResourceId(dword dwResTypeId, const std::string& strResName, dword dwNewResId); + /// Sets the ID of a certain resource. + void setResourceId(const std::string& strResTypeName, dword dwResId, dword dwNewResId); + /// Sets the ID of a certain resource. + void setResourceId(const std::string& strResTypeName, const std::string& strResName, dword dwNewResId); + + /// Sets the ID of a certain resource. + void setResourceIdByIndex(unsigned int uiResTypeIndex, unsigned int uiResIndex, dword dwNewResId); + + /// Returns the name of a certain resource. + std::string getResourceName(dword dwResTypeId, dword dwResId) const; + /// Returns the name of a certain resource. + std::string getResourceName(const std::string& strResTypeName, dword dwResId) const; + + /// Returns the name of a certain resource. + std::string getResourceNameByIndex(unsigned int uiResTypeIndex, unsigned int uiResIndex) const; + + /// Sets the name of a certain resource. + void setResourceName(dword dwResTypeId, dword dwResId, const std::string& strNewResName); + /// Sets the name of a certain resource. + void setResourceName(dword dwResTypeId, const std::string& strResName, const std::string& strNewResName); + /// Sets the name of a certain resource. + void setResourceName(const std::string& strResTypeName, dword dwResId, const std::string& strNewResName); + /// Sets the name of a certain resource. + void setResourceName(const std::string& strResTypeName, const std::string& strResName, const std::string& strNewResName); + + /// Sets the name of a certain resource. + void setResourceNameByIndex(unsigned int uiResTypeIndex, unsigned int uiResIndex, const std::string& strNewResName); + }; + + /** + * Looks through the entire resource tree and returns a const_iterator to the resource specified + * by the parameters. + * @param restypeid Identifier of the resource type (either ID or name). + * @param resid Identifier of the resource (either ID or name). + * @return A const_iterator to the specified resource. + **/ + template + std::vector::const_iterator ResourceDirectory::locateResourceT(S restypeid, T resid) const + { + typedef bool(ResourceChild::*CompFunc1)(S) const; + typedef bool(ResourceChild::*CompFunc2)(T) const; + + CompFunc1 comp1 = ResComparer::comp(); + CompFunc2 comp2 = ResComparer::comp(); + + std::vector::const_iterator Iter = std::find_if(m_rnRoot.children.begin(), m_rnRoot.children.end(), std::bind2nd(std::mem_fun_ref(comp1), restypeid)); + if (Iter == m_rnRoot.children.end()) + { + return Iter; + } + + ResourceNode* currNode = static_cast(Iter->child); + std::vector::const_iterator ResIter = std::find_if(currNode->children.begin(), currNode->children.end(), std::bind2nd(std::mem_fun_ref(comp2), resid)); + if (ResIter == currNode->children.end()) + { + return ResIter; + } + + return ResIter; + } + + /** + * Looks through the entire resource tree and returns an iterator to the resource specified + * by the parameters. + * @param restypeid Identifier of the resource type (either ID or name). + * @param resid Identifier of the resource (either ID or name). + * @return An iterator to the specified resource. + **/ + template + std::vector::iterator ResourceDirectory::locateResourceT(S restypeid, T resid) + { + typedef bool(ResourceChild::*CompFunc1)(S) const; + typedef bool(ResourceChild::*CompFunc2)(T) const; + + CompFunc1 comp1 = ResComparer::comp(); + CompFunc2 comp2 = ResComparer::comp(); + + std::vector::iterator Iter = std::find_if(m_rnRoot.children.begin(), m_rnRoot.children.end(), std::bind2nd(std::mem_fun_ref(comp1), restypeid)); + if (Iter == m_rnRoot.children.end()) + { + return Iter; + } + + ResourceNode* currNode = static_cast(Iter->child); + std::vector::iterator ResIter = std::find_if(currNode->children.begin(), currNode->children.end(), std::bind2nd(std::mem_fun_ref(comp2), resid)); + if (ResIter == currNode->children.end()) + { + return ResIter; + } + + return ResIter; + } + + /** + * Adds a new resource, resource type and ID are specified by the parameters. + * @param restypeid Identifier of the resource type (either ID or name). + * @param resid Identifier of the resource (either ID or name). + * @param rc ResourceChild that will be added. + **/ + template + int ResourceDirectory::addResourceT(S restypeid, T resid, ResourceChild& rc) + { + typedef bool(ResourceChild::*CompFunc1)(S) const; + typedef bool(ResourceChild::*CompFunc2)(T) const; + + CompFunc1 comp1 = ResComparer::comp(); + CompFunc2 comp2 = ResComparer::comp(); + + std::vector::iterator Iter = std::find_if(m_rnRoot.children.begin(), m_rnRoot.children.end(), std::bind2nd(std::mem_fun_ref(comp1), restypeid)); + if (Iter == m_rnRoot.children.end()) + { + return 1; + // throw Exceptions::ResourceTypeDoesNotExist(ResourceDirectoryId, __LINE__); + } + + ResourceNode* currNode = static_cast(Iter->child); + std::vector::iterator ResIter = std::find_if(currNode->children.begin(), currNode->children.end(), std::bind2nd(std::mem_fun_ref(comp2), resid)); + if (ResIter != currNode->children.end()) + { + return 1; +// throw Exceptions::EntryAlreadyExists(ResourceDirectoryId, __LINE__); + } + + rc.child = new ResourceNode; + ResourceChild rlnew; + rlnew.child = new ResourceLeaf; + ResourceNode* currNode2 = static_cast(rc.child); + currNode2->children.push_back(rlnew); + currNode->children.push_back(rc); + + fixNumberOfEntries::fix(currNode); + fixNumberOfEntries::fix(currNode2); + + return 0; + } + + /** + * Removes a resource, resource type and ID are specified by the parameters. + * @param restypeid Identifier of the resource type (either ID or name). + * @param resid Identifier of the resource (either ID or name). + **/ + template + int ResourceDirectory::removeResourceT(S restypeid, T resid) + { + typedef bool(ResourceChild::*CompFunc1)(S) const; + typedef bool(ResourceChild::*CompFunc2)(T) const; + + CompFunc1 comp1 = ResComparer::comp(); + CompFunc2 comp2 = ResComparer::comp(); + + std::vector::iterator Iter = std::find_if(m_rnRoot.children.begin(), m_rnRoot.children.end(), std::bind2nd(std::mem_fun_ref(comp1), restypeid)); + if (Iter == m_rnRoot.children.end()) + { + return 1; + //throw Exceptions::ResourceTypeDoesNotExist(ResourceDirectoryId, __LINE__); + } + + ResourceNode* currNode = static_cast(Iter->child); + std::vector::iterator ResIter = std::find_if(currNode->children.begin(), currNode->children.end(), std::bind2nd(std::mem_fun_ref(comp2), resid)); + if (ResIter == currNode->children.end()) + { + return 1; + // throw Exceptions::InvalidName(ResourceDirectoryId, __LINE__); + } + + currNode->children.erase(ResIter); + + fixNumberOfEntries::fix(currNode); + + return 0; + } + + /** + * Returns the data of a resource, resource type and ID are specified by the parameters. + * @param restypeid Identifier of the resource type (either ID or name). + * @param resid Identifier of the resource (either ID or name). + * @param data The data of the resource will be written into this vector. + **/ + template + int ResourceDirectory::getResourceDataT(S restypeid, T resid, std::vector& data) const + { + std::vector::const_iterator ResIter = locateResourceT(restypeid, resid); + ResourceNode* currNode = static_cast(ResIter->child); + ResourceLeaf* currLeaf = static_cast(currNode->children[0].child); + data.assign(currLeaf->m_data.begin(), currLeaf->m_data.end()); + + return 0; + } + + /** + * Sets the data of a resource, resource type and ID are specified by the parameters. + * @param restypeid Identifier of the resource type (either ID or name). + * @param resid Identifier of the resource (either ID or name). + * @param data The new data of the resource is taken from this vector. + **/ + template + int ResourceDirectory::setResourceDataT(S restypeid, T resid, std::vector& data) + { + std::vector::iterator ResIter = locateResourceT(restypeid, resid); + ResourceNode* currNode = static_cast(ResIter->child); + ResourceLeaf* currLeaf = static_cast(currNode->children[0].child); + currLeaf->m_data.assign(data.begin(), data.end()); + + return 0; + } + + /** + * Returns the id of a resource, resource type and ID are specified by the parameters. + * Note: Calling this function with resid == the ID of the resource makes no sense at all. + * @param restypeid Identifier of the resource type (either ID or name). + * @param resid Identifier of the resource (either ID or name). + * @return The ID of the specified resource. + **/ + template + dword ResourceDirectory::getResourceIdT(S restypeid, T resid) const + { + std::vector::const_iterator ResIter = locateResourceT(restypeid, resid); + return ResIter->entry.irde.Name; + } + + /** + * Sets the id of a resource, resource type and ID are specified by the parameters. + * @param restypeid Identifier of the resource type (either ID or name). + * @param resid Identifier of the resource (either ID or name). + * @param dwNewResId New ID of the resource. + **/ + template + int ResourceDirectory::setResourceIdT(S restypeid, T resid, dword dwNewResId) + { + std::vector::iterator ResIter = locateResourceT(restypeid, resid); + ResIter->entry.irde.Name = dwNewResId; + return 0; + } + + /** + * Returns the name of a resource, resource type and ID are specified by the parameters. + * Note: Calling this function with resid == the name of the resource makes no sense at all. + * @param restypeid Identifier of the resource type (either ID or name). + * @param resid Identifier of the resource (either ID or name). + * @return The name of the specified resource. + **/ + template + std::string ResourceDirectory::getResourceNameT(S restypeid, T resid) const + { + std::vector::const_iterator ResIter = locateResourceT(restypeid, resid); + return ResIter->entry.wstrName; + } + + /** + * Sets the name of a resource, resource type and ID are specified by the parameters. + * @param restypeid Identifier of the resource type (either ID or name). + * @param resid Identifier of the resource (either ID or name). + * @param strNewResName The new name of the resource. + **/ + template + int ResourceDirectory::setResourceNameT(S restypeid, T resid, std::string strNewResName) + { + std::vector::iterator ResIter = locateResourceT(restypeid, resid); + ResIter->entry.wstrName = strNewResName; + + return 0; + } +} + +#endif diff --git a/utils/zenutils/libraries/pelib-0.9/pelib/TlsDirectory.h b/utils/zenutils/libraries/pelib-0.9/pelib/TlsDirectory.h old mode 100755 new mode 100644 index ebea929f94..8190e65c3c --- a/utils/zenutils/libraries/pelib-0.9/pelib/TlsDirectory.h +++ b/utils/zenutils/libraries/pelib-0.9/pelib/TlsDirectory.h @@ -1,304 +1,304 @@ -/* -* TlsDirectory.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 TLSDIRECTORY_H -#define TLSDIRECTORY_H - -namespace PeLib -{ - /// Class that handles the TLS directory. - /** - * This class handles the TLS (Thread Local Storage) directory. - **/ - template - class TlsDirectory - { - private: - PELIB_IMAGE_TLS_DIRECTORY m_tls; ///< Structure that holds all information about the directory. - - void read(InputBuffer& inputbuffer); - - public: - /// Reads a file's TLS directory. - int read(const std::string& strFilename, unsigned int uiOffset, unsigned int uiSize); // EXPORT - int read(unsigned char* buffer, unsigned int buffersize); // EXPORT - /// Rebuilds the TLS directory. - void rebuild(std::vector& vBuffer) const; // EXPORT - /// Returns the size of the TLS Directory. - unsigned int size() const; // EXPORT - /// Writes the TLS directory to a file. - int write(const std::string& strFilename, unsigned int dwOffset) const; // EXPORT - - /// Returns the StartAddressOfRawData value of the TLS header. - dword getStartAddressOfRawData() const; // EXPORT - /// Returns the EndAddressOfRawData value of the TLS header. - dword getEndAddressOfRawData() const; // EXPORT - /// Returns the AddressOfIndex value of the TLS header. - dword getAddressOfIndex() const; // EXPORT - /// Returns the AddressOfCallBacks value of the TLS header. - dword getAddressOfCallBacks() const; // EXPORT - /// Returns the SizeOfZeroFill value of the TLS header. - dword getSizeOfZeroFill() const; // EXPORT - /// Returns the Characteristics value of the TLS header. - dword getCharacteristics() const; // EXPORT - - /// Sets the StartAddressOfRawData value of the TLS header. - void setStartAddressOfRawData(dword dwValue); // EXPORT - /// Sets the EndAddressOfRawData value of the TLS header. - void setEndAddressOfRawData(dword dwValue); // EXPORT - /// Sets the AddressOfIndex value of the TLS header. - void setAddressOfIndex(dword dwValue); // EXPORT - /// Sets the AddressOfCallBacks value of the TLS header. - void setAddressOfCallBacks(dword dwValue); // EXPORT - /// Sets the SizeOfZeroFill value of the TLS header. - void setSizeOfZeroFill(dword dwValue); // EXPORT - /// Sets the Characteristics value of the TLS header. - void setCharacteristics(dword dwValue); // EXPORT - }; - - template - void TlsDirectory::read(InputBuffer& inputBuffer) - { - PELIB_IMAGE_TLS_DIRECTORY itdCurr; - - inputBuffer >> itdCurr.StartAddressOfRawData; - inputBuffer >> itdCurr.EndAddressOfRawData; - inputBuffer >> itdCurr.AddressOfIndex; - inputBuffer >> itdCurr.AddressOfCallBacks; - inputBuffer >> itdCurr.SizeOfZeroFill; - inputBuffer >> itdCurr.Characteristics; - - std::swap(itdCurr, m_tls); - } - - template - int TlsDirectory::read(unsigned char* buffer, unsigned int buffersize) - { - if (buffersize < PELIB_IMAGE_TLS_DIRECTORY::size()) - { - return ERROR_INVALID_FILE; - } - - std::vector vTlsDirectory(buffer, buffer + buffersize); - - InputBuffer ibBuffer(vTlsDirectory); - read(ibBuffer); - return NO_ERROR; - } - - /** - * Reads a file's TLS directory. - * @param strFilename Name of the file. - * @param uiOffset File offset of the TLS directory. - * @param uiSize Size of the TLS directory. - **/ - template - int TlsDirectory::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 vTlsDirectory(uiSize); - ifFile.read(reinterpret_cast(&vTlsDirectory[0]), uiSize); - - InputBuffer ibBuffer(vTlsDirectory); - read(ibBuffer); - return NO_ERROR; - } - - /** - * Rebuilds the current TLS Directory. - * @param vBuffer Buffer where the TLS directory will be written to. - **/ - template - void TlsDirectory::rebuild(std::vector& vBuffer) const - { - OutputBuffer obBuffer(vBuffer); - - obBuffer << m_tls.StartAddressOfRawData; - obBuffer << m_tls.EndAddressOfRawData; - obBuffer << m_tls.AddressOfIndex; - obBuffer << m_tls.AddressOfCallBacks; - obBuffer << m_tls.SizeOfZeroFill; - obBuffer << m_tls.Characteristics; - } - - /** - * Returns the size of the TLS directory. Due to the static nature of this structure the return value - * will always be 24. - * @return Size in bytes. - **/ - template - unsigned int TlsDirectory::size() const - { - return PELIB_IMAGE_TLS_DIRECTORY::size(); - } - - /** - * @param strFilename Name of the file. - * @param dwOffset File offset the TLS Directory will be written to. - **/ - template - int TlsDirectory::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 vBuffer; - rebuild(vBuffer); - - ofFile.write(reinterpret_cast(&vBuffer[0]), vBuffer.size()); - - ofFile.close(); - - return NO_ERROR; - } - - /** - * @return The StartAddressOfRawData value of the TLS directory. - **/ - template - dword TlsDirectory::getStartAddressOfRawData() const - { - return m_tls.StartAddressOfRawData; - } - - /** - * @return The EndAddressOfRawData value of the TLS directory. - **/ - template - dword TlsDirectory::getEndAddressOfRawData() const - { - return m_tls.EndAddressOfRawData; - } - - /** - * @return The AddressOfIndex value of the TLS directory. - **/ - template - dword TlsDirectory::getAddressOfIndex() const - { - return m_tls.AddressOfIndex; - } - - /** - * @return The AddressOfCallBacks value of the TLS directory. - **/ - template - dword TlsDirectory::getAddressOfCallBacks() const - { - return m_tls.AddressOfCallBacks; - } - - /** - * @return The SizeOfZeroFill value of the TLS directory. - **/ - template - dword TlsDirectory::getSizeOfZeroFill() const - { - return m_tls.SizeOfZeroFill; - } - - /** - * @return The Characteristics value of the TLS directory. - **/ - template - dword TlsDirectory::getCharacteristics() const - { - return m_tls.Characteristics; - } - - /** - * @param dwValue The new StartAddressOfRawData value of the TLS directory. - **/ - template - void TlsDirectory::setStartAddressOfRawData(dword dwValue) - { - m_tls.StartAddressOfRawData = dwValue; - } - - /** - * @param dwValue The new EndAddressOfRawData value of the TLS directory. - **/ - template - void TlsDirectory::setEndAddressOfRawData(dword dwValue) - { - m_tls.EndAddressOfRawData = dwValue; - } - - /** - * @param dwValue The new AddressOfIndex value of the TLS directory. - **/ - template - void TlsDirectory::setAddressOfIndex(dword dwValue) - { - m_tls.AddressOfIndex = dwValue; - } - - /** - * @param dwValue The new AddressOfCallBacks value of the TLS directory. - **/ - template - void TlsDirectory::setAddressOfCallBacks(dword dwValue) - { - m_tls.AddressOfCallBacks = dwValue; - } - - /** - * @param dwValue The new SizeOfZeroFill value of the TLS directory. - **/ - template - void TlsDirectory::setSizeOfZeroFill(dword dwValue) - { - m_tls.SizeOfZeroFill = dwValue; - } - - /** - * @param dwValue The new Characteristics value of the TLS directory. - **/ - template - void TlsDirectory::setCharacteristics(dword dwValue) - { - m_tls.Characteristics = dwValue; - } - -} -#endif +/* +* TlsDirectory.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 TLSDIRECTORY_H +#define TLSDIRECTORY_H + +namespace PeLib +{ + /// Class that handles the TLS directory. + /** + * This class handles the TLS (Thread Local Storage) directory. + **/ + template + class TlsDirectory + { + private: + PELIB_IMAGE_TLS_DIRECTORY m_tls; ///< Structure that holds all information about the directory. + + void read(InputBuffer& inputbuffer); + + public: + /// Reads a file's TLS directory. + int read(const std::string& strFilename, unsigned int uiOffset, unsigned int uiSize); // EXPORT + int read(unsigned char* buffer, unsigned int buffersize); // EXPORT + /// Rebuilds the TLS directory. + void rebuild(std::vector& vBuffer) const; // EXPORT + /// Returns the size of the TLS Directory. + unsigned int size() const; // EXPORT + /// Writes the TLS directory to a file. + int write(const std::string& strFilename, unsigned int dwOffset) const; // EXPORT + + /// Returns the StartAddressOfRawData value of the TLS header. + dword getStartAddressOfRawData() const; // EXPORT + /// Returns the EndAddressOfRawData value of the TLS header. + dword getEndAddressOfRawData() const; // EXPORT + /// Returns the AddressOfIndex value of the TLS header. + dword getAddressOfIndex() const; // EXPORT + /// Returns the AddressOfCallBacks value of the TLS header. + dword getAddressOfCallBacks() const; // EXPORT + /// Returns the SizeOfZeroFill value of the TLS header. + dword getSizeOfZeroFill() const; // EXPORT + /// Returns the Characteristics value of the TLS header. + dword getCharacteristics() const; // EXPORT + + /// Sets the StartAddressOfRawData value of the TLS header. + void setStartAddressOfRawData(dword dwValue); // EXPORT + /// Sets the EndAddressOfRawData value of the TLS header. + void setEndAddressOfRawData(dword dwValue); // EXPORT + /// Sets the AddressOfIndex value of the TLS header. + void setAddressOfIndex(dword dwValue); // EXPORT + /// Sets the AddressOfCallBacks value of the TLS header. + void setAddressOfCallBacks(dword dwValue); // EXPORT + /// Sets the SizeOfZeroFill value of the TLS header. + void setSizeOfZeroFill(dword dwValue); // EXPORT + /// Sets the Characteristics value of the TLS header. + void setCharacteristics(dword dwValue); // EXPORT + }; + + template + void TlsDirectory::read(InputBuffer& inputBuffer) + { + PELIB_IMAGE_TLS_DIRECTORY itdCurr; + + inputBuffer >> itdCurr.StartAddressOfRawData; + inputBuffer >> itdCurr.EndAddressOfRawData; + inputBuffer >> itdCurr.AddressOfIndex; + inputBuffer >> itdCurr.AddressOfCallBacks; + inputBuffer >> itdCurr.SizeOfZeroFill; + inputBuffer >> itdCurr.Characteristics; + + std::swap(itdCurr, m_tls); + } + + template + int TlsDirectory::read(unsigned char* buffer, unsigned int buffersize) + { + if (buffersize < PELIB_IMAGE_TLS_DIRECTORY::size()) + { + return ERROR_INVALID_FILE; + } + + std::vector vTlsDirectory(buffer, buffer + buffersize); + + InputBuffer ibBuffer(vTlsDirectory); + read(ibBuffer); + return NO_ERROR; + } + + /** + * Reads a file's TLS directory. + * @param strFilename Name of the file. + * @param uiOffset File offset of the TLS directory. + * @param uiSize Size of the TLS directory. + **/ + template + int TlsDirectory::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 vTlsDirectory(uiSize); + ifFile.read(reinterpret_cast(&vTlsDirectory[0]), uiSize); + + InputBuffer ibBuffer(vTlsDirectory); + read(ibBuffer); + return NO_ERROR; + } + + /** + * Rebuilds the current TLS Directory. + * @param vBuffer Buffer where the TLS directory will be written to. + **/ + template + void TlsDirectory::rebuild(std::vector& vBuffer) const + { + OutputBuffer obBuffer(vBuffer); + + obBuffer << m_tls.StartAddressOfRawData; + obBuffer << m_tls.EndAddressOfRawData; + obBuffer << m_tls.AddressOfIndex; + obBuffer << m_tls.AddressOfCallBacks; + obBuffer << m_tls.SizeOfZeroFill; + obBuffer << m_tls.Characteristics; + } + + /** + * Returns the size of the TLS directory. Due to the static nature of this structure the return value + * will always be 24. + * @return Size in bytes. + **/ + template + unsigned int TlsDirectory::size() const + { + return PELIB_IMAGE_TLS_DIRECTORY::size(); + } + + /** + * @param strFilename Name of the file. + * @param dwOffset File offset the TLS Directory will be written to. + **/ + template + int TlsDirectory::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 vBuffer; + rebuild(vBuffer); + + ofFile.write(reinterpret_cast(&vBuffer[0]), vBuffer.size()); + + ofFile.close(); + + return NO_ERROR; + } + + /** + * @return The StartAddressOfRawData value of the TLS directory. + **/ + template + dword TlsDirectory::getStartAddressOfRawData() const + { + return m_tls.StartAddressOfRawData; + } + + /** + * @return The EndAddressOfRawData value of the TLS directory. + **/ + template + dword TlsDirectory::getEndAddressOfRawData() const + { + return m_tls.EndAddressOfRawData; + } + + /** + * @return The AddressOfIndex value of the TLS directory. + **/ + template + dword TlsDirectory::getAddressOfIndex() const + { + return m_tls.AddressOfIndex; + } + + /** + * @return The AddressOfCallBacks value of the TLS directory. + **/ + template + dword TlsDirectory::getAddressOfCallBacks() const + { + return m_tls.AddressOfCallBacks; + } + + /** + * @return The SizeOfZeroFill value of the TLS directory. + **/ + template + dword TlsDirectory::getSizeOfZeroFill() const + { + return m_tls.SizeOfZeroFill; + } + + /** + * @return The Characteristics value of the TLS directory. + **/ + template + dword TlsDirectory::getCharacteristics() const + { + return m_tls.Characteristics; + } + + /** + * @param dwValue The new StartAddressOfRawData value of the TLS directory. + **/ + template + void TlsDirectory::setStartAddressOfRawData(dword dwValue) + { + m_tls.StartAddressOfRawData = dwValue; + } + + /** + * @param dwValue The new EndAddressOfRawData value of the TLS directory. + **/ + template + void TlsDirectory::setEndAddressOfRawData(dword dwValue) + { + m_tls.EndAddressOfRawData = dwValue; + } + + /** + * @param dwValue The new AddressOfIndex value of the TLS directory. + **/ + template + void TlsDirectory::setAddressOfIndex(dword dwValue) + { + m_tls.AddressOfIndex = dwValue; + } + + /** + * @param dwValue The new AddressOfCallBacks value of the TLS directory. + **/ + template + void TlsDirectory::setAddressOfCallBacks(dword dwValue) + { + m_tls.AddressOfCallBacks = dwValue; + } + + /** + * @param dwValue The new SizeOfZeroFill value of the TLS directory. + **/ + template + void TlsDirectory::setSizeOfZeroFill(dword dwValue) + { + m_tls.SizeOfZeroFill = dwValue; + } + + /** + * @param dwValue The new Characteristics value of the TLS directory. + **/ + template + void TlsDirectory::setCharacteristics(dword dwValue) + { + m_tls.Characteristics = dwValue; + } + +} +#endif diff --git a/utils/zenutils/libraries/pelib-0.9/pelib/buffer/InputBuffer.cpp b/utils/zenutils/libraries/pelib-0.9/pelib/buffer/InputBuffer.cpp old mode 100755 new mode 100644 index ae2584edb1..579b152659 --- a/utils/zenutils/libraries/pelib-0.9/pelib/buffer/InputBuffer.cpp +++ b/utils/zenutils/libraries/pelib-0.9/pelib/buffer/InputBuffer.cpp @@ -1,58 +1,58 @@ -/* -* InputBuffer.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 "InputBuffer.h" - -namespace PeLib -{ - unsigned long InputBuffer::get() - { - return ulIndex; - } - - InputBuffer::InputBuffer(std::vector& vBuffer) : m_vBuffer(vBuffer), ulIndex(0) - { - } - - const unsigned char* InputBuffer::data() const - { - return &m_vBuffer[0]; - } - - unsigned long InputBuffer::size() - { - return static_cast(m_vBuffer.size()); - } - - void InputBuffer::read(char* lpBuffer, unsigned long ulSize) - { - std::copy(&m_vBuffer[ulIndex], &m_vBuffer[ulIndex + ulSize], lpBuffer); - ulIndex += ulSize; - } - - void InputBuffer::reset() - { - m_vBuffer.clear(); - } - - void InputBuffer::set(unsigned long ulIndex) - { - this->ulIndex = ulIndex; - } - - void InputBuffer::setBuffer(std::vector& vBuffer) - { - m_vBuffer = vBuffer; - ulIndex = 0; - } -} - +/* +* InputBuffer.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 "InputBuffer.h" + +namespace PeLib +{ + unsigned long InputBuffer::get() + { + return ulIndex; + } + + InputBuffer::InputBuffer(std::vector& vBuffer) : m_vBuffer(vBuffer), ulIndex(0) + { + } + + const unsigned char* InputBuffer::data() const + { + return &m_vBuffer[0]; + } + + unsigned long InputBuffer::size() + { + return static_cast(m_vBuffer.size()); + } + + void InputBuffer::read(char* lpBuffer, unsigned long ulSize) + { + std::copy(&m_vBuffer[ulIndex], &m_vBuffer[ulIndex + ulSize], lpBuffer); + ulIndex += ulSize; + } + + void InputBuffer::reset() + { + m_vBuffer.clear(); + } + + void InputBuffer::set(unsigned long ulIndex) + { + this->ulIndex = ulIndex; + } + + void InputBuffer::setBuffer(std::vector& vBuffer) + { + m_vBuffer = vBuffer; + ulIndex = 0; + } +} + diff --git a/utils/zenutils/libraries/pelib-0.9/pelib/buffer/InputBuffer.h b/utils/zenutils/libraries/pelib-0.9/pelib/buffer/InputBuffer.h old mode 100755 new mode 100644 index fc5a14e357..18a7b1b7bc --- a/utils/zenutils/libraries/pelib-0.9/pelib/buffer/InputBuffer.h +++ b/utils/zenutils/libraries/pelib-0.9/pelib/buffer/InputBuffer.h @@ -1,52 +1,52 @@ -/* -* InputBuffer.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 INPUTBUFFER_H -#define INPUTBUFFER_H - -#include -#include -#include - -namespace PeLib -{ - class InputBuffer - { - private: - std::vector& m_vBuffer; - unsigned long ulIndex; - - public: - InputBuffer(std::vector& vBuffer); - - const unsigned char* data() const; - unsigned long size(); - - template - InputBuffer& operator>>(T& value) - { - assert(ulIndex + sizeof(value) <= m_vBuffer.size()); - value = *(T*)(&m_vBuffer[ulIndex]);//reinterpret_cast(&m_vBuffer[ulIndex]); - ulIndex += sizeof(T); - return *this; - } - - void read(char* lpBuffer, unsigned long ulSize); - void reset(); - void set(unsigned long ulIndex); - unsigned long get(); - void setBuffer(std::vector& vBuffer); -// void updateData(unsigned long ulIndex, - }; -} - -#endif +/* +* InputBuffer.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 INPUTBUFFER_H +#define INPUTBUFFER_H + +#include +#include +#include + +namespace PeLib +{ + class InputBuffer + { + private: + std::vector& m_vBuffer; + unsigned long ulIndex; + + public: + InputBuffer(std::vector& vBuffer); + + const unsigned char* data() const; + unsigned long size(); + + template + InputBuffer& operator>>(T& value) + { + assert(ulIndex + sizeof(value) <= m_vBuffer.size()); + value = *(T*)(&m_vBuffer[ulIndex]);//reinterpret_cast(&m_vBuffer[ulIndex]); + ulIndex += sizeof(T); + return *this; + } + + void read(char* lpBuffer, unsigned long ulSize); + void reset(); + void set(unsigned long ulIndex); + unsigned long get(); + void setBuffer(std::vector& vBuffer); +// void updateData(unsigned long ulIndex, + }; +} + +#endif diff --git a/utils/zenutils/libraries/pelib-0.9/pelib/buffer/OutputBuffer.cpp b/utils/zenutils/libraries/pelib-0.9/pelib/buffer/OutputBuffer.cpp old mode 100755 new mode 100644 index b47fbb6ff8..4ad8471d83 --- a/utils/zenutils/libraries/pelib-0.9/pelib/buffer/OutputBuffer.cpp +++ b/utils/zenutils/libraries/pelib-0.9/pelib/buffer/OutputBuffer.cpp @@ -1,41 +1,41 @@ -/* -* OutputBuffer.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 "OutputBuffer.h" - -namespace PeLib -{ - OutputBuffer::OutputBuffer(std::vector& vBuffer) : m_vBuffer(vBuffer) - { - m_vBuffer.clear(); - } - - const unsigned char* OutputBuffer::data() const - { - return &m_vBuffer[0]; - } - - unsigned long OutputBuffer::size() - { - return static_cast(m_vBuffer.size()); - } - - void OutputBuffer::add(const char* lpBuffer, unsigned long ulSize) - { - std::copy(lpBuffer, lpBuffer + ulSize, std::back_inserter(m_vBuffer)); - } - - void OutputBuffer::reset() - { - m_vBuffer.clear(); - } -} +/* +* OutputBuffer.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 "OutputBuffer.h" + +namespace PeLib +{ + OutputBuffer::OutputBuffer(std::vector& vBuffer) : m_vBuffer(vBuffer) + { + m_vBuffer.clear(); + } + + const unsigned char* OutputBuffer::data() const + { + return &m_vBuffer[0]; + } + + unsigned long OutputBuffer::size() + { + return static_cast(m_vBuffer.size()); + } + + void OutputBuffer::add(const char* lpBuffer, unsigned long ulSize) + { + std::copy(lpBuffer, lpBuffer + ulSize, std::back_inserter(m_vBuffer)); + } + + void OutputBuffer::reset() + { + m_vBuffer.clear(); + } +} diff --git a/utils/zenutils/libraries/pelib-0.9/pelib/buffer/OutputBuffer.h b/utils/zenutils/libraries/pelib-0.9/pelib/buffer/OutputBuffer.h old mode 100755 new mode 100644 index f1ab99039b..06049ddd9d --- a/utils/zenutils/libraries/pelib-0.9/pelib/buffer/OutputBuffer.h +++ b/utils/zenutils/libraries/pelib-0.9/pelib/buffer/OutputBuffer.h @@ -1,51 +1,51 @@ -/* -* OutputBuffer.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 OUTPUTBUFFER_H -#define OUTPUTBUFFER_H - -#include -#include - -namespace PeLib -{ - class OutputBuffer - { - private: - std::vector& m_vBuffer; - - public: - OutputBuffer(std::vector& vBuffer); - const unsigned char* data() const; - unsigned long size(); - - template - OutputBuffer& operator<<(const T& value) - { - const unsigned char* p = reinterpret_cast(&value); - std::copy(p, p + sizeof(value), std::back_inserter(m_vBuffer)); - return *this; - } - void add(const char* lpBuffer, unsigned long ulSize); - void reset(); - void resize(unsigned int uiSize); - void set(unsigned int uiPosition); - - template - void update(unsigned long ulIndex, const T& value) - { - *(T*)(&m_vBuffer[ulIndex]) = value; - } - }; -} - -#endif +/* +* OutputBuffer.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 OUTPUTBUFFER_H +#define OUTPUTBUFFER_H + +#include +#include + +namespace PeLib +{ + class OutputBuffer + { + private: + std::vector& m_vBuffer; + + public: + OutputBuffer(std::vector& vBuffer); + const unsigned char* data() const; + unsigned long size(); + + template + OutputBuffer& operator<<(const T& value) + { + const unsigned char* p = reinterpret_cast(&value); + std::copy(p, p + sizeof(value), std::back_inserter(m_vBuffer)); + return *this; + } + void add(const char* lpBuffer, unsigned long ulSize); + void reset(); + void resize(unsigned int uiSize); + void set(unsigned int uiPosition); + + template + void update(unsigned long ulIndex, const T& value) + { + *(T*)(&m_vBuffer[ulIndex]) = value; + } + }; +} + +#endif diff --git a/utils/zenutils/libraries/pelib-0.9/pelib/changelog.txt b/utils/zenutils/libraries/pelib-0.9/pelib/changelog.txt old mode 100755 new mode 100644 index 37a7c36229..8099edf3db --- a/utils/zenutils/libraries/pelib-0.9/pelib/changelog.txt +++ b/utils/zenutils/libraries/pelib-0.9/pelib/changelog.txt @@ -1,321 +1,321 @@ -PeLib 0.09 alpha (February 09, 2005) - -Added: -- Added PeHeaderT::setIddBaseRelocRva(dword value) -- Added PeHeaderT::setIddBaseRelocSize(dword value) -- Added PeHeaderT::setIddArchitectureRva(dword value) -- Added PeHeaderT::setIddArchitectureSize(dword value) -- Added PeHeaderT::setIddComHeaderRva(dword value) -- Added PeHeaderT::setIddComHeaderSize(dword value) -- Added void PeHeaderT::setImageDataDirectoryRva(dword dwDirectory, dword value) -- Added void PeHeaderT::setImageDataDirectorySize(dword dwDirectory, dword value) -- Added bool PeHeaderT::isValid() const -- Added int PeHeaderT::read(const unsigned char* pcBuffer, unsigned int uiSize, unsigned int uiOffset); -- Added int BoundImportDirectory::read(unsigned char* pcBuffer, unsigned int uiSize); -- Added int BoundImportDirectory::read(InputBuffer& inpBuffer, unsigned char* data, unsigned int dwSize); -- Added unsigned int BoundImportDirectory::totalModules() -- Added DebugDirectory::setData to set the debug data of individual entries in the DebugDirectory. -- Added DebugDirectory::getData to get the debug data of individual entries in the DebugDirectory. -- Added int IatDirectory::read(unsigned char* buffer, unsigned int buffersize) -- Optimized the size and rebuild methods to avoid duplicate strings for files that appear - more than once in the BoundImport Directory. -- Added two read functions to TlsDirectory and updated the return values of the old read function. -- Added two read functions to ComHeaderDirectory and updated the return values of the old read function. -- Added two read functions to RelocationsDirectory and updated the return values of the old read function. -- Added void RelocationsDirectory::addRelocation() -- Added void RelocationsDirectory::removeRelocation(unsigned int index) -- Added void DebugDirectory::clear() -- Added void ExportDirectory::removeFunction(unsigned int index) -- Added void ExportDirectory::clear() -- Added unsigned int ExportDirectory::calcNumberOfFunctions() -- Added void ExportDirectory::setAddressOfNameOrdinals(dword value) - -Bugfixes: -- Fixed a bug in TlsDirectory::size() -- Fixed a bug in PeHeaderT<64>::isValid(dword) -- Fixed a bug in PeHeaderT::removeDataDirectory(dword) -- Fixed a bug in BoundImportDirectory::rebuild() -- Fixed a bug in BoundImportDirectory::read() (Potential buffer overflow) -- Fixed a bug in BoundImportDirectory::read() (Bug caused incorrect results when reading the directory more than once) -- Fixed a bug when reading the debug data of individual DebugDirectory entries. -- Fixed a bug that caused non-existing data directories to be read (PeFile). -- Fixed a bug that caused problems when reading import directories that were located close to the end of the file (ImportDirectory). -- Fixed a minor bug in PeHeader::calcSizeOfImage -- Fixed some return values and removed all exception handling blocks. - -Changes: -- Changed return values of PeHeader::addSection -- Changed return values of PeHeader::read -- Changed behaviour: PeHeader::m_uiOffset is initialized to 0 in default constructors. -- Changed return value of BoundImportDirectory::getNumberOfModules() from int to unsigned int -- Changed name of BoundImportDirectory::getNumberOfModules to calcNumberOfModules -- Changed the return values of the PeFile::read* functions. -- Renamed IatDirectory::removeAll to IatDirectory::clear -- Renamed IatDirectory::numberOfAddresses to IatDirectory::calcNumberOfAddresses -- Changed the parameter types of IatDirectory::getAddress from dword to unsigned int -- Changed the parameter types of IatDirectory::removeAddress from dword to unsigned int -- All constants taken from windows.h that were redefined in PeLibAux.h now have the prefix PELIB_ to avoid - conflicts with windows.h if both files are included to a project. -- Changed PELIB_IMAGE_TLS_DIRECTORY::size from an enum to a function. -- Changed the name of RelocationsDirectory::getNumberOfRelocationData to calcNumberOfRelocationData -- Changed void RelocationsDirectory::removeRelocationData(unsigned int ulRelocation, word wValue) - to void RelocationsDirectory::removeRelocationData(unsigned int relocindex, unsigned int dataindex) -- Removed dword ExportDirectory::getNumberOfNameOrdinals() -- Removed dword ExportDirectory::getNumberOfAddressOfFunctionNames() -- Removed dword ExportDirectory::getNumberOfAddressOfFunctions() -- Changed the parameters of some functions in ExportDirectory from dword to unsigned int. - - -January 16, 2005 PeLib 0.08 alpha - -Added: -- Added std::string ExportDirectory::getNameString() -- Added resource type RT_MANIFEST to PeLibAux.h -- Added the following functions of PeHeaderT: setIddDebugRva, setIddDebugSize, - setIddDelayImportRva, setIddDelayImportSize, setIddExceptionRva, setIddExceptionSize, setIddGlobalPtrRva, - setIddGlobalPtrSize, setIddIatRva, setIddIatSize, setIddLoadConfigRva, setIddLoadConfigSize, - setIddResourceRva, setIddResourceSize, setIddResourceRva, setIddResourceSize, setIddSecurityRva, - setIddSecuritySize, setIddTlsRva, setIddTlsSize -- ImportDirectory32 and ImportDirectory64 are now available. -- Added ImportDirectory::setFileName(dword, currdir, const std::string&) -- Added ImportDirectory::setFirstThunk(dword, currdir, dword) -- Added ImportDirectory::setForwarderChain(dword, currdir, dword) -- Added ImportDirectory::setRvaOfName(dword, currdir, dword) -- Added ImportDirectory::setOriginalFirstThunk(dword, currdir, dword) -- Added ImportDirectory::setTimeDateStamp(dword, currdir, dword) -- Added ImportDirectory::setOriginalFirstThunk(dword, dword, currdir, dword) -- Added ImportDirectory::setFirstThunk(dword, dword, currdir, dword) -- Added ImportDirectory::setFunctionHint(dword, dword, currdir, word) -- Added ImportDirectory::setFunctionName(dword, dword, currdir, const std::string&) -- Added dword BoundImportDirectory::getTimeDateStamp(dword dwBidnr, dword forwardedModule) const -- Added word BoundImportDirectory::getOffsetModuleName(dword dwBidnr, dword forwardedModule) const -- Added word BoundImportDirectory::getNumberOfModuleForwarderRefs(dword dwBidnr, dword forwardedModule) const -- Added std::string BoundImportDirectory::getModuleName(dword dwBidnr, dword forwardedModule) const -- Added void BoundImportDirectory::setTimeDateStamp(dword dwBidnr, dword forwardedModule, dword dwTds) -- Added void BoundImportDirectory::setOffsetModuleName(dword dwBidnr, dword forwardedModule, word wOmn) -- Added void BoundImportDirectory::setNumberOfModuleForwarderRefs(dword dwBidnr, dword forwardedModule, word wMfr) -- Added void BoundImportDirectory::setModuleName(dword dwBidnr, dword forwardedModule, const std::string& strModuleName) -- Added word calcNumberOfModuleForwarderRefs(dword dwBidnr) const -- Added void addForwardedModule(dword dwBidnr, const std::string& name, dword timeStamp = 0, word offsetModuleName = 0, word forwardedModules = 0) -- Added void removeForwardedModule(dword dwBidnr, word forwardedModule) -- Added PeHeaderT::addDataDirectory() -- Added PeHeaderT::removeDataDirectory(dword) - -Bugfixes: -- Fixed a bug in MzHeader::isValid -- Fixed a bug in PeHeaderT::size() -- Fixed a bug in PeHeaderT::calcRva() -- Fixed a bug in PeHeaderT::calcSizeOfImage() -- Fixed a bug in PeHeaderT::getSectionName(dword) -- Fixed a bug in PeHeaderT::calcStartOfCode() -- Fixed a bug in PELIB_THUNK_DATA::bool equalHint(word wHint) const -- Fixed a bug in PELIB_IMAGE_THUNK_DATA::bool equalHint(word wHint) const -- Fixed a bug in int ImportDirectory::removeFunction(const std::string& strFilename, word wHint) -- Fixed a bug in int ImportDirectory::removeFile(const std::string& strFilename) -- Function hints are now properly added when rebuilding import directories. -- Reading and rebuilding bound import directories now works with forwarded modules. - -Changes: -- Changed behaviour: Removed all exceptions from the MzHeader class. The functions work with return values now. -- Changed behaviour: The MzHeader::read() functions stopped checking if the MzHeader begins with "MZ". -- Changed behaviour: PeHeaderT::addSection(std::string, dword) doesn't use exceptions anymore. - Return values now indicate if the function succeeded or failed. -- Changed behaviour: PeHeaderT::getSectionWithOffset(dword) doesn't use exceptions anymore. - Return values now indicate if the function succeeded or failed. -- Changed behaviour: PeHeaderT::getSectionWithRva(dword) doesn't use exceptions anymore. - Return values now indicate if the function succeeded or failed. -- Changed behaviour: PeHeaderT::rvaToOffset(dword) doesn't use exceptions anymore. - Return values now indicate if the function succeeded or failed. -- Changed behaviour: PeHeaderT::write(std::string, unsigned int) doesn't use exceptions anymore. - Return values now indicate if the function succeeded or failed. -- Changed behaviour: PeHeaderT::writeSectionData(const std::string& strFilename, word wSecnr, - const std::vector& vBuffer) doesn't use exceptions anymore. - Return values now indicate if the function succeeded or failed. -- Changed behaviour: PeHeaderT::writeSections(std::string) doesn't use exceptions anymore. - Return values now indicate if the function succeeded or failed. -- Changed behaviour: Return value of PeHeaderT::calcSpaceAfterHeader() changed from unsigned long - to unsigned int. -- Changed behaviour: Return value of PeHeaderT::calcStartOfCode() changed from unsigned long - to unsigned int. -- Changed behaviour: Return value of PeHeaderT::calcOffset() changed from unsigned long - to unsigned int. -- Changed behaviour: Return value of PeHeaderT::offsetToRva(dword) changed from unsigned long - to unsigned int. -- Changed behaviour: Return value of PeHeaderT::offsetToVa(dword) changed from unsigned long - to unsigned int. -- Renamed ExportDirectory::setName(std::string) to ExportDirectory::setNameString(std::string) -- Renamed the PeHeaderT::getId* functions to PeHeaderT::getIdd* -- Renamed PeHeaderT::getImageDirectoryRva to PeHeaderT::getImageDataDirectoryRva -- Renamed PeHeaderT::getImageDirectorySize to PeHeaderT::getImageDataDirectorySize -- Renamed void PeHeaderT::setWinVersionValue(dword dwValue) to void PeHeaderT::setWin32VersionValue(dword dwValue) -- Renamed the following functions of PeHeaderT: setIdImportRva to setIddImportRva, - setIdImportSize to setIddImportSize, setIdExportRva to setIddExportRva, setIdExportSize to setIddExportSize -- Renamed dword ImportDirectory::getName to dword ImportDirectory::getRvaOfName -- Changed behaviour: All removeFunction and removeFile functions from ImportDirectory.h return int instead - of void now. -- Changed behavior: ResourceDirectory::resourceTypeNameToIndex returns int instead of unsigned int. - -------------------------------------------------------------------------------------------------------------- - -July 18, 2004 PeLib 0.07 alpha - -Added: -- Full support of the PE+ format. -- ImportDirectory::getName(string, currdir) -- ImportDirectory::getFirstThunk(dword, currdir) -- ImportDirectory::getOriginalFirstThunk(dword, currdir) -- ImportDirectory::getForwarderChain(dword, currdir) -- ImportDirectory::getName(dword, currdir) -- ImportDirectory::getTimeDateStamp(dword, currdir) -- PeLib::getFileType(string) -- PeLib::openPeFile(string) -- Added class PeFileVisitor -- Added PeFile::visit(PeFileVisitor&) - -Bugfixes: -- Fixed a bug in PeHeader::rvaToOffset - -Changes: -- Renamed ImportDirectory::OLD to PeLib::OLDDIR and ImportDirectory::NEW to PeLib::NEWDIR -- Renamed Relocations to RelocationsDirectory -- Renamed ImportAddressTable to IatDirectory -- Renamed ComDescriptor to ComHeader -- Renamed PeFile::comDescDir to PeFile::comDir -- Changed unsigned long ExportDirectory::getFunctionIndex to unsigned int ExportDirectory::getFunctionIndex - -------------------------------------------------------------------------------------------------------------- - -July 4, 2004 PeLib 0.06 alpha - -Added: -- TlsDirectory class - -Changes: -- ResourceElement::read and ResourceElement::rebuild are now protected. - -Bugfixes: -- Fixed a bug in PeHeader::rvaToOffset - -------------------------------------------------------------------------------------------------------------- - -June 26, 2004 PeLib 0.05 alpha - -Added: -- Constructor, Copy constructor, assignment operator and destructor for ResourceChild. -- ResourceDirectory::getRoot() -- ResourceElement::isLeaf() -- ResourceElement::getElementRva -- 10 new functions in ResourceLeaf. -- 22 new functions in ResourceNode. -- Added the RT_* constants which identify resource types to PeLibAux.h -- Added a new example (examples/ResourceTree) which shows how to use low level ResourceDirectory functions. -- Added PELIB_IMAGE_DEBUG_DIRECTORY and PELIB_IMG_DEBUG_DIRECTORY -- Added the new class DebugDirectory which handles the debug directory of PE files. -- Added readDebugDirectory() and debugDir() to PeFile. - -Removed: -- ~ResourceNode() - -------------------------------------------------------------------------------------------------------------- - -June 12, 2004 PeLib 0.04 alpha - -New: -- Finally implemented the class ResourceDirectory. That means lots of new functions I won't explicitly list here. - Check the documentation. - -Removed: -- The files buffer/ResTree.cpp and buffer/ResTree.h are obsolete and were removed. - -Bugfixes: -- Fixed PeHeader::calcStartOfCode -- Fixed PeHeader::getSectionWithRva - -Changes: -- Changed PeHeader::read to throw an exception if the NT signature is not 'PE'\0\0 -- Changed the 2nd parameter of void MzHeader::read(unsigned char*, unsigned long) from unsigned long to unsigned int. -- Changed the return value of MzHeader::size from long to int. -- Changed parameters of MzHeader::getReservedWords1, MzHeader::getReservedWords2, MzHeader::setReservedWords1 and - MzHeader::setReservedWords2 from long to int. -- Changed MzHeader::read(std::string) to MzHeader::read(const std::string&) -- Changed return value of BoundImportDirectory::getModuleIndex from long to int. -- Changed return value of BoundImportDirectory::size from long to int. -- Changed return value of ComDescriptor::size from long to int. -- Changed return value of ImportAddressTable::size from long to int. -- Changed return value of Relocations::getNumberOfRelocations from long to int. -- Changed return value of Relocations::getNumberOfRelocationData from long to int. -- Changed return value of Relocations::size from long to int. -- Changed parameter of Relocations::getVirtualAddress from long to int. -- Changed parameter of Relocations::getSizeOfBlock from long to int. -- Changed parameter of Relocations::getRelocationData from long to int. -- Changed parameters of Relocations::setRelocationData from long to int. -- Changed parameters of Relocations::setVirtualAddress from long to int. -- Changed parameters of Relocations::setSizeOfBlock from long to int. -- Changed parameters of Relocations::addRelocationData from long to int. -- Changed parameters of Relocations::removeRelocationData from long to int. -- Changed return value of ExportDirectory::getFunctionIndex(const std::string&) const from unsigned int to int. - -------------------------------------------------------------------------------------------------------------- - -May 31, 2004: PeLib 0.03 alpha - -Bugfixes: -- Fixed some bugs in FileDump.cpp - -Changes: -- Modified PeLibAux.cpp to make PeLib VC++ 7.1 compatible. -- Changed vector access from .at to operator[] all over the project. - Real undefined behaviour is probably better than spontaniously terminating applications. - -New: -- Added makefiles for Borland C++ commandline tools (tested on version 5.6.4) -- Added makefiles for Digital Mars Compiler (tested on version 8.38n) - Note that support for DMC is not yet complete. PeLib compiles though, the examples don't yet but the - reason for this is that I am unable to correctly specifiy the PeLib object files when compiling. -- Added makefiles for Visual C++ 7.1 (tested on compiler Version 13.10.3052) - -------------------------------------------------------------------------------------------------------------- - -Mai 1, 2004: PeLib 0.02 alpha - -Bugfixes: -- Fixed a bug in FileDump's and OON2's makefile.g++ -- Fixed ImportDirectory::size -- Changed parameter of PELIB_THUNK_DATA::equalHint from dword to word -- Fixed a bug in PeHeader::read (PeLib always assumed 0x10 data directories in version 0.01 alpha) - -Changes: -- Slightly changed ImportDirectory::removeFile (Changed function's signature) -- Moved the definitions of byte, word and dword into the PeLib namespace. -- Renamed PELIB_THUNK_DATA::equalName to PELIB_THUNK_DATA::equalFunctionName -- Started to add size() functions to structs defined in PeLibAux.h -- Moved PeFile::writeSectionData to PeHeader::writeSectionData -- Moved PeFile::writeSections to PeHeader::writeSections - -New: -- Added ImportDirectory::hasFunction -- Wrote BoundImportDirectory::size -- Added accumulate function to PeLibAux.h -- Added PELIB_IMAGE_SECTION_HEADER::biggerFileOffset -- Added PELIB_IMAGE_SECTION_HEADER::biggerVirtualAddress -- Added PeHeader::calcSizeOfImage -- Added PeHeader::enlargeLastSection - -Removed: -- Removed PeFile::write -- Removed PeFile::writeImportDirectory - -Other changes: -- Rewrote parts of ImportDirectory::read -- Rewrote ImportDirectory::removeFunction (both version). -- Changed std::ios:: to std::ios_base:: in ImportDirectory -- Changed ImportDirectory::addFunction (both versions) -- Changed ExportDirectory::rebuild -- Changed ExportDirectory::size -- Rewrote ImportDirectory::size -- Rewrote PeHeader::size -- Rewrote ComDescriptor::size - -------------------------------------------------------------------------------------------------------------- - +PeLib 0.09 alpha (February 09, 2005) + +Added: +- Added PeHeaderT::setIddBaseRelocRva(dword value) +- Added PeHeaderT::setIddBaseRelocSize(dword value) +- Added PeHeaderT::setIddArchitectureRva(dword value) +- Added PeHeaderT::setIddArchitectureSize(dword value) +- Added PeHeaderT::setIddComHeaderRva(dword value) +- Added PeHeaderT::setIddComHeaderSize(dword value) +- Added void PeHeaderT::setImageDataDirectoryRva(dword dwDirectory, dword value) +- Added void PeHeaderT::setImageDataDirectorySize(dword dwDirectory, dword value) +- Added bool PeHeaderT::isValid() const +- Added int PeHeaderT::read(const unsigned char* pcBuffer, unsigned int uiSize, unsigned int uiOffset); +- Added int BoundImportDirectory::read(unsigned char* pcBuffer, unsigned int uiSize); +- Added int BoundImportDirectory::read(InputBuffer& inpBuffer, unsigned char* data, unsigned int dwSize); +- Added unsigned int BoundImportDirectory::totalModules() +- Added DebugDirectory::setData to set the debug data of individual entries in the DebugDirectory. +- Added DebugDirectory::getData to get the debug data of individual entries in the DebugDirectory. +- Added int IatDirectory::read(unsigned char* buffer, unsigned int buffersize) +- Optimized the size and rebuild methods to avoid duplicate strings for files that appear + more than once in the BoundImport Directory. +- Added two read functions to TlsDirectory and updated the return values of the old read function. +- Added two read functions to ComHeaderDirectory and updated the return values of the old read function. +- Added two read functions to RelocationsDirectory and updated the return values of the old read function. +- Added void RelocationsDirectory::addRelocation() +- Added void RelocationsDirectory::removeRelocation(unsigned int index) +- Added void DebugDirectory::clear() +- Added void ExportDirectory::removeFunction(unsigned int index) +- Added void ExportDirectory::clear() +- Added unsigned int ExportDirectory::calcNumberOfFunctions() +- Added void ExportDirectory::setAddressOfNameOrdinals(dword value) + +Bugfixes: +- Fixed a bug in TlsDirectory::size() +- Fixed a bug in PeHeaderT<64>::isValid(dword) +- Fixed a bug in PeHeaderT::removeDataDirectory(dword) +- Fixed a bug in BoundImportDirectory::rebuild() +- Fixed a bug in BoundImportDirectory::read() (Potential buffer overflow) +- Fixed a bug in BoundImportDirectory::read() (Bug caused incorrect results when reading the directory more than once) +- Fixed a bug when reading the debug data of individual DebugDirectory entries. +- Fixed a bug that caused non-existing data directories to be read (PeFile). +- Fixed a bug that caused problems when reading import directories that were located close to the end of the file (ImportDirectory). +- Fixed a minor bug in PeHeader::calcSizeOfImage +- Fixed some return values and removed all exception handling blocks. + +Changes: +- Changed return values of PeHeader::addSection +- Changed return values of PeHeader::read +- Changed behaviour: PeHeader::m_uiOffset is initialized to 0 in default constructors. +- Changed return value of BoundImportDirectory::getNumberOfModules() from int to unsigned int +- Changed name of BoundImportDirectory::getNumberOfModules to calcNumberOfModules +- Changed the return values of the PeFile::read* functions. +- Renamed IatDirectory::removeAll to IatDirectory::clear +- Renamed IatDirectory::numberOfAddresses to IatDirectory::calcNumberOfAddresses +- Changed the parameter types of IatDirectory::getAddress from dword to unsigned int +- Changed the parameter types of IatDirectory::removeAddress from dword to unsigned int +- All constants taken from windows.h that were redefined in PeLibAux.h now have the prefix PELIB_ to avoid + conflicts with windows.h if both files are included to a project. +- Changed PELIB_IMAGE_TLS_DIRECTORY::size from an enum to a function. +- Changed the name of RelocationsDirectory::getNumberOfRelocationData to calcNumberOfRelocationData +- Changed void RelocationsDirectory::removeRelocationData(unsigned int ulRelocation, word wValue) + to void RelocationsDirectory::removeRelocationData(unsigned int relocindex, unsigned int dataindex) +- Removed dword ExportDirectory::getNumberOfNameOrdinals() +- Removed dword ExportDirectory::getNumberOfAddressOfFunctionNames() +- Removed dword ExportDirectory::getNumberOfAddressOfFunctions() +- Changed the parameters of some functions in ExportDirectory from dword to unsigned int. + + +January 16, 2005 PeLib 0.08 alpha + +Added: +- Added std::string ExportDirectory::getNameString() +- Added resource type RT_MANIFEST to PeLibAux.h +- Added the following functions of PeHeaderT: setIddDebugRva, setIddDebugSize, + setIddDelayImportRva, setIddDelayImportSize, setIddExceptionRva, setIddExceptionSize, setIddGlobalPtrRva, + setIddGlobalPtrSize, setIddIatRva, setIddIatSize, setIddLoadConfigRva, setIddLoadConfigSize, + setIddResourceRva, setIddResourceSize, setIddResourceRva, setIddResourceSize, setIddSecurityRva, + setIddSecuritySize, setIddTlsRva, setIddTlsSize +- ImportDirectory32 and ImportDirectory64 are now available. +- Added ImportDirectory::setFileName(dword, currdir, const std::string&) +- Added ImportDirectory::setFirstThunk(dword, currdir, dword) +- Added ImportDirectory::setForwarderChain(dword, currdir, dword) +- Added ImportDirectory::setRvaOfName(dword, currdir, dword) +- Added ImportDirectory::setOriginalFirstThunk(dword, currdir, dword) +- Added ImportDirectory::setTimeDateStamp(dword, currdir, dword) +- Added ImportDirectory::setOriginalFirstThunk(dword, dword, currdir, dword) +- Added ImportDirectory::setFirstThunk(dword, dword, currdir, dword) +- Added ImportDirectory::setFunctionHint(dword, dword, currdir, word) +- Added ImportDirectory::setFunctionName(dword, dword, currdir, const std::string&) +- Added dword BoundImportDirectory::getTimeDateStamp(dword dwBidnr, dword forwardedModule) const +- Added word BoundImportDirectory::getOffsetModuleName(dword dwBidnr, dword forwardedModule) const +- Added word BoundImportDirectory::getNumberOfModuleForwarderRefs(dword dwBidnr, dword forwardedModule) const +- Added std::string BoundImportDirectory::getModuleName(dword dwBidnr, dword forwardedModule) const +- Added void BoundImportDirectory::setTimeDateStamp(dword dwBidnr, dword forwardedModule, dword dwTds) +- Added void BoundImportDirectory::setOffsetModuleName(dword dwBidnr, dword forwardedModule, word wOmn) +- Added void BoundImportDirectory::setNumberOfModuleForwarderRefs(dword dwBidnr, dword forwardedModule, word wMfr) +- Added void BoundImportDirectory::setModuleName(dword dwBidnr, dword forwardedModule, const std::string& strModuleName) +- Added word calcNumberOfModuleForwarderRefs(dword dwBidnr) const +- Added void addForwardedModule(dword dwBidnr, const std::string& name, dword timeStamp = 0, word offsetModuleName = 0, word forwardedModules = 0) +- Added void removeForwardedModule(dword dwBidnr, word forwardedModule) +- Added PeHeaderT::addDataDirectory() +- Added PeHeaderT::removeDataDirectory(dword) + +Bugfixes: +- Fixed a bug in MzHeader::isValid +- Fixed a bug in PeHeaderT::size() +- Fixed a bug in PeHeaderT::calcRva() +- Fixed a bug in PeHeaderT::calcSizeOfImage() +- Fixed a bug in PeHeaderT::getSectionName(dword) +- Fixed a bug in PeHeaderT::calcStartOfCode() +- Fixed a bug in PELIB_THUNK_DATA::bool equalHint(word wHint) const +- Fixed a bug in PELIB_IMAGE_THUNK_DATA::bool equalHint(word wHint) const +- Fixed a bug in int ImportDirectory::removeFunction(const std::string& strFilename, word wHint) +- Fixed a bug in int ImportDirectory::removeFile(const std::string& strFilename) +- Function hints are now properly added when rebuilding import directories. +- Reading and rebuilding bound import directories now works with forwarded modules. + +Changes: +- Changed behaviour: Removed all exceptions from the MzHeader class. The functions work with return values now. +- Changed behaviour: The MzHeader::read() functions stopped checking if the MzHeader begins with "MZ". +- Changed behaviour: PeHeaderT::addSection(std::string, dword) doesn't use exceptions anymore. + Return values now indicate if the function succeeded or failed. +- Changed behaviour: PeHeaderT::getSectionWithOffset(dword) doesn't use exceptions anymore. + Return values now indicate if the function succeeded or failed. +- Changed behaviour: PeHeaderT::getSectionWithRva(dword) doesn't use exceptions anymore. + Return values now indicate if the function succeeded or failed. +- Changed behaviour: PeHeaderT::rvaToOffset(dword) doesn't use exceptions anymore. + Return values now indicate if the function succeeded or failed. +- Changed behaviour: PeHeaderT::write(std::string, unsigned int) doesn't use exceptions anymore. + Return values now indicate if the function succeeded or failed. +- Changed behaviour: PeHeaderT::writeSectionData(const std::string& strFilename, word wSecnr, + const std::vector& vBuffer) doesn't use exceptions anymore. + Return values now indicate if the function succeeded or failed. +- Changed behaviour: PeHeaderT::writeSections(std::string) doesn't use exceptions anymore. + Return values now indicate if the function succeeded or failed. +- Changed behaviour: Return value of PeHeaderT::calcSpaceAfterHeader() changed from unsigned long + to unsigned int. +- Changed behaviour: Return value of PeHeaderT::calcStartOfCode() changed from unsigned long + to unsigned int. +- Changed behaviour: Return value of PeHeaderT::calcOffset() changed from unsigned long + to unsigned int. +- Changed behaviour: Return value of PeHeaderT::offsetToRva(dword) changed from unsigned long + to unsigned int. +- Changed behaviour: Return value of PeHeaderT::offsetToVa(dword) changed from unsigned long + to unsigned int. +- Renamed ExportDirectory::setName(std::string) to ExportDirectory::setNameString(std::string) +- Renamed the PeHeaderT::getId* functions to PeHeaderT::getIdd* +- Renamed PeHeaderT::getImageDirectoryRva to PeHeaderT::getImageDataDirectoryRva +- Renamed PeHeaderT::getImageDirectorySize to PeHeaderT::getImageDataDirectorySize +- Renamed void PeHeaderT::setWinVersionValue(dword dwValue) to void PeHeaderT::setWin32VersionValue(dword dwValue) +- Renamed the following functions of PeHeaderT: setIdImportRva to setIddImportRva, + setIdImportSize to setIddImportSize, setIdExportRva to setIddExportRva, setIdExportSize to setIddExportSize +- Renamed dword ImportDirectory::getName to dword ImportDirectory::getRvaOfName +- Changed behaviour: All removeFunction and removeFile functions from ImportDirectory.h return int instead + of void now. +- Changed behavior: ResourceDirectory::resourceTypeNameToIndex returns int instead of unsigned int. + +------------------------------------------------------------------------------------------------------------- + +July 18, 2004 PeLib 0.07 alpha + +Added: +- Full support of the PE+ format. +- ImportDirectory::getName(string, currdir) +- ImportDirectory::getFirstThunk(dword, currdir) +- ImportDirectory::getOriginalFirstThunk(dword, currdir) +- ImportDirectory::getForwarderChain(dword, currdir) +- ImportDirectory::getName(dword, currdir) +- ImportDirectory::getTimeDateStamp(dword, currdir) +- PeLib::getFileType(string) +- PeLib::openPeFile(string) +- Added class PeFileVisitor +- Added PeFile::visit(PeFileVisitor&) + +Bugfixes: +- Fixed a bug in PeHeader::rvaToOffset + +Changes: +- Renamed ImportDirectory::OLD to PeLib::OLDDIR and ImportDirectory::NEW to PeLib::NEWDIR +- Renamed Relocations to RelocationsDirectory +- Renamed ImportAddressTable to IatDirectory +- Renamed ComDescriptor to ComHeader +- Renamed PeFile::comDescDir to PeFile::comDir +- Changed unsigned long ExportDirectory::getFunctionIndex to unsigned int ExportDirectory::getFunctionIndex + +------------------------------------------------------------------------------------------------------------- + +July 4, 2004 PeLib 0.06 alpha + +Added: +- TlsDirectory class + +Changes: +- ResourceElement::read and ResourceElement::rebuild are now protected. + +Bugfixes: +- Fixed a bug in PeHeader::rvaToOffset + +------------------------------------------------------------------------------------------------------------- + +June 26, 2004 PeLib 0.05 alpha + +Added: +- Constructor, Copy constructor, assignment operator and destructor for ResourceChild. +- ResourceDirectory::getRoot() +- ResourceElement::isLeaf() +- ResourceElement::getElementRva +- 10 new functions in ResourceLeaf. +- 22 new functions in ResourceNode. +- Added the RT_* constants which identify resource types to PeLibAux.h +- Added a new example (examples/ResourceTree) which shows how to use low level ResourceDirectory functions. +- Added PELIB_IMAGE_DEBUG_DIRECTORY and PELIB_IMG_DEBUG_DIRECTORY +- Added the new class DebugDirectory which handles the debug directory of PE files. +- Added readDebugDirectory() and debugDir() to PeFile. + +Removed: +- ~ResourceNode() + +------------------------------------------------------------------------------------------------------------- + +June 12, 2004 PeLib 0.04 alpha + +New: +- Finally implemented the class ResourceDirectory. That means lots of new functions I won't explicitly list here. + Check the documentation. + +Removed: +- The files buffer/ResTree.cpp and buffer/ResTree.h are obsolete and were removed. + +Bugfixes: +- Fixed PeHeader::calcStartOfCode +- Fixed PeHeader::getSectionWithRva + +Changes: +- Changed PeHeader::read to throw an exception if the NT signature is not 'PE'\0\0 +- Changed the 2nd parameter of void MzHeader::read(unsigned char*, unsigned long) from unsigned long to unsigned int. +- Changed the return value of MzHeader::size from long to int. +- Changed parameters of MzHeader::getReservedWords1, MzHeader::getReservedWords2, MzHeader::setReservedWords1 and + MzHeader::setReservedWords2 from long to int. +- Changed MzHeader::read(std::string) to MzHeader::read(const std::string&) +- Changed return value of BoundImportDirectory::getModuleIndex from long to int. +- Changed return value of BoundImportDirectory::size from long to int. +- Changed return value of ComDescriptor::size from long to int. +- Changed return value of ImportAddressTable::size from long to int. +- Changed return value of Relocations::getNumberOfRelocations from long to int. +- Changed return value of Relocations::getNumberOfRelocationData from long to int. +- Changed return value of Relocations::size from long to int. +- Changed parameter of Relocations::getVirtualAddress from long to int. +- Changed parameter of Relocations::getSizeOfBlock from long to int. +- Changed parameter of Relocations::getRelocationData from long to int. +- Changed parameters of Relocations::setRelocationData from long to int. +- Changed parameters of Relocations::setVirtualAddress from long to int. +- Changed parameters of Relocations::setSizeOfBlock from long to int. +- Changed parameters of Relocations::addRelocationData from long to int. +- Changed parameters of Relocations::removeRelocationData from long to int. +- Changed return value of ExportDirectory::getFunctionIndex(const std::string&) const from unsigned int to int. + +------------------------------------------------------------------------------------------------------------- + +May 31, 2004: PeLib 0.03 alpha + +Bugfixes: +- Fixed some bugs in FileDump.cpp + +Changes: +- Modified PeLibAux.cpp to make PeLib VC++ 7.1 compatible. +- Changed vector access from .at to operator[] all over the project. + Real undefined behaviour is probably better than spontaniously terminating applications. + +New: +- Added makefiles for Borland C++ commandline tools (tested on version 5.6.4) +- Added makefiles for Digital Mars Compiler (tested on version 8.38n) + Note that support for DMC is not yet complete. PeLib compiles though, the examples don't yet but the + reason for this is that I am unable to correctly specifiy the PeLib object files when compiling. +- Added makefiles for Visual C++ 7.1 (tested on compiler Version 13.10.3052) + +------------------------------------------------------------------------------------------------------------- + +Mai 1, 2004: PeLib 0.02 alpha + +Bugfixes: +- Fixed a bug in FileDump's and OON2's makefile.g++ +- Fixed ImportDirectory::size +- Changed parameter of PELIB_THUNK_DATA::equalHint from dword to word +- Fixed a bug in PeHeader::read (PeLib always assumed 0x10 data directories in version 0.01 alpha) + +Changes: +- Slightly changed ImportDirectory::removeFile (Changed function's signature) +- Moved the definitions of byte, word and dword into the PeLib namespace. +- Renamed PELIB_THUNK_DATA::equalName to PELIB_THUNK_DATA::equalFunctionName +- Started to add size() functions to structs defined in PeLibAux.h +- Moved PeFile::writeSectionData to PeHeader::writeSectionData +- Moved PeFile::writeSections to PeHeader::writeSections + +New: +- Added ImportDirectory::hasFunction +- Wrote BoundImportDirectory::size +- Added accumulate function to PeLibAux.h +- Added PELIB_IMAGE_SECTION_HEADER::biggerFileOffset +- Added PELIB_IMAGE_SECTION_HEADER::biggerVirtualAddress +- Added PeHeader::calcSizeOfImage +- Added PeHeader::enlargeLastSection + +Removed: +- Removed PeFile::write +- Removed PeFile::writeImportDirectory + +Other changes: +- Rewrote parts of ImportDirectory::read +- Rewrote ImportDirectory::removeFunction (both version). +- Changed std::ios:: to std::ios_base:: in ImportDirectory +- Changed ImportDirectory::addFunction (both versions) +- Changed ExportDirectory::rebuild +- Changed ExportDirectory::size +- Rewrote ImportDirectory::size +- Rewrote PeHeader::size +- Rewrote ComDescriptor::size + +------------------------------------------------------------------------------------------------------------- + April 9, 2004: PeLib 0.01 alpha Initial release \ No newline at end of file diff --git a/utils/zenutils/libraries/pelib-0.9/pelib/license.htm b/utils/zenutils/libraries/pelib-0.9/pelib/license.htm old mode 100755 new mode 100644 index 2978aa7920..74f3b5a83c --- a/utils/zenutils/libraries/pelib-0.9/pelib/license.htm +++ b/utils/zenutils/libraries/pelib-0.9/pelib/license.htm @@ -1,35 +1,35 @@ - - -

The zlib/libpng License

- - - -

Copyright (c) 2004 - Sebastian Porst

- -

This software is provided 'as-is', without any express or implied - warranty. In no event will the authors be held liable for any damages - arising from the use of this software.

- -

Permission is granted to anyone to use this software for any purpose, - including commercial applications, and to alter it and redistribute it - freely, subject to the following restrictions:

- -
- -

1. The origin of this software must not be misrepresented; you must not - claim that you wrote the original software. If you use this software - in a product, an acknowledgment in the product documentation would be - appreciated but is not required.

- -

2. Altered source versions must be plainly marked as such, and must not be - misrepresented as being the original software.

- -

3. This notice may not be removed or altered from any source - distribution.

- -
- -
- - + + +

The zlib/libpng License

+ + + +

Copyright (c) 2004 - Sebastian Porst

+ +

This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software.

+ +

Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions:

+ +
+ +

1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required.

+ +

2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software.

+ +

3. This notice may not be removed or altered from any source + distribution.

+ +
+ +
+ + \ No newline at end of file diff --git a/utils/zenutils/libraries/pelib-0.9/pelib/readme.txt b/utils/zenutils/libraries/pelib-0.9/pelib/readme.txt old mode 100755 new mode 100644 index b26a3e5287..65f289a8fe --- a/utils/zenutils/libraries/pelib-0.9/pelib/readme.txt +++ b/utils/zenutils/libraries/pelib-0.9/pelib/readme.txt @@ -1,44 +1,44 @@ -PeLib - Version 0.09 (alpha release) -========================================= - -Copyright 2004 by Sebastian Porst -WWW: http://www.pelib.com -E-Mail: webmaster@the-interweb.com - -========================================= - -1. What is PeLib? -2. Where can I find a documentation of PeLib DLL? -3. Which license is used for PeLib? -4. Which compilers are being supported? -5. How do I compile PeLib? - -1. What is PeLib DLL? - PeLib is an open-source C++ library to modify - PE files. See http://www.pelib.com for more details. - -2. Where can I find a documentation of PeLib DLL? - http://www.pelib.com - -3. All parts of PeLib are distributed under the zlib/libpng license. - See license.htm for details. - -4. The following compilers have been tested: - MingW with g++ 3.2.3 - Visual C++ 7.1 / Compiler version 13.10.3052 - Borland C++ 5.6.4 (currently not supported) - Digital Mars Compiler 8.38n (currently not supported) - -5. Go into the PeLib/source directory and enter the following lines - depending on which compiler you use. - - g++: make -f makefile.g++ - Borland C++: make -f makefile.bcc (currently not supported) - Visual C++ 7.1: nmake makefile.vc7 - Digital Mars: make makefile.dmc (currently not supported) - - If the compilation is succesful there should be some *.o/*.obj files - and (if you used g++) a PeLib.a file in the lib directory. - Then go to the examples directory and pick one example (I - suggest FileDump) and try to build it with the same make +PeLib - Version 0.09 (alpha release) +========================================= + +Copyright 2004 by Sebastian Porst +WWW: http://www.pelib.com +E-Mail: webmaster@the-interweb.com + +========================================= + +1. What is PeLib? +2. Where can I find a documentation of PeLib DLL? +3. Which license is used for PeLib? +4. Which compilers are being supported? +5. How do I compile PeLib? + +1. What is PeLib DLL? + PeLib is an open-source C++ library to modify + PE files. See http://www.pelib.com for more details. + +2. Where can I find a documentation of PeLib DLL? + http://www.pelib.com + +3. All parts of PeLib are distributed under the zlib/libpng license. + See license.htm for details. + +4. The following compilers have been tested: + MingW with g++ 3.2.3 + Visual C++ 7.1 / Compiler version 13.10.3052 + Borland C++ 5.6.4 (currently not supported) + Digital Mars Compiler 8.38n (currently not supported) + +5. Go into the PeLib/source directory and enter the following lines + depending on which compiler you use. + + g++: make -f makefile.g++ + Borland C++: make -f makefile.bcc (currently not supported) + Visual C++ 7.1: nmake makefile.vc7 + Digital Mars: make makefile.dmc (currently not supported) + + If the compilation is succesful there should be some *.o/*.obj files + and (if you used g++) a PeLib.a file in the lib directory. + Then go to the examples directory and pick one example (I + suggest FileDump) and try to build it with the same make command as above. \ No newline at end of file -- cgit v1.2.3