From 14c7f45cdae826f88dc539c8c38dd95caf305731 Mon Sep 17 00:00:00 2001 From: Maurus Cuelenaere Date: Fri, 11 Jul 2008 15:50:46 +0000 Subject: Add zook's ZenUtils to SVN git-svn-id: svn://svn.rockbox.org/rockbox/trunk@18010 a1c6a512-1295-4272-9138-f99709370657 --- .../libraries/pelib-0.9/pelib/IatDirectory.cpp | 179 +++++++++++++++++++++ 1 file changed, 179 insertions(+) create mode 100755 utils/zenutils/libraries/pelib-0.9/pelib/IatDirectory.cpp (limited to 'utils/zenutils/libraries/pelib-0.9/pelib/IatDirectory.cpp') diff --git a/utils/zenutils/libraries/pelib-0.9/pelib/IatDirectory.cpp b/utils/zenutils/libraries/pelib-0.9/pelib/IatDirectory.cpp new file mode 100755 index 0000000000..36482fcbeb --- /dev/null +++ b/utils/zenutils/libraries/pelib-0.9/pelib/IatDirectory.cpp @@ -0,0 +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; + } +} -- cgit v1.2.3