Botan 2.19.5 Crypto and TLS for C&

dyn_load.cpp
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#include <botan/dyn_load.h>
9#include <botan/exceptn.h>
10
11#if defined(BOTAN_TARGET_OS_HAS_POSIX1)
12 #include <dlfcn.h>
13#elif defined(BOTAN_TARGET_OS_HAS_WIN32)
14 #define NOMINMAX 1
15 #define _WINSOCKAPI_ // stop windows.h including winsock.h
16 #include <windows.h>
17#endif
18
19 namespace Botan {
20
21 namespace {
22
23 void raise_runtime_loader_exception(const std::string& lib_name,
24 const char* msg)
25 {
26 const std::string ex_msg =
27 "Failed to load " + lib_name + ": " +
28 (msg ? msg : "Unknown error");
29
30 throw System_Error(ex_msg, 0);
31 }
32
33}
34
35 Dynamically_Loaded_Library::Dynamically_Loaded_Library(
36 const std::string& library) :
37 m_lib_name(library), m_lib(nullptr)
38 {
39#if defined(BOTAN_TARGET_OS_HAS_POSIX1)
40 m_lib = ::dlopen(m_lib_name.c_str(), RTLD_LAZY);
41
42 if(!m_lib)
43 raise_runtime_loader_exception(m_lib_name, ::dlerror());
44
45#elif defined(BOTAN_TARGET_OS_HAS_WIN32)
46 m_lib = ::LoadLibraryA(m_lib_name.c_str());
47
48 if(!m_lib)
49 raise_runtime_loader_exception(m_lib_name, "LoadLibrary failed");
50#endif
51
52 if(!m_lib)
53 raise_runtime_loader_exception(m_lib_name, "Dynamic load not supported");
54 }
55
56 Dynamically_Loaded_Library::~Dynamically_Loaded_Library()
57 {
58#if defined(BOTAN_TARGET_OS_HAS_POSIX1)
59 ::dlclose(m_lib);
60#elif defined(BOTAN_TARGET_OS_HAS_WIN32)
61 ::FreeLibrary((HMODULE)m_lib);
62#endif
63 }
64
65 void* Dynamically_Loaded_Library::resolve_symbol(const std::string& symbol)
66 {
67 void* addr = nullptr;
68
69#if defined(BOTAN_TARGET_OS_HAS_POSIX1)
70 addr = ::dlsym(m_lib, symbol.c_str());
71#elif defined(BOTAN_TARGET_OS_HAS_WIN32)
72 addr = reinterpret_cast<void*>(::GetProcAddress((HMODULE)m_lib, symbol.c_str()));
73#endif
74
75 if(!addr)
76 throw Invalid_Argument("Failed to resolve symbol " + symbol +
77 " in " + m_lib_name);
78
79 return addr;
80 }
81
82}
Botan::Dynamically_Loaded_Library::Dynamically_Loaded_Library
Dynamically_Loaded_Library(const std::string &lib_name)
Definition: dyn_load.cpp:35
Botan::Dynamically_Loaded_Library::resolve_symbol
void * resolve_symbol(const std::string &symbol)
Definition: dyn_load.cpp:65
Botan::Dynamically_Loaded_Library::~Dynamically_Loaded_Library
~Dynamically_Loaded_Library()
Definition: dyn_load.cpp:56
Botan::Invalid_Argument
Definition: exceptn.h:137
Botan
Definition: alg_id.cpp:13