libfilezilla
util.hpp
Go to the documentation of this file.
1 #ifndef LIBFILEZILLA_UTIL_HEADER
2 #define LIBFILEZILLA_UTIL_HEADER
3 
4 #include "libfilezilla.hpp"
5 #include "time.hpp"
6 
7 #include <cstdint>
8 
13 namespace fz {
14 
23 void FZ_PUBLIC_SYMBOL sleep(duration const& d);
24 
29 void FZ_PUBLIC_SYMBOL yield();
30 
35 int64_t FZ_PUBLIC_SYMBOL random_number(int64_t min, int64_t max);
36 
41 std::vector<uint8_t> FZ_PUBLIC_SYMBOL random_bytes(size_t size);
42 
43 void FZ_PUBLIC_SYMBOL random_bytes(size_t size, uint8_t* destination);
44 
45 class buffer;
46 void FZ_PUBLIC_SYMBOL random_bytes(size_t size, buffer& destination);
47 
54 uint64_t FZ_PUBLIC_SYMBOL bitscan(uint64_t v);
55 
62 uint64_t FZ_PUBLIC_SYMBOL bitscan_reverse(uint64_t v);
63 
69 bool FZ_PUBLIC_SYMBOL equal_consttime(std::basic_string_view<uint8_t> const& lhs, std::basic_string_view<uint8_t> const& rhs);
70 
71 template <typename First, typename Second,
72  std::enable_if_t<sizeof(typename First::value_type) == sizeof(uint8_t) &&
73  sizeof(typename Second::value_type) == sizeof(uint8_t)>* = nullptr>
74 inline bool equal_consttime(First const& lhs, Second const& rhs)
75 {
76  return equal_consttime(std::basic_string_view<uint8_t>(reinterpret_cast<uint8_t const*>(lhs.data()), lhs.size()),
77  std::basic_string_view<uint8_t>(reinterpret_cast<uint8_t const*>(rhs.data()), rhs.size()));
78 }
79 
94 template<typename T, typename std::enable_if_t<std::is_final_v<T>>* = nullptr>
95 T& move_assign_through_move_constructor(T* p, T&& op) noexcept
96 {
97  p->~T();
98  new (p)T(std::move(op));
99  return *p;
100 }
101 
107 void FZ_PUBLIC_SYMBOL wipe(void* p, size_t n);
108 
113 void FZ_PUBLIC_SYMBOL wipe(std::string & s);
114 void FZ_PUBLIC_SYMBOL wipe(std::wstring & s);
115 void FZ_PUBLIC_SYMBOL wipe(std::vector<uint8_t> & v);
116 
117 template<typename T>
118 struct scoped_wiper final
119 {
120  [[nodiscard]] scoped_wiper(T& v)
121  : v_(v)
122  {}
123 
124  ~scoped_wiper()
125  {
126  wipe(v_);
127  }
128 
129  T& v_;
130 };
131 
136 void FZ_PUBLIC_SYMBOL wipe_unused(std::string & s);
137 void FZ_PUBLIC_SYMBOL wipe_unused(std::wstring & s);
138 void FZ_PUBLIC_SYMBOL wipe_unused(std::vector<uint8_t> & v);
139 
141 template<class A, class B>
142 constexpr bool cmp_less(A a, B b) noexcept
143 {
144  static_assert(std::is_integral_v<A>);
145  static_assert(std::is_integral_v<B>);
146  if constexpr (std::is_signed_v<A> == std::is_signed_v<B>) {
147  return a < b;
148  }
149  else if constexpr (std::is_signed_v<A>) {
150  if (a < 0) {
151  return true;
152  }
153  else {
154  return std::make_unsigned_t<A>(a) < b;
155  }
156  }
157  else {
158  if (b < 0) {
159  return false;
160  }
161  else {
162  return a < std::make_unsigned_t<B>(b);
163  }
164  }
165 }
166 
168 template<typename Out, typename In>
169 constexpr Out clamped_cast(In in) noexcept
170 {
171  if (cmp_less(in, std::numeric_limits<Out>::min())) {
172  return std::numeric_limits<Out>::min();
173  }
174  if (cmp_less(std::numeric_limits<Out>::max(), in)) {
175  return std::numeric_limits<Out>::max();
176  }
177  return static_cast<Out>(in);
178 }
179 
180 }
181 
182 #endif
T & move_assign_through_move_constructor(T *p, T &&op) noexcept
Helper to move-assign guaranteeing same member destruction order as the destructor.
Definition: util.hpp:95
void sleep(duration const &d)
Sleep current thread for the specified duration.
Definition: util.hpp:118
uint64_t bitscan_reverse(uint64_t v)
Returns index of the most-significant set bit.
std::vector< uint8_t > random_bytes(size_t size)
Get random uniformly distributed bytes.
Assorted classes dealing with time.
constexpr Out clamped_cast(In in) noexcept
Casts to a different integer type, clamping the new value to the min/max of the new type if the origi...
Definition: util.hpp:169
constexpr bool cmp_less(A a, B b) noexcept
Compares two integers which can be of different sizes and signeness.
Definition: util.hpp:142
int64_t random_number(int64_t min, int64_t max)
Get a secure random integer uniformly distributed in the closed interval [min, max].
The namespace used by libfilezilla.
Definition: apply.hpp:17
uint64_t bitscan(uint64_t v)
Returns index of the least-significant set bit.
Sets some global macros and further includes string.hpp.
void yield()
Relinquish control for a brief amount of time.
bool equal_consttime(std::basic_string_view< uint8_t > const &lhs, std::basic_string_view< uint8_t > const &rhs)
Secure equality test in constant time.