diff options
author | lloyd <[email protected]> | 2006-07-01 18:49:24 +0000 |
---|---|---|
committer | lloyd <[email protected]> | 2006-07-01 18:49:24 +0000 |
commit | 3d1d14cf405111e30643cf4c7674d441cc07a2e0 (patch) | |
tree | f3cdca0476a082634e97a4117c88e61c5a13036e /src | |
parent | 8192e9fde8060c740a2e03c9d0def6e87d10b86b (diff) |
Clean up initialization a little bit more
Diffstat (limited to 'src')
-rw-r--r-- | src/init_def.cpp | 11 | ||||
-rw-r--r-- | src/libstate.cpp | 23 | ||||
-rw-r--r-- | src/modules.cpp | 73 |
3 files changed, 74 insertions, 33 deletions
diff --git a/src/init_def.cpp b/src/init_def.cpp index 6b2e5e66b..38d683dea 100644 --- a/src/init_def.cpp +++ b/src/init_def.cpp @@ -6,7 +6,7 @@ #include <botan/init.h> #include <botan/libstate.h> #include <botan/modules.h> -#include <botan/conf.h> +#include <botan/config.h> #include <botan/defalloc.h> #include <botan/fips140.h> #include <botan/x931_rng.h> @@ -38,7 +38,7 @@ namespace Init { void initialize(const std::string& arg_string) { InitializerOptions args(arg_string); - Builtin_Modules modules; + Builtin_Modules modules(false); Mutex_Factory* mutex_factory = 0; @@ -52,18 +52,13 @@ void initialize(const std::string& arg_string) set_global_state(new Library_State(mutex_factory)); global_state().set_default_policy(); - global_state().set_timer(modules.timer()); - - modules.set_allocators(global_state(), false); + global_state().load(modules); if(args.config_file() != "") Config::load(args.config_file(), global_state()); - modules.set_engines(global_state(), args.use_engines()); - global_state().set_transcoder(new Default_Charset_Transcoder); global_state().set_prng(new ANSI_X931_RNG); - modules.set_entropy_sources(global_state()); const u32bit min_entropy = Config::get_u32bit("rng/min_entropy"); diff --git a/src/libstate.cpp b/src/libstate.cpp index 92dfd2913..36d6299ad 100644 --- a/src/libstate.cpp +++ b/src/libstate.cpp @@ -4,6 +4,7 @@ *************************************************/ #include <botan/libstate.h> +#include <botan/modules.h> #include <botan/engine.h> #include <botan/x509stat.h> #include <botan/stl_util.h> @@ -321,6 +322,28 @@ X509_GlobalState& Library_State::x509_state() } /************************************************* +* Load modules * +*************************************************/ +void Library_State::load(Modules& modules) + { + set_timer(modules.timer()); + + std::vector<Allocator*> allocators = modules.allocators(); + + for(u32bit j = 0; j != allocators.size(); j++) + add_allocator(allocators[j], + allocators[j]->type() == modules.default_allocator()); + + std::vector<Engine*> engines = modules.engines(); + for(u32bit j = 0; j != engines.size(); ++j) + add_engine(engines[j]); + + std::vector<EntropySource*> sources = modules.entropy_sources(); + for(u32bit j = 0; j != sources.size(); ++j) + add_entropy_source(sources[j]); + } + +/************************************************* * Library_State Constructor * *************************************************/ Library_State::Library_State(Mutex_Factory* mutex_factory) diff --git a/src/modules.cpp b/src/modules.cpp index ab9f3b68f..1216fc3b9 100644 --- a/src/modules.cpp +++ b/src/modules.cpp @@ -103,83 +103,106 @@ Timer* Builtin_Modules::timer() const #elif defined(BOTAN_EXT_TIMER_WIN32) return new Win32_Timer; #else - return 0; + return new Timer; #endif } /************************************************* -* Set any usable allocators * +* Find any usable allocators * *************************************************/ -void Builtin_Modules::set_allocators(Library_State& state, - bool secure_mem) const +std::vector<Allocator*> Builtin_Modules::allocators() const { - state.add_allocator(new Malloc_Allocator, !secure_mem); + std::vector<Allocator*> allocators; - state.add_allocator(new Locking_Allocator, secure_mem); +#if defined(BOTAN_EXT_ALLOC_MMAP) + allocators.push_back(new MemoryMapping_Allocator); +#endif + allocators.push_back(new Locking_Allocator); + allocators.push_back(new Malloc_Allocator); + + return allocators; + } + +/************************************************* +* Return the default allocator * +*************************************************/ +std::string Builtin_Modules::default_allocator() const + { + if(should_lock) + { #if defined(BOTAN_EXT_ALLOC_MMAP) - state.add_allocator(new MemoryMapping_Allocator, secure_mem); + return "mmap"; +#else + return "locking"; #endif + } + else + return "malloc"; } /************************************************* * Register any usable entropy sources * *************************************************/ -void Builtin_Modules::set_entropy_sources(Library_State& state) const +std::vector<EntropySource*> Builtin_Modules::entropy_sources() const { - state.add_entropy_source(new File_EntropySource); + std::vector<EntropySource*> sources; + + sources.push_back(new File_EntropySource); #if defined(BOTAN_EXT_ENTROPY_SRC_AEP) - state.add_entropy_source(new AEP_EntropySource); + sources.push_back(new AEP_EntropySource); #endif #if defined(BOTAN_EXT_ENTROPY_SRC_EGD) - state.add_entropy_source(new EGD_EntropySource); + sources.push_back(new EGD_EntropySource); #endif #if defined(BOTAN_EXT_ENTROPY_SRC_CAPI) - state.add_entropy_source(new Win32_CAPI_EntropySource); + sources.push_back(new Win32_CAPI_EntropySource); #endif #if defined(BOTAN_EXT_ENTROPY_SRC_WIN32) - state.add_entropy_source(new Win32_EntropySource); + sources.push_back(new Win32_EntropySource); #endif #if defined(BOTAN_EXT_ENTROPY_SRC_UNIX) - state.add_entropy_source(new Unix_EntropySource); + sources.push_back(new Unix_EntropySource); #endif #if defined(BOTAN_EXT_ENTROPY_SRC_BEOS) - state.add_entropy_source(new BeOS_EntropySource); + sources.push_back(new BeOS_EntropySource); #endif #if defined(BOTAN_EXT_ENTROPY_SRC_FTW) - state.add_entropy_source(new FTW_EntropySource); + sources.push_back(new FTW_EntropySource); #endif + + return sources; } /************************************************* * Find any usable engines * *************************************************/ -void Builtin_Modules::set_engines(Library_State& state, - bool use_engines) const +std::vector<Engine*> Builtin_Modules::engines() const { - if(use_engines) - { + std::vector<Engine*> engines; + #if defined(BOTAN_EXT_ENGINE_AEP) - state.add_engine(new AEP_Engine); + engines.push_back(new AEP_Engine); #endif #if defined(BOTAN_EXT_ENGINE_GNU_MP) - state.add_engine(new GMP_Engine); + engines.push_back(new GMP_Engine); #endif #if defined(BOTAN_EXT_ENGINE_OPENSSL) - state.add_engine(new OpenSSL_Engine); + engines.push_back(new OpenSSL_Engine); #endif - } - state.add_engine(new Default_Engine); + engines.push_back(new Default_Engine); + + return engines; } } |