Huffman tree used for inflation
Ejemplo n.º 1
0
		static InflaterHuffmanTree()
		{
			try {
				byte[] codeLengths = new byte[288];
				int i = 0;
				while (i < 144) {
					codeLengths[i++] = 8;
				}
				while (i < 256) {
					codeLengths[i++] = 9;
				}
				while (i < 280) {
					codeLengths[i++] = 7;
				}
				while (i < 288) {
					codeLengths[i++] = 8;
				}
				defLitLenTree = new InflaterHuffmanTree(codeLengths);
				
				codeLengths = new byte[32];
				i = 0;
				while (i < 32) {
					codeLengths[i++] = 5;
				}
				defDistTree = new InflaterHuffmanTree(codeLengths);
			} catch (Exception) {
				//throw new SharpZipBaseException("InflaterHuffmanTree: static tree length illegal");
			}
		}
Ejemplo n.º 2
0
 static InflaterHuffmanTree()
 {
     try
     {
         byte[] codeLengths = new byte[0x120];
         int num = 0;
         while (num < 0x90)
         {
             codeLengths[num++] = 8;
         }
         while (num < 0x100)
         {
             codeLengths[num++] = 9;
         }
         while (num < 280)
         {
             codeLengths[num++] = 7;
         }
         while (num < 0x120)
         {
             codeLengths[num++] = 8;
         }
         defLitLenTree = new InflaterHuffmanTree(codeLengths);
         codeLengths = new byte[0x20];
         num = 0;
         while (num < 0x20)
         {
             codeLengths[num++] = 5;
         }
         defDistTree = new InflaterHuffmanTree(codeLengths);
     }
     catch (Exception)
     {
         throw new SharpZipBaseException("InflaterHuffmanTree: static tree length illegal");
     }
 }
Ejemplo n.º 3
0
        static InflaterHuffmanTree()
        {
            var codeLengths = new byte[288];
            int i = 0;
            while (i < 144)
            {
                codeLengths[i++] = 8;
            }
            while (i < 256)
            {
                codeLengths[i++] = 9;
            }
            while (i < 280)
            {
                codeLengths[i++] = 7;
            }
            while (i < 288)
            {
                codeLengths[i++] = 8;
            }
            defLitLenTree = new InflaterHuffmanTree(codeLengths);

            codeLengths = new byte[32];
            i = 0;
            while (i < 32)
            {
                codeLengths[i++] = 5;
            }
            defDistTree = new InflaterHuffmanTree(codeLengths);
        }
Ejemplo n.º 4
0
		public bool Decode(StreamManipulator input)
		{
			decode_loop:
				for (;;) {
					switch (mode) {
						case LNUM:
							lnum = input.PeekBits(5);
							if (lnum < 0) {
								return false;
							}
							lnum += 257;
							input.DropBits(5);
							//  	    System.err.println("LNUM: "+lnum);
							mode = DNUM;
							goto case DNUM; // fall through
						case DNUM:
							dnum = input.PeekBits(5);
							if (dnum < 0) {
								return false;
							}
							dnum++;
							input.DropBits(5);
							//  	    System.err.println("DNUM: "+dnum);
							num = lnum+dnum;
							litdistLens = new byte[num];
							mode = BLNUM;
							goto case BLNUM; // fall through
						case BLNUM:
							blnum = input.PeekBits(4);
							if (blnum < 0) {
								return false;
							}
							blnum += 4;
							input.DropBits(4);
							blLens = new byte[19];
							ptr = 0;
							//  	    System.err.println("BLNUM: "+blnum);
							mode = BLLENS;
							goto case BLLENS; // fall through
						case BLLENS:
							while (ptr < blnum) {
								int len = input.PeekBits(3);
								if (len < 0) {
									return false;
								}
								input.DropBits(3);
								//  		System.err.println("blLens["+BL_ORDER[ptr]+"]: "+len);
								blLens[BL_ORDER[ptr]] = (byte) len;
								ptr++;
							}
							blTree = new InflaterHuffmanTree(blLens);
							blLens = null;
							ptr = 0;
							mode = LENS;
							goto case LENS; // fall through
						case LENS: 
						{
							int symbol;
							while (((symbol = blTree.GetSymbol(input)) & ~15) == 0) {
								/* Normal case: symbol in [0..15] */
							
								//  		  System.err.println("litdistLens["+ptr+"]: "+symbol);
								litdistLens[ptr++] = lastLen = (byte)symbol;
							
								if (ptr == num) {
									/* Finished */
									return true;
								}
							}
						
							/* need more input ? */
							if (symbol < 0) {
								return false;
							}
						
							/* otherwise repeat code */
							if (symbol >= 17) {
								/* repeat zero */
								//  		  System.err.println("repeating zero");
								lastLen = 0;
							} else {
								if (ptr == 0) {
									throw new SharpZipBaseException();
								}
							}
							repSymbol = symbol-16;
						}
							mode = REPS;
							goto case REPS; // fall through
						case REPS:
						{
							int bits = repBits[repSymbol];
							int count = input.PeekBits(bits);
							if (count < 0) {
								return false;
							}
							input.DropBits(bits);
							count += repMin[repSymbol];
							//  	      System.err.println("litdistLens repeated: "+count);
							
							if (ptr + count > num) {
								throw new SharpZipBaseException();
							}
							while (count-- > 0) {
								litdistLens[ptr++] = lastLen;
							}
							
							if (ptr == num) {
								/* Finished */
								return true;
							}
						}
							mode = LENS;
							goto decode_loop;
					}
				}
		}
