CRC.H 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. ** Command & Conquer Red Alert(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. /* $Header: /CounterStrike/CRC.H 1 3/03/97 10:24a Joe_bostic $ */
  19. /***********************************************************************************************
  20. *** 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 ***
  21. ***********************************************************************************************
  22. * *
  23. * Project Name : Command & Conquer *
  24. * *
  25. * File Name : CRC.H *
  26. * *
  27. * Programmer : Joe L. Bostic *
  28. * *
  29. * Start Date : 03/02/96 *
  30. * *
  31. * Last Update : March 2, 1996 [JLB] *
  32. * *
  33. *---------------------------------------------------------------------------------------------*
  34. * Functions: *
  35. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  36. #ifndef CRC_H
  37. #define CRC_H
  38. #include <stdlib.h>
  39. /*
  40. ** The "bool" integral type was defined by the C++ comittee in
  41. ** November of '94. Until the compiler supports this, use the following
  42. ** definition.
  43. */
  44. #ifndef __BORLANDC__
  45. #ifndef TRUE_FALSE_DEFINED
  46. #define TRUE_FALSE_DEFINED
  47. enum {false=0,true=1};
  48. typedef int bool;
  49. #endif
  50. #endif
  51. /*
  52. ** This is a CRC engine class. It will process submitted data and generate a CRC from it.
  53. ** Well, actually, the value returned is not a true CRC. However, it shares the same strength
  54. ** characteristic and is faster to generate than the traditional CRC. This object is treated like
  55. ** a method class. If it is called as a function (using the function operator), it will return
  56. ** the CRC value. There are other function operators to submit data for processing.
  57. */
  58. class CRCEngine {
  59. public:
  60. // Constructor for CRC engine (it can have an override initial CRC value).
  61. CRCEngine(long initial=0) : CRC(initial), Index(0) {
  62. StagingBuffer.Composite = 0;
  63. };
  64. // Fetches CRC value.
  65. long operator() (void) const {return(Value());};
  66. // Submits one byte sized datum to the CRC accumulator.
  67. void operator() (char datum);
  68. // Submits an arbitrary buffer to the CRC accumulator.
  69. long operator() (void const * buffer, int length);
  70. // Implicit conversion operator so this object appears like a 'long integer'.
  71. operator long(void) const {return(Value());};
  72. protected:
  73. bool Buffer_Needs_Data(void) const {
  74. return(Index != 0);
  75. };
  76. long Value(void) const {
  77. if (Buffer_Needs_Data()) {
  78. return(_lrotl(CRC, 1) + StagingBuffer.Composite);
  79. }
  80. return(CRC);
  81. };
  82. /*
  83. ** Current accumulator of the CRC value. This value doesn't take into
  84. ** consideration any pending data in the staging buffer.
  85. */
  86. long CRC;
  87. /*
  88. ** This is the sub index into the staging buffer used to keep track of
  89. ** partial data blocks as they are submitted to the CRC engine.
  90. */
  91. int Index;
  92. /*
  93. ** This is the buffer that holds the incoming partial data. When the buffer
  94. ** is filled, the value is transformed into the CRC and the buffer is flushed
  95. ** in preparation for additional data.
  96. */
  97. union {
  98. long Composite;
  99. char Buffer[sizeof(long)];
  100. } StagingBuffer;
  101. };
  102. #endif