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;
37
final class InfBlocks{
38
static final private int MANY=1440;
40
// And'ing with mask[n] masks the lower n bits
41
static final private int[] inflate_mask = {
42
0x00000000, 0x00000001, 0x00000003, 0x00000007, 0x0000000f,
43
0x0000001f, 0x0000003f, 0x0000007f, 0x000000ff, 0x000001ff,
44
0x000003ff, 0x000007ff, 0x00000fff, 0x00001fff, 0x00003fff,
45
0x00007fff, 0x0000ffff
48
// Table for deflate from PKZIP's appnote.txt.
49
static final int[] border = { // Order of the bit length code lengths
50
16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15
53
static final private int Z_OK=0;
54
static final private int Z_STREAM_END=1;
55
static final private int Z_NEED_DICT=2;
56
static final private int Z_ERRNO=-1;
57
static final private int Z_STREAM_ERROR=-2;
58
static final private int Z_DATA_ERROR=-3;
59
static final private int Z_MEM_ERROR=-4;
60
static final private int Z_BUF_ERROR=-5;
61
static final private int Z_VERSION_ERROR=-6;
63
static final private int TYPE=0; // get type bits (3, including end bit)
64
static final private int LENS=1; // get lengths for stored
65
static final private int STORED=2;// processing stored block
66
static final private int TABLE=3; // get table lengths
67
static final private int BTREE=4; // get bit lengths tree for a dynamic block
68
static final private int DTREE=5; // get length, distance trees for a dynamic block
69
static final private int CODES=6; // processing fixed or dynamic block
70
static final private int DRY=7; // output remaining window bytes
71
static final private int DONE=8; // finished last block, done
72
static final private int BAD=9; // ot a data error--stuck here
74
int mode; // current inflate_block mode
76
int left; // if STORED, bytes left to copy
78
int table; // table lengths (14 bits)
79
int index; // index into blens (or border)
80
int[] blens; // bit lengths of codes
81
int[] bb=new int[1]; // bit length tree depth
82
int[] tb=new int[1]; // bit length decoding tree
84
InfCodes codes=new InfCodes(); // if CODES, current state
86
int last; // true if this block is the last block
88
// mode independent information
89
int bitk; // bits in bit buffer
90
int bitb; // bit buffer
91
int[] hufts; // single malloc for tree space
92
byte[] window; // sliding window
93
int end; // one byte after sliding window
94
int read; // window read pointer
95
int write; // window write pointer
96
Object checkfn; // check function
97
long check; // check on output
99
InfTree inftree=new InfTree();
101
InfBlocks(ZStream z, Object checkfn, int w){
102
hufts=new int[MANY*3];
105
this.checkfn = checkfn;
110
void reset(ZStream z, long[] c){
111
if(c!=null) c[0]=check;
112
if(mode==BTREE || mode==DTREE){
123
z.adler=check=z._adler.adler32(0L, null, 0, 0);
126
int proc(ZStream z, int r){
127
int t; // temporary storage
129
int k; // bits in bit buffer
130
int p; // input data pointer
131
int n; // bytes available there
132
int q; // output window write pointer
133
int m; // bytes to end of window or read pointer
135
// copy input/output information to locals (UPDATE macro restores)
136
{p=z.next_in_index;n=z.avail_in;b=bitb;k=bitk;}
137
{q=write;m=(int)(q<read?read-q-1:end-q);}
139
// process input based on current state
151
z.total_in+=p-z.next_in_index;z.next_in_index=p;
153
return inflate_flush(z,r);
156
b|=(z.next_in[p++]&0xff)<<k;
165
t = k & 7; // go to byte boundary
168
mode = LENS; // get length of stored block
174
int[][] tl=new int[1][];
175
int[][] td=new int[1][];
177
InfTree.inflate_trees_fixed(bl, bd, tl, td, z);
178
codes.init(bl[0], bd[0], tl[0], 0, td[0], 0, z);
195
z.msg = "invalid block type";
199
z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;
201
return inflate_flush(z,r);
213
z.total_in+=p-z.next_in_index;z.next_in_index=p;
215
return inflate_flush(z,r);
218
b|=(z.next_in[p++]&0xff)<<k;
222
if ((((~b) >>> 16) & 0xffff) != (b & 0xffff)){
224
z.msg = "invalid stored block lengths";
228
z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;
230
return inflate_flush(z,r);
233
b = k = 0; // dump bits
234
mode = left!=0 ? STORED : (last!=0 ? DRY : TYPE);
239
z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;
241
return inflate_flush(z,r);
246
q=0; m=(int)(q<read?read-q-1:end-q);
250
r=inflate_flush(z,r);
251
q=write;m=(int)(q<read?read-q-1:end-q);
253
q=0; m=(int)(q<read?read-q-1:end-q);
257
z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;
259
return inflate_flush(z,r);
268
System.arraycopy(z.next_in, p, window, q, t);
271
if ((left -= t) != 0)
273
mode = last!=0 ? DRY : TYPE;
284
z.total_in+=p-z.next_in_index;z.next_in_index=p;
286
return inflate_flush(z,r);
289
b|=(z.next_in[p++]&0xff)<<k;
293
table = t = (b & 0x3fff);
294
if ((t & 0x1f) > 29 || ((t >> 5) & 0x1f) > 29)
297
z.msg = "too many length or distance symbols";
301
z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;
303
return inflate_flush(z,r);
305
t = 258 + (t & 0x1f) + ((t >> 5) & 0x1f);
306
if(blens==null || blens.length<t){
310
for(int i=0; i<t; i++){blens[i]=0;}
318
while (index < 4 + (table >>> 10)){
326
z.total_in+=p-z.next_in_index;z.next_in_index=p;
328
return inflate_flush(z,r);
331
b|=(z.next_in[p++]&0xff)<<k;
335
blens[border[index++]] = b&7;
341
blens[border[index++]] = 0;
345
t = inftree.inflate_trees_bits(blens, bb, tb, hufts, z);
348
if (r == Z_DATA_ERROR){
354
z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;
356
return inflate_flush(z,r);
364
if(!(index < 258 + (t & 0x1f) + ((t >> 5) & 0x1f))){
380
z.total_in+=p-z.next_in_index;z.next_in_index=p;
382
return inflate_flush(z,r);
385
b|=(z.next_in[p++]&0xff)<<k;
390
//System.err.println("null...");
393
t=hufts[(tb[0]+(b&inflate_mask[t]))*3+1];
394
c=hufts[(tb[0]+(b&inflate_mask[t]))*3+2];
400
else { // c == 16..18
401
i = c == 18 ? 7 : c - 14;
402
j = c == 18 ? 11 : 3;
411
z.total_in+=p-z.next_in_index;z.next_in_index=p;
413
return inflate_flush(z,r);
416
b|=(z.next_in[p++]&0xff)<<k;
422
j += (b & inflate_mask[i]);
428
if (i + j > 258 + (t & 0x1f) + ((t >> 5) & 0x1f) ||
432
z.msg = "invalid bit length repeat";
436
z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;
438
return inflate_flush(z,r);
441
c = c == 16 ? blens[i-1] : 0;
456
bl[0] = 9; // must be <= 9 for lookahead assumptions
457
bd[0] = 6; // must be <= 9 for lookahead assumptions
460
t = inftree.inflate_trees_dynamic(257 + (t & 0x1f),
461
1 + ((t >> 5) & 0x1f),
462
blens, bl, bd, tl, td, hufts, z);
465
if (t == Z_DATA_ERROR){
472
z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;
474
return inflate_flush(z,r);
476
codes.init(bl[0], bd[0], hufts, tl[0], hufts, td[0], z);
481
z.avail_in=n; z.total_in+=p-z.next_in_index;z.next_in_index=p;
484
if ((r = codes.proc(this, z, r)) != Z_STREAM_END){
485
return inflate_flush(z, r);
490
p=z.next_in_index; n=z.avail_in;b=bitb;k=bitk;
491
q=write;m=(int)(q<read?read-q-1:end-q);
500
r=inflate_flush(z, r);
501
q=write; m=(int)(q<read?read-q-1:end-q);
504
z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;
506
return inflate_flush(z, r);
513
z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;
515
return inflate_flush(z, r);
520
z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;
522
return inflate_flush(z, r);
528
z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;
530
return inflate_flush(z, r);
535
void free(ZStream z){
542
void set_dictionary(byte[] d, int start, int n){
543
System.arraycopy(d, start, window, 0, n);
547
// Returns true if inflate is currently at the end of a block generated
548
// by Z_SYNC_FLUSH or Z_FULL_FLUSH.
550
return mode == LENS ? 1 : 0;
553
// copy as much as possible from the sliding window to the output area
554
int inflate_flush(ZStream z, int r){
559
// local copies of source and destination pointers
560
p = z.next_out_index;
563
// compute number of bytes to copy as far as end of window
564
n = (int)((q <= write ? write : end) - q);
565
if (n > z.avail_out) n = z.avail_out;
566
if (n!=0 && r == Z_BUF_ERROR) r = Z_OK;
572
// update check information
574
z.adler=check=z._adler.adler32(check, window, q, n);
576
// copy as far as end of window
577
System.arraycopy(window, q, z.next_out, p, n);
581
// see if more to copy at beginning of window
588
// compute bytes to copy
590
if (n > z.avail_out) n = z.avail_out;
591
if (n!=0 && r == Z_BUF_ERROR) r = Z_OK;
597
// update check information
599
z.adler=check=z._adler.adler32(check, window, q, n);
602
System.arraycopy(window, q, z.next_out, p, n);
608
z.next_out_index = p;