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)
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=NULL)
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 // to allow the linker to discard Integer code if not needed.
302 typedef bool (CRYPTOPP_API * PAssignIntToInteger)(const std::type_info &valueType, void *pInteger, const void *pInt);
303 CRYPTOPP_DLL extern PAssignIntToInteger g_pAssignIntToInteger;
304 
305 CRYPTOPP_DLL const std::type_info & CRYPTOPP_API IntegerTypeId();
306 
307 //! \class AlgorithmParametersBase
308 //! \brief Base class for AlgorithmParameters
309 class CRYPTOPP_DLL AlgorithmParametersBase
310 {
311 public:
312  //! \class ParameterNotUsed
313  //! \brief Exception thrown when an AlgorithmParameter is unused
315  {
316  public:
317  ParameterNotUsed(const char *name) : Exception(OTHER_ERROR, std::string("AlgorithmParametersBase: parameter \"") + name + "\" not used") {}
318  };
319 
320  virtual ~AlgorithmParametersBase() CRYPTOPP_THROW
321  {
322 #ifdef CRYPTOPP_UNCAUGHT_EXCEPTION_AVAILABLE
323  if (!std::uncaught_exception())
324 #else
325  try
326 #endif
327  {
328  if (m_throwIfNotUsed && !m_used)
329  throw ParameterNotUsed(m_name);
330  }
331 #ifndef CRYPTOPP_UNCAUGHT_EXCEPTION_AVAILABLE
332  catch(const Exception&)
333  {
334  }
335 #endif
336  }
337 
338  // this is actually a move, not a copy
340  : m_name(x.m_name), m_throwIfNotUsed(x.m_throwIfNotUsed), m_used(x.m_used)
341  {
342  m_next.reset(const_cast<AlgorithmParametersBase &>(x).m_next.release());
343  x.m_used = true;
344  }
345 
346  //! \brief Construct a AlgorithmParametersBase
347  //! \param name the parameter name
348  //! \param throwIfNotUsed flags indicating whether an exception should be thrown
349  //! \details If throwIfNotUsed is true, then a ParameterNotUsed exception
350  //! will be thrown in the destructor if the parameter is not not retrieved.
351  AlgorithmParametersBase(const char *name, bool throwIfNotUsed)
352  : m_name(name), m_throwIfNotUsed(throwIfNotUsed), m_used(false) {}
353 
354  bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const;
355 
356 protected:
357  friend class AlgorithmParameters;
358  void operator=(const AlgorithmParametersBase& rhs); // assignment not allowed, declare this for VC60
359 
360  virtual void AssignValue(const char *name, const std::type_info &valueType, void *pValue) const =0;
361  virtual void MoveInto(void *p) const =0; // not really const
362 
363  const char *m_name;
364  bool m_throwIfNotUsed;
365  mutable bool m_used;
367 };
368 
369 //! \class AlgorithmParametersTemplate
370 //! \brief Template base class for AlgorithmParameters
371 //! \tparam T the class or type
372 template <class T>
374 {
375 public:
376  //! \brief Construct an AlgorithmParametersTemplate
377  //! \param name the name of the value
378  //! \param value a reference to the value
379  //! \param throwIfNotUsed flags indicating whether an exception should be thrown
380  //! \details If throwIfNotUsed is true, then a ParameterNotUsed exception
381  //! will be thrown in the destructor if the parameter is not not retrieved.
382  AlgorithmParametersTemplate(const char *name, const T &value, bool throwIfNotUsed)
383  : AlgorithmParametersBase(name, throwIfNotUsed), m_value(value)
384  {
385  }
386 
387  void AssignValue(const char *name, const std::type_info &valueType, void *pValue) const
388  {
389  // special case for retrieving an Integer parameter when an int was passed in
390  if (!(g_pAssignIntToInteger != NULL && typeid(T) == typeid(int) && g_pAssignIntToInteger(valueType, pValue, &m_value)))
391  {
392  NameValuePairs::ThrowIfTypeMismatch(name, typeid(T), valueType);
393  *reinterpret_cast<T *>(pValue) = m_value;
394  }
395  }
396 
397 #if defined(DEBUG_NEW) && (_MSC_VER >= 1300)
398 # pragma push_macro("new")
399 # undef new
400 #endif
401 
402  void MoveInto(void *buffer) const
403  {
405  CRYPTOPP_UNUSED(p); // silence warning
406  }
407 
408 #if defined(DEBUG_NEW) && (_MSC_VER >= 1300)
409 # pragma pop_macro("new")
410 #endif
411 
412 protected:
413  T m_value;
414 };
415 
416 CRYPTOPP_DLL_TEMPLATE_CLASS AlgorithmParametersTemplate<bool>;
417 CRYPTOPP_DLL_TEMPLATE_CLASS AlgorithmParametersTemplate<int>;
418 CRYPTOPP_DLL_TEMPLATE_CLASS AlgorithmParametersTemplate<ConstByteArrayParameter>;
419 
420 //! \class AlgorithmParameters
421 //! \brief An object that implements NameValuePairs
422 //! \tparam T the class or type
423 //! \param name the name of the object or value to retrieve
424 //! \param value reference to a variable that receives the value
425 //! \param throwIfNotUsed if true, the object will throw an exception if the value is not accessed
426 //! \note throwIfNotUsed is ignored if using a compiler that does not support std::uncaught_exception(),
427 //! such as MSVC 7.0 and earlier.
428 //! \note A NameValuePairs object containing an arbitrary number of name value pairs may be constructed by
429 //! repeatedly using operator() on the object returned by MakeParameters, for example:
430 //! <pre>
431 //! AlgorithmParameters parameters = MakeParameters(name1, value1)(name2, value2)(name3, value3);
432 //! </pre>
433 class CRYPTOPP_DLL AlgorithmParameters : public NameValuePairs
434 {
435 public:
437 
438 #ifdef __BORLANDC__
439  template <class T>
440  AlgorithmParameters(const char *name, const T &value, bool throwIfNotUsed=true)
441  : m_next(new AlgorithmParametersTemplate<T>(name, value, throwIfNotUsed))
442  , m_defaultThrowIfNotUsed(throwIfNotUsed)
443  {
444  }
445 #endif
446 
448 
449  AlgorithmParameters & operator=(const AlgorithmParameters &x);
450 
451  //! \tparam T the class or type
452  //! \param name the name of the object or value to retrieve
453  //! \param value reference to a variable that receives the value
454  //! \param throwIfNotUsed if true, the object will throw an exception if the value is not accessed
455  template <class T>
456  AlgorithmParameters & operator()(const char *name, const T &value, bool throwIfNotUsed)
457  {
458  member_ptr<AlgorithmParametersBase> p(new AlgorithmParametersTemplate<T>(name, value, throwIfNotUsed));
459  p->m_next.reset(m_next.release());
460  m_next.reset(p.release());
461  m_defaultThrowIfNotUsed = throwIfNotUsed;
462  return *this;
463  }
464 
465  //! \brief Appends a NameValuePair to a collection of NameValuePairs
466  //! \tparam T the class or type
467  //! \param name the name of the object or value to retrieve
468  //! \param value reference to a variable that receives the value
469  template <class T>
470  AlgorithmParameters & operator()(const char *name, const T &value)
471  {
472  return operator()(name, value, m_defaultThrowIfNotUsed);
473  }
474 
475  bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const;
476 
477 protected:
479  bool m_defaultThrowIfNotUsed;
480 };
481 
482 //! \brief Create an object that implements NameValuePairs
483 //! \tparam T the class or type
484 //! \param name the name of the object or value to retrieve
485 //! \param value reference to a variable that receives the value
486 //! \param throwIfNotUsed if true, the object will throw an exception if the value is not accessed
487 //! \note throwIfNotUsed is ignored if using a compiler that does not support std::uncaught_exception(),
488 //! such as MSVC 7.0 and earlier.
489 //! \note A NameValuePairs object containing an arbitrary number of name value pairs may be constructed by
490 //! repeatedly using \p operator() on the object returned by \p MakeParameters, for example:
491 //! <pre>
492 //! AlgorithmParameters parameters = MakeParameters(name1, value1)(name2, value2)(name3, value3);
493 //! </pre>
494 #ifdef __BORLANDC__
496 #else
497 template <class T>
498 AlgorithmParameters MakeParameters(const char *name, const T &value, bool throwIfNotUsed = true)
499 {
500  return AlgorithmParameters()(name, value, throwIfNotUsed);
501 }
502 #endif
503 
504 #define CRYPTOPP_GET_FUNCTION_ENTRY(name) (Name::name(), &ThisClass::Get##name)
505 #define CRYPTOPP_SET_FUNCTION_ENTRY(name) (Name::name(), &ThisClass::Set##name)
506 #define CRYPTOPP_SET_FUNCTION_ENTRY2(name1, name2) (Name::name1(), Name::name2(), &ThisClass::Set##name1##And##name2)
507 
508 // TODO: fix 6011 when the API/ABI can change
509 #if (CRYPTOPP_MSC_VERSION >= 1400)
510 # pragma warning(pop)
511 #endif
512 
513 NAMESPACE_END
514 
515 #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:309
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:351
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.
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<byte> 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:498
#define CRYPTOPP_COMPILE_ASSERT(expr)
Compile time assertion.
Definition: misc.h:130
Exception thrown when an AlgorithmParameter is unused.
Definition: algparam.h:314
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
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:373
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:470
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:456
An object that implements NameValuePairs.
Definition: algparam.h:433
Multiple precision integer with arithmetic operations.
AlgorithmParametersTemplate(const char *name, const T &value, bool throwIfNotUsed)
Construct an AlgorithmParametersTemplate.
Definition: algparam.h:382
Crypto++ library namespace.
Interface for retrieving values given their names.
Definition: cryptlib.h:279