InfBlocks.cs 24 KB

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