retdec
Public Member Functions | Private Types | Private Member Functions | Private Attributes | List of all members
PeLib::ImportDirectory Class Reference

Class that handles import directories. More...

#include <ImportDirectory.h>

Collaboration diagram for PeLib::ImportDirectory:
Collaboration graph
[legend]

Public Member Functions

 ImportDirectory ()
 Constructor. More...
 
int addFunction (const std::string &strFilename, std::uint16_t wHint)
 Add a function to the import directory. More...
 
int addFunction (const std::string &strFilename, const std::string &strFuncname)
 Add a function to the import directory. More...
 
unsigned int getFileIndex (const std::string &strFilename, currdir cdDir) const
 Get the ID of a file through it's name. More...
 
unsigned int getFunctionIndex (const std::string &strFilename, const std::string &strFuncname, currdir cdDir) const
 Get the ID of a function through it's name. More...
 
std::string getFileName (std::uint32_t dwFilenr, currdir cdDir) const
 Get the name of an imported file. More...
 
void setFileName (std::uint32_t filenr, currdir cdDir, const std::string &name)
 
LoaderError loaderError () const
 Retrieve the loader error. More...
 
void setLoaderError (LoaderError ldrError)
 
std::uint16_t getFunctionHint (std::uint32_t dwFilenr, std::uint32_t dwFuncnr, currdir cdDir) const
 Get the hint of an imported function. More...
 
void setFunctionHint (std::uint32_t dwFilenr, std::uint32_t dwFuncnr, currdir cdDir, std::uint16_t value)
 
std::string getFunctionName (std::uint32_t dwFilenr, std::uint32_t dwFuncnr, currdir cdDir) const
 Get the name of an imported function. More...
 
void setFunctionName (std::uint32_t dwFilenr, std::uint32_t dwFuncnr, currdir cdDir, const std::string &functionName)
 
std::uint32_t getNumberOfFiles (currdir cdDir) const
 Get the number of files which are imported. More...
 
std::uint32_t getNumberOfFunctions (std::uint32_t dwFilenr, currdir cdDir) const
 Get the number of fucntions which are imported by a specific file. More...
 
int read (ImageLoader &imageLoader)
 Read a file's import directory. More...
 
void writePointer (OutputBuffer &obBuffer, std::uint64_t pointerValue)
 Writes pointer to the buffer (32-bit or 64-bit) More...
 
void rebuild (std::vector< std::uint8_t > &vBuffer, std::uint32_t dwRva, bool fixEntries=true)
 Rebuild the import directory. More...
 
int removeFile (const std::string &strFilename)
 Remove a file from the import directory. More...
 
int removeFunction (const std::string &strFilename, const std::string &strFuncname)
 Remove a function from the import directory. More...
 
int removeFunction (const std::string &strFilename, std::uint16_t wHint)
 Remove a function from the import directory. More...
 
unsigned int calculateSize (std::uint32_t pointerSize) const
 Returns the size of the current import directory. More...
 
int write (const std::string &strFilename, std::uint32_t uiOffset, std::uint32_t uiRva, std::uint32_t pointerSize)
 Writes the import directory to a file. More...
 
void setPointerSize (std::uint32_t pointerSize)
 Updates the pointer size for the import directory. More...
 
std::uint32_t getFirstThunk (std::uint32_t dwFilenr, std::uint32_t dwFuncnr, currdir cdDir) const
 Returns the FirstThunk value of a function. More...
 
void setFirstThunk (std::uint32_t dwFilenr, std::uint32_t dwFuncnr, currdir cdDir, std::uint32_t value)
 
std::uint32_t getOriginalFirstThunk (std::uint32_t dwFilenr, std::uint32_t dwFuncnr, currdir cdDir) const
 Returns the OriginalFirstThunk value of a function. More...
 
void setOriginalFirstThunk (std::uint32_t dwFilenr, std::uint32_t dwFuncnr, currdir cdDir, std::uint32_t value)
 
std::uint32_t getFirstThunk (const std::string &strFilename, currdir cdDir) const
 Returns the FirstThunk value of a file. More...
 
std::uint32_t getOriginalFirstThunk (const std::string &strFilename, currdir cdDir) const
 Returns the OriginalFirstThunk value of a file. More...
 
std::uint32_t getForwarderChain (const std::string &strFilename, currdir cdDir) const
 Returns the ForwarderChain value of a file. More...
 
std::uint32_t getRvaOfName (const std::string &strFilename, currdir cdDir) const
 
