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
13namespace fz {
14
27{
28public:
29 typedef unsigned char value_type;
30
31 buffer() noexcept = default;
32
34 explicit buffer(size_t capacity);
35
36 buffer(buffer const& buf);
37 buffer(buffer && buf) noexcept;
38
39 ~buffer() { delete[] data_; }
40
41 buffer& operator=(buffer const& buf);
42 buffer& operator=(buffer && buf) noexcept;
43
45 unsigned char const* get() const { return pos_; }
46 unsigned char* get() { return pos_; }
47
49 unsigned char* data() { return pos_; }
50 unsigned char const* data() const { return pos_; }
51
70 unsigned char* get(size_t write_size);
71
73 void add(size_t added);
74
81 template<typename T, std::enable_if_t<std::is_signed_v<T>, int> = 0>
82 void add(T added) {
83 if (added > 0) {
84 add(static_cast<size_t>(added));
85 }
86 }
87
92 void consume(size_t consumed);
93
94 size_t size() const { return size_; }
95
99 void clear();
100
105 void append(unsigned char const* data, size_t len);
106 void append(std::string_view const& str);
107 void append(std::vector<uint8_t> const& data);
108 void append(fz::buffer const& b);
109 void append(unsigned char v);
110 void append(size_t len, unsigned char c);
111
112 buffer& operator+=(unsigned char v) {
113 append(v);
114 return *this;
115 }
116 buffer& operator+=(std::string_view const& str) {
117 append(str);
118 return *this;
119 }
120 buffer& operator+=(std::vector<uint8_t> const& data) {
121 append(data);
122 return *this;
123 }
124 buffer& operator+=(fz::buffer const& b) {
125 append(b);
126 return *this;
127 }
128
129 bool empty() const { return size_ == 0; }
130 explicit operator bool() const {
131 return size_ != 0;
132 }
133
134 size_t capacity() const { return capacity_; }
135 void reserve(size_t capacity);
136
137 void resize(size_t size);
138
140 unsigned char operator[](size_t i) const { return pos_[i]; }
141 unsigned char & operator[](size_t i) { return pos_[i]; }
142
143 bool operator==(buffer const& rhs) const;
144
145 bool operator!=(buffer const& rhs) const {
146 return !(*this == rhs);
147 }
148
149 std::string_view to_view() const;
150
151 void wipe();
152 void wipe_unused();
153
154private:
155
156 // Invariants:
157 // size_ <= capacity_
158 // data_ <= pos_
159 // pos_ <= data_ + capacity_
160 // pos_ + size_ <= data_ + capacity_
161 unsigned char* data_{};
162 unsigned char* pos_{};
163 size_t size_{};
164 size_t capacity_{};
165};
166
167inline void FZ_PUBLIC_SYMBOL wipe(buffer & b) {
168 b.wipe();
169}
170
171inline void FZ_PUBLIC_SYMBOL wipe_unused(buffer & b) {
172 b.wipe_unused();
173}
174
175}
176
177#endif
The buffer class is a simple buffer where data can be appended at the end and consumed at the front....
Definition buffer.hpp:27
void add(T added)
Overload of add for signed types, only adds if value is positive.
Definition buffer.hpp:82
buffer(size_t capacity)
Initially reserves the passed capacity.
unsigned char * data()
Same as get()
Definition buffer.hpp:49
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.
unsigned char const * get() const
Undefined if buffer is empty.
Definition buffer.hpp:45
void clear()
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:140
void add(size_t added)
Increase size by the passed amount. Call this after having obtained a writable buffer with get(size_t...
Sets some global macros and further includes string.hpp.
The namespace used by libfilezilla.
Definition apply.hpp:17
bool dispatch(event_base const &ev, F &&f)
Dispatch for simple_event<> based events to simple functors.
Definition event_handler.hpp:199
bool operator==(symmetric_key const &lhs, symmetric_key const &rhs)
Side-channel safe comparison.