Main Page | Namespace List | Class Hierarchy | Alphabetical List | Compound List | File List | Namespace Members | Compound Members | 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, unsigned int 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(string[0])==1);
    00027                 Assign((const byte *)string.data(), string.size(), deepCopy);
    00028         }
    00029
    00030         void Assign(const byte *data, unsigned int 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         unsigned int size() const {return m_deepCopy ? m_block.size() : m_size;}
    00045
    00046 private:
    00047         bool m_deepCopy;
    00048         const byte *m_data;
    00049         unsigned int 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         unsigned int size() const {return m_size;}
    00064
    00065 private:
    00066         byte *m_data;
    00067         unsigned int m_size;
    00068 };
    00069
    00070 class 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                 if (strcmp(name, "ValueNames") == 0)
    00079                         return m_pairs1.GetVoidValue(name, valueType, pValue) && m_pairs2.GetVoidValue(name, valueType, pValue);
    00080                 else
    00081                         return m_pairs1.GetVoidValue(name, valueType, pValue) || m_pairs2.GetVoidValue(name, valueType, pValue);
    00082         }
    00083
    00084         const NameValuePairs &m_pairs1, &m_pairs2;
    00085 };
    00086
    00087 template <class T, class BASE>
    00088 class GetValueHelperClass
    00089 {
    00090 public:
    00091         GetValueHelperClass(const T *pObject, const char *name, const std::type_info &valueType, void *pValue)
    00092                 : m_pObject(pObject), m_name(name), m_valueType(&valueType), m_pValue(pValue), m_found(false), m_getValueNames(false)
    00093         {
    00094                 if (strcmp(name, "ValueNames") == 0)
    00095                         m_found = m_getValueNames = true;
    00096
    00097                 std::string thisPointerName = std::string("ThisPointer:") + typeid(T).name();
    00098
    00099                 if (m_getValueNames)
    00100                 {
    00101                         NameValuePairs::ThrowIfTypeMismatch(name, typeid(std::string), *m_valueType);
    00102                         if (typeid(T) != typeid(BASE))
    00103                                 pObject->BASE::GetVoidValue(name, valueType, pValue);
    00104                         (*reinterpret_cast<std::string *>(m_pValue) += thisPointerName) += ";";
    00105                 }
    00106                 else if (name == thisPointerName)
    00107                 {
    00108                         NameValuePairs::ThrowIfTypeMismatch(name, typeid(T *), *m_valueType);
    00109                         *reinterpret_cast<const T **>(pValue) = pObject;
    00110                         m_found = true;
    00111                 }
    00112                 else if (typeid(T) != typeid(BASE))
    00113                         m_found = pObject->BASE::GetVoidValue(name, valueType, pValue);
    00114         }
    00115
    00116         operator bool() const {return m_found;}
    00117
    00118         template <class R>
    00119         GetValueHelperClass<T,BASE> & operator()(const char *name, const R & (T::*pm)() const)
    00120         {
    00121                 if (m_getValueNames)
    00122                         (*reinterpret_cast<std::string *>(m_pValue) += name) += ";";
    00123                 else if (!m_found && strcmp(name, m_name) == 0)
    00124                 {
    00125                         NameValuePairs::ThrowIfTypeMismatch(name, typeid(R), *m_valueType);
    00126                         *reinterpret_cast<R *>(m_pValue) = (m_pObject->*pm)();
    00127                         m_found = true;
    00128                 }
    00129                 return *this;
    00130         }
    00131
    00132         GetValueHelperClass<T,BASE> &Assignable()
    00133         {
    00134                 std::string thisObjectName = std::string("ThisObject:") + typeid(T).name();
    00135                 if (m_getValueNames)
    00136                         (*reinterpret_cast<std::string *>(m_pValue) += thisObjectName) += ";";
    00137                 else if (!m_found && m_name == thisObjectName)
    00138                 {
    00139                         NameValuePairs::ThrowIfTypeMismatch(m_name, typeid(T), *m_valueType);
    00140                         *reinterpret_cast<T *>(m_pValue) = *m_pObject;
    00141                         m_found = true;
    00142                 }
    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, BASE *dummy=NULL)
    00156 {
    00157         return GetValueHelperClass<T, BASE>(pObject, name, valueType, pValue);
    00158 }
    00159
    00160 template <class T>
    00161 GetValueHelperClass<T, T> GetValueHelper(const T *pObject, const char *name, const std::type_info &valueType, void *pValue)
    00162 {
    00163         return GetValueHelperClass<T, T>(pObject, name, valueType, pValue);
    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 CRYPTOPP_DLL void AssignIntToInteger(void *pInteger, const void *pInt);
    00243
    00244 CRYPTOPP_DLL extern const std::type_info &g_typeidInteger;
    00245
    00246 template <class BASE, class T>
    00247 class AlgorithmParameters : public NameValuePairs
    00248 {
    00249 public:
    00250         AlgorithmParameters(const BASE &base, const char *name, const T &value)
    00251                 : m_base(base), m_name(name), m_value(value)
    00252 #ifndef NDEBUG
    00253                 , m_used(false)
    00254 #endif
    00255         {}
    00256
    00257 #ifndef NDEBUG
    00258         AlgorithmParameters(const AlgorithmParameters &copy)
    00259                 : m_base(copy.m_base), m_name(copy.m_name), m_value(copy.m_value), m_used(false)
    00260         {
    00261                 copy.m_used = true;
    00262         }
    00263
    00264         // TODO: revisit after implementing some tracing mechanism, this won't work because of exceptions
    00265 //      ~AlgorithmParameters() {assert(m_used);}        // use assert here because we don't want to throw out of a destructor
    00266 #endif
    00267
    00268         template <class R>
    00269         AlgorithmParameters<AlgorithmParameters<BASE,T>, R> operator()(const char *name, const R &value) const
    00270         {
    00271                 return AlgorithmParameters<AlgorithmParameters<BASE,T>, R>(*this, name, value);
    00272         }
    00273
    00274         bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const
    00275         {
    00276                 if (strcmp(name, "ValueNames") == 0)
    00277                 {
    00278                         ThrowIfTypeMismatch(name, typeid(std::string), valueType);
    00279                         m_base.GetVoidValue(name, valueType, pValue);
    00280                         (*reinterpret_cast<std::string *>(pValue) += m_name) += ";";
    00281                         return true;
    00282                 }
    00283                 else if (strcmp(name, m_name) == 0)
    00284                 {
    00285                         // special case for retrieving an Integer parameter when an int was passed in
    00286                         if (valueType == g_typeidInteger && typeid(T) == typeid(int))
    00287                                 AssignIntToInteger(pValue, &m_value);
    00288                         else
    00289                         {
    00290                                 ThrowIfTypeMismatch(name, typeid(T), valueType);
    00291                                 *reinterpret_cast<T *>(pValue) = m_value;
    00292                         }
    00293 #ifndef NDEBUG
    00294                         m_used = true;
    00295 #endif
    00296                         return true;
    00297                 }
    00298                 else
    00299                         return m_base.GetVoidValue(name, valueType, pValue);
    00300         }
    00301
    00302 private:
    00303         BASE m_base;
    00304         const char *m_name;
    00305         T m_value;
    00306 #ifndef NDEBUG
    00307         mutable bool m_used;
    00308 #endif
    00309 };
    00310
    00311 template <class T>
    00312 AlgorithmParameters<NullNameValuePairs,T> MakeParameters(const char *name, const T &value)
    00313 {
    00314         return AlgorithmParameters<NullNameValuePairs,T>(g_nullNameValuePairs, name, value);
    00315 }
    00316
    00317 #define CRYPTOPP_GET_FUNCTION_ENTRY(name)               (Name::name(), &ThisClass::Get##name)
    00318 #define CRYPTOPP_SET_FUNCTION_ENTRY(name)               (Name::name(), &ThisClass::Set##name)
    00319 #define CRYPTOPP_SET_FUNCTION_ENTRY2(name1, name2)      (Name::name1(), Name::name2(), &ThisClass::Set##name1##And##name2)
    00320
    00321 NAMESPACE_END
    00322
    00323 #endif