Ejemplo n.º 5
0
        /// <summary>
        /// Decodes the deflated stream.
        /// </summary>
        /// <returns>
        /// false if more input is needed, or if finished.
        /// </returns>
        /// <exception cref="SharpZipBaseException">
        /// if deflated stream is invalid.
        /// </exception>
        private bool Decode()
        {
            switch (mode) {
                case DECODE_HEADER:
                    return DecodeHeader();

                case DECODE_DICT:
                    return DecodeDict();

                case DECODE_CHKSUM:
                    return DecodeChksum();
                
                case DECODE_BLOCKS:
                    if (isLastBlock) {
                        if (noHeader) {
                            mode = FINISHED;
                            return false;
                        } else {
                            input.SkipToByteBoundary();
                            neededBits = 32;
                            mode = DECODE_CHKSUM;
                            return true;
                        }
                    }
                    
                    int type = input.PeekBits(3);
                    if (type < 0) {
                        return false;
                    }
                    input.DropBits(3);
                    
                    if ((type & 1) != 0) {
                        isLastBlock = true;
                    }
                    switch (type >> 1){
                        case DeflaterConstants.STORED_BLOCK:
                            input.SkipToByteBoundary();
                            mode = DECODE_STORED_LEN1;
                            break;
                        case DeflaterConstants.STATIC_TREES:
                            litlenTree = InflaterHuffmanTree.defLitLenTree;
                            distTree = InflaterHuffmanTree.defDistTree;
                            mode = DECODE_HUFFMAN;
                            break;
                        case DeflaterConstants.DYN_TREES:
                            dynHeader = new InflaterDynHeader();
                            mode = DECODE_DYN_HEADER;
                            break;
                        default:
                            throw new SharpZipBaseException("Unknown block type " + type);
                    }
                    return true;
                
                case DECODE_STORED_LEN1: 
                {
                    if ((uncomprLen = input.PeekBits(16)) < 0) {
                        return false;
                    }
                    input.DropBits(16);
                    mode = DECODE_STORED_LEN2;
                }
                    goto case DECODE_STORED_LEN2; // fall through
                    
                case DECODE_STORED_LEN2: 
                {
                    int nlen = input.PeekBits(16);
                    if (nlen < 0) {
                        return false;
                    }
                    input.DropBits(16);
                    if (nlen != (uncomprLen ^ 0xffff)) {
                        throw new SharpZipBaseException("broken uncompressed block");
                    }
                    mode = DECODE_STORED;
                }
                    goto case DECODE_STORED; // fall through
                    
                case DECODE_STORED: 
                {
                    int more = outputWindow.CopyStored(input, uncomprLen);
                    uncomprLen -= more;
                    if (uncomprLen == 0) {
                        mode = DECODE_BLOCKS;
                        return true;
                    }
                    return !input.IsNeedingInput;
                }
                
                case DECODE_DYN_HEADER:
                    if (!dynHeader.Decode(input)) {
                        return false;
                    }
                    
                    litlenTree = dynHeader.BuildLitLenTree();
                    distTree = dynHeader.BuildDistTree();
                    mode = DECODE_HUFFMAN;
                    goto case DECODE_HUFFMAN; // fall through
                    
                case DECODE_HUFFMAN:
                case DECODE_HUFFMAN_LENBITS:
                case DECODE_HUFFMAN_DIST:
                case DECODE_HUFFMAN_DISTBITS:
                    return DecodeHuffman();
                
                case FINISHED:
                    return false;
                
                default:
                    throw new SharpZipBaseException("Inflater.Decode unknown mode");
            }
        }
