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;
38 import java.io.UnsupportedEncodingException;
41 * @see "http://www.ietf.org/rfc/rfc1952.txt"
43 final class GZIPHeader implements Cloneable {
45 public static final byte OS_MSDOS = (byte) 0x00;
46 public static final byte OS_AMIGA = (byte) 0x01;
47 public static final byte OS_VMS = (byte) 0x02;
48 public static final byte OS_UNIX = (byte) 0x03;
49 public static final byte OS_ATARI = (byte) 0x05;
50 public static final byte OS_OS2 = (byte) 0x06;
51 public static final byte OS_MACOS = (byte) 0x07;
52 public static final byte OS_TOPS20 = (byte) 0x0a;
53 public static final byte OS_WIN32 = (byte) 0x0b;
54 public static final byte OS_VMCMS = (byte) 0x04;
55 public static final byte OS_ZSYSTEM = (byte) 0x08;
56 public static final byte OS_CPM = (byte) 0x09;
57 public static final byte OS_QDOS = (byte) 0x0c;
58 public static final byte OS_RISCOS = (byte) 0x0d;
59 public static final byte OS_UNKNOWN = (byte) 0xff;
62 private boolean fhcrc = false;
74 public void setModifiedTime(long mtime) {
78 public long getModifiedTime() {
82 public void setOS(int os) {
83 if((0<=os && os <=13) || os==255)
86 throw new IllegalArgumentException("os: "+os);
93 public void setName(String name) {
95 this.name=name.getBytes("ISO-8859-1");
97 catch(UnsupportedEncodingException e){
98 throw new IllegalArgumentException("name must be in ISO-8859-1 "+name);
102 public String getName(){
103 if(name==null) return "";
105 return new String(name, "ISO-8859-1");
107 catch (UnsupportedEncodingException e) {
108 throw new IllegalArgumentException(e.toString());
112 public void setComment(String comment) {
114 this.comment=comment.getBytes("ISO-8859-1");
116 catch(UnsupportedEncodingException e){
117 throw new IllegalArgumentException("comment must be in ISO-8859-1 "+name);
121 public String getComment(){
122 if(comment==null) return "";
124 return new String(comment, "ISO-8859-1");
126 catch (UnsupportedEncodingException e) {
127 throw new IllegalArgumentException(e.toString());
131 public void setCRC(long crc){
135 public long getCRC(){
154 flag |= 16; // FCOMMENT
157 if(d.level == JZlib.Z_BEST_SPEED){
160 else if (d.level == JZlib.Z_BEST_COMPRESSION){
164 d.put_short((short)0x8b1f); // ID1 ID2
165 d.put_byte((byte)8); // CM(Compression Method)
166 d.put_byte((byte)flag);
167 d.put_byte((byte)mtime);
168 d.put_byte((byte)(mtime>>8));
169 d.put_byte((byte)(mtime>>16));
170 d.put_byte((byte)(mtime>>24));
171 d.put_byte((byte)xfl);
172 d.put_byte((byte)os);
175 d.put_byte((byte)extra.length);
176 d.put_byte((byte)(extra.length>>8));
177 d.put_byte(extra, 0, extra.length);
181 d.put_byte(name, 0, name.length);
186 d.put_byte(comment, 0, comment.length);
192 public Object clone() throws CloneNotSupportedException {
193 GZIPHeader gheader = (GZIPHeader)super.clone();
195 if(gheader.extra!=null){
196 tmp=new byte[gheader.extra.length];
197 FastJar.arraycopy(gheader.extra, 0, tmp, 0, tmp.length);
201 if(gheader.name!=null){
202 tmp=new byte[gheader.name.length];
203 FastJar.arraycopy(gheader.name, 0, tmp, 0, tmp.length);
207 if(gheader.comment!=null){
208 tmp=new byte[gheader.comment.length];
209 FastJar.arraycopy(gheader.comment, 0, tmp, 0, tmp.length);
210 gheader.comment = tmp;