jaroslav@694: /* -*-mode:java; c-basic-offset:2; -*- */ jaroslav@694: /* jaroslav@694: Copyright (c) 2011 ymnk, JCraft,Inc. All rights reserved. jaroslav@694: jaroslav@694: Redistribution and use in source and binary forms, with or without jaroslav@694: modification, are permitted provided that the following conditions are met: jaroslav@694: jaroslav@694: 1. Redistributions of source code must retain the above copyright notice, jaroslav@694: this list of conditions and the following disclaimer. jaroslav@694: jaroslav@694: 2. Redistributions in binary form must reproduce the above copyright jaroslav@694: notice, this list of conditions and the following disclaimer in jaroslav@694: the documentation and/or other materials provided with the distribution. jaroslav@694: jaroslav@694: 3. The names of the authors may not be used to endorse or promote products jaroslav@694: derived from this software without specific prior written permission. jaroslav@694: jaroslav@694: THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, jaroslav@694: INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND jaroslav@694: FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT, jaroslav@694: INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT, jaroslav@694: INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT jaroslav@694: LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, jaroslav@694: OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF jaroslav@694: LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING jaroslav@694: NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, jaroslav@694: EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. jaroslav@694: */ jaroslav@694: /* jaroslav@694: * This program is based on zlib-1.1.3, so all credit should go authors jaroslav@694: * Jean-loup Gailly(jloup@gzip.org) and Mark Adler(madler@alumni.caltech.edu) jaroslav@694: * and contributors of zlib. jaroslav@694: */ jaroslav@694: jaroslav@694: package org.apidesign.bck2brwsr.emul.zip; jaroslav@694: jaroslav@694: import org.apidesign.bck2brwsr.emul.lang.System; jaroslav@694: jaroslav@694: final class InfBlocks{ jaroslav@694: static final private int MANY=1440; jaroslav@694: jaroslav@694: // And'ing with mask[n] masks the lower n bits jaroslav@694: static final private int[] inflate_mask = { jaroslav@694: 0x00000000, 0x00000001, 0x00000003, 0x00000007, 0x0000000f, jaroslav@694: 0x0000001f, 0x0000003f, 0x0000007f, 0x000000ff, 0x000001ff, jaroslav@694: 0x000003ff, 0x000007ff, 0x00000fff, 0x00001fff, 0x00003fff, jaroslav@694: 0x00007fff, 0x0000ffff jaroslav@694: }; jaroslav@694: jaroslav@694: // Table for deflate from PKZIP's appnote.txt. jaroslav@694: static final int[] border = { // Order of the bit length code lengths jaroslav@694: 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15 jaroslav@694: }; jaroslav@694: jaroslav@694: static final private int Z_OK=0; jaroslav@694: static final private int Z_STREAM_END=1; jaroslav@694: static final private int Z_NEED_DICT=2; jaroslav@694: static final private int Z_ERRNO=-1; jaroslav@694: static final private int Z_STREAM_ERROR=-2; jaroslav@694: static final private int Z_DATA_ERROR=-3; jaroslav@694: static final private int Z_MEM_ERROR=-4; jaroslav@694: static final private int Z_BUF_ERROR=-5; jaroslav@694: static final private int Z_VERSION_ERROR=-6; jaroslav@694: jaroslav@694: static final private int TYPE=0; // get type bits (3, including end bit) jaroslav@694: static final private int LENS=1; // get lengths for stored jaroslav@694: static final private int STORED=2;// processing stored block jaroslav@694: static final private int TABLE=3; // get table lengths jaroslav@694: static final private int BTREE=4; // get bit lengths tree for a dynamic block jaroslav@694: static final private int DTREE=5; // get length, distance trees for a dynamic block jaroslav@694: static final private int CODES=6; // processing fixed or dynamic block jaroslav@694: static final private int DRY=7; // output remaining window bytes jaroslav@694: static final private int DONE=8; // finished last block, done jaroslav@694: static final private int BAD=9; // ot a data error--stuck here jaroslav@694: jaroslav@694: int mode; // current inflate_block mode jaroslav@694: jaroslav@694: int left; // if STORED, bytes left to copy jaroslav@694: jaroslav@694: int table; // table lengths (14 bits) jaroslav@694: int index; // index into blens (or border) jaroslav@694: int[] blens; // bit lengths of codes jaroslav@694: int[] bb=new int[1]; // bit length tree depth jaroslav@694: int[] tb=new int[1]; // bit length decoding tree jaroslav@694: jaroslav@694: int[] bl=new int[1]; jaroslav@694: int[] bd=new int[1]; jaroslav@694: jaroslav@694: int[][] tl=new int[1][]; jaroslav@694: int[][] td=new int[1][]; jaroslav@694: int[] tli=new int[1]; // tl_index jaroslav@694: int[] tdi=new int[1]; // td_index jaroslav@694: jaroslav@694: private final InfCodes codes; // if CODES, current state jaroslav@694: jaroslav@694: int last; // true if this block is the last block jaroslav@694: jaroslav@694: // mode independent information jaroslav@694: int bitk; // bits in bit buffer jaroslav@694: int bitb; // bit buffer jaroslav@694: int[] hufts; // single malloc for tree space jaroslav@694: byte[] window; // sliding window jaroslav@694: int end; // one byte after sliding window jaroslav@694: int read; // window read pointer jaroslav@694: int write; // window write pointer jaroslav@694: private boolean check; jaroslav@694: jaroslav@694: private final InfTree inftree=new InfTree(); jaroslav@694: jaroslav@694: private final ZStream z; jaroslav@694: jaroslav@694: InfBlocks(ZStream z, int w){ jaroslav@694: this.z=z; jaroslav@694: this.codes=new InfCodes(this.z, this); jaroslav@694: hufts=new int[MANY*3]; jaroslav@694: window=new byte[w]; jaroslav@694: end=w; jaroslav@694: this.check = (z.istate.wrap==0) ? false : true; jaroslav@694: mode = TYPE; jaroslav@694: reset(); jaroslav@694: } jaroslav@694: jaroslav@694: void reset(){ jaroslav@694: if(mode==BTREE || mode==DTREE){ jaroslav@694: } jaroslav@694: if(mode==CODES){ jaroslav@694: codes.free(z); jaroslav@694: } jaroslav@694: mode=TYPE; jaroslav@694: bitk=0; jaroslav@694: bitb=0; jaroslav@694: read=write=0; jaroslav@694: if(check){ jaroslav@694: z.adler.reset(); jaroslav@694: } jaroslav@694: } jaroslav@694: jaroslav@694: int proc(int r){ jaroslav@694: int t; // temporary storage jaroslav@694: int b; // bit buffer jaroslav@694: int k; // bits in bit buffer jaroslav@694: int p; // input data pointer jaroslav@694: int n; // bytes available there jaroslav@694: int q; // output window write pointer jaroslav@694: int m; // bytes to end of window or read pointer jaroslav@694: jaroslav@694: // copy input/output information to locals (UPDATE macro restores) jaroslav@694: {p=z.next_in_index;n=z.avail_in;b=bitb;k=bitk;} jaroslav@694: {q=write;m=(int)(q>> 1){ jaroslav@694: case 0: // stored jaroslav@694: {b>>>=(3);k-=(3);} jaroslav@694: t = k & 7; // go to byte boundary jaroslav@694: jaroslav@694: {b>>>=(t);k-=(t);} jaroslav@694: mode = LENS; // get length of stored block jaroslav@694: break; jaroslav@694: case 1: // fixed jaroslav@694: InfTree.inflate_trees_fixed(bl, bd, tl, td, z); jaroslav@694: codes.init(bl[0], bd[0], tl[0], 0, td[0], 0); jaroslav@694: jaroslav@694: {b>>>=(3);k-=(3);} jaroslav@694: jaroslav@694: mode = CODES; jaroslav@694: break; jaroslav@694: case 2: // dynamic jaroslav@694: jaroslav@694: {b>>>=(3);k-=(3);} jaroslav@694: jaroslav@694: mode = TABLE; jaroslav@694: break; jaroslav@694: case 3: // illegal jaroslav@694: jaroslav@694: {b>>>=(3);k-=(3);} jaroslav@694: mode = BAD; jaroslav@694: z.msg = "invalid block type"; jaroslav@694: r = Z_DATA_ERROR; jaroslav@694: jaroslav@694: bitb=b; bitk=k; jaroslav@694: z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p; jaroslav@694: write=q; jaroslav@694: return inflate_flush(r); jaroslav@694: } jaroslav@694: break; jaroslav@694: case LENS: jaroslav@694: jaroslav@694: while(k<(32)){ jaroslav@694: if(n!=0){ jaroslav@694: r=Z_OK; jaroslav@694: } jaroslav@694: else{ jaroslav@694: bitb=b; bitk=k; jaroslav@694: z.avail_in=n; jaroslav@694: z.total_in+=p-z.next_in_index;z.next_in_index=p; jaroslav@694: write=q; jaroslav@694: return inflate_flush(r); jaroslav@694: }; jaroslav@694: n--; jaroslav@694: b|=(z.next_in[p++]&0xff)<>> 16) & 0xffff) != (b & 0xffff)){ jaroslav@694: mode = BAD; jaroslav@694: z.msg = "invalid stored block lengths"; jaroslav@694: r = Z_DATA_ERROR; jaroslav@694: jaroslav@694: bitb=b; bitk=k; jaroslav@694: z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p; jaroslav@694: write=q; jaroslav@694: return inflate_flush(r); jaroslav@694: } jaroslav@694: left = (b & 0xffff); jaroslav@694: b = k = 0; // dump bits jaroslav@694: mode = left!=0 ? STORED : (last!=0 ? DRY : TYPE); jaroslav@694: break; jaroslav@694: case STORED: jaroslav@694: if (n == 0){ jaroslav@694: bitb=b; bitk=k; jaroslav@694: z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p; jaroslav@694: write=q; jaroslav@694: return inflate_flush(r); jaroslav@694: } jaroslav@694: jaroslav@694: if(m==0){ jaroslav@694: if(q==end&&read!=0){ jaroslav@694: q=0; m=(int)(qn) t = n; jaroslav@694: if(t>m) t = m; jaroslav@694: System.arraycopy(z.next_in, p, window, q, t); jaroslav@694: p += t; n -= t; jaroslav@694: q += t; m -= t; jaroslav@694: if ((left -= t) != 0) jaroslav@694: break; jaroslav@694: mode = last!=0 ? DRY : TYPE; jaroslav@694: break; jaroslav@694: case TABLE: jaroslav@694: jaroslav@694: while(k<(14)){ jaroslav@694: if(n!=0){ jaroslav@694: r=Z_OK; jaroslav@694: } jaroslav@694: else{ jaroslav@694: bitb=b; bitk=k; jaroslav@694: z.avail_in=n; jaroslav@694: z.total_in+=p-z.next_in_index;z.next_in_index=p; jaroslav@694: write=q; jaroslav@694: return inflate_flush(r); jaroslav@694: }; jaroslav@694: n--; jaroslav@694: b|=(z.next_in[p++]&0xff)< 29 || ((t >> 5) & 0x1f) > 29) jaroslav@694: { jaroslav@694: mode = BAD; jaroslav@694: z.msg = "too many length or distance symbols"; jaroslav@694: r = Z_DATA_ERROR; jaroslav@694: jaroslav@694: bitb=b; bitk=k; jaroslav@694: z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p; jaroslav@694: write=q; jaroslav@694: return inflate_flush(r); jaroslav@694: } jaroslav@694: t = 258 + (t & 0x1f) + ((t >> 5) & 0x1f); jaroslav@694: if(blens==null || blens.length>>=(14);k-=(14);} jaroslav@694: jaroslav@694: index = 0; jaroslav@694: mode = BTREE; jaroslav@694: case BTREE: jaroslav@694: while (index < 4 + (table >>> 10)){ jaroslav@694: while(k<(3)){ jaroslav@694: if(n!=0){ jaroslav@694: r=Z_OK; jaroslav@694: } jaroslav@694: else{ jaroslav@694: bitb=b; bitk=k; jaroslav@694: z.avail_in=n; jaroslav@694: z.total_in+=p-z.next_in_index;z.next_in_index=p; jaroslav@694: write=q; jaroslav@694: return inflate_flush(r); jaroslav@694: }; jaroslav@694: n--; jaroslav@694: b|=(z.next_in[p++]&0xff)<>>=(3);k-=(3);} jaroslav@694: } jaroslav@694: jaroslav@694: while(index < 19){ jaroslav@694: blens[border[index++]] = 0; jaroslav@694: } jaroslav@694: jaroslav@694: bb[0] = 7; jaroslav@694: t = inftree.inflate_trees_bits(blens, bb, tb, hufts, z); jaroslav@694: if (t != Z_OK){ jaroslav@694: r = t; jaroslav@694: if (r == Z_DATA_ERROR){ jaroslav@694: blens=null; jaroslav@694: mode = BAD; jaroslav@694: } jaroslav@694: jaroslav@694: bitb=b; bitk=k; jaroslav@694: z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p; jaroslav@694: write=q; jaroslav@694: return inflate_flush(r); jaroslav@694: } jaroslav@694: jaroslav@694: index = 0; jaroslav@694: mode = DTREE; jaroslav@694: case DTREE: jaroslav@694: while (true){ jaroslav@694: t = table; jaroslav@694: if(!(index < 258 + (t & 0x1f) + ((t >> 5) & 0x1f))){ jaroslav@694: break; jaroslav@694: } jaroslav@694: jaroslav@694: int[] h; jaroslav@694: int i, j, c; jaroslav@694: jaroslav@694: t = bb[0]; jaroslav@694: jaroslav@694: while(k<(t)){ jaroslav@694: if(n!=0){ jaroslav@694: r=Z_OK; jaroslav@694: } jaroslav@694: else{ jaroslav@694: bitb=b; bitk=k; jaroslav@694: z.avail_in=n; jaroslav@694: z.total_in+=p-z.next_in_index;z.next_in_index=p; jaroslav@694: write=q; jaroslav@694: return inflate_flush(r); jaroslav@694: }; jaroslav@694: n--; jaroslav@694: b|=(z.next_in[p++]&0xff)<>>=(t);k-=(t); jaroslav@694: blens[index++] = c; jaroslav@694: } jaroslav@694: else { // c == 16..18 jaroslav@694: i = c == 18 ? 7 : c - 14; jaroslav@694: j = c == 18 ? 11 : 3; jaroslav@694: jaroslav@694: while(k<(t+i)){ jaroslav@694: if(n!=0){ jaroslav@694: r=Z_OK; jaroslav@694: } jaroslav@694: else{ jaroslav@694: bitb=b; bitk=k; jaroslav@694: z.avail_in=n; jaroslav@694: z.total_in+=p-z.next_in_index;z.next_in_index=p; jaroslav@694: write=q; jaroslav@694: return inflate_flush(r); jaroslav@694: }; jaroslav@694: n--; jaroslav@694: b|=(z.next_in[p++]&0xff)<>>=(t);k-=(t); jaroslav@694: jaroslav@694: j += (b & inflate_mask[i]); jaroslav@694: jaroslav@694: b>>>=(i);k-=(i); jaroslav@694: jaroslav@694: i = index; jaroslav@694: t = table; jaroslav@694: if (i + j > 258 + (t & 0x1f) + ((t >> 5) & 0x1f) || jaroslav@694: (c == 16 && i < 1)){ jaroslav@694: blens=null; jaroslav@694: mode = BAD; jaroslav@694: z.msg = "invalid bit length repeat"; jaroslav@694: r = Z_DATA_ERROR; jaroslav@694: jaroslav@694: bitb=b; bitk=k; jaroslav@694: z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p; jaroslav@694: write=q; jaroslav@694: return inflate_flush(r); jaroslav@694: } jaroslav@694: jaroslav@694: c = c == 16 ? blens[i-1] : 0; jaroslav@694: do{ jaroslav@694: blens[i++] = c; jaroslav@694: } jaroslav@694: while (--j!=0); jaroslav@694: index = i; jaroslav@694: } jaroslav@694: } jaroslav@694: jaroslav@694: tb[0]=-1; jaroslav@694: { jaroslav@694: bl[0] = 9; // must be <= 9 for lookahead assumptions jaroslav@694: bd[0] = 6; // must be <= 9 for lookahead assumptions jaroslav@694: t = table; jaroslav@694: t = inftree.inflate_trees_dynamic(257 + (t & 0x1f), jaroslav@694: 1 + ((t >> 5) & 0x1f), jaroslav@694: blens, bl, bd, tli, tdi, hufts, z); jaroslav@694: jaroslav@694: if (t != Z_OK){ jaroslav@694: if (t == Z_DATA_ERROR){ jaroslav@694: blens=null; jaroslav@694: mode = BAD; jaroslav@694: } jaroslav@694: r = t; jaroslav@694: jaroslav@694: bitb=b; bitk=k; jaroslav@694: z.avail_in=n;z.total_in+=p-z.next_in_index;z.next_in_index=p; jaroslav@694: write=q; jaroslav@694: return inflate_flush(r); jaroslav@694: } jaroslav@694: codes.init(bl[0], bd[0], hufts, tli[0], hufts, tdi[0]); jaroslav@694: } jaroslav@694: mode = CODES; jaroslav@694: case CODES: jaroslav@694: bitb=b; bitk=k; jaroslav@694: z.avail_in=n; z.total_in+=p-z.next_in_index;z.next_in_index=p; jaroslav@694: write=q; jaroslav@694: jaroslav@694: if ((r = codes.proc(r)) != Z_STREAM_END){ jaroslav@694: return inflate_flush(r); jaroslav@694: } jaroslav@694: r = Z_OK; jaroslav@694: codes.free(z); jaroslav@694: jaroslav@694: p=z.next_in_index; n=z.avail_in;b=bitb;k=bitk; jaroslav@694: q=write;m=(int)(q z.avail_out) n = z.avail_out; jaroslav@694: if(n!=0 && r == Z_BUF_ERROR) r = Z_OK; jaroslav@694: jaroslav@694: // update counters jaroslav@694: z.avail_out -= n; jaroslav@694: z.total_out += n; jaroslav@694: jaroslav@694: // update check information jaroslav@694: if(check && n>0){ jaroslav@694: z.adler.update(window, q, n); jaroslav@694: } jaroslav@694: jaroslav@694: // copy as far as end of window jaroslav@694: System.arraycopy(window, q, z.next_out, p, n); jaroslav@694: p += n; jaroslav@694: q += n; jaroslav@694: jaroslav@694: // see if more to copy at beginning of window jaroslav@694: if (q == end){ jaroslav@694: // wrap pointers jaroslav@694: q = 0; jaroslav@694: if (write == end) jaroslav@694: write = 0; jaroslav@694: jaroslav@694: // compute bytes to copy jaroslav@694: n = write - q; jaroslav@694: if (n > z.avail_out) n = z.avail_out; jaroslav@694: if (n!=0 && r == Z_BUF_ERROR) r = Z_OK; jaroslav@694: jaroslav@694: // update counters jaroslav@694: z.avail_out -= n; jaroslav@694: z.total_out += n; jaroslav@694: jaroslav@694: // update check information jaroslav@694: if(check && n>0){ jaroslav@694: z.adler.update(window, q, n); jaroslav@694: } jaroslav@694: jaroslav@694: // copy jaroslav@694: System.arraycopy(window, q, z.next_out, p, n); jaroslav@694: p += n; jaroslav@694: q += n; jaroslav@694: } jaroslav@694: jaroslav@694: // update pointers jaroslav@694: z.next_out_index = p; jaroslav@694: read = q; jaroslav@694: jaroslav@694: // done jaroslav@694: return r; jaroslav@694: } jaroslav@694: }