Ejemplo n.º 6
0
 /// <summary>
 /// Decodes the huffman encoded symbols in the input stream.
 /// </summary>
 /// <returns>
 /// false if more input is needed, true if output window is
 /// full or the current block ends.
 /// </returns>
 /// <exception cref="SharpZipBaseException">
 /// if deflated stream is invalid.
 /// </exception>
 private bool DecodeHuffman()
 {
     int free = outputWindow.GetFreeSpace();
     while (free >= 258) 
     {
         int symbol;
         switch (mode) 
         {
             case DECODE_HUFFMAN:
                 // This is the inner loop so it is optimized a bit
                 while (((symbol = litlenTree.GetSymbol(input)) & ~0xff) == 0) 
                 {
                     outputWindow.Write(symbol);
                     if (--free < 258) 
                     {
                         return true;
                     }
                 }
                 
                 if (symbol < 257) 
                 {
                     if (symbol < 0) 
                     {
                         return false;
                     } 
                     else 
                     {
                         // symbol == 256: end of block
                         distTree = null;
                         litlenTree = null;
                         mode = DECODE_BLOCKS;
                         return true;
                     }
                 }
                 
                 try 
                 {
                     repLength = CPLENS[symbol - 257];
                     neededBits = CPLEXT[symbol - 257];
                 } 
                 catch (Exception) 
                 {
                     throw new SharpZipBaseException("Illegal rep length code");
                 }
                 goto case DECODE_HUFFMAN_LENBITS; // fall through
                 
             case DECODE_HUFFMAN_LENBITS:
                 if (neededBits > 0) 
                 {
                     mode = DECODE_HUFFMAN_LENBITS;
                     int i = input.PeekBits(neededBits);
                     if (i < 0) 
                     {
                         return false;
                     }
                     input.DropBits(neededBits);
                     repLength += i;
                 }
                 mode = DECODE_HUFFMAN_DIST;
                 goto case DECODE_HUFFMAN_DIST; // fall through
                 
             case DECODE_HUFFMAN_DIST:
                 symbol = distTree.GetSymbol(input);
                 if (symbol < 0) 
                 {
                     return false;
                 }
                 
                 try 
                 {
                     repDist = CPDIST[symbol];
                     neededBits = CPDEXT[symbol];
                 } 
                 catch (Exception) 
                 {
                     throw new SharpZipBaseException("Illegal rep dist code");
                 }
                 
                 goto case DECODE_HUFFMAN_DISTBITS; // fall through
                 
             case DECODE_HUFFMAN_DISTBITS:
                 if (neededBits > 0) 
                 {
                     mode = DECODE_HUFFMAN_DISTBITS;
                     int i = input.PeekBits(neededBits);
                     if (i < 0) 
                     {
                         return false;
                     }
                     input.DropBits(neededBits);
                     repDist += i;
                 }
                 
                 outputWindow.Repeat(repLength, repDist);
                 free -= repLength;
                 mode = DECODE_HUFFMAN;
                 break;
             
             default:
                 throw new SharpZipBaseException("Inflater unknown mode");
         }
     }
     return true;
 }
