The unified diff between revisions [92f17752..] and [395d2422..] is displayed below. It can also be downloaded as a raw diff.

This diff has been restricted to the following files: 'src/libstate.cpp'

#
#
# patch "src/libstate.cpp"
#  from [ab62d4062809876fb3f5e4f72bf6d44821bc218d]
#    to [233c9231a05eae79ae231f8136850b43c34914d3]
#
============================================================
--- src/libstate.cpp	ab62d4062809876fb3f5e4f72bf6d44821bc218d
+++ src/libstate.cpp	233c9231a05eae79ae231f8136850b43c34914d3
@@ -1,6 +1,6 @@
 /*************************************************
 * Library Internal/Global State Source File      *
-* (C) 1999-2006 The Botan Project                *
+* (C) 1999-2007 The Botan Project                *
 *************************************************/

 #include <botan/libstate.h>
@@ -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 {
@@ -31,10 +32,7 @@ Library_State& global_state()
 Library_State& global_state()
    {
    if(!global_lib_state)
-      {
-      abort();
-      throw Invalid_State("Library was not initialized correctly");
-      }
+      LibraryInitializer::initialize();
    return (*global_lib_state);
    }

@@ -139,11 +137,8 @@ void Library_State::set_timer(Timer* new
 *************************************************/
 void Library_State::set_timer(Timer* new_timer)
    {
-   if(new_timer)
-      {
-      delete timer;
-      timer = new_timer;
-      }
+   delete timer;
+   timer = new_timer;
    }

 /*************************************************
@@ -315,21 +310,45 @@ Config& Library_State::config() const
 Config& Library_State::config() const
    {
    if(!config_obj)
-      throw Invalid_State("Library_State::config(): No config set");
+      {
+      config_obj = new Config();
+      config_obj->load_defaults();
+      }

    return (*config_obj);
    }

 /*************************************************
-* Load modules                                   *
+* Load a set of modules                          *
 *************************************************/
-void Library_State::load(Modules& modules)
+void Library_State::initialize(const InitializerOptions& args,
+                               Modules& modules)
    {
-   set_timer(modules.timer());
-   set_transcoder(modules.transcoder());
+   if(mutex_factory)
+      throw Invalid_State("Library_State has already been initialized");

+   if(args.thread_safe())
+      mutex_factory = modules.mutex_factory();
+   else
+      mutex_factory = new Default_Mutex_Factory;
+
+   cached_default_allocator = 0;
+   x509_state_obj = 0;
+   ui = 0;
+
+   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();
+
    std::vector<Allocator*> mod_allocs = modules.allocators();
-   for(u32bit j = 0; j != mod_allocs.size(); j++)
+   for(u32bit j = 0; j != mod_allocs.size(); ++j)
       add_allocator(mod_allocs[j]);

    set_default_allocator(modules.default_allocator());
@@ -344,28 +363,38 @@ void Library_State::load(Modules& module
    std::vector<EntropySource*> sources = modules.entropy_sources();
    for(u32bit j = 0; j != sources.size(); ++j)
       add_entropy_source(sources[j]);
+
+   set_prng(new ANSI_X931_RNG);
+
+   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");
+      }
    }

 /*************************************************
 * Library_State Constructor                      *
 *************************************************/
-Library_State::Library_State(Mutex_Factory* mutex_factory)
+Library_State::Library_State()
    {
-   if(!mutex_factory)
-      throw Exception("Library_State: no mutex found");
+   mutex_factory = 0;

-   this->mutex_factory = mutex_factory;
-   this->timer = new Timer();
-   this->transcoder = 0;
-   this->config_obj = new Config();
+   timer = 0;
+   config_obj = 0;
+   x509_state_obj = 0;

-   locks["settings"] = get_mutex();
-   locks["allocator"] = get_mutex();
-   locks["rng"] = get_mutex();
-   locks["engine"] = get_mutex();
+   ui = 0;
+   transcoder = 0;
    rng = 0;
    cached_default_allocator = 0;
-   x509_state_obj = 0;
    ui = 0;
    }

@@ -387,7 +416,7 @@ Library_State::~Library_State()

    cached_default_allocator = 0;

-   for(u32bit j = 0; j != allocators.size(); j++)
+   for(u32bit j = 0; j != allocators.size(); ++j)
       {
       allocators[j]->destroy();
       delete allocators[j];