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.

72 lines
2.0 KiB

  1. #ifndef _CRC32_H_
  2. #define _CRC32_H_
  3. #ifndef INLINE
  4. #define INLINE __inline
  5. #endif
  6. extern const ULONG CrcTable32[ 256 ]; // defined in crctable.c
  7. VOID GenerateCrc32Table( VOID ); // stubbed in crctable.c, but not used
  8. // (exists for compatibility)
  9. ULONG
  10. INLINE
  11. Crc32(
  12. IN ULONG InitialCrc,
  13. IN PVOID Buffer,
  14. IN ULONG ByteCount
  15. )
  16. {
  17. #ifdef _X86_
  18. __asm {
  19. mov ecx, ByteCount ; number of bytes in buffer
  20. xor ebx, ebx ; ebx (bl) will be our table index
  21. mov esi, Buffer ; buffer pointer
  22. test ecx, ecx ; test for zero length buffer
  23. mov eax, InitialCrc ; CRC-32 value
  24. jnz short loopentry ; if non-zero buffer, start loop
  25. jmp short exitfunc ; else exit (crc already in eax)
  26. looptop: shr eax, 8 ; (crc>>8) (U1)
  27. mov edx, CrcTable32[ebx*4] ; fetch Table[ index ] (V1)
  28. xor eax, edx ; crc=(crc>>8)^Table[index] (U1)
  29. loopentry: mov bl, [esi] ; fetch next *buffer (V1)
  30. inc esi ; buffer++ (U1)
  31. xor bl, al ; index=(byte)crc^*buffer (V1)
  32. dec ecx ; adjust counter (U1)
  33. jnz short looptop ; loop while nBytes (V1)
  34. shr eax, 8 ; remaining math on last byte
  35. xor eax, CrcTable32[ebx*4] ; eax returns new crc value
  36. exitfunc:
  37. }
  38. #else // ! _X86_
  39. ULONG Value = InitialCrc;
  40. ULONG Count = ByteCount;
  41. PUCHAR p = Buffer;
  42. while ( Count-- ) {
  43. Value = ( Value >> 8 ) ^ CrcTable32[ (UCHAR)( *p++ ^ Value ) ];
  44. }
  45. return Value;
  46. #endif // ! _X86_
  47. }
  48. #endif // _CRC32_H_