Class Crc32
CRC-32 with reversed data and unreversed output
Inheritance
Implements
Inherited Members
Namespace: ICSharpCode.SharpZipLib.Checksum
Assembly: ICSharpCode.SharpZipLib.dll
Syntax
public sealed class Crc32 : IChecksum
Remarks
Generate a table for a byte-wise 32-bit CRC calculation on the polynomial: x^32+x^26+x^23+x^22+x^16+x^12+x^11+x^10+x^8+x^7+x^5+x^4+x^2+x^1+x^0.
Polynomials over GF(2) are represented in binary, one bit per coefficient, with the lowest powers in the most significant bit. Then adding polynomials is just exclusive-or, and multiplying a polynomial by x is a right shift by one. If we call the above polynomial p, and represent a byte as the polynomial q, also with the lowest power in the most significant bit (so the byte 0xb1 is the polynomial x^7+x^3+x+1), then the CRC is (q*x^32) mod p, where a mod b means the remainder after dividing a by b.
This calculation is done using the shift-register method of multiplying and taking the remainder. The register is initialized to zero, and for each incoming bit, x^32 is added mod p to the register if the bit is a one (where x^32 mod p is p+x^32 = x^26+...+1), and the register is multiplied mod p by x (which is shifting right by one and adding x^32 mod p if the bit shifted out is a one). We start with the highest power (least significant bit) of q and repeat for all eight bits of q.
This implementation uses sixteen lookup tables stored in one linear array to implement the slicing-by-16 algorithm, a variant of the slicing-by-8 algorithm described in this Intel white paper:
The first lookup table is simply the CRC of all possible eight bit values. Each successive lookup table is derived from the original table generated by Sarwate's algorithm. Slicing a 16-bit input and XORing the outputs together will produce the same output as a byte-by-byte CRC loop with fewer arithmetic and bit manipulation operations, at the cost of increased memory consumed by the lookup tables. (Slicing-by-16 requires a 16KB table, which is still small enough to fit in most processors' L1 cache.)
Constructors
| Improve this Doc View SourceCrc32()
Initialise a default instance of Crc32
Declaration
public Crc32()
Properties
| Improve this Doc View SourceValue
Returns the CRC data checksum computed so far.
Declaration
public long Value { get; }
Property Value
Type | Description |
---|---|
System.Int64 |
Remarks
Reversed Out = false
Methods
| Improve this Doc View SourceReset()
Resets the CRC data checksum as if no update was ever called.
Declaration
public void Reset()
Update(ArraySegment<Byte>)
Update CRC data checksum based on a portion of a block of data
Declaration
public void Update(ArraySegment<byte> segment)
Parameters
Type | Name | Description |
---|---|---|
System.ArraySegment<System.Byte> | segment | The chunk of data to add |
Update(Byte[])
Updates the CRC data checksum with the bytes taken from a block of data.
Declaration
public void Update(byte[] buffer)
Parameters
Type | Name | Description |
---|---|---|
System.Byte[] | buffer | Contains the data to update the CRC with. |
Update(Int32)
Updates the checksum with the int bval.
Declaration
public void Update(int bval)
Parameters
Type | Name | Description |
---|---|---|
System.Int32 | bval | the byte is taken as the lower 8 bits of bval |
Remarks
Reversed Data = true