10#ifndef BOTAN_STL_UTIL_H_
11#define BOTAN_STL_UTIL_H_
13#include <botan/assert.h>
14#include <botan/concepts.h>
15#include <botan/secmem.h>
16#include <botan/strong_type.h>
38template <
typename RetT,
typename KeyT,
typename ReducerT>
39RetT
reduce(
const std::vector<KeyT>& keys, RetT acc, ReducerT reducer)
40 requires std::is_convertible_v<ReducerT, std::function<RetT(RetT,
const KeyT&)>>
42 for(
const KeyT& key : keys) {
43 acc = reducer(std::move(acc), key);
51template <
typename T,
typename OT>
53 for(
size_t i = 0; i != vec.size(); ++i) {
61template <
typename T,
typename Pred>
63 auto i = assoc.begin();
64 while(i != assoc.end()) {
78 explicit BufferSlicer(std::span<const uint8_t> buffer) : m_remaining(buffer) {}
80 template <concepts::contiguous_container ContainerT>
81 auto copy(
const size_t count) {
82 const auto result =
take(count);
83 return ContainerT(result.begin(), result.end());
90 std::span<const uint8_t>
take(
const size_t count) {
92 auto result = m_remaining.first(count);
93 m_remaining = m_remaining.subspan(count);
97 template <
size_t count>
98 std::span<const uint8_t, count>
take() {
100 auto result = m_remaining.first<count>();
101 m_remaining = m_remaining.subspan(count);
105 template <concepts::contiguous_strong_type T>
113 const auto data =
take(sink.size());
114 std::copy(data.begin(), data.end(), sink.begin());
121 bool empty()
const {
return m_remaining.empty(); }
124 std::span<const uint8_t> m_remaining;
136 constexpr explicit BufferStuffer(std::span<uint8_t> buffer) : m_buffer(buffer) {}
142 constexpr std::span<uint8_t>
next(
size_t bytes) {
145 auto result = m_buffer.first(bytes);
146 m_buffer = m_buffer.subspan(bytes);
150 template <
size_t bytes>
151 constexpr std::span<uint8_t, bytes>
next() {
154 auto result = m_buffer.first<bytes>();
155 m_buffer = m_buffer.subspan(bytes);
159 template <concepts::contiguous_strong_type StrongT>
169 constexpr void append(std::span<const uint8_t> buffer) {
170 auto sink =
next(buffer.size());
171 std::copy(buffer.begin(), buffer.end(), sink.begin());
174 constexpr void append(uint8_t b,
size_t repeat = 1) {
175 auto sink =
next(repeat);
176 std::fill(sink.begin(), sink.end(), b);
179 constexpr bool full()
const {
return m_buffer.empty(); }
184 std::span<uint8_t> m_buffer;
195template <ranges::spanable_range OutR, ranges::spanable_range... Rs>
204 [[maybe_unused]]
auto fill_fn = [&] {
207 const size_t total_size = (
ranges.size() + ... + 0);
208 result.reserve(total_size);
211 return [&result](
auto&& range) {
213 std::ranges::begin(range), std::ranges::end(range), std::back_inserter(
unwrap_strong_type(result)));
219 [[maybe_unused]]
constexpr size_t total_size = (
decltype(std::span{
ranges})::extent + ... + 0);
220 static_assert(result.size() == total_size,
"size of result buffer does not match the sum of input buffers");
223 const size_t total_size = (
ranges.size() + ... + 0);
225 "result buffer has static extent that does not match the sum of input buffers");
229 return [itr = std::ranges::begin(result)](
auto&& range)
mutable {
230 std::copy(std::ranges::begin(range), std::ranges::end(range), itr);
231 std::advance(itr, std::ranges::size(range));
237 (fill_fn(std::forward<Rs>(
ranges)), ...);
254template <
typename OutR = detail::AutoDetect, ranges::spanable_range... Rs>
256 requires(all_same_v<std::ranges::range_value_t<Rs>...>)
258 if constexpr(std::same_as<detail::AutoDetect, OutR>) {
260 static_assert(
sizeof...(Rs) > 0,
"Cannot auto-detect the output type if not a single input range is provided.");
261 using candidate_result_t = std::remove_cvref_t<std::tuple_element_t<0, std::tuple<Rs...>>>;
262 using result_range_value_t = std::remove_cvref_t<std::ranges::range_value_t<candidate_result_t>>;
267 constexpr size_t total_size = (
decltype(std::span{
ranges})::extent + ... + 0);
268 using out_array_t = std::array<result_range_value_t, total_size>;
275 "First input range has static extent, but a dynamically allocated output range is required. Please explicitly specify a dynamically allocatable output type.");
284template <
typename... Alts,
typename... Ts>
286 return (std::holds_alternative<Alts>(v) || ...);
289template <
typename GeneralVariantT,
typename SpecialT>
291 return std::is_constructible_v<GeneralVariantT, SpecialT>;
294template <
typename GeneralVariantT,
typename... SpecialTs>
296 return (std::is_constructible_v<GeneralVariantT, SpecialTs> && ...);
306template <
typename GeneralVariantT,
typename SpecialT>
308 requires(std::is_constructible_v<GeneralVariantT, std::decay_t<SpecialT>>)
310 return std::forward<SpecialT>(specific);
320template <
typename GeneralVariantT,
typename... SpecialTs>
321constexpr GeneralVariantT
generalize_to(std::variant<SpecialTs...> specific) {
324 "Desired general type must be implicitly constructible by all types of the specialized std::variant<>");
325 return std::visit([](
auto s) -> GeneralVariantT {
return s; }, std::move(specific));
330template <
class... Ts>
332 using Ts::operator()...;
335template <
class... Ts>
345template <std::invocable FunT>
357 m_cleanup = std::move(other.m_cleanup);
364 if(m_cleanup.has_value()) {
375 std::optional<FunT> m_cleanup;
382T
assert_is_some(std::optional<T> v,
const char* expr,
const char* func,
const char* file,
int line) {
391#define BOTAN_ASSERT_IS_SOME(v) assert_is_some(v, #v, __func__, __FILE__, __LINE__)
408 requires std::is_enum_v<T>
410 return static_cast<std::underlying_type_t<T>
>(e);
415[[nodiscard]]
constexpr auto out_ptr(T& outptr)
noexcept {
418 constexpr ~out_ptr_t()
noexcept {
419 m_ptr.reset(m_rawptr);
424 constexpr out_ptr_t(T& outptr) noexcept : m_ptr(outptr), m_rawptr(
nullptr) {}
426 out_ptr_t(
const out_ptr_t&) =
delete;
427 out_ptr_t(out_ptr_t&&) =
delete;
428 out_ptr_t& operator=(
const out_ptr_t&) =
delete;
429 out_ptr_t& operator=(out_ptr_t&&) =
delete;
432 [[nodiscard]]
constexpr operator typename T::element_type **() &&
noexcept {
return &m_rawptr; }
436 typename T::element_type* m_rawptr;
439 return out_ptr_t{outptr};
443 requires std::is_default_constructible_v<T>
444[[nodiscard]]
constexpr auto out_opt(std::optional<T>& outopt)
noexcept {
447 constexpr ~out_opt_t()
noexcept { m_opt = m_raw; }
450 constexpr out_opt_t(std::optional<T>& outopt) noexcept : m_opt(outopt) {}
452 out_opt_t(
const out_opt_t&) =
delete;
453 out_opt_t(out_opt_t&&) =
delete;
454 out_opt_t& operator=(
const out_opt_t&) =
delete;
455 out_opt_t& operator=(out_opt_t&&) =
delete;
458 [[nodiscard]]
constexpr operator T*() &&
noexcept {
return &m_raw; }
461 std::optional<T>& m_opt;
465 return out_opt_t{outopt};
#define BOTAN_STATE_CHECK(expr)
#define BOTAN_ARG_CHECK(expr, msg)
void skip(const size_t count)
auto copy_as_secure_vector(const size_t count)
void copy_into(std::span< uint8_t > sink)
BufferSlicer(std::span< const uint8_t > buffer)
std::span< const uint8_t, count > take()
auto copy(const size_t count)
std::span< const uint8_t > take(const size_t count)
StrongSpan< const T > take(const size_t count)
auto copy_as_vector(const size_t count)
constexpr void append(std::span< const uint8_t > buffer)
constexpr void append(uint8_t b, size_t repeat=1)
constexpr size_t remaining_capacity() const
StrongSpan< StrongT > next(size_t bytes)
constexpr std::span< uint8_t > next(size_t bytes)
constexpr std::span< uint8_t, bytes > next()
constexpr uint8_t & next_byte()
constexpr BufferStuffer(std::span< uint8_t > buffer)
constexpr bool full() const
constexpr StringLiteral(const char(&str)[N])
scoped_cleanup(const scoped_cleanup &)=delete
scoped_cleanup(scoped_cleanup &&other) noexcept
scoped_cleanup & operator=(const scoped_cleanup &)=delete
scoped_cleanup(FunT cleanup)
void disengage() noexcept
scoped_cleanup & operator=(scoped_cleanup &&other) noexcept
constexpr OutR concatenate(Rs &&... ranges)
void map_remove_if(Pred pred, T &assoc)
constexpr auto out_ptr(T &outptr) noexcept
constexpr bool holds_any_of(const std::variant< Ts... > &v) noexcept
RetT reduce(const std::vector< KeyT > &keys, RetT acc, ReducerT reducer)
constexpr decltype(auto) unwrap_strong_type(T &&t)
Generically unwraps a strong type to its underlying type.
constexpr auto out_opt(std::optional< T > &outopt) noexcept
T assert_is_some(std::optional< T > v, const char *expr, const char *func, const char *file, int line)
constexpr auto concat(Rs &&... ranges)
bool value_exists(const std::vector< T > &vec, const OT &val)
constexpr bool is_generalizable_to(const SpecialT &) noexcept
auto to_underlying(T e) noexcept
void assertion_failure(const char *expr_str, const char *assertion_made, const char *func, const char *file, int line)
overloaded(Ts...) -> overloaded< Ts... >
constexpr GeneralVariantT generalize_to(SpecialT &&specific)
Converts a given variant into another variant-ish whose type states are a super set of the given vari...