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