2 // ------------------------------------------------------------------
4 // Copyright (c) 2009 Dino Chiesa and Microsoft Corporation.
5 // All rights reserved.
7 // This code module is part of DotNetZip, a zipfile class library.
9 // ------------------------------------------------------------------
11 // This code is licensed under the Microsoft Public License.
12 // See the file License.txt for the license details.
13 // More info on: http://dotnetzip.codeplex.com
15 // ------------------------------------------------------------------
17 // last saved (in emacs):
18 // Time-stamp: <2009-November-03 15:40:51>
20 // ------------------------------------------------------------------
22 // This module defines a Codec for ZLIB compression and
23 // decompression. This code extends code that was based the jzlib
24 // implementation of zlib, but this code is completely novel. The codec
25 // class is new, and encapsulates some behaviors that are new, and some
26 // that were present in other classes in the jzlib code base. In
27 // keeping with the license for jzlib, the copyright to the jzlib code
30 // ------------------------------------------------------------------
32 // Copyright (c) 2000,2001,2002,2003 ymnk, JCraft,Inc. All rights reserved.
34 // Redistribution and use in source and binary forms, with or without
35 // modification, are permitted provided that the following conditions are met:
37 // 1. Redistributions of source code must retain the above copyright notice,
38 // this list of conditions and the following disclaimer.
40 // 2. Redistributions in binary form must reproduce the above copyright
41 // notice, this list of conditions and the following disclaimer in
42 // the documentation and/or other materials provided with the distribution.
44 // 3. The names of the authors may not be used to endorse or promote products
45 // derived from this software without specific prior written permission.
47 // THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
48 // INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
49 // FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
50 // INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
51 // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
52 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
53 // OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
54 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
55 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
56 // EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
58 // -----------------------------------------------------------------------
60 // This program is based on zlib-1.1.3; credit to authors
61 // Jean-loup Gailly(jloup@gzip.org) and Mark Adler(madler@alumni.caltech.edu)
62 // and contributors of zlib.
64 // -----------------------------------------------------------------------
68 using Interop=System.Runtime.InteropServices;
73 /// Encoder and Decoder for ZLIB and DEFLATE (IETF RFC1950 and RFC1951).
77 /// This class compresses and decompresses data according to the Deflate algorithm
78 /// and optionally, the ZLIB format, as documented in <see
79 /// href="http://www.ietf.org/rfc/rfc1950.txt">RFC 1950 - ZLIB</see> and <see
80 /// href="http://www.ietf.org/rfc/rfc1951.txt">RFC 1951 - DEFLATE</see>.
82 [Interop.GuidAttribute("ebc25cf6-9120-4283-b972-0e5520d0000D")]
83 [Interop.ComVisible(true)]
85 [Interop.ClassInterface(Interop.ClassInterfaceType.AutoDispatch)]
87 sealed internal class ZlibCodec
90 /// The buffer from which data is taken.
92 public byte[] InputBuffer;
95 /// An index into the InputBuffer array, indicating where to start reading.
100 /// The number of bytes available in the InputBuffer, starting at NextIn.
103 /// Generally you should set this to InputBuffer.Length before the first Inflate() or Deflate() call.
104 /// The class will update this number as calls to Inflate/Deflate are made.
106 public int AvailableBytesIn;
109 /// Total number of bytes read so far, through all calls to Inflate()/Deflate().
111 public long TotalBytesIn;
114 /// Buffer to store output data.
116 public byte[] OutputBuffer;
119 /// An index into the OutputBuffer array, indicating where to start writing.
124 /// The number of bytes available in the OutputBuffer, starting at NextOut.
127 /// Generally you should set this to OutputBuffer.Length before the first Inflate() or Deflate() call.
128 /// The class will update this number as calls to Inflate/Deflate are made.
130 public int AvailableBytesOut;
133 /// Total number of bytes written to the output so far, through all calls to Inflate()/Deflate().
135 public long TotalBytesOut;
138 /// used for diagnostics, when something goes wrong!
140 public System.String Message;
142 internal DeflateManager dstate;
143 internal InflateManager istate;
145 internal uint _Adler32;
148 /// The compression level to use in this codec. Useful only in compression mode.
150 public CompressionLevel CompressLevel = CompressionLevel.Default;
153 /// The number of Window Bits to use.
156 /// This gauges the size of the sliding window, and hence the
157 /// compression effectiveness as well as memory consumption. It's best to just leave this
158 /// setting alone if you don't know what it is. The maximum value is 15 bits, which implies
161 public int WindowBits = ZlibConstants.WindowBitsDefault;
164 /// The compression strategy to use.
167 /// This is only effective in compression. The theory offered by ZLIB is that different
168 /// strategies could potentially produce significant differences in compression behavior
169 /// for different data sets. Unfortunately I don't have any good recommendations for how
170 /// to set it differently. When I tested changing the strategy I got minimally different
171 /// compression performance. It's best to leave this property alone if you don't have a
172 /// good feel for it. Or, you may want to produce a test harness that runs through the
173 /// different strategy options and evaluates them on different file types. If you do that,
174 /// let me know your results.
176 public CompressionStrategy Strategy = CompressionStrategy.Default;
180 /// The Adler32 checksum on the data transferred through the codec so far. You probably don't need to look at this.
182 public int Adler32 { get { return (int)_Adler32; } }
186 /// Create a ZlibCodec.
189 /// If you use this default constructor, you will later have to explicitly call
190 /// InitializeInflate() or InitializeDeflate() before using the ZlibCodec to compress
193 public ZlibCodec() { }
196 /// Create a ZlibCodec that either compresses or decompresses.
198 /// <param name="mode">
199 /// Indicates whether the codec should compress (deflate) or decompress (inflate).
201 public ZlibCodec(CompressionMode mode)
203 if (mode == CompressionMode.Compress)
205 int rc = InitializeDeflate();
206 if (rc != ZlibConstants.Z_OK) throw new ZlibException("Cannot initialize for deflate.");
208 else if (mode == CompressionMode.Decompress)
210 int rc = InitializeInflate();
211 if (rc != ZlibConstants.Z_OK) throw new ZlibException("Cannot initialize for inflate.");
213 else throw new ZlibException("Invalid ZlibStreamFlavor.");
217 /// Initialize the inflation state.
220 /// It is not necessary to call this before using the ZlibCodec to inflate data;
221 /// It is implicitly called when you call the constructor.
223 /// <returns>Z_OK if everything goes well.</returns>
224 public int InitializeInflate()
226 return InitializeInflate(this.WindowBits);
230 /// Initialize the inflation state with an explicit flag to
231 /// govern the handling of RFC1950 header bytes.
235 /// By default, the ZLIB header defined in <see
236 /// href="http://www.ietf.org/rfc/rfc1950.txt">RFC 1950</see> is expected. If
237 /// you want to read a zlib stream you should specify true for
238 /// expectRfc1950Header. If you have a deflate stream, you will want to specify
239 /// false. It is only necessary to invoke this initializer explicitly if you
240 /// want to specify false.
243 /// <param name="expectRfc1950Header">whether to expect an RFC1950 header byte
244 /// pair when reading the stream of data to be inflated.</param>
246 /// <returns>Z_OK if everything goes well.</returns>
247 public int InitializeInflate(bool expectRfc1950Header)
249 return InitializeInflate(this.WindowBits, expectRfc1950Header);
253 /// Initialize the ZlibCodec for inflation, with the specified number of window bits.
255 /// <param name="windowBits">The number of window bits to use. If you need to ask what that is,
256 /// then you shouldn't be calling this initializer.</param>
257 /// <returns>Z_OK if all goes well.</returns>
258 public int InitializeInflate(int windowBits)
260 this.WindowBits = windowBits;
261 return InitializeInflate(windowBits, true);
265 /// Initialize the inflation state with an explicit flag to govern the handling of
266 /// RFC1950 header bytes.
270 /// If you want to read a zlib stream you should specify true for
271 /// expectRfc1950Header. In this case, the library will expect to find a ZLIB
272 /// header, as defined in <see href="http://www.ietf.org/rfc/rfc1950.txt">RFC
273 /// 1950</see>, in the compressed stream. If you will be reading a DEFLATE or
274 /// GZIP stream, which does not have such a header, you will want to specify
278 /// <param name="expectRfc1950Header">whether to expect an RFC1950 header byte pair when reading
279 /// the stream of data to be inflated.</param>
280 /// <param name="windowBits">The number of window bits to use. If you need to ask what that is,
281 /// then you shouldn't be calling this initializer.</param>
282 /// <returns>Z_OK if everything goes well.</returns>
283 public int InitializeInflate(int windowBits, bool expectRfc1950Header)
285 this.WindowBits = windowBits;
286 if (dstate != null) throw new ZlibException("You may not call InitializeInflate() after calling InitializeDeflate().");
287 istate = new InflateManager(expectRfc1950Header);
288 return istate.Initialize(this, windowBits);
292 /// Inflate the data in the InputBuffer, placing the result in the OutputBuffer.
295 /// You must have set InputBuffer and OutputBuffer, NextIn and NextOut, and AvailableBytesIn and
296 /// AvailableBytesOut before calling this method.
300 /// private void InflateBuffer()
302 /// int bufferSize = 1024;
303 /// byte[] buffer = new byte[bufferSize];
304 /// ZlibCodec decompressor = new ZlibCodec();
306 /// Console.WriteLine("\n============================================");
307 /// Console.WriteLine("Size of Buffer to Inflate: {0} bytes.", CompressedBytes.Length);
308 /// MemoryStream ms = new MemoryStream(DecompressedBytes);
310 /// int rc = decompressor.InitializeInflate();
312 /// decompressor.InputBuffer = CompressedBytes;
313 /// decompressor.NextIn = 0;
314 /// decompressor.AvailableBytesIn = CompressedBytes.Length;
316 /// decompressor.OutputBuffer = buffer;
318 /// // pass 1: inflate
321 /// decompressor.NextOut = 0;
322 /// decompressor.AvailableBytesOut = buffer.Length;
323 /// rc = decompressor.Inflate(FlushType.None);
325 /// if (rc != ZlibConstants.Z_OK && rc != ZlibConstants.Z_STREAM_END)
326 /// throw new Exception("inflating: " + decompressor.Message);
328 /// ms.Write(decompressor.OutputBuffer, 0, buffer.Length - decompressor.AvailableBytesOut);
330 /// while (decompressor.AvailableBytesIn > 0 || decompressor.AvailableBytesOut == 0);
332 /// // pass 2: finish and flush
335 /// decompressor.NextOut = 0;
336 /// decompressor.AvailableBytesOut = buffer.Length;
337 /// rc = decompressor.Inflate(FlushType.Finish);
339 /// if (rc != ZlibConstants.Z_STREAM_END && rc != ZlibConstants.Z_OK)
340 /// throw new Exception("inflating: " + decompressor.Message);
342 /// if (buffer.Length - decompressor.AvailableBytesOut > 0)
343 /// ms.Write(buffer, 0, buffer.Length - decompressor.AvailableBytesOut);
345 /// while (decompressor.AvailableBytesIn > 0 || decompressor.AvailableBytesOut == 0);
347 /// decompressor.EndInflate();
352 /// <param name="flush">The flush to use when inflating.</param>
353 /// <returns>Z_OK if everything goes well.</returns>
354 public int Inflate(FlushType flush)
357 throw new ZlibException("No Inflate State!");
358 return istate.Inflate(flush);
363 /// Ends an inflation session.
366 /// Call this after successively calling Inflate(). This will cause all buffers to be flushed.
367 /// After calling this you cannot call Inflate() without a intervening call to one of the
368 /// InitializeInflate() overloads.
370 /// <returns>Z_OK if everything goes well.</returns>
371 public int EndInflate()
374 throw new ZlibException("No Inflate State!");
375 int ret = istate.End();
381 /// I don't know what this does!
383 /// <returns>Z_OK if everything goes well.</returns>
384 public int SyncInflate()
387 throw new ZlibException("No Inflate State!");
388 return istate.Sync();
392 /// Initialize the ZlibCodec for deflation operation.
395 /// The codec will use the MAX window bits and the default level of compression.
399 /// int bufferSize = 40000;
400 /// byte[] CompressedBytes = new byte[bufferSize];
401 /// byte[] DecompressedBytes = new byte[bufferSize];
403 /// ZlibCodec compressor = new ZlibCodec();
405 /// compressor.InitializeDeflate(CompressionLevel.Default);
407 /// compressor.InputBuffer = System.Text.ASCIIEncoding.ASCII.GetBytes(TextToCompress);
408 /// compressor.NextIn = 0;
409 /// compressor.AvailableBytesIn = compressor.InputBuffer.Length;
411 /// compressor.OutputBuffer = CompressedBytes;
412 /// compressor.NextOut = 0;
413 /// compressor.AvailableBytesOut = CompressedBytes.Length;
415 /// while (compressor.TotalBytesIn != TextToCompress.Length && compressor.TotalBytesOut < bufferSize)
417 /// compressor.Deflate(FlushType.None);
422 /// int rc= compressor.Deflate(FlushType.Finish);
423 /// if (rc == ZlibConstants.Z_STREAM_END) break;
426 /// compressor.EndDeflate();
430 /// <returns>Z_OK if all goes well. You generally don't need to check the return code.</returns>
431 public int InitializeDeflate()
433 return _InternalInitializeDeflate(true);
437 /// Initialize the ZlibCodec for deflation operation, using the specified CompressionLevel.
440 /// The codec will use the maximum window bits (15) and the specified
441 /// CompressionLevel. It will emit a ZLIB stream as it compresses.
443 /// <param name="level">The compression level for the codec.</param>
444 /// <returns>Z_OK if all goes well.</returns>
445 public int InitializeDeflate(CompressionLevel level)
447 this.CompressLevel = level;
448 return _InternalInitializeDeflate(true);
453 /// Initialize the ZlibCodec for deflation operation, using the specified CompressionLevel,
454 /// and the explicit flag governing whether to emit an RFC1950 header byte pair.
457 /// The codec will use the maximum window bits (15) and the specified CompressionLevel.
458 /// If you want to generate a zlib stream, you should specify true for
459 /// wantRfc1950Header. In this case, the library will emit a ZLIB
460 /// header, as defined in <see href="http://www.ietf.org/rfc/rfc1950.txt">RFC
461 /// 1950</see>, in the compressed stream.
463 /// <param name="level">The compression level for the codec.</param>
464 /// <param name="wantRfc1950Header">whether to emit an initial RFC1950 byte pair in the compressed stream.</param>
465 /// <returns>Z_OK if all goes well.</returns>
466 public int InitializeDeflate(CompressionLevel level, bool wantRfc1950Header)
468 this.CompressLevel = level;
469 return _InternalInitializeDeflate(wantRfc1950Header);
474 /// Initialize the ZlibCodec for deflation operation, using the specified CompressionLevel,
475 /// and the specified number of window bits.
478 /// The codec will use the specified number of window bits and the specified CompressionLevel.
480 /// <param name="level">The compression level for the codec.</param>
481 /// <param name="bits">the number of window bits to use. If you don't know what this means, don't use this method.</param>
482 /// <returns>Z_OK if all goes well.</returns>
483 public int InitializeDeflate(CompressionLevel level, int bits)
485 this.CompressLevel = level;
486 this.WindowBits = bits;
487 return _InternalInitializeDeflate(true);
491 /// Initialize the ZlibCodec for deflation operation, using the specified
492 /// CompressionLevel, the specified number of window bits, and the explicit flag
493 /// governing whether to emit an RFC1950 header byte pair.
496 /// <param name="level">The compression level for the codec.</param>
497 /// <param name="wantRfc1950Header">whether to emit an initial RFC1950 byte pair in the compressed stream.</param>
498 /// <param name="bits">the number of window bits to use. If you don't know what this means, don't use this method.</param>
499 /// <returns>Z_OK if all goes well.</returns>
500 public int InitializeDeflate(CompressionLevel level, int bits, bool wantRfc1950Header)
502 this.CompressLevel = level;
503 this.WindowBits = bits;
504 return _InternalInitializeDeflate(wantRfc1950Header);
507 private int _InternalInitializeDeflate(bool wantRfc1950Header)
509 if (istate != null) throw new ZlibException("You may not call InitializeDeflate() after calling InitializeInflate().");
510 dstate = new DeflateManager();
511 dstate.WantRfc1950HeaderBytes = wantRfc1950Header;
513 return dstate.Initialize(this, this.CompressLevel, this.WindowBits, this.Strategy);
517 /// Deflate one batch of data.
520 /// You must have set InputBuffer and OutputBuffer before calling this method.
524 /// private void DeflateBuffer(CompressionLevel level)
526 /// int bufferSize = 1024;
527 /// byte[] buffer = new byte[bufferSize];
528 /// ZlibCodec compressor = new ZlibCodec();
530 /// Console.WriteLine("\n============================================");
531 /// Console.WriteLine("Size of Buffer to Deflate: {0} bytes.", UncompressedBytes.Length);
532 /// MemoryStream ms = new MemoryStream();
534 /// int rc = compressor.InitializeDeflate(level);
536 /// compressor.InputBuffer = UncompressedBytes;
537 /// compressor.NextIn = 0;
538 /// compressor.AvailableBytesIn = UncompressedBytes.Length;
540 /// compressor.OutputBuffer = buffer;
542 /// // pass 1: deflate
545 /// compressor.NextOut = 0;
546 /// compressor.AvailableBytesOut = buffer.Length;
547 /// rc = compressor.Deflate(FlushType.None);
549 /// if (rc != ZlibConstants.Z_OK && rc != ZlibConstants.Z_STREAM_END)
550 /// throw new Exception("deflating: " + compressor.Message);
552 /// ms.Write(compressor.OutputBuffer, 0, buffer.Length - compressor.AvailableBytesOut);
554 /// while (compressor.AvailableBytesIn > 0 || compressor.AvailableBytesOut == 0);
556 /// // pass 2: finish and flush
559 /// compressor.NextOut = 0;
560 /// compressor.AvailableBytesOut = buffer.Length;
561 /// rc = compressor.Deflate(FlushType.Finish);
563 /// if (rc != ZlibConstants.Z_STREAM_END && rc != ZlibConstants.Z_OK)
564 /// throw new Exception("deflating: " + compressor.Message);
566 /// if (buffer.Length - compressor.AvailableBytesOut > 0)
567 /// ms.Write(buffer, 0, buffer.Length - compressor.AvailableBytesOut);
569 /// while (compressor.AvailableBytesIn > 0 || compressor.AvailableBytesOut == 0);
571 /// compressor.EndDeflate();
573 /// ms.Seek(0, SeekOrigin.Begin);
574 /// CompressedBytes = new byte[compressor.TotalBytesOut];
575 /// ms.Read(CompressedBytes, 0, CompressedBytes.Length);
579 /// <param name="flush">whether to flush all data as you deflate. Generally you will want to
580 /// use Z_NO_FLUSH here, in a series of calls to Deflate(), and then call EndDeflate() to
581 /// flush everything.
583 /// <returns>Z_OK if all goes well.</returns>
584 public int Deflate(FlushType flush)
587 throw new ZlibException("No Deflate State!");
588 return dstate.Deflate(flush);
592 /// End a deflation session.
595 /// Call this after making a series of one or more calls to Deflate(). All buffers are flushed.
597 /// <returns>Z_OK if all goes well.</returns>
598 public int EndDeflate()
601 throw new ZlibException("No Deflate State!");
602 // TODO: dinoch Tue, 03 Nov 2009 15:39 (test this)
603 //int ret = dstate.End();
605 return ZlibConstants.Z_OK; //ret;
609 /// Reset a codec for another deflation session.
612 /// Call this to reset the deflation state. For example if a thread is deflating
613 /// non-consecutive blocks, you can call Reset() after the Deflate(Sync) of the first
614 /// block and before the next Deflate(None) of the second block.
616 /// <returns>Z_OK if all goes well.</returns>
617 public void ResetDeflate()
620 throw new ZlibException("No Deflate State!");
626 /// Set the CompressionStrategy and CompressionLevel for a deflation session.
628 /// <param name="level">the level of compression to use.</param>
629 /// <param name="strategy">the strategy to use for compression.</param>
630 /// <returns>Z_OK if all goes well.</returns>
631 public int SetDeflateParams(CompressionLevel level, CompressionStrategy strategy)
634 throw new ZlibException("No Deflate State!");
635 return dstate.SetParams(level, strategy);
640 /// Set the dictionary to be used for either Inflation or Deflation.
642 /// <param name="dictionary">The dictionary bytes to use.</param>
643 /// <returns>Z_OK if all goes well.</returns>
644 public int SetDictionary(byte[] dictionary)
647 return istate.SetDictionary(dictionary);
650 return dstate.SetDictionary(dictionary);
652 throw new ZlibException("No Inflate or Deflate state!");
655 // Flush as much pending output as possible. All deflate() output goes
656 // through this function so some applications may wish to modify it
657 // to avoid allocating a large strm->next_out buffer and copying into it.
658 // (See also read_buf()).
659 internal void flush_pending()
661 int len = dstate.pendingCount;
663 if (len > AvailableBytesOut)
664 len = AvailableBytesOut;
668 if (dstate.pending.Length <= dstate.nextPending ||
669 OutputBuffer.Length <= NextOut ||
670 dstate.pending.Length < (dstate.nextPending + len) ||
671 OutputBuffer.Length < (NextOut + len))
673 throw new ZlibException(String.Format("Invalid State. (pending.Length={0}, pendingCount={1})",
674 dstate.pending.Length, dstate.pendingCount));
677 Array.Copy(dstate.pending, dstate.nextPending, OutputBuffer, NextOut, len);
680 dstate.nextPending += len;
681 TotalBytesOut += len;
682 AvailableBytesOut -= len;
683 dstate.pendingCount -= len;
684 if (dstate.pendingCount == 0)
686 dstate.nextPending = 0;
690 // Read a new buffer from the current input stream, update the adler32
691 // and total number of bytes read. All deflate() input goes through
692 // this function so some applications may wish to modify it to avoid
693 // allocating a large strm->next_in buffer and copying from it.
694 // (See also flush_pending()).
695 internal int read_buf(byte[] buf, int start, int size)
697 int len = AvailableBytesIn;
704 AvailableBytesIn -= len;
706 if (dstate.WantRfc1950HeaderBytes)
708 _Adler32 = Adler.Adler32(_Adler32, InputBuffer, NextIn, len);
710 Array.Copy(InputBuffer, NextIn, buf, start, len);