1 // InflaterHuffmanTree.cs
2 // Copyright (C) 2001 Mike Krueger
4 // This file was translated from java, it was part of the GNU Classpath
5 // Copyright (C) 2001 Free Software Foundation, Inc.
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License
9 // as published by the Free Software Foundation; either version 2
10 // of the License, or (at your option) any later version.
12 // This program is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 // Linking this library statically or dynamically with other modules is
22 // making a combined work based on this library. Thus, the terms and
23 // conditions of the GNU General Public License cover the whole
26 // As a special exception, the copyright holders of this library give you
27 // permission to link this library with independent modules to produce an
28 // executable, regardless of the license terms of these independent
29 // modules, and to copy and distribute the resulting executable under
30 // terms of your choice, provided that you also meet, for each linked
31 // independent module, the terms and conditions of the license of that
32 // module. An independent module is a module which is not derived from
33 // or based on this library. If you modify this library, you may extend
34 // this exception to your version of the library, but you are not
35 // obligated to do so. If you do not wish to do so, delete this
36 // exception statement from your version.
40 using ICSharpCode.SharpZipLib.Zip.Compression.Streams;
42 namespace ICSharpCode.SharpZipLib.Zip.Compression
45 public class InflaterHuffmanTree
47 private static int MAX_BITLEN = 15;
50 public static InflaterHuffmanTree defLitLenTree, defDistTree;
52 static InflaterHuffmanTree()
55 byte[] codeLengths = new byte[288];
69 defLitLenTree = new InflaterHuffmanTree(codeLengths);
71 codeLengths = new byte[32];
76 defDistTree = new InflaterHuffmanTree(codeLengths);
78 throw new ApplicationException("InflaterHuffmanTree: static tree length illegal");
83 /// Constructs a Huffman tree from the array of code lengths.
85 /// <param name = "codeLengths">
86 /// the array of code lengths
88 public InflaterHuffmanTree(byte[] codeLengths)
90 BuildTree(codeLengths);
93 private void BuildTree(byte[] codeLengths)
95 int[] blCount = new int[MAX_BITLEN + 1];
96 int[] nextCode = new int[MAX_BITLEN + 1];
98 for (int i = 0; i < codeLengths.Length; i++) {
99 int bits = codeLengths[i];
107 for (int bits = 1; bits <= MAX_BITLEN; bits++) {
108 nextCode[bits] = code;
109 code += blCount[bits] << (16 - bits);
111 /* We need an extra table for bit lengths >= 10. */
112 int start = nextCode[bits] & 0x1ff80;
113 int end = code & 0x1ff80;
114 treeSize += (end - start) >> (16 - bits);
117 /* -jr comment this out! doesnt work for dynamic trees and pkzip 2.04g
120 throw new Exception("Code lengths don't add up properly.");
123 /* Now create and fill the extra tables from longest to shortest
124 * bit len. This way the sub trees will be aligned.
126 tree = new short[treeSize];
128 for (int bits = MAX_BITLEN; bits >= 10; bits--) {
129 int end = code & 0x1ff80;
130 code -= blCount[bits] << (16 - bits);
131 int start = code & 0x1ff80;
132 for (int i = start; i < end; i += 1 << 7) {
133 tree[DeflaterHuffman.BitReverse(i)] = (short) ((-treePtr << 4) | bits);
134 treePtr += 1 << (bits-9);
138 for (int i = 0; i < codeLengths.Length; i++) {
139 int bits = codeLengths[i];
143 code = nextCode[bits];
144 int revcode = DeflaterHuffman.BitReverse(code);
147 tree[revcode] = (short) ((i << 4) | bits);
148 revcode += 1 << bits;
149 } while (revcode < 512);
151 int subTree = tree[revcode & 511];
152 int treeLen = 1 << (subTree & 15);
153 subTree = -(subTree >> 4);
155 tree[subTree | (revcode >> 9)] = (short) ((i << 4) | bits);
156 revcode += 1 << bits;
157 } while (revcode < treeLen);
159 nextCode[bits] = code + (1 << (16 - bits));
165 /// Reads the next symbol from input. The symbol is encoded using the
168 /// <param name="input">
169 /// input the input source.
172 /// the next symbol, or -1 if not enough input is available.
174 public int GetSymbol(StreamManipulator input)
176 int lookahead, symbol;
177 if ((lookahead = input.PeekBits(9)) >= 0) {
178 if ((symbol = tree[lookahead]) >= 0) {
179 input.DropBits(symbol & 15);
182 int subtree = -(symbol >> 4);
183 int bitlen = symbol & 15;
184 if ((lookahead = input.PeekBits(bitlen)) >= 0) {
185 symbol = tree[subtree | (lookahead >> 9)];
186 input.DropBits(symbol & 15);
189 int bits = input.AvailableBits;
190 lookahead = input.PeekBits(bits);
191 symbol = tree[subtree | (lookahead >> 9)];
192 if ((symbol & 15) <= bits) {
193 input.DropBits(symbol & 15);
200 int bits = input.AvailableBits;
201 lookahead = input.PeekBits(bits);
202 symbol = tree[lookahead];
203 if (symbol >= 0 && (symbol & 15) <= bits) {
204 input.DropBits(symbol & 15);