emul/mini/src/main/java/org/apidesign/bck2brwsr/emul/zip/GZIPHeader.java
branchemul
changeset 694 0d277415ed02
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/emul/mini/src/main/java/org/apidesign/bck2brwsr/emul/zip/GZIPHeader.java	Thu Feb 07 12:58:12 2013 +0100
     1.3 @@ -0,0 +1,215 @@
     1.4 +/* -*-mode:java; c-basic-offset:2; -*- */
     1.5 +/*
     1.6 +Copyright (c) 2011 ymnk, JCraft,Inc. All rights reserved.
     1.7 +
     1.8 +Redistribution and use in source and binary forms, with or without
     1.9 +modification, are permitted provided that the following conditions are met:
    1.10 +
    1.11 +  1. Redistributions of source code must retain the above copyright notice,
    1.12 +     this list of conditions and the following disclaimer.
    1.13 +
    1.14 +  2. Redistributions in binary form must reproduce the above copyright 
    1.15 +     notice, this list of conditions and the following disclaimer in 
    1.16 +     the documentation and/or other materials provided with the distribution.
    1.17 +
    1.18 +  3. The names of the authors may not be used to endorse or promote products
    1.19 +     derived from this software without specific prior written permission.
    1.20 +
    1.21 +THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
    1.22 +INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
    1.23 +FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
    1.24 +INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
    1.25 +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
    1.26 +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
    1.27 +OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
    1.28 +LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
    1.29 +NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
    1.30 +EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    1.31 + */
    1.32 +/*
    1.33 + * This program is based on zlib-1.1.3, so all credit should go authors
    1.34 + * Jean-loup Gailly(jloup@gzip.org) and Mark Adler(madler@alumni.caltech.edu)
    1.35 + * and contributors of zlib.
    1.36 + */
    1.37 +
    1.38 +package org.apidesign.bck2brwsr.emul.zip;
    1.39 +
    1.40 +import org.apidesign.bck2brwsr.emul.lang.System;
    1.41 +import java.io.UnsupportedEncodingException;
    1.42 +
    1.43 +/**
    1.44 + * @see "http://www.ietf.org/rfc/rfc1952.txt"
    1.45 + */
    1.46 +final class GZIPHeader implements Cloneable {
    1.47 +
    1.48 +  public static final byte OS_MSDOS = (byte) 0x00;
    1.49 +  public static final byte OS_AMIGA = (byte) 0x01;
    1.50 +  public static final byte OS_VMS = (byte) 0x02;
    1.51 +  public static final byte OS_UNIX = (byte) 0x03;
    1.52 +  public static final byte OS_ATARI = (byte) 0x05;
    1.53 +  public static final byte OS_OS2 = (byte) 0x06;
    1.54 +  public static final byte OS_MACOS = (byte) 0x07;
    1.55 +  public static final byte OS_TOPS20 = (byte) 0x0a;
    1.56 +  public static final byte OS_WIN32 = (byte) 0x0b;
    1.57 +  public static final byte OS_VMCMS = (byte) 0x04;
    1.58 +  public static final byte OS_ZSYSTEM = (byte) 0x08;
    1.59 +  public static final byte OS_CPM = (byte) 0x09;
    1.60 +  public static final byte OS_QDOS = (byte) 0x0c;
    1.61 +  public static final byte OS_RISCOS = (byte) 0x0d;
    1.62 +  public static final byte OS_UNKNOWN = (byte) 0xff;
    1.63 +
    1.64 +  boolean text = false;
    1.65 +  private boolean fhcrc = false;
    1.66 +  long time;
    1.67 +  int xflags;
    1.68 +  int os = 255;
    1.69 +  byte[] extra;
    1.70 +  byte[] name;
    1.71 +  byte[] comment;
    1.72 +  int hcrc;
    1.73 +  long crc;
    1.74 +  boolean done = false;
    1.75 +  long mtime = 0;
    1.76 +
    1.77 +  public void setModifiedTime(long mtime) {
    1.78 +    this.mtime = mtime;
    1.79 +  }
    1.80 +
    1.81 +  public long getModifiedTime() {
    1.82 +    return mtime;
    1.83 +  }
    1.84 +
    1.85 +  public void setOS(int os) {
    1.86 +    if((0<=os && os <=13) || os==255)
    1.87 +      this.os=os;
    1.88 +    else
    1.89 +      throw new IllegalArgumentException("os: "+os);
    1.90 +  }
    1.91 +
    1.92 +  public int getOS(){
    1.93 +    return os;
    1.94 +  }
    1.95 +
    1.96 +  public void setName(String name) {
    1.97 +    try{
    1.98 +      this.name=name.getBytes("ISO-8859-1");
    1.99 +    }
   1.100 +    catch(UnsupportedEncodingException e){
   1.101 +      throw new IllegalArgumentException("name must be in ISO-8859-1 "+name);
   1.102 +    }
   1.103 +  }
   1.104 +
   1.105 +  public String getName(){
   1.106 +    if(name==null) return "";
   1.107 +    try {
   1.108 +      return new String(name, "ISO-8859-1");
   1.109 +    }
   1.110 +    catch (UnsupportedEncodingException e) {
   1.111 +      throw new IllegalArgumentException(e.toString());
   1.112 +    }
   1.113 +  }
   1.114 +
   1.115 +  public void setComment(String comment) {
   1.116 +    try{
   1.117 +      this.comment=comment.getBytes("ISO-8859-1");
   1.118 +    }
   1.119 +    catch(UnsupportedEncodingException e){
   1.120 +      throw new IllegalArgumentException("comment must be in ISO-8859-1 "+name);
   1.121 +    }
   1.122 +  }
   1.123 +
   1.124 +  public String getComment(){
   1.125 +    if(comment==null) return "";
   1.126 +    try {
   1.127 +      return new String(comment, "ISO-8859-1");
   1.128 +    }
   1.129 +    catch (UnsupportedEncodingException e) {
   1.130 +      throw new IllegalArgumentException(e.toString());
   1.131 +    }
   1.132 +  }
   1.133 +
   1.134 +  public void setCRC(long crc){
   1.135 +    this.crc = crc;
   1.136 +  }
   1.137 +
   1.138 +  public long getCRC(){
   1.139 +    return crc;
   1.140 +  }
   1.141 +/*
   1.142 +  void put(Deflate d){
   1.143 +    int flag = 0;
   1.144 +    if(text){
   1.145 +      flag |= 1;     // FTEXT
   1.146 +    }
   1.147 +    if(fhcrc){
   1.148 +      flag |= 2;     // FHCRC
   1.149 +    }
   1.150 +    if(extra!=null){
   1.151 +      flag |= 4;     // FEXTRA
   1.152 +    }
   1.153 +    if(name!=null){
   1.154 +      flag |= 8;    // FNAME
   1.155 +    }
   1.156 +    if(comment!=null){
   1.157 +      flag |= 16;   // FCOMMENT
   1.158 +    }
   1.159 +    int xfl = 0;
   1.160 +    if(d.level == JZlib.Z_BEST_SPEED){
   1.161 +      xfl |= 4;
   1.162 +    }
   1.163 +    else if (d.level == JZlib.Z_BEST_COMPRESSION){
   1.164 +      xfl |= 2;
   1.165 +    }
   1.166 +
   1.167 +    d.put_short((short)0x8b1f);  // ID1 ID2
   1.168 +    d.put_byte((byte)8);         // CM(Compression Method)
   1.169 +    d.put_byte((byte)flag);
   1.170 +    d.put_byte((byte)mtime);
   1.171 +    d.put_byte((byte)(mtime>>8));
   1.172 +    d.put_byte((byte)(mtime>>16));
   1.173 +    d.put_byte((byte)(mtime>>24));
   1.174 +    d.put_byte((byte)xfl);
   1.175 +    d.put_byte((byte)os);
   1.176 +
   1.177 +    if(extra!=null){
   1.178 +      d.put_byte((byte)extra.length);
   1.179 +      d.put_byte((byte)(extra.length>>8));
   1.180 +      d.put_byte(extra, 0, extra.length);
   1.181 +    }
   1.182 +
   1.183 +    if(name!=null){
   1.184 +      d.put_byte(name, 0, name.length);
   1.185 +      d.put_byte((byte)0);
   1.186 +    }
   1.187 +
   1.188 +    if(comment!=null){
   1.189 +      d.put_byte(comment, 0, comment.length);
   1.190 +      d.put_byte((byte)0);
   1.191 +    }
   1.192 +  }
   1.193 +*/
   1.194 +  @Override
   1.195 +  public Object clone() throws CloneNotSupportedException {
   1.196 +    GZIPHeader gheader = (GZIPHeader)super.clone();
   1.197 +    byte[] tmp;
   1.198 +    if(gheader.extra!=null){
   1.199 +      tmp=new byte[gheader.extra.length];
   1.200 +      System.arraycopy(gheader.extra, 0, tmp, 0, tmp.length);
   1.201 +      gheader.extra = tmp;
   1.202 +    }
   1.203 +
   1.204 +    if(gheader.name!=null){
   1.205 +      tmp=new byte[gheader.name.length];
   1.206 +      System.arraycopy(gheader.name, 0, tmp, 0, tmp.length);
   1.207 +      gheader.name = tmp;
   1.208 +    }
   1.209 +
   1.210 +    if(gheader.comment!=null){
   1.211 +      tmp=new byte[gheader.comment.length];
   1.212 +      System.arraycopy(gheader.comment, 0, tmp, 0, tmp.length);
   1.213 +      gheader.comment = tmp;
   1.214 +    }
   1.215 +
   1.216 +    return gheader;
   1.217 +  }
   1.218 +}