LIEF: Library to Instrument Executable Formats Version 0.15.1
Loading...
Searching...
No Matches
iostream.hpp
Go to the documentation of this file.
1/* Copyright 2017 - 2024 R. Thomas
2 * Copyright 2017 - 2024 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 <ios>
20#include <cstdint>
21#include <cstring>
22#include <vector>
23#include <array>
24
25#include "LIEF/span.hpp"
27
28namespace LIEF {
30 public:
31 static size_t uleb128_size(uint64_t value);
32 static size_t sleb128_size(int64_t value);
33 using pos_type = std::streampos;
34 using off_type = std::streamoff;
35 vector_iostream() = default;
36 vector_iostream(bool endian_swap) :
37 endian_swap_(endian_swap)
38 {}
39 void reserve(size_t size) {
40 raw_.reserve(size);
41 }
42
43 vector_iostream& put(uint8_t c);
44 vector_iostream& write(const uint8_t* s, std::streamsize n);
46 return write(sp.data(), sp.size());
47 }
48
49 vector_iostream& write(std::vector<uint8_t> s);
50 vector_iostream& write(const std::string& s);
51 vector_iostream& write(size_t count, uint8_t value) {
52 raw_.insert(std::end(raw_), count, value);
53 current_pos_ += count;
54 return *this;
55 }
56 vector_iostream& write_sized_int(uint64_t value, size_t size) {
57 const uint64_t stack_val = value;
58 return write(reinterpret_cast<const uint8_t*>(&stack_val), size);
59 }
61 return write(other.raw());
62 }
63
64 template<class T, typename = typename std::enable_if<std::is_standard_layout<T>::value && std::is_trivial<T>::value>::type>
65 vector_iostream& write(const T& t) {
66 const auto pos = static_cast<size_t>(tellp());
67 if (raw_.size() < (pos + sizeof(T))) {
68 raw_.resize(pos + sizeof(T));
69 }
70 memcpy(raw_.data() + pos, &t, sizeof(T));
71 current_pos_ += sizeof(T);
72 return *this;
73 }
74
75 template<typename T>
76 vector_iostream& write_conv(const T& t);
77
78 template<typename T>
79 vector_iostream& write_conv_array(const std::vector<T>& v);
80
81 vector_iostream& align(size_t alignment, uint8_t fill = 0);
82
83 template<typename T, size_t size>
84 vector_iostream& write(const std::array<T, size>& t) {
85 static_assert(std::numeric_limits<T>::is_integer, "Requires integer type");
86 for (T val : t) {
87 write<T>(val);
88 }
89 return *this;
90 }
91
92
93 template<typename T>
94 vector_iostream& write(const std::vector<T>& elements) {
95 for (const T& e : elements) {
96 write(e);
97 }
98 return *this;
99 }
100
103
104 vector_iostream& get(std::vector<uint8_t>& c) {
105 c = raw_;
106 return *this;
107 }
108 vector_iostream& move(std::vector<uint8_t>& c) {
109 c = std::move(raw_);
110 return *this;
111 }
112
114 return *this;
115 }
116
117 size_t size() const {
118 return raw_.size();
119 }
120
121 // seeks:
122 pos_type tellp() const {
123 return current_pos_;
124 }
125
127 current_pos_ = p;
128 return *this;
129 }
130
131 vector_iostream& seekp(vector_iostream::off_type p, std::ios_base::seekdir dir);
132
133 const std::vector<uint8_t>& raw() const {
134 return raw_;
135 }
136
137 std::vector<uint8_t>& raw() {
138 return raw_;
139 }
140
141 void set_endian_swap(bool swap) {
142 endian_swap_ = swap;
143 }
144
145 private:
146 pos_type current_pos_ = 0;
147 std::vector<uint8_t> raw_;
148 bool endian_swap_ = false;
149};
150
151
152template<typename T>
154 const uint8_t *ptr = nullptr;
155 T tmp = t;
156 if (endian_swap_) {
158 ptr = reinterpret_cast<const uint8_t*>(&tmp);
159 } else {
160 ptr = reinterpret_cast<const uint8_t*>(&t);
161 }
162 write(ptr, sizeof(T));
163 return *this;
164}
165
166template<typename T>
168 for (const T& i: v) {
169 const uint8_t* ptr = nullptr;
170 T tmp = i;
171 if (endian_swap_) {
173 ptr = reinterpret_cast<const uint8_t*>(&tmp);
174 } else {
175 ptr = reinterpret_cast<const uint8_t*>(&i);
176 }
177 write(ptr, sizeof(T));
178 }
179 return *this;
180}
181
182
183}
184#endif
Definition iostream.hpp:29
std::vector< uint8_t > & raw()
Definition iostream.hpp:137
static size_t sleb128_size(int64_t value)
vector_iostream & flush()
Definition iostream.hpp:113
vector_iostream & seekp(vector_iostream::off_type p, std::ios_base::seekdir dir)
const std::vector< uint8_t > & raw() const
Definition iostream.hpp:133
vector_iostream & write(const std::array< T, size > &t)
Definition iostream.hpp:84
vector_iostream & write_conv(const T &t)
Definition iostream.hpp:153
size_t size() const
Definition iostream.hpp:117
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:126
void reserve(size_t size)
Definition iostream.hpp:39
vector_iostream & move(std::vector< uint8_t > &c)
Definition iostream.hpp:108
vector_iostream & write_sized_int(uint64_t value, size_t size)
Definition iostream.hpp:56
vector_iostream & write(const std::string &s)
std::streampos pos_type
Definition iostream.hpp:33
vector_iostream & write_conv_array(const std::vector< T > &v)
Definition iostream.hpp:167
pos_type tellp() const
Definition iostream.hpp:122
vector_iostream & write(const vector_iostream &other)
Definition iostream.hpp:60
vector_iostream & write(span< const uint8_t > sp)
Definition iostream.hpp:45
vector_iostream & align(size_t alignment, uint8_t fill=0)
void set_endian_swap(bool swap)
Definition iostream.hpp:141
vector_iostream & write(std::vector< uint8_t > s)
vector_iostream & write(const T &t)
Definition iostream.hpp:65
static size_t uleb128_size(uint64_t value)
std::streamoff off_type
Definition iostream.hpp:34
vector_iostream & write(const std::vector< T > &elements)
Definition iostream.hpp:94
vector_iostream & write_sleb128(int64_t value)
vector_iostream & write(size_t count, uint8_t value)
Definition iostream.hpp:51
vector_iostream(bool endian_swap)
Definition iostream.hpp:36
vector_iostream & get(std::vector< uint8_t > &c)
Definition iostream.hpp:104
void swap_endian(T *)
LIEF namespace.
Definition Abstract/Binary.hpp:32
tcb::span< ElementType, Extent > span
Definition span.hpp:22