retdec
Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
PeLib::ResourceDirectory Class Reference

Class that represents the resource directory of a PE file. More...

#include <ResourceDirectory.h>

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

Public Member Functions

 ResourceDirectory ()
 Constructor. More...
 
virtual ~ResourceDirectory ()=default
 Destructor. More...
 
int read (ImageLoader &imageLoader)
 Reads the resource directory from a file. More...
 
ResourceNodegetRoot ()
 
const ResourceNodegetRoot () const
 
LoaderError loaderError () const
 Retrieve the loader error. More...
 
void setLoaderError (LoaderError ldrError)
 
void makeValid ()
 Corrects a erroneous resource directory. More...
 
void rebuild (std::vector< std::uint8_t > &vBuffer, unsigned int uiRva) const
 Rebuilds the resource directory. More...
 
void recalculate (unsigned int &uiNewSize, unsigned int uiNewRva)
 Recalculate the tree for different RVA. More...
 
int write (const std::string &strFilename, unsigned int uiOffset, unsigned int uiRva) const
 Returns the size of the rebuilt resource directory. More...
 
int addResourceType (std::uint32_t dwResTypeId)
 Adds a new resource type. More...
 
int addResourceType (const std::string &strResTypeName)
 Adds a new resource type. More...
 
int removeResourceType (std::uint32_t dwResTypeId)
 Removes a resource type and all of it's resources. More...
 
int removeResourceType (const std::string &strResTypeName)
 Removes a resource type and all of it's resources. More...
 
int removeResourceTypeByIndex (unsigned int uiIndex)
 Removes a resource type and all of it's resources. More...
 
int addResource (std::uint32_t dwResTypeId, std::uint32_t dwResId)
 Adds a new resource. More...
 
int addResource (std::uint32_t dwResTypeId, const std::string &strResName)
 Adds a new resource. More...
 
int addResource (const std::string &strResTypeName, std::uint32_t dwResId)
 Adds a new resource. More...
 
int addResource (const std::string &strResTypeName, const std::string &strResName)
 Adds a new resource. More...
 
int removeResource (std::uint32_t dwResTypeId, std::uint32_t dwResId)
 Removes a resource. More...
 
int removeResource (std::uint32_t dwResTypeId, const std::string &strResName)
 Removes a resource. More...
 
int removeResource (const std::string &strResTypeName, std::uint32_t dwResId)
 Removes a resource. More...
 
int removeResource (const std::string &strResTypeName, const std::string &strResName)
 Removes a resource. More...
 
unsigned int getOffset () const
 Returns start offset of resource directory in file. More...
 
unsigned int getNumberOfResourceTypes () const
 Returns the number of resource types. More...
 
std::uint32_t getResourceTypeIdByIndex (unsigned int uiIndex) const
 Returns the ID of a resource type. More...
 
std::string getResourceTypeNameByIndex (unsigned int uiIndex) const
 Returns the name of a resource type. More...
 
int resourceTypeIdToIndex (std::uint32_t dwResTypeId) const
 Converts a resource type ID to an index. More...
 
int resourceTypeNameToIndex (const std::string &strResTypeName) const
 Converts a resource type name to an index. More...
 
unsigned int getNumberOfResources (std::uint32_t dwId) const
 Returns the number of resources of a certain resource type. More...
 
unsigned int getNumberOfResources (const std::string &strResTypeName) const
 Returns the number of resources of a certain resource type. More...
 
unsigned int getNumberOfResourcesByIndex (unsigned int uiIndex) const
 Returns the number of resources of a certain resource type. More...
 
void getResourceData (std::uint32_t dwResTypeId, std::uint32_t dwResId, std::vector< std::uint8_t > &data) const
 Returns the data of a certain resource. More...
 
void getResourceData (std::uint32_t dwResTypeId, const std::string &strResName, std::vector< std::uint8_t > &data) const
 Returns the data of a certain resource. More...
 
void getResourceData (const std::string &strResTypeName, std::uint32_t dwResId, std::vector< std::uint8_t > &data) const
 Returns the data of a certain resource. More...
 
void getResourceData (const std::string &strResTypeName, const std::string &strResName, std::vector< std::uint8_t > &data) const
 Returns the data of a certain resource. More...
 
void getResourceDataByIndex (unsigned int uiResTypeIndex, unsigned int uiResIndex, std::vector< std::uint8_t > &data) const
 Returns the data of a certain resource. More...
 
