Adler32() public static méthode

Calculates the Adler32 checksum.

This is used within ZLIB. You probably don't need to use this directly.

public static Adler32 ( uint adler, byte buf, int index, int len ) : uint
adler uint
buf byte
index int
len int
Résultat uint
        internal int SetDictionary(byte[] dictionary)
        {
            int num         = dictionary.Length;
            int sourceIndex = 0;

            if (dictionary == null || status != INIT_STATE)
            {
                throw new ZlibException("Stream error.");
            }
            _codec._Adler32 = Adler.Adler32(_codec._Adler32, dictionary, 0, dictionary.Length);
            if (num < MIN_MATCH)
            {
                return(0);
            }
            if (num > w_size - MIN_LOOKAHEAD)
            {
                num         = w_size - MIN_LOOKAHEAD;
                sourceIndex = dictionary.Length - num;
            }
            Array.Copy(dictionary, sourceIndex, window, 0, num);
            strstart    = num;
            block_start = num;
            ins_h       = (window[0] & 0xFF);
            ins_h       = (((ins_h << hash_shift) ^ (window[1] & 0xFF)) & hash_mask);
            for (int i = 0; i <= num - MIN_MATCH; i++)
            {
                ins_h            = (((ins_h << hash_shift) ^ (window[i + (MIN_MATCH - 1)] & 0xFF)) & hash_mask);
                prev[i & w_mask] = head[ins_h];
                head[ins_h]      = (short)i;
            }
            return(0);
        }
Exemple #2
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)
        {
            var 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);
        }
Exemple #3
0
        internal int SetDictionary(byte[] dictionary)
        {
            int length      = dictionary.Length;
            int sourceIndex = 0;

            if ((dictionary == null) || (this.status != 0x2a))
            {
                throw new ZlibException("Stream error.");
            }
            this._codec._Adler32 = Adler.Adler32(this._codec._Adler32, dictionary, 0, dictionary.Length);
            if (length >= 3)
            {
                if (length > (this.w_size - MIN_LOOKAHEAD))
                {
                    length      = this.w_size - MIN_LOOKAHEAD;
                    sourceIndex = dictionary.Length - length;
                }
                Array.Copy(dictionary, sourceIndex, this.window, 0, length);
                this.strstart    = length;
                this.block_start = length;
                this.ins_h       = this.window[0] & 0xff;
                this.ins_h       = ((this.ins_h << this.hash_shift) ^ (this.window[1] & 0xff)) & this.hash_mask;
                for (int i = 0; i <= (length - 3); i++)
                {
                    this.ins_h = ((this.ins_h << this.hash_shift) ^ (this.window[i + 2] & 0xff)) & this.hash_mask;
                    this.prev[i & this.w_mask] = this.head[this.ins_h];
                    this.head[this.ins_h]      = (short)i;
                }
            }
            return(0);
        }
        internal int read_buf(byte[] buf, int start, int size)
        {
            int num = this.AvailableBytesIn;

            if (num > size)
            {
                num = size;
            }
            int result;

            if (num == 0)
            {
                result = 0;
            }
            else
            {
                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;
                result             = num;
            }
            return(result);
        }
Exemple #5
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 ReadBuf(byte[] buf, int start, int size)
        {
            if (dstate == null)
            {
                throw new InvalidOperationException("Deflate ended");
            }
            if (InputBuffer == null)
            {
                throw new InvalidOperationException($"{nameof(InputBuffer)} is not set");
            }

            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);
        }
Exemple #6
0
        internal int SetDictionary(byte[] dictionary)
        {
            int index  = 0;
            int length = dictionary.Length;

            if (mode != InflateManagerMode.DICT0)
            {
                throw new ZlibException("Stream error.");
            }

            if (Adler.Adler32(1, dictionary, 0, dictionary.Length) != _codec._Adler32)
            {
                return(ZlibConstants.Z_DATA_ERROR);
            }

            _codec._Adler32 = Adler.Adler32(0, null, 0, 0);

            if (length >= (1 << wbits))
            {
                length = (1 << wbits) - 1;
                index  = dictionary.Length - length;
            }
            blocks.SetDictionary(dictionary, index, length);
            mode = InflateManagerMode.BLOCKS;
            return(ZlibConstants.Z_OK);
        }
