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 
51 uint64_t FZ_PUBLIC_SYMBOL bitscan(uint64_t v);
52 
59 uint64_t FZ_PUBLIC_SYMBOL bitscan_reverse(uint64_t v);
60 
66 bool FZ_PUBLIC_SYMBOL equal_consttime(std::basic_string_view<uint8_t> const& lhs, std::basic_string_view<uint8_t> const& rhs);
67 
68 template <typename First, typename Second,
69  std::enable_if_t<sizeof(typename First::value_type) == sizeof(uint8_t) &&
70  sizeof(typename Second::value_type) == sizeof(uint8_t)>* = nullptr>
71 inline bool equal_consttime(First const& lhs, Second const& rhs)
72 {
73  return equal_consttime(std::basic_string_view<uint8_t>(reinterpret_cast<uint8_t const*>(lhs.data()), lhs.size()),
74  std::basic_string_view<uint8_t>(reinterpret_cast<uint8_t const*>(rhs.data()), rhs.size()));
75 }
76 
91 template<typename T, typename std::enable_if_t<std::is_final_v<T>>* = nullptr>
92 T& move_assign_through_move_constructor(T* p, T&& op) noexcept
93 {
94  p->~T();
95  new (p)T(std::move(op));
96  return *p;
97 }
98 
99 }
100 
101 #endif
The duration class represents a time interval in milliseconds.
Definition: time.hpp:291
Sets some global macros and further includes string.hpp.
The namespace used by libfilezilla.
Definition: apply.hpp:17
void sleep(duration const &d)
Sleep current thread for the specified duration.
uint64_t bitscan(uint64_t v)
Returns index of the least-significant set bit.
int64_t random_number(int64_t min, int64_t max)
Get a secure random integer uniformly distributed in the closed interval [min, max].
T & move_assign_through_move_constructor(T *p, T &&op) noexcept
Definition: util.hpp:92
std::vector< uint8_t > random_bytes(size_t size)
Get random uniformly distributed bytes.
uint64_t bitscan_reverse(uint64_t v)
Returns index of the most-significant set bit.
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.
Assorted classes dealing with time.