2013/10/30(水)コピペ用 Crc32

はてブ数 2013/10/30 01:08 プログラミング::C++つーさ

コピペ用CRC32

#include <stdint.h>
uint32_t ComputeCRC32(void *data, size_t len, uint32_t crc = 0);
uint32_t ComputeCRC32(void *data, size_t len, uint32_t crc)
{
	static uint32_t tbl[256] = { 1 };
	if (tbl[0] == 1) for (uint32_t i = 0; i < 256; i++) { uint32_t c = i; for (int j = 0; j < 8; j++) c = (c & 1) ? (0xEDB88320 ^ (c >> 1)) : (c >> 1); tbl[i] = c; }
	uint32_t c = crc ^ 0xFFFFFFFF;
	for (size_t i = 0; i < len; i++) { c = tbl[(c ^ static_cast<uint8_t*>(data)[i]) & 0xFF] ^ (c >> 8); }
	return c ^ 0xFFFFFFFF;
}

↓おもしろいです
http://codegolf.stackexchange.com/questions/3268/compute-the-crc32-table-at-compile-time