Revert r20089, thanks SVN for not a nice conflict resolution!
[reactos.git] / irc / TechBot / Compression / InflaterDynHeader.cs
1 // InflaterDynHeader.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 class InflaterDynHeader
46 {
47 const int LNUM = 0;
48 const int DNUM = 1;
49 const int BLNUM = 2;
50 const int BLLENS = 3;
51 const int LENS = 4;
52 const int REPS = 5;
53
54 static readonly int[] repMin = { 3, 3, 11 };
55 static readonly int[] repBits = { 2, 3, 7 };
56
57 byte[] blLens;
58 byte[] litdistLens;
59
60 InflaterHuffmanTree blTree;
61
62 int mode;
63 int lnum, dnum, blnum, num;
64 int repSymbol;
65 byte lastLen;
66 int ptr;
67
68 static readonly int[] BL_ORDER =
69 { 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15 };
70
71 public InflaterDynHeader()
72 {
73 }
74
75 public bool Decode(StreamManipulator input)
76 {
77 decode_loop:
78 for (;;) {
79 switch (mode) {
80 case LNUM:
81 lnum = input.PeekBits(5);
82 if (lnum < 0) {
83 return false;
84 }
85 lnum += 257;
86 input.DropBits(5);
87 // System.err.println("LNUM: "+lnum);
88 mode = DNUM;
89 goto case DNUM; // fall through
90 case DNUM:
91 dnum = input.PeekBits(5);
92 if (dnum < 0) {
93 return false;
94 }
95 dnum++;
96 input.DropBits(5);
97 // System.err.println("DNUM: "+dnum);
98 num = lnum+dnum;
99 litdistLens = new byte[num];
100 mode = BLNUM;
101 goto case BLNUM; // fall through
102 case BLNUM:
103 blnum = input.PeekBits(4);
104 if (blnum < 0) {
105 return false;
106 }
107 blnum += 4;
108 input.DropBits(4);
109 blLens = new byte[19];
110 ptr = 0;
111 // System.err.println("BLNUM: "+blnum);
112 mode = BLLENS;
113 goto case BLLENS; // fall through
114 case BLLENS:
115 while (ptr < blnum) {
116 int len = input.PeekBits(3);
117 if (len < 0) {
118 return false;
119 }
120 input.DropBits(3);
121 // System.err.println("blLens["+BL_ORDER[ptr]+"]: "+len);
122 blLens[BL_ORDER[ptr]] = (byte) len;
123 ptr++;
124 }
125 blTree = new InflaterHuffmanTree(blLens);
126 blLens = null;
127 ptr = 0;
128 mode = LENS;
129 goto case LENS; // fall through
130 case LENS:
131 {
132 int symbol;
133 while (((symbol = blTree.GetSymbol(input)) & ~15) == 0) {
134 /* Normal case: symbol in [0..15] */
135
136 // System.err.println("litdistLens["+ptr+"]: "+symbol);
137 litdistLens[ptr++] = lastLen = (byte)symbol;
138
139 if (ptr == num) {
140 /* Finished */
141 return true;
142 }
143 }
144
145 /* need more input ? */
146 if (symbol < 0) {
147 return false;
148 }
149
150 /* otherwise repeat code */
151 if (symbol >= 17) {
152 /* repeat zero */
153 // System.err.println("repeating zero");
154 lastLen = 0;
155 } else {
156 if (ptr == 0) {
157 throw new Exception();
158 }
159 }
160 repSymbol = symbol-16;
161 }
162 mode = REPS;
163 goto case REPS; // fall through
164 case REPS:
165 {
166 int bits = repBits[repSymbol];
167 int count = input.PeekBits(bits);
168 if (count < 0) {
169 return false;
170 }
171 input.DropBits(bits);
172 count += repMin[repSymbol];
173 // System.err.println("litdistLens repeated: "+count);
174
175 if (ptr + count > num) {
176 throw new Exception();
177 }
178 while (count-- > 0) {
179 litdistLens[ptr++] = lastLen;
180 }
181
182 if (ptr == num) {
183 /* Finished */
184 return true;
185 }
186 }
187 mode = LENS;
188 goto decode_loop;
189 }
190 }
191 }
192
193 public InflaterHuffmanTree BuildLitLenTree()
194 {
195 byte[] litlenLens = new byte[lnum];
196 Array.Copy(litdistLens, 0, litlenLens, 0, lnum);
197 return new InflaterHuffmanTree(litlenLens);
198 }
199
200 public InflaterHuffmanTree BuildDistTree()
201 {
202 byte[] distLens = new byte[dnum];
203 Array.Copy(litdistLens, lnum, distLens, 0, dnum);
204 return new InflaterHuffmanTree(distLens);
205 }
206 }
207 }