void setResourceData (std::uint32_t dwResTypeId, std::uint32_t dwResId, std::vector< std::uint8_t > &data)
 Sets the data of a certain resource. More...
 
void setResourceData (std::uint32_t dwResTypeId, const std::string &strResName, std::vector< std::uint8_t > &data)
 Sets the data of a certain resource. More...
 
void setResourceData (const std::string &strResTypeName, std::uint32_t dwResId, std::vector< std::uint8_t > &data)
 Sets the data of a certain resource. More...
 
void setResourceData (const std::string &strResTypeName, const std::string &strResName, std::vector< std::uint8_t > &data)
 Sets the data of a certain resource. More...
 
void setResourceDataByIndex (unsigned int uiResTypeIndex, unsigned int uiResIndex, std::vector< std::uint8_t > &data)
 Sets the data of a certain resource. More...
 
std::uint32_t getResourceId (std::uint32_t dwResTypeId, const std::string &strResName) const
 Returns the ID of a certain resource. More...
 
std::uint32_t getResourceId (const std::string &strResTypeName, const std::string &strResName) const
 Returns the ID of a certain resource. More...
 
std::uint32_t getResourceIdByIndex (unsigned int uiResTypeIndex, unsigned int uiResIndex) const
 Returns the ID of a certain resource. More...
 
void setResourceId (std::uint32_t dwResTypeId, std::uint32_t dwResId, std::uint32_t dwNewResId)
 Sets the ID of a certain resource. More...
 
void setResourceId (std::uint32_t dwResTypeId, const std::string &strResName, std::uint32_t dwNewResId)
 Sets the ID of a certain resource. More...
 
void setResourceId (const std::string &strResTypeName, std::uint32_t dwResId, std::uint32_t dwNewResId)
 Sets the ID of a certain resource. More...
 
void setResourceId (const std::string &strResTypeName, const std::string &strResName, std::uint32_t dwNewResId)
 Sets the ID of a certain resource. More...
 
void setResourceIdByIndex (unsigned int uiResTypeIndex, unsigned int uiResIndex, std::uint32_t dwNewResId)
 Sets the ID of a certain resource. More...
 
std::string getResourceName (std::uint32_t dwResTypeId, std::uint32_t dwResId) const
 Returns the name of a certain resource. More...
 
std::string getResourceName (const std::string &strResTypeName, std::uint32_t dwResId) const
 Returns the name of a certain resource. More...
 
std::string getResourceNameByIndex (unsigned int uiResTypeIndex, unsigned int uiResIndex) const
 Returns the name of a certain resource. More...
 
void setResourceName (std::uint32_t dwResTypeId, std::uint32_t dwResId, const std::string &strNewResName)
 Sets the name of a certain resource. More...
 
void setResourceName (std::uint32_t dwResTypeId, const std::string &strResName, const std::string &strNewResName)
 Sets the name of a certain resource. More...
 
void setResourceName (const std::string &strResTypeName, std::uint32_t dwResId, const std::string &strNewResName)
 Sets the name of a certain resource. More...
 
void setResourceName (const std::string &strResTypeName, const std::string &strResName, const std::string &strNewResName)
 Sets the name of a certain resource. More...
 
void setResourceNameByIndex (unsigned int uiResTypeIndex, unsigned int uiResIndex, const std::string &strNewResName)
 Sets the name of a certain resource. More...
 
void insertNodeOffset (std::size_t nodeOffset)
 Insert offset of loaded node. More...
 
bool hasNodeOffset (std::size_t nodeOffset) const
 Check if node with specified offset was loaded. More...
 
void addOccupiedAddressRange (unsigned int start, unsigned int end)
 
const std::vector< std::pair< unsigned int, unsigned int > > & getOccupiedAddresses () const
 

Protected Member Functions

template<typename S , typename T >
std::vector< ResourceChild >::const_iterator locateResourceT (S restypeid, T resid) const
 Retrieves an iterator to a specified resource child. More...
 
template<typename S , typename T >
std::vector< ResourceChild >::iterator locateResourceT (S restypeid, T resid)
 Retrieves an iterator to a specified resource child. More...
 
template<typename S , typename T >
int addResourceT (S restypeid, T resid, ResourceChild &rc)
 Adds a new resource. More...
 
template<typename S , typename T >
int removeResourceT (S restypeid, T resid)
 Removes new resource. More...
 
template<typename S , typename T >
int getResourceDataT (S restypeid, T resid, std::vector< std::uint8_t > &data) const
 Returns the data of a resource. More...
 
