LIEF: Library to Instrument Executable Formats Version 2.0.0
Loading...
Searching...
No Matches
ResourceNode.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_RESOURCE_NODE_H
17#define LIEF_PE_RESOURCE_NODE_H
18#include <string_view>
19#include <cstdint>
20#include <memory>
21#include <sstream>
22#include <string>
23#include <vector>
24
25#include "LIEF/Object.hpp"
26#include "LIEF/iterators.hpp"
27#include "LIEF/span.hpp"
28#include "LIEF/visibility.h"
29
30namespace LIEF {
31class BinaryStream;
32
33namespace PE {
34namespace details {
35struct pe_resource_directory_table;
36}
37
38class Binary;
39
41class ResourceData;
42
43class Parser;
44class Builder;
45
48
49 friend class Parser;
50 friend class Builder;
51
52 public:
53 using childs_t = std::vector<std::unique_ptr<ResourceNode>>;
56
58 enum class TYPE {
59 UNKNOWN = 0,
60 DATA,
61 DIRECTORY,
62 };
63
66
67 ResourceNode(ResourceNode&& other) = default;
68 ResourceNode& operator=(ResourceNode&& other) = default;
69
70 void swap(ResourceNode& other);
71
72 ~ResourceNode() override;
73
79 static std::unique_ptr<ResourceNode> parse(BinaryStream& stream, uint64_t rva);
80
84 static std::unique_ptr<ResourceNode> parse(const uint8_t* buffer, size_t size,
85 uint64_t rva);
86
88 static std::unique_ptr<ResourceNode> parse(const std::vector<uint8_t>& buffer,
89 uint64_t rva) {
90 return parse(buffer.data(), buffer.size(), rva);
91 }
92
94 static std::unique_ptr<ResourceNode> parse(span<const uint8_t> buffer,
95 uint64_t rva) {
96 return parse(buffer.data(), buffer.size(), rva);
97 }
98
99 static std::unique_ptr<ResourceNode> parse(BinaryStream& stream,
100 const Binary& bin);
101
102 virtual std::unique_ptr<ResourceNode> clone() const = 0;
103
106 uint32_t id() const {
107 return id_;
108 }
109
111 const std::u16string& name() const LIEF_LIFETIMEBOUND {
112 return name_;
113 }
114
116 std::string utf8_name() const;
117
120 return childs_;
121 }
122
124 return childs_;
125 }
126
128 bool has_name() const {
129 return (bool)(id() & 0x80000000);
130 }
131
133 uint32_t depth() const {
134 return depth_;
135 }
136
144 bool is_directory() const {
145 return type_ == TYPE::DIRECTORY;
146 }
147
155 bool is_data() const {
156 return type_ == TYPE::DATA;
157 }
158
159 void id(uint32_t id) {
160 id_ = id;
161 }
162
163 void name(std::string_view name);
164
165 void name(std::u16string name) {
166 name_ = std::move(name);
167 }
168
171 ResourceNode& add_child(std::unique_ptr<ResourceNode> child) LIEF_LIFETIMEBOUND;
172
175 return add_child(child.clone());
176 }
177
179 void delete_child(uint32_t id);
180
182 void delete_child(const ResourceNode& node);
183
184 void accept(Visitor& visitor) const override;
185
186 template<class T>
187 const T* cast() const {
188 static_assert(std::is_base_of_v<ResourceNode, T>,
189 "Require inheritance relationship");
190 if (T::classof(this)) {
191 return static_cast<const T*>(this);
192 }
193 return nullptr;
194 }
195
196 template<class T>
197 T* cast() {
198 return const_cast<T*>(static_cast<const ResourceNode*>(this)->cast<T>());
199 }
200
201 LIEF_API friend std::ostream& operator<<(std::ostream& os,
202 const ResourceNode& node);
203
204 std::string to_string() const {
205 std::ostringstream oss;
206 oss << *this;
207 return oss.str();
208 }
209
215 LIEF_LOCAL const ResourceNode& safe_get_at(size_t idx) const LIEF_LIFETIMEBOUND;
216
218 LIEF_LOCAL ResourceNode& safe_get_at(size_t idx) LIEF_LIFETIMEBOUND {
219 return const_cast<ResourceNode&>(
220 static_cast<const ResourceNode*>(this)->safe_get_at(idx)
221 );
222 }
223
225 LIEF_LOCAL void set_depth(uint32_t depth) {
226 depth_ = depth;
227 }
228
229 LIEF_LOCAL void push_child(std::unique_ptr<ResourceNode> node) {
230 childs_.push_back(std::move(node));
231 }
232
233 LIEF_API friend bool operator==(const ResourceNode& LHS,
234 const ResourceNode& RHS);
235
236 LIEF_API friend bool operator!=(const ResourceNode& LHS,
237 const ResourceNode& RHS) {
238 return !(LHS == RHS);
239 }
240
241 protected:
242 ResourceNode() = default;
243 ResourceNode(TYPE type) :
244 type_(type) {}
245
246 std::unique_ptr<ResourceNode> parse_resource_node(
247 const details::pe_resource_directory_table& directory_table,
248 uint32_t base_offset, uint32_t current_offset, uint32_t depth = 0
249 );
250
251 childs_t::iterator insert_child(std::unique_ptr<ResourceNode> child);
252
253 TYPE type_ = TYPE::UNKNOWN;
254 uint32_t id_ = 0;
255 std::u16string name_;
256 childs_t childs_;
257 uint32_t depth_ = 0;
258};
259}
260}
261#endif
Class that is used to a read stream of data from different sources.
Definition BinaryStream.hpp:35
Class which represents a PE binary This is the main interface to manage and modify a PE executable.
Definition PE/Binary.hpp:59
Class that is used to rebuild a raw PE binary from a PE::Binary object.
Definition PE/Builder.hpp:44
Main interface to parse PE binaries. In particular, the static Parser::parse functions should be used...
Definition PE/Parser.hpp:54
Class which represents a Data Node in the PE resources tree.
Definition ResourceData.hpp:36
Definition ResourceDirectory.hpp:35
Class which represents a Node in the resource tree.
Definition ResourceNode.hpp:47
bool is_directory() const
True if the current entry is a ResourceDirectory.
Definition ResourceNode.hpp:144
void delete_child(uint32_t id)
Delete the node with the given id.
ResourceNode & add_child(const ResourceNode &child)
Add a new child to the current node.
Definition ResourceNode.hpp:174
static std::unique_ptr< ResourceNode > parse(BinaryStream &stream, const Binary &bin)
std::vector< std::unique_ptr< ResourceNode > > childs_t
Definition ResourceNode.hpp:53
it_const_childs childs() const
Definition ResourceNode.hpp:123
ResourceNode & add_child(std::unique_ptr< ResourceNode > child)
Add a new child to the current node, taking the ownership of the provided unique_ptr.
ResourceNode & operator=(const ResourceNode &other)
const std::u16string & name() const
Name of the entry (if any).
Definition ResourceNode.hpp:111
void push_child(std::unique_ptr< ResourceNode > node)
Definition ResourceNode.hpp:229
bool is_data() const
True if the current entry is a ResourceData.
Definition ResourceNode.hpp:155
static std::unique_ptr< ResourceNode > parse(const std::vector< uint8_t > &buffer, uint64_t rva)
See doc from other parse functions.
Definition ResourceNode.hpp:88
ResourceNode(ResourceNode &&other)=default
std::string to_string() const
Definition ResourceNode.hpp:204
ResourceNode & operator=(ResourceNode &&other)=default
static std::unique_ptr< ResourceNode > parse(BinaryStream &stream, uint64_t rva)
Parse the resource tree from the provided BinaryStream stream and with the original RVA provided in t...
friend bool operator==(const ResourceNode &LHS, const ResourceNode &RHS)
const T * cast() const
Definition ResourceNode.hpp:187
uint32_t depth() const
Current depth of the Node in the resource tree.
Definition ResourceNode.hpp:133
friend class Builder
Definition ResourceNode.hpp:50
bool has_name() const
True if the entry uses a name as ID
Definition ResourceNode.hpp:128
static std::unique_ptr< ResourceNode > parse(const uint8_t *buffer, size_t size, uint64_t rva)
Parse the resource tree from the provided buffer referenced by a pointer and the size....
void delete_child(const ResourceNode &node)
Delete the given node from the node's children.
ResourceNode(const ResourceNode &other)
virtual std::unique_ptr< ResourceNode > clone() const =0
uint32_t id() const
Integer that identifies the Type, Name, or Language ID of the entry depending on its depth in the tre...
Definition ResourceNode.hpp:106
friend class Parser
Definition ResourceNode.hpp:49
it_childs childs()
Iterator on node's children.
Definition ResourceNode.hpp:119
ref_iterator< childs_t &, ResourceNode * > it_childs
Definition ResourceNode.hpp:54
void swap(ResourceNode &other)
friend bool operator!=(const ResourceNode &LHS, const ResourceNode &RHS)
Definition ResourceNode.hpp:236
void name(std::u16string name)
Definition ResourceNode.hpp:165
TYPE
Enum that identifies the type of a node in the resource tree.
Definition ResourceNode.hpp:58
@ DIRECTORY
Definition ResourceNode.hpp:61
@ DATA
Definition ResourceNode.hpp:60
void accept(Visitor &visitor) const override
static std::unique_ptr< ResourceNode > parse(span< const uint8_t > buffer, uint64_t rva)
See doc from other parse functions.
Definition ResourceNode.hpp:94
void id(uint32_t id)
Definition ResourceNode.hpp:159
std::string utf8_name() const
UTF-8 representation of the name().
void name(std::string_view name)
friend std::ostream & operator<<(std::ostream &os, const ResourceNode &node)
const_ref_iterator< const childs_t &, ResourceNode * > it_const_childs
Definition ResourceNode.hpp:55
T * cast()
Definition ResourceNode.hpp:197
Definition Visitor.hpp:212
Iterator which returns reference on container's values.
Definition iterators.hpp:121
#define LIEF_LIFETIMEBOUND
Definition compiler_attributes.hpp:72
Definition DataDirectory.hpp:38
Namespace related to the LIEF's PE module.
Definition Abstract/Header.hpp:32
@ T
Definition AcceleratorCodes.hpp:97
LIEF namespace.
Definition Abstract/Binary.hpp:42
tcb::span< ElementType, Extent > span
Definition span.hpp:22
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:386
#define LIEF_API
Definition visibility.h:45
#define LIEF_LOCAL
Definition visibility.h:46