System.IO.Compression.GZipDecoder.ReadGzipHeader C# (CSharp) Méthode

ReadGzipHeader() public méthode

public ReadGzipHeader ( ) : bool
Résultat bool
        public bool ReadGzipHeader() {
            int bits;
            switch (this.gzipHeaderSubstate) {
                case GZIPHeaderState.ReadingID1:
                    bits = this.input.GetBits(8);
                    if (bits >= 0) {
                        if (bits != 0x1f) {
                            throw new InvalidDataException("The magic number in GZip header is not correct. Make sure you are passing in a GZip stream.");
                        }
                        this.gzipHeaderSubstate = GZIPHeaderState.ReadingID2;
                        break;
                    }
                    return false;

                case GZIPHeaderState.ReadingID2:
                    break;

                case GZIPHeaderState.ReadingCM:
                    goto Label_00AF;

                case GZIPHeaderState.ReadingFLG:
                    goto Label_00DD;

                case GZIPHeaderState.ReadingMMTime:
                    goto Label_0105;

                case GZIPHeaderState.ReadingXFL:
                    goto Label_0141;

                case GZIPHeaderState.ReadingOS:
                    goto Label_015B;

                case GZIPHeaderState.ReadingXLen1:
                    goto Label_0175;

                case GZIPHeaderState.ReadingXLen2:
                    goto Label_01A3;

                case GZIPHeaderState.ReadingXLenData:
                    goto Label_01D5;

                case GZIPHeaderState.ReadingFileName:
                    goto Label_0217;

                case GZIPHeaderState.ReadingComment:
                    goto Label_0249;

                case GZIPHeaderState.ReadingCRC16Part1:
                    goto Label_027C;

                case GZIPHeaderState.ReadingCRC16Part2:
                    goto Label_02AB;

                case GZIPHeaderState.Done:
                    goto Label_02C6;

                default:
                    throw new InvalidDataException("Decoder is in some unknown state. This might be caused by corrupted data.");
            }
            bits = this.input.GetBits(8);
            if (bits < 0) {
                return false;
            }
            if (bits != 0x8b) {
                throw new InvalidDataException("The magic number in GZip header is not correct. Make sure you are passing in a GZip stream.");
            }
            this.gzipHeaderSubstate = GZIPHeaderState.ReadingCM;
        Label_00AF:
            bits = this.input.GetBits(8);
            if (bits < 0) {
                return false;
            }
            if (bits != 8) {
                throw new InvalidDataException("The compression mode specified in GZip header is unknown.");
            }
            this.gzipHeaderSubstate = GZIPHeaderState.ReadingFLG;
        Label_00DD:
            bits = this.input.GetBits(8);
            if (bits < 0) {
                return false;
            }
            this.gzip_header_flag = bits;
            this.gzipHeaderSubstate = GZIPHeaderState.ReadingMMTime;
            this.loopCounter = 0;
        Label_0105:
            bits = 0;
            while (this.loopCounter < 4) {
                if (this.input.GetBits(8) < 0) {
                    return false;
                }
                this.loopCounter++;
            }
            this.gzipHeaderSubstate = GZIPHeaderState.ReadingXFL;
            this.loopCounter = 0;
        Label_0141:
            if (this.input.GetBits(8) < 0) {
                return false;
            }
            this.gzipHeaderSubstate = GZIPHeaderState.ReadingOS;
        Label_015B:
            if (this.input.GetBits(8) < 0) {
                return false;
            }
            this.gzipHeaderSubstate = GZIPHeaderState.ReadingXLen1;
        Label_0175:
            if ((this.gzip_header_flag & 4) == 0) {
                goto Label_0217;
            }
            bits = this.input.GetBits(8);
            if (bits < 0) {
                return false;
            }
            this.gzip_header_xlen = bits;
            this.gzipHeaderSubstate = GZIPHeaderState.ReadingXLen2;
        Label_01A3:
            bits = this.input.GetBits(8);
            if (bits < 0) {
                return false;
            }
            this.gzip_header_xlen |= bits << 8;
            this.gzipHeaderSubstate = GZIPHeaderState.ReadingXLenData;
            this.loopCounter = 0;
        Label_01D5:
            bits = 0;
            while (this.loopCounter < this.gzip_header_xlen) {
                if (this.input.GetBits(8) < 0) {
                    return false;
                }
                this.loopCounter++;
            }
            this.gzipHeaderSubstate = GZIPHeaderState.ReadingFileName;
            this.loopCounter = 0;
        Label_0217:
            if ((this.gzip_header_flag & 8) == 0) {
                this.gzipHeaderSubstate = GZIPHeaderState.ReadingComment;
            }
            else {
                do {
                    bits = this.input.GetBits(8);
                    if (bits < 0) {
                        return false;
                    }
                }
                while (bits != 0);
                this.gzipHeaderSubstate = GZIPHeaderState.ReadingComment;
            }
        Label_0249:
            if ((this.gzip_header_flag & 0x10) == 0) {
                this.gzipHeaderSubstate = GZIPHeaderState.ReadingCRC16Part1;
            }
            else {
                do {
                    bits = this.input.GetBits(8);
                    if (bits < 0) {
                        return false;
                    }
                }
                while (bits != 0);
                this.gzipHeaderSubstate = GZIPHeaderState.ReadingCRC16Part1;
            }
        Label_027C:
            if ((this.gzip_header_flag & 2) == 0) {
                this.gzipHeaderSubstate = GZIPHeaderState.Done;
                goto Label_02C6;
            }
            if (this.input.GetBits(8) < 0) {
                return false;
            }
            this.gzipHeaderSubstate = GZIPHeaderState.ReadingCRC16Part2;
        Label_02AB:
            if (this.input.GetBits(8) < 0) {
                return false;
            }
            this.gzipHeaderSubstate = GZIPHeaderState.Done;
        Label_02C6:
            return true;
        }

