LIEF: Library to Instrument Executable Formats Version 0.15.1
Loading...
Searching...
No Matches
ResourcesManager.hpp
Go to the documentation of this file.
1/* Copyright 2017 - 2024 R. Thomas
2 * Copyright 2017 - 2024 Quarkslab
3 * Copyright 2017 - 2021 K. Nakagawa
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17#ifndef LIEF_PE_RESOURCES_MANAGER_H
18#define LIEF_PE_RESOURCES_MANAGER_H
19#include <ostream>
20
21#include "LIEF/errors.hpp"
22#include "LIEF/visibility.h"
23#include "LIEF/Object.hpp"
24
30
31namespace LIEF {
32class VectorStream;
33
34namespace PE {
35class ResourceNode;
36
39 public:
40
43 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 static constexpr uint32_t lang_from_id(size_t id) {
68 return id & 0x3ff;
69 }
70
71 static constexpr uint32_t sublang_from_id(size_t id) {
72 return id >> 10;
73 }
74
75 public:
76 using dialogs_t = std::vector<ResourceDialog>;
78
79 using icons_t = std::vector<ResourceIcon>;
81
82 using strings_table_t = std::vector<ResourceStringTable>;
84
85 using accelerators_t = std::vector<ResourceAccelerator>;
87
88 ResourcesManager() = delete;
90 resources_{&rsrc}
91 {}
92
95
98
99 ~ResourcesManager() override = default;
100
104 const ResourceNode* get_node_type(TYPE type) const;
105
107 std::vector<TYPE> get_types() const;
108
110 bool has_type(TYPE type) const {
111 return get_node_type(type) != nullptr;
112 }
113
115 bool has_manifest() const {
116 return get_node_type(TYPE::MANIFEST) != nullptr;
117 }
118
121 std::string manifest() const;
122
124 void manifest(const std::string& manifest);
125
127 bool has_version() const {
128 return get_node_type(TYPE::VERSION) != nullptr;
129 }
130
133
135 bool has_icons() const {
136 return get_node_type(TYPE::ICON) != nullptr &&
137 get_node_type(TYPE::GROUP_ICON) != nullptr;
138 }
139
142
144 void add_icon(const ResourceIcon& icon);
145
146 void change_icon(const ResourceIcon& original, const ResourceIcon& newone);
147
149 bool has_dialogs() const {
150 return get_node_type(TYPE::DIALOG) != nullptr;
151 }
152
155
157 bool has_string_table() const {
158 return get_node_type(TYPE::STRING) != nullptr;
159 }
160
163
165 bool has_html() const {
166 return get_node_type(TYPE::HTML) != nullptr;
167 }
168
170 std::vector<std::string> html() const;
171
173 bool has_accelerator() const {
174 return get_node_type(TYPE::ACCELERATOR) != nullptr;
175 }
176
179
181 std::string print(uint32_t depth = 0) const;
182
183 void accept(Visitor& visitor) const override;
184
185 LIEF_API friend std::ostream& operator<<(std::ostream& os, const ResourcesManager& m);
186
187 private:
188 void print_tree(const ResourceNode& node, std::ostringstream& stream,
189 uint32_t current_depth, uint32_t max_depth) const;
190 ResourceNode* resources_ = nullptr;
191};
192
194
195} // namespace PE
196} // namespace LIEF
197
198#endif
Definition Object.hpp:25
Definition ResourceIcon.hpp:39
Class which represents a Node in the resource tree.
Definition ResourceNode.hpp:36
The Resource Manager provides an enhanced API to manipulate the resource tree.
Definition ResourcesManager.hpp:38
std::vector< ResourceIcon > icons_t
Definition ResourcesManager.hpp:79
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:127
bool has_html() const
true if the resources contain html
Definition ResourcesManager.hpp:165
std::vector< ResourceDialog > dialogs_t
Definition ResourcesManager.hpp:76
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 KIND or a nullptr if not found;.
bool has_type(TYPE type) const
true if the resource has the given LIEF::PE::KIND
Definition ResourcesManager.hpp:110
~ResourcesManager() override=default
result< ResourceVersion > version() const
Return the ResourceVersion if any.
bool has_icons() const
true if resources contain a LIEF::PE::ResourceIcon
Definition ResourcesManager.hpp:135
bool has_manifest() const
true if resources contain the Manifest element
Definition ResourcesManager.hpp:115
std::vector< ResourceAccelerator > accelerators_t
Definition ResourcesManager.hpp:85
std::string print(uint32_t depth=0) const
Print the resource tree to the given depth.
static constexpr uint32_t lang_from_id(size_t id)
Definition ResourcesManager.hpp:67
void change_icon(const ResourceIcon &original, const ResourceIcon &newone)
ResourcesManager(ResourceNode &rsrc)
Definition ResourcesManager.hpp:89
std::vector< TYPE > get_types() const
List of TYPE present in the resources.
it_const_icons icons() const
Return the list of the icons present in the resources.
static constexpr uint32_t sublang_from_id(size_t id)
Definition ResourcesManager.hpp:71
TYPE
The different types of resources Ref: From https://docs.microsoft.com/en-us/windows/win32/menurc/reso...
Definition ResourcesManager.hpp:43
it_const_strings_table string_table() const
Return the list of the string table in the resource.
bool has_string_table() const
true if the resources contain a LIEF::PE::ResourceStringTable
Definition ResourcesManager.hpp:157
std::string manifest() const
Return the manifest as a std::string or an empty string if not found or corrupted.
ResourcesManager(const ResourcesManager &)=default
ResourcesManager & operator=(const ResourcesManager &)=default
bool has_accelerator() const
true if the resources contain LIEF::PE::ResourceAccelerator
Definition ResourcesManager.hpp:173
friend std::ostream & operator<<(std::ostream &os, const ResourcesManager &m)
ResourcesManager & operator=(ResourcesManager &&)=default
std::vector< ResourceStringTable > strings_table_t
Definition ResourcesManager.hpp:82
bool has_dialogs() const
true if resources contain dialogs
Definition ResourcesManager.hpp:149
const ResourceNode * get_node_type(TYPE type) const
void accept(Visitor &visitor) const override
std::vector< std::string > html() const
Return the list of the html resources.
ResourcesManager(ResourcesManager &&)=default
void manifest(const std::string &manifest)
Update the manifest with the given string.
Definition Visitor.hpp:224
Iterator which returns reference on container's values.
Definition iterators.hpp:48
const char * to_string(DataDirectory::TYPES e)
LIEF namespace.
Definition Abstract/Binary.hpp:32
tl::expected< T, lief_errors > result
Wrapper that contains an Object (T) or an error.
Definition errors.hpp:74
#define LIEF_API
Definition visibility.h:41