forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHandler.cpp
More file actions
182 lines (144 loc) · 5.1 KB
/
Copy pathHandler.cpp
File metadata and controls
182 lines (144 loc) · 5.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/* Copyright 2017 - 2025 R. Thomas
* Copyright 2017 - 2025 Quarkslab
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <algorithm>
#include <utility>
#include "logging.hpp"
#include "LIEF/BinaryStream/MemoryStream.hpp"
#include "LIEF/BinaryStream/VectorStream.hpp"
#include "LIEF/BinaryStream/SpanStream.hpp"
#include "LIEF/BinaryStream/FileStream.hpp"
#include "ELF/DataHandler/Handler.hpp"
namespace LIEF {
namespace ELF {
namespace DataHandler {
class DataHandlerStream : public BinaryStream {
public:
DataHandlerStream(std::vector<uint8_t>& ref) :
BinaryStream(STREAM_TYPE::ELF_DATA_HANDLER),
data_{ref}
{
}
~DataHandlerStream() override = default;
uint64_t size() const override {
return data_.size();
}
result<const void*> read_at(uint64_t offset, uint64_t size, uint64_t /*va*/) const override {
if (offset > data_.size() || (offset + size) > data_.size()) {
return make_error_code(lief_errors::read_error);
}
return data_.data() + offset;
}
private:
std::vector<uint8_t>& data_;
};
result<std::unique_ptr<Handler>> Handler::from_stream(std::unique_ptr<BinaryStream>& stream) {
auto hdl = std::unique_ptr<Handler>(new Handler{});
if (VectorStream::classof(*stream)) {
auto& vs = static_cast<VectorStream&>(*stream);
hdl->data_ = std::move(vs.move_content());
const uint64_t pos = vs.pos();
stream = std::make_unique<DataHandlerStream>(hdl->data_);
stream->setpos(pos);
return hdl;
}
if (SpanStream::classof(*stream)) {
auto& vs = static_cast<SpanStream&>(*stream);
hdl->data_ = vs.content();
return hdl;
}
if (FileStream::classof(*stream)) {
auto& vs = static_cast<FileStream&>(*stream);
hdl->data_ = vs.content();
const uint64_t pos = vs.pos();
stream = std::make_unique<DataHandlerStream>(hdl->data_);
stream->setpos(pos);
return hdl;
}
if (MemoryStream::classof(*stream)) {
return make_error_code(lief_errors::not_implemented);
}
LIEF_ERR("Unknown stream for Handler");
return make_error_code(lief_errors::not_supported);
}
bool Handler::has(uint64_t offset, uint64_t size, Node::Type type) {
Node tmp{offset, size, type};
const auto it_node = std::find_if(std::begin(nodes_), std::end(nodes_),
[&tmp] (const std::unique_ptr<Node>& node) {
return tmp == *node;
});
return it_node != std::end(nodes_);
}
result<Handler::ref_t<Node>> Handler::get(uint64_t offset, uint64_t size, Node::Type type) {
Node tmp{offset, size, type};
const auto it_node = std::find_if(std::begin(nodes_), std::end(nodes_),
[&tmp] (const std::unique_ptr<Node>& node) {
return tmp == *node;
});
if (it_node == std::end(nodes_)) {
return make_error_code(lief_errors::not_found);
}
return **it_node;
}
void Handler::remove(uint64_t offset, uint64_t size, Node::Type type) {
Node tmp{offset, size, type};
const auto it_node = std::find_if(std::begin(nodes_), std::end(nodes_),
[&tmp] (const std::unique_ptr<Node>& node) {
return tmp == *node;
});
if (it_node == std::end(nodes_)) {
LIEF_ERR("Unable to find the node");
}
nodes_.erase(it_node);
}
Node& Handler::create(uint64_t offset, uint64_t size, Node::Type type) {
nodes_.push_back(std::make_unique<Node>(offset, size, type));
return *nodes_.back();
}
Node& Handler::add(const Node& node) {
nodes_.push_back(std::make_unique<Node>(node));
return *nodes_.back();
}
ok_error_t Handler::make_hole(uint64_t offset, uint64_t size) {
auto res = reserve(offset, size);
if (!res) {
return res;
}
data_.insert(std::begin(data_) + offset, size, 0);
return ok();
}
ok_error_t Handler::reserve(uint64_t offset, uint64_t size) {
static constexpr auto MAX_MEMORY_SIZE = 6_GB;
const auto full_size = static_cast<int64_t>(offset) +
static_cast<int64_t>(size);
if (full_size < 0) {
return make_error_code(lief_errors::corrupted);
}
if (static_cast<uint64_t>(full_size) > data_.max_size()) {
return make_error_code(lief_errors::corrupted);
}
if (static_cast<uint64_t>(full_size) > MAX_MEMORY_SIZE) {
return make_error_code(lief_errors::corrupted);
}
const bool must_resize = data_.size() < (offset + size);
if (!must_resize) {
return ok();
}
data_.resize(offset + size, 0);
return ok();
}
} // namespace DataHandler
} // namespace ELF
} // namespace LIEF