Ejemplo n.º 7
0
 /// <summary>
 /// Resets the inflater so that a new stream can be decompressed.  All
 /// pending input and output will be discarded.
 /// </summary>
 public void Reset()
 {
     mode = noHeader ? DECODE_BLOCKS : DECODE_HEADER;
     totalIn = 0;
     totalOut = 0;
     input.Reset();
     outputWindow.Reset();
     dynHeader = null;
     litlenTree = null;
     distTree = null;
     isLastBlock = false;
     adler.Reset();
 }
        public bool Decode(StreamManipulator input)
        {
            decode_loop:
                for (;;) {
                    switch (mode) {
                        case LNUM:
                            lnum = input.PeekBits(5);
                            if (lnum < 0) {
                                return false;
                            }
                            lnum += 257;
                            input.DropBits(5);

                            mode = DNUM;
                            goto case DNUM;
                        case DNUM:
                            dnum = input.PeekBits(5);
                            if (dnum < 0) {
                                return false;
                            }
                            dnum++;
                            input.DropBits(5);
                            num = lnum+dnum;
                            litdistLens = new byte[num];
                            mode = BLNUM;
                            goto case BLNUM;
                        case BLNUM:
                            blnum = input.PeekBits(4);
                            if (blnum < 0) {
                                return false;
                            }
                            blnum += 4;
                            input.DropBits(4);
                            blLens = new byte[19];
                            ptr = 0;
                            mode = BLLENS;
                            goto case BLLENS;
                        case BLLENS:
                            while (ptr < blnum) {
                                int len = input.PeekBits(3);
                                if (len < 0) {
                                    return false;
                                }
                                input.DropBits(3);

                                blLens[BL_ORDER[ptr]] = (byte) len;
                                ptr++;
                            }
                            blTree = new InflaterHuffmanTree(blLens);
                            blLens = null;
                            ptr = 0;
                            mode = LENS;
                            goto case LENS;
                        case LENS:
                        {
                            int symbol;
                            while (((symbol = blTree.GetSymbol(input)) & ~15) == 0) {

                                litdistLens[ptr++] = lastLen = (byte)symbol;

                                if (ptr == num) {
                                    return true;
                                }
                            }

                            if (symbol < 0) {
                                return false;
                            }

                            if (symbol >= 17) {

                                lastLen = 0;
                            } else {
                                if (ptr == 0) {
                                    throw new SharpZipBaseException();
                                }
                            }
                            repSymbol = symbol-16;
                        }
                            mode = REPS;
                            goto case REPS;
                        case REPS:
                        {
                            int bits = repBits[repSymbol];
                            int count = input.PeekBits(bits);
                            if (count < 0) {
                                return false;
                            }
                            input.DropBits(bits);
                            count += repMin[repSymbol];

                            if (ptr + count > num) {
                                throw new SharpZipBaseException();
                            }
                            while (count-- > 0) {
                                litdistLens[ptr++] = lastLen;
                            }

                            if (ptr == num) {

                                return true;
                            }
                        }
                            mode = LENS;
                            goto decode_loop;
                    }
                }
        }
Ejemplo n.º 9
0
        public bool Decode(StreamManipulator input)
        {
            int num2;
            int num3;
        Label_0000:
            switch (this.mode)
            {
                case 0:
                    this.lnum = input.PeekBits(5);
                    if (this.lnum >= 0)
                    {
                        this.lnum += 0x101;
                        input.DropBits(5);
                        this.mode = 1;
                        break;
                    }
                    return false;

                case 1:
                    break;

                case 2:
                    goto Label_00B9;

                case 3:
                    goto Label_013B;

                case 4:
                    goto Label_01A8;

                case 5:
                    goto Label_01EE;

                default:
                    goto Label_0000;
            }
            this.dnum = input.PeekBits(5);
            if (this.dnum < 0)
            {
                return false;
            }
            this.dnum++;
            input.DropBits(5);
            this.num = this.lnum + this.dnum;
            this.litdistLens = new byte[this.num];
            this.mode = 2;
        Label_00B9:
            this.blnum = input.PeekBits(4);
            if (this.blnum < 0)
            {
                return false;
            }
            this.blnum += 4;
            input.DropBits(4);
            this.blLens = new byte[0x13];
            this.ptr = 0;
            this.mode = 3;
        Label_013B:
            while (this.ptr < this.blnum)
            {
                int num = input.PeekBits(3);
                if (num < 0)
                {
                    return false;
                }
                input.DropBits(3);
                this.blLens[BL_ORDER[this.ptr]] = (byte) num;
                this.ptr++;
            }
            this.blTree = new InflaterHuffmanTree(this.blLens);
            this.blLens = null;
            this.ptr = 0;
            this.mode = 4;
        Label_01A8:
            while (((num2 = this.blTree.GetSymbol(input)) & -16) == 0)
            {
                this.litdistLens[this.ptr++] = this.lastLen = (byte) num2;
                if (this.ptr == this.num)
                {
                    return true;
                }
            }
            if (num2 < 0)
            {
                return false;
            }
            if (num2 >= 0x11)
            {
                this.lastLen = 0;
            }
            else if (this.ptr == 0)
            {
                throw new SharpZipBaseException();
            }
            this.repSymbol = num2 - 0x10;
            this.mode = 5;
        Label_01EE:
            num3 = repBits[this.repSymbol];
            int num4 = input.PeekBits(num3);
            if (num4 < 0)
            {
                return false;
            }
            input.DropBits(num3);
            num4 += repMin[this.repSymbol];
            if ((this.ptr + num4) > this.num)
            {
                throw new SharpZipBaseException();
            }
            while (num4-- > 0)
            {
                this.litdistLens[this.ptr++] = this.lastLen;
            }
            if (this.ptr == this.num)
            {
                return true;
            }
            this.mode = 4;
            goto Label_0000;
        }
