1 | // Zlib.cs
|
---|
2 | // ------------------------------------------------------------------
|
---|
3 | //
|
---|
4 | // Copyright (c) 2009-2011 Dino Chiesa and Microsoft Corporation.
|
---|
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-03 19:52:28>
|
---|
18 | //
|
---|
19 | // ------------------------------------------------------------------
|
---|
20 | //
|
---|
21 | // This module defines classes for ZLIB compression and
|
---|
22 | // decompression. This code is derived from the jzlib implementation of
|
---|
23 | // zlib, but significantly modified. The object model is not the same,
|
---|
24 | // and many of the behaviors are new or different. Nonetheless, in
|
---|
25 | // keeping with the license for jzlib, the copyright to that code is
|
---|
26 | // included below.
|
---|
27 | //
|
---|
28 | // ------------------------------------------------------------------
|
---|
29 | //
|
---|
30 | // The following notice applies to jzlib:
|
---|
31 | //
|
---|
32 | // Copyright (c) 2000,2001,2002,2003 ymnk, JCraft,Inc. All rights reserved.
|
---|
33 | //
|
---|
34 | // Redistribution and use in source and binary forms, with or without
|
---|
35 | // modification, are permitted provided that the following conditions are met:
|
---|
36 | //
|
---|
37 | // 1. Redistributions of source code must retain the above copyright notice,
|
---|
38 | // this list of conditions and the following disclaimer.
|
---|
39 | //
|
---|
40 | // 2. Redistributions in binary form must reproduce the above copyright
|
---|
41 | // notice, this list of conditions and the following disclaimer in
|
---|
42 | // the documentation and/or other materials provided with the distribution.
|
---|
43 | //
|
---|
44 | // 3. The names of the authors may not be used to endorse or promote products
|
---|
45 | // derived from this software without specific prior written permission.
|
---|
46 | //
|
---|
47 | // THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
|
---|
48 | // INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
---|
49 | // FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
|
---|
50 | // INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
51 | // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
---|
52 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
|
---|
53 | // OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
---|
54 | // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
---|
55 | // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
|
---|
56 | // EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
57 | //
|
---|
58 | // -----------------------------------------------------------------------
|
---|
59 | //
|
---|
60 | // jzlib is based on zlib-1.1.3.
|
---|
61 | //
|
---|
62 | // The following notice applies to zlib:
|
---|
63 | //
|
---|
64 | // -----------------------------------------------------------------------
|
---|
65 | //
|
---|
66 | // Copyright (C) 1995-2004 Jean-loup Gailly and Mark Adler
|
---|
67 | //
|
---|
68 | // The ZLIB software is provided 'as-is', without any express or implied
|
---|
69 | // warranty. In no event will the authors be held liable for any damages
|
---|
70 | // arising from the use of this software.
|
---|
71 | //
|
---|
72 | // Permission is granted to anyone to use this software for any purpose,
|
---|
73 | // including commercial applications, and to alter it and redistribute it
|
---|
74 | // freely, subject to the following restrictions:
|
---|
75 | //
|
---|
76 | // 1. The origin of this software must not be misrepresented; you must not
|
---|
77 | // claim that you wrote the original software. If you use this software
|
---|
78 | // in a product, an acknowledgment in the product documentation would be
|
---|
79 | // appreciated but is not required.
|
---|
80 | // 2. Altered source versions must be plainly marked as such, and must not be
|
---|
81 | // misrepresented as being the original software.
|
---|
82 | // 3. This notice may not be removed or altered from any source distribution.
|
---|
83 | //
|
---|
84 | // Jean-loup Gailly jloup@gzip.org
|
---|
85 | // Mark Adler madler@alumni.caltech.edu
|
---|
86 | //
|
---|
87 | // -----------------------------------------------------------------------
|
---|
88 |
|
---|
89 |
|
---|
90 |
|
---|
91 | using System;
|
---|
92 | using Interop=System.Runtime.InteropServices;
|
---|
93 |
|
---|
94 | namespace OfficeOpenXml.Packaging.Ionic.Zlib
|
---|
95 | {
|
---|
96 |
|
---|
97 | /// <summary>
|
---|
98 | /// Describes how to flush the current deflate operation.
|
---|
99 | /// </summary>
|
---|
100 | /// <remarks>
|
---|
101 | /// The different FlushType values are useful when using a Deflate in a streaming application.
|
---|
102 | /// </remarks>
|
---|
103 | public enum FlushType
|
---|
104 | {
|
---|
105 | /// <summary>No flush at all.</summary>
|
---|
106 | None = 0,
|
---|
107 |
|
---|
108 | /// <summary>Closes the current block, but doesn't flush it to
|
---|
109 | /// the output. Used internally only in hypothetical
|
---|
110 | /// scenarios. This was supposed to be removed by Zlib, but it is
|
---|
111 | /// still in use in some edge cases.
|
---|
112 | /// </summary>
|
---|
113 | Partial,
|
---|
114 |
|
---|
115 | /// <summary>
|
---|
116 | /// Use this during compression to specify that all pending output should be
|
---|
117 | /// flushed to the output buffer and the output should be aligned on a byte
|
---|
118 | /// boundary. You might use this in a streaming communication scenario, so that
|
---|
119 | /// the decompressor can get all input data available so far. When using this
|
---|
120 | /// with a ZlibCodec, <c>AvailableBytesIn</c> will be zero after the call if
|
---|
121 | /// enough output space has been provided before the call. Flushing will
|
---|
122 | /// degrade compression and so it should be used only when necessary.
|
---|
123 | /// </summary>
|
---|
124 | Sync,
|
---|
125 |
|
---|
126 | /// <summary>
|
---|
127 | /// Use this during compression to specify that all output should be flushed, as
|
---|
128 | /// with <c>FlushType.Sync</c>, but also, the compression state should be reset
|
---|
129 | /// so that decompression can restart from this point if previous compressed
|
---|
130 | /// data has been damaged or if random access is desired. Using
|
---|
131 | /// <c>FlushType.Full</c> too often can significantly degrade the compression.
|
---|
132 | /// </summary>
|
---|
133 | Full,
|
---|
134 |
|
---|
135 | /// <summary>Signals the end of the compression/decompression stream.</summary>
|
---|
136 | Finish,
|
---|
137 | }
|
---|
138 |
|
---|
139 |
|
---|
140 | /// <summary>
|
---|
141 | /// The compression level to be used when using a DeflateStream or ZlibStream with CompressionMode.Compress.
|
---|
142 | /// </summary>
|
---|
143 | public enum CompressionLevel
|
---|
144 | {
|
---|
145 | /// <summary>
|
---|
146 | /// None means that the data will be simply stored, with no change at all.
|
---|
147 | /// If you are producing ZIPs for use on Mac OSX, be aware that archives produced with CompressionLevel.None
|
---|
148 | /// cannot be opened with the default zip reader. Use a different CompressionLevel.
|
---|
149 | /// </summary>
|
---|
150 | None= 0,
|
---|
151 | /// <summary>
|
---|
152 | /// Same as None.
|
---|
153 | /// </summary>
|
---|
154 | Level0 = 0,
|
---|
155 |
|
---|
156 | /// <summary>
|
---|
157 | /// The fastest but least effective compression.
|
---|
158 | /// </summary>
|
---|
159 | BestSpeed = 1,
|
---|
160 |
|
---|
161 | /// <summary>
|
---|
162 | /// A synonym for BestSpeed.
|
---|
163 | /// </summary>
|
---|
164 | Level1 = 1,
|
---|
165 |
|
---|
166 | /// <summary>
|
---|
167 | /// A little slower, but better, than level 1.
|
---|
168 | /// </summary>
|
---|
169 | Level2 = 2,
|
---|
170 |
|
---|
171 | /// <summary>
|
---|
172 | /// A little slower, but better, than level 2.
|
---|
173 | /// </summary>
|
---|
174 | Level3 = 3,
|
---|
175 |
|
---|
176 | /// <summary>
|
---|
177 | /// A little slower, but better, than level 3.
|
---|
178 | /// </summary>
|
---|
179 | Level4 = 4,
|
---|
180 |
|
---|
181 | /// <summary>
|
---|
182 | /// A little slower than level 4, but with better compression.
|
---|
183 | /// </summary>
|
---|
184 | Level5 = 5,
|
---|
185 |
|
---|
186 | /// <summary>
|
---|
187 | /// The default compression level, with a good balance of speed and compression efficiency.
|
---|
188 | /// </summary>
|
---|
189 | Default = 6,
|
---|
190 | /// <summary>
|
---|
191 | /// A synonym for Default.
|
---|
192 | /// </summary>
|
---|
193 | Level6 = 6,
|
---|
194 |
|
---|
195 | /// <summary>
|
---|
196 | /// Pretty good compression!
|
---|
197 | /// </summary>
|
---|
198 | Level7 = 7,
|
---|
199 |
|
---|
200 | /// <summary>
|
---|
201 | /// Better compression than Level7!
|
---|
202 | /// </summary>
|
---|
203 | Level8 = 8,
|
---|
204 |
|
---|
205 | /// <summary>
|
---|
206 | /// The "best" compression, where best means greatest reduction in size of the input data stream.
|
---|
207 | /// This is also the slowest compression.
|
---|
208 | /// </summary>
|
---|
209 | BestCompression = 9,
|
---|
210 |
|
---|
211 | /// <summary>
|
---|
212 | /// A synonym for BestCompression.
|
---|
213 | /// </summary>
|
---|
214 | Level9 = 9,
|
---|
215 | }
|
---|
216 |
|
---|
217 | /// <summary>
|
---|
218 | /// Describes options for how the compression algorithm is executed. Different strategies
|
---|
219 | /// work better on different sorts of data. The strategy parameter can affect the compression
|
---|
220 | /// ratio and the speed of compression but not the correctness of the compresssion.
|
---|
221 | /// </summary>
|
---|
222 | public enum CompressionStrategy
|
---|
223 | {
|
---|
224 | /// <summary>
|
---|
225 | /// The default strategy is probably the best for normal data.
|
---|
226 | /// </summary>
|
---|
227 | Default = 0,
|
---|
228 |
|
---|
229 | /// <summary>
|
---|
230 | /// The <c>Filtered</c> strategy is intended to be used most effectively with data produced by a
|
---|
231 | /// filter or predictor. By this definition, filtered data consists mostly of small
|
---|
232 | /// values with a somewhat random distribution. In this case, the compression algorithm
|
---|
233 | /// is tuned to compress them better. The effect of <c>Filtered</c> is to force more Huffman
|
---|
234 | /// coding and less string matching; it is a half-step between <c>Default</c> and <c>HuffmanOnly</c>.
|
---|
235 | /// </summary>
|
---|
236 | Filtered = 1,
|
---|
237 |
|
---|
238 | /// <summary>
|
---|
239 | /// Using <c>HuffmanOnly</c> will force the compressor to do Huffman encoding only, with no
|
---|
240 | /// string matching.
|
---|
241 | /// </summary>
|
---|
242 | HuffmanOnly = 2,
|
---|
243 | }
|
---|
244 |
|
---|
245 |
|
---|
246 | /// <summary>
|
---|
247 | /// An enum to specify the direction of transcoding - whether to compress or decompress.
|
---|
248 | /// </summary>
|
---|
249 | public enum CompressionMode
|
---|
250 | {
|
---|
251 | /// <summary>
|
---|
252 | /// Used to specify that the stream should compress the data.
|
---|
253 | /// </summary>
|
---|
254 | Compress= 0,
|
---|
255 | /// <summary>
|
---|
256 | /// Used to specify that the stream should decompress the data.
|
---|
257 | /// </summary>
|
---|
258 | Decompress = 1,
|
---|
259 | }
|
---|
260 |
|
---|
261 |
|
---|
262 | /// <summary>
|
---|
263 | /// A general purpose exception class for exceptions in the Zlib library.
|
---|
264 | /// </summary>
|
---|
265 | [Interop.GuidAttribute("ebc25cf6-9120-4283-b972-0e5520d0000E")]
|
---|
266 | public class ZlibException : System.Exception
|
---|
267 | {
|
---|
268 | /// <summary>
|
---|
269 | /// The ZlibException class captures exception information generated
|
---|
270 | /// by the Zlib library.
|
---|
271 | /// </summary>
|
---|
272 | public ZlibException()
|
---|
273 | : base()
|
---|
274 | {
|
---|
275 | }
|
---|
276 |
|
---|
277 | /// <summary>
|
---|
278 | /// This ctor collects a message attached to the exception.
|
---|
279 | /// </summary>
|
---|
280 | /// <param name="s">the message for the exception.</param>
|
---|
281 | public ZlibException(System.String s)
|
---|
282 | : base(s)
|
---|
283 | {
|
---|
284 | }
|
---|
285 | }
|
---|
286 |
|
---|
287 |
|
---|
288 | internal class SharedUtils
|
---|
289 | {
|
---|
290 | /// <summary>
|
---|
291 | /// Performs an unsigned bitwise right shift with the specified number
|
---|
292 | /// </summary>
|
---|
293 | /// <param name="number">Number to operate on</param>
|
---|
294 | /// <param name="bits">Ammount of bits to shift</param>
|
---|
295 | /// <returns>The resulting number from the shift operation</returns>
|
---|
296 | public static int URShift(int number, int bits)
|
---|
297 | {
|
---|
298 | return (int)((uint)number >> bits);
|
---|
299 | }
|
---|
300 |
|
---|
301 | #if NOT
|
---|
302 | /// <summary>
|
---|
303 | /// Performs an unsigned bitwise right shift with the specified number
|
---|
304 | /// </summary>
|
---|
305 | /// <param name="number">Number to operate on</param>
|
---|
306 | /// <param name="bits">Ammount of bits to shift</param>
|
---|
307 | /// <returns>The resulting number from the shift operation</returns>
|
---|
308 | public static long URShift(long number, int bits)
|
---|
309 | {
|
---|
310 | return (long) ((UInt64)number >> bits);
|
---|
311 | }
|
---|
312 | #endif
|
---|
313 |
|
---|
314 | /// <summary>
|
---|
315 | /// Reads a number of characters from the current source TextReader and writes
|
---|
316 | /// the data to the target array at the specified index.
|
---|
317 | /// </summary>
|
---|
318 | ///
|
---|
319 | /// <param name="sourceTextReader">The source TextReader to read from</param>
|
---|
320 | /// <param name="target">Contains the array of characteres read from the source TextReader.</param>
|
---|
321 | /// <param name="start">The starting index of the target array.</param>
|
---|
322 | /// <param name="count">The maximum number of characters to read from the source TextReader.</param>
|
---|
323 | ///
|
---|
324 | /// <returns>
|
---|
325 | /// The number of characters read. The number will be less than or equal to
|
---|
326 | /// count depending on the data available in the source TextReader. Returns -1
|
---|
327 | /// if the end of the stream is reached.
|
---|
328 | /// </returns>
|
---|
329 | public static System.Int32 ReadInput(System.IO.TextReader sourceTextReader, byte[] target, int start, int count)
|
---|
330 | {
|
---|
331 | // Returns 0 bytes if not enough space in target
|
---|
332 | if (target.Length == 0) return 0;
|
---|
333 |
|
---|
334 | char[] charArray = new char[target.Length];
|
---|
335 | int bytesRead = sourceTextReader.Read(charArray, start, count);
|
---|
336 |
|
---|
337 | // Returns -1 if EOF
|
---|
338 | if (bytesRead == 0) return -1;
|
---|
339 |
|
---|
340 | for (int index = start; index < start + bytesRead; index++)
|
---|
341 | target[index] = (byte)charArray[index];
|
---|
342 |
|
---|
343 | return bytesRead;
|
---|
344 | }
|
---|
345 |
|
---|
346 |
|
---|
347 | internal static byte[] ToByteArray(System.String sourceString)
|
---|
348 | {
|
---|
349 | return System.Text.UTF8Encoding.UTF8.GetBytes(sourceString);
|
---|
350 | }
|
---|
351 |
|
---|
352 |
|
---|
353 | internal static char[] ToCharArray(byte[] byteArray)
|
---|
354 | {
|
---|
355 | return System.Text.UTF8Encoding.UTF8.GetChars(byteArray);
|
---|
356 | }
|
---|
357 | }
|
---|
358 |
|
---|
359 | internal static class InternalConstants
|
---|
360 | {
|
---|
361 | internal static readonly int MAX_BITS = 15;
|
---|
362 | internal static readonly int BL_CODES = 19;
|
---|
363 | internal static readonly int D_CODES = 30;
|
---|
364 | internal static readonly int LITERALS = 256;
|
---|
365 | internal static readonly int LENGTH_CODES = 29;
|
---|
366 | internal static readonly int L_CODES = (LITERALS + 1 + LENGTH_CODES);
|
---|
367 |
|
---|
368 | // Bit length codes must not exceed MAX_BL_BITS bits
|
---|
369 | internal static readonly int MAX_BL_BITS = 7;
|
---|
370 |
|
---|
371 | // repeat previous bit length 3-6 times (2 bits of repeat count)
|
---|
372 | internal static readonly int REP_3_6 = 16;
|
---|
373 |
|
---|
374 | // repeat a zero length 3-10 times (3 bits of repeat count)
|
---|
375 | internal static readonly int REPZ_3_10 = 17;
|
---|
376 |
|
---|
377 | // repeat a zero length 11-138 times (7 bits of repeat count)
|
---|
378 | internal static readonly int REPZ_11_138 = 18;
|
---|
379 |
|
---|
380 | }
|
---|
381 |
|
---|
382 | internal sealed class StaticTree
|
---|
383 | {
|
---|
384 | internal static readonly short[] lengthAndLiteralsTreeCodes = new short[] {
|
---|
385 | 12, 8, 140, 8, 76, 8, 204, 8, 44, 8, 172, 8, 108, 8, 236, 8,
|
---|
386 | 28, 8, 156, 8, 92, 8, 220, 8, 60, 8, 188, 8, 124, 8, 252, 8,
|
---|
387 | 2, 8, 130, 8, 66, 8, 194, 8, 34, 8, 162, 8, 98, 8, 226, 8,
|
---|
388 | 18, 8, 146, 8, 82, 8, 210, 8, 50, 8, 178, 8, 114, 8, 242, 8,
|
---|
389 | 10, 8, 138, 8, 74, 8, 202, 8, 42, 8, 170, 8, 106, 8, 234, 8,
|
---|
390 | 26, 8, 154, 8, 90, 8, 218, 8, 58, 8, 186, 8, 122, 8, 250, 8,
|
---|
391 | 6, 8, 134, 8, 70, 8, 198, 8, 38, 8, 166, 8, 102, 8, 230, 8,
|
---|
392 | 22, 8, 150, 8, 86, 8, 214, 8, 54, 8, 182, 8, 118, 8, 246, 8,
|
---|
393 | 14, 8, 142, 8, 78, 8, 206, 8, 46, 8, 174, 8, 110, 8, 238, 8,
|
---|
394 | 30, 8, 158, 8, 94, 8, 222, 8, 62, 8, 190, 8, 126, 8, 254, 8,
|
---|
395 | 1, 8, 129, 8, 65, 8, 193, 8, 33, 8, 161, 8, 97, 8, 225, 8,
|
---|
396 | 17, 8, 145, 8, 81, 8, 209, 8, 49, 8, 177, 8, 113, 8, 241, 8,
|
---|
397 | 9, 8, 137, 8, 73, 8, 201, 8, 41, 8, 169, 8, 105, 8, 233, 8,
|
---|
398 | 25, 8, 153, 8, 89, 8, 217, 8, 57, 8, 185, 8, 121, 8, 249, 8,
|
---|
399 | 5, 8, 133, 8, 69, 8, 197, 8, 37, 8, 165, 8, 101, 8, 229, 8,
|
---|
400 | 21, 8, 149, 8, 85, 8, 213, 8, 53, 8, 181, 8, 117, 8, 245, 8,
|
---|
401 | 13, 8, 141, 8, 77, 8, 205, 8, 45, 8, 173, 8, 109, 8, 237, 8,
|
---|
402 | 29, 8, 157, 8, 93, 8, 221, 8, 61, 8, 189, 8, 125, 8, 253, 8,
|
---|
403 | 19, 9, 275, 9, 147, 9, 403, 9, 83, 9, 339, 9, 211, 9, 467, 9,
|
---|
404 | 51, 9, 307, 9, 179, 9, 435, 9, 115, 9, 371, 9, 243, 9, 499, 9,
|
---|
405 | 11, 9, 267, 9, 139, 9, 395, 9, 75, 9, 331, 9, 203, 9, 459, 9,
|
---|
406 | 43, 9, 299, 9, 171, 9, 427, 9, 107, 9, 363, 9, 235, 9, 491, 9,
|
---|
407 | 27, 9, 283, 9, 155, 9, 411, 9, 91, 9, 347, 9, 219, 9, 475, 9,
|
---|
408 | 59, 9, 315, 9, 187, 9, 443, 9, 123, 9, 379, 9, 251, 9, 507, 9,
|
---|
409 | 7, 9, 263, 9, 135, 9, 391, 9, 71, 9, 327, 9, 199, 9, 455, 9,
|
---|
410 | 39, 9, 295, 9, 167, 9, 423, 9, 103, 9, 359, 9, 231, 9, 487, 9,
|
---|
411 | 23, 9, 279, 9, 151, 9, 407, 9, 87, 9, 343, 9, 215, 9, 471, 9,
|
---|
412 | 55, 9, 311, 9, 183, 9, 439, 9, 119, 9, 375, 9, 247, 9, 503, 9,
|
---|
413 | 15, 9, 271, 9, 143, 9, 399, 9, 79, 9, 335, 9, 207, 9, 463, 9,
|
---|
414 | 47, 9, 303, 9, 175, 9, 431, 9, 111, 9, 367, 9, 239, 9, 495, 9,
|
---|
415 | 31, 9, 287, 9, 159, 9, 415, 9, 95, 9, 351, 9, 223, 9, 479, 9,
|
---|
416 | 63, 9, 319, 9, 191, 9, 447, 9, 127, 9, 383, 9, 255, 9, 511, 9,
|
---|
417 | 0, 7, 64, 7, 32, 7, 96, 7, 16, 7, 80, 7, 48, 7, 112, 7,
|
---|
418 | 8, 7, 72, 7, 40, 7, 104, 7, 24, 7, 88, 7, 56, 7, 120, 7,
|
---|
419 | 4, 7, 68, 7, 36, 7, 100, 7, 20, 7, 84, 7, 52, 7, 116, 7,
|
---|
420 | 3, 8, 131, 8, 67, 8, 195, 8, 35, 8, 163, 8, 99, 8, 227, 8
|
---|
421 | };
|
---|
422 |
|
---|
423 | internal static readonly short[] distTreeCodes = new short[] {
|
---|
424 | 0, 5, 16, 5, 8, 5, 24, 5, 4, 5, 20, 5, 12, 5, 28, 5,
|
---|
425 | 2, 5, 18, 5, 10, 5, 26, 5, 6, 5, 22, 5, 14, 5, 30, 5,
|
---|
426 | 1, 5, 17, 5, 9, 5, 25, 5, 5, 5, 21, 5, 13, 5, 29, 5,
|
---|
427 | 3, 5, 19, 5, 11, 5, 27, 5, 7, 5, 23, 5 };
|
---|
428 |
|
---|
429 | internal static readonly StaticTree Literals;
|
---|
430 | internal static readonly StaticTree Distances;
|
---|
431 | internal static readonly StaticTree BitLengths;
|
---|
432 |
|
---|
433 | internal short[] treeCodes; // static tree or null
|
---|
434 | internal int[] extraBits; // extra bits for each code or null
|
---|
435 | internal int extraBase; // base index for extra_bits
|
---|
436 | internal int elems; // max number of elements in the tree
|
---|
437 | internal int maxLength; // max bit length for the codes
|
---|
438 |
|
---|
439 | private StaticTree(short[] treeCodes, int[] extraBits, int extraBase, int elems, int maxLength)
|
---|
440 | {
|
---|
441 | this.treeCodes = treeCodes;
|
---|
442 | this.extraBits = extraBits;
|
---|
443 | this.extraBase = extraBase;
|
---|
444 | this.elems = elems;
|
---|
445 | this.maxLength = maxLength;
|
---|
446 | }
|
---|
447 | static StaticTree()
|
---|
448 | {
|
---|
449 | Literals = new StaticTree(lengthAndLiteralsTreeCodes, Tree.ExtraLengthBits, InternalConstants.LITERALS + 1, InternalConstants.L_CODES, InternalConstants.MAX_BITS);
|
---|
450 | Distances = new StaticTree(distTreeCodes, Tree.ExtraDistanceBits, 0, InternalConstants.D_CODES, InternalConstants.MAX_BITS);
|
---|
451 | BitLengths = new StaticTree(null, Tree.extra_blbits, 0, InternalConstants.BL_CODES, InternalConstants.MAX_BL_BITS);
|
---|
452 | }
|
---|
453 | }
|
---|
454 |
|
---|
455 |
|
---|
456 |
|
---|
457 | /// <summary>
|
---|
458 | /// Computes an Adler-32 checksum.
|
---|
459 | /// </summary>
|
---|
460 | /// <remarks>
|
---|
461 | /// The Adler checksum is similar to a CRC checksum, but faster to compute, though less
|
---|
462 | /// reliable. It is used in producing RFC1950 compressed streams. The Adler checksum
|
---|
463 | /// is a required part of the "ZLIB" standard. Applications will almost never need to
|
---|
464 | /// use this class directly.
|
---|
465 | /// </remarks>
|
---|
466 | ///
|
---|
467 | /// <exclude/>
|
---|
468 | public sealed class Adler
|
---|
469 | {
|
---|
470 | // largest prime smaller than 65536
|
---|
471 | private static readonly uint BASE = 65521;
|
---|
472 | // NMAX is the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1
|
---|
473 | private static readonly int NMAX = 5552;
|
---|
474 |
|
---|
475 |
|
---|
476 | #pragma warning disable 3001
|
---|
477 | #pragma warning disable 3002
|
---|
478 |
|
---|
479 | /// <summary>
|
---|
480 | /// Calculates the Adler32 checksum.
|
---|
481 | /// </summary>
|
---|
482 | /// <remarks>
|
---|
483 | /// <para>
|
---|
484 | /// This is used within ZLIB. You probably don't need to use this directly.
|
---|
485 | /// </para>
|
---|
486 | /// </remarks>
|
---|
487 | /// <example>
|
---|
488 | /// To compute an Adler32 checksum on a byte array:
|
---|
489 | /// <code>
|
---|
490 | /// var adler = Adler.Adler32(0, null, 0, 0);
|
---|
491 | /// adler = Adler.Adler32(adler, buffer, index, length);
|
---|
492 | /// </code>
|
---|
493 | /// </example>
|
---|
494 | public static uint Adler32(uint adler, byte[] buf, int index, int len)
|
---|
495 | {
|
---|
496 | if (buf == null)
|
---|
497 | return 1;
|
---|
498 |
|
---|
499 | uint s1 = (uint) (adler & 0xffff);
|
---|
500 | uint s2 = (uint) ((adler >> 16) & 0xffff);
|
---|
501 |
|
---|
502 | while (len > 0)
|
---|
503 | {
|
---|
504 | int k = len < NMAX ? len : NMAX;
|
---|
505 | len -= k;
|
---|
506 | while (k >= 16)
|
---|
507 | {
|
---|
508 | //s1 += (buf[index++] & 0xff); s2 += s1;
|
---|
509 | s1 += buf[index++]; s2 += s1;
|
---|
510 | s1 += buf[index++]; s2 += s1;
|
---|
511 | s1 += buf[index++]; s2 += s1;
|
---|
512 | s1 += buf[index++]; s2 += s1;
|
---|
513 | s1 += buf[index++]; s2 += s1;
|
---|
514 | s1 += buf[index++]; s2 += s1;
|
---|
515 | s1 += buf[index++]; s2 += s1;
|
---|
516 | s1 += buf[index++]; s2 += s1;
|
---|
517 | s1 += buf[index++]; s2 += s1;
|
---|
518 | s1 += buf[index++]; s2 += s1;
|
---|
519 | s1 += buf[index++]; s2 += s1;
|
---|
520 | s1 += buf[index++]; s2 += s1;
|
---|
521 | s1 += buf[index++]; s2 += s1;
|
---|
522 | s1 += buf[index++]; s2 += s1;
|
---|
523 | s1 += buf[index++]; s2 += s1;
|
---|
524 | s1 += buf[index++]; s2 += s1;
|
---|
525 | k -= 16;
|
---|
526 | }
|
---|
527 | if (k != 0)
|
---|
528 | {
|
---|
529 | do
|
---|
530 | {
|
---|
531 | s1 += buf[index++];
|
---|
532 | s2 += s1;
|
---|
533 | }
|
---|
534 | while (--k != 0);
|
---|
535 | }
|
---|
536 | s1 %= BASE;
|
---|
537 | s2 %= BASE;
|
---|
538 | }
|
---|
539 | return (uint)((s2 << 16) | s1);
|
---|
540 | }
|
---|
541 | #pragma warning restore 3001
|
---|
542 | #pragma warning restore 3002
|
---|
543 |
|
---|
544 | }
|
---|
545 |
|
---|
546 | } |
---|