InflaterHuffmanTree.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. // InflaterHuffmanTree.cs
  2. // Copyright (C) 2001 Mike Krueger
  3. //
  4. // This file was translated from java, it was part of the GNU Classpath
  5. // Copyright (C) 2001 Free Software Foundation, Inc.
  6. //
  7. // This program is free software; you can redistribute it and/or
  8. // modify it under the terms of the GNU General Public License
  9. // as published by the Free Software Foundation; either version 2
  10. // of the License, or (at your option) any later version.
  11. //
  12. // This program is distributed in the hope that it will be useful,
  13. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. // GNU General Public License for more details.
  16. //
  17. // You should have received a copy of the GNU General Public License
  18. // along with this program; if not, write to the Free Software
  19. // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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. using System;
  38. using ICSharpCode.SharpZipLib.Zip.Compression.Streams;
  39. namespace ICSharpCode.SharpZipLib.Zip.Compression
  40. {
  41. public class InflaterHuffmanTree
  42. {
  43. private static int MAX_BITLEN = 15;
  44. private short[] tree;
  45. public static InflaterHuffmanTree defLitLenTree, defDistTree;
  46. static InflaterHuffmanTree()
  47. {
  48. try {
  49. byte[] codeLengths = new byte[288];
  50. int i = 0;
  51. while (i < 144) {
  52. codeLengths[i++] = 8;
  53. }
  54. while (i < 256) {
  55. codeLengths[i++] = 9;
  56. }
  57. while (i < 280) {
  58. codeLengths[i++] = 7;
  59. }
  60. while (i < 288) {
  61. codeLengths[i++] = 8;
  62. }
  63. defLitLenTree = new InflaterHuffmanTree(codeLengths);
  64. codeLengths = new byte[32];
  65. i = 0;
  66. while (i < 32) {
  67. codeLengths[i++] = 5;
  68. }
  69. defDistTree = new InflaterHuffmanTree(codeLengths);
  70. } catch (Exception) {
  71. throw new ApplicationException("InflaterHuffmanTree: static tree length illegal");
  72. }
  73. }
  74. /// <summary>
  75. /// Constructs a Huffman tree from the array of code lengths.
  76. /// </summary>
  77. /// <param name = "codeLengths">
  78. /// the array of code lengths
  79. /// </param>
  80. public InflaterHuffmanTree(byte[] codeLengths)
  81. {
  82. BuildTree(codeLengths);
  83. }
  84. private void BuildTree(byte[] codeLengths)
  85. {
  86. int[] blCount = new int[MAX_BITLEN + 1];
  87. int[] nextCode = new int[MAX_BITLEN + 1];
  88. for (int i = 0; i < codeLengths.Length; i++) {
  89. int bits = codeLengths[i];
  90. if (bits > 0) {
  91. blCount[bits]++;
  92. }
  93. }
  94. int code = 0;
  95. int treeSize = 512;
  96. for (int bits = 1; bits <= MAX_BITLEN; bits++) {
  97. nextCode[bits] = code;
  98. code += blCount[bits] << (16 - bits);
  99. if (bits >= 10) {
  100. /* We need an extra table for bit lengths >= 10. */
  101. int start = nextCode[bits] & 0x1ff80;
  102. int end = code & 0x1ff80;
  103. treeSize += (end - start) >> (16 - bits);
  104. }
  105. }
  106. /* -jr comment this out! doesnt work for dynamic trees and pkzip 2.04g
  107. if (code != 65536)
  108. {
  109. throw new Exception("Code lengths don't add up properly.");
  110. }
  111. */
  112. /* Now create and fill the extra tables from longest to shortest
  113. * bit len. This way the sub trees will be aligned.
  114. */
  115. tree = new short[treeSize];
  116. int treePtr = 512;
  117. for (int bits = MAX_BITLEN; bits >= 10; bits--) {
  118. int end = code & 0x1ff80;
  119. code -= blCount[bits] << (16 - bits);
  120. int start = code & 0x1ff80;
  121. for (int i = start; i < end; i += 1 << 7) {
  122. tree[DeflaterHuffman.BitReverse(i)] = (short) ((-treePtr << 4) | bits);
  123. treePtr += 1 << (bits-9);
  124. }
  125. }
  126. for (int i = 0; i < codeLengths.Length; i++) {
  127. int bits = codeLengths[i];
  128. if (bits == 0) {
  129. continue;
  130. }
  131. code = nextCode[bits];
  132. int revcode = DeflaterHuffman.BitReverse(code);
  133. if (bits <= 9) {
  134. do {
  135. tree[revcode] = (short) ((i << 4) | bits);
  136. revcode += 1 << bits;
  137. } while (revcode < 512);
  138. } else {
  139. int subTree = tree[revcode & 511];
  140. int treeLen = 1 << (subTree & 15);
  141. subTree = -(subTree >> 4);
  142. do {
  143. tree[subTree | (revcode >> 9)] = (short) ((i << 4) | bits);
  144. revcode += 1 << bits;
  145. } while (revcode < treeLen);
  146. }
  147. nextCode[bits] = code + (1 << (16 - bits));
  148. }
  149. }
  150. /// <summary>
  151. /// Reads the next symbol from input. The symbol is encoded using the
  152. /// huffman tree.
  153. /// </summary>
  154. /// <param name="input">
  155. /// input the input source.
  156. /// </param>
  157. /// <returns>
  158. /// the next symbol, or -1 if not enough input is available.
  159. /// </returns>
  160. public int GetSymbol(StreamManipulator input)
  161. {
  162. int lookahead, symbol;
  163. if ((lookahead = input.PeekBits(9)) >= 0) {
  164. if ((symbol = tree[lookahead]) >= 0) {
  165. input.DropBits(symbol & 15);
  166. return symbol >> 4;
  167. }
  168. int subtree = -(symbol >> 4);
  169. int bitlen = symbol & 15;
  170. if ((lookahead = input.PeekBits(bitlen)) >= 0) {
  171. symbol = tree[subtree | (lookahead >> 9)];
  172. input.DropBits(symbol & 15);
  173. return symbol >> 4;
  174. } else {
  175. int bits = input.AvailableBits;
  176. lookahead = input.PeekBits(bits);
  177. symbol = tree[subtree | (lookahead >> 9)];
  178. if ((symbol & 15) <= bits) {
  179. input.DropBits(symbol & 15);
  180. return symbol >> 4;
  181. } else {
  182. return -1;
  183. }
  184. }
  185. } else {
  186. int bits = input.AvailableBits;
  187. lookahead = input.PeekBits(bits);
  188. symbol = tree[lookahead];
  189. if (symbol >= 0 && (symbol & 15) <= bits) {
  190. input.DropBits(symbol & 15);
  191. return symbol >> 4;
  192. } else {
  193. return -1;
  194. }
  195. }
  196. }
  197. }
  198. }