Ejemplo n.º 10
0
        /// <summary>
        /// Decodes the deflated stream.
        /// </summary>
        /// <returns>
        /// false if more input is needed, or if finished.
        /// </returns>
        /// <exception cref="SharpZipBaseException">
        /// if deflated stream is invalid.
        /// </exception>
        private bool Decode()
        {
            switch (mode) {
                case DECODE_HEADER:
                    return DecodeHeader();

                case DECODE_DICT:
                    return DecodeDict();

                case DECODE_CHKSUM:
                    return DecodeChksum();

                case DECODE_BLOCKS:
                    if (isLastBlock) {
                        if (noHeader) {
                            mode = FINISHED;
                            return false;
                        } else {
                            input.SkipToByteBoundary();
                            neededBits = 32;
                            mode = DECODE_CHKSUM;
                            return true;
                        }
                    }

                    int blockType;
                    if (!ReadHeader(ref isLastBlock, out blockType)) {
                        return false;
                    }
                    switch (blockType){
                        case STORED_BLOCK:
                            input.SkipToByteBoundary();
                            mode = DECODE_STORED_LEN1;
                            break;
                        case STATIC_TREES:
                            litlenTree = InflaterHuffmanTree.defLitLenTree;
                            distTree = InflaterHuffmanTree.defDistTree;
                            mode = DECODE_HUFFMAN;
                            break;
                        case DYN_TREES:
                            dynHeader = new InflaterDynHeader();
                            mode = DECODE_DYN_HEADER;
                            break;
                        default:
                            throw new SharpZipBaseException("Unknown block type " + blockType);
                    }
                    return true;

                case DECODE_STORED_LEN1:
                    if (!DecodeStoredLength()) {
                        return false;
                    }
                    mode = DECODE_STORED;
                    goto case DECODE_STORED; // fall through

                case DECODE_STORED:
                {
                    int more = outputWindow.CopyStored(input, uncomprLen);
                    uncomprLen -= more;
                    if (uncomprLen == 0) {
                        mode = DECODE_BLOCKS;
                        return true;
                    }
                    return !input.IsNeedingInput;
                }

                case DECODE_DYN_HEADER:
                    if (!dynHeader.Decode(input)) {
                        return false;
                    }

                    litlenTree = dynHeader.BuildLitLenTree();
                    distTree = dynHeader.BuildDistTree();
                    mode = DECODE_HUFFMAN;
                    goto case DECODE_HUFFMAN; // fall through

                case DECODE_HUFFMAN:
                case DECODE_HUFFMAN_LENBITS:
                case DECODE_HUFFMAN_DIST:
                case DECODE_HUFFMAN_DISTBITS:
                    return DecodeHuffman();

                case FINISHED:
                    return false;

                default:
                    throw new SharpZipBaseException("Inflater.Decode unknown mode");
            }
        }
