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>
29template <concepts::contiguous_container T = std::vector<u
int8_t>>
31 return T(s.cbegin(), s.cend());
34inline std::string
to_string(std::span<const uint8_t> bytes) {
35 return std::string(bytes.begin(), bytes.end());
47template <
typename RetT,
typename KeyT,
typename ReducerT>
48RetT
reduce(
const std::vector<KeyT>& keys, RetT acc, ReducerT reducer)
49 requires std::is_convertible_v<ReducerT, std::function<RetT(RetT,
const KeyT&)>>
51 for(
const KeyT& key : keys) {
52 acc = reducer(std::move(acc), key);
60template <
typename T,
typename OT>
62 for(
size_t i = 0; i != vec.size(); ++i) {
70template <
typename T,
typename Pred>
72 auto i = assoc.begin();
73 while(i != assoc.end()) {
87 BufferSlicer(std::span<const uint8_t> buffer) : m_remaining(buffer) {}
89 template <concepts::contiguous_container ContainerT>
90 auto copy(
const size_t count) {
91 const auto result =
take(count);
92 return ContainerT(result.begin(), result.end());
99 std::span<const uint8_t>
take(
const size_t count) {
101 auto result = m_remaining.first(count);
102 m_remaining = m_remaining.subspan(count);
106 template <
size_t count>
107 std::span<const uint8_t, count>
take() {
109 auto result = m_remaining.first<count>();
110 m_remaining = m_remaining.subspan(count);
114 template <concepts::contiguous_strong_type T>
122 const auto data =
take(sink.size());
123 std::copy(data.begin(), data.end(), sink.begin());
130 bool empty()
const {
return m_remaining.empty(); }
133 std::span<const uint8_t> m_remaining;
151 constexpr std::span<uint8_t>
next(
size_t bytes) {
154 auto result = m_buffer.first(bytes);
155 m_buffer = m_buffer.subspan(bytes);
159 template <
size_t bytes>
160 constexpr std::span<uint8_t, bytes>
next() {
163 auto result = m_buffer.first<bytes>();
164 m_buffer = m_buffer.subspan(bytes);
168 template <concepts::contiguous_strong_type StrongT>
178 constexpr void append(std::span<const uint8_t> buffer) {
179 auto sink =
next(buffer.size());
180 std::copy(buffer.begin(), buffer.end(), sink.begin());
183 constexpr void append(uint8_t b,
size_t repeat = 1) {
184 auto sink =
next(repeat);
185 std::fill(sink.begin(), sink.end(), b);
188 constexpr bool full()
const {
return m_buffer.empty(); }
193 std::span<uint8_t> m_buffer;
204template <ranges::spanable_range OutR, ranges::spanable_range... Rs>
213 [[maybe_unused]]
auto fill_fn = [&] {
216 const size_t total_size = (
ranges.size() + ... + 0);
217 result.reserve(total_size);
220 return [&result](
auto&& range) {
222 std::ranges::begin(range), std::ranges::end(range), std::back_inserter(
unwrap_strong_type(result)));
228 [[maybe_unused]]
constexpr size_t total_size = (
decltype(std::span{
ranges})::extent + ... + 0);
229 static_assert(result.size() == total_size,
"size of result buffer does not match the sum of input buffers");
232 const size_t total_size = (
ranges.size() + ... + 0);
234 "result buffer has static extent that does not match the sum of input buffers");
238 return [itr = std::ranges::begin(result)](
auto&& range)
mutable {
239 std::copy(std::ranges::begin(range), std::ranges::end(range), itr);
240 std::advance(itr, std::ranges::size(range));
246 (fill_fn(std::forward<Rs>(
ranges)), ...);
263template <
typename OutR = detail::AutoDetect, ranges::spanable_range... Rs>
265 requires(all_same_v<std::ranges::range_value_t<Rs>...>)
267 if constexpr(std::same_as<detail::AutoDetect, OutR>) {
269 static_assert(
sizeof...(Rs) > 0,
"Cannot auto-detect the output type if not a single input range is provided.");
270 using candidate_result_t = std::remove_cvref_t<std::tuple_element_t<0, std::tuple<Rs...>>>;
271 using result_range_value_t = std::remove_cvref_t<std::ranges::range_value_t<candidate_result_t>>;
276 constexpr size_t total_size = (
decltype(std::span{
ranges})::extent + ... + 0);
277 using out_array_t = std::array<result_range_value_t, total_size>;
284 "First input range has static extent, but a dynamically allocated output range is required. Please explicitly specify a dynamically allocatable output type.");
293template <
typename... Alts,
typename... Ts>
295 return (std::holds_alternative<Alts>(v) || ...);
298template <
typename GeneralVariantT,
typename SpecialT>
300 return std::is_constructible_v<GeneralVariantT, SpecialT>;
303template <
typename GeneralVariantT,
typename... SpecialTs>
305 return (std::is_constructible_v<GeneralVariantT, SpecialTs> && ...);
315template <
typename GeneralVariantT,
typename SpecialT>
317 requires(std::is_constructible_v<GeneralVariantT, std::decay_t<SpecialT>>)
319 return std::forward<SpecialT>(specific);
329template <
typename GeneralVariantT,
typename... SpecialTs>
330constexpr GeneralVariantT
generalize_to(std::variant<SpecialTs...> specific)
noexcept {
333 "Desired general type must be implicitly constructible by all types of the specialized std::variant<>");
334 return std::visit([](
auto s) -> GeneralVariantT {
return s; }, std::move(specific));
339template <
class... Ts>
341 using Ts::operator()...;
344template <
class... Ts>
354template <std::invocable FunT>
366 m_cleanup = std::move(other.m_cleanup);
373 if(m_cleanup.has_value()) {
384 std::optional<FunT> m_cleanup;
391T
assert_is_some(std::optional<T> v,
const char* expr,
const char* func,
const char* file,
int line) {
399#define BOTAN_ASSERT_IS_SOME(v) assert_is_some(v, #v, __func__, __FILE__, __LINE__)
414 requires std::is_enum_v<T>
416 return static_cast<std::underlying_type_t<T>
>(e);
421[[nodiscard]]
constexpr auto out_ptr(T& outptr)
noexcept {
424 constexpr ~out_ptr_t()
noexcept {
425 m_ptr.reset(m_rawptr);
429 constexpr out_ptr_t(T& outptr) noexcept : m_ptr(outptr), m_rawptr(
nullptr) {}
431 out_ptr_t(
const out_ptr_t&) =
delete;
432 out_ptr_t(out_ptr_t&&) =
delete;
433 out_ptr_t& operator=(
const out_ptr_t&) =
delete;
434 out_ptr_t& operator=(out_ptr_t&&) =
delete;
436 [[nodiscard]]
constexpr operator typename T::element_type **() &&
noexcept {
return &m_rawptr; }
440 typename T::element_type* m_rawptr;
443 return out_ptr_t{outptr};
447 requires std::is_default_constructible_v<T>
448[[nodiscard]]
constexpr auto out_opt(std::optional<T>& outopt)
noexcept {
451 constexpr ~out_opt_t()
noexcept { m_opt = m_raw; }
453 constexpr out_opt_t(std::optional<T>& outopt) noexcept : m_opt(outopt) {}
455 out_opt_t(
const out_opt_t&) =
delete;
456 out_opt_t(out_opt_t&&) =
delete;
457 out_opt_t& operator=(
const out_opt_t&) =
delete;
458 out_opt_t& operator=(out_opt_t&&) =
delete;
460 [[nodiscard]]
constexpr operator T*() &&
noexcept {
return &m_raw; }
463 std::optional<T>& m_opt;
467 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 & operator=(const scoped_cleanup &)=delete
scoped_cleanup(FunT cleanup)
scoped_cleanup & operator=(scoped_cleanup &&other)
scoped_cleanup(scoped_cleanup &&other)
constexpr OutR concatenate(Rs &&... ranges)
constexpr GeneralVariantT generalize_to(SpecialT &&specific) noexcept
Converts a given variant into another variant-ish whose type states are a super set of the given vari...
void map_remove_if(Pred pred, T &assoc)
T to_byte_vector(std::string_view s)
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
std::string to_string(ErrorType type)
Convert an ErrorType to string.
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... >