crc32_small.c 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. ///////////////////////////////////////////////////////////////////////////////
  2. //
  3. /// \file crc32_small.c
  4. /// \brief CRC32 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. uint32_t lzma_crc32_table[1][256];
  14. static void
  15. crc32_init(void)
  16. {
  17. static const uint32_t poly32 = UINT32_C(0xEDB88320);
  18. for (size_t b = 0; b < 256; ++b) {
  19. uint32_t r = b;
  20. for (size_t i = 0; i < 8; ++i) {
  21. if (r & 1)
  22. r = (r >> 1) ^ poly32;
  23. else
  24. r >>= 1;
  25. }
  26. lzma_crc32_table[0][b] = r;
  27. }
  28. return;
  29. }
  30. extern void
  31. lzma_crc32_init(void)
  32. {
  33. mythread_once(crc32_init);
  34. return;
  35. }
  36. extern LZMA_API(uint32_t)
  37. lzma_crc32(const uint8_t *buf, size_t size, uint32_t crc)
  38. {
  39. lzma_crc32_init();
  40. crc = ~crc;
  41. while (size != 0) {
  42. crc = lzma_crc32_table[0][*buf++ ^ (crc & 0xFF)] ^ (crc >> 8);
  43. --size;
  44. }
  45. return ~crc;
  46. }