BVB Source Codes

PDF file browser Show Deflater.cs Source code

Return Download PDF file browser: download Deflater.cs Source code - Download PDF file browser Source code - Type:.cs
  1. // Deflater.cs
  2. //
  3. // Copyright (C) 2001 Mike Krueger
  4. // Copyright (C) 2004 John Reilly
  5. //
  6. // This file was translated from java, it was part of the GNU Classpath
  7. // Copyright (C) 2001 Free Software Foundation, Inc.
  8. //
  9. // This program is free software; you can redistribute it and/or
  10. // modify it under the terms of the GNU General Public License
  11. // as published by the Free Software Foundation; either version 2
  12. // of the License, or (at your option) any later version.
  13. //
  14. // This program is distributed in the hope that it will be useful,
  15. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17. // GNU General Public License for more details.
  18. //
  19. // You should have received a copy of the GNU General Public License
  20. // along with this program; if not, write to the Free Software
  21. // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  22. //
  23. // Linking this library statically or dynamically with other modules is
  24. // making a combined work based on this library.  Thus, the terms and
  25. // conditions of the GNU General Public License cover the whole
  26. // combination.
  27. //
  28. // As a special exception, the copyright holders of this library give you
  29. // permission to link this library with independent modules to produce an
  30. // executable, regardless of the license terms of these independent
  31. // modules, and to copy and distribute the resulting executable under
  32. // terms of your choice, provided that you also meet, for each linked
  33. // independent module, the terms and conditions of the license of that
  34. // module.  An independent module is a module which is not derived from
  35. // or based on this library.  If you modify this library, you may extend
  36. // this exception to your version of the library, but you are not
  37. // obligated to do so.  If you do not wish to do so, delete this
  38. // exception statement from your version.
  39.  
  40. using System;
  41.  
  42. namespace PdfSharp.SharpZipLib.Zip.Compression
  43. {
  44.        
  45.         /// <summary>
  46.         /// This is the Deflater class.  The deflater class compresses input
  47.         /// with the deflate algorithm described in RFC 1951.  It has several
  48.         /// compression levels and three different strategies described below.
  49.         ///
  50.         /// This class is <i>not</i> thread safe.  This is inherent in the API, due
  51.         /// to the split of deflate and setInput.
  52.         ///
  53.         /// author of the original java version : Jochen Hoenicke
  54.         /// </summary>
  55.         internal class Deflater
  56.         {
  57.                 /// <summary>
  58.                 /// The best and slowest compression level.  This tries to find very
  59.                 /// long and distant string repetitions.
  60.                 /// </summary>
  61.                 public static  int BEST_COMPRESSION = 9;
  62.                
  63.                 /// <summary>
  64.                 /// The worst but fastest compression level.
  65.                 /// </summary>
  66.                 public static  int BEST_SPEED = 1;
  67.                
  68.                 /// <summary>
  69.                 /// The default compression level.
  70.                 /// </summary>
  71.                 public static  int DEFAULT_COMPRESSION = -1;
  72.                
  73.                 /// <summary>
  74.                 /// This level won't compress at all but output uncompressed blocks.
  75.                 /// </summary>
  76.                 public static  int NO_COMPRESSION = 0;
  77.                                
  78.                 /// <summary>
  79.                 /// The compression method.  This is the only method supported so far.
  80.                 /// There is no need to use this constant at all.
  81.                 /// </summary>
  82.                 public static  int DEFLATED = 8;
  83.                
  84.                 /*
  85.                 * The Deflater can do the following state transitions:
  86.                         *
  87.                         * (1) -> INIT_STATE   ----> INIT_FINISHING_STATE ---.
  88.                         *        /  | (2)      (5)                         |
  89.                         *       /   v          (5)                         |
  90.                         *   (3)| SETDICT_STATE ---> SETDICT_FINISHING_STATE |(3)
  91.                         *       \   | (3)                 |        ,-------'
  92.                         *        |  |                     | (3)   /
  93.                         *        v  v          (5)        v      v
  94.                         * (1) -> BUSY_STATE   ----> FINISHING_STATE
  95.                         *                                | (6)
  96.                         *                                v
  97.                         *                           FINISHED_STATE
  98.                         *    \_____________________________________/
  99.                         *          | (7)
  100.                         *          v
  101.                         *        CLOSED_STATE
  102.                         *
  103.                         * (1) If we should produce a header we start in INIT_STATE, otherwise
  104.                         *     we start in BUSY_STATE.
  105.                         * (2) A dictionary may be set only when we are in INIT_STATE, then
  106.                         *     we change the state as indicated.
  107.                         * (3) Whether a dictionary is set or not, on the first call of deflate
  108.                         *     we change to BUSY_STATE.
  109.                         * (4) -- intentionally left blank -- :)
  110.                         * (5) FINISHING_STATE is entered, when flush() is called to indicate that
  111.                         *     there is no more INPUT.  There are also states indicating, that
  112.                         *     the header wasn't written yet.
  113.                         * (6) FINISHED_STATE is entered, when everything has been flushed to the
  114.                         *     internal pending output buffer.
  115.                         * (7) At any time (7)
  116.                         *
  117.                         */
  118.                        
  119.                 private static  int IS_SETDICT              = 0x01;
  120.                 private static  int IS_FLUSHING             = 0x04;
  121.                 private static  int IS_FINISHING            = 0x08;
  122.                
  123.                 private static  int INIT_STATE              = 0x00;
  124.                 private static  int SETDICT_STATE           = 0x01;
  125.                 //              private static  int INIT_FINISHING_STATE    = 0x08;
  126.                 //              private static  int SETDICT_FINISHING_STATE = 0x09;
  127.                 private static  int BUSY_STATE              = 0x10;
  128.                 private static  int FLUSHING_STATE          = 0x14;
  129.                 private static  int FINISHING_STATE         = 0x1c;
  130.                 private static  int FINISHED_STATE          = 0x1e;
  131.                 private static  int CLOSED_STATE            = 0x7f;
  132.                
  133.                 /// <summary>
  134.                 /// Compression level.
  135.                 /// </summary>
  136.                 private int level;
  137.                
  138.                 /// <summary>
  139.                 /// If true no Zlib/RFC1950 headers or footers are generated
  140.                 /// </summary>
  141.                 private bool noZlibHeaderOrFooter;
  142.                
  143.                 /// <summary>
  144.                 /// The current state.
  145.                 /// </summary>
  146.                 private int state;
  147.                
  148.                 /// <summary>
  149.                 /// The total bytes of output written.
  150.                 /// </summary>
  151.                 private long totalOut;
  152.                
  153.                 /// <summary>
  154.                 /// The pending output.
  155.                 /// </summary>
  156.                 private DeflaterPending pending;
  157.                
  158.                 /// <summary>
  159.                 /// The deflater engine.
  160.                 /// </summary>
  161.                 private DeflaterEngine engine;
  162.                
  163.                 /// <summary>
  164.                 /// Creates a new deflater with default compression level.
  165.                 /// </summary>
  166.                 public Deflater() : this(DEFAULT_COMPRESSION, false)
  167.                 {
  168.                        
  169.                 }
  170.                
  171.                 /// <summary>
  172.                 /// Creates a new deflater with given compression level.
  173.                 /// </summary>
  174.                 /// <param name="lvl">
  175.                 /// the compression level, a value between NO_COMPRESSION
  176.                 /// and BEST_COMPRESSION, or DEFAULT_COMPRESSION.
  177.                 /// </param>
  178.                 /// <exception cref="System.ArgumentOutOfRangeException">if lvl is out of range.</exception>
  179.                 public Deflater(int lvl) : this(lvl, false)
  180.                 {
  181.                        
  182.                 }
  183.                
  184.                 /// <summary>
  185.                 /// Creates a new deflater with given compression level.
  186.                 /// </summary>
  187.                 /// <param name="level">
  188.                 /// the compression level, a value between NO_COMPRESSION
  189.                 /// and BEST_COMPRESSION.
  190.                 /// </param>
  191.                 /// <param name="noZlibHeaderOrFooter">
  192.                 /// true, if we should suppress the Zlib/RFC1950 header at the
  193.                 /// beginning and the adler checksum at the end of the output.  This is
  194.                 /// useful for the GZIP/PKZIP formats.
  195.                 /// </param>
  196.                 /// <exception cref="System.ArgumentOutOfRangeException">if lvl is out of range.</exception>
  197.                 public Deflater(int level, bool noZlibHeaderOrFooter)
  198.                 {
  199.                         if (level == DEFAULT_COMPRESSION) {
  200.                                 level = 6;
  201.                         } else if (level < NO_COMPRESSION || level > BEST_COMPRESSION) {
  202.                                 throw new ArgumentOutOfRangeException("level");
  203.                         }
  204.                        
  205.                         pending = new DeflaterPending();
  206.                         engine = new DeflaterEngine(pending);
  207.                         this.noZlibHeaderOrFooter = noZlibHeaderOrFooter;
  208.                         SetStrategy(DeflateStrategy.Default);
  209.                         SetLevel(level);
  210.                         Reset();
  211.                 }
  212.                
  213.                
  214.                 /// <summary>
  215.                 /// Resets the deflater.  The deflater acts afterwards as if it was
  216.                 /// just created with the same compression level and strategy as it
  217.                 /// had before.
  218.                 /// </summary>
  219.                 public void Reset()
  220.                 {
  221.                         state = (noZlibHeaderOrFooter ? BUSY_STATE : INIT_STATE);
  222.                         totalOut = 0;
  223.                         pending.Reset();
  224.                         engine.Reset();
  225.                 }
  226.                
  227.                 /// <summary>
  228.                 /// Gets the current adler checksum of the data that was processed so far.
  229.                 /// </summary>
  230.                 public int Adler {
  231.                         get {
  232.                                 return engine.Adler;
  233.                         }
  234.                 }
  235.                
  236.                 /// <summary>
  237.                 /// Gets the number of input bytes processed so far.
  238.                 /// </summary>
  239.                 public int TotalIn {
  240.                         get {
  241.                                 return engine.TotalIn;
  242.                         }
  243.                 }
  244.                
  245.                 /// <summary>
  246.                 /// Gets the number of output bytes so far.
  247.                 /// </summary>
  248.                 public long TotalOut {
  249.                         get {
  250.                                 return totalOut;
  251.                         }
  252.                 }
  253.                
  254.                 /// <summary>
  255.                 /// Flushes the current input block.  Further calls to deflate() will
  256.                 /// produce enough output to inflate everything in the current input
  257.                 /// block.  This is not part of Sun's JDK so I have made it package
  258.                 /// private.  It is used by DeflaterOutputStream to implement
  259.                 /// flush().
  260.                 /// </summary>
  261.                 public void Flush()
  262.                 {
  263.                         state |= IS_FLUSHING;
  264.                 }
  265.                
  266.                 /// <summary>
  267.                 /// Finishes the deflater with the current input block.  It is an error
  268.                 /// to give more input after this method was called.  This method must
  269.                 /// be called to force all bytes to be flushed.
  270.                 /// </summary>
  271.                 public void Finish()
  272.                 {
  273.                         state |= IS_FLUSHING | IS_FINISHING;
  274.                 }
  275.                
  276.                 /// <summary>
  277.                 /// Returns true if the stream was finished and no more output bytes
  278.                 /// are available.
  279.                 /// </summary>
  280.                 public bool IsFinished {
  281.                         get {
  282.                                 return state == FINISHED_STATE && pending.IsFlushed;
  283.                         }
  284.                 }
  285.                
  286.                 /// <summary>
  287.                 /// Returns true, if the input buffer is empty.
  288.                 /// You should then call setInput().
  289.                 /// NOTE: This method can also return true when the stream
  290.                 /// was finished.
  291.                 /// </summary>
  292.                 public bool IsNeedingInput {
  293.                         get {
  294.                                 return engine.NeedsInput();
  295.                         }
  296.                 }
  297.                
  298.                 /// <summary>
  299.                 /// Sets the data which should be compressed next.  This should be only
  300.                 /// called when needsInput indicates that more input is needed.
  301.                 /// If you call setInput when needsInput() returns false, the
  302.                 /// previous input that is still pending will be thrown away.
  303.                 /// The given byte array should not be changed, before needsInput() returns
  304.                 /// true again.
  305.                 /// This call is equivalent to <code>setInput(input, 0, input.length)</code>.
  306.                 /// </summary>
  307.                 /// <param name="input">
  308.                 /// the buffer containing the input data.
  309.                 /// </param>
  310.                 /// <exception cref="System.InvalidOperationException">
  311.                 /// if the buffer was finished() or ended().
  312.                 /// </exception>
  313.                 public void SetInput(byte[] input)
  314.                 {
  315.                         SetInput(input, 0, input.Length);
  316.                 }
  317.                
  318.                 /// <summary>
  319.                 /// Sets the data which should be compressed next.  This should be
  320.                 /// only called when needsInput indicates that more input is needed.
  321.                 /// The given byte array should not be changed, before needsInput() returns
  322.                 /// true again.
  323.                 /// </summary>
  324.                 /// <param name="input">
  325.                 /// the buffer containing the input data.
  326.                 /// </param>
  327.                 /// <param name="off">
  328.                 /// the start of the data.
  329.                 /// </param>
  330.                 /// <param name="len">
  331.                 /// the length of the data.
  332.                 /// </param>
  333.                 /// <exception cref="System.InvalidOperationException">
  334.                 /// if the buffer was finished() or ended() or if previous input is still pending.
  335.                 /// </exception>
  336.                 public void SetInput(byte[] input, int off, int len)
  337.                 {
  338.                         if ((state & IS_FINISHING) != 0) {
  339.                                 throw new InvalidOperationException("finish()/end() already called");
  340.                         }
  341.                         engine.SetInput(input, off, len);
  342.                 }
  343.                
  344.                 /// <summary>
  345.                 /// Sets the compression level.  There is no guarantee of the exact
  346.                 /// position of the change, but if you call this when needsInput is
  347.                 /// true the change of compression level will occur somewhere near
  348.                 /// before the end of the so far given input.
  349.                 /// </summary>
  350.                 /// <param name="lvl">
  351.                 /// the new compression level.
  352.                 /// </param>
  353.                 public void SetLevel(int lvl)
  354.                 {
  355.                         if (lvl == DEFAULT_COMPRESSION) {
  356.                                 lvl = 6;
  357.                         } else if (lvl < NO_COMPRESSION || lvl > BEST_COMPRESSION) {
  358.                                 throw new ArgumentOutOfRangeException("lvl");
  359.                         }
  360.                        
  361.                         if (level != lvl) {
  362.                                 level = lvl;
  363.                                 engine.SetLevel(lvl);
  364.                         }
  365.                 }
  366.                
  367.                 /// <summary>
  368.                 /// Get current compression level
  369.                 /// </summary>
  370.                 /// <returns>Returns the current compression level</returns>
  371.                 public int GetLevel() {
  372.                         return level;
  373.                 }
  374.                
  375.                 /// <summary>
  376.                 /// Sets the compression strategy. Strategy is one of
  377.                 /// DEFAULT_STRATEGY, HUFFMAN_ONLY and FILTERED.  For the exact
  378.                 /// position where the strategy is changed, the same as for
  379.                 /// setLevel() applies.
  380.                 /// </summary>
  381.                 /// <param name="strategy">
  382.                 /// The new compression strategy.
  383.                 /// </param>
  384.                 public void SetStrategy(DeflateStrategy strategy)
  385.                 {
  386.                         engine.Strategy = strategy;
  387.                 }
  388.                
  389.                 /// <summary>
  390.                 /// Deflates the current input block with to the given array.
  391.                 /// </summary>
  392.                 /// <param name="output">
  393.                 /// The buffer where compressed data is stored
  394.                 /// </param>
  395.                 /// <returns>
  396.                 /// The number of compressed bytes added to the output, or 0 if either
  397.                 /// needsInput() or finished() returns true or length is zero.
  398.                 /// </returns>
  399.                 public int Deflate(byte[] output)
  400.                 {
  401.                         return Deflate(output, 0, output.Length);
  402.                 }
  403.                
  404.                 /// <summary>
  405.                 /// Deflates the current input block to the given array.
  406.                 /// </summary>
  407.                 /// <param name="output">
  408.                 /// Buffer to store the compressed data.
  409.                 /// </param>
  410.                 /// <param name="offset">
  411.                 /// Offset into the output array.
  412.                 /// </param>
  413.                 /// <param name="length">
  414.                 /// The maximum number of bytes that may be stored.
  415.                 /// </param>
  416.                 /// <returns>
  417.                 /// The number of compressed bytes added to the output, or 0 if either
  418.                 /// needsInput() or finished() returns true or length is zero.
  419.                 /// </returns>
  420.                 /// <exception cref="System.InvalidOperationException">
  421.                 /// If end() was previously called.
  422.                 /// </exception>
  423.                 /// <exception cref="System.ArgumentOutOfRangeException">
  424.                 /// If offset and/or length don't match the array length.
  425.                 /// </exception>
  426.                 public int Deflate(byte[] output, int offset, int length)
  427.                 {
  428.                         int origLength = length;
  429.                        
  430.                         if (state == CLOSED_STATE) {
  431.                                 throw new InvalidOperationException("Deflater closed");
  432.                         }
  433.                        
  434.                         if (state < BUSY_STATE) {
  435.                                 /* output header */
  436.                                 int header = (DEFLATED +
  437.                                         ((DeflaterConstants.MAX_WBITS - 8) << 4)) << 8;
  438.                                 int level_flags = (level - 1) >> 1;
  439.                                 if (level_flags < 0 || level_flags > 3) {
  440.                                         level_flags = 3;
  441.                                 }
  442.                                 header |= level_flags << 6;
  443.                                 if ((state & IS_SETDICT) != 0) {
  444.                                         /* Dictionary was set */
  445.                                         header |= DeflaterConstants.PRESET_DICT;
  446.                                 }
  447.                                 header += 31 - (header % 31);
  448.                                
  449.                                
  450.                                 pending.WriteShortMSB(header);
  451.                                 if ((state & IS_SETDICT) != 0) {
  452.                                         int chksum = engine.Adler;
  453.                                         engine.ResetAdler();
  454.                                         pending.WriteShortMSB(chksum >> 16);
  455.                                         pending.WriteShortMSB(chksum & 0xffff);
  456.                                 }
  457.                                
  458.                                 state = BUSY_STATE | (state & (IS_FLUSHING | IS_FINISHING));
  459.                         }
  460.                        
  461.                         for (;;) {
  462.                                 int count = pending.Flush(output, offset, length);
  463.                                 offset   += count;
  464.                                 totalOut += count;
  465.                                 length   -= count;
  466.                                
  467.                                 if (length == 0 || state == FINISHED_STATE) {
  468.                                         break;
  469.                                 }
  470.                                
  471.                                 if (!engine.Deflate((state & IS_FLUSHING) != 0, (state & IS_FINISHING) != 0)) {
  472.                                         if (state == BUSY_STATE) {
  473.                                                 /* We need more input now */
  474.                                                 return origLength - length;
  475.                                         } else if (state == FLUSHING_STATE) {
  476.                                                 if (level != NO_COMPRESSION) {
  477.                                                         /* We have to supply some lookahead.  8 bit lookahead
  478.                                                          * is needed by the zlib inflater, and we must fill
  479.                                                          * the next byte, so that all bits are flushed.
  480.                                                          */
  481.                                                         int neededbits = 8 + ((-pending.BitCount) & 7);
  482.                                                         while (neededbits > 0) {
  483.                                                                 /* write a static tree block consisting solely of
  484.                                                                  * an EOF:
  485.                                                                  */
  486.                                                                 pending.WriteBits(2, 10);
  487.                                                                 neededbits -= 10;
  488.                                                         }
  489.                                                 }
  490.                                                 state = BUSY_STATE;
  491.                                         } else if (state == FINISHING_STATE) {
  492.                                                 pending.AlignToByte();
  493.  
  494.                                                 // Compressed data is complete.  Write footer information if required.
  495.                                                 if (!noZlibHeaderOrFooter) {
  496.                                                         int adler = engine.Adler;
  497.                                                         pending.WriteShortMSB(adler >> 16);
  498.                                                         pending.WriteShortMSB(adler & 0xffff);
  499.                                                 }
  500.                                                 state = FINISHED_STATE;
  501.                                         }
  502.                                 }
  503.                         }
  504.                         return origLength - length;
  505.                 }
  506.                
  507.                 /// <summary>
  508.                 /// Sets the dictionary which should be used in the deflate process.
  509.                 /// This call is equivalent to <code>setDictionary(dict, 0, dict.Length)</code>.
  510.                 /// </summary>
  511.                 /// <param name="dict">
  512.                 /// the dictionary.
  513.                 /// </param>
  514.                 /// <exception cref="System.InvalidOperationException">
  515.                 /// if setInput () or deflate () were already called or another dictionary was already set.
  516.                 /// </exception>
  517.                 public void SetDictionary(byte[] dict)
  518.                 {
  519.                         SetDictionary(dict, 0, dict.Length);
  520.                 }
  521.                
  522.                 /// <summary>
  523.                 /// Sets the dictionary which should be used in the deflate process.
  524.                 /// The dictionary is a byte array containing strings that are
  525.                 /// likely to occur in the data which should be compressed.  The
  526.                 /// dictionary is not stored in the compressed output, only a
  527.                 /// checksum.  To decompress the output you need to supply the same
  528.                 /// dictionary again.
  529.                 /// </summary>
  530.                 /// <param name="dict">
  531.                 /// The dictionary data
  532.                 /// </param>
  533.                 /// <param name="offset">
  534.                 /// An offset into the dictionary.
  535.                 /// </param>
  536.                 /// <param name="length">
  537.                 /// The length of the dictionary data to use
  538.                 /// </param>
  539.                 /// <exception cref="System.InvalidOperationException">
  540.                 /// If setInput () or deflate () were already called or another dictionary was already set.
  541.                 /// </exception>
  542.                 public void SetDictionary(byte[] dict, int offset, int length)
  543.                 {
  544.                         if (state != INIT_STATE) {
  545.                                 throw new InvalidOperationException();
  546.                         }
  547.                        
  548.                         state = SETDICT_STATE;
  549.                         engine.SetDictionary(dict, offset, length);
  550.                 }
  551.         }
  552. }
  553.  
