LIEF: Library to Instrument Executable Formats Version 1.0.0
Loading...
Searching...
No Matches
iostream.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_OSTREAM_H
17#define LIEF_OSTREAM_H
18#include <limits>
19#include <cassert>
20#include <ios>
21#include <cstdint>
22#include <cstring>
23#include <vector>
24#include <array>
25
26#include "LIEF/visibility.h"
27#include "LIEF/span.hpp"
28#include "LIEF/optional.hpp"
30
31namespace LIEF {
33 public:
34 static size_t uleb128_size(uint64_t value);
35 static size_t sleb128_size(int64_t value);
36
37 using pos_type = std::streampos;
38 using off_type = std::streamoff;
39 enum class RELOC_OP {
41 };
42
43 vector_iostream() = default;
44 vector_iostream(std::vector<uint8_t>& ref) :
45 raw_(&ref)
46 {}
48 endian_swap_(endian_swap)
49 {}
50
52 raw_->reserve(size);
53 return *this;
54 }
55
57 raw_->reserve(raw_->size() + size);
58 return *this;
59 }
60
61 vector_iostream& put(uint8_t c);
62 vector_iostream& write(const uint8_t* s, std::streamsize n);
64 return write(sp.data(), sp.size());
65 }
66
67 vector_iostream& write(std::vector<uint8_t> s) {
68 if (s.empty()) {
69 return *this;
70 }
71 return write(s.data(), s.size());
72 }
73
74 vector_iostream& write(const std::string& s) {
75 return write(reinterpret_cast<const uint8_t*>(s.c_str()), s.size() + 1);
76 }
77
78 bool empty() const {
79 return raw_->empty();
80 }
81
82 vector_iostream& write(const std::u16string& s, bool with_null_char);
83
84 vector_iostream& write(size_t count, uint8_t value) {
85 raw_->insert(raw_->end(), count, value);
86 current_pos_ += count;
87 return *this;
88 }
89 vector_iostream& write_sized_int(uint64_t value, size_t size) {
90 const uint64_t stack_val = value;
91 return write(reinterpret_cast<const uint8_t*>(&stack_val), size);
92 }
93
95 return write(other.data());
96 }
97
98 template<class T, typename = typename std::enable_if<std::is_standard_layout<T>::value && std::is_trivial<T>::value>::type>
99 vector_iostream& write(const T& t) {
100 const auto pos = static_cast<size_t>(tellp());
101 if (raw_->size() < (pos + sizeof(T))) {
102 raw_->resize(pos + sizeof(T));
103 }
104 if (endian_swap_) {
105 T tmp = t;
106 swap_endian(&tmp);
107 memcpy(raw_->data() + pos, &tmp, sizeof(T));
108 } else {
109 memcpy(raw_->data() + pos, &t, sizeof(T));
110 }
111 current_pos_ += sizeof(T);
112 return *this;
113 }
114
115 vector_iostream& align(size_t alignment, uint8_t fill = 0);
116
117 template<typename T>
118 vector_iostream& write(const std::pair<T, T>& p) {
119 write(p.first);
120 write(p.second);
121 return *this;
122 }
123
124 template<typename T, size_t size>
125 vector_iostream& write(const std::array<T, size>& t) {
126 static_assert(std::numeric_limits<T>::is_integer, "Requires integer type");
127 for (T val : t) {
128 write<T>(val);
129 }
130 return *this;
131 }
132
133 template<typename T>
134 vector_iostream& write(const std::vector<T>& elements) {
135 for (const T& e : elements) {
136 write(e);
137 }
138 return *this;
139 }
140
141 template<typename T, class U>
143 return opt ? write<T>(*opt) : *this;
144 }
145
148
149 vector_iostream& get(std::vector<uint8_t>& c) {
150 c = *raw_;
151 return *this;
152 }
153 vector_iostream& move(std::vector<uint8_t>& c) {
154 c = std::move(owned_);
155 return *this;
156 }
157
159 return *this;
160 }
161
162 size_t size() const {
163 return raw_->size();
164 }
165
166 // seeks:
167 pos_type tellp() const {
168 return current_pos_;
169 }
170
172 current_pos_ = p;
173 return *this;
174 }
175
177 return seekp(raw_->size());
178 }
179
180 vector_iostream& pad(size_t size, uint8_t value = 0) {
181 raw_->resize(raw_->size() + size, value);
182 return *this;
183 }
184
185 vector_iostream& seekp(vector_iostream::off_type p, std::ios_base::seekdir dir);
186
187 const std::vector<uint8_t>& raw() const {
188 return *raw_;
189 }
190
191 std::vector<uint8_t>& raw() {
192 return *raw_;
193 }
194
195 void set_endian_swap(bool swap) {
196 endian_swap_ = swap;
197 }
198
199 bool endian_swap() const {
200 return endian_swap_;
201 }
202
203 template<class T>
204 vector_iostream& reloc(uint64_t offset, T shift, RELOC_OP op = RELOC_OP::ADD) {
205 static_assert(std::numeric_limits<T>::is_integer, "Requires integer type");
206 if (offset > raw_->size() || (offset + sizeof(T) > raw_->size())) {
207 return *this;
208 }
209 T& value = *reinterpret_cast<T*>(raw_->data() + offset);
210
211 switch (op) {
212 case RELOC_OP::ADD: value += shift; break;
213 case RELOC_OP::SUB: value -= shift; break;
214 }
215
216 return *this;
217 }
218
220 fixups_.resize(count);
221 return *this;
222 }
223
224
225 vector_iostream& record_fixup(size_t id, bool cond) {
226 if (cond) {
227 fixups_[id].push_back(tellp());
228 }
229 return *this;
230 }
231
232
234 return record_fixup(id, /*cond=*/true);
235 }
236
237 template<class T>
238 vector_iostream& apply_fixup(size_t id, T value) {
239 static_assert(std::numeric_limits<T>::is_integer, "Requires integer type");
240 for (uint64_t offset : get_fixups(id)) {
241 reloc<T>(offset, value);
242 }
243 return *this;
244 }
245
246 const std::vector<uint64_t>& get_fixups(size_t id) const {
247 return fixups_[id];
248 }
249
250 template<class T>
251 T* edit_as() {
252 assert(((size_t)current_pos_ + sizeof(T)) <= raw_->size());
253 return reinterpret_cast<T*>(raw_->data() + current_pos_);
254 }
255
256 template<class T>
257 T* edit_as(size_t pos) {
258 seekp(pos);
259 assert(((size_t)current_pos_ + sizeof(T)) <= raw_->size());
260 return reinterpret_cast<T*>(raw_->data() + current_pos_);
261 }
262
263 const vector_iostream& copy_into(const span<uint8_t>& sp, size_t sz) const {
264 assert(sz <= raw_->size());
265 std::copy(raw_->data(), raw_->data() + sz, sp.data());
266 return *this;
267 }
268
269 const vector_iostream& copy_into(const span<uint8_t>& sp) const {
270 if (sp.size() < this->size()) {
271 return *this;
272 }
273
274 std::copy(raw_->begin(), raw_->end(), sp.begin());
275 return *this;
276 }
277
279 return *raw_;
280 }
281
283 raw_->clear();
284 return *this;
285 }
286
287 std::vector<uint8_t>::iterator begin() {
288 return raw_->begin();
289 }
290
291 std::vector<uint8_t>::iterator end() {
292 return raw_->end();
293 }
294
295 std::vector<uint8_t>::const_iterator begin() const {
296 return raw_->begin();
297 }
298
299 std::vector<uint8_t>::const_iterator end() const {
300 return raw_->end();
301 }
302
303 private:
304 std::vector<std::vector<uint64_t>> fixups_;
305 pos_type current_pos_ = 0;
306 std::vector<uint8_t> owned_;
307 std::vector<uint8_t>* raw_ = &owned_;
308 bool endian_swap_ = false;
309};
310
312 public:
313 ScopeOStream(const ScopeOStream&) = delete;
315
318
319 explicit ScopeOStream(vector_iostream& stream, uint64_t pos) :
320 pos_{stream.tellp()},
321 stream_{stream}
322 {
323 stream_.seekp(pos);
324 }
325
326 explicit ScopeOStream(vector_iostream& stream) :
327 pos_{stream.tellp()},
328 stream_{stream}
329 {}
330
332 stream_.seekp(pos_);
333 }
334
336 return &stream_;
337 }
338
340 return stream_;
341 }
342
343 const vector_iostream& operator*() const {
344 return stream_;
345 }
346
347 private:
348 std::streampos pos_ = 0;
349 vector_iostream& stream_;
350};
351
352
353
354}
355#endif
vector_iostream & operator*()
Definition iostream.hpp:339
ScopeOStream(vector_iostream &stream)
Definition iostream.hpp:326
vector_iostream * operator->()
Definition iostream.hpp:335
ScopeOStream(const ScopeOStream &)=delete
ScopeOStream & operator=(const ScopeOStream &)=delete
~ScopeOStream()
Definition iostream.hpp:331
ScopeOStream(vector_iostream &stream, uint64_t pos)
Definition iostream.hpp:319
ScopeOStream & operator=(ScopeOStream &&)=delete
const vector_iostream & operator*() const
Definition iostream.hpp:343
ScopeOStream(ScopeOStream &&)=delete
Definition optional.hpp:23
Definition iostream.hpp:32
std::vector< uint8_t > & raw()
Definition iostream.hpp:191
vector_iostream & reloc(uint64_t offset, T shift, RELOC_OP op=RELOC_OP::ADD)
Definition iostream.hpp:204
static size_t sleb128_size(int64_t value)
vector_iostream & flush()
Definition iostream.hpp:158
std::vector< uint8_t >::iterator end()
Definition iostream.hpp:291
vector_iostream & seekp(vector_iostream::off_type p, std::ios_base::seekdir dir)
const vector_iostream & copy_into(const span< uint8_t > &sp) const
Definition iostream.hpp:269
const std::vector< uint8_t > & raw() const
Definition iostream.hpp:187
bool empty() const
Definition iostream.hpp:78
vector_iostream & write(const std::array< T, size > &t)
Definition iostream.hpp:125
vector_iostream & seek_end()
Definition iostream.hpp:176
vector_iostream & apply_fixup(size_t id, T value)
Definition iostream.hpp:238
size_t size() const
Definition iostream.hpp:162
vector_iostream & put(uint8_t c)
vector_iostream & write_uleb128(uint64_t value)
vector_iostream & write(const uint8_t *s, std::streamsize n)
vector_iostream & seekp(pos_type p)
Definition iostream.hpp:171
vector_iostream & write(const std::u16string &s, bool with_null_char)
vector_iostream & clear()
Definition iostream.hpp:282
bool endian_swap() const
Definition iostream.hpp:199
vector_iostream & write(const optional< U > &opt)
Definition iostream.hpp:142
span< const uint8_t > data() const
Definition iostream.hpp:278
vector_iostream & increase_capacity(size_t size)
Definition iostream.hpp:56
vector_iostream & init_fixups(size_t count)
Definition iostream.hpp:219
vector_iostream & move(std::vector< uint8_t > &c)
Definition iostream.hpp:153
T * edit_as(size_t pos)
Definition iostream.hpp:257
vector_iostream & write_sized_int(uint64_t value, size_t size)
Definition iostream.hpp:89
vector_iostream & write(const std::pair< T, T > &p)
Definition iostream.hpp:118
vector_iostream & write(const std::string &s)
Definition iostream.hpp:74
std::streampos pos_type
Definition iostream.hpp:37
vector_iostream & record_fixup(size_t id)
Definition iostream.hpp:233
vector_iostream & record_fixup(size_t id, bool cond)
Definition iostream.hpp:225
const std::vector< uint64_t > & get_fixups(size_t id) const
Definition iostream.hpp:246
pos_type tellp() const
Definition iostream.hpp:167
vector_iostream & write(const vector_iostream &other)
Definition iostream.hpp:94
vector_iostream & write(span< const uint8_t > sp)
Definition iostream.hpp:63
vector_iostream & align(size_t alignment, uint8_t fill=0)
vector_iostream & reserve(size_t size)
Definition iostream.hpp:51
void set_endian_swap(bool swap)
Definition iostream.hpp:195
RELOC_OP
Definition iostream.hpp:39
@ SUB
Definition iostream.hpp:40
@ ADD
Definition iostream.hpp:40
vector_iostream & write(std::vector< uint8_t > s)
Definition iostream.hpp:67
vector_iostream & write(const T &t)
Definition iostream.hpp:99
std::vector< uint8_t >::const_iterator end() const
Definition iostream.hpp:299
T * edit_as()
Definition iostream.hpp:251
static size_t uleb128_size(uint64_t value)
std::streamoff off_type
Definition iostream.hpp:38
std::vector< uint8_t >::iterator begin()
Definition iostream.hpp:287
vector_iostream & write(const std::vector< T > &elements)
Definition iostream.hpp:134
vector_iostream(std::vector< uint8_t > &ref)
Definition iostream.hpp:44
vector_iostream & write_sleb128(int64_t value)
const vector_iostream & copy_into(const span< uint8_t > &sp, size_t sz) const
Definition iostream.hpp:263
std::vector< uint8_t >::const_iterator begin() const
Definition iostream.hpp:295
vector_iostream & write(size_t count, uint8_t value)
Definition iostream.hpp:84
vector_iostream(bool endian_swap)
Definition iostream.hpp:47
vector_iostream & pad(size_t size, uint8_t value=0)
Definition iostream.hpp:180
vector_iostream & get(std::vector< uint8_t > &c)
Definition iostream.hpp:149
LIEF namespace.
Definition Abstract/Binary.hpp:40
tcb::span< ElementType, Extent > span
Definition span.hpp:22
void swap_endian(T *)
Definition endianness_support.hpp:117
#define LIEF_API
Definition visibility.h:41