Example #1
0
        public Deflate(
            ZLibStream zStream,
            ZlibOptions options,
            int method,
            int windowBits,
            int memLevel)
        {
            int noheader = 0;

            zStream.Message = null;

            CompressionLevel    level    = options.CompressionLevel.GetValueOrDefault();
            CompressionStrategy strategy = options.CompressionStrategy;

            if (level == CompressionLevel.DefaultCompression)
            {
                level = CompressionLevel.Level6;
            }

            if (level == CompressionLevel.NoCompression)
            {
                memLevel = DEFNOCOMPRESSIONMEMLEVEL;
            }

            if (windowBits < 0)
            {
                // undocumented feature: suppress zlib header
                noheader   = 1;
                windowBits = -windowBits;
            }

            if (memLevel < 1 || memLevel > MAXMEMLEVEL)
            {
                ThrowHelper.ThrowArgumentRangeException(nameof(memLevel));
            }

            if (method != ZDEFLATED)
            {
                ThrowHelper.ThrowArgumentRangeException(nameof(method));
            }

            if (windowBits < 9 || windowBits > 15)
            {
                ThrowHelper.ThrowArgumentRangeException(nameof(windowBits));
            }

            if (level < CompressionLevel.NoCompression || level > CompressionLevel.BestCompression)
            {
                ThrowHelper.ThrowArgumentRangeException(nameof(level));
            }

            if (strategy < CompressionStrategy.DefaultStrategy || strategy > CompressionStrategy.Fixed)
            {
                ThrowHelper.ThrowArgumentRangeException(nameof(strategy));
            }

#if USE_QUICK
            if (level == ZlibCompressionLevel.ZBESTSPEED)
            {
                windowBits = 13;
            }
#endif
            this.NoHeader = noheader;
            this.wBits    = windowBits;
            this.wSize    = 1 << this.wBits;
            this.wMask    = this.wSize - 1;

            this.hashBits = memLevel + 7;
            this.hashSize = 1 << this.hashBits;
            this.hashMask = (uint)this.hashSize - 1;

            this.litBufsize = 1 << (memLevel + 6); // 16K elements by default
            this.dBuf       = this.litBufsize;
            this.lBuf       = (1 + 2) * this.litBufsize;

            this.level    = level;
            this.Strategy = strategy;
            this.method   = (byte)method;

            this.FixedBuffers   = new FixedLengthBuffers();
            this.DynamicBuffers = new DynamicLengthBuffers(this.wSize, this.hashSize, this.litBufsize * 4);

            this.DeflateReset(zStream);
        }
Example #2
0
 /// <summary>
 /// Initializes compression.
 /// </summary>
 /// <param name="options">The options.</param>
 /// <param name="windowBits">The window bits to use.</param>
 public void DeflateInit(ZlibOptions options, int windowBits)
 => this.DeflateState = new Deflate(this, options, windowBits);
Example #3
0
 /// <summary>
 /// Initializes a new instance of the <see cref="Deflate"/> class.
 /// </summary>
 /// <param name="zStream">The zlib stream.</param>
 /// <param name="options">The zlib options.</param>
 /// <param name="windowBits">The window size in bits.</param>
 public Deflate(ZLibStream zStream, ZlibOptions options, int windowBits)
     : this(zStream, options, ZDEFLATED, windowBits, DEFMEMLEVEL)
 {
 }
Example #4
0
 /// <summary>
 /// Initializes compression.
 /// </summary>
 /// <param name="options">The options.</param>
 public void DeflateInit(ZlibOptions options)
 => this.DeflateInit(options, MAXWBITS);