From 9d529fb82a301f14b9cb0efb6c69c1fdb9c984e2 Mon Sep 17 00:00:00 2001
From: lloyd <lloyd@randombit.net>
Date: Mon, 10 Nov 2008 20:14:38 +0000
Subject: Have Algorithm_Factory::make_hash_function throw an exception if it
 can't find an object to clone.

Add a new constructor to Hash_Filter taking a HashFunction*
---
 src/codec/openpgp/info.txt    |  1 +
 src/codec/openpgp/openpgp.cpp |  5 +++--
 src/filters/algo_filt.cpp     |  6 ++++--
 src/filters/filters.h         | 17 +++++++++++++++--
 src/libstate/algo_factory.cpp |  4 +++-
 src/libstate/lookup.cpp       |  8 +-------
 6 files changed, 27 insertions(+), 14 deletions(-)

(limited to 'src')

diff --git a/src/codec/openpgp/info.txt b/src/codec/openpgp/info.txt
index 5ba418dbf..d43c72843 100644
--- a/src/codec/openpgp/info.txt
+++ b/src/codec/openpgp/info.txt
@@ -10,6 +10,7 @@ openpgp.h
 </add>
 
 <requires>
+crc24
 base64
 filters
 </requires>
diff --git a/src/codec/openpgp/openpgp.cpp b/src/codec/openpgp/openpgp.cpp
index 2a84b037e..25eb15ec5 100644
--- a/src/codec/openpgp/openpgp.cpp
+++ b/src/codec/openpgp/openpgp.cpp
@@ -6,6 +6,7 @@
 #include <botan/openpgp.h>
 #include <botan/filters.h>
 #include <botan/charset.h>
+#include <botan/crc24.h>
 
 namespace Botan {
 
@@ -38,7 +39,7 @@ std::string encode(const byte input[], u32bit length,
 
    Pipe pipe(new Fork(
                 new Base64_Encoder(true, PGP_WIDTH),
-                new Chain(new Hash_Filter("CRC24"), new Base64_Encoder)
+                new Chain(new Hash_Filter(new CRC24), new Base64_Encoder)
                 )
       );
 
@@ -133,7 +134,7 @@ SecureVector<byte> decode(DataSource& source, std::string& label,
 
    Pipe base64(new Base64_Decoder,
                new Fork(0,
-                        new Chain(new Hash_Filter("CRC24"),
+                        new Chain(new Hash_Filter(new CRC24),
                                   new Base64_Encoder)
                   )
       );
diff --git a/src/filters/algo_filt.cpp b/src/filters/algo_filt.cpp
index 2c8402601..33a22b703 100644
--- a/src/filters/algo_filt.cpp
+++ b/src/filters/algo_filt.cpp
@@ -5,6 +5,7 @@
 
 #include <botan/filters.h>
 #include <botan/lookup.h>
+#include <botan/libstate.h>
 #include <algorithm>
 
 namespace Botan {
@@ -55,10 +56,11 @@ void StreamCipher_Filter::write(const byte input[], u32bit length)
 /*************************************************
 * Hash_Filter Constructor                        *
 *************************************************/
-Hash_Filter::Hash_Filter(const std::string& hash_name, u32bit len) :
+Hash_Filter::Hash_Filter(const std::string& algo_spec,
+                         u32bit len) :
    OUTPUT_LENGTH(len)
    {
-   hash = get_hash(hash_name);
+   hash = global_state().algo_factory().make_hash_function(algo_spec);
    }
 
 /*************************************************
diff --git a/src/filters/filters.h b/src/filters/filters.h
index fce38318c..af9e6a6cc 100644
--- a/src/filters/filters.h
+++ b/src/filters/filters.h
@@ -14,6 +14,7 @@
 #include <botan/pipe.h>
 #include <botan/basefilt.h>
 #include <botan/data_snk.h>
+#include <botan/scan_name.h>
 
 #if defined(BOTAN_HAS_BASE64_CODEC)
   #include <botan/base64.h>
@@ -82,13 +83,25 @@ class BOTAN_DLL Hash_Filter : public Filter
 
       /**
       * Construct a hash filter.
-      * @param hash the name of the hash algorithm to use
+      * @param hash_fun the hash function to use
       * @param len the output length of this filter. Leave the default
       * value 0 if you want to use the full output of the hashfunction
       * hash. Otherwise, specify a smaller value here so that the
       * output of the hash algorithm will be cut off.
       */
-      Hash_Filter(const std::string& hash, u32bit len = 0);
+      Hash_Filter(HashFunction* hash_fun, u32bit len = 0) :
+         OUTPUT_LENGTH(len), hash(hash_fun) {}
+
+      /**
+      * Construct a hash filter.
+      * @param request the name of the hash algorithm to use
+      * @param len the output length of this filter. Leave the default
+      * value 0 if you want to use the full output of the hashfunction
+      * hash. Otherwise, specify a smaller value here so that the
+      * output of the hash algorithm will be cut off.
+      */
+      Hash_Filter(const std::string& request, u32bit len = 0);
+
       ~Hash_Filter() { delete hash; }
    private:
       const u32bit OUTPUT_LENGTH;
diff --git a/src/libstate/algo_factory.cpp b/src/libstate/algo_factory.cpp
index c7f10a68a..cbb3c8faa 100644
--- a/src/libstate/algo_factory.cpp
+++ b/src/libstate/algo_factory.cpp
@@ -6,6 +6,7 @@ Algorithm Factory
 #include <botan/libstate.h>
 #include <botan/stl_util.h>
 #include <botan/engine.h>
+#include <botan/exceptn.h>
 #include <algorithm>
 
 namespace Botan {
@@ -66,7 +67,8 @@ HashFunction* Algorithm_Factory::make_hash_function(const SCAN_Name& request)
    const HashFunction* prototype = prototype_hash_function(request);
    if(prototype)
       return prototype->clone();
-   return 0;
+
+   throw Algorithm_Not_Found(request.as_string());
    }
 
 }
diff --git a/src/libstate/lookup.cpp b/src/libstate/lookup.cpp
index d8fab625b..52e986ff2 100644
--- a/src/libstate/lookup.cpp
+++ b/src/libstate/lookup.cpp
@@ -23,13 +23,7 @@ const HashFunction* retrieve_hash(Library_State& libstate,
 *************************************************/
 HashFunction* get_hash(const std::string& algo_spec)
    {
-   const HashFunction* hash =
-      global_state().algo_factory().prototype_hash_function(algo_spec);
-
-   if(hash)
-      return hash->clone();
-
-   throw Algorithm_Not_Found(algo_spec);
+   return global_state().algo_factory().make_hash_function(algo_spec);
    }
 
 /*************************************************
-- 
cgit v1.2.3