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.

103 lines
1.9 KiB

  1. #ifndef CRYPTOPP_WORDS_H
  2. #define CRYPTOPP_WORDS_H
  3. #include "misc.h"
  4. NAMESPACE_BEGIN(CryptoPP)
  5. inline size_t CountWords(const word *X, size_t N)
  6. {
  7. while (N && X[N-1]==0)
  8. N--;
  9. return N;
  10. }
  11. inline void SetWords(word *r, word a, size_t n)
  12. {
  13. for (size_t i=0; i<n; i++)
  14. r[i] = a;
  15. }
  16. inline void CopyWords(word *r, const word *a, size_t n)
  17. {
  18. if (r != a)
  19. memcpy(r, a, n*WORD_SIZE);
  20. }
  21. inline void XorWords(word *r, const word *a, const word *b, size_t n)
  22. {
  23. for (size_t i=0; i<n; i++)
  24. r[i] = a[i] ^ b[i];
  25. }
  26. inline void XorWords(word *r, const word *a, size_t n)
  27. {
  28. for (size_t i=0; i<n; i++)
  29. r[i] ^= a[i];
  30. }
  31. inline void AndWords(word *r, const word *a, const word *b, size_t n)
  32. {
  33. for (size_t i=0; i<n; i++)
  34. r[i] = a[i] & b[i];
  35. }
  36. inline void AndWords(word *r, const word *a, size_t n)
  37. {
  38. for (size_t i=0; i<n; i++)
  39. r[i] &= a[i];
  40. }
  41. inline word ShiftWordsLeftByBits(word *r, size_t n, unsigned int shiftBits)
  42. {
  43. assert (shiftBits<WORD_BITS);
  44. word u, carry=0;
  45. if (shiftBits)
  46. for (size_t i=0; i<n; i++)
  47. {
  48. u = r[i];
  49. r[i] = (u << shiftBits) | carry;
  50. carry = u >> (WORD_BITS-shiftBits);
  51. }
  52. return carry;
  53. }
  54. inline word ShiftWordsRightByBits(word *r, size_t n, unsigned int shiftBits)
  55. {
  56. assert (shiftBits<WORD_BITS);
  57. word u, carry=0;
  58. if (shiftBits)
  59. for (size_t i=n; i>0; i--)
  60. {
  61. u = r[i-1];
  62. r[i-1] = (u >> shiftBits) | carry;
  63. carry = u << (WORD_BITS-shiftBits);
  64. }
  65. return carry;
  66. }
  67. inline void ShiftWordsLeftByWords(word *r, size_t n, size_t shiftWords)
  68. {
  69. shiftWords = STDMIN(shiftWords, n);
  70. if (shiftWords)
  71. {
  72. for (size_t i=n-1; i>=shiftWords; i--)
  73. r[i] = r[i-shiftWords];
  74. SetWords(r, 0, shiftWords);
  75. }
  76. }
  77. inline void ShiftWordsRightByWords(word *r, size_t n, size_t shiftWords)
  78. {
  79. shiftWords = STDMIN(shiftWords, n);
  80. if (shiftWords)
  81. {
  82. for (size_t i=0; i+shiftWords<n; i++)
  83. r[i] = r[i+shiftWords];
  84. SetWords(r+n-shiftWords, 0, shiftWords);
  85. }
  86. }
  87. NAMESPACE_END
  88. #endif