// 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;
        }
        IFileFormatReader formatReader;  // class to decode header and footer (e.g. gzip)

        public Inflater() {           
            output = new OutputWindow();
            input  = new InputBuffer();

            codeList = new byte[HuffmanTree.MaxLiteralTreeElements + HuffmanTree.MaxDistTreeElements];
            codeLengthTreeCodeLength = new byte[HuffmanTree.NumberOfCodeLengthTreeElements];
            Reset();
        }
        public bool ReadFooter(InputBuffer input) {
            input.SkipToByteBoundary();
            if (gzipFooterSubstate == GzipHeaderState.ReadingCRC) {
                while (loopCounter < 4) { 
                    int bits = input.GetBits(8);
                    if (bits < 0) {
                        return false;
                    }

                    expectedCrc32 |= ((uint)bits << (8 * loopCounter));
                    loopCounter++;
                }
                gzipFooterSubstate = GzipHeaderState.ReadingFileSize;
                loopCounter = 0;

            }

            if (gzipFooterSubstate == GzipHeaderState.ReadingFileSize) {
                if (loopCounter == 0)
                    expectedOutputStreamSizeModulo = 0;

                while (loopCounter < 4) {
                    int bits = input.GetBits(8);
                    if (bits < 0) {
                        return false;
                    }

                    expectedOutputStreamSizeModulo |= ((uint) bits << (8 * loopCounter));
                    loopCounter++;
                }
            }

            return true;
        }
        public bool ReadHeader(InputBuffer input) {

            while (true) {
                int bits;
                switch (gzipHeaderSubstate) {
                    case GzipHeaderState.ReadingID1:
                        bits = input.GetBits(8);
                        if (bits < 0) {
                            return false;
                        }

                        if (bits != GZipConstants.ID1) {
                            throw new InvalidDataException(SR.GetString(SR.CorruptedGZipHeader));
                        }
                        gzipHeaderSubstate = GzipHeaderState.ReadingID2;
                        goto case GzipHeaderState.ReadingID2;

                    case GzipHeaderState.ReadingID2:
                        bits = input.GetBits(8);
                        if (bits < 0) {
                            return false;
                        }

                        if (bits != GZipConstants.ID2) {
                            throw new InvalidDataException(SR.GetString(SR.CorruptedGZipHeader));
                        }

                        gzipHeaderSubstate = GzipHeaderState.ReadingCM;
                        goto case GzipHeaderState.ReadingCM;

                    case GzipHeaderState.ReadingCM:
                        bits = input.GetBits(8);
                        if (bits < 0) {
                            return false;
                        }

                        if (bits != GZipConstants.Deflate) {         // compression mode must be 8 (deflate)
                            throw new InvalidDataException(SR.GetString(SR.UnknownCompressionMode));
                        }

                        gzipHeaderSubstate = GzipHeaderState.ReadingFLG; ;
                        goto case GzipHeaderState.ReadingFLG;

                    case GzipHeaderState.ReadingFLG:
                        bits = input.GetBits(8);
                        if (bits < 0) {
                            return false;
                        }

                        gzip_header_flag = bits;
                        gzipHeaderSubstate = GzipHeaderState.ReadingMMTime;
                        loopCounter = 0; // 4 MMTIME bytes
                        goto case GzipHeaderState.ReadingMMTime;

                    case GzipHeaderState.ReadingMMTime:
                        bits = 0;
                        while (loopCounter < 4) {
                            bits = input.GetBits(8);
                            if (bits < 0) {
                                return false;
                            }

                            loopCounter++;
                        }

                        gzipHeaderSubstate = GzipHeaderState.ReadingXFL;
                        loopCounter = 0;
                        goto case GzipHeaderState.ReadingXFL;

                    case GzipHeaderState.ReadingXFL:      // ignore XFL
                        bits = input.GetBits(8);
                        if (bits < 0) {
                            return false;
                        }

                        gzipHeaderSubstate = GzipHeaderState.ReadingOS;
                        goto case GzipHeaderState.ReadingOS;

                    case GzipHeaderState.ReadingOS:      // ignore OS
                        bits = input.GetBits(8);
                        if (bits < 0) {
                            return false;
                        }

                        gzipHeaderSubstate = GzipHeaderState.ReadingXLen1;
                        goto case GzipHeaderState.ReadingXLen1;

                    case GzipHeaderState.ReadingXLen1:
                        if ((gzip_header_flag & (int)GZipOptionalHeaderFlags.ExtraFieldsFlag) == 0) {
                            goto case GzipHeaderState.ReadingFileName;
                        }

                        bits = input.GetBits(8);
                        if (bits < 0) {
                            return false;
                        }

                        gzip_header_xlen = bits;
                        gzipHeaderSubstate = GzipHeaderState.ReadingXLen2;
                        goto case GzipHeaderState.ReadingXLen2;

                    case GzipHeaderState.ReadingXLen2:
                        bits = input.GetBits(8);
                        if (bits < 0) {
                            return false;
                        }

                        gzip_header_xlen |= (bits << 8);
                        gzipHeaderSubstate = GzipHeaderState.ReadingXLenData;
                        loopCounter = 0; // 0 bytes of XLEN data read so far
                        goto case GzipHeaderState.ReadingXLenData;

                    case GzipHeaderState.ReadingXLenData:
                        bits = 0;
                        while (loopCounter < gzip_header_xlen) {
                            bits = input.GetBits(8);
                            if (bits < 0) {
                                return false;
                            }

                            loopCounter++;
                        }
                        gzipHeaderSubstate = GzipHeaderState.ReadingFileName;
                        loopCounter = 0;
                        goto case GzipHeaderState.ReadingFileName;

                    case GzipHeaderState.ReadingFileName:
                        if ((gzip_header_flag & (int)GZipOptionalHeaderFlags.FileNameFlag) == 0) {
                            gzipHeaderSubstate = GzipHeaderState.ReadingComment;
                            goto case GzipHeaderState.ReadingComment;
                        }

                        do {
                            bits = input.GetBits(8);
                            if (bits < 0) {
                                return false;
                            }

                            if (bits == 0) {   // see '\0' in the file name string
                                break;
                            }
                        } while (true);

                        gzipHeaderSubstate = GzipHeaderState.ReadingComment;
                        goto case GzipHeaderState.ReadingComment;

                    case GzipHeaderState.ReadingComment:
                        if ((gzip_header_flag & (int)GZipOptionalHeaderFlags.CommentFlag) == 0) {
                            gzipHeaderSubstate = GzipHeaderState.ReadingCRC16Part1;
                            goto case GzipHeaderState.ReadingCRC16Part1;
                        }

                        do {
                            bits = input.GetBits(8);
                            if (bits < 0) {
                                return false;
                            }

                            if (bits == 0) {   // see '\0' in the file name string
                                break;
                            }
                        } while (true);

                        gzipHeaderSubstate = GzipHeaderState.ReadingCRC16Part1;
                        goto case GzipHeaderState.ReadingCRC16Part1;

                    case GzipHeaderState.ReadingCRC16Part1:
                        if ((gzip_header_flag & (int)GZipOptionalHeaderFlags.CRCFlag) == 0) {
                            gzipHeaderSubstate = GzipHeaderState.Done;
                            goto case GzipHeaderState.Done;
                        }

                        bits = input.GetBits(8);     // ignore crc
                        if (bits < 0) {
                            return false;
                        }

                        gzipHeaderSubstate = GzipHeaderState.ReadingCRC16Part2;
                        goto case GzipHeaderState.ReadingCRC16Part2;

                    case GzipHeaderState.ReadingCRC16Part2:
                        bits = input.GetBits(8);     // ignore crc
                        if (bits < 0) {
                            return false;
                        }

                        gzipHeaderSubstate = GzipHeaderState.Done;
                        goto case GzipHeaderState.Done;

                    case GzipHeaderState.Done:
                        return true;
                    default:
                        Debug.Assert(false, "We should not reach unknown state!");
                        throw new InvalidDataException(SR.GetString(SR.UnknownState));
                }
            }
        }
        //
        // 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); 
            }

            int codeLength = codeLengthArray[symbol];
            
            // huffman code lengths must be at least 1 bit long
            if (codeLength <= 0)
            {
                throw new InvalidDataException(SR.GetString(SR.InvalidHuffmanData));
            }

            //
            // 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 (codeLength > 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(codeLength);
            return symbol;
        }