ZTree.cs 19 KB

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