1 | // CRC32.cs
|
---|
2 | // ------------------------------------------------------------------
|
---|
3 | //
|
---|
4 | // Copyright (c) 2011 Dino Chiesa.
|
---|
5 | // All rights reserved.
|
---|
6 | //
|
---|
7 | // This code module is part of DotNetZip, a zipfile class library.
|
---|
8 | //
|
---|
9 | // ------------------------------------------------------------------
|
---|
10 | //
|
---|
11 | // This code is licensed under the Microsoft Public License.
|
---|
12 | // See the file License.txt for the license details.
|
---|
13 | // More info on: http://dotnetzip.codeplex.com
|
---|
14 | //
|
---|
15 | // ------------------------------------------------------------------
|
---|
16 | //
|
---|
17 | // Last Saved: <2011-August-02 18:25:54>
|
---|
18 | //
|
---|
19 | // ------------------------------------------------------------------
|
---|
20 | //
|
---|
21 | // This module defines the CRC32 class, which can do the CRC32 algorithm, using
|
---|
22 | // arbitrary starting polynomials, and bit reversal. The bit reversal is what
|
---|
23 | // distinguishes this CRC-32 used in BZip2 from the CRC-32 that is used in PKZIP
|
---|
24 | // files, or GZIP files. This class does both.
|
---|
25 | //
|
---|
26 | // ------------------------------------------------------------------
|
---|
27 |
|
---|
28 |
|
---|
29 | using System;
|
---|
30 | using Interop = System.Runtime.InteropServices;
|
---|
31 |
|
---|
32 | namespace OfficeOpenXml.Packaging.Ionic.Crc
|
---|
33 | {
|
---|
34 | /// <summary>
|
---|
35 | /// Computes a CRC-32. The CRC-32 algorithm is parameterized - you
|
---|
36 | /// can set the polynomial and enable or disable bit
|
---|
37 | /// reversal. This can be used for GZIP, BZip2, or ZIP.
|
---|
38 | /// </summary>
|
---|
39 | /// <remarks>
|
---|
40 | /// This type is used internally by DotNetZip; it is generally not used
|
---|
41 | /// directly by applications wishing to create, read, or manipulate zip
|
---|
42 | /// archive files.
|
---|
43 | /// </remarks>
|
---|
44 |
|
---|
45 | [Interop.GuidAttribute("ebc25cf6-9120-4283-b972-0e5520d0000C")]
|
---|
46 | [Interop.ComVisible(true)]
|
---|
47 | #if !NETCF
|
---|
48 | [Interop.ClassInterface(Interop.ClassInterfaceType.AutoDispatch)]
|
---|
49 | #endif
|
---|
50 | internal class CRC32
|
---|
51 | {
|
---|
52 | /// <summary>
|
---|
53 | /// Indicates the total number of bytes applied to the CRC.
|
---|
54 | /// </summary>
|
---|
55 | public Int64 TotalBytesRead
|
---|
56 | {
|
---|
57 | get
|
---|
58 | {
|
---|
59 | return _TotalBytesRead;
|
---|
60 | }
|
---|
61 | }
|
---|
62 |
|
---|
63 | /// <summary>
|
---|
64 | /// Indicates the current CRC for all blocks slurped in.
|
---|
65 | /// </summary>
|
---|
66 | public Int32 Crc32Result
|
---|
67 | {
|
---|
68 | get
|
---|
69 | {
|
---|
70 | return unchecked((Int32)(~_register));
|
---|
71 | }
|
---|
72 | }
|
---|
73 |
|
---|
74 | /// <summary>
|
---|
75 | /// Returns the CRC32 for the specified stream.
|
---|
76 | /// </summary>
|
---|
77 | /// <param name="input">The stream over which to calculate the CRC32</param>
|
---|
78 | /// <returns>the CRC32 calculation</returns>
|
---|
79 | public Int32 GetCrc32(System.IO.Stream input)
|
---|
80 | {
|
---|
81 | return GetCrc32AndCopy(input, null);
|
---|
82 | }
|
---|
83 |
|
---|
84 | /// <summary>
|
---|
85 | /// Returns the CRC32 for the specified stream, and writes the input into the
|
---|
86 | /// output stream.
|
---|
87 | /// </summary>
|
---|
88 | /// <param name="input">The stream over which to calculate the CRC32</param>
|
---|
89 | /// <param name="output">The stream into which to deflate the input</param>
|
---|
90 | /// <returns>the CRC32 calculation</returns>
|
---|
91 | public Int32 GetCrc32AndCopy(System.IO.Stream input, System.IO.Stream output)
|
---|
92 | {
|
---|
93 | if (input == null)
|
---|
94 | throw new Exception("The input stream must not be null.");
|
---|
95 |
|
---|
96 | unchecked
|
---|
97 | {
|
---|
98 | byte[] buffer = new byte[BUFFER_SIZE];
|
---|
99 | int readSize = BUFFER_SIZE;
|
---|
100 |
|
---|
101 | _TotalBytesRead = 0;
|
---|
102 | int count = input.Read(buffer, 0, readSize);
|
---|
103 | if (output != null) output.Write(buffer, 0, count);
|
---|
104 | _TotalBytesRead += count;
|
---|
105 | while (count > 0)
|
---|
106 | {
|
---|
107 | SlurpBlock(buffer, 0, count);
|
---|
108 | count = input.Read(buffer, 0, readSize);
|
---|
109 | if (output != null) output.Write(buffer, 0, count);
|
---|
110 | _TotalBytesRead += count;
|
---|
111 | }
|
---|
112 |
|
---|
113 | return (Int32)(~_register);
|
---|
114 | }
|
---|
115 | }
|
---|
116 |
|
---|
117 |
|
---|
118 | /// <summary>
|
---|
119 | /// Get the CRC32 for the given (word,byte) combo. This is a
|
---|
120 | /// computation defined by PKzip for PKZIP 2.0 (weak) encryption.
|
---|
121 | /// </summary>
|
---|
122 | /// <param name="W">The word to start with.</param>
|
---|
123 | /// <param name="B">The byte to combine it with.</param>
|
---|
124 | /// <returns>The CRC-ized result.</returns>
|
---|
125 | public Int32 ComputeCrc32(Int32 W, byte B)
|
---|
126 | {
|
---|
127 | return _InternalComputeCrc32((UInt32)W, B);
|
---|
128 | }
|
---|
129 |
|
---|
130 | internal Int32 _InternalComputeCrc32(UInt32 W, byte B)
|
---|
131 | {
|
---|
132 | return (Int32)(crc32Table[(W ^ B) & 0xFF] ^ (W >> 8));
|
---|
133 | }
|
---|
134 |
|
---|
135 |
|
---|
136 | /// <summary>
|
---|
137 | /// Update the value for the running CRC32 using the given block of bytes.
|
---|
138 | /// This is useful when using the CRC32() class in a Stream.
|
---|
139 | /// </summary>
|
---|
140 | /// <param name="block">block of bytes to slurp</param>
|
---|
141 | /// <param name="offset">starting point in the block</param>
|
---|
142 | /// <param name="count">how many bytes within the block to slurp</param>
|
---|
143 | public void SlurpBlock(byte[] block, int offset, int count)
|
---|
144 | {
|
---|
145 | if (block == null)
|
---|
146 | throw new Exception("The data buffer must not be null.");
|
---|
147 |
|
---|
148 | // bzip algorithm
|
---|
149 | for (int i = 0; i < count; i++)
|
---|
150 | {
|
---|
151 | int x = offset + i;
|
---|
152 | byte b = block[x];
|
---|
153 | if (this.reverseBits)
|
---|
154 | {
|
---|
155 | UInt32 temp = (_register >> 24) ^ b;
|
---|
156 | _register = (_register << 8) ^ crc32Table[temp];
|
---|
157 | }
|
---|
158 | else
|
---|
159 | {
|
---|
160 | UInt32 temp = (_register & 0x000000FF) ^ b;
|
---|
161 | _register = (_register >> 8) ^ crc32Table[temp];
|
---|
162 | }
|
---|
163 | }
|
---|
164 | _TotalBytesRead += count;
|
---|
165 | }
|
---|
166 |
|
---|
167 |
|
---|
168 | /// <summary>
|
---|
169 | /// Process one byte in the CRC.
|
---|
170 | /// </summary>
|
---|
171 | /// <param name = "b">the byte to include into the CRC . </param>
|
---|
172 | public void UpdateCRC(byte b)
|
---|
173 | {
|
---|
174 | if (this.reverseBits)
|
---|
175 | {
|
---|
176 | UInt32 temp = (_register >> 24) ^ b;
|
---|
177 | _register = (_register << 8) ^ crc32Table[temp];
|
---|
178 | }
|
---|
179 | else
|
---|
180 | {
|
---|
181 | UInt32 temp = (_register & 0x000000FF) ^ b;
|
---|
182 | _register = (_register >> 8) ^ crc32Table[temp];
|
---|
183 | }
|
---|
184 | }
|
---|
185 |
|
---|
186 | /// <summary>
|
---|
187 | /// Process a run of N identical bytes into the CRC.
|
---|
188 | /// </summary>
|
---|
189 | /// <remarks>
|
---|
190 | /// <para>
|
---|
191 | /// This method serves as an optimization for updating the CRC when a
|
---|
192 | /// run of identical bytes is found. Rather than passing in a buffer of
|
---|
193 | /// length n, containing all identical bytes b, this method accepts the
|
---|
194 | /// byte value and the length of the (virtual) buffer - the length of
|
---|
195 | /// the run.
|
---|
196 | /// </para>
|
---|
197 | /// </remarks>
|
---|
198 | /// <param name = "b">the byte to include into the CRC. </param>
|
---|
199 | /// <param name = "n">the number of times that byte should be repeated. </param>
|
---|
200 | public void UpdateCRC(byte b, int n)
|
---|
201 | {
|
---|
202 | while (n-- > 0)
|
---|
203 | {
|
---|
204 | if (this.reverseBits)
|
---|
205 | {
|
---|
206 | uint temp = (_register >> 24) ^ b;
|
---|
207 | _register = (_register << 8) ^ crc32Table[(temp >= 0)
|
---|
208 | ? temp
|
---|
209 | : (temp + 256)];
|
---|
210 | }
|
---|
211 | else
|
---|
212 | {
|
---|
213 | UInt32 temp = (_register & 0x000000FF) ^ b;
|
---|
214 | _register = (_register >> 8) ^ crc32Table[(temp >= 0)
|
---|
215 | ? temp
|
---|
216 | : (temp + 256)];
|
---|
217 |
|
---|
218 | }
|
---|
219 | }
|
---|
220 | }
|
---|
221 |
|
---|
222 |
|
---|
223 |
|
---|
224 | private static uint ReverseBits(uint data)
|
---|
225 | {
|
---|
226 | unchecked
|
---|
227 | {
|
---|
228 | uint ret = data;
|
---|
229 | ret = (ret & 0x55555555) << 1 | (ret >> 1) & 0x55555555;
|
---|
230 | ret = (ret & 0x33333333) << 2 | (ret >> 2) & 0x33333333;
|
---|
231 | ret = (ret & 0x0F0F0F0F) << 4 | (ret >> 4) & 0x0F0F0F0F;
|
---|
232 | ret = (ret << 24) | ((ret & 0xFF00) << 8) | ((ret >> 8) & 0xFF00) | (ret >> 24);
|
---|
233 | return ret;
|
---|
234 | }
|
---|
235 | }
|
---|
236 |
|
---|
237 | private static byte ReverseBits(byte data)
|
---|
238 | {
|
---|
239 | unchecked
|
---|
240 | {
|
---|
241 | uint u = (uint)data * 0x00020202;
|
---|
242 | uint m = 0x01044010;
|
---|
243 | uint s = u & m;
|
---|
244 | uint t = (u << 2) & (m << 1);
|
---|
245 | return (byte)((0x01001001 * (s + t)) >> 24);
|
---|
246 | }
|
---|
247 | }
|
---|
248 |
|
---|
249 |
|
---|
250 |
|
---|
251 | private void GenerateLookupTable()
|
---|
252 | {
|
---|
253 | crc32Table = new UInt32[256];
|
---|
254 | unchecked
|
---|
255 | {
|
---|
256 | UInt32 dwCrc;
|
---|
257 | byte i = 0;
|
---|
258 | do
|
---|
259 | {
|
---|
260 | dwCrc = i;
|
---|
261 | for (byte j = 8; j > 0; j--)
|
---|
262 | {
|
---|
263 | if ((dwCrc & 1) == 1)
|
---|
264 | {
|
---|
265 | dwCrc = (dwCrc >> 1) ^ dwPolynomial;
|
---|
266 | }
|
---|
267 | else
|
---|
268 | {
|
---|
269 | dwCrc >>= 1;
|
---|
270 | }
|
---|
271 | }
|
---|
272 | if (reverseBits)
|
---|
273 | {
|
---|
274 | crc32Table[ReverseBits(i)] = ReverseBits(dwCrc);
|
---|
275 | }
|
---|
276 | else
|
---|
277 | {
|
---|
278 | crc32Table[i] = dwCrc;
|
---|
279 | }
|
---|
280 | i++;
|
---|
281 | } while (i!=0);
|
---|
282 | }
|
---|
283 |
|
---|
284 | #if VERBOSE
|
---|
285 | Console.WriteLine();
|
---|
286 | Console.WriteLine("private static readonly UInt32[] crc32Table = {");
|
---|
287 | for (int i = 0; i < crc32Table.Length; i+=4)
|
---|
288 | {
|
---|
289 | Console.Write(" ");
|
---|
290 | for (int j=0; j < 4; j++)
|
---|
291 | {
|
---|
292 | Console.Write(" 0x{0:X8}U,", crc32Table[i+j]);
|
---|
293 | }
|
---|
294 | Console.WriteLine();
|
---|
295 | }
|
---|
296 | Console.WriteLine("};");
|
---|
297 | Console.WriteLine();
|
---|
298 | #endif
|
---|
299 | }
|
---|
300 |
|
---|
301 |
|
---|
302 | private uint gf2_matrix_times(uint[] matrix, uint vec)
|
---|
303 | {
|
---|
304 | uint sum = 0;
|
---|
305 | int i=0;
|
---|
306 | while (vec != 0)
|
---|
307 | {
|
---|
308 | if ((vec & 0x01)== 0x01)
|
---|
309 | sum ^= matrix[i];
|
---|
310 | vec >>= 1;
|
---|
311 | i++;
|
---|
312 | }
|
---|
313 | return sum;
|
---|
314 | }
|
---|
315 |
|
---|
316 | private void gf2_matrix_square(uint[] square, uint[] mat)
|
---|
317 | {
|
---|
318 | for (int i = 0; i < 32; i++)
|
---|
319 | square[i] = gf2_matrix_times(mat, mat[i]);
|
---|
320 | }
|
---|
321 |
|
---|
322 |
|
---|
323 |
|
---|
324 | /// <summary>
|
---|
325 | /// Combines the given CRC32 value with the current running total.
|
---|
326 | /// </summary>
|
---|
327 | /// <remarks>
|
---|
328 | /// This is useful when using a divide-and-conquer approach to
|
---|
329 | /// calculating a CRC. Multiple threads can each calculate a
|
---|
330 | /// CRC32 on a segment of the data, and then combine the
|
---|
331 | /// individual CRC32 values at the end.
|
---|
332 | /// </remarks>
|
---|
333 | /// <param name="crc">the crc value to be combined with this one</param>
|
---|
334 | /// <param name="length">the length of data the CRC value was calculated on</param>
|
---|
335 | public void Combine(int crc, int length)
|
---|
336 | {
|
---|
337 | uint[] even = new uint[32]; // even-power-of-two zeros operator
|
---|
338 | uint[] odd = new uint[32]; // odd-power-of-two zeros operator
|
---|
339 |
|
---|
340 | if (length == 0)
|
---|
341 | return;
|
---|
342 |
|
---|
343 | uint crc1= ~_register;
|
---|
344 | uint crc2= (uint) crc;
|
---|
345 |
|
---|
346 | // put operator for one zero bit in odd
|
---|
347 | odd[0] = this.dwPolynomial; // the CRC-32 polynomial
|
---|
348 | uint row = 1;
|
---|
349 | for (int i = 1; i < 32; i++)
|
---|
350 | {
|
---|
351 | odd[i] = row;
|
---|
352 | row <<= 1;
|
---|
353 | }
|
---|
354 |
|
---|
355 | // put operator for two zero bits in even
|
---|
356 | gf2_matrix_square(even, odd);
|
---|
357 |
|
---|
358 | // put operator for four zero bits in odd
|
---|
359 | gf2_matrix_square(odd, even);
|
---|
360 |
|
---|
361 | uint len2 = (uint) length;
|
---|
362 |
|
---|
363 | // apply len2 zeros to crc1 (first square will put the operator for one
|
---|
364 | // zero byte, eight zero bits, in even)
|
---|
365 | do {
|
---|
366 | // apply zeros operator for this bit of len2
|
---|
367 | gf2_matrix_square(even, odd);
|
---|
368 |
|
---|
369 | if ((len2 & 1)== 1)
|
---|
370 | crc1 = gf2_matrix_times(even, crc1);
|
---|
371 | len2 >>= 1;
|
---|
372 |
|
---|
373 | if (len2 == 0)
|
---|
374 | break;
|
---|
375 |
|
---|
376 | // another iteration of the loop with odd and even swapped
|
---|
377 | gf2_matrix_square(odd, even);
|
---|
378 | if ((len2 & 1)==1)
|
---|
379 | crc1 = gf2_matrix_times(odd, crc1);
|
---|
380 | len2 >>= 1;
|
---|
381 |
|
---|
382 |
|
---|
383 | } while (len2 != 0);
|
---|
384 |
|
---|
385 | crc1 ^= crc2;
|
---|
386 |
|
---|
387 | _register= ~crc1;
|
---|
388 |
|
---|
389 | //return (int) crc1;
|
---|
390 | return;
|
---|
391 | }
|
---|
392 |
|
---|
393 |
|
---|
394 | /// <summary>
|
---|
395 | /// Create an instance of the CRC32 class using the default settings: no
|
---|
396 | /// bit reversal, and a polynomial of 0xEDB88320.
|
---|
397 | /// </summary>
|
---|
398 | public CRC32() : this(false)
|
---|
399 | {
|
---|
400 | }
|
---|
401 |
|
---|
402 | /// <summary>
|
---|
403 | /// Create an instance of the CRC32 class, specifying whether to reverse
|
---|
404 | /// data bits or not.
|
---|
405 | /// </summary>
|
---|
406 | /// <param name='reverseBits'>
|
---|
407 | /// specify true if the instance should reverse data bits.
|
---|
408 | /// </param>
|
---|
409 | /// <remarks>
|
---|
410 | /// <para>
|
---|
411 | /// In the CRC-32 used by BZip2, the bits are reversed. Therefore if you
|
---|
412 | /// want a CRC32 with compatibility with BZip2, you should pass true
|
---|
413 | /// here. In the CRC-32 used by GZIP and PKZIP, the bits are not
|
---|
414 | /// reversed; Therefore if you want a CRC32 with compatibility with
|
---|
415 | /// those, you should pass false.
|
---|
416 | /// </para>
|
---|
417 | /// </remarks>
|
---|
418 | public CRC32(bool reverseBits) :
|
---|
419 | this( unchecked((int)0xEDB88320), reverseBits)
|
---|
420 | {
|
---|
421 | }
|
---|
422 |
|
---|
423 |
|
---|
424 | /// <summary>
|
---|
425 | /// Create an instance of the CRC32 class, specifying the polynomial and
|
---|
426 | /// whether to reverse data bits or not.
|
---|
427 | /// </summary>
|
---|
428 | /// <param name='polynomial'>
|
---|
429 | /// The polynomial to use for the CRC, expressed in the reversed (LSB)
|
---|
430 | /// format: the highest ordered bit in the polynomial value is the
|
---|
431 | /// coefficient of the 0th power; the second-highest order bit is the
|
---|
432 | /// coefficient of the 1 power, and so on. Expressed this way, the
|
---|
433 | /// polynomial for the CRC-32C used in IEEE 802.3, is 0xEDB88320.
|
---|
434 | /// </param>
|
---|
435 | /// <param name='reverseBits'>
|
---|
436 | /// specify true if the instance should reverse data bits.
|
---|
437 | /// </param>
|
---|
438 | ///
|
---|
439 | /// <remarks>
|
---|
440 | /// <para>
|
---|
441 | /// In the CRC-32 used by BZip2, the bits are reversed. Therefore if you
|
---|
442 | /// want a CRC32 with compatibility with BZip2, you should pass true
|
---|
443 | /// here for the <c>reverseBits</c> parameter. In the CRC-32 used by
|
---|
444 | /// GZIP and PKZIP, the bits are not reversed; Therefore if you want a
|
---|
445 | /// CRC32 with compatibility with those, you should pass false for the
|
---|
446 | /// <c>reverseBits</c> parameter.
|
---|
447 | /// </para>
|
---|
448 | /// </remarks>
|
---|
449 | public CRC32(int polynomial, bool reverseBits)
|
---|
450 | {
|
---|
451 | this.reverseBits = reverseBits;
|
---|
452 | this.dwPolynomial = (uint) polynomial;
|
---|
453 | this.GenerateLookupTable();
|
---|
454 | }
|
---|
455 |
|
---|
456 | /// <summary>
|
---|
457 | /// Reset the CRC-32 class - clear the CRC "remainder register."
|
---|
458 | /// </summary>
|
---|
459 | /// <remarks>
|
---|
460 | /// <para>
|
---|
461 | /// Use this when employing a single instance of this class to compute
|
---|
462 | /// multiple, distinct CRCs on multiple, distinct data blocks.
|
---|
463 | /// </para>
|
---|
464 | /// </remarks>
|
---|
465 | public void Reset()
|
---|
466 | {
|
---|
467 | _register = 0xFFFFFFFFU;
|
---|
468 | }
|
---|
469 |
|
---|
470 | // private member vars
|
---|
471 | private UInt32 dwPolynomial;
|
---|
472 | private Int64 _TotalBytesRead;
|
---|
473 | private bool reverseBits;
|
---|
474 | private UInt32[] crc32Table;
|
---|
475 | private const int BUFFER_SIZE = 8192;
|
---|
476 | private UInt32 _register = 0xFFFFFFFFU;
|
---|
477 | }
|
---|
478 |
|
---|
479 |
|
---|
480 | /// <summary>
|
---|
481 | /// A Stream that calculates a CRC32 (a checksum) on all bytes read,
|
---|
482 | /// or on all bytes written.
|
---|
483 | /// </summary>
|
---|
484 | ///
|
---|
485 | /// <remarks>
|
---|
486 | /// <para>
|
---|
487 | /// This class can be used to verify the CRC of a ZipEntry when
|
---|
488 | /// reading from a stream, or to calculate a CRC when writing to a
|
---|
489 | /// stream. The stream should be used to either read, or write, but
|
---|
490 | /// not both. If you intermix reads and writes, the results are not
|
---|
491 | /// defined.
|
---|
492 | /// </para>
|
---|
493 | ///
|
---|
494 | /// <para>
|
---|
495 | /// This class is intended primarily for use internally by the
|
---|
496 | /// DotNetZip library.
|
---|
497 | /// </para>
|
---|
498 | /// </remarks>
|
---|
499 | internal class CrcCalculatorStream : System.IO.Stream, System.IDisposable
|
---|
500 | {
|
---|
501 | private static readonly Int64 UnsetLengthLimit = -99;
|
---|
502 |
|
---|
503 | internal System.IO.Stream _innerStream;
|
---|
504 | private CRC32 _Crc32;
|
---|
505 | private Int64 _lengthLimit = -99;
|
---|
506 | private bool _leaveOpen;
|
---|
507 |
|
---|
508 | /// <summary>
|
---|
509 | /// The default constructor.
|
---|
510 | /// </summary>
|
---|
511 | /// <remarks>
|
---|
512 | /// <para>
|
---|
513 | /// Instances returned from this constructor will leave the underlying
|
---|
514 | /// stream open upon Close(). The stream uses the default CRC32
|
---|
515 | /// algorithm, which implies a polynomial of 0xEDB88320.
|
---|
516 | /// </para>
|
---|
517 | /// </remarks>
|
---|
518 | /// <param name="stream">The underlying stream</param>
|
---|
519 | public CrcCalculatorStream(System.IO.Stream stream)
|
---|
520 | : this(true, CrcCalculatorStream.UnsetLengthLimit, stream, null)
|
---|
521 | {
|
---|
522 | }
|
---|
523 |
|
---|
524 | /// <summary>
|
---|
525 | /// The constructor allows the caller to specify how to handle the
|
---|
526 | /// underlying stream at close.
|
---|
527 | /// </summary>
|
---|
528 | /// <remarks>
|
---|
529 | /// <para>
|
---|
530 | /// The stream uses the default CRC32 algorithm, which implies a
|
---|
531 | /// polynomial of 0xEDB88320.
|
---|
532 | /// </para>
|
---|
533 | /// </remarks>
|
---|
534 | /// <param name="stream">The underlying stream</param>
|
---|
535 | /// <param name="leaveOpen">true to leave the underlying stream
|
---|
536 | /// open upon close of the <c>CrcCalculatorStream</c>; false otherwise.</param>
|
---|
537 | public CrcCalculatorStream(System.IO.Stream stream, bool leaveOpen)
|
---|
538 | : this(leaveOpen, CrcCalculatorStream.UnsetLengthLimit, stream, null)
|
---|
539 | {
|
---|
540 | }
|
---|
541 |
|
---|
542 | /// <summary>
|
---|
543 | /// A constructor allowing the specification of the length of the stream
|
---|
544 | /// to read.
|
---|
545 | /// </summary>
|
---|
546 | /// <remarks>
|
---|
547 | /// <para>
|
---|
548 | /// The stream uses the default CRC32 algorithm, which implies a
|
---|
549 | /// polynomial of 0xEDB88320.
|
---|
550 | /// </para>
|
---|
551 | /// <para>
|
---|
552 | /// Instances returned from this constructor will leave the underlying
|
---|
553 | /// stream open upon Close().
|
---|
554 | /// </para>
|
---|
555 | /// </remarks>
|
---|
556 | /// <param name="stream">The underlying stream</param>
|
---|
557 | /// <param name="length">The length of the stream to slurp</param>
|
---|
558 | public CrcCalculatorStream(System.IO.Stream stream, Int64 length)
|
---|
559 | : this(true, length, stream, null)
|
---|
560 | {
|
---|
561 | if (length < 0)
|
---|
562 | throw new ArgumentException("length");
|
---|
563 | }
|
---|
564 |
|
---|
565 | /// <summary>
|
---|
566 | /// A constructor allowing the specification of the length of the stream
|
---|
567 | /// to read, as well as whether to keep the underlying stream open upon
|
---|
568 | /// Close().
|
---|
569 | /// </summary>
|
---|
570 | /// <remarks>
|
---|
571 | /// <para>
|
---|
572 | /// The stream uses the default CRC32 algorithm, which implies a
|
---|
573 | /// polynomial of 0xEDB88320.
|
---|
574 | /// </para>
|
---|
575 | /// </remarks>
|
---|
576 | /// <param name="stream">The underlying stream</param>
|
---|
577 | /// <param name="length">The length of the stream to slurp</param>
|
---|
578 | /// <param name="leaveOpen">true to leave the underlying stream
|
---|
579 | /// open upon close of the <c>CrcCalculatorStream</c>; false otherwise.</param>
|
---|
580 | public CrcCalculatorStream(System.IO.Stream stream, Int64 length, bool leaveOpen)
|
---|
581 | : this(leaveOpen, length, stream, null)
|
---|
582 | {
|
---|
583 | if (length < 0)
|
---|
584 | throw new ArgumentException("length");
|
---|
585 | }
|
---|
586 |
|
---|
587 | /// <summary>
|
---|
588 | /// A constructor allowing the specification of the length of the stream
|
---|
589 | /// to read, as well as whether to keep the underlying stream open upon
|
---|
590 | /// Close(), and the CRC32 instance to use.
|
---|
591 | /// </summary>
|
---|
592 | /// <remarks>
|
---|
593 | /// <para>
|
---|
594 | /// The stream uses the specified CRC32 instance, which allows the
|
---|
595 | /// application to specify how the CRC gets calculated.
|
---|
596 | /// </para>
|
---|
597 | /// </remarks>
|
---|
598 | /// <param name="stream">The underlying stream</param>
|
---|
599 | /// <param name="length">The length of the stream to slurp</param>
|
---|
600 | /// <param name="leaveOpen">true to leave the underlying stream
|
---|
601 | /// open upon close of the <c>CrcCalculatorStream</c>; false otherwise.</param>
|
---|
602 | /// <param name="crc32">the CRC32 instance to use to calculate the CRC32</param>
|
---|
603 | public CrcCalculatorStream(System.IO.Stream stream, Int64 length, bool leaveOpen,
|
---|
604 | CRC32 crc32)
|
---|
605 | : this(leaveOpen, length, stream, crc32)
|
---|
606 | {
|
---|
607 | if (length < 0)
|
---|
608 | throw new ArgumentException("length");
|
---|
609 | }
|
---|
610 |
|
---|
611 |
|
---|
612 | // This ctor is private - no validation is done here. This is to allow the use
|
---|
613 | // of a (specific) negative value for the _lengthLimit, to indicate that there
|
---|
614 | // is no length set. So we validate the length limit in those ctors that use an
|
---|
615 | // explicit param, otherwise we don't validate, because it could be our special
|
---|
616 | // value.
|
---|
617 | private CrcCalculatorStream
|
---|
618 | (bool leaveOpen, Int64 length, System.IO.Stream stream, CRC32 crc32)
|
---|
619 | : base()
|
---|
620 | {
|
---|
621 | _innerStream = stream;
|
---|
622 | _Crc32 = crc32 ?? new CRC32();
|
---|
623 | _lengthLimit = length;
|
---|
624 | _leaveOpen = leaveOpen;
|
---|
625 | }
|
---|
626 |
|
---|
627 |
|
---|
628 | /// <summary>
|
---|
629 | /// Gets the total number of bytes run through the CRC32 calculator.
|
---|
630 | /// </summary>
|
---|
631 | ///
|
---|
632 | /// <remarks>
|
---|
633 | /// This is either the total number of bytes read, or the total number of
|
---|
634 | /// bytes written, depending on the direction of this stream.
|
---|
635 | /// </remarks>
|
---|
636 | public Int64 TotalBytesSlurped
|
---|
637 | {
|
---|
638 | get { return _Crc32.TotalBytesRead; }
|
---|
639 | }
|
---|
640 |
|
---|
641 | /// <summary>
|
---|
642 | /// Provides the current CRC for all blocks slurped in.
|
---|
643 | /// </summary>
|
---|
644 | /// <remarks>
|
---|
645 | /// <para>
|
---|
646 | /// The running total of the CRC is kept as data is written or read
|
---|
647 | /// through the stream. read this property after all reads or writes to
|
---|
648 | /// get an accurate CRC for the entire stream.
|
---|
649 | /// </para>
|
---|
650 | /// </remarks>
|
---|
651 | public Int32 Crc
|
---|
652 | {
|
---|
653 | get { return _Crc32.Crc32Result; }
|
---|
654 | }
|
---|
655 |
|
---|
656 | /// <summary>
|
---|
657 | /// Indicates whether the underlying stream will be left open when the
|
---|
658 | /// <c>CrcCalculatorStream</c> is Closed.
|
---|
659 | /// </summary>
|
---|
660 | /// <remarks>
|
---|
661 | /// <para>
|
---|
662 | /// Set this at any point before calling <see cref="Close()"/>.
|
---|
663 | /// </para>
|
---|
664 | /// </remarks>
|
---|
665 | public bool LeaveOpen
|
---|
666 | {
|
---|
667 | get { return _leaveOpen; }
|
---|
668 | set { _leaveOpen = value; }
|
---|
669 | }
|
---|
670 |
|
---|
671 | /// <summary>
|
---|
672 | /// Read from the stream
|
---|
673 | /// </summary>
|
---|
674 | /// <param name="buffer">the buffer to read</param>
|
---|
675 | /// <param name="offset">the offset at which to start</param>
|
---|
676 | /// <param name="count">the number of bytes to read</param>
|
---|
677 | /// <returns>the number of bytes actually read</returns>
|
---|
678 | public override int Read(byte[] buffer, int offset, int count)
|
---|
679 | {
|
---|
680 | int bytesToRead = count;
|
---|
681 |
|
---|
682 | // Need to limit the # of bytes returned, if the stream is intended to have
|
---|
683 | // a definite length. This is especially useful when returning a stream for
|
---|
684 | // the uncompressed data directly to the application. The app won't
|
---|
685 | // necessarily read only the UncompressedSize number of bytes. For example
|
---|
686 | // wrapping the stream returned from OpenReader() into a StreadReader() and
|
---|
687 | // calling ReadToEnd() on it, We can "over-read" the zip data and get a
|
---|
688 | // corrupt string. The length limits that, prevents that problem.
|
---|
689 |
|
---|
690 | if (_lengthLimit != CrcCalculatorStream.UnsetLengthLimit)
|
---|
691 | {
|
---|
692 | if (_Crc32.TotalBytesRead >= _lengthLimit) return 0; // EOF
|
---|
693 | Int64 bytesRemaining = _lengthLimit - _Crc32.TotalBytesRead;
|
---|
694 | if (bytesRemaining < count) bytesToRead = (int)bytesRemaining;
|
---|
695 | }
|
---|
696 | int n = _innerStream.Read(buffer, offset, bytesToRead);
|
---|
697 | if (n > 0) _Crc32.SlurpBlock(buffer, offset, n);
|
---|
698 | return n;
|
---|
699 | }
|
---|
700 |
|
---|
701 | /// <summary>
|
---|
702 | /// Write to the stream.
|
---|
703 | /// </summary>
|
---|
704 | /// <param name="buffer">the buffer from which to write</param>
|
---|
705 | /// <param name="offset">the offset at which to start writing</param>
|
---|
706 | /// <param name="count">the number of bytes to write</param>
|
---|
707 | public override void Write(byte[] buffer, int offset, int count)
|
---|
708 | {
|
---|
709 | if (count > 0) _Crc32.SlurpBlock(buffer, offset, count);
|
---|
710 | _innerStream.Write(buffer, offset, count);
|
---|
711 | }
|
---|
712 |
|
---|
713 | /// <summary>
|
---|
714 | /// Indicates whether the stream supports reading.
|
---|
715 | /// </summary>
|
---|
716 | public override bool CanRead
|
---|
717 | {
|
---|
718 | get { return _innerStream.CanRead; }
|
---|
719 | }
|
---|
720 |
|
---|
721 | /// <summary>
|
---|
722 | /// Indicates whether the stream supports seeking.
|
---|
723 | /// </summary>
|
---|
724 | /// <remarks>
|
---|
725 | /// <para>
|
---|
726 | /// Always returns false.
|
---|
727 | /// </para>
|
---|
728 | /// </remarks>
|
---|
729 | public override bool CanSeek
|
---|
730 | {
|
---|
731 | get { return false; }
|
---|
732 | }
|
---|
733 |
|
---|
734 | /// <summary>
|
---|
735 | /// Indicates whether the stream supports writing.
|
---|
736 | /// </summary>
|
---|
737 | public override bool CanWrite
|
---|
738 | {
|
---|
739 | get { return _innerStream.CanWrite; }
|
---|
740 | }
|
---|
741 |
|
---|
742 | /// <summary>
|
---|
743 | /// Flush the stream.
|
---|
744 | /// </summary>
|
---|
745 | public override void Flush()
|
---|
746 | {
|
---|
747 | _innerStream.Flush();
|
---|
748 | }
|
---|
749 |
|
---|
750 | /// <summary>
|
---|
751 | /// Returns the length of the underlying stream.
|
---|
752 | /// </summary>
|
---|
753 | public override long Length
|
---|
754 | {
|
---|
755 | get
|
---|
756 | {
|
---|
757 | if (_lengthLimit == CrcCalculatorStream.UnsetLengthLimit)
|
---|
758 | return _innerStream.Length;
|
---|
759 | else return _lengthLimit;
|
---|
760 | }
|
---|
761 | }
|
---|
762 |
|
---|
763 | /// <summary>
|
---|
764 | /// The getter for this property returns the total bytes read.
|
---|
765 | /// If you use the setter, it will throw
|
---|
766 | /// <see cref="NotSupportedException"/>.
|
---|
767 | /// </summary>
|
---|
768 | public override long Position
|
---|
769 | {
|
---|
770 | get { return _Crc32.TotalBytesRead; }
|
---|
771 | set { throw new NotSupportedException(); }
|
---|
772 | }
|
---|
773 |
|
---|
774 | /// <summary>
|
---|
775 | /// Seeking is not supported on this stream. This method always throws
|
---|
776 | /// <see cref="NotSupportedException"/>
|
---|
777 | /// </summary>
|
---|
778 | /// <param name="offset">N/A</param>
|
---|
779 | /// <param name="origin">N/A</param>
|
---|
780 | /// <returns>N/A</returns>
|
---|
781 | public override long Seek(long offset, System.IO.SeekOrigin origin)
|
---|
782 | {
|
---|
783 | throw new NotSupportedException();
|
---|
784 | }
|
---|
785 |
|
---|
786 | /// <summary>
|
---|
787 | /// This method always throws
|
---|
788 | /// <see cref="NotSupportedException"/>
|
---|
789 | /// </summary>
|
---|
790 | /// <param name="value">N/A</param>
|
---|
791 | public override void SetLength(long value)
|
---|
792 | {
|
---|
793 | throw new NotSupportedException();
|
---|
794 | }
|
---|
795 |
|
---|
796 |
|
---|
797 | void IDisposable.Dispose()
|
---|
798 | {
|
---|
799 | Close();
|
---|
800 | }
|
---|
801 |
|
---|
802 | /// <summary>
|
---|
803 | /// Closes the stream.
|
---|
804 | /// </summary>
|
---|
805 | public override void Close()
|
---|
806 | {
|
---|
807 | base.Close();
|
---|
808 | if (!_leaveOpen)
|
---|
809 | _innerStream.Close();
|
---|
810 | }
|
---|
811 |
|
---|
812 | }
|
---|
813 |
|
---|
814 | } |
---|