comparison src/com/jcraft/jzlib/InfBlocks.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
1 /* -*-mode:java; c-basic-offset:2; -*- */ 1 /* -*-mode:java; c-basic-offset:2; -*- */
2 /* 2 /*
3 Copyright (c) 2000,2001,2002,2003 ymnk, JCraft,Inc. All rights reserved. 3 Copyright (c) 2011 ymnk, JCraft,Inc. All rights reserved.
4 4
5 Redistribution and use in source and binary forms, with or without 5 Redistribution and use in source and binary forms, with or without
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 InfBlocks { 37 final class InfBlocks{
38 static final private int MANY = 1440; 38 static final private int MANY=1440;
39 39
40 // And'ing with mask[n] masks the lower n bits 40 // And'ing with mask[n] masks the lower n bits
41 static final private int[] inflate_mask = { 41 static final private int[] inflate_mask = {
42 0x00000000, 0x00000001, 0x00000003, 0x00000007, 0x0000000f, 42 0x00000000, 0x00000001, 0x00000003, 0x00000007, 0x0000000f,
43 0x0000001f, 0x0000003f, 0x0000007f, 0x000000ff, 0x000001ff, 43 0x0000001f, 0x0000003f, 0x0000007f, 0x000000ff, 0x000001ff,
44 0x000003ff, 0x000007ff, 0x00000fff, 0x00001fff, 0x00003fff, 44 0x000003ff, 0x000007ff, 0x00000fff, 0x00001fff, 0x00003fff,
45 0x00007fff, 0x0000ffff 45 0x00007fff, 0x0000ffff
46 }; 46 };
47 47
48 // Table for deflate from PKZIP's appnote.txt. 48 // Table for deflate from PKZIP's appnote.txt.
49 static final int[] border = { // Order of the bit length code lengths 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 50 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15
51 }; 51 };
52 52
53 static final private int Z_OK = 0; 53 static final private int Z_OK=0;
54 static final private int Z_STREAM_END = 1; 54 static final private int Z_STREAM_END=1;
55 static final private int Z_NEED_DICT = 2; 55 static final private int Z_NEED_DICT=2;
56 static final private int Z_ERRNO = -1; 56 static final private int Z_ERRNO=-1;
57 static final private int Z_STREAM_ERROR = -2; 57 static final private int Z_STREAM_ERROR=-2;
58 static final private int Z_DATA_ERROR = -3; 58 static final private int Z_DATA_ERROR=-3;
59 static final private int Z_MEM_ERROR = -4; 59 static final private int Z_MEM_ERROR=-4;
60 static final private int Z_BUF_ERROR = -5; 60 static final private int Z_BUF_ERROR=-5;
61 static final private int Z_VERSION_ERROR = -6; 61 static final private int Z_VERSION_ERROR=-6;
62 62
63 static final private int TYPE = 0; // get type bits (3, including end bit) 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 64 static final private int LENS=1; // get lengths for stored
65 static final private int STORED = 2; // processing stored block 65 static final private int STORED=2;// processing stored block
66 static final private int TABLE = 3; // get table lengths 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 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 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 69 static final private int CODES=6; // processing fixed or dynamic block
70 static final private int DRY = 7; // output remaining window bytes 70 static final private int DRY=7; // output remaining window bytes
71 static final private int DONE = 8; // finished last block, done 71 static final private int DONE=8; // finished last block, done
72 static final private int BAD = 9; // ot a data error--stuck here 72 static final private int BAD=9; // ot a data error--stuck here
73 73
74 int mode; // current inflate_block mode 74 int mode; // current inflate_block mode
75 75
76 int left; // if STORED, bytes left to copy 76 int left; // if STORED, bytes left to copy
77 77
78 int table; // table lengths (14 bits) 78 int table; // table lengths (14 bits)
79 int index; // index into blens (or border) 79 int index; // index into blens (or border)
80 int[] blens; // bit lengths of codes 80 int[] blens; // bit lengths of codes
81 int[] bb = new int[1]; // bit length tree depth 81 int[] bb=new int[1]; // bit length tree depth
82 int[] tb = new int[1]; // bit length decoding tree 82 int[] tb=new int[1]; // bit length decoding tree
83 83
84 InfCodes codes = new InfCodes(); // if CODES, current state 84 int[] bl=new int[1];
85 85 int[] bd=new int[1];
86 int last; // true if this block is the last block 86
87 87 int[][] tl=new int[1][];
88 // mode independent information 88 int[][] td=new int[1][];
89 int bitk; // bits in bit buffer 89 int[] tli=new int[1]; // tl_index
90 int bitb; // bit buffer 90 int[] tdi=new int[1]; // td_index
91 int[] hufts; // single malloc for tree space 91
92 byte[] window; // sliding window 92 private final InfCodes codes; // if CODES, current state
93 int end; // one byte after sliding window 93
94 int read; // window read pointer 94 int last; // true if this block is the last block
95 int write; // window write pointer 95
96 Object checkfn; // check function 96 // mode independent information
97 long check; // check on output 97 int bitk; // bits in bit buffer
98 98 int bitb; // bit buffer
99 InfTree inftree = new InfTree(); 99 int[] hufts; // single malloc for tree space
100 100 byte[] window; // sliding window
101 InfBlocks(ZStream z, Object checkfn, int w) { 101 int end; // one byte after sliding window
102 hufts = new int[MANY * 3]; 102 int read; // window read pointer
103 window = new byte[w]; 103 int write; // window write pointer
104 end = w; 104 private boolean check;
105 this.checkfn = checkfn; 105
106 mode = TYPE; 106 private final InfTree inftree=new InfTree();
107 reset(z, null); 107
108 private final ZStream z;
109
110 InfBlocks(ZStream z, int w){
111 this.z=z;
112 this.codes=new InfCodes(this.z, this);
113 hufts=new int[MANY*3];
114 window=new byte[w];
115 end=w;
116 this.check = (z.istate.wrap==0) ? false : true;
117 mode = TYPE;
118 reset();
119 }
120
121 void reset(){
122 if(mode==BTREE || mode==DTREE){
108 } 123 }
109 124 if(mode==CODES){
110 void reset(ZStream z, long[] c) { 125 codes.free(z);
111 if (c != null) c[0] = check;
112
113 if (mode == BTREE || mode == DTREE) {
114 }
115
116 if (mode == CODES) {
117 codes.free(z);
118 }
119
120 mode = TYPE;
121 bitk = 0;
122 bitb = 0;
123 read = write = 0;
124
125 if (checkfn != null)
126 z.adler = check = z._adler.adler32(0L, null, 0, 0);
127 } 126 }
128 127 mode=TYPE;
129 int proc(ZStream z, int r) { 128 bitk=0;
130 int t; // temporary storage 129 bitb=0;
131 int b; // bit buffer 130 read=write=0;
132 int k; // bits in bit buffer 131 if(check){
133 int p; // input data pointer 132 z.adler.reset();
134 int n; // bytes available there
135 int q; // output window write pointer
136 int m; // bytes to end of window or read pointer
137 // copy input/output information to locals (UPDATE macro restores)
138 {p = z.next_in_index; n = z.avail_in; b = bitb; k = bitk;}
139 {q = write; m = (int)(q < read ? read - q - 1 : end - q);}
140
141 // process input based on current state
142 while (true) {
143 switch (mode) {
144 case TYPE:
145 while (k < (3)) {
146 if (n != 0) {
147 r = Z_OK;
148 }
149 else {
150 bitb = b; bitk = k;
151 z.avail_in = n;
152 z.total_in += p - z.next_in_index; z.next_in_index = p;
153 write = q;
154 return inflate_flush(z, r);
155 };
156
157 n--;
158
159 b |= (z.next_in[p++] & 0xff) << k;
160
161 k += 8;
162 }
163
164 t = (int)(b & 7);
165 last = t & 1;
166
167 switch (t >>> 1) {
168 case 0: // stored
169 {b >>>= (3); k -= (3);}
170
171 t = k & 7; // go to byte boundary
172 {b >>>= (t); k -= (t);}
173 mode = LENS; // get length of stored block
174 break;
175
176 case 1: { // fixed
177 int[] bl = new int[1];
178 int[] bd = new int[1];
179 int[][] tl = new int[1][];
180 int[][] td = new int[1][];
181 InfTree.inflate_trees_fixed(bl, bd, tl, td, z);
182 codes.init(bl[0], bd[0], tl[0], 0, td[0], 0, z);
183 }
184
185 {b >>>= (3); k -= (3);}
186
187 mode = CODES;
188 break;
189
190 case 2: // dynamic
191 {b >>>= (3); k -= (3);}
192
193 mode = TABLE;
194 break;
195
196 case 3: // illegal
197 {b >>>= (3); k -= (3);}
198
199 mode = BAD;
200 z.msg = "invalid block type";
201 r = Z_DATA_ERROR;
202 bitb = b; bitk = k;
203 z.avail_in = n; z.total_in += p - z.next_in_index; z.next_in_index = p;
204 write = q;
205 return inflate_flush(z, r);
206 }
207
208 break;
209
210 case LENS:
211 while (k < (32)) {
212 if (n != 0) {
213 r = Z_OK;
214 }
215 else {
216 bitb = b; bitk = k;
217 z.avail_in = n;
218 z.total_in += p - z.next_in_index; z.next_in_index = p;
219 write = q;
220 return inflate_flush(z, r);
221 };
222
223 n--;
224
225 b |= (z.next_in[p++] & 0xff) << k;
226
227 k += 8;
228 }
229
230 if ((((~b) >>> 16) & 0xffff) != (b & 0xffff)) {
231 mode = BAD;
232 z.msg = "invalid stored block lengths";
233 r = Z_DATA_ERROR;
234 bitb = b; bitk = k;
235 z.avail_in = n; z.total_in += p - z.next_in_index; z.next_in_index = p;
236 write = q;
237 return inflate_flush(z, r);
238 }
239
240 left = (b & 0xffff);
241 b = k = 0; // dump bits
242 mode = left != 0 ? STORED : (last != 0 ? DRY : TYPE);
243 break;
244
245 case STORED:
246 if (n == 0) {
247 bitb = b; bitk = k;
248 z.avail_in = n; z.total_in += p - z.next_in_index; z.next_in_index = p;
249 write = q;
250 return inflate_flush(z, r);
251 }
252
253 if (m == 0) {
254 if (q == end && read != 0) {
255 q = 0; m = (int)(q < read ? read - q - 1 : end - q);
256 }
257
258 if (m == 0) {
259 write = q;
260 r = inflate_flush(z, r);
261 q = write; m = (int)(q < read ? read - q - 1 : end - q);
262
263 if (q == end && read != 0) {
264 q = 0; m = (int)(q < read ? read - q - 1 : end - q);
265 }
266
267 if (m == 0) {
268 bitb = b; bitk = k;
269 z.avail_in = n; z.total_in += p - z.next_in_index; z.next_in_index = p;
270 write = q;
271 return inflate_flush(z, r);
272 }
273 }
274 }
275
276 r = Z_OK;
277 t = left;
278
279 if (t > n) t = n;
280
281 if (t > m) t = m;
282
283 System.arraycopy(z.next_in, p, window, q, t);
284 p += t; n -= t;
285 q += t; m -= t;
286
287 if ((left -= t) != 0)
288 break;
289
290 mode = last != 0 ? DRY : TYPE;
291 break;
292
293 case TABLE:
294 while (k < (14)) {
295 if (n != 0) {
296 r = Z_OK;
297 }
298 else {
299 bitb = b; bitk = k;
300 z.avail_in = n;
301 z.total_in += p - z.next_in_index; z.next_in_index = p;
302 write = q;
303 return inflate_flush(z, r);
304 };
305
306 n--;
307
308 b |= (z.next_in[p++] & 0xff) << k;
309
310 k += 8;
311 }
312
313 table = t = (b & 0x3fff);
314
315 if ((t & 0x1f) > 29 || ((t >> 5) & 0x1f) > 29) {
316 mode = BAD;
317 z.msg = "too many length or distance symbols";
318 r = Z_DATA_ERROR;
319 bitb = b; bitk = k;
320 z.avail_in = n; z.total_in += p - z.next_in_index; z.next_in_index = p;
321 write = q;
322 return inflate_flush(z, r);
323 }
324
325 t = 258 + (t & 0x1f) + ((t >> 5) & 0x1f);
326
327 if (blens == null || blens.length < t) {
328 blens = new int[t];
329 }
330 else {
331 for (int i = 0; i < t; i++) {blens[i] = 0;}
332 }
333
334 {b >>>= (14); k -= (14);}
335
336 index = 0;
337 mode = BTREE;
338
339 case BTREE:
340 while (index < 4 + (table >>> 10)) {
341 while (k < (3)) {
342 if (n != 0) {
343 r = Z_OK;
344 }
345 else {
346 bitb = b; bitk = k;
347 z.avail_in = n;
348 z.total_in += p - z.next_in_index; z.next_in_index = p;
349 write = q;
350 return inflate_flush(z, r);
351 };
352
353 n--;
354
355 b |= (z.next_in[p++] & 0xff) << k;
356
357 k += 8;
358 }
359
360 blens[border[index++]] = b & 7;
361 {b >>>= (3); k -= (3);}
362 }
363
364 while (index < 19) {
365 blens[border[index++]] = 0;
366 }
367
368 bb[0] = 7;
369 t = inftree.inflate_trees_bits(blens, bb, tb, hufts, z);
370
371 if (t != Z_OK) {
372 r = t;
373
374 if (r == Z_DATA_ERROR) {
375 blens = null;
376 mode = BAD;
377 }
378
379 bitb = b; bitk = k;
380 z.avail_in = n; z.total_in += p - z.next_in_index; z.next_in_index = p;
381 write = q;
382 return inflate_flush(z, r);
383 }
384
385 index = 0;
386 mode = DTREE;
387
388 case DTREE:
389 while (true) {
390 t = table;
391
392 if (!(index < 258 + (t & 0x1f) + ((t >> 5) & 0x1f))) {
393 break;
394 }
395
396 int[] h;
397 int i, j, c;
398 t = bb[0];
399
400 while (k < (t)) {
401 if (n != 0) {
402 r = Z_OK;
403 }
404 else {
405 bitb = b; bitk = k;
406 z.avail_in = n;
407 z.total_in += p - z.next_in_index; z.next_in_index = p;
408 write = q;
409 return inflate_flush(z, r);
410 };
411
412 n--;
413
414 b |= (z.next_in[p++] & 0xff) << k;
415
416 k += 8;
417 }
418
419 if (tb[0] == -1) {
420 //System.err.println("null...");
421 }
422
423 t = hufts[(tb[0] + (b & inflate_mask[t])) * 3 + 1];
424 c = hufts[(tb[0] + (b & inflate_mask[t])) * 3 + 2];
425
426 if (c < 16) {
427 b >>>= (t); k -= (t);
428 blens[index++] = c;
429 }
430 else { // c == 16..18
431 i = c == 18 ? 7 : c - 14;
432 j = c == 18 ? 11 : 3;
433
434 while (k < (t + i)) {
435 if (n != 0) {
436 r = Z_OK;
437 }
438 else {
439 bitb = b; bitk = k;
440 z.avail_in = n;
441 z.total_in += p - z.next_in_index; z.next_in_index = p;
442 write = q;
443 return inflate_flush(z, r);
444 };
445
446 n--;
447
448 b |= (z.next_in[p++] & 0xff) << k;
449
450 k += 8;
451 }
452
453 b >>>= (t); k -= (t);
454 j += (b & inflate_mask[i]);
455 b >>>= (i); k -= (i);
456 i = index;
457 t = table;
458
459 if (i + j > 258 + (t & 0x1f) + ((t >> 5) & 0x1f) ||
460 (c == 16 && i < 1)) {
461 blens = null;
462 mode = BAD;
463 z.msg = "invalid bit length repeat";
464 r = Z_DATA_ERROR;
465 bitb = b; bitk = k;
466 z.avail_in = n; z.total_in += p - z.next_in_index; z.next_in_index = p;
467 write = q;
468 return inflate_flush(z, r);
469 }
470
471 c = c == 16 ? blens[i - 1] : 0;
472
473 do {
474 blens[i++] = c;
475 }
476 while (--j != 0);
477
478 index = i;
479 }
480 }
481
482 tb[0] = -1;
483 {
484 int[] bl = new int[1];
485 int[] bd = new int[1];
486 int[] tl = new int[1];
487 int[] td = new int[1];
488 bl[0] = 9; // must be <= 9 for lookahead assumptions
489 bd[0] = 6; // must be <= 9 for lookahead assumptions
490 t = table;
491 t = inftree.inflate_trees_dynamic(257 + (t & 0x1f),
492 1 + ((t >> 5) & 0x1f),
493 blens, bl, bd, tl, td, hufts, z);
494
495 if (t != Z_OK) {
496 if (t == Z_DATA_ERROR) {
497 blens = null;
498 mode = BAD;
499 }
500
501 r = t;
502 bitb = b; bitk = k;
503 z.avail_in = n; z.total_in += p - z.next_in_index; z.next_in_index = p;
504 write = q;
505 return inflate_flush(z, r);
506 }
507
508 codes.init(bl[0], bd[0], hufts, tl[0], hufts, td[0], z);
509 }
510 mode = CODES;
511
512 case CODES:
513 bitb = b; bitk = k;
514 z.avail_in = n; z.total_in += p - z.next_in_index; z.next_in_index = p;
515 write = q;
516
517 if ((r = codes.proc(this, z, r)) != Z_STREAM_END) {
518 return inflate_flush(z, r);
519 }
520
521 r = Z_OK;
522 codes.free(z);
523 p = z.next_in_index; n = z.avail_in; b = bitb; k = bitk;
524 q = write; m = (int)(q < read ? read - q - 1 : end - q);
525
526 if (last == 0) {
527 mode = TYPE;
528 break;
529 }
530
531 mode = DRY;
532
533 case DRY:
534 write = q;
535 r = inflate_flush(z, r);
536 q = write; m = (int)(q < read ? read - q - 1 : end - q);
537
538 if (read != write) {
539 bitb = b; bitk = k;
540 z.avail_in = n; z.total_in += p - z.next_in_index; z.next_in_index = p;
541 write = q;
542 return inflate_flush(z, r);
543 }
544
545 mode = DONE;
546
547 case DONE:
548 r = Z_STREAM_END;
549 bitb = b; bitk = k;
550 z.avail_in = n; z.total_in += p - z.next_in_index; z.next_in_index = p;
551 write = q;
552 return inflate_flush(z, r);
553
554 case BAD:
555 r = Z_DATA_ERROR;
556 bitb = b; bitk = k;
557 z.avail_in = n; z.total_in += p - z.next_in_index; z.next_in_index = p;
558 write = q;
559 return inflate_flush(z, r);
560
561 default:
562 r = Z_STREAM_ERROR;
563 bitb = b; bitk = k;
564 z.avail_in = n; z.total_in += p - z.next_in_index; z.next_in_index = p;
565 write = q;
566 return inflate_flush(z, r);
567 }
568 }
569 } 133 }
570 134 }
571 void free(ZStream z) { 135
572 reset(z, null); 136 int proc(int r){
573 window = null; 137 int t; // temporary storage
574 hufts = null; 138 int b; // bit buffer
575 //ZFREE(z, s); 139 int k; // bits in bit buffer
140 int p; // input data pointer
141 int n; // bytes available there
142 int q; // output window write pointer
143 int m; // bytes to end of window or read pointer
144
145 // copy input/output information to locals (UPDATE macro restores)
146 {p=z.next_in_index;n=z.avail_in;b=bitb;k=bitk;}
147 {q=write;m=(int)(q<read?read-q-1:end-q);}
148
149 // process input based on current state
150 while(true){
151 switch (mode){
152 case TYPE:
153
154 while(k<(3)){
155 if(n!=0){
156 r=Z_OK;
157 }
158 else{
159 bitb=b; bitk=k;
160 z.avail_in=n;
161 z.total_in+=p-z.next_in_index;z.next_in_index=p;
162 write=q;
163 return inflate_flush(r);
164 };
165 n--;
166 b|=(z.next_in[p++]&0xff)<<k;
167 k+=8;
168 }
169 t = (int)(b & 7);
170 last = t & 1;
171
172 switch (t >>> 1){
173 case 0: // stored
174 {b>>>=(3);k-=(3);}
175 t = k & 7; // go to byte boundary
176
177 {b>>>=(t);k-=(t);}
178 mode = LENS; // get length of stored block
179 break;
180 case 1: // fixed
181 InfTree.inflate_trees_fixed(bl, bd, tl, td, z);
182 codes.init(bl[0], bd[0], tl[0], 0, td[0], 0);
183
184 {b>>>=(3);k-=(3);}
185
186 mode = CODES;
187 break;
188 case 2: // dynamic
189
190 {b>>>=(3);k-=(3);}
191
192 mode = TABLE;
193 break;
194 case 3: // illegal
195
196 {b>>>=(3);k-=(3);}
197 mode = BAD;
198 z.msg = "invalid block type";
199 r = Z_DATA_ERROR;
200
201 bitb=b; bitk=k;
202 z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;
203 write=q;
204 return inflate_flush(r);
205 }
206 break;
207 case LENS:
208
209 while(k<(32)){
210 if(n!=0){
211 r=Z_OK;
212 }
213 else{
214 bitb=b; bitk=k;
215 z.avail_in=n;
216 z.total_in+=p-z.next_in_index;z.next_in_index=p;
217 write=q;
218 return inflate_flush(r);
219 };
220 n--;
221 b|=(z.next_in[p++]&0xff)<<k;
222 k+=8;
223 }
224
225 if ((((~b) >>> 16) & 0xffff) != (b & 0xffff)){
226 mode = BAD;
227 z.msg = "invalid stored block lengths";
228 r = Z_DATA_ERROR;
229
230 bitb=b; bitk=k;
231 z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;
232 write=q;
233 return inflate_flush(r);
234 }
235 left = (b & 0xffff);
236 b = k = 0; // dump bits
237 mode = left!=0 ? STORED : (last!=0 ? DRY : TYPE);
238 break;
239 case STORED:
240 if (n == 0){
241 bitb=b; bitk=k;
242 z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;
243 write=q;
244 return inflate_flush(r);
245 }
246
247 if(m==0){
248 if(q==end&&read!=0){
249 q=0; m=(int)(q<read?read-q-1:end-q);
250 }
251 if(m==0){
252 write=q;
253 r=inflate_flush(r);
254 q=write;m=(int)(q<read?read-q-1:end-q);
255 if(q==end&&read!=0){
256 q=0; m=(int)(q<read?read-q-1:end-q);
257 }
258 if(m==0){
259 bitb=b; bitk=k;
260 z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;
261 write=q;
262 return inflate_flush(r);
263 }
264 }
265 }
266 r=Z_OK;
267
268 t = left;
269 if(t>n) t = n;
270 if(t>m) t = m;
271 System.arraycopy(z.next_in, p, window, q, t);
272 p += t; n -= t;
273 q += t; m -= t;
274 if ((left -= t) != 0)
275 break;
276 mode = last!=0 ? DRY : TYPE;
277 break;
278 case TABLE:
279
280 while(k<(14)){
281 if(n!=0){
282 r=Z_OK;
283 }
284 else{
285 bitb=b; bitk=k;
286 z.avail_in=n;
287 z.total_in+=p-z.next_in_index;z.next_in_index=p;
288 write=q;
289 return inflate_flush(r);
290 };
291 n--;
292 b|=(z.next_in[p++]&0xff)<<k;
293 k+=8;
294 }
295
296 table = t = (b & 0x3fff);
297 if ((t & 0x1f) > 29 || ((t >> 5) & 0x1f) > 29)
298 {
299 mode = BAD;
300 z.msg = "too many length or distance symbols";
301 r = Z_DATA_ERROR;
302
303 bitb=b; bitk=k;
304 z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;
305 write=q;
306 return inflate_flush(r);
307 }
308 t = 258 + (t & 0x1f) + ((t >> 5) & 0x1f);
309 if(blens==null || blens.length<t){
310 blens=new int[t];
311 }
312 else{
313 for(int i=0; i<t; i++){blens[i]=0;}
314 }
315
316 {b>>>=(14);k-=(14);}
317
318 index = 0;
319 mode = BTREE;
320 case BTREE:
321 while (index < 4 + (table >>> 10)){
322 while(k<(3)){
323 if(n!=0){
324 r=Z_OK;
325 }
326 else{
327 bitb=b; bitk=k;
328 z.avail_in=n;
329 z.total_in+=p-z.next_in_index;z.next_in_index=p;
330 write=q;
331 return inflate_flush(r);
332 };
333 n--;
334 b|=(z.next_in[p++]&0xff)<<k;
335 k+=8;
336 }
337
338 blens[border[index++]] = b&7;
339
340 {b>>>=(3);k-=(3);}
341 }
342
343 while(index < 19){
344 blens[border[index++]] = 0;
345 }
346
347 bb[0] = 7;
348 t = inftree.inflate_trees_bits(blens, bb, tb, hufts, z);
349 if (t != Z_OK){
350 r = t;
351 if (r == Z_DATA_ERROR){
352 blens=null;
353 mode = BAD;
354 }
355
356 bitb=b; bitk=k;
357 z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;
358 write=q;
359 return inflate_flush(r);
360 }
361
362 index = 0;
363 mode = DTREE;
364 case DTREE:
365 while (true){
366 t = table;
367 if(!(index < 258 + (t & 0x1f) + ((t >> 5) & 0x1f))){
368 break;
369 }
370
371 int[] h;
372 int i, j, c;
373
374 t = bb[0];
375
376 while(k<(t)){
377 if(n!=0){
378 r=Z_OK;
379 }
380 else{
381 bitb=b; bitk=k;
382 z.avail_in=n;
383 z.total_in+=p-z.next_in_index;z.next_in_index=p;
384 write=q;
385 return inflate_flush(r);
386 };
387 n--;
388 b|=(z.next_in[p++]&0xff)<<k;
389 k+=8;
390 }
391
392 if(tb[0]==-1){
393 //System.err.println("null...");
394 }
395
396 t=hufts[(tb[0]+(b&inflate_mask[t]))*3+1];
397 c=hufts[(tb[0]+(b&inflate_mask[t]))*3+2];
398
399 if (c < 16){
400 b>>>=(t);k-=(t);
401 blens[index++] = c;
402 }
403 else { // c == 16..18
404 i = c == 18 ? 7 : c - 14;
405 j = c == 18 ? 11 : 3;
406
407 while(k<(t+i)){
408 if(n!=0){
409 r=Z_OK;
410 }
411 else{
412 bitb=b; bitk=k;
413 z.avail_in=n;
414 z.total_in+=p-z.next_in_index;z.next_in_index=p;
415 write=q;
416 return inflate_flush(r);
417 };
418 n--;
419 b|=(z.next_in[p++]&0xff)<<k;
420 k+=8;
421 }
422
423 b>>>=(t);k-=(t);
424
425 j += (b & inflate_mask[i]);
426
427 b>>>=(i);k-=(i);
428
429 i = index;
430 t = table;
431 if (i + j > 258 + (t & 0x1f) + ((t >> 5) & 0x1f) ||
432 (c == 16 && i < 1)){
433 blens=null;
434 mode = BAD;
435 z.msg = "invalid bit length repeat";
436 r = Z_DATA_ERROR;
437
438 bitb=b; bitk=k;
439 z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;
440 write=q;
441 return inflate_flush(r);
442 }
443
444 c = c == 16 ? blens[i-1] : 0;
445 do{
446 blens[i++] = c;
447 }
448 while (--j!=0);
449 index = i;
450 }
451 }
452
453 tb[0]=-1;
454 {
455 bl[0] = 9; // must be <= 9 for lookahead assumptions
456 bd[0] = 6; // must be <= 9 for lookahead assumptions
457 t = table;
458 t = inftree.inflate_trees_dynamic(257 + (t & 0x1f),
459 1 + ((t >> 5) & 0x1f),
460 blens, bl, bd, tli, tdi, hufts, z);
461
462 if (t != Z_OK){
463 if (t == Z_DATA_ERROR){
464 blens=null;
465 mode = BAD;
466 }
467 r = t;
468
469 bitb=b; bitk=k;
470 z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;
471 write=q;
472 return inflate_flush(r);
473 }
474 codes.init(bl[0], bd[0], hufts, tli[0], hufts, tdi[0]);
475 }
476 mode = CODES;
477 case CODES:
478 bitb=b; bitk=k;
479 z.avail_in=n; z.total_in+=p-z.next_in_index;z.next_in_index=p;
480 write=q;
481
482 if ((r = codes.proc(r)) != Z_STREAM_END){
483 return inflate_flush(r);
484 }
485 r = Z_OK;
486 codes.free(z);
487
488 p=z.next_in_index; n=z.avail_in;b=bitb;k=bitk;
489 q=write;m=(int)(q<read?read-q-1:end-q);
490
491 if (last==0){
492 mode = TYPE;
493 break;
494 }
495 mode = DRY;
496 case DRY:
497 write=q;
498 r=inflate_flush(r);
499 q=write; m=(int)(q<read?read-q-1:end-q);
500 if (read != write){
501 bitb=b; bitk=k;
502 z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;
503 write=q;
504 return inflate_flush(r);
505 }
506 mode = DONE;
507 case DONE:
508 r = Z_STREAM_END;
509
510 bitb=b; bitk=k;
511 z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;
512 write=q;
513 return inflate_flush(r);
514 case BAD:
515 r = Z_DATA_ERROR;
516
517 bitb=b; bitk=k;
518 z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;
519 write=q;
520 return inflate_flush(r);
521
522 default:
523 r = Z_STREAM_ERROR;
524
525 bitb=b; bitk=k;
526 z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;
527 write=q;
528 return inflate_flush(r);
529 }
576 } 530 }
577 531 }
578 void set_dictionary(byte[] d, int start, int n) { 532
579 System.arraycopy(d, start, window, 0, n); 533 void free(){
580 read = write = n; 534 reset();
535 window=null;
536 hufts=null;
537 //ZFREE(z, s);
538 }
539
540 void set_dictionary(byte[] d, int start, int n){
541 System.arraycopy(d, start, window, 0, n);
542 read = write = n;
543 }
544
545 // Returns true if inflate is currently at the end of a block generated
546 // by Z_SYNC_FLUSH or Z_FULL_FLUSH.
547 int sync_point(){
548 return mode == LENS ? 1 : 0;
549 }
550
551 // copy as much as possible from the sliding window to the output area
552 int inflate_flush(int r){
553 int n;
554 int p;
555 int q;
556
557 // local copies of source and destination pointers
558 p = z.next_out_index;
559 q = read;
560
561 // compute number of bytes to copy as far as end of window
562 n = (int)((q <= write ? write : end) - q);
563 if(n > z.avail_out) n = z.avail_out;
564 if(n!=0 && r == Z_BUF_ERROR) r = Z_OK;
565
566 // update counters
567 z.avail_out -= n;
568 z.total_out += n;
569
570 // update check information
571 if(check && n>0){
572 z.adler.update(window, q, n);
581 } 573 }
582 574
583 // Returns true if inflate is currently at the end of a block generated 575 // copy as far as end of window
584 // by Z_SYNC_FLUSH or Z_FULL_FLUSH. 576 System.arraycopy(window, q, z.next_out, p, n);
585 int sync_point() { 577 p += n;
586 return mode == LENS ? 1 : 0; 578 q += n;
579
580 // see if more to copy at beginning of window
581 if (q == end){
582 // wrap pointers
583 q = 0;
584 if (write == end)
585 write = 0;
586
587 // compute bytes to copy
588 n = write - q;
589 if (n > z.avail_out) n = z.avail_out;
590 if (n!=0 && r == Z_BUF_ERROR) r = Z_OK;
591
592 // update counters
593 z.avail_out -= n;
594 z.total_out += n;
595
596 // update check information
597 if(check && n>0){
598 z.adler.update(window, q, n);
599 }
600
601 // copy
602 System.arraycopy(window, q, z.next_out, p, n);
603 p += n;
604 q += n;
587 } 605 }
588 606
589 // copy as much as possible from the sliding window to the output area 607 // update pointers
590 int inflate_flush(ZStream z, int r) { 608 z.next_out_index = p;
591 int n; 609 read = q;
592 int p; 610
593 int q; 611 // done
594 // local copies of source and destination pointers 612 return r;
595 p = z.next_out_index; 613 }
596 q = read;
597 // compute number of bytes to copy as far as end of window
598 n = (int)((q <= write ? write : end) - q);
599
600 if (n > z.avail_out) n = z.avail_out;
601
602 if (n != 0 && r == Z_BUF_ERROR) r = Z_OK;
603
604 // update counters
605 z.avail_out -= n;
606 z.total_out += n;
607
608 // update check information
609 if (checkfn != null)
610 z.adler = check = z._adler.adler32(check, window, q, n);
611
612 // copy as far as end of window
613 System.arraycopy(window, q, z.next_out, p, n);
614 p += n;
615 q += n;
616
617 // see if more to copy at beginning of window
618 if (q == end) {
619 // wrap pointers
620 q = 0;
621
622 if (write == end)
623 write = 0;
624
625 // compute bytes to copy
626 n = write - q;
627
628 if (n > z.avail_out) n = z.avail_out;
629
630 if (n != 0 && r == Z_BUF_ERROR) r = Z_OK;
631
632 // update counters
633 z.avail_out -= n;
634 z.total_out += n;
635
636 // update check information
637 if (checkfn != null)
638 z.adler = check = z._adler.adler32(check, window, q, n);
639
640 // copy
641 System.arraycopy(window, q, z.next_out, p, n);
642 p += n;
643 q += n;
644 }
645
646 // update pointers
647 z.next_out_index = p;
648 read = q;
649 // done
650 return r;
651 }
652 } 614 }