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: import java.io.UnsupportedEncodingException; jaroslav@694: jaroslav@694: /** jaroslav@694: * @see "http://www.ietf.org/rfc/rfc1952.txt" jaroslav@694: */ jaroslav@694: final class GZIPHeader implements Cloneable { jaroslav@694: jaroslav@694: public static final byte OS_MSDOS = (byte) 0x00; jaroslav@694: public static final byte OS_AMIGA = (byte) 0x01; jaroslav@694: public static final byte OS_VMS = (byte) 0x02; jaroslav@694: public static final byte OS_UNIX = (byte) 0x03; jaroslav@694: public static final byte OS_ATARI = (byte) 0x05; jaroslav@694: public static final byte OS_OS2 = (byte) 0x06; jaroslav@694: public static final byte OS_MACOS = (byte) 0x07; jaroslav@694: public static final byte OS_TOPS20 = (byte) 0x0a; jaroslav@694: public static final byte OS_WIN32 = (byte) 0x0b; jaroslav@694: public static final byte OS_VMCMS = (byte) 0x04; jaroslav@694: public static final byte OS_ZSYSTEM = (byte) 0x08; jaroslav@694: public static final byte OS_CPM = (byte) 0x09; jaroslav@694: public static final byte OS_QDOS = (byte) 0x0c; jaroslav@694: public static final byte OS_RISCOS = (byte) 0x0d; jaroslav@694: public static final byte OS_UNKNOWN = (byte) 0xff; jaroslav@694: jaroslav@694: boolean text = false; jaroslav@694: private boolean fhcrc = false; jaroslav@694: long time; jaroslav@694: int xflags; jaroslav@694: int os = 255; jaroslav@694: byte[] extra; jaroslav@694: byte[] name; jaroslav@694: byte[] comment; jaroslav@694: int hcrc; jaroslav@694: long crc; jaroslav@694: boolean done = false; jaroslav@694: long mtime = 0; jaroslav@694: jaroslav@694: public void setModifiedTime(long mtime) { jaroslav@694: this.mtime = mtime; jaroslav@694: } jaroslav@694: jaroslav@694: public long getModifiedTime() { jaroslav@694: return mtime; jaroslav@694: } jaroslav@694: jaroslav@694: public void setOS(int os) { jaroslav@694: if((0<=os && os <=13) || os==255) jaroslav@694: this.os=os; jaroslav@694: else jaroslav@694: throw new IllegalArgumentException("os: "+os); jaroslav@694: } jaroslav@694: jaroslav@694: public int getOS(){ jaroslav@694: return os; jaroslav@694: } jaroslav@694: jaroslav@694: public void setName(String name) { jaroslav@694: try{ jaroslav@694: this.name=name.getBytes("ISO-8859-1"); jaroslav@694: } jaroslav@694: catch(UnsupportedEncodingException e){ jaroslav@694: throw new IllegalArgumentException("name must be in ISO-8859-1 "+name); jaroslav@694: } jaroslav@694: } jaroslav@694: jaroslav@694: public String getName(){ jaroslav@694: if(name==null) return ""; jaroslav@694: try { jaroslav@694: return new String(name, "ISO-8859-1"); jaroslav@694: } jaroslav@694: catch (UnsupportedEncodingException e) { jaroslav@694: throw new IllegalArgumentException(e.toString()); jaroslav@694: } jaroslav@694: } jaroslav@694: jaroslav@694: public void setComment(String comment) { jaroslav@694: try{ jaroslav@694: this.comment=comment.getBytes("ISO-8859-1"); jaroslav@694: } jaroslav@694: catch(UnsupportedEncodingException e){ jaroslav@694: throw new IllegalArgumentException("comment must be in ISO-8859-1 "+name); jaroslav@694: } jaroslav@694: } jaroslav@694: jaroslav@694: public String getComment(){ jaroslav@694: if(comment==null) return ""; jaroslav@694: try { jaroslav@694: return new String(comment, "ISO-8859-1"); jaroslav@694: } jaroslav@694: catch (UnsupportedEncodingException e) { jaroslav@694: throw new IllegalArgumentException(e.toString()); jaroslav@694: } jaroslav@694: } jaroslav@694: jaroslav@694: public void setCRC(long crc){ jaroslav@694: this.crc = crc; jaroslav@694: } jaroslav@694: jaroslav@694: public long getCRC(){ jaroslav@694: return crc; jaroslav@694: } jaroslav@694: /* jaroslav@694: void put(Deflate d){ jaroslav@694: int flag = 0; jaroslav@694: if(text){ jaroslav@694: flag |= 1; // FTEXT jaroslav@694: } jaroslav@694: if(fhcrc){ jaroslav@694: flag |= 2; // FHCRC jaroslav@694: } jaroslav@694: if(extra!=null){ jaroslav@694: flag |= 4; // FEXTRA jaroslav@694: } jaroslav@694: if(name!=null){ jaroslav@694: flag |= 8; // FNAME jaroslav@694: } jaroslav@694: if(comment!=null){ jaroslav@694: flag |= 16; // FCOMMENT jaroslav@694: } jaroslav@694: int xfl = 0; jaroslav@694: if(d.level == JZlib.Z_BEST_SPEED){ jaroslav@694: xfl |= 4; jaroslav@694: } jaroslav@694: else if (d.level == JZlib.Z_BEST_COMPRESSION){ jaroslav@694: xfl |= 2; jaroslav@694: } jaroslav@694: jaroslav@694: d.put_short((short)0x8b1f); // ID1 ID2 jaroslav@694: d.put_byte((byte)8); // CM(Compression Method) jaroslav@694: d.put_byte((byte)flag); jaroslav@694: d.put_byte((byte)mtime); jaroslav@694: d.put_byte((byte)(mtime>>8)); jaroslav@694: d.put_byte((byte)(mtime>>16)); jaroslav@694: d.put_byte((byte)(mtime>>24)); jaroslav@694: d.put_byte((byte)xfl); jaroslav@694: d.put_byte((byte)os); jaroslav@694: jaroslav@694: if(extra!=null){ jaroslav@694: d.put_byte((byte)extra.length); jaroslav@694: d.put_byte((byte)(extra.length>>8)); jaroslav@694: d.put_byte(extra, 0, extra.length); jaroslav@694: } jaroslav@694: jaroslav@694: if(name!=null){ jaroslav@694: d.put_byte(name, 0, name.length); jaroslav@694: d.put_byte((byte)0); jaroslav@694: } jaroslav@694: jaroslav@694: if(comment!=null){ jaroslav@694: d.put_byte(comment, 0, comment.length); jaroslav@694: d.put_byte((byte)0); jaroslav@694: } jaroslav@694: } jaroslav@694: */ jaroslav@694: @Override jaroslav@694: public Object clone() throws CloneNotSupportedException { jaroslav@694: GZIPHeader gheader = (GZIPHeader)super.clone(); jaroslav@694: byte[] tmp; jaroslav@694: if(gheader.extra!=null){ jaroslav@694: tmp=new byte[gheader.extra.length]; jaroslav@694: System.arraycopy(gheader.extra, 0, tmp, 0, tmp.length); jaroslav@694: gheader.extra = tmp; jaroslav@694: } jaroslav@694: jaroslav@694: if(gheader.name!=null){ jaroslav@694: tmp=new byte[gheader.name.length]; jaroslav@694: System.arraycopy(gheader.name, 0, tmp, 0, tmp.length); jaroslav@694: gheader.name = tmp; jaroslav@694: } jaroslav@694: jaroslav@694: if(gheader.comment!=null){ jaroslav@694: tmp=new byte[gheader.comment.length]; jaroslav@694: System.arraycopy(gheader.comment, 0, tmp, 0, tmp.length); jaroslav@694: gheader.comment = tmp; jaroslav@694: } jaroslav@694: jaroslav@694: return gheader; jaroslav@694: } jaroslav@694: }