crc.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*
  2. ** Command & Conquer Generals(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. #include <iostream>
  19. #include <signal.h>
  20. #ifdef _WINDOWS
  21. #include <process.h> // *MUST* be included before ANY Wnet/Wlib headers if _REENTRANT is defined
  22. #endif
  23. #include "crc.h"
  24. #include "configfile.h"
  25. #include "threadfac.h"
  26. #include "endian.h"
  27. #include "xtime.h"
  28. #include <filed.h>
  29. #include <wstring.h>
  30. #include <wdebug.h>
  31. #include <udp.h>
  32. /***************************************************************************
  33. * Add_CRC -- Adds a value to a CRC *
  34. * *
  35. * INPUT: *
  36. * crc ptr to crc *
  37. * val value to add *
  38. * *
  39. * OUTPUT: *
  40. * none *
  41. * *
  42. * WARNINGS: *
  43. * none *
  44. * *
  45. * HISTORY: *
  46. * 05/09/1995 BRR : Created. *
  47. *=========================================================================*/
  48. void Add_CRC(unsigned long *crc, unsigned char val)
  49. {
  50. int hibit;
  51. //cout << "\t\t" << hex << val;
  52. // val = htonl(val);
  53. //cout << " / " << hex << val <<endl;
  54. if ((*crc) & 0x80000000) {
  55. hibit = 1;
  56. } else {
  57. hibit = 0;
  58. }
  59. (*crc) <<= 1;
  60. (*crc) += val;
  61. (*crc) += hibit;
  62. //cout << hex << (*crc) <<endl;
  63. }
  64. void Build_Packet_CRC(unsigned char *buf, int len)
  65. {
  66. if (len < 5)
  67. {
  68. DBGMSG("Ack! Constructing a packet too small to hold a CRC!");
  69. return;
  70. }
  71. if (!buf)
  72. {
  73. DBGMSG("Ack! Constructing a CRC for a void *");
  74. return;
  75. }
  76. *((unsigned long *)buf) = 0;
  77. unsigned long *crc_ptr = (unsigned long *)buf;
  78. unsigned char *packetptr = (unsigned char*) (buf+4);
  79. len -= 4; // look past CRC
  80. for (int i=0 ; i<len ; i++) {
  81. Add_CRC (crc_ptr, *packetptr++);
  82. }
  83. /*
  84. int leftover = len & 3;
  85. if (leftover) {
  86. unsigned long val = 0;
  87. unsigned char *c = (unsigned char *)packetptr;
  88. for (int i=0; i<leftover; i++)
  89. {
  90. val += (c[i] << (i*8));
  91. }
  92. val = htonl(val);
  93. Add_CRC (crc_ptr, val);
  94. }
  95. */
  96. *crc_ptr = htonl(*crc_ptr);
  97. }
  98. /***********************************************************************************************
  99. * Passes_CRC_Check -- Checks the CRC for a packet *
  100. * *
  101. * *
  102. * *
  103. * INPUT: ptr to packet *
  104. * *
  105. * OUTPUT: true if packet passes CRC check *
  106. * *
  107. * WARNINGS: None *
  108. * *
  109. * HISTORY: *
  110. * 10/5/99 1:26PM ST : Created *
  111. * 1/9/2001 2:21PM MDC: Ripped from RA2 (WinsockInterfaceClass in wsproto.cpp/queue.cpp) *
  112. * 1/31/2001 4:30PM MDC: Converted to network-byte-order so Sparc boxes can talk with Intels. *
  113. * 2/1/2001 4:07PM MDC: Converted back to Intel order to avoid messing with C&C packets *
  114. *=============================================================================================*/
  115. bool Passes_CRC_Check(unsigned char *buf, int len)
  116. {
  117. if (len < 5)
  118. {
  119. DBGMSG("Recieved packet too small to contain a CRC");
  120. return false;
  121. }
  122. if (!buf)
  123. {
  124. DBGMSG("Ack! Checking a CRC for a void *");
  125. return false;
  126. }
  127. unsigned long crc = 0;
  128. unsigned long *crc_ptr = &crc;
  129. unsigned char *packetptr = (unsigned char*) (buf+4);
  130. len -= 4; // remove the CRC from packet size - just look at payload
  131. for (int i=0 ; i<len ; i++) {
  132. Add_CRC (crc_ptr, *packetptr++);
  133. }
  134. /*
  135. int leftover = len & 3;
  136. if (leftover) {
  137. unsigned long val = 0;
  138. unsigned char *c = (unsigned char *)packetptr;
  139. for (int i=0; i<leftover; i++)
  140. {
  141. val += (c[i] << (i*8));
  142. }
  143. val = htonl(val);
  144. Add_CRC (crc_ptr, val);
  145. }
  146. */
  147. crc = htonl(crc);
  148. if (crc == *((unsigned long *)buf)) {
  149. return (true);
  150. }
  151. DBGMSG("Invalid packet CRC");
  152. return (false);
  153. }