Fabcoin Core  0.16.2
P2P Digital Currency
algparam.h
Go to the documentation of this file.
1 // algparam.h - written and placed in the public domain by Wei Dai
2 
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 
26 
30 {
31 public:
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 
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 
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 
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 
89  const byte *begin() const {return m_deepCopy ? m_block.begin() : m_data;}
91  const byte *end() const {return m_deepCopy ? m_block.end() : m_data + m_size;}
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;
100 };
101 
105 {
106 public:
110  ByteArrayParameter(byte *data = NULL, unsigned int size = 0)
111  : m_data(data), m_size(size) {}
112 
116  : m_data(block.begin()), m_size(block.size()) {}
117 
119  byte *begin() const {return m_data;}
121  byte *end() const {return m_data + m_size;}
123  size_t size() const {return m_size;}
124 
125 private:
127  size_t m_size;
128 };
129 
136 {
137 public:
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>
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 
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>
239 {
240 public:
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:
282  bool m_done;
283 };
284 
285 template <class BASE, class T>
287 {
288  return AssignFromHelperClass<T, BASE>(pObject, source);
289 }
290 
291 template <class T>
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);
304 
305 CRYPTOPP_DLL const std::type_info & CRYPTOPP_API IntegerTypeId();
306 
310 {
311 public:
315  {
316  public:
317  ParameterNotUsed(const char *name) : Exception(OTHER_ERROR, std::string("AlgorithmParametersBase: parameter \"") + name + "\" not used") {}
318  };
319 
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 
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;
365  mutable bool m_used;
367 };
368 
372 template <class T>
374 {
375 public:
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:
414 };
415 
419 
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 
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 
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:
480 };
481 
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 
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
uint8_t byte
Definition: Common.h:57
Utility functions for the Crypto++ library.
bool(CRYPTOPP_API * PAssignIntToInteger)(const std::type_info &valueType, void *pInteger, const void *pInt)
Definition: algparam.h:302
member_ptr< AlgorithmParametersBase > m_next
Definition: algparam.h:478
const NameValuePairs & m_source
Definition: algparam.h:281
Base class for AlgorithmParameters.
Definition: algparam.h:309
AssignFromHelperClass(T *pObject, const NameValuePairs &source)
Definition: algparam.h:241
const std::type_info * m_valueType
Definition: algparam.h:218
#define T(i, x)
#define NAMESPACE_BEGIN(x)
Definition: config.h:200
ConstByteArrayParameter(const T &string, bool deepCopy=false)
Construct a ConstByteArrayParameter.
Definition: algparam.h:61
#define CRYPTOPP_DLL_TEMPLATE_CLASS
Definition: config.h:720
AlgorithmParametersBase(const char *name, bool throwIfNotUsed)
Construct a AlgorithmParametersBase.
Definition: algparam.h:351
AssignFromHelperClass & operator()(const char *name1, const char *name2, void(T::*pm)(const R &, const S &))
Definition: algparam.h:264
ConstByteArrayParameter(const byte *data, size_t size, bool deepCopy=false)
Construct a ConstByteArrayParameter.
Definition: algparam.h:49
AlgorithmParametersBase(const AlgorithmParametersBase &x)
Definition: algparam.h:339
Abstract base classes that provide a uniform interface to this library.
#define R(a, b)
Classes for automatic resource management.
size_t size() const
Length of the memory block.
Definition: algparam.h:93
Library configuration file.
member_ptr< AlgorithmParametersBase > m_next
Definition: algparam.h:366
std::hash for asio::adress
Definition: Common.h:323
ConstByteArrayParameter(const char *data=NULL, bool deepCopy=false)
Construct a ConstByteArrayParameter.
Definition: algparam.h:37
CRYPTOPP_DLL const std::type_info &CRYPTOPP_API IntegerTypeId()
Combines two sets of NameValuePairs.
Definition: algparam.h:135
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
const NameValuePairs & m_pairs2
Definition: algparam.h:147
const char * m_name
Definition: algparam.h:217
virtual CRYPTOPP_DLL bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const =0
Get a named value.
Classes and functions for secure memory allocations.
CRYPTOPP_DLL PAssignIntToInteger g_pAssignIntToInteger
Definition: algparam.cpp:12
#define CRYPTOPP_THROW
Definition: config.h:883
#define x(i)
const char * m_name
Definition: algparam.h:363
GetValueHelperClass< T, BASE > & operator()(const char *name, const R &(T::*pm)() const)
Definition: algparam.h:187
const char * source
Definition: rpcconsole.cpp:60
#define CRYPTOPP_COMPILE_ASSERT(assertion)
Definition: misc.h:139
AlgorithmParameters MakeParameters(const char *name, const T &value, bool throwIfNotUsed=true)
Create an object that implements NameValuePairs.
Definition: algparam.h:498
const T * m_pObject
Definition: algparam.h:216
const char * name
Definition: rest.cpp:36
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
#define CRYPTOPP_API
Definition: config.h:705
Template base class for AlgorithmParameters.
Definition: algparam.h:373
size_t size() const
Length of the memory block.
Definition: algparam.h:123
AssignFromHelperClass< T, BASE > AssignFromHelper(T *pObject, const NameValuePairs &source)
Definition: algparam.h:286
AlgorithmParameters & operator()(const char *name, const T &value)
Appends a NameValuePair to a collection of NameValuePairs.
Definition: algparam.h:470
GetValueHelperClass< T, BASE > & Assignable()
Definition: algparam.h:200
AssignFromHelperClass & operator()(const char *name, void(T::*pm)(const R &))
Definition: algparam.h:251
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
AlgorithmParameters & operator=(const AlgorithmParameters &x)
Definition: algparam.cpp:61
ByteArrayParameter(SecByteBlock &block)
Construct a ByteArrayParameter.
Definition: algparam.h:115
void MoveInto(void *buffer) const
Definition: algparam.h:402
T * release()
Definition: smartptr.h:55
const byte * m_data
Definition: algparam.h:97
uint8_t const size_t const size
Definition: sha3.h:20
void AssignValue(const char *name, const std::type_info &valueType, void *pValue) const
Definition: algparam.h:387
#define CRYPTOPP_UNUSED(x)
Definition: config.h:741
AlgorithmParameters & operator()(const char *name, const T &value, bool throwIfNotUsed)
Definition: algparam.h:456
virtual ~AlgorithmParametersBase() CRYPTOPP_THROW
Definition: algparam.h:320
An object that implements NameValuePairs.
Definition: algparam.h:433
Multiple precision integer with arithmetic operations.
#define NAMESPACE_END
Definition: config.h:201
AlgorithmParametersTemplate(const char *name, const T &value, bool throwIfNotUsed)
Construct an AlgorithmParametersTemplate.
Definition: algparam.h:382
#define S(a)
Definition: mars.cpp:50
#define CRYPTOPP_DLL
Definition: config.h:704
void reset(T *p=0)
Definition: smartptr.h:72
uint8_t const * data
Definition: sha3.h:19
static CRYPTOPP_DLL void CRYPTOPP_API 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
SecByteBlock m_block
Definition: algparam.h:99
GetValueHelperClass(const T *pObject, const char *name, const std::type_info &valueType, void *pValue, const NameValuePairs *searchFirst)
Definition: algparam.h:155
Interface for retrieving values given their names.
Definition: cryptlib.h:279
GetValueHelperClass< T, BASE > GetValueHelper(const T *pObject, const char *name, const std::type_info &valueType, void *pValue, const NameValuePairs *searchFirst=NULL)
Definition: algparam.h:224
bool m_defaultThrowIfNotUsed
Definition: algparam.h:479