1 2 29 34 35 package com.jcraft.jzlib; 36 37 final class InfCodes{ 38 39 static final private int[] inflate_mask = { 40 0x00000000, 0x00000001, 0x00000003, 0x00000007, 0x0000000f, 41 0x0000001f, 0x0000003f, 0x0000007f, 0x000000ff, 0x000001ff, 42 0x000003ff, 0x000007ff, 0x00000fff, 0x00001fff, 0x00003fff, 43 0x00007fff, 0x0000ffff 44 }; 45 46 static final private int Z_OK=0; 47 static final private int Z_STREAM_END=1; 48 static final private int Z_NEED_DICT=2; 49 static final private int Z_ERRNO=-1; 50 static final private int Z_STREAM_ERROR=-2; 51 static final private int Z_DATA_ERROR=-3; 52 static final private int Z_MEM_ERROR=-4; 53 static final private int Z_BUF_ERROR=-5; 54 static final private int Z_VERSION_ERROR=-6; 55 56 static final private int START=0; static final private int LEN=1; static final private int LENEXT=2; static final private int DIST=3; static final private int DISTEXT=4; static final private int COPY=5; static final private int LIT=6; static final private int WASH=7; static final private int END=8; static final private int BADCODE=9; 70 int mode; 72 int len; 74 75 int[] tree; int tree_index=0; 77 int need; 79 int lit; 80 81 int get; int dist; 85 byte lbits; byte dbits; int[] ltree; int ltree_index; int[] dtree; int dtree_index; 92 InfCodes(){ 93 } 94 void init(int bl, int bd, 95 int[] tl, int tl_index, 96 int[] td, int td_index, ZStream z){ 97 mode=START; 98 lbits=(byte)bl; 99 dbits=(byte)bd; 100 ltree=tl; 101 ltree_index=tl_index; 102 dtree = td; 103 dtree_index=td_index; 104 tree=null; 105 } 106 107 int proc(InfBlocks s, ZStream z, int r){ 108 int j; int[] t; int tindex; int e; int b=0; int k=0; int p=0; int n; int q; int m; int f; 120 p=z.next_in_index;n=z.avail_in;b=s.bitb;k=s.bitk; 122 q=s.write;m=q<s.read?s.read-q-1:s.end-q; 123 124 while (true){ 126 switch (mode){ 127 case START: if (m >= 258 && n >= 10){ 130 131 s.bitb=b;s.bitk=k; 132 z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p; 133 s.write=q; 134 r = inflate_fast(lbits, dbits, 135 ltree, ltree_index, 136 dtree, dtree_index, 137 s, z); 138 139 p=z.next_in_index;n=z.avail_in;b=s.bitb;k=s.bitk; 140 q=s.write;m=q<s.read?s.read-q-1:s.end-q; 141 142 if (r != Z_OK){ 143 mode = r == Z_STREAM_END ? WASH : BADCODE; 144 break; 145 } 146 } 147 need = lbits; 148 tree = ltree; 149 tree_index=ltree_index; 150 151 mode = LEN; 152 case LEN: j = need; 154 155 while(k<(j)){ 156 if(n!=0)r=Z_OK; 157 else{ 158 159 s.bitb=b;s.bitk=k; 160 z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p; 161 s.write=q; 162 return s.inflate_flush(z,r); 163 } 164 n--; 165 b|=(z.next_in[p++]&0xff)<<k; 166 k+=8; 167 } 168 169 tindex=(tree_index+(b&inflate_mask[j]))*3; 170 171 b>>>=(tree[tindex+1]); 172 k-=(tree[tindex+1]); 173 174 e=tree[tindex]; 175 176 if(e == 0){ lit = tree[tindex+2]; 178 mode = LIT; 179 break; 180 } 181 if((e & 16)!=0 ){ get = e & 15; 183 len = tree[tindex+2]; 184 mode = LENEXT; 185 break; 186 } 187 if ((e & 64) == 0){ need = e; 189 tree_index = tindex/3+tree[tindex+2]; 190 break; 191 } 192 if ((e & 32)!=0){ mode = WASH; 194 break; 195 } 196 mode = BADCODE; z.msg = "invalid literal/length code"; 198 r = Z_DATA_ERROR; 199 200 s.bitb=b;s.bitk=k; 201 z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p; 202 s.write=q; 203 return s.inflate_flush(z,r); 204 205 case LENEXT: j = get; 207 208 while(k<(j)){ 209 if(n!=0)r=Z_OK; 210 else{ 211 212 s.bitb=b;s.bitk=k; 213 z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p; 214 s.write=q; 215 return s.inflate_flush(z,r); 216 } 217 n--; b|=(z.next_in[p++]&0xff)<<k; 218 k+=8; 219 } 220 221 len += (b & inflate_mask[j]); 222 223 b>>=j; 224 k-=j; 225 226 need = dbits; 227 tree = dtree; 228 tree_index=dtree_index; 229 mode = DIST; 230 case DIST: j = need; 232 233 while(k<(j)){ 234 if(n!=0)r=Z_OK; 235 else{ 236 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 246 tindex=(tree_index+(b & inflate_mask[j]))*3; 247 248 b>>=tree[tindex+1]; 249 k-=tree[tindex+1]; 250 251 e = (tree[tindex]); 252 if((e & 16)!=0){ get = e & 15; 254 dist = tree[tindex+2]; 255 mode = DISTEXT; 256 break; 257 } 258 if ((e & 64) == 0){ need = e; 260 tree_index = tindex/3 + tree[tindex+2]; 261 break; 262 } 263 mode = BADCODE; z.msg = "invalid distance code"; 265 r = Z_DATA_ERROR; 266 267 s.bitb=b;s.bitk=k; 268 z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p; 269 s.write=q; 270 return s.inflate_flush(z,r); 271 272 case DISTEXT: j = get; 274 275 while(k<(j)){ 276 if(n!=0)r=Z_OK; 277 else{ 278 279 s.bitb=b;s.bitk=k; 280 z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p; 281 s.write=q; 282 return s.inflate_flush(z,r); 283 } 284 n--; b|=(z.next_in[p++]&0xff)<<k; 285 k+=8; 286 } 287 288 dist += (b & inflate_mask[j]); 289 290 b>>=j; 291 k-=j; 292 293 mode = COPY; 294 case COPY: f = q - dist; 296 while(f < 0){ f += s.end; } 299 while (len!=0){ 300 301 if(m==0){ 302 if(q==s.end&&s.read!=0){q=0;m=q<s.read?s.read-q-1:s.end-q;} 303 if(m==0){ 304 s.write=q; r=s.inflate_flush(z,r); 305 q=s.write;m=q<s.read?s.read-q-1:s.end-q; 306 307 if(q==s.end&&s.read!=0){q=0;m=q<s.read?s.read-q-1:s.end-q;} 308 309 if(m==0){ 310 s.bitb=b;s.bitk=k; 311 z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p; 312 s.write=q; 313 return s.inflate_flush(z,r); 314 } 315 } 316 } 317 318 s.window[q++]=s.window[f++]; m--; 319 320 if (f == s.end) 321 f = 0; 322 len--; 323 } 324 mode = START; 325 break; 326 case LIT: if(m==0){ 328 if(q==s.end&&s.read!=0){q=0;m=q<s.read?s.read-q-1:s.end-q;} 329 if(m==0){ 330 s.write=q; r=s.inflate_flush(z,r); 331 q=s.write;m=q<s.read?s.read-q-1:s.end-q; 332 333 if(q==s.end&&s.read!=0){q=0;m=q<s.read?s.read-q-1:s.end-q;} 334 if(m==0){ 335 s.bitb=b;s.bitk=k; 336 z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p; 337 s.write=q; 338 return s.inflate_flush(z,r); 339 } 340 } 341 } 342 r=Z_OK; 343 344 s.window[q++]=(byte)lit; m--; 345 346 mode = START; 347 break; 348 case WASH: if (k > 7){ k -= 8; 351 n++; 352 p--; } 354 355 s.write=q; r=s.inflate_flush(z,r); 356 q=s.write;m=q<s.read?s.read-q-1:s.end-q; 357 358 if (s.read != s.write){ 359 s.bitb=b;s.bitk=k; 360 z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p; 361 s.write=q; 362 return s.inflate_flush(z,r); 363 } 364 mode = END; 365 case END: 366 r = Z_STREAM_END; 367 s.bitb=b;s.bitk=k; 368 z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p; 369 s.write=q; 370 return s.inflate_flush(z,r); 371 372 case BADCODE: 374 r = Z_DATA_ERROR; 375 376 s.bitb=b;s.bitk=k; 377 z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p; 378 s.write=q; 379 return s.inflate_flush(z,r); 380 381 default: 382 r = Z_STREAM_ERROR; 383 384 s.bitb=b;s.bitk=k; 385 z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p; 386 s.write=q; 387 return s.inflate_flush(z,r); 388 } 389 } 390 } 391 392 void free(ZStream z){ 393 } 395 396 401 int inflate_fast(int bl, int bd, 402 int[] tl, int tl_index, 403 int[] td, int td_index, 404 InfBlocks s, ZStream z){ 405 int t; int[] tp; int tp_index; int e; int b; int k; int p; int n; int q; int m; int ml; int md; int c; int d; int r; 421 int tp_index_t_3; 423 p=z.next_in_index;n=z.avail_in;b=s.bitb;k=s.bitk; 425 q=s.write;m=q<s.read?s.read-q-1:s.end-q; 426 427 ml = inflate_mask[bl]; 429 md = inflate_mask[bd]; 430 431 do { while(k<(20)){ n--; 436 b|=(z.next_in[p++]&0xff)<<k;k+=8; 437 } 438 439 t= b&ml; 440 tp=tl; 441 tp_index=tl_index; 442 tp_index_t_3=(tp_index+t)*3; 443 if ((e = tp[tp_index_t_3]) == 0){ 444 b>>=(tp[tp_index_t_3+1]); k-=(tp[tp_index_t_3+1]); 445 446 s.window[q++] = (byte)tp[tp_index_t_3+2]; 447 m--; 448 continue; 449 } 450 do { 451 452 b>>=(tp[tp_index_t_3+1]); k-=(tp[tp_index_t_3+1]); 453 454 if((e&16)!=0){ 455 e &= 15; 456 c = tp[tp_index_t_3+2] + ((int)b & inflate_mask[e]); 457 458 b>>=e; k-=e; 459 460 while(k<(15)){ n--; 463 b|=(z.next_in[p++]&0xff)<<k;k+=8; 464 } 465 466 t= b&md; 467 tp=td; 468 tp_index=td_index; 469 tp_index_t_3=(tp_index+t)*3; 470 e = tp[tp_index_t_3]; 471 472 do { 473 474 b>>=(tp[tp_index_t_3+1]); k-=(tp[tp_index_t_3+1]); 475 476 if((e&16)!=0){ 477 e &= 15; 479 while(k<(e)){ n--; 481 b|=(z.next_in[p++]&0xff)<<k;k+=8; 482 } 483 484 d = tp[tp_index_t_3+2] + (b&inflate_mask[e]); 485 486 b>>=(e); k-=(e); 487 488 m -= c; 490 if (q >= d){ r=q-d; 493 if(q-r>0 && 2>(q-r)){ 494 s.window[q++]=s.window[r++]; s.window[q++]=s.window[r++]; c-=2; 497 } 498 else{ 499 System.arraycopy(s.window, r, s.window, q, 2); 500 q+=2; r+=2; c-=2; 501 } 502 } 503 else{ r=q-d; 505 do{ 506 r+=s.end; }while(r<0); e=s.end-r; 509 if(c>e){ c-=e; if(q-r>0 && e>(q-r)){ 512 do{s.window[q++] = s.window[r++];} 513 while(--e!=0); 514 } 515 else{ 516 System.arraycopy(s.window, r, s.window, q, e); 517 q+=e; r+=e; e=0; 518 } 519 r = 0; } 521 522 } 523 524 if(q-r>0 && c>(q-r)){ 526 do{s.window[q++] = s.window[r++];} 527 while(--c!=0); 528 } 529 else{ 530 System.arraycopy(s.window, r, s.window, q, c); 531 q+=c; r+=c; c=0; 532 } 533 break; 534 } 535 else if((e&64)==0){ 536 t+=tp[tp_index_t_3+2]; 537 t+=(b&inflate_mask[e]); 538 tp_index_t_3=(tp_index+t)*3; 539 e=tp[tp_index_t_3]; 540 } 541 else{ 542 z.msg = "invalid distance code"; 543 544 c=z.avail_in-n;c=(k>>3)<c?k>>3:c;n+=c;p-=c;k-=c<<3; 545 546 s.bitb=b;s.bitk=k; 547 z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p; 548 s.write=q; 549 550 return Z_DATA_ERROR; 551 } 552 } 553 while(true); 554 break; 555 } 556 557 if((e&64)==0){ 558 t+=tp[tp_index_t_3+2]; 559 t+=(b&inflate_mask[e]); 560 tp_index_t_3=(tp_index+t)*3; 561 if((e=tp[tp_index_t_3])==0){ 562 563 b>>=(tp[tp_index_t_3+1]); k-=(tp[tp_index_t_3+1]); 564 565 s.window[q++]=(byte)tp[tp_index_t_3+2]; 566 m--; 567 break; 568 } 569 } 570 else if((e&32)!=0){ 571 572 c=z.avail_in-n;c=(k>>3)<c?k>>3:c;n+=c;p-=c;k-=c<<3; 573 574 s.bitb=b;s.bitk=k; 575 z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p; 576 s.write=q; 577 578 return Z_STREAM_END; 579 } 580 else{ 581 z.msg="invalid literal/length code"; 582 583 c=z.avail_in-n;c=(k>>3)<c?k>>3:c;n+=c;p-=c;k-=c<<3; 584 585 s.bitb=b;s.bitk=k; 586 z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p; 587 s.write=q; 588 589 return Z_DATA_ERROR; 590 } 591 } 592 while(true); 593 } 594 while(m>=258 && n>= 10); 595 596 c=z.avail_in-n;c=(k>>3)<c?k>>3:c;n+=c;p-=c;k-=c<<3; 598 599 s.bitb=b;s.bitk=k; 600 z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p; 601 s.write=q; 602 603 return Z_OK; 604 } 605 } 606 | Popular Tags |