public int CopyStored(StreamManipulator input, int len) { len = Math.Min(Math.Min(len, WINDOW_SIZE - window_filled), input.AvailableBytes); int copied; int tailLen = WINDOW_SIZE - window_end; if (len > tailLen) { copied = input.CopyBytes(window, window_end, tailLen); if (copied == tailLen) { copied += input.CopyBytes(window, 0, len - tailLen); } } else { copied = input.CopyBytes(window, window_end, len); } window_end = (window_end + copied) & WINDOW_MASK; window_filled += copied; return copied; }
/// <summary> /// Reads the next symbol from input. The symbol is encoded using the /// huffman tree. /// </summary> /// <param name="input"> /// input the input source. /// </param> /// <returns> /// the next symbol, or -1 if not enough input is available. /// </returns> public int GetSymbol(StreamManipulator input) { int lookahead, symbol; if ((lookahead = input.PeekBits(9)) >= 0) { if ((symbol = tree[lookahead]) >= 0) { input.DropBits(symbol & 15); return symbol >> 4; } int subtree = -(symbol >> 4); int bitlen = symbol & 15; if ((lookahead = input.PeekBits(bitlen)) >= 0) { symbol = tree[subtree | (lookahead >> 9)]; input.DropBits(symbol & 15); return symbol >> 4; } else { int bits = input.AvailableBits; lookahead = input.PeekBits(bits); symbol = tree[subtree | (lookahead >> 9)]; if ((symbol & 15) <= bits) { input.DropBits(symbol & 15); return symbol >> 4; } else { return -1; } } } else { int bits = input.AvailableBits; lookahead = input.PeekBits(bits); symbol = tree[lookahead]; if (symbol >= 0 && (symbol & 15) <= bits) { input.DropBits(symbol & 15); return symbol >> 4; } else { return -1; } } }
/// <summary> /// Copy from input manipulator to internal window /// </summary> /// <param name="input">source of data</param> /// <param name="length">length of data to copy</param> /// <returns>the number of bytes copied</returns> public int CopyStored(StreamManipulator input, int length) { length = Math.Min(Math.Min(length, WindowSize - windowFilled), input.AvailableBytes); int copied; int tailLen = WindowSize - windowEnd; if (length > tailLen) { copied = input.CopyBytes(window, windowEnd, tailLen); if (copied == tailLen) { copied += input.CopyBytes(window, 0, length - tailLen); } } else { copied = input.CopyBytes(window, windowEnd, length); } windowEnd = (windowEnd + copied) & WindowMask; windowFilled += copied; return copied; }
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); litlenLens = new byte[lnum]; // 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); distLens = new byte[dnum]; // System.err.println("DNUM: "+dnum); 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 = LLENS; goto case LLENS;/* fall through */ case LLENS: while (ptr < lnum) { int symbol = blTree.GetSymbol(input); if (symbol < 0) { return false; } switch (symbol) { default: // System.err.println("litlenLens["+ptr+"]: "+symbol); litlenLens[ptr++] = (byte) symbol; break; case 16: /* repeat last len 3-6 times */ if (ptr == 0) { throw new Exception("Repeating, but no prev len"); } // System.err.println("litlenLens["+ptr+"]: repeat"); repeatedLen = litlenLens[ptr-1]; repBits = 2; for (int i = 3; i-- > 0; ) { if (ptr >= lnum) { throw new Exception(); } litlenLens[ptr++] = repeatedLen; } mode = LREPS; goto decode_loop; case 17: /* repeat zero 3-10 times */ // System.err.println("litlenLens["+ptr+"]: zero repeat"); repeatedLen = 0; repBits = 3; for (int i = 3; i-- > 0; ) { if (ptr >= lnum) { throw new Exception(); } litlenLens[ptr++] = repeatedLen; } mode = LREPS; goto decode_loop; case 18: /* repeat zero 11-138 times */ // System.err.println("litlenLens["+ptr+"]: zero repeat"); repeatedLen = 0; repBits = 7; for (int i = 11; i-- > 0; ) { if (ptr >= lnum) { throw new Exception(); } litlenLens[ptr++] = repeatedLen; } mode = LREPS; goto decode_loop; } } ptr = 0; mode = DLENS; goto case DLENS;/* fall through */ case DLENS: while (ptr < dnum) { int symbol = blTree.GetSymbol(input); if (symbol < 0) { return false; } switch (symbol) { default: distLens[ptr++] = (byte) symbol; // System.err.println("distLens["+ptr+"]: "+symbol); break; case 16: /* repeat last len 3-6 times */ if (ptr == 0) { throw new Exception("Repeating, but no prev len"); } // System.err.println("distLens["+ptr+"]: repeat"); repeatedLen = distLens[ptr-1]; repBits = 2; for (int i = 3; i-- > 0; ) { if (ptr >= dnum) { throw new Exception(); } distLens[ptr++] = repeatedLen; } mode = DREPS; goto decode_loop; case 17: /* repeat zero 3-10 times */ // System.err.println("distLens["+ptr+"]: repeat zero"); repeatedLen = 0; repBits = 3; for (int i = 3; i-- > 0; ) { if (ptr >= dnum) { throw new Exception(); } distLens[ptr++] = repeatedLen; } mode = DREPS; goto decode_loop; case 18: /* repeat zero 11-138 times */ // System.err.println("distLens["+ptr+"]: repeat zero"); repeatedLen = 0; repBits = 7; for (int i = 11; i-- > 0; ) { if (ptr >= dnum) { throw new Exception(); } distLens[ptr++] = repeatedLen; } mode = DREPS; goto decode_loop; } } mode = FINISH; return true; case LREPS: { int count = input.PeekBits(repBits); if (count < 0) { return false; } input.DropBits(repBits); // System.err.println("litlenLens repeat: "+repBits); while (count-- > 0) { if (ptr >= lnum) { throw new Exception(); } litlenLens[ptr++] = repeatedLen; } } mode = LLENS; goto decode_loop; case DREPS: { int count = input.PeekBits(repBits); if (count < 0) { return false; } input.DropBits(repBits); while (count-- > 0) { if (ptr >= dnum) { throw new Exception(); } distLens[ptr++] = repeatedLen; } } mode = DLENS; goto decode_loop; } } }
/// <summary> /// Check the stream header which is returend from the server. /// </summary> /// <param name="value">The value as an array of <see cref="byte"/></param> /// <returns>wheater is input is compressed [true] or its not compressed [false]</returns> public static bool CheckHeader(byte[] value) { #region Access Log #if TRACE { Handler.LogHandler.Tracking("Access Method: " + typeof(Compression).FullName + "->" + ((object)MethodBase.GetCurrentMethod()).ToString() + " ;"); } #endif #endregion Access Log MemoryStream ms = new MemoryStream(value); StreamManipulator input = new StreamManipulator(); input.SetInput(value, 0, value.Length); return DecodeHeader(input); }
/// <summary> /// Decodes a zlib/RFC1950 header - to evaluate input if /// decompression is needed or not. /// </summary> /// <param name="input">The input as an object of type <see cref="StreamManipulator"/></param> /// <returns>wheater is input is compressed [true] or its not compressed [false]</returns> private static bool DecodeHeader(StreamManipulator input) { #region Access Log #if TRACE { Handler.LogHandler.Tracking("Access Method: " + typeof(Compression).FullName + "->" + ((object)MethodBase.GetCurrentMethod()).ToString() + " ;"); } #endif #endregion Access Log int header = input.PeekBits(16); if (header < 0) { return false; } input.DropBits(16); /* header is written in "wrong" byte order */ header = ((header << 8) | (header >> 8)) & 0xffff; if (header % 31 != 0) { // not compressed return false; } // its compressed return true; }
internal Inflater() { input = new StreamManipulator(); output = new Output(); mode = 2; }
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; } } }
internal bool Decode(StreamManipulator input) { decode_loop: while (true) { switch (mode) { case 0: lnum = input.PeekBits(5); if (lnum < 0) { return(false); } lnum += 257; input.DropBits(5); mode = 1; goto case 1; case 1: dnum = input.PeekBits(5); if (dnum < 0) { return(false); } dnum++; input.DropBits(5); num = lnum + dnum; litdistLens = new byte[num]; mode = 2; goto case 2; case 2: blnum = input.PeekBits(4); if (blnum < 0) { return(false); } blnum += 4; input.DropBits(4); blLens = new byte[19]; ptr = 0; mode = 3; goto case 3; case 3: 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 HuffmanTree(blLens); blLens = null; ptr = 0; mode = 4; goto case 4; case 4: 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; } repSymbol = symbol - 16; mode = 5; goto case 5; case 5: int bits = repBits[repSymbol]; int count = input.PeekBits(bits); if (count < 0) { return(false); } input.DropBits(bits); count += repMin[repSymbol]; while (count-- > 0) { litdistLens[ptr++] = lastLen; } if (ptr == num) { return(true); } mode = 4; goto decode_loop; } } }
public bool Decode(StreamManipulator input) { try { lnum = input.GrabBits(5) + 257; dnum = input.GrabBits(5) + 1; blnum = input.GrabBits(4) + 4; num = lnum + dnum; lengths = new byte[19]; for (int i = 0; i < blnum; i++) { lengths[BL_ORDER[i]] = (byte)input.GrabBits(3, true); } blTree = new InflaterHuffmanTree(lengths); lengths = new byte[num]; int index = 0; while (index < lnum + dnum) { byte len; int symbol = blTree.GetSymbol(input); if (symbol < 0) { return(false); } if (symbol < 16) { lengths[index++] = (byte)symbol; } else { len = 0; if (symbol == 16) { if (index == 0) { return(false); // No last length! } len = lengths[index - 1]; symbol = input.GrabBits(2, true) + 3; } else if (symbol == 17) { // repeat zero 3..10 times symbol = input.GrabBits(3, true) + 3; } else { // (symbol == 18), repeat zero 11..138 times symbol = input.GrabBits(7, true) + 11; } if (index + symbol > lnum + dnum) { return(false); // too many lengths! } // repeat last or zero symbol times while (symbol-- > 0) { lengths[index++] = len; } } } if (lengths[256] == 0) { return(false); // No end-of-block code! } return(true); } catch (Exception x) { return(false); } }
public InflaterDynHeader(StreamManipulator input) { this.input = input; stateMachine = CreateStateMachine(); state = stateMachine.GetEnumerator(); }
public bool Decode(StreamManipulator input) { int mode; int num5; TR_002D: while (true) { mode = this.mode; switch (mode) { case 0: this.lnum = input.PeekBits(5); if (this.lnum < 0) { return(false); } this.lnum += 0x101; input.DropBits(5); this.mode = 1; goto TR_0026; case 1: goto TR_0026; case 2: goto TR_0023; case 3: goto TR_0021; case 4: goto TR_001A; case 5: break; default: { continue; } } goto TR_000E; } goto TR_0026; TR_000E: num5 = repBits[this.repSymbol]; int num6 = input.PeekBits(num5); if (num6 < 0) { return(false); } input.DropBits(num5); num6 += repMin[this.repSymbol]; if ((this.ptr + num6) > this.num) { throw new SharpZipBaseException(); } while (true) { if (num6-- <= 0) { if (this.ptr == this.num) { return(true); } this.mode = 4; break; } mode = this.ptr; this.ptr = mode + 1; this.litdistLens[mode] = this.lastLen; } goto TR_002D; TR_001A: while (true) { int num3; if (((num3 = this.blTree.GetSymbol(input)) & -16) != 0) { if (num3 < 0) { return(false); } if (num3 >= 0x11) { this.lastLen = 0; } else if (this.ptr == 0) { throw new SharpZipBaseException(); } this.repSymbol = num3 - 0x10; this.mode = 5; break; } mode = this.ptr; this.ptr = mode + 1; this.litdistLens[mode] = this.lastLen = (byte)num3; if (this.ptr == this.num) { return(true); } } goto TR_000E; TR_0021: while (true) { if (this.ptr >= this.blnum) { this.blTree = new InflaterHuffmanTree(this.blLens); this.blLens = null; this.ptr = 0; this.mode = 4; break; } int num2 = input.PeekBits(3); if (num2 < 0) { return(false); } input.DropBits(3); this.blLens[BL_ORDER[this.ptr]] = (byte)num2; this.ptr++; } goto TR_001A; TR_0023: 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; goto TR_0021; TR_0026: 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; goto TR_0023; }
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; } } }
/// <summary> /// Creates a new inflater. /// </summary> /// <param name="noHeader"> /// True if no RFC1950/Zlib header and footer fields are expected in the input data /// /// This is used for GZIPed/Zipped input. /// /// For compatibility with /// Sun JDK you should provide one byte of input more than needed in /// this case. /// </param> public Inflater(bool noHeader) { this.noHeader = noHeader; this.adler = new Adler32(); input = new StreamManipulator(); outputWindow = new OutputWindow(); mode = noHeader ? DECODE_BLOCKS : DECODE_HEADER; }