1 // InflaterDynHeader.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 class InflaterDynHeader
54 static readonly int[] repMin = { 3, 3, 11 };
55 static readonly int[] repBits = { 2, 3, 7 };
60 InflaterHuffmanTree blTree;
63 int lnum, dnum, blnum, num;
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 };
71 public InflaterDynHeader()
75 public bool Decode(StreamManipulator input)
81 lnum = input.PeekBits(5);
87 // System.err.println("LNUM: "+lnum);
89 goto case DNUM; // fall through
91 dnum = input.PeekBits(5);
97 // System.err.println("DNUM: "+dnum);
99 litdistLens = new byte[num];
101 goto case BLNUM; // fall through
103 blnum = input.PeekBits(4);
109 blLens = new byte[19];
111 // System.err.println("BLNUM: "+blnum);
113 goto case BLLENS; // fall through
115 while (ptr < blnum) {
116 int len = input.PeekBits(3);
121 // System.err.println("blLens["+BL_ORDER[ptr]+"]: "+len);
122 blLens[BL_ORDER[ptr]] = (byte) len;
125 blTree = new InflaterHuffmanTree(blLens);
129 goto case LENS; // fall through
133 while (((symbol = blTree.GetSymbol(input)) & ~15) == 0) {
134 /* Normal case: symbol in [0..15] */
136 // System.err.println("litdistLens["+ptr+"]: "+symbol);
137 litdistLens[ptr++] = lastLen = (byte)symbol;
145 /* need more input ? */
150 /* otherwise repeat code */
153 // System.err.println("repeating zero");
157 throw new Exception();
160 repSymbol = symbol-16;
163 goto case REPS; // fall through
166 int bits = repBits[repSymbol];
167 int count = input.PeekBits(bits);
171 input.DropBits(bits);
172 count += repMin[repSymbol];
173 // System.err.println("litdistLens repeated: "+count);
175 if (ptr + count > num) {
176 throw new Exception();
178 while (count-- > 0) {
179 litdistLens[ptr++] = lastLen;
193 public InflaterHuffmanTree BuildLitLenTree()
195 byte[] litlenLens = new byte[lnum];
196 Array.Copy(litdistLens, 0, litlenLens, 0, lnum);
197 return new InflaterHuffmanTree(litlenLens);
200 public InflaterHuffmanTree BuildDistTree()
202 byte[] distLens = new byte[dnum];
203 Array.Copy(litdistLens, lnum, distLens, 0, dnum);
204 return new InflaterHuffmanTree(distLens);