Exemple #7
0
        internal int SetDictionary(byte[] dictionary)
        {
            int start = 0;
            int num   = dictionary.Length;

            if (this.mode != InflateManager.InflateManagerMode.DICT0)
            {
                throw new ZlibException("Stream error.");
            }
            int result;

            if (Adler.Adler32(1u, dictionary, 0, dictionary.Length) != this._codec._Adler32)
            {
                result = -3;
            }
            else
            {
                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;
                result    = 0;
            }
            return(result);
        }
Exemple #8
0
        internal int SetDictionary(byte[] dictionary)
        {
            int num         = dictionary.Length;
            int sourceIndex = 0;

            if (dictionary != null && this.status == DeflateManager.INIT_STATE)
            {
                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       = (this.window[0] & 0xFF);
                this.ins_h       = ((this.ins_h << this.hash_shift ^ (this.window[1] & 0xFF)) & this.hash_mask);
                for (int i = 0; i <= num - DeflateManager.MIN_MATCH; i++)
                {
                    this.ins_h = ((this.ins_h << this.hash_shift ^ (this.window[i + (DeflateManager.MIN_MATCH - 1)] & 0xFF)) & this.hash_mask);
                    this.prev[i & this.w_mask] = this.head[this.ins_h];
                    this.head[this.ins_h]      = (short)i;
                }
                return(0);
            }
            throw new ZlibException("Stream error.");
        }
        internal int Flush(int r)
        {
            int nextOut = this._codec.NextOut;
            int read    = this.read;
            int len     = ((read <= this.write) ? this.write : this.end) - read;

            if (len > this._codec.AvailableBytesOut)
            {
                len = this._codec.AvailableBytesOut;
            }
            if ((len != 0) && (r == -5))
            {
                r = 0;
            }
            this._codec.AvailableBytesOut -= len;
            this._codec.TotalBytesOut     += len;
            if (this.checkfn != null)
            {
                this._codec._Adler32 = this.check = Adler.Adler32(this.check, this.window, read, len);
            }
            Array.Copy(this.window, read, this._codec.OutputBuffer, nextOut, len);
            nextOut += len;
            read    += len;
            if (read == this.end)
            {
                read = 0;
                if (this.write == this.end)
                {
                    this.write = 0;
                }
                len = this.write - read;
                if (len > this._codec.AvailableBytesOut)
                {
                    len = this._codec.AvailableBytesOut;
                }
                if ((len != 0) && (r == -5))
                {
                    r = 0;
                }
                this._codec.AvailableBytesOut -= len;
                this._codec.TotalBytesOut     += len;
                if (this.checkfn != null)
                {
                    this._codec._Adler32 = this.check = Adler.Adler32(this.check, this.window, read, len);
                }
                Array.Copy(this.window, read, this._codec.OutputBuffer, nextOut, len);
                nextOut += len;
                read    += len;
            }
            this._codec.NextOut = nextOut;
            this.read           = read;
            return(r);
        }
 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);
 }
Exemple #11
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 ? 0x2a : 0x71;
     this._codec._Adler32 = Adler.Adler32(0L, null, 0, 0);
     this.last_flush      = 0;
     this._InitializeTreeData();
     this._InitializeLazyMatch();
 }
Exemple #12
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();
 }
Exemple #13
0
 internal void Reset()
 {
     _codec.TotalBytesIn = (_codec.TotalBytesOut = 0L);
     _codec.Message      = null;
     pendingCount        = 0;
     nextPending         = 0;
     Rfc1950BytesEmitted = false;
     status          = (WantRfc1950HeaderBytes ? INIT_STATE : BUSY_STATE);
     _codec._Adler32 = Adler.Adler32(0u, null, 0, 0);
     last_flush      = 0;
     _InitializeTreeData();
     _InitializeLazyMatch();
 }
        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);
        }
Exemple #15
0
        internal uint Reset()
        {
            uint num = 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, (byte[])null, 0, 0);
            }
            return(num);
        }
Exemple #16
0
        internal uint Reset()
        {
            uint result = check;

            mode   = InflateBlockMode.TYPE;
            bitk   = 0;
            bitb   = 0;
            readAt = (writeAt = 0);
            if (checkfn != null)
            {
                _codec._Adler32 = (check = Adler.Adler32(0u, null, 0, 0));
            }
            return(result);
        }
