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.

187 lines
3.6 KiB

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