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 MAX_WBITS=15; // 32K LZ77 window
41 // preset dictionary flag in zlib header
42 static final private int PRESET_DICT=0x20;
44 static final int Z_NO_FLUSH=0;
45 static final int Z_PARTIAL_FLUSH=1;
46 static final int Z_SYNC_FLUSH=2;
47 static final int Z_FULL_FLUSH=3;
48 static final int Z_FINISH=4;
50 static final private int Z_DEFLATED=8;
52 static final private int Z_OK=0;
53 static final private int Z_STREAM_END=1;
54 static final private int Z_NEED_DICT=2;
55 static final private int Z_ERRNO=-1;
56 static final private int Z_STREAM_ERROR=-2;
57 static final private int Z_DATA_ERROR=-3;
58 static final private int Z_MEM_ERROR=-4;
59 static final private int Z_BUF_ERROR=-5;
60 static final private int Z_VERSION_ERROR=-6;
62 static final private int METHOD=0; // waiting for method byte
63 static final private int FLAG=1; // waiting for flag byte
64 static final private int DICT4=2; // four dictionary check bytes to go
65 static final private int DICT3=3; // three dictionary check bytes to go
66 static final private int DICT2=4; // two dictionary check bytes to go
67 static final private int DICT1=5; // one dictionary check byte to go
68 static final private int DICT0=6; // waiting for inflateSetDictionary
69 static final private int BLOCKS=7; // decompressing blocks
70 static final private int CHECK4=8; // four check bytes to go
71 static final private int CHECK3=9; // three check bytes to go
72 static final private int CHECK2=10; // two check bytes to go
73 static final private int CHECK1=11; // one check byte to go
74 static final private int DONE=12; // finished check, done
75 static final private int BAD=13; // got an error--stay here
77 int mode; // current inflate mode
79 // mode dependent information
80 int method; // if FLAGS, method byte
82 // if CHECK, check values to compare
83 long[] was=new long[1] ; // computed check value
84 long need; // stream check value
86 // if BAD, inflateSync's marker bytes count
89 // mode independent information
90 int nowrap; // flag for no wrapper
91 int wbits; // log2(window size) (8..15, defaults to 15)
93 InfBlocks blocks; // current inflate_blocks state
95 int inflateReset(ZStream z){
96 if(z == null || z.istate == null) return Z_STREAM_ERROR;
98 z.total_in = z.total_out = 0;
100 z.istate.mode = z.istate.nowrap!=0 ? BLOCKS : METHOD;
101 z.istate.blocks.reset(z, null);
105 int inflateEnd(ZStream z){
109 // ZFREE(z, z->state);
113 int inflateInit(ZStream z, int w){
117 // handle undocumented nowrap option (no zlib header or check)
127 return Z_STREAM_ERROR;
131 z.istate.blocks=new InfBlocks(z,
132 z.istate.nowrap!=0 ? null : this,
140 int inflate(ZStream z, int f){
144 if(z == null || z.istate == null || z.next_in == null)
145 return Z_STREAM_ERROR;
146 f = f == Z_FINISH ? Z_BUF_ERROR : Z_OK;
149 //System.out.println("mode: "+z.istate.mode);
150 switch (z.istate.mode){
153 if(z.avail_in==0)return r;r=f;
155 z.avail_in--; z.total_in++;
156 if(((z.istate.method = z.next_in[z.next_in_index++])&0xf)!=Z_DEFLATED){
158 z.msg="unknown compression method";
159 z.istate.marker = 5; // can't try inflateSync
162 if((z.istate.method>>4)+8>z.istate.wbits){
164 z.msg="invalid window size";
165 z.istate.marker = 5; // can't try inflateSync
171 if(z.avail_in==0)return r;r=f;
173 z.avail_in--; z.total_in++;
174 b = (z.next_in[z.next_in_index++])&0xff;
176 if((((z.istate.method << 8)+b) % 31)!=0){
178 z.msg = "incorrect header check";
179 z.istate.marker = 5; // can't try inflateSync
183 if((b&PRESET_DICT)==0){
184 z.istate.mode = BLOCKS;
187 z.istate.mode = DICT4;
190 if(z.avail_in==0)return r;r=f;
192 z.avail_in--; z.total_in++;
193 z.istate.need=((z.next_in[z.next_in_index++]&0xff)<<24)&0xff000000L;
197 if(z.avail_in==0)return r;r=f;
199 z.avail_in--; z.total_in++;
200 z.istate.need+=((z.next_in[z.next_in_index++]&0xff)<<16)&0xff0000L;
204 if(z.avail_in==0)return r;r=f;
206 z.avail_in--; z.total_in++;
207 z.istate.need+=((z.next_in[z.next_in_index++]&0xff)<<8)&0xff00L;
211 if(z.avail_in==0)return r;r=f;
213 z.avail_in--; z.total_in++;
214 z.istate.need += (z.next_in[z.next_in_index++]&0xffL);
215 z.adler = z.istate.need;
216 z.istate.mode = DICT0;
220 z.msg = "need dictionary";
221 z.istate.marker = 0; // can try inflateSync
222 return Z_STREAM_ERROR;
225 r = z.istate.blocks.proc(z, r);
226 if(r == Z_DATA_ERROR){
228 z.istate.marker = 0; // can try inflateSync
234 if(r != Z_STREAM_END){
238 z.istate.blocks.reset(z, z.istate.was);
239 if(z.istate.nowrap!=0){
243 z.istate.mode=CHECK4;
246 if(z.avail_in==0)return r;r=f;
248 z.avail_in--; z.total_in++;
249 z.istate.need=((z.next_in[z.next_in_index++]&0xff)<<24)&0xff000000L;
250 z.istate.mode=CHECK3;
253 if(z.avail_in==0)return r;r=f;
255 z.avail_in--; z.total_in++;
256 z.istate.need+=((z.next_in[z.next_in_index++]&0xff)<<16)&0xff0000L;
257 z.istate.mode = CHECK2;
260 if(z.avail_in==0)return r;r=f;
262 z.avail_in--; z.total_in++;
263 z.istate.need+=((z.next_in[z.next_in_index++]&0xff)<<8)&0xff00L;
264 z.istate.mode = CHECK1;
267 if(z.avail_in==0)return r;r=f;
269 z.avail_in--; z.total_in++;
270 z.istate.need+=(z.next_in[z.next_in_index++]&0xffL);
272 if(((int)(z.istate.was[0])) != ((int)(z.istate.need))){
274 z.msg = "incorrect data check";
275 z.istate.marker = 5; // can't try inflateSync
279 z.istate.mode = DONE;
285 return Z_STREAM_ERROR;
291 int inflateSetDictionary(ZStream z, byte[] dictionary, int dictLength){
293 int length = dictLength;
294 if(z==null || z.istate == null|| z.istate.mode != DICT0)
295 return Z_STREAM_ERROR;
297 if(z._adler.adler32(1L, dictionary, 0, dictLength)!=z.adler){
301 z.adler = z._adler.adler32(0, null, 0, 0);
303 if(length >= (1<<z.istate.wbits)){
304 length = (1<<z.istate.wbits)-1;
305 index=dictLength - length;
307 z.istate.blocks.set_dictionary(dictionary, index, length);
308 z.istate.mode = BLOCKS;
312 static private byte[] mark = {(byte)0, (byte)0, (byte)0xff, (byte)0xff};
314 int inflateSync(ZStream z){
315 int n; // number of bytes to look at
316 int p; // pointer to bytes
317 int m; // number of marker bytes found in a row
318 long r, w; // temporaries to save total_in and total_out
321 if(z == null || z.istate == null)
322 return Z_STREAM_ERROR;
323 if(z.istate.mode != BAD){
327 if((n=z.avail_in)==0)
333 while (n!=0 && m < 4){
334 if(z.next_in[p] == mark[m]){
337 else if(z.next_in[p]!=0){
347 z.total_in += p-z.next_in_index;
352 // return no joy or set up to restart on a new block
356 r=z.total_in; w=z.total_out;
358 z.total_in=r; z.total_out = w;
359 z.istate.mode = BLOCKS;
363 // Returns true if inflate is currently at the end of a block generated
364 // by Z_SYNC_FLUSH or Z_FULL_FLUSH. This function is used by one PPP
365 // implementation to provide an additional safety check. PPP uses Z_SYNC_FLUSH
366 // but removes the length bytes of the resulting empty stored block. When
367 // decompressing, PPP checks that at the end of input packet, inflate is
368 // waiting for these length bytes.
369 int inflateSyncPoint(ZStream z){
370 if(z == null || z.istate == null || z.istate.blocks == null)
371 return Z_STREAM_ERROR;
372 return z.istate.blocks.sync_point();