1 #ifndef CRYPTOPP_ALGPARAM_H
2 #define CRYPTOPP_ALGPARAM_H
8 NAMESPACE_BEGIN(CryptoPP)
16 ConstByteArrayParameter(
const char *data = NULL,
bool deepCopy =
false)
18 Assign((
const byte *)data, data ? strlen(data) : 0, deepCopy);
20 ConstByteArrayParameter(
const byte *data,
size_t size,
bool deepCopy =
false)
22 Assign(data, size, deepCopy);
24 template <
class T> ConstByteArrayParameter(
const T &
string,
bool deepCopy =
false)
26 CRYPTOPP_COMPILE_ASSERT(
sizeof(CPP_TYPENAME T::value_type) == 1);
27 Assign((
const byte *)
string.data(),
string.size(), deepCopy);
30 void Assign(
const byte *data,
size_t size,
bool deepCopy)
33 m_block.Assign(data, size);
39 m_deepCopy = deepCopy;
42 const byte *begin()
const {
return m_deepCopy ? m_block.begin() : m_data;}
43 const byte *end()
const {
return m_deepCopy ? m_block.end() : m_data + m_size;}
44 size_t size()
const {
return m_deepCopy ? m_block.size() : m_size;}
57 : m_data(data), m_size(size) {}
59 : m_data(block.begin()), m_size(block.size()) {}
61 byte *begin()
const {
return m_data;}
62 byte *end()
const {
return m_data + m_size;}
63 size_t size()
const {
return m_size;}
74 : m_pairs1(pairs1), m_pairs2(pairs2) {}
76 bool GetVoidValue(
const char *name,
const std::type_info &valueType,
void *pValue)
const;
82 template <
class T,
class BASE>
87 : m_pObject(pObject), m_name(name), m_valueType(&valueType), m_pValue(pValue), m_found(
false), m_getValueNames(
false)
89 if (strcmp(m_name,
"ValueNames") == 0)
91 m_found = m_getValueNames =
true;
95 if (
typeid(T) !=
typeid(BASE))
96 pObject->BASE::GetVoidValue(m_name, valueType, pValue);
97 ((*
reinterpret_cast<std::string *
>(m_pValue) +=
"ThisPointer:") +=
typeid(T).name()) +=
';';
100 if (!m_found && strncmp(m_name,
"ThisPointer:", 12) == 0 && strcmp(m_name+12,
typeid(T).name()) == 0)
103 *
reinterpret_cast<const T **
>(pValue) = pObject;
108 if (!m_found && searchFirst)
109 m_found = searchFirst->
GetVoidValue(m_name, valueType, pValue);
111 if (!m_found &&
typeid(T) !=
typeid(BASE))
112 m_found = pObject->BASE::GetVoidValue(m_name, valueType, pValue);
115 operator bool()
const {
return m_found;}
121 (*
reinterpret_cast<std::string *
>(m_pValue) += name) +=
";";
122 if (!m_found && strcmp(name, m_name) == 0)
125 *
reinterpret_cast<R *
>(m_pValue) = (m_pObject->*pm)();
133 #ifndef __INTEL_COMPILER // ICL 9.1 workaround: Intel compiler copies the vTable pointer for some reason
135 ((*
reinterpret_cast<std::string *
>(m_pValue) +=
"ThisObject:") +=
typeid(T).name()) +=
';';
136 if (!m_found && strncmp(m_name,
"ThisObject:", 11) == 0 && strcmp(m_name+11,
typeid(T).name()) == 0)
139 *
reinterpret_cast<T *
>(m_pValue) = *m_pObject;
149 const std::type_info *m_valueType;
151 bool m_found, m_getValueNames;
154 template <
class BASE,
class T>
169 R Hack_DefaultValueFromConstReferenceType(
const R &)
175 bool Hack_GetValueIntoConstReference(
const NameValuePairs &source,
const char *name,
const R &value)
177 return source.
GetValue(name, const_cast<R &>(value));
180 template <
class T,
class BASE>
185 : m_pObject(pObject), m_source(source), m_done(
false)
189 else if (
typeid(BASE) !=
typeid(T))
190 pObject->BASE::AssignFrom(source);
198 R value = Hack_DefaultValueFromConstReferenceType(reinterpret_cast<R>(*(
int *)NULL));
199 if (!Hack_GetValueIntoConstReference(m_source, name, value))
200 throw InvalidArgument(std::string(
typeid(T).name()) +
": Missing required parameter '" + name +
"'");
201 (m_pObject->*pm)(value);
206 template <
class R,
class S>
211 R value1 = Hack_DefaultValueFromConstReferenceType(reinterpret_cast<R>(*(
int *)NULL));
212 if (!Hack_GetValueIntoConstReference(m_source, name1, value1))
213 throw InvalidArgument(std::string(
typeid(T).name()) +
": Missing required parameter '" + name1 +
"'");
214 S value2 = Hack_DefaultValueFromConstReferenceType(reinterpret_cast<S>(*(
int *)NULL));
215 if (!Hack_GetValueIntoConstReference(m_source, name2, value2))
216 throw InvalidArgument(std::string(
typeid(T).name()) +
": Missing required parameter '" + name2 +
"'");
217 (m_pObject->*pm)(value1, value2);
228 template <
class BASE,
class T>
243 typedef bool (CRYPTOPP_API * PAssignIntToInteger)(
const std::type_info &valueType,
void *pInteger,
const void *pInt);
244 CRYPTOPP_DLL
extern PAssignIntToInteger g_pAssignIntToInteger;
246 CRYPTOPP_DLL
const std::type_info & CRYPTOPP_API IntegerTypeId();
254 ParameterNotUsed(
const char *name) :
Exception(OTHER_ERROR, std::string(
"AlgorithmParametersBase: parameter \"") + name +
"\" not used") {}
259 : m_name(x.m_name), m_throwIfNotUsed(x.m_throwIfNotUsed), m_used(x.m_used)
261 m_next.reset(const_cast<AlgorithmParametersBase &>(x).m_next.release());
266 : m_name(name), m_throwIfNotUsed(throwIfNotUsed), m_used(false) {}
270 #ifdef CRYPTOPP_UNCAUGHT_EXCEPTION_AVAILABLE
271 if (!std::uncaught_exception())
276 if (m_throwIfNotUsed && !m_used)
277 throw ParameterNotUsed(m_name);
279 #ifndef CRYPTOPP_UNCAUGHT_EXCEPTION_AVAILABLE
286 bool GetVoidValue(
const char *name,
const std::type_info &valueType,
void *pValue)
const;
292 virtual void AssignValue(
const char *name,
const std::type_info &valueType,
void *pValue)
const =0;
293 virtual void MoveInto(
void *p)
const =0;
296 bool m_throwIfNotUsed;
310 void AssignValue(
const char *name,
const std::type_info &valueType,
void *pValue)
const
313 if (!(g_pAssignIntToInteger != NULL &&
typeid(T) ==
typeid(
int) && g_pAssignIntToInteger(valueType, pValue, &m_value)))
316 *
reinterpret_cast<T *
>(pValue) = m_value;
320 void MoveInto(
void *buffer)
const
342 , m_defaultThrowIfNotUsed(throwIfNotUsed)
355 p->m_next.reset(m_next.release());
356 m_next.reset(p.release());
357 m_defaultThrowIfNotUsed = throwIfNotUsed;
364 return operator()(name, value, m_defaultThrowIfNotUsed);
367 bool GetVoidValue(
const char *name,
const std::type_info &valueType,
void *pValue)
const;
371 bool m_defaultThrowIfNotUsed;
386 AlgorithmParameters MakeParameters(
const char *name,
const T &value,
bool throwIfNotUsed =
true)
392 #define CRYPTOPP_GET_FUNCTION_ENTRY(name) (Name::name(), &ThisClass::Get##name)
393 #define CRYPTOPP_SET_FUNCTION_ENTRY(name) (Name::name(), &ThisClass::Set##name)
394 #define CRYPTOPP_SET_FUNCTION_ENTRY2(name1, name2) (Name::name1(), Name::name2(), &ThisClass::Set##name1##And##name2)
used to pass byte array input as part of a NameValuePairs object
base class for all exceptions thrown by Crypto++
exception thrown when an invalid argument is detected
bool GetThisObject(T &object) const
get a copy of this object or a subobject of it
bool GetValue(const char *name, T &value) const
get a named value, returns true if the name exists
virtual bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const =0
to be implemented by derived classes, users should use one of the above functions instead ...
static void ThrowIfTypeMismatch(const char *name, const std::type_info &stored, const std::type_info &retrieving)
used by derived classes to check for type mismatch
interface for retrieving values given their names