Botan 3.11.0
Crypto and TLS for C&
dyn_load.h
Go to the documentation of this file.
1/*
2* Dynamically Loaded Object
3* (C) 2010 Jack Lloyd
4*
5* Botan is released under the Simplified BSD License (see license.txt)
6*/
7
8#ifndef BOTAN_DYNAMIC_LOADER_H_
9#define BOTAN_DYNAMIC_LOADER_H_
10
11#include <botan/types.h>
12#include <optional>
13#include <string>
14
15namespace Botan {
16
17/**
18* Represents a DLL or shared object
19*/
21 public:
22 /**
23 * Load a DLL (or fail with an exception)
24 * @param lib_name name or path to a library
25 *
26 * If you don't use a full path, the search order will be defined
27 * by whatever the system linker does by default. Always using fully
28 * qualified pathnames can help prevent code injection attacks (eg
29 * via manipulation of LD_LIBRARY_PATH on Linux)
30 */
31 explicit Dynamically_Loaded_Library(std::string_view lib_name);
32
33 /**
34 * Unload the DLL
35 * @warning Any pointers returned by resolve()/resolve_symbol()
36 * should not be used after this destructor runs.
37 */
39
40 /**
41 * Try to load a symbol
42 * @param symbol names the symbol to load
43 * @return address of the loaded symbol or std::nullopt if the symbol
44 * was not found
45 */
46 template <typename PtrT>
47 std::optional<PtrT> try_resolve_symbol(const std::string& symbol) const
48 requires(std::is_pointer_v<PtrT>)
49 {
50 void* addr = resolve_symbol_internal(symbol);
51 return addr ? std::optional(reinterpret_cast<PtrT>(addr)) : std::nullopt;
52 }
53
54 /**
55 * Load a symbol (or fail with an exception)
56 * @param symbol names the symbol to load
57 * @return address of the loaded symbol
58 * @throws Invalid_Argument if the symbol is not found
59 */
60 void* resolve_symbol(const std::string& symbol) const;
61
62 /**
63 * Convenience function for casting symbol to the right type
64 * @param symbol names the symbol to load
65 * @return address of the loaded symbol
66 * @throws Invalid_Argument if the symbol is not found
67 */
68 template <typename PtrT>
69 PtrT resolve(const std::string& symbol) const
70 requires(std::is_pointer_v<PtrT>)
71 {
72 return reinterpret_cast<PtrT>(resolve_symbol(symbol));
73 }
74
79
80 private:
81 /// Returns a pointer to the symbol or nullptr if the symbol is not found.
82 void* resolve_symbol_internal(const std::string& symbol) const;
83
84 std::string m_lib_name;
85 void* m_lib;
86};
87
88} // namespace Botan
89
90#endif
#define BOTAN_TEST_API
Definition api.h:41
Dynamically_Loaded_Library & operator=(Dynamically_Loaded_Library &&)=default
std::optional< PtrT > try_resolve_symbol(const std::string &symbol) const
Definition dyn_load.h:47
Dynamically_Loaded_Library(std::string_view lib_name)
Definition dyn_load.cpp:64
Dynamically_Loaded_Library & operator=(const Dynamically_Loaded_Library &)=delete
void * resolve_symbol(const std::string &symbol) const
Definition dyn_load.cpp:75
Dynamically_Loaded_Library(const Dynamically_Loaded_Library &)=delete
Dynamically_Loaded_Library(Dynamically_Loaded_Library &&)=default
PtrT resolve(const std::string &symbol) const
Definition dyn_load.h:69