1 /* -*-mode:java; c-basic-offset:2; -*- */ 2 /* 3 Copyright (c) 2011 ymnk, JCraft,Inc. All rights reserved. 4 5 Redistribution and use in source and binary forms, with or without 6 modification, are permitted provided that the following conditions are met: 7 8 1. Redistributions of source code must retain the above copyright notice, 9 this list of conditions and the following disclaimer. 10 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. 14 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. 17 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. 28 */ 29 /* 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. 33 */ 34 35 package com.jcraft.jzlib; 36 37 import java.io.UnsupportedEncodingException; 38 39 /** 40 * @see "http://www.ietf.org/rfc/rfc1952.txt" 41 */ 42 public class GZIPHeader implements Cloneable { 43 44 public static final byte OS_MSDOS = (byte) 0x00; 45 public static final byte OS_AMIGA = (byte) 0x01; 46 public static final byte OS_VMS = (byte) 0x02; 47 public static final byte OS_UNIX = (byte) 0x03; 48 public static final byte OS_ATARI = (byte) 0x05; 49 public static final byte OS_OS2 = (byte) 0x06; 50 public static final byte OS_MACOS = (byte) 0x07; 51 public static final byte OS_TOPS20 = (byte) 0x0a; 52 public static final byte OS_WIN32 = (byte) 0x0b; 53 public static final byte OS_VMCMS = (byte) 0x04; 54 public static final byte OS_ZSYSTEM = (byte) 0x08; 55 public static final byte OS_CPM = (byte) 0x09; 56 public static final byte OS_QDOS = (byte) 0x0c; 57 public static final byte OS_RISCOS = (byte) 0x0d; 58 public static final byte OS_UNKNOWN = (byte) 0xff; 59 60 boolean text = false; 61 private boolean fhcrc = false; 62 long time; 63 int xflags; 64 int os = 255; 65 byte[] extra; 66 byte[] name; 67 byte[] comment; 68 int hcrc; 69 long crc; 70 boolean done = false; 71 long mtime = 0; 72 setModifiedTime(long mtime)73 public void setModifiedTime(long mtime) { 74 this.mtime = mtime; 75 } 76 getModifiedTime()77 public long getModifiedTime() { 78 return mtime; 79 } 80 setOS(int os)81 public void setOS(int os) { 82 if((0<=os && os <=13) || os==255) 83 this.os=os; 84 else 85 throw new IllegalArgumentException("os: "+os); 86 } 87 getOS()88 public int getOS(){ 89 return os; 90 } 91 setName(String name)92 public void setName(String name) { 93 try{ 94 this.name=name.getBytes("ISO-8859-1"); 95 } 96 catch(UnsupportedEncodingException e){ 97 throw new IllegalArgumentException("name must be in ISO-8859-1 "+name); 98 } 99 } 100 getName()101 public String getName(){ 102 if(name==null) return ""; 103 try { 104 return new String(name, "ISO-8859-1"); 105 } 106 catch (UnsupportedEncodingException e) { 107 throw new InternalError(e.toString()); 108 } 109 } 110 setComment(String comment)111 public void setComment(String comment) { 112 try{ 113 this.comment=comment.getBytes("ISO-8859-1"); 114 } 115 catch(UnsupportedEncodingException e){ 116 throw new IllegalArgumentException("comment must be in ISO-8859-1 "+name); 117 } 118 } 119 getComment()120 public String getComment(){ 121 if(comment==null) return ""; 122 try { 123 return new String(comment, "ISO-8859-1"); 124 } 125 catch (UnsupportedEncodingException e) { 126 throw new InternalError(e.toString()); 127 } 128 } 129 setCRC(long crc)130 public void setCRC(long crc){ 131 this.crc = crc; 132 } 133 getCRC()134 public long getCRC(){ 135 return crc; 136 } 137 put(Deflate d)138 void put(Deflate d){ 139 int flag = 0; 140 if(text){ 141 flag |= 1; // FTEXT 142 } 143 if(fhcrc){ 144 flag |= 2; // FHCRC 145 } 146 if(extra!=null){ 147 flag |= 4; // FEXTRA 148 } 149 if(name!=null){ 150 flag |= 8; // FNAME 151 } 152 if(comment!=null){ 153 flag |= 16; // FCOMMENT 154 } 155 int xfl = 0; 156 if(d.level == JZlib.Z_BEST_SPEED){ 157 xfl |= 4; 158 } 159 else if (d.level == JZlib.Z_BEST_COMPRESSION){ 160 xfl |= 2; 161 } 162 163 d.put_short((short)0x8b1f); // ID1 ID2 164 d.put_byte((byte)8); // CM(Compression Method) 165 d.put_byte((byte)flag); 166 d.put_byte((byte)mtime); 167 d.put_byte((byte)(mtime>>8)); 168 d.put_byte((byte)(mtime>>16)); 169 d.put_byte((byte)(mtime>>24)); 170 d.put_byte((byte)xfl); 171 d.put_byte((byte)os); 172 173 if(extra!=null){ 174 d.put_byte((byte)extra.length); 175 d.put_byte((byte)(extra.length>>8)); 176 d.put_byte(extra, 0, extra.length); 177 } 178 179 if(name!=null){ 180 d.put_byte(name, 0, name.length); 181 d.put_byte((byte)0); 182 } 183 184 if(comment!=null){ 185 d.put_byte(comment, 0, comment.length); 186 d.put_byte((byte)0); 187 } 188 } 189 190 @Override clone()191 public Object clone() throws CloneNotSupportedException { 192 GZIPHeader gheader = (GZIPHeader)super.clone(); 193 byte[] tmp; 194 if(gheader.extra!=null){ 195 tmp=new byte[gheader.extra.length]; 196 System.arraycopy(gheader.extra, 0, tmp, 0, tmp.length); 197 gheader.extra = tmp; 198 } 199 200 if(gheader.name!=null){ 201 tmp=new byte[gheader.name.length]; 202 System.arraycopy(gheader.name, 0, tmp, 0, tmp.length); 203 gheader.name = tmp; 204 } 205 206 if(gheader.comment!=null){ 207 tmp=new byte[gheader.comment.length]; 208 System.arraycopy(gheader.comment, 0, tmp, 0, tmp.length); 209 gheader.comment = tmp; 210 } 211 212 return gheader; 213 } 214 } 215