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) {}
50 endian_swap_(endian_swap) {}
51
53 raw_->reserve(size);
54 return *this;
55 }
56
58 raw_->reserve(raw_->size() + size);
59 return *this;
60 }
61
62 vector_iostream& put(uint8_t c);
63 vector_iostream& write(const uint8_t* s, std::streamsize n);
65 return write(sp.data(), sp.size());
66 }
67
68 vector_iostream& write(const std::vector<uint8_t>& s) {
69 if (s.empty()) {
70 return *this;
71 }
72 return write(s.data(), s.size());
73 }
74
75 vector_iostream& write(const std::string& s) {
76 return write(reinterpret_cast<const uint8_t*>(s.c_str()), s.size() + 1);
77 }
78
79 vector_iostream& write(const std::string_view& s) {
80 write(reinterpret_cast<const uint8_t*>(s.data()), s.size());
81 return write<uint8_t>(0);
82 }
83
84 bool empty() const {
85 return raw_->empty();
86 }
87
88 vector_iostream& write(const std::u16string& s, bool with_null_char);
89
90 vector_iostream& write(size_t count, uint8_t value) {
91 size_t pos = 0;
92 if (!checked_write_pos(count, pos) || count > raw_->max_size() - raw_->size())
93 {
94 return *this;
95 }
96
97 raw_->insert(raw_->end(), count, value);
98 current_pos_ = (off_type)(pos + count);
99 return *this;
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 vector_iostream& move(std::vector<uint8_t>& c) {
172 c = std::move(owned_);
173 return *this;
174 }
175
177 return *this;
178 }
179
180 size_t size() const {
181 return raw_->size();
182 }
183
184 // seeks:
185 pos_type tellp() const {
186 return current_pos_;
187 }
188
190 if ((off_type)p < 0) {
191 return *this;
192 }
193 current_pos_ = p;
194 return *this;
195 }
196
198 return seekp(raw_->size());
199 }
200
201 vector_iostream& pad(size_t size, uint8_t value = 0) LIEF_LIFETIMEBOUND {
202 raw_->resize(raw_->size() + size, value);
203 return *this;
204 }
205
206 vector_iostream& seekp(vector_iostream::off_type p, std::ios_base::seekdir dir);
207
208 const std::vector<uint8_t>& raw() const {
209 return *raw_;
210 }
211
212 std::vector<uint8_t>& raw() {
213 return *raw_;
214 }
215
216 void set_endian_swap(bool swap) {
217 endian_swap_ = swap;
218 }
219
220 bool endian_swap() const {
221 return endian_swap_;
222 }
223
224 template<class T>
225 vector_iostream& reloc(uint64_t offset, T shift, RELOC_OP op = RELOC_OP::ADD) {
226 static_assert(std::numeric_limits<T>::is_integer, "Requires integer type");
227 if (offset > raw_->size() || sizeof(T) > raw_->size() - offset) {
228 return *this;
229 }
230 T& value = *reinterpret_cast<T*>(raw_->data() + offset);
231
232 switch (op) {
233 case RELOC_OP::ADD: value += shift; break;
234 case RELOC_OP::SUB: value -= shift; break;
235 }
236
237 return *this;
238 }
239
241 fixups_.resize(count);
242 return *this;
243 }
244
245
246 vector_iostream& record_fixup(size_t id, bool cond) {
247 if (cond) {
248 fixups_[id].push_back(tellp());
249 }
250 return *this;
251 }
252
253
255 return record_fixup(id, /*cond=*/true);
256 }
257
258 template<class T>
259 vector_iostream& apply_fixup(size_t id, T value) {
260 static_assert(std::numeric_limits<T>::is_integer, "Requires integer type");
261 for (uint64_t offset : get_fixups(id)) {
262 reloc<T>(offset, value);
263 }
264 return *this;
265 }
266
267 const std::vector<uint64_t>& get_fixups(size_t id) const {
268 return fixups_[id];
269 }
270
271 template<class T>
272 T* edit_as() {
273 size_t pos = 0;
274 if (!checked_write_pos(sizeof(T), pos) || pos > raw_->size() ||
275 sizeof(T) > raw_->size() - pos)
276 {
277 return nullptr;
278 }
279 return reinterpret_cast<T*>(raw_->data() + pos);
280 }
281
282 template<class T>
283 T* edit_as(size_t pos) {
284 if ((uintmax_t)pos > (uintmax_t)((std::numeric_limits<off_type>::max)())) {
285 return nullptr;
286 }
287 seekp((pos_type)pos);
288 return edit_as<T>();
289 }
290
291 const vector_iostream& copy_into(const span<uint8_t>& sp, size_t sz) const {
292 assert(sz <= raw_->size());
293 std::copy(raw_->data(), raw_->data() + sz, sp.data());
294 return *this;
295 }
296
297 const vector_iostream& copy_into(const span<uint8_t>& sp) const {
298 if (sp.size() < this->size()) {
299 return *this;
300 }
301
302 std::copy(raw_->begin(), raw_->end(), sp.begin());
303 return *this;
304 }
305
307 return *raw_;
308 }
309
311 raw_->clear();
312 return *this;
313 }
314
315 std::vector<uint8_t>::iterator begin() {
316 return raw_->begin();
317 }
318
319 std::vector<uint8_t>::iterator end() {
320 return raw_->end();
321 }
322
323 std::vector<uint8_t>::const_iterator begin() const {
324 return raw_->begin();
325 }
326
327 std::vector<uint8_t>::const_iterator end() const {
328 return raw_->end();
329 }
330
331 private:
332 bool checked_write_pos(size_t count, size_t& pos) const {
333 const auto offset = (off_type)current_pos_;
334 if (offset < 0) {
335 return false;
336 }
337
338 const auto unsigned_offset = (uintmax_t)offset;
339 const auto max_vector_pos = (uintmax_t)raw_->max_size();
340 const auto max_stream_pos =
341 (uintmax_t)((std::numeric_limits<off_type>::max)());
342 const uintmax_t max_pos = (std::min)(max_vector_pos, max_stream_pos);
343
344 if (unsigned_offset > max_pos || (uintmax_t)count > max_pos - unsigned_offset)
345 {
346 return false;
347 }
348
349 pos = (size_t)unsigned_offset;
350 return true;
351 }
352
353 std::vector<std::vector<uint64_t>> fixups_;
354 pos_type current_pos_ = 0;
355 std::vector<uint8_t> owned_;
356 std::vector<uint8_t>* raw_ = &owned_;
357 bool endian_swap_ = false;
358};
359
361 public:
362 ScopeOStream(const ScopeOStream&) = delete;
364
367
368 explicit ScopeOStream(vector_iostream& stream, uint64_t pos) :
369 pos_{stream.tellp()},
370 stream_{stream} {
371 stream_.seekp(pos);
372 }
373
374 explicit ScopeOStream(vector_iostream& stream) :
375 pos_{stream.tellp()},
376 stream_{stream} {}
377
379 stream_.seekp(pos_);
380 }
381
383 return &stream_;
384 }
385
387 return stream_;
388 }
389
390 const vector_iostream& operator*() const {
391 return stream_;
392 }
393
394 private:
395 std::streampos pos_ = 0;
396 vector_iostream& stream_;
397};
398
399
400}
401#endif
vector_iostream & operator*()
Definition iostream.hpp:386
ScopeOStream(vector_iostream &stream)
Definition iostream.hpp:374
vector_iostream * operator->()
Definition iostream.hpp:382
ScopeOStream(const ScopeOStream &)=delete
ScopeOStream & operator=(const ScopeOStream &)=delete
~ScopeOStream()
Definition iostream.hpp:378
ScopeOStream(vector_iostream &stream, uint64_t pos)
Definition iostream.hpp:368
ScopeOStream & operator=(ScopeOStream &&)=delete
const vector_iostream & operator*() const
Definition iostream.hpp:390
ScopeOStream(ScopeOStream &&)=delete
Definition iostream.hpp:34
std::vector< uint8_t > & raw()
Definition iostream.hpp:212
vector_iostream & reloc(uint64_t offset, T shift, RELOC_OP op=RELOC_OP::ADD)
Definition iostream.hpp:225
static size_t sleb128_size(int64_t value)
vector_iostream & write(const std::vector< uint8_t > &s)
Definition iostream.hpp:68
vector_iostream & flush()
Definition iostream.hpp:176
std::vector< uint8_t >::iterator end()
Definition iostream.hpp:319
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:297
const std::vector< uint8_t > & raw() const
Definition iostream.hpp:208
bool empty() const
Definition iostream.hpp:84
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:197
vector_iostream & apply_fixup(size_t id, T value)
Definition iostream.hpp:259
size_t size() const
Definition iostream.hpp:180
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:189
vector_iostream & write(const std::u16string &s, bool with_null_char)
vector_iostream & clear()
Definition iostream.hpp:310
bool endian_swap() const
Definition iostream.hpp:220
span< const uint8_t > data() const
Definition iostream.hpp:306
vector_iostream & increase_capacity(size_t size)
Definition iostream.hpp:57
vector_iostream & init_fixups(size_t count)
Definition iostream.hpp:240
vector_iostream & move(std::vector< uint8_t > &c)
Definition iostream.hpp:171
T * edit_as(size_t pos)
Definition iostream.hpp:283
vector_iostream & write(const std::string_view &s)
Definition iostream.hpp:79
vector_iostream & write_sized_int(uint64_t value, size_t size)
Definition iostream.hpp:101
vector_iostream & write(const std::string &s)
Definition iostream.hpp:75
std::streampos pos_type
Definition iostream.hpp:39
vector_iostream & record_fixup(size_t id)
Definition iostream.hpp:254
vector_iostream & record_fixup(size_t id, bool cond)
Definition iostream.hpp:246
const std::vector< uint64_t > & get_fixups(size_t id) const
Definition iostream.hpp:267
pos_type tellp() const
Definition iostream.hpp:185
vector_iostream & write(const vector_iostream &other)
Definition iostream.hpp:106
vector_iostream & write(span< const uint8_t > sp)
Definition iostream.hpp:64
vector_iostream & align(size_t alignment, uint8_t fill=0)
vector_iostream & reserve(size_t size)
Definition iostream.hpp:52
void set_endian_swap(bool swap)
Definition iostream.hpp:216
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:327
T * edit_as()
Definition iostream.hpp:272
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:315
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:291
std::vector< uint8_t >::const_iterator begin() const
Definition iostream.hpp:323
vector_iostream & write(size_t count, uint8_t value)
Definition iostream.hpp:90
vector_iostream(bool endian_swap)
Definition iostream.hpp:49
vector_iostream & pad(size_t size, uint8_t value=0)
Definition iostream.hpp:201
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:41
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