template<typename S , typename T >
int setResourceDataT (S restypeid, T resid, std::vector< std::uint8_t > &data)
 Sets the data of a resource. More...
 
template<typename S , typename T >
std::uint32_t getResourceIdT (S restypeid, T resid) const
 Returns the ID of a resource. More...
 
template<typename S , typename T >
int setResourceIdT (S restypeid, T resid, std::uint32_t dwNewResId)
 Sets the ID of a resource. More...
 
template<typename S , typename T >
std::string getResourceNameT (S restypeid, T resid) const
 Returns the name of a resource. More...
 
template<typename S , typename T >
int setResourceNameT (S restypeid, T resid, std::string strNewResName)
 Sets the name of a resource. More...
 

Protected Attributes

unsigned int m_readOffset
 Start offset of directory in file. More...
 
ResourceNode m_rnRoot
 The root node of the resource directory. More...
 
std::set< std::size_t > m_resourceNodeOffsets
 Detection of invalid structure of nodes in directory. More...
 
std::vector< std::pair< unsigned int, unsigned int > > m_occupiedAddresses
 Stores RVAs which are occupied by this export directory. More...
 
LoaderError m_ldrError
 Error detected by the import table parser. More...
 

Detailed Description

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<bits>::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.

Constructor & Destructor Documentation

◆ ResourceDirectory()

PeLib::ResourceDirectory::ResourceDirectory ( )

Constructor.

Constructor

◆ ~ResourceDirectory()

virtual PeLib::ResourceDirectory::~ResourceDirectory ( )
virtualdefault

Destructor.

Member Function Documentation

◆ addOccupiedAddressRange()

void PeLib::ResourceDirectory::addOccupiedAddressRange ( unsigned int  start,
unsigned int  end 
)

◆ addResource() [1/4]

int PeLib::ResourceDirectory::addResource ( const std::string &  strResTypeName,
const std::string &  strResName 
)

Adds a new resource.

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.

Parameters
strResTypeNameName of the resource type.
strResNameName of the resource.

◆ addResource() [2/4]

int PeLib::ResourceDirectory::addResource ( const std::string &  strResTypeName,
std::uint32_t  dwResId 
)

Adds a new resource.

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.

Parameters
strResTypeNameName of the resource type.
dwResIdID of the resource.

◆ addResource() [3/4]

int PeLib::ResourceDirectory::addResource ( std::uint32_t  dwResTypeId,
const std::string &  strResName 
)

Adds a new resource.

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.

Parameters
dwResTypeIdID of the resource type.
strResNameName of the resource.

◆ addResource() [4/4]

int PeLib::ResourceDirectory::addResource ( std::uint32_t  dwResTypeId,
std::uint32_t  dwResId 
)

Adds a new resource.

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.

Parameters
dwResTypeIdID of the resource type.
dwResIdID of the resource.

◆ addResourceT()

template<typename S , typename T >
int PeLib::ResourceDirectory::addResourceT ( restypeid,
resid,
ResourceChild rc 
)
protected

Adds a new resource.

Adds a new resource, resource type and ID are specified by the parameters.

Parameters
restypeidIdentifier of the resource type (either ID or name).
residIdentifier of the resource (either ID or name).
rcResourceChild that will be added.

◆ addResourceType() [1/2]

int PeLib::ResourceDirectory::addResourceType ( const std::string &  strResTypeName)

Adds a new resource type.

Adds another resource type. The new resource type is identified by the name strResTypeName.

Parameters
strResTypeNameName which identifies the resource type.

◆ addResourceType() [2/2]

int PeLib::ResourceDirectory::addResourceType ( std::uint32_t  dwResTypeId)

Adds a new resource type.

Adds another resource type. The new resource type is identified by the ID dwResTypeId.

Parameters
dwResTypeIdID which identifies the resource type.

◆ getNumberOfResources() [1/2]

unsigned int PeLib::ResourceDirectory::getNumberOfResources ( const std::string &  strResTypeName) const

Returns the number of resources of a certain resource type.

Returns the number of resources of a specific resource type.

Parameters
strResTypeNameName of the resource type.
Returns
Number of resources of resource type strResTypeName.

◆ getNumberOfResources() [2/2]

unsigned int PeLib::ResourceDirectory::getNumberOfResources ( std::uint32_t  dwId) const

Returns the number of resources of a certain resource type.

Returns the number of resources of a specific resource type.

