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