crc64_small.c 966 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. ///////////////////////////////////////////////////////////////////////////////
  2. //
  3. /// \file crc64_small.c
  4. /// \brief CRC64 calculation (size-optimized)
  5. //
  6. // Author: Lasse Collin
  7. //
  8. // This file has been put into the public domain.
  9. // You can do whatever you want with this file.
  10. //
  11. ///////////////////////////////////////////////////////////////////////////////
  12. #include "check.h"
  13. static uint64_t crc64_table[256];
  14. static void
  15. crc64_init(void)
  16. {
  17. static const uint64_t poly64 = UINT64_C(0xC96C5795D7870F42);
  18. for (size_t b = 0; b < 256; ++b) {
  19. uint64_t r = b;
  20. for (size_t i = 0; i < 8; ++i) {
  21. if (r & 1)
  22. r = (r >> 1) ^ poly64;
  23. else
  24. r >>= 1;
  25. }
  26. crc64_table[b] = r;
  27. }
  28. return;
  29. }
  30. extern LZMA_API(uint64_t)
  31. lzma_crc64(const uint8_t *buf, size_t size, uint64_t crc)
  32. {
  33. mythread_once(crc64_init);
  34. crc = ~crc;
  35. while (size != 0) {
  36. crc = crc64_table[*buf++ ^ (crc & 0xFF)] ^ (crc >> 8);
  37. --size;
  38. }
  39. return ~crc;
  40. }