1
/* -*-mode:java; c-basic-offset:2; -*- */
2
/* JZlib -- zlib in pure Java
4
* Copyright (C) 2000 ymnk, JCraft, Inc.
6
* This program is free software; you can redistribute it and/or
7
* modify it under the terms of the GNU Library General Public License
8
* as published by the Free Software Foundation; either version 2 of
9
* the License, or (at your option) any later version.
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU Library General Public License for more details.
16
* You should have received a copy of the GNU Library General Public
17
* License along with this program; if not, write to the Free Software
18
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21
* This program is based on zlib-1.1.3, so all credit should go authors
22
* Jean-loup Gailly(jloup@gzip.org) and Mark Adler(madler@alumni.caltech.edu)
23
* and contributors of zlib.
26
package com.jcraft.jzlib;
30
static final private int[] inflate_mask = {
31
0x00000000, 0x00000001, 0x00000003, 0x00000007, 0x0000000f,
32
0x0000001f, 0x0000003f, 0x0000007f, 0x000000ff, 0x000001ff,
33
0x000003ff, 0x000007ff, 0x00000fff, 0x00001fff, 0x00003fff,
34
0x00007fff, 0x0000ffff
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;
47
// waiting for "i:"=input,
50
static final private int START=0; // x: set up for LEN
51
static final private int LEN=1; // i: get length/literal/eob next
52
static final private int LENEXT=2; // i: getting length extra (have base)
53
static final private int DIST=3; // i: get distance next
54
static final private int DISTEXT=4;// i: getting distance extra
55
static final private int COPY=5; // o: copying bytes in window, waiting for space
56
static final private int LIT=6; // o: got literal, waiting for output space
57
static final private int WASH=7; // o: got eob, possibly still output waiting
58
static final private int END=8; // x: got eob and all data flushed
59
static final private int BADCODE=9;// x: got error
61
int mode; // current inflate_codes mode
63
// mode dependent information
66
int[] tree; // pointer into tree
68
int need; // bits needed
72
// if EXT or COPY, where and how much
73
int get; // bits to get for extra
74
int dist; // distance back to copy from
76
byte lbits; // ltree bits decoded per branch
77
byte dbits; // dtree bits decoder per branch
78
int[] ltree; // literal/length/eob tree
79
int ltree_index; // literal/length/eob tree
80
int[] dtree; // distance tree
81
int dtree_index; // distance tree
83
InfCodes(int bl, int bd,
84
int[] tl, int tl_index,
85
int[] td, int td_index, ZStream z){
95
InfCodes(int bl, int bd, int[] tl, int[] td, ZStream z){
105
int proc(InfBlocks s, ZStream z, int r){
106
int j; // temporary storage
107
int[] t; // temporary pointer
108
int tindex; // temporary pointer
109
int e; // extra bits or operation
110
int b=0; // bit buffer
111
int k=0; // bits in bit buffer
112
int p=0; // input data pointer
113
int n; // bytes available there
114
int q; // output window write pointer
115
int m; // bytes to end of window or read pointer
116
int f; // pointer to copy strings from
118
// copy input/output information to locals (UPDATE macro restores)
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;
122
// process input and output based on current state
125
// waiting for "i:"=input, "o:"=output, "x:"=nothing
126
case START: // x: set up for LEN
127
if (m >= 258 && n >= 10){
130
z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;
132
r = inflate_fast(lbits, dbits,
137
p=z.next_in_index;n=z.avail_in;b=s.bitb;k=s.bitk;
138
q=s.write;m=q<s.read?s.read-q-1:s.end-q;
141
mode = r == Z_STREAM_END ? WASH : BADCODE;
147
tree_index=ltree_index;
150
case LEN: // i: get length/literal/eob next
158
z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;
160
return s.inflate_flush(z,r);
163
b|=(z.next_in[p++]&0xff)<<k;
167
tindex=(tree_index+(b&inflate_mask[j]))*3;
169
b>>>=(tree[tindex+1]);
174
if(e == 0){ // literal
175
lit = tree[tindex+2];
179
if((e & 16)!=0 ){ // length
181
len = tree[tindex+2];
185
if ((e & 64) == 0){ // next table
187
tree_index = tindex/3+tree[tindex+2];
190
if ((e & 32)!=0){ // end of block
194
mode = BADCODE; // invalid code
195
z.msg = "invalid literal/length code";
199
z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;
201
return s.inflate_flush(z,r);
203
case LENEXT: // i: getting length extra (have base)
211
z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;
213
return s.inflate_flush(z,r);
215
n--; b|=(z.next_in[p++]&0xff)<<k;
219
len += (b & inflate_mask[j]);
226
tree_index=dtree_index;
228
case DIST: // i: get distance next
236
z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;
238
return s.inflate_flush(z,r);
240
n--; b|=(z.next_in[p++]&0xff)<<k;
244
tindex=(tree_index+(b & inflate_mask[j]))*3;
250
if((e & 16)!=0){ // distance
252
dist = tree[tindex+2];
256
if ((e & 64) == 0){ // next table
258
tree_index = tindex/3 + tree[tindex+2];
261
mode = BADCODE; // invalid code
262
z.msg = "invalid distance code";
266
z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;
268
return s.inflate_flush(z,r);
270
case DISTEXT: // i: getting distance extra
278
z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;
280
return s.inflate_flush(z,r);
282
n--; b|=(z.next_in[p++]&0xff)<<k;
286
dist += (b & inflate_mask[j]);
292
case COPY: // o: copying bytes in window, waiting for space
294
while(f < 0){ // modulo window size-"while" instead
295
f += s.end; // of "if" handles invalid distances
300
if(q==s.end&&s.read!=0){q=0;m=q<s.read?s.read-q-1:s.end-q;}
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;
305
if(q==s.end&&s.read!=0){q=0;m=q<s.read?s.read-q-1:s.end-q;}
309
z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;
311
return s.inflate_flush(z,r);
316
s.window[q++]=s.window[f++]; m--;
324
case LIT: // o: got literal, waiting for output space
326
if(q==s.end&&s.read!=0){q=0;m=q<s.read?s.read-q-1:s.end-q;}
328
s.write=q; r=s.inflate_flush(z,r);
329
q=s.write;m=q<s.read?s.read-q-1:s.end-q;
331
if(q==s.end&&s.read!=0){q=0;m=q<s.read?s.read-q-1:s.end-q;}
334
z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;
336
return s.inflate_flush(z,r);
342
s.window[q++]=(byte)lit; m--;
346
case WASH: // o: got eob, possibly more output
347
if (k > 7){ // return unused byte, if any
350
p--; // can always return one
353
s.write=q; r=s.inflate_flush(z,r);
354
q=s.write;m=q<s.read?s.read-q-1:s.end-q;
356
if (s.read != s.write){
358
z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;
360
return s.inflate_flush(z,r);
366
z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;
368
return s.inflate_flush(z,r);
370
case BADCODE: // x: got error
375
z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;
377
return s.inflate_flush(z,r);
383
z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;
385
return s.inflate_flush(z,r);
390
void free(ZStream z){
394
// Called with number of bytes left to write in window at least 258
395
// (the maximum string length) and number of input bytes available
396
// at least ten. The ten bytes are six bytes for the longest length/
397
// distance pair plus four bytes for overloading the bit buffer.
399
int inflate_fast(int bl, int bd,
400
int[] tl, int tl_index,
401
int[] td, int td_index,
402
InfBlocks s, ZStream z){
403
int t; // temporary pointer
404
int[] tp; // temporary pointer
405
int tp_index; // temporary pointer
406
int e; // extra bits or operation
408
int k; // bits in bit buffer
409
int p; // input data pointer
410
int n; // bytes available there
411
int q; // output window write pointer
412
int m; // bytes to end of window or read pointer
413
int ml; // mask for literal/length tree
414
int md; // mask for distance tree
415
int c; // bytes to copy
416
int d; // distance back to copy from
417
int r; // copy source pointer
419
// load input, output, bit values
420
p=z.next_in_index;n=z.avail_in;b=s.bitb;k=s.bitk;
421
q=s.write;m=q<s.read?s.read-q-1:s.end-q;
424
ml = inflate_mask[bl];
425
md = inflate_mask[bd];
427
// do until not enough input or output space for fast loop
428
do { // assume called with m >= 258 && n >= 10
429
// get literal/length code
430
while(k<(20)){ // max bits for literal/length code
432
b|=(z.next_in[p++]&0xff)<<k;k+=8;
438
if ((e = tp[(tp_index+t)*3]) == 0){
439
b>>=(tp[(tp_index+t)*3+1]); k-=(tp[(tp_index+t)*3+1]);
441
s.window[q++] = (byte)tp[(tp_index+t)*3+2];
447
b>>=(tp[(tp_index+t)*3+1]); k-=(tp[(tp_index+t)*3+1]);
451
c = tp[(tp_index+t)*3+2] + ((int)b & inflate_mask[e]);
455
// decode distance base of block to copy
456
while(k<(15)){ // max bits for distance code
458
b|=(z.next_in[p++]&0xff)<<k;k+=8;
464
e = tp[(tp_index+t)*3];
468
b>>=(tp[(tp_index+t)*3+1]); k-=(tp[(tp_index+t)*3+1]);
471
// get extra bits to add to distance base
473
while(k<(e)){ // get extra bits (up to 13)
475
b|=(z.next_in[p++]&0xff)<<k;k+=8;
478
d = tp[(tp_index+t)*3+2] + (b&inflate_mask[e]);
484
if (q >= d){ // offset before dest
487
if(q-r>0 && 2>(q-r)){
488
s.window[q++]=s.window[r++]; c--; // minimum count is three,
489
s.window[q++]=s.window[r++]; c--; // so unroll loop a little
492
System.arraycopy(s.window, r, s.window, q, 2);
496
else{ // else offset after destination
499
r+=s.end; // force pointer in window
500
}while(r<0); // covers invalid distances
502
if(c>e){ // if source crosses,
503
c-=e; // wrapped copy
504
if(q-r>0 && e>(q-r)){
505
do{s.window[q++] = s.window[r++];}
509
System.arraycopy(s.window, r, s.window, q, e);
512
r = 0; // copy rest from start of window
517
// copy all or what's left
518
if(q-r>0 && c>(q-r)){
519
do{s.window[q++] = s.window[r++];}
523
System.arraycopy(s.window, r, s.window, q, c);
529
t+=tp[(tp_index+t)*3+2];
530
t+=(b&inflate_mask[e]);
531
e=tp[(tp_index+t)*3];
534
z.msg = "invalid distance code";
536
c=z.avail_in-n;c=(k>>3)<c?k>>3:c;n+=c;p-=c;k-=c<<3;
539
z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;
550
t+=tp[(tp_index+t)*3+2];
551
t+=(b&inflate_mask[e]);
552
if((e=tp[(tp_index+t)*3])==0){
554
b>>=(tp[(tp_index+t)*3+1]); k-=(tp[(tp_index+t)*3+1]);
556
s.window[q++]=(byte)tp[(tp_index+t)*3+2];
563
c=z.avail_in-n;c=(k>>3)<c?k>>3:c;n+=c;p-=c;k-=c<<3;
566
z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;
572
z.msg="invalid literal/length code";
574
c=z.avail_in-n;c=(k>>3)<c?k>>3:c;n+=c;p-=c;k-=c<<3;
577
z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;
585
while(m>=258 && n>= 10);
587
// not enough input or output--restore pointers and return
588
c=z.avail_in-n;c=(k>>3)<c?k>>3:c;n+=c;p-=c;k-=c<<3;
591
z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;