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.
39 using ICSharpCode.SharpZipLib.Silverlight.Zip.Compression.Streams;
41 namespace ICSharpCode.SharpZipLib.Silverlight.Zip.Compression
43 class InflaterDynHeader
53 static readonly int[] repMin = { 3, 3, 11 };
54 static readonly int[] repBits = { 2, 3, 7 };
56 static readonly int[] BL_ORDER =
57 { 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15 };
62 public InflaterDynHeader()
67 public bool Decode(StreamManipulator input)
73 lnum = input.PeekBits(5);
79 // System.err.println("LNUM: "+lnum);
81 goto case DNUM; // fall through
83 dnum = input.PeekBits(5);
89 // System.err.println("DNUM: "+dnum);
91 litdistLens = new byte[num];
93 goto case BLNUM; // fall through
95 blnum = input.PeekBits(4);
101 blLens = new byte[19];
103 // System.err.println("BLNUM: "+blnum);
105 goto case BLLENS; // fall through
107 while (ptr < blnum) {
108 int len = input.PeekBits(3);
113 // System.err.println("blLens["+BL_ORDER[ptr]+"]: "+len);
114 blLens[BL_ORDER[ptr]] = (byte) len;
117 blTree = new InflaterHuffmanTree(blLens);
121 goto case LENS; // fall through
125 while (((symbol = blTree.GetSymbol(input)) & ~15) == 0) {
126 /* Normal case: symbol in [0..15] */
128 // System.err.println("litdistLens["+ptr+"]: "+symbol);
129 litdistLens[ptr++] = lastLen = (byte)symbol;
137 /* need more input ? */
142 /* otherwise repeat code */
145 // System.err.println("repeating zero");
149 throw new SharpZipBaseException();
152 repSymbol = symbol-16;
155 goto case REPS; // fall through
158 int bits = repBits[repSymbol];
159 int count = input.PeekBits(bits);
163 input.DropBits(bits);
164 count += repMin[repSymbol];
165 // System.err.println("litdistLens repeated: "+count);
167 if (ptr + count > num) {
168 throw new SharpZipBaseException();
170 while (count-- > 0) {
171 litdistLens[ptr++] = lastLen;
185 public InflaterHuffmanTree BuildLitLenTree()
187 byte[] litlenLens = new byte[lnum];
188 Array.Copy(litdistLens, 0, litlenLens, 0, lnum);
189 return new InflaterHuffmanTree(litlenLens);
192 public InflaterHuffmanTree BuildDistTree()
194 byte[] distLens = new byte[dnum];
195 Array.Copy(litdistLens, lnum, distLens, 0, dnum);
196 return new InflaterHuffmanTree(distLens);
199 #region Instance Fields
203 InflaterHuffmanTree blTree;
206 int lnum, dnum, blnum, num;