Exemple #17
0
        internal uint Reset()
        {
            uint oldCheck = check;

            mode   = InflateBlockMode.TYPE;
            bitk   = 0;
            bitb   = 0;
            readAt = writeAt = 0;

            if (checkfn != null)
            {
                _codec._Adler32 = check = Adler.Adler32(0, null, 0, 0);
            }
            return(oldCheck);
        }
Exemple #18
0
 internal int Flush(int r)
 {
     for (int pass = 0; pass < 2; pass++)
     {
         int nBytes = ((pass != 0) ? (writeAt - readAt) : (((readAt <= writeAt) ? writeAt : end) - readAt));
         if (nBytes == 0)
         {
             if (r == -5)
             {
                 r = 0;
             }
             return(r);
         }
         if (nBytes > _codec.AvailableBytesOut)
         {
             nBytes = _codec.AvailableBytesOut;
         }
         if (nBytes != 0 && r == -5)
         {
             r = 0;
         }
         _codec.AvailableBytesOut -= nBytes;
         _codec.TotalBytesOut     += nBytes;
         if (checkfn != null)
         {
             _codec._Adler32 = (check = Adler.Adler32(check, window, readAt, nBytes));
         }
         Array.Copy(window, readAt, _codec.OutputBuffer, _codec.NextOut, nBytes);
         _codec.NextOut += nBytes;
         readAt         += nBytes;
         if (readAt == end && pass == 0)
         {
             readAt = 0;
             if (writeAt == end)
             {
                 writeAt = 0;
             }
         }
         else
         {
             pass++;
         }
     }
     return(r);
 }
Exemple #19
0
 internal int Flush(int r)
 {
     for (int index = 0; index < 2; ++index)
     {
         int num = index != 0 ? this.writeAt - this.readAt : (this.readAt <= this.writeAt ? this.writeAt : this.end) - 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((Array)this.window, this.readAt, (Array)this._codec.OutputBuffer, this._codec.NextOut, num);
         this._codec.NextOut += num;
         this.readAt         += num;
         if (this.readAt == this.end && index == 0)
         {
             this.readAt = 0;
             if (this.writeAt == this.end)
             {
                 this.writeAt = 0;
             }
         }
         else
         {
             ++index;
         }
     }
     return(r);
 }
Exemple #20
0
 internal int Flush(int r)
 {
     for (int i = 0; i < 2; i++)
     {
         int num = (i != 0) ? (writeAt - readAt) : (((readAt > writeAt) ? end : writeAt) - readAt);
         if (num == 0)
         {
             if (r == -5)
             {
                 r = 0;
             }
             return(r);
         }
         if (num > _codec.AvailableBytesOut)
         {
             num = _codec.AvailableBytesOut;
         }
         if (num != 0 && r == -5)
         {
             r = 0;
         }
         _codec.AvailableBytesOut -= num;
         _codec.TotalBytesOut     += num;
         if (checkfn != null)
         {
             _codec._Adler32 = (check = Adler.Adler32(check, window, readAt, num));
         }
         Array.Copy(window, readAt, _codec.OutputBuffer, _codec.NextOut, num);
         _codec.NextOut += num;
         readAt         += num;
         if (readAt == end && i == 0)
         {
             readAt = 0;
             if (writeAt == end)
             {
                 writeAt = 0;
             }
         }
         else
         {
             i++;
         }
     }
     return(r);
 }
 internal void Reset(long[] c)
 {
     if (c != null)
     {
         c[0] = this.check;
     }
     if ((this.mode == 4) || (this.mode == 5))
     {
     }
     if (this.mode == 6)
     {
     }
     this.mode = 0;
     this.bitk = 0;
     this.bitb = 0;
     this.read = this.write = 0;
     if (this.checkfn != null)
     {
         this._codec._Adler32 = this.check = Adler.Adler32(0L, null, 0, 0);
     }
 }
        internal int read_buf(byte[] buf, int start, int size)
        {
            int num = AvailableBytesIn;

            if (num > size)
            {
                num = size;
            }
            if (num == 0)
            {
                return(0);
            }
            AvailableBytesIn -= num;
            if (dstate.WantRfc1950HeaderBytes)
            {
                _Adler32 = Adler.Adler32(_Adler32, InputBuffer, NextIn, num);
            }
            Array.Copy(InputBuffer, NextIn, buf, start, num);
            NextIn       += num;
            TotalBytesIn += num;
            return(num);
        }
