ext-cryptopp/algparam.h

534 lines
19 KiB
C
Raw Normal View History

// algparam.h - originally written and placed in the public domain by Wei Dai
2015-11-05 06:59:46 +00:00
//! \file
//! \headerfile algparam.h
//! \brief Classes for working with NameValuePairs
2015-11-05 06:59:46 +00:00
#ifndef CRYPTOPP_ALGPARAM_H
#define CRYPTOPP_ALGPARAM_H
#include "config.h"
2015-12-17 06:37:01 +00:00
#include "cryptlib.h"
2015-11-05 06:59:46 +00:00
#if CRYPTOPP_MSC_VERSION
2015-11-05 06:59:46 +00:00
# pragma warning(push)
# pragma warning(disable: 4231 4275)
# if (CRYPTOPP_MSC_VERSION >= 1400)
# pragma warning(disable: 6011 6386 28193)
# endif
2015-11-05 06:59:46 +00:00
#endif
#include "smartptr.h"
#include "secblock.h"
#include "integer.h"
#include "misc.h"
NAMESPACE_BEGIN(CryptoPP)
2015-12-17 06:37:01 +00:00
//! \class ConstByteArrayParameter
//! \brief Used to pass byte array input as part of a NameValuePairs object
2015-11-05 06:59:46 +00:00
class ConstByteArrayParameter
{
public:
2015-12-17 06:37:01 +00:00
//! \brief Construct a ConstByteArrayParameter
//! \param data a C-String
//! \param deepCopy flag indicating whether the data should be copied
//! \details The deepCopy option is used when the NameValuePairs object can't
//! keep a copy of the data available
2017-03-01 11:10:06 +00:00
ConstByteArrayParameter(const char *data = NULLPTR, bool deepCopy = false)
: m_deepCopy(false), m_data(NULLPTR), m_size(0)
2015-11-05 06:59:46 +00:00
{
Assign((const byte *)data, data ? strlen(data) : 0, deepCopy);
}
2015-12-17 06:37:01 +00:00
//! \brief Construct a ConstByteArrayParameter
//! \param data a memory buffer
//! \param size the length of the memory buffer
//! \param deepCopy flag indicating whether the data should be copied
//! \details The deepCopy option is used when the NameValuePairs object can't
//! keep a copy of the data available
2015-11-05 06:59:46 +00:00
ConstByteArrayParameter(const byte *data, size_t size, bool deepCopy = false)
2017-03-01 11:10:06 +00:00
: m_deepCopy(false), m_data(NULLPTR), m_size(0)
2015-11-05 06:59:46 +00:00
{
Assign(data, size, deepCopy);
}
2015-12-17 06:37:01 +00:00
//! \brief Construct a ConstByteArrayParameter
//! \tparam T a std::basic_string<char> class
//! \param string a std::basic_string<char> class
//! \param deepCopy flag indicating whether the data should be copied
//! \details The deepCopy option is used when the NameValuePairs object can't
//! keep a copy of the data available
2015-11-05 06:59:46 +00:00
template <class T> ConstByteArrayParameter(const T &string, bool deepCopy = false)
2017-03-01 11:10:06 +00:00
: m_deepCopy(false), m_data(NULLPTR), m_size(0)
2015-11-05 06:59:46 +00:00
{
CRYPTOPP_COMPILE_ASSERT(sizeof(typename T::value_type) == 1);
2015-11-05 06:59:46 +00:00
Assign((const byte *)string.data(), string.size(), deepCopy);
}
2015-12-17 06:37:01 +00:00
//! \brief Assign contents from a memory buffer
//! \param data a memory buffer
//! \param size the length of the memory buffer
//! \param deepCopy flag indicating whether the data should be copied
//! \details The deepCopy option is used when the NameValuePairs object can't
//! keep a copy of the data available
2015-11-05 06:59:46 +00:00
void Assign(const byte *data, size_t size, bool deepCopy)
{
// This fires, which means: no data with a size, or data with no size.
// CRYPTOPP_ASSERT((data && size) || !(data || size));
2015-11-05 06:59:46 +00:00
if (deepCopy)
m_block.Assign(data, size);
else
{
m_data = data;
m_size = size;
}
m_deepCopy = deepCopy;
}
2015-12-17 06:37:01 +00:00
//! \brief Pointer to the first byte in the memory block
2015-11-05 06:59:46 +00:00
const byte *begin() const {return m_deepCopy ? m_block.begin() : m_data;}
2015-12-17 06:37:01 +00:00
//! \brief Pointer beyond the last byte in the memory block
2015-11-05 06:59:46 +00:00
const byte *end() const {return m_deepCopy ? m_block.end() : m_data + m_size;}
2015-12-17 06:37:01 +00:00
//! \brief Length of the memory block
2015-11-05 06:59:46 +00:00
size_t size() const {return m_deepCopy ? m_block.size() : m_size;}
private:
bool m_deepCopy;
const byte *m_data;
size_t m_size;
SecByteBlock m_block;
};
2015-12-17 06:37:01 +00:00
//! \class ByteArrayParameter
//! \brief Used to pass byte array input as part of a NameValuePairs object
2015-11-05 06:59:46 +00:00
class ByteArrayParameter
{
public:
2015-12-17 06:37:01 +00:00
//! \brief Construct a ByteArrayParameter
//! \param data a memory buffer
//! \param size the length of the memory buffer
2017-03-01 11:10:06 +00:00
ByteArrayParameter(byte *data = NULLPTR, unsigned int size = 0)
2015-11-05 06:59:46 +00:00
: m_data(data), m_size(size) {}
2015-12-17 06:37:01 +00:00
//! \brief Construct a ByteArrayParameter
//! \param block a SecByteBlock
2015-11-05 06:59:46 +00:00
ByteArrayParameter(SecByteBlock &block)
: m_data(block.begin()), m_size(block.size()) {}
2015-12-17 06:37:01 +00:00
//! \brief Pointer to the first byte in the memory block
2015-11-05 06:59:46 +00:00
byte *begin() const {return m_data;}
2015-12-17 06:37:01 +00:00
//! \brief Pointer beyond the last byte in the memory block
2015-11-05 06:59:46 +00:00
byte *end() const {return m_data + m_size;}
2015-12-17 06:37:01 +00:00
//! \brief Length of the memory block
2015-11-05 06:59:46 +00:00
size_t size() const {return m_size;}
private:
byte *m_data;
size_t m_size;
};
2015-12-17 06:37:01 +00:00
//! \class CombinedNameValuePairs
//! \brief Combines two sets of NameValuePairs
//! \details CombinedNameValuePairs allows you to provide two sets of of NameValuePairs.
//! If a name is not found in the first set, then the second set is searched for the
//! name and value pair. The second set of NameValuePairs often provides default values.
2015-11-05 06:59:46 +00:00
class CRYPTOPP_DLL CombinedNameValuePairs : public NameValuePairs
{
public:
2015-12-17 06:37:01 +00:00
//! \brief Construct a CombinedNameValuePairs
//! \param pairs1 reference to the first set of NameValuePairs
//! \param pairs2 reference to the second set of NameValuePairs
2015-11-05 06:59:46 +00:00
CombinedNameValuePairs(const NameValuePairs &pairs1, const NameValuePairs &pairs2)
: m_pairs1(pairs1), m_pairs2(pairs2) {}
bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const;
private:
const NameValuePairs &m_pairs1, &m_pairs2;
};
2015-12-17 06:37:01 +00:00
#ifndef CRYPTOPP_DOXYGEN_PROCESSING
2015-11-05 06:59:46 +00:00
template <class T, class BASE>
class GetValueHelperClass
{
public:
GetValueHelperClass(const T *pObject, const char *name, const std::type_info &valueType, void *pValue, const NameValuePairs *searchFirst)
: m_pObject(pObject), m_name(name), m_valueType(&valueType), m_pValue(pValue), m_found(false), m_getValueNames(false)
{
if (strcmp(m_name, "ValueNames") == 0)
{
m_found = m_getValueNames = true;
NameValuePairs::ThrowIfTypeMismatch(m_name, typeid(std::string), *m_valueType);
if (searchFirst)
searchFirst->GetVoidValue(m_name, valueType, pValue);
if (typeid(T) != typeid(BASE))
pObject->BASE::GetVoidValue(m_name, valueType, pValue);
((*reinterpret_cast<std::string *>(m_pValue) += "ThisPointer:") += typeid(T).name()) += ';';
}
if (!m_found && strncmp(m_name, "ThisPointer:", 12) == 0 && strcmp(m_name+12, typeid(T).name()) == 0)
{
NameValuePairs::ThrowIfTypeMismatch(m_name, typeid(T *), *m_valueType);
*reinterpret_cast<const T **>(pValue) = pObject;
m_found = true;
return;
}
if (!m_found && searchFirst)
m_found = searchFirst->GetVoidValue(m_name, valueType, pValue);
2016-09-10 08:57:48 +00:00
2015-11-05 06:59:46 +00:00
if (!m_found && typeid(T) != typeid(BASE))
m_found = pObject->BASE::GetVoidValue(m_name, valueType, pValue);
}
operator bool() const {return m_found;}
template <class R>
GetValueHelperClass<T,BASE> & operator()(const char *name, const R & (T::*pm)() const)
{
if (m_getValueNames)
(*reinterpret_cast<std::string *>(m_pValue) += name) += ";";
if (!m_found && strcmp(name, m_name) == 0)
{
NameValuePairs::ThrowIfTypeMismatch(name, typeid(R), *m_valueType);
*reinterpret_cast<R *>(m_pValue) = (m_pObject->*pm)();
m_found = true;
}
return *this;
}
GetValueHelperClass<T,BASE> &Assignable()
{
#ifndef __INTEL_COMPILER // ICL 9.1 workaround: Intel compiler copies the vTable pointer for some reason
if (m_getValueNames)
((*reinterpret_cast<std::string *>(m_pValue) += "ThisObject:") += typeid(T).name()) += ';';
if (!m_found && strncmp(m_name, "ThisObject:", 11) == 0 && strcmp(m_name+11, typeid(T).name()) == 0)
{
NameValuePairs::ThrowIfTypeMismatch(m_name, typeid(T), *m_valueType);
*reinterpret_cast<T *>(m_pValue) = *m_pObject;
m_found = true;
}
#endif
return *this;
}
private:
const T *m_pObject;
const char *m_name;
const std::type_info *m_valueType;
void *m_pValue;
bool m_found, m_getValueNames;
};
template <class BASE, class T>
2017-03-01 11:10:06 +00:00
GetValueHelperClass<T, BASE> GetValueHelper(const T *pObject, const char *name, const std::type_info &valueType, void *pValue, const NameValuePairs *searchFirst=NULLPTR)
2015-11-05 06:59:46 +00:00
{
return GetValueHelperClass<T, BASE>(pObject, name, valueType, pValue, searchFirst);
}
template <class T>
2017-03-01 11:10:06 +00:00
GetValueHelperClass<T, T> GetValueHelper(const T *pObject, const char *name, const std::type_info &valueType, void *pValue, const NameValuePairs *searchFirst=NULLPTR)
2015-11-05 06:59:46 +00:00
{
return GetValueHelperClass<T, T>(pObject, name, valueType, pValue, searchFirst);
}
// ********************************************************
template <class T, class BASE>
class AssignFromHelperClass
{
public:
AssignFromHelperClass(T *pObject, const NameValuePairs &source)
: m_pObject(pObject), m_source(source), m_done(false)
{
if (source.GetThisObject(*pObject))
m_done = true;
else if (typeid(BASE) != typeid(T))
pObject->BASE::AssignFrom(source);
}
template <class R>
AssignFromHelperClass & operator()(const char *name, void (T::*pm)(const R&))
{
if (!m_done)
{
R value;
if (!m_source.GetValue(name, value))
throw InvalidArgument(std::string(typeid(T).name()) + ": Missing required parameter '" + name + "'");
(m_pObject->*pm)(value);
}
return *this;
}
template <class R, class S>
AssignFromHelperClass & operator()(const char *name1, const char *name2, void (T::*pm)(const R&, const S&))
{
if (!m_done)
{
R value1;
if (!m_source.GetValue(name1, value1))
throw InvalidArgument(std::string(typeid(T).name()) + ": Missing required parameter '" + name1 + "'");
S value2;
if (!m_source.GetValue(name2, value2))
throw InvalidArgument(std::string(typeid(T).name()) + ": Missing required parameter '" + name2 + "'");
(m_pObject->*pm)(value1, value2);
}
return *this;
}
private:
T *m_pObject;
const NameValuePairs &m_source;
bool m_done;
};
template <class BASE, class T>
AssignFromHelperClass<T, BASE> AssignFromHelper(T *pObject, const NameValuePairs &source)
2015-11-05 06:59:46 +00:00
{
return AssignFromHelperClass<T, BASE>(pObject, source);
}
template <class T>
AssignFromHelperClass<T, T> AssignFromHelper(T *pObject, const NameValuePairs &source)
{
return AssignFromHelperClass<T, T>(pObject, source);
}
2015-12-17 06:37:01 +00:00
#endif // CRYPTOPP_DOXYGEN_PROCESSING
2015-11-05 06:59:46 +00:00
// ********************************************************
#ifndef CRYPTOPP_NO_ASSIGN_TO_INTEGER
// Allow the linker to discard Integer code if not needed.
// Also see http://github.com/weidai11/cryptopp/issues/389.
CRYPTOPP_DLL bool AssignIntToInteger(const std::type_info &valueType, void *pInteger, const void *pInt);
#endif
2015-11-05 06:59:46 +00:00
CRYPTOPP_DLL const std::type_info & CRYPTOPP_API IntegerTypeId();
2015-12-17 06:37:01 +00:00
//! \class AlgorithmParametersBase
//! \brief Base class for AlgorithmParameters
2015-11-05 06:59:46 +00:00
class CRYPTOPP_DLL AlgorithmParametersBase
{
public:
2015-12-17 06:37:01 +00:00
//! \class ParameterNotUsed
//! \brief Exception thrown when an AlgorithmParameter is unused
2015-11-05 06:59:46 +00:00
class ParameterNotUsed : public Exception
{
2016-09-10 08:57:48 +00:00
public:
2015-11-05 06:59:46 +00:00
ParameterNotUsed(const char *name) : Exception(OTHER_ERROR, std::string("AlgorithmParametersBase: parameter \"") + name + "\" not used") {}
};
virtual ~AlgorithmParametersBase() CRYPTOPP_THROW
{
#ifdef CRYPTOPP_UNCAUGHT_EXCEPTION_AVAILABLE
if (!std::uncaught_exception())
#else
try
#endif
{
if (m_throwIfNotUsed && !m_used)
throw ParameterNotUsed(m_name);
}
#ifndef CRYPTOPP_UNCAUGHT_EXCEPTION_AVAILABLE
catch(const Exception&)
{
}
#endif
}
// this is actually a move, not a copy
AlgorithmParametersBase(const AlgorithmParametersBase &x)
: m_name(x.m_name), m_throwIfNotUsed(x.m_throwIfNotUsed), m_used(x.m_used)
{
m_next.reset(const_cast<AlgorithmParametersBase &>(x).m_next.release());
x.m_used = true;
}
//! \brief Construct a AlgorithmParametersBase
//! \param name the parameter name
//! \param throwIfNotUsed flags indicating whether an exception should be thrown
//! \details If throwIfNotUsed is true, then a ParameterNotUsed exception
//! will be thrown in the destructor if the parameter is not not retrieved.
AlgorithmParametersBase(const char *name, bool throwIfNotUsed)
: m_name(name), m_throwIfNotUsed(throwIfNotUsed), m_used(false) {}
2015-11-05 06:59:46 +00:00
bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const;
2016-09-10 08:57:48 +00:00
2015-11-05 06:59:46 +00:00
protected:
friend class AlgorithmParameters;
void operator=(const AlgorithmParametersBase& rhs); // assignment not allowed, declare this for VC60
2015-11-05 06:59:46 +00:00
virtual void AssignValue(const char *name, const std::type_info &valueType, void *pValue) const =0;
virtual void MoveInto(void *p) const =0; // not really const
const char *m_name;
bool m_throwIfNotUsed;
mutable bool m_used;
member_ptr<AlgorithmParametersBase> m_next;
};
2015-12-17 06:37:01 +00:00
//! \class AlgorithmParametersTemplate
//! \brief Template base class for AlgorithmParameters
//! \tparam T the class or type
2015-11-05 06:59:46 +00:00
template <class T>
class AlgorithmParametersTemplate : public AlgorithmParametersBase
{
public:
2015-12-17 06:37:01 +00:00
//! \brief Construct an AlgorithmParametersTemplate
//! \param name the name of the value
//! \param value a reference to the value
//! \param throwIfNotUsed flags indicating whether an exception should be thrown
//! \details If throwIfNotUsed is true, then a ParameterNotUsed exception
//! will be thrown in the destructor if the parameter is not not retrieved.
2015-11-05 06:59:46 +00:00
AlgorithmParametersTemplate(const char *name, const T &value, bool throwIfNotUsed)
: AlgorithmParametersBase(name, throwIfNotUsed), m_value(value)
{
}
void AssignValue(const char *name, const std::type_info &valueType, void *pValue) const
{
#ifndef CRYPTOPP_NO_ASSIGN_TO_INTEGER
// Special case for retrieving an Integer parameter when an int was passed in
if (!(typeid(T) == typeid(int) && AssignIntToInteger(valueType, pValue, &m_value)))
#endif
2015-11-05 06:59:46 +00:00
{
NameValuePairs::ThrowIfTypeMismatch(name, typeid(T), valueType);
*reinterpret_cast<T *>(pValue) = m_value;
}
}
#if defined(DEBUG_NEW) && (_MSC_VER >= 1300)
# pragma push_macro("new")
# undef new
#endif
2015-11-05 06:59:46 +00:00
void MoveInto(void *buffer) const
{
AlgorithmParametersTemplate<T>* p = new(buffer) AlgorithmParametersTemplate<T>(*this);
CRYPTOPP_UNUSED(p); // silence warning
}
#if defined(DEBUG_NEW) && (_MSC_VER >= 1300)
# pragma pop_macro("new")
#endif
2015-11-05 06:59:46 +00:00
protected:
T m_value;
};
CRYPTOPP_DLL_TEMPLATE_CLASS AlgorithmParametersTemplate<bool>;
CRYPTOPP_DLL_TEMPLATE_CLASS AlgorithmParametersTemplate<int>;
CRYPTOPP_DLL_TEMPLATE_CLASS AlgorithmParametersTemplate<ConstByteArrayParameter>;
//! \class AlgorithmParameters
//! \brief An object that implements NameValuePairs
//! \note A NameValuePairs object containing an arbitrary number of name value pairs may be constructed by
//! repeatedly using operator() on the object returned by MakeParameters, for example:
//! <pre>
//! AlgorithmParameters parameters = MakeParameters(name1, value1)(name2, value2)(name3, value3);
//! </pre>
2015-11-05 06:59:46 +00:00
class CRYPTOPP_DLL AlgorithmParameters : public NameValuePairs
{
public:
//! \brief Construct a AlgorithmParameters
//! \note A NameValuePairs object containing an arbitrary number of name value pairs may be constructed by
//! repeatedly using operator() on the object returned by MakeParameters, for example:
//! <pre>
//! AlgorithmParameters parameters = MakeParameters(name1, value1)(name2, value2)(name3, value3);
//! </pre>
2015-11-05 06:59:46 +00:00
AlgorithmParameters();
#ifdef __BORLANDC__
//! \brief Construct a AlgorithmParameters
//! \tparam T the class or type
//! \param name the name of the object or value to retrieve
//! \param value reference to a variable that receives the value
//! \param throwIfNotUsed if true, the object will throw an exception if the value is not accessed
//! \note throwIfNotUsed is ignored if using a compiler that does not support std::uncaught_exception(),
//! such as MSVC 7.0 and earlier.
//! \note A NameValuePairs object containing an arbitrary number of name value pairs may be constructed by
//! repeatedly using operator() on the object returned by MakeParameters, for example:
//! <pre>
//! AlgorithmParameters parameters = MakeParameters(name1, value1)(name2, value2)(name3, value3);
//! </pre>
2015-11-05 06:59:46 +00:00
template <class T>
AlgorithmParameters(const char *name, const T &value, bool throwIfNotUsed=true)
: m_next(new AlgorithmParametersTemplate<T>(name, value, throwIfNotUsed))
, m_defaultThrowIfNotUsed(throwIfNotUsed)
{
}
#endif
AlgorithmParameters(const AlgorithmParameters &x);
AlgorithmParameters & operator=(const AlgorithmParameters &x);
//! \tparam T the class or type
//! \param name the name of the object or value to retrieve
//! \param value reference to a variable that receives the value
//! \param throwIfNotUsed if true, the object will throw an exception if the value is not accessed
2015-11-05 06:59:46 +00:00
template <class T>
AlgorithmParameters & operator()(const char *name, const T &value, bool throwIfNotUsed)
{
member_ptr<AlgorithmParametersBase> p(new AlgorithmParametersTemplate<T>(name, value, throwIfNotUsed));
p->m_next.reset(m_next.release());
m_next.reset(p.release());
m_defaultThrowIfNotUsed = throwIfNotUsed;
return *this;
}
//! \brief Appends a NameValuePair to a collection of NameValuePairs
//! \tparam T the class or type
//! \param name the name of the object or value to retrieve
//! \param value reference to a variable that receives the value
2015-11-05 06:59:46 +00:00
template <class T>
AlgorithmParameters & operator()(const char *name, const T &value)
{
return operator()(name, value, m_defaultThrowIfNotUsed);
}
bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const;
protected:
member_ptr<AlgorithmParametersBase> m_next;
bool m_defaultThrowIfNotUsed;
};
//! \brief Create an object that implements NameValuePairs
//! \tparam T the class or type
//! \param name the name of the object or value to retrieve
//! \param value reference to a variable that receives the value
//! \param throwIfNotUsed if true, the object will throw an exception if the value is not accessed
//! \note throwIfNotUsed is ignored if using a compiler that does not support std::uncaught_exception(),
//! such as MSVC 7.0 and earlier.
//! \note A NameValuePairs object containing an arbitrary number of name value pairs may be constructed by
//! repeatedly using \p operator() on the object returned by \p MakeParameters, for example:
//! <pre>
//! AlgorithmParameters parameters = MakeParameters(name1, value1)(name2, value2)(name3, value3);
//! </pre>
2015-11-05 06:59:46 +00:00
#ifdef __BORLANDC__
typedef AlgorithmParameters MakeParameters;
#else
template <class T>
AlgorithmParameters MakeParameters(const char *name, const T &value, bool throwIfNotUsed = true)
{
return AlgorithmParameters()(name, value, throwIfNotUsed);
}
#endif
#define CRYPTOPP_GET_FUNCTION_ENTRY(name) (Name::name(), &ThisClass::Get##name)
#define CRYPTOPP_SET_FUNCTION_ENTRY(name) (Name::name(), &ThisClass::Set##name)
#define CRYPTOPP_SET_FUNCTION_ENTRY2(name1, name2) (Name::name1(), Name::name2(), &ThisClass::Set##name1##And##name2)
2015-11-23 00:17:15 +00:00
// TODO: fix 6011 when the API/ABI can change
#if (CRYPTOPP_MSC_VERSION >= 1400)
# pragma warning(pop)
#endif
2015-11-05 06:59:46 +00:00
NAMESPACE_END
#endif