Botan 1.10.17
Botan::Library_State Class Reference

#include <libstate.h>

Public Member Functions

void add_alias (const std::string &key, const std::string &value)
void add_allocator (Allocator *alloc)
Algorithm_Factoryalgorithm_factory () const
std::string deref_alias (const std::string &alias) const
std::string get (const std::string &section, const std::string &key) const
Allocatorget_allocator (const std::string &name="") const
Mutexget_mutex () const
RandomNumberGeneratorglobal_rng ()
void initialize (bool thread_safe)
bool is_set (const std::string &section, const std::string &key) const
 Library_State ()
void set (const std::string &section, const std::string &key, const std::string &value, bool overwrite=true)
void set_default_allocator (const std::string &name)
 ~Library_State ()

Detailed Description

Global state container aka the buritto at the center of it all

Definition at line 27 of file libstate.h.

Constructor & Destructor Documentation

◆ Library_State()

Botan::Library_State::Library_State ( )

Definition at line 266 of file libstate.cpp.

267 {
268 mutex_factory = 0;
269 allocator_lock = config_lock = 0;
270 cached_default_allocator = 0;
271 m_algorithm_factory = 0;
272
273 global_rng_lock = 0;
274 global_rng_ptr = 0;
275 }

◆ ~Library_State()

Botan::Library_State::~Library_State ( )

Definition at line 280 of file libstate.cpp.

281 {
282 delete m_algorithm_factory;
283 delete global_rng_ptr;
284
285 cached_default_allocator = 0;
286
287 for(size_t i = 0; i != allocators.size(); ++i)
288 {
289 allocators[i]->destroy();
290 delete allocators[i];
291 }
292
293 delete global_rng_lock;
294 delete allocator_lock;
295 delete mutex_factory;
296 delete config_lock;
297 }

Member Function Documentation

◆ add_alias()

void Botan::Library_State::add_alias ( const std::string & key,
const std::string & value )

Add a parameter value to the "alias" section.

Parameters
keythe name of the parameter which shall have a new alias
valuethe new alias

Definition at line 154 of file libstate.cpp.

155 {
156 set("alias", key, value);
157 }
void set(const std::string &section, const std::string &key, const std::string &value, bool overwrite=true)
Definition libstate.cpp:137

References set().

◆ add_allocator()

void Botan::Library_State::add_allocator ( Allocator * alloc)

Add a new allocator to the list of available ones

Parameters
allocthe allocator to add

Definition at line 87 of file libstate.cpp.

88 {
89 Mutex_Holder lock(allocator_lock);
90
91 allocator->init();
92
93 allocators.push_back(allocator);
94 alloc_factory[allocator->type()] = allocator;
95 }

References Botan::Allocator::init(), and Botan::Allocator::type().

Referenced by initialize().

◆ algorithm_factory()

◆ deref_alias()

std::string Botan::Library_State::deref_alias ( const std::string & alias) const

Resolve an alias.

Parameters
aliasthe alias to resolve.
Returns
what the alias stands for

Definition at line 162 of file libstate.cpp.

163 {
164 std::string result = key;
165 while(is_set("alias", result))
166 result = get("alias", result);
167 return result;
168 }
bool is_set(const std::string &section, const std::string &key) const
Definition libstate.cpp:126
std::string get(const std::string &section, const std::string &key) const
Definition libstate.cpp:114

References get(), and is_set().

Referenced by Botan::get_pbe(), Botan::get_pbe(), and Botan::SCAN_Name::SCAN_Name().

◆ get()

std::string Botan::Library_State::get ( const std::string & section,
const std::string & key ) const

Get a parameter value as std::string.

Parameters
sectionthe section of the desired key
keythe desired keys name
Returns
the value of the parameter

Definition at line 114 of file libstate.cpp.

116 {
117 Mutex_Holder lock(config_lock);
118
120 section + "/" + key, "");
121 }
V search_map(const std::map< K, V > &mapping, const K &key, const V &null_result=V())
Definition stl_util.h:43

References Botan::search_map().

Referenced by deref_alias(), Botan::DL_Group::DL_Group(), Botan::EC_Group::EC_Group(), Botan::OIDS::lookup(), and Botan::OIDS::lookup().

◆ get_allocator()

Allocator * Botan::Library_State::get_allocator ( const std::string & name = "") const
Parameters
namethe name of the allocator
Returns
allocator matching this name, or NULL

Definition at line 67 of file libstate.cpp.

68 {
69 Mutex_Holder lock(allocator_lock);
70
71 if(type != "")
72 return search_map<std::string, Allocator*>(alloc_factory, type, 0);
73
74 if(!cached_default_allocator)
75 {
76 cached_default_allocator =
78 default_allocator_name, 0);
79 }
80
81 return cached_default_allocator;
82 }

References Botan::search_map().

Referenced by Botan::Allocator::get().

◆ get_mutex()

Mutex * Botan::Library_State::get_mutex ( ) const
Returns
newly created Mutex (free with delete)

Definition at line 59 of file libstate.cpp.

60 {
61 return mutex_factory->make();
62 }

Referenced by initialize().

◆ global_rng()

RandomNumberGenerator & Botan::Library_State::global_rng ( )
Returns
global RandomNumberGenerator

Definition at line 183 of file libstate.cpp.

