jadeite/game_payload/include/crc32.h

27 lines
538 B
C
Raw Normal View History

2023-06-05 21:23:08 +00:00
#pragma once
// Modified from https://stackoverflow.com/a/27950866
#include <stddef.h>
#include <stdint.h>
/* CRC-32C (iSCSI) polynomial in reversed bit order. */
#define __POLY 0x82f63b78
2023-08-04 19:17:31 +00:00
static inline uint32_t crc32c(uint32_t crc, const void *buf, size_t len) {
const unsigned char *cbuf = (const unsigned char*)buf;
2023-06-05 21:23:08 +00:00
crc = ~crc;
while (len--) {
2023-08-04 19:17:31 +00:00
crc ^= *cbuf++;
2023-06-05 21:23:08 +00:00
for (int k = 0; k < 8; k++) {
crc = crc & 1 ? (crc >> 1) ^ __POLY : crc >> 1;
}
}
return ~crc;
}
#undef __POLY