Rebasing the Inflater support on jzlib which, unlike GNU ClassPath, has correct implementation of Huffman code. Making the implementation more easily testable by turning Inflater and ZipInputStream into pure delegates. Current implementation is going to need proper long support.
1 /* -*-mode:java; c-basic-offset:2; -*- */
3 Copyright (c) 2011 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 org.apidesign.bck2brwsr.emul.zip;
37 import org.apidesign.bck2brwsr.emul.lang.System;
39 final class InfBlocks{
40 static final private int MANY=1440;
42 // And'ing with mask[n] masks the lower n bits
43 static final private int[] inflate_mask = {
44 0x00000000, 0x00000001, 0x00000003, 0x00000007, 0x0000000f,
45 0x0000001f, 0x0000003f, 0x0000007f, 0x000000ff, 0x000001ff,
46 0x000003ff, 0x000007ff, 0x00000fff, 0x00001fff, 0x00003fff,
47 0x00007fff, 0x0000ffff
50 // Table for deflate from PKZIP's appnote.txt.
51 static final int[] border = { // Order of the bit length code lengths
52 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15
55 static final private int Z_OK=0;
56 static final private int Z_STREAM_END=1;
57 static final private int Z_NEED_DICT=2;
58 static final private int Z_ERRNO=-1;
59 static final private int Z_STREAM_ERROR=-2;
60 static final private int Z_DATA_ERROR=-3;
61 static final private int Z_MEM_ERROR=-4;
62 static final private int Z_BUF_ERROR=-5;
63 static final private int Z_VERSION_ERROR=-6;
65 static final private int TYPE=0; // get type bits (3, including end bit)
66 static final private int LENS=1; // get lengths for stored
67 static final private int STORED=2;// processing stored block
68 static final private int TABLE=3; // get table lengths
69 static final private int BTREE=4; // get bit lengths tree for a dynamic block
70 static final private int DTREE=5; // get length, distance trees for a dynamic block
71 static final private int CODES=6; // processing fixed or dynamic block
72 static final private int DRY=7; // output remaining window bytes
73 static final private int DONE=8; // finished last block, done
74 static final private int BAD=9; // ot a data error--stuck here
76 int mode; // current inflate_block mode
78 int left; // if STORED, bytes left to copy
80 int table; // table lengths (14 bits)
81 int index; // index into blens (or border)
82 int[] blens; // bit lengths of codes
83 int[] bb=new int[1]; // bit length tree depth
84 int[] tb=new int[1]; // bit length decoding tree
89 int[][] tl=new int[1][];
90 int[][] td=new int[1][];
91 int[] tli=new int[1]; // tl_index
92 int[] tdi=new int[1]; // td_index
94 private final InfCodes codes; // if CODES, current state
96 int last; // true if this block is the last block
98 // mode independent information
99 int bitk; // bits in bit buffer
100 int bitb; // bit buffer
101 int[] hufts; // single malloc for tree space
102 byte[] window; // sliding window
103 int end; // one byte after sliding window
104 int read; // window read pointer
105 int write; // window write pointer
106 private boolean check;
108 private final InfTree inftree=new InfTree();
110 private final ZStream z;
112 InfBlocks(ZStream z, int w){
114 this.codes=new InfCodes(this.z, this);
115 hufts=new int[MANY*3];
118 this.check = (z.istate.wrap==0) ? false : true;
124 if(mode==BTREE || mode==DTREE){
139 int t; // temporary storage
141 int k; // bits in bit buffer
142 int p; // input data pointer
143 int n; // bytes available there
144 int q; // output window write pointer
145 int m; // bytes to end of window or read pointer
147 // copy input/output information to locals (UPDATE macro restores)
148 {p=z.next_in_index;n=z.avail_in;b=bitb;k=bitk;}
149 {q=write;m=(int)(q<read?read-q-1:end-q);}
151 // process input based on current state
163 z.total_in+=p-z.next_in_index;z.next_in_index=p;
165 return inflate_flush(r);
168 b|=(z.next_in[p++]&0xff)<<k;
177 t = k & 7; // go to byte boundary
180 mode = LENS; // get length of stored block
183 InfTree.inflate_trees_fixed(bl, bd, tl, td, z);
184 codes.init(bl[0], bd[0], tl[0], 0, td[0], 0);
200 z.msg = "invalid block type";
204 z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;
206 return inflate_flush(r);
218 z.total_in+=p-z.next_in_index;z.next_in_index=p;
220 return inflate_flush(r);
223 b|=(z.next_in[p++]&0xff)<<k;
227 if ((((~b) >>> 16) & 0xffff) != (b & 0xffff)){
229 z.msg = "invalid stored block lengths";
233 z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;
235 return inflate_flush(r);
238 b = k = 0; // dump bits
239 mode = left!=0 ? STORED : (last!=0 ? DRY : TYPE);
244 z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;
246 return inflate_flush(r);
251 q=0; m=(int)(q<read?read-q-1:end-q);
256 q=write;m=(int)(q<read?read-q-1:end-q);
258 q=0; m=(int)(q<read?read-q-1:end-q);
262 z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;
264 return inflate_flush(r);
273 System.arraycopy(z.next_in, p, window, q, t);
276 if ((left -= t) != 0)
278 mode = last!=0 ? DRY : TYPE;
289 z.total_in+=p-z.next_in_index;z.next_in_index=p;
291 return inflate_flush(r);
294 b|=(z.next_in[p++]&0xff)<<k;
298 table = t = (b & 0x3fff);
299 if ((t & 0x1f) > 29 || ((t >> 5) & 0x1f) > 29)
302 z.msg = "too many length or distance symbols";
306 z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;
308 return inflate_flush(r);
310 t = 258 + (t & 0x1f) + ((t >> 5) & 0x1f);
311 if(blens==null || blens.length<t){
315 for(int i=0; i<t; i++){blens[i]=0;}
323 while (index < 4 + (table >>> 10)){
331 z.total_in+=p-z.next_in_index;z.next_in_index=p;
333 return inflate_flush(r);
336 b|=(z.next_in[p++]&0xff)<<k;
340 blens[border[index++]] = b&7;
346 blens[border[index++]] = 0;
350 t = inftree.inflate_trees_bits(blens, bb, tb, hufts, z);
353 if (r == Z_DATA_ERROR){
359 z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;
361 return inflate_flush(r);
369 if(!(index < 258 + (t & 0x1f) + ((t >> 5) & 0x1f))){
385 z.total_in+=p-z.next_in_index;z.next_in_index=p;
387 return inflate_flush(r);
390 b|=(z.next_in[p++]&0xff)<<k;
395 //System.err.println("null...");
398 t=hufts[(tb[0]+(b&inflate_mask[t]))*3+1];
399 c=hufts[(tb[0]+(b&inflate_mask[t]))*3+2];
405 else { // c == 16..18
406 i = c == 18 ? 7 : c - 14;
407 j = c == 18 ? 11 : 3;
416 z.total_in+=p-z.next_in_index;z.next_in_index=p;
418 return inflate_flush(r);
421 b|=(z.next_in[p++]&0xff)<<k;
427 j += (b & inflate_mask[i]);
433 if (i + j > 258 + (t & 0x1f) + ((t >> 5) & 0x1f) ||
437 z.msg = "invalid bit length repeat";
441 z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;
443 return inflate_flush(r);
446 c = c == 16 ? blens[i-1] : 0;
457 bl[0] = 9; // must be <= 9 for lookahead assumptions
458 bd[0] = 6; // must be <= 9 for lookahead assumptions
460 t = inftree.inflate_trees_dynamic(257 + (t & 0x1f),
461 1 + ((t >> 5) & 0x1f),
462 blens, bl, bd, tli, tdi, hufts, z);
465 if (t == Z_DATA_ERROR){
472 z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;
474 return inflate_flush(r);
476 codes.init(bl[0], bd[0], hufts, tli[0], hufts, tdi[0]);
481 z.avail_in=n; z.total_in+=p-z.next_in_index;z.next_in_index=p;
484 if ((r = codes.proc(r)) != Z_STREAM_END){
485 return inflate_flush(r);
490 p=z.next_in_index; n=z.avail_in;b=bitb;k=bitk;
491 q=write;m=(int)(q<read?read-q-1:end-q);
501 q=write; m=(int)(q<read?read-q-1:end-q);
504 z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;
506 return inflate_flush(r);
513 z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;
515 return inflate_flush(r);
520 z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;
522 return inflate_flush(r);
528 z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p;
530 return inflate_flush(r);
542 void set_dictionary(byte[] d, int start, int n){
543 System.arraycopy(d, start, window, 0, n);
547 // Returns true if inflate is currently at the end of a block generated
548 // by Z_SYNC_FLUSH or Z_FULL_FLUSH.
550 return mode == LENS ? 1 : 0;
553 // copy as much as possible from the sliding window to the output area
554 int inflate_flush(int r){
559 // local copies of source and destination pointers
560 p = z.next_out_index;
563 // compute number of bytes to copy as far as end of window
564 n = (int)((q <= write ? write : end) - q);
565 if(n > z.avail_out) n = z.avail_out;
566 if(n!=0 && r == Z_BUF_ERROR) r = Z_OK;
572 // update check information
574 z.adler.update(window, q, n);
577 // copy as far as end of window
578 System.arraycopy(window, q, z.next_out, p, n);
582 // see if more to copy at beginning of window
589 // compute bytes to copy
591 if (n > z.avail_out) n = z.avail_out;
592 if (n!=0 && r == Z_BUF_ERROR) r = Z_OK;
598 // update check information
600 z.adler.update(window, q, n);
604 System.arraycopy(window, q, z.next_out, p, n);
610 z.next_out_index = p;