You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

InfCodes.java 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605
  1. /* -*-mode:java; c-basic-offset:2; -*- */
  2. /*
  3. Copyright (c) 2000,2001,2002,2003 ymnk, JCraft,Inc. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions are met:
  6. 1. Redistributions of source code must retain the above copyright notice,
  7. this list of conditions and the following disclaimer.
  8. 2. Redistributions in binary form must reproduce the above copyright
  9. notice, this list of conditions and the following disclaimer in
  10. the documentation and/or other materials provided with the distribution.
  11. 3. The names of the authors may not be used to endorse or promote products
  12. derived from this software without specific prior written permission.
  13. THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
  14. INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  15. FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
  16. INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
  17. INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  18. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  19. OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  20. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  21. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  22. EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  23. */
  24. /*
  25. * This program is based on zlib-1.1.3, so all credit should go authors
  26. * Jean-loup Gailly(jloup@gzip.org) and Mark Adler(madler@alumni.caltech.edu)
  27. * and contributors of zlib.
  28. */
  29. package com.jcraft.jzlib;
  30. final class InfCodes{
  31. static final private int[] inflate_mask = {
  32. 0x00000000, 0x00000001, 0x00000003, 0x00000007, 0x0000000f,
  33. 0x0000001f, 0x0000003f, 0x0000007f, 0x000000ff, 0x000001ff,
  34. 0x000003ff, 0x000007ff, 0x00000fff, 0x00001fff, 0x00003fff,
  35. 0x00007fff, 0x0000ffff
  36. };
  37. static final private int Z_OK=0;
  38. static final private int Z_STREAM_END=1;
  39. static final private int Z_NEED_DICT=2;
  40. static final private int Z_ERRNO=-1;
  41. static final private int Z_STREAM_ERROR=-2;
  42. static final private int Z_DATA_ERROR=-3;
  43. static final private int Z_MEM_ERROR=-4;
  44. static final private int Z_BUF_ERROR=-5;
  45. static final private int Z_VERSION_ERROR=-6;
  46. // waiting for "i:"=input,
  47. // "o:"=output,
  48. // "x:"=nothing
  49. static final private int START=0; // x: set up for LEN
  50. static final private int LEN=1; // i: get length/literal/eob next
  51. static final private int LENEXT=2; // i: getting length extra (have base)
  52. static final private int DIST=3; // i: get distance next
  53. static final private int DISTEXT=4;// i: getting distance extra
  54. static final private int COPY=5; // o: copying bytes in window, waiting for space
  55. static final private int LIT=6; // o: got literal, waiting for output space
  56. static final private int WASH=7; // o: got eob, possibly still output waiting
  57. static final private int END=8; // x: got eob and all data flushed
  58. static final private int BADCODE=9;// x: got error
  59. int mode; // current inflate_codes mode
  60. // mode dependent information
  61. int len;
  62. int[] tree; // pointer into tree
  63. int tree_index=0;
  64. int need; // bits needed
  65. int lit;
  66. // if EXT or COPY, where and how much
  67. int get; // bits to get for extra
  68. int dist; // distance back to copy from
  69. byte lbits; // ltree bits decoded per branch
  70. byte dbits; // dtree bits decoder per branch
  71. int[] ltree; // literal/length/eob tree
  72. int ltree_index; // literal/length/eob tree
  73. int[] dtree; // distance tree
  74. int dtree_index; // distance tree
  75. InfCodes(){
  76. }
  77. void init(int bl, int bd,
  78. int[] tl, int tl_index,
  79. int[] td, int td_index, ZStream z){
  80. mode=START;
  81. lbits=(byte)bl;
  82. dbits=(byte)bd;
  83. ltree=tl;
  84. ltree_index=tl_index;
  85. dtree = td;
  86. dtree_index=td_index;
  87. tree=null;
  88. }
  89. int proc(InfBlocks s, ZStream z, int r){
  90. int j; // temporary storage
  91. int[] t; // temporary pointer
  92. int tindex; // temporary pointer
  93. int e; // extra bits or operation
  94. int b=0; // bit buffer
  95. int k=0; // bits in bit buffer
  96. int p=0; // input data pointer
  97. int n; // bytes available there
  98. int q; // output window write pointer
  99. int m; // bytes to end of window or read pointer
  100. int f; // pointer to copy strings from
  101. // copy input/output information to locals (UPDATE macro restores)
  102. p=z.next_in_index;n=z.avail_in;b=s.bitb;k=s.bitk;
  103. q=s.write;m=q<s.read?s.read-q-1:s.end-q;
  104. // process input and output based on current state
  105. while (true){
  106. switch (mode){
  107. // waiting for "i:"=input, "o:"=output, "x:"=nothing
  108. case START: // x: set up for LEN
  109. if (m >= 258 && n >= 10){
  110. s.bitb=b;s.bitk=k;
  111. z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;
  112. s.write=q;
  113. r = inflate_fast(lbits, dbits,
  114. ltree, ltree_index,
  115. dtree, dtree_index,
  116. s, z);
  117. p=z.next_in_index;n=z.avail_in;b=s.bitb;k=s.bitk;
  118. q=s.write;m=q<s.read?s.read-q-1:s.end-q;
  119. if (r != Z_OK){
  120. mode = r == Z_STREAM_END ? WASH : BADCODE;
  121. break;
  122. }
  123. }
  124. need = lbits;
  125. tree = ltree;
  126. tree_index=ltree_index;
  127. mode = LEN;
  128. case LEN: // i: get length/literal/eob next
  129. j = need;
  130. while(k<(j)){
  131. if(n!=0)r=Z_OK;
  132. else{
  133. s.bitb=b;s.bitk=k;
  134. z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;
  135. s.write=q;
  136. return s.inflate_flush(z,r);
  137. }
  138. n--;
  139. b|=(z.next_in[p++]&0xff)<<k;
  140. k+=8;
  141. }
  142. tindex=(tree_index+(b&inflate_mask[j]))*3;
  143. b>>>=(tree[tindex+1]);
  144. k-=(tree[tindex+1]);
  145. e=tree[tindex];
  146. if(e == 0){ // literal
  147. lit = tree[tindex+2];
  148. mode = LIT;
  149. break;
  150. }
  151. if((e & 16)!=0 ){ // length
  152. get = e & 15;
  153. len = tree[tindex+2];
  154. mode = LENEXT;
  155. break;
  156. }
  157. if ((e & 64) == 0){ // next table
  158. need = e;
  159. tree_index = tindex/3+tree[tindex+2];
  160. break;
  161. }
  162. if ((e & 32)!=0){ // end of block
  163. mode = WASH;
  164. break;
  165. }
  166. mode = BADCODE; // invalid code
  167. z.msg = "invalid literal/length code";
  168. r = Z_DATA_ERROR;
  169. s.bitb=b;s.bitk=k;
  170. z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;
  171. s.write=q;
  172. return s.inflate_flush(z,r);
  173. case LENEXT: // i: getting length extra (have base)
  174. j = get;
  175. while(k<(j)){
  176. if(n!=0)r=Z_OK;
  177. else{
  178. s.bitb=b;s.bitk=k;
  179. z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;
  180. s.write=q;
  181. return s.inflate_flush(z,r);
  182. }
  183. n--; b|=(z.next_in[p++]&0xff)<<k;
  184. k+=8;
  185. }
  186. len += (b & inflate_mask[j]);
  187. b>>=j;
  188. k-=j;
  189. need = dbits;
  190. tree = dtree;
  191. tree_index=dtree_index;
  192. mode = DIST;
  193. case DIST: // i: get distance next
  194. j = need;
  195. while(k<(j)){
  196. if(n!=0)r=Z_OK;
  197. else{
  198. s.bitb=b;s.bitk=k;
  199. z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;
  200. s.write=q;
  201. return s.inflate_flush(z,r);
  202. }
  203. n--; b|=(z.next_in[p++]&0xff)<<k;
  204. k+=8;
  205. }
  206. tindex=(tree_index+(b & inflate_mask[j]))*3;
  207. b>>=tree[tindex+1];
  208. k-=tree[tindex+1];
  209. e = (tree[tindex]);
  210. if((e & 16)!=0){ // distance
  211. get = e & 15;
  212. dist = tree[tindex+2];
  213. mode = DISTEXT;
  214. break;
  215. }
  216. if ((e & 64) == 0){ // next table
  217. need = e;
  218. tree_index = tindex/3 + tree[tindex+2];
  219. break;
  220. }
  221. mode = BADCODE; // invalid code
  222. z.msg = "invalid distance code";
  223. r = Z_DATA_ERROR;
  224. s.bitb=b;s.bitk=k;
  225. z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;
  226. s.write=q;
  227. return s.inflate_flush(z,r);
  228. case DISTEXT: // i: getting distance extra
  229. j = get;
  230. while(k<(j)){
  231. if(n!=0)r=Z_OK;
  232. else{
  233. s.bitb=b;s.bitk=k;
  234. z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;
  235. s.write=q;
  236. return s.inflate_flush(z,r);
  237. }
  238. n--; b|=(z.next_in[p++]&0xff)<<k;
  239. k+=8;
  240. }
  241. dist += (b & inflate_mask[j]);
  242. b>>=j;
  243. k-=j;
  244. mode = COPY;
  245. case COPY: // o: copying bytes in window, waiting for space
  246. f = q - dist;
  247. while(f < 0){ // modulo window size-"while" instead
  248. f += s.end; // of "if" handles invalid distances
  249. }
  250. while (len!=0){
  251. if(m==0){
  252. if(q==s.end&&s.read!=0){q=0;m=q<s.read?s.read-q-1:s.end-q;}
  253. if(m==0){
  254. s.write=q; r=s.inflate_flush(z,r);
  255. q=s.write;m=q<s.read?s.read-q-1:s.end-q;
  256. if(q==s.end&&s.read!=0){q=0;m=q<s.read?s.read-q-1:s.end-q;}
  257. if(m==0){
  258. s.bitb=b;s.bitk=k;
  259. z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;
  260. s.write=q;
  261. return s.inflate_flush(z,r);
  262. }
  263. }
  264. }
  265. s.window[q++]=s.window[f++]; m--;
  266. if (f == s.end)
  267. f = 0;
  268. len--;
  269. }
  270. mode = START;
  271. break;
  272. case LIT: // o: got literal, waiting for output space
  273. if(m==0){
  274. if(q==s.end&&s.read!=0){q=0;m=q<s.read?s.read-q-1:s.end-q;}
  275. if(m==0){
  276. s.write=q; r=s.inflate_flush(z,r);
  277. q=s.write;m=q<s.read?s.read-q-1:s.end-q;
  278. if(q==s.end&&s.read!=0){q=0;m=q<s.read?s.read-q-1:s.end-q;}
  279. if(m==0){
  280. s.bitb=b;s.bitk=k;
  281. z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;
  282. s.write=q;
  283. return s.inflate_flush(z,r);
  284. }
  285. }
  286. }
  287. r=Z_OK;
  288. s.window[q++]=(byte)lit; m--;
  289. mode = START;
  290. break;
  291. case WASH: // o: got eob, possibly more output
  292. if (k > 7){ // return unused byte, if any
  293. k -= 8;
  294. n++;
  295. p--; // can always return one
  296. }
  297. s.write=q; r=s.inflate_flush(z,r);
  298. q=s.write;m=q<s.read?s.read-q-1:s.end-q;
  299. if (s.read != s.write){
  300. s.bitb=b;s.bitk=k;
  301. z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;
  302. s.write=q;
  303. return s.inflate_flush(z,r);
  304. }
  305. mode = END;
  306. case END:
  307. r = Z_STREAM_END;
  308. s.bitb=b;s.bitk=k;
  309. z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;
  310. s.write=q;
  311. return s.inflate_flush(z,r);
  312. case BADCODE: // x: got error
  313. r = Z_DATA_ERROR;
  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. default:
  319. r = Z_STREAM_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. }
  325. }
  326. }
  327. void free(ZStream z){
  328. // ZFREE(z, c);
  329. }
  330. // Called with number of bytes left to write in window at least 258
  331. // (the maximum string length) and number of input bytes available
  332. // at least ten. The ten bytes are six bytes for the longest length/
  333. // distance pair plus four bytes for overloading the bit buffer.
  334. int inflate_fast(int bl, int bd,
  335. int[] tl, int tl_index,
  336. int[] td, int td_index,
  337. InfBlocks s, ZStream z){
  338. int t; // temporary pointer
  339. int[] tp; // temporary pointer
  340. int tp_index; // temporary pointer
  341. int e; // extra bits or operation
  342. int b; // bit buffer
  343. int k; // bits in bit buffer
  344. int p; // input data pointer
  345. int n; // bytes available there
  346. int q; // output window write pointer
  347. int m; // bytes to end of window or read pointer
  348. int ml; // mask for literal/length tree
  349. int md; // mask for distance tree
  350. int c; // bytes to copy
  351. int d; // distance back to copy from
  352. int r; // copy source pointer
  353. int tp_index_t_3; // (tp_index+t)*3
  354. // load input, output, bit values
  355. p=z.next_in_index;n=z.avail_in;b=s.bitb;k=s.bitk;
  356. q=s.write;m=q<s.read?s.read-q-1:s.end-q;
  357. // initialize masks
  358. ml = inflate_mask[bl];
  359. md = inflate_mask[bd];
  360. // do until not enough input or output space for fast loop
  361. do { // assume called with m >= 258 && n >= 10
  362. // get literal/length code
  363. while(k<(20)){ // max bits for literal/length code
  364. n--;
  365. b|=(z.next_in[p++]&0xff)<<k;k+=8;
  366. }
  367. t= b&ml;
  368. tp=tl;
  369. tp_index=tl_index;
  370. tp_index_t_3=(tp_index+t)*3;
  371. if ((e = tp[tp_index_t_3]) == 0){
  372. b>>=(tp[tp_index_t_3+1]); k-=(tp[tp_index_t_3+1]);
  373. s.window[q++] = (byte)tp[tp_index_t_3+2];
  374. m--;
  375. continue;
  376. }
  377. do {
  378. b>>=(tp[tp_index_t_3+1]); k-=(tp[tp_index_t_3+1]);
  379. if((e&16)!=0){
  380. e &= 15;
  381. c = tp[tp_index_t_3+2] + ((int)b & inflate_mask[e]);
  382. b>>=e; k-=e;
  383. // decode distance base of block to copy
  384. while(k<(15)){ // max bits for distance code
  385. n--;
  386. b|=(z.next_in[p++]&0xff)<<k;k+=8;
  387. }
  388. t= b&md;
  389. tp=td;
  390. tp_index=td_index;
  391. tp_index_t_3=(tp_index+t)*3;
  392. e = tp[tp_index_t_3];
  393. do {
  394. b>>=(tp[tp_index_t_3+1]); k-=(tp[tp_index_t_3+1]);
  395. if((e&16)!=0){
  396. // get extra bits to add to distance base
  397. e &= 15;
  398. while(k<(e)){ // get extra bits (up to 13)
  399. n--;
  400. b|=(z.next_in[p++]&0xff)<<k;k+=8;
  401. }
  402. d = tp[tp_index_t_3+2] + (b&inflate_mask[e]);
  403. b>>=(e); k-=(e);
  404. // do the copy
  405. m -= c;
  406. if (q >= d){ // offset before dest
  407. // just copy
  408. r=q-d;
  409. if(q-r>0 && 2>(q-r)){
  410. s.window[q++]=s.window[r++]; // minimum count is three,
  411. s.window[q++]=s.window[r++]; // so unroll loop a little
  412. c-=2;
  413. }
  414. else{
  415. System.arraycopy(s.window, r, s.window, q, 2);
  416. q+=2; r+=2; c-=2;
  417. }
  418. }
  419. else{ // else offset after destination
  420. r=q-d;
  421. do{
  422. r+=s.end; // force pointer in window
  423. }while(r<0); // covers invalid distances
  424. e=s.end-r;
  425. if(c>e){ // if source crosses,
  426. c-=e; // wrapped copy
  427. if(q-r>0 && e>(q-r)){
  428. do{s.window[q++] = s.window[r++];}
  429. while(--e!=0);
  430. }
  431. else{
  432. System.arraycopy(s.window, r, s.window, q, e);
  433. q+=e; r+=e; e=0;
  434. }
  435. r = 0; // copy rest from start of window
  436. }
  437. }
  438. // copy all or what's left
  439. if(q-r>0 && c>(q-r)){
  440. do{s.window[q++] = s.window[r++];}
  441. while(--c!=0);
  442. }
  443. else{
  444. System.arraycopy(s.window, r, s.window, q, c);
  445. q+=c; r+=c; c=0;
  446. }
  447. break;
  448. }
  449. else if((e&64)==0){
  450. t+=tp[tp_index_t_3+2];
  451. t+=(b&inflate_mask[e]);
  452. tp_index_t_3=(tp_index+t)*3;
  453. e=tp[tp_index_t_3];
  454. }
  455. else{
  456. z.msg = "invalid distance code";
  457. c=z.avail_in-n;c=(k>>3)<c?k>>3:c;n+=c;p-=c;k-=c<<3;
  458. s.bitb=b;s.bitk=k;
  459. z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;
  460. s.write=q;
  461. return Z_DATA_ERROR;
  462. }
  463. }
  464. while(true);
  465. break;
  466. }
  467. if((e&64)==0){
  468. t+=tp[tp_index_t_3+2];
  469. t+=(b&inflate_mask[e]);
  470. tp_index_t_3=(tp_index+t)*3;
  471. if((e=tp[tp_index_t_3])==0){
  472. b>>=(tp[tp_index_t_3+1]); k-=(tp[tp_index_t_3+1]);
  473. s.window[q++]=(byte)tp[tp_index_t_3+2];
  474. m--;
  475. break;
  476. }
  477. }
  478. else if((e&32)!=0){
  479. c=z.avail_in-n;c=(k>>3)<c?k>>3:c;n+=c;p-=c;k-=c<<3;
  480. s.bitb=b;s.bitk=k;
  481. z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;
  482. s.write=q;
  483. return Z_STREAM_END;
  484. }
  485. else{
  486. z.msg="invalid literal/length code";
  487. c=z.avail_in-n;c=(k>>3)<c?k>>3:c;n+=c;p-=c;k-=c<<3;
  488. s.bitb=b;s.bitk=k;
  489. z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;
  490. s.write=q;
  491. return Z_DATA_ERROR;
  492. }
  493. }
  494. while(true);
  495. }
  496. while(m>=258 && n>= 10);
  497. // not enough input or output--restore pointers and return
  498. c=z.avail_in-n;c=(k>>3)<c?k>>3:c;n+=c;p-=c;k-=c<<3;
  499. s.bitb=b;s.bitk=k;
  500. z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;
  501. s.write=q;
  502. return Z_OK;
  503. }
  504. }