Debian Codegraph @

Source/7zip/Common/CRC.cpp

raw
1 /*
2 * CRC.cpp
3 *
4 * This file is a part of LZMA compression module for NSIS.
5 *
6 * Original LZMA SDK Copyright (C) 1999-2006 Igor Pavlov
7 * Modifications Copyright (C) 2003-2026 Amir Szekely <[email protected]>
8 *
9 * Licensed under the Common Public License version 1.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 *
12 * Licence details can be found in the file COPYING.
13 *
14 * This software is provided 'as-is', without any express or implied
15 * warranty.
16 *
17 * Reviewed for Unicode support by Jim Park -- 08/28/2007
18 */
19
20 #include "StdAfx.h"
21
22 #include "CRC.h"
23
24 static const UInt32 kCRCPoly = 0xEDB88320;
25
26 UInt32 CCRC::Table[256];
27
28 void CCRC::InitTable()
29 {
30 for (UInt32 i = 0; i < 256; i++)
31 {
32 UInt32 r = i;
33 for (int j = 0; j < 8; j++)
34 if (r & 1)
35 r = (r >> 1) ^ kCRCPoly;
36 else
37 r >>= 1;
38 CCRC::Table[i] = r;
39 }
40 }
41
42 class CCRCTableInit
43 {
44 public:
45 CCRCTableInit() { CCRC::InitTable(); }
46 } g_CRCTableInit;
47
48 void CCRC::UpdateByte(Byte b)
49 {
50 _value = Table[((Byte)(_value)) ^ b] ^ (_value >> 8);
51 }
52
53 void CCRC::UpdateUInt16(UInt16 v)
54 {
55 UpdateByte(Byte(v));
56 UpdateByte(Byte(v >> 8));
57 }
58
59 void CCRC::UpdateUInt32(UInt32 v)
60 {
61 for (int i = 0; i < 4; i++)
62 UpdateByte((Byte)(v >> (8 * i)));
63 }
64
65 void CCRC::UpdateUInt64(UInt64 v)
66 {
67 for (int i = 0; i < 8; i++)
68 UpdateByte((Byte)(v >> (8 * i)));
69 }
70
71 void CCRC::Update(const void *data, size_t size)
72 {
73 UInt32 v = _value;
74 const Byte *p = (const Byte *)data;
75 for (; size > 0 ; size--, p++)
76 v = Table[((Byte)(v)) ^ *p] ^ (v >> 8);
77 _value = v;
78 }