InfCodes.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. using System;
  3. /*
  4. * $Id: InfCodes.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 InfCodes{
  34. private static readonly int[] inflate_mask = {
  35. 0x00000000, 0x00000001, 0x00000003, 0x00000007, 0x0000000f,
  36. 0x0000001f, 0x0000003f, 0x0000007f, 0x000000ff, 0x000001ff,
  37. 0x000003ff, 0x000007ff, 0x00000fff, 0x00001fff, 0x00003fff,
  38. 0x00007fff, 0x0000ffff
  39. };
  40. private const int Z_OK=0;
  41. private const int Z_STREAM_END=1;
  42. private const int Z_NEED_DICT=2;
  43. private const int Z_ERRNO=-1;
  44. private const int Z_STREAM_ERROR=-2;
  45. private const int Z_DATA_ERROR=-3;
  46. private const int Z_MEM_ERROR=-4;
  47. private const int Z_BUF_ERROR=-5;
  48. private const int Z_VERSION_ERROR=-6;
  49. // waiting for "i:"=input,
  50. // "o:"=output,
  51. // "x:"=nothing
  52. private const int START=0; // x: set up for LEN
  53. private const int LEN=1; // i: get length/literal/eob next
  54. private const int LENEXT=2; // i: getting length extra (have base)
  55. private const int DIST=3; // i: get distance next
  56. private const int DISTEXT=4;// i: getting distance extra
  57. private const int COPY=5; // o: copying bytes in window, waiting for space
  58. private const int LIT=6; // o: got literal, waiting for output space
  59. private const int WASH=7; // o: got eob, possibly still output waiting
  60. private const int END=8; // x: got eob and all data flushed
  61. private const int BADCODE=9;// x: got error
  62. int mode; // current inflate_codes mode
  63. // mode dependent information
  64. int len;
  65. int[] tree; // pointer into tree
  66. int tree_index=0;
  67. int need; // bits needed
  68. int lit;
  69. // if EXT or COPY, where and how much
  70. int get; // bits to get for extra
  71. int dist; // distance back to copy from
  72. byte lbits; // ltree bits decoded per branch
  73. byte dbits; // dtree bits decoder per branch
  74. int[] ltree; // literal/length/eob tree
  75. int ltree_index; // literal/length/eob tree
  76. int[] dtree; // distance tree
  77. int dtree_index; // distance tree
  78. internal InfCodes(){
  79. }
  80. internal void init(int bl, int bd,
  81. int[] tl, int tl_index,
  82. int[] td, int td_index, ZStream z){
  83. mode=START;
  84. lbits=(byte)bl;
  85. dbits=(byte)bd;
  86. ltree=tl;
  87. ltree_index=tl_index;
  88. dtree = td;
  89. dtree_index=td_index;
  90. tree=null;
  91. }
  92. internal int proc(InfBlocks s, ZStream z, int r){
  93. int j; // temporary storage
  94. int tindex; // temporary pointer
  95. int e; // extra bits or operation
  96. int b=0; // bit buffer
  97. int k=0; // bits in bit buffer
  98. int p=0; // input data pointer
  99. int n; // bytes available there
  100. int q; // output window write pointer
  101. int m; // bytes to end of window or read pointer
  102. int f; // pointer to copy strings from
  103. // copy input/output information to locals (UPDATE macro restores)
  104. p=z.next_in_index;n=z.avail_in;b=s.bitb;k=s.bitk;
  105. q=s.write;m=q<s.read?s.read-q-1:s.end-q;
  106. // process input and output based on current state
  107. while (true){
  108. switch (mode){
  109. // waiting for "i:"=input, "o:"=output, "x:"=nothing
  110. case START: // x: set up for LEN
  111. if (m >= 258 && n >= 10){
  112. s.bitb=b;s.bitk=k;
  113. z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;
  114. s.write=q;
  115. r = inflate_fast(lbits, dbits,
  116. ltree, ltree_index,
  117. dtree, dtree_index,
  118. s, z);
  119. p=z.next_in_index;n=z.avail_in;b=s.bitb;k=s.bitk;
  120. q=s.write;m=q<s.read?s.read-q-1:s.end-q;
  121. if (r != Z_OK){
  122. mode = r == Z_STREAM_END ? WASH : BADCODE;
  123. break;
  124. }
  125. }
  126. need = lbits;
  127. tree = ltree;
  128. tree_index=ltree_index;
  129. mode = LEN;
  130. goto case LEN;
  131. case LEN: // i: get length/literal/eob next
  132. j = need;
  133. while(k<(j)){
  134. if(n!=0)r=Z_OK;
  135. else{
  136. s.bitb=b;s.bitk=k;
  137. z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;
  138. s.write=q;
  139. return s.inflate_flush(z,r);
  140. }
  141. n--;
  142. b|=(z.next_in[p++]&0xff)<<k;
  143. k+=8;
  144. }
  145. tindex=(tree_index+(b&inflate_mask[j]))*3;
  146. b>>=(tree[tindex+1]);
  147. k-=(tree[tindex+1]);
  148. e=tree[tindex];
  149. if(e == 0){ // literal
  150. lit = tree[tindex+2];
  151. mode = LIT;
  152. break;
  153. }
  154. if((e & 16)!=0 ){ // length
  155. get = e & 15;
  156. len = tree[tindex+2];
  157. mode = LENEXT;
  158. break;
  159. }
  160. if ((e & 64) == 0){ // next table
  161. need = e;
  162. tree_index = tindex/3+tree[tindex+2];
  163. break;
  164. }
  165. if ((e & 32)!=0){ // end of block
  166. mode = WASH;
  167. break;
  168. }
  169. mode = BADCODE; // invalid code
  170. z.msg = "invalid literal/length code";
  171. r = Z_DATA_ERROR;
  172. s.bitb=b;s.bitk=k;
  173. z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;
  174. s.write=q;
  175. return s.inflate_flush(z,r);
  176. case LENEXT: // i: getting length extra (have base)
  177. j = get;
  178. while(k<(j)){
  179. if(n!=0)r=Z_OK;
  180. else{
  181. s.bitb=b;s.bitk=k;
  182. z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;
  183. s.write=q;
  184. return s.inflate_flush(z,r);
  185. }
  186. n--; b|=(z.next_in[p++]&0xff)<<k;
  187. k+=8;
  188. }
  189. len += (b & inflate_mask[j]);
  190. b>>=j;
  191. k-=j;
  192. need = dbits;
  193. tree = dtree;
  194. tree_index=dtree_index;
  195. mode = DIST;
  196. goto case DIST;
  197. case DIST: // i: get distance next
  198. j = need;
  199. while(k<(j)){
  200. if(n!=0)r=Z_OK;
  201. else{
  202. s.bitb=b;s.bitk=k;
  203. z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;
  204. s.write=q;
  205. return s.inflate_flush(z,r);
  206. }
  207. n--; b|=(z.next_in[p++]&0xff)<<k;
  208. k+=8;
  209. }
  210. tindex=(tree_index+(b & inflate_mask[j]))*3;
  211. b>>=tree[tindex+1];
  212. k-=tree[tindex+1];
  213. e = (tree[tindex]);
  214. if((e & 16)!=0){ // distance
  215. get = e & 15;
  216. dist = tree[tindex+2];
  217. mode = DISTEXT;
  218. break;
  219. }
  220. if ((e & 64) == 0){ // next table
  221. need = e;
  222. tree_index = tindex/3 + tree[tindex+2];
  223. break;
  224. }
  225. mode = BADCODE; // invalid code
  226. z.msg = "invalid distance code";
  227. r = Z_DATA_ERROR;
  228. s.bitb=b;s.bitk=k;
  229. z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;
  230. s.write=q;
  231. return s.inflate_flush(z,r);
  232. case DISTEXT: // i: getting distance extra
  233. j = get;
  234. while(k<(j)){
  235. if(n!=0)r=Z_OK;
  236. else{
  237. s.bitb=b;s.bitk=k;
  238. z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;
  239. s.write=q;
  240. return s.inflate_flush(z,r);
  241. }
  242. n--; b|=(z.next_in[p++]&0xff)<<k;
  243. k+=8;
  244. }
  245. dist += (b & inflate_mask[j]);
  246. b>>=j;
  247. k-=j;
  248. mode = COPY;
  249. goto case COPY;
  250. case COPY: // o: copying bytes in window, waiting for space
  251. f = q - dist;
  252. while(f < 0){ // modulo window size-"while" instead
  253. f += s.end; // of "if" handles invalid distances
  254. }
  255. while (len!=0){
  256. if(m==0){
  257. if(q==s.end&&s.read!=0){q=0;m=q<s.read?s.read-q-1:s.end-q;}
  258. if(m==0){
  259. s.write=q; r=s.inflate_flush(z,r);
  260. q=s.write;m=q<s.read?s.read-q-1:s.end-q;
  261. if(q==s.end&&s.read!=0){q=0;m=q<s.read?s.read-q-1:s.end-q;}
  262. if(m==0){
  263. s.bitb=b;s.bitk=k;
  264. z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;
  265. s.write=q;
  266. return s.inflate_flush(z,r);
  267. }
  268. }
  269. }
  270. s.window[q++]=s.window[f++]; m--;
  271. if (f == s.end)
  272. f = 0;
  273. len--;
  274. }
  275. mode = START;
  276. break;
  277. case LIT: // o: got literal, waiting for output space
  278. if(m==0){
  279. if(q==s.end&&s.read!=0){q=0;m=q<s.read?s.read-q-1:s.end-q;}
  280. if(m==0){
  281. s.write=q; r=s.inflate_flush(z,r);
  282. q=s.write;m=q<s.read?s.read-q-1:s.end-q;
  283. if(q==s.end&&s.read!=0){q=0;m=q<s.read?s.read-q-1:s.end-q;}
  284. if(m==0){
  285. s.bitb=b;s.bitk=k;
  286. z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;
  287. s.write=q;
  288. return s.inflate_flush(z,r);
  289. }
  290. }
  291. }
  292. r=Z_OK;
  293. s.window[q++]=(byte)lit; m--;
  294. mode = START;
  295. break;
  296. case WASH: // o: got eob, possibly more output
  297. if (k > 7){ // return unused byte, if any
  298. k -= 8;
  299. n++;
  300. p--; // can always return one
  301. }
  302. s.write=q; r=s.inflate_flush(z,r);
  303. q=s.write;m=q<s.read?s.read-q-1:s.end-q;
  304. if (s.read != s.write){
  305. s.bitb=b;s.bitk=k;
  306. z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;
  307. s.write=q;
  308. return s.inflate_flush(z,r);
  309. }
  310. mode = END;
  311. goto case END;
  312. case END:
  313. r = Z_STREAM_END;
  314. s.bitb=b;s.bitk=k;
  315. z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;
  316. s.write=q;
  317. return s.inflate_flush(z,r);
  318. case BADCODE: // x: got error
  319. r = Z_DATA_ERROR;
  320. s.bitb=b;s.bitk=k;
  321. z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;
  322. s.write=q;
  323. return s.inflate_flush(z,r);
  324. default:
  325. r = Z_STREAM_ERROR;
  326. s.bitb=b;s.bitk=k;
  327. z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;
  328. s.write=q;
  329. return s.inflate_flush(z,r);
  330. }
  331. }
  332. }
  333. internal void free(ZStream z){
  334. // ZFREE(z, c);
  335. }
  336. // Called with number of bytes left to write in window at least 258
  337. // (the maximum string length) and number of input bytes available
  338. // at least ten. The ten bytes are six bytes for the longest length/
  339. // distance pair plus four bytes for overloading the bit buffer.
  340. internal int inflate_fast(int bl, int bd,
  341. int[] tl, int tl_index,
  342. int[] td, int td_index,
  343. InfBlocks s, ZStream z){
  344. int t; // temporary pointer
  345. int[] tp; // temporary pointer
  346. int tp_index; // temporary pointer
  347. int e; // extra bits or operation
  348. int b; // bit buffer
  349. int k; // bits in bit buffer
  350. int p; // input data pointer
  351. int n; // bytes available there
  352. int q; // output window write pointer
  353. int m; // bytes to end of window or read pointer
  354. int ml; // mask for literal/length tree
  355. int md; // mask for distance tree
  356. int c; // bytes to copy
  357. int d; // distance back to copy from
  358. int r; // copy source pointer
  359. int tp_index_t_3; // (tp_index+t)*3
  360. // load input, output, bit values
  361. p=z.next_in_index;n=z.avail_in;b=s.bitb;k=s.bitk;
  362. q=s.write;m=q<s.read?s.read-q-1:s.end-q;
  363. // initialize masks
  364. ml = inflate_mask[bl];
  365. md = inflate_mask[bd];
  366. // do until not enough input or output space for fast loop
  367. do { // assume called with m >= 258 && n >= 10
  368. // get literal/length code
  369. while(k<(20)){ // max bits for literal/length code
  370. n--;
  371. b|=(z.next_in[p++]&0xff)<<k;k+=8;
  372. }
  373. t= b&ml;
  374. tp=tl;
  375. tp_index=tl_index;
  376. tp_index_t_3=(tp_index+t)*3;
  377. if ((e = tp[tp_index_t_3]) == 0){
  378. b>>=(tp[tp_index_t_3+1]); k-=(tp[tp_index_t_3+1]);
  379. s.window[q++] = (byte)tp[tp_index_t_3+2];
  380. m--;
  381. continue;
  382. }
  383. do {
  384. b>>=(tp[tp_index_t_3+1]); k-=(tp[tp_index_t_3+1]);
  385. if((e&16)!=0){
  386. e &= 15;
  387. c = tp[tp_index_t_3+2] + ((int)b & inflate_mask[e]);
  388. b>>=e; k-=e;
  389. // decode distance base of block to copy
  390. while(k<(15)){ // max bits for distance code
  391. n--;
  392. b|=(z.next_in[p++]&0xff)<<k;k+=8;
  393. }
  394. t= b&md;
  395. tp=td;
  396. tp_index=td_index;
  397. tp_index_t_3=(tp_index+t)*3;
  398. e = tp[tp_index_t_3];
  399. do {
  400. b>>=(tp[tp_index_t_3+1]); k-=(tp[tp_index_t_3+1]);
  401. if((e&16)!=0){
  402. // get extra bits to add to distance base
  403. e &= 15;
  404. while(k<(e)){ // get extra bits (up to 13)
  405. n--;
  406. b|=(z.next_in[p++]&0xff)<<k;k+=8;
  407. }
  408. d = tp[tp_index_t_3+2] + (b&inflate_mask[e]);
  409. b>>=(e); k-=(e);
  410. // do the copy
  411. m -= c;
  412. if (q >= d){ // offset before dest
  413. // just copy
  414. r=q-d;
  415. if(q-r>0 && 2>(q-r)){
  416. s.window[q++]=s.window[r++]; // minimum count is three,
  417. s.window[q++]=s.window[r++]; // so unroll loop a little
  418. c-=2;
  419. }
  420. else{
  421. System.Array.Copy(s.window, r, s.window, q, 2);
  422. q+=2; r+=2; c-=2;
  423. }
  424. }
  425. else{ // else offset after destination
  426. r=q-d;
  427. do{
  428. r+=s.end; // force pointer in window
  429. }while(r<0); // covers invalid distances
  430. e=s.end-r;
  431. if(c>e){ // if source crosses,
  432. c-=e; // wrapped copy
  433. if(q-r>0 && e>(q-r)){
  434. do{s.window[q++] = s.window[r++];}
  435. while(--e!=0);
  436. }
  437. else{
  438. System.Array.Copy(s.window, r, s.window, q, e);
  439. q+=e; r+=e; e=0;
  440. }
  441. r = 0; // copy rest from start of window
  442. }
  443. }
  444. // copy all or what's left
  445. if(q-r>0 && c>(q-r)){
  446. do{s.window[q++] = s.window[r++];}
  447. while(--c!=0);
  448. }
  449. else{
  450. System.Array.Copy(s.window, r, s.window, q, c);
  451. q+=c; r+=c; c=0;
  452. }
  453. break;
  454. }
  455. else if((e&64)==0){
  456. t+=tp[tp_index_t_3+2];
  457. t+=(b&inflate_mask[e]);
  458. tp_index_t_3=(tp_index+t)*3;
  459. e=tp[tp_index_t_3];
  460. }
  461. else{
  462. z.msg = "invalid distance code";
  463. c=z.avail_in-n;c=(k>>3)<c?k>>3:c;n+=c;p-=c;k-=c<<3;
  464. s.bitb=b;s.bitk=k;
  465. z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;
  466. s.write=q;
  467. return Z_DATA_ERROR;
  468. }
  469. }
  470. while(true);
  471. break;
  472. }
  473. if((e&64)==0){
  474. t+=tp[tp_index_t_3+2];
  475. t+=(b&inflate_mask[e]);
  476. tp_index_t_3=(tp_index+t)*3;
  477. if((e=tp[tp_index_t_3])==0){
  478. b>>=(tp[tp_index_t_3+1]); k-=(tp[tp_index_t_3+1]);
  479. s.window[q++]=(byte)tp[tp_index_t_3+2];
  480. m--;
  481. break;
  482. }
  483. }
  484. else if((e&32)!=0){
  485. c=z.avail_in-n;c=(k>>3)<c?k>>3:c;n+=c;p-=c;k-=c<<3;
  486. s.bitb=b;s.bitk=k;
  487. z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;
  488. s.write=q;
  489. return Z_STREAM_END;
  490. }
  491. else{
  492. z.msg="invalid literal/length code";
  493. c=z.avail_in-n;c=(k>>3)<c?k>>3:c;n+=c;p-=c;k-=c<<3;
  494. s.bitb=b;s.bitk=k;
  495. z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;
  496. s.write=q;
  497. return Z_DATA_ERROR;
  498. }
  499. }
  500. while(true);
  501. }
  502. while(m>=258 && n>= 10);
  503. // not enough input or output--restore pointers and return
  504. c=z.avail_in-n;c=(k>>3)<c?k>>3:c;n+=c;p-=c;k-=c<<3;
  505. s.bitb=b;s.bitk=k;
  506. z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;
  507. s.write=q;
  508. return Z_OK;
  509. }
  510. }
  511. }
  512. #endif