Source code of Windows XP (NT5)
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.

109 lines
2.4 KiB

  1. /* ------------------------------------------------------------------------ */
  2. /* */
  3. /* Copyright (c) Microsoft Corporation, 2000-2001. All rights reserved. */
  4. /* Copyright (c) Andrew Kadatch, 1991-2001. All rights reserved. */
  5. /* */
  6. /* Microsoft Confidential -- do not redistribute. */
  7. /* */
  8. /* ------------------------------------------------------------------------ */
  9. #include "xpress.h"
  10. #include "xprs.h"
  11. /* ------------------------ CRC-32 ---------------------------- */
  12. /* ------ */
  13. static uint32 crc32_table[256];
  14. static int crc32_initialized = 0;
  15. static void crc32_init (void)
  16. {
  17. xint i, j;
  18. uint32 k, m = 0xedb88320L;
  19. if (crc32_initialized) return;
  20. for (i = 0; i < 256; ++i)
  21. {
  22. k = i;
  23. j = 8;
  24. do {
  25. if ((k&1) == 0)
  26. k >>= 1;
  27. else
  28. {
  29. k >>= 1;
  30. k ^= m;
  31. };
  32. --j;
  33. } while (j);
  34. crc32_table[i] = k;
  35. }
  36. crc32_initialized = 1;
  37. }
  38. static uint32 crc32 (const uchar *p, xint n, uint32 k)
  39. {
  40. uint32 *table;
  41. if (n <= 0)
  42. return (k);
  43. k ^= (uint32) 0xffffffffL;
  44. table = crc32_table;
  45. #define X(i) k = table[((uchar) k) ^ p[i]] ^ (k >> 8)
  46. if ((n -= 32) >= 0) do
  47. {
  48. X(000); X(001); X(002); X(003); X(004); X(005); X(006); X(007);
  49. X(010); X(011); X(012); X(013); X(014); X(015); X(016); X(017);
  50. X(020); X(021); X(022); X(023); X(024); X(025); X(026); X(027);
  51. X(030); X(031); X(032); X(033); X(034); X(035); X(036); X(037);
  52. p += 32;
  53. }
  54. while ((n -= 32) >= 0);
  55. if ((n += 32) > 0) do
  56. {
  57. X (0);
  58. ++p;
  59. --n;
  60. }
  61. while (n);
  62. #undef X
  63. k ^= (uint32) 0xffffffffL;
  64. return (k);
  65. }
  66. int
  67. XPRESS_CALL
  68. XpressCrc32
  69. (
  70. const void *data, // beginning of data block
  71. int bytes, // number of bytes
  72. int crc // initial value
  73. )
  74. {
  75. if (!crc32_initialized)
  76. crc32_init ();
  77. return (crc32 (data, bytes, crc));
  78. }
  79. #if 0
  80. int main (void)
  81. {
  82. int i = 0;
  83. crc32_init ();
  84. printf ("{\n");
  85. for (;;)
  86. {
  87. printf (" 0x%08lx", (unsigned long) crc32_table[i]);
  88. ++i;
  89. if (i & 3)
  90. printf (",");
  91. else if (i == 256)
  92. break;
  93. else printf (",\n");
  94. }
  95. printf ("\n}\n");
  96. return (0);
  97. }
  98. #endif