]>
Commit | Line | Data |
---|---|---|
0763e16d JW |
1 | /* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */ |
2 | /* | |
3 | Copyright (c) 2000,2001,2002,2003 ymnk, JCraft,Inc. All rights reserved. | |
4 | ||
5 | Redistribution and use in source and binary forms, with or without | |
6 | modification, are permitted provided that the following conditions are met: | |
7 | ||
8 | 1. Redistributions of source code must retain the above copyright notice, | |
9 | this list of conditions and the following disclaimer. | |
10 | ||
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. | |
14 | ||
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. | |
17 | ||
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. | |
28 | */ | |
29 | /* | |
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. | |
33 | */ | |
34 | ||
35 | package com.jcraft.jzlib; | |
36 | ||
37 | final public class ZStream{ | |
38 | ||
39 | static final private int MAX_WBITS=15; // 32K LZ77 window | |
40 | static final private int DEF_WBITS=MAX_WBITS; | |
41 | ||
42 | static final private int Z_NO_FLUSH=0; | |
43 | static final private int Z_PARTIAL_FLUSH=1; | |
44 | static final private int Z_SYNC_FLUSH=2; | |
45 | static final private int Z_FULL_FLUSH=3; | |
46 | static final private int Z_FINISH=4; | |
47 | ||
48 | static final private int MAX_MEM_LEVEL=9; | |
49 | ||
50 | static final private int Z_OK=0; | |
51 | static final private int Z_STREAM_END=1; | |
52 | static final private int Z_NEED_DICT=2; | |
53 | static final private int Z_ERRNO=-1; | |
54 | static final private int Z_STREAM_ERROR=-2; | |
55 | static final private int Z_DATA_ERROR=-3; | |
56 | static final private int Z_MEM_ERROR=-4; | |
57 | static final private int Z_BUF_ERROR=-5; | |
58 | static final private int Z_VERSION_ERROR=-6; | |
59 | ||
60 | public byte[] next_in; // next input byte | |
61 | public int next_in_index; | |
62 | public int avail_in; // number of bytes available at next_in | |
63 | public long total_in; // total nb of input bytes read so far | |
64 | ||
65 | public byte[] next_out; // next output byte should be put there | |
66 | public int next_out_index; | |
67 | public int avail_out; // remaining free space at next_out | |
68 | public long total_out; // total nb of bytes output so far | |
69 | ||
70 | public String msg; | |
71 | ||
72 | Deflate dstate; | |
73 | Inflate istate; | |
74 | ||
75 | int data_type; // best guess about the data type: ascii or binary | |
76 | ||
77 | public long adler; | |
78 | Adler32 _adler=new Adler32(); | |
79 | ||
80 | public int inflateInit(){ | |
81 | return inflateInit(DEF_WBITS); | |
82 | } | |
83 | public int inflateInit(boolean nowrap){ | |
84 | return inflateInit(DEF_WBITS, nowrap); | |
85 | } | |
86 | public int inflateInit(int w){ | |
87 | return inflateInit(w, false); | |
88 | } | |
89 | ||
90 | public int inflateInit(int w, boolean nowrap){ | |
91 | istate=new Inflate(); | |
92 | return istate.inflateInit(this, nowrap?-w:w); | |
93 | } | |
94 | ||
95 | public int inflate(int f){ | |
96 | if(istate==null) return Z_STREAM_ERROR; | |
97 | return istate.inflate(this, f); | |
98 | } | |
99 | public int inflateEnd(){ | |
100 | if(istate==null) return Z_STREAM_ERROR; | |
101 | int ret=istate.inflateEnd(this); | |
102 | istate = null; | |
103 | return ret; | |
104 | } | |
105 | public int inflateSync(){ | |
106 | if(istate == null) | |
107 | return Z_STREAM_ERROR; | |
108 | return istate.inflateSync(this); | |
109 | } | |
110 | public int inflateSetDictionary(byte[] dictionary, int dictLength){ | |
111 | if(istate == null) | |
112 | return Z_STREAM_ERROR; | |
113 | return istate.inflateSetDictionary(this, dictionary, dictLength); | |
114 | } | |
115 | ||
116 | public int deflateInit(int level){ | |
117 | return deflateInit(level, MAX_WBITS); | |
118 | } | |
119 | public int deflateInit(int level, boolean nowrap){ | |
120 | return deflateInit(level, MAX_WBITS, nowrap); | |
121 | } | |
122 | public int deflateInit(int level, int bits){ | |
123 | return deflateInit(level, bits, false); | |
124 | } | |
125 | public int deflateInit(int level, int bits, boolean nowrap){ | |
126 | dstate=new Deflate(); | |
127 | return dstate.deflateInit(this, level, nowrap?-bits:bits); | |
128 | } | |
129 | public int deflate(int flush){ | |
130 | if(dstate==null){ | |
131 | return Z_STREAM_ERROR; | |
132 | } | |
133 | return dstate.deflate(this, flush); | |
134 | } | |
135 | public int deflateEnd(){ | |
136 | if(dstate==null) return Z_STREAM_ERROR; | |
137 | int ret=dstate.deflateEnd(); | |
138 | dstate=null; | |
139 | return ret; | |
140 | } | |
141 | public int deflateParams(int level, int strategy){ | |
142 | if(dstate==null) return Z_STREAM_ERROR; | |
143 | return dstate.deflateParams(this, level, strategy); | |
144 | } | |
145 | public int deflateSetDictionary (byte[] dictionary, int dictLength){ | |
146 | if(dstate == null) | |
147 | return Z_STREAM_ERROR; | |
148 | return dstate.deflateSetDictionary(this, dictionary, dictLength); | |
149 | } | |
150 | ||
151 | // Flush as much pending output as possible. All deflate() output goes | |
152 | // through this function so some applications may wish to modify it | |
153 | // to avoid allocating a large strm->next_out buffer and copying into it. | |
154 | // (See also read_buf()). | |
155 | void flush_pending(){ | |
156 | int len=dstate.pending; | |
157 | ||
158 | if(len>avail_out) len=avail_out; | |
159 | if(len==0) return; | |
160 | ||
161 | if(dstate.pending_buf.length<=dstate.pending_out || | |
162 | next_out.length<=next_out_index || | |
163 | dstate.pending_buf.length<(dstate.pending_out+len) || | |
164 | next_out.length<(next_out_index+len)){ | |
165 | System.out.println(dstate.pending_buf.length+", "+dstate.pending_out+ | |
166 | ", "+next_out.length+", "+next_out_index+", "+len); | |
167 | System.out.println("avail_out="+avail_out); | |
168 | } | |
169 | ||
170 | System.arraycopy(dstate.pending_buf, dstate.pending_out, | |
171 | next_out, next_out_index, len); | |
172 | ||
173 | next_out_index+=len; | |
174 | dstate.pending_out+=len; | |
175 | total_out+=len; | |
176 | avail_out-=len; | |
177 | dstate.pending-=len; | |
178 | if(dstate.pending==0){ | |
179 | dstate.pending_out=0; | |
180 | } | |
181 | } | |
182 | ||
183 | // Read a new buffer from the current input stream, update the adler32 | |
184 | // and total number of bytes read. All deflate() input goes through | |
185 | // this function so some applications may wish to modify it to avoid | |
186 | // allocating a large strm->next_in buffer and copying from it. | |
187 | // (See also flush_pending()). | |
188 | int read_buf(byte[] buf, int start, int size) { | |
189 | int len=avail_in; | |
190 | ||
191 | if(len>size) len=size; | |
192 | if(len==0) return 0; | |
193 | ||
194 | avail_in-=len; | |
195 | ||
196 | if(dstate.noheader==0) { | |
197 | adler=_adler.adler32(adler, next_in, next_in_index, len); | |
198 | } | |
199 | System.arraycopy(next_in, next_in_index, buf, start, len); | |
200 | next_in_index += len; | |
201 | total_in += len; | |
202 | return len; | |
203 | } | |
204 | ||
205 | public void free(){ | |
206 | next_in=null; | |
207 | next_out=null; | |
208 | msg=null; | |
209 | _adler=null; | |
210 | } | |
211 | } |