Crypto++  5.6.5
Free C++ class library of cryptographic schemes
algparam.h
Go to the documentation of this file.
1 // algparam.h - originally written and placed in the public domain by Wei Dai
2 
3 //! \file
4 //! \headerfile algparam.h
5 //! \brief Classes for working with NameValuePairs
6 
7 
8 #ifndef CRYPTOPP_ALGPARAM_H
9 #define CRYPTOPP_ALGPARAM_H
10 
11 #include "config.h"
12 #include "cryptlib.h"
13 
14 // TODO: fix 6011 when the API/ABI can change
15 #if (CRYPTOPP_MSC_VERSION >= 1400)
16 # pragma warning(push)
17 # pragma warning(disable: 6011 28193)
18 #endif
19 
20 #include "smartptr.h"
21 #include "secblock.h"
22 #include "integer.h"
23 #include "misc.h"
24 
25 NAMESPACE_BEGIN(CryptoPP)
26 
27 //! \class ConstByteArrayParameter
28 //! \brief Used to pass byte array input as part of a NameValuePairs object
30 {
31 public:
32  //! \brief Construct a ConstByteArrayParameter
33  //! \param data a C-String
34  //! \param deepCopy flag indicating whether the data should be copied
35  //! \details The deepCopy option is used when the NameValuePairs object can't
36  //! keep a copy of the data available
37  ConstByteArrayParameter(const char *data = NULLPTR, bool deepCopy = false)
38  : m_deepCopy(false), m_data(NULLPTR), m_size(0)
39  {
40  Assign((const byte *)data, data ? strlen(data) : 0, deepCopy);
41  }
42 
43  //! \brief Construct a ConstByteArrayParameter
44  //! \param data a memory buffer
45  //! \param size the length of the memory buffer
46  //! \param deepCopy flag indicating whether the data should be copied
47  //! \details The deepCopy option is used when the NameValuePairs object can't
48  //! keep a copy of the data available
49  ConstByteArrayParameter(const byte *data, size_t size, bool deepCopy = false)
50  : m_deepCopy(false), m_data(NULLPTR), m_size(0)
51  {
52  Assign(data, size, deepCopy);
53  }
54 
55  //! \brief Construct a ConstByteArrayParameter
56  //! \tparam T a std::basic_string<char> class
57  //! \param string a std::basic_string<char> class
58  //! \param deepCopy flag indicating whether the data should be copied
59  //! \details The deepCopy option is used when the NameValuePairs object can't
60  //! keep a copy of the data available
61  template <class T> ConstByteArrayParameter(const T &string, bool deepCopy = false)
62  : m_deepCopy(false), m_data(NULLPTR), m_size(0)
63  {
64  CRYPTOPP_COMPILE_ASSERT(sizeof(typename T::value_type) == 1);
65  Assign((const byte *)string.data(), string.size(), deepCopy);
66  }
67 
68  //! \brief Assign contents from a memory buffer
69  //! \param data a memory buffer
70  //! \param size the length of the memory buffer
71  //! \param deepCopy flag indicating whether the data should be copied
72  //! \details The deepCopy option is used when the NameValuePairs object can't
73  //! keep a copy of the data available
74  void Assign(const byte *data, size_t size, bool deepCopy)
75  {
76  // This fires, which means: no data with a size, or data with no size.
77  // CRYPTOPP_ASSERT((data && size) || !(data || size));
78  if (deepCopy)
79  m_block.Assign(data, size);
80  else
81  {
82  m_data = data;
83  m_size = size;
84  }
85  m_deepCopy = deepCopy;
86  }
87 
88  //! \brief Pointer to the first byte in the memory block
89  const byte *begin() const {return m_deepCopy ? m_block.begin() : m_data;}
90  //! \brief Pointer beyond the last byte in the memory block
91  const byte *end() const {return m_deepCopy ? m_block.end() : m_data + m_size;}
92  //! \brief Length of the memory block
93  size_t size() const {return m_deepCopy ? m_block.size() : m_size;}
94 
95 private:
96  bool m_deepCopy;
97  const byte *m_data;
98  size_t m_size;
99  SecByteBlock m_block;
100 };
101 
102 //! \class ByteArrayParameter
103 //! \brief Used to pass byte array input as part of a NameValuePairs object
105 {
106 public:
107  //! \brief Construct a ByteArrayParameter
108  //! \param data a memory buffer
109  //! \param size the length of the memory buffer
110  ByteArrayParameter(byte *data = NULLPTR, unsigned int size = 0)
111  : m_data(data), m_size(size) {}
112 
113  //! \brief Construct a ByteArrayParameter
114  //! \param block a SecByteBlock
116  : m_data(block.begin()), m_size(block.size()) {}
117 
118  //! \brief Pointer to the first byte in the memory block
119  byte *begin() const {return m_data;}
120  //! \brief Pointer beyond the last byte in the memory block
121  byte *end() const {return m_data + m_size;}
122  //! \brief Length of the memory block
123  size_t size() const {return m_size;}
124 
125 private:
126  byte *m_data;
127  size_t m_size;
128 };
129 
130 //! \class CombinedNameValuePairs
131 //! \brief Combines two sets of NameValuePairs
132 //! \details CombinedNameValuePairs allows you to provide two sets of of NameValuePairs.
133 //! If a name is not found in the first set, then the second set is searched for the
134 //! name and value pair. The second set of NameValuePairs often provides default values.
135 class CRYPTOPP_DLL CombinedNameValuePairs : public NameValuePairs
136 {
137 public:
138  //! \brief Construct a CombinedNameValuePairs
139  //! \param pairs1 reference to the first set of NameValuePairs
140  //! \param pairs2 reference to the second set of NameValuePairs
142  : m_pairs1(pairs1), m_pairs2(pairs2) {}
143 
144  bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const;
145 
146 private:
147  const NameValuePairs &m_pairs1, &m_pairs2;
148 };
149 
150 #ifndef CRYPTOPP_DOXYGEN_PROCESSING
151 template <class T, class BASE>
152 class GetValueHelperClass
153 {
154 public:
155  GetValueHelperClass(const T *pObject, const char *name, const std::type_info &valueType, void *pValue, const NameValuePairs *searchFirst)
156  : m_pObject(pObject), m_name(name), m_valueType(&valueType), m_pValue(pValue), m_found(false), m_getValueNames(false)
157  {
158  if (strcmp(m_name, "ValueNames") == 0)
159  {
160  m_found = m_getValueNames = true;
161  NameValuePairs::ThrowIfTypeMismatch(m_name, typeid(std::string), *m_valueType);
162  if (searchFirst)
163  searchFirst->GetVoidValue(m_name, valueType, pValue);
164  if (typeid(T) != typeid(BASE))
165  pObject->BASE::GetVoidValue(m_name, valueType, pValue);
166  ((*reinterpret_cast<std::string *>(m_pValue) += "ThisPointer:") += typeid(T).name()) += ';';
167  }
168 
169  if (!m_found && strncmp(m_name, "ThisPointer:", 12) == 0 && strcmp(m_name+12, typeid(T).name()) == 0)
170  {
171  NameValuePairs::ThrowIfTypeMismatch(m_name, typeid(T *), *m_valueType);
172  *reinterpret_cast<const T **>(pValue) = pObject;
173  m_found = true;
174  return;
175  }
176 
177  if (!m_found && searchFirst)
178  m_found = searchFirst->GetVoidValue(m_name, valueType, pValue);
179 
180  if (!m_found && typeid(T) != typeid(BASE))
181  m_found = pObject->BASE::GetVoidValue(m_name, valueType, pValue);
182  }
183 
184  operator bool() const {return m_found;}
185 
186  template <class R>
187  GetValueHelperClass<T,BASE> & operator()(const char *name, const R & (T::*pm)() const)
188  {
189  if (m_getValueNames)
190  (*reinterpret_cast<std::string *>(m_pValue) += name) += ";";
191  if (!m_found && strcmp(name, m_name) == 0)
192  {
193  NameValuePairs::ThrowIfTypeMismatch(name, typeid(R), *m_valueType);
194  *reinterpret_cast<R *>(m_pValue) = (m_pObject->*pm)();
195  m_found = true;
196  }
197  return *this;
198  }
199 
200  GetValueHelperClass<T,BASE> &Assignable()
201  {
202 #ifndef __INTEL_COMPILER // ICL 9.1 workaround: Intel compiler copies the vTable pointer for some reason
203  if (m_getValueNames)
204  ((*reinterpret_cast<std::string *>(m_pValue) += "ThisObject:") += typeid(T).name()) += ';';
205  if (!m_found && strncmp(m_name, "ThisObject:", 11) == 0 && strcmp(m_name+11, typeid(T).name()) == 0)
206  {
207  NameValuePairs::ThrowIfTypeMismatch(m_name, typeid(T), *m_valueType);
208  *reinterpret_cast<T *>(m_pValue) = *m_pObject;
209  m_found = true;
210  }
211 #endif
212  return *this;
213  }
214 
215 private:
216  const T *m_pObject;
217  const char *m_name;
218  const std::type_info *m_valueType;
219  void *m_pValue;
220  bool m_found, m_getValueNames;
221 };
222 
223 template <class BASE, class T>
224 GetValueHelperClass<T, BASE> GetValueHelper(const T *pObject, const char *name, const std::type_info &valueType, void *pValue, const NameValuePairs *searchFirst=NULLPTR)
225 {
226  return GetValueHelperClass<T, BASE>(pObject, name, valueType, pValue, searchFirst);
227 }
228 
229 template <class T>
230 GetValueHelperClass<T, T> GetValueHelper(const T *pObject, const char *name, const std::type_info &valueType, void *pValue, const NameValuePairs *searchFirst=NULLPTR)
231 {
232  return GetValueHelperClass<T, T>(pObject, name, valueType, pValue, searchFirst);
233 }
234 
235 // ********************************************************
236 
237 template <class T, class BASE>
238 class AssignFromHelperClass
239 {
240 public:
241  AssignFromHelperClass(T *pObject, const NameValuePairs &source)
242  : m_pObject(pObject), m_source(source), m_done(false)
243  {
244  if (source.GetThisObject(*pObject))
245  m_done = true;
246  else if (typeid(BASE) != typeid(T))
247  pObject->BASE::AssignFrom(source);
248  }
249 
250  template <class R>
251  AssignFromHelperClass & operator()(const char *name, void (T::*pm)(const R&))
252  {
253  if (!m_done)
254  {
255  R value;
256  if (!m_source.GetValue(name, value))
257  throw InvalidArgument(std::string(typeid(T).name()) + ": Missing required parameter '" + name + "'");
258  (m_pObject->*pm)(value);
259  }
260  return *this;
261  }
262 
263  template <class R, class S>
264  AssignFromHelperClass & operator()(const char *name1, const char *name2, void (T::*pm)(const R&, const S&))
265  {
266  if (!m_done)
267  {
268  R value1;
269  if (!m_source.GetValue(name1, value1))
270  throw InvalidArgument(std::string(typeid(T).name()) + ": Missing required parameter '" + name1 + "'");
271  S value2;
272  if (!m_source.GetValue(name2, value2))
273  throw InvalidArgument(std::string(typeid(T).name()) + ": Missing required parameter '" + name2 + "'");
274  (m_pObject->*pm)(value1, value2);
275  }
276  return *this;
277  }
278 
279 private:
280  T *m_pObject;
281  const NameValuePairs &m_source;
282  bool m_done;
283 };
284 
285 template <class BASE, class T>
286 AssignFromHelperClass<T, BASE> AssignFromHelper(T *pObject, const NameValuePairs &source)
287 {
288  return AssignFromHelperClass<T, BASE>(pObject, source);
289 }
290 
291 template <class T>
292 AssignFromHelperClass<T, T> AssignFromHelper(T *pObject, const NameValuePairs &source)
293 {
294  return AssignFromHelperClass<T, T>(pObject, source);
295 }
296 
297 #endif // CRYPTOPP_DOXYGEN_PROCESSING
298 
299 // ********************************************************
300 
301 #ifndef CRYPTOPP_NO_ASSIGN_TO_INTEGER
302 // Allow the linker to discard Integer code if not needed.
303 // Also see http://github.com/weidai11/cryptopp/issues/389.
304 bool AssignIntToInteger(const std::type_info &valueType, void *pInteger, const void *pInt);
305 #endif
306 
307 CRYPTOPP_DLL const std::type_info & CRYPTOPP_API IntegerTypeId();
308 
309 //! \class AlgorithmParametersBase
310 //! \brief Base class for AlgorithmParameters
311 class CRYPTOPP_DLL AlgorithmParametersBase
312 {
313 public:
314  //! \class ParameterNotUsed
315  //! \brief Exception thrown when an AlgorithmParameter is unused
317  {
318  public:
319  ParameterNotUsed(const char *name) : Exception(OTHER_ERROR, std::string("AlgorithmParametersBase: parameter \"") + name + "\" not used") {}
320  };
321 
322  virtual ~AlgorithmParametersBase() CRYPTOPP_THROW
323  {
324 #ifdef CRYPTOPP_UNCAUGHT_EXCEPTION_AVAILABLE
325  if (!std::uncaught_exception())
326 #else
327  try
328 #endif
329  {
330  if (m_throwIfNotUsed && !m_used)
331  throw ParameterNotUsed(m_name);
332  }
333 #ifndef CRYPTOPP_UNCAUGHT_EXCEPTION_AVAILABLE
334  catch(const Exception&)
335  {
336  }
337 #endif
338  }
339 
340  // this is actually a move, not a copy
342  : m_name(x.m_name), m_throwIfNotUsed(x.m_throwIfNotUsed), m_used(x.m_used)
343  {
344  m_next.reset(const_cast<AlgorithmParametersBase &>(x).m_next.release());
345  x.m_used = true;
346  }
347 
348  //! \brief Construct a AlgorithmParametersBase
349  //! \param name the parameter name
350  //! \param throwIfNotUsed flags indicating whether an exception should be thrown
351  //! \details If throwIfNotUsed is true, then a ParameterNotUsed exception
352  //! will be thrown in the destructor if the parameter is not not retrieved.
353  AlgorithmParametersBase(const char *name, bool throwIfNotUsed)
354  : m_name(name), m_throwIfNotUsed(throwIfNotUsed), m_used(false) {}
355 
356  bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const;
357 
358 protected:
359  friend class AlgorithmParameters;
360  void operator=(const AlgorithmParametersBase& rhs); // assignment not allowed, declare this for VC60
361 
362  virtual void AssignValue(const char *name, const std::type_info &valueType, void *pValue) const =0;
363  virtual void MoveInto(void *p) const =0; // not really const
364 
365  const char *m_name;
366  bool m_throwIfNotUsed;
367  mutable bool m_used;
369 };
370 
371 //! \class AlgorithmParametersTemplate
372 //! \brief Template base class for AlgorithmParameters
373 //! \tparam T the class or type
374 template <class T>
376 {
377 public:
378  //! \brief Construct an AlgorithmParametersTemplate
379  //! \param name the name of the value
380  //! \param value a reference to the value
381  //! \param throwIfNotUsed flags indicating whether an exception should be thrown
382  //! \details If throwIfNotUsed is true, then a ParameterNotUsed exception
383  //! will be thrown in the destructor if the parameter is not not retrieved.
384  AlgorithmParametersTemplate(const char *name, const T &value, bool throwIfNotUsed)
385  : AlgorithmParametersBase(name, throwIfNotUsed), m_value(value)
386  {
387  }
388 
389  void AssignValue(const char *name, const std::type_info &valueType, void *pValue) const
390  {
391 #ifndef CRYPTOPP_NO_ASSIGN_TO_INTEGER
392  // Special case for retrieving an Integer parameter when an int was passed in
393  if (!(typeid(T) == typeid(int) && AssignIntToInteger(valueType, pValue, &m_value)))
394 #endif
395  {
396  NameValuePairs::ThrowIfTypeMismatch(name, typeid(T), valueType);
397  *reinterpret_cast<T *>(pValue) = m_value;
398  }
399  }
400 
401 #if defined(DEBUG_NEW) && (_MSC_VER >= 1300)
402 # pragma push_macro("new")
403 # undef new
404 #endif
405 
406  void MoveInto(void *buffer) const
407  {
409  CRYPTOPP_UNUSED(p); // silence warning
410  }
411 
412 #if defined(DEBUG_NEW) && (_MSC_VER >= 1300)
413 # pragma pop_macro("new")
414 #endif
415 
416 protected:
417  T m_value;
418 };
419 
420 CRYPTOPP_DLL_TEMPLATE_CLASS AlgorithmParametersTemplate<bool>;
421 CRYPTOPP_DLL_TEMPLATE_CLASS AlgorithmParametersTemplate<int>;
422 CRYPTOPP_DLL_TEMPLATE_CLASS AlgorithmParametersTemplate<ConstByteArrayParameter>;
423 
424 //! \class AlgorithmParameters
425 //! \brief An object that implements NameValuePairs
426 //! \tparam T the class or type
427 //! \param name the name of the object or value to retrieve
428 //! \param value reference to a variable that receives the value
429 //! \param throwIfNotUsed if true, the object will throw an exception if the value is not accessed
430 //! \note throwIfNotUsed is ignored if using a compiler that does not support std::uncaught_exception(),
431 //! such as MSVC 7.0 and earlier.
432 //! \note A NameValuePairs object containing an arbitrary number of name value pairs may be constructed by
433 //! repeatedly using operator() on the object returned by MakeParameters, for example:
434 //! <pre>
435 //! AlgorithmParameters parameters = MakeParameters(name1, value1)(name2, value2)(name3, value3);
436 //! </pre>
437 class CRYPTOPP_DLL AlgorithmParameters : public NameValuePairs
438 {
439 public:
441 
442 #ifdef __BORLANDC__
443  template <class T>
444  AlgorithmParameters(const char *name, const T &value, bool throwIfNotUsed=true)
445  : m_next(new AlgorithmParametersTemplate<T>(name, value, throwIfNotUsed))
446  , m_defaultThrowIfNotUsed(throwIfNotUsed)
447  {
448  }
449 #endif
450 
452 
453  AlgorithmParameters & operator=(const AlgorithmParameters &x);
454 
455  //! \tparam T the class or type
456  //! \param name the name of the object or value to retrieve
457  //! \param value reference to a variable that receives the value
458  //! \param throwIfNotUsed if true, the object will throw an exception if the value is not accessed
459  template <class T>
460  AlgorithmParameters & operator()(const char *name, const T &value, bool throwIfNotUsed)
461  {
462  member_ptr<AlgorithmParametersBase> p(new AlgorithmParametersTemplate<T>(name, value, throwIfNotUsed));
463  p->m_next.reset(m_next.release());
464  m_next.reset(p.release());
465  m_defaultThrowIfNotUsed = throwIfNotUsed;
466  return *this;
467  }
468 
469  //! \brief Appends a NameValuePair to a collection of NameValuePairs
470  //! \tparam T the class or type
471  //! \param name the name of the object or value to retrieve
472  //! \param value reference to a variable that receives the value
473  template <class T>
474  AlgorithmParameters & operator()(const char *name, const T &value)
475  {
476  return operator()(name, value, m_defaultThrowIfNotUsed);
477  }
478 
479  bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const;
480 
481 protected:
483  bool m_defaultThrowIfNotUsed;
484 };
485 
486 //! \brief Create an object that implements NameValuePairs
487 //! \tparam T the class or type
488 //! \param name the name of the object or value to retrieve
489 //! \param value reference to a variable that receives the value
490 //! \param throwIfNotUsed if true, the object will throw an exception if the value is not accessed
491 //! \note throwIfNotUsed is ignored if using a compiler that does not support std::uncaught_exception(),
492 //! such as MSVC 7.0 and earlier.
493 //! \note A NameValuePairs object containing an arbitrary number of name value pairs may be constructed by
494 //! repeatedly using \p operator() on the object returned by \p MakeParameters, for example:
495 //! <pre>
496 //! AlgorithmParameters parameters = MakeParameters(name1, value1)(name2, value2)(name3, value3);
497 //! </pre>
498 #ifdef __BORLANDC__
500 #else
501 template <class T>
502 AlgorithmParameters MakeParameters(const char *name, const T &value, bool throwIfNotUsed = true)
503 {
504  return AlgorithmParameters()(name, value, throwIfNotUsed);
505 }
506 #endif
507 
508 #define CRYPTOPP_GET_FUNCTION_ENTRY(name) (Name::name(), &ThisClass::Get##name)
509 #define CRYPTOPP_SET_FUNCTION_ENTRY(name) (Name::name(), &ThisClass::Set##name)
510 #define CRYPTOPP_SET_FUNCTION_ENTRY2(name1, name2) (Name::name1(), Name::name2(), &ThisClass::Set##name1##And##name2)
511 
512 // TODO: fix 6011 when the API/ABI can change
513 #if (CRYPTOPP_MSC_VERSION >= 1400)
514 # pragma warning(pop)
515 #endif
516 
517 NAMESPACE_END
518 
519 #endif
Used to pass byte array input as part of a NameValuePairs object.
Definition: algparam.h:29
Base class for all exceptions thrown by the library.
Definition: cryptlib.h:150
An invalid argument was detected.
Definition: cryptlib.h:194
CombinedNameValuePairs(const NameValuePairs &pairs1, const NameValuePairs &pairs2)
Construct a CombinedNameValuePairs.
Definition: algparam.h:141
Utility functions for the Crypto++ library.
Base class for AlgorithmParameters.
Definition: algparam.h:311
size_t size() const
Length of the memory block.
Definition: algparam.h:123
size_t size() const
Length of the memory block.
Definition: algparam.h:93
ConstByteArrayParameter(const T &string, bool deepCopy=false)
Construct a ConstByteArrayParameter.
Definition: algparam.h:61
AlgorithmParametersBase(const char *name, bool throwIfNotUsed)
Construct a AlgorithmParametersBase.
Definition: algparam.h:353
ConstByteArrayParameter(const byte *data, size_t size, bool deepCopy=false)
Construct a ConstByteArrayParameter.
Definition: algparam.h:49
bool GetThisObject(T &object) const
Get a copy of this object or subobject.
Definition: cryptlib.h:321
Abstract base classes that provide a uniform interface to this library.
Classes for automatic resource management.
Library configuration file.
ConstByteArrayParameter(const char *data=NULL, bool deepCopy=false)
Construct a ConstByteArrayParameter.
Definition: algparam.h:37
Combines two sets of NameValuePairs.
Definition: algparam.h:135
SecBlock<byte> typedef.
Definition: secblock.h:810
Used to pass byte array input as part of a NameValuePairs object.
Definition: algparam.h:104
Classes and functions for secure memory allocations.
const byte * begin() const
Pointer to the first byte in the memory block.
Definition: algparam.h:89
AlgorithmParameters MakeParameters(const char *name, const T &value, bool throwIfNotUsed=true)
Create an object that implements NameValuePairs.
Definition: algparam.h:502
#define CRYPTOPP_COMPILE_ASSERT(expr)
Compile time assertion.
Definition: misc.h:159
Exception thrown when an AlgorithmParameter is unused.
Definition: algparam.h:316
void Assign(const byte *data, size_t size, bool deepCopy)
Assign contents from a memory buffer.
Definition: algparam.h:74
const byte * end() const
Pointer beyond the last byte in the memory block.
Definition: algparam.h:91
virtual bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const =0
Get a named value.
Template base class for AlgorithmParameters.
Definition: algparam.h:375
AlgorithmParameters & operator()(const char *name, const T &value)
Appends a NameValuePair to a collection of NameValuePairs.
Definition: algparam.h:474
ByteArrayParameter(byte *data=NULL, unsigned int size=0)
Construct a ByteArrayParameter.
Definition: algparam.h:110
ByteArrayParameter(SecByteBlock &block)
Construct a ByteArrayParameter.
Definition: algparam.h:115
static void ThrowIfTypeMismatch(const char *name, const std::type_info &stored, const std::type_info &retrieving)
Ensures an expected name and type is present.
Definition: cryptlib.h:400
AlgorithmParameters & operator()(const char *name, const T &value, bool throwIfNotUsed)
Definition: algparam.h:460
byte * begin() const
Pointer to the first byte in the memory block.
Definition: algparam.h:119
An object that implements NameValuePairs.
Definition: algparam.h:437
Multiple precision integer with arithmetic operations.
AlgorithmParametersTemplate(const char *name, const T &value, bool throwIfNotUsed)
Construct an AlgorithmParametersTemplate.
Definition: algparam.h:384
Crypto++ library namespace.
Interface for retrieving values given their names.
Definition: cryptlib.h:285
byte * end() const
Pointer beyond the last byte in the memory block.
Definition: algparam.h:121