InfCodes.cs 25 KB

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