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