1#ifndef LIBFILEZILLA_BUFFER_HEADER
2#define LIBFILEZILLA_BUFFER_HEADER
16enum class buffer_wipe_policy {
32template<buffer_wipe_policy Policy>
36 using value_type =
unsigned char;
38 basic_buffer()
noexcept =
default;
43 explicit basic_buffer(std::string_view
const&
data);
45 basic_buffer(basic_buffer
const& buf);
46 basic_buffer(basic_buffer && buf)
noexcept;
48 basic_buffer& operator=(basic_buffer
const& buf);
49 basic_buffer& operator=(basic_buffer && buf)
noexcept;
51 basic_buffer& operator=(std::string_view
const& v);
58 unsigned char const*
get()
const {
return pos_; }
59 unsigned char*
get() {
return pos_; }
62 unsigned char*
data() {
return pos_; }
63 unsigned char const*
data()
const {
return pos_; }
83 unsigned char*
get(
size_t write_size);
86 void add(
size_t added);
94 template<
typename T, std::enable_if_t<std::is_
signed_v<T>,
int> = 0>
97 add(
static_cast<size_t>(added));
107 size_t size()
const {
return size_; }
114 void clear_and_free();
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);
128 void push_back(
unsigned char v) {
136 basic_buffer& operator+=(std::string_view
const& str) {
140 basic_buffer& operator+=(std::vector<uint8_t>
const&
data) {
144 basic_buffer& operator+=(basic_buffer
const& b) {
149 bool empty()
const {
return size_ == 0; }
150 explicit operator bool()
const {
154 size_t capacity()
const {
return capacity_; }
155 void reserve(
size_t capacity);
157 void resize(
size_t size);
161 unsigned char &
operator[](
size_t i) {
return pos_[i]; }
163 bool operator==(basic_buffer
const& rhs)
const {
164 return *
this == rhs.to_view();
167 bool operator==(std::string_view
const& rhs)
const;
169 bool operator!=(basic_buffer
const& rhs)
const {
170 return !(*
this == rhs);
172 bool operator!=(std::string_view
const& rhs)
const {
173 return !(*
this == rhs);
176 std::string_view to_view()
const;
188 unsigned char* data_{};
189 unsigned char* pos_{};
194#if BUILDING_LIBFILEZILLA
199class FZ_PUBLIC_SYMBOL
buffer final :
public basic_buffer<buffer_wipe_policy::normal>
202 using basic_buffer::basic_buffer;
203 using basic_buffer::operator=;
208class FZ_PUBLIC_SYMBOL
secure_buffer final :
public basic_buffer<buffer_wipe_policy::wipe_on_release>
211 using basic_buffer::basic_buffer;
212 using basic_buffer::operator=;
215inline void wipe(
buffer & b) {
219inline void wipe_unused(buffer & b) {
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