InflaterDynHeader.java 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /* java.util.zip.InflaterDynHeader
  2. Copyright (C) 2001 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 InflaterDynHeader
  33. {
  34. private static final int LNUM = 0;
  35. private static final int DNUM = 1;
  36. private static final int BLNUM = 2;
  37. private static final int BLLENS = 3;
  38. private static final int LENS = 4;
  39. private static final int REPS = 5;
  40. private static final int repMin[] = { 3, 3, 11 };
  41. private static final int repBits[] = { 2, 3, 7 };
  42. private byte[] blLens;
  43. private byte[] litdistLens;
  44. private InflaterHuffmanTree blTree;
  45. private int mode;
  46. private int lnum, dnum, blnum, num;
  47. private int repSymbol;
  48. private byte lastLen;
  49. private int ptr;
  50. private static final int[] BL_ORDER =
  51. { 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15 };
  52. public InflaterDynHeader()
  53. {
  54. }
  55. public boolean decode(StreamManipulator input) throws DataFormatException
  56. {
  57. decode_loop:
  58. for (;;)
  59. {
  60. switch (mode)
  61. {
  62. case LNUM:
  63. lnum = input.peekBits(5);
  64. if (lnum < 0)
  65. return false;
  66. lnum += 257;
  67. input.dropBits(5);
  68. // System.err.println("LNUM: "+lnum);
  69. mode = DNUM;
  70. /* fall through */
  71. case DNUM:
  72. dnum = input.peekBits(5);
  73. if (dnum < 0)
  74. return false;
  75. dnum++;
  76. input.dropBits(5);
  77. // System.err.println("DNUM: "+dnum);
  78. num = lnum+dnum;
  79. litdistLens = new byte[num];
  80. mode = BLNUM;
  81. /* fall through */
  82. case BLNUM:
  83. blnum = input.peekBits(4);
  84. if (blnum < 0)
  85. return false;
  86. blnum += 4;
  87. input.dropBits(4);
  88. blLens = new byte[19];
  89. ptr = 0;
  90. // System.err.println("BLNUM: "+blnum);
  91. mode = BLLENS;
  92. /* fall through */
  93. case BLLENS:
  94. while (ptr < blnum)
  95. {
  96. int len = input.peekBits(3);
  97. if (len < 0)
  98. return false;
  99. input.dropBits(3);
  100. // System.err.println("blLens["+BL_ORDER[ptr]+"]: "+len);
  101. blLens[BL_ORDER[ptr]] = (byte) len;
  102. ptr++;
  103. }
  104. blTree = new InflaterHuffmanTree(blLens);
  105. blLens = null;
  106. ptr = 0;
  107. mode = LENS;
  108. /* fall through */
  109. case LENS:
  110. {
  111. int symbol;
  112. while (((symbol = blTree.getSymbol(input)) & ~15) == 0)
  113. {
  114. /* Normal case: symbol in [0..15] */
  115. // System.err.println("litdistLens["+ptr+"]: "+symbol);
  116. litdistLens[ptr++] = lastLen = (byte) symbol;
  117. if (ptr == num)
  118. {
  119. /* Finished */
  120. return true;
  121. }
  122. }
  123. /* need more input ? */
  124. if (symbol < 0)
  125. return false;
  126. /* otherwise repeat code */
  127. if (symbol >= 17)
  128. {
  129. /* repeat zero */
  130. // System.err.println("repeating zero");
  131. lastLen = 0;
  132. }
  133. else
  134. {
  135. if (ptr == 0)
  136. throw new DataFormatException();
  137. }
  138. repSymbol = symbol-16;
  139. mode = REPS;
  140. }
  141. /* fall through */
  142. case REPS:
  143. {
  144. int bits = repBits[repSymbol];
  145. int count = input.peekBits(bits);
  146. if (count < 0)
  147. return false;
  148. input.dropBits(bits);
  149. count += repMin[repSymbol];
  150. // System.err.println("litdistLens repeated: "+count);
  151. if (ptr + count > num)
  152. throw new DataFormatException();
  153. while (count-- > 0)
  154. litdistLens[ptr++] = lastLen;
  155. if (ptr == num)
  156. {
  157. /* Finished */
  158. return true;
  159. }
  160. }
  161. mode = LENS;
  162. continue decode_loop;
  163. }
  164. }
  165. }
  166. public InflaterHuffmanTree buildLitLenTree() throws DataFormatException
  167. {
  168. byte[] litlenLens = new byte[lnum];
  169. System.arraycopy(litdistLens, 0, litlenLens, 0, lnum);
  170. return new InflaterHuffmanTree(litlenLens);
  171. }
  172. public InflaterHuffmanTree buildDistTree() throws DataFormatException
  173. {
  174. byte[] distLens = new byte[dnum];
  175. System.arraycopy(litdistLens, lnum, distLens, 0, dnum);
  176. return new InflaterHuffmanTree(distLens);
  177. }
  178. }