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.

ZTree.cs 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. // Tree.cs
  2. // ------------------------------------------------------------------
  3. //
  4. // Copyright (c) 2009 Dino Chiesa and Microsoft Corporation.
  5. // All rights reserved.
  6. //
  7. // This code module is part of DotNetZip, a zipfile class library.
  8. //
  9. // ------------------------------------------------------------------
  10. //
  11. // This code is licensed under the Microsoft Public License.
  12. // See the file License.txt for the license details.
  13. // More info on: http://dotnetzip.codeplex.com
  14. //
  15. // ------------------------------------------------------------------
  16. //
  17. // last saved (in emacs):
  18. // Time-stamp: <2009-October-28 13:29:50>
  19. //
  20. // ------------------------------------------------------------------
  21. //
  22. // This module defines classes for zlib compression and
  23. // decompression. This code is derived from the jzlib implementation of
  24. // zlib. In keeping with the license for jzlib, the copyright to that
  25. // code is below.
  26. //
  27. // ------------------------------------------------------------------
  28. //
  29. // Copyright (c) 2000,2001,2002,2003 ymnk, JCraft,Inc. All rights reserved.
  30. //
  31. // Redistribution and use in source and binary forms, with or without
  32. // modification, are permitted provided that the following conditions are met:
  33. //
  34. // 1. Redistributions of source code must retain the above copyright notice,
  35. // this list of conditions and the following disclaimer.
  36. //
  37. // 2. Redistributions in binary form must reproduce the above copyright
  38. // notice, this list of conditions and the following disclaimer in
  39. // the documentation and/or other materials provided with the distribution.
  40. //
  41. // 3. The names of the authors may not be used to endorse or promote products
  42. // derived from this software without specific prior written permission.
  43. //
  44. // THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
  45. // INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  46. // FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
  47. // INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
  48. // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  49. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  50. // OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  51. // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  52. // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  53. // EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  54. //
  55. // -----------------------------------------------------------------------
  56. //
  57. // This program is based on zlib-1.1.3; credit to authors
  58. // Jean-loup Gailly(jloup@gzip.org) and Mark Adler(madler@alumni.caltech.edu)
  59. // and contributors of zlib.
  60. //
  61. // -----------------------------------------------------------------------
  62. using System;
  63. namespace BestHTTP.Decompression.Zlib
  64. {
  65. sealed class ZTree
  66. {
  67. private static readonly int HEAP_SIZE = (2 * InternalConstants.L_CODES + 1);
  68. // extra bits for each length code
  69. internal static readonly int[] ExtraLengthBits = new int[]
  70. {
  71. 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2,
  72. 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0
  73. };
  74. // extra bits for each distance code
  75. internal static readonly int[] ExtraDistanceBits = new int[]
  76. {
  77. 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6,
  78. 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13
  79. };
  80. // extra bits for each bit length code
  81. 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};
  82. internal static readonly sbyte[] bl_order = new sbyte[]{16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
  83. // The lengths of the bit length codes are sent in order of decreasing
  84. // probability, to avoid transmitting the lengths for unused bit
  85. // length codes.
  86. internal const int Buf_size = 8 * 2;
  87. // see definition of array dist_code below
  88. //internal const int DIST_CODE_LEN = 512;
  89. private static readonly sbyte[] _dist_code = new sbyte[]
  90. {
  91. 0, 1, 2, 3, 4, 4, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7,
  92. 8, 8, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9,
  93. 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
  94. 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11,
  95. 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,
  96. 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,
  97. 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13,
  98. 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13,
  99. 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
  100. 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
  101. 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
  102. 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
  103. 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
  104. 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
  105. 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
  106. 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
  107. 0, 0, 16, 17, 18, 18, 19, 19, 20, 20, 20, 20, 21, 21, 21, 21,
  108. 22, 22, 22, 22, 22, 22, 22, 22, 23, 23, 23, 23, 23, 23, 23, 23,
  109. 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
  110. 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25,
  111. 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26,
  112. 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26,
  113. 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
  114. 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
  115. 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28,
  116. 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28,
  117. 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28,
  118. 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28,
  119. 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29,
  120. 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29,
  121. 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29,
  122. 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29
  123. };
  124. internal static readonly sbyte[] LengthCode = new sbyte[]
  125. {
  126. 0, 1, 2, 3, 4, 5, 6, 7, 8, 8, 9, 9, 10, 10, 11, 11,
  127. 12, 12, 12, 12, 13, 13, 13, 13, 14, 14, 14, 14, 15, 15, 15, 15,
  128. 16, 16, 16, 16, 16, 16, 16, 16, 17, 17, 17, 17, 17, 17, 17, 17,
  129. 18, 18, 18, 18, 18, 18, 18, 18, 19, 19, 19, 19, 19, 19, 19, 19,
  130. 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
  131. 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21,
  132. 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22,
  133. 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23,
  134. 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
  135. 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
  136. 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25,
  137. 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25,
  138. 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26,
  139. 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26,
  140. 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
  141. 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 28
  142. };
  143. internal static readonly int[] LengthBase = new int[]
  144. {
  145. 0, 1, 2, 3, 4, 5, 6, 7, 8, 10, 12, 14, 16, 20, 24, 28,
  146. 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 0
  147. };
  148. internal static readonly int[] DistanceBase = new int[]
  149. {
  150. 0, 1, 2, 3, 4, 6, 8, 12, 16, 24, 32, 48, 64, 96, 128, 192,
  151. 256, 384, 512, 768, 1024, 1536, 2048, 3072, 4096, 6144, 8192, 12288, 16384, 24576
  152. };
  153. /// <summary>
  154. /// Map from a distance to a distance code.
  155. /// </summary>
  156. /// <remarks>
  157. /// No side effects. _dist_code[256] and _dist_code[257] are never used.
  158. /// </remarks>
  159. internal static int DistanceCode(int dist)
  160. {
  161. return (dist < 256)
  162. ? _dist_code[dist]
  163. : _dist_code[256 + SharedUtils.URShift(dist, 7)];
  164. }
  165. internal short[] dyn_tree; // the dynamic tree
  166. internal int max_code; // largest code with non zero frequency
  167. internal StaticTree staticTree; // the corresponding static tree
  168. // Compute the optimal bit lengths for a tree and update the total bit length
  169. // for the current block.
  170. // IN assertion: the fields freq and dad are set, heap[heap_max] and
  171. // above are the tree nodes sorted by increasing frequency.
  172. // OUT assertions: the field len is set to the optimal bit length, the
  173. // array bl_count contains the frequencies for each bit length.
  174. // The length opt_len is updated; static_len is also updated if stree is
  175. // not null.
  176. internal void gen_bitlen(DeflateManager s)
  177. {
  178. short[] tree = dyn_tree;
  179. short[] stree = staticTree.treeCodes;
  180. int[] extra = staticTree.extraBits;
  181. int base_Renamed = staticTree.extraBase;
  182. int max_length = staticTree.maxLength;
  183. int h; // heap index
  184. int n, m; // iterate over the tree elements
  185. int bits; // bit length
  186. int xbits; // extra bits
  187. short f; // frequency
  188. int overflow = 0; // number of elements with bit length too large
  189. for (bits = 0; bits <= InternalConstants.MAX_BITS; bits++)
  190. s.bl_count[bits] = 0;
  191. // In a first pass, compute the optimal bit lengths (which may
  192. // overflow in the case of the bit length tree).
  193. tree[s.heap[s.heap_max] * 2 + 1] = 0; // root of the heap
  194. for (h = s.heap_max + 1; h < HEAP_SIZE; h++)
  195. {
  196. n = s.heap[h];
  197. bits = tree[tree[n * 2 + 1] * 2 + 1] + 1;
  198. if (bits > max_length)
  199. {
  200. bits = max_length; overflow++;
  201. }
  202. tree[n * 2 + 1] = (short) bits;
  203. // We overwrite tree[n*2+1] which is no longer needed
  204. if (n > max_code)
  205. continue; // not a leaf node
  206. s.bl_count[bits]++;
  207. xbits = 0;
  208. if (n >= base_Renamed)
  209. xbits = extra[n - base_Renamed];
  210. f = tree[n * 2];
  211. s.opt_len += f * (bits + xbits);
  212. if (stree != null)
  213. s.static_len += f * (stree[n * 2 + 1] + xbits);
  214. }
  215. if (overflow == 0)
  216. return ;
  217. // This happens for example on obj2 and pic of the Calgary corpus
  218. // Find the first bit length which could increase:
  219. do
  220. {
  221. bits = max_length - 1;
  222. while (s.bl_count[bits] == 0)
  223. bits--;
  224. s.bl_count[bits]--; // move one leaf down the tree
  225. s.bl_count[bits + 1] = (short) (s.bl_count[bits + 1] + 2); // move one overflow item as its brother
  226. s.bl_count[max_length]--;
  227. // The brother of the overflow item also moves one step up,
  228. // but this does not affect bl_count[max_length]
  229. overflow -= 2;
  230. }
  231. while (overflow > 0);
  232. for (bits = max_length; bits != 0; bits--)
  233. {
  234. n = s.bl_count[bits];
  235. while (n != 0)
  236. {
  237. m = s.heap[--h];
  238. if (m > max_code)
  239. continue;
  240. if (tree[m * 2 + 1] != bits)
  241. {
  242. s.opt_len = (int) (s.opt_len + ((long) bits - (long) tree[m * 2 + 1]) * (long) tree[m * 2]);
  243. tree[m * 2 + 1] = (short) bits;
  244. }
  245. n--;
  246. }
  247. }
  248. }
  249. // Construct one Huffman tree and assigns the code bit strings and lengths.
  250. // Update the total bit length for the current block.
  251. // IN assertion: the field freq is set for all tree elements.
  252. // OUT assertions: the fields len and code are set to the optimal bit length
  253. // and corresponding code. The length opt_len is updated; static_len is
  254. // also updated if stree is not null. The field max_code is set.
  255. internal void build_tree(DeflateManager s)
  256. {
  257. short[] tree = dyn_tree;
  258. short[] stree = staticTree.treeCodes;
  259. int elems = staticTree.elems;
  260. int n, m; // iterate over heap elements
  261. int max_code = -1; // largest code with non zero frequency
  262. int node; // new node being created
  263. // Construct the initial heap, with least frequent element in
  264. // heap[1]. The sons of heap[n] are heap[2*n] and heap[2*n+1].
  265. // heap[0] is not used.
  266. s.heap_len = 0;
  267. s.heap_max = HEAP_SIZE;
  268. for (n = 0; n < elems; n++)
  269. {
  270. if (tree[n * 2] != 0)
  271. {
  272. s.heap[++s.heap_len] = max_code = n;
  273. s.depth[n] = 0;
  274. }
  275. else
  276. {
  277. tree[n * 2 + 1] = 0;
  278. }
  279. }
  280. // The pkzip format requires that at least one distance code exists,
  281. // and that at least one bit should be sent even if there is only one
  282. // possible code. So to avoid special checks later on we force at least
  283. // two codes of non zero frequency.
  284. while (s.heap_len < 2)
  285. {
  286. node = s.heap[++s.heap_len] = (max_code < 2?++max_code:0);
  287. tree[node * 2] = 1;
  288. s.depth[node] = 0;
  289. s.opt_len--;
  290. if (stree != null)
  291. s.static_len -= stree[node * 2 + 1];
  292. // node is 0 or 1 so it does not have extra bits
  293. }
  294. this.max_code = max_code;
  295. // The elements heap[heap_len/2+1 .. heap_len] are leaves of the tree,
  296. // establish sub-heaps of increasing lengths:
  297. for (n = s.heap_len / 2; n >= 1; n--)
  298. s.pqdownheap(tree, n);
  299. // Construct the Huffman tree by repeatedly combining the least two
  300. // frequent nodes.
  301. node = elems; // next internal node of the tree
  302. do
  303. {
  304. // n = node of least frequency
  305. n = s.heap[1];
  306. s.heap[1] = s.heap[s.heap_len--];
  307. s.pqdownheap(tree, 1);
  308. m = s.heap[1]; // m = node of next least frequency
  309. s.heap[--s.heap_max] = n; // keep the nodes sorted by frequency
  310. s.heap[--s.heap_max] = m;
  311. // Create a new node father of n and m
  312. tree[node * 2] = unchecked((short) (tree[n * 2] + tree[m * 2]));
  313. s.depth[node] = (sbyte) (System.Math.Max((byte) s.depth[n], (byte) s.depth[m]) + 1);
  314. tree[n * 2 + 1] = tree[m * 2 + 1] = (short) node;
  315. // and insert the new node in the heap
  316. s.heap[1] = node++;
  317. s.pqdownheap(tree, 1);
  318. }
  319. while (s.heap_len >= 2);
  320. s.heap[--s.heap_max] = s.heap[1];
  321. // At this point, the fields freq and dad are set. We can now
  322. // generate the bit lengths.
  323. gen_bitlen(s);
  324. // The field len is now set, we can generate the bit codes
  325. gen_codes(tree, max_code, s.bl_count);
  326. }
  327. // Generate the codes for a given tree and bit counts (which need not be
  328. // optimal).
  329. // IN assertion: the array bl_count contains the bit length statistics for
  330. // the given tree and the field len is set for all tree elements.
  331. // OUT assertion: the field code is set for all tree elements of non
  332. // zero code length.
  333. internal static void gen_codes(short[] tree, int max_code, short[] bl_count)
  334. {
  335. short[] next_code = new short[InternalConstants.MAX_BITS + 1]; // next code value for each bit length
  336. short code = 0; // running code value
  337. int bits; // bit index
  338. int n; // code index
  339. // The distribution counts are first used to generate the code values
  340. // without bit reversal.
  341. for (bits = 1; bits <= InternalConstants.MAX_BITS; bits++)
  342. unchecked {
  343. next_code[bits] = code = (short) ((code + bl_count[bits - 1]) << 1);
  344. }
  345. // Check that the bit counts in bl_count are consistent. The last code
  346. // must be all ones.
  347. //Assert (code + bl_count[MAX_BITS]-1 == (1<<MAX_BITS)-1,
  348. // "inconsistent bit counts");
  349. //Tracev((stderr,"\ngen_codes: max_code %d ", max_code));
  350. for (n = 0; n <= max_code; n++)
  351. {
  352. int len = tree[n * 2 + 1];
  353. if (len == 0)
  354. continue;
  355. // Now reverse the bits
  356. tree[n * 2] = unchecked((short) (bi_reverse(next_code[len]++, len)));
  357. }
  358. }
  359. // Reverse the first len bits of a code, using straightforward code (a faster
  360. // method would use a table)
  361. // IN assertion: 1 <= len <= 15
  362. internal static int bi_reverse(int code, int len)
  363. {
  364. int res = 0;
  365. do
  366. {
  367. res |= code & 1;
  368. code >>= 1; //SharedUtils.URShift(code, 1);
  369. res <<= 1;
  370. }
  371. while (--len > 0);
  372. return res >> 1;
  373. }
  374. }
  375. }