aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorlloyd <[email protected]>2007-10-13 20:11:51 +0000
committerlloyd <[email protected]>2007-10-13 20:11:51 +0000
commite0fe7b1a473dd862e41a662d4bb9edab381370cf (patch)
treee4820c0fe53636fa9d640f0a8fa7b4987fbc3796
parent0fcaf44cec8d534f070ae336785f7d4889a8b1ad (diff)
Move most of the initializer code directly into the Library_State constructor
-rw-r--r--include/libstate.h7
-rw-r--r--src/init_def.cpp71
-rw-r--r--src/libstate.cpp74
3 files changed, 68 insertions, 84 deletions
diff --git a/include/libstate.h b/include/libstate.h
index dcd6f26a5..564605a2a 100644
--- a/include/libstate.h
+++ b/include/libstate.h
@@ -8,6 +8,7 @@
#include <botan/base.h>
#include <botan/enums.h>
+#include <botan/init.h>
#include <botan/ui.h>
#include <string>
#include <vector>
@@ -52,8 +53,6 @@ class Library_State
void add_entropy(EntropySource&, bool);
u32bit seed_prng(bool, u32bit);
- void load(class Modules&);
-
void set_timer(class Timer*);
u64bit system_clock() const;
@@ -74,7 +73,7 @@ class Library_State
std::string transcode(const std::string,
Character_Set, Character_Set) const;
- Library_State(class Mutex_Factory*);
+ Library_State(const InitializerOptions&, Modules&);
~Library_State();
private:
Library_State(const Library_State&) {}
@@ -84,7 +83,7 @@ class Library_State
class Mutex_Factory* mutex_factory;
class Timer* timer;
- class Config* config_obj;
+ mutable class Config* config_obj;
class X509_GlobalState* x509_state_obj;
std::map<std::string, class Mutex*> locks;
diff --git a/src/init_def.cpp b/src/init_def.cpp
index 7f465d3eb..50365b3d6 100644
--- a/src/init_def.cpp
+++ b/src/init_def.cpp
@@ -6,67 +6,19 @@
#include <botan/init.h>
#include <botan/libstate.h>
#include <botan/modules.h>
-#include <botan/config.h>
-#include <botan/defalloc.h>
#include <botan/fips140.h>
-#include <botan/x931_rng.h>
-#include <botan/def_char.h>
namespace Botan {
/*************************************************
* Library Initialization *
*************************************************/
-void LibraryInitializer::initialize(const std::string& arg_string)
- {
- InitializerOptions args(arg_string);
- initialize(args);
- }
-
-/*************************************************
-* Library Initialization *
-*************************************************/
-void LibraryInitializer::initialize(const InitializerOptions& args)
- {
- Builtin_Modules modules(args);
- initialize(args, modules);
- }
-
-/*************************************************
-* Library Initialization *
-*************************************************/
void LibraryInitializer::initialize(const InitializerOptions& args,
Modules& modules)
{
try
{
- set_global_state(
- new Library_State(
- args.thread_safe() ?
- modules.mutex_factory() :
- new Default_Mutex_Factory
- )
- );
-
- global_state().config().load_defaults();
- if(args.config_file() != "")
- global_config().load_inifile(args.config_file());
-
- global_state().load(modules);
- global_state().set_prng(new ANSI_X931_RNG);
-
- if(args.seed_rng())
- {
- for(u32bit j = 0; j != 4; ++j)
- {
- global_state().seed_prng(true, 384);
- if(global_state().rng_is_seeded())
- break;
- }
-
- if(!global_state().rng_is_seeded())
- throw PRNG_Unseeded("Unable to collect sufficient entropy");
- }
+ set_global_state(new Library_State(args, modules));
if(args.fips_mode() || args.self_test())
{
@@ -89,4 +41,25 @@ void LibraryInitializer::deinitialize()
set_global_state(0);
}
+/*************************************************
+* Library Initialization *
+*************************************************/
+void LibraryInitializer::initialize(const std::string& arg_string)
+ {
+ InitializerOptions args(arg_string);
+ Builtin_Modules modules(args);
+
+ initialize(args, modules);
+ }
+
+/*************************************************
+* Library Initialization *
+*************************************************/
+void LibraryInitializer::initialize(const InitializerOptions& args)
+ {
+ Builtin_Modules modules(args);
+
+ initialize(args, modules);
+ }
+
}
diff --git a/src/libstate.cpp b/src/libstate.cpp
index 62ecd5cdf..8d6475e64 100644
--- a/src/libstate.cpp
+++ b/src/libstate.cpp
@@ -12,6 +12,7 @@
#include <botan/mutex.h>
#include <botan/timers.h>
#include <botan/charset.h>
+#include <botan/x931_rng.h>
#include <algorithm>
namespace Botan {
@@ -136,11 +137,8 @@ void Library_State::set_default_allocator(const std::string& type) const
*************************************************/
void Library_State::set_timer(Timer* new_timer)
{
- if(new_timer)
- {
- delete timer;
- timer = new_timer;
- }
+ delete timer;
+ timer = new_timer;
}
/*************************************************
@@ -312,18 +310,40 @@ void Library_State::pulse(Pulse_Type pulse_type) const
Config& Library_State::config() const
{
if(!config_obj)
- throw Invalid_State("Library_State::config(): No config set");
+ {
+ printf("Lazy creation of the global config\n");
+ config_obj = new Config();
+ config_obj->load_defaults();
+ }
return (*config_obj);
}
/*************************************************
-* Load modules *
+* Library_State Constructor *
*************************************************/
-void Library_State::load(Modules& modules)
+Library_State::Library_State(const InitializerOptions& args,
+ Modules& modules)
{
- set_timer(modules.timer());
- set_transcoder(modules.transcoder());
+ if(args.thread_safe())
+ mutex_factory = modules.mutex_factory();
+ else
+ mutex_factory = new Default_Mutex_Factory;
+
+ timer = modules.timer();
+ transcoder = modules.transcoder();
+
+ if(args.config_file() != "")
+ config().load_inifile(args.config_file());
+
+ locks["settings"] = get_mutex();
+ locks["allocator"] = get_mutex();
+ locks["rng"] = get_mutex();
+ locks["engine"] = get_mutex();
+ rng = 0;
+ cached_default_allocator = 0;
+ x509_state_obj = 0;
+ ui = 0;
std::vector<Allocator*> mod_allocs = modules.allocators();
for(u32bit j = 0; j != mod_allocs.size(); ++j)
@@ -341,29 +361,21 @@ void Library_State::load(Modules& modules)
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)
- {
- if(!mutex_factory)
- throw Exception("Library_State: no mutex found");
-
- this->mutex_factory = mutex_factory;
- this->timer = new Timer();
- this->transcoder = 0;
- this->config_obj = new Config();
+ set_prng(new ANSI_X931_RNG);
- locks["settings"] = get_mutex();
- locks["allocator"] = get_mutex();
- locks["rng"] = get_mutex();
- locks["engine"] = get_mutex();
- rng = 0;
- cached_default_allocator = 0;
- x509_state_obj = 0;
- ui = 0;
+ if(args.seed_rng())
+ {
+ for(u32bit j = 0; j != 4; ++j)
+ {
+ seed_prng(true, 384);
+ if(rng_is_seeded())
+ break;
+ }
+
+ if(!rng_is_seeded())
+ throw PRNG_Unseeded("Unable to collect sufficient entropy");
+ }
}
/*************************************************