/Utilities/Compression/Inflaters/InflaterDynHeader.cs
C# | 300 lines | 195 code | 31 blank | 74 comment | 20 complexity | 6f46816e7ff09a4c97bffd279739bdfb MD5 | raw file
Possible License(s): Apache-2.0
- // Based on Mike Krueger's SharpZipLib, Copyright (C) 2001 (GNU license).
- // Authors of the original java version: Jochen Hoenicke, John Leuner
- // See http://www.ISeeSharpCode.com for more information.
-
- using System;
- using Delta.Utilities.Compression.Streams;
-
- namespace Delta.Utilities.Compression.Inflaters
- {
- /// <summary>
- /// Inflater dyn header
- /// </summary>
- internal class InflaterDynHeader
- {
- #region Constants
- private const int LNUM = 0;
-
- private const int DNUM = 1;
-
- private const int BLNUM = 2;
-
- private const int BLLENS = 3;
-
- private const int LENS = 4;
-
- private const int REPS = 5;
-
- /// <summary>
- /// Rep minimum
- /// </summary>
- private static readonly int[] repMin = {
- 3, 3, 11
- };
-
- /// <summary>
- /// Rep bits
- /// </summary>
- private static readonly int[] repBits = {
- 2, 3, 7
- };
-
- /// <summary>
- /// BL_ ORDER
- /// </summary>
- private static readonly int[] BL_ORDER =
- {
- 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15
- };
- #endregion
-
- #region Private
-
- #region blLens (Private)
- /// <summary>
- /// Bl lens
- /// </summary>
- private byte[] blLens;
- #endregion
-
- #region litdistLens (Private)
- /// <summary>
- /// Litdist lens
- /// </summary>
- private byte[] litdistLens;
- #endregion
-
- #region blTree (Private)
- /// <summary>
- /// Bl tree
- /// </summary>
- private InflaterHuffmanTree blTree;
- #endregion
-
- #region mode (Private)
- /// <summary>
- /// Mode
- /// </summary>
- private int mode;
- #endregion
-
- #region lnum (Private)
- /// <summary>
- /// Lnum
- /// </summary>
- private int lnum;
- #endregion
-
- #region dnum (Private)
- /// <summary>
- /// Lnum
- /// </summary>
- private int dnum;
- #endregion
-
- #region blnum (Private)
- /// <summary>
- /// Lnum
- /// </summary>
- private int blnum;
- #endregion
-
- #region num (Private)
- /// <summary>
- /// Lnum
- /// </summary>
- private int num;
- #endregion
-
- #region repSymbol (Private)
- /// <summary>
- /// Rep symbol
- /// </summary>
- private int repSymbol;
- #endregion
-
- #region lastLen (Private)
- /// <summary>
- /// Last len
- /// </summary>
- private byte lastLen;
- #endregion
-
- #region ptr (Private)
- /// <summary>
- /// Ptr
- /// </summary>
- private int ptr;
- #endregion
-
- #endregion
-
- #region Decode (Public)
- /// <summary>
- /// Decode
- /// </summary>
- /// <param name="input">Input</param>
- /// <returns>True if we are done decoding.</returns>
- public bool Decode(StreamManipulator input)
- {
- decode_loop:
- for (;;)
- {
- switch (mode)
- {
- case LNUM:
- lnum = input.PeekBits(5);
- if (lnum < 0)
- {
- return false;
- } // if (lnum)
- lnum += 257;
- input.DropBits(5);
- //Console.WriteLine("LNUM: "+lnum);
- mode = DNUM;
- goto case DNUM; // fall through
- case DNUM:
- dnum = input.PeekBits(5);
- if (dnum < 0)
- {
- return false;
- } // if (dnum)
- dnum++;
- input.DropBits(5);
- //Console.WriteLine("DNUM: "+dnum);
- num = lnum + dnum;
- litdistLens = new byte[num];
- mode = BLNUM;
- goto case BLNUM; // fall through
- case BLNUM:
- blnum = input.PeekBits(4);
- if (blnum < 0)
- {
- return false;
- } // if (blnum)
- blnum += 4;
- input.DropBits(4);
- blLens = new byte[19];
- ptr = 0;
- //Console.WriteLine("BLNUM: "+blnum);
- mode = BLLENS;
- goto case BLLENS; // fall through
- case BLLENS:
- while (ptr < blnum)
- {
- int len = input.PeekBits(3);
- if (len < 0)
- {
- return false;
- } // if (len)
- input.DropBits(3);
- //Console.WriteLine("blLens["+BLOrder[ptr]+"]: "+len);
- blLens[BL_ORDER[ptr]] = (byte)len;
- ptr++;
- } // while (ptr)
- blTree = new InflaterHuffmanTree(blLens);
- blLens = null;
- ptr = 0;
- mode = LENS;
- goto case LENS; // fall through
- case LENS:
- {
- int symbol;
- while (((symbol = blTree.GetSymbol(input)) & ~15) == 0)
- {
- // Normal case: symbol in [0..15]
- //Console.WriteLine("litdistLens["+ptr+"]: "+symbol);
- litdistLens[ptr++] = lastLen = (byte)symbol;
-
- if (ptr == num)
- {
- // Finished
- return true;
- } // if (ptr)
- } // while ()
-
- // Need more input?
- if (symbol < 0)
- {
- return false;
- } // if (symbol)
-
- // Otherwise repeat code
- if (symbol >= 17)
- {
- // Repeat zero
- //Console.WriteLine("repeating zero");
- lastLen = 0;
- } // if (symbol)
- else
- {
- if (ptr == 0)
- {
- throw new CompressionException();
- } // if (ptr)
- } // else
- repSymbol = symbol - 16;
- } // block
- mode = REPS;
- goto case REPS; // fall through
- case REPS:
- {
- int bits = repBits[repSymbol];
- int count = input.PeekBits(bits);
- if (count < 0)
- {
- return false;
- } // if (count)
- input.DropBits(bits);
- count += repMin[repSymbol];
- //Console.WriteLine("litdistLens repeated: "+count);
-
- if (ptr + count > num)
- {
- throw new CompressionException();
- } // if (ptr)
- while (count-- > 0)
- {
- litdistLens[ptr++] = lastLen;
- } // while (count--)
-
- if (ptr == num)
- {
- // Finished
- return true;
- } // if (ptr)
- } // block
- mode = LENS;
- goto decode_loop;
- } // switch
- } // for
- }
- #endregion
-
- #region BuildLitLenTree (Public)
- /// <summary>
- /// Build lit len tree
- /// </summary>
- /// <returns>Inflater huffman tree</returns>
- public InflaterHuffmanTree BuildLitLenTree()
- {
- byte[] litlenLens = new byte[lnum];
- Array.Copy(litdistLens, 0, litlenLens, 0, lnum);
- return new InflaterHuffmanTree(litlenLens);
- }
- #endregion
-
- #region BuildDistTree (Public)
- /// <summary>
- /// Build dist tree
- /// </summary>
- /// <returns>Inflater huffman tree</returns>
- public InflaterHuffmanTree BuildDistTree()
- {
- byte[] distLens = new byte[dnum];
- Array.Copy(litdistLens, lnum, distLens, 0, dnum);
- return new InflaterHuffmanTree(distLens);
- }
- #endregion
- }
- }