LIEF: Library to Instrument Executable Formats Version 0.17.0
Loading...
Searching...
No Matches
ResourcesManager.hpp
Go to the documentation of this file.
1/* Copyright 2017 - 2025 R. Thomas
2 * Copyright 2017 - 2025 Quarkslab
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16#ifndef LIEF_PE_RESOURCES_MANAGER_H
17#define LIEF_PE_RESOURCES_MANAGER_H
18#include <ostream>
19
20#include "LIEF/errors.hpp"
21#include "LIEF/visibility.h"
22#include "LIEF/Object.hpp"
23#include "LIEF/iterators.hpp"
24
30
31namespace LIEF {
32class VectorStream;
33
34namespace PE {
35class ResourceNode;
36class LIEF_API ResourcesManager : public Object {
39 public:
40 enum class TYPE {
44 CURSOR = 1,
45 BITMAP = 2,
46 ICON = 3,
47 MENU = 4,
48 DIALOG = 5,
49 STRING = 6,
50 FONTDIR = 7,
51 FONT = 8,
52 ACCELERATOR = 9,
53 RCDATA = 10,
54 MESSAGETABLE = 11,
55 GROUP_CURSOR = 12,
56 GROUP_ICON = 14,
57 VERSION = 16,
58 DLGINCLUDE = 17,
59 PLUGPLAY = 19,
60 VXD = 20,
61 ANICURSOR = 21,
62 ANIICON = 22,
63 HTML = 23,
64 MANIFEST = 24
65 };
66
67 public:
68 using dialogs_t = ResourceDialog::dialogs_t;
69 using it_const_dialogs = const_ref_iterator<const dialogs_t&, const ResourceDialog*>;
70
71 using icons_t = std::vector<ResourceIcon>;
72 using it_const_icons = const_ref_iterator<icons_t>;
73
74 using accelerators_t = std::vector<ResourceAccelerator>;
75 using it_const_accelerators = const_ref_iterator<accelerators_t>;
76 struct LIEF_API string_entry_t {
79 std::u16string string;
80 uint32_t id = 0;
81
82 std::string string_u8() const;
83
84 bool is_defined() const {
85 return !string.empty();
86 }
87
88 operator bool() const {
89 return is_defined();
90 }
91
92 friend LIEF_API
93 std::ostream& operator<<(std::ostream& os, const string_entry_t& str)
94 {
95 os << std::to_string(str.id) << ", " << str.string_u8();
96 return os;
97 }
98 };
99
100 using strings_table_t = std::vector<string_entry_t>;
101
102 ResourcesManager() = delete;
103 ResourcesManager(ResourceNode& rsrc) :
104 resources_{&rsrc}
105 {}
106
107 ResourcesManager(const ResourcesManager& other) :
108 Object(other),
109 resources_(other.resources_)
110 // Skip (on purpose) the `dialogs_` cache
111 {}
112
113 ResourcesManager& operator=(const ResourcesManager& other) {
114 if (&other != this) {
115 Object::operator=(other);
116 resources_ = other.resources_;
117 // Skip (on purpose) the `dialogs_` cache
118 }
119 return *this;
120 }
121
122 ResourcesManager(ResourcesManager&&) = default;
123 ResourcesManager& operator=(ResourcesManager&&) = default;
124
125 ~ResourcesManager() override = default;
126 ResourceNode* get_node_type(TYPE type) {
130 return const_cast<ResourceNode*>(
131 static_cast<const ResourcesManager*>(this)->get_node_type(type));
132 }
133 const ResourceNode* get_node_type(TYPE type) const;
134 std::vector<TYPE> get_types() const;
137 bool has_type(TYPE type) const {
140 return get_node_type(type) != nullptr;
141 }
142 bool has_manifest() const {
145 return get_node_type(TYPE::MANIFEST) != nullptr;
146 }
147 std::string manifest() const;
151 void manifest(const std::string& manifest);
155 bool has_version() const {
158 return get_node_type(TYPE::VERSION) != nullptr;
159 }
160 std::vector<ResourceVersion> version() const;
163 bool has_icons() const {
166 return get_node_type(TYPE::ICON) != nullptr &&
167 get_node_type(TYPE::GROUP_ICON) != nullptr;
168 }
169 it_const_icons icons() const;
172 void add_icon(const ResourceIcon& icon);
175
176 void change_icon(const ResourceIcon& original, const ResourceIcon& newone);
177 bool has_dialogs() const {
180 return get_node_type(TYPE::DIALOG) != nullptr;
181 }
182 it_const_dialogs dialogs() const;
185 bool has_string_table() const {
188 return get_node_type(TYPE::STRING) != nullptr;
189 }
190 strings_table_t string_table() const;
193 bool has_html() const {
196 return get_node_type(TYPE::HTML) != nullptr;
197 }
198 std::vector<std::string> html() const;
201 bool has_accelerator() const {
204 return get_node_type(TYPE::ACCELERATOR) != nullptr;
205 }
206 it_const_accelerators accelerator() const;
209 std::string print(uint32_t depth = 0) const;
212
213 void accept(Visitor& visitor) const override;
214
215 LIEF_API friend std::ostream& operator<<(std::ostream& os, const ResourcesManager& m);
216
217 private:
218 void print_tree(const ResourceNode& node, std::ostringstream& stream,
219 uint32_t current_depth, uint32_t max_depth,
220 const ResourceNode* parent = nullptr,
221 std::string header = "", bool is_last = false) const;
222 ResourceNode* resources_ = nullptr;
223 mutable dialogs_t dialogs_;
224};
225
226LIEF_API const char* to_string(ResourcesManager::TYPE type);
227
228} // namespace PE
229} // namespace LIEF
230
231#endif
Object.hpp
ResourceAccelerator.hpp
ResourceDialog.hpp
ResourceIcon.hpp
ResourceStringTable.hpp
ResourceVersion.hpp
LIEF::PE::ResourceNode
Class which represents a Node in the resource tree.
Definition ResourceNode.hpp:45
LIEF::PE::ResourcesManager
The Resource Manager provides an enhanced API to manipulate the resource tree.
Definition ResourcesManager.hpp:38
LIEF::PE::ResourcesManager::add_icon
void add_icon(const ResourceIcon &icon)
Add an icon to the resources.
LIEF::PE::ResourcesManager::has_version
bool has_version() const
true if resources a LIEF::PE::ResourceVersion
Definition ResourcesManager.hpp:157
LIEF::PE::ResourcesManager::has_html
bool has_html() const
true if the resources contain html
Definition ResourcesManager.hpp:195
LIEF::PE::ResourcesManager::version
std::vector< ResourceVersion > version() const
Return a list of verison info (VS_VERSIONINFO).
LIEF::PE::ResourcesManager::accelerator
it_const_accelerators accelerator() const
Return the list of the accelerator in the resource.
LIEF::PE::ResourcesManager::dialogs
it_const_dialogs dialogs() const
Return the list of the dialogs present in the resource.
LIEF::PE::ResourcesManager::get_node_type
ResourceNode * get_node_type(TYPE type)
Return the ResourceNode associated with the given TYPE or a nullptr if not found;.
Definition ResourcesManager.hpp:129
LIEF::PE::ResourcesManager::has_type
bool has_type(TYPE type) const
true if the resource has the given TYPE node
Definition ResourcesManager.hpp:139
LIEF::PE::ResourcesManager::~ResourcesManager
~ResourcesManager() override=default
LIEF::PE::ResourcesManager::has_icons
bool has_icons() const
true if resources contain a LIEF::PE::ResourceIcon
Definition ResourcesManager.hpp:165
LIEF::PE::ResourcesManager::has_manifest
bool has_manifest() const
true if resources contain the Manifest element
Definition ResourcesManager.hpp:144
LIEF::PE::ResourcesManager::print
std::string print(uint32_t depth=0) const
Print the resource tree to the given depth.
LIEF::PE::ResourcesManager::change_icon
void change_icon(const ResourceIcon &original, const ResourceIcon &newone)
LIEF::PE::ResourcesManager::ResourcesManager
ResourcesManager(ResourceNode &rsrc)
Definition ResourcesManager.hpp:103
LIEF::PE::ResourcesManager::get_types
std::vector< TYPE > get_types() const
List of TYPE present in the resources.
LIEF::PE::ResourcesManager::icons
it_const_icons icons() const
Return the list of the icons present in the resources.
LIEF::PE::ResourcesManager::TYPE
TYPE
The different types of resources From https://learn.microsoft.com/en-us/windows/win32/menurc/resource...
Definition ResourcesManager.hpp:43
LIEF::PE::ResourcesManager::has_string_table
bool has_string_table() const
true if the resources contain a string table
Definition ResourcesManager.hpp:187
LIEF::PE::ResourcesManager::manifest
std::string manifest() const
Return the manifest as a std::string or an empty string if not found or corrupted.
LIEF::PE::ResourcesManager::ResourcesManager
ResourcesManager(const ResourcesManager &other)
Definition ResourcesManager.hpp:107
LIEF::PE::ResourcesManager::has_accelerator
bool has_accelerator() const
true if the resources contain accelerator info
Definition ResourcesManager.hpp:203
LIEF::PE::ResourcesManager::operator<<
friend std::ostream & operator<<(std::ostream &os, const ResourcesManager &m)
LIEF::PE::ResourcesManager::operator=
ResourcesManager & operator=(ResourcesManager &&)=default
LIEF::PE::ResourcesManager::has_dialogs
bool has_dialogs() const
true if resources contain dialogs
Definition ResourcesManager.hpp:179
LIEF::PE::ResourcesManager::string_table
strings_table_t string_table() const
Return the list of the strings embedded in the string table (RT_STRING)
LIEF::PE::ResourcesManager::get_node_type
const ResourceNode * get_node_type(TYPE type) const
LIEF::PE::ResourcesManager::accept
void accept(Visitor &visitor) const override
LIEF::PE::ResourcesManager::operator=
ResourcesManager & operator=(const ResourcesManager &other)
Definition ResourcesManager.hpp:113
LIEF::PE::ResourcesManager::ResourcesManager
ResourcesManager()=delete
LIEF::PE::ResourcesManager::html
std::vector< std::string > html() const
Return the list of the html resources.
LIEF::PE::ResourcesManager::ResourcesManager
ResourcesManager(ResourcesManager &&)=default
LIEF::PE::ResourcesManager::manifest
void manifest(const std::string &manifest)
Change or set the manifest. If the manifest node path does not exist, all required nodes are created.
LIEF::VectorStream
Definition VectorStream.hpp:29
errors.hpp
iterators.hpp
LIEF::PE
Namespace related to the LIEF's PE module.
Definition Abstract/Header.hpp:32
LIEF::PE::ACCELERATOR_CODES::MENU
@ MENU
Definition AcceleratorCodes.hpp:38
LIEF::PE::to_string
const char * to_string(AuxiliaryWeakExternal::CHARACTERISTICS e)
LIEF
LIEF namespace.
Definition Abstract/Binary.hpp:36
LIEF::PE::ResourcesManager::string_entry_t::string_u8
std::string string_u8() const
LIEF::PE::ResourcesManager::string_entry_t::operator<<
friend std::ostream & operator<<(std::ostream &os, const string_entry_t &str)
Definition ResourcesManager.hpp:93
LIEF::PE::ResourcesManager::string_entry_t::is_defined
bool is_defined() const
Definition ResourcesManager.hpp:84
LIEF::PE::ResourcesManager::string_entry_t::id
uint32_t id
Definition ResourcesManager.hpp:80
visibility.h
LIEF_API
#define LIEF_API
Definition visibility.h:41