LIEF: Library to Instrument Executable Formats Version 1.0.0
Loading...
Searching...
No Matches
ResourcesManager.hpp
Go to the documentation of this file.
1/* Copyright 2017 - 2026 R. Thomas
2 * Copyright 2017 - 2026 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/visibility.h"
21#include "LIEF/Object.hpp"
22#include "LIEF/iterators.hpp"
23
28
29namespace LIEF {
30class VectorStream;
31
32namespace PE {
33class ResourceNode;
34
37 public:
40 enum class TYPE {
41 CURSOR = 1,
42 BITMAP = 2,
43 ICON = 3,
44 MENU = 4,
45 DIALOG = 5,
46 STRING = 6,
47 FONTDIR = 7,
48 FONT = 8,
49 ACCELERATOR = 9,
50 RCDATA = 10,
51 MESSAGETABLE = 11,
52 GROUP_CURSOR = 12,
53 GROUP_ICON = 14,
54 VERSION = 16,
55 DLGINCLUDE = 17,
56 PLUGPLAY = 19,
57 VXD = 20,
58 ANICURSOR = 21,
59 ANIICON = 22,
60 HTML = 23,
61 MANIFEST = 24,
62 };
63
64 public:
68
69 using icons_t = std::vector<ResourceIcon>;
71
72 using accelerators_t = std::vector<ResourceAccelerator>;
74
77 std::u16string string;
78 uint32_t id = 0;
79
80 std::string string_u8() const;
81
82 bool is_defined() const {
83 return !string.empty();
84 }
85
86 operator bool() const {
87 return is_defined();
88 }
89
90 friend LIEF_API std::ostream& operator<<(std::ostream& os,
91 const string_entry_t& str) {
92 os << std::to_string(str.id) << ", " << str.string_u8();
93 return os;
94 }
95 };
96
97 using strings_table_t = std::vector<string_entry_t>;
98
99 ResourcesManager() = delete;
101 resources_{&rsrc} {}
102
104 Object(other),
105 resources_(other.resources_)
106 // Skip (on purpose) the `dialogs_` cache
107 {}
108
110 if (&other != this) {
111 Object::operator=(other);
112 resources_ = other.resources_;
113 // Skip (on purpose) the `dialogs_` cache
114 }
115 return *this;
116 }
117
120
121 ~ResourcesManager() override = default;
122
126 return const_cast<ResourceNode*>(
127 static_cast<const ResourcesManager*>(this)->get_node_type(type)
128 );
129 }
131
133 std::vector<TYPE> get_types() const;
134
136 bool has_type(TYPE type) const {
137 return get_node_type(type) != nullptr;
138 }
139
141 bool has_manifest() const {
142 return get_node_type(TYPE::MANIFEST) != nullptr;
143 }
144
147 std::string manifest() const;
148
151 void manifest(const std::string& manifest);
152
154 bool has_version() const {
155 return get_node_type(TYPE::VERSION) != nullptr;
156 }
157
159 std::vector<ResourceVersion> version() const;
160
162 bool has_icons() const {
163 return get_node_type(TYPE::ICON) != nullptr &&
165 }
166
169
171 void add_icon(const ResourceIcon& icon);
172
173 void change_icon(const ResourceIcon& original, const ResourceIcon& newone);
174
176 bool has_dialogs() const {
177 return get_node_type(TYPE::DIALOG) != nullptr;
178 }
179
182
184 bool has_string_table() const {
185 return get_node_type(TYPE::STRING) != nullptr;
186 }
187
190
192 bool has_html() const {
193 return get_node_type(TYPE::HTML) != nullptr;
194 }
195
197 std::vector<std::string> html() const;
198
200 bool has_accelerator() const {
201 return get_node_type(TYPE::ACCELERATOR) != nullptr;
202 }
203
206
208 std::string print(uint32_t depth = 0) const;
209
210 void accept(Visitor& visitor) const override;
211
212 LIEF_API friend std::ostream& operator<<(std::ostream& os,
213 const ResourcesManager& m);
214
215 private:
216 void print_tree(const ResourceNode& node, std::ostringstream& stream,
217 uint32_t current_depth, uint32_t max_depth,
218 const ResourceNode* parent = nullptr, std::string header = "",
219 bool is_last = false) const;
220 ResourceNode* resources_ = nullptr;
221 mutable dialogs_t dialogs_;
222};
223
225
226} // namespace PE
227} // namespace LIEF
228
229#endif
Object & operator=(const Object &other)
std::vector< std::unique_ptr< ResourceDialog > > dialogs_t
Definition ResourceDialog.hpp:44
Definition ResourceIcon.hpp:37
Class which represents a Node in the resource tree.
Definition ResourceNode.hpp:46
std::vector< ResourceIcon > icons_t
Definition ResourcesManager.hpp:69
void add_icon(const ResourceIcon &icon)
Add an icon to the resources.
bool has_version() const
true if resources contain a LIEF::PE::ResourceVersion
Definition ResourcesManager.hpp:154
bool has_html() const
true if the resources contain html
Definition ResourcesManager.hpp:192
std::vector< ResourceVersion > version() const
Return a list of version info (VS_VERSIONINFO).
it_const_accelerators accelerator() const
Return the list of the accelerator in the resource.
it_const_dialogs dialogs() const
Return the list of the dialogs present in the resource.
ResourceNode * get_node_type(TYPE type)
Return the ResourceNode associated with the given TYPE or a nullptr if not found;.
Definition ResourcesManager.hpp:125
bool has_type(TYPE type) const
true if the resource has the given TYPE node
Definition ResourcesManager.hpp:136
~ResourcesManager() override=default
bool has_icons() const
true if resources contain a LIEF::PE::ResourceIcon
Definition ResourcesManager.hpp:162
bool has_manifest() const
true if resources contain the Manifest element
Definition ResourcesManager.hpp:141
std::vector< string_entry_t > strings_table_t
Definition ResourcesManager.hpp:97
std::vector< ResourceAccelerator > accelerators_t
Definition ResourcesManager.hpp:72
std::string print(uint32_t depth=0) const
Print the resource tree to the given depth.
const_ref_iterator< const dialogs_t &, const ResourceDialog * > it_const_dialogs
Definition ResourcesManager.hpp:66
void change_icon(const ResourceIcon &original, const ResourceIcon &newone)
ResourcesManager(ResourceNode &rsrc)
Definition ResourcesManager.hpp:100
std::vector< TYPE > get_types() const
List of TYPE present in the resources.
const_ref_iterator< accelerators_t > it_const_accelerators
Definition ResourcesManager.hpp:73
it_const_icons icons() const
Return the list of the icons present in the resources.
TYPE
The different types of resources From https://learn.microsoft.com/en-us/windows/win32/menurc/resource...
Definition ResourcesManager.hpp:40
@ VERSION
Definition ResourcesManager.hpp:54
@ MANIFEST
Definition ResourcesManager.hpp:61
@ GROUP_ICON
Definition ResourcesManager.hpp:53
@ HTML
Definition ResourcesManager.hpp:60
@ DIALOG
Definition ResourcesManager.hpp:45
@ STRING
Definition ResourcesManager.hpp:46
@ ACCELERATOR
Definition ResourcesManager.hpp:49
@ ICON
Definition ResourcesManager.hpp:43
bool has_string_table() const
true if the resources contain a string table
Definition ResourcesManager.hpp:184
const_ref_iterator< icons_t > it_const_icons
Definition ResourcesManager.hpp:70
std::string manifest() const
Return the manifest as a std::string or an empty string if not found or corrupted.
ResourcesManager(const ResourcesManager &other)
Definition ResourcesManager.hpp:103
bool has_accelerator() const
true if the resources contain accelerator info
Definition ResourcesManager.hpp:200
friend std::ostream & operator<<(std::ostream &os, const ResourcesManager &m)
ResourceDialog::dialogs_t dialogs_t
Definition ResourcesManager.hpp:65
ResourcesManager & operator=(ResourcesManager &&)=default
bool has_dialogs() const
true if resources contain dialogs
Definition ResourcesManager.hpp:176
strings_table_t string_table() const
Return the list of the strings embedded in the string table (RT_STRING).
const ResourceNode * get_node_type(TYPE type) const
void accept(Visitor &visitor) const override
ResourcesManager & operator=(const ResourcesManager &other)
Definition ResourcesManager.hpp:109
std::vector< std::string > html() const
Return the list of the html resources.
ResourcesManager(ResourcesManager &&)=default
void manifest(const std::string &manifest)
Change or set the manifest. If the manifest node path does not exist, all required nodes are created.
Definition VectorStream.hpp:29
Definition Visitor.hpp:212
#define LIEF_LIFETIMEBOUND
Definition compiler_attributes.hpp:72
Namespace related to the LIEF's PE module.
Definition Abstract/Header.hpp:32
const char * to_string(CODE_PAGES e)
@ MENU
Definition AcceleratorCodes.hpp:38
LIEF namespace.
Definition Abstract/Binary.hpp:40
ref_iterator< CT, U, typename decay_t< CT >::const_iterator > const_ref_iterator
Iterator which returns a const ref on container's values.
Definition iterators.hpp:318
This structure represent an entry in the string table (RT_STRING).
Definition ResourcesManager.hpp:76
std::string string_u8() const
friend std::ostream & operator<<(std::ostream &os, const string_entry_t &str)
Definition ResourcesManager.hpp:90
std::u16string string
Definition ResourcesManager.hpp:77
bool is_defined() const
Definition ResourcesManager.hpp:82
uint32_t id
Definition ResourcesManager.hpp:78
#define LIEF_API
Definition visibility.h:45