Ejemplo n.º 11
0
        private bool Decode()
        {
            int num2;
            int num3;
            switch (this.mode)
            {
                case 0:
                    return this.DecodeHeader();

                case 1:
                    return this.DecodeDict();

                case 2:
                    if (!this.isLastBlock)
                    {
                        int num = this.input.PeekBits(3);
                        if (num < 0)
                        {
                            return false;
                        }
                        this.input.DropBits(3);
                        if ((num & 1) != 0)
                        {
                            this.isLastBlock = true;
                        }
                        switch ((num >> 1))
                        {
                            case 0:
                                this.input.SkipToByteBoundary();
                                this.mode = 3;
                                break;

                            case 1:
                                this.litlenTree = InflaterHuffmanTree.defLitLenTree;
                                this.distTree = InflaterHuffmanTree.defDistTree;
                                this.mode = 7;
                                break;

                            case 2:
                                this.dynHeader = new InflaterDynHeader();
                                this.mode = 6;
                                break;
                        }
                        throw new SharpZipBaseException("Unknown block type " + num);
                    }
                    if (!this.noHeader)
                    {
                        this.input.SkipToByteBoundary();
                        this.neededBits = 0x20;
                        this.mode = 11;
                        return true;
                    }
                    this.mode = 12;
                    return false;

                case 3:
                    this.uncomprLen = this.input.PeekBits(0x10);
                    if (this.uncomprLen >= 0)
                    {
                        this.input.DropBits(0x10);
                        this.mode = 4;
                        goto Label_0167;
                    }
                    return false;

                case 4:
                    goto Label_0167;

                case 5:
                    goto Label_01A9;

                case 6:
                    if (this.dynHeader.Decode(this.input))
                    {
                        this.litlenTree = this.dynHeader.BuildLitLenTree();
                        this.distTree = this.dynHeader.BuildDistTree();
                        this.mode = 7;
                        goto Label_022D;
                    }
                    return false;

                case 7:
                case 8:
                case 9:
                case 10:
                    goto Label_022D;

                case 11:
                    return this.DecodeChksum();

                case 12:
                    return false;

                default:
                    throw new SharpZipBaseException("Inflater.Decode unknown mode");
            }
            return true;
        Label_0167:
            num2 = this.input.PeekBits(0x10);
            if (num2 < 0)
            {
                return false;
            }
            this.input.DropBits(0x10);
            if (num2 != (this.uncomprLen ^ 0xffff))
            {
                throw new SharpZipBaseException("broken uncompressed block");
            }
            this.mode = 5;
        Label_01A9:
            num3 = this.outputWindow.CopyStored(this.input, this.uncomprLen);
            this.uncomprLen -= num3;
            if (this.uncomprLen == 0)
            {
                this.mode = 2;
                return true;
            }
            return !this.input.IsNeedingInput;
        Label_022D:
            return this.DecodeHuffman();
        }
Ejemplo n.º 12
0
 public void Reset()
 {
     this.mode = this.noHeader ? 2 : 0;
     this.totalIn = 0L;
     this.totalOut = 0L;
     this.input.Reset();
     this.outputWindow.Reset();
     this.dynHeader = null;
     this.litlenTree = null;
     this.distTree = null;
     this.isLastBlock = false;
     this.adler.Reset();
 }
