crc.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. ** Command & Conquer Generals Zero Hour(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. ////////////////////////////////////////////////////////////////////////////////
  19. // //
  20. // (c) 2001-2003 Electronic Arts Inc. //
  21. // //
  22. ////////////////////////////////////////////////////////////////////////////////
  23. // CRC.h ///////////////////////////////////////////////////////////////
  24. // A class encapsulating CRC calculation
  25. // Author: Matthew D. Campbell, October 2001
  26. #pragma once
  27. #ifndef _CRC_H_
  28. #define _CRC_H_
  29. #include "Lib/BaseType.h"
  30. #include "winsock2.h" // for htonl
  31. #ifdef _DEBUG
  32. class CRC
  33. {
  34. public:
  35. CRC() { crc = 0; }
  36. void computeCRC( const void *buf, Int len ); ///< Compute the CRC for a buffer, added into current CRC
  37. void clear( void ) { crc = 0; } ///< Clears the CRC to 0
  38. // UnsignedInt get( void ) { return htonl(crc); } ///< Get the combined CRC
  39. UnsignedInt get( void );
  40. private:
  41. void addCRC( UnsignedByte val ); ///< CRC a 4-byte block
  42. UnsignedInt crc;
  43. };
  44. #else
  45. // optimized inline only version
  46. class CRC
  47. {
  48. public:
  49. CRC(void) { crc=0; }
  50. /// Compute the CRC for a buffer, added into current CRC
  51. __forceinline void computeCRC( const void *buf, Int len )
  52. {
  53. if (!buf||len<1)
  54. return;
  55. /* C++ version left in for reference purposes
  56. for (UnsignedByte *uintPtr=(UnsignedByte *)buf;len>0;len--,uintPtr++)
  57. {
  58. int hibit;
  59. if (crc & 0x80000000)
  60. {
  61. hibit = 1;
  62. }
  63. else
  64. {
  65. hibit = 0;
  66. }
  67. crc <<= 1;
  68. crc += *uintPtr;
  69. crc += hibit;
  70. }
  71. */
  72. // ASM version, verified by comparing resulting data with C++ version data
  73. unsigned *crcPtr=&crc;
  74. _asm
  75. {
  76. mov esi,[buf]
  77. mov ecx,[len]
  78. dec ecx
  79. mov edi,[crcPtr]
  80. mov ebx,dword ptr [edi]
  81. xor eax,eax
  82. lp:
  83. mov al,byte ptr [esi]
  84. shl ebx,1
  85. inc esi
  86. adc ebx,eax
  87. dec ecx
  88. jns lp
  89. mov dword ptr [edi],ebx
  90. };
  91. }
  92. /// Clears the CRC to 0
  93. void clear( void )
  94. {
  95. crc = 0;
  96. }
  97. ///< Get the combined CRC
  98. UnsignedInt get( void ) const
  99. {
  100. return crc;
  101. }
  102. private:
  103. UnsignedInt crc;
  104. };
  105. #endif
  106. #endif // _CRC_H_