예제 #1
0
파일: GZipDecoder.cs 프로젝트: supermax/TMS
        public bool ReadFooter(InputBuffer input)
        {
            input.SkipToByteBoundary();
            if (gzipFooterSubstate == GzipHeaderState.ReadingCRC)
            {
                while (loopCounter < 4)
                {
                    var 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)
                {
                    var bits = input.GetBits(8);
                    if (bits < 0)
                    {
                        return(false);
                    }

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

            return(true);
        }
예제 #2
0
 public bool ReadFooter(InputBuffer input)
 {
     input.SkipToByteBoundary();
     if (this.gzipFooterSubstate == GZipDecoder.GzipHeaderState.ReadingCRC)
     {
         while (this.loopCounter < 4)
         {
             int bits = input.GetBits(8);
             if (bits < 0)
             {
                 return(false);
             }
             this.expectedCrc32 |= (uint)((uint)bits << 8 * this.loopCounter);
             this.loopCounter++;
         }
         this.gzipFooterSubstate = GZipDecoder.GzipHeaderState.ReadingFileSize;
         this.loopCounter        = 0;
     }
     if (this.gzipFooterSubstate == GZipDecoder.GzipHeaderState.ReadingFileSize)
     {
         if (this.loopCounter == 0)
         {
             this.expectedOutputStreamSizeModulo = 0u;
         }
         while (this.loopCounter < 4)
         {
             int bits2 = input.GetBits(8);
             if (bits2 < 0)
             {
                 return(false);
             }
             this.expectedOutputStreamSizeModulo |= (uint)((uint)bits2 << 8 * this.loopCounter);
             this.loopCounter++;
         }
     }
     return(true);
 }
예제 #3
0
        public bool ReadHeader(InputBuffer input)
        {
            int bits;

            switch (this.gzipHeaderSubstate)
            {
            case GZipDecoder.GzipHeaderState.ReadingID1:
                bits = input.GetBits(8);
                if (bits < 0)
                {
                    return(false);
                }
                if (bits != 31)
                {
                    throw new InvalidDataException(SR.GetString("Corrupted gzip header"));
                }
                this.gzipHeaderSubstate = GZipDecoder.GzipHeaderState.ReadingID2;
                break;

            case GZipDecoder.GzipHeaderState.ReadingID2:
                break;

            case GZipDecoder.GzipHeaderState.ReadingCM:
                goto IL_A5;

            case GZipDecoder.GzipHeaderState.ReadingFLG:
                goto IL_CE;

            case GZipDecoder.GzipHeaderState.ReadingMMTime:
                goto IL_F1;

            case GZipDecoder.GzipHeaderState.ReadingXFL:
                goto IL_128;

            case GZipDecoder.GzipHeaderState.ReadingOS:
                goto IL_13D;

            case GZipDecoder.GzipHeaderState.ReadingXLen1:
                goto IL_152;

            case GZipDecoder.GzipHeaderState.ReadingXLen2:
                goto IL_17B;

            case GZipDecoder.GzipHeaderState.ReadingXLenData:
                goto IL_1A8;

            case GZipDecoder.GzipHeaderState.ReadingFileName:
                goto IL_1E5;

            case GZipDecoder.GzipHeaderState.ReadingComment:
                goto IL_212;

            case GZipDecoder.GzipHeaderState.ReadingCRC16Part1:
                goto IL_240;

            case GZipDecoder.GzipHeaderState.ReadingCRC16Part2:
                goto IL_26A;

            case GZipDecoder.GzipHeaderState.Done:
                return(true);

            default:
                throw new InvalidDataException(SR.GetString("Unknown state"));
            }
            bits = input.GetBits(8);
            if (bits < 0)
            {
                return(false);
            }
            if (bits != 139)
            {
                throw new InvalidDataException(SR.GetString("Corrupted gzip header"));
            }
            this.gzipHeaderSubstate = GZipDecoder.GzipHeaderState.ReadingCM;
IL_A5:
            bits = input.GetBits(8);
            if (bits < 0)
            {
                return(false);
            }
            if (bits != 8)
            {
                throw new InvalidDataException(SR.GetString("Unknown compression mode"));
            }
            this.gzipHeaderSubstate = GZipDecoder.GzipHeaderState.ReadingFLG;
IL_CE:
            bits = input.GetBits(8);
            if (bits < 0)
            {
                return(false);
            }
            this.gzip_header_flag   = bits;
            this.gzipHeaderSubstate = GZipDecoder.GzipHeaderState.ReadingMMTime;
            this.loopCounter        = 0;
IL_F1:
            while (this.loopCounter < 4)
            {
                bits = input.GetBits(8);
                if (bits < 0)
                {
                    return(false);
                }
                this.loopCounter++;
            }
            this.gzipHeaderSubstate = GZipDecoder.GzipHeaderState.ReadingXFL;
            this.loopCounter        = 0;
IL_128:
            bits = input.GetBits(8);
            if (bits < 0)
            {
                return(false);
            }
            this.gzipHeaderSubstate = GZipDecoder.GzipHeaderState.ReadingOS;
IL_13D:
            bits = input.GetBits(8);
            if (bits < 0)
            {
                return(false);
            }
            this.gzipHeaderSubstate = GZipDecoder.GzipHeaderState.ReadingXLen1;
IL_152:
            if ((this.gzip_header_flag & 4) == 0)
            {
                goto IL_1E5;
            }
            bits = input.GetBits(8);
            if (bits < 0)
            {
                return(false);
            }
            this.gzip_header_xlen   = bits;
            this.gzipHeaderSubstate = GZipDecoder.GzipHeaderState.ReadingXLen2;
IL_17B:
            bits = input.GetBits(8);
            if (bits < 0)
            {
                return(false);
            }
            this.gzip_header_xlen  |= bits << 8;
            this.gzipHeaderSubstate = GZipDecoder.GzipHeaderState.ReadingXLenData;
            this.loopCounter        = 0;
IL_1A8:
            while (this.loopCounter < this.gzip_header_xlen)
            {
                bits = input.GetBits(8);
                if (bits < 0)
                {
                    return(false);
                }
                this.loopCounter++;
            }
            this.gzipHeaderSubstate = GZipDecoder.GzipHeaderState.ReadingFileName;
            this.loopCounter        = 0;
IL_1E5:
            if ((this.gzip_header_flag & 8) == 0)
            {
                this.gzipHeaderSubstate = GZipDecoder.GzipHeaderState.ReadingComment;
            }
            else
            {
                while (true)
                {
                    bits = input.GetBits(8);
                    if (bits < 0)
                    {
                        break;
                    }
                    if (bits == 0)
                    {
                        goto Block_20;
                    }
                }
                return(false);

Block_20:
                this.gzipHeaderSubstate = GZipDecoder.GzipHeaderState.ReadingComment;
            }
IL_212:
            if ((this.gzip_header_flag & 16) == 0)
            {
                this.gzipHeaderSubstate = GZipDecoder.GzipHeaderState.ReadingCRC16Part1;
            }
            else
            {
                while (true)
                {
                    bits = input.GetBits(8);
                    if (bits < 0)
                    {
                        break;
                    }
                    if (bits == 0)
                    {
                        goto Block_23;
                    }
                }
                return(false);

Block_23:
                this.gzipHeaderSubstate = GZipDecoder.GzipHeaderState.ReadingCRC16Part1;
            }
IL_240:
            if ((this.gzip_header_flag & 2) == 0)
            {
                this.gzipHeaderSubstate = GZipDecoder.GzipHeaderState.Done;
                return(true);
            }
            bits = input.GetBits(8);
            if (bits < 0)
            {
                return(false);
            }
            this.gzipHeaderSubstate = GZipDecoder.GzipHeaderState.ReadingCRC16Part2;
IL_26A:
            bits = input.GetBits(8);
            if (bits < 0)
            {
                return(false);
            }
            this.gzipHeaderSubstate = GZipDecoder.GzipHeaderState.Done;
            return(true);
        }
예제 #4
0
파일: GZipDecoder.cs 프로젝트: supermax/TMS
        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)
                    {
                        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)
                        {
                            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)
                        {
                            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));
                }
            }
        }