184 {
185 Mutex_Holder lock(global_rng_lock);
186
187 if(!global_rng_ptr)
188 global_rng_ptr = make_global_rng(algorithm_factory(),
189 global_rng_lock);
190
191 return *global_rng_ptr;
192 }
Algorithm_Factory & algorithm_factory() const
Definition libstate.cpp:173

References algorithm_factory().

Referenced by Botan::AutoSeeded_RNG::AutoSeeded_RNG().

◆ initialize()

void Botan::Library_State::initialize ( bool thread_safe)
Parameters
thread_safeshould a mutex be used for serialization

Definition at line 197 of file libstate.cpp.

198 {
200
201 if(mutex_factory)
202 throw Invalid_State("Library_State has already been initialized");
203
204 if(!thread_safe)
205 {
206 mutex_factory = new Noop_Mutex_Factory;
207 }
208 else
209 {
210#if defined(BOTAN_HAS_MUTEX_PTHREAD)
211 mutex_factory = new Pthread_Mutex_Factory;
212#elif defined(BOTAN_HAS_MUTEX_WIN32)
213 mutex_factory = new Win32_Mutex_Factory;
214#else
215 throw Invalid_State("Could not find a thread-safe mutex object to use");
216#endif
217 }
218
219 allocator_lock = get_mutex();
220 config_lock = get_mutex();
221 global_rng_lock = get_mutex();
222
223 default_allocator_name = has_mlock() ? "locking" : "malloc";
224
225 add_allocator(new Malloc_Allocator);
227
228#if defined(BOTAN_HAS_ALLOC_MMAP)
230#endif
231
232 load_default_config();
233
234 m_algorithm_factory = new Algorithm_Factory(*mutex_factory);
235
236#if defined(BOTAN_HAS_ENGINE_GNU_MP)
237 algorithm_factory().add_engine(new GMP_Engine);
238#endif
239
240#if defined(BOTAN_HAS_ENGINE_OPENSSL)
241 algorithm_factory().add_engine(new OpenSSL_Engine);
242#endif
243
244#if defined(BOTAN_HAS_ENGINE_AES_ISA)
245 algorithm_factory().add_engine(new AES_ISA_Engine);
246#endif
247
248#if defined(BOTAN_HAS_ENGINE_SIMD)
249 algorithm_factory().add_engine(new SIMD_Engine);
250#endif
251
252#if defined(BOTAN_HAS_ENGINE_ASSEMBLER)
253 algorithm_factory().add_engine(new Assembler_Engine);
254#endif
255
256 algorithm_factory().add_engine(new Core_Engine);
257
258#if defined(BOTAN_HAS_SELFTESTS)
260#endif
261 }
void add_engine(Engine *engine)
Algorithm_Factory(Mutex_Factory &mf)
static void initialize()
Definition cpuid.cpp:196
void add_allocator(Allocator *alloc)
Definition libstate.cpp:87
Mutex * get_mutex() const
Definition libstate.cpp:59
Locking_Allocator(Mutex *mutex)
Definition defalloc.h:36
MemoryMapping_Allocator(Mutex *mutex)
Definition mmap_mem.h:27
bool has_mlock()
Definition mlock.cpp:19
void confirm_startup_self_tests(Algorithm_Factory &af)
Definition selftest.cpp:226

References add_allocator(), Botan::Algorithm_Factory::add_engine(), Botan::Algorithm_Factory::Algorithm_Factory(), algorithm_factory(), Botan::confirm_startup_self_tests(), get_mutex(), Botan::has_mlock(), Botan::CPUID::initialize(), Botan::Invalid_State::Invalid_State(), Botan::Locking_Allocator::Locking_Allocator(), and Botan::MemoryMapping_Allocator::MemoryMapping_Allocator().

Referenced by Botan::Global_State_Management::global_state(), and Botan::LibraryInitializer::initialize().

◆ is_set()

bool Botan::Library_State::is_set ( const std::string & section,
const std::string & key ) const

Check whether a certain parameter is set or not.

Parameters
sectionthe section of the desired key
keythe desired keys name
Returns
true if the parameters value is set, false otherwise

Definition at line 126 of file libstate.cpp.

128 {
129 Mutex_Holder lock(config_lock);
130
131 return config.count(section + "/" + key) != 0;
132 }

Referenced by deref_alias(), and Botan::OIDS::have_oid().

◆ set()

void Botan::Library_State::set ( const std::string & section,
const std::string & key,
const std::string & value,
bool overwrite = true )

Set a configuration parameter.

Parameters
sectionthe section of the desired key
keythe desired keys name
valuethe new value
overwriteif set to true, the parameters value will be overwritten even if it is already set, otherwise no existing values will be overwritten.

Definition at line 137 of file libstate.cpp.

139 {
140 Mutex_Holder lock(config_lock);
141
142 std::string full_key = section + "/" + key;
143
144 std::map<std::string, std::string>::const_iterator i =
145 config.find(full_key);
146
147 if(overwrite || i == config.end() || i->second == "")
148 config[full_key] = value;
149 }

Referenced by add_alias(), and Botan::OIDS::add_oid().

◆ set_default_allocator()

void Botan::Library_State::set_default_allocator ( const std::string & name)

Set the default allocator

Parameters
namethe name of the allocator to use as the default

Definition at line 100 of file libstate.cpp.

101 {
102 Mutex_Holder lock(allocator_lock);
103
104 if(type == "")
105 return;
106
107 default_allocator_name = type;
108 cached_default_allocator = 0;
109 }

The documentation for this class was generated from the following files: