File:  [ELWIX - Embedded LightWeight unIX -] / elwix / tools / oldlzma / SRC / Common / CRC.cpp
Revision 1.1.1.1 (vendor branch): download - view: text, annotated - select for diffs - revision graph
Tue May 14 09:04:51 2013 UTC (11 years, 5 months ago) by misho
Branches: misho, elwix1_9_mips, MAIN
CVS tags: start, elwix2_8, elwix2_7, elwix2_6, elwix2_3, elwix2_2, HEAD, ELWIX2_7, ELWIX2_6, ELWIX2_5, ELWIX2_2p0
oldlzma needs for uboot

// Common/CRC.cpp

#include "StdAfx.h"

#include "CRC.h"

static const UInt32 kCRCPoly = 0xEDB88320;

UInt32 CCRC::Table[256];

void CCRC::InitTable()
{
  for (UInt32 i = 0; i < 256; i++)
  {
    UInt32 r = i;
    for (int j = 0; j < 8; j++)
      if (r & 1) 
        r = (r >> 1) ^ kCRCPoly;
      else     
        r >>= 1;
    CCRC::Table[i] = r;
  }
}

class CCRCTableInit
{
public:
  CCRCTableInit() { CCRC::InitTable(); }
} g_CRCTableInit;

void CCRC::UpdateByte(Byte b)
{
  _value = Table[((Byte)(_value)) ^ b] ^ (_value >> 8);
}

void CCRC::UpdateUInt16(UInt16 v)
{
  UpdateByte(Byte(v));
  UpdateByte(Byte(v >> 8));
}

void CCRC::UpdateUInt32(UInt32 v)
{
  for (int i = 0; i < 4; i++)
    UpdateByte((Byte)(v >> (8 * i)));
}

void CCRC::UpdateUInt64(UInt64 v)
{
  for (int i = 0; i < 8; i++)
    UpdateByte((Byte)(v >> (8 * i)));
}

void CCRC::Update(const void *data, UInt32 size)
{
  UInt32 v = _value;
  const Byte *p = (const Byte *)data;
  for (; size > 0 ; size--, p++)
    v = Table[((Byte)(v)) ^ *p] ^ (v >> 8);
  _value = v;
}

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>