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.

143 lines
4.3 KiB

  1. /****************************************************************************/
  2. /* Start of crcmodel.c */
  3. /****************************************************************************/
  4. /* */
  5. /* Author : Ross Williams ([email protected].). */
  6. /* Date : 3 June 1993. */
  7. /* Status : Public domain. */
  8. /* */
  9. /* Description : This is the implementation (.c) file for the reference */
  10. /* implementation of the Rocksoft^tm Model CRC Algorithm. For more */
  11. /* information on the Rocksoft^tm Model CRC Algorithm, see the document */
  12. /* titled "A Painless Guide to CRC Error Detection Algorithms" by Ross */
  13. /* Williams ([email protected].). This document is likely to be in */
  14. /* "ftp.adelaide.edu.au/pub/rocksoft". */
  15. /* */
  16. /* Note: Rocksoft is a trademark of Rocksoft Pty Ltd, Adelaide, Australia. */
  17. /* */
  18. /****************************************************************************/
  19. /* */
  20. /* Implementation Notes */
  21. /* -------------------- */
  22. /* To avoid inconsistencies, the specification of each function is not */
  23. /* echoed here. See the header file for a description of these functions. */
  24. /* This package is light on checking because I want to keep it short and */
  25. /* simple and portable (i.e. it would be too messy to distribute my entire */
  26. /* C culture (e.g. assertions package) with this package. */
  27. /* */
  28. /****************************************************************************/
  29. #include "crcmodel.h"
  30. /****************************************************************************/
  31. /* The following definitions make the code more readable. */
  32. #define BITMASK(X) (1L << (X))
  33. #define MASK32 0xFFFFFFFFL
  34. #define LOCAL static
  35. /****************************************************************************/
  36. /* Returns the value v with the bottom b [0,32] bits reflected. */
  37. /* Example: reflect(0x3e23L,3) == 0x3e26 */
  38. LOCAL ulong reflect(
  39. ulong v,
  40. int b)
  41. {
  42. int i;
  43. ulong t = v;
  44. for (i=0; i<b; i++)
  45. {
  46. if (t & 1L)
  47. v|= BITMASK((b-1)-i);
  48. else
  49. v&= ~BITMASK((b-1)-i);
  50. t>>=1;
  51. }
  52. return v;
  53. }
  54. /****************************************************************************/
  55. /* Returns a longword whose value is (2^p_cm->cm_width)-1. */
  56. /* The trick is to do this portably (e.g. without doing <<32). */
  57. LOCAL ulong widmask(p_cm_t p_cm)
  58. {
  59. return (((1L<<(p_cm->cm_width-1))-1L)<<1)|1L;
  60. }
  61. /****************************************************************************/
  62. void cm_ini (p_cm_t p_cm)
  63. {
  64. p_cm->cm_reg = p_cm->cm_init;
  65. }
  66. /****************************************************************************/
  67. void cm_nxt(p_cm_t p_cm, int ch)
  68. {
  69. int i;
  70. ulong uch = (ulong) ch;
  71. ulong topbit = BITMASK(p_cm->cm_width-1);
  72. if (p_cm->cm_refin) uch = reflect(uch,8);
  73. p_cm->cm_reg ^= (uch << (p_cm->cm_width-8));
  74. for (i=0; i<8; i++)
  75. {
  76. if (p_cm->cm_reg & topbit)
  77. p_cm->cm_reg = (p_cm->cm_reg << 1) ^ p_cm->cm_poly;
  78. else
  79. p_cm->cm_reg <<= 1;
  80. p_cm->cm_reg &= widmask(p_cm);
  81. }
  82. }
  83. /****************************************************************************/
  84. void cm_blk(
  85. p_cm_t p_cm,
  86. p_ubyte_ blk_adr,
  87. ulong blk_len)
  88. {
  89. while (blk_len--) cm_nxt(p_cm,*blk_adr++);
  90. }
  91. /****************************************************************************/
  92. ulong cm_crc(p_cm_t p_cm)
  93. {
  94. if (p_cm->cm_refot)
  95. return p_cm->cm_xorot ^ reflect(p_cm->cm_reg,p_cm->cm_width);
  96. else
  97. return p_cm->cm_xorot ^ p_cm->cm_reg;
  98. }
  99. /****************************************************************************/
  100. ulong cm_tab(p_cm_t p_cm, int index)
  101. {
  102. int i;
  103. ulong r;
  104. ulong topbit = BITMASK(p_cm->cm_width-1);
  105. ulong inbyte = (ulong) index;
  106. if (p_cm->cm_refin) inbyte = reflect(inbyte,8);
  107. r = inbyte << (p_cm->cm_width-8);
  108. for (i=0; i<8; i++)
  109. if (r & topbit)
  110. r = (r << 1) ^ p_cm->cm_poly;
  111. else
  112. r<<=1;
  113. if (p_cm->cm_refin) r = reflect(r,p_cm->cm_width);
  114. return r & widmask(p_cm);
  115. }
  116. /****************************************************************************/
  117. /* End of crcmodel.c */
  118. /****************************************************************************/