123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293 |
- using System;
- using System.IO;
- namespace SharpCompress.Compressors.Deflate
- {
-
-
-
-
-
-
- internal class CRC32
- {
- private const int BUFFER_SIZE = 8192;
- private static readonly UInt32[] crc32Table;
- private UInt32 runningCrc32Result = 0xFFFFFFFF;
- static CRC32()
- {
- unchecked
- {
-
-
-
-
-
- UInt32 dwPolynomial = 0xEDB88320;
- UInt32 i, j;
- crc32Table = new UInt32[256];
- UInt32 dwCrc;
- for (i = 0; i < 256; i++)
- {
- dwCrc = i;
- for (j = 8; j > 0; j--)
- {
- if ((dwCrc & 1) == 1)
- {
- dwCrc = (dwCrc >> 1) ^ dwPolynomial;
- }
- else
- {
- dwCrc >>= 1;
- }
- }
- crc32Table[i] = dwCrc;
- }
- }
- }
-
-
-
-
- public Int64 TotalBytesRead { get; private set; }
-
-
-
- public Int32 Crc32Result => unchecked((Int32)(~runningCrc32Result));
-
-
-
-
-
- public UInt32 GetCrc32(Stream input)
- {
- return GetCrc32AndCopy(input, null);
- }
-
-
-
-
-
-
-
- public UInt32 GetCrc32AndCopy(Stream input, Stream output)
- {
- if (input == null)
- {
- throw new ZlibException("The input stream must not be null.");
- }
- unchecked
- {
-
-
- var buffer = new byte[BUFFER_SIZE];
- int readSize = BUFFER_SIZE;
- TotalBytesRead = 0;
- int count = input.Read(buffer, 0, readSize);
- if (output != null)
- {
- output.Write(buffer, 0, count);
- }
- TotalBytesRead += count;
- while (count > 0)
- {
- SlurpBlock(buffer, 0, count);
- count = input.Read(buffer, 0, readSize);
- if (output != null)
- {
- output.Write(buffer, 0, count);
- }
- TotalBytesRead += count;
- }
- return ~runningCrc32Result;
- }
- }
-
-
-
-
-
-
-
- public Int32 ComputeCrc32(Int32 W, byte B)
- {
- return _InternalComputeCrc32((UInt32)W, B);
- }
- internal Int32 _InternalComputeCrc32(UInt32 W, byte B)
- {
- return (Int32)(crc32Table[(W ^ B) & 0xFF] ^ (W >> 8));
- }
-
-
-
-
-
-
-
- public void SlurpBlock(byte[] block, int offset, int count)
- {
- if (block == null)
- {
- throw new ZlibException("The data buffer must not be null.");
- }
- for (int i = 0; i < count; i++)
- {
- int x = offset + i;
- runningCrc32Result = ((runningCrc32Result) >> 8) ^
- crc32Table[(block[x]) ^ ((runningCrc32Result) & 0x000000FF)];
- }
- TotalBytesRead += count;
- }
-
- private uint gf2_matrix_times(uint[] matrix, uint vec)
- {
- uint sum = 0;
- int i = 0;
- while (vec != 0)
- {
- if ((vec & 0x01) == 0x01)
- {
- sum ^= matrix[i];
- }
- vec >>= 1;
- i++;
- }
- return sum;
- }
- private void gf2_matrix_square(uint[] square, uint[] mat)
- {
- for (int i = 0; i < 32; i++)
- {
- square[i] = gf2_matrix_times(mat, mat[i]);
- }
- }
-
-
-
-
-
-
-
-
-
-
- public void Combine(int crc, int length)
- {
- var even = new uint[32];
- var odd = new uint[32];
- if (length == 0)
- {
- return;
- }
- uint crc1 = ~runningCrc32Result;
- var crc2 = (uint)crc;
-
- odd[0] = 0xEDB88320;
- uint row = 1;
- for (int i = 1; i < 32; i++)
- {
- odd[i] = row;
- row <<= 1;
- }
-
- gf2_matrix_square(even, odd);
-
- gf2_matrix_square(odd, even);
- var len2 = (uint)length;
-
-
- do
- {
-
- gf2_matrix_square(even, odd);
- if ((len2 & 1) == 1)
- {
- crc1 = gf2_matrix_times(even, crc1);
- }
- len2 >>= 1;
- if (len2 == 0)
- {
- break;
- }
-
- gf2_matrix_square(odd, even);
- if ((len2 & 1) == 1)
- {
- crc1 = gf2_matrix_times(odd, crc1);
- }
- len2 >>= 1;
- }
- while (len2 != 0);
- crc1 ^= crc2;
- runningCrc32Result = ~crc1;
-
- }
-
- }
- }
|