123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473 |
- using System;
- using Interop = System.Runtime.InteropServices;
- namespace BestHTTP.Decompression.Crc
- {
-
-
-
-
-
-
-
-
-
-
- internal class CRC32
- {
-
-
-
- public Int64 TotalBytesRead
- {
- get
- {
- return _TotalBytesRead;
- }
- }
-
-
-
- public Int32 Crc32Result
- {
- get
- {
- return unchecked((Int32)(~_register));
- }
- }
-
-
-
-
-
- public Int32 GetCrc32(System.IO.Stream input)
- {
- return GetCrc32AndCopy(input, null);
- }
-
-
-
-
-
-
-
- public Int32 GetCrc32AndCopy(System.IO.Stream input, System.IO.Stream output)
- {
- if (input == null)
- throw new Exception("The input stream must not be null.");
- unchecked
- {
- byte[] 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 (Int32)(~_register);
- }
- }
-
-
-
-
-
-
-
- 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 Exception("The data buffer must not be null.");
-
- for (int i = 0; i < count; i++)
- {
- int x = offset + i;
- byte b = block[x];
- if (this.reverseBits)
- {
- UInt32 temp = (_register >> 24) ^ b;
- _register = (_register << 8) ^ crc32Table[temp];
- }
- else
- {
- UInt32 temp = (_register & 0x000000FF) ^ b;
- _register = (_register >> 8) ^ crc32Table[temp];
- }
- }
- _TotalBytesRead += count;
- }
-
-
-
-
- public void UpdateCRC(byte b)
- {
- if (this.reverseBits)
- {
- UInt32 temp = (_register >> 24) ^ b;
- _register = (_register << 8) ^ crc32Table[temp];
- }
- else
- {
- UInt32 temp = (_register & 0x000000FF) ^ b;
- _register = (_register >> 8) ^ crc32Table[temp];
- }
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public void UpdateCRC(byte b, int n)
- {
- while (n-- > 0)
- {
- if (this.reverseBits)
- {
- uint temp = (_register >> 24) ^ b;
- _register = (_register << 8) ^ crc32Table[(temp >= 0)
- ? temp
- : (temp + 256)];
- }
- else
- {
- UInt32 temp = (_register & 0x000000FF) ^ b;
- _register = (_register >> 8) ^ crc32Table[(temp >= 0)
- ? temp
- : (temp + 256)];
- }
- }
- }
- private static uint ReverseBits(uint data)
- {
- unchecked
- {
- uint ret = data;
- ret = (ret & 0x55555555) << 1 | (ret >> 1) & 0x55555555;
- ret = (ret & 0x33333333) << 2 | (ret >> 2) & 0x33333333;
- ret = (ret & 0x0F0F0F0F) << 4 | (ret >> 4) & 0x0F0F0F0F;
- ret = (ret << 24) | ((ret & 0xFF00) << 8) | ((ret >> 8) & 0xFF00) | (ret >> 24);
- return ret;
- }
- }
- private static byte ReverseBits(byte data)
- {
- unchecked
- {
- uint u = (uint)data * 0x00020202;
- uint m = 0x01044010;
- uint s = u & m;
- uint t = (u << 2) & (m << 1);
- return (byte)((0x01001001 * (s + t)) >> 24);
- }
- }
- private void GenerateLookupTable()
- {
- crc32Table = new UInt32[256];
- unchecked
- {
- UInt32 dwCrc;
- byte i = 0;
- do
- {
- dwCrc = i;
- for (byte j = 8; j > 0; j--)
- {
- if ((dwCrc & 1) == 1)
- {
- dwCrc = (dwCrc >> 1) ^ dwPolynomial;
- }
- else
- {
- dwCrc >>= 1;
- }
- }
- if (reverseBits)
- {
- crc32Table[ReverseBits(i)] = ReverseBits(dwCrc);
- }
- else
- {
- crc32Table[i] = dwCrc;
- }
- i++;
- } while (i!=0);
- }
- #if VERBOSE
- Console.WriteLine();
- Console.WriteLine("private static readonly UInt32[] crc32Table = {");
- for (int i = 0; i < crc32Table.Length; i+=4)
- {
- Console.Write(" ");
- for (int j=0; j < 4; j++)
- {
- Console.Write(" 0x{0:X8}U,", crc32Table[i+j]);
- }
- Console.WriteLine();
- }
- Console.WriteLine("};");
- Console.WriteLine();
- #endif
- }
- 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)
- {
- uint[] even = new uint[32];
- uint[] odd = new uint[32];
- if (length == 0)
- return;
- uint crc1= ~_register;
- uint crc2= (uint) crc;
-
- odd[0] = this.dwPolynomial;
- 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);
- uint 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;
- _register= ~crc1;
-
- return;
- }
-
-
-
-
- public CRC32() : this(false)
- {
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public CRC32(bool reverseBits) :
- this( unchecked((int)0xEDB88320), reverseBits)
- {
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public CRC32(int polynomial, bool reverseBits)
- {
- this.reverseBits = reverseBits;
- this.dwPolynomial = (uint) polynomial;
- this.GenerateLookupTable();
- }
-
-
-
-
-
-
-
-
-
- public void Reset()
- {
- _register = 0xFFFFFFFFU;
- }
-
- private UInt32 dwPolynomial;
- private Int64 _TotalBytesRead;
- private bool reverseBits;
- private UInt32[] crc32Table;
- private const int BUFFER_SIZE = 8192;
- private UInt32 _register = 0xFFFFFFFFU;
- }
- }
|