1 // DeflaterOutputStream.cs
\r
3 // Copyright (C) 2001 Mike Krueger
\r
5 // This file was translated from java, it was part of the GNU Classpath
\r
6 // Copyright (C) 2001 Free Software Foundation, Inc.
\r
8 // This program is free software; you can redistribute it and/or
\r
9 // modify it under the terms of the GNU General Public License
\r
10 // as published by the Free Software Foundation; either version 2
\r
11 // of the License, or (at your option) any later version.
\r
13 // This program is distributed in the hope that it will be useful,
\r
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
16 // GNU General Public License for more details.
\r
18 // You should have received a copy of the GNU General Public License
\r
19 // along with this program; if not, write to the Free Software
\r
20 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
\r
22 // Linking this library statically or dynamically with other modules is
\r
23 // making a combined work based on this library. Thus, the terms and
\r
24 // conditions of the GNU General Public License cover the whole
\r
27 // As a special exception, the copyright holders of this library give you
\r
28 // permission to link this library with independent modules to produce an
\r
29 // executable, regardless of the license terms of these independent
\r
30 // modules, and to copy and distribute the resulting executable under
\r
31 // terms of your choice, provided that you also meet, for each linked
\r
32 // independent module, the terms and conditions of the license of that
\r
33 // module. An independent module is a module which is not derived from
\r
34 // or based on this library. If you modify this library, you may extend
\r
35 // this exception to your version of the library, but you are not
\r
36 // obligated to do so. If you do not wish to do so, delete this
\r
37 // exception statement from your version.
\r
41 using ICSharpCode.SharpZipLib.Checksums;
\r
42 using ICSharpCode.SharpZipLib.Zip.Compression;
\r
44 namespace ICSharpCode.SharpZipLib.Zip.Compression.Streams
\r
48 /// A special stream deflating or compressing the bytes that are
\r
49 /// written to it. It uses a Deflater to perform actual deflating.<br/>
\r
50 /// Authors of the original java version : Tom Tromey, Jochen Hoenicke
\r
52 public class DeflaterOutputStream : Stream
\r
55 /// This buffer is used temporarily to retrieve the bytes from the
\r
56 /// deflater and write them to the underlying output stream.
\r
58 protected byte[] buf;
\r
61 /// The deflater which is used to deflate the stream.
\r
63 protected Deflater def;
\r
66 /// Base stream the deflater depends on.
\r
68 protected Stream baseOutputStream;
\r
70 bool isClosed = false;
\r
71 bool isStreamOwner = true;
\r
74 /// Get/set flag indicating ownership of underlying stream.
\r
75 /// When the flag is true <see cref="Close"></see> will close the underlying stream also.
\r
77 public bool IsStreamOwner
\r
79 get { return isStreamOwner; }
\r
80 set { isStreamOwner = value; }
\r
84 /// Allows client to determine if an entry can be patched after its added
\r
86 public bool CanPatchEntries {
\r
88 return baseOutputStream.CanSeek;
\r
93 /// Gets value indicating stream can be read from
\r
95 public override bool CanRead {
\r
97 return baseOutputStream.CanRead;
\r
102 /// Gets a value indicating if seeking is supported for this stream
\r
103 /// This property always returns false
\r
105 public override bool CanSeek {
\r
112 /// Get value indicating if this stream supports writing
\r
114 public override bool CanWrite {
\r
116 return baseOutputStream.CanWrite;
\r
121 /// Get current length of stream
\r
123 public override long Length {
\r
125 return baseOutputStream.Length;
\r
130 /// The current position within the stream.
\r
131 /// Always throws a NotSupportedExceptionNotSupportedException
\r
133 /// <exception cref="NotSupportedException">Any attempt to set position</exception>
\r
134 public override long Position {
\r
136 return baseOutputStream.Position;
\r
139 throw new NotSupportedException("DefalterOutputStream Position not supported");
\r
144 /// Sets the current position of this stream to the given value. Not supported by this class!
\r
146 /// <exception cref="NotSupportedException">Any access</exception>
\r
147 public override long Seek(long offset, SeekOrigin origin)
\r
149 throw new NotSupportedException("DeflaterOutputStream Seek not supported");
\r
153 /// Sets the length of this stream to the given value. Not supported by this class!
\r
155 /// <exception cref="NotSupportedException">Any access</exception>
\r
156 public override void SetLength(long val)
\r
158 throw new NotSupportedException("DeflaterOutputStream SetLength not supported");
\r
162 /// Read a byte from stream advancing position by one
\r
164 /// <exception cref="NotSupportedException">Any access</exception>
\r
165 public override int ReadByte()
\r
167 throw new NotSupportedException("DeflaterOutputStream ReadByte not supported");
\r
171 /// Read a block of bytes from stream
\r
173 /// <exception cref="NotSupportedException">Any access</exception>
\r
174 public override int Read(byte[] b, int off, int len)
\r
176 throw new NotSupportedException("DeflaterOutputStream Read not supported");
\r
180 /// Asynchronous reads are not supported a NotSupportedException is always thrown
\r
182 /// <param name="buffer"></param>
\r
183 /// <param name="offset"></param>
\r
184 /// <param name="count"></param>
\r
185 /// <param name="callback"></param>
\r
186 /// <param name="state"></param>
\r
187 /// <returns></returns>
\r
188 /// <exception cref="NotSupportedException">Any access</exception>
\r
189 public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback callback, object state)
\r
191 throw new NotSupportedException("DeflaterOutputStream BeginRead not currently supported");
\r
195 /// Asynchronous writes arent supported, a NotSupportedException is always thrown
\r
197 /// <param name="buffer"></param>
\r
198 /// <param name="offset"></param>
\r
199 /// <param name="count"></param>
\r
200 /// <param name="callback"></param>
\r
201 /// <param name="state"></param>
\r
202 /// <returns></returns>
\r
203 /// <exception cref="NotSupportedException">Any access</exception>
\r
204 public override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback callback, object state)
\r
206 throw new NotSupportedException("DeflaterOutputStream BeginWrite not currently supported");
\r
210 /// Deflates everything in the input buffers. This will call
\r
211 /// <code>def.deflate()</code> until all bytes from the input buffers
\r
214 protected void Deflate()
\r
216 while (!def.IsNeedingInput) {
\r
217 int len = def.Deflate(buf, 0, buf.Length);
\r
223 if (this.keys != null) {
\r
224 this.EncryptBlock(buf, 0, len);
\r
227 baseOutputStream.Write(buf, 0, len);
\r
230 if (!def.IsNeedingInput) {
\r
231 throw new SharpZipBaseException("DeflaterOutputStream can't deflate all input?");
\r
236 /// Creates a new DeflaterOutputStream with a default Deflater and default buffer size.
\r
238 /// <param name="baseOutputStream">
\r
239 /// the output stream where deflated output should be written.
\r
241 public DeflaterOutputStream(Stream baseOutputStream) : this(baseOutputStream, new Deflater(), 512)
\r
246 /// Creates a new DeflaterOutputStream with the given Deflater and
\r
247 /// default buffer size.
\r
249 /// <param name="baseOutputStream">
\r
250 /// the output stream where deflated output should be written.
\r
252 /// <param name="defl">
\r
253 /// the underlying deflater.
\r
255 public DeflaterOutputStream(Stream baseOutputStream, Deflater defl) : this(baseOutputStream, defl, 512)
\r
260 /// Creates a new DeflaterOutputStream with the given Deflater and
\r
263 /// <param name="baseOutputStream">
\r
264 /// The output stream where deflated output is written.
\r
266 /// <param name="deflater">
\r
267 /// The underlying deflater to use
\r
269 /// <param name="bufsize">
\r
270 /// The buffer size to use when deflating
\r
272 /// <exception cref="ArgumentOutOfRangeException">
\r
273 /// bufsize is less than or equal to zero.
\r
275 /// <exception cref="ArgumentException">
\r
276 /// baseOutputStream does not support writing
\r
278 /// <exception cref="ArgumentNullException">
\r
279 /// deflater instance is null
\r
281 public DeflaterOutputStream(Stream baseOutputStream, Deflater deflater, int bufsize)
\r
283 if (baseOutputStream.CanWrite == false) {
\r
284 throw new ArgumentException("baseOutputStream", "must support writing");
\r
287 if (deflater == null) {
\r
288 throw new ArgumentNullException("deflater");
\r
291 if (bufsize <= 0) {
\r
292 throw new ArgumentOutOfRangeException("bufsize");
\r
295 this.baseOutputStream = baseOutputStream;
\r
296 buf = new byte[bufsize];
\r
301 /// Flushes the stream by calling flush() on the deflater and then
\r
302 /// on the underlying stream. This ensures that all bytes are
\r
305 public override void Flush()
\r
309 baseOutputStream.Flush();
\r
313 /// Finishes the stream by calling finish() on the deflater.
\r
315 /// <exception cref="SharpZipBaseException">
\r
316 /// Not all input is deflated
\r
318 public virtual void Finish()
\r
321 while (!def.IsFinished) {
\r
322 int len = def.Deflate(buf, 0, buf.Length);
\r
327 if (this.keys != null) {
\r
328 this.EncryptBlock(buf, 0, len);
\r
331 baseOutputStream.Write(buf, 0, len);
\r
333 if (!def.IsFinished) {
\r
334 throw new SharpZipBaseException("Can't deflate all input?");
\r
336 baseOutputStream.Flush();
\r
341 /// Calls finish() and closes the underlying
\r
342 /// stream when <see cref="IsStreamOwner"></see> is true.
\r
344 public override void Close()
\r
349 if ( isStreamOwner ) {
\r
350 baseOutputStream.Close();
\r
356 /// Writes a single byte to the compressed output stream.
\r
358 /// <param name="bval">
\r
359 /// The byte value.
\r
361 public override void WriteByte(byte bval)
\r
363 byte[] b = new byte[1];
\r
369 /// Writes bytes from an array to the compressed stream.
\r
371 /// <param name="buf">
\r
374 /// <param name="off">
\r
375 /// The offset into the byte array where to start.
\r
377 /// <param name="len">
\r
378 /// The number of bytes to write.
\r
380 public override void Write(byte[] buf, int off, int len)
\r
382 def.SetInput(buf, off, len);
\r
388 // TODO: Refactor this code. The presence of Zip specific code in this low level class is wrong
\r
389 string password = null;
\r
390 uint[] keys = null;
\r
393 /// Get/set the password used for encryption. When null no encryption is performed
\r
395 public string Password {
\r
400 if ( value != null && value.Length == 0 ) {
\r
410 /// Encrypt a single byte
\r
413 /// The encrypted value
\r
415 protected byte EncryptByte()
\r
417 uint temp = ((keys[2] & 0xFFFF) | 2);
\r
418 return (byte)((temp * (temp ^ 1)) >> 8);
\r
423 /// Encrypt a block of data
\r
425 /// <param name="buffer">
\r
426 /// Data to encrypt. NOTE the original contents of the buffer are lost
\r
428 /// <param name="offset">
\r
429 /// Offset of first byte in buffer to encrypt
\r
431 /// <param name="length">
\r
432 /// Number of bytes in buffer to encrypt
\r
434 protected void EncryptBlock(byte[] buffer, int offset, int length)
\r
436 // TODO: refactor to use crypto transform
\r
437 for (int i = offset; i < offset + length; ++i) {
\r
438 byte oldbyte = buffer[i];
\r
439 buffer[i] ^= EncryptByte();
\r
440 UpdateKeys(oldbyte);
\r
445 /// Initializes encryption keys based on given password
\r
447 protected void InitializePassword(string password) {
\r
448 keys = new uint[] {
\r
454 for (int i = 0; i < password.Length; ++i) {
\r
455 UpdateKeys((byte)password[i]);
\r
460 /// Update encryption keys
\r
462 protected void UpdateKeys(byte ch)
\r
464 keys[0] = Crc32.ComputeCrc32(keys[0], ch);
\r
465 keys[1] = keys[1] + (byte)keys[0];
\r
466 keys[1] = keys[1] * 134775813 + 1;
\r
467 keys[2] = Crc32.ComputeCrc32(keys[2], (byte)(keys[1] >> 24));
\r