Parameters
dwIdID of the resource type.
Returns
Number of resources of resource type dwId.

◆ getNumberOfResourcesByIndex()

unsigned int PeLib::ResourceDirectory::getNumberOfResourcesByIndex ( unsigned int  uiIndex) const

Returns the number of resources of a certain resource type.

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.

Parameters
uiIndexIndex which identifies a resource type.
Returns
The number of resources of the specified resource type.

◆ getNumberOfResourceTypes()

unsigned int PeLib::ResourceDirectory::getNumberOfResourceTypes ( ) const

Returns the number of resource types.

Returns the number of resource types.

◆ getOccupiedAddresses()

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

◆ getOffset()

unsigned int PeLib::ResourceDirectory::getOffset ( ) const

Returns start offset of resource directory in file.

Returns start offset of resource directory in file.

◆ getResourceData() [1/4]

void PeLib::ResourceDirectory::getResourceData ( const std::string &  strResTypeName,
const std::string &  strResName,
std::vector< std::uint8_t > &  data 
) const

Returns the data of a certain resource.

Gets the resource data of a specific resource.

Parameters
strResTypeNameIdentifies the resource type of the resource.
strResNameIdentifies the resource.
dataVector where the data is stored.

◆ getResourceData() [2/4]

void PeLib::ResourceDirectory::getResourceData ( const std::string &  strResTypeName,
std::uint32_t  dwResId,
std::vector< std::uint8_t > &  data 
) const

Returns the data of a certain resource.

Gets the resource data of a specific resource.

Parameters
strResTypeNameIdentifies the resource type of the resource.
dwResIdIdentifies the resource.
dataVector where the data is stored.

◆ getResourceData() [3/4]

void PeLib::ResourceDirectory::getResourceData ( std::uint32_t  dwResTypeId,
const std::string &  strResName,
std::vector< std::uint8_t > &  data 
) const

Returns the data of a certain resource.

Gets the resource data of a specific resource.

Parameters
dwResTypeIdIdentifies the resource type of the resource.
strResNameIdentifies the resource.
dataVector where the data is stored.

◆ getResourceData() [4/4]

void PeLib::ResourceDirectory::getResourceData ( std::uint32_t  dwResTypeId,
std::uint32_t  dwResId,
std::vector< std::uint8_t > &  data 
) const

Returns the data of a certain resource.

Gets the resource data of a specific resource.

Parameters
dwResTypeIdIdentifies the resource type of the resource.
dwResIdIdentifies the resource.
dataVector where the data is stored.

◆ getResourceDataByIndex()

void PeLib::ResourceDirectory::getResourceDataByIndex ( unsigned int  uiResTypeIndex,
unsigned int  uiResIndex,
std::vector< std::uint8_t > &  data 
) const

Returns the data of a certain resource.

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.

Parameters
uiResTypeIndexIdentifies the resource type of the resource.
uiResIndexIdentifies the resource.
dataVector where the data is stored.

◆ getResourceDataT()

template<typename S , typename T >
int PeLib::ResourceDirectory::getResourceDataT ( restypeid,
resid,
std::vector< std::uint8_t > &  data 
) const
protected

Returns the data of a resource.

Returns the data of a resource, resource type and ID are specified by the parameters.

Parameters
restypeidIdentifier of the resource type (either ID or name).
residIdentifier of the resource (either ID or name).
dataThe data of the resource will be written into this vector.

◆ getResourceId() [1/2]

std::uint32_t PeLib::ResourceDirectory::getResourceId ( const std::string &  strResTypeName,
const std::string &  strResName 
) const

Returns the ID of a certain resource.

Gets the ID of a specific resource.

Parameters
strResTypeNameIdentifies the resource type of the resource.
strResNameIdentifies the resource.
Returns
ID of the specified resource.

◆ getResourceId() [2/2]

std::uint32_t PeLib::ResourceDirectory::getResourceId ( std::uint32_t  dwResTypeId,
const std::string &  strResName 
) const

Returns the ID of a certain resource.

Gets the ID of a specific resource.

Parameters
dwResTypeIdIdentifies the resource type of the resource.
strResNameIdentifies the resource.
Returns
ID of the specified resource.

◆ getResourceIdByIndex()

std::uint32_t PeLib::ResourceDirectory::getResourceIdByIndex ( unsigned int  uiResTypeIndex,
unsigned int  uiResIndex 
) const

Returns the ID of a certain resource.

Gets the ID of a specific resource by index.

