40 #include "common/system.h" 47 CRCNormal(T poly, T init_remainder, T final_xor);
49 T crcFast(byte
const message[],
int nBytes)
const;
50 T processByte(byte byteVal, T remainder)
const;
51 T getInitRemainder()
const {
return _init_remainder; }
52 T finalize(T remainder)
const {
return remainder ^ _final_xor; }
55 const T _init_remainder;
66 T crcFast(byte
const message[],
int nBytes)
const;
67 T processByte(byte byteVal, T remainder)
const;
68 T getInitRemainder()
const {
return _reflected_init_remainder; }
69 T finalize(T remainder)
const {
return remainder ^ _final_xor; }
73 const T _reflected_init_remainder;
79 CRCNormal<T>::CRCNormal(T poly, T init_remainder, T final_xor) : _init_remainder(init_remainder), _final_xor(final_xor) {
80 const T topbit = 1ULL << (8 *
sizeof(T) - 1);
85 for (
int dividend = 0; dividend < 256; ++dividend) {
89 T remainder = dividend << (8 *
sizeof(T) - 8);
94 for (byte bit = 8; bit > 0; --bit) {
98 if (remainder & topbit) {
99 remainder = (remainder << 1) ^ poly;
101 remainder = (remainder << 1);
108 _crcTable[dividend] = remainder;
112 template <
typename T>
113 CRCReflected<T>::CRCReflected(T reflected_poly, T reflected_init_remainder, T final_xor) : _reflected_init_remainder(reflected_init_remainder), _final_xor(final_xor) {
117 for (
int dividend = 0; dividend < 256; ++dividend) {
121 T remainder = dividend;
126 for (byte bit = 8; bit > 0; --bit) {
131 remainder = (remainder >> 1) ^ reflected_poly;
133 remainder = (remainder >> 1);
140 _crcTable[dividend] = remainder;
157 T remainder = _init_remainder;
162 for (
int b = 0; b < nBytes; ++b) {
163 byte data = message[b] ^ (remainder >> (8 *
sizeof(T) - 8));
164 remainder = _crcTable[data] ^ (remainder << 8);
170 return remainder ^ _final_xor;
186 T remainder = _reflected_init_remainder;
191 for (
int b = 0; b < nBytes; ++b) {
192 byte data = message[b] ^ remainder;
193 remainder = _crcTable[data] ^ (remainder >> 8);
199 return remainder ^ _final_xor;
204 byte data = byteVal ^ (remainder >> (8 *
sizeof(T) - 8));
206 return _crcTable[data] ^ (remainder << 8);
211 byte data = byteVal ^ remainder;
213 return _crcTable[data] ^ (remainder >> 8);
Definition: algorithm.h:29