CRC.CPP 7.4 KB

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