7#ifndef BOTAN_FFI_UTILS_H_
8#define BOTAN_FFI_UTILS_H_
10#include <botan/exceptn.h>
12#include <botan/internal/mem_utils.h>
28 int error_code() const noexcept
override {
return m_err_code; }
41template <
typename T,
typename Deleter>
102 uint32_t
magic()
const {
return m_magic; }
106 uint32_t m_magic_expected;
114template <
typename T, uint32_t MAGIC,
template <
typename... Args>
typename PtrType>
115 requires ffi_object_smart_pointer<PtrType<T>>
140 return static_cast<size_t>(m_obj.use_count());
160concept ffi_object = std::derived_from<T, botan_ffi_obj_base>;
164#define BOTAN_FFI_DECLARE_UNIQUE_STRUCT(NAME, TYPE, MAGIC) \
165 struct NAME final : public Botan_FFI::botan_ffi_obj<TYPE, MAGIC, std::unique_ptr> { \
166 using botan_ffi_obj::botan_ffi_obj; \
169#define BOTAN_FFI_DECLARE_SHARED_STRUCT(NAME, TYPE, MAGIC) \
170 struct NAME final : public Botan_FFI::botan_ffi_obj<TYPE, MAGIC, std::shared_ptr> { \
171 using botan_ffi_obj::botan_ffi_obj; \
174#define BOTAN_FFI_DECLARE_DUMMY_STRUCT(NAME, MAGIC) \
175 struct NAME final : public Botan_FFI::botan_ffi_obj<int, MAGIC, std::unique_ptr> {}
190template <ffi_
object T>
199 if(
auto* t = p->unsafe_get()) {
211template <ffi_
object T>
220 auto& sp = p->unsafe_get_sp();
228template <std::invocable T>
234 }
catch(std::bad_alloc&) {
240 }
catch(std::exception& e) {
250concept ffi_visit_result = std::is_void_v<T> || std::same_as<T, int> || std::same_as<T, BOTAN_FFI_ERROR>;
252template <
typename FnT,
typename ArgT>
257template <
typename FnT,
typename HandleT>
260template <
typename FnT,
typename HandleT>
263template <
typename FnT,
typename HandleT>
275template <ffi_
object HandleT, ffi_visitor_fn<HandleT> VisitorFnT>
285 const auto& p = o->unsafe_get_sp();
290 auto call_visitor = [&](
auto&& ref) ->
int {
291 if constexpr(std::is_void_v<
decltype(func(ref))>) {
305 return call_visitor(*p);
307 return call_visitor(p);
327#define BOTAN_FFI_VISIT(obj, lambda) botan_ffi_visit(obj, lambda, __func__)
329template <ffi_
object T>
337 if(!obj->magic_ok()) {
346template <
typename T,
typename... Args>
352 *obj =
new std::remove_pointer_t<T>(std::forward<Args>(args)...);
357#define BOTAN_FFI_CHECKED_DELETE(o) ffi_delete_object(o, __func__)
360 if(view ==
nullptr) {
363 return view(ctx, buf.data(), buf.size());
368 if(view ==
nullptr) {
371 return view(ctx, str.data(), str.size() + 1);
382template <
typename Fn,
typename... Args>
384 if(out_len ==
nullptr) {
391template <
typename Fn,
typename... Args>
400template <std::
integral T>
401 requires(
sizeof(T) == 1)
403 if(out_len ==
nullptr) {
407 const size_t avail = *out_len;
408 *out_len = required_len;
410 if(avail < required_len || out ==
nullptr) {
412 std::memset(out, 0,
sizeof(T) * avail);
420template <std::
integral T>
421inline int write_output(T out[],
size_t* out_len,
const T buf[],
size_t buf_len) {
422 static_assert(
sizeof(T) == 1,
"T should be either uint8_t or char");
430 std::memcpy(out, buf,
sizeof(T) * buf_len);
#define BOTAN_UNSTABLE_API
const char * what() const noexcept override
Exception(std::string_view msg)
Botan::ErrorType error_type() const noexcept override
FFI_Error(std::string_view what, int err_code)
int error_code() const noexcept override
int(* botan_view_bin_fn)(botan_view_ctx view_ctx, const uint8_t *data, size_t len)
@ BOTAN_FFI_ERROR_EXCEPTION_THROWN
@ BOTAN_FFI_ERROR_OUT_OF_MEMORY
@ BOTAN_FFI_ERROR_INVALID_OBJECT
@ BOTAN_FFI_ERROR_NULL_POINTER
@ BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE
int(* botan_view_str_fn)(botan_view_ctx view_ctx, const char *str, size_t len)
int botan_ffi_visit(HandleT *o, VisitorFnT func, const char *func_name)
void ffi_clear_last_exception()
int invoke_view_callback(botan_view_bin_fn view, botan_view_ctx ctx, std::span< const uint8_t > buf)
auto safe_get_sp(T *p) -> const typename T::smart_pointer_type &
int ffi_error_exception_thrown(const char *func_name, const char *exn, int rc)
int botan_view_bin_bounce_fn(botan_view_ctx vctx, const uint8_t *buf, size_t len)
int botan_view_str_bounce_fn(botan_view_ctx vctx, const char *str, size_t len)
int copy_view_bin(uint8_t out[], size_t *out_len, Fn fn, Args... args)
auto safe_get(T *p) -> typename T::element_type &
int check_and_prepare_output_space(T out[], size_t *out_len, size_t required_len)
int ffi_delete_object(T *obj, const char *func_name)
BOTAN_FFI_ERROR ffi_new_object(T *obj, Args &&... args)
int copy_view_str(uint8_t out[], size_t *out_len, Fn fn, Args... args)
int write_output(T out[], size_t *out_len, const T buf[], size_t buf_len)
int ffi_guard_thunk(const char *func_name, T thunk)
int write_vec_output(uint8_t out[], size_t *out_len, std::span< const uint8_t > buf)
bool any_null_pointers(Ptrs... ptr)
int write_str_output(char out[], size_t *out_len, const std::string &str)
bool any_null_pointers(Ptrs... ptr)
botan_ffi_obj_base(botan_ffi_obj_base &&other)=delete
botan_ffi_obj_base(const botan_ffi_obj_base &other)=delete
virtual size_t use_count() const =0
bool appears_to_be_valid_ffi_handle() const
botan_ffi_obj_base & operator=(const botan_ffi_obj_base &other)=delete
virtual bool is_shared() const =0
~botan_ffi_obj_base()=default
botan_ffi_obj_base & operator=(botan_ffi_obj_base &&other)=delete
constexpr botan_ffi_obj_base(uint32_t magic)
const smart_pointer_type & unsafe_get_sp() const
Retrieve the smart pointer to the underlying object. May be invalid.
size_t use_count() const final
botan_ffi_obj & operator=(const botan_ffi_obj &other)=delete
smart_pointer_type & unsafe_get_sp()
Retrieve the smart pointer to the underlying object. May be invalid.
PtrType< T > smart_pointer_type
botan_ffi_obj(botan_ffi_obj &&other)=delete
T * unsafe_get() const
Retrieve the raw pointer to the underlying object. May be nullptr.
bool is_shared() const final
botan_ffi_obj(smart_pointer_type obj)
botan_ffi_obj(const botan_ffi_obj &other)=delete
botan_ffi_obj & operator=(botan_ffi_obj &&other)=delete