Parameters
uiResTypeIndexIdentifies the resource type of the resource.
uiResIndexIdentifies the resource.
Returns
ID of the specified resource.

◆ getResourceIdT()

template<typename S , typename T >
std::uint32_t PeLib::ResourceDirectory::getResourceIdT ( restypeid,
resid 
) const
protected

Returns the ID of a resource.

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.

Parameters
restypeidIdentifier of the resource type (either ID or name).
residIdentifier of the resource (either ID or name).
Returns
The ID of the specified resource.

◆ getResourceName() [1/2]

std::string PeLib::ResourceDirectory::getResourceName ( const std::string &  strResTypeName,
std::uint32_t  dwResId 
) const

Returns the name of a certain resource.

Gets the Name of a specific resource.

Parameters
strResTypeNameIdentifies the resource type of the resource.
dwResIdIdentifies the resource.
Returns
Name of the specified resource.

◆ getResourceName() [2/2]

std::string PeLib::ResourceDirectory::getResourceName ( std::uint32_t  dwResTypeId,
std::uint32_t  dwResId 
) const

Returns the name of a certain resource.

Gets the Name of a specific resource.

Parameters
dwResTypeIdIdentifies the resource type of the resource.
dwResIdIdentifies the resource.
Returns
Name of the specified resource.

◆ getResourceNameByIndex()

std::string PeLib::ResourceDirectory::getResourceNameByIndex ( unsigned int  uiResTypeIndex,
unsigned int  uiResIndex 
) const

Returns the name of a certain resource.

Gets the name of a specific resource by index.

Parameters
uiResTypeIndexIdentifies the resource type of the resource.
uiResIndexIdentifies the resource.
Returns
Name of the specified resource.

◆ getResourceNameT()

template<typename S , typename T >
std::string PeLib::ResourceDirectory::getResourceNameT ( restypeid,
resid 
) const
protected

Returns the name of a resource.

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.

Parameters
restypeidIdentifier of the resource type (either ID or name).
residIdentifier of the resource (either ID or name).
Returns
The name of the specified resource.

◆ getResourceTypeIdByIndex()

std::uint32_t PeLib::ResourceDirectory::getResourceTypeIdByIndex ( unsigned int  uiIndex) const

Returns the ID of a resource type.

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.

Parameters
uiIndexIndex which identifies a resource type.
Returns
The ID of the specified resource type.

◆ getResourceTypeNameByIndex()

std::string PeLib::ResourceDirectory::getResourceTypeNameByIndex ( unsigned int  uiIndex) const

Returns the name of a resource type.

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.

Parameters
uiIndexIndex which identifies a resource type.
Returns
The name of the specified resource type.

◆ getRoot() [1/2]

ResourceNode * PeLib::ResourceDirectory::getRoot ( )

Returns the root node of the resource directory.

Returns
Root node of the resource directory.

◆ getRoot() [2/2]

const ResourceNode * PeLib::ResourceDirectory::getRoot ( ) const

Returns the root node of the resource directory.

Returns
Root node of the resource directory.

◆ hasNodeOffset()

bool PeLib::ResourceDirectory::hasNodeOffset ( std::size_t  nodeOffset) const

Check if node with specified offset was loaded.

Check if node with specified offset was loaded.

Parameters
nodeOffsetOffset of node.

◆ insertNodeOffset()

void PeLib::ResourceDirectory::insertNodeOffset ( std::size_t  nodeOffset)

Insert offset of loaded node.

Insert offset of loaded node.

Parameters
nodeOffsetOffset of loaded node.

◆ loaderError()

LoaderError PeLib::ResourceDirectory::loaderError ( ) const

Retrieve the loader error.

Get the error that was detected during resource parsing

◆ locateResourceT() [1/2]

template<typename S , typename T >
std::vector< ResourceChild >::iterator PeLib::ResourceDirectory::locateResourceT ( restypeid,
resid 
)
protected

Retrieves an iterator to a specified resource child.

Looks through the entire resource tree and returns an iterator to the resource specified by the parameters.

Parameters
restypeidIdentifier of the resource type (either ID or name).
residIdentifier of the resource (either ID or name).
Returns
An iterator to the specified resource.

◆ locateResourceT() [2/2]

template<typename S , typename T >
std::vector< ResourceChild >::const_iterator PeLib::ResourceDirectory::locateResourceT ( restypeid,
resid 
) const
protected

Retrieves an iterator to a specified resource child.

