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.

79 lines
1.5 KiB

  1. /*
  2. * CRC32.C -- CRC32 computation
  3. */
  4. #include "crc32.h"
  5. long crc32Table[256];
  6. /*
  7. * GenerateCRC32Table - Construct CRC-32 constant table
  8. *
  9. * We construct the table on-the-fly because the code needed
  10. * to do build it is much smaller than the table it creates.
  11. */
  12. void GenerateCRC32Table(void)
  13. {
  14. int iIndex;
  15. int cBit;
  16. long shiftIn;
  17. long shiftOut;
  18. for (iIndex = 0; iIndex < 256; iIndex++)
  19. {
  20. shiftOut = iIndex;
  21. shiftIn = 0;
  22. for (cBit = 0; cBit < 8; cBit++)
  23. {
  24. shiftIn <<= 1;
  25. shiftIn |= (shiftOut & 1);
  26. shiftOut >>= 1;
  27. }
  28. shiftIn <<= 24;
  29. for (cBit = 0; cBit < 8; cBit++)
  30. {
  31. if (shiftIn & 0x80000000L)
  32. {
  33. shiftIn = (shiftIn << 1) ^ 0x04C11DB7L;
  34. }
  35. else
  36. {
  37. shiftIn <<= 1;
  38. }
  39. }
  40. for (cBit = 0; cBit < 32; cBit++)
  41. {
  42. shiftOut <<= 1;
  43. shiftOut |= (shiftIn & 1);
  44. shiftIn >>= 1;
  45. }
  46. crc32Table[iIndex] = shiftOut;
  47. }
  48. }
  49. /*
  50. * update CRC32 accumulator from contents of a buffer
  51. */
  52. void CRC32Update(unsigned long *pCRC32,void *p,unsigned long cb)
  53. {
  54. unsigned char *pb = p;
  55. unsigned long crc32;
  56. crc32 = (-1L - *pCRC32);
  57. while (cb--)
  58. {
  59. crc32 = crc32Table[(unsigned char)crc32 ^ *pb++] ^
  60. ((crc32 >> 8) & 0x00FFFFFFL);
  61. }
  62. *pCRC32 = (-1L - crc32);
  63. }