1 // Common/CRC.cpp 2 3 #include "StdAfx.h" 4 5 #include "CRC.h" 6 7 static const UInt32 kCRCPoly = 0xEDB88320; 8 9 UInt32 CCRC::Table[256]; 10 InitTable()11void CCRC::InitTable() 12 { 13 for (UInt32 i = 0; i < 256; i++) 14 { 15 UInt32 r = i; 16 for (int j = 0; j < 8; j++) 17 if (r & 1) 18 r = (r >> 1) ^ kCRCPoly; 19 else 20 r >>= 1; 21 CCRC::Table[i] = r; 22 } 23 } 24 25 class CCRCTableInit 26 { 27 public: CCRCTableInit()28 CCRCTableInit() { CCRC::InitTable(); } 29 } g_CRCTableInit; 30 UpdateByte(Byte b)31void CCRC::UpdateByte(Byte b) 32 { 33 _value = Table[((Byte)(_value)) ^ b] ^ (_value >> 8); 34 } 35 UpdateUInt16(UInt16 v)36void CCRC::UpdateUInt16(UInt16 v) 37 { 38 UpdateByte(Byte(v)); 39 UpdateByte(Byte(v >> 8)); 40 } 41 UpdateUInt32(UInt32 v)42void CCRC::UpdateUInt32(UInt32 v) 43 { 44 for (int i = 0; i < 4; i++) 45 UpdateByte((Byte)(v >> (8 * i))); 46 } 47 UpdateUInt64(UInt64 v)48void CCRC::UpdateUInt64(UInt64 v) 49 { 50 for (int i = 0; i < 8; i++) 51 UpdateByte((Byte)(v >> (8 * i))); 52 } 53 Update(const void * data,size_t size)54void CCRC::Update(const void *data, size_t size) 55 { 56 UInt32 v = _value; 57 const Byte *p = (const Byte *)data; 58 for (; size > 0 ; size--, p++) 59 v = Table[((Byte)(v)) ^ *p] ^ (v >> 8); 60 _value = v; 61 } 62