Looks through the entire resource tree and returns a const_iterator to the resource specified by the parameters.

Parameters
restypeidIdentifier of the resource type (either ID or name).
residIdentifier of the resource (either ID or name).
Returns
A const_iterator to the specified resource.

◆ makeValid()

void PeLib::ResourceDirectory::makeValid ( )

Corrects a erroneous resource directory.

Correctly sorts the resource nodes of the resource tree. This function should be called before calling rebuild.

◆ read()

int PeLib::ResourceDirectory::read ( ImageLoader imageLoader)

Reads the resource directory from a file.

Reads the resource directory from a file.

Parameters
imageLoaderimage loader

◆ rebuild()

void PeLib::ResourceDirectory::rebuild ( std::vector< std::uint8_t > &  vBuffer,
unsigned int  uiRva 
) const

Rebuilds the resource directory.

Rebuilds the resource directory.

Parameters
vBufferBuffer the source directory will be written to.
uiRvaRVA of the resource directory.

◆ recalculate()

void PeLib::ResourceDirectory::recalculate ( unsigned int &  uiNewSize,
unsigned int  uiNewRva 
)

Recalculate the tree for different RVA.

Recalculates the resource directory for directory with new RVA.

Parameters
uiNewSizeThe size of the new directory. Is recalculated after the whole resource tree is traversed.
uiNewRvaThe RVA of the new directory.

◆ removeResource() [1/4]

int PeLib::ResourceDirectory::removeResource ( const std::string &  strResTypeName,
const std::string &  strResName 
)

Removes a resource.

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.

Parameters
strResTypeNameName of the resource type.
strResNameName of the resource.

◆ removeResource() [2/4]

int PeLib::ResourceDirectory::removeResource ( const std::string &  strResTypeName,
std::uint32_t  dwResId 
)

Removes a resource.

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.

Parameters
strResTypeNameName of the resource type.
dwResIdID of the resource.

◆ removeResource() [3/4]

int PeLib::ResourceDirectory::removeResource ( std::uint32_t  dwResTypeIndex,
const std::string &  strResName 
)

Removes a resource.

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.

Parameters
dwResTypeIndexID of the resource type.
strResNameName of the resource.

◆ removeResource() [4/4]

int PeLib::ResourceDirectory::removeResource ( std::uint32_t  dwResTypeIndex,
std::uint32_t  dwResId 
)

Removes a resource.

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.

Parameters
dwResTypeIndexID of the resource type.
dwResIdID of the resource.

◆ removeResourceT()

template<typename S , typename T >
int PeLib::ResourceDirectory::removeResourceT ( restypeid,
resid 
)
protected

Removes new resource.

Removes a resource, resource type and ID are specified by the parameters.

Parameters
restypeidIdentifier of the resource type (either ID or name).
residIdentifier of the resource (either ID or name).

◆ removeResourceType() [1/2]

int PeLib::ResourceDirectory::removeResourceType ( const std::string &  strResTypeName)

Removes a resource type and all of it's resources.

Removes the resource type identified by the name strResTypeName.

Parameters
strResTypeNameName which identifies the resource type.

◆ removeResourceType() [2/2]

int PeLib::ResourceDirectory::removeResourceType ( std::uint32_t  dwResTypeId)

Removes a resource type and all of it's resources.

Removes the resource type identified by the ID dwResTypeId.

Parameters
dwResTypeIdID which identifies the resource type.

◆ removeResourceTypeByIndex()

int PeLib::ResourceDirectory::removeResourceTypeByIndex ( unsigned int  uiIndex)

Removes a resource type and all of it's resources.

Removes the resource type identified by the index uiIndex.

Parameters
uiIndexIndex which identifies the resource type.

◆ resourceTypeIdToIndex()

int PeLib::ResourceDirectory::resourceTypeIdToIndex ( std::uint32_t  dwResTypeId) const

Converts a resource type ID to an index.

Converts the ID of a resource type to an index.

Parameters
dwResTypeIdID of the resource type.
Returns
Index of that resource type.

◆ resourceTypeNameToIndex()

int PeLib::ResourceDirectory::resourceTypeNameToIndex ( const std::string &  strResTypeName) const

Converts a resource type name to an index.

Converts the name of a resource type to an index.

Parameters
strResTypeNameID of the resource type.
Returns
Index of that resource type.

◆ setLoaderError()

void PeLib::ResourceDirectory::setLoaderError ( LoaderError  ldrError)

◆ setResourceData() [1/4]

