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#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;
44class LIEF_API ResourceNode : public Object {
47
48 friend class Parser;
49 friend class Builder;
50
51 public:
52 using childs_t = std::vector<std::unique_ptr<ResourceNode>>;
53 using it_childs = ref_iterator<childs_t&, ResourceNode*>;
54 using it_const_childs = const_ref_iterator<const childs_t&, ResourceNode*>;
55 enum class TYPE {
58 UNKNOWN = 0,
59 DATA,
60 DIRECTORY,
61 };
62
63 ResourceNode(const ResourceNode& other);
64 ResourceNode& operator=(const ResourceNode& other);
65
66 ResourceNode(ResourceNode&& other) = default;
67 ResourceNode& operator=(ResourceNode&& other) = default;
68
69 void swap(ResourceNode& other);
70
71 ~ResourceNode() override;
72 static std::unique_ptr<ResourceNode> parse(BinaryStream& stream, uint64_t rva);
79 static std::unique_ptr<ResourceNode> parse(const uint8_t* buffer, size_t size,
84 uint64_t rva);
85 static std::unique_ptr<ResourceNode> parse(const std::vector<uint8_t>& buffer,
88 uint64_t rva)
89 {
90 return parse(buffer.data(), buffer.size(), rva);
91 }
92 static std::unique_ptr<ResourceNode> parse(span<const uint8_t> buffer,
95 uint64_t rva)
96 {
97 return parse(buffer.data(), buffer.size(), rva);
98 }
99
100 static std::unique_ptr<ResourceNode>
101 parse(BinaryStream& stream, const Binary& bin);
102
103 virtual std::unique_ptr<ResourceNode> clone() const = 0;
104 uint32_t id() const {
108 return id_;
109 }
110 const std::u16string& name() const {
113 return name_;
114 }
115 std::string utf8_name() const;
118 it_childs childs() {
121 return childs_;
122 }
123
124 it_const_childs childs() const {
125 return childs_;
126 }
127 bool has_name() const {
130 return (bool)(id() & 0x80000000);
131 }
132 uint32_t depth() const {
135 return depth_;
136 }
137 bool is_directory() const {
146 return type_ == TYPE::DIRECTORY;
147 }
148 bool is_data() const {
157 return type_ == TYPE::DATA;
158 }
159
160 void id(uint32_t id) {
161 id_ = id;
162 }
163
164 void name(const std::string& name);
165
166 void name(std::u16string name) {
167 name_ = std::move(name);
168 }
169 ResourceNode& add_child(std::unique_ptr<ResourceNode> child);
173 ResourceNode& add_child(const ResourceNode& child) {
176 return add_child(child.clone());
177 }
178 void delete_child(uint32_t id);
181 void delete_child(const ResourceNode& node);
184
185 void accept(Visitor& visitor) const override;
186
187 template<class T>
188 const T* cast() const {
189 static_assert(std::is_base_of<ResourceNode, T>::value, "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, const ResourceNode& node);
202
203 std::string to_string() const {
204 std::ostringstream oss;
205 oss << *this;
206 return oss.str();
207 }
208 LIEF_LOCAL const ResourceNode& safe_get_at(size_t idx) const;
215 LIEF_LOCAL ResourceNode& safe_get_at(size_t idx) {
218 return const_cast<ResourceNode&>(static_cast<const ResourceNode*>(this)->safe_get_at(idx));
219 }
220 LIEF_LOCAL void set_depth(uint32_t depth) {
223 depth_ = depth;
224 }
225
226 LIEF_LOCAL void push_child(std::unique_ptr<ResourceNode> node) {
227 childs_.push_back(std::move(node));
228 }
229
230 LIEF_API friend bool operator==(const ResourceNode& LHS, const ResourceNode& RHS);
231
232 LIEF_API friend bool operator!=(const ResourceNode& LHS, const ResourceNode& RHS) {
233 return !(LHS == RHS);
234 }
235
236 protected:
237 ResourceNode() = default;
238 ResourceNode(TYPE type) :
239 type_(type)
240 {}
241
242 std::unique_ptr<ResourceNode> parse_resource_node(
243 const details::pe_resource_directory_table& directory_table,
244 uint32_t base_offset, uint32_t current_offset, uint32_t depth = 0);
245
246 childs_t::iterator insert_child(std::unique_ptr<ResourceNode> child);
247
248 TYPE type_ = TYPE::UNKNOWN;
249 uint32_t id_ = 0;
250 std::u16string name_;
251 childs_t childs_;
252 uint32_t depth_ = 0;
253};
254}
255}
256#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:46
LIEF::PE::ResourceNode::is_directory
bool is_directory() const
True if the current entry is a ResourceDirectory.
Definition ResourceNode.hpp:145
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:175
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:124
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:112
LIEF::PE::ResourceNode::push_child
void push_child(std::unique_ptr< ResourceNode > node)
Definition ResourceNode.hpp:226
LIEF::PE::ResourceNode::is_data
bool is_data() const
True if the current entry is a ResourceData.
Definition ResourceNode.hpp:156
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:87
LIEF::PE::ResourceNode::ResourceNode
ResourceNode(ResourceNode &&other)=default
LIEF::PE::ResourceNode::to_string
std::string to_string() const
Definition ResourceNode.hpp:203
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:188
LIEF::PE::ResourceNode::depth
uint32_t depth() const
Current depth of the Node in the resource tree.
Definition ResourceNode.hpp:134
LIEF::PE::ResourceNode::has_name
bool has_name() const
True if the entry uses a name as ID
Definition ResourceNode.hpp:129
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:107
LIEF::PE::ResourceNode::childs
it_childs childs()
Iterator on node's children.
Definition ResourceNode.hpp:120
LIEF::PE::ResourceNode::swap
void swap(ResourceNode &other)
LIEF::PE::ResourceNode::operator!=
friend bool operator!=(const ResourceNode &LHS, const ResourceNode &RHS)
Definition ResourceNode.hpp:232
LIEF::PE::ResourceNode::name
void name(std::u16string name)
Definition ResourceNode.hpp:166
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:94
LIEF::PE::ResourceNode::name
void name(const std::string &name)
LIEF::PE::ResourceNode::id
void id(uint32_t id)
Definition ResourceNode.hpp:160
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:197
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