crc.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. ** Command & Conquer Renegade(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. *** C O N F I D E N T I A L --- W E S T W O O D S T U D I O S ***
  20. ***********************************************************************************************
  21. * *
  22. * Project Name : Command & Conquer *
  23. * *
  24. * $Archive:: /G/wwlib/crc.h $*
  25. * *
  26. * $Author:: Neal_k $*
  27. * *
  28. * $Modtime:: 10/04/99 10:25a $*
  29. * *
  30. * $Revision:: 4 $*
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Functions: *
  34. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  35. #if _MSC_VER >= 1000
  36. #pragma once
  37. #endif // _MSC_VER >= 1000
  38. #ifndef CRC_H
  39. #define CRC_H
  40. #include <stdlib.h>
  41. #ifdef _UNIX
  42. #include "osdep.h"
  43. #endif
  44. /*
  45. ** This is a CRC engine class. It will process submitted data and generate a CRC from it.
  46. ** Well, actually, the value returned is not a true CRC. However, it shares the same strength
  47. ** characteristic and is faster to generate than the traditional CRC. This object is treated like
  48. ** a method class. If it is called as a function (using the function operator), it will return
  49. ** the CRC value. There are other function operators to submit data for processing.
  50. */
  51. class CRCEngine {
  52. public:
  53. // Constructor for CRC engine (it can have an override initial CRC value).
  54. CRCEngine(long initial=0) : CRC(initial), Index(0) {
  55. StagingBuffer.Composite = 0;
  56. };
  57. // Fetches CRC value.
  58. long operator() (void) const {return(Value());};
  59. // Submits one byte sized datum to the CRC accumulator.
  60. void operator() (char datum);
  61. // Submits an arbitrary buffer to the CRC accumulator.
  62. long operator() (void const * buffer, int length);
  63. // Implicit conversion operator so this object appears like a 'long integer'.
  64. operator long(void) const {return(Value());};
  65. protected:
  66. bool Buffer_Needs_Data(void) const {
  67. return(Index != 0);
  68. };
  69. long Value(void) const {
  70. if (Buffer_Needs_Data()) {
  71. return(_lrotl(CRC, 1) + StagingBuffer.Composite);
  72. }
  73. return(CRC);
  74. };
  75. /*
  76. ** Current accumulator of the CRC value. This value doesn't take into
  77. ** consideration any pending data in the staging buffer.
  78. */
  79. long CRC;
  80. /*
  81. ** This is the sub index into the staging buffer used to keep track of
  82. ** partial data blocks as they are submitted to the CRC engine.
  83. */
  84. int Index;
  85. /*
  86. ** This is the buffer that holds the incoming partial data. When the buffer
  87. ** is filled, the value is transformed into the CRC and the buffer is flushed
  88. ** in preparation for additional data.
  89. */
  90. union {
  91. long Composite;
  92. char Buffer[sizeof(long)];
  93. } StagingBuffer;
  94. };
  95. // the CRC class defines a few static functions for dealing with CRCs a little differently than
  96. // the CRCEngine. This class uses a algorithm that produces CRC values that have a high probability
  97. // of being unique under most circumstances.
  98. // Note: this code was provided by Byon Garrabrant
  99. //
  100. // 12/09/97 EHC - converted from c to c++ static class and added to crc.h and crc.cpp
  101. //
  102. #define CRC32(c,crc) (CRC::_Table[((unsigned long)(crc) ^ (c)) & 0xFFL] ^ (((crc) >> 8) & 0x00FFFFFFL))
  103. class CRC {
  104. // CRC for poly 0x04C11DB7
  105. static unsigned long _Table[256];
  106. public:
  107. // get the CRC of a block of memory
  108. static unsigned long Memory( unsigned char *data, unsigned long length, unsigned long crc = 0 );
  109. // get the CRC of a null-terminated string
  110. static unsigned long String( const char *string, unsigned long crc = 0 );
  111. };
  112. #endif