1 /*
   2  * Copyright (c) 2009, 2019, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package jdk.nio.zipfs;
  27 
  28 /**
  29  * @author Xueming Shen
  30  */
  31 class ZipConstants {
  32     /*
  33      * Compression methods
  34      */
  35     static final int METHOD_STORED     = 0;
  36     static final int METHOD_DEFLATED   = 8;
  37     static final int METHOD_DEFLATED64 = 9;
  38     static final int METHOD_BZIP2      = 12;
  39     static final int METHOD_LZMA       = 14;
  40     static final int METHOD_LZ77       = 19;
  41     static final int METHOD_AES        = 99;
  42 
  43     /*
  44      * General purpose bit flag
  45      */
  46     static final int FLAG_ENCRYPTED  = 0x01;
  47     static final int FLAG_DATADESCR  = 0x08;    // crc, size and csize in dd
  48     static final int FLAG_USE_UTF8   = 0x800;   // If this bit is set the filename and
  49                                                 // comment fields for this file must be
  50                                                 // encoded using UTF-8.
  51     /*
  52      * Header signatures
  53      */
  54     static long LOCSIG = 0x04034b50L;   // "PK\003\004"
  55     static long EXTSIG = 0x08074b50L;   // "PK\007\008"
  56     static long CENSIG = 0x02014b50L;   // "PK\001\002"
  57     static long ENDSIG = 0x06054b50L;   // "PK\005\006"
  58 
  59     /*
  60      * Header sizes in bytes (including signatures)
  61      */
  62     static final int LOCHDR = 30;       // LOC header size
  63     static final int EXTHDR = 16;       // EXT header size
  64     static final int CENHDR = 46;       // CEN header size
  65     static final int ENDHDR = 22;       // END header size
  66 
  67     /*
  68      * Local file (LOC) header field offsets
  69      */
  70     static final int LOCVER = 4;        // version needed to extract
  71     static final int LOCFLG = 6;        // general purpose bit flag
  72     static final int LOCHOW = 8;        // compression method
  73     static final int LOCTIM = 10;       // modification time
  74     static final int LOCCRC = 14;       // uncompressed file crc-32 value
  75     static final int LOCSIZ = 18;       // compressed size
  76     static final int LOCLEN = 22;       // uncompressed size
  77     static final int LOCNAM = 26;       // filename length
  78     static final int LOCEXT = 28;       // extra field length
  79 
  80     /*
  81      * Extra local (EXT) header field offsets
  82      */
  83     static final int EXTCRC = 4;        // uncompressed file crc-32 value
  84     static final int EXTSIZ = 8;        // compressed size
  85     static final int EXTLEN = 12;       // uncompressed size
  86 
  87     /*
  88      * Central directory (CEN) header field offsets
  89      */
  90     static final int CENVEM = 4;        // version made by
  91     static final int CENVER = 6;        // version needed to extract
  92     static final int CENFLG = 8;        // encrypt, decrypt flags
  93     static final int CENHOW = 10;       // compression method
  94     static final int CENTIM = 12;       // modification time
  95     static final int CENCRC = 16;       // uncompressed file crc-32 value
  96     static final int CENSIZ = 20;       // compressed size
  97     static final int CENLEN = 24;       // uncompressed size
  98     static final int CENNAM = 28;       // filename length
  99     static final int CENEXT = 30;       // extra field length
 100     static final int CENCOM = 32;       // comment length
 101     static final int CENDSK = 34;       // disk number start
 102     static final int CENATT = 36;       // internal file attributes
 103     static final int CENATX = 38;       // external file attributes
 104     static final int CENOFF = 42;       // LOC header offset
 105 
 106     /*
 107      * End of central directory (END) header field offsets
 108      */
 109     static final int ENDSUB = 8;        // number of entries on this disk
 110     static final int ENDTOT = 10;       // total number of entries
 111     static final int ENDSIZ = 12;       // central directory size in bytes
 112     static final int ENDOFF = 16;       // offset of first CEN header
 113     static final int ENDCOM = 20;       // zip file comment length
 114 
 115     /*
 116      * ZIP64 constants
 117      */
 118     static final long ZIP64_ENDSIG = 0x06064b50L;  // "PK\006\006"
 119     static final long ZIP64_LOCSIG = 0x07064b50L;  // "PK\006\007"
 120     static final int  ZIP64_ENDHDR = 56;           // ZIP64 end header size
 121     static final int  ZIP64_LOCHDR = 20;           // ZIP64 end loc header size
 122     static final int  ZIP64_EXTHDR = 24;           // EXT header size
 123     static final int  ZIP64_EXTID  = 0x0001;       // Extra field Zip64 header ID
 124 
 125     static final int  ZIP64_MINVAL32 = 0xFFFF;
 126     static final long ZIP64_MINVAL = 0xFFFFFFFFL;
 127 
 128     /*
 129      * Zip64 End of central directory (END) header field offsets
 130      */
 131     static final int  ZIP64_ENDLEN = 4;       // size of zip64 end of central dir
 132     static final int  ZIP64_ENDVEM = 12;      // version made by
 133     static final int  ZIP64_ENDVER = 14;      // version needed to extract
 134     static final int  ZIP64_ENDNMD = 16;      // number of this disk
 135     static final int  ZIP64_ENDDSK = 20;      // disk number of start
 136     static final int  ZIP64_ENDTOD = 24;      // total number of entries on this disk
 137     static final int  ZIP64_ENDTOT = 32;      // total number of entries
 138     static final int  ZIP64_ENDSIZ = 40;      // central directory size in bytes
 139     static final int  ZIP64_ENDOFF = 48;      // offset of first CEN header
 140     static final int  ZIP64_ENDEXT = 56;      // zip64 extensible data sector
 141 
 142     /*
 143      * Zip64 End of central directory locator field offsets
 144      */
 145     static final int  ZIP64_LOCDSK = 4;       // disk number start
 146     static final int  ZIP64_LOCOFF = 8;       // offset of zip64 end
 147     static final int  ZIP64_LOCTOT = 16;      // total number of disks
 148 
 149     /*
 150      * Zip64 Extra local (EXT) header field offsets
 151      */
 152     static final int  ZIP64_EXTCRC = 4;       // uncompressed file crc-32 value
 153     static final int  ZIP64_EXTSIZ = 8;       // compressed size, 8-byte
 154     static final int  ZIP64_EXTLEN = 16;      // uncompressed size, 8-byte
 155 
 156     /*
 157      * Extra field header ID
 158      */
 159     static final int  EXTID_ZIP64 = 0x0001;      // ZIP64
 160     static final int  EXTID_NTFS  = 0x000a;      // NTFS
 161     static final int  EXTID_UNIX  = 0x000d;      // UNIX
 162     static final int  EXTID_EFS   = 0x0017;      // Strong Encryption
 163     static final int  EXTID_EXTT  = 0x5455;      // Info-ZIP Extended Timestamp
 164 
 165     /*
 166      * fields access methods
 167      */
 168     ///////////////////////////////////////////////////////
 169     static final int CH(byte[] b, int n) {
 170         return Byte.toUnsignedInt(b[n]);
 171     }
 172 
 173     static final int SH(byte[] b, int n) {
 174         return Byte.toUnsignedInt(b[n]) | (Byte.toUnsignedInt(b[n + 1]) << 8);
 175     }
 176 
 177     static final long LG(byte[] b, int n) {
 178         return ((SH(b, n)) | (SH(b, n + 2) << 16)) & 0xffffffffL;
 179     }
 180 
 181     static final long LL(byte[] b, int n) {
 182         return (LG(b, n)) | (LG(b, n + 4) << 32);
 183     }
 184 
 185     static long getSig(byte[] b, int n) { return LG(b, n); }
 186 
 187     private static boolean pkSigAt(byte[] b, int n, int b1, int b2) {
 188         return b[n] == 'P' & b[n + 1] == 'K' & b[n + 2] == b1 & b[n + 3] == b2;
 189     }
 190 
 191     static boolean cenSigAt(byte[] b, int n) { return pkSigAt(b, n, 1, 2); }
 192     static boolean locSigAt(byte[] b, int n) { return pkSigAt(b, n, 3, 4); }
 193     static boolean endSigAt(byte[] b, int n) { return pkSigAt(b, n, 5, 6); }
 194     static boolean extSigAt(byte[] b, int n) { return pkSigAt(b, n, 7, 8); }
 195     static boolean end64SigAt(byte[] b, int n) { return pkSigAt(b, n, 6, 6); }
 196     static boolean locator64SigAt(byte[] b, int n) { return pkSigAt(b, n, 6, 7); }
 197 
 198     // local file (LOC) header fields
 199     static final long LOCSIG(byte[] b) { return LG(b, 0); } // signature
 200     static final int  LOCVER(byte[] b) { return SH(b, 4); } // version needed to extract
 201     static final int  LOCFLG(byte[] b) { return SH(b, 6); } // general purpose bit flags
 202     static final int  LOCHOW(byte[] b) { return SH(b, 8); } // compression method
 203     static final long LOCTIM(byte[] b) { return LG(b, 10);} // modification time
 204     static final long LOCCRC(byte[] b) { return LG(b, 14);} // crc of uncompressed data
 205     static final long LOCSIZ(byte[] b) { return LG(b, 18);} // compressed data size
 206     static final long LOCLEN(byte[] b) { return LG(b, 22);} // uncompressed data size
 207     static final int  LOCNAM(byte[] b) { return SH(b, 26);} // filename length
 208     static final int  LOCEXT(byte[] b) { return SH(b, 28);} // extra field length
 209 
 210     // extra local (EXT) header fields
 211     static final long EXTCRC(byte[] b) { return LG(b, 4);}  // crc of uncompressed data
 212     static final long EXTSIZ(byte[] b) { return LG(b, 8);}  // compressed size
 213     static final long EXTLEN(byte[] b) { return LG(b, 12);} // uncompressed size
 214 
 215     // end of central directory header (END) fields
 216     static final int  ENDSUB(byte[] b) { return SH(b, 8); }  // number of entries on this disk
 217     static final int  ENDTOT(byte[] b) { return SH(b, 10);}  // total number of entries
 218     static final long ENDSIZ(byte[] b) { return LG(b, 12);}  // central directory size
 219     static final long ENDOFF(byte[] b) { return LG(b, 16);}  // central directory offset
 220     static final int  ENDCOM(byte[] b) { return SH(b, 20);}  // size of zip file comment
 221     static final int  ENDCOM(byte[] b, int off) { return SH(b, off + 20);}
 222 
 223     // zip64 end of central directory recoder fields
 224     static final long ZIP64_ENDTOD(byte[] b) { return LL(b, 24);}  // total number of entries on disk
 225     static final long ZIP64_ENDTOT(byte[] b) { return LL(b, 32);}  // total number of entries
 226     static final long ZIP64_ENDSIZ(byte[] b) { return LL(b, 40);}  // central directory size
 227     static final long ZIP64_ENDOFF(byte[] b) { return LL(b, 48);}  // central directory offset
 228     static final long ZIP64_LOCOFF(byte[] b) { return LL(b, 8);}   // zip64 end offset
 229 
 230     // central directory header (CEN) fields
 231     static final long CENSIG(byte[] b, int pos) { return LG(b, pos + 0); }
 232     static final int  CENVEM(byte[] b, int pos) { return SH(b, pos + 4); }
 233     static final int  CENVEM_FA(byte[] b, int pos) { return CH(b, pos + 5); }
 234     static final int  CENVER(byte[] b, int pos) { return SH(b, pos + 6); }
 235     static final int  CENFLG(byte[] b, int pos) { return SH(b, pos + 8); }
 236     static final int  CENHOW(byte[] b, int pos) { return SH(b, pos + 10);}
 237     static final long CENTIM(byte[] b, int pos) { return LG(b, pos + 12);}
 238     static final long CENCRC(byte[] b, int pos) { return LG(b, pos + 16);}
 239     static final long CENSIZ(byte[] b, int pos) { return LG(b, pos + 20);}
 240     static final long CENLEN(byte[] b, int pos) { return LG(b, pos + 24);}
 241     static final int  CENNAM(byte[] b, int pos) { return SH(b, pos + 28);}
 242     static final int  CENEXT(byte[] b, int pos) { return SH(b, pos + 30);}
 243     static final int  CENCOM(byte[] b, int pos) { return SH(b, pos + 32);}
 244     static final int  CENDSK(byte[] b, int pos) { return SH(b, pos + 34);}
 245     static final int  CENATT(byte[] b, int pos) { return SH(b, pos + 36);}
 246     static final long CENATX(byte[] b, int pos) { return LG(b, pos + 38);}
 247     static final int  CENATX_PERMS(byte[] b, int pos) { return SH(b, pos + 40);}
 248     static final long CENOFF(byte[] b, int pos) { return LG(b, pos + 42);}
 249 
 250     /* The END header is followed by a variable length comment of size < 64k. */
 251     static final long END_MAXLEN = 0xFFFF + ENDHDR;
 252     static final int READBLOCKSZ = 128;
 253 }