Пример #1
0
        internal int SetDictionary(byte[] dictionary)
        {
            int num         = dictionary.Length;
            int sourceIndex = 0;

            if (dictionary == null || this.status != DeflateManager.INIT_STATE)
            {
                throw new ZlibException("Stream error.");
            }
            this._codec._Adler32 = Adler.Adler32(this._codec._Adler32, dictionary, 0, dictionary.Length);
            if (num < DeflateManager.MIN_MATCH)
            {
                return(0);
            }
            if (num > this.w_size - DeflateManager.MIN_LOOKAHEAD)
            {
                num         = this.w_size - DeflateManager.MIN_LOOKAHEAD;
                sourceIndex = dictionary.Length - num;
            }
            Array.Copy(dictionary, sourceIndex, this.window, 0, num);
            this.strstart    = num;
            this.block_start = num;
            this.ins_h       = (int)(this.window[0] & 255);
            this.ins_h       = ((this.ins_h << this.hash_shift ^ (int)(this.window[1] & 255)) & this.hash_mask);
            for (int i = 0; i <= num - DeflateManager.MIN_MATCH; i++)
            {
                this.ins_h = ((this.ins_h << this.hash_shift ^ (int)(this.window[i + (DeflateManager.MIN_MATCH - 1)] & 255)) & this.hash_mask);
                this.prev[i & this.w_mask] = this.head[this.ins_h];
                this.head[this.ins_h]      = (short)i;
            }
            return(0);
        }
Пример #2
0
 internal int Flush(int r)
 {
     for (int i = 0; i < 2; i++)
     {
         int num;
         if (i == 0)
         {
             num = ((this.readAt > this.writeAt) ? this.end : this.writeAt) - this.readAt;
         }
         else
         {
             num = this.writeAt - this.readAt;
         }
         if (num == 0)
         {
             if (r == -5)
             {
                 r = 0;
             }
             return(r);
         }
         if (num > this._codec.AvailableBytesOut)
         {
             num = this._codec.AvailableBytesOut;
         }
         if (num != 0 && r == -5)
         {
             r = 0;
         }
         this._codec.AvailableBytesOut -= num;
         this._codec.TotalBytesOut     += (long)num;
         if (this.checkfn != null)
         {
             this._codec._Adler32 = (this.check = Adler.Adler32(this.check, this.window, this.readAt, num));
         }
         Array.Copy(this.window, this.readAt, this._codec.OutputBuffer, this._codec.NextOut, num);
         this._codec.NextOut += num;
         this.readAt         += num;
         if (this.readAt == this.end && i == 0)
         {
             this.readAt = 0;
             if (this.writeAt == this.end)
             {
                 this.writeAt = 0;
             }
         }
         else
         {
             i++;
         }
     }
     return(r);
 }
Пример #3
0
 internal void Reset()
 {
     this._codec.TotalBytesIn = (this._codec.TotalBytesOut = 0L);
     this._codec.Message      = null;
     this.pendingCount        = 0;
     this.nextPending         = 0;
     this.Rfc1950BytesEmitted = false;
     this.status          = ((!this.WantRfc1950HeaderBytes) ? DeflateManager.BUSY_STATE : DeflateManager.INIT_STATE);
     this._codec._Adler32 = Adler.Adler32(0u, null, 0, 0);
     this.last_flush      = 0;
     this._InitializeTreeData();
     this._InitializeLazyMatch();
 }
Пример #4
0
        internal uint Reset()
        {
            uint result = this.check;

            this.mode   = InflateBlocks.InflateBlockMode.TYPE;
            this.bitk   = 0;
            this.bitb   = 0;
            this.readAt = (this.writeAt = 0);
            if (this.checkfn != null)
            {
                this._codec._Adler32 = (this.check = Adler.Adler32(0u, null, 0, 0));
            }
            return(result);
        }
Пример #5
0
        internal int read_buf(byte[] buf, int start, int size)
        {
            int num = this.AvailableBytesIn;

            if (num > size)
            {
                num = size;
            }
            if (num == 0)
            {
                return(0);
            }
            this.AvailableBytesIn -= num;
            if (this.dstate.WantRfc1950HeaderBytes)
            {
                this._Adler32 = Adler.Adler32(this._Adler32, this.InputBuffer, this.NextIn, num);
            }
            Array.Copy(this.InputBuffer, this.NextIn, buf, start, num);
            this.NextIn       += num;
            this.TotalBytesIn += (long)num;
            return(num);
        }
Пример #6
0
        internal int SetDictionary(byte[] dictionary)
        {
            int start = 0;
            int num   = dictionary.Length;

            if (this.mode != InflateManager.InflateManagerMode.DICT0)
            {
                throw new ZlibException("Stream error.");
            }
            if (Adler.Adler32(1u, dictionary, 0, dictionary.Length) != this._codec._Adler32)
            {
                return(-3);
            }
            this._codec._Adler32 = Adler.Adler32(0u, null, 0, 0);
            if (num >= 1 << this.wbits)
            {
                num   = (1 << this.wbits) - 1;
                start = dictionary.Length - num;
            }
            this.blocks.SetDictionary(dictionary, start, num);
            this.mode = InflateManager.InflateManagerMode.BLOCKS;
            return(0);
        }
