InflaterHuffmanTree.java 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /* InflaterHuffmanTree.java --
  2. Copyright (C) 2001, 2004 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package java.util.zip;
  32. class InflaterHuffmanTree
  33. {
  34. private static final int MAX_BITLEN = 15;
  35. private short[] tree;
  36. static InflaterHuffmanTree defLitLenTree, defDistTree;
  37. static
  38. {
  39. try
  40. {
  41. byte[] codeLengths = new byte[288];
  42. int i = 0;
  43. while (i < 144)
  44. codeLengths[i++] = 8;
  45. while (i < 256)
  46. codeLengths[i++] = 9;
  47. while (i < 280)
  48. codeLengths[i++] = 7;
  49. while (i < 288)
  50. codeLengths[i++] = 8;
  51. defLitLenTree = new InflaterHuffmanTree(codeLengths);
  52. codeLengths = new byte[32];
  53. i = 0;
  54. while (i < 32)
  55. codeLengths[i++] = 5;
  56. defDistTree = new InflaterHuffmanTree(codeLengths);
  57. }
  58. catch (DataFormatException ex)
  59. {
  60. throw new InternalError
  61. ("InflaterHuffmanTree: static tree length illegal");
  62. }
  63. }
  64. /**
  65. * Constructs a Huffman tree from the array of code lengths.
  66. *
  67. * @param codeLengths the array of code lengths
  68. */
  69. InflaterHuffmanTree(byte[] codeLengths) throws DataFormatException
  70. {
  71. buildTree(codeLengths);
  72. }
  73. private void buildTree(byte[] codeLengths) throws DataFormatException
  74. {
  75. int[] blCount = new int[MAX_BITLEN+1];
  76. int[] nextCode = new int[MAX_BITLEN+1];
  77. for (int i = 0; i < codeLengths.length; i++)
  78. {
  79. int bits = codeLengths[i];
  80. if (bits > 0)
  81. blCount[bits]++;
  82. }
  83. int max = 0;
  84. int code = 0;
  85. int treeSize = 512;
  86. for (int bits = 1; bits <= MAX_BITLEN; bits++)
  87. {
  88. nextCode[bits] = code;
  89. if (blCount[bits] > 0)
  90. max = bits;
  91. code += blCount[bits] << (16 - bits);
  92. if (bits >= 10)
  93. {
  94. /* We need an extra table for bit lengths >= 10. */
  95. int start = nextCode[bits] & 0x1ff80;
  96. int end = code & 0x1ff80;
  97. treeSize += (end - start) >> (16 - bits);
  98. }
  99. }
  100. if (code != 65536 && max > 1)
  101. throw new DataFormatException("incomplete dynamic bit lengths tree");
  102. /* Now create and fill the extra tables from longest to shortest
  103. * bit len. This way the sub trees will be aligned.
  104. */
  105. tree = new short[treeSize];
  106. int treePtr = 512;
  107. for (int bits = MAX_BITLEN; bits >= 10; bits--)
  108. {
  109. int end = code & 0x1ff80;
  110. code -= blCount[bits] << (16 - bits);
  111. int start = code & 0x1ff80;
  112. for (int i = start; i < end; i += 1 << 7)
  113. {
  114. tree[DeflaterHuffman.bitReverse(i)]
  115. = (short) ((-treePtr << 4) | bits);
  116. treePtr += 1 << (bits-9);
  117. }
  118. }
  119. for (int i = 0; i < codeLengths.length; i++)
  120. {
  121. int bits = codeLengths[i];
  122. if (bits == 0)
  123. continue;
  124. code = nextCode[bits];
  125. int revcode = DeflaterHuffman.bitReverse(code);
  126. if (bits <= 9)
  127. {
  128. do
  129. {
  130. tree[revcode] = (short) ((i << 4) | bits);
  131. revcode += 1 << bits;
  132. }
  133. while (revcode < 512);
  134. }
  135. else
  136. {
  137. int subTree = tree[revcode & 511];
  138. int treeLen = 1 << (subTree & 15);
  139. subTree = -(subTree >> 4);
  140. do
  141. {
  142. tree[subTree | (revcode >> 9)] = (short) ((i << 4) | bits);
  143. revcode += 1 << bits;
  144. }
  145. while (revcode < treeLen);
  146. }
  147. nextCode[bits] = code + (1 << (16 - bits));
  148. }
  149. }
  150. /**
  151. * Reads the next symbol from input. The symbol is encoded using the
  152. * huffman tree.
  153. * @param input the input source.
  154. * @return the next symbol, or -1 if not enough input is available.
  155. */
  156. int getSymbol(StreamManipulator input) throws DataFormatException
  157. {
  158. int lookahead, symbol;
  159. if ((lookahead = input.peekBits(9)) >= 0)
  160. {
  161. if ((symbol = tree[lookahead]) >= 0)
  162. {
  163. input.dropBits(symbol & 15);
  164. return symbol >> 4;
  165. }
  166. int subtree = -(symbol >> 4);
  167. int bitlen = symbol & 15;
  168. if ((lookahead = input.peekBits(bitlen)) >= 0)
  169. {
  170. symbol = tree[subtree | (lookahead >> 9)];
  171. input.dropBits(symbol & 15);
  172. return symbol >> 4;
  173. }
  174. else
  175. {
  176. int bits = input.getAvailableBits();
  177. lookahead = input.peekBits(bits);
  178. symbol = tree[subtree | (lookahead >> 9)];
  179. if ((symbol & 15) <= bits)
  180. {
  181. input.dropBits(symbol & 15);
  182. return symbol >> 4;
  183. }
  184. else
  185. return -1;
  186. }
  187. }
  188. else
  189. {
  190. int bits = input.getAvailableBits();
  191. lookahead = input.peekBits(bits);
  192. symbol = tree[lookahead];
  193. if (symbol >= 0 && (symbol & 15) <= bits)
  194. {
  195. input.dropBits(symbol & 15);
  196. return symbol >> 4;
  197. }
  198. else
  199. return -1;
  200. }
  201. }
  202. }