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