• Main Page
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

algparam.h

00001 #ifndef CRYPTOPP_ALGPARAM_H
00002 #define CRYPTOPP_ALGPARAM_H
00003 
00004 #include "cryptlib.h"
00005 #include "smartptr.h"
00006 #include "secblock.h"
00007 
00008 NAMESPACE_BEGIN(CryptoPP)
00009 
00010 //! used to pass byte array input as part of a NameValuePairs object
00011 /*! the deepCopy option is used when the NameValuePairs object can't
00012     keep a copy of the data available */
00013 class ConstByteArrayParameter
00014 {
00015 public:
00016     ConstByteArrayParameter(const char *data = NULL, bool deepCopy = false)
00017     {
00018         Assign((const byte *)data, data ? strlen(data) : 0, deepCopy);
00019     }
00020     ConstByteArrayParameter(const byte *data, size_t size, bool deepCopy = false)
00021     {
00022         Assign(data, size, deepCopy);
00023     }
00024     template <class T> ConstByteArrayParameter(const T &string, bool deepCopy = false)
00025     {
00026         CRYPTOPP_COMPILE_ASSERT(sizeof(CPP_TYPENAME T::value_type) == 1);
00027         Assign((const byte *)string.data(), string.size(), deepCopy);
00028     }
00029 
00030     void Assign(const byte *data, size_t size, bool deepCopy)
00031     {
00032         if (deepCopy)
00033             m_block.Assign(data, size);
00034         else
00035         {
00036             m_data = data;
00037             m_size = size;
00038         }
00039         m_deepCopy = deepCopy;
00040     }
00041 
00042     const byte *begin() const {return m_deepCopy ? m_block.begin() : m_data;}
00043     const byte *end() const {return m_deepCopy ? m_block.end() : m_data + m_size;}
00044     size_t size() const {return m_deepCopy ? m_block.size() : m_size;}
00045 
00046 private:
00047     bool m_deepCopy;
00048     const byte *m_data;
00049     size_t m_size;
00050     SecByteBlock m_block;
00051 };
00052 
00053 class ByteArrayParameter
00054 {
00055 public:
00056     ByteArrayParameter(byte *data = NULL, unsigned int size = 0)
00057         : m_data(data), m_size(size) {}
00058     ByteArrayParameter(SecByteBlock &block)
00059         : m_data(block.begin()), m_size(block.size()) {}
00060 
00061     byte *begin() const {return m_data;}
00062     byte *end() const {return m_data + m_size;}
00063     size_t size() const {return m_size;}
00064 
00065 private:
00066     byte *m_data;
00067     size_t m_size;
00068 };
00069 
00070 class CRYPTOPP_DLL CombinedNameValuePairs : public NameValuePairs
00071 {
00072 public:
00073     CombinedNameValuePairs(const NameValuePairs &pairs1, const NameValuePairs &pairs2)
00074         : m_pairs1(pairs1), m_pairs2(pairs2) {}
00075 
00076     bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const;
00077 
00078 private:
00079     const NameValuePairs &m_pairs1, &m_pairs2;
00080 };
00081 
00082 template <class T, class BASE>
00083 class GetValueHelperClass
00084 {
00085 public:
00086     GetValueHelperClass(const T *pObject, const char *name, const std::type_info &valueType, void *pValue, const NameValuePairs *searchFirst)
00087         : m_pObject(pObject), m_name(name), m_valueType(&valueType), m_pValue(pValue), m_found(false), m_getValueNames(false)
00088     {
00089         if (strcmp(m_name, "ValueNames") == 0)
00090         {
00091             m_found = m_getValueNames = true;
00092             NameValuePairs::ThrowIfTypeMismatch(m_name, typeid(std::string), *m_valueType);
00093             if (searchFirst)
00094                 searchFirst->GetVoidValue(m_name, valueType, pValue);
00095             if (typeid(T) != typeid(BASE))
00096                 pObject->BASE::GetVoidValue(m_name, valueType, pValue);
00097             ((*reinterpret_cast<std::string *>(m_pValue) += "ThisPointer:") += typeid(T).name()) += ';';
00098         }
00099 
00100         if (!m_found && strncmp(m_name, "ThisPointer:", 12) == 0 && strcmp(m_name+12, typeid(T).name()) == 0)
00101         {
00102             NameValuePairs::ThrowIfTypeMismatch(m_name, typeid(T *), *m_valueType);
00103             *reinterpret_cast<const T **>(pValue) = pObject;
00104             m_found = true;
00105             return;
00106         }
00107 
00108         if (!m_found && searchFirst)
00109             m_found = searchFirst->GetVoidValue(m_name, valueType, pValue);
00110         
00111         if (!m_found && typeid(T) != typeid(BASE))
00112             m_found = pObject->BASE::GetVoidValue(m_name, valueType, pValue);
00113     }
00114 
00115     operator bool() const {return m_found;}
00116 
00117     template <class R>
00118     GetValueHelperClass<T,BASE> & operator()(const char *name, const R & (T::*pm)() const)
00119     {
00120         if (m_getValueNames)
00121             (*reinterpret_cast<std::string *>(m_pValue) += name) += ";";
00122         if (!m_found && strcmp(name, m_name) == 0)
00123         {
00124             NameValuePairs::ThrowIfTypeMismatch(name, typeid(R), *m_valueType);
00125             *reinterpret_cast<R *>(m_pValue) = (m_pObject->*pm)();
00126             m_found = true;
00127         }
00128         return *this;
00129     }
00130 
00131     GetValueHelperClass<T,BASE> &Assignable()
00132     {
00133 #ifndef __INTEL_COMPILER    // ICL 9.1 workaround: Intel compiler copies the vTable pointer for some reason
00134         if (m_getValueNames)
00135             ((*reinterpret_cast<std::string *>(m_pValue) += "ThisObject:") += typeid(T).name()) += ';';
00136         if (!m_found && strncmp(m_name, "ThisObject:", 11) == 0 && strcmp(m_name+11, typeid(T).name()) == 0)
00137         {
00138             NameValuePairs::ThrowIfTypeMismatch(m_name, typeid(T), *m_valueType);
00139             *reinterpret_cast<T *>(m_pValue) = *m_pObject;
00140             m_found = true;
00141         }
00142 #endif
00143         return *this;
00144     }
00145 
00146 private:
00147     const T *m_pObject;
00148     const char *m_name;
00149     const std::type_info *m_valueType;
00150     void *m_pValue;
00151     bool m_found, m_getValueNames;
00152 };
00153 
00154 template <class BASE, class T>
00155 GetValueHelperClass<T, BASE> GetValueHelper(const T *pObject, const char *name, const std::type_info &valueType, void *pValue, const NameValuePairs *searchFirst=NULL, BASE *dummy=NULL)
00156 {
00157     return GetValueHelperClass<T, BASE>(pObject, name, valueType, pValue, searchFirst);
00158 }
00159 
00160 template <class T>
00161 GetValueHelperClass<T, T> GetValueHelper(const T *pObject, const char *name, const std::type_info &valueType, void *pValue, const NameValuePairs *searchFirst=NULL)
00162 {
00163     return GetValueHelperClass<T, T>(pObject, name, valueType, pValue, searchFirst);
00164 }
00165 
00166 // ********************************************************
00167 
00168 template <class R>
00169 R Hack_DefaultValueFromConstReferenceType(const R &)
00170 {
00171     return R();
00172 }
00173 
00174 template <class R>
00175 bool Hack_GetValueIntoConstReference(const NameValuePairs &source, const char *name, const R &value)
00176 {
00177     return source.GetValue(name, const_cast<R &>(value));
00178 }
00179 
00180 template <class T, class BASE>
00181 class AssignFromHelperClass
00182 {
00183 public:
00184     AssignFromHelperClass(T *pObject, const NameValuePairs &source)
00185         : m_pObject(pObject), m_source(source), m_done(false)
00186     {
00187         if (source.GetThisObject(*pObject))
00188             m_done = true;
00189         else if (typeid(BASE) != typeid(T))
00190             pObject->BASE::AssignFrom(source);
00191     }
00192 
00193     template <class R>
00194     AssignFromHelperClass & operator()(const char *name, void (T::*pm)(R))  // VC60 workaround: "const R &" here causes compiler error
00195     {
00196         if (!m_done)
00197         {
00198             R value = Hack_DefaultValueFromConstReferenceType(reinterpret_cast<R>(*(int *)NULL));
00199             if (!Hack_GetValueIntoConstReference(m_source, name, value))
00200                 throw InvalidArgument(std::string(typeid(T).name()) + ": Missing required parameter '" + name + "'");
00201             (m_pObject->*pm)(value);
00202         }
00203         return *this;
00204     }
00205 
00206     template <class R, class S>
00207     AssignFromHelperClass & operator()(const char *name1, const char *name2, void (T::*pm)(R, S))   // VC60 workaround: "const R &" here causes compiler error
00208     {
00209         if (!m_done)
00210         {
00211             R value1 = Hack_DefaultValueFromConstReferenceType(reinterpret_cast<R>(*(int *)NULL));
00212             if (!Hack_GetValueIntoConstReference(m_source, name1, value1))
00213                 throw InvalidArgument(std::string(typeid(T).name()) + ": Missing required parameter '" + name1 + "'");
00214             S value2 = Hack_DefaultValueFromConstReferenceType(reinterpret_cast<S>(*(int *)NULL));
00215             if (!Hack_GetValueIntoConstReference(m_source, name2, value2))
00216                 throw InvalidArgument(std::string(typeid(T).name()) + ": Missing required parameter '" + name2 + "'");
00217             (m_pObject->*pm)(value1, value2);
00218         }
00219         return *this;
00220     }
00221 
00222 private:
00223     T *m_pObject;
00224     const NameValuePairs &m_source;
00225     bool m_done;
00226 };
00227 
00228 template <class BASE, class T>
00229 AssignFromHelperClass<T, BASE> AssignFromHelper(T *pObject, const NameValuePairs &source, BASE *dummy=NULL)
00230 {
00231     return AssignFromHelperClass<T, BASE>(pObject, source);
00232 }
00233 
00234 template <class T>
00235 AssignFromHelperClass<T, T> AssignFromHelper(T *pObject, const NameValuePairs &source)
00236 {
00237     return AssignFromHelperClass<T, T>(pObject, source);
00238 }
00239 
00240 // ********************************************************
00241 
00242 // to allow the linker to discard Integer code if not needed.
00243 typedef bool (CRYPTOPP_API * PAssignIntToInteger)(const std::type_info &valueType, void *pInteger, const void *pInt);
00244 CRYPTOPP_DLL extern PAssignIntToInteger g_pAssignIntToInteger;
00245 
00246 CRYPTOPP_DLL const std::type_info & CRYPTOPP_API IntegerTypeId();
00247 
00248 class CRYPTOPP_DLL AlgorithmParametersBase
00249 {
00250 public:
00251     class ParameterNotUsed : public Exception
00252     {
00253     public: 
00254         ParameterNotUsed(const char *name) : Exception(OTHER_ERROR, std::string("AlgorithmParametersBase: parameter \"") + name + "\" not used") {}
00255     };
00256 
00257     // this is actually a move, not a copy
00258     AlgorithmParametersBase(const AlgorithmParametersBase &x)
00259         : m_name(x.m_name), m_throwIfNotUsed(x.m_throwIfNotUsed), m_used(x.m_used)
00260     {
00261         m_next.reset(const_cast<AlgorithmParametersBase &>(x).m_next.release());
00262         x.m_used = true;
00263     }
00264 
00265     AlgorithmParametersBase(const char *name, bool throwIfNotUsed)
00266         : m_name(name), m_throwIfNotUsed(throwIfNotUsed), m_used(false) {}
00267 
00268     virtual ~AlgorithmParametersBase()
00269     {
00270 #ifdef CRYPTOPP_UNCAUGHT_EXCEPTION_AVAILABLE
00271         if (!std::uncaught_exception())
00272 #else
00273         try
00274 #endif
00275         {
00276             if (m_throwIfNotUsed && !m_used)
00277                 throw ParameterNotUsed(m_name);
00278         }
00279 #ifndef CRYPTOPP_UNCAUGHT_EXCEPTION_AVAILABLE
00280         catch(...)
00281         {
00282         }
00283 #endif
00284     }
00285 
00286     bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const;
00287     
00288 protected:
00289     friend class AlgorithmParameters;
00290     void operator=(const AlgorithmParametersBase& rhs); // assignment not allowed, declare this for VC60
00291 
00292     virtual void AssignValue(const char *name, const std::type_info &valueType, void *pValue) const =0;
00293     virtual void MoveInto(void *p) const =0;    // not really const
00294 
00295     const char *m_name;
00296     bool m_throwIfNotUsed;
00297     mutable bool m_used;
00298     member_ptr<AlgorithmParametersBase> m_next;
00299 };
00300 
00301 template <class T>
00302 class AlgorithmParametersTemplate : public AlgorithmParametersBase
00303 {
00304 public:
00305     AlgorithmParametersTemplate(const char *name, const T &value, bool throwIfNotUsed)
00306         : AlgorithmParametersBase(name, throwIfNotUsed), m_value(value)
00307     {
00308     }
00309 
00310     void AssignValue(const char *name, const std::type_info &valueType, void *pValue) const
00311     {
00312         // special case for retrieving an Integer parameter when an int was passed in
00313         if (!(g_pAssignIntToInteger != NULL && typeid(T) == typeid(int) && g_pAssignIntToInteger(valueType, pValue, &m_value)))
00314         {
00315             NameValuePairs::ThrowIfTypeMismatch(name, typeid(T), valueType);
00316             *reinterpret_cast<T *>(pValue) = m_value;
00317         }
00318     }
00319 
00320     void MoveInto(void *buffer) const
00321     {
00322         AlgorithmParametersTemplate<T>* p = new(buffer) AlgorithmParametersTemplate<T>(*this);
00323     }
00324 
00325 protected:
00326     T m_value;
00327 };
00328 
00329 CRYPTOPP_DLL_TEMPLATE_CLASS AlgorithmParametersTemplate<bool>;
00330 CRYPTOPP_DLL_TEMPLATE_CLASS AlgorithmParametersTemplate<int>;
00331 CRYPTOPP_DLL_TEMPLATE_CLASS AlgorithmParametersTemplate<ConstByteArrayParameter>;
00332 
00333 class CRYPTOPP_DLL AlgorithmParameters : public NameValuePairs
00334 {
00335 public:
00336     AlgorithmParameters();
00337 
00338 #ifdef __BORLANDC__
00339     template <class T>
00340     AlgorithmParameters(const char *name, const T &value, bool throwIfNotUsed=true)
00341         : m_next(new AlgorithmParametersTemplate<T>(name, value, throwIfNotUsed))
00342         , m_defaultThrowIfNotUsed(throwIfNotUsed)
00343     {
00344     }
00345 #endif
00346 
00347     AlgorithmParameters(const AlgorithmParameters &x);
00348 
00349     AlgorithmParameters & operator=(const AlgorithmParameters &x);
00350 
00351     template <class T>
00352     AlgorithmParameters & operator()(const char *name, const T &value, bool throwIfNotUsed)
00353     {
00354         member_ptr<AlgorithmParametersBase> p(new AlgorithmParametersTemplate<T>(name, value, throwIfNotUsed));
00355         p->m_next.reset(m_next.release());
00356         m_next.reset(p.release());
00357         m_defaultThrowIfNotUsed = throwIfNotUsed;
00358         return *this;
00359     }
00360 
00361     template <class T>
00362     AlgorithmParameters & operator()(const char *name, const T &value)
00363     {
00364         return operator()(name, value, m_defaultThrowIfNotUsed);
00365     }
00366 
00367     bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const;
00368 
00369 protected:
00370     member_ptr<AlgorithmParametersBase> m_next;
00371     bool m_defaultThrowIfNotUsed;
00372 };
00373 
00374 //! Create an object that implements NameValuePairs for passing parameters
00375 /*! \param throwIfNotUsed if true, the object will throw an exception if the value is not accessed
00376     \note throwIfNotUsed is ignored if using a compiler that does not support std::uncaught_exception(),
00377     such as MSVC 7.0 and earlier.
00378     \note A NameValuePairs object containing an arbitrary number of name value pairs may be constructed by
00379     repeatedly using operator() on the object returned by MakeParameters, for example:
00380     AlgorithmParameters parameters = MakeParameters(name1, value1)(name2, value2)(name3, value3);
00381 */
00382 #ifdef __BORLANDC__
00383 typedef AlgorithmParameters MakeParameters;
00384 #else
00385 template <class T>
00386 AlgorithmParameters MakeParameters(const char *name, const T &value, bool throwIfNotUsed = true)
00387 {
00388     return AlgorithmParameters()(name, value, throwIfNotUsed);
00389 }
00390 #endif
00391 
00392 #define CRYPTOPP_GET_FUNCTION_ENTRY(name)       (Name::name(), &ThisClass::Get##name)
00393 #define CRYPTOPP_SET_FUNCTION_ENTRY(name)       (Name::name(), &ThisClass::Set##name)
00394 #define CRYPTOPP_SET_FUNCTION_ENTRY2(name1, name2)  (Name::name1(), Name::name2(), &ThisClass::Set##name1##And##name2)
00395 
00396 NAMESPACE_END
00397 
00398 #endif

Generated on Tue Jun 30 2015 19:07:04 for Crypto++ by  doxygen 1.7.1