Пример #7
0
        // Read a new buffer from the current input stream, update the adler32
        // and total number of bytes read.  All deflate() input goes through
        // this function so some applications may wish to modify it to avoid
        // allocating a large strm->next_in buffer and copying from it.
        // (See also flush_pending()).
        internal int read_buf(byte[] buf, int start, int size)
        {
            int len = AvailableBytesIn;

            if (len > size)
            {
                len = size;
            }
            if (len == 0)
            {
                return(0);
            }

            AvailableBytesIn -= len;

            if (dstate.WantRfc1950HeaderBytes)
            {
                _Adler32 = Adler.Adler32(_Adler32, InputBuffer, NextIn, len);
            }
            Array.Copy(InputBuffer, NextIn, buf, start, len);
            NextIn       += len;
            TotalBytesIn += len;
            return(len);
        }
Пример #8
0
        internal int Deflate(FlushType flush)
        {
            if (this._codec.OutputBuffer == null || (this._codec.InputBuffer == null && this._codec.AvailableBytesIn != 0) || (this.status == DeflateManager.FINISH_STATE && flush != FlushType.Finish))
            {
                this._codec.Message = DeflateManager._ErrorMessage[4];
                throw new ZlibException(string.Format("Something is fishy. [{0}]", this._codec.Message));
            }
            if (this._codec.AvailableBytesOut == 0)
            {
                this._codec.Message = DeflateManager._ErrorMessage[7];
                throw new ZlibException("OutputBuffer is full (AvailableBytesOut == 0)");
            }
            int num = this.last_flush;

            this.last_flush = (int)flush;
            if (this.status == DeflateManager.INIT_STATE)
            {
                int num2 = DeflateManager.Z_DEFLATED + (this.w_bits - 8 << 4) << 8;
                int num3 = (this.compressionLevel - CompressionLevel.BestSpeed & 255) >> 1;
                if (num3 > 3)
                {
                    num3 = 3;
                }
                num2 |= num3 << 6;
                if (this.strstart != 0)
                {
                    num2 |= DeflateManager.PRESET_DICT;
                }
                num2       += 31 - num2 % 31;
                this.status = DeflateManager.BUSY_STATE;
                this.pending[this.pendingCount++] = (byte)(num2 >> 8);
                this.pending[this.pendingCount++] = (byte)num2;
                if (this.strstart != 0)
                {
                    this.pending[this.pendingCount++] = (byte)((this._codec._Adler32 & 4278190080u) >> 24);
                    this.pending[this.pendingCount++] = (byte)((this._codec._Adler32 & 16711680u) >> 16);
                    this.pending[this.pendingCount++] = (byte)((this._codec._Adler32 & 65280u) >> 8);
                    this.pending[this.pendingCount++] = (byte)(this._codec._Adler32 & 255u);
                }
                this._codec._Adler32 = Adler.Adler32(0u, null, 0, 0);
            }
            if (this.pendingCount != 0)
            {
                this._codec.flush_pending();
                if (this._codec.AvailableBytesOut == 0)
                {
                    this.last_flush = -1;
                    return(0);
                }
            }
            else if (this._codec.AvailableBytesIn == 0 && flush <= (FlushType)num && flush != FlushType.Finish)
            {
                return(0);
            }
            if (this.status == DeflateManager.FINISH_STATE && this._codec.AvailableBytesIn != 0)
            {
                this._codec.Message = DeflateManager._ErrorMessage[7];
                throw new ZlibException("status == FINISH_STATE && _codec.AvailableBytesIn != 0");
            }
            if (this._codec.AvailableBytesIn != 0 || this.lookahead != 0 || (flush != FlushType.None && this.status != DeflateManager.FINISH_STATE))
            {
                BlockState blockState = this.DeflateFunction(flush);
                if (blockState == BlockState.FinishStarted || blockState == BlockState.FinishDone)
                {
                    this.status = DeflateManager.FINISH_STATE;
                }
                if (blockState == BlockState.NeedMore || blockState == BlockState.FinishStarted)
                {
                    if (this._codec.AvailableBytesOut == 0)
                    {
                        this.last_flush = -1;
                    }
                    return(0);
                }
                if (blockState == BlockState.BlockDone)
                {
                    if (flush == FlushType.Partial)
                    {
                        this._tr_align();
                    }
                    else
                    {
                        this._tr_stored_block(0, 0, false);
                        if (flush == FlushType.Full)
                        {
                            for (int i = 0; i < this.hash_size; i++)
                            {
                                this.head[i] = 0;
                            }
                        }
                    }
                    this._codec.flush_pending();
                    if (this._codec.AvailableBytesOut == 0)
                    {
                        this.last_flush = -1;
                        return(0);
                    }
                }
            }
            if (flush != FlushType.Finish)
            {
                return(0);
            }
            if (!this.WantRfc1950HeaderBytes || this.Rfc1950BytesEmitted)
            {
                return(1);
            }
            this.pending[this.pendingCount++] = (byte)((this._codec._Adler32 & 4278190080u) >> 24);
            this.pending[this.pendingCount++] = (byte)((this._codec._Adler32 & 16711680u) >> 16);
            this.pending[this.pendingCount++] = (byte)((this._codec._Adler32 & 65280u) >> 8);
            this.pending[this.pendingCount++] = (byte)(this._codec._Adler32 & 255u);
            this._codec.flush_pending();
            this.Rfc1950BytesEmitted = true;
            return((this.pendingCount == 0) ? 1 : 0);
        }