Usage Example

Exemple #1
0
        //Each block of compressed data begins with 3 header bits
        // containing the following data:
        //    first bit       BFINAL
        //    next 2 bits     BTYPE
        // Note that the header bits do not necessarily begin on a byte
        // boundary, since a block does not necessarily occupy an integral
        // number of bytes.
        // BFINAL is set if and only if this is the last block of the data
        // set.
        // BTYPE specifies how the data are compressed, as follows:
        //    00 - no compression
        //    01 - compressed with fixed Huffman codes
        //    10 - compressed with dynamic Huffman codes
        //    11 - reserved (error)
        // The only difference between the two compressed cases is how the
        // Huffman codes for the literal/length and distance alphabets are
        // defined.
        //
        // This function returns true for success (end of block or output window is full,)
        // false if we are short of input
        //
        private bool Decode()
        {
            bool eob    = false;
            bool result = false;

            if (Finished())
            {
                return(true);
            }

            if (using_gzip)
            {
                if (state == InflaterState.ReadingGZIPHeader)
                {
                    if (!gZipDecoder.ReadGzipHeader())
                    {
                        return(false);
                    }
                    state = InflaterState.ReadingBFinal;
                }
                else if (state == InflaterState.StartReadingGZIPFooter || state == InflaterState.ReadingGZIPFooter)
                {
                    if (!gZipDecoder.ReadGzipFooter())
                    {
                        return(false);
                    }

                    state = InflaterState.VerifyingGZIPFooter;
                    return(true);
                }
            }

            if (state == InflaterState.ReadingBFinal)     // reading bfinal bit
            // Need 1 bit
            {
                if (!input.EnsureBitsAvailable(1))
                {
                    return(false);
                }

                bfinal = input.GetBits(1);
                state  = InflaterState.ReadingBType;
            }

            if (state == InflaterState.ReadingBType)
            {
                // Need 2 bits
                if (!input.EnsureBitsAvailable(2))
                {
                    state = InflaterState.ReadingBType;
                    return(false);
                }

                blockType = (BlockType)input.GetBits(2);
                if (blockType == BlockType.Dynamic)
                {
                    Debug.WriteLineIf(CompressionTracingSwitch.Informational, "Decoding Dynamic Block", "Compression");
                    state = InflaterState.ReadingNumLitCodes;
                }
                else if (blockType == BlockType.Static)
                {
                    Debug.WriteLineIf(CompressionTracingSwitch.Informational, "Decoding Static Block", "Compression");
                    literalLengthTree = HuffmanTree.StaticLiteralLengthTree;
                    distanceTree      = HuffmanTree.StaticDistanceTree;
                    state             = InflaterState.DecodeTop;
                }
                else if (blockType == BlockType.Uncompressed)
                {
                    Debug.WriteLineIf(CompressionTracingSwitch.Informational, "Decoding UnCompressed Block", "Compression");
                    state = InflaterState.UncompressedAligning;
                }
                else
                {
                    throw new InvalidDataException(SR.GetString(SR.UnknownBlockType));
                }
            }

            if (blockType == BlockType.Dynamic)
            {
                if (state < InflaterState.DecodeTop)     // we are reading the header
                {
                    result = DecodeDynamicBlockHeader();
                }
                else
                {
                    result = DecodeBlock(out eob);  // this can returns true when output is full
                }
            }
            else if (blockType == BlockType.Static)
            {
                result = DecodeBlock(out eob);
            }
            else if (blockType == BlockType.Uncompressed)
            {
                result = DecodeUncompressedBlock(out eob);
            }
            else
            {
                throw new InvalidDataException(SR.GetString(SR.UnknownBlockType));
            }

            //
            // If we reached the end of the block and the block we were decoding had
            // bfinal=1 (final block)
            //
            if (eob && (bfinal != 0))
            {
                if (using_gzip)
                {
                    state = InflaterState.StartReadingGZIPFooter;
                }
                else
                {
                    state = InflaterState.Done;
                }
            }
            return(result);
        }