std::uint32_t getTimeDateStamp (const std::string &strFilename, currdir cdDir) const
 Returns the TimeDateStamp value of a file. More...
 
std::uint32_t getFirstThunk (std::uint32_t dwFilenr, currdir cdDir) const
 Returns the FirstThunk value of a file. More...
 
void setFirstThunk (std::uint32_t dwFilenr, currdir cdDir, std::uint32_t value)
 
std::uint32_t getOriginalFirstThunk (std::uint32_t dwFilenr, currdir cdDir) const
 Returns the OriginalFirstThunk value of a file. More...
 
void setOriginalFirstThunk (std::uint32_t dwFilenr, currdir cdDir, std::uint32_t value)
 
std::uint32_t getForwarderChain (std::uint32_t dwFilenr, currdir cdDir) const
 Returns the ForwarderChain value of a file. More...
 
void setForwarderChain (std::uint32_t dwFilenr, currdir cdDir, std::uint32_t value)
 
std::uint32_t getRvaOfName (std::uint32_t dwFilenr, currdir cdDir) const
 
void setRvaOfName (std::uint32_t dwFilenr, currdir cdDir, std::uint32_t value)
 
std::uint32_t getTimeDateStamp (std::uint32_t dwFilenr, currdir cdDir) const
 Returns the TimeDateStamp value of a file. More...
 
void setTimeDateStamp (std::uint32_t dwFilenr, currdir cdDir, std::uint32_t value)
 
const std::vector< std::pair< unsigned int, unsigned int > > & getOccupiedAddresses () const
 

Private Types

typedef std::vector< PELIB_IMAGE_IMPORT_DIRECTORY >::iterator ImpDirFileIterator
 
typedef std::vector< PELIB_IMAGE_IMPORT_DIRECTORY >::const_iterator ConstImpDirFileIterator
 

Private Member Functions

template<typename T >
bool hasFunction (std::string strFilename, T value, bool(PELIB_THUNK_DATA::*comp)(T) const) const
 Tests if a certain function is imported. More...
 

Private Attributes

std::vector< PELIB_IMAGE_IMPORT_DIRECTORYm_vOldiid
 Stores information about already imported DLLs. More...
 
std::vector< PELIB_IMAGE_IMPORT_DIRECTORYm_vNewiid
 Stores information about imported DLLs which will be added. More...
 
std::vector< std::pair< unsigned int, unsigned int > > m_occupiedAddresses
 Stores RVAs which are occupied by this import directory. More...
 
std::uint64_t m_ordinalMask
 Mask for file ordinal. More...
 
LoaderError m_ldrError
 Error detected by the import table parser. More...
 
std::size_t m_thunkSize
 size of single thunk item More...
 

Detailed Description

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).

Somehow store the rvas of the chunks in the file.

Member Typedef Documentation

◆ ConstImpDirFileIterator

◆ ImpDirFileIterator

Constructor & Destructor Documentation

◆ ImportDirectory()

PeLib::ImportDirectory::ImportDirectory ( )
inline

Constructor.

Member Function Documentation

◆ addFunction() [1/2]

int PeLib::ImportDirectory::addFunction ( const std::string &  strFilename,
const std::string &  strFuncname 
)
inline

Add a function to the import directory.

Add a function to the Import Directory.

Parameters
strFilenameName of the file which will be imported
strFuncnameName of the function which will be imported.

◆ addFunction() [2/2]

int PeLib::ImportDirectory::addFunction ( const std::string &  strFilename,
std::uint16_t  wHint 
)
inline

Add a function to the import directory.

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.

Parameters
strFilenameThe name of a DLL.
wHintThe ordinal of the function in the DLL.

◆ calculateSize()

std::uint32_t PeLib::ImportDirectory::calculateSize ( std::uint32_t  pointerSize) const
inline

Returns the size of the current import directory.

Calculates size of import directory that would be written to a PE file.

Returns
Size of the import directory.

◆ getFileIndex()

unsigned int PeLib::ImportDirectory::getFileIndex ( const std::string &  strFilename,
currdir  cdDir 
) const
inline

Get the ID of a file through it's name.

Searches through the import directory and returns the number of the import directory entry which belongs to the given filename.

Parameters
strFilenameName of the imported file.
cdDirFlag to decide if the OLDDIR or new import directory is used.
Returns
The ID of an imported file.

◆ getFileName()

std::string PeLib::ImportDirectory::getFileName ( std::uint32_t  dwFilenr,
currdir  cdDir 
) const
inline

Get the name of an imported file.

Get the name of an imported file.

