1 /* java.util.zip.InflaterDynHeader
2    Copyright (C) 2001 Free Software Foundation, Inc.
3 
4 This file is part of GNU Classpath.
5 
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.
10 
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.
15 
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.
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 package java.util.zip;
39 
40 class InflaterDynHeader
41 {
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;
48 
49   private static final int repMin[]  = { 3, 3, 11 };
50   private static final int repBits[] = { 2, 3,  7 };
51 
52 
53   private byte[] blLens;
54   private byte[] litdistLens;
55 
56   private InflaterHuffmanTree blTree;
57 
58   private int mode;
59   private int lnum, dnum, blnum, num;
60   private int repSymbol;
61   private byte lastLen;
62   private int ptr;
63 
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 };
66 
InflaterDynHeader()67   public InflaterDynHeader()
68   {
69   }
70 
decode(StreamManipulator input)71   public boolean decode(StreamManipulator input) throws DataFormatException
72   {
73   decode_loop:
74     for (;;)
75       {
76 	switch (mode)
77 	  {
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)
111 	      {
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++;
119 	      }
120 	    blTree = new InflaterHuffmanTree(blLens);
121 	    blLens = null;
122 	    ptr = 0;
123 	    mode = LENS;
124 	    /* fall through */
125 	  case LENS:
126 	    {
127 	      int symbol;
128 	      while (((symbol = blTree.getSymbol(input)) & ~15) == 0)
129 		{
130 		  /* Normal case: symbol in [0..15] */
131 
132 //  		  System.err.println("litdistLens["+ptr+"]: "+symbol);
133 		  litdistLens[ptr++] = lastLen = (byte) symbol;
134 
135 		  if (ptr == num)
136 		    {
137 		      /* Finished */
138 		      return true;
139 		    }
140 		}
141 
142 	      /* need more input ? */
143 	      if (symbol < 0)
144 		return false;
145 
146 	      /* otherwise repeat code */
147 	      if (symbol >= 17)
148 		{
149 		  /* repeat zero */
150 //  		  System.err.println("repeating zero");
151 		  lastLen = 0;
152 		}
153 	      else
154 		{
155 		  if (ptr == 0)
156 		    throw new DataFormatException();
157 		}
158 	      repSymbol = symbol-16;
159 	      mode = REPS;
160 	    }
161 	    /* fall through */
162 
163 	  case REPS:
164 	    {
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);
172 
173 	      if (ptr + count > num)
174 		throw new DataFormatException();
175 	      while (count-- > 0)
176 		litdistLens[ptr++] = lastLen;
177 
178 	      if (ptr == num)
179 		{
180 		  /* Finished */
181 		  return true;
182 		}
183 	    }
184 	    mode = LENS;
185 	    continue decode_loop;
186 	  }
187       }
188   }
189 
buildLitLenTree()190   public InflaterHuffmanTree buildLitLenTree() throws DataFormatException
191   {
192     byte[] litlenLens = new byte[lnum];
193     System.arraycopy(litdistLens, 0, litlenLens, 0, lnum);
194     return new InflaterHuffmanTree(litlenLens);
195   }
196 
buildDistTree()197   public InflaterHuffmanTree buildDistTree() throws DataFormatException
198   {
199     byte[] distLens = new byte[dnum];
200     System.arraycopy(litdistLens, lnum, distLens, 0, dnum);
201     return new InflaterHuffmanTree(distLens);
202   }
203 }
204