crc32.h 538 B

123456789101112131415161718192021222324252627
  1. #pragma once
  2. // Modified from https://stackoverflow.com/a/27950866
  3. #include <stddef.h>
  4. #include <stdint.h>
  5. /* CRC-32C (iSCSI) polynomial in reversed bit order. */
  6. #define __POLY 0x82f63b78
  7. static inline uint32_t crc32c(uint32_t crc, const void *buf, size_t len) {
  8. const unsigned char *cbuf = (const unsigned char*)buf;
  9. crc = ~crc;
  10. while (len--) {
  11. crc ^= *cbuf++;
  12. for (int k = 0; k < 8; k++) {
  13. crc = crc & 1 ? (crc >> 1) ^ __POLY : crc >> 1;
  14. }
  15. }
  16. return ~crc;
  17. }
  18. #undef __POLY