Parameters
dwFilenrIdentifies which file should be checked.
cdDirFlag to decide if the OLDDIR or new import directory is used.
Returns
Name of an imported file.

◆ getFirstThunk() [1/3]

std::uint32_t PeLib::ImportDirectory::getFirstThunk ( const std::string &  strFilename,
currdir  cdDir 
) const
inline

Returns the FirstThunk value of a file.

Parameters
strFilenameName of the imported file.
cdDirFlag to decide if the OLDDIR or new import directory is used.
Returns
FirstThunk value of an imported file.

◆ getFirstThunk() [2/3]

std::uint32_t PeLib::ImportDirectory::getFirstThunk ( std::uint32_t  dwFilenr,
currdir  cdDir 
) const
inline

Returns the FirstThunk value of a file.

Parameters
dwFilenrName of the imported file.
cdDirFlag to decide if the OLDDIR or new import directory is used.
Returns
FirstThunk value of an imported file.

◆ getFirstThunk() [3/3]

std::uint32_t PeLib::ImportDirectory::getFirstThunk ( std::uint32_t  dwFilenr,
std::uint32_t  dwFuncnr,
currdir  cdDir 
) const
inline

Returns the FirstThunk value of a function.

Parameters
dwFilenrID of the imported file.
dwFuncnrID of the imported function.
cdDirFlag to decide if the OLDDIR or new import directory is used.
Returns
FirstThunk value of an imported function.

◆ getForwarderChain() [1/2]

std::uint32_t PeLib::ImportDirectory::getForwarderChain ( const std::string &  strFilename,
currdir  cdDir 
) const
inline

Returns the ForwarderChain value of a file.

Parameters
strFilenameName of the imported file.
cdDirFlag to decide if the OLDDIR or new import directory is used.
Returns
ForwarderChain value of an imported file.

◆ getForwarderChain() [2/2]

std::uint32_t PeLib::ImportDirectory::getForwarderChain ( std::uint32_t  dwFilenr,
currdir  cdDir 
) const
inline

Returns the ForwarderChain value of a file.

Parameters
dwFilenrName of the imported file.
cdDirFlag to decide if the OLDDIR or new import directory is used.
Returns
ForwarderChain value of an imported file.

◆ getFunctionHint()

std::uint16_t PeLib::ImportDirectory::getFunctionHint ( std::uint32_t  dwFilenr,
std::uint32_t  dwFuncnr,
currdir  cdDir 
) const
inline

Get the hint of an imported function.

Get the hint of an imported function.

Parameters
dwFilenrIdentifies which file should be checked.
dwFuncnrIdentifies which function should be checked.
cdDirFlag to decide if the OLDDIR or new import directory is used.
Returns
Hint of an imported function.

◆ getFunctionIndex()

unsigned int PeLib::ImportDirectory::getFunctionIndex ( const std::string &  strFilename,
const std::string &  strFuncname,
currdir  cdDir 
) const
inline

Get the ID of a function through it's name.

Searches through an imported file for a specific function.

Parameters
strFilenameName of the imported file.
strFuncnameName of the imported function.
cdDirFlag to decide if the OLDDIR or new import directory is used.
Returns
ID of the imported function.

◆ getFunctionName()

std::string PeLib::ImportDirectory::getFunctionName ( std::uint32_t  dwFilenr,
std::uint32_t  dwFuncnr,
currdir  cdDir 
) const
inline

Get the name of an imported function.

Get the name of an imported function.

Parameters
dwFilenrIdentifies which file should be checked.
dwFuncnrIdentifies which function should be checked.
cdDirFlag to decide if the OLDDIR or new import directory is used.
Returns
Name of an imported function.
Todo:
Marked line is unsafe (function should be rewritten).

◆ getNumberOfFiles()

std::uint32_t PeLib::ImportDirectory::getNumberOfFiles ( currdir  cdDir) const
inline

Get the number of files which are imported.

Get the number of files which are currently being imported.

Parameters
cdDirFlag to decide if the OLDDIR or new import directory is used.
Returns
Number of files which are currently being imported.

◆ getNumberOfFunctions()

std::uint32_t PeLib::ImportDirectory::getNumberOfFunctions ( std::uint32_t  dwFilenr,
currdir  cdDir 
) const
inline

Get the number of fucntions which are imported by a specific file.

Get the number of functions which are currently being imported from a specific file.

Parameters
dwFilenrIdentifies which file should be checked.
cdDirFlag to decide if the OLDDIR or new import directory is used.
Returns
Number of functions which are currently being imported from a specific file.

◆ getOccupiedAddresses()

