Exemple #1
0
        // Copy up to length of bytes from input directly. 
        // This is used for uncompressed block.
        public int CopyFrom(InputBuffer input, int length)
        {
            length = Math.Min(Math.Min(length, WindowSize - _bytesUsed), input.AvailableBytes);
            int copied;

            // We might need wrap around to copy all bytes. 
            int tailLen = WindowSize - _end;
            if (length > tailLen)
            {
                // copy the first part 
                copied = input.CopyTo(_window, _end, tailLen);
                if (copied == tailLen)
                {
                    // only try to copy the second part if we have enough bytes in input
                    copied += input.CopyTo(_window, 0, length - tailLen);
                }
            }
            else
            {
                // only one copy is needed if there is no wrap around.
                copied = input.CopyTo(_window, _end, length);
            }

            _end = (_end + copied) & WindowMask;
            _bytesUsed += copied;
            return copied;
        }
 public GZipDecoder(InputBuffer input)
 {
     this.input = input;
     Reset();
 }
Exemple #3
0
 public GZipDecoder(InputBuffer input)
 {
     this.input = input;
     Reset();
 }
        // 
        // This function will try to get enough bits from input and
        // try to decode the bits. 
        // If there are no enought bits in the input, this function will return -1. 
        //
        public int GetNextSymbol(InputBuffer input)
        {
            // Try to load 16 bits into input buffer if possible and get the bitBuffer value.
            // If there aren't 16 bits available we will return all we have in the
            // input buffer.
            uint bitBuffer = input.TryLoad16Bits();
            if (input.AvailableBits == 0)
            {
                // running out of input.
                return -1;
            }

            // decode an element 
            int symbol = _table[bitBuffer & _tableMask];
            if (symbol < 0)
            {
                //  this will be the start of the binary tree
                // navigate the tree
                uint mask = (uint) 1 << _tableBits;
                do
                {
                    symbol = -symbol;
                    if ((bitBuffer & mask) == 0)
                        symbol = _left[symbol];
                    else
                        symbol = _right[symbol];
                    mask <<= 1;
                } while (symbol < 0);
            }

            // 
            // If this code is longer than the # bits we had in the bit buffer (i.e.
            // we read only part of the code), we can hit the entry in the table or the tree 
            // for another symbol. However the length of another symbol will not match the
            // available bits count.
            if (_codeLengthArray[symbol] > input.AvailableBits)
            {
                // We already tried to load 16 bits and maximum length is 15, 
                // so this means we are running out of input.
                return -1;
            }

            input.SkipBits(_codeLengthArray[symbol]);
            return symbol;
        }