1#ifndef LIBFILEZILLA_UTIL_HEADER
2#define LIBFILEZILLA_UTIL_HEADER
43void FZ_PUBLIC_SYMBOL
random_bytes(
size_t size, uint8_t* destination);
54uint64_t FZ_PUBLIC_SYMBOL
bitscan(uint64_t v);
69bool FZ_PUBLIC_SYMBOL
equal_consttime(std::basic_string_view<uint8_t>
const& lhs, std::basic_string_view<uint8_t>
const& rhs);
71template <
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>
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()));
94template<
typename T,
typename std::enable_if_t<std::is_final_v<T>>* =
nullptr>
98 new (p)T(std::move(op));
107void FZ_PUBLIC_SYMBOL wipe(
void* p,
size_t n);
113void FZ_PUBLIC_SYMBOL wipe(std::string & s);
114void FZ_PUBLIC_SYMBOL wipe(std::vector<uint8_t> & v);
120void FZ_PUBLIC_SYMBOL wipe_unused(std::string & s);
121void FZ_PUBLIC_SYMBOL wipe_unused(std::vector<uint8_t> & v);
124template<
class A,
class B>
127 static_assert(std::is_integral_v<A>);
128 static_assert(std::is_integral_v<B>);
129 if constexpr (std::is_signed_v<A> == std::is_signed_v<B>) {
132 else if constexpr (std::is_signed_v<A>) {
137 return std::make_unsigned_t<A>(a) < b;
145 return a < std::make_unsigned_t<B>(b);
151template<
typename Out,
typename In>
154 if (
cmp_less(in, std::numeric_limits<Out>::min())) {
155 return std::numeric_limits<Out>::min();
157 if (
cmp_less(std::numeric_limits<Out>::max(), in)) {
158 return std::numeric_limits<Out>::max();
160 return static_cast<Out
>(in);
The buffer class is a simple buffer where data can be appended at the end and consumed at the front....
Definition buffer.hpp:27
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
std::vector< uint8_t > random_bytes(size_t size)
Get random uniformly distributed bytes.
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 yield()
Relinquish control for a brief amount of time.
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].
constexpr bool cmp_less(A a, B b) noexcept
Compares two integers which can be of different sizes and signeness.
Definition util.hpp:125
void sleep(duration const &d)
Sleep current thread for the specified duration.
uint64_t bitscan_reverse(uint64_t v)
Returns index of the most-significant set bit.
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:152
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.