Dead
[official-gcc.git] / gomp-20050608-branch / libjava / classpath / java / util / zip / InflaterDynHeader.java
blobbff84a894d8389aef0b5c6c6a2fadb300f0bb941
1 /* java.util.zip.InflaterDynHeader
2 Copyright (C) 2001 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 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
24 combination.
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. */
38 package java.util.zip;
40 class InflaterDynHeader
42 private static final int LNUM = 0;
43 private static final int DNUM = 1;
44 private static final int BLNUM = 2;
45 private static final int BLLENS = 3;
46 private static final int LENS = 4;
47 private static final int REPS = 5;
49 private static final int repMin[] = { 3, 3, 11 };
50 private static final int repBits[] = { 2, 3, 7 };
53 private byte[] blLens;
54 private byte[] litdistLens;
56 private InflaterHuffmanTree blTree;
58 private int mode;
59 private int lnum, dnum, blnum, num;
60 private int repSymbol;
61 private byte lastLen;
62 private int ptr;
64 private static final int[] BL_ORDER =
65 { 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15 };
67 public InflaterDynHeader()
71 public boolean decode(StreamManipulator input) throws DataFormatException
73 decode_loop:
74 for (;;)
76 switch (mode)
78 case LNUM:
79 lnum = input.peekBits(5);
80 if (lnum < 0)
81 return false;
82 lnum += 257;
83 input.dropBits(5);
84 // System.err.println("LNUM: "+lnum);
85 mode = DNUM;
86 /* fall through */
87 case DNUM:
88 dnum = input.peekBits(5);
89 if (dnum < 0)
90 return false;
91 dnum++;
92 input.dropBits(5);
93 // System.err.println("DNUM: "+dnum);
94 num = lnum+dnum;
95 litdistLens = new byte[num];
96 mode = BLNUM;
97 /* fall through */
98 case BLNUM:
99 blnum = input.peekBits(4);
100 if (blnum < 0)
101 return false;
102 blnum += 4;
103 input.dropBits(4);
104 blLens = new byte[19];
105 ptr = 0;
106 // System.err.println("BLNUM: "+blnum);
107 mode = BLLENS;
108 /* fall through */
109 case BLLENS:
110 while (ptr < blnum)
112 int len = input.peekBits(3);
113 if (len < 0)
114 return false;
115 input.dropBits(3);
116 // System.err.println("blLens["+BL_ORDER[ptr]+"]: "+len);
117 blLens[BL_ORDER[ptr]] = (byte) len;
118 ptr++;
120 blTree = new InflaterHuffmanTree(blLens);
121 blLens = null;
122 ptr = 0;
123 mode = LENS;
124 /* fall through */
125 case LENS:
127 int symbol;
128 while (((symbol = blTree.getSymbol(input)) & ~15) == 0)
130 /* Normal case: symbol in [0..15] */
132 // System.err.println("litdistLens["+ptr+"]: "+symbol);
133 litdistLens[ptr++] = lastLen = (byte) symbol;
135 if (ptr == num)
137 /* Finished */
138 return true;
142 /* need more input ? */
143 if (symbol < 0)
144 return false;
146 /* otherwise repeat code */
147 if (symbol >= 17)
149 /* repeat zero */
150 // System.err.println("repeating zero");
151 lastLen = 0;
153 else
155 if (ptr == 0)
156 throw new DataFormatException();
158 repSymbol = symbol-16;
159 mode = REPS;
161 /* fall through */
163 case REPS:
165 int bits = repBits[repSymbol];
166 int count = input.peekBits(bits);
167 if (count < 0)
168 return false;
169 input.dropBits(bits);
170 count += repMin[repSymbol];
171 // System.err.println("litdistLens repeated: "+count);
173 if (ptr + count > num)
174 throw new DataFormatException();
175 while (count-- > 0)
176 litdistLens[ptr++] = lastLen;
178 if (ptr == num)
180 /* Finished */
181 return true;
184 mode = LENS;
185 continue decode_loop;
190 public InflaterHuffmanTree buildLitLenTree() throws DataFormatException
192 byte[] litlenLens = new byte[lnum];
193 System.arraycopy(litdistLens, 0, litlenLens, 0, lnum);
194 return new InflaterHuffmanTree(litlenLens);
197 public InflaterHuffmanTree buildDistTree() throws DataFormatException
199 byte[] distLens = new byte[dnum];
200 System.arraycopy(litdistLens, lnum, distLens, 0, dnum);
201 return new InflaterHuffmanTree(distLens);