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  buffer() noexcept = default;
30 
32  explicit buffer(size_t capacity);
33 
34  buffer(buffer const& buf);
35  buffer(buffer && buf) noexcept;
36 
37  ~buffer() { delete[] data_; }
38 
39  buffer& operator=(buffer const& buf);
40  buffer& operator=(buffer && buf) noexcept;
41 
43  unsigned char const* get() const { return pos_; }
44  unsigned char* get() { return pos_; }
45 
64  unsigned char* get(size_t write_size);
65 
67  void add(size_t added);
68 
75  template<typename T, std::enable_if_t<std::is_signed_v<T>, int> = 0>
76  void add(T added) {
77  if (added > 0) {
78  add(static_cast<size_t>(added));
79  }
80  }
81 
86  void consume(size_t consumed);
87 
88  size_t size() const { return size_; }
89 
93  void clear();
94 
99  void append(unsigned char const* data, size_t len);
100  void append(std::string_view const& str);
101  void append(std::vector<uint8_t> const& data);
102  void append(fz::buffer const& b);
103  void append(unsigned char v);
104  void append(size_t len, unsigned char c);
105 
106  buffer& operator+=(unsigned char v) {
107  append(v);
108  return *this;
109  }
110  buffer& operator+=(std::string_view const& str) {
111  append(str);
112  return *this;
113  }
114  buffer& operator+=(std::vector<uint8_t> const& data) {
115  append(data);
116  return *this;
117  }
118  buffer& operator+=(fz::buffer const& b) {
119  append(b);
120  return *this;
121  }
122 
123  bool empty() const { return size_ == 0; }
124  explicit operator bool() const {
125  return size_ != 0;
126  }
127 
128  size_t capacity() const { return capacity_; }
129  void reserve(size_t capacity);
130 
131  void resize(size_t size);
132 
134  unsigned char operator[](size_t i) const { return pos_[i]; }
135  unsigned char & operator[](size_t i) { return pos_[i]; }
136 
137  bool operator==(buffer const& rhs) const;
138 
139  bool operator!=(buffer const& rhs) const {
140  return !(*this == rhs);
141  }
142 
143  std::string_view to_view() const;
144 private:
145 
146  // Invariants:
147  // size_ <= capacity_
148  // data_ <= pos_
149  // pos_ <= data_ + capacity_
150  // pos_ + size_ <= data_ + capacity_
151  unsigned char* data_{};
152  unsigned char* pos_{};
153  size_t size_{};
154  size_t capacity_{};
155 };
156 
157 }
158 
159 #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:76
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 operator[](size_t i) const
Gets element at offset i. Does not do bounds checking.
Definition: buffer.hpp:134