const std::vector< std::pair< unsigned int, unsigned int > > & PeLib::ImportDirectory::getOccupiedAddresses ( ) const
inline

◆ getOriginalFirstThunk() [1/3]

std::uint32_t PeLib::ImportDirectory::getOriginalFirstThunk ( const std::string &  strFilename,
currdir  cdDir 
) const
inline

Returns the OriginalFirstThunk value of a file.

Parameters
strFilenameName of the imported file.
cdDirFlag to decide if the OLDDIR or new import directory is used.
Returns
OriginalFirstThunk value of an imported file.

◆ getOriginalFirstThunk() [2/3]

std::uint32_t PeLib::ImportDirectory::getOriginalFirstThunk ( std::uint32_t  dwFilenr,
currdir  cdDir 
) const
inline

Returns the OriginalFirstThunk value of a file.

Parameters
dwFilenrName of the imported file.
cdDirFlag to decide if the OLDDIR or new import directory is used.
Returns
OriginalFirstThunk value of an imported file.

◆ getOriginalFirstThunk() [3/3]

std::uint32_t PeLib::ImportDirectory::getOriginalFirstThunk ( std::uint32_t  dwFilenr,
std::uint32_t  dwFuncnr,
currdir  cdDir 
) const
inline

Returns the OriginalFirstThunk value of a function.

Parameters
dwFilenrID of the imported file.
dwFuncnrID of the imported function.
cdDirFlag to decide if the OLDDIR or new import directory is used.
Returns
OriginalFirstThunk value of an imported function.

◆ getRvaOfName() [1/2]

std::uint32_t PeLib::ImportDirectory::getRvaOfName ( const std::string &  strFilename,
currdir  cdDir 
) const
inline

◆ getRvaOfName() [2/2]

std::uint32_t PeLib::ImportDirectory::getRvaOfName ( std::uint32_t  dwFilenr,
currdir  cdDir 
) const
inline

◆ getTimeDateStamp() [1/2]

std::uint32_t PeLib::ImportDirectory::getTimeDateStamp ( const std::string &  strFilename,
currdir  cdDir 
) const
inline

Returns the TimeDateStamp value of a file.

Parameters
strFilenameName of the imported file.
cdDirFlag to decide if the OLDDIR or new import directory is used.
Returns
TimeDateStamp value of an imported file.

◆ getTimeDateStamp() [2/2]

std::uint32_t PeLib::ImportDirectory::getTimeDateStamp ( std::uint32_t  dwFilenr,
currdir  cdDir 
) const
inline

Returns the TimeDateStamp value of a file.

Parameters
dwFilenrName of the imported file.
cdDirFlag to decide if the OLDDIR or new import directory is used.
Returns
TimeDateStamp value of an imported file.

◆ hasFunction()

template<typename T >
bool PeLib::ImportDirectory::hasFunction ( std::string  strFilename,
value,
bool(PELIB_THUNK_DATA::*)(T) const  comp 
) const
inlineprivate

Tests if a certain function is imported.

◆ loaderError()

LoaderError PeLib::ImportDirectory::loaderError ( ) const
inline

Retrieve the loader error.

Get the error that was detected during import table parsing

◆ read()

int PeLib::ImportDirectory::read ( ImageLoader imageLoader)
inline

Read a file's import directory.

Read an import directory from a file.

Todo:
Check if streams failed.
Parameters
imageLoaderA valid PE loader.

◆ rebuild()

void PeLib::ImportDirectory::rebuild ( std::vector< std::uint8_t > &  vBuffer,
std::uint32_t  dwRva,
bool  fixEntries = true 
)
inline

Rebuild the import directory.

Rebuilds the import directory.

Parameters
vBufferBuffer the rebuilt import directory will be written to.
dwRvaThe RVA of the ImportDirectory in the file.
fixEntriesBoolean flag.
Todo:
uiSizeoffuncnames is not used.

◆ removeFile()

int PeLib::ImportDirectory::removeFile ( const std::string &  strFilename)
inline

Remove a file from the import directory.

Removes a specific file and all functions of it from the import directory.

Parameters
strFilenameName of the file which will be removed.

◆ removeFunction() [1/2]

int PeLib::ImportDirectory::removeFunction ( const std::string &  strFilename,
const std::string &  strFuncname 
)
inline

Remove a function from the import directory.

Removes a specific function from the import directory.

Parameters
strFilenameName of the file which exports the function.
strFuncnameName of the imported function.

◆ removeFunction() [2/2]

