TcWrapper.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*
  2. * Tomcrypt library <= firebird : c++ wrapper.
  3. *
  4. * The contents of this file are subject to the Initial
  5. * Developer's Public License Version 1.0 (the "License");
  6. * you may not use this file except in compliance with the
  7. * License. You may obtain a copy of the License at
  8. * https://www.firebirdsql.org/en/initial-developer-s-public-license-version-1-0/
  9. *
  10. * Software distributed under the License is distributed AS IS,
  11. * WITHOUT WARRANTY OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing rights
  13. * and limitations under the License.
  14. *
  15. * The Original Code was created by Alexander Peshkoff
  16. * for the Firebird Open Source RDBMS project.
  17. *
  18. * Copyright (c) 2020 Alexander Peshkoff <[email protected]>
  19. * and all contributors signed below.
  20. *
  21. * All Rights Reserved.
  22. * Contributor(s): ______________________________________.
  23. */
  24. #include <tomcrypt.h>
  25. #include <firebird/Interface.h>
  26. using namespace Firebird;
  27. #include <stdio.h>
  28. void error(ThrowStatusWrapper* status, const char* text);
  29. void check(ThrowStatusWrapper* status, int err, const char* text);
  30. unsigned readHexKey(ThrowStatusWrapper* status, const char* hex, unsigned char* key, unsigned bufSize);
  31. class PseudoRandom
  32. {
  33. public:
  34. #if CRYPT > 0x0100
  35. typedef ltc_prng_descriptor PrngDescriptor;
  36. #else
  37. typedef _prng_descriptor PrngDescriptor;
  38. #endif
  39. void init(ThrowStatusWrapper* status);
  40. void fini();
  41. const PrngDescriptor* getDsc();
  42. int index;
  43. prng_state state;
  44. };
  45. class Hash
  46. {
  47. protected:
  48. void init(ThrowStatusWrapper* status, const ltc_hash_descriptor* desc);
  49. public:
  50. void fini()
  51. { }
  52. int index;
  53. };
  54. class HashSha1 : public Hash
  55. {
  56. public:
  57. void init(ThrowStatusWrapper* status)
  58. {
  59. Hash::init(status, &sha1_desc);
  60. }
  61. };
  62. class HashSha256 : public Hash
  63. {
  64. public:
  65. void init(ThrowStatusWrapper* status)
  66. {
  67. Hash::init(status, &sha256_desc);
  68. }
  69. };
  70. // controls reference counter of the object where points
  71. enum NoIncrement {NO_INCREMENT};
  72. template <typename T>
  73. class RefPtr
  74. {
  75. public:
  76. RefPtr() : ptr(NULL)
  77. { }
  78. explicit RefPtr(T* p) : ptr(p)
  79. {
  80. if (ptr)
  81. {
  82. ptr->addRef();
  83. }
  84. }
  85. // This special form of ctor is used to create refcounted ptr from interface,
  86. // returned by a function (which increments counter on return)
  87. RefPtr(NoIncrement x, T* p) : ptr(p)
  88. { }
  89. RefPtr(const RefPtr& r) : ptr(r.ptr)
  90. {
  91. if (ptr)
  92. {
  93. ptr->addRef();
  94. }
  95. }
  96. ~RefPtr()
  97. {
  98. if (ptr)
  99. {
  100. ptr->release();
  101. }
  102. }
  103. T* operator=(T* p)
  104. {
  105. return assign(p);
  106. }
  107. T* operator=(const RefPtr& r)
  108. {
  109. return assign(r.ptr);
  110. }
  111. operator T*()
  112. {
  113. return ptr;
  114. }
  115. T* operator->()
  116. {
  117. return ptr;
  118. }
  119. operator const T*() const
  120. {
  121. return ptr;
  122. }
  123. const T* operator->() const
  124. {
  125. return ptr;
  126. }
  127. bool operator !() const
  128. {
  129. return !ptr;
  130. }
  131. bool operator ==(const RefPtr& r) const
  132. {
  133. return ptr == r.ptr;
  134. }
  135. bool operator !=(const RefPtr& r) const
  136. {
  137. return ptr != r.ptr;
  138. }
  139. void clear() throw() // Used after detach/commit/close/etc., i.e. release() not needed
  140. {
  141. ptr = NULL;
  142. }
  143. void assignNoIncrement(T* const p)
  144. {
  145. assign(NULL);
  146. ptr = p;
  147. }
  148. private:
  149. T* assign(T* const p)
  150. {
  151. if (ptr != p)
  152. {
  153. if (p)
  154. {
  155. p->addRef();
  156. }
  157. T* tmp = ptr;
  158. ptr = p;
  159. if (tmp)
  160. {
  161. tmp->release();
  162. }
  163. }
  164. return ptr;
  165. }
  166. T* ptr;
  167. };
  168. // Often used form of RefPtr
  169. template <class R>
  170. class AutoRelease : public RefPtr<R>
  171. {
  172. public:
  173. AutoRelease(R* r)
  174. : RefPtr<R>(NO_INCREMENT, r)
  175. { }
  176. };