Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3044_variableScaling/HeuristicLab.ExtLibs/HeuristicLab.EPPlus/4.0.3/EPPlus-4.0.3/Packaging/DotNetZip/Zlib/ZlibCodec.cs

Last change on this file was 12074, checked in by sraggl, 10 years ago

#2341: Added EPPlus-4.0.3 to ExtLibs

File size: 30.3 KB
Line 
1// ZlibCodec.cs
2// ------------------------------------------------------------------
3//
4// Copyright (c) 2009 Dino Chiesa and Microsoft Corporation. 
5// All rights reserved.
6//
7// This code module is part of DotNetZip, a zipfile class library.
8//
9// ------------------------------------------------------------------
10//
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
14//
15// ------------------------------------------------------------------
16//
17// last saved (in emacs):
18// Time-stamp: <2009-November-03 15:40:51>
19//
20// ------------------------------------------------------------------
21//
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
28// is included below.
29//
30// ------------------------------------------------------------------
31//
32// Copyright (c) 2000,2001,2002,2003 ymnk, JCraft,Inc. All rights reserved.
33//
34// Redistribution and use in source and binary forms, with or without
35// modification, are permitted provided that the following conditions are met:
36//
37// 1. Redistributions of source code must retain the above copyright notice,
38// this list of conditions and the following disclaimer.
39//
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.
43//
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.
46//
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.
57//
58// -----------------------------------------------------------------------
59//
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.
63//
64// -----------------------------------------------------------------------
65
66
67using System;
68using Interop=System.Runtime.InteropServices;
69
70namespace OfficeOpenXml.Packaging.Ionic.Zlib
71{
72    /// <summary>
73    /// Encoder and Decoder for ZLIB and DEFLATE (IETF RFC1950 and RFC1951).
74    /// </summary>
75    ///
76    /// <remarks>
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>.
81    /// </remarks>
82    [Interop.GuidAttribute("ebc25cf6-9120-4283-b972-0e5520d0000D")]
83    [Interop.ComVisible(true)]
84#if !NETCF   
85    [Interop.ClassInterface(Interop.ClassInterfaceType.AutoDispatch)]
86#endif
87    sealed public class ZlibCodec
88    {
89        /// <summary>
90        /// The buffer from which data is taken.
91        /// </summary>
92        public byte[] InputBuffer;
93
94        /// <summary>
95        /// An index into the InputBuffer array, indicating where to start reading.
96        /// </summary>
97        public int NextIn;
98
99        /// <summary>
100        /// The number of bytes available in the InputBuffer, starting at NextIn.
101        /// </summary>
102        /// <remarks>
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.
105        /// </remarks>
106        public int AvailableBytesIn;
107
108        /// <summary>
109        /// Total number of bytes read so far, through all calls to Inflate()/Deflate().
110        /// </summary>
111        public long TotalBytesIn;
112
113        /// <summary>
114        /// Buffer to store output data.
115        /// </summary>
116        public byte[] OutputBuffer;
117
118        /// <summary>
119        /// An index into the OutputBuffer array, indicating where to start writing.
120        /// </summary>
121        public int NextOut;
122
123        /// <summary>
124        /// The number of bytes available in the OutputBuffer, starting at NextOut.
125        /// </summary>
126        /// <remarks>
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.
129        /// </remarks>
130        public int AvailableBytesOut;
131
132        /// <summary>
133        /// Total number of bytes written to the output so far, through all calls to Inflate()/Deflate().
134        /// </summary>
135        public long TotalBytesOut;
136
137        /// <summary>
138        /// used for diagnostics, when something goes wrong!
139        /// </summary>
140        public System.String Message;
141
142        internal DeflateManager dstate;
143        internal InflateManager istate;
144
145        internal uint _Adler32;
146
147        /// <summary>
148        /// The compression level to use in this codec.  Useful only in compression mode.
149        /// </summary>
150        public CompressionLevel CompressLevel = CompressionLevel.Default;
151
152        /// <summary>
153        /// The number of Window Bits to use. 
154        /// </summary>
155        /// <remarks>
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
159        /// a 32k window. 
160        /// </remarks>
161        public int WindowBits = ZlibConstants.WindowBitsDefault;
162
163        /// <summary>
164        /// The compression strategy to use.
165        /// </summary>
166        /// <remarks>
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.
175        /// </remarks>
176        public CompressionStrategy Strategy = CompressionStrategy.Default;
177
178
179        /// <summary>
180        /// The Adler32 checksum on the data transferred through the codec so far. You probably don't need to look at this.
181        /// </summary>
182        public int Adler32 { get { return (int)_Adler32; } }
183
184
185        /// <summary>
186        /// Create a ZlibCodec.
187        /// </summary>
188        /// <remarks>
189        /// If you use this default constructor, you will later have to explicitly call
190        /// InitializeInflate() or InitializeDeflate() before using the ZlibCodec to compress
191        /// or decompress.
192        /// </remarks>
193        public ZlibCodec() { }
194
195        /// <summary>
196        /// Create a ZlibCodec that either compresses or decompresses.
197        /// </summary>
198        /// <param name="mode">
199        /// Indicates whether the codec should compress (deflate) or decompress (inflate).
200        /// </param>
201        public ZlibCodec(CompressionMode mode)
202        {
203            if (mode == CompressionMode.Compress)
204            {
205                int rc = InitializeDeflate();
206                if (rc != ZlibConstants.Z_OK) throw new ZlibException("Cannot initialize for deflate.");
207            }
208            else if (mode == CompressionMode.Decompress)
209            {
210                int rc = InitializeInflate();
211                if (rc != ZlibConstants.Z_OK) throw new ZlibException("Cannot initialize for inflate.");
212            }
213            else throw new ZlibException("Invalid ZlibStreamFlavor.");
214        }
215
216        /// <summary>
217        /// Initialize the inflation state.
218        /// </summary>
219        /// <remarks>
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.
222        /// </remarks>
223        /// <returns>Z_OK if everything goes well.</returns>
224        public int InitializeInflate()
225        {
226            return InitializeInflate(this.WindowBits);
227        }
228
229        /// <summary>
230        /// Initialize the inflation state with an explicit flag to
231        /// govern the handling of RFC1950 header bytes.
232        /// </summary>
233        ///
234        /// <remarks>
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.
241        /// </remarks>
242        ///
243        /// <param name="expectRfc1950Header">whether to expect an RFC1950 header byte
244        /// pair when reading the stream of data to be inflated.</param>
245        ///
246        /// <returns>Z_OK if everything goes well.</returns>
247        public int InitializeInflate(bool expectRfc1950Header)
248        {
249            return InitializeInflate(this.WindowBits, expectRfc1950Header);
250        }
251
252        /// <summary>
253        /// Initialize the ZlibCodec for inflation, with the specified number of window bits.
254        /// </summary>
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)
259        {
260            this.WindowBits = windowBits;           
261            return InitializeInflate(windowBits, true);
262        }
263
264        /// <summary>
265        /// Initialize the inflation state with an explicit flag to govern the handling of
266        /// RFC1950 header bytes.
267        /// </summary>
268        ///
269        /// <remarks>
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
275        /// false.
276        /// </remarks>
277        ///
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)
284        {
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);
289        }
290
291        /// <summary>
292        /// Inflate the data in the InputBuffer, placing the result in the OutputBuffer.
293        /// </summary>
294        /// <remarks>
295        /// You must have set InputBuffer and OutputBuffer, NextIn and NextOut, and AvailableBytesIn and
296        /// AvailableBytesOut  before calling this method.
297        /// </remarks>
298        /// <example>
299        /// <code>
300        /// private void InflateBuffer()
301        /// {
302        ///     int bufferSize = 1024;
303        ///     byte[] buffer = new byte[bufferSize];
304        ///     ZlibCodec decompressor = new ZlibCodec();
305        ///
306        ///     Console.WriteLine("\n============================================");
307        ///     Console.WriteLine("Size of Buffer to Inflate: {0} bytes.", CompressedBytes.Length);
308        ///     MemoryStream ms = new MemoryStream(DecompressedBytes);
309        ///
310        ///     int rc = decompressor.InitializeInflate();
311        ///
312        ///     decompressor.InputBuffer = CompressedBytes;
313        ///     decompressor.NextIn = 0;
314        ///     decompressor.AvailableBytesIn = CompressedBytes.Length;
315        ///
316        ///     decompressor.OutputBuffer = buffer;
317        ///
318        ///     // pass 1: inflate
319        ///     do
320        ///     {
321        ///         decompressor.NextOut = 0;
322        ///         decompressor.AvailableBytesOut = buffer.Length;
323        ///         rc = decompressor.Inflate(FlushType.None);
324        ///
325        ///         if (rc != ZlibConstants.Z_OK &amp;&amp; rc != ZlibConstants.Z_STREAM_END)
326        ///             throw new Exception("inflating: " + decompressor.Message);
327        ///
328        ///         ms.Write(decompressor.OutputBuffer, 0, buffer.Length - decompressor.AvailableBytesOut);
329        ///     }
330        ///     while (decompressor.AvailableBytesIn &gt; 0 || decompressor.AvailableBytesOut == 0);
331        ///
332        ///     // pass 2: finish and flush
333        ///     do
334        ///     {
335        ///         decompressor.NextOut = 0;
336        ///         decompressor.AvailableBytesOut = buffer.Length;
337        ///         rc = decompressor.Inflate(FlushType.Finish);
338        ///
339        ///         if (rc != ZlibConstants.Z_STREAM_END &amp;&amp; rc != ZlibConstants.Z_OK)
340        ///             throw new Exception("inflating: " + decompressor.Message);
341        ///
342        ///         if (buffer.Length - decompressor.AvailableBytesOut &gt; 0)
343        ///             ms.Write(buffer, 0, buffer.Length - decompressor.AvailableBytesOut);
344        ///     }
345        ///     while (decompressor.AvailableBytesIn &gt; 0 || decompressor.AvailableBytesOut == 0);
346        ///
347        ///     decompressor.EndInflate();
348        /// }
349        ///
350        /// </code>
351        /// </example>
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)
355        {
356            if (istate == null)
357                throw new ZlibException("No Inflate State!");
358            return istate.Inflate(flush);
359        }
360
361
362        /// <summary>
363        /// Ends an inflation session.
364        /// </summary>
365        /// <remarks>
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.
369        /// </remarks>
370        /// <returns>Z_OK if everything goes well.</returns>
371        public int EndInflate()
372        {
373            if (istate == null)
374                throw new ZlibException("No Inflate State!");
375            int ret = istate.End();
376            istate = null;
377            return ret;
378        }
379
380        /// <summary>
381        /// I don't know what this does!
382        /// </summary>
383        /// <returns>Z_OK if everything goes well.</returns>
384        public int SyncInflate()
385        {
386            if (istate == null)
387                throw new ZlibException("No Inflate State!");
388            return istate.Sync();
389        }
390
391        /// <summary>
392        /// Initialize the ZlibCodec for deflation operation.
393        /// </summary>
394        /// <remarks>
395        /// The codec will use the MAX window bits and the default level of compression.
396        /// </remarks>
397        /// <example>
398        /// <code>
399        ///  int bufferSize = 40000;
400        ///  byte[] CompressedBytes = new byte[bufferSize];
401        ///  byte[] DecompressedBytes = new byte[bufferSize];
402        /// 
403        ///  ZlibCodec compressor = new ZlibCodec();
404        /// 
405        ///  compressor.InitializeDeflate(CompressionLevel.Default);
406        /// 
407        ///  compressor.InputBuffer = System.Text.ASCIIEncoding.ASCII.GetBytes(TextToCompress);
408        ///  compressor.NextIn = 0;
409        ///  compressor.AvailableBytesIn = compressor.InputBuffer.Length;
410        /// 
411        ///  compressor.OutputBuffer = CompressedBytes;
412        ///  compressor.NextOut = 0;
413        ///  compressor.AvailableBytesOut = CompressedBytes.Length;
414        /// 
415        ///  while (compressor.TotalBytesIn != TextToCompress.Length &amp;&amp; compressor.TotalBytesOut &lt; bufferSize)
416        ///  {
417        ///    compressor.Deflate(FlushType.None);
418        ///  }
419        /// 
420        ///  while (true)
421        ///  {
422        ///    int rc= compressor.Deflate(FlushType.Finish);
423        ///    if (rc == ZlibConstants.Z_STREAM_END) break;
424        ///  }
425        /// 
426        ///  compressor.EndDeflate();
427        ///   
428        /// </code>
429        /// </example>
430        /// <returns>Z_OK if all goes well. You generally don't need to check the return code.</returns>
431        public int InitializeDeflate()
432        {
433            return _InternalInitializeDeflate(true);
434        }
435
436        /// <summary>
437        /// Initialize the ZlibCodec for deflation operation, using the specified CompressionLevel.
438        /// </summary>
439        /// <remarks>
440        /// The codec will use the maximum window bits (15) and the specified
441        /// CompressionLevel.  It will emit a ZLIB stream as it compresses.
442        /// </remarks>
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)
446        {
447            this.CompressLevel = level;
448            return _InternalInitializeDeflate(true);
449        }
450
451
452        /// <summary>
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.
455        /// </summary>
456        /// <remarks>
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. 
462        /// </remarks>
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)
467        {
468            this.CompressLevel = level;
469            return _InternalInitializeDeflate(wantRfc1950Header);
470        }
471
472
473        /// <summary>
474        /// Initialize the ZlibCodec for deflation operation, using the specified CompressionLevel,
475        /// and the specified number of window bits.
476        /// </summary>
477        /// <remarks>
478        /// The codec will use the specified number of window bits and the specified CompressionLevel.
479        /// </remarks>
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)
484        {
485            this.CompressLevel = level;
486            this.WindowBits = bits;
487            return _InternalInitializeDeflate(true);
488        }
489
490        /// <summary>
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.
494        /// </summary>
495        ///
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)
501        {
502            this.CompressLevel = level;
503            this.WindowBits = bits;
504            return _InternalInitializeDeflate(wantRfc1950Header);
505        }
506
507        private int _InternalInitializeDeflate(bool wantRfc1950Header)
508        {
509            if (istate != null) throw new ZlibException("You may not call InitializeDeflate() after calling InitializeInflate().");
510            dstate = new DeflateManager();
511            dstate.WantRfc1950HeaderBytes = wantRfc1950Header;
512
513            return dstate.Initialize(this, this.CompressLevel, this.WindowBits, this.Strategy);
514        }
515
516        /// <summary>
517        /// Deflate one batch of data.
518        /// </summary>
519        /// <remarks>
520        /// You must have set InputBuffer and OutputBuffer before calling this method.
521        /// </remarks>
522        /// <example>
523        /// <code>
524        /// private void DeflateBuffer(CompressionLevel level)
525        /// {
526        ///     int bufferSize = 1024;
527        ///     byte[] buffer = new byte[bufferSize];
528        ///     ZlibCodec compressor = new ZlibCodec();
529        ///
530        ///     Console.WriteLine("\n============================================");
531        ///     Console.WriteLine("Size of Buffer to Deflate: {0} bytes.", UncompressedBytes.Length);
532        ///     MemoryStream ms = new MemoryStream();
533        ///
534        ///     int rc = compressor.InitializeDeflate(level);
535        ///
536        ///     compressor.InputBuffer = UncompressedBytes;
537        ///     compressor.NextIn = 0;
538        ///     compressor.AvailableBytesIn = UncompressedBytes.Length;
539        ///
540        ///     compressor.OutputBuffer = buffer;
541        ///
542        ///     // pass 1: deflate
543        ///     do
544        ///     {
545        ///         compressor.NextOut = 0;
546        ///         compressor.AvailableBytesOut = buffer.Length;
547        ///         rc = compressor.Deflate(FlushType.None);
548        ///
549        ///         if (rc != ZlibConstants.Z_OK &amp;&amp; rc != ZlibConstants.Z_STREAM_END)
550        ///             throw new Exception("deflating: " + compressor.Message);
551        ///
552        ///         ms.Write(compressor.OutputBuffer, 0, buffer.Length - compressor.AvailableBytesOut);
553        ///     }
554        ///     while (compressor.AvailableBytesIn &gt; 0 || compressor.AvailableBytesOut == 0);
555        ///
556        ///     // pass 2: finish and flush
557        ///     do
558        ///     {
559        ///         compressor.NextOut = 0;
560        ///         compressor.AvailableBytesOut = buffer.Length;
561        ///         rc = compressor.Deflate(FlushType.Finish);
562        ///
563        ///         if (rc != ZlibConstants.Z_STREAM_END &amp;&amp; rc != ZlibConstants.Z_OK)
564        ///             throw new Exception("deflating: " + compressor.Message);
565        ///
566        ///         if (buffer.Length - compressor.AvailableBytesOut &gt; 0)
567        ///             ms.Write(buffer, 0, buffer.Length - compressor.AvailableBytesOut);
568        ///     }
569        ///     while (compressor.AvailableBytesIn &gt; 0 || compressor.AvailableBytesOut == 0);
570        ///
571        ///     compressor.EndDeflate();
572        ///
573        ///     ms.Seek(0, SeekOrigin.Begin);
574        ///     CompressedBytes = new byte[compressor.TotalBytesOut];
575        ///     ms.Read(CompressedBytes, 0, CompressedBytes.Length);
576        /// }
577        /// </code>
578        /// </example>
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.
582        /// </param>
583        /// <returns>Z_OK if all goes well.</returns>
584        public int Deflate(FlushType flush)
585        {
586            if (dstate == null)
587                throw new ZlibException("No Deflate State!");
588            return dstate.Deflate(flush);
589        }
590
591        /// <summary>
592        /// End a deflation session.
593        /// </summary>
594        /// <remarks>
595        /// Call this after making a series of one or more calls to Deflate(). All buffers are flushed.
596        /// </remarks>
597        /// <returns>Z_OK if all goes well.</returns>
598        public int EndDeflate()
599        {
600            if (dstate == null)
601                throw new ZlibException("No Deflate State!");
602            // TODO: dinoch Tue, 03 Nov 2009  15:39 (test this)
603            //int ret = dstate.End();
604            dstate = null;
605            return ZlibConstants.Z_OK; //ret;
606        }
607
608        /// <summary>
609        /// Reset a codec for another deflation session.
610        /// </summary>
611        /// <remarks>
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.
615        /// </remarks>
616        /// <returns>Z_OK if all goes well.</returns>
617        public void ResetDeflate()
618        {
619            if (dstate == null)
620                throw new ZlibException("No Deflate State!");
621            dstate.Reset();
622        }
623
624
625        /// <summary>
626        /// Set the CompressionStrategy and CompressionLevel for a deflation session.
627        /// </summary>
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)
632        {
633            if (dstate == null)
634                throw new ZlibException("No Deflate State!");
635            return dstate.SetParams(level, strategy);
636        }
637
638
639        /// <summary>
640        /// Set the dictionary to be used for either Inflation or Deflation.
641        /// </summary>
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)
645        {
646            if (istate != null)
647                return istate.SetDictionary(dictionary);
648
649            if (dstate != null)
650                return dstate.SetDictionary(dictionary);
651
652            throw new ZlibException("No Inflate or Deflate state!");
653        }
654
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()
660        {
661            int len = dstate.pendingCount;
662
663            if (len > AvailableBytesOut)
664                len = AvailableBytesOut;
665            if (len == 0)
666                return;
667
668            if (dstate.pending.Length <= dstate.nextPending ||
669                OutputBuffer.Length <= NextOut ||
670                dstate.pending.Length < (dstate.nextPending + len) ||
671                OutputBuffer.Length < (NextOut + len))
672            {
673                throw new ZlibException(String.Format("Invalid State. (pending.Length={0}, pendingCount={1})",
674                    dstate.pending.Length, dstate.pendingCount));
675            }
676
677            Array.Copy(dstate.pending, dstate.nextPending, OutputBuffer, NextOut, len);
678
679            NextOut             += len;
680            dstate.nextPending  += len;
681            TotalBytesOut       += len;
682            AvailableBytesOut   -= len;
683            dstate.pendingCount -= len;
684            if (dstate.pendingCount == 0)
685            {
686                dstate.nextPending = 0;
687            }
688        }
689
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)
696        {
697            int len = AvailableBytesIn;
698
699            if (len > size)
700                len = size;
701            if (len == 0)
702                return 0;
703
704            AvailableBytesIn -= len;
705
706            if (dstate.WantRfc1950HeaderBytes)
707            {
708                _Adler32 = Adler.Adler32(_Adler32, InputBuffer, NextIn, len);
709            }
710            Array.Copy(InputBuffer, NextIn, buf, start, len);
711            NextIn += len;
712            TotalBytesIn += len;
713            return len;
714        }
715
716    }
717}
Note: See TracBrowser for help on using the repository browser.