other-licenses/7zstub/src/Common/CRC.cpp

Fri, 16 Jan 2015 18:13:44 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Fri, 16 Jan 2015 18:13:44 +0100
branch
TOR_BUG_9701
changeset 14
925c144e1f1f
permissions
-rw-r--r--

Integrate suggestion from review to improve consistency with existing code.

     1 // Common/CRC.cpp
     3 #include "StdAfx.h"
     5 #include "CRC.h"
     7 static const UInt32 kCRCPoly = 0xEDB88320;
     9 UInt32 CCRC::Table[256];
    11 void 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 }
    25 class CCRCTableInit
    26 {
    27 public:
    28   CCRCTableInit() { CCRC::InitTable(); }
    29 } g_CRCTableInit;
    31 void CCRC::UpdateByte(Byte b)
    32 {
    33   _value = Table[((Byte)(_value)) ^ b] ^ (_value >> 8);
    34 }
    36 void CCRC::UpdateUInt16(UInt16 v)
    37 {
    38   UpdateByte(Byte(v));
    39   UpdateByte(Byte(v >> 8));
    40 }
    42 void CCRC::UpdateUInt32(UInt32 v)
    43 {
    44   for (int i = 0; i < 4; i++)
    45     UpdateByte((Byte)(v >> (8 * i)));
    46 }
    48 void CCRC::UpdateUInt64(UInt64 v)
    49 {
    50   for (int i = 0; i < 8; i++)
    51     UpdateByte((Byte)(v >> (8 * i)));
    52 }
    54 void 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 }

mercurial