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 : public NameValuePairs
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         AlgorithmParametersBase(const char *name, bool throwIfNotUsed)
00258                 : m_name(name), m_throwIfNotUsed(throwIfNotUsed), m_used(false) {}
00259 
00260         ~AlgorithmParametersBase()
00261         {
00262 #ifdef CRYPTOPP_UNCAUGHT_EXCEPTION_AVAILABLE
00263                 if (!std::uncaught_exception())
00264 #else
00265                 try
00266 #endif
00267                 {
00268                         if (m_throwIfNotUsed && !m_used)
00269                                 throw ParameterNotUsed(m_name);
00270                 }
00271 #ifndef CRYPTOPP_UNCAUGHT_EXCEPTION_AVAILABLE
00272                 catch(...)
00273                 {
00274                 }
00275 #endif
00276         }
00277 
00278         bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const;
00279 
00280 protected:
00281         virtual void AssignValue(const char *name, const std::type_info &valueType, void *pValue) const =0;
00282         virtual const NameValuePairs & GetParent() const =0;
00283 
00284         const char *m_name;
00285         bool m_throwIfNotUsed;
00286         mutable bool m_used;
00287 };
00288 
00289 template <class T>
00290 class AlgorithmParametersBase2 : public AlgorithmParametersBase
00291 {
00292 public:
00293         AlgorithmParametersBase2(const char *name, const T &value, bool throwIfNotUsed) : AlgorithmParametersBase(name, throwIfNotUsed), m_value(value) {}
00294 
00295         void AssignValue(const char *name, const std::type_info &valueType, void *pValue) const
00296         {
00297                 // special case for retrieving an Integer parameter when an int was passed in
00298                 if (!(g_pAssignIntToInteger != NULL && typeid(T) == typeid(int) && g_pAssignIntToInteger(valueType, pValue, &m_value)))
00299                 {
00300                         ThrowIfTypeMismatch(name, typeid(T), valueType);
00301                         *reinterpret_cast<T *>(pValue) = m_value;
00302                 }
00303         }
00304 
00305 protected:
00306         T m_value;
00307 };
00308 
00309 template <class PARENT, class T>
00310 class AlgorithmParameters : public AlgorithmParametersBase2<T>
00311 {
00312 public:
00313         AlgorithmParameters(const PARENT &parent, const char *name, const T &value, bool throwIfNotUsed)
00314                 : AlgorithmParametersBase2<T>(name, value, throwIfNotUsed), m_parent(parent)
00315         {}
00316 
00317         AlgorithmParameters(const AlgorithmParameters &copy)
00318                 : AlgorithmParametersBase2<T>(copy), m_parent(copy.m_parent)
00319         {
00320                 copy.m_used = true;
00321         }
00322 
00323         template <class R>
00324         AlgorithmParameters<AlgorithmParameters<PARENT,T>, R> operator()(const char *name, const R &value) const
00325         {
00326                 return AlgorithmParameters<AlgorithmParameters<PARENT,T>, R>(*this, name, value, this->m_throwIfNotUsed);
00327         }
00328 
00329         template <class R>
00330         AlgorithmParameters<AlgorithmParameters<PARENT,T>, R> operator()(const char *name, const R &value, bool throwIfNotUsed) const
00331         {
00332                 return AlgorithmParameters<AlgorithmParameters<PARENT,T>, R>(*this, name, value, throwIfNotUsed);
00333         }
00334 
00335 private:
00336         const NameValuePairs & GetParent() const {return m_parent;}
00337         PARENT m_parent;
00338 };
00339 
00340 //! Create an object that implements NameValuePairs for passing parameters
00341 /*! \param throwIfNotUsed if true, the object will throw an exception if the value is not accessed
00342         \note throwIfNotUsed is ignored if using a compiler that does not support std::uncaught_exception(),
00343         such as MSVC 7.0 and earlier.
00344         \note A NameValuePairs object containing an arbitrary number of name value pairs may be constructed by
00345         repeatedly using operator() on the object returned by MakeParameters, for example:
00346         const NameValuePairs &parameters = MakeParameters(name1, value1)(name2, value2)(name3, value3);
00347 */
00348 template <class T>
00349 AlgorithmParameters<NullNameValuePairs,T> MakeParameters(const char *name, const T &value, bool throwIfNotUsed = true)
00350 {
00351         return AlgorithmParameters<NullNameValuePairs,T>(g_nullNameValuePairs, name, value, throwIfNotUsed);
00352 }
00353 
00354 #define CRYPTOPP_GET_FUNCTION_ENTRY(name)               (Name::name(), &ThisClass::Get##name)
00355 #define CRYPTOPP_SET_FUNCTION_ENTRY(name)               (Name::name(), &ThisClass::Set##name)
00356 #define CRYPTOPP_SET_FUNCTION_ENTRY2(name1, name2)      (Name::name1(), Name::name2(), &ThisClass::Set##name1##And##name2)
00357 
00358 NAMESPACE_END
00359 
00360 #endif

Generated on Fri Jun 1 11:11:19 2007 for Crypto++ by  doxygen 1.5.2