CRC.H 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. //
  2. // Copyright 2020 Electronic Arts Inc.
  3. //
  4. // TiberianDawn.DLL and RedAlert.dll and corresponding source code is free
  5. // software: you can redistribute it and/or modify it under the terms of
  6. // the GNU General Public License as published by the Free Software Foundation,
  7. // either version 3 of the License, or (at your option) any later version.
  8. // TiberianDawn.DLL and RedAlert.dll and corresponding source code is distributed
  9. // in the hope that it will be useful, but with permitted additional restrictions
  10. // under Section 7 of the GPL. See the GNU General Public License in LICENSE.TXT
  11. // distributed with this program. You should have received a copy of the
  12. // GNU General Public License along with permitted additional restrictions
  13. // with this program. If not, see https://github.com/electronicarts/CnC_Remastered_Collection
  14. /* $Header: /CounterStrike/CRC.H 1 3/03/97 10:24a Joe_bostic $ */
  15. /***********************************************************************************************
  16. *** 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 ***
  17. ***********************************************************************************************
  18. * *
  19. * Project Name : Command & Conquer *
  20. * *
  21. * File Name : CRC.H *
  22. * *
  23. * Programmer : Joe L. Bostic *
  24. * *
  25. * Start Date : 03/02/96 *
  26. * *
  27. * Last Update : March 2, 1996 [JLB] *
  28. * *
  29. *---------------------------------------------------------------------------------------------*
  30. * Functions: *
  31. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  32. #ifndef CRC_H
  33. #define CRC_H
  34. #include <stdlib.h>
  35. /*
  36. ** The "bool" integral type was defined by the C++ comittee in
  37. ** November of '94. Until the compiler supports this, use the following
  38. ** definition.
  39. */
  40. #ifndef __BORLANDC__
  41. #ifndef TRUE_FALSE_DEFINED
  42. #define TRUE_FALSE_DEFINED
  43. enum {false=0,true=1};
  44. typedef int bool;
  45. #endif
  46. #endif
  47. /*
  48. ** This is a CRC engine class. It will process submitted data and generate a CRC from it.
  49. ** Well, actually, the value returned is not a true CRC. However, it shares the same strength
  50. ** characteristic and is faster to generate than the traditional CRC. This object is treated like
  51. ** a method class. If it is called as a function (using the function operator), it will return
  52. ** the CRC value. There are other function operators to submit data for processing.
  53. */
  54. class CRCEngine {
  55. public:
  56. // Constructor for CRC engine (it can have an override initial CRC value).
  57. CRCEngine(long initial=0) : CRC(initial), Index(0) {
  58. StagingBuffer.Composite = 0;
  59. };
  60. // Fetches CRC value.
  61. long operator() (void) const {return(Value());};
  62. // Submits one byte sized datum to the CRC accumulator.
  63. void operator() (char datum);
  64. // Submits an arbitrary buffer to the CRC accumulator.
  65. long operator() (void const * buffer, int length);
  66. // Implicit conversion operator so this object appears like a 'long integer'.
  67. operator long(void) const {return(Value());};
  68. protected:
  69. bool Buffer_Needs_Data(void) const {
  70. return(Index != 0);
  71. };
  72. long Value(void) const {
  73. if (Buffer_Needs_Data()) {
  74. return(_lrotl(CRC, 1) + StagingBuffer.Composite);
  75. }
  76. return(CRC);
  77. };
  78. /*
  79. ** Current accumulator of the CRC value. This value doesn't take into
  80. ** consideration any pending data in the staging buffer.
  81. */
  82. long CRC;
  83. /*
  84. ** This is the sub index into the staging buffer used to keep track of
  85. ** partial data blocks as they are submitted to the CRC engine.
  86. */
  87. int Index;
  88. /*
  89. ** This is the buffer that holds the incoming partial data. When the buffer
  90. ** is filled, the value is transformed into the CRC and the buffer is flushed
  91. ** in preparation for additional data.
  92. */
  93. union {
  94. long Composite;
  95. char Buffer[sizeof(long)];
  96. } StagingBuffer;
  97. };
  98. #endif