// Zlib.cs // ------------------------------------------------------------------ // // Copyright (c) 2009 Dino Chiesa and Microsoft Corporation. // All rights reserved. // // This code module is part of DotNetZip, a zipfile class library. // // ------------------------------------------------------------------ // // This code is licensed under the Microsoft Public License. // See the file License.txt for the license details. // More info on: http://dotnetzip.codeplex.com // // ------------------------------------------------------------------ // // last saved (in emacs): // Time-stamp: <2009-November-07 05:26:55> // // ------------------------------------------------------------------ // // This module defines classes for ZLIB compression and // decompression. This code is derived from the jzlib implementation of // zlib, but significantly modified. The object model is not the same, // and many of the behaviors are new or different. Nonetheless, in // keeping with the license for jzlib, the copyright to that code is // included below. // // ------------------------------------------------------------------ // // Copyright (c) 2000,2001,2002,2003 ymnk, JCraft,Inc. All rights reserved. // // Redistribution and use in source and binary forms, with or without // modification, are permitted provided that the following conditions are met: // // 1. Redistributions of source code must retain the above copyright notice, // this list of conditions and the following disclaimer. // // 2. Redistributions in binary form must reproduce the above copyright // notice, this list of conditions and the following disclaimer in // the documentation and/or other materials provided with the distribution. // // 3. The names of the authors may not be used to endorse or promote products // derived from this software without specific prior written permission. // // THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, // INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND // FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT, // INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT, // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, // OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, // EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. // // ----------------------------------------------------------------------- // // This program is based on zlib-1.1.3; credit to authors // Jean-loup Gailly(jloup@gzip.org) and Mark Adler(madler@alumni.caltech.edu) // and contributors of zlib. // // ----------------------------------------------------------------------- namespace SharpCompress.Compressor.Deflate { /// /// The compression level to be used when using a DeflateStream or ZlibStream with CompressionMode.Compress. /// internal enum CompressionLevel { /// /// None means that the data will be simply stored, with no change at all. /// If you are producing ZIPs for use on Mac OSX, be aware that archives produced with CompressionLevel.None /// cannot be opened with the default zip reader. Use a different CompressionLevel. /// None = 0, /// /// Same as None. /// Level0 = 0, /// /// The fastest but least effective compression. /// BestSpeed = 1, /// /// A synonym for BestSpeed. /// Level1 = 1, /// /// A little slower, but better, than level 1. /// Level2 = 2, /// /// A little slower, but better, than level 2. /// Level3 = 3, /// /// A little slower, but better, than level 3. /// Level4 = 4, /// /// A little slower than level 4, but with better compression. /// Level5 = 5, /// /// The default compression level, with a good balance of speed and compression efficiency. /// Default = 6, /// /// A synonym for Default. /// Level6 = 6, /// /// Pretty good compression! /// Level7 = 7, /// /// Better compression than Level7! /// Level8 = 8, /// /// The "best" compression, where best means greatest reduction in size of the input data stream. /// This is also the slowest compression. /// BestCompression = 9, /// /// A synonym for BestCompression. /// Level9 = 9, } /// /// Describes options for how the compression algorithm is executed. Different strategies /// work better on different sorts of data. The strategy parameter can affect the compression /// ratio and the speed of compression but not the correctness of the compresssion. /// internal enum CompressionStrategy { /// /// The default strategy is probably the best for normal data. /// Default = 0, /// /// The Filtered strategy is intended to be used most effectively with data produced by a /// filter or predictor. By this definition, filtered data consists mostly of small /// values with a somewhat random distribution. In this case, the compression algorithm /// is tuned to compress them better. The effect of Filtered is to force more Huffman /// coding and less string matching; it is a half-step between Default and HuffmanOnly. /// Filtered = 1, /// /// Using HuffmanOnly will force the compressor to do Huffman encoding only, with no /// string matching. /// HuffmanOnly = 2, } /// /// A general purpose exception class for exceptions in the Zlib library. /// internal class ZlibException : System.Exception { /// /// The ZlibException class captures exception information generated /// by the Zlib library. /// public ZlibException() : base() { } /// /// This ctor collects a message attached to the exception. /// /// public ZlibException(System.String s) : base(s) { } } /// /// Computes an Adler-32 checksum. /// /// /// The Adler checksum is similar to a CRC checksum, but faster to compute, though less /// reliable. It is used in producing RFC1950 compressed streams. The Adler checksum /// is a required part of the "ZLIB" standard. Applications will almost never need to /// use this class directly. /// internal sealed class Adler { // largest prime smaller than 65536 private static readonly int BASE = 65521; // NMAX is the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1 private static readonly int NMAX = 5552; internal static uint Adler32(uint adler, byte[] buf, int index, int len) { if (buf == null) return 1; int s1 = (int) (adler & 0xffff); int s2 = (int) ((adler >> 16) & 0xffff); while (len > 0) { int k = len < NMAX ? len : NMAX; len -= k; while (k >= 16) { //s1 += (buf[index++] & 0xff); s2 += s1; s1 += buf[index++]; s2 += s1; s1 += buf[index++]; s2 += s1; s1 += buf[index++]; s2 += s1; s1 += buf[index++]; s2 += s1; s1 += buf[index++]; s2 += s1; s1 += buf[index++]; s2 += s1; s1 += buf[index++]; s2 += s1; s1 += buf[index++]; s2 += s1; s1 += buf[index++]; s2 += s1; s1 += buf[index++]; s2 += s1; s1 += buf[index++]; s2 += s1; s1 += buf[index++]; s2 += s1; s1 += buf[index++]; s2 += s1; s1 += buf[index++]; s2 += s1; s1 += buf[index++]; s2 += s1; s1 += buf[index++]; s2 += s1; k -= 16; } if (k != 0) { do { s1 += buf[index++]; s2 += s1; } while (--k != 0); } s1 %= BASE; s2 %= BASE; } return (uint) ((s2 << 16) | s1); } } }