10#ifndef BOTAN_STL_UTIL_H_
11#define BOTAN_STL_UTIL_H_
22#include <botan/concepts.h>
23#include <botan/secmem.h>
24#include <botan/strong_type.h>
28template <concepts::contiguous_container T = std::vector<u
int8_t>>
30 return T(s.cbegin(), s.cend());
33inline std::string
to_string(std::span<const uint8_t> bytes) {
34 return std::string(bytes.begin(), bytes.end());
46template <
typename RetT,
typename KeyT,
typename ReducerT>
47RetT
reduce(
const std::vector<KeyT>& keys, RetT acc, ReducerT reducer)
48 requires std::is_convertible_v<ReducerT, std::function<RetT(RetT,
const KeyT&)>>
50 for(
const KeyT& key : keys) {
51 acc = reducer(std::move(acc), key);
59template <
typename T,
typename OT>
61 for(
size_t i = 0; i != vec.size(); ++i) {
69template <
typename T,
typename Pred>
71 auto i = assoc.begin();
72 while(i != assoc.end()) {
86 BufferSlicer(std::span<const uint8_t> buffer) : m_remaining(buffer) {}
88 template <concepts::contiguous_container ContainerT>
89 auto copy(
const size_t count) {
90 const auto result =
take(count);
91 return ContainerT(result.begin(), result.end());
98 std::span<const uint8_t>
take(
const size_t count) {
100 auto result = m_remaining.first(count);
101 m_remaining = m_remaining.subspan(count);
105 template <
size_t count>
106 std::span<const uint8_t, count>
take() {
108 auto result = m_remaining.first<count>();
109 m_remaining = m_remaining.subspan(count);
113 template <concepts::contiguous_strong_type T>
121 const auto data =
take(sink.size());
122 std::copy(data.begin(), data.end(), sink.begin());
129 bool empty()
const {
return m_remaining.empty(); }
132 std::span<const uint8_t> m_remaining;
150 constexpr std::span<uint8_t>
next(
size_t bytes) {
153 auto result = m_buffer.first(bytes);
154 m_buffer = m_buffer.subspan(bytes);
158 template <
size_t bytes>
159 constexpr std::span<uint8_t, bytes>
next() {
162 auto result = m_buffer.first<bytes>();
163 m_buffer = m_buffer.subspan(bytes);
167 template <concepts::contiguous_strong_type StrongT>
177 constexpr void append(std::span<const uint8_t> buffer) {
178 auto sink =
next(buffer.size());
179 std::copy(buffer.begin(), buffer.end(), sink.begin());
182 constexpr void append(uint8_t b,
size_t repeat = 1) {
183 auto sink =
next(repeat);
184 std::fill(sink.begin(), sink.end(), b);
187 constexpr bool full()
const {
return m_buffer.empty(); }
192 std::span<uint8_t> m_buffer;
203template <ranges::spanable_range OutR, ranges::spanable_range... Rs>
212 [[maybe_unused]]
auto fill_fn = [&] {
215 const size_t total_size = (ranges.size() + ... + 0);
216 result.reserve(total_size);
219 return [&result](
auto&& range) {
220 std::copy(std::ranges::begin(range), std::ranges::end(range), std::back_inserter(
unpack(result)));
226 [[maybe_unused]]
constexpr size_t total_size = (
decltype(std::span{ranges})::extent + ... + 0);
227 static_assert(result.size() == total_size,
"size of result buffer does not match the sum of input buffers");
230 const size_t total_size = (ranges.size() + ... + 0);
232 "result buffer has static extent that does not match the sum of input buffers");
236 return [itr = std::ranges::begin(result)](
auto&& range)
mutable {
237 std::copy(std::ranges::begin(range), std::ranges::end(range), itr);
238 std::advance(itr, std::ranges::size(range));
244 (fill_fn(std::forward<Rs>(ranges)), ...);
261template <
typename OutR = detail::AutoDetect, ranges::spanable_range... Rs>
263 requires(all_same_v<std::ranges::range_value_t<Rs>...>)
265 if constexpr(std::same_as<detail::AutoDetect, OutR>) {
267 static_assert(
sizeof...(Rs) > 0,
"Cannot auto-detect the output type if not a single input range is provided.");
268 using candidate_result_t = std::remove_cvref_t<std::tuple_element_t<0, std::tuple<Rs...>>>;
269 using result_range_value_t = std::remove_cvref_t<std::ranges::range_value_t<candidate_result_t>>;
274 constexpr size_t total_size = (
decltype(std::span{ranges})::extent + ... + 0);
275 using out_array_t = std::array<result_range_value_t, total_size>;
282 "First input range has static extent, but a dynamically allocated output range is required. Please explicitly specify a dynamically allocatable output type.");
291template <
typename... Alts,
typename... Ts>
293 return (std::holds_alternative<Alts>(v) || ...);
296template <
typename GeneralVariantT,
typename SpecialT>
298 return std::is_constructible_v<GeneralVariantT, SpecialT>;
301template <
typename GeneralVariantT,
typename... SpecialTs>
303 return (std::is_constructible_v<GeneralVariantT, SpecialTs> && ...);
313template <
typename GeneralVariantT,
typename SpecialT>
315 requires(std::is_constructible_v<GeneralVariantT, std::decay_t<SpecialT>>)
317 return std::forward<SpecialT>(specific);
327template <
typename GeneralVariantT,
typename... SpecialTs>
328constexpr GeneralVariantT
generalize_to(std::variant<SpecialTs...> specific)
noexcept {
331 "Desired general type must be implicitly constructible by all types of the specialized std::variant<>");
332 return std::visit([](
auto s) -> GeneralVariantT {
return s; }, std::move(specific));
337template <
class... Ts>
339 using Ts::operator()...;
342template <
class... Ts>
352template <std::invocable FunT>
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) {
389#define BOTAN_ASSERT_IS_SOME(v) assert_is_some(v, #v, __func__, __FILE__, __LINE__)
#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)
Helper class to ease in-place marshalling of concatenated fixed-length values.
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])
Helper class to create a RAII-style cleanup callback.
scoped_cleanup(const scoped_cleanup &)=delete
scoped_cleanup & operator=(const scoped_cleanup &)=delete
scoped_cleanup & operator=(scoped_cleanup &&)=delete
scoped_cleanup(FunT cleanup)
scoped_cleanup(scoped_cleanup &&)=delete
int(* final)(unsigned char *, CTX *)
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 bool holds_any_of(const std::variant< Ts... > &v) noexcept
RetT reduce(const std::vector< KeyT > &keys, RetT acc, ReducerT reducer)
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
constexpr decltype(auto) unpack(T &t)
std::string to_string(ErrorType type)
Convert an ErrorType to string.
void assertion_failure(const char *expr_str, const char *assertion_made, const char *func, const char *file, int line)
overloaded(Ts...) -> overloaded< Ts... >