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#include <cstdint>
9
13
14namespace fz {
15
17enum class buffer_wipe_policy {
18 normal,
19 wipe_on_release
20};
21
33template<buffer_wipe_policy Policy>
34class basic_buffer
35{
36public:
37 using value_type = unsigned char;
38
39 basic_buffer() noexcept = default;
40
42 explicit basic_buffer(size_t capacity);
43
44 explicit basic_buffer(std::string_view const& data);
45
46 basic_buffer(basic_buffer const& buf);
47 basic_buffer(basic_buffer && buf) noexcept;
48
49 basic_buffer& operator=(basic_buffer const& buf);
50 basic_buffer& operator=(basic_buffer && buf) noexcept;
51
52 basic_buffer& operator=(std::string_view const& v);
53
54protected:
55 ~basic_buffer();
56
57public:
59 unsigned char const* get() const { return pos_; }
60 unsigned char* get() { return pos_; }
61
63 unsigned char* data() { return pos_; }
64 unsigned char const* data() const { return pos_; }
65
84 unsigned char* get(size_t write_size);
85
87 void add(size_t added);
88
95 template<typename T, std::enable_if_t<std::is_signed_v<T>, int> = 0>
96 void add(T added) {
97 if (added > 0) {
98 add(static_cast<size_t>(added));
99 }
100 }
101
106 void consume(size_t consumed);
107
108 size_t size() const { return size_; }
109
113 void clear();
114
115 void clear_and_free();
116
121 void append(unsigned char const* data, size_t len);
122 void append(std::string_view const& str);
123 void append(std::basic_string_view<uint8_t> const& data);
124 void append(std::vector<uint8_t> const& data);
125 void append(basic_buffer const& b);
126 void append(unsigned char v);
127 void append(size_t len, unsigned char c);
128
129 void push_back(unsigned char v) {
130 return append(v);
131 }
132
133 basic_buffer& operator+=(unsigned char v) {
134 append(v);
135 return *this;
136 }
137 basic_buffer& operator+=(std::string_view const& str) {
138 append(str);
139 return *this;
140 }
141 basic_buffer& operator+=(std::vector<uint8_t> const& data) {
142 append(data);
143 return *this;
144 }
145 basic_buffer& operator+=(basic_buffer const& b) {
146 append(b);
147 return *this;
148 }
149
150 bool empty() const { return size_ == 0; }
151 explicit operator bool() const {
152 return size_ != 0;
153 }
154
155 size_t capacity() const { return capacity_; }
156 void reserve(size_t capacity);
157
158 void resize(size_t size);
159
161 unsigned char operator[](size_t i) const { return pos_[i]; }
162 unsigned char & operator[](size_t i) { return pos_[i]; }
163
164 bool operator==(basic_buffer const& rhs) const {
165 return *this == rhs.to_view();
166 }
167
168 bool operator==(std::string_view const& rhs) const;
169
170 bool operator!=(basic_buffer const& rhs) const {
171 return !(*this == rhs);
172 }
173 bool operator!=(std::string_view const& rhs) const {
174 return !(*this == rhs);
175 }
176
177 std::string_view to_view() const;
178
179 void wipe();
180 void wipe_unused();
181
182private:
183
184 // Invariants:
185 // size_ <= capacity_
186 // data_ <= pos_
187 // pos_ <= data_ + capacity_
188 // pos_ + size_ <= data_ + capacity_
189 unsigned char* data_{};
190 unsigned char* pos_{};
191 size_t size_{};
192 size_t capacity_{};
193};
194
195#if BUILDING_LIBFILEZILLA
196extern template class FZ_PUBLIC_SYMBOL basic_buffer<buffer_wipe_policy::normal>;
197extern template class FZ_PUBLIC_SYMBOL basic_buffer<buffer_wipe_policy::wipe_on_release>;
198#endif
199
200class FZ_PUBLIC_SYMBOL buffer final : public basic_buffer<buffer_wipe_policy::normal>
201{
202public:
203 using basic_buffer::basic_buffer;
204 using basic_buffer::operator=;
205};
206
209class FZ_PUBLIC_SYMBOL secure_buffer final : public basic_buffer<buffer_wipe_policy::wipe_on_release>
210{
211public:
212 using basic_buffer::basic_buffer;
213 using basic_buffer::operator=;
214};
215
216inline void wipe(buffer & b) {
217 b.wipe();
218}
219
220inline void wipe_unused(buffer & b) {
221 b.wipe_unused();
222}
223
224inline void wipe(secure_buffer & b) {
225 b.wipe();
226}
227
228inline void wipe_unused(secure_buffer & b) {
229 b.wipe_unused();
230}
231
232}
233
234#endif
The buffer class is a simple buffer where data can be appended at the end and consumed at the front....
Definition buffer.hpp:35
void add(T added)
Overload of add for signed types, only adds if value is positive.
Definition buffer.hpp:96
unsigned char * data()
Same as get().
Definition buffer.hpp:63
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:59
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:161
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:201
Definition buffer.hpp:210
Sets some global macros and further includes string.hpp.
The namespace used by libfilezilla.
Definition apply.hpp:17