private uint ReadHuffSym(ushort[] table, byte[] lengths, uint nsyms, uint nbits, LzxDecoder.BitBuffer bitbuf) { bitbuf.EnsureBits((byte)16); uint num1; if ((num1 = (uint)table[(IntPtr)bitbuf.PeekBits((byte)nbits)]) >= nsyms) { uint num2 = (uint)(1 << 32 - (int)nbits); uint num3; do { num2 >>= 1; num3 = num1 << 1 | (((int)bitbuf.GetBuffer() & (int)num2) != 0 ? 1U : 0U); if ((int)num2 == 0) { return(0U); } }while ((num1 = (uint)table[(IntPtr)num3]) >= nsyms); } uint num4 = (uint)lengths[(IntPtr)num1]; bitbuf.RemoveBits((byte)num4); return(num1); }
public int Decompress(Stream inData, int inLen, Stream outData, int outLen) { LzxDecoder.BitBuffer bitbuf = new LzxDecoder.BitBuffer(inData); long position = inData.Position; long num1 = inData.Position + (long) inLen; byte[] buffer1 = this.m_state.window; uint num2 = this.m_state.window_posn; uint num3 = this.m_state.window_size; uint num4 = this.m_state.R0; uint num5 = this.m_state.R1; uint num6 = this.m_state.R2; int num7 = outLen; bitbuf.InitBitStream(); if (this.m_state.header_read == 0) { if ((int) bitbuf.ReadBits((byte) 1) != 0) this.m_state.intel_filesize = (int) bitbuf.ReadBits((byte) 16) << 16 | (int) bitbuf.ReadBits((byte) 16); this.m_state.header_read = 1; } label_87: while (num7 > 0) { if ((int) this.m_state.block_remaining == 0) { if (this.m_state.block_type == LzxConstants.BLOCKTYPE.UNCOMPRESSED) { if (((int) this.m_state.block_length & 1) == 1) inData.ReadByte(); bitbuf.InitBitStream(); } this.m_state.block_type = (LzxConstants.BLOCKTYPE) bitbuf.ReadBits((byte) 3); this.m_state.block_remaining = this.m_state.block_length = bitbuf.ReadBits((byte) 16) << 8 | bitbuf.ReadBits((byte) 8); switch (this.m_state.block_type) { case LzxConstants.BLOCKTYPE.VERBATIM: this.ReadLengths(this.m_state.MAINTREE_len, 0U, 256U, bitbuf); this.ReadLengths(this.m_state.MAINTREE_len, 256U, (uint) this.m_state.main_elements, bitbuf); this.MakeDecodeTable(656U, 12U, this.m_state.MAINTREE_len, this.m_state.MAINTREE_table); if ((int) this.m_state.MAINTREE_len[232] != 0) this.m_state.intel_started = 1; this.ReadLengths(this.m_state.LENGTH_len, 0U, 249U, bitbuf); this.MakeDecodeTable(250U, 12U, this.m_state.LENGTH_len, this.m_state.LENGTH_table); break; case LzxConstants.BLOCKTYPE.ALIGNED: uint num8 = 0U; for (; num8 < 8U; ++num8) { uint num9 = bitbuf.ReadBits((byte) 3); this.m_state.ALIGNED_len[(IntPtr) num8] = (byte) num9; } this.MakeDecodeTable(8U, 7U, this.m_state.ALIGNED_len, this.m_state.ALIGNED_table); goto case LzxConstants.BLOCKTYPE.VERBATIM; case LzxConstants.BLOCKTYPE.UNCOMPRESSED: this.m_state.intel_started = 1; bitbuf.EnsureBits((byte) 16); if ((int) bitbuf.GetBitsLeft() > 16) inData.Seek(-2L, SeekOrigin.Current); num4 = (uint) ((int) (byte) inData.ReadByte() | (int) (byte) inData.ReadByte() << 8 | (int) (byte) inData.ReadByte() << 16 | (int) (byte) inData.ReadByte() << 24); num5 = (uint) ((int) (byte) inData.ReadByte() | (int) (byte) inData.ReadByte() << 8 | (int) (byte) inData.ReadByte() << 16 | (int) (byte) inData.ReadByte() << 24); num6 = (uint) ((int) (byte) inData.ReadByte() | (int) (byte) inData.ReadByte() << 8 | (int) (byte) inData.ReadByte() << 16 | (int) (byte) inData.ReadByte() << 24); break; default: return -1; } } if (inData.Position > position + (long) inLen && (inData.Position > position + (long) inLen + 2L || (int) bitbuf.GetBitsLeft() < 16)) return -1; label_86: int count; while (true) { if ((count = (int) this.m_state.block_remaining) > 0 && num7 > 0) { if (count > num7) count = num7; num7 -= count; this.m_state.block_remaining -= (uint) count; num2 &= num3 - 1U; if ((long) num2 + (long) count <= (long) num3) { switch (this.m_state.block_type) { case LzxConstants.BLOCKTYPE.VERBATIM: goto label_52; case LzxConstants.BLOCKTYPE.ALIGNED: goto label_81; case LzxConstants.BLOCKTYPE.UNCOMPRESSED: if (inData.Position + (long) count <= num1) { byte[] buffer2 = new byte[count]; inData.Read(buffer2, 0, count); buffer2.CopyTo((Array) buffer1, (int) num2); num2 += (uint) count; continue; } else goto label_83; default: goto label_85; } } else break; } else goto label_87; } return -1; label_52: while (count > 0) { int num8 = (int) this.ReadHuffSym(this.m_state.MAINTREE_table, this.m_state.MAINTREE_len, 656U, 12U, bitbuf); if (num8 < 256) { buffer1[(IntPtr) num2++] = (byte) num8; --count; } else { int num9 = num8 - 256; int num10 = num9 & 7; if (num10 == 7) { int num11 = (int) this.ReadHuffSym(this.m_state.LENGTH_table, this.m_state.LENGTH_len, 250U, 12U, bitbuf); num10 += num11; } int num12 = num10 + 2; int index = num9 >> 3; int num13; if (index > 2) { if (index != 3) { int num11 = (int) LzxDecoder.extra_bits[index]; int num14 = (int) bitbuf.ReadBits((byte) num11); num13 = (int) LzxDecoder.position_base[index] - 2 + num14; } else num13 = 1; num6 = num5; num5 = num4; num4 = (uint) num13; } else if (index == 0) num13 = (int) num4; else if (index == 1) { num13 = (int) num5; num5 = num4; num4 = (uint) num13; } else { num13 = (int) num6; num6 = num4; num4 = (uint) num13; } int num15 = (int) num2; count -= num12; int num16; if ((long) num2 >= (long) num13) { num16 = num15 - num13; } else { num16 = num15 + ((int) num3 - num13); int num11 = num13 - (int) num2; if (num11 < num12) { num12 -= num11; num2 += (uint) num11; while (num11-- > 0) buffer1[num15++] = buffer1[num16++]; num16 = 0; } } num2 += (uint) num12; while (num12-- > 0) buffer1[num15++] = buffer1[num16++]; } } goto label_86; label_81: while (count > 0) { int num8 = (int) this.ReadHuffSym(this.m_state.MAINTREE_table, this.m_state.MAINTREE_len, 656U, 12U, bitbuf); if (num8 < 256) { buffer1[(IntPtr) num2++] = (byte) num8; --count; } else { int num9 = num8 - 256; int num10 = num9 & 7; if (num10 == 7) { int num11 = (int) this.ReadHuffSym(this.m_state.LENGTH_table, this.m_state.LENGTH_len, 250U, 12U, bitbuf); num10 += num11; } int num12 = num10 + 2; int index = num9 >> 3; int num13; if (index > 2) { int num11 = (int) LzxDecoder.extra_bits[index]; int num14 = (int) LzxDecoder.position_base[index] - 2; if (num11 > 3) { int num15 = num11 - 3; int num16 = (int) bitbuf.ReadBits((byte) num15); num13 = num14 + (num16 << 3) + (int) this.ReadHuffSym(this.m_state.ALIGNED_table, this.m_state.ALIGNED_len, 8U, 7U, bitbuf); } else if (num11 == 3) { int num15 = (int) this.ReadHuffSym(this.m_state.ALIGNED_table, this.m_state.ALIGNED_len, 8U, 7U, bitbuf); num13 = num14 + num15; } else if (num11 > 0) { int num15 = (int) bitbuf.ReadBits((byte) num11); num13 = num14 + num15; } else num13 = 1; num6 = num5; num5 = num4; num4 = (uint) num13; } else if (index == 0) num13 = (int) num4; else if (index == 1) { num13 = (int) num5; num5 = num4; num4 = (uint) num13; } else { num13 = (int) num6; num6 = num4; num4 = (uint) num13; } int num17 = (int) num2; count -= num12; int num18; if ((long) num2 >= (long) num13) { num18 = num17 - num13; } else { num18 = num17 + ((int) num3 - num13); int num11 = num13 - (int) num2; if (num11 < num12) { num12 -= num11; num2 += (uint) num11; while (num11-- > 0) buffer1[num17++] = buffer1[num18++]; num18 = 0; } } num2 += (uint) num12; while (num12-- > 0) buffer1[num17++] = buffer1[num18++]; } } goto label_86; label_83: return -1; label_85: return -1; } if (num7 != 0) return -1; int num19 = (int) num2; if (num19 == 0) num19 = (int) num3; int offset = num19 - outLen; outData.Write(buffer1, offset, outLen); this.m_state.window_posn = num2; this.m_state.R0 = num4; this.m_state.R1 = num5; this.m_state.R2 = num6; if (this.m_state.frames_read++ >= 32768U || this.m_state.intel_filesize == 0) return 0; if (outLen <= 6 || this.m_state.intel_started == 0) { this.m_state.intel_curpos += outLen; } else { int num8 = outLen - 10; uint num9 = (uint) this.m_state.intel_curpos; this.m_state.intel_curpos = (int) num9 + outLen; while (outData.Position < (long) num8) { if (outData.ReadByte() != 232) ++num9; } } return -1; }
public int Decompress(Stream inData, int inLen, Stream outData, int outLen) { LzxDecoder.BitBuffer bitbuf = new LzxDecoder.BitBuffer(inData); long position = inData.Position; long num1 = inData.Position + (long)inLen; byte[] buffer1 = this.m_state.window; uint num2 = this.m_state.window_posn; uint num3 = this.m_state.window_size; uint num4 = this.m_state.R0; uint num5 = this.m_state.R1; uint num6 = this.m_state.R2; int num7 = outLen; bitbuf.InitBitStream(); if (this.m_state.header_read == 0) { if ((int)bitbuf.ReadBits((byte)1) != 0) { this.m_state.intel_filesize = (int)bitbuf.ReadBits((byte)16) << 16 | (int)bitbuf.ReadBits((byte)16); } this.m_state.header_read = 1; } while (num7 > 0) { if ((int)this.m_state.block_remaining == 0) { if (this.m_state.block_type == LzxConstants.BLOCKTYPE.UNCOMPRESSED) { if (((int)this.m_state.block_length & 1) == 1) { inData.ReadByte(); } bitbuf.InitBitStream(); } this.m_state.block_type = (LzxConstants.BLOCKTYPE)bitbuf.ReadBits((byte)3); this.m_state.block_remaining = this.m_state.block_length = bitbuf.ReadBits((byte)16) << 8 | bitbuf.ReadBits((byte)8); switch (this.m_state.block_type) { case LzxConstants.BLOCKTYPE.VERBATIM: this.ReadLengths(this.m_state.MAINTREE_len, 0U, 256U, bitbuf); this.ReadLengths(this.m_state.MAINTREE_len, 256U, (uint)this.m_state.main_elements, bitbuf); this.MakeDecodeTable(656U, 12U, this.m_state.MAINTREE_len, this.m_state.MAINTREE_table); if ((int)this.m_state.MAINTREE_len[232] != 0) { this.m_state.intel_started = 1; } this.ReadLengths(this.m_state.LENGTH_len, 0U, 249U, bitbuf); this.MakeDecodeTable(250U, 12U, this.m_state.LENGTH_len, this.m_state.LENGTH_table); break; case LzxConstants.BLOCKTYPE.ALIGNED: uint num8 = 0U; for (; num8 < 8U; ++num8) { uint num9 = bitbuf.ReadBits((byte)3); this.m_state.ALIGNED_len[(IntPtr)num8] = (byte)num9; } this.MakeDecodeTable(8U, 7U, this.m_state.ALIGNED_len, this.m_state.ALIGNED_table); goto case LzxConstants.BLOCKTYPE.VERBATIM; case LzxConstants.BLOCKTYPE.UNCOMPRESSED: this.m_state.intel_started = 1; bitbuf.EnsureBits((byte)16); if ((int)bitbuf.GetBitsLeft() > 16) { inData.Seek(-2L, SeekOrigin.Current); } num4 = (uint)((int)(byte)inData.ReadByte() | (int)(byte)inData.ReadByte() << 8 | (int)(byte)inData.ReadByte() << 16 | (int)(byte)inData.ReadByte() << 24); num5 = (uint)((int)(byte)inData.ReadByte() | (int)(byte)inData.ReadByte() << 8 | (int)(byte)inData.ReadByte() << 16 | (int)(byte)inData.ReadByte() << 24); num6 = (uint)((int)(byte)inData.ReadByte() | (int)(byte)inData.ReadByte() << 8 | (int)(byte)inData.ReadByte() << 16 | (int)(byte)inData.ReadByte() << 24); break; default: return(-1); } } if (inData.Position > position + (long)inLen && (inData.Position > position + (long)inLen + 2L || (int)bitbuf.GetBitsLeft() < 16)) { return(-1); } int count; while ((count = (int)this.m_state.block_remaining) > 0 && num7 > 0) { if (count > num7) { count = num7; } num7 -= count; this.m_state.block_remaining -= (uint)count; num2 &= num3 - 1U; if ((long)num2 + (long)count > (long)num3) { return(-1); } switch (this.m_state.block_type) { case LzxConstants.BLOCKTYPE.VERBATIM: while (count > 0) { int num8 = (int)this.ReadHuffSym(this.m_state.MAINTREE_table, this.m_state.MAINTREE_len, 656U, 12U, bitbuf); if (num8 < 256) { buffer1[(IntPtr)num2++] = (byte)num8; --count; } else { int num9 = num8 - 256; int num10 = num9 & 7; if (num10 == 7) { int num11 = (int)this.ReadHuffSym(this.m_state.LENGTH_table, this.m_state.LENGTH_len, 250U, 12U, bitbuf); num10 += num11; } int num12 = num10 + 2; int index = num9 >> 3; int num13; if (index > 2) { if (index != 3) { int num11 = (int)LzxDecoder.extra_bits[index]; int num14 = (int)bitbuf.ReadBits((byte)num11); num13 = (int)LzxDecoder.position_base[index] - 2 + num14; } else { num13 = 1; } num6 = num5; num5 = num4; num4 = (uint)num13; } else if (index == 0) { num13 = (int)num4; } else if (index == 1) { num13 = (int)num5; num5 = num4; num4 = (uint)num13; } else { num13 = (int)num6; num6 = num4; num4 = (uint)num13; } int num15 = (int)num2; count -= num12; int num16; if ((long)num2 >= (long)num13) { num16 = num15 - num13; } else { num16 = num15 + ((int)num3 - num13); int num11 = num13 - (int)num2; if (num11 < num12) { num12 -= num11; num2 += (uint)num11; while (num11-- > 0) { buffer1[num15++] = buffer1[num16++]; } num16 = 0; } } num2 += (uint)num12; while (num12-- > 0) { buffer1[num15++] = buffer1[num16++]; } } } break; case LzxConstants.BLOCKTYPE.ALIGNED: while (count > 0) { int num8 = (int)this.ReadHuffSym(this.m_state.MAINTREE_table, this.m_state.MAINTREE_len, 656U, 12U, bitbuf); if (num8 < 256) { buffer1[(IntPtr)num2++] = (byte)num8; --count; } else { int num9 = num8 - 256; int num10 = num9 & 7; if (num10 == 7) { int num11 = (int)this.ReadHuffSym(this.m_state.LENGTH_table, this.m_state.LENGTH_len, 250U, 12U, bitbuf); num10 += num11; } int num12 = num10 + 2; int index = num9 >> 3; int num13; if (index > 2) { int num11 = (int)LzxDecoder.extra_bits[index]; int num14 = (int)LzxDecoder.position_base[index] - 2; if (num11 > 3) { int num15 = num11 - 3; int num16 = (int)bitbuf.ReadBits((byte)num15); num13 = num14 + (num16 << 3) + (int)this.ReadHuffSym(this.m_state.ALIGNED_table, this.m_state.ALIGNED_len, 8U, 7U, bitbuf); } else if (num11 == 3) { int num15 = (int)this.ReadHuffSym(this.m_state.ALIGNED_table, this.m_state.ALIGNED_len, 8U, 7U, bitbuf); num13 = num14 + num15; } else if (num11 > 0) { int num15 = (int)bitbuf.ReadBits((byte)num11); num13 = num14 + num15; } else { num13 = 1; } num6 = num5; num5 = num4; num4 = (uint)num13; } else if (index == 0) { num13 = (int)num4; } else if (index == 1) { num13 = (int)num5; num5 = num4; num4 = (uint)num13; } else { num13 = (int)num6; num6 = num4; num4 = (uint)num13; } int num17 = (int)num2; count -= num12; int num18; if ((long)num2 >= (long)num13) { num18 = num17 - num13; } else { num18 = num17 + ((int)num3 - num13); int num11 = num13 - (int)num2; if (num11 < num12) { num12 -= num11; num2 += (uint)num11; while (num11-- > 0) { buffer1[num17++] = buffer1[num18++]; } num18 = 0; } } num2 += (uint)num12; while (num12-- > 0) { buffer1[num17++] = buffer1[num18++]; } } } break; case LzxConstants.BLOCKTYPE.UNCOMPRESSED: if (inData.Position + (long)count > num1) { return(-1); } byte[] buffer2 = new byte[count]; inData.Read(buffer2, 0, count); buffer2.CopyTo((Array)buffer1, (int)num2); num2 += (uint)count; break; default: return(-1); } } } if (num7 != 0) { return(-1); } int num19 = (int)num2; if (num19 == 0) { num19 = (int)num3; } int offset = num19 - outLen; outData.Write(buffer1, offset, outLen); this.m_state.window_posn = num2; this.m_state.R0 = num4; this.m_state.R1 = num5; this.m_state.R2 = num6; if (this.m_state.frames_read++ >= 32768U || this.m_state.intel_filesize == 0) { return(0); } if (outLen <= 6 || this.m_state.intel_started == 0) { this.m_state.intel_curpos += outLen; } else { int num8 = outLen - 10; uint num9 = (uint)this.m_state.intel_curpos; uint num10 = (uint)this.m_state.intel_filesize; this.m_state.intel_curpos = (int)num9 + outLen; while (outData.Position < (long)num8) { if (outData.ReadByte() != 232) { ++num9; } } } return(-1); }