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>
37template <
typename RetT,
typename KeyT,
typename ReducerT>
38RetT
reduce(
const std::vector<KeyT>& keys, RetT acc, ReducerT reducer)
39 requires std::is_convertible_v<ReducerT, std::function<RetT(RetT,
const KeyT&)>>
41 for(
const KeyT& key : keys) {
42 acc = reducer(std::move(acc), key);
50template <
typename T,
typename V>
52 for(
size_t i = 0; i != vec.size(); ++i) {
60template <
typename T,
typename Pred>
62 auto i = assoc.begin();
63 while(i != assoc.end()) {
77 explicit BufferSlicer(std::span<const uint8_t> buffer) : m_remaining(buffer) {}
79 template <concepts::contiguous_container ContainerT>
80 auto copy(
const size_t count) {
81 const auto result =
take(count);
82 return ContainerT(result.begin(), result.end());
89 std::span<const uint8_t>
take(
const size_t count) {
91 auto result = m_remaining.first(count);
92 m_remaining = m_remaining.subspan(count);
96 template <
size_t count>
97 std::span<const uint8_t, count>
take() {
99 auto result = m_remaining.first<count>();
100 m_remaining = m_remaining.subspan(count);
104 template <concepts::contiguous_strong_type T>
112 const auto data =
take(sink.size());
113 std::copy(data.begin(), data.end(), sink.begin());
120 bool empty()
const {
return m_remaining.empty(); }
123 std::span<const uint8_t> m_remaining;
135 constexpr explicit BufferStuffer(std::span<uint8_t> buffer) : m_buffer(buffer) {}
141 constexpr std::span<uint8_t>
next(
size_t bytes) {
144 auto result = m_buffer.first(bytes);
145 m_buffer = m_buffer.subspan(bytes);
149 template <
size_t bytes>
150 constexpr std::span<uint8_t, bytes>
next() {
153 auto result = m_buffer.first<bytes>();
154 m_buffer = m_buffer.subspan(bytes);
158 template <concepts::contiguous_strong_type StrongT>
168 constexpr void append(std::span<const uint8_t> buffer) {
169 auto sink =
next(buffer.size());
170 std::copy(buffer.begin(), buffer.end(), sink.begin());
173 constexpr void append(uint8_t b,
size_t repeat = 1) {
174 auto sink =
next(repeat);
175 std::fill(sink.begin(), sink.end(), b);
178 constexpr bool full()
const {
return m_buffer.empty(); }
183 std::span<uint8_t> m_buffer;
194template <ranges::spanable_range OutR, ranges::spanable_range... Rs>
203 [[maybe_unused]]
auto fill_fn = [&] {
206 const size_t total_size = (
ranges.size() + ... + 0);
207 result.reserve(total_size);
210 return [&result](
auto&& range) {
212 std::ranges::begin(range), std::ranges::end(range), std::back_inserter(
unwrap_strong_type(result)));
218 [[maybe_unused]]
constexpr size_t total_size = (
decltype(std::span{
ranges})::extent + ... + 0);
219 static_assert(result.size() == total_size,
"size of result buffer does not match the sum of input buffers");
222 const size_t total_size = (
ranges.size() + ... + 0);
224 "result buffer has static extent that does not match the sum of input buffers");
228 return [itr = std::ranges::begin(result)](
auto&& range)
mutable {
229 std::copy(std::ranges::begin(range), std::ranges::end(range), itr);
230 std::advance(itr, std::ranges::size(range));
236 (fill_fn(std::forward<Rs>(
ranges)), ...);
253template <
typename OutR = detail::AutoDetect, ranges::spanable_range... Rs>
255 requires(all_same_v<std::ranges::range_value_t<Rs>...>)
257 if constexpr(std::same_as<detail::AutoDetect, OutR>) {
259 static_assert(
sizeof...(Rs) > 0,
"Cannot auto-detect the output type if not a single input range is provided.");
260 using candidate_result_t = std::remove_cvref_t<std::tuple_element_t<0, std::tuple<Rs...>>>;
261 using result_range_value_t = std::remove_cvref_t<std::ranges::range_value_t<candidate_result_t>>;
266 constexpr size_t total_size = (
decltype(std::span{
ranges})::extent + ... + 0);
267 using out_array_t = std::array<result_range_value_t, total_size>;
274 "First input range has static extent, but a dynamically allocated output range is required. Please explicitly specify a dynamically allocatable output type.");
283template <
typename... Alts,
typename... Ts>
285 return (std::holds_alternative<Alts>(v) || ...);
288template <
typename GeneralVariantT,
typename SpecialT>
290 return std::is_constructible_v<GeneralVariantT, SpecialT>;
293template <
typename GeneralVariantT,
typename... SpecialTs>
295 return (std::is_constructible_v<GeneralVariantT, SpecialTs> && ...);
305template <
typename GeneralVariantT,
typename SpecialT>
307 requires(std::is_constructible_v<GeneralVariantT, std::decay_t<SpecialT>>)
309 return std::forward<SpecialT>(specific);
319template <
typename GeneralVariantT,
typename... SpecialTs>
320constexpr GeneralVariantT
generalize_to(std::variant<SpecialTs...> specific) {
323 "Desired general type must be implicitly constructible by all types of the specialized std::variant<>");
324 return std::visit([](
auto s) -> GeneralVariantT {
return s; }, std::move(specific));
329template <
class... Ts>
331 using Ts::operator()...;
334template <
class... Ts>
344template <std::invocable FunT>
356 m_cleanup = std::move(other.m_cleanup);
363 if(m_cleanup.has_value()) {
374 std::optional<FunT> m_cleanup;
381T
assert_is_some(std::optional<T> v,
const char* expr,
const char* func,
const char* file,
int line) {
390#define BOTAN_ASSERT_IS_SOME(v) assert_is_some(v, #v, __func__, __FILE__, __LINE__)
407 requires std::is_enum_v<T>
409 return static_cast<std::underlying_type_t<T>
>(e);
414[[nodiscard]]
constexpr auto out_ptr(T& outptr)
noexcept {
417 constexpr ~out_ptr_t()
noexcept {
418 m_ptr.reset(m_rawptr);
423 constexpr out_ptr_t(T& outptr) noexcept : m_ptr(outptr), m_rawptr(
nullptr) {}
425 out_ptr_t(
const out_ptr_t&) =
delete;
426 out_ptr_t(out_ptr_t&&) =
delete;
427 out_ptr_t& operator=(
const out_ptr_t&) =
delete;
428 out_ptr_t& operator=(out_ptr_t&&) =
delete;
431 [[nodiscard]]
constexpr operator typename T::element_type **() &&
noexcept {
return &m_rawptr; }
435 typename T::element_type* m_rawptr;
438 return out_ptr_t{outptr};
442 requires std::is_default_constructible_v<T>
443[[nodiscard]]
constexpr auto out_opt(std::optional<T>& outopt)
noexcept {
446 constexpr ~out_opt_t()
noexcept { m_opt = m_raw; }
449 constexpr out_opt_t(std::optional<T>& outopt) noexcept : m_opt(outopt) {}
451 out_opt_t(
const out_opt_t&) =
delete;
452 out_opt_t(out_opt_t&&) =
delete;
453 out_opt_t& operator=(
const out_opt_t&) =
delete;
454 out_opt_t& operator=(out_opt_t&&) =
delete;
457 [[nodiscard]]
constexpr operator T*() &&
noexcept {
return &m_raw; }
460 std::optional<T>& m_opt;
464 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 V &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...