retdec
ResourceDirectory.h
Go to the documentation of this file.
1 /*
2 * ResourceDirectory.cpp - Part of the PeLib library.
3 *
4 * Copyright (c) 2004 - 2005 Sebastian Porst (webmaster@the-interweb.com)
5 * All rights reserved.
6 *
7 * This software is licensed under the zlib/libpng License.
8 * For more details see http://www.opensource.org/licenses/zlib-license.php
9 * or the license information file (license.htm) in the root directory
10 * of PeLib.
11 */
12 
13 #ifndef RETDEC_PELIB_RESOURCEDIRECTORY_H
14 #define RETDEC_PELIB_RESOURCEDIRECTORY_H
15 
16 #include <set>
17 
18 #include "retdec/pelib/PeLibInc.h"
20 
21 namespace PeLib
22 {
23  class ResourceElement;
24  class ResourceDirectory;
25 
28  {
29  friend class ResourceElement;
30  friend class ResourceDirectory;
31  friend class ResourceNode;
32  friend class ResourceLeaf;
33  friend class ResourceDirectory;
34 
39 
40  public:
42  bool equalId(std::uint32_t wId) const; // EXPORT
44  bool equalName(std::string strName) const; // EXPORT
46  bool isNamedResource() const; // EXPORT
48  bool operator<(const ResourceChild& rc) const; // EXPORT
49 
51  bool hasEqual(std::uint32_t id) const { return equalId(id); }
53  bool hasEqual(const std::string& name) const { return equalName(name); }
54 
56  unsigned int getNumberOfChildren() const; // EXPORT
58  ResourceChild* getChildOfThisChild(std::size_t uiIndex); // EXPORT
59  const ResourceChild* getChildOfThisChild(std::size_t uiIndex) const; // EXPORT
60 
63  const ResourceElement* getNode() const;
65  void setNode(ResourceElement* node);
66 
68  std::string getName() const; // EXPORT
70  std::uint32_t getOffsetToName() const; // EXPORT
72  std::uint32_t getOffsetToData() const; // EXPORT
73 
75  void setName(const std::string& strNewName); // EXPORT
77  void setOffsetToName(std::uint32_t dwNewOffset); // EXPORT
79  void setOffsetToData(std::uint32_t dwNewOffset); // EXPORT
80 
82 // unsigned int size() const;
83 
85  ResourceChild();
87  ResourceChild(const ResourceChild& rhs);
92  };
93 
97  {
98  friend class ResourceChild;
99  friend class ResourceNode;
100  friend class ResourceLeaf;
101 
102  protected:
104  unsigned int uiElementRva;
105 
107  virtual int read(ImageLoader & imageLoader, std::uint32_t, std::uint32_t, std::uint32_t, ResourceDirectory* resDir) = 0;
109  virtual void rebuild(OutputBuffer&, unsigned int, unsigned int, const std::string&) const = 0;
111  virtual void recalculate(unsigned int& uiCurrentOffset, unsigned int uiNewRva) = 0;
112 
113  public:
115  unsigned int getElementRva() const; // EXPORT
117  virtual bool isLeaf() const = 0; // EXPORT
119  virtual void makeValid() = 0; // EXPORT
121 // virtual unsigned int size() const = 0;
123  ResourceElement();
125  virtual ~ResourceElement() {}
126  };
127 
130  {
131  friend class ResourceChild;
132  friend class ResourceDirectory;
133  template <typename T> friend struct fixNumberOfEntries;
134  friend class ResourceDirectory;
135 
136  private:
138  std::vector<std::uint8_t> m_data;
141 
142  protected:
143  int read(ImageLoader & imageLoader, std::uint32_t uiRsrcRva, std::uint32_t uiOffset, std::uint32_t sizeOfImage, ResourceDirectory* resDir);
145  void rebuild(OutputBuffer&, unsigned int uiOffset, unsigned int uiRva, const std::string&) const;
147  virtual void recalculate(unsigned int& uiCurrentOffset, unsigned int uiNewRva) override;
148 
149  public:
151  bool isLeaf() const; // EXPORT
153  void makeValid(); // EXPORT
156 // unsigned int size() const;
157 
159  std::vector<std::uint8_t> getData() const; // EXPORT
161  void setData(const std::vector<std::uint8_t>& vData); // EXPORT
162 
164  std::uint32_t getOffsetToData() const; // EXPORT
166  std::uint32_t getSize() const; // EXPORT
168  std::uint32_t getCodePage() const; // EXPORT
170  std::uint32_t getReserved() const; // EXPORT
171 
173  void setOffsetToData(std::uint32_t dwValue); // EXPORT
175  void setSize(std::uint32_t dwValue); // EXPORT
177  void setCodePage(std::uint32_t dwValue); // EXPORT
179  void setReserved(std::uint32_t dwValue); // EXPORT
181  ResourceLeaf();
183  virtual ~ResourceLeaf() override;
184  };
185 
188  {
189  friend class ResourceChild;
190  friend class ResourceDirectory;
191  template <typename T> friend struct fixNumberOfEntries;
192  friend class ResourceDirectory;
193 
195  std::vector<ResourceChild> children;
198 
199  protected:
201  int read(ImageLoader & imageLoader, std::uint32_t uiRsrcRva, std::uint32_t uiOffset, std::uint32_t sizeOfImage, ResourceDirectory* resDir);
203  void rebuild(OutputBuffer&, unsigned int uiOffset, unsigned int uiRva, const std::string&) const;
205  virtual void recalculate(unsigned int& uiCurrentOffset, unsigned int uiNewRva) override;
206 
207  public:
209  bool isLeaf() const; // EXPORT
211  void makeValid(); // EXPORT
212 
214  unsigned int getNumberOfChildren() const; // EXPORT
216  ResourceChild* addChild(); // EXPORT
218  ResourceChild* getChild(std::size_t uiIndex); // EXPORT
219  const ResourceChild* getChild(std::size_t uiIndex) const; // EXPORT
221  void removeChild(unsigned int uiIndex); // EXPORT
222 
224  std::string getChildName(unsigned int uiIndex) const; // EXPORT
226  std::uint32_t getOffsetToChildName(unsigned int uiIndex) const; // EXPORT
228  std::uint32_t getOffsetToChildData(unsigned int uiIndex) const; // EXPORT
229 
231  void setChildName(unsigned int uiIndex, const std::string& strNewName); // EXPORT
233  void setOffsetToChildName(unsigned int uiIndex, std::uint32_t dwNewOffset); // EXPORT
235  void setOffsetToChildData(unsigned int uiIndex, std::uint32_t dwNewOffset); // EXPORT
236 
238  std::uint32_t getCharacteristics() const; // EXPORT
240  std::uint32_t getTimeDateStamp() const; // EXPORT
242  std::uint16_t getMajorVersion() const; // EXPORT
244  std::uint16_t getMinorVersion() const; // EXPORT
246  std::uint16_t getNumberOfNamedEntries() const; // EXPORT
248  std::uint16_t getNumberOfIdEntries() const; // EXPORT
249 
251  void setCharacteristics(std::uint32_t value); // EXPORT
253  void setTimeDateStamp(std::uint32_t value); // EXPORT
255  void setMajorVersion(std::uint16_t value); // EXPORT
257  void setMinorVersion(std::uint16_t value); // EXPORT
259  void setNumberOfNamedEntries(std::uint16_t value); // EXPORT
261  void setNumberOfIdEntries(std::uint16_t value); // EXPORT
262 
264 // unsigned int size() const;
265 
267  ResourceNode();
269  virtual ~ResourceNode() override;
270  };
271 
273  template<typename T>
275  {
277  static void fix(ResourceNode*);
278  };
279 
281  template<>
282  struct fixNumberOfEntries<std::uint32_t>
283  {
285  static void fix(ResourceNode* node)
286  {
287  node->header.NumberOfIdEntries = static_cast<std::uint16_t>(
288  node->children.size() - std::count_if(
289  node->children.begin(),
290  node->children.end(),
291  [](const auto& i) { return i.isNamedResource(); }
292  )
293  );
294  }
295  };
296 
298  template<>
299  struct fixNumberOfEntries<std::string>
300  {
302  static void fix(ResourceNode* node)
303  {
304  node->header.NumberOfNamedEntries = static_cast<std::uint16_t>(
305  std::count_if(
306  node->children.begin(),
307  node->children.end(),
308  [](const auto& i) { return i.isNamedResource(); }
309  )
310  );
311  }
312  };
313 
315 
342  {
343  protected:
345  unsigned int m_readOffset;
349  std::set<std::size_t> m_resourceNodeOffsets;
351  std::vector<std::pair<unsigned int, unsigned int>> m_occupiedAddresses;
354 
355  // Prepare for some crazy syntax below to make Digital Mars happy.
356 
358  template<typename S, typename T>
359  std::vector<ResourceChild>::const_iterator locateResourceT(S restypeid, T resid) const;
360 
362  template<typename S, typename T>
363  std::vector<ResourceChild>::iterator locateResourceT(S restypeid, T resid);
364 
366  template<typename S, typename T>
367  int addResourceT(S restypeid, T resid, ResourceChild& rc);
368 
370  template<typename S, typename T>
371  int removeResourceT(S restypeid, T resid);
372 
374  template<typename S, typename T>
375  int getResourceDataT(S restypeid, T resid, std::vector<std::uint8_t>& data) const;
376 
378  template<typename S, typename T>
379  int setResourceDataT(S restypeid, T resid, std::vector<std::uint8_t>& data);
380 
382  template<typename S, typename T>
383  std::uint32_t getResourceIdT(S restypeid, T resid) const;
384 
386  template<typename S, typename T>
387  int setResourceIdT(S restypeid, T resid, std::uint32_t dwNewResId);
388 
390  template<typename S, typename T>
391  std::string getResourceNameT(S restypeid, T resid) const;
392 
394  template<typename S, typename T>
395  int setResourceNameT(S restypeid, T resid, std::string strNewResName);
396 
397  public:
401  virtual ~ResourceDirectory() = default;
402 
404  int read(ImageLoader & imageLoader);
405 
407  const ResourceNode* getRoot() const;
408 
410  LoaderError loaderError() const;
411  void setLoaderError(LoaderError ldrError);
412 
414  void makeValid();
416  void rebuild(std::vector<std::uint8_t>& vBuffer, unsigned int uiRva) const;
418  void recalculate(unsigned int& uiNewSize, unsigned int uiNewRva);
420 // unsigned int size() const;
422  int write(const std::string& strFilename, unsigned int uiOffset, unsigned int uiRva) const;
423 
425  int addResourceType(std::uint32_t dwResTypeId);
427  int addResourceType(const std::string& strResTypeName);
428 
430  int removeResourceType(std::uint32_t dwResTypeId);
432  int removeResourceType(const std::string& strResTypeName);
433 
435  int removeResourceTypeByIndex(unsigned int uiIndex);
436 
438  int addResource(std::uint32_t dwResTypeId, std::uint32_t dwResId);
440  int addResource(std::uint32_t dwResTypeId, const std::string& strResName);
442  int addResource(const std::string& strResTypeName, std::uint32_t dwResId);
444  int addResource(const std::string& strResTypeName, const std::string& strResName);
445 
447  int removeResource(std::uint32_t dwResTypeId, std::uint32_t dwResId);
449  int removeResource(std::uint32_t dwResTypeId, const std::string& strResName);
451  int removeResource(const std::string& strResTypeName, std::uint32_t dwResId);
453  int removeResource(const std::string& strResTypeName, const std::string& strResName);
454 
456  unsigned int getOffset() const;
457 
459  unsigned int getNumberOfResourceTypes() const;
460 
462  std::uint32_t getResourceTypeIdByIndex(unsigned int uiIndex) const;
464  std::string getResourceTypeNameByIndex(unsigned int uiIndex) const;
465 
467  int resourceTypeIdToIndex(std::uint32_t dwResTypeId) const;
469  int resourceTypeNameToIndex(const std::string& strResTypeName) const;
470 
472  unsigned int getNumberOfResources(std::uint32_t dwId) const;
474  unsigned int getNumberOfResources(const std::string& strResTypeName) const;
475 
477  unsigned int getNumberOfResourcesByIndex(unsigned int uiIndex) const;
478 
480  void getResourceData(std::uint32_t dwResTypeId, std::uint32_t dwResId, std::vector<std::uint8_t>& data) const;
482  void getResourceData(std::uint32_t dwResTypeId, const std::string& strResName, std::vector<std::uint8_t>& data) const;
484  void getResourceData(const std::string& strResTypeName, std::uint32_t dwResId, std::vector<std::uint8_t>& data) const;
486  void getResourceData(const std::string& strResTypeName, const std::string& strResName, std::vector<std::uint8_t>& data) const;
487 
489  void getResourceDataByIndex(unsigned int uiResTypeIndex, unsigned int uiResIndex, std::vector<std::uint8_t>& data) const;
490 
492  void setResourceData(std::uint32_t dwResTypeId, std::uint32_t dwResId, std::vector<std::uint8_t>& data);
494  void setResourceData(std::uint32_t dwResTypeId, const std::string& strResName, std::vector<std::uint8_t>& data);
496  void setResourceData(const std::string& strResTypeName, std::uint32_t dwResId, std::vector<std::uint8_t>& data);
498  void setResourceData(const std::string& strResTypeName, const std::string& strResName, std::vector<std::uint8_t>& data);
499 
501  void setResourceDataByIndex(unsigned int uiResTypeIndex, unsigned int uiResIndex, std::vector<std::uint8_t>& data);
502 
504  std::uint32_t getResourceId(std::uint32_t dwResTypeId, const std::string& strResName) const;
506  std::uint32_t getResourceId(const std::string& strResTypeName, const std::string& strResName) const;
507 
509  std::uint32_t getResourceIdByIndex(unsigned int uiResTypeIndex, unsigned int uiResIndex) const;
510 
512  void setResourceId(std::uint32_t dwResTypeId, std::uint32_t dwResId, std::uint32_t dwNewResId);
514  void setResourceId(std::uint32_t dwResTypeId, const std::string& strResName, std::uint32_t dwNewResId);
516  void setResourceId(const std::string& strResTypeName, std::uint32_t dwResId, std::uint32_t dwNewResId);
518  void setResourceId(const std::string& strResTypeName, const std::string& strResName, std::uint32_t dwNewResId);
519 
521  void setResourceIdByIndex(unsigned int uiResTypeIndex, unsigned int uiResIndex, std::uint32_t dwNewResId);
522 
524  std::string getResourceName(std::uint32_t dwResTypeId, std::uint32_t dwResId) const;
526  std::string getResourceName(const std::string& strResTypeName, std::uint32_t dwResId) const;
527 
529  std::string getResourceNameByIndex(unsigned int uiResTypeIndex, unsigned int uiResIndex) const;
530 
532  void setResourceName(std::uint32_t dwResTypeId, std::uint32_t dwResId, const std::string& strNewResName);
534  void setResourceName(std::uint32_t dwResTypeId, const std::string& strResName, const std::string& strNewResName);
536  void setResourceName(const std::string& strResTypeName, std::uint32_t dwResId, const std::string& strNewResName);
538  void setResourceName(const std::string& strResTypeName, const std::string& strResName, const std::string& strNewResName);
539 
541  void setResourceNameByIndex(unsigned int uiResTypeIndex, unsigned int uiResIndex, const std::string& strNewResName);
542 
544  void insertNodeOffset(std::size_t nodeOffset);
546  bool hasNodeOffset(std::size_t nodeOffset) const;
547 
548  void addOccupiedAddressRange(unsigned int start, unsigned int end);
549  const std::vector<std::pair<unsigned int, unsigned int>>& getOccupiedAddresses() const;
550  };
551 
559  template<typename S, typename T>
560  std::vector<ResourceChild>::const_iterator ResourceDirectory::locateResourceT(S restypeid, T resid) const
561  {
562  auto Iter = std::find_if(
563  m_rnRoot.children.begin(),
564  m_rnRoot.children.end(),
565  [&](const auto& res) { return res.hasEqual(restypeid); }
566  );
567  if (Iter == m_rnRoot.children.end())
568  {
569  return Iter;
570  }
571 
572  ResourceNode* currNode = static_cast<ResourceNode*>(Iter->child);
573  return std::find_if(
574  currNode->children.begin(),
575  currNode->children.end(),
576  [&](const auto& res) { return res.hasEqual(resid); }
577  );
578  }
579 
587  template<typename S, typename T>
588  std::vector<ResourceChild>::iterator ResourceDirectory::locateResourceT(S restypeid, T resid)
589  {
590  auto Iter = std::find_if(
591  m_rnRoot.children.begin(),
592  m_rnRoot.children.end(),
593  [&](const auto& res) { return res.hasEqual(restypeid); }
594  );
595  if (Iter == m_rnRoot.children.end())
596  {
597  return Iter;
598  }
599 
600  ResourceNode* currNode = static_cast<ResourceNode*>(Iter->child);
601  return std::find_if(
602  currNode->children.begin(),
603  currNode->children.end(),
604  [&](const auto& res) { return res.hasEqual(resid); }
605  );
606  }
607 
614  template<typename S, typename T>
615  int ResourceDirectory::addResourceT(S restypeid, T resid, ResourceChild& rc)
616  {
617  auto Iter = std::find_if(
618  m_rnRoot.children.begin(),
619  m_rnRoot.children.end(),
620  [&](const auto& res) { return res.hasEqual(restypeid); }
621  );
622  if (Iter == m_rnRoot.children.end())
623  {
624  return ERROR_ENTRY_NOT_FOUND;
625  // throw Exceptions::ResourceTypeDoesNotExist(ResourceDirectoryId, __LINE__);
626  }
627 
628  ResourceNode* currNode = static_cast<ResourceNode*>(Iter->child);
629  auto ResIter = std::find_if(
630  currNode->children.begin(),
631  currNode->children.end(),
632  [&](const auto& res) { return res.hasEqual(resid); }
633  );
634  if (ResIter != currNode->children.end())
635  {
636  return ERROR_DUPLICATE_ENTRY;
637 // throw Exceptions::EntryAlreadyExists(ResourceDirectoryId, __LINE__);
638  }
639 
640  rc.child = new ResourceNode;
641  ResourceChild rlnew;
642  rlnew.child = new ResourceLeaf;
643  ResourceNode* currNode2 = static_cast<ResourceNode*>(rc.child);
644  currNode2->children.push_back(rlnew);
645  currNode->children.push_back(rc);
646 
647  fixNumberOfEntries<T>::fix(currNode);
648  fixNumberOfEntries<T>::fix(currNode2);
649 
650  return ERROR_NONE;
651  }
652 
658  template<typename S, typename T>
659  int ResourceDirectory::removeResourceT(S restypeid, T resid)
660  {
661  auto Iter = std::find_if(
662  m_rnRoot.children.begin(),
663  m_rnRoot.children.end(),
664  [&](const auto& res) { return res.hasEqual(restypeid); }
665  );
666  if (Iter == m_rnRoot.children.end())
667  {
668  return ERROR_ENTRY_NOT_FOUND;
669  //throw Exceptions::ResourceTypeDoesNotExist(ResourceDirectoryId, __LINE__);
670  }
671 
672  ResourceNode* currNode = static_cast<ResourceNode*>(Iter->child);
673  auto ResIter = std::find_if(
674  currNode->children.begin(),
675  currNode->children.end(),
676  [&](const auto& res) { return res.hasEqual(resid); }
677  );
678  if (ResIter == currNode->children.end())
679  {
680  return ERROR_ENTRY_NOT_FOUND;
681  // throw Exceptions::InvalidName(ResourceDirectoryId, __LINE__);
682  }
683 
684  currNode->children.erase(ResIter);
685 
686  fixNumberOfEntries<T>::fix(currNode);
687 
688  return ERROR_NONE;
689  }
690 
697  template<typename S, typename T>
698  int ResourceDirectory::getResourceDataT(S restypeid, T resid, std::vector<std::uint8_t>& data) const
699  {
700  std::vector<ResourceChild>::const_iterator ResIter = locateResourceT(restypeid, resid);
701  ResourceNode* currNode = static_cast<ResourceNode*>(ResIter->child);
702  ResourceLeaf* currLeaf = static_cast<ResourceLeaf*>(currNode->children[0].child);
703  data.assign(currLeaf->m_data.begin(), currLeaf->m_data.end());
704 
705  return ERROR_NONE;
706  }
707 
714  template<typename S, typename T>
715  int ResourceDirectory::setResourceDataT(S restypeid, T resid, std::vector<std::uint8_t>& data)
716  {
717  std::vector<ResourceChild>::iterator ResIter = locateResourceT(restypeid, resid);
718  ResourceNode* currNode = static_cast<ResourceNode*>(ResIter->child);
719  ResourceLeaf* currLeaf = static_cast<ResourceLeaf*>(currNode->children[0].child);
720  currLeaf->m_data.assign(data.begin(), data.end());
721 
722  return ERROR_NONE;
723  }
724 
732  template<typename S, typename T>
733  std::uint32_t ResourceDirectory::getResourceIdT(S restypeid, T resid) const
734  {
735  std::vector<ResourceChild>::const_iterator ResIter = locateResourceT(restypeid, resid);
736  return ResIter->entry.irde.Name;
737  }
738 
745  template<typename S, typename T>
746  int ResourceDirectory::setResourceIdT(S restypeid, T resid, std::uint32_t dwNewResId)
747  {
748  std::vector<ResourceChild>::iterator ResIter = locateResourceT(restypeid, resid);
749  ResIter->entry.irde.Name = dwNewResId;
750  return ERROR_NONE;
751  }
752 
760  template<typename S, typename T>
761  std::string ResourceDirectory::getResourceNameT(S restypeid, T resid) const
762  {
763  std::vector<ResourceChild>::const_iterator ResIter = locateResourceT(restypeid, resid);
764  return ResIter->entry.wstrName;
765  }
766 
773  template<typename S, typename T>
774  int ResourceDirectory::setResourceNameT(S restypeid, T resid, std::string strNewResName)
775  {
776  std::vector<ResourceChild>::iterator ResIter = locateResourceT(restypeid, resid);
777  ResIter->entry.wstrName = strNewResName;
778 
779  return ERROR_NONE;
780  }
781 }
782 
783 #endif
Definition: ImageLoader.h:138
Definition: OutputBuffer.h:22
The class ResourceChild is used to store information about a resource node.
Definition: ResourceDirectory.h:28
void setName(const std::string &strNewName)
Sets the name of the node.
Definition: ResourceDirectory.cpp:243
void setNode(ResourceElement *node)
Sets a pointer to ResourceElement.
Definition: ResourceDirectory.cpp:203
ResourceChild * getChildOfThisChild(std::size_t uiIndex)
Returns a child of this child.
Definition: ResourceDirectory.cpp:153
ResourceChild()
Returns the size of a resource child.
Definition: ResourceDirectory.cpp:20
bool hasEqual(const std::string &name) const
A comparison function for searching a resource element by its name.
Definition: ResourceDirectory.h:53
bool hasEqual(std::uint32_t id) const
A comparison function for searching a resource element by its ID.
Definition: ResourceDirectory.h:51
bool isNamedResource() const
Predicate that determines if a child is identified by name or by ID.
Definition: ResourceDirectory.cpp:107
bool equalName(std::string strName) const
Function which compares a string to the node's resource name.
Definition: ResourceDirectory.cpp:99
unsigned int getNumberOfChildren() const
Returns the node's number of children.
Definition: ResourceDirectory.cpp:139
~ResourceChild()
Deletes a ResourceChild object.
Definition: ResourceDirectory.cpp:79
bool equalId(std::uint32_t wId) const
Function which compares a resource ID to the node's resource ID.
Definition: ResourceDirectory.cpp:89
ResourceElement * getNode()
Returns a pointer to ResourceElement.
Definition: ResourceDirectory.cpp:183
void setOffsetToData(std::uint32_t dwNewOffset)
Sets the OffsetToData value of the node.
Definition: ResourceDirectory.cpp:263
void setOffsetToName(std::uint32_t dwNewOffset)
Sets the Name value of the node.
Definition: ResourceDirectory.cpp:253
ResourceChild & operator=(const ResourceChild &rhs)
Makes a deep copy of a ResourceChild object.
Definition: ResourceDirectory.cpp:49
bool operator<(const ResourceChild &rc) const
Used for sorting a node's children.
Definition: ResourceDirectory.cpp:116
PELIB_IMG_RES_DIR_ENTRY entry
Stores name and offset of a resource node.
Definition: ResourceDirectory.h:36
std::uint32_t getOffsetToData() const
Returns the OffsetToData value of the node.
Definition: ResourceDirectory.cpp:233
std::string getName() const
Returns the name of the node.
Definition: ResourceDirectory.cpp:213
ResourceElement * child
A pointer to one of the node's child nodes.
Definition: ResourceDirectory.h:38
std::uint32_t getOffsetToName() const
Returns the Name value of the node.
Definition: ResourceDirectory.cpp:223
Class that represents the resource directory of a PE file.
Definition: ResourceDirectory.h:342
int removeResource(std::uint32_t dwResTypeId, std::uint32_t dwResId)
Removes a resource.
Definition: ResourceDirectory.cpp:1325
int getResourceDataT(S restypeid, T resid, std::vector< std::uint8_t > &data) const
Returns the data of a resource.
Definition: ResourceDirectory.h:698
ResourceDirectory()
Constructor.
Definition: ResourceDirectory.cpp:1011
void setResourceId(std::uint32_t dwResTypeId, std::uint32_t dwResId, std::uint32_t dwNewResId)
Sets the ID of a certain resource.
Definition: ResourceDirectory.cpp:1665
unsigned int getNumberOfResourcesByIndex(unsigned int uiIndex) const
Returns the number of resources of a certain resource type.
Definition: ResourceDirectory.cpp:1496
int write(const std::string &strFilename, unsigned int uiOffset, unsigned int uiRva) const
Returns the size of the rebuilt resource directory.
Definition: ResourceDirectory.cpp:1112
void setResourceIdByIndex(unsigned int uiResTypeIndex, unsigned int uiResIndex, std::uint32_t dwNewResId)
Sets the ID of a certain resource.
Definition: ResourceDirectory.cpp:1709
std::uint32_t getResourceIdT(S restypeid, T resid) const
Returns the ID of a resource.
Definition: ResourceDirectory.h:733
std::string getResourceName(std::uint32_t dwResTypeId, std::uint32_t dwResId) const
Returns the name of a certain resource.
Definition: ResourceDirectory.cpp:1721
int addResourceType(std::uint32_t dwResTypeId)
Adds a new resource type.
Definition: ResourceDirectory.cpp:1148
std::uint32_t getResourceTypeIdByIndex(unsigned int uiIndex) const
Returns the ID of a resource type.
Definition: ResourceDirectory.cpp:1386
int setResourceNameT(S restypeid, T resid, std::string strNewResName)
Sets the name of a resource.
Definition: ResourceDirectory.h:774
void addOccupiedAddressRange(unsigned int start, unsigned int end)
Definition: ResourceDirectory.cpp:1823
int resourceTypeIdToIndex(std::uint32_t dwResTypeId) const
Converts a resource type ID to an index.
Definition: ResourceDirectory.cpp:1408
const std::vector< std::pair< unsigned int, unsigned int > > & getOccupiedAddresses() const
Definition: ResourceDirectory.cpp:1828
void insertNodeOffset(std::size_t nodeOffset)
Insert offset of loaded node.
Definition: ResourceDirectory.cpp:1809
unsigned int getNumberOfResourceTypes() const
Returns the number of resource types.
Definition: ResourceDirectory.cpp:1374
int read(ImageLoader &imageLoader)
Reads the resource directory from a file.
Definition: ResourceDirectory.cpp:1029
void rebuild(std::vector< std::uint8_t > &vBuffer, unsigned int uiRva) const
Rebuilds the resource directory.
Definition: ResourceDirectory.cpp:1077
void setLoaderError(LoaderError ldrError)
Definition: ResourceDirectory.cpp:1054
std::string getResourceNameByIndex(unsigned int uiResTypeIndex, unsigned int uiResIndex) const
Returns the name of a certain resource.
Definition: ResourceDirectory.cpp:1743
int removeResourceTypeByIndex(unsigned int uiIndex)
Removes a resource type and all of it's resources.
Definition: ResourceDirectory.cpp:1254
std::uint32_t getResourceIdByIndex(unsigned int uiResTypeIndex, unsigned int uiResIndex) const
Returns the ID of a certain resource.
Definition: ResourceDirectory.cpp:1653
void setResourceName(std::uint32_t dwResTypeId, std::uint32_t dwResId, const std::string &strNewResName)
Sets the name of a certain resource.
Definition: ResourceDirectory.cpp:1755
unsigned int getNumberOfResources(std::uint32_t dwId) const
Returns the number of resources of a certain resource type.
Definition: ResourceDirectory.cpp:1440
void setResourceData(std::uint32_t dwResTypeId, std::uint32_t dwResId, std::vector< std::uint8_t > &data)
Sets the data of a certain resource.
Definition: ResourceDirectory.cpp:1570
std::uint32_t getResourceId(std::uint32_t dwResTypeId, const std::string &strResName) const
Returns the ID of a certain resource.
Definition: ResourceDirectory.cpp:1631
int setResourceDataT(S restypeid, T resid, std::vector< std::uint8_t > &data)
Sets the data of a resource.
Definition: ResourceDirectory.h:715
void makeValid()
Corrects a erroneous resource directory.
Definition: ResourceDirectory.cpp:1067
LoaderError loaderError() const
Retrieve the loader error.
Definition: ResourceDirectory.cpp:1049
void recalculate(unsigned int &uiNewSize, unsigned int uiNewRva)
Recalculate the tree for different RVA.
Definition: ResourceDirectory.cpp:1091
LoaderError m_ldrError
Error detected by the import table parser.
Definition: ResourceDirectory.h:353
void getResourceDataByIndex(unsigned int uiResTypeIndex, unsigned int uiResIndex, std::vector< std::uint8_t > &data) const
Returns the data of a certain resource.
Definition: ResourceDirectory.cpp:1555
void getResourceData(std::uint32_t dwResTypeId, std::uint32_t dwResId, std::vector< std::uint8_t > &data) const
Returns the data of a certain resource.
Definition: ResourceDirectory.cpp:1508
int removeResourceType(std::uint32_t dwResTypeId)
Removes a resource type and all of it's resources.
Definition: ResourceDirectory.cpp:1198
int setResourceIdT(S restypeid, T resid, std::uint32_t dwNewResId)
Sets the ID of a resource.
Definition: ResourceDirectory.h:746
int removeResourceT(S restypeid, T resid)
Removes new resource.
Definition: ResourceDirectory.h:659
std::string getResourceTypeNameByIndex(unsigned int uiIndex) const
Returns the name of a resource type.
Definition: ResourceDirectory.cpp:1398
std::vector< std::pair< unsigned int, unsigned int > > m_occupiedAddresses
Stores RVAs which are occupied by this export directory.
Definition: ResourceDirectory.h:351
int resourceTypeNameToIndex(const std::string &strResTypeName) const
Converts a resource type name to an index.
Definition: ResourceDirectory.cpp:1424
virtual ~ResourceDirectory()=default
Destructor.
std::vector< ResourceChild >::const_iterator locateResourceT(S restypeid, T resid) const
Retrieves an iterator to a specified resource child.
Definition: ResourceDirectory.h:560
int addResource(std::uint32_t dwResTypeId, std::uint32_t dwResId)
Adds a new resource.
Definition: ResourceDirectory.cpp:1273
int addResourceT(S restypeid, T resid, ResourceChild &rc)
Adds a new resource.
Definition: ResourceDirectory.h:615
void setResourceNameByIndex(unsigned int uiResTypeIndex, unsigned int uiResIndex, const std::string &strNewResName)
Sets the name of a certain resource.
Definition: ResourceDirectory.cpp:1799
ResourceNode * getRoot()
Definition: ResourceDirectory.cpp:1020
unsigned int getOffset() const
Returns start offset of resource directory in file.
Definition: ResourceDirectory.cpp:1366
std::set< std::size_t > m_resourceNodeOffsets
Detection of invalid structure of nodes in directory.
Definition: ResourceDirectory.h:349
ResourceNode m_rnRoot
The root node of the resource directory.
Definition: ResourceDirectory.h:347
bool hasNodeOffset(std::size_t nodeOffset) const
Check if node with specified offset was loaded.
Definition: ResourceDirectory.cpp:1818
unsigned int m_readOffset
Start offset of directory in file.
Definition: ResourceDirectory.h:345
void setResourceDataByIndex(unsigned int uiResTypeIndex, unsigned int uiResIndex, std::vector< std::uint8_t > &data)
Sets the data of a certain resource.
Definition: ResourceDirectory.cpp:1617
std::string getResourceNameT(S restypeid, T resid) const
Returns the name of a resource.
Definition: ResourceDirectory.h:761
Definition: ResourceDirectory.h:97
unsigned int getElementRva() const
Returns the RVA of the element in the file.
Definition: ResourceDirectory.cpp:282
virtual void rebuild(OutputBuffer &, unsigned int, unsigned int, const std::string &) const =0
Writes the next resource element into the OutputBuffer.
virtual ~ResourceElement()
Necessary virtual destructor.
Definition: ResourceDirectory.h:125
virtual void recalculate(unsigned int &uiCurrentOffset, unsigned int uiNewRva)=0
Recalculates the tree for different RVA.
virtual void makeValid()=0
Corrects erroneous values in the ResourceElement.
ResourceElement()
Returns the size of a resource element.
Definition: ResourceDirectory.cpp:287
virtual int read(ImageLoader &imageLoader, std::uint32_t, std::uint32_t, std::uint32_t, ResourceDirectory *resDir)=0
Reads the next resource element from the InputBuffer.
unsigned int uiElementRva
Stores RVA of the resource element in the file.
Definition: ResourceDirectory.h:104
virtual bool isLeaf() const =0
Indicates if the resource element is a leaf or a node.
ResourceLeafs represent the leafs of the resource tree: The actual resources.
Definition: ResourceDirectory.h:130
void setOffsetToData(std::uint32_t dwValue)
Sets the OffsetToData value of this resource leaf.
Definition: ResourceDirectory.cpp:466
virtual ~ResourceLeaf() override
Destructor.
Definition: ResourceDirectory.cpp:503
std::vector< std::uint8_t > m_data
The resource data.
Definition: ResourceDirectory.h:138
void makeValid()
Corrects erroneous values in the ResourceLeaf.
Definition: ResourceDirectory.cpp:395
std::uint32_t getCodePage() const
Returns the CodePage value of this resource leaf.
Definition: ResourceDirectory.cpp:448
std::vector< std::uint8_t > getData() const
Returns the resource data of this resource leaf.
Definition: ResourceDirectory.cpp:411
std::uint32_t getReserved() const
Returns the Reserved value of this resource leaf.
Definition: ResourceDirectory.cpp:457
bool isLeaf() const
Indicates if the resource element is a leaf or a node.
Definition: ResourceDirectory.cpp:298
ResourceLeaf()
Constructor.
Definition: ResourceDirectory.cpp:498
PELIB_IMAGE_RESOURCE_DATA_ENTRY entry
PeLib equivalent of the Win32 structure IMAGE_RESOURCE_DATA_ENTRY.
Definition: ResourceDirectory.h:140
void setCodePage(std::uint32_t dwValue)
Sets the CodePage value of this resource leaf.
Definition: ResourceDirectory.cpp:484
std::uint32_t getOffsetToData() const
Returns the OffsetToData value of this resource leaf.
Definition: ResourceDirectory.cpp:430
std::uint32_t getSize() const
Returns the Size value of this resource leaf.
Definition: ResourceDirectory.cpp:439
void setData(const std::vector< std::uint8_t > &vData)
Sets the resource data of this resource leaf.
Definition: ResourceDirectory.cpp:420
int read(ImageLoader &imageLoader, std::uint32_t uiRsrcRva, std::uint32_t uiOffset, std::uint32_t sizeOfImage, ResourceDirectory *resDir)
Definition: ResourceDirectory.cpp:311
void setSize(std::uint32_t dwValue)
Sets the Size value of this resource leaf.
Definition: ResourceDirectory.cpp:475
virtual void recalculate(unsigned int &uiCurrentOffset, unsigned int uiNewRva) override
Recalculates the tree for different RVA.
Definition: ResourceDirectory.cpp:388
void rebuild(OutputBuffer &, unsigned int uiOffset, unsigned int uiRva, const std::string &) const
Writes the next resource leaf into the OutputBuffer.
Definition: ResourceDirectory.cpp:358
void setReserved(std::uint32_t dwValue)
Sets the Reserved value of this resource leaf.
Definition: ResourceDirectory.cpp:493
ResourceNodes represent the nodes in the resource tree.
Definition: ResourceDirectory.h:188
unsigned int getNumberOfChildren() const
Returns the node's number of children.
Definition: ResourceDirectory.cpp:766
std::uint32_t getTimeDateStamp() const
Returns the node's TimeDateStamp value.
Definition: ResourceDirectory.cpp:885
std::uint32_t getOffsetToChildName(unsigned int uiIndex) const
Returns the Name value of one of the node's children.
Definition: ResourceDirectory.cpp:827
void setOffsetToChildData(unsigned int uiIndex, std::uint32_t dwNewOffset)
Sets the OffsetToData value of one of the node's children.
Definition: ResourceDirectory.cpp:867
std::vector< ResourceChild > children
The node's children.
Definition: ResourceDirectory.h:195
void setTimeDateStamp(std::uint32_t value)
Sets the node's TimeDateStamp value.
Definition: ResourceDirectory.cpp:939
int read(ImageLoader &imageLoader, std::uint32_t uiRsrcRva, std::uint32_t uiOffset, std::uint32_t sizeOfImage, ResourceDirectory *resDir)
Reads the next resource node.
Definition: ResourceDirectory.cpp:632
std::uint32_t getCharacteristics() const
Returns the node's Characteristics value.
Definition: ResourceDirectory.cpp:876
void setCharacteristics(std::uint32_t value)
Sets the node's Characteristics value.
Definition: ResourceDirectory.cpp:930
std::string getChildName(unsigned int uiIndex) const
Returns the name of one of the node's children.
Definition: ResourceDirectory.cpp:817
ResourceNode()
Returns the size of a resource node.
Definition: ResourceDirectory.cpp:996
void rebuild(OutputBuffer &, unsigned int uiOffset, unsigned int uiRva, const std::string &) const
Writes the next resource node into the OutputBuffer.
Definition: ResourceDirectory.cpp:540
PELIB_IMAGE_RESOURCE_DIRECTORY header
The node's header. Equivalent to IMAGE_RESOURCE_DIRECTORY from the Win32 API.
Definition: ResourceDirectory.h:197
void removeChild(unsigned int uiIndex)
Removes a node's child.
Definition: ResourceDirectory.cpp:807
void setNumberOfNamedEntries(std::uint16_t value)
Sets the node's NumberOfNamedEntries value.
Definition: ResourceDirectory.cpp:966
std::uint32_t getOffsetToChildData(unsigned int uiIndex) const
Returns the OffsetToData value of one of the node's children.
Definition: ResourceDirectory.cpp:837
virtual void recalculate(unsigned int &uiCurrentOffset, unsigned int uiNewRva) override
Recalculates the tree for different RVA.
Definition: ResourceDirectory.cpp:595
std::uint16_t getNumberOfIdEntries() const
Returns the node's NumberOfIdEntries value.
Definition: ResourceDirectory.cpp:921
void setMajorVersion(std::uint16_t value)
Sets the node's MajorVersion value.
Definition: ResourceDirectory.cpp:948
bool isLeaf() const
Indicates if the resource element is a leaf or a node.
Definition: ResourceDirectory.cpp:514
void makeValid()
Corrects erroneous values in the ResourceNode.
Definition: ResourceDirectory.cpp:522
void setOffsetToChildName(unsigned int uiIndex, std::uint32_t dwNewOffset)
Sets the Name value of one of the node's children.
Definition: ResourceDirectory.cpp:857
ResourceChild * getChild(std::size_t uiIndex)
Returns a node's child.
Definition: ResourceDirectory.cpp:788
std::uint16_t getNumberOfNamedEntries() const
Returns the node's NumberOfNamedEntries value.
Definition: ResourceDirectory.cpp:912
void setNumberOfIdEntries(std::uint16_t value)
Sets the node's NumberOfIdEntries value.
Definition: ResourceDirectory.cpp:975
void setMinorVersion(std::uint16_t value)
Sets the node's MinorVersion value.
Definition: ResourceDirectory.cpp:957
ResourceChild * addChild()
Adds another child to node.
Definition: ResourceDirectory.cpp:775
std::uint16_t getMajorVersion() const
Returns the node's MajorVersion value.
Definition: ResourceDirectory.cpp:894
std::uint16_t getMinorVersion() const
Returns the node's MinorVersion value.
Definition: ResourceDirectory.cpp:903
void setChildName(unsigned int uiIndex, const std::string &strNewName)
Sets the name of one of the node's children.
Definition: ResourceDirectory.cpp:847
virtual ~ResourceNode() override
Destructor.
Definition: ResourceDirectory.cpp:1001
Definition: BoundImportDirectory.h:21
LoaderError
Definition: PeLibAux.h:47
@ ERROR_NONE
Definition: PeLibAux.h:33
@ ERROR_DUPLICATE_ENTRY
Definition: PeLibAux.h:41
@ ERROR_ENTRY_NOT_FOUND
Definition: PeLibAux.h:40
Definition: PeLibAux.h:1134
Definition: PeLibAux.h:972
std::uint16_t NumberOfIdEntries
Definition: PeLibAux.h:978
std::uint16_t NumberOfNamedEntries
Definition: PeLibAux.h:977
Definition: PeLibAux.h:998
static void fix(ResourceNode *node)
Fixes a resource node's NumberOfNamedEntries value.
Definition: ResourceDirectory.h:302
static void fix(ResourceNode *node)
Fixes a resource node's NumberOfIdEntries value.
Definition: ResourceDirectory.h:285
Unspecialized function that's used as base template for the specialized versions below.
Definition: ResourceDirectory.h:275
static void fix(ResourceNode *)
Fixes a resource node's header.