1 /* -*-mode:java; c-basic-offset:2; -*- */
3 Copyright (c) 2000,2001,2002,2003 ymnk, JCraft,Inc. All rights reserved.
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
8 1. Redistributions of source code must retain the above copyright notice,
9 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.
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.
18 THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
19 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
21 INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
22 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
24 OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
27 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 * This program is based on zlib-1.1.3, so all credit should go authors
31 * Jean-loup Gailly(jloup@gzip.org) and Mark Adler(madler@alumni.caltech.edu)
32 * and contributors of zlib.
35 package com.jcraft.jzlib;
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
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;
56 // waiting for "i:"=input,
59 static final private int START=0; // x: set up for LEN
60 static final private int LEN=1; // i: get length/literal/eob next
61 static final private int LENEXT=2; // i: getting length extra (have base)
62 static final private int DIST=3; // i: get distance next
63 static final private int DISTEXT=4;// i: getting distance extra
64 static final private int COPY=5; // o: copying bytes in window, waiting for space
65 static final private int LIT=6; // o: got literal, waiting for output space
66 static final private int WASH=7; // o: got eob, possibly still output waiting
67 static final private int END=8; // x: got eob and all data flushed
68 static final private int BADCODE=9;// x: got error
70 int mode; // current inflate_codes mode
72 // mode dependent information
75 int[] tree; // pointer into tree
77 int need; // bits needed
81 // if EXT or COPY, where and how much
82 int get; // bits to get for extra
83 int dist; // distance back to copy from
85 byte lbits; // ltree bits decoded per branch
86 byte dbits; // dtree bits decoder per branch
87 int[] ltree; // literal/length/eob tree
88 int ltree_index; // literal/length/eob tree
89 int[] dtree; // distance tree
90 int dtree_index; // distance tree
94 void init(int bl, int bd,
95 int[] tl, int tl_index,
96 int[] td, int td_index, ZStream z){
101 ltree_index=tl_index;
103 dtree_index=td_index;
107 int proc(InfBlocks s, ZStream z, int r){
108 int j; // temporary storage
109 int[] t; // temporary pointer
110 int tindex; // temporary pointer
111 int e; // extra bits or operation
112 int b=0; // bit buffer
113 int k=0; // bits in bit buffer
114 int p=0; // input data pointer
115 int n; // bytes available there
116 int q; // output window write pointer
117 int m; // bytes to end of window or read pointer
118 int f; // pointer to copy strings from
120 // copy input/output information to locals (UPDATE macro restores)
121 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;
124 // process input and output based on current state
127 // waiting for "i:"=input, "o:"=output, "x:"=nothing
128 case START: // x: set up for LEN
129 if (m >= 258 && n >= 10){
132 z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;
134 r = inflate_fast(lbits, dbits,
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;
143 mode = r == Z_STREAM_END ? WASH : BADCODE;
149 tree_index=ltree_index;
152 case LEN: // i: get length/literal/eob next
160 z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;
162 return s.inflate_flush(z,r);
165 b|=(z.next_in[p++]&0xff)<<k;
169 tindex=(tree_index+(b&inflate_mask[j]))*3;
171 b>>>=(tree[tindex+1]);
176 if(e == 0){ // literal
177 lit = tree[tindex+2];
181 if((e & 16)!=0 ){ // length
183 len = tree[tindex+2];
187 if ((e & 64) == 0){ // next table
189 tree_index = tindex/3+tree[tindex+2];
192 if ((e & 32)!=0){ // end of block
196 mode = BADCODE; // invalid code
197 z.msg = "invalid literal/length code";
201 z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;
203 return s.inflate_flush(z,r);
205 case LENEXT: // i: getting length extra (have base)
213 z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;
215 return s.inflate_flush(z,r);
217 n--; b|=(z.next_in[p++]&0xff)<<k;
221 len += (b & inflate_mask[j]);
228 tree_index=dtree_index;
230 case DIST: // i: get distance next
238 z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;
240 return s.inflate_flush(z,r);
242 n--; b|=(z.next_in[p++]&0xff)<<k;
246 tindex=(tree_index+(b & inflate_mask[j]))*3;
252 if((e & 16)!=0){ // distance
254 dist = tree[tindex+2];
258 if ((e & 64) == 0){ // next table
260 tree_index = tindex/3 + tree[tindex+2];
263 mode = BADCODE; // invalid code
264 z.msg = "invalid distance code";
268 z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;
270 return s.inflate_flush(z,r);
272 case DISTEXT: // i: getting distance extra
280 z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;
282 return s.inflate_flush(z,r);
284 n--; b|=(z.next_in[p++]&0xff)<<k;
288 dist += (b & inflate_mask[j]);
294 case COPY: // o: copying bytes in window, waiting for space
296 while(f < 0){ // modulo window size-"while" instead
297 f += s.end; // of "if" handles invalid distances
302 if(q==s.end&&s.read!=0){q=0;m=q<s.read?s.read-q-1:s.end-q;}
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;
307 if(q==s.end&&s.read!=0){q=0;m=q<s.read?s.read-q-1:s.end-q;}
311 z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;
313 return s.inflate_flush(z,r);
318 s.window[q++]=s.window[f++]; m--;
326 case LIT: // o: got literal, waiting for output space
328 if(q==s.end&&s.read!=0){q=0;m=q<s.read?s.read-q-1:s.end-q;}
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;
333 if(q==s.end&&s.read!=0){q=0;m=q<s.read?s.read-q-1:s.end-q;}
336 z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;
338 return s.inflate_flush(z,r);
344 s.window[q++]=(byte)lit; m--;
348 case WASH: // o: got eob, possibly more output
349 if (k > 7){ // return unused byte, if any
352 p--; // can always return one
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;
358 if (s.read != s.write){
360 z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;
362 return s.inflate_flush(z,r);
368 z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;
370 return s.inflate_flush(z,r);
372 case BADCODE: // x: got error
377 z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;
379 return s.inflate_flush(z,r);
385 z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;
387 return s.inflate_flush(z,r);
392 void free(ZStream z){
396 // Called with number of bytes left to write in window at least 258
397 // (the maximum string length) and number of input bytes available
398 // at least ten. The ten bytes are six bytes for the longest length/
399 // distance pair plus four bytes for overloading the bit buffer.
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; // temporary pointer
406 int[] tp; // temporary pointer
407 int tp_index; // temporary pointer
408 int e; // extra bits or operation
410 int k; // bits in bit buffer
411 int p; // input data pointer
412 int n; // bytes available there
413 int q; // output window write pointer
414 int m; // bytes to end of window or read pointer
415 int ml; // mask for literal/length tree
416 int md; // mask for distance tree
417 int c; // bytes to copy
418 int d; // distance back to copy from
419 int r; // copy source pointer
421 int tp_index_t_3; // (tp_index+t)*3
423 // load input, output, bit values
424 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;
428 ml = inflate_mask[bl];
429 md = inflate_mask[bd];
431 // do until not enough input or output space for fast loop
432 do { // assume called with m >= 258 && n >= 10
433 // get literal/length code
434 while(k<(20)){ // max bits for literal/length code
436 b|=(z.next_in[p++]&0xff)<<k;k+=8;
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]);
446 s.window[q++] = (byte)tp[tp_index_t_3+2];
452 b>>=(tp[tp_index_t_3+1]); k-=(tp[tp_index_t_3+1]);
456 c = tp[tp_index_t_3+2] + ((int)b & inflate_mask[e]);
460 // decode distance base of block to copy
461 while(k<(15)){ // max bits for distance code
463 b|=(z.next_in[p++]&0xff)<<k;k+=8;
469 tp_index_t_3=(tp_index+t)*3;
470 e = tp[tp_index_t_3];
474 b>>=(tp[tp_index_t_3+1]); k-=(tp[tp_index_t_3+1]);
477 // get extra bits to add to distance base
479 while(k<(e)){ // get extra bits (up to 13)
481 b|=(z.next_in[p++]&0xff)<<k;k+=8;
484 d = tp[tp_index_t_3+2] + (b&inflate_mask[e]);
490 if (q >= d){ // offset before dest
493 if(q-r>0 && 2>(q-r)){
494 s.window[q++]=s.window[r++]; // minimum count is three,
495 s.window[q++]=s.window[r++]; // so unroll loop a little
499 System.arraycopy(s.window, r, s.window, q, 2);
503 else{ // else offset after destination
506 r+=s.end; // force pointer in window
507 }while(r<0); // covers invalid distances
509 if(c>e){ // if source crosses,
510 c-=e; // wrapped copy
511 if(q-r>0 && e>(q-r)){
512 do{s.window[q++] = s.window[r++];}
516 System.arraycopy(s.window, r, s.window, q, e);
519 r = 0; // copy rest from start of window
524 // copy all or what's left
525 if(q-r>0 && c>(q-r)){
526 do{s.window[q++] = s.window[r++];}
530 System.arraycopy(s.window, r, s.window, q, c);
536 t+=tp[tp_index_t_3+2];
537 t+=(b&inflate_mask[e]);
538 tp_index_t_3=(tp_index+t)*3;
542 z.msg = "invalid distance code";
544 c=z.avail_in-n;c=(k>>3)<c?k>>3:c;n+=c;p-=c;k-=c<<3;
547 z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;
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){
563 b>>=(tp[tp_index_t_3+1]); k-=(tp[tp_index_t_3+1]);
565 s.window[q++]=(byte)tp[tp_index_t_3+2];
572 c=z.avail_in-n;c=(k>>3)<c?k>>3:c;n+=c;p-=c;k-=c<<3;
575 z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;
581 z.msg="invalid literal/length code";
583 c=z.avail_in-n;c=(k>>3)<c?k>>3:c;n+=c;p-=c;k-=c<<3;
586 z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;
594 while(m>=258 && n>= 10);
596 // not enough input or output--restore pointers and return
597 c=z.avail_in-n;c=(k>>3)<c?k>>3:c;n+=c;p-=c;k-=c<<3;
600 z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;