InfTree.cs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. /*
  5. * $Id: InfTree.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 InfTree{
  35. private const int MANY=1440;
  36. private const int Z_OK=0;
  37. private const int Z_STREAM_END=1;
  38. private const int Z_NEED_DICT=2;
  39. private const int Z_ERRNO=-1;
  40. private const int Z_STREAM_ERROR=-2;
  41. private const int Z_DATA_ERROR=-3;
  42. private const int Z_MEM_ERROR=-4;
  43. private const int Z_BUF_ERROR=-5;
  44. private const int Z_VERSION_ERROR=-6;
  45. private const int fixed_bl = 9;
  46. private const int fixed_bd = 5;
  47. static readonly int[] fixed_tl = {
  48. 96,7,256, 0,8,80, 0,8,16, 84,8,115,
  49. 82,7,31, 0,8,112, 0,8,48, 0,9,192,
  50. 80,7,10, 0,8,96, 0,8,32, 0,9,160,
  51. 0,8,0, 0,8,128, 0,8,64, 0,9,224,
  52. 80,7,6, 0,8,88, 0,8,24, 0,9,144,
  53. 83,7,59, 0,8,120, 0,8,56, 0,9,208,
  54. 81,7,17, 0,8,104, 0,8,40, 0,9,176,
  55. 0,8,8, 0,8,136, 0,8,72, 0,9,240,
  56. 80,7,4, 0,8,84, 0,8,20, 85,8,227,
  57. 83,7,43, 0,8,116, 0,8,52, 0,9,200,
  58. 81,7,13, 0,8,100, 0,8,36, 0,9,168,
  59. 0,8,4, 0,8,132, 0,8,68, 0,9,232,
  60. 80,7,8, 0,8,92, 0,8,28, 0,9,152,
  61. 84,7,83, 0,8,124, 0,8,60, 0,9,216,
  62. 82,7,23, 0,8,108, 0,8,44, 0,9,184,
  63. 0,8,12, 0,8,140, 0,8,76, 0,9,248,
  64. 80,7,3, 0,8,82, 0,8,18, 85,8,163,
  65. 83,7,35, 0,8,114, 0,8,50, 0,9,196,
  66. 81,7,11, 0,8,98, 0,8,34, 0,9,164,
  67. 0,8,2, 0,8,130, 0,8,66, 0,9,228,
  68. 80,7,7, 0,8,90, 0,8,26, 0,9,148,
  69. 84,7,67, 0,8,122, 0,8,58, 0,9,212,
  70. 82,7,19, 0,8,106, 0,8,42, 0,9,180,
  71. 0,8,10, 0,8,138, 0,8,74, 0,9,244,
  72. 80,7,5, 0,8,86, 0,8,22, 192,8,0,
  73. 83,7,51, 0,8,118, 0,8,54, 0,9,204,
  74. 81,7,15, 0,8,102, 0,8,38, 0,9,172,
  75. 0,8,6, 0,8,134, 0,8,70, 0,9,236,
  76. 80,7,9, 0,8,94, 0,8,30, 0,9,156,
  77. 84,7,99, 0,8,126, 0,8,62, 0,9,220,
  78. 82,7,27, 0,8,110, 0,8,46, 0,9,188,
  79. 0,8,14, 0,8,142, 0,8,78, 0,9,252,
  80. 96,7,256, 0,8,81, 0,8,17, 85,8,131,
  81. 82,7,31, 0,8,113, 0,8,49, 0,9,194,
  82. 80,7,10, 0,8,97, 0,8,33, 0,9,162,
  83. 0,8,1, 0,8,129, 0,8,65, 0,9,226,
  84. 80,7,6, 0,8,89, 0,8,25, 0,9,146,
  85. 83,7,59, 0,8,121, 0,8,57, 0,9,210,
  86. 81,7,17, 0,8,105, 0,8,41, 0,9,178,
  87. 0,8,9, 0,8,137, 0,8,73, 0,9,242,
  88. 80,7,4, 0,8,85, 0,8,21, 80,8,258,
  89. 83,7,43, 0,8,117, 0,8,53, 0,9,202,
  90. 81,7,13, 0,8,101, 0,8,37, 0,9,170,
  91. 0,8,5, 0,8,133, 0,8,69, 0,9,234,
  92. 80,7,8, 0,8,93, 0,8,29, 0,9,154,
  93. 84,7,83, 0,8,125, 0,8,61, 0,9,218,
  94. 82,7,23, 0,8,109, 0,8,45, 0,9,186,
  95. 0,8,13, 0,8,141, 0,8,77, 0,9,250,
  96. 80,7,3, 0,8,83, 0,8,19, 85,8,195,
  97. 83,7,35, 0,8,115, 0,8,51, 0,9,198,
  98. 81,7,11, 0,8,99, 0,8,35, 0,9,166,
  99. 0,8,3, 0,8,131, 0,8,67, 0,9,230,
  100. 80,7,7, 0,8,91, 0,8,27, 0,9,150,
  101. 84,7,67, 0,8,123, 0,8,59, 0,9,214,
  102. 82,7,19, 0,8,107, 0,8,43, 0,9,182,
  103. 0,8,11, 0,8,139, 0,8,75, 0,9,246,
  104. 80,7,5, 0,8,87, 0,8,23, 192,8,0,
  105. 83,7,51, 0,8,119, 0,8,55, 0,9,206,
  106. 81,7,15, 0,8,103, 0,8,39, 0,9,174,
  107. 0,8,7, 0,8,135, 0,8,71, 0,9,238,
  108. 80,7,9, 0,8,95, 0,8,31, 0,9,158,
  109. 84,7,99, 0,8,127, 0,8,63, 0,9,222,
  110. 82,7,27, 0,8,111, 0,8,47, 0,9,190,
  111. 0,8,15, 0,8,143, 0,8,79, 0,9,254,
  112. 96,7,256, 0,8,80, 0,8,16, 84,8,115,
  113. 82,7,31, 0,8,112, 0,8,48, 0,9,193,
  114. 80,7,10, 0,8,96, 0,8,32, 0,9,161,
  115. 0,8,0, 0,8,128, 0,8,64, 0,9,225,
  116. 80,7,6, 0,8,88, 0,8,24, 0,9,145,
  117. 83,7,59, 0,8,120, 0,8,56, 0,9,209,
  118. 81,7,17, 0,8,104, 0,8,40, 0,9,177,
  119. 0,8,8, 0,8,136, 0,8,72, 0,9,241,
  120. 80,7,4, 0,8,84, 0,8,20, 85,8,227,
  121. 83,7,43, 0,8,116, 0,8,52, 0,9,201,
  122. 81,7,13, 0,8,100, 0,8,36, 0,9,169,
  123. 0,8,4, 0,8,132, 0,8,68, 0,9,233,
  124. 80,7,8, 0,8,92, 0,8,28, 0,9,153,
  125. 84,7,83, 0,8,124, 0,8,60, 0,9,217,
  126. 82,7,23, 0,8,108, 0,8,44, 0,9,185,
  127. 0,8,12, 0,8,140, 0,8,76, 0,9,249,
  128. 80,7,3, 0,8,82, 0,8,18, 85,8,163,
  129. 83,7,35, 0,8,114, 0,8,50, 0,9,197,
  130. 81,7,11, 0,8,98, 0,8,34, 0,9,165,
  131. 0,8,2, 0,8,130, 0,8,66, 0,9,229,
  132. 80,7,7, 0,8,90, 0,8,26, 0,9,149,
  133. 84,7,67, 0,8,122, 0,8,58, 0,9,213,
  134. 82,7,19, 0,8,106, 0,8,42, 0,9,181,
  135. 0,8,10, 0,8,138, 0,8,74, 0,9,245,
  136. 80,7,5, 0,8,86, 0,8,22, 192,8,0,
  137. 83,7,51, 0,8,118, 0,8,54, 0,9,205,
  138. 81,7,15, 0,8,102, 0,8,38, 0,9,173,
  139. 0,8,6, 0,8,134, 0,8,70, 0,9,237,
  140. 80,7,9, 0,8,94, 0,8,30, 0,9,157,
  141. 84,7,99, 0,8,126, 0,8,62, 0,9,221,
  142. 82,7,27, 0,8,110, 0,8,46, 0,9,189,
  143. 0,8,14, 0,8,142, 0,8,78, 0,9,253,
  144. 96,7,256, 0,8,81, 0,8,17, 85,8,131,
  145. 82,7,31, 0,8,113, 0,8,49, 0,9,195,
  146. 80,7,10, 0,8,97, 0,8,33, 0,9,163,
  147. 0,8,1, 0,8,129, 0,8,65, 0,9,227,
  148. 80,7,6, 0,8,89, 0,8,25, 0,9,147,
  149. 83,7,59, 0,8,121, 0,8,57, 0,9,211,
  150. 81,7,17, 0,8,105, 0,8,41, 0,9,179,
  151. 0,8,9, 0,8,137, 0,8,73, 0,9,243,
  152. 80,7,4, 0,8,85, 0,8,21, 80,8,258,
  153. 83,7,43, 0,8,117, 0,8,53, 0,9,203,
  154. 81,7,13, 0,8,101, 0,8,37, 0,9,171,
  155. 0,8,5, 0,8,133, 0,8,69, 0,9,235,
  156. 80,7,8, 0,8,93, 0,8,29, 0,9,155,
  157. 84,7,83, 0,8,125, 0,8,61, 0,9,219,
  158. 82,7,23, 0,8,109, 0,8,45, 0,9,187,
  159. 0,8,13, 0,8,141, 0,8,77, 0,9,251,
  160. 80,7,3, 0,8,83, 0,8,19, 85,8,195,
  161. 83,7,35, 0,8,115, 0,8,51, 0,9,199,
  162. 81,7,11, 0,8,99, 0,8,35, 0,9,167,
  163. 0,8,3, 0,8,131, 0,8,67, 0,9,231,
  164. 80,7,7, 0,8,91, 0,8,27, 0,9,151,
  165. 84,7,67, 0,8,123, 0,8,59, 0,9,215,
  166. 82,7,19, 0,8,107, 0,8,43, 0,9,183,
  167. 0,8,11, 0,8,139, 0,8,75, 0,9,247,
  168. 80,7,5, 0,8,87, 0,8,23, 192,8,0,
  169. 83,7,51, 0,8,119, 0,8,55, 0,9,207,
  170. 81,7,15, 0,8,103, 0,8,39, 0,9,175,
  171. 0,8,7, 0,8,135, 0,8,71, 0,9,239,
  172. 80,7,9, 0,8,95, 0,8,31, 0,9,159,
  173. 84,7,99, 0,8,127, 0,8,63, 0,9,223,
  174. 82,7,27, 0,8,111, 0,8,47, 0,9,191,
  175. 0,8,15, 0,8,143, 0,8,79, 0,9,255
  176. };
  177. static readonly int[] fixed_td = {
  178. 80,5,1, 87,5,257, 83,5,17, 91,5,4097,
  179. 81,5,5, 89,5,1025, 85,5,65, 93,5,16385,
  180. 80,5,3, 88,5,513, 84,5,33, 92,5,8193,
  181. 82,5,9, 90,5,2049, 86,5,129, 192,5,24577,
  182. 80,5,2, 87,5,385, 83,5,25, 91,5,6145,
  183. 81,5,7, 89,5,1537, 85,5,97, 93,5,24577,
  184. 80,5,4, 88,5,769, 84,5,49, 92,5,12289,
  185. 82,5,13, 90,5,3073, 86,5,193, 192,5,24577
  186. };
  187. // Tables for deflate from PKZIP's appnote.txt.
  188. static readonly int[] cplens = { // Copy lengths for literal codes 257..285
  189. 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
  190. 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0
  191. };
  192. // see note #13 above about 258
  193. static readonly int[] cplext = { // Extra bits for literal codes 257..285
  194. 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2,
  195. 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0, 112, 112 // 112==invalid
  196. };
  197. static readonly int[] cpdist = { // Copy offsets for distance codes 0..29
  198. 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
  199. 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
  200. 8193, 12289, 16385, 24577
  201. };
  202. static readonly int[] cpdext = { // Extra bits for distance codes
  203. 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6,
  204. 7, 7, 8, 8, 9, 9, 10, 10, 11, 11,
  205. 12, 12, 13, 13};
  206. // If BMAX needs to be larger than 16, then h and x[] should be uLong.
  207. const int BMAX=15; // maximum bit length of any code
  208. int[] hn = null; // hufts used in space
  209. int[] v = null; // work area for huft_build
  210. int[] c = null; // bit length count table
  211. int[] r = null; // table entry for structure assignment
  212. int[] u = null; // table stack
  213. int[] x = null; // bit offsets, then code stack
  214. private int huft_build(int[] b, // code lengths in bits (all assumed <= BMAX)
  215. int bindex,
  216. int n, // number of codes (assumed <= 288)
  217. int s, // number of simple-valued codes (0..s-1)
  218. int[] d, // list of base values for non-simple codes
  219. int[] e, // list of extra bits for non-simple codes
  220. int[] t, // result: starting table
  221. int[] m, // maximum lookup bits, returns actual
  222. int[] hp,// space for trees
  223. int[] hn,// hufts used in space
  224. int[] v // working area: values in order of bit length
  225. ){
  226. // Given a list of code lengths and a maximum table size, make a set of
  227. // tables to decode that set of codes. Return Z_OK on success, Z_BUF_ERROR
  228. // if the given code set is incomplete (the tables are still built in this
  229. // case), Z_DATA_ERROR if the input is invalid (an over-subscribed set of
  230. // lengths), or Z_MEM_ERROR if not enough memory.
  231. int a; // counter for codes of length k
  232. int f; // i repeats in table every f entries
  233. int g; // maximum code length
  234. int h; // table level
  235. int i; // counter, current code
  236. int j; // counter
  237. int k; // number of bits in current code
  238. int l; // bits per table (returned in m)
  239. int mask; // (1 << w) - 1, to avoid cc -O bug on HP
  240. int p; // pointer into c[], b[], or v[]
  241. int q; // points to current table
  242. int w; // bits before this table == (l * h)
  243. int xp; // pointer into x
  244. int y; // number of dummy codes added
  245. int z; // number of entries in current table
  246. // Generate counts for each bit length
  247. p = 0; i = n;
  248. do {
  249. c[b[bindex+p]]++; p++; i--; // assume all entries <= BMAX
  250. }while(i!=0);
  251. if(c[0] == n){ // null input--all zero length codes
  252. t[0] = -1;
  253. m[0] = 0;
  254. return Z_OK;
  255. }
  256. // Find minimum and maximum length, bound *m by those
  257. l = m[0];
  258. for (j = 1; j <= BMAX; j++)
  259. if(c[j]!=0) break;
  260. k = j; // minimum code length
  261. if(l < j){
  262. l = j;
  263. }
  264. for (i = BMAX; i!=0; i--){
  265. if(c[i]!=0) break;
  266. }
  267. g = i; // maximum code length
  268. if(l > i){
  269. l = i;
  270. }
  271. m[0] = l;
  272. // Adjust last length count to fill out codes, if needed
  273. for (y = 1 << j; j < i; j++, y <<= 1){
  274. if ((y -= c[j]) < 0){
  275. return Z_DATA_ERROR;
  276. }
  277. }
  278. if ((y -= c[i]) < 0){
  279. return Z_DATA_ERROR;
  280. }
  281. c[i] += y;
  282. // Generate starting offsets into the value table for each length
  283. x[1] = j = 0;
  284. p = 1; xp = 2;
  285. while (--i!=0) { // note that i == g from above
  286. x[xp] = (j += c[p]);
  287. xp++;
  288. p++;
  289. }
  290. // Make a table of values in order of bit lengths
  291. i = 0; p = 0;
  292. do {
  293. if ((j = b[bindex+p]) != 0){
  294. v[x[j]++] = i;
  295. }
  296. p++;
  297. }
  298. while (++i < n);
  299. n = x[g]; // set n to length of v
  300. // Generate the Huffman codes and for each, make the table entries
  301. x[0] = i = 0; // first Huffman code is zero
  302. p = 0; // grab values in bit order
  303. h = -1; // no tables yet--level -1
  304. w = -l; // bits decoded == (l * h)
  305. u[0] = 0; // just to keep compilers happy
  306. q = 0; // ditto
  307. z = 0; // ditto
  308. // go through the bit lengths (k already is bits in shortest code)
  309. for (; k <= g; k++){
  310. a = c[k];
  311. while (a--!=0){
  312. // here i is the Huffman code of length k bits for value *p
  313. // make tables up to required level
  314. while (k > w + l){
  315. h++;
  316. w += l; // previous table always l bits
  317. // compute minimum size table less than or equal to l bits
  318. z = g - w;
  319. z = (z > l) ? l : z; // table size upper limit
  320. if((f=1<<(j=k-w))>a+1){ // try a k-w bit table
  321. // too few codes for k-w bit table
  322. f -= a + 1; // deduct codes from patterns left
  323. xp = k;
  324. if(j < z){
  325. while (++j < z){ // try smaller tables up to z bits
  326. if((f <<= 1) <= c[++xp])
  327. break; // enough codes to use up j bits
  328. f -= c[xp]; // else deduct codes from patterns
  329. }
  330. }
  331. }
  332. z = 1 << j; // table entries for j-bit table
  333. // allocate new table
  334. if (hn[0] + z > MANY){ // (note: doesn't matter for fixed)
  335. return Z_DATA_ERROR; // overflow of MANY
  336. }
  337. u[h] = q = /*hp+*/ hn[0]; // DEBUG
  338. hn[0] += z;
  339. // connect to last table, if there is one
  340. if(h!=0){
  341. x[h]=i; // save pattern for backing up
  342. r[0]=(byte)j; // bits in this table
  343. r[1]=(byte)l; // bits to dump before this table
  344. j=i>>(w - l);
  345. r[2] = (int)(q - u[h-1] - j); // offset to this table
  346. System.Array.Copy(r, 0, hp, (u[h-1]+j)*3, 3); // connect to last table
  347. }
  348. else{
  349. t[0] = q; // first table is returned result
  350. }
  351. }
  352. // set up table entry in r
  353. r[1] = (byte)(k - w);
  354. if (p >= n){
  355. r[0] = 128 + 64; // out of values--invalid code
  356. }
  357. else if (v[p] < s){
  358. r[0] = (byte)(v[p] < 256 ? 0 : 32 + 64); // 256 is end-of-block
  359. r[2] = v[p++]; // simple code is just the value
  360. }
  361. else{
  362. r[0]=(byte)(e[v[p]-s]+16+64); // non-simple--look up in lists
  363. r[2]=d[v[p++] - s];
  364. }
  365. // fill code-like entries with r
  366. f=1<<(k-w);
  367. for (j=i>>w;j<z;j+=f){
  368. System.Array.Copy(r, 0, hp, (q+j)*3, 3);
  369. }
  370. // backwards increment the k-bit code i
  371. for (j = 1 << (k - 1); (i & j)!=0; j >>= 1){
  372. i ^= j;
  373. }
  374. i ^= j;
  375. // backup over finished tables
  376. mask = (1 << w) - 1; // needed on HP, cc -O bug
  377. while ((i & mask) != x[h]){
  378. h--; // don't need to update q
  379. w -= l;
  380. mask = (1 << w) - 1;
  381. }
  382. }
  383. }
  384. // Return Z_BUF_ERROR if we were given an incomplete table
  385. return y != 0 && g != 1 ? Z_BUF_ERROR : Z_OK;
  386. }
  387. internal int inflate_trees_bits(int[] c, // 19 code lengths
  388. int[] bb, // bits tree desired/actual depth
  389. int[] tb, // bits tree result
  390. int[] hp, // space for trees
  391. ZStream z // for messages
  392. ){
  393. int result;
  394. initWorkArea(19);
  395. hn[0]=0;
  396. result = huft_build(c, 0, 19, 19, null, null, tb, bb, hp, hn, v);
  397. if(result == Z_DATA_ERROR){
  398. z.msg = "oversubscribed dynamic bit lengths tree";
  399. }
  400. else if(result == Z_BUF_ERROR || bb[0] == 0){
  401. z.msg = "incomplete dynamic bit lengths tree";
  402. result = Z_DATA_ERROR;
  403. }
  404. return result;
  405. }
  406. internal int inflate_trees_dynamic(int nl, // number of literal/length codes
  407. int nd, // number of distance codes
  408. int[] c, // that many (total) code lengths
  409. int[] bl, // literal desired/actual bit depth
  410. int[] bd, // distance desired/actual bit depth
  411. int[] tl, // literal/length tree result
  412. int[] td, // distance tree result
  413. int[] hp, // space for trees
  414. ZStream z // for messages
  415. ){
  416. int result;
  417. // build literal/length tree
  418. initWorkArea(288);
  419. hn[0]=0;
  420. result = huft_build(c, 0, nl, 257, cplens, cplext, tl, bl, hp, hn, v);
  421. if (result != Z_OK || bl[0] == 0){
  422. if(result == Z_DATA_ERROR){
  423. z.msg = "oversubscribed literal/length tree";
  424. }
  425. else if (result != Z_MEM_ERROR){
  426. z.msg = "incomplete literal/length tree";
  427. result = Z_DATA_ERROR;
  428. }
  429. return result;
  430. }
  431. // build distance tree
  432. initWorkArea(288);
  433. result = huft_build(c, nl, nd, 0, cpdist, cpdext, td, bd, hp, hn, v);
  434. if (result != Z_OK || (bd[0] == 0 && nl > 257)){
  435. if (result == Z_DATA_ERROR){
  436. z.msg = "oversubscribed distance tree";
  437. }
  438. else if (result == Z_BUF_ERROR) {
  439. z.msg = "incomplete distance tree";
  440. result = Z_DATA_ERROR;
  441. }
  442. else if (result != Z_MEM_ERROR){
  443. z.msg = "empty distance tree with lengths";
  444. result = Z_DATA_ERROR;
  445. }
  446. return result;
  447. }
  448. return Z_OK;
  449. }
  450. internal static int inflate_trees_fixed(int[] bl, //literal desired/actual bit depth
  451. int[] bd, //distance desired/actual bit depth
  452. int[][] tl,//literal/length tree result
  453. int[][] td,//distance tree result
  454. ZStream z //for memory allocation
  455. ){
  456. bl[0]=fixed_bl;
  457. bd[0]=fixed_bd;
  458. tl[0]=fixed_tl;
  459. td[0]=fixed_td;
  460. return Z_OK;
  461. }
  462. private void initWorkArea(int vsize){
  463. if(hn==null){
  464. hn=new int[1];
  465. v=new int[vsize];
  466. c=new int[BMAX+1];
  467. r=new int[3];
  468. u=new int[BMAX];
  469. x=new int[BMAX+1];
  470. }
  471. if(v.Length<vsize){ v=new int[vsize]; }
  472. for(int i=0; i<vsize; i++){v[i]=0;}
  473. for(int i=0; i<BMAX+1; i++){c[i]=0;}
  474. for(int i=0; i<3; i++){r[i]=0;}
  475. // for(int i=0; i<BMAX; i++){u[i]=0;}
  476. System.Array.Copy(c, 0, u, 0, BMAX);
  477. // for(int i=0; i<BMAX+1; i++){x[i]=0;}
  478. System.Array.Copy(c, 0, x, 0, BMAX+1);
  479. }
  480. }
  481. }
  482. #pragma warning restore
  483. #endif