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/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;
36
39 public:
42 enum class TYPE {
43 CURSOR = 1,
44 BITMAP = 2,
45 ICON = 3,
46 MENU = 4,
47 DIALOG = 5,
48 STRING = 6,
49 FONTDIR = 7,
50 FONT = 8,
51 ACCELERATOR = 9,
52 RCDATA = 10,
53 MESSAGETABLE = 11,
54 GROUP_CURSOR = 12,
55 GROUP_ICON = 14,
56 VERSION = 16,
57 DLGINCLUDE = 17,
58 PLUGPLAY = 19,
59 VXD = 20,
60 ANICURSOR = 21,
61 ANIICON = 22,
62 HTML = 23,
63 MANIFEST = 24,
64 };
65
66 public:
70
71 using icons_t = std::vector<ResourceIcon>;
73
74 using accelerators_t = std::vector<ResourceAccelerator>;
76
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 std::ostream& operator<<(std::ostream& os,
93 const string_entry_t& str) {
94 os << std::to_string(str.id) << ", " << str.string_u8();
95 return os;
96 }
97 };
98
99 using strings_table_t = std::vector<string_entry_t>;
100
103 resources_{&rsrc} {}
104
106 Object(other),
107 resources_(other.resources_)
108 // Skip (on purpose) the `dialogs_` cache
109 {}
110
112 if (&other != this) {
113 Object::operator=(other);
114 resources_ = other.resources_;
115 // Skip (on purpose) the `dialogs_` cache
116 }
117 return *this;
118 }
119
122
123 ~ResourcesManager() override = default;
124
128 return const_cast<ResourceNode*>(
129 static_cast<const ResourcesManager*>(this)->get_node_type(type)
130 );
131 }
132 const ResourceNode* get_node_type(TYPE type) const;
133
135 std::vector<TYPE> get_types() const;
136
138 bool has_type(TYPE type) const {
139 return get_node_type(type) != nullptr;
140 }
141
143 bool has_manifest() const {
144 return get_node_type(TYPE::MANIFEST) != nullptr;
145 }
146
149 std::string manifest() const;
150
153 void manifest(const std::string& manifest);
154
156 bool has_version() const {
157 return get_node_type(TYPE::VERSION) != nullptr;
158 }
159
161 std::vector<ResourceVersion> version() const;
162
164 bool has_icons() const {
165 return get_node_type(TYPE::ICON) != nullptr &&
167 }
168
171
173 void add_icon(const ResourceIcon& icon);
174
175 void change_icon(const ResourceIcon& original, const ResourceIcon& newone);
176
178 bool has_dialogs() const {
179 return get_node_type(TYPE::DIALOG) != nullptr;
180 }
181
184
186 bool has_string_table() const {
187 return get_node_type(TYPE::STRING) != nullptr;
188 }
189
192
194 bool has_html() const {
195 return get_node_type(TYPE::HTML) != nullptr;
196 }
197
199 std::vector<std::string> html() const;
200
202 bool has_accelerator() const {
203 return get_node_type(TYPE::ACCELERATOR) != nullptr;
204 }
205
208
210 std::string print(uint32_t depth = 0) const;
211
212 void accept(Visitor& visitor) const override;
213
214 LIEF_API friend std::ostream& operator<<(std::ostream& os,
215 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, std::string header = "",
221 bool is_last = false) const;
222 ResourceNode* resources_ = nullptr;
223 mutable dialogs_t dialogs_;
224};
225
227
228} // namespace PE
229} // namespace LIEF
230
231#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:71
void add_icon(const ResourceIcon &icon)
Add an icon to the resources.
bool has_version() const
true if resources a LIEF::PE::ResourceVersion
Definition ResourcesManager.hpp:156
bool has_html() const
true if the resources contain html
Definition ResourcesManager.hpp:194
std::vector< ResourceVersion > version() const
Return a list of verison 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:127
bool has_type(TYPE type) const
true if the resource has the given TYPE node
Definition ResourcesManager.hpp:138
~ResourcesManager() override=default
bool has_icons() const
true if resources contain a LIEF::PE::ResourceIcon
Definition ResourcesManager.hpp:164
bool has_manifest() const
true if resources contain the Manifest element
Definition ResourcesManager.hpp:143
std::vector< string_entry_t > strings_table_t
Definition ResourcesManager.hpp:99
std::vector< ResourceAccelerator > accelerators_t
Definition ResourcesManager.hpp:74
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:68
void change_icon(const ResourceIcon &original, const ResourceIcon &newone)
ResourcesManager(ResourceNode &rsrc)
Definition ResourcesManager.hpp:102
std::vector< TYPE > get_types() const
List of TYPE present in the resources.
const_ref_iterator< accelerators_t > it_const_accelerators
Definition ResourcesManager.hpp:75
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:42
@ VERSION
Definition ResourcesManager.hpp:56
@ MANIFEST
Definition ResourcesManager.hpp:63
@ GROUP_ICON
Definition ResourcesManager.hpp:55
@ HTML
Definition ResourcesManager.hpp:62
@ DIALOG
Definition ResourcesManager.hpp:47
@ STRING
Definition ResourcesManager.hpp:48
@ ACCELERATOR
Definition ResourcesManager.hpp:51
@ ICON
Definition ResourcesManager.hpp:45
bool has_string_table() const
true if the resources contain a string table
Definition ResourcesManager.hpp:186
const_ref_iterator< icons_t > it_const_icons
Definition ResourcesManager.hpp:72
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:105
bool has_accelerator() const
true if the resources contain accelerator info
Definition ResourcesManager.hpp:202
friend std::ostream & operator<<(std::ostream &os, const ResourcesManager &m)
ResourceDialog::dialogs_t dialogs_t
Definition ResourcesManager.hpp:67
ResourcesManager & operator=(ResourcesManager &&)=default
bool has_dialogs() const
true if resources contain dialogs
Definition ResourcesManager.hpp:178
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:111
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
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 return const ref on container's values.
Definition iterators.hpp:286
This structure represent an entry in the string table (RT_STRING).
Definition ResourcesManager.hpp:78
std::string string_u8() const
friend std::ostream & operator<<(std::ostream &os, const string_entry_t &str)
Definition ResourcesManager.hpp:92
std::u16string string
Definition ResourcesManager.hpp:79
bool is_defined() const
Definition ResourcesManager.hpp:84
uint32_t id
Definition ResourcesManager.hpp:80
#define LIEF_API
Definition visibility.h:43