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);
        }
 internal void Reset()
 {
     _codec.TotalBytesIn = (_codec.TotalBytesOut = 0L);
     _codec.Message      = null;
     pendingCount        = 0;
     nextPending         = 0;
     Rfc1950BytesEmitted = false;
     status          = ((!WantRfc1950HeaderBytes) ? BUSY_STATE : INIT_STATE);
     _codec._Adler32 = Adler.Adler32(0u, null, 0, 0);
     last_flush      = 0;
     _InitializeTreeData();
     _InitializeLazyMatch();
 }
Exemple #3
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 #4
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);
 }
Exemple #5
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);
        }
        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;
            return((pendingCount == 0) ? 1 : 0);
        }