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 - 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;
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 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 ResourceNode* get_node_type(TYPE type);
104 const ResourceNode* get_node_type(TYPE type) const;
105 std::vector<TYPE> get_types() const;
108 bool has_type(TYPE type) const {
111 return get_node_type(type) != nullptr;
112 }
113 bool has_manifest() const {
116 return get_node_type(TYPE::MANIFEST) != nullptr;
117 }
118 std::string manifest() const;
122 void manifest(const std::string& manifest);
125 bool has_version() const {
128 return get_node_type(TYPE::VERSION) != nullptr;
129 }
130 result<ResourceVersion> version() const;
133 bool has_icons() const {
136 return get_node_type(TYPE::ICON) != nullptr &&
137 get_node_type(TYPE::GROUP_ICON) != nullptr;
138 }
139 it_const_icons icons() const;
142 void add_icon(const ResourceIcon& icon);
145
146 void change_icon(const ResourceIcon& original, const ResourceIcon& newone);
147 bool has_dialogs() const {
150 return get_node_type(TYPE::DIALOG) != nullptr;
151 }
152 it_const_dialogs dialogs() const;
155 bool has_string_table() const {
158 return get_node_type(TYPE::STRING) != nullptr;
159 }
160 it_const_strings_table string_table() const;
163 bool has_html() const {
166 return get_node_type(TYPE::HTML) != nullptr;
167 }
168 std::vector<std::string> html() const;
171 bool has_accelerator() const {
174 return get_node_type(TYPE::ACCELERATOR) != nullptr;
175 }
176 it_const_accelerators accelerator() const;
179 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
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:36
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 contain a LIEF::PE::ResourceVersion
Definition ResourcesManager.hpp:127
LIEF::PE::ResourcesManager::has_html
bool has_html() const
true if the resources contain html
Definition ResourcesManager.hpp:165
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 KIND or a nullptr if not found;.
LIEF::PE::ResourcesManager::has_type
bool has_type(TYPE type) const
true if the resource has the given LIEF::PE::KIND
Definition ResourcesManager.hpp:110
LIEF::PE::ResourcesManager::~ResourcesManager
~ResourcesManager() override=default
LIEF::PE::ResourcesManager::version
result< ResourceVersion > version() const
Return the ResourceVersion if any.
LIEF::PE::ResourcesManager::has_icons
bool has_icons() const
true if resources contain a LIEF::PE::ResourceIcon
Definition ResourcesManager.hpp:135
LIEF::PE::ResourcesManager::has_manifest
bool has_manifest() const
true if resources contain the Manifest element
Definition ResourcesManager.hpp:115
LIEF::PE::ResourcesManager::print
std::string print(uint32_t depth=0) const
Print the resource tree to the given depth.
LIEF::PE::ResourcesManager::lang_from_id
static constexpr uint32_t lang_from_id(size_t id)
Definition ResourcesManager.hpp:67
LIEF::PE::ResourcesManager::change_icon
void change_icon(const ResourceIcon &original, const ResourceIcon &newone)
LIEF::PE::ResourcesManager::ResourcesManager
ResourcesManager(ResourceNode &rsrc)
Definition ResourcesManager.hpp:89
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::sublang_from_id
static constexpr uint32_t sublang_from_id(size_t id)
Definition ResourcesManager.hpp:71
LIEF::PE::ResourcesManager::TYPE
TYPE
The different types of resources Ref: From https://docs.microsoft.com/en-us/windows/win32/menurc/reso...
Definition ResourcesManager.hpp:43
LIEF::PE::ResourcesManager::string_table
it_const_strings_table string_table() const
Return the list of the string table in the resource.
LIEF::PE::ResourcesManager::has_string_table
bool has_string_table() const
true if the resources contain a LIEF::PE::ResourceStringTable
Definition ResourcesManager.hpp:157
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 &)=default
LIEF::PE::ResourcesManager::operator=
ResourcesManager & operator=(const ResourcesManager &)=default
LIEF::PE::ResourcesManager::has_accelerator
bool has_accelerator() const
true if the resources contain LIEF::PE::ResourceAccelerator
Definition ResourcesManager.hpp:173
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:149
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::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)
Update the manifest with the given string.
LIEF::VectorStream
Definition VectorStream.hpp:29
errors.hpp
LIEF::PE
Namespace related to the LIEF's PE module.
Definition Abstract/Header.hpp:32
LIEF::PE::to_string
const char * to_string(DataDirectory::TYPES e)
LIEF
LIEF namespace.
Definition Abstract/Binary.hpp:36
visibility.h
LIEF_API
#define LIEF_API
Definition visibility.h:41