aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorlloyd <[email protected]>2006-06-25 14:31:43 +0000
committerlloyd <[email protected]>2006-06-25 14:31:43 +0000
commitcbdfc3aff3d9ef7b422f48efe22a11fc2115e248 (patch)
tree33e7c029cb542edf82a93879c41c306f8ae16373 /src
parente6f36f3a64bee29f22f0f3b68a51ab5308a2c186 (diff)
Change how builtin modules are loaded - provide an interface to a
factory class. Currently hardcoded (Builtin_Modules, instantiated in init_def.cpp), but this will allow for some flexibility later on.
Diffstat (limited to 'src')
-rw-r--r--src/init_def.cpp78
-rw-r--r--src/modules.cpp68
2 files changed, 75 insertions, 71 deletions
diff --git a/src/init_def.cpp b/src/init_def.cpp
index 30f2228f5..6a5074665 100644
--- a/src/init_def.cpp
+++ b/src/init_def.cpp
@@ -7,9 +7,7 @@
#include <botan/libstate.h>
#include <botan/modules.h>
#include <botan/conf.h>
-#include <botan/parsing.h>
#include <botan/defalloc.h>
-#include <botan/eng_def.h>
#include <botan/fips140.h>
#include <botan/x931_rng.h>
#include <botan/def_char.h>
@@ -34,90 +32,48 @@ LibraryInitializer::~LibraryInitializer()
namespace Init {
-namespace {
-
-/*************************************************
-* Parse the options string *
-*************************************************/
-std::map<std::string, std::string> parse_args(const std::string& arg_string)
- {
- std::map<std::string, std::string> arg_map;
- std::vector<std::string> args = split_on(arg_string, ' ');
- for(u32bit j = 0; j != args.size(); ++j)
- {
- if(args[j].find('=') == std::string::npos)
- arg_map[args[j]] = "";
- else
- {
- std::vector<std::string> name_and_value = split_on(args[j], '=');
- arg_map[name_and_value[0]] = name_and_value[1];
- }
- }
-
- return arg_map;
- }
-
-/*************************************************
-* Check if an option is set in the argument *
-*************************************************/
-bool arg_set(const std::map<std::string, std::string>& args,
- const std::string& option)
- {
- return (args.find(option) != args.end());
- }
-
-}
-
/*************************************************
* Library Initialization *
*************************************************/
void initialize(const std::string& arg_string)
{
- std::map<std::string, std::string> args = parse_args(arg_string);
+ InitializerOptions args(arg_string);
+ Builtin_Modules modules;
Mutex_Factory* mutex_factory = 0;
- if(arg_set(args, "thread_safe"))
+
+ if(args.thread_safe())
{
- mutex_factory = Modules::get_mutex_factory();
+ mutex_factory = modules.mutex_factory();
if(!mutex_factory)
throw Exception("LibraryInitializer: thread safety impossible");
}
- set_global_state(new Library_State(mutex_factory,
- Modules::get_timer()));
-
- global_state().add_allocator(new Malloc_Allocator);
- global_state().add_allocator(new Locking_Allocator);
+ set_global_state(new Library_State(mutex_factory));
- if(arg_set(args, "secure_memory"))
- {
- std::vector<Allocator*> allocators = Modules::get_allocators();
+ global_state().set_timer(modules.timer());
- for(u32bit j = 0; j != allocators.size(); ++j)
- global_state().add_allocator(allocators[j]);
- }
+ std::vector<Allocator*> allocators = modules.allocators();
+ for(u32bit j = 0; j != allocators.size(); ++j)
+ global_state().add_allocator(allocators[j]);
- if(arg_set(args, "config") && args["config"] != "")
- Config::load(args["config"], global_state());
+ if(args.config_file() != "")
+ Config::load(args.config_file(), global_state());
- if(arg_set(args, "use_engines"))
- {
- std::vector<Engine*> engines = Modules::get_engines();
- for(u32bit j = 0; j != engines.size(); ++j)
- global_state().add_engine(engines[j]);
- }
- global_state().add_engine(new Default_Engine);
+ std::vector<Engine*> engines = modules.engines();
+ for(u32bit j = 0; j != engines.size(); ++j)
+ global_state().add_engine(engines[j]);
global_state().set_transcoder(new Default_Charset_Transcoder);
global_state().set_prng(new ANSI_X931_RNG);
- std::vector<EntropySource*> sources = Modules::get_entropy_sources();
+ std::vector<EntropySource*> sources = modules.entropy_sources();
for(u32bit j = 0; j != sources.size(); ++j)
global_state().add_entropy_source(sources[j], true);
const u32bit min_entropy = Config::get_u32bit("rng/min_entropy");
- if(min_entropy != 0 && !arg_set(args, "no_rng_seed"))
+ if(min_entropy != 0 && args.seed_rng())
{
u32bit total_bits = 0;
for(u32bit j = 0; j != 4; ++j)
diff --git a/src/modules.cpp b/src/modules.cpp
index ccbe65739..fdc4dac71 100644
--- a/src/modules.cpp
+++ b/src/modules.cpp
@@ -4,6 +4,9 @@
*************************************************/
#include <botan/modules.h>
+#include <botan/libstate.h>
+#include <botan/defalloc.h>
+#include <botan/eng_def.h>
#include <botan/es_file.h>
#if defined(BOTAN_EXT_MUTEX_PTHREAD)
@@ -70,12 +73,52 @@
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 {
/*************************************************
-* Register a mutex type, if possible *
+* Return a mutex factory, if available *
*************************************************/
-Mutex_Factory* get_mutex_factory()
+Mutex_Factory* Builtin_Modules::mutex_factory() const
{
#if defined(BOTAN_EXT_MUTEX_PTHREAD)
return new Pthread_Mutex_Factory;
@@ -83,15 +126,15 @@ Mutex_Factory* get_mutex_factory()
return new Win32_Mutex_Factory;
#elif defined(BOTAN_EXT_MUTEX_QT)
return new Qt_Mutex_Factory;
-#endif
-
+#else
return 0;
+#endif
}
/*************************************************
* Find a high resolution timer, if possible *
*************************************************/
-Timer* get_timer()
+Timer* Builtin_Modules::timer() const
{
#if defined(BOTAN_EXT_TIMER_HARDWARE)
return new Hardware_Timer;
@@ -101,15 +144,15 @@ Timer* get_timer()
return new Unix_Timer;
#elif defined(BOTAN_EXT_TIMER_WIN32)
return new Win32_Timer;
-#endif
-
+#else
return 0;
+#endif
}
/*************************************************
* Find any usable allocators *
*************************************************/
-std::vector<Allocator*> get_allocators()
+std::vector<Allocator*> Builtin_Modules::allocators() const
{
std::vector<Allocator*> allocators;
@@ -117,13 +160,16 @@ std::vector<Allocator*> get_allocators()
allocators.push_back(new MemoryMapping_Allocator);
#endif
+ allocators.push_back(new Locking_Allocator);
+ allocators.push_back(new Malloc_Allocator);
+
return allocators;
}
/*************************************************
* Register any usable entropy sources *
*************************************************/
-std::vector<EntropySource*> get_entropy_sources()
+std::vector<EntropySource*> Builtin_Modules::entropy_sources() const
{
std::vector<EntropySource*> sources;
@@ -163,7 +209,7 @@ std::vector<EntropySource*> get_entropy_sources()
/*************************************************
* Find any usable engines *
*************************************************/
-std::vector<Engine*> get_engines()
+std::vector<Engine*> Builtin_Modules::engines() const
{
std::vector<Engine*> engines;
@@ -179,6 +225,8 @@ std::vector<Engine*> get_engines()
engines.push_back(new OpenSSL_Engine);
#endif
+ engines.push_back(new Default_Engine);
+
return engines;
}