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.

77 lines
1.2 KiB

  1. // adler32.cpp - written and placed in the public domain by Wei Dai
  2. #include "pch.h"
  3. #include "adler32.h"
  4. NAMESPACE_BEGIN(CryptoPP)
  5. void Adler32::Update(const byte *input, size_t length)
  6. {
  7. const unsigned long BASE = 65521;
  8. unsigned long s1 = m_s1;
  9. unsigned long s2 = m_s2;
  10. if (length % 8 != 0)
  11. {
  12. do
  13. {
  14. s1 += *input++;
  15. s2 += s1;
  16. length--;
  17. } while (length % 8 != 0);
  18. if (s1 >= BASE)
  19. s1 -= BASE;
  20. s2 %= BASE;
  21. }
  22. while (length > 0)
  23. {
  24. s1 += input[0]; s2 += s1;
  25. s1 += input[1]; s2 += s1;
  26. s1 += input[2]; s2 += s1;
  27. s1 += input[3]; s2 += s1;
  28. s1 += input[4]; s2 += s1;
  29. s1 += input[5]; s2 += s1;
  30. s1 += input[6]; s2 += s1;
  31. s1 += input[7]; s2 += s1;
  32. length -= 8;
  33. input += 8;
  34. if (s1 >= BASE)
  35. s1 -= BASE;
  36. if (length % 0x8000 == 0)
  37. s2 %= BASE;
  38. }
  39. assert(s1 < BASE);
  40. assert(s2 < BASE);
  41. m_s1 = (word16)s1;
  42. m_s2 = (word16)s2;
  43. }
  44. void Adler32::TruncatedFinal(byte *hash, size_t size)
  45. {
  46. ThrowIfInvalidTruncatedSize(size);
  47. switch (size)
  48. {
  49. default:
  50. hash[3] = byte(m_s1);
  51. case 3:
  52. hash[2] = byte(m_s1 >> 8);
  53. case 2:
  54. hash[1] = byte(m_s2);
  55. case 1:
  56. hash[0] = byte(m_s2 >> 8);
  57. case 0:
  58. ;
  59. }
  60. Reset();
  61. }
  62. NAMESPACE_END