From caf9e032e6b4ccb114a74a3936c916bcfaba262d Mon Sep 17 00:00:00 2001 From: weidai Date: Mon, 2 Mar 2009 02:39:17 +0000 Subject: changes for 5.6: - added AuthenticatedSymmetricCipher interface class and Filter wrappers - added CCM, GCM (with SSE2 assembly), CMAC, and SEED - improved AES speed on x86 and x64 - removed WORD64_AVAILABLE; compiler 64-bit int support is now required git-svn-id: svn://svn.code.sf.net/p/cryptopp/code/trunk/c5@433 57ff6487-cd31-0410-9ec3-f628ee90f5f0 --- algparam.h | 75 ++++++++++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 56 insertions(+), 19 deletions(-) (limited to 'algparam.h') diff --git a/algparam.h b/algparam.h index 36cc16f..80bbbc6 100644 --- a/algparam.h +++ b/algparam.h @@ -245,7 +245,7 @@ CRYPTOPP_DLL extern PAssignIntToInteger g_pAssignIntToInteger; CRYPTOPP_DLL const std::type_info & CRYPTOPP_API IntegerTypeId(); -class CRYPTOPP_DLL AlgorithmParametersBase : public NameValuePairs +class CRYPTOPP_DLL AlgorithmParametersBase { public: class ParameterNotUsed : public Exception @@ -254,10 +254,18 @@ public: ParameterNotUsed(const char *name) : Exception(OTHER_ERROR, std::string("AlgorithmParametersBase: parameter \"") + name + "\" not used") {} }; + // this is actually a move, not a copy + AlgorithmParametersBase(const AlgorithmParametersBase &x) + : m_name(x.m_name), m_throwIfNotUsed(x.m_throwIfNotUsed), m_used(x.m_used) + { + m_next.reset(const_cast(x).m_next.release()); + x.m_used = true; + } + AlgorithmParametersBase(const char *name, bool throwIfNotUsed) : m_name(name), m_throwIfNotUsed(throwIfNotUsed), m_used(false) {} - ~AlgorithmParametersBase() + virtual ~AlgorithmParametersBase() { #ifdef CRYPTOPP_UNCAUGHT_EXCEPTION_AVAILABLE if (!std::uncaught_exception()) @@ -281,62 +289,91 @@ protected: friend class AlgorithmParameters; virtual void AssignValue(const char *name, const std::type_info &valueType, void *pValue) const =0; + virtual void MoveInto(void *p) const =0; // not really const const char *m_name; bool m_throwIfNotUsed; mutable bool m_used; - member_ptr m_next; + member_ptr m_next; }; template class AlgorithmParametersTemplate : public AlgorithmParametersBase { public: - AlgorithmParametersTemplate(const char *name, const T &value, bool throwIfNotUsed) : AlgorithmParametersBase(name, throwIfNotUsed), m_value(value) {} + AlgorithmParametersTemplate(const char *name, const T &value, bool throwIfNotUsed) + : AlgorithmParametersBase(name, throwIfNotUsed), m_value(value) + { + } void AssignValue(const char *name, const std::type_info &valueType, void *pValue) const { // special case for retrieving an Integer parameter when an int was passed in if (!(g_pAssignIntToInteger != NULL && typeid(T) == typeid(int) && g_pAssignIntToInteger(valueType, pValue, &m_value))) { - ThrowIfTypeMismatch(name, typeid(T), valueType); + NameValuePairs::ThrowIfTypeMismatch(name, typeid(T), valueType); *reinterpret_cast(pValue) = m_value; } } + void MoveInto(void *buffer) const + { + AlgorithmParametersTemplate* p = new(buffer) AlgorithmParametersTemplate(*this); + } + protected: T m_value; }; -class AlgorithmParameters : public NameValuePairs +CRYPTOPP_DLL_TEMPLATE_CLASS AlgorithmParametersTemplate; +CRYPTOPP_DLL_TEMPLATE_CLASS AlgorithmParametersTemplate; +CRYPTOPP_DLL_TEMPLATE_CLASS AlgorithmParametersTemplate; + +class CRYPTOPP_DLL AlgorithmParameters : public NameValuePairs { public: - AlgorithmParameters(AlgorithmParameters &x) : m_ptr(x.m_ptr.release()) {} - - AlgorithmParameters(AlgorithmParametersBase *p) : m_ptr(p) {} - + AlgorithmParameters(); + + AlgorithmParameters(const AlgorithmParameters &x); + + ~AlgorithmParameters(); + template AlgorithmParameters & operator()(const char *name, const T &value, bool throwIfNotUsed) { - member_ptr p(new AlgorithmParametersTemplate(name, value, throwIfNotUsed)); - p->m_next.reset(m_ptr.release()); - m_ptr.reset(p.release()); + if (m_constructed || sizeof(m_first) < sizeof(AlgorithmParametersTemplate)) + { + member_ptr p(new AlgorithmParametersTemplate(name, value, throwIfNotUsed)); + p->m_next.reset(Next().release()); + Next().reset(p.release()); + } + else + { + member_ptr temp(Next().release()); + AlgorithmParametersTemplate* p = new(m_first) AlgorithmParametersTemplate(name, value, throwIfNotUsed); + p->m_next.reset(temp.release()); + m_constructed = true; + } + m_defaultThrowIfNotUsed = throwIfNotUsed; return *this; } template AlgorithmParameters & operator()(const char *name, const T &value) { - member_ptr p(new AlgorithmParametersTemplate(name, value, m_ptr->m_throwIfNotUsed)); - p->m_next.reset(m_ptr.release()); - m_ptr.reset(p.release()); - return *this; + return operator()(name, value, m_defaultThrowIfNotUsed); } bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const; protected: - member_ptr m_ptr; + AlgorithmParametersBase & First(); + member_ptr & Next(); + const AlgorithmParametersBase & First() const {return const_cast(this)->First();} + member_ptr & Next() const {return const_cast(this)->Next();} + + bool m_constructed, m_defaultThrowIfNotUsed; + char m_first[sizeof(AlgorithmParametersBase) + 16]; }; //! Create an object that implements NameValuePairs for passing parameters @@ -350,7 +387,7 @@ protected: template AlgorithmParameters MakeParameters(const char *name, const T &value, bool throwIfNotUsed = true) { - return AlgorithmParameters(new AlgorithmParametersTemplate(name, value, throwIfNotUsed)); + return AlgorithmParameters()(name, value, throwIfNotUsed); } #define CRYPTOPP_GET_FUNCTION_ENTRY(name) (Name::name(), &ThisClass::Get##name) -- cgit v1.2.1