summaryrefslogtreecommitdiff
path: root/Client/Assets/Scripts/XUtliPoolLib/Ionic/Crc/CRC32.cs
blob: c73c17eb2c535bbef947c85abf21d6c7110c0045 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
using System;
using System.IO;
using System.Runtime.InteropServices;

namespace Ionic.Crc
{
	[Guid("ebc25cf6-9120-4283-b972-0e5520d0000C")]
	[ComVisible(true)]
	[ClassInterface(ClassInterfaceType.AutoDispatch)]
	public class CRC32
	{
		public long TotalBytesRead
		{
			get
			{
				return this._TotalBytesRead;
			}
		}

		public int Crc32Result
		{
			get
			{
				return (int)(~(int)this._register);
			}
		}

		private uint dwPolynomial;

		private long _TotalBytesRead;

		private bool reverseBits;

		private uint[] crc32Table;

		private const int BUFFER_SIZE = 8192;

		private uint _register = uint.MaxValue;

		public int GetCrc32(Stream input)
		{
			return this.GetCrc32AndCopy(input, null);
		}

		public int GetCrc32AndCopy(Stream input, Stream output)
		{
			bool flag = input == null;
			if (flag)
			{
				throw new Exception("The input stream must not be null.");
			}
			byte[] array = new byte[8192];
			int count = 8192;
			this._TotalBytesRead = 0L;
			int i = input.Read(array, 0, count);
			bool flag2 = output != null;
			if (flag2)
			{
				output.Write(array, 0, i);
			}
			this._TotalBytesRead += (long)i;
			while (i > 0)
			{
				this.SlurpBlock(array, 0, i);
				i = input.Read(array, 0, count);
				bool flag3 = output != null;
				if (flag3)
				{
					output.Write(array, 0, i);
				}
				this._TotalBytesRead += (long)i;
			}
			return (int)(~(int)this._register);
		}

		public int ComputeCrc32(int W, byte B)
		{
			return this._InternalComputeCrc32((uint)W, B);
		}

		internal int _InternalComputeCrc32(uint W, byte B)
		{
			return (int)(this.crc32Table[(int)((W ^ (uint)B) & 255u)] ^ W >> 8);
		}

		public void SlurpBlock(byte[] block, int offset, int count)
		{
			bool flag = block == null;
			if (flag)
			{
				throw new Exception("The data buffer must not be null.");
			}
			for (int i = 0; i < count; i++)
			{
				int num = offset + i;
				byte b = block[num];
				bool flag2 = this.reverseBits;
				if (flag2)
				{
					uint num2 = this._register >> 24 ^ (uint)b;
					this._register = (this._register << 8 ^ this.crc32Table[(int)num2]);
				}
				else
				{
					uint num3 = (this._register & 255u) ^ (uint)b;
					this._register = (this._register >> 8 ^ this.crc32Table[(int)num3]);
				}
			}
			this._TotalBytesRead += (long)count;
		}

		public void UpdateCRC(byte b)
		{
			bool flag = this.reverseBits;
			if (flag)
			{
				uint num = this._register >> 24 ^ (uint)b;
				this._register = (this._register << 8 ^ this.crc32Table[(int)num]);
			}
			else
			{
				uint num2 = (this._register & 255u) ^ (uint)b;
				this._register = (this._register >> 8 ^ this.crc32Table[(int)num2]);
			}
		}

		public void UpdateCRC(byte b, int n)
		{
			while (n-- > 0)
			{
				bool flag = this.reverseBits;
				if (flag)
				{
					uint num = this._register >> 24 ^ (uint)b;
					this._register = (this._register << 8 ^ this.crc32Table[(int)((num >= 0u) ? num : (num + 256u))]);
				}
				else
				{
					uint num2 = (this._register & 255u) ^ (uint)b;
					this._register = (this._register >> 8 ^ this.crc32Table[(int)((num2 >= 0u) ? num2 : (num2 + 256u))]);
				}
			}
		}

		private static uint ReverseBits(uint data)
		{
			uint num = (data & 1431655765u) << 1 | (data >> 1 & 1431655765u);
			num = ((num & 858993459u) << 2 | (num >> 2 & 858993459u));
			num = ((num & 252645135u) << 4 | (num >> 4 & 252645135u));
			return num << 24 | (num & 65280u) << 8 | (num >> 8 & 65280u) | num >> 24;
		}

		private static byte ReverseBits(byte data)
		{
			uint num = (uint)data * 131586u;
			uint num2 = 17055760u;
			uint num3 = num & num2;
			uint num4 = num << 2 & num2 << 1;
			return (byte)(16781313u * (num3 + num4) >> 24);
		}

		private void GenerateLookupTable()
		{
			this.crc32Table = new uint[256];
			byte b = 0;
			do
			{
				uint num = (uint)b;
				for (byte b2 = 8; b2 > 0; b2 -= 1)
				{
					bool flag = (num & 1u) == 1u;
					if (flag)
					{
						num = (num >> 1 ^ this.dwPolynomial);
					}
					else
					{
						num >>= 1;
					}
				}
				bool flag2 = this.reverseBits;
				if (flag2)
				{
					this.crc32Table[(int)CRC32.ReverseBits(b)] = CRC32.ReverseBits(num);
				}
				else
				{
					this.crc32Table[(int)b] = num;
				}
				b += 1;
			}
			while (b > 0);
		}

		private uint gf2_matrix_times(uint[] matrix, uint vec)
		{
			uint num = 0u;
			int num2 = 0;
			while (vec > 0u)
			{
				bool flag = (vec & 1u) == 1u;
				if (flag)
				{
					num ^= matrix[num2];
				}
				vec >>= 1;
				num2++;
			}
			return num;
		}

		private void gf2_matrix_square(uint[] square, uint[] mat)
		{
			for (int i = 0; i < 32; i++)
			{
				square[i] = this.gf2_matrix_times(mat, mat[i]);
			}
		}

		public void Combine(int crc, int length)
		{
			uint[] array = new uint[32];
			uint[] array2 = new uint[32];
			bool flag = length == 0;
			if (!flag)
			{
				uint num = ~this._register;
				array2[0] = this.dwPolynomial;
				uint num2 = 1u;
				for (int i = 1; i < 32; i++)
				{
					array2[i] = num2;
					num2 <<= 1;
				}
				this.gf2_matrix_square(array, array2);
				this.gf2_matrix_square(array2, array);
				uint num3 = (uint)length;
				do
				{
					this.gf2_matrix_square(array, array2);
					bool flag2 = (num3 & 1u) == 1u;
					if (flag2)
					{
						num = this.gf2_matrix_times(array, num);
					}
					num3 >>= 1;
					bool flag3 = num3 == 0u;
					if (flag3)
					{
						break;
					}
					this.gf2_matrix_square(array2, array);
					bool flag4 = (num3 & 1u) == 1u;
					if (flag4)
					{
						num = this.gf2_matrix_times(array2, num);
					}
					num3 >>= 1;
				}
				while (num3 > 0u);
				num ^= (uint)crc;
				this._register = ~num;
			}
		}

		public CRC32() : this(false)
		{
		}

		public CRC32(bool reverseBits) : this(-306674912, reverseBits)
		{
		}

		public CRC32(int polynomial, bool reverseBits)
		{
			this.reverseBits = reverseBits;
			this.dwPolynomial = (uint)polynomial;
			this.GenerateLookupTable();
		}

		public void Reset()
		{
			this._register = uint.MaxValue;
		}
	}
}