libfilezilla
Loading...
Searching...
No Matches
buffer.hpp
Go to the documentation of this file.
1#ifndef LIBFILEZILLA_BUFFER_HEADER
2#define LIBFILEZILLA_BUFFER_HEADER
3
4#include "libfilezilla.hpp"
5
6#include <vector>
7#include <type_traits>
8
12
13namespace fz {
14
16enum class buffer_wipe_policy {
17 normal,
18 wipe_on_release
19};
20
32template<buffer_wipe_policy Policy>
33class basic_buffer
34{
35public:
36 using value_type = unsigned char;
37
38 basic_buffer() noexcept = default;
39
41 explicit basic_buffer(size_t capacity);
42
43 explicit basic_buffer(std::string_view const& data);
44
45 basic_buffer(basic_buffer const& buf);
46 basic_buffer(basic_buffer && buf) noexcept;
47
48 basic_buffer& operator=(basic_buffer const& buf);
49 basic_buffer& operator=(basic_buffer && buf) noexcept;
50
51 basic_buffer& operator=(std::string_view const& v);
52
53protected:
54 ~basic_buffer();
55
56public:
58 unsigned char const* get() const { return pos_; }
59 unsigned char* get() { return pos_; }
60
62 unsigned char* data() { return pos_; }
63 unsigned char const* data() const { return pos_; }
64
83 unsigned char* get(size_t write_size);
84
86 void add(size_t added);
87
94 template<typename T, std::enable_if_t<std::is_signed_v<T>, int> = 0>
95 void add(T added) {
96 if (added > 0) {
97 add(static_cast<size_t>(added));
98 }
99 }
100
105 void consume(size_t consumed);
106
107 size_t size() const { return size_; }
108
112 void clear();
113
114 void clear_and_free();
115
120 void append(unsigned char const* data, size_t len);
121 void append(std::string_view const& str);
122 void append(std::basic_string_view<uint8_t> const& data);
123 void append(std::vector<uint8_t> const& data);
124 void append(basic_buffer const& b);
125 void append(unsigned char v);
126 void append(size_t len, unsigned char c);
127
128 void push_back(unsigned char v) {
129 return append(v);
130 }
131
132 basic_buffer& operator+=(unsigned char v) {
133 append(v);
134 return *this;
135 }
136 basic_buffer& operator+=(std::string_view const& str) {
137 append(str);
138 return *this;
139 }
140 basic_buffer& operator+=(std::vector<uint8_t> const& data) {
141 append(data);
142 return *this;
143 }
144 basic_buffer& operator+=(basic_buffer const& b) {
145 append(b);
146 return *this;
147 }
148
149 bool empty() const { return size_ == 0; }
150 explicit operator bool() const {
151 return size_ != 0;
152 }
153
154 size_t capacity() const { return capacity_; }
155 void reserve(size_t capacity);
156
157 void resize(size_t size);
158
160 unsigned char operator[](size_t i) const { return pos_[i]; }
161 unsigned char & operator[](size_t i) { return pos_[i]; }
162
163 bool operator==(basic_buffer const& rhs) const {
164 return *this == rhs.to_view();
165 }
166
167 bool operator==(std::string_view const& rhs) const;
168
169 bool operator!=(basic_buffer const& rhs) const {
170 return !(*this == rhs);
171 }
172 bool operator!=(std::string_view const& rhs) const {
173 return !(*this == rhs);
174 }
175
176 std::string_view to_view() const;
177
178 void wipe();
179 void wipe_unused();
180
181private:
182
183 // Invariants:
184 // size_ <= capacity_
185 // data_ <= pos_
186 // pos_ <= data_ + capacity_
187 // pos_ + size_ <= data_ + capacity_
188 unsigned char* data_{};
189 unsigned char* pos_{};
190 size_t size_{};
191 size_t capacity_{};
192};
193
194#if BUILDING_LIBFILEZILLA
195extern template class FZ_PUBLIC_SYMBOL basic_buffer<buffer_wipe_policy::normal>;
196extern template class FZ_PUBLIC_SYMBOL basic_buffer<buffer_wipe_policy::wipe_on_release>;
197#endif
198
199class FZ_PUBLIC_SYMBOL buffer final : public basic_buffer<buffer_wipe_policy::normal>
200{
201public:
202 using basic_buffer::basic_buffer;
203 using basic_buffer::operator=;
204};
205
208class FZ_PUBLIC_SYMBOL secure_buffer final : public basic_buffer<buffer_wipe_policy::wipe_on_release>
209{
210public:
211 using basic_buffer::basic_buffer;
212 using basic_buffer::operator=;
213};
214
215inline void wipe(buffer & b) {
216 b.wipe();
217}
218
219inline void wipe_unused(buffer & b) {
220 b.wipe_unused();
221}
222
223inline void wipe(secure_buffer & b) {
224 b.wipe();
225}
226
227inline void wipe_unused(secure_buffer & b) {
228 b.wipe_unused();
229}
230
231}
232
233#endif
The buffer class is a simple buffer where data can be appended at the end and consumed at the front....
Definition buffer.hpp:34
void add(T added)
Overload of add for signed types, only adds if value is positive.
Definition buffer.hpp:95
unsigned char * data()
Same as get().
Definition buffer.hpp:62
void consume(size_t consumed)
Removes consumed bytes from the beginning of the buffer.
unsigned char * get(size_t write_size)
Returns a writable buffer guaranteed to be large enough for write_size bytes, call add when done.
basic_buffer(size_t capacity)
Initially reserves the passed capacity.
unsigned char const * get() const
Undefined if buffer is empty.
Definition buffer.hpp:58
void append(unsigned char const *data, size_t len)
Appends the passed data to the buffer.
unsigned char operator[](size_t i) const
Gets element at offset i. Does not do bounds checking.
Definition buffer.hpp:160
void add(size_t added)
Increase size by the passed amount. Call this after having obtained a writable buffer with get(size_t...
Definition buffer.hpp:200
Definition buffer.hpp:209
Sets some global macros and further includes string.hpp.
The namespace used by libfilezilla.
Definition apply.hpp:17