forked from pig4210/xlib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
crc.cpp
87 lines (80 loc) · 2.16 KB
/
crc.cpp
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include "crc.h"
#include <stdlib.h>
static const unsigned __int16* GetCrc16Table()
{
static unsigned __int16 Crc16Table[0x100];
for(unsigned __int16 i = 0; i < _countof(Crc16Table); ++i)
{
unsigned __int16 crc = i;
for(size_t j = 0; j < 8; ++j)
{
crc = (crc >> 1) ^ ((crc & 1) ? 0xA001 : 0);
}
Crc16Table[i] = crc;
}
return Crc16Table;
}
unsigned __int16 crc16(const void* buf, const size_t size)
{
if(buf == nullptr) return 0;
static const unsigned __int16* const ct = GetCrc16Table();
unsigned __int16 ret = 0;
const unsigned char* _buf = (const unsigned char*)buf;
for(size_t i = 0; i < size; ++i)
{
ret = ct[(ret & 0xFF) ^ _buf[i]] ^ (ret >> 8);
}
return ret;
}
static const unsigned __int32* GetCrc32Table()
{
static unsigned __int32 Crc32Table[0x100];
for(unsigned __int32 i = 0; i < _countof(Crc32Table); ++i)
{
unsigned __int32 crc = i;
for(size_t j = 0; j < 8; ++j)
{
crc = (crc >> 1) ^ ((crc & 1) ? 0xEDB88320 : 0);
}
Crc32Table[i] = crc;
}
return Crc32Table;
}
unsigned __int32 crc32(const void* buf, const size_t size)
{
if(buf == nullptr) return 0;
static const unsigned __int32* const ct = GetCrc32Table();
unsigned __int32 ret = 0xFFFFFFFF;
const unsigned char* _buf = (const unsigned char*)buf;
for(size_t i = 0; i < size; ++i)
{
ret = ct[ (ret & 0xFF) ^ _buf[i] ] ^ (ret >> 8);
}
return ~ret;
}
static const unsigned __int64* GetCrc64Table()
{
static unsigned __int64 Crc64Table[0x100];
for(unsigned __int64 i = 0; i < _countof(Crc64Table); ++i)
{
unsigned __int64 crc = i;
for(size_t j = 0; j < 8; ++j)
{
crc = (crc >> 1) ^ ((crc & 1) ? 0xC96C5795D7870F42 : 0);
}
Crc64Table[i] = crc;
}
return Crc64Table;
}
unsigned __int64 crc64(const void* buf,const size_t size)
{
if(buf == nullptr) return 0;
static const unsigned __int64* const ct = GetCrc64Table();
unsigned __int64 ret = 0xFFFFFFFFFFFFFFFF;
const unsigned char* _buf = (const unsigned char*)buf;
for(size_t i = 0; i < size; ++i)
{
ret = ct[ (ret & 0xFF) ^ _buf[i] ] ^ (ret >> 8);
}
return ~ret;
}