Mercurial > 510Connectbot
comparison src/com/jcraft/jzlib/InfCodes.java @ 357:46c2115ae1c8
update jzlib to a21be20213d66eff15904d925e9b721956a01ef7
author | Carl Byington <carl@five-ten-sg.com> |
---|---|
date | Fri, 01 Aug 2014 13:34:58 -0700 |
parents | 0ce5cc452d02 |
children |
comparison
equal
deleted
inserted
replaced
356:5e91b559b5fe | 357:46c2115ae1c8 |
---|---|
6 modification, are permitted provided that the following conditions are met: | 6 modification, are permitted provided that the following conditions are met: |
7 | 7 |
8 1. Redistributions of source code must retain the above copyright notice, | 8 1. Redistributions of source code must retain the above copyright notice, |
9 this list of conditions and the following disclaimer. | 9 this list of conditions and the following disclaimer. |
10 | 10 |
11 2. Redistributions in binary form must reproduce the above copyright | 11 2. Redistributions in binary form must reproduce the above copyright |
12 notice, this list of conditions and the following disclaimer in | 12 notice, this list of conditions and the following disclaimer in |
13 the documentation and/or other materials provided with the distribution. | 13 the documentation and/or other materials provided with the distribution. |
14 | 14 |
15 3. The names of the authors may not be used to endorse or promote products | 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. | 16 derived from this software without specific prior written permission. |
17 | 17 |
32 * and contributors of zlib. | 32 * and contributors of zlib. |
33 */ | 33 */ |
34 | 34 |
35 package com.jcraft.jzlib; | 35 package com.jcraft.jzlib; |
36 | 36 |
37 final class InfCodes { | 37 final class InfCodes{ |
38 | 38 |
39 static final private int[] inflate_mask = { | 39 static final private int[] inflate_mask = { |
40 0x00000000, 0x00000001, 0x00000003, 0x00000007, 0x0000000f, | 40 0x00000000, 0x00000001, 0x00000003, 0x00000007, 0x0000000f, |
41 0x0000001f, 0x0000003f, 0x0000007f, 0x000000ff, 0x000001ff, | 41 0x0000001f, 0x0000003f, 0x0000007f, 0x000000ff, 0x000001ff, |
42 0x000003ff, 0x000007ff, 0x00000fff, 0x00001fff, 0x00003fff, | 42 0x000003ff, 0x000007ff, 0x00000fff, 0x00001fff, 0x00003fff, |
43 0x00007fff, 0x0000ffff | 43 0x00007fff, 0x0000ffff |
44 }; | 44 }; |
45 | 45 |
46 static final private int Z_OK = 0; | 46 static final private int Z_OK=0; |
47 static final private int Z_STREAM_END = 1; | 47 static final private int Z_STREAM_END=1; |
48 static final private int Z_NEED_DICT = 2; | 48 static final private int Z_NEED_DICT=2; |
49 static final private int Z_ERRNO = -1; | 49 static final private int Z_ERRNO=-1; |
50 static final private int Z_STREAM_ERROR = -2; | 50 static final private int Z_STREAM_ERROR=-2; |
51 static final private int Z_DATA_ERROR = -3; | 51 static final private int Z_DATA_ERROR=-3; |
52 static final private int Z_MEM_ERROR = -4; | 52 static final private int Z_MEM_ERROR=-4; |
53 static final private int Z_BUF_ERROR = -5; | 53 static final private int Z_BUF_ERROR=-5; |
54 static final private int Z_VERSION_ERROR = -6; | 54 static final private int Z_VERSION_ERROR=-6; |
55 | 55 |
56 // waiting for "i:"=input, | 56 // waiting for "i:"=input, |
57 // "o:"=output, | 57 // "o:"=output, |
58 // "x:"=nothing | 58 // "x:"=nothing |
59 static final private int START = 0; // x: set up for LEN | 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 | 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) | 61 static final private int LENEXT=2; // i: getting length extra (have base) |
62 static final private int DIST = 3; // i: get distance next | 62 static final private int DIST=3; // i: get distance next |
63 static final private int DISTEXT = 4; // i: getting distance extra | 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 | 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 | 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 | 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 | 67 static final private int END=8; // x: got eob and all data flushed |
68 static final private int BADCODE = 9; // x: got error | 68 static final private int BADCODE=9;// x: got error |
69 | 69 |
70 int mode; // current inflate_codes mode | 70 int mode; // current inflate_codes mode |
71 | 71 |
72 // mode dependent information | 72 // mode dependent information |
73 int len; | 73 int len; |
74 | 74 |
75 int[] tree; // pointer into tree | 75 int[] tree; // pointer into tree |
76 int tree_index = 0; | 76 int tree_index=0; |
77 int need; // bits needed | 77 int need; // bits needed |
78 | 78 |
79 int lit; | 79 int lit; |
80 | 80 |
81 // if EXT or COPY, where and how much | 81 // if EXT or COPY, where and how much |
82 int get; // bits to get for extra | 82 int get; // bits to get for extra |
83 int dist; // distance back to copy from | 83 int dist; // distance back to copy from |
84 | 84 |
85 byte lbits; // ltree bits decoded per branch | 85 byte lbits; // ltree bits decoded per branch |
86 byte dbits; // dtree bits decoder per branch | 86 byte dbits; // dtree bits decoder per branch |
87 int[] ltree; // literal/length/eob tree | 87 int[] ltree; // literal/length/eob tree |
88 int ltree_index; // literal/length/eob tree | 88 int ltree_index; // literal/length/eob tree |
89 int[] dtree; // distance tree | 89 int[] dtree; // distance tree |
90 int dtree_index; // distance tree | 90 int dtree_index; // distance tree |
91 | 91 |
92 InfCodes() { | 92 private final ZStream z; |
93 private final InfBlocks s; | |
94 InfCodes(ZStream z, InfBlocks s){ | |
95 this.z=z; | |
96 this.s=s; | |
97 } | |
98 | |
99 void init(int bl, int bd, | |
100 int[] tl, int tl_index, | |
101 int[] td, int td_index){ | |
102 mode=START; | |
103 lbits=(byte)bl; | |
104 dbits=(byte)bd; | |
105 ltree=tl; | |
106 ltree_index=tl_index; | |
107 dtree = td; | |
108 dtree_index=td_index; | |
109 tree=null; | |
110 } | |
111 | |
112 int proc(int r){ | |
113 int j; // temporary storage | |
114 int[] t; // temporary pointer | |
115 int tindex; // temporary pointer | |
116 int e; // extra bits or operation | |
117 int b=0; // bit buffer | |
118 int k=0; // bits in bit buffer | |
119 int p=0; // input data pointer | |
120 int n; // bytes available there | |
121 int q; // output window write pointer | |
122 int m; // bytes to end of window or read pointer | |
123 int f; // pointer to copy strings from | |
124 | |
125 // copy input/output information to locals (UPDATE macro restores) | |
126 p=z.next_in_index;n=z.avail_in;b=s.bitb;k=s.bitk; | |
127 q=s.write;m=q<s.read?s.read-q-1:s.end-q; | |
128 | |
129 // process input and output based on current state | |
130 while (true){ | |
131 switch (mode){ | |
132 // waiting for "i:"=input, "o:"=output, "x:"=nothing | |
133 case START: // x: set up for LEN | |
134 if (m >= 258 && n >= 10){ | |
135 | |
136 s.bitb=b;s.bitk=k; | |
137 z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p; | |
138 s.write=q; | |
139 r = inflate_fast(lbits, dbits, | |
140 ltree, ltree_index, | |
141 dtree, dtree_index, | |
142 s, z); | |
143 | |
144 p=z.next_in_index;n=z.avail_in;b=s.bitb;k=s.bitk; | |
145 q=s.write;m=q<s.read?s.read-q-1:s.end-q; | |
146 | |
147 if (r != Z_OK){ | |
148 mode = r == Z_STREAM_END ? WASH : BADCODE; | |
149 break; | |
150 } | |
151 } | |
152 need = lbits; | |
153 tree = ltree; | |
154 tree_index=ltree_index; | |
155 | |
156 mode = LEN; | |
157 case LEN: // i: get length/literal/eob next | |
158 j = need; | |
159 | |
160 while(k<(j)){ | |
161 if(n!=0)r=Z_OK; | |
162 else{ | |
163 | |
164 s.bitb=b;s.bitk=k; | |
165 z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p; | |
166 s.write=q; | |
167 return s.inflate_flush(r); | |
168 } | |
169 n--; | |
170 b|=(z.next_in[p++]&0xff)<<k; | |
171 k+=8; | |
172 } | |
173 | |
174 tindex=(tree_index+(b&inflate_mask[j]))*3; | |
175 | |
176 b>>>=(tree[tindex+1]); | |
177 k-=(tree[tindex+1]); | |
178 | |
179 e=tree[tindex]; | |
180 | |
181 if(e == 0){ // literal | |
182 lit = tree[tindex+2]; | |
183 mode = LIT; | |
184 break; | |
185 } | |
186 if((e & 16)!=0 ){ // length | |
187 get = e & 15; | |
188 len = tree[tindex+2]; | |
189 mode = LENEXT; | |
190 break; | |
191 } | |
192 if ((e & 64) == 0){ // next table | |
193 need = e; | |
194 tree_index = tindex/3+tree[tindex+2]; | |
195 break; | |
196 } | |
197 if ((e & 32)!=0){ // end of block | |
198 mode = WASH; | |
199 break; | |
200 } | |
201 mode = BADCODE; // invalid code | |
202 z.msg = "invalid literal/length code"; | |
203 r = Z_DATA_ERROR; | |
204 | |
205 s.bitb=b;s.bitk=k; | |
206 z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p; | |
207 s.write=q; | |
208 return s.inflate_flush(r); | |
209 | |
210 case LENEXT: // i: getting length extra (have base) | |
211 j = get; | |
212 | |
213 while(k<(j)){ | |
214 if(n!=0)r=Z_OK; | |
215 else{ | |
216 | |
217 s.bitb=b;s.bitk=k; | |
218 z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p; | |
219 s.write=q; | |
220 return s.inflate_flush(r); | |
221 } | |
222 n--; b|=(z.next_in[p++]&0xff)<<k; | |
223 k+=8; | |
224 } | |
225 | |
226 len += (b & inflate_mask[j]); | |
227 | |
228 b>>=j; | |
229 k-=j; | |
230 | |
231 need = dbits; | |
232 tree = dtree; | |
233 tree_index=dtree_index; | |
234 mode = DIST; | |
235 case DIST: // i: get distance next | |
236 j = need; | |
237 | |
238 while(k<(j)){ | |
239 if(n!=0)r=Z_OK; | |
240 else{ | |
241 | |
242 s.bitb=b;s.bitk=k; | |
243 z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p; | |
244 s.write=q; | |
245 return s.inflate_flush(r); | |
246 } | |
247 n--; b|=(z.next_in[p++]&0xff)<<k; | |
248 k+=8; | |
249 } | |
250 | |
251 tindex=(tree_index+(b & inflate_mask[j]))*3; | |
252 | |
253 b>>=tree[tindex+1]; | |
254 k-=tree[tindex+1]; | |
255 | |
256 e = (tree[tindex]); | |
257 if((e & 16)!=0){ // distance | |
258 get = e & 15; | |
259 dist = tree[tindex+2]; | |
260 mode = DISTEXT; | |
261 break; | |
262 } | |
263 if ((e & 64) == 0){ // next table | |
264 need = e; | |
265 tree_index = tindex/3 + tree[tindex+2]; | |
266 break; | |
267 } | |
268 mode = BADCODE; // invalid code | |
269 z.msg = "invalid distance code"; | |
270 r = Z_DATA_ERROR; | |
271 | |
272 s.bitb=b;s.bitk=k; | |
273 z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p; | |
274 s.write=q; | |
275 return s.inflate_flush(r); | |
276 | |
277 case DISTEXT: // i: getting distance extra | |
278 j = get; | |
279 | |
280 while(k<(j)){ | |
281 if(n!=0)r=Z_OK; | |
282 else{ | |
283 | |
284 s.bitb=b;s.bitk=k; | |
285 z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p; | |
286 s.write=q; | |
287 return s.inflate_flush(r); | |
288 } | |
289 n--; b|=(z.next_in[p++]&0xff)<<k; | |
290 k+=8; | |
291 } | |
292 | |
293 dist += (b & inflate_mask[j]); | |
294 | |
295 b>>=j; | |
296 k-=j; | |
297 | |
298 mode = COPY; | |
299 case COPY: // o: copying bytes in window, waiting for space | |
300 f = q - dist; | |
301 while(f < 0){ // modulo window size-"while" instead | |
302 f += s.end; // of "if" handles invalid distances | |
303 } | |
304 while (len!=0){ | |
305 | |
306 if(m==0){ | |
307 if(q==s.end&&s.read!=0){q=0;m=q<s.read?s.read-q-1:s.end-q;} | |
308 if(m==0){ | |
309 s.write=q; r=s.inflate_flush(r); | |
310 q=s.write;m=q<s.read?s.read-q-1:s.end-q; | |
311 | |
312 if(q==s.end&&s.read!=0){q=0;m=q<s.read?s.read-q-1:s.end-q;} | |
313 | |
314 if(m==0){ | |
315 s.bitb=b;s.bitk=k; | |
316 z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p; | |
317 s.write=q; | |
318 return s.inflate_flush(r); | |
319 } | |
320 } | |
321 } | |
322 | |
323 s.window[q++]=s.window[f++]; m--; | |
324 | |
325 if (f == s.end) | |
326 f = 0; | |
327 len--; | |
328 } | |
329 mode = START; | |
330 break; | |
331 case LIT: // o: got literal, waiting for output space | |
332 if(m==0){ | |
333 if(q==s.end&&s.read!=0){q=0;m=q<s.read?s.read-q-1:s.end-q;} | |
334 if(m==0){ | |
335 s.write=q; r=s.inflate_flush(r); | |
336 q=s.write;m=q<s.read?s.read-q-1:s.end-q; | |
337 | |
338 if(q==s.end&&s.read!=0){q=0;m=q<s.read?s.read-q-1:s.end-q;} | |
339 if(m==0){ | |
340 s.bitb=b;s.bitk=k; | |
341 z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p; | |
342 s.write=q; | |
343 return s.inflate_flush(r); | |
344 } | |
345 } | |
346 } | |
347 r=Z_OK; | |
348 | |
349 s.window[q++]=(byte)lit; m--; | |
350 | |
351 mode = START; | |
352 break; | |
353 case WASH: // o: got eob, possibly more output | |
354 if (k > 7){ // return unused byte, if any | |
355 k -= 8; | |
356 n++; | |
357 p--; // can always return one | |
358 } | |
359 | |
360 s.write=q; r=s.inflate_flush(r); | |
361 q=s.write;m=q<s.read?s.read-q-1:s.end-q; | |
362 | |
363 if (s.read != s.write){ | |
364 s.bitb=b;s.bitk=k; | |
365 z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p; | |
366 s.write=q; | |
367 return s.inflate_flush(r); | |
368 } | |
369 mode = END; | |
370 case END: | |
371 r = Z_STREAM_END; | |
372 s.bitb=b;s.bitk=k; | |
373 z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p; | |
374 s.write=q; | |
375 return s.inflate_flush(r); | |
376 | |
377 case BADCODE: // x: got error | |
378 | |
379 r = Z_DATA_ERROR; | |
380 | |
381 s.bitb=b;s.bitk=k; | |
382 z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p; | |
383 s.write=q; | |
384 return s.inflate_flush(r); | |
385 | |
386 default: | |
387 r = Z_STREAM_ERROR; | |
388 | |
389 s.bitb=b;s.bitk=k; | |
390 z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p; | |
391 s.write=q; | |
392 return s.inflate_flush(r); | |
393 } | |
93 } | 394 } |
94 void init(int bl, int bd, | 395 } |
95 int[] tl, int tl_index, | 396 |
96 int[] td, int td_index, ZStream z) { | 397 void free(ZStream z){ |
97 mode = START; | 398 // ZFREE(z, c); |
98 lbits = (byte)bl; | 399 } |
99 dbits = (byte)bd; | 400 |
100 ltree = tl; | 401 // Called with number of bytes left to write in window at least 258 |
101 ltree_index = tl_index; | 402 // (the maximum string length) and number of input bytes available |
102 dtree = td; | 403 // at least ten. The ten bytes are six bytes for the longest length/ |
103 dtree_index = td_index; | 404 // distance pair plus four bytes for overloading the bit buffer. |
104 tree = null; | 405 |
105 } | 406 int inflate_fast(int bl, int bd, |
106 | 407 int[] tl, int tl_index, |
107 int proc(InfBlocks s, ZStream z, int r) { | 408 int[] td, int td_index, |
108 int j; // temporary storage | 409 InfBlocks s, ZStream z){ |
109 int[] t; // temporary pointer | 410 int t; // temporary pointer |
110 int tindex; // temporary pointer | 411 int[] tp; // temporary pointer |
111 int e; // extra bits or operation | 412 int tp_index; // temporary pointer |
112 int b = 0; // bit buffer | 413 int e; // extra bits or operation |
113 int k = 0; // bits in bit buffer | 414 int b; // bit buffer |
114 int p = 0; // input data pointer | 415 int k; // bits in bit buffer |
115 int n; // bytes available there | 416 int p; // input data pointer |
116 int q; // output window write pointer | 417 int n; // bytes available there |
117 int m; // bytes to end of window or read pointer | 418 int q; // output window write pointer |
118 int f; // pointer to copy strings from | 419 int m; // bytes to end of window or read pointer |
119 // copy input/output information to locals (UPDATE macro restores) | 420 int ml; // mask for literal/length tree |
120 p = z.next_in_index; n = z.avail_in; b = s.bitb; k = s.bitk; | 421 int md; // mask for distance tree |
121 q = s.write; m = q < s.read ? s.read - q - 1 : s.end - q; | 422 int c; // bytes to copy |
122 | 423 int d; // distance back to copy from |
123 // process input and output based on current state | 424 int r; // copy source pointer |
124 while (true) { | 425 |
125 switch (mode) { | 426 int tp_index_t_3; // (tp_index+t)*3 |
126 // waiting for "i:"=input, "o:"=output, "x:"=nothing | 427 |
127 case START: // x: set up for LEN | 428 // load input, output, bit values |
128 if (m >= 258 && n >= 10) { | 429 p=z.next_in_index;n=z.avail_in;b=s.bitb;k=s.bitk; |
129 s.bitb = b; s.bitk = k; | 430 q=s.write;m=q<s.read?s.read-q-1:s.end-q; |
130 z.avail_in = n; z.total_in += p - z.next_in_index; z.next_in_index = p; | 431 |
131 s.write = q; | 432 // initialize masks |
132 r = inflate_fast(lbits, dbits, | 433 ml = inflate_mask[bl]; |
133 ltree, ltree_index, | 434 md = inflate_mask[bd]; |
134 dtree, dtree_index, | 435 |
135 s, z); | 436 // do until not enough input or output space for fast loop |
136 p = z.next_in_index; n = z.avail_in; b = s.bitb; k = s.bitk; | 437 do { // assume called with m >= 258 && n >= 10 |
137 q = s.write; m = q < s.read ? s.read - q - 1 : s.end - q; | 438 // get literal/length code |
138 | 439 while(k<(20)){ // max bits for literal/length code |
139 if (r != Z_OK) { | 440 n--; |
140 mode = r == Z_STREAM_END ? WASH : BADCODE; | 441 b|=(z.next_in[p++]&0xff)<<k;k+=8; |
141 break; | 442 } |
142 } | 443 |
143 } | 444 t= b&ml; |
144 | 445 tp=tl; |
145 need = lbits; | 446 tp_index=tl_index; |
146 tree = ltree; | 447 tp_index_t_3=(tp_index+t)*3; |
147 tree_index = ltree_index; | 448 if ((e = tp[tp_index_t_3]) == 0){ |
148 mode = LEN; | 449 b>>=(tp[tp_index_t_3+1]); k-=(tp[tp_index_t_3+1]); |
149 | 450 |
150 case LEN: // i: get length/literal/eob next | 451 s.window[q++] = (byte)tp[tp_index_t_3+2]; |
151 j = need; | 452 m--; |
152 | 453 continue; |
153 while (k < (j)) { | 454 } |
154 if (n != 0)r = Z_OK; | 455 do { |
155 else { | 456 |
156 s.bitb = b; s.bitk = k; | 457 b>>=(tp[tp_index_t_3+1]); k-=(tp[tp_index_t_3+1]); |
157 z.avail_in = n; z.total_in += p - z.next_in_index; z.next_in_index = p; | 458 |
158 s.write = q; | 459 if((e&16)!=0){ |
159 return s.inflate_flush(z, r); | 460 e &= 15; |
160 } | 461 c = tp[tp_index_t_3+2] + ((int)b & inflate_mask[e]); |
161 | 462 |
162 n--; | 463 b>>=e; k-=e; |
163 b |= (z.next_in[p++] & 0xff) << k; | 464 |
164 k += 8; | 465 // decode distance base of block to copy |
165 } | 466 while(k<(15)){ // max bits for distance code |
166 | 467 n--; |
167 tindex = (tree_index + (b & inflate_mask[j])) * 3; | 468 b|=(z.next_in[p++]&0xff)<<k;k+=8; |
168 b >>>= (tree[tindex + 1]); | 469 } |
169 k -= (tree[tindex + 1]); | 470 |
170 e = tree[tindex]; | 471 t= b&md; |
171 | 472 tp=td; |
172 if (e == 0) { // literal | 473 tp_index=td_index; |
173 lit = tree[tindex + 2]; | 474 tp_index_t_3=(tp_index+t)*3; |
174 mode = LIT; | 475 e = tp[tp_index_t_3]; |
175 break; | 476 |
176 } | 477 do { |
177 | 478 |
178 if ((e & 16) != 0) { // length | 479 b>>=(tp[tp_index_t_3+1]); k-=(tp[tp_index_t_3+1]); |
179 get = e & 15; | 480 |
180 len = tree[tindex + 2]; | 481 if((e&16)!=0){ |
181 mode = LENEXT; | 482 // get extra bits to add to distance base |
182 break; | 483 e &= 15; |
183 } | 484 while(k<(e)){ // get extra bits (up to 13) |
184 | 485 n--; |
185 if ((e & 64) == 0) { // next table | 486 b|=(z.next_in[p++]&0xff)<<k;k+=8; |
186 need = e; | 487 } |
187 tree_index = tindex / 3 + tree[tindex + 2]; | 488 |
188 break; | 489 d = tp[tp_index_t_3+2] + (b&inflate_mask[e]); |
189 } | 490 |
190 | 491 b>>=(e); k-=(e); |
191 if ((e & 32) != 0) { // end of block | 492 |
192 mode = WASH; | 493 // do the copy |
193 break; | 494 m -= c; |
194 } | 495 if (q >= d){ // offset before dest |
195 | 496 // just copy |
196 mode = BADCODE; // invalid code | 497 r=q-d; |
197 z.msg = "invalid literal/length code"; | 498 if(q-r>0 && 2>(q-r)){ |
198 r = Z_DATA_ERROR; | 499 s.window[q++]=s.window[r++]; // minimum count is three, |
199 s.bitb = b; s.bitk = k; | 500 s.window[q++]=s.window[r++]; // so unroll loop a little |
200 z.avail_in = n; z.total_in += p - z.next_in_index; z.next_in_index = p; | 501 c-=2; |
201 s.write = q; | 502 } |
202 return s.inflate_flush(z, r); | 503 else{ |
203 | 504 System.arraycopy(s.window, r, s.window, q, 2); |
204 case LENEXT: // i: getting length extra (have base) | 505 q+=2; r+=2; c-=2; |
205 j = get; | 506 } |
206 | 507 } |
207 while (k < (j)) { | 508 else{ // else offset after destination |
208 if (n != 0)r = Z_OK; | 509 r=q-d; |
209 else { | 510 do{ |
210 s.bitb = b; s.bitk = k; | 511 r+=s.end; // force pointer in window |
211 z.avail_in = n; z.total_in += p - z.next_in_index; z.next_in_index = p; | 512 }while(r<0); // covers invalid distances |
212 s.write = q; | 513 e=s.end-r; |
213 return s.inflate_flush(z, r); | 514 if(c>e){ // if source crosses, |
214 } | 515 c-=e; // wrapped copy |
215 | 516 if(q-r>0 && e>(q-r)){ |
216 n--; b |= (z.next_in[p++] & 0xff) << k; | 517 do{s.window[q++] = s.window[r++];} |
217 k += 8; | 518 while(--e!=0); |
218 } | 519 } |
219 | 520 else{ |
220 len += (b & inflate_mask[j]); | 521 System.arraycopy(s.window, r, s.window, q, e); |
221 b >>= j; | 522 q+=e; r+=e; e=0; |
222 k -= j; | 523 } |
223 need = dbits; | 524 r = 0; // copy rest from start of window |
224 tree = dtree; | 525 } |
225 tree_index = dtree_index; | 526 |
226 mode = DIST; | 527 } |
227 | 528 |
228 case DIST: // i: get distance next | 529 // copy all or what's left |
229 j = need; | 530 if(q-r>0 && c>(q-r)){ |
230 | 531 do{s.window[q++] = s.window[r++];} |
231 while (k < (j)) { | 532 while(--c!=0); |
232 if (n != 0)r = Z_OK; | 533 } |
233 else { | 534 else{ |
234 s.bitb = b; s.bitk = k; | 535 System.arraycopy(s.window, r, s.window, q, c); |
235 z.avail_in = n; z.total_in += p - z.next_in_index; z.next_in_index = p; | 536 q+=c; r+=c; c=0; |
236 s.write = q; | 537 } |
237 return s.inflate_flush(z, r); | 538 break; |
238 } | 539 } |
239 | 540 else if((e&64)==0){ |
240 n--; b |= (z.next_in[p++] & 0xff) << k; | 541 t+=tp[tp_index_t_3+2]; |
241 k += 8; | 542 t+=(b&inflate_mask[e]); |
242 } | 543 tp_index_t_3=(tp_index+t)*3; |
243 | 544 e=tp[tp_index_t_3]; |
244 tindex = (tree_index + (b & inflate_mask[j])) * 3; | 545 } |
245 b >>= tree[tindex + 1]; | 546 else{ |
246 k -= tree[tindex + 1]; | 547 z.msg = "invalid distance code"; |
247 e = (tree[tindex]); | 548 |
248 | 549 c=z.avail_in-n;c=(k>>3)<c?k>>3:c;n+=c;p-=c;k-=c<<3; |
249 if ((e & 16) != 0) { // distance | 550 |
250 get = e & 15; | 551 s.bitb=b;s.bitk=k; |
251 dist = tree[tindex + 2]; | 552 z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p; |
252 mode = DISTEXT; | 553 s.write=q; |
253 break; | 554 |
254 } | 555 return Z_DATA_ERROR; |
255 | 556 } |
256 if ((e & 64) == 0) { // next table | 557 } |
257 need = e; | 558 while(true); |
258 tree_index = tindex / 3 + tree[tindex + 2]; | 559 break; |
259 break; | 560 } |
260 } | 561 |
261 | 562 if((e&64)==0){ |
262 mode = BADCODE; // invalid code | 563 t+=tp[tp_index_t_3+2]; |
263 z.msg = "invalid distance code"; | 564 t+=(b&inflate_mask[e]); |
264 r = Z_DATA_ERROR; | 565 tp_index_t_3=(tp_index+t)*3; |
265 s.bitb = b; s.bitk = k; | 566 if((e=tp[tp_index_t_3])==0){ |
266 z.avail_in = n; z.total_in += p - z.next_in_index; z.next_in_index = p; | 567 |
267 s.write = q; | 568 b>>=(tp[tp_index_t_3+1]); k-=(tp[tp_index_t_3+1]); |
268 return s.inflate_flush(z, r); | 569 |
269 | 570 s.window[q++]=(byte)tp[tp_index_t_3+2]; |
270 case DISTEXT: // i: getting distance extra | 571 m--; |
271 j = get; | 572 break; |
272 | 573 } |
273 while (k < (j)) { | 574 } |
274 if (n != 0)r = Z_OK; | 575 else if((e&32)!=0){ |
275 else { | 576 |
276 s.bitb = b; s.bitk = k; | 577 c=z.avail_in-n;c=(k>>3)<c?k>>3:c;n+=c;p-=c;k-=c<<3; |
277 z.avail_in = n; z.total_in += p - z.next_in_index; z.next_in_index = p; | 578 |
278 s.write = q; | 579 s.bitb=b;s.bitk=k; |
279 return s.inflate_flush(z, r); | 580 z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p; |
280 } | 581 s.write=q; |
281 | 582 |
282 n--; b |= (z.next_in[p++] & 0xff) << k; | 583 return Z_STREAM_END; |
283 k += 8; | 584 } |
284 } | 585 else{ |
285 | 586 z.msg="invalid literal/length code"; |
286 dist += (b & inflate_mask[j]); | 587 |
287 b >>= j; | 588 c=z.avail_in-n;c=(k>>3)<c?k>>3:c;n+=c;p-=c;k-=c<<3; |
288 k -= j; | 589 |
289 mode = COPY; | 590 s.bitb=b;s.bitk=k; |
290 | 591 z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p; |
291 case COPY: // o: copying bytes in window, waiting for space | 592 s.write=q; |
292 f = q - dist; | 593 |
293 | 594 return Z_DATA_ERROR; |
294 while (f < 0) { // modulo window size-"while" instead | 595 } |
295 f += s.end; // of "if" handles invalid distances | 596 } |
296 } | 597 while(true); |
297 | 598 } |
298 while (len != 0) { | 599 while(m>=258 && n>= 10); |
299 if (m == 0) { | 600 |
300 if (q == s.end && s.read != 0) {q = 0; m = q < s.read ? s.read - q - 1 : s.end - q;} | 601 // not enough input or output--restore pointers and return |
301 | 602 c=z.avail_in-n;c=(k>>3)<c?k>>3:c;n+=c;p-=c;k-=c<<3; |
302 if (m == 0) { | 603 |
303 s.write = q; r = s.inflate_flush(z, r); | 604 s.bitb=b;s.bitk=k; |
304 q = s.write; m = q < s.read ? s.read - q - 1 : s.end - q; | 605 z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p; |
305 | 606 s.write=q; |
306 if (q == s.end && s.read != 0) {q = 0; m = q < s.read ? s.read - q - 1 : s.end - q;} | 607 |
307 | 608 return Z_OK; |
308 if (m == 0) { | 609 } |
309 s.bitb = b; s.bitk = k; | |
310 z.avail_in = n; z.total_in += p - z.next_in_index; z.next_in_index = p; | |
311 s.write = q; | |
312 return s.inflate_flush(z, r); | |
313 } | |
314 } | |
315 } | |
316 | |
317 s.window[q++] = s.window[f++]; m--; | |
318 | |
319 if (f == s.end) | |
320 f = 0; | |
321 | |
322 len--; | |
323 } | |
324 | |
325 mode = START; | |
326 break; | |
327 | |
328 case LIT: // o: got literal, waiting for output space | |
329 if (m == 0) { | |
330 if (q == s.end && s.read != 0) {q = 0; m = q < s.read ? s.read - q - 1 : s.end - q;} | |
331 | |
332 if (m == 0) { | |
333 s.write = q; r = s.inflate_flush(z, r); | |
334 q = s.write; m = q < s.read ? s.read - q - 1 : s.end - q; | |
335 | |
336 if (q == s.end && s.read != 0) {q = 0; m = q < s.read ? s.read - q - 1 : s.end - q;} | |
337 | |
338 if (m == 0) { | |
339 s.bitb = b; s.bitk = k; | |
340 z.avail_in = n; z.total_in += p - z.next_in_index; z.next_in_index = p; | |
341 s.write = q; | |
342 return s.inflate_flush(z, r); | |
343 } | |
344 } | |
345 } | |
346 | |
347 r = Z_OK; | |
348 s.window[q++] = (byte)lit; m--; | |
349 mode = START; | |
350 break; | |
351 | |
352 case WASH: // o: got eob, possibly more output | |
353 if (k > 7) { // return unused byte, if any | |
354 k -= 8; | |
355 n++; | |
356 p--; // can always return one | |
357 } | |
358 | |
359 s.write = q; r = s.inflate_flush(z, r); | |
360 q = s.write; m = q < s.read ? s.read - q - 1 : s.end - q; | |
361 | |
362 if (s.read != s.write) { | |
363 s.bitb = b; s.bitk = k; | |
364 z.avail_in = n; z.total_in += p - z.next_in_index; z.next_in_index = p; | |
365 s.write = q; | |
366 return s.inflate_flush(z, r); | |
367 } | |
368 | |
369 mode = END; | |
370 | |
371 case END: | |
372 r = Z_STREAM_END; | |
373 s.bitb = b; s.bitk = k; | |
374 z.avail_in = n; z.total_in += p - z.next_in_index; z.next_in_index = p; | |
375 s.write = q; | |
376 return s.inflate_flush(z, r); | |
377 | |
378 case BADCODE: // x: got error | |
379 r = Z_DATA_ERROR; | |
380 s.bitb = b; s.bitk = k; | |
381 z.avail_in = n; z.total_in += p - z.next_in_index; z.next_in_index = p; | |
382 s.write = q; | |
383 return s.inflate_flush(z, r); | |
384 | |
385 default: | |
386 r = Z_STREAM_ERROR; | |
387 s.bitb = b; s.bitk = k; | |
388 z.avail_in = n; z.total_in += p - z.next_in_index; z.next_in_index = p; | |
389 s.write = q; | |
390 return s.inflate_flush(z, r); | |
391 } | |
392 } | |
393 } | |
394 | |
395 void free(ZStream z) { | |
396 // ZFREE(z, c); | |
397 } | |
398 | |
399 // Called with number of bytes left to write in window at least 258 | |
400 // (the maximum string length) and number of input bytes available | |
401 // at least ten. The ten bytes are six bytes for the longest length/ | |
402 // distance pair plus four bytes for overloading the bit buffer. | |
403 | |
404 int inflate_fast(int bl, int bd, | |
405 int[] tl, int tl_index, | |
406 int[] td, int td_index, | |
407 InfBlocks s, ZStream z) { | |
408 int t; // temporary pointer | |
409 int[] tp; // temporary pointer | |
410 int tp_index; // temporary pointer | |
411 int e; // extra bits or operation | |
412 int b; // bit buffer | |
413 int k; // bits in bit buffer | |
414 int p; // input data pointer | |
415 int n; // bytes available there | |
416 int q; // output window write pointer | |
417 int m; // bytes to end of window or read pointer | |
418 int ml; // mask for literal/length tree | |
419 int md; // mask for distance tree | |
420 int c; // bytes to copy | |
421 int d; // distance back to copy from | |
422 int r; // copy source pointer | |
423 int tp_index_t_3; // (tp_index+t)*3 | |
424 // load input, output, bit values | |
425 p = z.next_in_index; n = z.avail_in; b = s.bitb; k = s.bitk; | |
426 q = s.write; m = q < s.read ? s.read - q - 1 : s.end - q; | |
427 // initialize masks | |
428 ml = inflate_mask[bl]; | |
429 md = inflate_mask[bd]; | |
430 | |
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 | |
435 n--; | |
436 b |= (z.next_in[p++] & 0xff) << k; k += 8; | |
437 } | |
438 | |
439 t = b & ml; | |
440 tp = tl; | |
441 tp_index = tl_index; | |
442 tp_index_t_3 = (tp_index + t) * 3; | |
443 | |
444 if ((e = tp[tp_index_t_3]) == 0) { | |
445 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]; | |
447 m--; | |
448 continue; | |
449 } | |
450 | |
451 do { | |
452 b >>= (tp[tp_index_t_3 + 1]); k -= (tp[tp_index_t_3 + 1]); | |
453 | |
454 if ((e & 16) != 0) { | |
455 e &= 15; | |
456 c = tp[tp_index_t_3 + 2] + ((int)b & inflate_mask[e]); | |
457 b >>= e; k -= e; | |
458 | |
459 // decode distance base of block to copy | |
460 while (k < (15)) { // max bits for distance code | |
461 n--; | |
462 b |= (z.next_in[p++] & 0xff) << k; k += 8; | |
463 } | |
464 | |
465 t = b & md; | |
466 tp = td; | |
467 tp_index = td_index; | |
468 tp_index_t_3 = (tp_index + t) * 3; | |
469 e = tp[tp_index_t_3]; | |
470 | |
471 do { | |
472 b >>= (tp[tp_index_t_3 + 1]); k -= (tp[tp_index_t_3 + 1]); | |
473 | |
474 if ((e & 16) != 0) { | |
475 // get extra bits to add to distance base | |
476 e &= 15; | |
477 | |
478 while (k < (e)) { // get extra bits (up to 13) | |
479 n--; | |
480 b |= (z.next_in[p++] & 0xff) << k; k += 8; | |
481 } | |
482 | |
483 d = tp[tp_index_t_3 + 2] + (b & inflate_mask[e]); | |
484 b >>= (e); k -= (e); | |
485 // do the copy | |
486 m -= c; | |
487 | |
488 if (q >= d) { // offset before dest | |
489 // just copy | |
490 r = q - d; | |
491 | |
492 if (q - r > 0 && 2 > (q - r)) { | |
493 s.window[q++] = s.window[r++]; // minimum count is three, | |
494 s.window[q++] = s.window[r++]; // so unroll loop a little | |
495 c -= 2; | |
496 } | |
497 else { | |
498 System.arraycopy(s.window, r, s.window, q, 2); | |
499 q += 2; r += 2; c -= 2; | |
500 } | |
501 } | |
502 else { // else offset after destination | |
503 r = q - d; | |
504 | |
505 do { | |
506 r += s.end; // force pointer in window | |
507 } | |
508 while (r < 0); // covers invalid distances | |
509 | |
510 e = s.end - r; | |
511 | |
512 if (c > e) { // if source crosses, | |
513 c -= e; // wrapped copy | |
514 | |
515 if (q - r > 0 && e > (q - r)) { | |
516 do {s.window[q++] = s.window[r++];} | |
517 while (--e != 0); | |
518 } | |
519 else { | |
520 System.arraycopy(s.window, r, s.window, q, e); | |
521 q += e; r += e; e = 0; | |
522 } | |
523 | |
524 r = 0; // copy rest from start of window | |
525 } | |
526 } | |
527 | |
528 // copy all or what's left | |
529 if (q - r > 0 && c > (q - r)) { | |
530 do {s.window[q++] = s.window[r++];} | |
531 while (--c != 0); | |
532 } | |
533 else { | |
534 System.arraycopy(s.window, r, s.window, q, c); | |
535 q += c; r += c; c = 0; | |
536 } | |
537 | |
538 break; | |
539 } | |
540 else if ((e & 64) == 0) { | |
541 t += tp[tp_index_t_3 + 2]; | |
542 t += (b & inflate_mask[e]); | |
543 tp_index_t_3 = (tp_index + t) * 3; | |
544 e = tp[tp_index_t_3]; | |
545 } | |
546 else { | |
547 z.msg = "invalid distance code"; | |
548 c = z.avail_in - n; c = (k >> 3)<c ? k >> 3: c; n += c; p -= c; k -= c << 3; | |
549 s.bitb = b; s.bitk = k; | |
550 z.avail_in = n; z.total_in += p - z.next_in_index; z.next_in_index = p; | |
551 s.write = q; | |
552 return Z_DATA_ERROR; | |
553 } | |
554 } | |
555 while (true); | |
556 | |
557 break; | |
558 } | |
559 | |
560 if ((e & 64) == 0) { | |
561 t += tp[tp_index_t_3 + 2]; | |
562 t += (b & inflate_mask[e]); | |
563 tp_index_t_3 = (tp_index + t) * 3; | |
564 | |
565 if ((e = tp[tp_index_t_3]) == 0) { | |
566 b >>= (tp[tp_index_t_3 + 1]); k -= (tp[tp_index_t_3 + 1]); | |
567 s.window[q++] = (byte)tp[tp_index_t_3 + 2]; | |
568 m--; | |
569 break; | |
570 } | |
571 } | |
572 else if ((e & 32) != 0) { | |
573 c = z.avail_in - n; c = (k >> 3)<c ? k >> 3: c; n += c; p -= c; k -= c << 3; | |
574 s.bitb = b; s.bitk = k; | |
575 z.avail_in = n; z.total_in += p - z.next_in_index; z.next_in_index = p; | |
576 s.write = q; | |
577 return Z_STREAM_END; | |
578 } | |
579 else { | |
580 z.msg = "invalid literal/length code"; | |
581 c = z.avail_in - n; c = (k >> 3)<c ? k >> 3: c; n += c; p -= c; k -= c << 3; | |
582 s.bitb = b; s.bitk = k; | |
583 z.avail_in = n; z.total_in += p - z.next_in_index; z.next_in_index = p; | |
584 s.write = q; | |
585 return Z_DATA_ERROR; | |
586 } | |
587 } | |
588 while (true); | |
589 } | |
590 while (m >= 258 && n >= 10); | |
591 | |
592 // not enough input or output--restore pointers and return | |
593 c = z.avail_in - n; c = (k >> 3)<c ? k >> 3: c; n += c; p -= c; k -= c << 3; | |
594 s.bitb = b; s.bitk = k; | |
595 z.avail_in = n; z.total_in += p - z.next_in_index; z.next_in_index = p; | |
596 s.write = q; | |
597 return Z_OK; | |
598 } | |
599 } | 610 } |