ZTree.cs 19 KB

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