jaroslav@694: /* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */ 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: final class JzLibInflater extends ZStream{ jaroslav@694: jaroslav@694: static final private int MAX_WBITS=15; // 32K LZ77 window jaroslav@694: static final private int DEF_WBITS=MAX_WBITS; jaroslav@694: jaroslav@694: public static final int Z_NO_FLUSH=0; jaroslav@694: static final private int Z_PARTIAL_FLUSH=1; jaroslav@694: static final private int Z_SYNC_FLUSH=2; jaroslav@694: static final private int Z_FULL_FLUSH=3; jaroslav@694: static final private int Z_FINISH=4; jaroslav@694: jaroslav@694: static final private int MAX_MEM_LEVEL=9; 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: public JzLibInflater() { jaroslav@694: super(); jaroslav@694: init(); jaroslav@694: } jaroslav@694: jaroslav@694: public JzLibInflater(int w) { jaroslav@694: this(w, false); jaroslav@694: } jaroslav@694: jaroslav@694: public JzLibInflater(int w, boolean nowrap) { jaroslav@694: super(); jaroslav@694: int ret = init(w, nowrap); jaroslav@694: if(ret!=Z_OK) jaroslav@694: throw new IllegalStateException(ret+": "+msg); jaroslav@694: } jaroslav@694: jaroslav@694: private boolean finished = false; jaroslav@694: jaroslav@694: public int init(){ jaroslav@694: return init(DEF_WBITS); jaroslav@694: } jaroslav@694: jaroslav@694: public int init(boolean nowrap){ jaroslav@694: return init(DEF_WBITS, nowrap); jaroslav@694: } jaroslav@694: jaroslav@694: public int init(int w){ jaroslav@694: return init(w, false); jaroslav@694: } jaroslav@694: jaroslav@694: public int init(int w, boolean nowrap){ jaroslav@694: finished = false; jaroslav@694: istate=new Inflate(this); jaroslav@694: return istate.inflateInit(nowrap?-w:w); jaroslav@694: } jaroslav@694: jaroslav@694: public int inflate(int f){ jaroslav@694: if(istate==null) return Z_STREAM_ERROR; jaroslav@694: int ret = istate.inflate(f); jaroslav@694: if(ret == Z_STREAM_END) jaroslav@694: finished = true; jaroslav@694: return ret; jaroslav@694: } jaroslav@694: jaroslav@694: public int end(){ jaroslav@694: finished = true; jaroslav@694: if(istate==null) return Z_STREAM_ERROR; jaroslav@694: int ret=istate.inflateEnd(); jaroslav@694: // istate = null; jaroslav@694: return ret; jaroslav@694: } jaroslav@694: jaroslav@694: public int sync(){ jaroslav@694: if(istate == null) jaroslav@694: return Z_STREAM_ERROR; jaroslav@694: return istate.inflateSync(); jaroslav@694: } jaroslav@694: jaroslav@694: public int syncPoint(){ jaroslav@694: if(istate == null) jaroslav@694: return Z_STREAM_ERROR; jaroslav@694: return istate.inflateSyncPoint(); jaroslav@694: } jaroslav@694: jaroslav@694: public int setDictionary(byte[] dictionary, int dictLength){ jaroslav@694: if(istate == null) jaroslav@694: return Z_STREAM_ERROR; jaroslav@694: return istate.inflateSetDictionary(dictionary, dictLength); jaroslav@694: } jaroslav@694: jaroslav@694: public boolean finished(){ jaroslav@694: return istate.mode==12 /*DONE*/; jaroslav@694: } jaroslav@694: jaroslav@694: public boolean needDict() { jaroslav@694: return istate == null ? false : istate.mode == Inflate.DICT0; jaroslav@694: } jaroslav@694: }