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.

256 lines
4.9 KiB

  1. #include "precomp.h"
  2. DEBUG_FILEZONE(ZONE_T120_T123PSTN);
  3. /* crc.cpp
  4. *
  5. * Copyright (c) 1994-1995 by DataBeam Corporation, Lexington, KY
  6. *
  7. * Abstract:
  8. * This is the implementation file for the CRC class.
  9. *
  10. * Private Instance Variables:
  11. * CRC_Table[] - Look-up table for pre-calc CRC values.
  12. * CRC_Poly - The algorithm's polynomial.
  13. * CRC_Init - Initial register value.
  14. * CRC_Check_Value - The value to compare the resulting received CRC.
  15. * Invert - Determines wether to invert the CRC value before
  16. * sending.
  17. * CRC_Register - CRC register during execution.
  18. *
  19. * Caveats:
  20. * None.
  21. *
  22. * Authors:
  23. * Marvin Nicholson
  24. */
  25. #include "crc.h"
  26. /* CRC::CRC()
  27. *
  28. * Public
  29. *
  30. * Functional Description:
  31. * The constructor fo CRC class initializes some member variables.
  32. */
  33. CRC::CRC()
  34. {
  35. CRC_Width = 16; /* Width of the CRC register. */
  36. CRC_Poly = 0x8408; /* Polynomial used in generating the CRC. */
  37. CRC_Init = 0xFFFF; /* Initial value of the CRC register. */
  38. Invert = TRUE; /* Enables 1's complement of CRC */
  39. if (Invert)
  40. {
  41. /*
  42. * If using 1's complement use this value to check incoming
  43. * CRC.
  44. */
  45. CRC_Check_Value = 0xF0B8;
  46. }
  47. else
  48. {
  49. /*
  50. * If not inverting CRC on transmittion, use this value to
  51. * check received CRC.
  52. */
  53. CRC_Check_Value = 0x0000;
  54. }
  55. CRCTableGenerator (CRC_Poly);
  56. }
  57. /* CRC::~CRC()
  58. *
  59. * Public
  60. *
  61. * Functional Description:
  62. * This is the destructor for the CRC class.
  63. */
  64. CRC::~CRC()
  65. {
  66. }
  67. /* ULONG CRC::OldCRCGenerator(HPUChar block_adr, ULONG block_len)
  68. *
  69. * Public
  70. *
  71. * Functional Description:
  72. * This routine computes the CRC value using standard bit-shifting.
  73. */
  74. ULONG CRC::OldCRCGenerator (
  75. LPBYTE block_adr,
  76. ULONG block_len)
  77. {
  78. Int i;
  79. UChar input_byte;
  80. USHORT byte_count;
  81. CRC_Register = (USHORT) CRC_Init;
  82. byte_count = 0;
  83. while(byte_count < block_len)
  84. {
  85. input_byte = *(block_adr + byte_count);
  86. CRC_Register ^= input_byte;
  87. for(i=0; i<8; i++)
  88. {
  89. if(CRC_Register & 1)
  90. {
  91. CRC_Register >>= 1;
  92. CRC_Register ^= (USHORT) CRC_Poly;
  93. }
  94. else
  95. CRC_Register >>= 1;
  96. }
  97. byte_count++;
  98. }
  99. if (Invert)
  100. return (CRC_Register ^ 0xFFFF);
  101. else
  102. return (CRC_Register);
  103. }
  104. /* ULONG CRC::CRCGenerator(HPUChar block_adr, ULONG block_len)
  105. *
  106. * Public
  107. *
  108. * Functional Descriprion:
  109. * This routine computes the CRC value using a look-up table.
  110. */
  111. ULONG CRC::CRCGenerator(
  112. LPBYTE block_adr,
  113. ULONG block_len)
  114. {
  115. CRC_Register = (USHORT) CRC_Init;
  116. while(block_len--)
  117. {
  118. CRC_Register =
  119. CRC_Table[(UChar) (((UChar) CRC_Register) ^ *block_adr++)] ^
  120. (CRC_Register >> 8);
  121. }
  122. if (Invert)
  123. return (CRC_Register ^ 0xFFFF);
  124. else
  125. return (CRC_Register);
  126. }
  127. /*
  128. * USHORT CRC::CRCTableValue(
  129. * Int index,
  130. * ULONG poly)
  131. *
  132. * Functional Description
  133. * This function generates a value that goes in the CRC_Table
  134. *
  135. * Formal Parameters
  136. * index (i) - Index into the table
  137. * poly (i) - Polynomial used to generate the value
  138. *
  139. * Return Value
  140. * Value generated.
  141. *
  142. * Side Effects
  143. * None
  144. *
  145. * Caveats
  146. * None
  147. */
  148. USHORT CRC::CRCTableValue(
  149. Int index,
  150. ULONG poly)
  151. {
  152. Int i;
  153. ULONG r;
  154. ULONG inbyte = (ULONG) index;
  155. r = inbyte;
  156. for(i=0; i<8; i++)
  157. {
  158. if (r & 1)
  159. r = (r >> 1) ^ poly;
  160. else
  161. r >>= 1;
  162. }
  163. return ((USHORT) r);
  164. }
  165. /*
  166. * void CRC::CRCTableGenerator (ULONG poly)
  167. *
  168. * Functional Description
  169. * This function generates the CRC table
  170. *
  171. * Formal Parameters
  172. * poly (i) - Polynomial used to generate the table
  173. *
  174. * Return Value
  175. * None
  176. *
  177. * Side Effects
  178. * None
  179. *
  180. * Caveats
  181. * None
  182. */
  183. void CRC::CRCTableGenerator (ULONG poly)
  184. {
  185. Int i;
  186. for(i=0; i<CRC_TABLE_SIZE; i++)
  187. CRC_Table[i] = CRCTableValue(i,poly);
  188. }
  189. /* BOOL CRC::CheckCRC(HPUChar block_adr, ULONG block_len)
  190. *
  191. * Public
  192. *
  193. * Functional Description:
  194. * This routine computes the CRC of a datablock and its associated CRC and
  195. * returns a TRUE value if the resulting CRC value is 0x0000
  196. * or 0xF0B8.
  197. */
  198. BOOL CRC::CheckCRC(
  199. LPBYTE block_adr,
  200. ULONG block_len)
  201. {
  202. CRC_Register = (USHORT) CRC_Init;
  203. while(block_len--)
  204. {
  205. CRC_Register =
  206. CRC_Table[(UChar) (((UChar) CRC_Register) ^ *block_adr++)] ^
  207. (CRC_Register >> 8);
  208. }
  209. if (CRC_Register == CRC_Check_Value)
  210. return TRUE;
  211. else
  212. return FALSE;
  213. }
  214. /*
  215. * void CRC::GetOverhead (
  216. * USHORT maximum_packet,
  217. * USHORT * new_maximum_packet)
  218. *
  219. * Public
  220. *
  221. * Functional Description:
  222. * This routine adds the number of overhead bytes generated by a CRC to
  223. * the packet size passed in.
  224. */
  225. void CRC::GetOverhead (
  226. USHORT maximum_packet,
  227. USHORT * new_maximum_packet)
  228. {
  229. *new_maximum_packet = maximum_packet + 2;
  230. }