downloadDeflater.cs Source code - Download PDF file browser Source code
Related Source Codes/Software:
Campus id - This system includes a campus dormitory management... 2015-04-10
ShopXP online system source code - ShopXP ShoppingNet online system source code infor... 2015-04-10
TOM cat demo in learning to speak - 实现汤米猫学说话的演示,即实现变声功能鲜豔,,,,,,,开发者说:自己经常来看code4app,看看... 2015-04-10
Another OpenFolder - Implements can commence in the middle of the scree... 2015-04-10
Bus query system - A simple bus query system. Can be divided into rou... 2015-04-10
Customer service management system - To a store and development of customer service man... 2015-04-10
OA office automation system - OA office automation system is tomorrow technology... 2015-04-10
Chapters novel divider (ChapCuttor) - Chapter divider (ChapCuttor) by & have spent Black... 2015-04-10
Graduation design file (graphics algorithm design) - Reference image effect algorithm design the softwa... 2015-04-10
CheatEngine5.6 source code - Game cheat the 5.6 version of the source code, it ... 2015-04-10
Online ZhaXunWang science formula query V1.0 - Including junior middle school and high school sci... 2016-10-20
Observe science formula query v1.0 - Observe network science formulas 2016-10-20
Lele v1.1 QQ login system - "Le Le QQ Group landing system Ver1.1" * Fix QQ ... 2016-10-20
The new network domain name host distribution system V1.0 - Use Notepad to open \Global.asa idc1688 into your ... 2016-10-20
QQ group v2.1 release system - ha ha - "Copyright" This system is based on the Wu Meng de... 2016-10-20
Interesting content ASPCMS v1.0 - Update log: 2016-10-20
Residential feng shui fortune-telling v1.0 - The project according to the orientation of the ar... 2016-10-20
The self-help chain home beautiful - The self-help chain home beautiful 2016-10-20
EXW FLASH web site management system v2.0 - EXW FLASH introduction: EXW website management sys... 2016-10-20
WAP2.0 web site management system V1.0 - System installation steps 1, install the QuickWAP ... 2016-10-20

 Back to top