+++ /dev/null
-// InflaterInputStream.cs\r
-// Copyright (C) 2001 Mike Krueger\r
-//\r
-// This file was translated from java, it was part of the GNU Classpath\r
-// Copyright (C) 2001 Free Software Foundation, Inc.\r
-//\r
-// This program is free software; you can redistribute it and/or\r
-// modify it under the terms of the GNU General Public License\r
-// as published by the Free Software Foundation; either version 2\r
-// of the License, or (at your option) any later version.\r
-//\r
-// This program is distributed in the hope that it will be useful,\r
-// but WITHOUT ANY WARRANTY; without even the implied warranty of\r
-// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\r
-// GNU General Public License for more details.\r
-//\r
-// You should have received a copy of the GNU General Public License\r
-// along with this program; if not, write to the Free Software\r
-// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.\r
-//\r
-// Linking this library statically or dynamically with other modules is\r
-// making a combined work based on this library. Thus, the terms and\r
-// conditions of the GNU General Public License cover the whole\r
-// combination.\r
-// \r
-// As a special exception, the copyright holders of this library give you\r
-// permission to link this library with independent modules to produce an\r
-// executable, regardless of the license terms of these independent\r
-// modules, and to copy and distribute the resulting executable under\r
-// terms of your choice, provided that you also meet, for each linked\r
-// independent module, the terms and conditions of the license of that\r
-// module. An independent module is a module which is not derived from\r
-// or based on this library. If you modify this library, you may extend\r
-// this exception to your version of the library, but you are not\r
-// obligated to do so. If you do not wish to do so, delete this\r
-// exception statement from your version.\r
-\r
-using System;\r
-using System.IO;\r
-\r
-using ICSharpCode.SharpZipLib.Zip.Compression;\r
-using ICSharpCode.SharpZipLib.Checksums;\r
-\r
-namespace ICSharpCode.SharpZipLib.Zip.Compression.Streams \r
-{\r
- \r
- /// <summary>\r
- /// This filter stream is used to decompress data compressed baseInputStream the "deflate"\r
- /// format. The "deflate" format is described baseInputStream RFC 1951.\r
- ///\r
- /// This stream may form the basis for other decompression filters, such\r
- /// as the <code>GzipInputStream</code>.\r
- ///\r
- /// author of the original java version : John Leuner\r
- /// </summary>\r
- public class InflaterInputStream : Stream\r
- {\r
- //Variables\r
- \r
- /// <summary>\r
- /// Decompressor for this filter\r
- /// </summary>\r
- protected Inflater inf;\r
- \r
- /// <summary>\r
- /// Byte array used as a buffer\r
- /// </summary>\r
- protected byte[] buf;\r
- \r
- /// <summary>\r
- /// Size of buffer\r
- /// </summary>\r
- protected int len;\r
- \r
- //We just use this if we are decoding one byte at a time with the read() call\r
- private byte[] onebytebuffer = new byte[1];\r
- \r
- /// <summary>\r
- /// base stream the inflater depends on.\r
- /// </summary>\r
- protected Stream baseInputStream;\r
- \r
- protected long csize;\r
- \r
- /// <summary>\r
- /// I needed to implement the abstract member.\r
- /// </summary>\r
- public override bool CanRead {\r
- get {\r
- return baseInputStream.CanRead;\r
- }\r
- }\r
- \r
- /// <summary>\r
- /// I needed to implement the abstract member.\r
- /// </summary>\r
- public override bool CanSeek {\r
- get {\r
- return false;\r
- // return baseInputStream.CanSeek;\r
- }\r
- }\r
- \r
- /// <summary>\r
- /// I needed to implement the abstract member.\r
- /// </summary>\r
- public override bool CanWrite {\r
- get {\r
- return baseInputStream.CanWrite;\r
- }\r
- }\r
- \r
- /// <summary>\r
- /// I needed to implement the abstract member.\r
- /// </summary>\r
- public override long Length {\r
- get {\r
- return len;\r
- }\r
- }\r
- \r
- /// <summary>\r
- /// I needed to implement the abstract member.\r
- /// </summary>\r
- public override long Position {\r
- get {\r
- return baseInputStream.Position;\r
- }\r
- set {\r
- baseInputStream.Position = value;\r
- }\r
- }\r
- \r
- /// <summary>\r
- /// Flushes the baseInputStream\r
- /// </summary>\r
- public override void Flush()\r
- {\r
- baseInputStream.Flush();\r
- }\r
- \r
- /// <summary>\r
- /// I needed to implement the abstract member.\r
- /// </summary>\r
- public override long Seek(long offset, SeekOrigin origin)\r
- {\r
- throw new NotSupportedException("Seek not supported"); // -jr- 01-Dec-2003\r
- }\r
- \r
- /// <summary>\r
- /// I needed to implement the abstract member.\r
- /// </summary>\r
- public override void SetLength(long val)\r
- {\r
- baseInputStream.SetLength(val);\r
- }\r
- \r
- /// <summary>\r
- /// I needed to implement the abstract member.\r
- /// </summary>\r
- public override void Write(byte[] array, int offset, int count)\r
- {\r
- baseInputStream.Write(array, offset, count);\r
- }\r
- \r
- /// <summary>\r
- /// I needed to implement the abstract member.\r
- /// </summary>\r
- public override void WriteByte(byte val)\r
- {\r
- baseInputStream.WriteByte(val);\r
- }\r
- \r
- // -jr- 01-Dec-2003 This may be flawed for some base streams? Depends on implementation of BeginWrite\r
- public override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback callback, object state)\r
- {\r
- throw new NotSupportedException("Asynch write not currently supported");\r
- }\r
- \r
- //Constructors\r
- \r
- /// <summary>\r
- /// Create an InflaterInputStream with the default decompresseor\r
- /// and a default buffer size.\r
- /// </summary>\r
- /// <param name = "baseInputStream">\r
- /// the InputStream to read bytes from\r
- /// </param>\r
- public InflaterInputStream(Stream baseInputStream) : this(baseInputStream, new Inflater(), 4096)\r
- {\r
- \r
- }\r
- \r
- /// <summary>\r
- /// Create an InflaterInputStream with the specified decompresseor\r
- /// and a default buffer size.\r
- /// </summary>\r
- /// <param name = "baseInputStream">\r
- /// the InputStream to read bytes from\r
- /// </param>\r
- /// <param name = "inf">\r
- /// the decompressor used to decompress data read from baseInputStream\r
- /// </param>\r
- public InflaterInputStream(Stream baseInputStream, Inflater inf) : this(baseInputStream, inf, 4096)\r
- {\r
- }\r
- \r
- /// <summary>\r
- /// Create an InflaterInputStream with the specified decompresseor\r
- /// and a specified buffer size.\r
- /// </summary>\r
- /// <param name = "baseInputStream">\r
- /// the InputStream to read bytes from\r
- /// </param>\r
- /// <param name = "inf">\r
- /// the decompressor used to decompress data read from baseInputStream\r
- /// </param>\r
- /// <param name = "size">\r
- /// size of the buffer to use\r
- /// </param>\r
- public InflaterInputStream(Stream baseInputStream, Inflater inf, int size)\r
- {\r
- this.baseInputStream = baseInputStream;\r
- this.inf = inf;\r
- try {\r
- this.len = (int)baseInputStream.Length;\r
- } catch (Exception) {\r
- // the stream may not support .Length\r
- this.len = 0;\r
- }\r
- \r
- if (size <= 0) {\r
- throw new ArgumentOutOfRangeException("size <= 0");\r
- }\r
- \r
- buf = new byte[size]; //Create the buffer\r
- }\r
- \r
- //Methods\r
- \r
- /// <summary>\r
- /// Returns 0 once the end of the stream (EOF) has been reached.\r
- /// Otherwise returns 1.\r
- /// </summary>\r
- public virtual int Available {\r
- get {\r
- return inf.IsFinished ? 0 : 1;\r
- }\r
- }\r
- \r
- /// <summary>\r
- /// Closes the input stream\r
- /// </summary>\r
- public override void Close()\r
- {\r
- baseInputStream.Close();\r
- }\r
- \r
- /// <summary>\r
- /// Fills the buffer with more data to decompress.\r
- /// </summary>\r
- protected void Fill()\r
- {\r
- len = baseInputStream.Read(buf, 0, buf.Length);\r
- // decrypting crypted data\r
- if (cryptbuffer != null) {\r
- DecryptBlock(buf, 0, System.Math.Min((int)(csize - inf.TotalIn), buf.Length));\r
- }\r
- \r
- if (len <= 0) {\r
- throw new ApplicationException("Deflated stream ends early.");\r
- }\r
- inf.SetInput(buf, 0, len);\r
- }\r
- \r
- /// <summary>\r
- /// Reads one byte of decompressed data.\r
- ///\r
- /// The byte is baseInputStream the lower 8 bits of the int.\r
- /// </summary>\r
- public override int ReadByte()\r
- {\r
- int nread = Read(onebytebuffer, 0, 1); //read one byte\r
- if (nread > 0) {\r
- return onebytebuffer[0] & 0xff;\r
- }\r
- return -1; // ok\r
- }\r
- \r
- /// <summary>\r
- /// Decompresses data into the byte array\r
- /// </summary>\r
- /// <param name ="b">\r
- /// the array to read and decompress data into\r
- /// </param>\r
- /// <param name ="off">\r
- /// the offset indicating where the data should be placed\r
- /// </param>\r
- /// <param name ="len">\r
- /// the number of bytes to decompress\r
- /// </param>\r
- public override int Read(byte[] b, int off, int len)\r
- {\r
- for (;;) {\r
- int count;\r
- try {\r
- count = inf.Inflate(b, off, len);\r
- } catch (Exception e) {\r
- throw new ZipException(e.ToString());\r
- }\r
- \r
- if (count > 0) {\r
- return count;\r
- }\r
- \r
- if (inf.IsNeedingDictionary) {\r
- throw new ZipException("Need a dictionary");\r
- } else if (inf.IsFinished) {\r
- return 0;\r
- } else if (inf.IsNeedingInput) {\r
- Fill();\r
- } else {\r
- throw new InvalidOperationException("Don't know what to do");\r
- }\r
- }\r
- }\r
- \r
- /// <summary>\r
- /// Skip specified number of bytes of uncompressed data\r
- /// </summary>\r
- /// <param name ="n">\r
- /// number of bytes to skip\r
- /// </param>\r
- public long Skip(long n)\r
- {\r
- if (n < 0) {\r
- throw new ArgumentOutOfRangeException("n");\r
- }\r
- int len = 2048;\r
- if (n < len) {\r
- len = (int) n;\r
- }\r
- byte[] tmp = new byte[len];\r
- return (long)baseInputStream.Read(tmp, 0, tmp.Length);\r
- }\r
- \r
- #region Encryption stuff\r
- protected byte[] cryptbuffer = null;\r
- \r
- uint[] keys = null;\r
- protected byte DecryptByte()\r
- {\r
- uint temp = ((keys[2] & 0xFFFF) | 2);\r
- return (byte)((temp * (temp ^ 1)) >> 8);\r
- }\r
- \r
- protected void DecryptBlock(byte[] buf, int off, int len)\r
- {\r
- for (int i = off; i < off + len; ++i) {\r
- buf[i] ^= DecryptByte();\r
- UpdateKeys(buf[i]);\r
- }\r
- }\r
- \r
- protected void InitializePassword(string password)\r
- {\r
- keys = new uint[] {\r
- 0x12345678,\r
- 0x23456789,\r
- 0x34567890\r
- };\r
- for (int i = 0; i < password.Length; ++i) {\r
- UpdateKeys((byte)password[i]);\r
- }\r
- }\r
- \r
- protected void UpdateKeys(byte ch)\r
- {\r
- keys[0] = Crc32.ComputeCrc32(keys[0], ch);\r
- keys[1] = keys[1] + (byte)keys[0];\r
- keys[1] = keys[1] * 134775813 + 1;\r
- keys[2] = Crc32.ComputeCrc32(keys[2], (byte)(keys[1] >> 24));\r
- }\r
- #endregion\r
- }\r
-}\r