Exemple #23
0
        internal int read_buf(byte[] buf, int start, int size)
        {
            int availableBytesIn = this.AvailableBytesIn;

            if (availableBytesIn > size)
            {
                availableBytesIn = size;
            }
            if (availableBytesIn == 0)
            {
                return(0);
            }
            this.AvailableBytesIn -= availableBytesIn;
            if (this.dstate.WantRfc1950HeaderBytes)
            {
                this._Adler32 = Adler.Adler32(this._Adler32, this.InputBuffer, this.NextIn, availableBytesIn);
            }
            Array.Copy(this.InputBuffer, this.NextIn, buf, start, availableBytesIn);
            this.NextIn       += availableBytesIn;
            this.TotalBytesIn += availableBytesIn;
            return(availableBytesIn);
        }
        internal int SetDictionary(byte[] dictionary)
        {
            int start  = 0;
            int length = dictionary.Length;

            if (this.mode != 6)
            {
                throw new ZlibException("Stream error.");
            }
            if (Adler.Adler32(1L, dictionary, 0, dictionary.Length) != this._codec._Adler32)
            {
                return(-3);
            }
            this._codec._Adler32 = Adler.Adler32(0L, null, 0, 0);
            if (length >= (((int)1) << this.wbits))
            {
                length = (((int)1) << this.wbits) - 1;
                start  = dictionary.Length - length;
            }
            this.blocks.SetDictionary(dictionary, start, length);
            this.mode = 7;
            return(0);
        }
Exemple #25
0
        // copy as much as possible from the sliding window to the output area
        internal int Flush(int r)
        {
            int nBytes;

            for (int pass = 0; pass < 2; pass++)
            {
                if (pass == 0)
                {
                    // compute number of bytes to copy as far as end of window
                    nBytes = (int)((readAt <= writeAt ? writeAt : end) - readAt);
                }
                else
                {
                    // compute bytes to copy
                    nBytes = writeAt - readAt;
                }

                // workitem 8870
                if (nBytes == 0)
                {
                    if (r == ZlibConstants.Z_BUF_ERROR)
                    {
                        r = ZlibConstants.Z_OK;
                    }
                    return(r);
                }

                if (nBytes > _codec.AvailableBytesOut)
                {
                    nBytes = _codec.AvailableBytesOut;
                }

                if (nBytes != 0 && r == ZlibConstants.Z_BUF_ERROR)
                {
                    r = ZlibConstants.Z_OK;
                }

                // update counters
                _codec.AvailableBytesOut -= nBytes;
                _codec.TotalBytesOut     += nBytes;

                // update check information
                if (checkfn != null)
                {
                    _codec._Adler32 = check = Adler.Adler32(check, window, readAt, nBytes);
                }

                // copy as far as end of window
                Array.Copy(window, readAt, _codec.OutputBuffer, _codec.NextOut, nBytes);
                _codec.NextOut += nBytes;
                readAt         += nBytes;

                // see if more to copy at beginning of window
                if (readAt == end && pass == 0)
                {
                    // wrap pointers
                    readAt = 0;
                    if (writeAt == end)
                    {
                        writeAt = 0;
                    }
                }
                else
                {
                    pass++;
                }
            }

            // done
            return(r);
        }
