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
00011
00012
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(CPP_TYPENAME T::value_type) == 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 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))
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))
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
00241 CRYPTOPP_DLL
extern bool (*AssignIntToInteger)(
const std::type_info &valueType,
void *pInteger,
const void *pInt);
00242
00243 CRYPTOPP_DLL
const std::type_info & IntegerTypeId();
00244
00245
class CRYPTOPP_DLL AlgorithmParametersBase :
public NameValuePairs
00246 {
00247
public:
00248
class ParameterNotUsed :
public Exception
00249 {
00250
public:
00251 ParameterNotUsed(
const char *name) :
Exception(OTHER_ERROR, std::string("AlgorithmParametersBase: parameter \"") + name + "\" not used") {}
00252 };
00253
00254 AlgorithmParametersBase(
const char *name,
bool throwIfNotUsed)
00255 : m_name(name), m_throwIfNotUsed(throwIfNotUsed), m_used(false) {}
00256
00257 ~AlgorithmParametersBase()
00258 {
00259
#ifdef CRYPTOPP_UNCAUGHT_EXCEPTION_AVAILABLE
00260
if (!std::uncaught_exception())
00261
#else
00262
try
00263
#endif
00264
{
00265
if (m_throwIfNotUsed && !m_used)
00266
throw ParameterNotUsed(m_name);
00267 }
00268
#ifndef CRYPTOPP_UNCAUGHT_EXCEPTION_AVAILABLE
00269
catch(...)
00270 {
00271 }
00272
#endif
00273
}
00274
00275
bool GetVoidValue(
const char *name,
const std::type_info &valueType,
void *pValue)
const;
00276
00277
protected:
00278
virtual void AssignValue(
const char *name,
const std::type_info &valueType,
void *pValue)
const =0;
00279
virtual const NameValuePairs & GetParent() const =0;
00280
00281 const
char *m_name;
00282
bool m_throwIfNotUsed;
00283 mutable
bool m_used;
00284 };
00285
00286 template <class T>
00287 class AlgorithmParametersBase2 : public AlgorithmParametersBase
00288 {
00289
public:
00290 AlgorithmParametersBase2(
const char *name,
const T &value,
bool throwIfNotUsed) : AlgorithmParametersBase(name, throwIfNotUsed), m_value(value) {}
00291
00292
void AssignValue(
const char *name,
const std::type_info &valueType,
void *pValue)
const
00293
{
00294
00295
if (!(AssignIntToInteger != NULL &&
typeid(T) ==
typeid(
int) && AssignIntToInteger(valueType, pValue, &m_value)))
00296 {
00297
ThrowIfTypeMismatch(name,
typeid(T), valueType);
00298 *reinterpret_cast<T *>(pValue) = m_value;
00299 }
00300 }
00301
00302
protected:
00303 T m_value;
00304 };
00305
00306
template <
class PARENT,
class T>
00307
class AlgorithmParameters :
public AlgorithmParametersBase2<T>
00308 {
00309
public:
00310 AlgorithmParameters(
const PARENT &parent,
const char *name,
const T &value,
bool throwIfNotUsed)
00311 : AlgorithmParametersBase2<T>(name, value, throwIfNotUsed), m_parent(parent)
00312 {}
00313
00314 AlgorithmParameters(
const AlgorithmParameters ©)
00315 : AlgorithmParametersBase2<T>(copy), m_parent(copy.m_parent)
00316 {
00317 copy.m_used =
true;
00318 }
00319
00320
template <
class R>
00321 AlgorithmParameters<AlgorithmParameters<PARENT,T>, R> operator()(
const char *name,
const R &value)
const
00322
{
00323
return AlgorithmParameters<AlgorithmParameters<PARENT,T>, R>(*
this, name, value, this->m_throwIfNotUsed);
00324 }
00325
00326
template <
class R>
00327 AlgorithmParameters<AlgorithmParameters<PARENT,T>, R> operator()(
const char *name,
const R &value,
bool throwIfNotUsed)
const
00328
{
00329
return AlgorithmParameters<AlgorithmParameters<PARENT,T>, R>(*
this, name, value, throwIfNotUsed);
00330 }
00331
00332
private:
00333
const NameValuePairs & GetParent()
const {
return m_parent;}
00334 PARENT m_parent;
00335 };
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
template <
class T>
00346 AlgorithmParameters<NullNameValuePairs,T> MakeParameters(
const char *name,
const T &value,
bool throwIfNotUsed =
true)
00347 {
00348
return AlgorithmParameters<NullNameValuePairs,T>(g_nullNameValuePairs, name, value, throwIfNotUsed);
00349 }
00350
00351
#define CRYPTOPP_GET_FUNCTION_ENTRY(name) (Name::name(), &ThisClass::Get##name)
00352
#define CRYPTOPP_SET_FUNCTION_ENTRY(name) (Name::name(), &ThisClass::Set##name)
00353
#define CRYPTOPP_SET_FUNCTION_ENTRY2(name1, name2) (Name::name1(), Name::name2(), &ThisClass::Set##name1##And##name2)
00354
00355 NAMESPACE_END
00356
00357
#endif