Exemple #1
0
 public Inflater(bool nowrap)
 {
     this.nowrap = nowrap;
     this.adler = new Adler32();
     this.input = new StreamManipulator();
     this.outputWindow = new OutputWindow();
     this.mode = nowrap ? 2 : 0;
 }
 public int GetSymbol(StreamManipulator input)
 {
     int num2;
     int index = input.PeekBits(9);
     if (index >= 0)
     {
         num2 = this.tree[index];
         if (num2 >= 0)
         {
             input.DropBits(num2 & 15);
             return (num2 >> 4);
         }
         int num3 = -(num2 >> 4);
         int n = num2 & 15;
         index = input.PeekBits(n);
         if (index >= 0)
         {
             num2 = this.tree[num3 | (index >> 9)];
             input.DropBits(num2 & 15);
             return (num2 >> 4);
         }
         int num5 = input.AvailableBits;
         index = input.PeekBits(num5);
         num2 = this.tree[num3 | (index >> 9)];
         if ((num2 & 15) <= num5)
         {
             input.DropBits(num2 & 15);
             return (num2 >> 4);
         }
         return -1;
     }
     int availableBits = input.AvailableBits;
     index = input.PeekBits(availableBits);
     num2 = this.tree[index];
     if ((num2 >= 0) && ((num2 & 15) <= availableBits))
     {
         input.DropBits(num2 & 15);
         return (num2 >> 4);
     }
     return -1;
 }
 public int CopyStored(StreamManipulator input, int len)
 {
     int num;
     len = Math.Min(Math.Min(len, WINDOW_SIZE - this.window_filled), input.AvailableBytes);
     int length = WINDOW_SIZE - this.window_end;
     if (len > length)
     {
         num = input.CopyBytes(this.window, this.window_end, length);
         if (num == length)
         {
             num += input.CopyBytes(this.window, 0, len - length);
         }
     }
     else
     {
         num = input.CopyBytes(this.window, this.window_end, len);
     }
     this.window_end = (this.window_end + num) & WINDOW_MASK;
     this.window_filled += num;
     return num;
 }
        public bool Decode(StreamManipulator input)
        {
            int num2;
            int num3;
            Label_0005:
            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_00D1;

                case 3:
                    goto Label_0161;

                case 4:
                    goto Label_01D6;

                case 5:
                    goto Label_0224;

                default:
                    goto Label_0005;
            }
            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_00D1:
            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_0161:
            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_01D6:
            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 Exception();
            }
            this.repSymbol = num2 - 0x10;
            this.mode = 5;
            Label_0224:
            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 Exception();
            }
            while (num4-- > 0)
            {
                this.litdistLens[this.ptr++] = this.lastLen;
            }
            if (this.ptr == this.num)
            {
                return true;
            }
            this.mode = 4;
            goto Label_0005;
        }