michael@0: // Common/CRC.cpp michael@0: michael@0: #include "StdAfx.h" michael@0: michael@0: #include "CRC.h" michael@0: michael@0: static const UInt32 kCRCPoly = 0xEDB88320; michael@0: michael@0: UInt32 CCRC::Table[256]; michael@0: michael@0: void CCRC::InitTable() michael@0: { michael@0: for (UInt32 i = 0; i < 256; i++) michael@0: { michael@0: UInt32 r = i; michael@0: for (int j = 0; j < 8; j++) michael@0: if (r & 1) michael@0: r = (r >> 1) ^ kCRCPoly; michael@0: else michael@0: r >>= 1; michael@0: CCRC::Table[i] = r; michael@0: } michael@0: } michael@0: michael@0: class CCRCTableInit michael@0: { michael@0: public: michael@0: CCRCTableInit() { CCRC::InitTable(); } michael@0: } g_CRCTableInit; michael@0: michael@0: void CCRC::UpdateByte(Byte b) michael@0: { michael@0: _value = Table[((Byte)(_value)) ^ b] ^ (_value >> 8); michael@0: } michael@0: michael@0: void CCRC::UpdateUInt16(UInt16 v) michael@0: { michael@0: UpdateByte(Byte(v)); michael@0: UpdateByte(Byte(v >> 8)); michael@0: } michael@0: michael@0: void CCRC::UpdateUInt32(UInt32 v) michael@0: { michael@0: for (int i = 0; i < 4; i++) michael@0: UpdateByte((Byte)(v >> (8 * i))); michael@0: } michael@0: michael@0: void CCRC::UpdateUInt64(UInt64 v) michael@0: { michael@0: for (int i = 0; i < 8; i++) michael@0: UpdateByte((Byte)(v >> (8 * i))); michael@0: } michael@0: michael@0: void CCRC::Update(const void *data, size_t size) michael@0: { michael@0: UInt32 v = _value; michael@0: const Byte *p = (const Byte *)data; michael@0: for (; size > 0 ; size--, p++) michael@0: v = Table[((Byte)(v)) ^ *p] ^ (v >> 8); michael@0: _value = v; michael@0: }