1 // InflaterInputStream.cs
2 // Copyright (C) 2001 Mike Krueger
4 // This file was translated from java, it was part of the GNU Classpath
5 // Copyright (C) 2001 Free Software Foundation, Inc.
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License
9 // as published by the Free Software Foundation; either version 2
10 // of the License, or (at your option) any later version.
12 // This program is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 // Linking this library statically or dynamically with other modules is
22 // making a combined work based on this library. Thus, the terms and
23 // conditions of the GNU General Public License cover the whole
26 // As a special exception, the copyright holders of this library give you
27 // permission to link this library with independent modules to produce an
28 // executable, regardless of the license terms of these independent
29 // modules, and to copy and distribute the resulting executable under
30 // terms of your choice, provided that you also meet, for each linked
31 // independent module, the terms and conditions of the license of that
32 // module. An independent module is a module which is not derived from
33 // or based on this library. If you modify this library, you may extend
34 // this exception to your version of the library, but you are not
35 // obligated to do so. If you do not wish to do so, delete this
36 // exception statement from your version.
41 using ICSharpCode.SharpZipLib.Zip.Compression;
42 using ICSharpCode.SharpZipLib.Checksums;
44 namespace ICSharpCode.SharpZipLib.Zip.Compression.Streams
48 /// This filter stream is used to decompress data compressed baseInputStream the "deflate"
49 /// format. The "deflate" format is described baseInputStream RFC 1951.
51 /// This stream may form the basis for other decompression filters, such
52 /// as the <code>GzipInputStream</code>.
54 /// author of the original java version : John Leuner
56 public class InflaterInputStream : Stream
61 /// Decompressor for this filter
63 protected Inflater inf;
66 /// Byte array used as a buffer
75 //We just use this if we are decoding one byte at a time with the read() call
76 private byte[] onebytebuffer = new byte[1];
79 /// base stream the inflater depends on.
81 protected Stream baseInputStream;
86 /// I needed to implement the abstract member.
88 public override bool CanRead {
90 return baseInputStream.CanRead;
95 /// I needed to implement the abstract member.
97 public override bool CanSeek {
100 // return baseInputStream.CanSeek;
105 /// I needed to implement the abstract member.
107 public override bool CanWrite {
109 return baseInputStream.CanWrite;
114 /// I needed to implement the abstract member.
116 public override long Length {
123 /// I needed to implement the abstract member.
125 public override long Position {
127 return baseInputStream.Position;
130 baseInputStream.Position = value;
135 /// Flushes the baseInputStream
137 public override void Flush()
139 baseInputStream.Flush();
143 /// I needed to implement the abstract member.
145 public override long Seek(long offset, SeekOrigin origin)
147 throw new NotSupportedException("Seek not supported"); // -jr- 01-Dec-2003
151 /// I needed to implement the abstract member.
153 public override void SetLength(long val)
155 baseInputStream.SetLength(val);
159 /// I needed to implement the abstract member.
161 public override void Write(byte[] array, int offset, int count)
163 baseInputStream.Write(array, offset, count);
167 /// I needed to implement the abstract member.
169 public override void WriteByte(byte val)
171 baseInputStream.WriteByte(val);
174 // -jr- 01-Dec-2003 This may be flawed for some base streams? Depends on implementation of BeginWrite
175 public override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback callback, object state)
177 throw new NotSupportedException("Asynch write not currently supported");
183 /// Create an InflaterInputStream with the default decompresseor
184 /// and a default buffer size.
186 /// <param name = "baseInputStream">
187 /// the InputStream to read bytes from
189 public InflaterInputStream(Stream baseInputStream) : this(baseInputStream, new Inflater(), 4096)
195 /// Create an InflaterInputStream with the specified decompresseor
196 /// and a default buffer size.
198 /// <param name = "baseInputStream">
199 /// the InputStream to read bytes from
201 /// <param name = "inf">
202 /// the decompressor used to decompress data read from baseInputStream
204 public InflaterInputStream(Stream baseInputStream, Inflater inf) : this(baseInputStream, inf, 4096)
209 /// Create an InflaterInputStream with the specified decompresseor
210 /// and a specified buffer size.
212 /// <param name = "baseInputStream">
213 /// the InputStream to read bytes from
215 /// <param name = "inf">
216 /// the decompressor used to decompress data read from baseInputStream
218 /// <param name = "size">
219 /// size of the buffer to use
221 public InflaterInputStream(Stream baseInputStream, Inflater inf, int size)
223 this.baseInputStream = baseInputStream;
226 this.len = (int)baseInputStream.Length;
227 } catch (Exception) {
228 // the stream may not support .Length
233 throw new ArgumentOutOfRangeException("size <= 0");
236 buf = new byte[size]; //Create the buffer
242 /// Returns 0 once the end of the stream (EOF) has been reached.
243 /// Otherwise returns 1.
245 public virtual int Available {
247 return inf.IsFinished ? 0 : 1;
252 /// Closes the input stream
254 public override void Close()
256 baseInputStream.Close();
260 /// Fills the buffer with more data to decompress.
262 protected void Fill()
264 len = baseInputStream.Read(buf, 0, buf.Length);
265 // decrypting crypted data
266 if (cryptbuffer != null) {
267 DecryptBlock(buf, 0, System.Math.Min((int)(csize - inf.TotalIn), buf.Length));
271 throw new ApplicationException("Deflated stream ends early.");
273 inf.SetInput(buf, 0, len);
277 /// Reads one byte of decompressed data.
279 /// The byte is baseInputStream the lower 8 bits of the int.
281 public override int ReadByte()
283 int nread = Read(onebytebuffer, 0, 1); //read one byte
285 return onebytebuffer[0] & 0xff;
291 /// Decompresses data into the byte array
293 /// <param name ="b">
294 /// the array to read and decompress data into
296 /// <param name ="off">
297 /// the offset indicating where the data should be placed
299 /// <param name ="len">
300 /// the number of bytes to decompress
302 public override int Read(byte[] b, int off, int len)
307 count = inf.Inflate(b, off, len);
308 } catch (Exception e) {
309 throw new ZipException(e.ToString());
316 if (inf.IsNeedingDictionary) {
317 throw new ZipException("Need a dictionary");
318 } else if (inf.IsFinished) {
320 } else if (inf.IsNeedingInput) {
323 throw new InvalidOperationException("Don't know what to do");
329 /// Skip specified number of bytes of uncompressed data
331 /// <param name ="n">
332 /// number of bytes to skip
334 public long Skip(long n)
337 throw new ArgumentOutOfRangeException("n");
343 byte[] tmp = new byte[len];
344 return (long)baseInputStream.Read(tmp, 0, tmp.Length);
347 #region Encryption stuff
348 protected byte[] cryptbuffer = null;
351 protected byte DecryptByte()
353 uint temp = ((keys[2] & 0xFFFF) | 2);
354 return (byte)((temp * (temp ^ 1)) >> 8);
357 protected void DecryptBlock(byte[] buf, int off, int len)
359 for (int i = off; i < off + len; ++i) {
360 buf[i] ^= DecryptByte();
365 protected void InitializePassword(string password)
372 for (int i = 0; i < password.Length; ++i) {
373 UpdateKeys((byte)password[i]);
377 protected void UpdateKeys(byte ch)
379 keys[0] = Crc32.ComputeCrc32(keys[0], ch);
380 keys[1] = keys[1] + (byte)keys[0];
381 keys[1] = keys[1] * 134775813 + 1;
382 keys[2] = Crc32.ComputeCrc32(keys[2], (byte)(keys[1] >> 24));