Ejemplo n.º 13
0
        private bool DecodeHuffman()
        {
            int freeSpace = this.outputWindow.GetFreeSpace();
            while (freeSpace >= 0x102)
            {
                int num2;
                switch (this.mode)
                {
                    case 7:
                        goto Label_0051;

                    case 8:
                        goto Label_00C5;

                    case 9:
                        goto Label_0114;

                    case 10:
                        goto Label_0154;

                    default:
                        throw new SharpZipBaseException("Inflater unknown mode");
                }
            Label_0037:
                this.outputWindow.Write(num2);
                if (--freeSpace < 0x102)
                {
                    return true;
                }
            Label_0051:
                if (((num2 = this.litlenTree.GetSymbol(this.input)) & -256) == 0)
                {
                    goto Label_0037;
                }
                if (num2 < 0x101)
                {
                    if (num2 < 0)
                    {
                        return false;
                    }
                    this.distTree = null;
                    this.litlenTree = null;
                    this.mode = 2;
                    return true;
                }
                try
                {
                    this.repLength = CPLENS[num2 - 0x101];
                    this.neededBits = CPLEXT[num2 - 0x101];
                }
                catch (Exception)
                {
                    throw new SharpZipBaseException("Illegal rep length code");
                }
            Label_00C5:
                if (this.neededBits > 0)
                {
                    this.mode = 8;
                    int num3 = this.input.PeekBits(this.neededBits);
                    if (num3 < 0)
                    {
                        return false;
                    }
                    this.input.DropBits(this.neededBits);
                    this.repLength += num3;
                }
                this.mode = 9;
            Label_0114:
                num2 = this.distTree.GetSymbol(this.input);
                if (num2 < 0)
                {
                    return false;
                }
                try
                {
                    this.repDist = CPDIST[num2];
                    this.neededBits = CPDEXT[num2];
                }
                catch (Exception)
                {
                    throw new SharpZipBaseException("Illegal rep dist code");
                }
            Label_0154:
                if (this.neededBits > 0)
                {
                    this.mode = 10;
                    int num4 = this.input.PeekBits(this.neededBits);
                    if (num4 < 0)
                    {
                        return false;
                    }
                    this.input.DropBits(this.neededBits);
                    this.repDist += num4;
                }
                this.outputWindow.Repeat(this.repLength, this.repDist);
                freeSpace -= this.repLength;
                this.mode = 7;
            }
            return true;
        }
Ejemplo n.º 14
0
        /// <summary>
        /// Decodes the huffman encoded symbols in the input stream.
        /// </summary>
        /// <returns>
        /// false if more input is needed, true if output window is
        /// full or the current block ends.
        /// </returns>
        /// <exception cref="SharpZipBaseException">
        /// if deflated stream is invalid.
        /// </exception>
        private bool DecodeHuffman()
        {
            int free = outputWindow.GetFreeSpace();

            while (free >= 258)
            {
                int symbol;
                switch (mode)
                {
                case DECODE_HUFFMAN:
                    // This is the inner loop so it is optimized a bit
                    while (((symbol = litlenTree.GetSymbol(input)) & ~0xff) == 0)
                    {
                        outputWindow.Write(symbol);
                        if (--free < 258)
                        {
                            return(true);
                        }
                    }

                    if (symbol < 257)
                    {
                        if (symbol < 0)
                        {
                            return(false);
                        }
                        else
                        {
                            // symbol == 256: end of block
                            distTree   = null;
                            litlenTree = null;
                            mode       = DECODE_BLOCKS;
                            return(true);
                        }
                    }

                    try {
                        repLength  = CPLENS[symbol - 257];
                        neededBits = CPLEXT[symbol - 257];
                    } catch (Exception) {
                        throw new SharpZipBaseException("Illegal rep length code");
                    }
                    goto case DECODE_HUFFMAN_LENBITS;     // fall through

                case DECODE_HUFFMAN_LENBITS:
                    if (neededBits > 0)
                    {
                        mode = DECODE_HUFFMAN_LENBITS;
                        int i = input.PeekBits(neededBits);
                        if (i < 0)
                        {
                            return(false);
                        }
                        input.DropBits(neededBits);
                        repLength += i;
                    }
                    mode = DECODE_HUFFMAN_DIST;
                    goto case DECODE_HUFFMAN_DIST;     // fall through

                case DECODE_HUFFMAN_DIST:
                    symbol = distTree.GetSymbol(input);
                    if (symbol < 0)
                    {
                        return(false);
                    }

                    try {
                        repDist    = CPDIST[symbol];
                        neededBits = CPDEXT[symbol];
                    } catch (Exception) {
                        throw new SharpZipBaseException("Illegal rep dist code");
                    }

                    goto case DECODE_HUFFMAN_DISTBITS;     // fall through

                case DECODE_HUFFMAN_DISTBITS:
                    if (neededBits > 0)
                    {
                        mode = DECODE_HUFFMAN_DISTBITS;
                        int i = input.PeekBits(neededBits);
                        if (i < 0)
                        {
                            return(false);
                        }
                        input.DropBits(neededBits);
                        repDist += i;
                    }

                    outputWindow.Repeat(repLength, repDist);
                    free -= repLength;
                    mode  = DECODE_HUFFMAN;
                    break;

                default:
                    throw new SharpZipBaseException("Inflater unknown mode");
                }
            }
            return(true);
        }