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                 if (m_getValueNames)
00134                         ((*reinterpret_cast<std::string *>(m_pValue) += "ThisObject:") += typeid(T).name()) += ';';
00135                 if (!m_found && strncmp(m_name, "ThisObject:", 11) == 0 && strcmp(m_name+11, typeid(T).name()) == 0)
00136                 {
00137                         NameValuePairs::ThrowIfTypeMismatch(m_name, typeid(T), *m_valueType);
00138                         *reinterpret_cast<T *>(m_pValue) = *m_pObject;
00139                         m_found = true;
00140                 }
00141                 return *this;
00142         }
00143 
00144 private:
00145         const T *m_pObject;
00146         const char *m_name;
00147         const std::type_info *m_valueType;
00148         void *m_pValue;
00149         bool m_found, m_getValueNames;
00150 };
00151 
00152 template <class BASE, class T>
00153 GetValueHelperClass<T, BASE> GetValueHelper(const T *pObject, const char *name, const std::type_info &valueType, void *pValue, const NameValuePairs *searchFirst=NULL, BASE *dummy=NULL)
00154 {
00155         return GetValueHelperClass<T, BASE>(pObject, name, valueType, pValue, searchFirst);
00156 }
00157 
00158 template <class T>
00159 GetValueHelperClass<T, T> GetValueHelper(const T *pObject, const char *name, const std::type_info &valueType, void *pValue, const NameValuePairs *searchFirst=NULL)
00160 {
00161         return GetValueHelperClass<T, T>(pObject, name, valueType, pValue, searchFirst);
00162 }
00163 
00164 // ********************************************************
00165 
00166 template <class R>
00167 R Hack_DefaultValueFromConstReferenceType(const R &)
00168 {
00169         return R();
00170 }
00171 
00172 template <class R>
00173 bool Hack_GetValueIntoConstReference(const NameValuePairs &source, const char *name, const R &value)
00174 {
00175         return source.GetValue(name, const_cast<R &>(value));
00176 }
00177 
00178 template <class T, class BASE>
00179 class AssignFromHelperClass
00180 {
00181 public:
00182         AssignFromHelperClass(T *pObject, const NameValuePairs &source)
00183                 : m_pObject(pObject), m_source(source), m_done(false)
00184         {
00185                 if (source.GetThisObject(*pObject))
00186                         m_done = true;
00187                 else if (typeid(BASE) != typeid(T))
00188                         pObject->BASE::AssignFrom(source);
00189         }
00190 
00191         template <class R>
00192         AssignFromHelperClass & operator()(const char *name, void (T::*pm)(R))  // VC60 workaround: "const R &" here causes compiler error
00193         {
00194                 if (!m_done)
00195                 {
00196                         R value = Hack_DefaultValueFromConstReferenceType(reinterpret_cast<R>(*(int *)NULL));
00197                         if (!Hack_GetValueIntoConstReference(m_source, name, value))
00198                                 throw InvalidArgument(std::string(typeid(T).name()) + ": Missing required parameter '" + name + "'");
00199                         (m_pObject->*pm)(value);
00200                 }
00201                 return *this;
00202         }
00203 
00204         template <class R, class S>
00205         AssignFromHelperClass & operator()(const char *name1, const char *name2, void (T::*pm)(R, S))   // VC60 workaround: "const R &" here causes compiler error
00206         {
00207                 if (!m_done)
00208                 {
00209                         R value1 = Hack_DefaultValueFromConstReferenceType(reinterpret_cast<R>(*(int *)NULL));
00210                         if (!Hack_GetValueIntoConstReference(m_source, name1, value1))
00211                                 throw InvalidArgument(std::string(typeid(T).name()) + ": Missing required parameter '" + name1 + "'");
00212                         S value2 = Hack_DefaultValueFromConstReferenceType(reinterpret_cast<S>(*(int *)NULL));
00213                         if (!Hack_GetValueIntoConstReference(m_source, name2, value2))
00214                                 throw InvalidArgument(std::string(typeid(T).name()) + ": Missing required parameter '" + name2 + "'");
00215                         (m_pObject->*pm)(value1, value2);
00216                 }
00217                 return *this;
00218         }
00219 
00220 private:
00221         T *m_pObject;
00222         const NameValuePairs &m_source;
00223         bool m_done;
00224 };
00225 
00226 template <class BASE, class T>
00227 AssignFromHelperClass<T, BASE> AssignFromHelper(T *pObject, const NameValuePairs &source, BASE *dummy=NULL)
00228 {
00229         return AssignFromHelperClass<T, BASE>(pObject, source);
00230 }
00231 
00232 template <class T>
00233 AssignFromHelperClass<T, T> AssignFromHelper(T *pObject, const NameValuePairs &source)
00234 {
00235         return AssignFromHelperClass<T, T>(pObject, source);
00236 }
00237 
00238 // ********************************************************
00239 
00240 // to allow the linker to discard Integer code if not needed.
00241 typedef bool (CRYPTOPP_API * PAssignIntToInteger)(const std::type_info &valueType, void *pInteger, const void *pInt);
00242 CRYPTOPP_DLL extern PAssignIntToInteger g_pAssignIntToInteger;
00243 
00244 CRYPTOPP_DLL const std::type_info & CRYPTOPP_API IntegerTypeId();
00245 
00246 class CRYPTOPP_DLL AlgorithmParametersBase : public NameValuePairs
00247 {
00248 public:
00249         class ParameterNotUsed : public Exception
00250         {
00251         public: 
00252                 ParameterNotUsed(const char *name) : Exception(OTHER_ERROR, std::string("AlgorithmParametersBase: parameter \"") + name + "\" not used") {}
00253         };
00254 
00255         AlgorithmParametersBase(const char *name, bool throwIfNotUsed)
00256                 : m_name(name), m_throwIfNotUsed(throwIfNotUsed), m_used(false) {}
00257 
00258         ~AlgorithmParametersBase()
00259         {
00260 #ifdef CRYPTOPP_UNCAUGHT_EXCEPTION_AVAILABLE
00261                 if (!std::uncaught_exception())
00262 #else
00263                 try
00264 #endif
00265                 {
00266                         if (m_throwIfNotUsed && !m_used)
00267                                 throw ParameterNotUsed(m_name);
00268                 }
00269 #ifndef CRYPTOPP_UNCAUGHT_EXCEPTION_AVAILABLE
00270                 catch(...)
00271                 {
00272                 }
00273 #endif
00274         }
00275 
00276         bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const;
00277 
00278 protected:
00279         virtual void AssignValue(const char *name, const std::type_info &valueType, void *pValue) const =0;
00280         virtual const NameValuePairs & GetParent() const =0;
00281 
00282         const char *m_name;
00283         bool m_throwIfNotUsed;
00284         mutable bool m_used;
00285 };
00286 
00287 template <class T>
00288 class AlgorithmParametersBase2 : public AlgorithmParametersBase
00289 {
00290 public:
00291         AlgorithmParametersBase2(const char *name, const T &value, bool throwIfNotUsed) : AlgorithmParametersBase(name, throwIfNotUsed), m_value(value) {}
00292 
00293         void AssignValue(const char *name, const std::type_info &valueType, void *pValue) const
00294         {
00295                 // special case for retrieving an Integer parameter when an int was passed in
00296                 if (!(g_pAssignIntToInteger != NULL && typeid(T) == typeid(int) && g_pAssignIntToInteger(valueType, pValue, &m_value)))
00297                 {
00298                         ThrowIfTypeMismatch(name, typeid(T), valueType);
00299                         *reinterpret_cast<T *>(pValue) = m_value;
00300                 }
00301         }
00302 
00303 protected:
00304         T m_value;
00305 };
00306 
00307 template <class PARENT, class T>
00308 class AlgorithmParameters : public AlgorithmParametersBase2<T>
00309 {
00310 public:
00311         AlgorithmParameters(const PARENT &parent, const char *name, const T &value, bool throwIfNotUsed)
00312                 : AlgorithmParametersBase2<T>(name, value, throwIfNotUsed), m_parent(parent)
00313         {}
00314 
00315         AlgorithmParameters(const AlgorithmParameters &copy)
00316                 : AlgorithmParametersBase2<T>(copy), m_parent(copy.m_parent)
00317         {
00318                 copy.m_used = true;
00319         }
00320 
00321         template <class R>
00322         AlgorithmParameters<AlgorithmParameters<PARENT,T>, R> operator()(const char *name, const R &value) const
00323         {
00324                 return AlgorithmParameters<AlgorithmParameters<PARENT,T>, R>(*this, name, value, this->m_throwIfNotUsed);
00325         }
00326 
00327         template <class R>
00328         AlgorithmParameters<AlgorithmParameters<PARENT,T>, R> operator()(const char *name, const R &value, bool throwIfNotUsed) const
00329         {
00330                 return AlgorithmParameters<AlgorithmParameters<PARENT,T>, R>(*this, name, value, throwIfNotUsed);
00331         }
00332 
00333 private:
00334         const NameValuePairs & GetParent() const {return m_parent;}
00335         PARENT m_parent;
00336 };
00337 
00338 //! Create an object that implements NameValuePairs for passing parameters
00339 /*! \param throwIfNotUsed if true, the object will throw an exception if the value is not accessed
00340         \note throwIfNotUsed is ignored if using a compiler that does not support std::uncaught_exception(),
00341         such as MSVC 7.0 and earlier.
00342         \note A NameValuePairs object containing an arbitrary number of name value pairs may be constructed by
00343         repeatedly using operator() on the object returned by MakeParameters, for example:
00344         const NameValuePairs &parameters = MakeParameters(name1, value1)(name2, value2)(name3, value3);
00345 */
00346 template <class T>
00347 AlgorithmParameters<NullNameValuePairs,T> MakeParameters(const char *name, const T &value, bool throwIfNotUsed = true)
00348 {
00349         return AlgorithmParameters<NullNameValuePairs,T>(g_nullNameValuePairs, name, value, throwIfNotUsed);
00350 }
00351 
00352 #define CRYPTOPP_GET_FUNCTION_ENTRY(name)               (Name::name(), &ThisClass::Get##name)
00353 #define CRYPTOPP_SET_FUNCTION_ENTRY(name)               (Name::name(), &ThisClass::Set##name)
00354 #define CRYPTOPP_SET_FUNCTION_ENTRY2(name1, name2)      (Name::name1(), Name::name2(), &ThisClass::Set##name1##And##name2)
00355 
00356 NAMESPACE_END
00357 
00358 #endif

Generated on Sat Dec 23 02:07:05 2006 for Crypto++ by  doxygen 1.5.1-p1