Crypto++  5.6.5
Free C++ class library of cryptographic schemes
algparam.h
Go to the documentation of this file.
1 // algparam.h - 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 = NULL, bool deepCopy = false)
38  : m_deepCopy(false), m_data(NULL), 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(NULL), 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(NULL), 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 = NULL, 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=NULL, BASE *dummy=NULL)
225 {
226  CRYPTOPP_UNUSED(dummy);
227  return GetValueHelperClass<T, BASE>(pObject, name, valueType, pValue, searchFirst);
228 }
229 
230 template <class T>
231 GetValueHelperClass<T, T> GetValueHelper(const T *pObject, const char *name, const std::type_info &valueType, void *pValue, const NameValuePairs *searchFirst=NULL)
232 {
233  return GetValueHelperClass<T, T>(pObject, name, valueType, pValue, searchFirst);
234 }
235 
236 // ********************************************************
237 
238 template <class T, class BASE>
239 class AssignFromHelperClass
240 {
241 public:
242  AssignFromHelperClass(T *pObject, const NameValuePairs &source)
243  : m_pObject(pObject), m_source(source), m_done(false)
244  {
245  if (source.GetThisObject(*pObject))
246  m_done = true;
247  else if (typeid(BASE) != typeid(T))
248  pObject->BASE::AssignFrom(source);
249  }
250 
251  template <class R>
252  AssignFromHelperClass & operator()(const char *name, void (T::*pm)(const R&))
253  {
254  if (!m_done)
255  {
256  R value;
257  if (!m_source.GetValue(name, value))
258  throw InvalidArgument(std::string(typeid(T).name()) + ": Missing required parameter '" + name + "'");
259  (m_pObject->*pm)(value);
260  }
261  return *this;
262  }
263 
264  template <class R, class S>
265  AssignFromHelperClass & operator()(const char *name1, const char *name2, void (T::*pm)(const R&, const S&))
266  {
267  if (!m_done)
268  {
269  R value1;
270  if (!m_source.GetValue(name1, value1))
271  throw InvalidArgument(std::string(typeid(T).name()) + ": Missing required parameter '" + name1 + "'");
272  S value2;
273  if (!m_source.GetValue(name2, value2))
274  throw InvalidArgument(std::string(typeid(T).name()) + ": Missing required parameter '" + name2 + "'");
275  (m_pObject->*pm)(value1, value2);
276  }
277  return *this;
278  }
279 
280 private:
281  T *m_pObject;
282  const NameValuePairs &m_source;
283  bool m_done;
284 };
285 
286 template <class BASE, class T>
287 AssignFromHelperClass<T, BASE> AssignFromHelper(T *pObject, const NameValuePairs &source, BASE *dummy=NULL)
288 {
289  CRYPTOPP_UNUSED(dummy);
290  return AssignFromHelperClass<T, BASE>(pObject, source);
291 }
292 
293 template <class T>
294 AssignFromHelperClass<T, T> AssignFromHelper(T *pObject, const NameValuePairs &source)
295 {
296  return AssignFromHelperClass<T, T>(pObject, source);
297 }
298 
299 #endif // CRYPTOPP_DOXYGEN_PROCESSING
300 
301 // ********************************************************
302 
303 // to allow the linker to discard Integer code if not needed.
304 typedef bool (CRYPTOPP_API * PAssignIntToInteger)(const std::type_info &valueType, void *pInteger, const void *pInt);
305 CRYPTOPP_DLL extern PAssignIntToInteger g_pAssignIntToInteger;
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  // special case for retrieving an Integer parameter when an int was passed in
392  if (!(g_pAssignIntToInteger != NULL && typeid(T) == typeid(int) && g_pAssignIntToInteger(valueType, pValue, &m_value)))
393  {
394  NameValuePairs::ThrowIfTypeMismatch(name, typeid(T), valueType);
395  *reinterpret_cast<T *>(pValue) = m_value;
396  }
397  }
398 
399 #if defined(DEBUG_NEW) && (_MSC_VER >= 1300)
400 # pragma push_macro("new")
401 # undef new
402 #endif
403 
404  void MoveInto(void *buffer) const
405  {
407  CRYPTOPP_UNUSED(p); // silence warning
408  }
409 
410 #if defined(DEBUG_NEW) && (_MSC_VER >= 1300)
411 # pragma pop_macro("new")
412 #endif
413 
414 protected:
415  T m_value;
416 };
417 
418 CRYPTOPP_DLL_TEMPLATE_CLASS AlgorithmParametersTemplate<bool>;
419 CRYPTOPP_DLL_TEMPLATE_CLASS AlgorithmParametersTemplate<int>;
420 CRYPTOPP_DLL_TEMPLATE_CLASS AlgorithmParametersTemplate<ConstByteArrayParameter>;
421 
422 //! \class AlgorithmParameters
423 //! \brief An object that implements NameValuePairs
424 //! \tparam T the class or type
425 //! \param name the name of the object or value to retrieve
426 //! \param value reference to a variable that receives the value
427 //! \param throwIfNotUsed if true, the object will throw an exception if the value is not accessed
428 //! \note throwIfNotUsed is ignored if using a compiler that does not support std::uncaught_exception(),
429 //! such as MSVC 7.0 and earlier.
430 //! \note A NameValuePairs object containing an arbitrary number of name value pairs may be constructed by
431 //! repeatedly using operator() on the object returned by MakeParameters, for example:
432 //! <pre>
433 //! AlgorithmParameters parameters = MakeParameters(name1, value1)(name2, value2)(name3, value3);
434 //! </pre>
435 class CRYPTOPP_DLL AlgorithmParameters : public NameValuePairs
436 {
437 public:
439 
440 #ifdef __BORLANDC__
441  template <class T>
442  AlgorithmParameters(const char *name, const T &value, bool throwIfNotUsed=true)
443  : m_next(new AlgorithmParametersTemplate<T>(name, value, throwIfNotUsed))
444  , m_defaultThrowIfNotUsed(throwIfNotUsed)
445  {
446  }
447 #endif
448 
450 
451  AlgorithmParameters & operator=(const AlgorithmParameters &x);
452 
453  //! \tparam T the class or type
454  //! \param name the name of the object or value to retrieve
455  //! \param value reference to a variable that receives the value
456  //! \param throwIfNotUsed if true, the object will throw an exception if the value is not accessed
457  template <class T>
458  AlgorithmParameters & operator()(const char *name, const T &value, bool throwIfNotUsed)
459  {
460  member_ptr<AlgorithmParametersBase> p(new AlgorithmParametersTemplate<T>(name, value, throwIfNotUsed));
461  p->m_next.reset(m_next.release());
462  m_next.reset(p.release());
463  m_defaultThrowIfNotUsed = throwIfNotUsed;
464  return *this;
465  }
466 
467  //! \brief Appends a NameValuePair to a collection of NameValuePairs
468  //! \tparam T the class or type
469  //! \param name the name of the object or value to retrieve
470  //! \param value reference to a variable that receives the value
471  template <class T>
472  AlgorithmParameters & operator()(const char *name, const T &value)
473  {
474  return operator()(name, value, m_defaultThrowIfNotUsed);
475  }
476 
477  bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const;
478 
479 protected:
481  bool m_defaultThrowIfNotUsed;
482 };
483 
484 //! \brief Create an object that implements NameValuePairs
485 //! \tparam T the class or type
486 //! \param name the name of the object or value to retrieve
487 //! \param value reference to a variable that receives the value
488 //! \param throwIfNotUsed if true, the object will throw an exception if the value is not accessed
489 //! \note throwIfNotUsed is ignored if using a compiler that does not support std::uncaught_exception(),
490 //! such as MSVC 7.0 and earlier.
491 //! \note A NameValuePairs object containing an arbitrary number of name value pairs may be constructed by
492 //! repeatedly using \p operator() on the object returned by \p MakeParameters, for example:
493 //! <pre>
494 //! AlgorithmParameters parameters = MakeParameters(name1, value1)(name2, value2)(name3, value3);
495 //! </pre>
496 #ifdef __BORLANDC__
498 #else
499 template <class T>
500 AlgorithmParameters MakeParameters(const char *name, const T &value, bool throwIfNotUsed = true)
501 {
502  return AlgorithmParameters()(name, value, throwIfNotUsed);
503 }
504 #endif
505 
506 #define CRYPTOPP_GET_FUNCTION_ENTRY(name) (Name::name(), &ThisClass::Get##name)
507 #define CRYPTOPP_SET_FUNCTION_ENTRY(name) (Name::name(), &ThisClass::Set##name)
508 #define CRYPTOPP_SET_FUNCTION_ENTRY2(name1, name2) (Name::name1(), Name::name2(), &ThisClass::Set##name1##And##name2)
509 
510 // TODO: fix 6011 when the API/ABI can change
511 #if (CRYPTOPP_MSC_VERSION >= 1400)
512 # pragma warning(pop)
513 #endif
514 
515 NAMESPACE_END
516 
517 #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:140
An invalid argument was detected.
Definition: cryptlib.h:184
bool GetThisObject(T &object) const
Get a copy of this object or subobject.
Definition: cryptlib.h:315
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
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
Abstract base classes that provide a uniform interface to this library.
virtual bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const =0
Get a named value.
Classes for automatic resource management.
size_t size() const
Length of the memory block.
Definition: algparam.h:93
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 typedef.
Definition: secblock.h:731
Used to pass byte array input as part of a NameValuePairs object.
Definition: algparam.h:104
const byte * begin() const
Pointer to the first byte in the memory block.
Definition: algparam.h:89
Classes and functions for secure memory allocations.
AlgorithmParameters MakeParameters(const char *name, const T &value, bool throwIfNotUsed=true)
Create an object that implements NameValuePairs.
Definition: algparam.h:500
#define CRYPTOPP_COMPILE_ASSERT(expr)
Compile time assertion.
Definition: misc.h:130
Exception thrown when an AlgorithmParameter is unused.
Definition: algparam.h:316
byte * begin() const
Pointer to the first byte in the memory block.
Definition: algparam.h:119
void Assign(const byte *data, size_t size, bool deepCopy)
Assign contents from a memory buffer.
Definition: algparam.h:74
Template base class for AlgorithmParameters.
Definition: algparam.h:375
size_t size() const
Length of the memory block.
Definition: algparam.h:123
AlgorithmParameters & operator()(const char *name, const T &value)
Appends a NameValuePair to a collection of NameValuePairs.
Definition: algparam.h:472
const byte * end() const
Pointer beyond the last byte in the memory block.
Definition: algparam.h:91
byte * end() const
Pointer beyond the last byte in the memory block.
Definition: algparam.h:121
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:394
AlgorithmParameters & operator()(const char *name, const T &value, bool throwIfNotUsed)
Definition: algparam.h:458
An object that implements NameValuePairs.
Definition: algparam.h:435
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:279