+++ /dev/null
-// Deflater.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
-\r
-namespace ICSharpCode.SharpZipLib.Zip.Compression \r
-{\r
- \r
- /// <summary>\r
- /// This is the Deflater class. The deflater class compresses input\r
- /// with the deflate algorithm described in RFC 1951. It has several\r
- /// compression levels and three different strategies described below.\r
- ///\r
- /// This class is <i>not</i> thread safe. This is inherent in the API, due\r
- /// to the split of deflate and setInput.\r
- /// \r
- /// author of the original java version : Jochen Hoenicke\r
- /// </summary>\r
- public class Deflater\r
- {\r
- /// <summary>\r
- /// The best and slowest compression level. This tries to find very\r
- /// long and distant string repetitions.\r
- /// </summary>\r
- public static int BEST_COMPRESSION = 9;\r
- \r
- /// <summary>\r
- /// The worst but fastest compression level.\r
- /// </summary>\r
- public static int BEST_SPEED = 1;\r
- \r
- /// <summary>\r
- /// The default compression level.\r
- /// </summary>\r
- public static int DEFAULT_COMPRESSION = -1;\r
- \r
- /// <summary>\r
- /// This level won't compress at all but output uncompressed blocks.\r
- /// </summary>\r
- public static int NO_COMPRESSION = 0;\r
- \r
- /// <summary>\r
- /// The compression method. This is the only method supported so far.\r
- /// There is no need to use this constant at all.\r
- /// </summary>\r
- public static int DEFLATED = 8;\r
- \r
- /*\r
- * The Deflater can do the following state transitions:\r
- *\r
- * (1) -> INIT_STATE ----> INIT_FINISHING_STATE ---.\r
- * / | (2) (5) |\r
- * / v (5) |\r
- * (3)| SETDICT_STATE ---> SETDICT_FINISHING_STATE |(3)\r
- * \ | (3) | ,-------'\r
- * | | | (3) /\r
- * v v (5) v v\r
- * (1) -> BUSY_STATE ----> FINISHING_STATE\r
- * | (6)\r
- * v\r
- * FINISHED_STATE\r
- * \_____________________________________/\r
- * | (7)\r
- * v\r
- * CLOSED_STATE\r
- *\r
- * (1) If we should produce a header we start in INIT_STATE, otherwise\r
- * we start in BUSY_STATE.\r
- * (2) A dictionary may be set only when we are in INIT_STATE, then\r
- * we change the state as indicated.\r
- * (3) Whether a dictionary is set or not, on the first call of deflate\r
- * we change to BUSY_STATE.\r
- * (4) -- intentionally left blank -- :)\r
- * (5) FINISHING_STATE is entered, when flush() is called to indicate that\r
- * there is no more INPUT. There are also states indicating, that\r
- * the header wasn't written yet.\r
- * (6) FINISHED_STATE is entered, when everything has been flushed to the\r
- * internal pending output buffer.\r
- * (7) At any time (7)\r
- *\r
- */\r
- \r
- private static int IS_SETDICT = 0x01;\r
- private static int IS_FLUSHING = 0x04;\r
- private static int IS_FINISHING = 0x08;\r
- \r
- private static int INIT_STATE = 0x00;\r
- private static int SETDICT_STATE = 0x01;\r
- // private static int INIT_FINISHING_STATE = 0x08;\r
- // private static int SETDICT_FINISHING_STATE = 0x09;\r
- private static int BUSY_STATE = 0x10;\r
- private static int FLUSHING_STATE = 0x14;\r
- private static int FINISHING_STATE = 0x1c;\r
- private static int FINISHED_STATE = 0x1e;\r
- private static int CLOSED_STATE = 0x7f;\r
- \r
- /// <summary>\r
- /// Compression level.\r
- /// </summary>\r
- private int level;\r
- \r
- /// <summary>\r
- /// should we include a header.\r
- /// </summary>\r
- private bool noHeader;\r
- \r
- // /// <summary>\r
- // /// Compression strategy.\r
- // /// </summary>\r
- // private int strategy;\r
- \r
- /// <summary>\r
- /// The current state.\r
- /// </summary>\r
- private int state;\r
- \r
- /// <summary>\r
- /// The total bytes of output written.\r
- /// </summary>\r
- private int totalOut;\r
- \r
- /// <summary>\r
- /// The pending output.\r
- /// </summary>\r
- private DeflaterPending pending;\r
- \r
- /// <summary>\r
- /// The deflater engine.\r
- /// </summary>\r
- private DeflaterEngine engine;\r
- \r
- /// <summary>\r
- /// Creates a new deflater with default compression level.\r
- /// </summary>\r
- public Deflater() : this(DEFAULT_COMPRESSION, false)\r
- {\r
- \r
- }\r
- \r
- /// <summary>\r
- /// Creates a new deflater with given compression level.\r
- /// </summary>\r
- /// <param name="lvl">\r
- /// the compression level, a value between NO_COMPRESSION\r
- /// and BEST_COMPRESSION, or DEFAULT_COMPRESSION.\r
- /// </param>\r
- /// <exception cref="System.ArgumentOutOfRangeException">if lvl is out of range.</exception>\r
- public Deflater(int lvl) : this(lvl, false)\r
- {\r
- \r
- }\r
- \r
- /// <summary>\r
- /// Creates a new deflater with given compression level.\r
- /// </summary>\r
- /// <param name="lvl">\r
- /// the compression level, a value between NO_COMPRESSION\r
- /// and BEST_COMPRESSION.\r
- /// </param>\r
- /// <param name="nowrap">\r
- /// true, if we should suppress the deflate header at the\r
- /// beginning and the adler checksum at the end of the output. This is\r
- /// useful for the GZIP format.\r
- /// </param>\r
- /// <exception cref="System.ArgumentOutOfRangeException">if lvl is out of range.</exception>\r
- public Deflater(int lvl, bool nowrap)\r
- {\r
- if (lvl == DEFAULT_COMPRESSION) {\r
- lvl = 6;\r
- } else if (lvl < NO_COMPRESSION || lvl > BEST_COMPRESSION) {\r
- throw new ArgumentOutOfRangeException("lvl");\r
- }\r
- \r
- pending = new DeflaterPending();\r
- engine = new DeflaterEngine(pending);\r
- this.noHeader = nowrap;\r
- SetStrategy(DeflateStrategy.Default);\r
- SetLevel(lvl);\r
- Reset();\r
- }\r
- \r
- \r
- /// <summary>\r
- /// Resets the deflater. The deflater acts afterwards as if it was\r
- /// just created with the same compression level and strategy as it\r
- /// had before.\r
- /// </summary>\r
- public void Reset()\r
- {\r
- state = (noHeader ? BUSY_STATE : INIT_STATE);\r
- totalOut = 0;\r
- pending.Reset();\r
- engine.Reset();\r
- }\r
- \r
- /// <summary>\r
- /// Gets the current adler checksum of the data that was processed so far.\r
- /// </summary>\r
- public int Adler {\r
- get {\r
- return engine.Adler;\r
- }\r
- }\r
- \r
- /// <summary>\r
- /// Gets the number of input bytes processed so far.\r
- /// </summary>\r
- public int TotalIn {\r
- get {\r
- return engine.TotalIn;\r
- }\r
- }\r
- \r
- /// <summary>\r
- /// Gets the number of output bytes so far.\r
- /// </summary>\r
- public int TotalOut {\r
- get {\r
- return totalOut;\r
- }\r
- }\r
- \r
- /// <summary>\r
- /// Flushes the current input block. Further calls to deflate() will\r
- /// produce enough output to inflate everything in the current input\r
- /// block. This is not part of Sun's JDK so I have made it package\r
- /// private. It is used by DeflaterOutputStream to implement\r
- /// flush().\r
- /// </summary>\r
- public void Flush() \r
- {\r
- state |= IS_FLUSHING;\r
- }\r
- \r
- /// <summary>\r
- /// Finishes the deflater with the current input block. It is an error\r
- /// to give more input after this method was called. This method must\r
- /// be called to force all bytes to be flushed.\r
- /// </summary>\r
- public void Finish() \r
- {\r
- state |= IS_FLUSHING | IS_FINISHING;\r
- }\r
- \r
- /// <summary>\r
- /// Returns true if the stream was finished and no more output bytes\r
- /// are available.\r
- /// </summary>\r
- public bool IsFinished {\r
- get {\r
- return state == FINISHED_STATE && pending.IsFlushed;\r
- }\r
- }\r
- \r
- /// <summary>\r
- /// Returns true, if the input buffer is empty.\r
- /// You should then call setInput(). \r
- /// NOTE: This method can also return true when the stream\r
- /// was finished.\r
- /// </summary>\r
- public bool IsNeedingInput {\r
- get {\r
- return engine.NeedsInput();\r
- }\r
- }\r
- \r
- /// <summary>\r
- /// Sets the data which should be compressed next. This should be only\r
- /// called when needsInput indicates that more input is needed.\r
- /// If you call setInput when needsInput() returns false, the\r
- /// previous input that is still pending will be thrown away.\r
- /// The given byte array should not be changed, before needsInput() returns\r
- /// true again.\r
- /// This call is equivalent to <code>setInput(input, 0, input.length)</code>.\r
- /// </summary>\r
- /// <param name="input">\r
- /// the buffer containing the input data.\r
- /// </param>\r
- /// <exception cref="System.InvalidOperationException">\r
- /// if the buffer was finished() or ended().\r
- /// </exception>\r
- public void SetInput(byte[] input)\r
- {\r
- SetInput(input, 0, input.Length);\r
- }\r
- \r
- /// <summary>\r
- /// Sets the data which should be compressed next. This should be\r
- /// only called when needsInput indicates that more input is needed.\r
- /// The given byte array should not be changed, before needsInput() returns\r
- /// true again.\r
- /// </summary>\r
- /// <param name="input">\r
- /// the buffer containing the input data.\r
- /// </param>\r
- /// <param name="off">\r
- /// the start of the data.\r
- /// </param>\r
- /// <param name="len">\r
- /// the length of the data.\r
- /// </param>\r
- /// <exception cref="System.InvalidOperationException">\r
- /// if the buffer was finished() or ended() or if previous input is still pending.\r
- /// </exception>\r
- public void SetInput(byte[] input, int off, int len)\r
- {\r
- if ((state & IS_FINISHING) != 0) {\r
- throw new InvalidOperationException("finish()/end() already called");\r
- }\r
- engine.SetInput(input, off, len);\r
- }\r
- \r
- /// <summary>\r
- /// Sets the compression level. There is no guarantee of the exact\r
- /// position of the change, but if you call this when needsInput is\r
- /// true the change of compression level will occur somewhere near\r
- /// before the end of the so far given input.\r
- /// </summary>\r
- /// <param name="lvl">\r
- /// the new compression level.\r
- /// </param>\r
- public void SetLevel(int lvl)\r
- {\r
- if (lvl == DEFAULT_COMPRESSION) {\r
- lvl = 6;\r
- } else if (lvl < NO_COMPRESSION || lvl > BEST_COMPRESSION) {\r
- throw new ArgumentOutOfRangeException("lvl");\r
- }\r
- \r
- if (level != lvl) {\r
- level = lvl;\r
- engine.SetLevel(lvl);\r
- }\r
- }\r
- \r
- /// <summary>\r
- /// Sets the compression strategy. Strategy is one of\r
- /// DEFAULT_STRATEGY, HUFFMAN_ONLY and FILTERED. For the exact\r
- /// position where the strategy is changed, the same as for\r
- /// setLevel() applies.\r
- /// </summary>\r
- /// <param name="stgy">\r
- /// the new compression strategy.\r
- /// </param>\r
- public void SetStrategy(DeflateStrategy stgy)\r
- {\r
- engine.Strategy = stgy;\r
- }\r
- \r
- /// <summary>\r
- /// Deflates the current input block to the given array. It returns\r
- /// the number of bytes compressed, or 0 if either\r
- /// needsInput() or finished() returns true or length is zero.\r
- /// </summary>\r
- /// <param name="output">\r
- /// the buffer where to write the compressed data.\r
- /// </param>\r
- public int Deflate(byte[] output)\r
- {\r
- return Deflate(output, 0, output.Length);\r
- }\r
- \r
- /// <summary>\r
- /// Deflates the current input block to the given array. It returns\r
- /// the number of bytes compressed, or 0 if either\r
- /// needsInput() or finished() returns true or length is zero.\r
- /// </summary>\r
- /// <param name="output">\r
- /// the buffer where to write the compressed data.\r
- /// </param>\r
- /// <param name="offset">\r
- /// the offset into the output array.\r
- /// </param>\r
- /// <param name="length">\r
- /// the maximum number of bytes that may be written.\r
- /// </param>\r
- /// <exception cref="System.InvalidOperationException">\r
- /// if end() was called.\r
- /// </exception>\r
- /// <exception cref="System.ArgumentOutOfRangeException">\r
- /// if offset and/or length don't match the array length.\r
- /// </exception>\r
- public int Deflate(byte[] output, int offset, int length)\r
- {\r
- int origLength = length;\r
- \r
- if (state == CLOSED_STATE) {\r
- throw new InvalidOperationException("Deflater closed");\r
- }\r
- \r
- if (state < BUSY_STATE) {\r
- /* output header */\r
- int header = (DEFLATED +\r
- ((DeflaterConstants.MAX_WBITS - 8) << 4)) << 8;\r
- int level_flags = (level - 1) >> 1;\r
- if (level_flags < 0 || level_flags > 3) {\r
- level_flags = 3;\r
- }\r
- header |= level_flags << 6;\r
- if ((state & IS_SETDICT) != 0) {\r
- /* Dictionary was set */\r
- header |= DeflaterConstants.PRESET_DICT;\r
- }\r
- header += 31 - (header % 31);\r
- \r
- \r
- pending.WriteShortMSB(header);\r
- if ((state & IS_SETDICT) != 0) {\r
- int chksum = engine.Adler;\r
- engine.ResetAdler();\r
- pending.WriteShortMSB(chksum >> 16);\r
- pending.WriteShortMSB(chksum & 0xffff);\r
- }\r
- \r
- state = BUSY_STATE | (state & (IS_FLUSHING | IS_FINISHING));\r
- }\r
- \r
- for (;;) {\r
- int count = pending.Flush(output, offset, length);\r
- offset += count;\r
- totalOut += count;\r
- length -= count;\r
- \r
- if (length == 0 || state == FINISHED_STATE) {\r
- break;\r
- }\r
- \r
- if (!engine.Deflate((state & IS_FLUSHING) != 0, (state & IS_FINISHING) != 0)) {\r
- if (state == BUSY_STATE) {\r
- /* We need more input now */\r
- return origLength - length;\r
- } else if (state == FLUSHING_STATE) {\r
- if (level != NO_COMPRESSION) {\r
- /* We have to supply some lookahead. 8 bit lookahead\r
- * are needed by the zlib inflater, and we must fill\r
- * the next byte, so that all bits are flushed.\r
- */\r
- int neededbits = 8 + ((-pending.BitCount) & 7);\r
- while (neededbits > 0) {\r
- /* write a static tree block consisting solely of\r
- * an EOF:\r
- */\r
- pending.WriteBits(2, 10);\r
- neededbits -= 10;\r
- }\r
- }\r
- state = BUSY_STATE;\r
- } else if (state == FINISHING_STATE) {\r
- pending.AlignToByte();\r
- /* We have completed the stream */\r
- if (!noHeader) {\r
- int adler = engine.Adler;\r
- pending.WriteShortMSB(adler >> 16);\r
- pending.WriteShortMSB(adler & 0xffff);\r
- }\r
- state = FINISHED_STATE;\r
- }\r
- }\r
- }\r
- return origLength - length;\r
- }\r
- \r
- /// <summary>\r
- /// Sets the dictionary which should be used in the deflate process.\r
- /// This call is equivalent to <code>setDictionary(dict, 0, dict.Length)</code>.\r
- /// </summary>\r
- /// <param name="dict">\r
- /// the dictionary.\r
- /// </param>\r
- /// <exception cref="System.InvalidOperationException">\r
- /// if setInput () or deflate () were already called or another dictionary was already set.\r
- /// </exception>\r
- public void SetDictionary(byte[] dict)\r
- {\r
- SetDictionary(dict, 0, dict.Length);\r
- }\r
- \r
- /// <summary>\r
- /// Sets the dictionary which should be used in the deflate process.\r
- /// The dictionary should be a byte array containing strings that are\r
- /// likely to occur in the data which should be compressed. The\r
- /// dictionary is not stored in the compressed output, only a\r
- /// checksum. To decompress the output you need to supply the same\r
- /// dictionary again.\r
- /// </summary>\r
- /// <param name="dict">\r
- /// the dictionary.\r
- /// </param>\r
- /// <param name="offset">\r
- /// an offset into the dictionary.\r
- /// </param>\r
- /// <param name="length">\r
- /// the length of the dictionary.\r
- /// </param>\r
- /// <exception cref="System.InvalidOperationException">\r
- /// if setInput () or deflate () were already called or another dictionary was already set.\r
- /// </exception>\r
- public void SetDictionary(byte[] dict, int offset, int length)\r
- {\r
- if (state != INIT_STATE) {\r
- throw new InvalidOperationException();\r
- }\r
- \r
- state = SETDICT_STATE;\r
- engine.SetDictionary(dict, offset, length);\r
- }\r
- }\r
-}\r