Exemple #26
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))
            {
                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 = (int)(this.compressionLevel - 1 & (CompressionLevel)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)((uint)((int)this._codec._Adler32 & -16777216) >> 24);
                        this.pending[this.pendingCount++] = (byte)((this._codec._Adler32 & 16711680) >> 16);
                        this.pending[this.pendingCount++] = (byte)((this._codec._Adler32 & 65280) >> 8);
                        this.pending[this.pendingCount++] = (byte)(this._codec._Adler32 & 0xFF);
                    }
                    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 && (int)flush <= 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 != 0 && this.status != DeflateManager.FINISH_STATE))
                {
                    BlockState blockState = this.DeflateFunction(flush);
                    if (blockState == BlockState.FinishStarted || blockState == BlockState.FinishDone)
                    {
                        this.status = DeflateManager.FINISH_STATE;
                    }
                    switch (blockState)
                    {
                    case BlockState.NeedMore:
                    case BlockState.FinishStarted:
                        if (this._codec.AvailableBytesOut == 0)
                        {
                            this.last_flush = -1;
                        }
                        return(0);

                    case 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)
                        {
                            break;
                        }
                        this.last_flush = -1;
                        return(0);
                    }
                }
                if (flush != FlushType.Finish)
                {
                    return(0);
                }
                if (this.WantRfc1950HeaderBytes && !this.Rfc1950BytesEmitted)
                {
                    this.pending[this.pendingCount++] = (byte)((uint)((int)this._codec._Adler32 & -16777216) >> 24);
                    this.pending[this.pendingCount++] = (byte)((this._codec._Adler32 & 16711680) >> 16);
                    this.pending[this.pendingCount++] = (byte)((this._codec._Adler32 & 65280) >> 8);
                    this.pending[this.pendingCount++] = (byte)(this._codec._Adler32 & 0xFF);
                    this._codec.flush_pending();
                    this.Rfc1950BytesEmitted = true;
                    return((this.pendingCount == 0) ? 1 : 0);
                }
                return(1);
            }
            this._codec.Message = DeflateManager._ErrorMessage[4];
            throw new ZlibException(string.Format("Something is fishy. [{0}]", this._codec.Message));
        }
