LIEF: Library to Instrument Executable Formats Version 0.15.0
Loading...
Searching...
No Matches
ResourcesManager.hpp
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
25#include "LIEF/PE/resources/ResourceVersion.hpp"
26#include "LIEF/PE/resources/ResourceIcon.hpp"
27#include "LIEF/PE/resources/ResourceDialog.hpp"
28#include "LIEF/PE/resources/ResourceStringTable.hpp"
29#include "LIEF/PE/resources/ResourceAccelerator.hpp"
30
31namespace LIEF {
32class VectorStream;
33
34namespace PE {
35class ResourceNode;
36
38class LIEF_API ResourcesManager : public Object {
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>;
77 using it_const_dialogs = const_ref_iterator<dialogs_t>;
78
79 using icons_t = std::vector<ResourceIcon>;
80 using it_const_icons = const_ref_iterator<icons_t>;
81
82 using strings_table_t = std::vector<ResourceStringTable>;
83 using it_const_strings_table = const_ref_iterator<strings_table_t>;
84
85 using accelerators_t = std::vector<ResourceAccelerator>;
86 using it_const_accelerators = const_ref_iterator<accelerators_t>;
87
88 ResourcesManager() = delete;
89 ResourcesManager(ResourceNode& rsrc) :
90 resources_{&rsrc}
91 {}
92
93 ResourcesManager(const ResourcesManager&) = default;
94 ResourcesManager& operator=(const ResourcesManager&) = default;
95
96 ResourcesManager(ResourcesManager&&) = default;
97 ResourcesManager& operator=(ResourcesManager&&) = default;
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
193LIEF_API const char* to_string(ResourcesManager::TYPE type);
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:38
The Resource Manager provides an enhanced API to manipulate the resource tree.
Definition ResourcesManager.hpp:38
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
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
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::string print(uint32_t depth=0) const
Print the resource tree to the given depth.
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.
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.
bool has_accelerator() const
true if the resources contain LIEF::PE::ResourceAccelerator
Definition ResourcesManager.hpp:173
bool has_dialogs() const
true if resources contain dialogs
Definition ResourcesManager.hpp:149
std::vector< std::string > html() const
Return the list of the html resources.
void manifest(const std::string &manifest)
Update the manifest with the given string.
Definition Visitor.hpp:219
Iterator which returns reference on container's values.
Definition iterators.hpp:48
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:72