Add !error command which checks NtStatus, Winerror and hResult.
[reactos.git] / irc / TechBot / Compression / InflaterHuffmanTree.cs
1 // InflaterHuffmanTree.cs
2 // Copyright (C) 2001 Mike Krueger
3 //
4 // This file was translated from java, it was part of the GNU Classpath
5 // Copyright (C) 2001 Free Software Foundation, Inc.
6 //
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.
11 //
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.
16 //
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.
20 //
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
24 // combination.
25 //
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.
37
38 using System;
39
40 using ICSharpCode.SharpZipLib.Zip.Compression.Streams;
41
42 namespace ICSharpCode.SharpZipLib.Zip.Compression
43 {
44
45 public class InflaterHuffmanTree
46 {
47 private static int MAX_BITLEN = 15;
48 private short[] tree;
49
50 public static InflaterHuffmanTree defLitLenTree, defDistTree;
51
52 static InflaterHuffmanTree()
53 {
54 try {
55 byte[] codeLengths = new byte[288];
56 int i = 0;
57 while (i < 144) {
58 codeLengths[i++] = 8;
59 }
60 while (i < 256) {
61 codeLengths[i++] = 9;
62 }
63 while (i < 280) {
64 codeLengths[i++] = 7;
65 }
66 while (i < 288) {
67 codeLengths[i++] = 8;
68 }
69 defLitLenTree = new InflaterHuffmanTree(codeLengths);
70
71 codeLengths = new byte[32];
72 i = 0;
73 while (i < 32) {
74 codeLengths[i++] = 5;
75 }
76 defDistTree = new InflaterHuffmanTree(codeLengths);
77 } catch (Exception) {
78 throw new ApplicationException("InflaterHuffmanTree: static tree length illegal");
79 }
80 }
81
82 /// <summary>
83 /// Constructs a Huffman tree from the array of code lengths.
84 /// </summary>
85 /// <param name = "codeLengths">
86 /// the array of code lengths
87 /// </param>
88 public InflaterHuffmanTree(byte[] codeLengths)
89 {
90 BuildTree(codeLengths);
91 }
92
93 private void BuildTree(byte[] codeLengths)
94 {
95 int[] blCount = new int[MAX_BITLEN + 1];
96 int[] nextCode = new int[MAX_BITLEN + 1];
97
98 for (int i = 0; i < codeLengths.Length; i++) {
99 int bits = codeLengths[i];
100 if (bits > 0) {
101 blCount[bits]++;
102 }
103 }
104
105 int code = 0;
106 int treeSize = 512;
107 for (int bits = 1; bits <= MAX_BITLEN; bits++) {
108 nextCode[bits] = code;
109 code += blCount[bits] << (16 - bits);
110 if (bits >= 10) {
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);
115 }
116 }
117 /* -jr comment this out! doesnt work for dynamic trees and pkzip 2.04g
118 if (code != 65536)
119 {
120 throw new Exception("Code lengths don't add up properly.");
121 }
122 */
123 /* Now create and fill the extra tables from longest to shortest
124 * bit len. This way the sub trees will be aligned.
125 */
126 tree = new short[treeSize];
127 int treePtr = 512;
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);
135 }
136 }
137
138 for (int i = 0; i < codeLengths.Length; i++) {
139 int bits = codeLengths[i];
140 if (bits == 0) {
141 continue;
142 }
143 code = nextCode[bits];
144 int revcode = DeflaterHuffman.BitReverse(code);
145 if (bits <= 9) {
146 do {
147 tree[revcode] = (short) ((i << 4) | bits);
148 revcode += 1 << bits;
149 } while (revcode < 512);
150 } else {
151 int subTree = tree[revcode & 511];
152 int treeLen = 1 << (subTree & 15);
153 subTree = -(subTree >> 4);
154 do {
155 tree[subTree | (revcode >> 9)] = (short) ((i << 4) | bits);
156 revcode += 1 << bits;
157 } while (revcode < treeLen);
158 }
159 nextCode[bits] = code + (1 << (16 - bits));
160 }
161
162 }
163
164 /// <summary>
165 /// Reads the next symbol from input. The symbol is encoded using the
166 /// huffman tree.
167 /// </summary>
168 /// <param name="input">
169 /// input the input source.
170 /// </param>
171 /// <returns>
172 /// the next symbol, or -1 if not enough input is available.
173 /// </returns>
174 public int GetSymbol(StreamManipulator input)
175 {
176 int lookahead, symbol;
177 if ((lookahead = input.PeekBits(9)) >= 0) {
178 if ((symbol = tree[lookahead]) >= 0) {
179 input.DropBits(symbol & 15);
180 return symbol >> 4;
181 }
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);
187 return symbol >> 4;
188 } else {
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);
194 return symbol >> 4;
195 } else {
196 return -1;
197 }
198 }
199 } else {
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);
205 return symbol >> 4;
206 } else {
207 return -1;
208 }
209 }
210 }
211 }
212 }
213