Path: blob/main/src/lwjgl/java/com/jcraft/jzlib/GZIPHeader.java
8650 views
/* -*-mode:java; c-basic-offset:2; -*- */1/*2Copyright (c) 2011 ymnk, JCraft,Inc. All rights reserved.34Redistribution and use in source and binary forms, with or without5modification, are permitted provided that the following conditions are met:671. Redistributions of source code must retain the above copyright notice,8this list of conditions and the following disclaimer.9102. Redistributions in binary form must reproduce the above copyright11notice, this list of conditions and the following disclaimer in12the documentation and/or other materials provided with the distribution.13143. The names of the authors may not be used to endorse or promote products15derived from this software without specific prior written permission.1617THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,18INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND19FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,20INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,21INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT22LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,23OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF24LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING25NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,26EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.27*/28/*29* This program is based on zlib-1.1.3, so all credit should go authors30* Jean-loup Gailly([email protected]) and Mark Adler([email protected])31* and contributors of zlib.32*/3334package com.jcraft.jzlib;3536import java.io.UnsupportedEncodingException;3738/**39* @see "http://www.ietf.org/rfc/rfc1952.txt"40*/41public class GZIPHeader implements Cloneable {4243public static final byte OS_MSDOS = (byte) 0x00;44public static final byte OS_AMIGA = (byte) 0x01;45public static final byte OS_VMS = (byte) 0x02;46public static final byte OS_UNIX = (byte) 0x03;47public static final byte OS_ATARI = (byte) 0x05;48public static final byte OS_OS2 = (byte) 0x06;49public static final byte OS_MACOS = (byte) 0x07;50public static final byte OS_TOPS20 = (byte) 0x0a;51public static final byte OS_WIN32 = (byte) 0x0b;52public static final byte OS_VMCMS = (byte) 0x04;53public static final byte OS_ZSYSTEM = (byte) 0x08;54public static final byte OS_CPM = (byte) 0x09;55public static final byte OS_QDOS = (byte) 0x0c;56public static final byte OS_RISCOS = (byte) 0x0d;57public static final byte OS_UNKNOWN = (byte) 0xff;5859boolean text = false;60private boolean fhcrc = false;61long time;62int xflags;63int os = 255;64byte[] extra;65byte[] name;66byte[] comment;67int hcrc;68long crc;69boolean done = false;70long mtime = 0;7172public void setModifiedTime(long mtime) {73this.mtime = mtime;74}7576public long getModifiedTime() {77return mtime;78}7980public void setOS(int os) {81if((0<=os && os <=13) || os==255)82this.os=os;83else84throw new IllegalArgumentException("os: "+os);85}8687public int getOS(){88return os;89}9091public void setName(String name) {92try{93this.name=name.getBytes("ISO-8859-1");94}95catch(UnsupportedEncodingException e){96throw new IllegalArgumentException("name must be in ISO-8859-1 "+name);97}98}99100public String getName(){101if(name==null) return "";102try {103return new String(name, "ISO-8859-1");104}105catch (UnsupportedEncodingException e) {106throw new InternalError(e.toString());107}108}109110public void setComment(String comment) {111try{112this.comment=comment.getBytes("ISO-8859-1");113}114catch(UnsupportedEncodingException e){115throw new IllegalArgumentException("comment must be in ISO-8859-1 "+name);116}117}118119public String getComment(){120if(comment==null) return "";121try {122return new String(comment, "ISO-8859-1");123}124catch (UnsupportedEncodingException e) {125throw new InternalError(e.toString());126}127}128129public void setCRC(long crc){130this.crc = crc;131}132133public long getCRC(){134return crc;135}136137void put(Deflate d){138int flag = 0;139if(text){140flag |= 1; // FTEXT141}142if(fhcrc){143flag |= 2; // FHCRC144}145if(extra!=null){146flag |= 4; // FEXTRA147}148if(name!=null){149flag |= 8; // FNAME150}151if(comment!=null){152flag |= 16; // FCOMMENT153}154int xfl = 0;155if(d.level == JZlib.Z_BEST_SPEED){156xfl |= 4;157}158else if (d.level == JZlib.Z_BEST_COMPRESSION){159xfl |= 2;160}161162d.put_short((short)0x8b1f); // ID1 ID2163d.put_byte((byte)8); // CM(Compression Method)164d.put_byte((byte)flag);165d.put_byte((byte)mtime);166d.put_byte((byte)(mtime>>8));167d.put_byte((byte)(mtime>>16));168d.put_byte((byte)(mtime>>24));169d.put_byte((byte)xfl);170d.put_byte((byte)os);171172if(extra!=null){173d.put_byte((byte)extra.length);174d.put_byte((byte)(extra.length>>8));175d.put_byte(extra, 0, extra.length);176}177178if(name!=null){179d.put_byte(name, 0, name.length);180d.put_byte((byte)0);181}182183if(comment!=null){184d.put_byte(comment, 0, comment.length);185d.put_byte((byte)0);186}187}188189@Override190public Object clone() throws CloneNotSupportedException {191GZIPHeader gheader = (GZIPHeader)super.clone();192byte[] tmp;193if(gheader.extra!=null){194tmp=new byte[gheader.extra.length];195System.arraycopy(gheader.extra, 0, tmp, 0, tmp.length);196gheader.extra = tmp;197}198199if(gheader.name!=null){200tmp=new byte[gheader.name.length];201System.arraycopy(gheader.name, 0, tmp, 0, tmp.length);202gheader.name = tmp;203}204205if(gheader.comment!=null){206tmp=new byte[gheader.comment.length];207System.arraycopy(gheader.comment, 0, tmp, 0, tmp.length);208gheader.comment = tmp;209}210211return gheader;212}213}214215216