comparison src/com/jcraft/jzlib/InfCodes.java @ 0:0ce5cc452d02

initial version
author Carl Byington <carl@five-ten-sg.com>
date Thu, 22 May 2014 10:41:19 -0700
parents
children 46c2115ae1c8
comparison
equal deleted inserted replaced
-1:000000000000 0:0ce5cc452d02
1 /* -*-mode:java; c-basic-offset:2; -*- */
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 class InfCodes {
38
39 static final private int[] inflate_mask = {
40 0x00000000, 0x00000001, 0x00000003, 0x00000007, 0x0000000f,
41 0x0000001f, 0x0000003f, 0x0000007f, 0x000000ff, 0x000001ff,
42 0x000003ff, 0x000007ff, 0x00000fff, 0x00001fff, 0x00003fff,
43 0x00007fff, 0x0000ffff
44 };
45
46 static final private int Z_OK = 0;
47 static final private int Z_STREAM_END = 1;
48 static final private int Z_NEED_DICT = 2;
49 static final private int Z_ERRNO = -1;
50 static final private int Z_STREAM_ERROR = -2;
51 static final private int Z_DATA_ERROR = -3;
52 static final private int Z_MEM_ERROR = -4;
53 static final private int Z_BUF_ERROR = -5;
54 static final private int Z_VERSION_ERROR = -6;
55
56 // waiting for "i:"=input,
57 // "o:"=output,
58 // "x:"=nothing
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
61 static final private int LENEXT = 2; // i: getting length extra (have base)
62 static final private int DIST = 3; // i: get distance next
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
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
67 static final private int END = 8; // x: got eob and all data flushed
68 static final private int BADCODE = 9; // x: got error
69
70 int mode; // current inflate_codes mode
71
72 // mode dependent information
73 int len;
74
75 int[] tree; // pointer into tree
76 int tree_index = 0;
77 int need; // bits needed
78
79 int lit;
80
81 // if EXT or COPY, where and how much
82 int get; // bits to get for extra
83 int dist; // distance back to copy from
84
85 byte lbits; // ltree bits decoded per branch
86 byte dbits; // dtree bits decoder per branch
87 int[] ltree; // literal/length/eob tree
88 int ltree_index; // literal/length/eob tree
89 int[] dtree; // distance tree
90 int dtree_index; // distance tree
91
92 InfCodes() {
93 }
94 void init(int bl, int bd,
95 int[] tl, int tl_index,
96 int[] td, int td_index, ZStream z) {
97 mode = START;
98 lbits = (byte)bl;
99 dbits = (byte)bd;
100 ltree = tl;
101 ltree_index = tl_index;
102 dtree = td;
103 dtree_index = td_index;
104 tree = null;
105 }
106
107 int proc(InfBlocks s, ZStream z, int r) {
108 int j; // temporary storage
109 int[] t; // temporary pointer
110 int tindex; // temporary pointer
111 int e; // extra bits or operation
112 int b = 0; // bit buffer
113 int k = 0; // bits in bit buffer
114 int p = 0; // input data pointer
115 int n; // bytes available there
116 int q; // output window write pointer
117 int m; // bytes to end of window or read pointer
118 int f; // pointer to copy strings from
119 // copy input/output information to locals (UPDATE macro restores)
120 p = z.next_in_index; n = z.avail_in; b = s.bitb; k = s.bitk;
121 q = s.write; m = q < s.read ? s.read - q - 1 : s.end - q;
122
123 // process input and output based on current state
124 while (true) {
125 switch (mode) {
126 // waiting for "i:"=input, "o:"=output, "x:"=nothing
127 case START: // x: set up for LEN
128 if (m >= 258 && n >= 10) {
129 s.bitb = b; s.bitk = k;
130 z.avail_in = n; z.total_in += p - z.next_in_index; z.next_in_index = p;
131 s.write = q;
132 r = inflate_fast(lbits, dbits,
133 ltree, ltree_index,
134 dtree, dtree_index,
135 s, z);
136 p = z.next_in_index; n = z.avail_in; b = s.bitb; k = s.bitk;
137 q = s.write; m = q < s.read ? s.read - q - 1 : s.end - q;
138
139 if (r != Z_OK) {
140 mode = r == Z_STREAM_END ? WASH : BADCODE;
141 break;
142 }
143 }
144
145 need = lbits;
146 tree = ltree;
147 tree_index = ltree_index;
148 mode = LEN;
149
150 case LEN: // i: get length/literal/eob next
151 j = need;
152
153 while (k < (j)) {
154 if (n != 0)r = Z_OK;
155 else {
156 s.bitb = b; s.bitk = k;
157 z.avail_in = n; z.total_in += p - z.next_in_index; z.next_in_index = p;
158 s.write = q;
159 return s.inflate_flush(z, r);
160 }
161
162 n--;
163 b |= (z.next_in[p++] & 0xff) << k;
164 k += 8;
165 }
166
167 tindex = (tree_index + (b & inflate_mask[j])) * 3;
168 b >>>= (tree[tindex + 1]);
169 k -= (tree[tindex + 1]);
170 e = tree[tindex];
171
172 if (e == 0) { // literal
173 lit = tree[tindex + 2];
174 mode = LIT;
175 break;
176 }
177
178 if ((e & 16) != 0) { // length
179 get = e & 15;
180 len = tree[tindex + 2];
181 mode = LENEXT;
182 break;
183 }
184
185 if ((e & 64) == 0) { // next table
186 need = e;
187 tree_index = tindex / 3 + tree[tindex + 2];
188 break;
189 }
190
191 if ((e & 32) != 0) { // end of block
192 mode = WASH;
193 break;
194 }
195
196 mode = BADCODE; // invalid code
197 z.msg = "invalid literal/length code";
198 r = Z_DATA_ERROR;
199 s.bitb = b; s.bitk = k;
200 z.avail_in = n; z.total_in += p - z.next_in_index; z.next_in_index = p;
201 s.write = q;
202 return s.inflate_flush(z, r);
203
204 case LENEXT: // i: getting length extra (have base)
205 j = get;
206
207 while (k < (j)) {
208 if (n != 0)r = Z_OK;
209 else {
210 s.bitb = b; s.bitk = k;
211 z.avail_in = n; z.total_in += p - z.next_in_index; z.next_in_index = p;
212 s.write = q;
213 return s.inflate_flush(z, r);
214 }
215
216 n--; b |= (z.next_in[p++] & 0xff) << k;
217 k += 8;
218 }
219
220 len += (b & inflate_mask[j]);
221 b >>= j;
222 k -= j;
223 need = dbits;
224 tree = dtree;
225 tree_index = dtree_index;
226 mode = DIST;
227
228 case DIST: // i: get distance next
229 j = need;
230
231 while (k < (j)) {
232 if (n != 0)r = Z_OK;
233 else {
234 s.bitb = b; s.bitk = k;
235 z.avail_in = n; z.total_in += p - z.next_in_index; z.next_in_index = p;
236 s.write = q;
237 return s.inflate_flush(z, r);
238 }
239
240 n--; b |= (z.next_in[p++] & 0xff) << k;
241 k += 8;
242 }
243
244 tindex = (tree_index + (b & inflate_mask[j])) * 3;
245 b >>= tree[tindex + 1];
246 k -= tree[tindex + 1];
247 e = (tree[tindex]);
248
249 if ((e & 16) != 0) { // distance
250 get = e & 15;
251 dist = tree[tindex + 2];
252 mode = DISTEXT;
253 break;
254 }
255
256 if ((e & 64) == 0) { // next table
257 need = e;
258 tree_index = tindex / 3 + tree[tindex + 2];
259 break;
260 }
261
262 mode = BADCODE; // invalid code
263 z.msg = "invalid distance code";
264 r = Z_DATA_ERROR;
265 s.bitb = b; s.bitk = k;
266 z.avail_in = n; z.total_in += p - z.next_in_index; z.next_in_index = p;
267 s.write = q;
268 return s.inflate_flush(z, r);
269
270 case DISTEXT: // i: getting distance extra
271 j = get;
272
273 while (k < (j)) {
274 if (n != 0)r = Z_OK;
275 else {
276 s.bitb = b; s.bitk = k;
277 z.avail_in = n; z.total_in += p - z.next_in_index; z.next_in_index = p;
278 s.write = q;
279 return s.inflate_flush(z, r);
280 }
281
282 n--; b |= (z.next_in[p++] & 0xff) << k;
283 k += 8;
284 }
285
286 dist += (b & inflate_mask[j]);
287 b >>= j;
288 k -= j;
289 mode = COPY;
290
291 case COPY: // o: copying bytes in window, waiting for space
292 f = q - dist;
293
294 while (f < 0) { // modulo window size-"while" instead
295 f += s.end; // of "if" handles invalid distances
296 }
297
298 while (len != 0) {
299 if (m == 0) {
300 if (q == s.end && s.read != 0) {q = 0; m = q < s.read ? s.read - q - 1 : s.end - q;}
301
302 if (m == 0) {
303 s.write = q; r = s.inflate_flush(z, r);
304 q = s.write; m = q < s.read ? s.read - q - 1 : s.end - q;
305
306 if (q == s.end && s.read != 0) {q = 0; m = q < s.read ? s.read - q - 1 : s.end - q;}
307
308 if (m == 0) {
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 }