libfilezilla
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 
13 namespace fz {
14 
26 class FZ_PUBLIC_SYMBOL buffer final
27 {
28 public:
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 
101  void clear_and_free();
102 
107  void append(unsigned char const* data, size_t len);
108  void append(std::string_view const& str);
109  void append(std::basic_string_view<uint8_t> const& data);
110  void append(std::vector<uint8_t> const& data);
111  void append(fz::buffer const& b);
112  void append(unsigned char v);
113  void append(size_t len, unsigned char c);
114 
115  buffer& operator+=(unsigned char v) {
116  append(v);
117  return *this;
118  }
119  buffer& operator+=(std::string_view const& str) {
120  append(str);
121  return *this;
122  }
123  buffer& operator+=(std::vector<uint8_t> const& data) {
124  append(data);
125  return *this;
126  }
127  buffer& operator+=(fz::buffer const& b) {
128  append(b);
129  return *this;
130  }
131 
132  bool empty() const { return size_ == 0; }
133  explicit operator bool() const {
134  return size_ != 0;
135  }
136 
137  size_t capacity() const { return capacity_; }
138  void reserve(size_t capacity);
139 
140  void resize(size_t size);
141 
143  unsigned char operator[](size_t i) const { return pos_[i]; }
144  unsigned char & operator[](size_t i) { return pos_[i]; }
145 
146  bool operator==(buffer const& rhs) const;
147 
148  bool operator!=(buffer const& rhs) const {
149  return !(*this == rhs);
150  }
151 
152  std::string_view to_view() const;
153 
154  void wipe();
155  void wipe_unused();
156 
157 private:
158 
159  // Invariants:
160  // size_ <= capacity_
161  // data_ <= pos_
162  // pos_ <= data_ + capacity_
163  // pos_ + size_ <= data_ + capacity_
164  unsigned char* data_{};
165  unsigned char* pos_{};
166  size_t size_{};
167  size_t capacity_{};
168 };
169 
170 inline void wipe(buffer & b) {
171  b.wipe();
172 }
173 
174 inline void wipe_unused(buffer & b) {
175  b.wipe_unused();
176 }
177 
178 }
179 
180 #endif
bool operator==(symmetric_key const &lhs, symmetric_key const &rhs)
Side-channel safe comparison.
void add(T added)
Overload of add for signed types, only adds if value is positive.
Definition: buffer.hpp:82
The namespace used by libfilezilla.
Definition: apply.hpp:17
Sets some global macros and further includes string.hpp.
The buffer class is a simple buffer where data can be appended at the end and consumed at the front...
Definition: buffer.hpp:26
unsigned char * data()
Same as get()
Definition: buffer.hpp:49
unsigned char operator[](size_t i) const
Gets element at offset i. Does not do bounds checking.
Definition: buffer.hpp:143