You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

348 lines
16 KiB

  1. // Copyright (c) 2006, ComponentAce
  2. // http://www.componentace.com
  3. // All rights reserved.
  4. // Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
  5. // Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  6. // Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
  7. // Neither the name of ComponentAce nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
  8. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  9. /*
  10. Copyright (c) 2000,2001,2002,2003 ymnk, JCraft,Inc. All rights reserved.
  11. Redistribution and use in source and binary forms, with or without
  12. modification, are permitted provided that the following conditions are met:
  13. 1. Redistributions of source code must retain the above copyright notice,
  14. this list of conditions and the following disclaimer.
  15. 2. Redistributions in binary form must reproduce the above copyright
  16. notice, this list of conditions and the following disclaimer in
  17. the documentation and/or other materials provided with the distribution.
  18. 3. The names of the authors may not be used to endorse or promote products
  19. derived from this software without specific prior written permission.
  20. THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
  21. INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  22. FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
  23. INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
  24. INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  26. OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  27. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  28. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  29. EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. */
  31. /*
  32. * This program is based on zlib-1.1.3, so all credit should go authors
  33. * Jean-loup Gailly(jloup@gzip.org) and Mark Adler(madler@alumni.caltech.edu)
  34. * and contributors of zlib.
  35. */
  36. using System;
  37. namespace zlib
  38. {
  39. sealed class Tree
  40. {
  41. private const int MAX_BITS = 15;
  42. private const int BL_CODES = 19;
  43. private const int D_CODES = 30;
  44. private const int LITERALS = 256;
  45. private const int LENGTH_CODES = 29;
  46. private static readonly int L_CODES = (LITERALS + 1 + LENGTH_CODES);
  47. private static readonly int HEAP_SIZE = (2 * L_CODES + 1);
  48. // Bit length codes must not exceed MAX_BL_BITS bits
  49. internal const int MAX_BL_BITS = 7;
  50. // end of block literal code
  51. internal const int END_BLOCK = 256;
  52. // repeat previous bit length 3-6 times (2 bits of repeat count)
  53. internal const int REP_3_6 = 16;
  54. // repeat a zero length 3-10 times (3 bits of repeat count)
  55. internal const int REPZ_3_10 = 17;
  56. // repeat a zero length 11-138 times (7 bits of repeat count)
  57. internal const int REPZ_11_138 = 18;
  58. // extra bits for each length code
  59. internal static readonly int[] extra_lbits = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0};
  60. // extra bits for each distance code
  61. internal static readonly int[] extra_dbits = new int[]{0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13};
  62. // extra bits for each bit length code
  63. internal static readonly int[] extra_blbits = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 3, 7};
  64. internal static readonly byte[] bl_order = new byte[]{16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
  65. // The lengths of the bit length codes are sent in order of decreasing
  66. // probability, to avoid transmitting the lengths for unused bit
  67. // length codes.
  68. internal const int Buf_size = 8 * 2;
  69. // see definition of array dist_code below
  70. internal const int DIST_CODE_LEN = 512;
  71. internal static readonly byte[] _dist_code = new byte[]{0, 1, 2, 3, 4, 4, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 0, 0, 16, 17, 18, 18, 19, 19, 20, 20, 20, 20, 21, 21, 21, 21, 22, 22, 22, 22, 22, 22, 22, 22, 23, 23, 23, 23, 23, 23, 23, 23, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29,
  72. 29, 29, 29, 29, 29, 29, 29, 29, 29};
  73. internal static readonly byte[] _length_code = new byte[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 12, 12, 13, 13, 13, 13, 14, 14, 14, 14, 15, 15, 15, 15, 16, 16, 16, 16, 16, 16, 16, 16, 17, 17, 17, 17, 17, 17, 17, 17, 18, 18, 18, 18, 18, 18, 18, 18, 19, 19, 19, 19, 19, 19, 19, 19, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 28};
  74. internal static readonly int[] base_length = new int[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 10, 12, 14, 16, 20, 24, 28, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 0};
  75. internal static readonly int[] base_dist = new int[]{0, 1, 2, 3, 4, 6, 8, 12, 16, 24, 32, 48, 64, 96, 128, 192, 256, 384, 512, 768, 1024, 1536, 2048, 3072, 4096, 6144, 8192, 12288, 16384, 24576};
  76. // Mapping from a distance to a distance code. dist is the distance - 1 and
  77. // must not have side effects. _dist_code[256] and _dist_code[257] are never
  78. // used.
  79. internal static int d_code(int dist)
  80. {
  81. return ((dist) < 256?_dist_code[dist]:_dist_code[256 + (SupportClass.URShift((dist), 7))]);
  82. }
  83. internal short[] dyn_tree; // the dynamic tree
  84. internal int max_code; // largest code with non zero frequency
  85. internal StaticTree stat_desc; // the corresponding static tree
  86. // Compute the optimal bit lengths for a tree and update the total bit length
  87. // for the current block.
  88. // IN assertion: the fields freq and dad are set, heap[heap_max] and
  89. // above are the tree nodes sorted by increasing frequency.
  90. // OUT assertions: the field len is set to the optimal bit length, the
  91. // array bl_count contains the frequencies for each bit length.
  92. // The length opt_len is updated; static_len is also updated if stree is
  93. // not null.
  94. internal void gen_bitlen(Deflate s)
  95. {
  96. short[] tree = dyn_tree;
  97. short[] stree = stat_desc.static_tree;
  98. int[] extra = stat_desc.extra_bits;
  99. int base_Renamed = stat_desc.extra_base;
  100. int max_length = stat_desc.max_length;
  101. int h; // heap index
  102. int n, m; // iterate over the tree elements
  103. int bits; // bit length
  104. int xbits; // extra bits
  105. short f; // frequency
  106. int overflow = 0; // number of elements with bit length too large
  107. for (bits = 0; bits <= MAX_BITS; bits++)
  108. s.bl_count[bits] = 0;
  109. // In a first pass, compute the optimal bit lengths (which may
  110. // overflow in the case of the bit length tree).
  111. tree[s.heap[s.heap_max] * 2 + 1] = 0; // root of the heap
  112. for (h = s.heap_max + 1; h < HEAP_SIZE; h++)
  113. {
  114. n = s.heap[h];
  115. bits = tree[tree[n * 2 + 1] * 2 + 1] + 1;
  116. if (bits > max_length)
  117. {
  118. bits = max_length; overflow++;
  119. }
  120. tree[n * 2 + 1] = (short) bits;
  121. // We overwrite tree[n*2+1] which is no longer needed
  122. if (n > max_code)
  123. continue; // not a leaf node
  124. s.bl_count[bits]++;
  125. xbits = 0;
  126. if (n >= base_Renamed)
  127. xbits = extra[n - base_Renamed];
  128. f = tree[n * 2];
  129. s.opt_len += f * (bits + xbits);
  130. if (stree != null)
  131. s.static_len += f * (stree[n * 2 + 1] + xbits);
  132. }
  133. if (overflow == 0)
  134. return ;
  135. // This happens for example on obj2 and pic of the Calgary corpus
  136. // Find the first bit length which could increase:
  137. do
  138. {
  139. bits = max_length - 1;
  140. while (s.bl_count[bits] == 0)
  141. bits--;
  142. s.bl_count[bits]--; // move one leaf down the tree
  143. s.bl_count[bits + 1] = (short) (s.bl_count[bits + 1] + 2); // move one overflow item as its brother
  144. s.bl_count[max_length]--;
  145. // The brother of the overflow item also moves one step up,
  146. // but this does not affect bl_count[max_length]
  147. overflow -= 2;
  148. }
  149. while (overflow > 0);
  150. for (bits = max_length; bits != 0; bits--)
  151. {
  152. n = s.bl_count[bits];
  153. while (n != 0)
  154. {
  155. m = s.heap[--h];
  156. if (m > max_code)
  157. continue;
  158. if (tree[m * 2 + 1] != bits)
  159. {
  160. s.opt_len = (int) (s.opt_len + ((long) bits - (long) tree[m * 2 + 1]) * (long) tree[m * 2]);
  161. tree[m * 2 + 1] = (short) bits;
  162. }
  163. n--;
  164. }
  165. }
  166. }
  167. // Construct one Huffman tree and assigns the code bit strings and lengths.
  168. // Update the total bit length for the current block.
  169. // IN assertion: the field freq is set for all tree elements.
  170. // OUT assertions: the fields len and code are set to the optimal bit length
  171. // and corresponding code. The length opt_len is updated; static_len is
  172. // also updated if stree is not null. The field max_code is set.
  173. internal void build_tree(Deflate s)
  174. {
  175. short[] tree = dyn_tree;
  176. short[] stree = stat_desc.static_tree;
  177. int elems = stat_desc.elems;
  178. int n, m; // iterate over heap elements
  179. int max_code = - 1; // largest code with non zero frequency
  180. int node; // new node being created
  181. // Construct the initial heap, with least frequent element in
  182. // heap[1]. The sons of heap[n] are heap[2*n] and heap[2*n+1].
  183. // heap[0] is not used.
  184. s.heap_len = 0;
  185. s.heap_max = HEAP_SIZE;
  186. for (n = 0; n < elems; n++)
  187. {
  188. if (tree[n * 2] != 0)
  189. {
  190. s.heap[++s.heap_len] = max_code = n;
  191. s.depth[n] = 0;
  192. }
  193. else
  194. {
  195. tree[n * 2 + 1] = 0;
  196. }
  197. }
  198. // The pkzip format requires that at least one distance code exists,
  199. // and that at least one bit should be sent even if there is only one
  200. // possible code. So to avoid special checks later on we force at least
  201. // two codes of non zero frequency.
  202. while (s.heap_len < 2)
  203. {
  204. node = s.heap[++s.heap_len] = (max_code < 2?++max_code:0);
  205. tree[node * 2] = 1;
  206. s.depth[node] = 0;
  207. s.opt_len--;
  208. if (stree != null)
  209. s.static_len -= stree[node * 2 + 1];
  210. // node is 0 or 1 so it does not have extra bits
  211. }
  212. this.max_code = max_code;
  213. // The elements heap[heap_len/2+1 .. heap_len] are leaves of the tree,
  214. // establish sub-heaps of increasing lengths:
  215. for (n = s.heap_len / 2; n >= 1; n--)
  216. s.pqdownheap(tree, n);
  217. // Construct the Huffman tree by repeatedly combining the least two
  218. // frequent nodes.
  219. node = elems; // next internal node of the tree
  220. do
  221. {
  222. // n = node of least frequency
  223. n = s.heap[1];
  224. s.heap[1] = s.heap[s.heap_len--];
  225. s.pqdownheap(tree, 1);
  226. m = s.heap[1]; // m = node of next least frequency
  227. s.heap[--s.heap_max] = n; // keep the nodes sorted by frequency
  228. s.heap[--s.heap_max] = m;
  229. // Create a new node father of n and m
  230. tree[node * 2] = (short) (tree[n * 2] + tree[m * 2]);
  231. s.depth[node] = (byte) (System.Math.Max((byte) s.depth[n], (byte) s.depth[m]) + 1);
  232. tree[n * 2 + 1] = tree[m * 2 + 1] = (short) node;
  233. // and insert the new node in the heap
  234. s.heap[1] = node++;
  235. s.pqdownheap(tree, 1);
  236. }
  237. while (s.heap_len >= 2);
  238. s.heap[--s.heap_max] = s.heap[1];
  239. // At this point, the fields freq and dad are set. We can now
  240. // generate the bit lengths.
  241. gen_bitlen(s);
  242. // The field len is now set, we can generate the bit codes
  243. gen_codes(tree, max_code, s.bl_count);
  244. }
  245. // Generate the codes for a given tree and bit counts (which need not be
  246. // optimal).
  247. // IN assertion: the array bl_count contains the bit length statistics for
  248. // the given tree and the field len is set for all tree elements.
  249. // OUT assertion: the field code is set for all tree elements of non
  250. // zero code length.
  251. internal static void gen_codes(short[] tree, int max_code, short[] bl_count)
  252. {
  253. short[] next_code = new short[MAX_BITS + 1]; // next code value for each bit length
  254. short code = 0; // running code value
  255. int bits; // bit index
  256. int n; // code index
  257. // The distribution counts are first used to generate the code values
  258. // without bit reversal.
  259. for (bits = 1; bits <= MAX_BITS; bits++)
  260. {
  261. next_code[bits] = code = (short) ((code + bl_count[bits - 1]) << 1);
  262. }
  263. // Check that the bit counts in bl_count are consistent. The last code
  264. // must be all ones.
  265. //Assert (code + bl_count[MAX_BITS]-1 == (1<<MAX_BITS)-1,
  266. // "inconsistent bit counts");
  267. //Tracev((stderr,"\ngen_codes: max_code %d ", max_code));
  268. for (n = 0; n <= max_code; n++)
  269. {
  270. int len = tree[n * 2 + 1];
  271. if (len == 0)
  272. continue;
  273. // Now reverse the bits
  274. tree[n * 2] = (short) (bi_reverse(next_code[len]++, len));
  275. }
  276. }
  277. // Reverse the first len bits of a code, using straightforward code (a faster
  278. // method would use a table)
  279. // IN assertion: 1 <= len <= 15
  280. internal static int bi_reverse(int code, int len)
  281. {
  282. int res = 0;
  283. do
  284. {
  285. res |= code & 1;
  286. code = SupportClass.URShift(code, 1);
  287. res <<= 1;
  288. }
  289. while (--len > 0);
  290. return SupportClass.URShift(res, 1);
  291. }
  292. }
  293. }