InfTree.cs 26 KB

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