CRC.CPP 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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.CPP 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.CPP *
  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. * CRCEngine::operator() -- Submits one byte of data to the CRC engine. *
  36. * CRCEngine::operator() -- Submits an arbitrary data block to the CRC engine. *
  37. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  38. #include "crc.h"
  39. /***********************************************************************************************
  40. * CRCEngine::operator() -- Submits one byte of data to the CRC engine. *
  41. * *
  42. * This routine will take the specified byte of data and submit it to the CRC engine *
  43. * for processing. This routine is designed to be as fast as possible since the typical *
  44. * use of this routine is to feed one of presumably many byte sized chunks of data to the *
  45. * CRC engine. *
  46. * *
  47. * INPUT: datum -- One byte of data to submit to the CRC engine. *
  48. * *
  49. * OUTPUT: none *
  50. * *
  51. * WARNINGS: If possible, use the buffer/size operator to submit data rather than repeated *
  52. * calls to this routine. *
  53. * *
  54. * HISTORY: *
  55. * 03/02/1996 JLB : Created. *
  56. *=============================================================================================*/
  57. void CRCEngine::operator() (char datum)
  58. {
  59. StagingBuffer.Buffer[Index++] = datum;
  60. if (Index == sizeof(long)) {
  61. CRC = Value();
  62. StagingBuffer.Composite = 0;
  63. Index = 0;
  64. }
  65. }
  66. /***********************************************************************************************
  67. * CRCEngine::operator() -- Submits an arbitrary data block to the CRC engine. *
  68. * *
  69. * This routine will submit the specified block to the CRC engine. The block can be of *
  70. * arbitrary length. *
  71. * *
  72. * INPUT: buffer -- Pointer to the buffer that contains the data. The buffer will not *
  73. * be modified. *
  74. * *
  75. * length -- The length of the buffer (in bytes). *
  76. * *
  77. * OUTPUT: Returns with the current CRC value accumulated so far. *
  78. * *
  79. * WARNINGS: none *
  80. * *
  81. * HISTORY: *
  82. * 03/02/1996 JLB : Created. *
  83. *=============================================================================================*/
  84. long CRCEngine::operator() (void const * buffer, int length)
  85. {
  86. if (buffer != NULL && length > 0) {
  87. char const * dataptr = (char const *)buffer;
  88. int bytes_left = length;
  89. /*
  90. ** If there are any leader bytes (needed to fill the staging buffer)
  91. ** then process those by first using them to fill up the staging
  92. ** buffer. The bulk of the data block will be processed by the high
  93. ** speed longword processing loop.
  94. */
  95. while (bytes_left && Buffer_Needs_Data()) {
  96. operator()(*dataptr);
  97. dataptr++;
  98. bytes_left--;
  99. }
  100. /*
  101. ** Perform the fast 'bulk' processing by reading long word sized
  102. ** data blocks.
  103. */
  104. long const * longptr = (long const *)dataptr;
  105. int longcount = bytes_left / sizeof(long); // Whole 'long' elements remaining.
  106. while (longcount--) {
  107. CRC = _lrotl(CRC, 1) + *longptr++;
  108. bytes_left -= sizeof(long);
  109. }
  110. /*
  111. ** If there are remainder bytes, then process these by adding them
  112. ** to the staging buffer.
  113. */
  114. dataptr = (char const *)longptr;
  115. while (bytes_left) {
  116. operator()(*dataptr);
  117. dataptr++;
  118. bytes_left--;
  119. }
  120. }
  121. /*
  122. ** Return the current CRC value.
  123. */
  124. return(Value());
  125. }