예제 #5
0
        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));
                }
            }
        }
예제 #6
0
        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;
        }
예제 #7
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 (hasFormatReader)
            {
                if (state == InflaterState.ReadingHeader)
                {
                    if (!formatReader.ReadHeader(input))
                    {
                        return(false);
                    }
                    state = InflaterState.ReadingBFinal;
                }
                else if (state == InflaterState.StartReadingFooter || state == InflaterState.ReadingFooter)
                {
                    if (!formatReader.ReadFooter(input))
                    {
                        return(false);
                    }

                    state = InflaterState.VerifyingFooter;
                    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)
                {
                    state = InflaterState.ReadingNumLitCodes;
                }
                else if (blockType == BlockType.Static)
                {
                    literalLengthTree = HuffmanTree.StaticLiteralLengthTree;
                    distanceTree      = HuffmanTree.StaticDistanceTree;
                    state             = InflaterState.DecodeTop;
                }
                else if (blockType == BlockType.Uncompressed)
                {
                    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 (hasFormatReader)
                {
                    state = InflaterState.StartReadingFooter;
                }
                else
                {
                    state = InflaterState.Done;
                }
            }
            return(result);
        }
예제 #8
0
        public Boolean ReadHeader(InputBuffer input)
        {
            Int32 bits;

            switch (this.gzipHeaderSubstate)
            {
            case GZipDecoder.GzipHeaderState.ReadingID1:
                bits = input.GetBits(8);
                if (bits < 0)
                {
                    return(false);
                }
                if (bits != 31)
                {
                    throw new InvalidDataException(SR.GetString("Corrupted gzip header"));
                }
                this.gzipHeaderSubstate = GZipDecoder.GzipHeaderState.ReadingID2;
                break;

            case GZipDecoder.GzipHeaderState.ReadingID2:
                break;

            case GZipDecoder.GzipHeaderState.ReadingCM:
                goto IL_BB;

            case GZipDecoder.GzipHeaderState.ReadingFLG:
                goto IL_EF;

            case GZipDecoder.GzipHeaderState.ReadingMMTime:
                goto IL_11A;

            case GZipDecoder.GzipHeaderState.ReadingXFL:
                goto IL_15F;

            case GZipDecoder.GzipHeaderState.ReadingOS:
                goto IL_17C;

            case GZipDecoder.GzipHeaderState.ReadingXLen1:
                goto IL_199;

            case GZipDecoder.GzipHeaderState.ReadingXLen2:
                goto IL_1CF;

            case GZipDecoder.GzipHeaderState.ReadingXLenData:
                goto IL_204;

            case GZipDecoder.GzipHeaderState.ReadingFileName:
                goto IL_24F;

            case GZipDecoder.GzipHeaderState.ReadingComment:
                goto IL_297;

            case GZipDecoder.GzipHeaderState.ReadingCRC16Part1:
                goto IL_2E0;

            case GZipDecoder.GzipHeaderState.ReadingCRC16Part2:
                goto IL_318;

            case GZipDecoder.GzipHeaderState.Done:
                return(true);

            default:
                throw new InvalidDataException(SR.GetString("Unknown state"));
            }
            bits = input.GetBits(8);
            if (bits < 0)
            {
                return(false);
            }
            if (bits != 139)
            {
                throw new InvalidDataException(SR.GetString("Corrupted gzip header"));
            }
            this.gzipHeaderSubstate = GZipDecoder.GzipHeaderState.ReadingCM;
IL_BB:
            bits = input.GetBits(8);
            if (bits < 0)
            {
                return(false);
            }
            if (bits != 8)
            {
                throw new InvalidDataException(SR.GetString("Unknown compression mode"));
            }
            this.gzipHeaderSubstate = GZipDecoder.GzipHeaderState.ReadingFLG;
IL_EF:
            bits = input.GetBits(8);
            if (bits < 0)
            {
                return(false);
            }
            this.gzip_header_flag   = bits;
            this.gzipHeaderSubstate = GZipDecoder.GzipHeaderState.ReadingMMTime;
            this.loopCounter        = 0;
IL_11A:
            while (this.loopCounter < 4)
            {
                bits = input.GetBits(8);
                if (bits < 0)
                {
                    return(false);
                }
                this.loopCounter++;
            }
            this.gzipHeaderSubstate = GZipDecoder.GzipHeaderState.ReadingXFL;
            this.loopCounter        = 0;
IL_15F:
            bits = input.GetBits(8);
            if (bits < 0)
            {
                return(false);
            }
            this.gzipHeaderSubstate = GZipDecoder.GzipHeaderState.ReadingOS;
IL_17C:
            bits = input.GetBits(8);
            if (bits < 0)
            {
                return(false);
            }
            this.gzipHeaderSubstate = GZipDecoder.GzipHeaderState.ReadingXLen1;
IL_199:
            if ((this.gzip_header_flag & 4) == 0)
            {
                goto IL_24F;
            }
            bits = input.GetBits(8);
            if (bits < 0)
            {
                return(false);
            }
            this.gzip_header_xlen   = bits;
            this.gzipHeaderSubstate = GZipDecoder.GzipHeaderState.ReadingXLen2;
IL_1CF:
            bits = input.GetBits(8);
            if (bits < 0)
            {
                return(false);
            }
            this.gzip_header_xlen  |= bits << 8;
            this.gzipHeaderSubstate = GZipDecoder.GzipHeaderState.ReadingXLenData;
            this.loopCounter        = 0;
IL_204:
            while (this.loopCounter < this.gzip_header_xlen)
            {
                bits = input.GetBits(8);
                if (bits < 0)
                {
                    return(false);
                }
                this.loopCounter++;
            }
            this.gzipHeaderSubstate = GZipDecoder.GzipHeaderState.ReadingFileName;
            this.loopCounter        = 0;
IL_24F:
            if ((this.gzip_header_flag & 8) == 0)
            {
                this.gzipHeaderSubstate = GZipDecoder.GzipHeaderState.ReadingComment;
            }
            else
            {
                for (;;)
                {
                    bits = input.GetBits(8);
                    if (bits < 0)
                    {
                        break;
                    }
                    if (bits == 0)
                    {
                        goto Block_20;
                    }
                }
                return(false);

Block_20:
                this.gzipHeaderSubstate = GZipDecoder.GzipHeaderState.ReadingComment;
            }
IL_297:
            if ((this.gzip_header_flag & 16) == 0)
            {
                this.gzipHeaderSubstate = GZipDecoder.GzipHeaderState.ReadingCRC16Part1;
            }
            else
            {
                for (;;)
                {
                    bits = input.GetBits(8);
                    if (bits < 0)
                    {
                        break;
                    }
                    if (bits == 0)
                    {
                        goto Block_23;
                    }
                }
                return(false);

Block_23:
                this.gzipHeaderSubstate = GZipDecoder.GzipHeaderState.ReadingCRC16Part1;
            }
IL_2E0:
            if ((this.gzip_header_flag & 2) == 0)
            {
                this.gzipHeaderSubstate = GZipDecoder.GzipHeaderState.Done;
                return(true);
            }
            bits = input.GetBits(8);
            if (bits < 0)
            {
                return(false);
            }
            this.gzipHeaderSubstate = GZipDecoder.GzipHeaderState.ReadingCRC16Part2;
IL_318:
            bits = input.GetBits(8);
            if (bits < 0)
            {
                return(false);
            }
            this.gzipHeaderSubstate = GZipDecoder.GzipHeaderState.Done;
            return(true);
        }