InfBlocks.cs 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. /*
  5. * $Id: InfBlocks.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 InfBlocks{
  35. private const int MANY=1440;
  36. // And'ing with mask[n] masks the lower n bits
  37. private static readonly int[] inflate_mask = {
  38. 0x00000000, 0x00000001, 0x00000003, 0x00000007, 0x0000000f,
  39. 0x0000001f, 0x0000003f, 0x0000007f, 0x000000ff, 0x000001ff,
  40. 0x000003ff, 0x000007ff, 0x00000fff, 0x00001fff, 0x00003fff,
  41. 0x00007fff, 0x0000ffff
  42. };
  43. // Table for deflate from PKZIP's appnote.txt.
  44. static readonly int[] border = { // Order of the bit length code lengths
  45. 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15
  46. };
  47. private const int Z_OK=0;
  48. private const int Z_STREAM_END=1;
  49. private const int Z_NEED_DICT=2;
  50. private const int Z_ERRNO=-1;
  51. private const int Z_STREAM_ERROR=-2;
  52. private const int Z_DATA_ERROR=-3;
  53. private const int Z_MEM_ERROR=-4;
  54. private const int Z_BUF_ERROR=-5;
  55. private const int Z_VERSION_ERROR=-6;
  56. private const int TYPE=0; // get type bits (3, including end bit)
  57. private const int LENS=1; // get lengths for stored
  58. private const int STORED=2;// processing stored block
  59. private const int TABLE=3; // get table lengths
  60. private const int BTREE=4; // get bit lengths tree for a dynamic block
  61. private const int DTREE=5; // get length, distance trees for a dynamic block
  62. private const int CODES=6; // processing fixed or dynamic block
  63. private const int DRY=7; // output remaining window bytes
  64. private const int DONE=8; // finished last block, done
  65. private const int BAD=9; // ot a data error--stuck here
  66. internal int mode; // current inflate_block mode
  67. internal int left; // if STORED, bytes left to copy
  68. internal int table; // table lengths (14 bits)
  69. internal int index; // index into blens (or border)
  70. internal int[] blens; // bit lengths of codes
  71. internal int[] bb=new int[1]; // bit length tree depth
  72. internal int[] tb=new int[1]; // bit length decoding tree
  73. internal InfCodes codes=new InfCodes(); // if CODES, current state
  74. int last; // true if this block is the last block
  75. // mode independent information
  76. internal int bitk; // bits in bit buffer
  77. internal int bitb; // bit buffer
  78. internal int[] hufts; // single malloc for tree space
  79. internal byte[] window; // sliding window
  80. internal int end; // one byte after sliding window
  81. internal int read; // window read pointer
  82. internal int write; // window write pointer
  83. internal Object checkfn; // check function
  84. internal long check; // check on output
  85. internal InfTree inftree=new InfTree();
  86. internal InfBlocks(ZStream z, Object checkfn, int w){
  87. hufts=new int[MANY*3];
  88. window=new byte[w];
  89. end=w;
  90. this.checkfn = checkfn;
  91. mode = TYPE;
  92. reset(z, null);
  93. }
  94. internal void reset(ZStream z, long[] c){
  95. if(c!=null) c[0]=check;
  96. if(mode==BTREE || mode==DTREE){
  97. }
  98. if(mode==CODES){
  99. codes.free(z);
  100. }
  101. mode=TYPE;
  102. bitk=0;
  103. bitb=0;
  104. read=write=0;
  105. if(checkfn != null)
  106. z.adler=check=z._adler.adler32(0L, null, 0, 0);
  107. }
  108. internal int proc(ZStream z, int r){
  109. int t; // temporary storage
  110. int b; // bit buffer
  111. int k; // bits in bit buffer
  112. int p; // input data pointer
  113. int n; // bytes available there
  114. int q; // output window write pointer
  115. int m; { // bytes to end of window or read pointer
  116. // copy input/output information to locals (UPDATE macro restores)
  117. p=z.next_in_index;n=z.avail_in;b=bitb;k=bitk;} {
  118. q=write;m=(int)(q<read?read-q-1:end-q);}
  119. // process input based on current state
  120. while(true){
  121. switch (mode){
  122. case TYPE:
  123. while(k<(3)){
  124. if(n!=0){
  125. r=Z_OK;
  126. }
  127. else{
  128. bitb=b; bitk=k;
  129. z.avail_in=n;
  130. z.total_in+=p-z.next_in_index;z.next_in_index=p;
  131. write=q;
  132. return inflate_flush(z,r);
  133. };
  134. n--;
  135. b|=(z.next_in[p++]&0xff)<<k;
  136. k+=8;
  137. }
  138. t = (int)(b & 7);
  139. last = t & 1;
  140. switch (t >> 1){
  141. case 0: { // stored
  142. b>>=(3);k-=(3);}
  143. t = k & 7; { // go to byte boundary
  144. b>>=(t);k-=(t);}
  145. mode = LENS; // get length of stored block
  146. break;
  147. case 1: { // fixed
  148. int[] bl=new int[1];
  149. int[] bd=new int[1];
  150. int[][] tl=new int[1][];
  151. int[][] td=new int[1][];
  152. InfTree.inflate_trees_fixed(bl, bd, tl, td, z);
  153. codes.init(bl[0], bd[0], tl[0], 0, td[0], 0, z);
  154. } {
  155. b>>=(3);k-=(3);}
  156. mode = CODES;
  157. break;
  158. case 2: { // dynamic
  159. b>>=(3);k-=(3);}
  160. mode = TABLE;
  161. break;
  162. case 3: { // illegal
  163. b>>=(3);k-=(3);}
  164. mode = BAD;
  165. z.msg = "invalid block type";
  166. r = Z_DATA_ERROR;
  167. bitb=b; bitk=k;
  168. z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;
  169. write=q;
  170. return inflate_flush(z,r);
  171. }
  172. break;
  173. case LENS:
  174. while(k<(32)){
  175. if(n!=0){
  176. r=Z_OK;
  177. }
  178. else{
  179. bitb=b; bitk=k;
  180. z.avail_in=n;
  181. z.total_in+=p-z.next_in_index;z.next_in_index=p;
  182. write=q;
  183. return inflate_flush(z,r);
  184. };
  185. n--;
  186. b|=(z.next_in[p++]&0xff)<<k;
  187. k+=8;
  188. }
  189. if ((((~b) >> 16) & 0xffff) != (b & 0xffff)){
  190. mode = BAD;
  191. z.msg = "invalid stored block lengths";
  192. r = Z_DATA_ERROR;
  193. bitb=b; bitk=k;
  194. z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;
  195. write=q;
  196. return inflate_flush(z,r);
  197. }
  198. left = (b & 0xffff);
  199. b = k = 0; // dump bits
  200. mode = left!=0 ? STORED : (last!=0 ? DRY : TYPE);
  201. break;
  202. case STORED:
  203. if (n == 0){
  204. bitb=b; bitk=k;
  205. z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;
  206. write=q;
  207. return inflate_flush(z,r);
  208. }
  209. if(m==0){
  210. if(q==end&&read!=0){
  211. q=0; m=(int)(q<read?read-q-1:end-q);
  212. }
  213. if(m==0){
  214. write=q;
  215. r=inflate_flush(z,r);
  216. q=write;m=(int)(q<read?read-q-1:end-q);
  217. if(q==end&&read!=0){
  218. q=0; m=(int)(q<read?read-q-1:end-q);
  219. }
  220. if(m==0){
  221. bitb=b; bitk=k;
  222. z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;
  223. write=q;
  224. return inflate_flush(z,r);
  225. }
  226. }
  227. }
  228. r=Z_OK;
  229. t = left;
  230. if(t>n) t = n;
  231. if(t>m) t = m;
  232. System.Array.Copy(z.next_in, p, window, q, t);
  233. p += t; n -= t;
  234. q += t; m -= t;
  235. if ((left -= t) != 0)
  236. break;
  237. mode = last!=0 ? DRY : TYPE;
  238. break;
  239. case TABLE:
  240. while(k<(14)){
  241. if(n!=0){
  242. r=Z_OK;
  243. }
  244. else{
  245. bitb=b; bitk=k;
  246. z.avail_in=n;
  247. z.total_in+=p-z.next_in_index;z.next_in_index=p;
  248. write=q;
  249. return inflate_flush(z,r);
  250. };
  251. n--;
  252. b|=(z.next_in[p++]&0xff)<<k;
  253. k+=8;
  254. }
  255. table = t = (b & 0x3fff);
  256. if ((t & 0x1f) > 29 || ((t >> 5) & 0x1f) > 29) {
  257. mode = BAD;
  258. z.msg = "too many length or distance symbols";
  259. r = Z_DATA_ERROR;
  260. bitb=b; bitk=k;
  261. z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;
  262. write=q;
  263. return inflate_flush(z,r);
  264. }
  265. t = 258 + (t & 0x1f) + ((t >> 5) & 0x1f);
  266. if(blens==null || blens.Length<t){
  267. blens=new int[t];
  268. }
  269. else{
  270. for(int i=0; i<t; i++){blens[i]=0;}
  271. } {
  272. b>>=(14);k-=(14);}
  273. index = 0;
  274. mode = BTREE;
  275. goto case BTREE;
  276. case BTREE:
  277. while (index < 4 + (table >> 10)){
  278. while(k<(3)){
  279. if(n!=0){
  280. r=Z_OK;
  281. }
  282. else{
  283. bitb=b; bitk=k;
  284. z.avail_in=n;
  285. z.total_in+=p-z.next_in_index;z.next_in_index=p;
  286. write=q;
  287. return inflate_flush(z,r);
  288. };
  289. n--;
  290. b|=(z.next_in[p++]&0xff)<<k;
  291. k+=8;
  292. }
  293. blens[border[index++]] = b&7; {
  294. b>>=(3);k-=(3);}
  295. }
  296. while(index < 19){
  297. blens[border[index++]] = 0;
  298. }
  299. bb[0] = 7;
  300. t = inftree.inflate_trees_bits(blens, bb, tb, hufts, z);
  301. if (t != Z_OK){
  302. r = t;
  303. if (r == Z_DATA_ERROR){
  304. blens=null;
  305. mode = BAD;
  306. }
  307. bitb=b; bitk=k;
  308. z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;
  309. write=q;
  310. return inflate_flush(z,r);
  311. }
  312. index = 0;
  313. mode = DTREE;
  314. goto case DTREE;
  315. case DTREE:
  316. while (true){
  317. t = table;
  318. if(!(index < 258 + (t & 0x1f) + ((t >> 5) & 0x1f))){
  319. break;
  320. }
  321. int i, j, c;
  322. t = bb[0];
  323. while(k<(t)){
  324. if(n!=0){
  325. r=Z_OK;
  326. }
  327. else{
  328. bitb=b; bitk=k;
  329. z.avail_in=n;
  330. z.total_in+=p-z.next_in_index;z.next_in_index=p;
  331. write=q;
  332. return inflate_flush(z,r);
  333. };
  334. n--;
  335. b|=(z.next_in[p++]&0xff)<<k;
  336. k+=8;
  337. }
  338. if(tb[0]==-1){
  339. //System.err.println("null...");
  340. }
  341. t=hufts[(tb[0]+(b&inflate_mask[t]))*3+1];
  342. c=hufts[(tb[0]+(b&inflate_mask[t]))*3+2];
  343. if (c < 16){
  344. b>>=(t);k-=(t);
  345. blens[index++] = c;
  346. }
  347. else { // c == 16..18
  348. i = c == 18 ? 7 : c - 14;
  349. j = c == 18 ? 11 : 3;
  350. while(k<(t+i)){
  351. if(n!=0){
  352. r=Z_OK;
  353. }
  354. else{
  355. bitb=b; bitk=k;
  356. z.avail_in=n;
  357. z.total_in+=p-z.next_in_index;z.next_in_index=p;
  358. write=q;
  359. return inflate_flush(z,r);
  360. };
  361. n--;
  362. b|=(z.next_in[p++]&0xff)<<k;
  363. k+=8;
  364. }
  365. b>>=(t);k-=(t);
  366. j += (b & inflate_mask[i]);
  367. b>>=(i);k-=(i);
  368. i = index;
  369. t = table;
  370. if (i + j > 258 + (t & 0x1f) + ((t >> 5) & 0x1f) ||
  371. (c == 16 && i < 1)){
  372. blens=null;
  373. mode = BAD;
  374. z.msg = "invalid bit length repeat";
  375. r = Z_DATA_ERROR;
  376. bitb=b; bitk=k;
  377. z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;
  378. write=q;
  379. return inflate_flush(z,r);
  380. }
  381. c = c == 16 ? blens[i-1] : 0;
  382. do{
  383. blens[i++] = c;
  384. }
  385. while (--j!=0);
  386. index = i;
  387. }
  388. }
  389. tb[0]=-1; {
  390. int[] bl=new int[1];
  391. int[] bd=new int[1];
  392. int[] tl=new int[1];
  393. int[] td=new int[1];
  394. bl[0] = 9; // must be <= 9 for lookahead assumptions
  395. bd[0] = 6; // must be <= 9 for lookahead assumptions
  396. t = table;
  397. t = inftree.inflate_trees_dynamic(257 + (t & 0x1f),
  398. 1 + ((t >> 5) & 0x1f),
  399. blens, bl, bd, tl, td, hufts, z);
  400. if (t != Z_OK){
  401. if (t == Z_DATA_ERROR){
  402. blens=null;
  403. mode = BAD;
  404. }
  405. r = t;
  406. bitb=b; bitk=k;
  407. z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;
  408. write=q;
  409. return inflate_flush(z,r);
  410. }
  411. codes.init(bl[0], bd[0], hufts, tl[0], hufts, td[0], z);
  412. }
  413. mode = CODES;
  414. goto case CODES;
  415. case CODES:
  416. bitb=b; bitk=k;
  417. z.avail_in=n; z.total_in+=p-z.next_in_index;z.next_in_index=p;
  418. write=q;
  419. if ((r = codes.proc(this, z, r)) != Z_STREAM_END){
  420. return inflate_flush(z, r);
  421. }
  422. r = Z_OK;
  423. codes.free(z);
  424. p=z.next_in_index; n=z.avail_in;b=bitb;k=bitk;
  425. q=write;m=(int)(q<read?read-q-1:end-q);
  426. if (last==0){
  427. mode = TYPE;
  428. break;
  429. }
  430. mode = DRY;
  431. goto case DRY;
  432. case DRY:
  433. write=q;
  434. r=inflate_flush(z, r);
  435. q=write; m=(int)(q<read?read-q-1:end-q);
  436. if (read != write){
  437. bitb=b; bitk=k;
  438. z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;
  439. write=q;
  440. return inflate_flush(z, r);
  441. }
  442. mode = DONE;
  443. goto case DONE;
  444. case DONE:
  445. r = Z_STREAM_END;
  446. bitb=b; bitk=k;
  447. z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;
  448. write=q;
  449. return inflate_flush(z, r);
  450. case BAD:
  451. r = Z_DATA_ERROR;
  452. bitb=b; bitk=k;
  453. z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;
  454. write=q;
  455. return inflate_flush(z, r);
  456. default:
  457. r = Z_STREAM_ERROR;
  458. bitb=b; bitk=k;
  459. z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;
  460. write=q;
  461. return inflate_flush(z, r);
  462. }
  463. }
  464. }
  465. internal void free(ZStream z){
  466. reset(z, null);
  467. window=null;
  468. hufts=null;
  469. //ZFREE(z, s);
  470. }
  471. internal void set_dictionary(byte[] d, int start, int n){
  472. System.Array.Copy(d, start, window, 0, n);
  473. read = write = n;
  474. }
  475. // Returns true if inflate is currently at the end of a block generated
  476. // by Z_SYNC_FLUSH or Z_FULL_FLUSH.
  477. internal int sync_point(){
  478. return mode == LENS ? 1 : 0;
  479. }
  480. // copy as much as possible from the sliding window to the output area
  481. internal int inflate_flush(ZStream z, int r){
  482. int n;
  483. int p;
  484. int q;
  485. // local copies of source and destination pointers
  486. p = z.next_out_index;
  487. q = read;
  488. // compute number of bytes to copy as far as end of window
  489. n = (int)((q <= write ? write : end) - q);
  490. if (n > z.avail_out) n = z.avail_out;
  491. if (n!=0 && r == Z_BUF_ERROR) r = Z_OK;
  492. // update counters
  493. z.avail_out -= n;
  494. z.total_out += n;
  495. // update check information
  496. if(checkfn != null)
  497. z.adler=check=z._adler.adler32(check, window, q, n);
  498. // copy as far as end of window
  499. System.Array.Copy(window, q, z.next_out, p, n);
  500. p += n;
  501. q += n;
  502. // see if more to copy at beginning of window
  503. if (q == end){
  504. // wrap pointers
  505. q = 0;
  506. if (write == end)
  507. write = 0;
  508. // compute bytes to copy
  509. n = write - q;
  510. if (n > z.avail_out) n = z.avail_out;
  511. if (n!=0 && r == Z_BUF_ERROR) r = Z_OK;
  512. // update counters
  513. z.avail_out -= n;
  514. z.total_out += n;
  515. // update check information
  516. if(checkfn != null)
  517. z.adler=check=z._adler.adler32(check, window, q, n);
  518. // copy
  519. System.Array.Copy(window, q, z.next_out, p, n);
  520. p += n;
  521. q += n;
  522. }
  523. // update pointers
  524. z.next_out_index = p;
  525. read = q;
  526. // done
  527. return r;
  528. }
  529. }
  530. }
  531. #pragma warning restore
  532. #endif