Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

403 lines
13 KiB

  1. #ifndef CRYPTOPP_ALGPARAM_H
  2. #define CRYPTOPP_ALGPARAM_H
  3. #include "cryptlib.h"
  4. #include "smartptr.h"
  5. #include "secblock.h"
  6. NAMESPACE_BEGIN(CryptoPP)
  7. //! used to pass byte array input as part of a NameValuePairs object
  8. /*! the deepCopy option is used when the NameValuePairs object can't
  9. keep a copy of the data available */
  10. class ConstByteArrayParameter
  11. {
  12. public:
  13. ConstByteArrayParameter(const char *data = NULL, bool deepCopy = false)
  14. {
  15. Assign((const byte *)data, data ? strlen(data) : 0, deepCopy);
  16. }
  17. ConstByteArrayParameter(const byte *data, size_t sizex, bool deepCopy = false)
  18. {
  19. Assign(data, sizex, deepCopy);
  20. }
  21. template <class T> ConstByteArrayParameter(const T &string, bool deepCopy = false)
  22. {
  23. CRYPTOPP_COMPILE_ASSERT(sizeof(CPP_TYPENAME T::value_type) == 1);
  24. Assign((const byte *)string.data(), string.size(), deepCopy);
  25. }
  26. void Assign(const byte *data, size_t sizex, bool deepCopy)
  27. {
  28. if (deepCopy)
  29. m_block.Assign(data, sizex);
  30. else
  31. {
  32. m_data = data;
  33. m_size = sizex;
  34. }
  35. m_deepCopy = deepCopy;
  36. }
  37. const byte *begin() const {return m_deepCopy ? m_block.begin() : m_data;}
  38. const byte *end() const {return m_deepCopy ? m_block.end() : m_data + m_size;}
  39. size_t size() const {return m_deepCopy ? m_block.size() : m_size;}
  40. private:
  41. bool m_deepCopy;
  42. const byte *m_data;
  43. size_t m_size;
  44. SecByteBlock m_block;
  45. };
  46. class ByteArrayParameter
  47. {
  48. public:
  49. ByteArrayParameter(byte *data = NULL, unsigned int sizex = 0)
  50. : m_data(data), m_size(sizex) {}
  51. ByteArrayParameter(SecByteBlock &block)
  52. : m_data(block.begin()), m_size(block.size()) {}
  53. byte *begin() const {return m_data;}
  54. byte *end() const {return m_data + m_size;}
  55. size_t size() const {return m_size;}
  56. private:
  57. byte *m_data;
  58. size_t m_size;
  59. };
  60. class CRYPTOPP_DLL CombinedNameValuePairs : public NameValuePairs
  61. {
  62. public:
  63. CombinedNameValuePairs(const NameValuePairs &pairs1, const NameValuePairs &pairs2)
  64. : m_pairs1(pairs1), m_pairs2(pairs2) {}
  65. bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const;
  66. private:
  67. const NameValuePairs &m_pairs1, &m_pairs2;
  68. };
  69. template <class T, class BASE>
  70. class GetValueHelperClass
  71. {
  72. public:
  73. GetValueHelperClass(const T *pObject, const char *name, const std::type_info &valueType, void *pValue, const NameValuePairs *searchFirst)
  74. : m_pObject(pObject), m_name(name), m_valueType(&valueType), m_pValue(pValue), m_found(false), m_getValueNames(false)
  75. {
  76. if (strcmp(m_name, "ValueNames") == 0)
  77. {
  78. m_found = m_getValueNames = true;
  79. NameValuePairs::ThrowIfTypeMismatch(m_name, typeid(std::string), *m_valueType);
  80. if (searchFirst)
  81. searchFirst->GetVoidValue(m_name, valueType, pValue);
  82. if (typeid(T) != typeid(BASE))
  83. pObject->BASE::GetVoidValue(m_name, valueType, pValue);
  84. ((*reinterpret_cast<std::string *>(m_pValue) += "ThisPointer:") += typeid(T).name()) += ';';
  85. }
  86. if (!m_found && strncmp(m_name, "ThisPointer:", 12) == 0 && strcmp(m_name+12, typeid(T).name()) == 0)
  87. {
  88. NameValuePairs::ThrowIfTypeMismatch(m_name, typeid(T *), *m_valueType);
  89. *reinterpret_cast<const T **>(pValue) = pObject;
  90. m_found = true;
  91. return;
  92. }
  93. if (!m_found && searchFirst)
  94. m_found = searchFirst->GetVoidValue(m_name, valueType, pValue);
  95. if (!m_found && typeid(T) != typeid(BASE))
  96. m_found = pObject->BASE::GetVoidValue(m_name, valueType, pValue);
  97. }
  98. operator bool() const {return m_found;}
  99. template <class R>
  100. GetValueHelperClass<T,BASE> & operator()(const char *name, const R & (T::*pm)() const)
  101. {
  102. if (m_getValueNames)
  103. (*reinterpret_cast<std::string *>(m_pValue) += name) += ";";
  104. if (!m_found && strcmp(name, m_name) == 0)
  105. {
  106. NameValuePairs::ThrowIfTypeMismatch(name, typeid(R), *m_valueType);
  107. *reinterpret_cast<R *>(m_pValue) = (m_pObject->*pm)();
  108. m_found = true;
  109. }
  110. return *this;
  111. }
  112. GetValueHelperClass<T,BASE> &Assignable()
  113. {
  114. #ifndef __INTEL_COMPILER // ICL 9.1 workaround: Intel compiler copies the vTable pointer for some reason
  115. if (m_getValueNames)
  116. ((*reinterpret_cast<std::string *>(m_pValue) += "ThisObject:") += typeid(T).name()) += ';';
  117. if (!m_found && strncmp(m_name, "ThisObject:", 11) == 0 && strcmp(m_name+11, typeid(T).name()) == 0)
  118. {
  119. NameValuePairs::ThrowIfTypeMismatch(m_name, typeid(T), *m_valueType);
  120. *reinterpret_cast<T *>(m_pValue) = *m_pObject;
  121. m_found = true;
  122. }
  123. #endif
  124. return *this;
  125. }
  126. private:
  127. const T *m_pObject;
  128. const char *m_name;
  129. const std::type_info *m_valueType;
  130. void *m_pValue;
  131. bool m_found, m_getValueNames;
  132. };
  133. template <class BASE, class T>
  134. GetValueHelperClass<T, BASE> GetValueHelper(const T *pObject, const char *name, const std::type_info &valueType, void *pValue, const NameValuePairs *searchFirst=NULL, BASE *dummy=NULL)
  135. {
  136. return GetValueHelperClass<T, BASE>(pObject, name, valueType, pValue, searchFirst);
  137. }
  138. template <class T>
  139. GetValueHelperClass<T, T> GetValueHelper(const T *pObject, const char *name, const std::type_info &valueType, void *pValue, const NameValuePairs *searchFirst=NULL)
  140. {
  141. return GetValueHelperClass<T, T>(pObject, name, valueType, pValue, searchFirst);
  142. }
  143. // ********************************************************
  144. template <class R>
  145. R Hack_DefaultValueFromConstReferenceType(const R &)
  146. {
  147. return R();
  148. }
  149. template <class R>
  150. bool Hack_GetValueIntoConstReference(const NameValuePairs &source, const char *name, const R &value)
  151. {
  152. return source.GetValue(name, const_cast<R &>(value));
  153. }
  154. template <class T, class BASE>
  155. class AssignFromHelperClass
  156. {
  157. public:
  158. AssignFromHelperClass(T *pObject, const NameValuePairs &source)
  159. : m_pObject(pObject), m_source(source), m_done(false)
  160. {
  161. if (source.GetThisObject(*pObject))
  162. m_done = true;
  163. else if (typeid(BASE) != typeid(T))
  164. pObject->BASE::AssignFrom(source);
  165. }
  166. template <class R>
  167. AssignFromHelperClass & operator()(const char *name, void (T::*pm)(R)) // VC60 workaround: "const R &" here causes compiler error
  168. {
  169. if (!m_done)
  170. {
  171. R value = Hack_DefaultValueFromConstReferenceType(reinterpret_cast<R>(*(int *)NULL));
  172. if (!Hack_GetValueIntoConstReference(m_source, name, value))
  173. throw InvalidArgument(std::string(typeid(T).name()) + ": Missing required parameter '" + name + "'");
  174. (m_pObject->*pm)(value);
  175. }
  176. return *this;
  177. }
  178. template <class R, class S>
  179. AssignFromHelperClass & operator()(const char *name1, const char *name2, void (T::*pm)(R, S)) // VC60 workaround: "const R &" here causes compiler error
  180. {
  181. if (!m_done)
  182. {
  183. R value1 = Hack_DefaultValueFromConstReferenceType(reinterpret_cast<R>(*(int *)NULL));
  184. if (!Hack_GetValueIntoConstReference(m_source, name1, value1))
  185. throw InvalidArgument(std::string(typeid(T).name()) + ": Missing required parameter '" + name1 + "'");
  186. S value2 = Hack_DefaultValueFromConstReferenceType(reinterpret_cast<S>(*(int *)NULL));
  187. if (!Hack_GetValueIntoConstReference(m_source, name2, value2))
  188. throw InvalidArgument(std::string(typeid(T).name()) + ": Missing required parameter '" + name2 + "'");
  189. (m_pObject->*pm)(value1, value2);
  190. }
  191. return *this;
  192. }
  193. private:
  194. T *m_pObject;
  195. const NameValuePairs &m_source;
  196. bool m_done;
  197. };
  198. template <class BASE, class T>
  199. AssignFromHelperClass<T, BASE> AssignFromHelper(T *pObject, const NameValuePairs &source, BASE *dummy=NULL)
  200. {
  201. return AssignFromHelperClass<T, BASE>(pObject, source);
  202. }
  203. template <class T>
  204. AssignFromHelperClass<T, T> AssignFromHelper(T *pObject, const NameValuePairs &source)
  205. {
  206. return AssignFromHelperClass<T, T>(pObject, source);
  207. }
  208. // ********************************************************
  209. // to allow the linker to discard Integer code if not needed.
  210. typedef bool (CRYPTOPP_API * PAssignIntToInteger)(const std::type_info &valueType, void *pInteger, const void *pInt);
  211. CRYPTOPP_DLL extern PAssignIntToInteger g_pAssignIntToInteger;
  212. CRYPTOPP_DLL const std::type_info & CRYPTOPP_API IntegerTypeId();
  213. class CRYPTOPP_DLL AlgorithmParametersBase
  214. {
  215. public:
  216. class ParameterNotUsed : public Exception
  217. {
  218. public:
  219. ParameterNotUsed(const char *name) : Exception(OTHER_ERROR, std::string("AlgorithmParametersBase: parameter \"") + name + "\" not used") {}
  220. };
  221. // this is actually a move, not a copy
  222. AlgorithmParametersBase(const AlgorithmParametersBase &x)
  223. : m_name(x.m_name), m_throwIfNotUsed(x.m_throwIfNotUsed), m_used(x.m_used)
  224. {
  225. m_next.reset(const_cast<AlgorithmParametersBase &>(x).m_next.release());
  226. x.m_used = true;
  227. }
  228. AlgorithmParametersBase(const char *name, bool throwIfNotUsed)
  229. : m_name(name), m_throwIfNotUsed(throwIfNotUsed), m_used(false) {}
  230. virtual ~AlgorithmParametersBase()
  231. {
  232. #ifdef CRYPTOPP_UNCAUGHT_EXCEPTION_AVAILABLE
  233. if (!std::uncaught_exception())
  234. #else
  235. try
  236. #endif
  237. {
  238. if (m_throwIfNotUsed && !m_used)
  239. throw ParameterNotUsed(m_name);
  240. }
  241. #ifndef CRYPTOPP_UNCAUGHT_EXCEPTION_AVAILABLE
  242. catch(...)
  243. {
  244. }
  245. #endif
  246. }
  247. bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const;
  248. protected:
  249. friend class AlgorithmParameters;
  250. void operator=(const AlgorithmParametersBase& rhs); // assignment not allowed, declare this for VC60
  251. virtual void AssignValue(const char *name, const std::type_info &valueType, void *pValue) const =0;
  252. //VALVE: Our debug allocator doesn't implement placement new, and this function is never
  253. // actually called.
  254. //virtual void MoveInto(void *p) const =0; // not really const
  255. const char *m_name;
  256. bool m_throwIfNotUsed;
  257. mutable bool m_used;
  258. member_ptr<AlgorithmParametersBase> m_next;
  259. };
  260. template <class T>
  261. class AlgorithmParametersTemplate : public AlgorithmParametersBase
  262. {
  263. public:
  264. AlgorithmParametersTemplate(const char *name, const T &value, bool throwIfNotUsed)
  265. : AlgorithmParametersBase(name, throwIfNotUsed), m_value(value)
  266. {
  267. }
  268. void AssignValue(const char *name, const std::type_info &valueType, void *pValue) const
  269. {
  270. // special case for retrieving an Integer parameter when an int was passed in
  271. if (!(g_pAssignIntToInteger != NULL && typeid(T) == typeid(int) && g_pAssignIntToInteger(valueType, pValue, &m_value)))
  272. {
  273. NameValuePairs::ThrowIfTypeMismatch(name, typeid(T), valueType);
  274. *reinterpret_cast<T *>(pValue) = m_value;
  275. }
  276. }
  277. //VALVE: Our debug allocator doesn't implement placement new, and this function is never
  278. // actually called.
  279. //void MoveInto(void *buffer) const
  280. //{
  281. // AlgorithmParametersTemplate<T>* p = new(buffer) AlgorithmParametersTemplate<T>(*this);
  282. //}
  283. protected:
  284. T m_value;
  285. };
  286. CRYPTOPP_DLL_TEMPLATE_CLASS AlgorithmParametersTemplate<bool>;
  287. CRYPTOPP_DLL_TEMPLATE_CLASS AlgorithmParametersTemplate<int>;
  288. CRYPTOPP_DLL_TEMPLATE_CLASS AlgorithmParametersTemplate<ConstByteArrayParameter>;
  289. class CRYPTOPP_DLL AlgorithmParameters : public NameValuePairs
  290. {
  291. public:
  292. AlgorithmParameters();
  293. #ifdef __BORLANDC__
  294. template <class T>
  295. AlgorithmParameters(const char *name, const T &value, bool throwIfNotUsed=true)
  296. : m_next(new AlgorithmParametersTemplate<T>(name, value, throwIfNotUsed))
  297. , m_defaultThrowIfNotUsed(throwIfNotUsed)
  298. {
  299. }
  300. #endif
  301. AlgorithmParameters(const AlgorithmParameters &x);
  302. AlgorithmParameters & operator=(const AlgorithmParameters &x);
  303. template <class T>
  304. AlgorithmParameters & operator()(const char *name, const T &value, bool throwIfNotUsed)
  305. {
  306. member_ptr<AlgorithmParametersBase> p(new AlgorithmParametersTemplate<T>(name, value, throwIfNotUsed));
  307. p->m_next.reset(m_next.release());
  308. m_next.reset(p.release());
  309. m_defaultThrowIfNotUsed = throwIfNotUsed;
  310. return *this;
  311. }
  312. template <class T>
  313. AlgorithmParameters & operator()(const char *name, const T &value)
  314. {
  315. return operator()(name, value, m_defaultThrowIfNotUsed);
  316. }
  317. bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const;
  318. protected:
  319. member_ptr<AlgorithmParametersBase> m_next;
  320. bool m_defaultThrowIfNotUsed;
  321. };
  322. //! Create an object that implements NameValuePairs for passing parameters
  323. /*! \param throwIfNotUsed if true, the object will throw an exception if the value is not accessed
  324. \note throwIfNotUsed is ignored if using a compiler that does not support std::uncaught_exception(),
  325. such as MSVC 7.0 and earlier.
  326. \note A NameValuePairs object containing an arbitrary number of name value pairs may be constructed by
  327. repeatedly using operator() on the object returned by MakeParameters, for example:
  328. AlgorithmParameters parameters = MakeParameters(name1, value1)(name2, value2)(name3, value3);
  329. */
  330. #ifdef __BORLANDC__
  331. typedef AlgorithmParameters MakeParameters;
  332. #else
  333. template <class T>
  334. AlgorithmParameters MakeParameters(const char *name, const T &value, bool throwIfNotUsed = true)
  335. {
  336. return AlgorithmParameters()(name, value, throwIfNotUsed);
  337. }
  338. #endif
  339. #define CRYPTOPP_GET_FUNCTION_ENTRY(name) (Name::name(), &ThisClass::Get##name)
  340. #define CRYPTOPP_SET_FUNCTION_ENTRY(name) (Name::name(), &ThisClass::Set##name)
  341. #define CRYPTOPP_SET_FUNCTION_ENTRY2(name1, name2) (Name::name1(), Name::name2(), &ThisClass::Set##name1##And##name2)
  342. NAMESPACE_END
  343. #endif