int PeLib::ImportDirectory::removeFunction ( const std::string &  strFilename,
std::uint16_t  wHint 
)
inline

Remove a function from the import directory.

Removes a specific function from the import directory.

Parameters
strFilenameName of the file which exports the function.
wHintThe hint of the function.

◆ setFileName()

void PeLib::ImportDirectory::setFileName ( std::uint32_t  filenr,
currdir  cdDir,
const std::string &  name 
)
inline

◆ setFirstThunk() [1/2]

void PeLib::ImportDirectory::setFirstThunk ( std::uint32_t  dwFilenr,
currdir  cdDir,
std::uint32_t  value 
)
inline

◆ setFirstThunk() [2/2]

void PeLib::ImportDirectory::setFirstThunk ( std::uint32_t  dwFilenr,
std::uint32_t  dwFuncnr,
currdir  cdDir,
std::uint32_t  value 
)
inline

◆ setForwarderChain()

void PeLib::ImportDirectory::setForwarderChain ( std::uint32_t  dwFilenr,
currdir  cdDir,
std::uint32_t  value 
)
inline

◆ setFunctionHint()

void PeLib::ImportDirectory::setFunctionHint ( std::uint32_t  dwFilenr,
std::uint32_t  dwFuncnr,
currdir  cdDir,
std::uint16_t  value 
)
inline

◆ setFunctionName()

void PeLib::ImportDirectory::setFunctionName ( std::uint32_t  dwFilenr,
std::uint32_t  dwFuncnr,
currdir  cdDir,
const std::string &  functionName 
)
inline

◆ setLoaderError()

void PeLib::ImportDirectory::setLoaderError ( LoaderError  ldrError)
inline

◆ setOriginalFirstThunk() [1/2]

void PeLib::ImportDirectory::setOriginalFirstThunk ( std::uint32_t  dwFilenr,
currdir  cdDir,
std::uint32_t  value 
)
inline

◆ setOriginalFirstThunk() [2/2]

void PeLib::ImportDirectory::setOriginalFirstThunk ( std::uint32_t  dwFilenr,
std::uint32_t  dwFuncnr,
currdir  cdDir,
std::uint32_t  value 
)
inline

◆ setPointerSize()

void PeLib::ImportDirectory::setPointerSize ( std::uint32_t  pointerSize)
inline

Updates the pointer size for the import directory.

Updates pointer size for import directory

Parameters
pointerSizeSize of the pointer (4 or 8 bytes).

◆ setRvaOfName()

void PeLib::ImportDirectory::setRvaOfName ( std::uint32_t  dwFilenr,
currdir  cdDir,
std::uint32_t  value 
)
inline

◆ setTimeDateStamp()

void PeLib::ImportDirectory::setTimeDateStamp ( std::uint32_t  dwFilenr,
currdir  cdDir,
std::uint32_t  value 
)
inline

◆ write()

int PeLib::ImportDirectory::write ( const std::string &  strFilename,
std::uint32_t  uiOffset,
std::uint32_t  uiRva,
std::uint32_t  pointerSize 
)
inline

Writes the import directory to a file.

Writes the current import directory to a file.

Parameters
strFilenameName of the file.
uiOffsetFile Offset of the new import directory.
uiRvaRVA which belongs to that file offset.
pointerSizeSize of the pointer (4 bytes or 8 bytes)

◆ writePointer()

void PeLib::ImportDirectory::writePointer ( OutputBuffer obBuffer,
std::uint64_t  pointerValue 
)
inline

Writes pointer to the buffer (32-bit or 64-bit)

Member Data Documentation

◆ m_ldrError

LoaderError PeLib::ImportDirectory::m_ldrError
private

Error detected by the import table parser.

◆ m_occupiedAddresses

std::vector<std::pair<unsigned int, unsigned int> > PeLib::ImportDirectory::m_occupiedAddresses
private

Stores RVAs which are occupied by this import directory.

◆ m_ordinalMask

std::uint64_t PeLib::ImportDirectory::m_ordinalMask
private

Mask for file ordinal.

◆ m_thunkSize

std::size_t PeLib::ImportDirectory::m_thunkSize
private

size of single thunk item

◆ m_vNewiid

std::vector<PELIB_IMAGE_IMPORT_DIRECTORY> PeLib::ImportDirectory::m_vNewiid
private

Stores information about imported DLLs which will be added.

◆ m_vOldiid

std::vector<PELIB_IMAGE_IMPORT_DIRECTORY> PeLib::ImportDirectory::m_vOldiid
private

Stores information about already imported DLLs.


The documentation for this class was generated from the following file: