diff options
-rw-r--r-- | include/libstate.h | 2 | ||||
-rw-r--r-- | include/modules.h | 19 | ||||
-rw-r--r-- | src/init_def.cpp | 14 | ||||
-rw-r--r-- | src/libstate.cpp | 6 | ||||
-rw-r--r-- | src/modules.cpp | 100 |
5 files changed, 42 insertions, 99 deletions
diff --git a/include/libstate.h b/include/libstate.h index b2879c7d9..6b4c2cac1 100644 --- a/include/libstate.h +++ b/include/libstate.h @@ -31,7 +31,7 @@ class Library_State friend class Engine_Iterator; Allocator* get_allocator(const std::string& = "") const; - void add_allocator(Allocator*); + void add_allocator(Allocator*, bool = false); void set_prng(RandomNumberGenerator*); void randomize(byte[], u32bit); diff --git a/include/modules.h b/include/modules.h index 7441134d1..48aeb11fb 100644 --- a/include/modules.h +++ b/include/modules.h @@ -6,9 +6,6 @@ #ifndef BOTAN_MODULE_FACTORIES_H__ #define BOTAN_MODULE_FACTORIES_H__ -#include <string> -#include <vector> - namespace Botan { /************************************************* @@ -19,12 +16,12 @@ class Modules public: void load(class Library_State&) const; - virtual class Mutex_Factory* mutex_factory() const; - virtual class Timer* timer() const; + virtual class Mutex_Factory* mutex_factory() const { return 0; } + virtual class Timer* timer() const { return 0; } - virtual std::vector<class Allocator*> allocators() const; - virtual std::vector<class EntropySource*> entropy_sources() const; - virtual std::vector<class Engine*> engines() const; + virtual void set_allocators(class Library_State&, bool) const {} + virtual void set_entropy_sources(class Library_State&) const {} + virtual void set_engines(class Library_State&, bool) const {} virtual ~Modules() {} }; @@ -38,9 +35,9 @@ class Builtin_Modules : public Modules class Mutex_Factory* mutex_factory() const; class Timer* timer() const; - std::vector<class Allocator*> allocators() const; - std::vector<class EntropySource*> entropy_sources() const; - std::vector<class Engine*> engines() const; + void set_allocators(class Library_State&, bool) const; + void set_entropy_sources(class Library_State&) const; + void set_engines(class Library_State&, bool) const; }; } diff --git a/src/init_def.cpp b/src/init_def.cpp index 17d8899e2..6b2e5e66b 100644 --- a/src/init_def.cpp +++ b/src/init_def.cpp @@ -50,28 +50,20 @@ 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()); - std::vector<Allocator*> allocators = modules.allocators(); - for(u32bit j = 0; j != allocators.size(); ++j) - global_state().add_allocator(allocators[j]); + modules.set_allocators(global_state(), false); if(args.config_file() != "") Config::load(args.config_file(), global_state()); - std::vector<Engine*> engines = modules.engines(); - for(u32bit j = 0; j != engines.size(); ++j) - global_state().add_engine(engines[j]); + modules.set_engines(global_state(), args.use_engines()); global_state().set_transcoder(new Default_Charset_Transcoder); - global_state().set_prng(new ANSI_X931_RNG); - std::vector<EntropySource*> sources = modules.entropy_sources(); - for(u32bit j = 0; j != sources.size(); ++j) - global_state().add_entropy_source(sources[j], true); + 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 d5841d924..92dfd2913 100644 --- a/src/libstate.cpp +++ b/src/libstate.cpp @@ -101,7 +101,8 @@ Allocator* Library_State::get_allocator(const std::string& type) const /************************************************* * Create a new name to object mapping * *************************************************/ -void Library_State::add_allocator(Allocator* allocator) +void Library_State::add_allocator(Allocator* allocator, + bool set_as_default) { Named_Mutex_Holder lock("allocator"); @@ -112,6 +113,9 @@ void Library_State::add_allocator(Allocator* allocator) delete alloc_factory[type]; alloc_factory[type] = allocator; + + if(set_as_default) + set_option("conf", "base/default_allocator", type); } /************************************************* diff --git a/src/modules.cpp b/src/modules.cpp index fdc4dac71..ab9f3b68f 100644 --- a/src/modules.cpp +++ b/src/modules.cpp @@ -74,48 +74,6 @@ namespace Botan { /************************************************* -* Default No-Op Implementation * -*************************************************/ -class Mutex_Factory* Modules::mutex_factory() const - { - return 0; - } - -/************************************************* -* Default No-Op Implementation * -*************************************************/ -class Timer* Modules::timer() const - { - return 0; - } - -/************************************************* -* Default No-Op Implementation * -*************************************************/ -std::vector<Allocator*> Modules::allocators() const - { - return std::vector<Allocator*>(); - } - -/************************************************* -* Default No-Op Implementation * -*************************************************/ -std::vector<EntropySource*> Modules::entropy_sources() const - { - return std::vector<EntropySource*>(); - } - -/************************************************* -* Default No-Op Implementation * -*************************************************/ -std::vector<Engine*> Modules::engines() const - { - return std::vector<Engine*>(); - } - -namespace Modules { - -/************************************************* * Return a mutex factory, if available * *************************************************/ Mutex_Factory* Builtin_Modules::mutex_factory() const @@ -150,86 +108,78 @@ Timer* Builtin_Modules::timer() const } /************************************************* -* Find any usable allocators * +* Set any usable allocators * *************************************************/ -std::vector<Allocator*> Builtin_Modules::allocators() const +void Builtin_Modules::set_allocators(Library_State& state, + bool secure_mem) const { - std::vector<Allocator*> allocators; + state.add_allocator(new Malloc_Allocator, !secure_mem); + + state.add_allocator(new Locking_Allocator, secure_mem); #if defined(BOTAN_EXT_ALLOC_MMAP) - allocators.push_back(new MemoryMapping_Allocator); + state.add_allocator(new MemoryMapping_Allocator, secure_mem); #endif - - allocators.push_back(new Locking_Allocator); - allocators.push_back(new Malloc_Allocator); - - return allocators; } /************************************************* * Register any usable entropy sources * *************************************************/ -std::vector<EntropySource*> Builtin_Modules::entropy_sources() const +void Builtin_Modules::set_entropy_sources(Library_State& state) const { - std::vector<EntropySource*> sources; - - sources.push_back(new File_EntropySource); + state.add_entropy_source(new File_EntropySource); #if defined(BOTAN_EXT_ENTROPY_SRC_AEP) - sources.push_back(new AEP_EntropySource); + state.add_entropy_source(new AEP_EntropySource); #endif #if defined(BOTAN_EXT_ENTROPY_SRC_EGD) - sources.push_back(new EGD_EntropySource); + state.add_entropy_source(new EGD_EntropySource); #endif #if defined(BOTAN_EXT_ENTROPY_SRC_CAPI) - sources.push_back(new Win32_CAPI_EntropySource); + state.add_entropy_source(new Win32_CAPI_EntropySource); #endif #if defined(BOTAN_EXT_ENTROPY_SRC_WIN32) - sources.push_back(new Win32_EntropySource); + state.add_entropy_source(new Win32_EntropySource); #endif #if defined(BOTAN_EXT_ENTROPY_SRC_UNIX) - sources.push_back(new Unix_EntropySource); + state.add_entropy_source(new Unix_EntropySource); #endif #if defined(BOTAN_EXT_ENTROPY_SRC_BEOS) - sources.push_back(new BeOS_EntropySource); + state.add_entropy_source(new BeOS_EntropySource); #endif #if defined(BOTAN_EXT_ENTROPY_SRC_FTW) - sources.push_back(new FTW_EntropySource); + state.add_entropy_source(new FTW_EntropySource); #endif - - return sources; } /************************************************* * Find any usable engines * *************************************************/ -std::vector<Engine*> Builtin_Modules::engines() const +void Builtin_Modules::set_engines(Library_State& state, + bool use_engines) const { - std::vector<Engine*> engines; - + if(use_engines) + { #if defined(BOTAN_EXT_ENGINE_AEP) - engines.push_back(new AEP_Engine); + state.add_engine(new AEP_Engine); #endif #if defined(BOTAN_EXT_ENGINE_GNU_MP) - engines.push_back(new GMP_Engine); + state.add_engine(new GMP_Engine); #endif #if defined(BOTAN_EXT_ENGINE_OPENSSL) - engines.push_back(new OpenSSL_Engine); + state.add_engine(new OpenSSL_Engine); #endif + } - engines.push_back(new Default_Engine); - - return engines; + state.add_engine(new Default_Engine); } } - -} |