aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/utils
diff options
context:
space:
mode:
authorlloyd <[email protected]>2015-01-28 04:32:10 +0000
committerlloyd <[email protected]>2015-01-28 04:32:10 +0000
commit7b56f1bd570dc684ffd7c945dee0d9b5480354ff (patch)
tree0c50ad534280a292a1b76daee9a19b34cfd96367 /src/lib/utils
parentb8fa304ec981d273c45d7ef31705d65ccfb00cc1 (diff)
Add a runtime map of string->func() which when called return
Transforms and BlockCiphers. Registration for all types is done at startup but is very cheap as just a std::function and a std::map entry are created, no actual objects are created until needed. This is a huge improvement over Algorithm_Factory which used T::clone() as the function and thus kept a prototype object of each type in memory. Replace existing lookup mechanisms for ciphers, AEADs, and compression to use the transform lookup. The existing Engine framework remains in place for BlockCipher, but the engines now just call to the registry instead of having hardcoded lookups. s/Transformation/Transform/ with typedefs for compatability. Remove lib/selftest code (for runtime selftesting): not the right approach.
Diffstat (limited to 'src/lib/utils')
-rw-r--r--src/lib/utils/cpuid.cpp26
-rw-r--r--src/lib/utils/cpuid.h15
-rw-r--r--src/lib/utils/parsing.cpp10
3 files changed, 34 insertions, 17 deletions
diff --git a/src/lib/utils/cpuid.cpp b/src/lib/utils/cpuid.cpp
index 7b7b27f7c..9dd2297e4 100644
--- a/src/lib/utils/cpuid.cpp
+++ b/src/lib/utils/cpuid.cpp
@@ -73,9 +73,10 @@
namespace Botan {
-u64bit CPUID::m_x86_processor_flags[2] = { 0, 0 };
-size_t CPUID::m_cache_line_size = 0;
-bool CPUID::m_altivec_capable = false;
+u64bit CPUID::g_x86_processor_flags[2] = { 0, 0 };
+size_t CPUID::g_cache_line_size = 0;
+bool CPUID::g_altivec_capable = false;
+bool CPUID::g_initialized = false;
namespace {
@@ -183,9 +184,12 @@ void CPUID::print(std::ostream& o)
void CPUID::initialize()
{
+ if(g_initialized)
+ return;
+
#if defined(BOTAN_TARGET_CPU_IS_PPC_FAMILY)
if(altivec_check_sysctl() || altivec_check_pvr_emul())
- m_altivec_capable = true;
+ g_altivec_capable = true;
#endif
#if defined(BOTAN_TARGET_CPU_IS_X86_FAMILY)
@@ -205,22 +209,22 @@ void CPUID::initialize()
X86_CPUID(1, cpuid);
- m_x86_processor_flags[0] = (static_cast<u64bit>(cpuid[2]) << 32) | cpuid[3];
+ g_x86_processor_flags[0] = (static_cast<u64bit>(cpuid[2]) << 32) | cpuid[3];
if(is_intel)
- m_cache_line_size = 8 * get_byte(2, cpuid[1]);
+ g_cache_line_size = 8 * get_byte(2, cpuid[1]);
if(max_supported_sublevel >= 7)
{
clear_mem(cpuid, 4);
X86_CPUID_SUBLEVEL(7, 0, cpuid);
- m_x86_processor_flags[1] = (static_cast<u64bit>(cpuid[2]) << 32) | cpuid[1];
+ g_x86_processor_flags[1] = (static_cast<u64bit>(cpuid[2]) << 32) | cpuid[1];
}
if(is_amd)
{
X86_CPUID(0x80000005, cpuid);
- m_cache_line_size = get_byte(3, cpuid[2]);
+ g_cache_line_size = get_byte(3, cpuid[2]);
}
#endif
@@ -230,9 +234,11 @@ void CPUID::initialize()
* If we don't have access to CPUID, we can still safely assume that
* any x86-64 processor has SSE2 and RDTSC
*/
- if(m_x86_processor_flags[0] == 0)
- m_x86_processor_flags[0] = (1 << CPUID_SSE2_BIT) | (1 << CPUID_RDTSC_BIT);
+ if(g_x86_processor_flags[0] == 0)
+ g_x86_processor_flags[0] = (1 << CPUID_SSE2_BIT) | (1 << CPUID_RDTSC_BIT);
#endif
+
+ g_initialized = true;
}
}
diff --git a/src/lib/utils/cpuid.h b/src/lib/utils/cpuid.h
index cbbdcad6d..14901199c 100644
--- a/src/lib/utils/cpuid.h
+++ b/src/lib/utils/cpuid.h
@@ -27,7 +27,7 @@ class BOTAN_DLL CPUID
/**
* Return a best guess of the cache line size
*/
- static size_t cache_line_size() { return m_cache_line_size; }
+ static size_t cache_line_size() { return g_cache_line_size; }
/**
* Check if the processor supports RDTSC
@@ -116,7 +116,7 @@ class BOTAN_DLL CPUID
/**
* Check if the processor supports AltiVec/VMX
*/
- static bool has_altivec() { return m_altivec_capable; }
+ static bool has_altivec() { return g_altivec_capable; }
static void print(std::ostream& o);
private:
@@ -140,12 +140,15 @@ class BOTAN_DLL CPUID
static bool x86_processor_flags_has(u64bit bit)
{
- return ((m_x86_processor_flags[bit/64] >> (bit % 64)) & 1);
+ if(!g_initialized)
+ initialize();
+ return ((g_x86_processor_flags[bit/64] >> (bit % 64)) & 1);
}
- static u64bit m_x86_processor_flags[2];
- static size_t m_cache_line_size;
- static bool m_altivec_capable;
+ static bool g_initialized;
+ static u64bit g_x86_processor_flags[2];
+ static size_t g_cache_line_size;
+ static bool g_altivec_capable;
};
}
diff --git a/src/lib/utils/parsing.cpp b/src/lib/utils/parsing.cpp
index ff8d5f0fb..36b168290 100644
--- a/src/lib/utils/parsing.cpp
+++ b/src/lib/utils/parsing.cpp
@@ -10,12 +10,20 @@
#include <botan/charset.h>
#include <botan/get_byte.h>
#include <set>
+#include <stdexcept>
namespace Botan {
u32bit to_u32bit(const std::string& str)
{
- return std::stoul(str, nullptr);
+ try
+ {
+ return std::stoul(str, nullptr);
+ }
+ catch(std::exception&)
+ {
+ throw std::runtime_error("Could not read '" + str + "' as decimal string");
+ }
}
/*