Team Fortress 2 Source Code as on 22/4/2020
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.

210 lines
4.2 KiB

  1. // misc.cpp - written and placed in the public domain by Wei Dai
  2. #include "pch.h"
  3. #include "config.h"
  4. #if CRYPTOPP_MSC_VERSION
  5. # pragma warning(disable: 4189)
  6. # if (CRYPTOPP_MSC_VERSION >= 1400)
  7. # pragma warning(disable: 6237)
  8. # endif
  9. #endif
  10. #ifndef CRYPTOPP_IMPORTS
  11. #include "misc.h"
  12. #include "words.h"
  13. #include "words.h"
  14. #include "stdcpp.h"
  15. #include "integer.h"
  16. // for memalign
  17. #if defined(CRYPTOPP_MEMALIGN_AVAILABLE) || defined(CRYPTOPP_MM_MALLOC_AVAILABLE) || defined(QNX)
  18. # include <malloc.h>
  19. #endif
  20. NAMESPACE_BEGIN(CryptoPP)
  21. void xorbuf(byte *buf, const byte *mask, size_t count)
  22. {
  23. assert(buf != NULL);
  24. assert(mask != NULL);
  25. assert(count > 0);
  26. size_t i=0;
  27. if (IsAligned<word32>(buf) && IsAligned<word32>(mask))
  28. {
  29. if (!CRYPTOPP_BOOL_SLOW_WORD64 && IsAligned<word64>(buf) && IsAligned<word64>(mask))
  30. {
  31. for (i=0; i<count/8; i++)
  32. ((word64*)buf)[i] ^= ((word64*)mask)[i];
  33. count -= 8*i;
  34. if (!count)
  35. return;
  36. buf += 8*i;
  37. mask += 8*i;
  38. }
  39. for (i=0; i<count/4; i++)
  40. ((word32*)buf)[i] ^= ((word32*)mask)[i];
  41. count -= 4*i;
  42. if (!count)
  43. return;
  44. buf += 4*i;
  45. mask += 4*i;
  46. }
  47. for (i=0; i<count; i++)
  48. buf[i] ^= mask[i];
  49. }
  50. void xorbuf(byte *output, const byte *input, const byte *mask, size_t count)
  51. {
  52. assert(output != NULL);
  53. assert(input != NULL);
  54. assert(count > 0);
  55. size_t i=0;
  56. if (IsAligned<word32>(output) && IsAligned<word32>(input) && IsAligned<word32>(mask))
  57. {
  58. if (!CRYPTOPP_BOOL_SLOW_WORD64 && IsAligned<word64>(output) && IsAligned<word64>(input) && IsAligned<word64>(mask))
  59. {
  60. for (i=0; i<count/8; i++)
  61. ((word64*)output)[i] = ((word64*)input)[i] ^ ((word64*)mask)[i];
  62. count -= 8*i;
  63. if (!count)
  64. return;
  65. output += 8*i;
  66. input += 8*i;
  67. mask += 8*i;
  68. }
  69. for (i=0; i<count/4; i++)
  70. ((word32*)output)[i] = ((word32*)input)[i] ^ ((word32*)mask)[i];
  71. count -= 4*i;
  72. if (!count)
  73. return;
  74. output += 4*i;
  75. input += 4*i;
  76. mask += 4*i;
  77. }
  78. for (i=0; i<count; i++)
  79. output[i] = input[i] ^ mask[i];
  80. }
  81. bool VerifyBufsEqual(const byte *buf, const byte *mask, size_t count)
  82. {
  83. assert(buf != NULL);
  84. assert(mask != NULL);
  85. assert(count > 0);
  86. size_t i=0;
  87. byte acc8 = 0;
  88. if (IsAligned<word32>(buf) && IsAligned<word32>(mask))
  89. {
  90. word32 acc32 = 0;
  91. if (!CRYPTOPP_BOOL_SLOW_WORD64 && IsAligned<word64>(buf) && IsAligned<word64>(mask))
  92. {
  93. word64 acc64 = 0;
  94. for (i=0; i<count/8; i++)
  95. acc64 |= ((word64*)buf)[i] ^ ((word64*)mask)[i];
  96. count -= 8*i;
  97. if (!count)
  98. return acc64 == 0;
  99. buf += 8*i;
  100. mask += 8*i;
  101. acc32 = word32(acc64) | word32(acc64>>32);
  102. }
  103. for (i=0; i<count/4; i++)
  104. acc32 |= ((word32*)buf)[i] ^ ((word32*)mask)[i];
  105. count -= 4*i;
  106. if (!count)
  107. return acc32 == 0;
  108. buf += 4*i;
  109. mask += 4*i;
  110. acc8 = byte(acc32) | byte(acc32>>8) | byte(acc32>>16) | byte(acc32>>24);
  111. }
  112. for (i=0; i<count; i++)
  113. acc8 |= buf[i] ^ mask[i];
  114. return acc8 == 0;
  115. }
  116. #if !(defined(_MSC_VER) && (_MSC_VER < 1300))
  117. using std::new_handler;
  118. using std::set_new_handler;
  119. #endif
  120. void CallNewHandler()
  121. {
  122. new_handler newHandler = set_new_handler(NULL);
  123. if (newHandler)
  124. set_new_handler(newHandler);
  125. if (newHandler)
  126. newHandler();
  127. else
  128. throw std::bad_alloc();
  129. }
  130. #if CRYPTOPP_BOOL_ALIGN16
  131. void * AlignedAllocate(size_t size)
  132. {
  133. byte *p;
  134. #if defined(CRYPTOPP_APPLE_ALLOC_AVAILABLE)
  135. while ((p = (byte *)calloc(1, size)) == NULL)
  136. #elif defined(CRYPTOPP_MM_MALLOC_AVAILABLE)
  137. while ((p = (byte *)_mm_malloc(size, 16)) == NULL)
  138. #elif defined(CRYPTOPP_MEMALIGN_AVAILABLE)
  139. while ((p = (byte *)memalign(16, size)) == NULL)
  140. #elif defined(CRYPTOPP_MALLOC_ALIGNMENT_IS_16)
  141. while ((p = (byte *)malloc(size)) == NULL)
  142. #else
  143. while ((p = (byte *)malloc(size + 16)) == NULL)
  144. #endif
  145. CallNewHandler();
  146. #ifdef CRYPTOPP_NO_ALIGNED_ALLOC
  147. size_t adjustment = 16-((size_t)p%16);
  148. p += adjustment;
  149. p[-1] = (byte)adjustment;
  150. #endif
  151. assert(IsAlignedOn(p, 16));
  152. return p;
  153. }
  154. void AlignedDeallocate(void *p)
  155. {
  156. #ifdef CRYPTOPP_MM_MALLOC_AVAILABLE
  157. _mm_free(p);
  158. #elif defined(CRYPTOPP_NO_ALIGNED_ALLOC)
  159. p = (byte *)p - ((byte *)p)[-1];
  160. free(p);
  161. #else
  162. free(p);
  163. #endif
  164. }
  165. #endif
  166. void * UnalignedAllocate(size_t size)
  167. {
  168. void *p;
  169. while ((p = malloc(size)) == NULL)
  170. CallNewHandler();
  171. return p;
  172. }
  173. void UnalignedDeallocate(void *p)
  174. {
  175. free(p);
  176. }
  177. NAMESPACE_END
  178. #endif