Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

201 lines
6.1 KiB

  1. #include <pch.cpp>
  2. #pragma hdrstop
  3. /*
  4. * $Header: /entproj/all/base/etfile/crcutil.c_v 1.3 Wed Dec 07 15:05:18 1994 markbc $
  5. * $Log: /entproj/all/base/etfile/crcutil.c_v $
  6. *
  7. * Rev 1.3 Wed Dec 07 15:05:18 1994 markbc
  8. * Alpha port checkin
  9. *
  10. * Rev 1.2 19 Oct 1994 15:44:26 chucker
  11. * Synced up headers with the code.
  12. *
  13. * Rev 1.1 18 Aug 1994 15:09:00 DILKIE
  14. *
  15. * Rev 1.0 11 Aug 1994 10:11:36 JackK
  16. * Initial file check in
  17. */
  18. /***********************************************************************
  19. * CRC utility routines for general 16 and 32 bit CRCs.
  20. *
  21. * 1990 Gary P. Mussar
  22. * This code is released to the public domain. There are no restrictions,
  23. * however, acknowledging the author by keeping this comment around
  24. * would be appreciated.
  25. ***********************************************************************/
  26. //#include "strcore.h"
  27. #include <crcutil.h>
  28. /***********************************************************************
  29. * Utilities for fast CRC using table lookup
  30. *
  31. * CRC calculations are performed one byte at a time using a table lookup
  32. * mechanism. Two routines are provided: one to initialize the CRC table;
  33. * and one to perform the CRC calculation over an array of bytes.
  34. *
  35. * A CRC is the remainder produced by dividing a generator polynomial into
  36. * a data polynomial using binary arthimetic (XORs). The data polynomial
  37. * is formed by using each bit of the data as a coefficient of a term in
  38. * the polynomial. These utilities assume the data communications ordering
  39. * of bits for the data polynomial, ie. the LSB of the first byte of data
  40. * is the coefficient of the highest term of the polynomial, etc..
  41. *
  42. * I_CRCxx - Initialize the 256 entry CRC lookup table based on the
  43. * specified generator polynomial.
  44. * Input:
  45. * Table[256] - Lookup table
  46. * *GenPolynomial - Pointer to generator polynomial
  47. *
  48. * F_CRCxx - Calculate CRC over an array of characters using fast
  49. * table lookup.
  50. * Input:
  51. * Table[256] - Lookup table
  52. * *CRC - Pointer to the variable containing the result of
  53. * CRC calculations of previous characters. The CRC
  54. * variable must be initialized to a known value
  55. * before the first call to this routine.
  56. * *dataptr - Pointer to array of characters to be included in
  57. * the CRC calculation.
  58. * count - Number of characters in the array.
  59. ***********************************************************************/
  60. void I_CRC16( CRC16 Table[256],
  61. CRC16 *GenPolynomial )
  62. {
  63. int i;
  64. CRC16 crc, poly;
  65. for (poly=*GenPolynomial, i=0; i<256; i++) {
  66. crc = (CRC16) i;
  67. crc = (CRC16) ((crc >> 1) ^ ((crc & 1) ? poly : 0));
  68. crc = (CRC16) ((crc >> 1) ^ ((crc & 1) ? poly : 0));
  69. crc = (CRC16) ((crc >> 1) ^ ((crc & 1) ? poly : 0));
  70. crc = (CRC16) ((crc >> 1) ^ ((crc & 1) ? poly : 0));
  71. crc = (CRC16) ((crc >> 1) ^ ((crc & 1) ? poly : 0));
  72. crc = (CRC16) ((crc >> 1) ^ ((crc & 1) ? poly : 0));
  73. crc = (CRC16) ((crc >> 1) ^ ((crc & 1) ? poly : 0));
  74. crc = (CRC16) ((crc >> 1) ^ ((crc & 1) ? poly : 0));
  75. Table[i] = crc;
  76. }
  77. }
  78. void I_CRC32( CRC32 Table[256],
  79. CRC32 *GenPolynomial )
  80. {
  81. int i;
  82. CRC32 crc, poly;
  83. for (poly=*GenPolynomial, i=0; i<256; i++)
  84. {
  85. crc = (CRC32) i;
  86. crc = (crc >> 1) ^ ((crc & 1) ? poly : 0);
  87. crc = (crc >> 1) ^ ((crc & 1) ? poly : 0);
  88. crc = (crc >> 1) ^ ((crc & 1) ? poly : 0);
  89. crc = (crc >> 1) ^ ((crc & 1) ? poly : 0);
  90. crc = (crc >> 1) ^ ((crc & 1) ? poly : 0);
  91. crc = (crc >> 1) ^ ((crc & 1) ? poly : 0);
  92. crc = (crc >> 1) ^ ((crc & 1) ? poly : 0);
  93. crc = (crc >> 1) ^ ((crc & 1) ? poly : 0);
  94. Table[i] = crc;
  95. }
  96. }
  97. void F_CRC16( CRC16 Table[256],
  98. CRC16 *CRC,
  99. const void *dataptr,
  100. unsigned int count )
  101. {
  102. CRC16 temp_crc;
  103. unsigned char *p;
  104. p = (unsigned char *)dataptr;
  105. for (temp_crc = *CRC; count; count--) {
  106. temp_crc = (CRC16) ((temp_crc >> 8) ^ Table[(temp_crc & 0xff) ^ *p++]);
  107. }
  108. *CRC = temp_crc;
  109. }
  110. void F_CRC32( CRC32 Table[256],
  111. CRC32 *CRC,
  112. const void *dataptr,
  113. unsigned int count )
  114. {
  115. CRC32 temp_crc;
  116. unsigned char *p;
  117. p = (unsigned char *)dataptr;
  118. for (temp_crc = *CRC; count; count--)
  119. {
  120. temp_crc = (temp_crc >> 8) ^ Table[(temp_crc & 0xff) ^ *p++];
  121. }
  122. *CRC = temp_crc;
  123. }
  124. /***********************************************************************
  125. * Utility CRC using slower, smaller non-table lookup method
  126. *
  127. * S_CRCxx - Calculate CRC over an array of characters using slower but
  128. * smaller non-table lookup method.
  129. * Input:
  130. * GenPolynomial - Generator polynomial
  131. * *CRC - Pointer to the variable containing the result of
  132. * CRC calculations of previous characters. The CRC
  133. * variable must be initialized to a known value
  134. * before the first call to this routine.
  135. * *dataptr - Pointer to array of characters to be included in
  136. * the CRC calculation.
  137. * count - Number of characters in the array.
  138. ***********************************************************************/
  139. void S_CRC16( CRC16 *GenPolynomial,
  140. CRC16 *CRC,
  141. const void *dataptr,
  142. unsigned int count )
  143. {
  144. int i;
  145. CRC16 temp_crc, poly;
  146. unsigned char *p;
  147. p = (unsigned char *)dataptr;
  148. for (poly=*GenPolynomial, temp_crc = *CRC; count; count--)
  149. {
  150. temp_crc ^= *p++;
  151. for (i=0; i<8; i++) {
  152. temp_crc = (CRC16) ((temp_crc >> 1) ^ ((temp_crc & 1) ? poly : 0));
  153. }
  154. }
  155. *CRC = temp_crc;
  156. }
  157. void S_CRC32( CRC32 *GenPolynomial,
  158. CRC32 *CRC,
  159. const void *dataptr,
  160. unsigned int count )
  161. {
  162. int i;
  163. CRC32 temp_crc, poly;
  164. unsigned char *p;
  165. p = (unsigned char *)dataptr;
  166. for (poly=*GenPolynomial, temp_crc = *CRC; count; count--)
  167. {
  168. temp_crc ^= *p++;
  169. for (i=0; i<8; i++)
  170. {
  171. temp_crc = (temp_crc >> 1) ^ ((temp_crc & 1) ? poly : 0);
  172. }
  173. }
  174. *CRC = temp_crc;
  175. }