/Utilities/Compression/Inflaters/InflaterHuffmanTree.cs
C# | 259 lines | 191 code | 19 blank | 49 comment | 23 complexity | eff5f103efb7fcdcb2f38cdff32faaaa 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.Deflaters;
- using Delta.Utilities.Compression.Streams;
-
- namespace Delta.Utilities.Compression.Inflaters
- {
- /// <summary>
- /// Huffman tree used for inflation
- /// </summary>
- public class InflaterHuffmanTree
- {
- #region Constants
- /// <summary>
- /// Maximum bit length
- /// </summary>
- private const int MaximumBitLength = 15;
- #endregion
-
- #region defLitLenTree (Static)
- /// <summary>
- /// Literal length tree
- /// </summary>
- public static InflaterHuffmanTree defLitLenTree;
- #endregion
-
- #region defDistTree (Static)
- /// <summary>
- /// Distance tree
- /// </summary>
- public static InflaterHuffmanTree defDistTree;
- #endregion
-
- #region Private
-
- #region tree (Private)
- /// <summary>
- /// Tree
- /// </summary>
- private short[] tree;
- #endregion
-
- #endregion
-
- #region Constructors
- /// <summary>
- /// Create inflater huffman tree
- /// </summary>
- static InflaterHuffmanTree()
- {
- try
- {
- byte[] codeLengths = new byte[288];
- int i = 0;
- while (i < 144)
- {
- codeLengths[i++] = 8;
- } // while (i)
- while (i < 256)
- {
- codeLengths[i++] = 9;
- } // while (i)
- while (i < 280)
- {
- codeLengths[i++] = 7;
- } // while (i)
- while (i < 288)
- {
- codeLengths[i++] = 8;
- } // while (i)
- defLitLenTree = new InflaterHuffmanTree(codeLengths);
-
- codeLengths = new byte[32];
- i = 0;
- while (i < 32)
- {
- codeLengths[i++] = 5;
- } // while (i)
- defDistTree = new InflaterHuffmanTree(codeLengths);
- } // try
- catch (Exception)
- {
- throw new CompressionException(
- "InflaterHuffmanTree: static tree length illegal");
- } // catch (Exception)
- }
-
- /// <summary>
- /// Constructs a Huffman tree from the array of code lengths.
- /// </summary>
- /// <param name = "codeLengths">
- /// the array of code lengths
- /// </param>
- public InflaterHuffmanTree(byte[] codeLengths)
- {
- BuildTree(codeLengths);
- }
- #endregion
-
- #region GetSymbol (Public)
- /// <summary>
- /// Reads the next symbol from input. The symbol is encoded using the
- /// huffman tree.
- /// </summary>
- /// <param name="input">
- /// input the input source.
- /// </param>
- /// <returns>
- /// the next symbol, or -1 if not enough input is available.
- /// </returns>
- public int GetSymbol(StreamManipulator input)
- {
- int lookahead, symbol;
- if ((lookahead = input.PeekBits(9)) >= 0)
- {
- if ((symbol = tree[lookahead]) >= 0)
- {
- input.DropBits(symbol & 15);
- return symbol >> 4;
- } // if (symbol)
- int subtree = -(symbol >> 4);
- int bitlen = symbol & 15;
- if ((lookahead = input.PeekBits(bitlen)) >= 0)
- {
- symbol = tree[subtree | (lookahead >> 9)];
- input.DropBits(symbol & 15);
- return symbol >> 4;
- } // if (lookahead)
- else
- {
- int bits = input.AvailableBits;
- lookahead = input.PeekBits(bits);
- symbol = tree[subtree | (lookahead >> 9)];
- if ((symbol & 15) <= bits)
- {
- input.DropBits(symbol & 15);
- return symbol >> 4;
- } // if (symbol)
- else
- {
- return -1;
- } // else
- } // else
- } // if (lookahead)
- else
- {
- int bits = input.AvailableBits;
- lookahead = input.PeekBits(bits);
- symbol = tree[lookahead];
- if (symbol >= 0 && (symbol & 15) <= bits)
- {
- input.DropBits(symbol & 15);
- return symbol >> 4;
- } // if (symbol)
- else
- {
- return -1;
- } // else
- } // else
- }
- #endregion
-
- #region Methods (Private)
-
- #region BuildTree
- /// <summary>
- /// Build tree
- /// </summary>
- /// <param name="codeLengths">Code lengths</param>
- private void BuildTree(byte[] codeLengths)
- {
- int[] blCount = new int[MaximumBitLength + 1];
- int[] nextCode = new int[MaximumBitLength + 1];
-
- for (int i = 0; i < codeLengths.Length; i++)
- {
- int bits = codeLengths[i];
- if (bits > 0)
- {
- blCount[bits]++;
- } // if (bits)
- } // for (int)
-
- int code = 0;
- int treeSize = 512;
- for (int bits = 1; bits <= MaximumBitLength; bits++)
- {
- nextCode[bits] = code;
- code += blCount[bits] << (16 - bits);
- if (bits >= 10)
- {
- /* We need an extra table for bit lengths >= 10. */
- int start = nextCode[bits] & 0x1ff80;
- int end = code & 0x1ff80;
- treeSize += (end - start) >> (16 - bits);
- } // if (bits)
- } // for (bits)
-
- // -jr comment this out! doesnt work for dynamic trees and pkzip 2.04g
- //if (code != 65536)
- //{
- // throw new CompressionException("Code lengths don't add up properly.");
- //}
-
- // Now create and fill the extra tables from longest to shortest
- // bit len. This way the sub trees will be aligned.
- tree = new short[treeSize];
- int treePtr = 512;
- for (int bits = MaximumBitLength; bits >= 10; bits--)
- {
- int end = code & 0x1ff80;
- code -= blCount[bits] << (16 - bits);
- int start = code & 0x1ff80;
- for (int i = start; i < end; i += 1 << 7)
- {
- tree[DeflaterHuffman.BitReverse(i)] = (short)((-treePtr << 4) | bits);
- treePtr += 1 << (bits - 9);
- } // for (int)
- } // for (bits)
-
- for (int i = 0; i < codeLengths.Length; i++)
- {
- int bits = codeLengths[i];
- if (bits == 0)
- {
- continue;
- } // if (bits)
- code = nextCode[bits];
- int revcode = DeflaterHuffman.BitReverse(code);
- if (bits <= 9)
- {
- do
- {
- tree[revcode] = (short)((i << 4) | bits);
- revcode += 1 << bits;
- } while (revcode < 512);
- } // if (bits)
- else
- {
- int subTree = tree[revcode & 511];
- int treeLen = 1 << (subTree & 15);
- subTree = -(subTree >> 4);
- do
- {
- tree[subTree | (revcode >> 9)] = (short)((i << 4) | bits);
- revcode += 1 << bits;
- } while (revcode < treeLen);
- } // else
- nextCode[bits] = code + (1 << (16 - bits));
- } // for (int)
- }
- #endregion
-
- #endregion
- }
- }