Exemple #27
0
        internal int Deflate(FlushType flush)
        {
            if (((this._codec.OutputBuffer == null) || ((this._codec.InputBuffer == null) && (this._codec.AvailableBytesIn != 0))) || ((this.status == 0x29a) && (flush != FlushType.Finish)))
            {
                this._codec.Message = z_errmsg[4];
                throw new ZlibException(string.Format("Something is fishy. [{0}]", this._codec.Message));
            }
            if (this._codec.AvailableBytesOut == 0)
            {
                this._codec.Message = z_errmsg[7];
                throw new ZlibException("OutputBuffer is full (AvailableBytesOut == 0)");
            }
            int num = this.last_flush;

            this.last_flush = (int)flush;
            if (this.status == 0x2a)
            {
                int b    = (8 + ((this.w_bits - 8) << 4)) << 8;
                int num3 = ((int)((int)(this.compressionLevel - 1) & 0xff)) >> 1;
                if (num3 > 3)
                {
                    num3 = 3;
                }
                b |= num3 << 6;
                if (this.strstart != 0)
                {
                    b |= 0x20;
                }
                b          += 0x1f - (b % 0x1f);
                this.status = 0x71;
                this.putShortMSB(b);
                if (this.strstart != 0)
                {
                    this.putShortMSB((int)SharedUtils.URShift(this._codec._Adler32, 0x10));
                    this.putShortMSB((int)(this._codec._Adler32 & 0xffffL));
                }
                this._codec._Adler32 = Adler.Adler32(0L, 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) && ((int)flush <= num)) && (flush != FlushType.Finish))
            {
                return(0);
            }
            if ((this.status == 0x29a) && (this._codec.AvailableBytesIn != 0))
            {
                this._codec.Message = z_errmsg[7];
                throw new ZlibException("status == FINISH_STATE && _codec.AvailableBytesIn != 0");
            }
            if (((this._codec.AvailableBytesIn != 0) || (this.lookahead != 0)) || ((flush != FlushType.None) && (this.status != 0x29a)))
            {
                BlockState state = this.DeflateFunction(flush);
                switch (state)
                {
                case BlockState.FinishStarted:
                case BlockState.FinishDone:
                    this.status = 0x29a;
                    break;
                }
                if ((state == BlockState.NeedMore) || (state == BlockState.FinishStarted))
                {
                    if (this._codec.AvailableBytesOut == 0)
                    {
                        this.last_flush = -1;
                    }
                    return(0);
                }
                if (state == 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.putShortMSB((int)SharedUtils.URShift(this._codec._Adler32, 0x10));
            this.putShortMSB((int)(this._codec._Adler32 & 0xffffL));
            this._codec.flush_pending();
            this.Rfc1950BytesEmitted = true;
            return((this.pendingCount != 0) ? 0 : 1);
        }
Exemple #28
0
        internal int Deflate(FlushType flush)
        {
            if (_codec.OutputBuffer == null || (_codec.InputBuffer == null && _codec.AvailableBytesIn != 0) || (status == FINISH_STATE && flush != FlushType.Finish))
            {
                _codec.Message = _ErrorMessage[4];
                throw new ZlibException($"Something is fishy. [{_codec.Message}]");
            }
            if (_codec.AvailableBytesOut == 0)
            {
                _codec.Message = _ErrorMessage[7];
                throw new ZlibException("OutputBuffer is full (AvailableBytesOut == 0)");
            }
            int num = last_flush;

            last_flush = (int)flush;
            if (status == INIT_STATE)
            {
                int num2 = Z_DEFLATED + (w_bits - 8 << 4) << 8;
                int num3 = (int)((compressionLevel - 1) & (CompressionLevel)255) >> 1;
                if (num3 > 3)
                {
                    num3 = 3;
                }
                num2 |= num3 << 6;
                if (strstart != 0)
                {
                    num2 |= PRESET_DICT;
                }
                num2  += 31 - num2 % 31;
                status = BUSY_STATE;
                pending[pendingCount++] = (byte)(num2 >> 8);
                pending[pendingCount++] = (byte)num2;
                if (strstart != 0)
                {
                    pending[pendingCount++] = (byte)((uint)((int)_codec._Adler32 & -16777216) >> 24);
                    pending[pendingCount++] = (byte)((_codec._Adler32 & 0xFF0000) >> 16);
                    pending[pendingCount++] = (byte)((_codec._Adler32 & 0xFF00) >> 8);
                    pending[pendingCount++] = (byte)(_codec._Adler32 & 0xFF);
                }
                _codec._Adler32 = Adler.Adler32(0u, null, 0, 0);
            }
            if (pendingCount != 0)
            {
                _codec.flush_pending();
                if (_codec.AvailableBytesOut == 0)
                {
                    last_flush = -1;
                    return(0);
                }
            }
            else if (_codec.AvailableBytesIn == 0 && (int)flush <= num && flush != FlushType.Finish)
            {
                return(0);
            }
            if (status == FINISH_STATE && _codec.AvailableBytesIn != 0)
            {
                _codec.Message = _ErrorMessage[7];
                throw new ZlibException("status == FINISH_STATE && _codec.AvailableBytesIn != 0");
            }
            if (_codec.AvailableBytesIn != 0 || lookahead != 0 || (flush != 0 && status != FINISH_STATE))
            {
                BlockState blockState = DeflateFunction(flush);
                if (blockState == BlockState.FinishStarted || blockState == BlockState.FinishDone)
                {
                    status = FINISH_STATE;
                }
                switch (blockState)
                {
                case BlockState.NeedMore:
                case BlockState.FinishStarted:
                    if (_codec.AvailableBytesOut == 0)
                    {
                        last_flush = -1;
                    }
                    return(0);

                case BlockState.BlockDone:
                    if (flush == FlushType.Partial)
                    {
                        _tr_align();
                    }
                    else
                    {
                        _tr_stored_block(0, 0, eof: false);
                        if (flush == FlushType.Full)
                        {
                            for (int i = 0; i < hash_size; i++)
                            {
                                head[i] = 0;
                            }
                        }
                    }
                    _codec.flush_pending();
                    if (_codec.AvailableBytesOut == 0)
                    {
                        last_flush = -1;
                        return(0);
                    }
                    break;
                }
            }
            if (flush != FlushType.Finish)
            {
                return(0);
            }
            if (!WantRfc1950HeaderBytes || Rfc1950BytesEmitted)
            {
                return(1);
            }
            pending[pendingCount++] = (byte)((uint)((int)_codec._Adler32 & -16777216) >> 24);
            pending[pendingCount++] = (byte)((_codec._Adler32 & 0xFF0000) >> 16);
            pending[pendingCount++] = (byte)((_codec._Adler32 & 0xFF00) >> 8);
            pending[pendingCount++] = (byte)(_codec._Adler32 & 0xFF);
            _codec.flush_pending();
            Rfc1950BytesEmitted = true;
            if (pendingCount == 0)
            {
                return(1);
            }
            return(0);
        }