summaryrefslogtreecommitdiff
path: root/plugins/FileAsMessage/src/crc32.cpp
blob: 9bf7a63d2dea5a839825d8979b486fa886db1ba1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include "stdafx.h"

const ulong CRCPoly = 0xEDB88320;
ulong CRC32Table[256];

void InitCRC32()
{
	for (UINT32 i = 0; i < 256; i++)
	{
		UINT32 r = i;
		for (int j = 0; j < 8; j++)
			if (r & 1)
				r = (r >> 1) ^ CRCPoly;
			else
				r >>= 1;
		CRC32Table[i] = r;
	}
}

const ulong INITCRC = -1L;

inline ulong UpdateCRC32(uchar val, ulong crc)
{
	return CRC32Table[(uchar)crc^val] ^ (crc >> 8);
}

ulong memcrc32(uchar *ptr, int size, ulong crc)
{
	while (size--) crc = UpdateCRC32(*ptr++, crc);
	return crc;
}