void PeLib::ResourceDirectory::setResourceData ( const std::string &  strResTypeName,
const std::string &  strResName,
std::vector< std::uint8_t > &  data 
)

Sets the data of a certain resource.

Sets the resource data of a specific resource.

Parameters
strResTypeNameIdentifies the resource type of the resource.
strResNameIdentifies the resource.
dataThe new resource data.

◆ setResourceData() [2/4]

void PeLib::ResourceDirectory::setResourceData ( const std::string &  strResTypeName,
std::uint32_t  dwResId,
std::vector< std::uint8_t > &  data 
)

Sets the data of a certain resource.

Sets the resource data of a specific resource.

Parameters
strResTypeNameIdentifies the resource type of the resource.
dwResIdIdentifies the resource.
dataThe new resource data.

◆ setResourceData() [3/4]

void PeLib::ResourceDirectory::setResourceData ( std::uint32_t  dwResTypeId,
const std::string &  strResName,
std::vector< std::uint8_t > &  data 
)

Sets the data of a certain resource.

Sets the resource data of a specific resource.

Parameters
dwResTypeIdIdentifies the resource type of the resource.
strResNameIdentifies the resource.
dataThe new resource data.

◆ setResourceData() [4/4]

void PeLib::ResourceDirectory::setResourceData ( std::uint32_t  dwResTypeId,
std::uint32_t  dwResId,
std::vector< std::uint8_t > &  data 
)

Sets the data of a certain resource.

Sets the resource data of a specific resource.

Parameters
dwResTypeIdIdentifies the resource type of the resource.
dwResIdIdentifies the resource.
dataThe new resource data.

◆ setResourceDataByIndex()

void PeLib::ResourceDirectory::setResourceDataByIndex ( unsigned int  uiResTypeIndex,
unsigned int  uiResIndex,
std::vector< std::uint8_t > &  data 
)

Sets the data of a certain resource.

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.

Parameters
uiResTypeIndexIdentifies the resource type of the resource.
uiResIndexIdentifies the resource.
dataThe new resource data.

◆ setResourceDataT()

template<typename S , typename T >
int PeLib::ResourceDirectory::setResourceDataT ( restypeid,
resid,
std::vector< std::uint8_t > &  data 
)
protected

Sets the data of a resource.

Sets the data of a resource, resource type and ID are specified by the parameters.

Parameters
restypeidIdentifier of the resource type (either ID or name).
residIdentifier of the resource (either ID or name).
dataThe new data of the resource is taken from this vector.

◆ setResourceId() [1/4]

void PeLib::ResourceDirectory::setResourceId ( const std::string &  strResTypeName,
const std::string &  strResName,
std::uint32_t  dwNewResId 
)

Sets the ID of a certain resource.

Sets the ID of a specific resource.

Parameters
strResTypeNameIdentifies the resource type of the resource.
strResNameIdentifies the resource.
dwNewResIdNew ID of the resource.

◆ setResourceId() [2/4]

void PeLib::ResourceDirectory::setResourceId ( const std::string &  strResTypeName,
std::uint32_t  dwResId,
std::uint32_t  dwNewResId 
)

Sets the ID of a certain resource.

Sets the ID of a specific resource.

Parameters
strResTypeNameIdentifies the resource type of the resource.
dwResIdIdentifies the resource.
dwNewResIdNew ID of the resource.

◆ setResourceId() [3/4]

void PeLib::ResourceDirectory::setResourceId ( std::uint32_t  dwResTypeId,
const std::string &  strResName,
std::uint32_t  dwNewResId 
)

Sets the ID of a certain resource.

Sets the ID of a specific resource.

Parameters
dwResTypeIdIdentifies the resource type of the resource.
strResNameIdentifies the resource.
dwNewResIdNew ID of the resource.

◆ setResourceId() [4/4]

void PeLib::ResourceDirectory::setResourceId ( std::uint32_t  dwResTypeId,
std::uint32_t  dwResId,
std::uint32_t  dwNewResId 
)

Sets the ID of a certain resource.

Sets the ID of a specific resource.

Parameters
dwResTypeIdIdentifies the resource type of the resource.
dwResIdIdentifies the resource.
dwNewResIdNew ID of the resource.

◆ setResourceIdByIndex()

void PeLib::ResourceDirectory::setResourceIdByIndex ( unsigned int  uiResTypeIndex,
unsigned int  uiResIndex,
std::uint32_t  dwNewResId 
)

