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