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.

75 lines
1.2 KiB

  1. /*++
  2. Copyright (c) 1998-1999, Microsoft Corporation
  3. Module Name:
  4. Crc32.cpp
  5. Abstract:
  6. --*/
  7. #include "Crc32.h"
  8. CCrc32::CCrc32()
  9. {
  10. uClassID = CCRC32_CLASSID;
  11. InitialiseCRCTable();
  12. }
  13. CCrc32::~CCrc32()
  14. {
  15. }
  16. VOID CCrc32::InitialiseCRCTable()
  17. {
  18. INT nIndex;
  19. INT nBitIndex;
  20. ULONG uTableValue;
  21. for ( nIndex = 0; nIndex < 256; nIndex++ )
  22. {
  23. uTableValue = nIndex;
  24. for ( nBitIndex = 0; nBitIndex < 8; nBitIndex++ )
  25. {
  26. if ( ( uTableValue & 1 ) == 1 )
  27. {
  28. uTableValue = ( uTableValue >> 1 ) ^ CRC32_POLYNOMIAL;
  29. } else
  30. {
  31. uTableValue = uTableValue >> 1;
  32. }
  33. }
  34. m_uCRC32Table[ nIndex ] = uTableValue;
  35. }
  36. }
  37. ULONG CCrc32::CalculateBlockCRC(LPVOID lpvBlock, INT nBlockLength)
  38. {
  39. INT nIndex;
  40. LPBYTE lpbBlock;
  41. ULONG uCRCValue;
  42. _ASSERT( nBlockLength > 0 );
  43. lpbBlock = (LPBYTE)lpvBlock;
  44. uCRCValue = 0xFFFFFFFFL;
  45. for ( nIndex = 0; nIndex < nBlockLength; nIndex++ )
  46. {
  47. uCRCValue = ( ( uCRCValue >> 8 ) & 0x00FFFFFFL ) ^ ( m_uCRC32Table[ ( uCRCValue ^ lpbBlock[ nIndex ] ) & 0xFFL ] );
  48. }
  49. uCRCValue = uCRCValue ^ 0xFFFFFFFFL;
  50. return uCRCValue;
  51. }