OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libjava/] [classpath/] [java/] [util/] [zip/] [InflaterHuffmanTree.java] - Blame information for rev 771

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 771 jeremybenn
/* InflaterHuffmanTree.java --
2
   Copyright (C) 2001, 2004  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 InflaterHuffmanTree
41
{
42
  private static final int MAX_BITLEN = 15;
43
 
44
  private short[] tree;
45
 
46
  static InflaterHuffmanTree defLitLenTree, defDistTree;
47
 
48
  static
49
  {
50
    try
51
      {
52
        byte[] codeLengths = new byte[288];
53
        int i = 0;
54
        while (i < 144)
55
          codeLengths[i++] = 8;
56
        while (i < 256)
57
          codeLengths[i++] = 9;
58
        while (i < 280)
59
          codeLengths[i++] = 7;
60
        while (i < 288)
61
          codeLengths[i++] = 8;
62
        defLitLenTree = new InflaterHuffmanTree(codeLengths);
63
 
64
        codeLengths = new byte[32];
65
        i = 0;
66
        while (i < 32)
67
          codeLengths[i++] = 5;
68
        defDistTree = new InflaterHuffmanTree(codeLengths);
69
      }
70
    catch (DataFormatException ex)
71
      {
72
        throw new InternalError
73
          ("InflaterHuffmanTree: static tree length illegal");
74
      }
75
  }
76
 
77
  /**
78
   * Constructs a Huffman tree from the array of code lengths.
79
   *
80
   * @param codeLengths the array of code lengths
81
   */
82
  InflaterHuffmanTree(byte[] codeLengths) throws DataFormatException
83
  {
84
    buildTree(codeLengths);
85
  }
86
 
87
  private void buildTree(byte[] codeLengths) throws DataFormatException
88
  {
89
    int[] blCount = new int[MAX_BITLEN+1];
90
    int[] nextCode = new int[MAX_BITLEN+1];
91
    for (int i = 0; i < codeLengths.length; i++)
92
      {
93
        int bits = codeLengths[i];
94
        if (bits > 0)
95
          blCount[bits]++;
96
      }
97
 
98
    int code = 0;
99
    int treeSize = 512;
100
    for (int bits = 1; bits <= MAX_BITLEN; bits++)
101
      {
102
        nextCode[bits] = code;
103
        code += blCount[bits] << (16 - bits);
104
        if (bits >= 10)
105
          {
106
            /* We need an extra table for bit lengths >= 10. */
107
            int start = nextCode[bits] & 0x1ff80;
108
            int end   = code & 0x1ff80;
109
            treeSize += (end - start) >> (16 - bits);
110
          }
111
      }
112
    if (code != 65536)
113
      throw new DataFormatException("Code lengths don't add up properly.");
114
 
115
    /* Now create and fill the extra tables from longest to shortest
116
     * bit len.  This way the sub trees will be aligned.
117
     */
118
    tree = new short[treeSize];
119
    int treePtr = 512;
120
    for (int bits = MAX_BITLEN; bits >= 10; bits--)
121
      {
122
        int end   = code & 0x1ff80;
123
        code -= blCount[bits] << (16 - bits);
124
        int start = code & 0x1ff80;
125
        for (int i = start; i < end; i += 1 << 7)
126
          {
127
            tree[DeflaterHuffman.bitReverse(i)]
128
              = (short) ((-treePtr << 4) | bits);
129
            treePtr += 1 << (bits-9);
130
          }
131
      }
132
 
133
    for (int i = 0; i < codeLengths.length; i++)
134
      {
135
        int bits = codeLengths[i];
136
        if (bits == 0)
137
          continue;
138
        code = nextCode[bits];
139
        int revcode = DeflaterHuffman.bitReverse(code);
140
        if (bits <= 9)
141
          {
142
            do
143
              {
144
                tree[revcode] = (short) ((i << 4) | bits);
145
                revcode += 1 << bits;
146
              }
147
            while (revcode < 512);
148
          }
149
        else
150
          {
151
            int subTree = tree[revcode & 511];
152
            int treeLen = 1 << (subTree & 15);
153
            subTree = -(subTree >> 4);
154
            do
155
              {
156
                tree[subTree | (revcode >> 9)] = (short) ((i << 4) | bits);
157
                revcode += 1 << bits;
158
              }
159
            while (revcode < treeLen);
160
          }
161
        nextCode[bits] = code + (1 << (16 - bits));
162
      }
163
  }
164
 
165
  /**
166
   * Reads the next symbol from input.  The symbol is encoded using the
167
   * huffman tree.
168
   * @param input the input source.
169
   * @return the next symbol, or -1 if not enough input is available.
170
   */
171
  int getSymbol(StreamManipulator input) throws DataFormatException
172
  {
173
    int lookahead, symbol;
174
    if ((lookahead = input.peekBits(9)) >= 0)
175
      {
176
        if ((symbol = tree[lookahead]) >= 0)
177
          {
178
            input.dropBits(symbol & 15);
179
            return symbol >> 4;
180
          }
181
        int subtree = -(symbol >> 4);
182
        int bitlen = symbol & 15;
183
        if ((lookahead = input.peekBits(bitlen)) >= 0)
184
          {
185
            symbol = tree[subtree | (lookahead >> 9)];
186
            input.dropBits(symbol & 15);
187
            return symbol >> 4;
188
          }
189
        else
190
          {
191
            int bits = input.getAvailableBits();
192
            lookahead = input.peekBits(bits);
193
            symbol = tree[subtree | (lookahead >> 9)];
194
            if ((symbol & 15) <= bits)
195
              {
196
                input.dropBits(symbol & 15);
197
                return symbol >> 4;
198
              }
199
            else
200
              return -1;
201
          }
202
      }
203
    else
204
      {
205
        int bits = input.getAvailableBits();
206
        lookahead = input.peekBits(bits);
207
        symbol = tree[lookahead];
208
        if (symbol >= 0 && (symbol & 15) <= bits)
209
          {
210
            input.dropBits(symbol & 15);
211
            return symbol >> 4;
212
          }
213
        else
214
          return -1;
215
      }
216
  }
217
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.