LIEF: Library to Instrument Executable Formats Version 0.17.0
Loading...
Searching...
No Matches
ResourceNode.hpp
Go to the documentation of this file.
1/* Copyright 2017 - 2025 R. Thomas
2 * Copyright 2017 - 2025 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
23#include "LIEF/Object.hpp"
24#include "LIEF/visibility.h"
25#include "LIEF/iterators.hpp"
26#include "LIEF/span.hpp"
27
28namespace LIEF {
29class BinaryStream;
30
31namespace PE {
32namespace details {
33struct pe_resource_directory_table;
34}
35
36class Binary;
37
39class ResourceData;
40
41class Parser;
42class Builder;
43class LIEF_API ResourceNode : public Object {
46
47 friend class Parser;
48 friend class Builder;
49
50 public:
51 using childs_t = std::vector<std::unique_ptr<ResourceNode>>;
52 using it_childs = ref_iterator<childs_t&, ResourceNode*>;
53 using it_const_childs = const_ref_iterator<const childs_t&, ResourceNode*>;
54 enum class TYPE {
57 UNKNOWN = 0,
58 DATA,
59 DIRECTORY,
60 };
61
62 ResourceNode(const ResourceNode& other);
63 ResourceNode& operator=(const ResourceNode& other);
64
65 ResourceNode(ResourceNode&& other) = default;
66 ResourceNode& operator=(ResourceNode&& other) = default;
67
68 void swap(ResourceNode& other);
69
70 ~ResourceNode() override;
71 static std::unique_ptr<ResourceNode> parse(BinaryStream& stream, uint64_t rva);
78 static std::unique_ptr<ResourceNode> parse(const uint8_t* buffer, size_t size,
83 uint64_t rva);
84 static std::unique_ptr<ResourceNode> parse(const std::vector<uint8_t>& buffer,
87 uint64_t rva)
88 {
89 return parse(buffer.data(), buffer.size(), rva);
90 }
91 static std::unique_ptr<ResourceNode> parse(span<const uint8_t> buffer,
94 uint64_t rva)
95 {
96 return parse(buffer.data(), buffer.size(), rva);
97 }
98
99 static std::unique_ptr<ResourceNode>
100 parse(BinaryStream& stream, const Binary& bin);
101
102 virtual std::unique_ptr<ResourceNode> clone() const = 0;
103 uint32_t id() const {
107 return id_;
108 }
109 const std::u16string& name() const {
112 return name_;
113 }
114 std::string utf8_name() const;
117 it_childs childs() {
120 return childs_;
121 }
122
123 it_const_childs childs() const {
124 return childs_;
125 }
126 bool has_name() const {
129 return (bool)(id() & 0x80000000);
130 }
131 uint32_t depth() const {
134 return depth_;
135 }
136 bool is_directory() const {
145 return type_ == TYPE::DIRECTORY;
146 }
147 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(const std::string& name);
164
165 void name(std::u16string name) {
166 name_ = std::move(name);
167 }
168 ResourceNode& add_child(std::unique_ptr<ResourceNode> child);
172 ResourceNode& add_child(const ResourceNode& child) {
175 return add_child(child.clone());
176 }
177 void delete_child(uint32_t id);
180 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<ResourceNode, T>::value, "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, const ResourceNode& node);
201
202 std::string to_string() const {
203 std::ostringstream oss;
204 oss << *this;
205 return oss.str();
206 }
207 LIEF_LOCAL const ResourceNode& safe_get_at(size_t idx) const;
214 LIEF_LOCAL ResourceNode& safe_get_at(size_t idx) {
217 return const_cast<ResourceNode&>(static_cast<const ResourceNode*>(this)->safe_get_at(idx));
218 }
219 LIEF_LOCAL void set_depth(uint32_t depth) {
222 depth_ = depth;
223 }
224
225 LIEF_LOCAL void push_child(std::unique_ptr<ResourceNode> node) {
226 childs_.push_back(std::move(node));
227 }
228
229 LIEF_API friend bool operator==(const ResourceNode& LHS, const ResourceNode& RHS);
230
231 LIEF_API friend bool operator!=(const ResourceNode& LHS, const ResourceNode& RHS) {
232 return !(LHS == RHS);
233 }
234
235 protected:
236 ResourceNode() = default;
237 ResourceNode(TYPE type) :
238 type_(type)
239 {}
240
241 std::unique_ptr<ResourceNode> parse_resource_node(
242 const details::pe_resource_directory_table& directory_table,
243 uint32_t base_offset, uint32_t current_offset, uint32_t depth = 0);
244
245 childs_t::iterator insert_child(std::unique_ptr<ResourceNode> child);
246
247 TYPE type_ = TYPE::UNKNOWN;
248 uint32_t id_ = 0;
249 std::u16string name_;
250 childs_t childs_;
251 uint32_t depth_ = 0;
252};
253}
254}
255#endif /* RESOURCENODE_H */
Object.hpp
LIEF::BinaryStream
Class that is used to a read stream of data from different sources.
Definition BinaryStream.hpp:33
LIEF::PE::Binary
Class which represents a PE binary This is the main interface to manage and modify a PE executable.
Definition PE/Binary.hpp:56
LIEF::PE::Builder
Class that is used to rebuild a raw PE binary from a PE::Binary object.
Definition PE/Builder.hpp:45
LIEF::PE::Parser
Main interface to parse PE binaries. In particular the static functions: Parser::parse should be used...
Definition PE/Parser.hpp:52
LIEF::PE::ResourceData
Class which represents a Data Node in the PE resources tree.
Definition ResourceData.hpp:33
LIEF::PE::ResourceDirectory
Definition ResourceDirectory.hpp:33
LIEF::PE::ResourceNode
Class which represents a Node in the resource tree.
Definition ResourceNode.hpp:45
LIEF::PE::ResourceNode::is_directory
bool is_directory() const
True if the current entry is a ResourceDirectory.
Definition ResourceNode.hpp:144
LIEF::PE::ResourceNode::delete_child
void delete_child(uint32_t id)
Delete the node with the given id
LIEF::PE::ResourceNode::add_child
ResourceNode & add_child(const ResourceNode &child)
Add a new child to the current node.
Definition ResourceNode.hpp:174
LIEF::PE::ResourceNode::parse
static std::unique_ptr< ResourceNode > parse(BinaryStream &stream, const Binary &bin)
LIEF::PE::ResourceNode::childs
it_const_childs childs() const
Definition ResourceNode.hpp:123
LIEF::PE::ResourceNode::add_child
ResourceNode & add_child(std::unique_ptr< ResourceNode > child)
Add a new child to the current node, taking the ownership of the provided unique_ptr
LIEF::PE::ResourceNode::operator=
ResourceNode & operator=(const ResourceNode &other)
LIEF::PE::ResourceNode::name
const std::u16string & name() const
Name of the entry (if any)
Definition ResourceNode.hpp:111
LIEF::PE::ResourceNode::push_child
void push_child(std::unique_ptr< ResourceNode > node)
Definition ResourceNode.hpp:225
LIEF::PE::ResourceNode::is_data
bool is_data() const
True if the current entry is a ResourceData.
Definition ResourceNode.hpp:155
LIEF::PE::ResourceNode::parse
static std::unique_ptr< ResourceNode > parse(const std::vector< uint8_t > &buffer, uint64_t rva)
See doc from other parse functions.
Definition ResourceNode.hpp:86
LIEF::PE::ResourceNode::ResourceNode
ResourceNode(ResourceNode &&other)=default
LIEF::PE::ResourceNode::to_string
std::string to_string() const
Definition ResourceNode.hpp:202
LIEF::PE::ResourceNode::operator=
ResourceNode & operator=(ResourceNode &&other)=default
LIEF::PE::ResourceNode::parse
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...
LIEF::PE::ResourceNode::operator==
friend bool operator==(const ResourceNode &LHS, const ResourceNode &RHS)
LIEF::PE::ResourceNode::cast
const T * cast() const
Definition ResourceNode.hpp:187
LIEF::PE::ResourceNode::depth
uint32_t depth() const
Current depth of the Node in the resource tree.
Definition ResourceNode.hpp:133
LIEF::PE::ResourceNode::has_name
bool has_name() const
True if the entry uses a name as ID
Definition ResourceNode.hpp:128
LIEF::PE::ResourceNode::parse
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....
LIEF::PE::ResourceNode::delete_child
void delete_child(const ResourceNode &node)
Delete the given node from the node's children.
LIEF::PE::ResourceNode::ResourceNode
ResourceNode(const ResourceNode &other)
LIEF::PE::ResourceNode::clone
virtual std::unique_ptr< ResourceNode > clone() const =0
LIEF::PE::ResourceNode::id
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
LIEF::PE::ResourceNode::childs
it_childs childs()
Iterator on node's children.
Definition ResourceNode.hpp:119
LIEF::PE::ResourceNode::swap
void swap(ResourceNode &other)
LIEF::PE::ResourceNode::operator!=
friend bool operator!=(const ResourceNode &LHS, const ResourceNode &RHS)
Definition ResourceNode.hpp:231
LIEF::PE::ResourceNode::name
void name(std::u16string name)
Definition ResourceNode.hpp:165
LIEF::PE::ResourceNode::accept
void accept(Visitor &visitor) const override
LIEF::PE::ResourceNode::parse
static std::unique_ptr< ResourceNode > parse(span< const uint8_t > buffer, uint64_t rva)
See doc from other parse functions.
Definition ResourceNode.hpp:93
LIEF::PE::ResourceNode::name
void name(const std::string &name)
LIEF::PE::ResourceNode::id
void id(uint32_t id)
Definition ResourceNode.hpp:159
LIEF::PE::ResourceNode::utf8_name
std::string utf8_name() const
UTF-8 representation of the name()
LIEF::PE::ResourceNode::operator<<
friend std::ostream & operator<<(std::ostream &os, const ResourceNode &node)
LIEF::PE::ResourceNode::~ResourceNode
~ResourceNode() override
LIEF::PE::ResourceNode::cast
T * cast()
Definition ResourceNode.hpp:196
iterators.hpp
LIEF::PE::details
Definition DataDirectory.hpp:37
LIEF::PE
Namespace related to the LIEF's PE module.
Definition Abstract/Header.hpp:32
LIEF::PE::ACCELERATOR_CODES::T
@ T
Definition AcceleratorCodes.hpp:97
LIEF
LIEF namespace.
Definition Abstract/Binary.hpp:36
span.hpp
visibility.h
LIEF_API
#define LIEF_API
Definition visibility.h:41
LIEF_LOCAL
#define LIEF_LOCAL
Definition visibility.h:42