Sets the ID of a certain resource.

Sets the ID of a specific resource by index.

Parameters
uiResTypeIndexIdentifies the resource type of the resource.
uiResIndexIdentifies the resource.
dwNewResIdNew ID of the specified resource.

◆ setResourceIdT()

template<typename S , typename T >
int PeLib::ResourceDirectory::setResourceIdT ( restypeid,
resid,
std::uint32_t  dwNewResId 
)
protected

Sets the ID of a resource.

Sets the id of a resource, resource type and ID are specified by the parameters.

Parameters
restypeidIdentifier of the resource type (either ID or name).
residIdentifier of the resource (either ID or name).
dwNewResIdNew ID of the resource.

◆ setResourceName() [1/4]

void PeLib::ResourceDirectory::setResourceName ( const std::string &  strResTypeName,
const std::string &  strResName,
const std::string &  strNewResName 
)

Sets the name of a certain resource.

Sets the name of a specific resource.

Parameters
strResTypeNameIdentifies the resource type of the resource.
strResNameIdentifies the resource.
strNewResNameNew name of the specified resource.

◆ setResourceName() [2/4]

void PeLib::ResourceDirectory::setResourceName ( const std::string &  strResTypeName,
std::uint32_t  dwResId,
const std::string &  strNewResName 
)

Sets the name of a certain resource.

Sets the name of a specific resource.

Parameters
strResTypeNameIdentifies the resource type of the resource.
dwResIdIdentifies the resource.
strNewResNameNew name of the specified resource.

◆ setResourceName() [3/4]

void PeLib::ResourceDirectory::setResourceName ( std::uint32_t  dwResTypeId,
const std::string &  strResName,
const std::string &  strNewResName 
)

Sets the name of a certain resource.

Sets the name of a specific resource.

Parameters
dwResTypeIdIdentifies the resource type of the resource.
strResNameIdentifies the resource.
strNewResNameNew name of the specified resource.

◆ setResourceName() [4/4]

void PeLib::ResourceDirectory::setResourceName ( std::uint32_t  dwResTypeId,
std::uint32_t  dwResId,
const std::string &  strNewResName 
)

Sets the name of a certain resource.

Sets the name of a specific resource.

Parameters
dwResTypeIdIdentifies the resource type of the resource.
dwResIdIdentifies the resource.
strNewResNameNew name of the specified resource.

◆ setResourceNameByIndex()

void PeLib::ResourceDirectory::setResourceNameByIndex ( unsigned int  uiResTypeIndex,
unsigned int  uiResIndex,
const std::string &  strNewResName 
)

Sets the name of a certain resource.

Sets the name of a specific resource by index.

Parameters
uiResTypeIndexIdentifies the resource type of the resource.
uiResIndexIdentifies the resource.
strNewResNameNew name of the specified resource.

◆ setResourceNameT()

template<typename S , typename T >
int PeLib::ResourceDirectory::setResourceNameT ( restypeid,
resid,
std::string  strNewResName 
)
protected

Sets the name of a resource.

Sets the name of a resource, resource type and ID are specified by the parameters.

Parameters
restypeidIdentifier of the resource type (either ID or name).
residIdentifier of the resource (either ID or name).
strNewResNameThe new name of the resource.

◆ write()

int PeLib::ResourceDirectory::write ( const std::string &  strFilename,
unsigned int  uiOffset,
unsigned int  uiRva 
) const

Returns the size of the rebuilt resource directory.

Writes the resource directory to a file.

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. Writes the current resource directory back into a file.

Parameters
strFilenameName of the output file.
uiOffsetFile offset where the resource directory will be written to.
uiRvaRVA of the file offset.

Member Data Documentation

◆ m_ldrError

LoaderError PeLib::ResourceDirectory::m_ldrError
protected

Error detected by the import table parser.

◆ m_occupiedAddresses

std::vector<std::pair<unsigned int, unsigned int> > PeLib::ResourceDirectory::m_occupiedAddresses
protected

Stores RVAs which are occupied by this export directory.

◆ m_readOffset

unsigned int PeLib::ResourceDirectory::m_readOffset
protected

Start offset of directory in file.

◆ m_resourceNodeOffsets

std::set<std::size_t> PeLib::ResourceDirectory::m_resourceNodeOffsets
protected

Detection of invalid structure of nodes in directory.

◆ m_rnRoot

ResourceNode PeLib::ResourceDirectory::m_rnRoot
protected

The root node of the resource directory.


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