1/*
2 * Copyright (c) 2009, 2014, 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
26package jdk.nio.zipfs;
27
28/**
29 *
30 * @author Xueming Shen
31 */
32
33class ZipConstants {
34    /*
35     * Compression methods
36     */
37    static final int METHOD_STORED     = 0;
38    static final int METHOD_DEFLATED   = 8;
39    static final int METHOD_DEFLATED64 = 9;
40    static final int METHOD_BZIP2      = 12;
41    static final int METHOD_LZMA       = 14;
42    static final int METHOD_LZ77       = 19;
43    static final int METHOD_AES        = 99;
44
45    /*
46     * General purpose big flag
47     */
48    static final int FLAG_ENCRYPTED  = 0x01;
49    static final int FLAG_DATADESCR  = 0x08;    // crc, size and csize in dd
50    static final int FLAG_EFS        = 0x800;   // If this bit is set the filename and
51                                                // comment fields for this file must be
52                                                // encoded using UTF-8.
53    /*
54     * Header signatures
55     */
56    static long LOCSIG = 0x04034b50L;   // "PK\003\004"
57    static long EXTSIG = 0x08074b50L;   // "PK\007\008"
58    static long CENSIG = 0x02014b50L;   // "PK\001\002"
59    static long ENDSIG = 0x06054b50L;   // "PK\005\006"
60
61    /*
62     * Header sizes in bytes (including signatures)
63     */
64    static final int LOCHDR = 30;       // LOC header size
65    static final int EXTHDR = 16;       // EXT header size
66    static final int CENHDR = 46;       // CEN header size
67    static final int ENDHDR = 22;       // END header size
68
69    /*
70     * Local file (LOC) header field offsets
71     */
72    static final int LOCVER = 4;        // version needed to extract
73    static final int LOCFLG = 6;        // general purpose bit flag
74    static final int LOCHOW = 8;        // compression method
75    static final int LOCTIM = 10;       // modification time
76    static final int LOCCRC = 14;       // uncompressed file crc-32 value
77    static final int LOCSIZ = 18;       // compressed size
78    static final int LOCLEN = 22;       // uncompressed size
79    static final int LOCNAM = 26;       // filename length
80    static final int LOCEXT = 28;       // extra field length
81
82    /*
83     * Extra local (EXT) header field offsets
84     */
85    static final int EXTCRC = 4;        // uncompressed file crc-32 value
86    static final int EXTSIZ = 8;        // compressed size
87    static final int EXTLEN = 12;       // uncompressed size
88
89    /*
90     * Central directory (CEN) header field offsets
91     */
92    static final int CENVEM = 4;        // version made by
93    static final int CENVER = 6;        // version needed to extract
94    static final int CENFLG = 8;        // encrypt, decrypt flags
95    static final int CENHOW = 10;       // compression method
96    static final int CENTIM = 12;       // modification time
97    static final int CENCRC = 16;       // uncompressed file crc-32 value
98    static final int CENSIZ = 20;       // compressed size
99    static final int CENLEN = 24;       // uncompressed size
100    static final int CENNAM = 28;       // filename length
101    static final int CENEXT = 30;       // extra field length
102    static final int CENCOM = 32;       // comment length
103    static final int CENDSK = 34;       // disk number start
104    static final int CENATT = 36;       // internal file attributes
105    static final int CENATX = 38;       // external file attributes
106    static final int CENOFF = 42;       // LOC header offset
107
108    /*
109     * End of central directory (END) header field offsets
110     */
111    static final int ENDSUB = 8;        // number of entries on this disk
112    static final int ENDTOT = 10;       // total number of entries
113    static final int ENDSIZ = 12;       // central directory size in bytes
114    static final int ENDOFF = 16;       // offset of first CEN header
115    static final int ENDCOM = 20;       // zip file comment length
116
117    /*
118     * ZIP64 constants
119     */
120    static final long ZIP64_ENDSIG = 0x06064b50L;  // "PK\006\006"
121    static final long ZIP64_LOCSIG = 0x07064b50L;  // "PK\006\007"
122    static final int  ZIP64_ENDHDR = 56;           // ZIP64 end header size
123    static final int  ZIP64_LOCHDR = 20;           // ZIP64 end loc header size
124    static final int  ZIP64_EXTHDR = 24;           // EXT header size
125    static final int  ZIP64_EXTID  = 0x0001;       // Extra field Zip64 header ID
126
127    static final int  ZIP64_MINVAL32 = 0xFFFF;
128    static final long ZIP64_MINVAL = 0xFFFFFFFFL;
129
130    /*
131     * Zip64 End of central directory (END) header field offsets
132     */
133    static final int  ZIP64_ENDLEN = 4;       // size of zip64 end of central dir
134    static final int  ZIP64_ENDVEM = 12;      // version made by
135    static final int  ZIP64_ENDVER = 14;      // version needed to extract
136    static final int  ZIP64_ENDNMD = 16;      // number of this disk
137    static final int  ZIP64_ENDDSK = 20;      // disk number of start
138    static final int  ZIP64_ENDTOD = 24;      // total number of entries on this disk
139    static final int  ZIP64_ENDTOT = 32;      // total number of entries
140    static final int  ZIP64_ENDSIZ = 40;      // central directory size in bytes
141    static final int  ZIP64_ENDOFF = 48;      // offset of first CEN header
142    static final int  ZIP64_ENDEXT = 56;      // zip64 extensible data sector
143
144    /*
145     * Zip64 End of central directory locator field offsets
146     */
147    static final int  ZIP64_LOCDSK = 4;       // disk number start
148    static final int  ZIP64_LOCOFF = 8;       // offset of zip64 end
149    static final int  ZIP64_LOCTOT = 16;      // total number of disks
150
151    /*
152     * Zip64 Extra local (EXT) header field offsets
153     */
154    static final int  ZIP64_EXTCRC = 4;       // uncompressed file crc-32 value
155    static final int  ZIP64_EXTSIZ = 8;       // compressed size, 8-byte
156    static final int  ZIP64_EXTLEN = 16;      // uncompressed size, 8-byte
157
158    /*
159     * Extra field header ID
160     */
161    static final int  EXTID_ZIP64 = 0x0001;      // ZIP64
162    static final int  EXTID_NTFS  = 0x000a;      // NTFS
163    static final int  EXTID_UNIX  = 0x000d;      // UNIX
164    static final int  EXTID_EFS   = 0x0017;      // Strong Encryption
165    static final int  EXTID_EXTT  = 0x5455;      // Info-ZIP Extended Timestamp
166
167    /*
168     * fields access methods
169     */
170    ///////////////////////////////////////////////////////
171    static final int CH(byte[] b, int n) {
172        return Byte.toUnsignedInt(b[n]);
173    }
174
175    static final int SH(byte[] b, int n) {
176        return Byte.toUnsignedInt(b[n]) | (Byte.toUnsignedInt(b[n + 1]) << 8);
177    }
178
179    static final long LG(byte[] b, int n) {
180        return ((SH(b, n)) | (SH(b, n + 2) << 16)) & 0xffffffffL;
181    }
182
183    static final long LL(byte[] b, int n) {
184        return (LG(b, n)) | (LG(b, n + 4) << 32);
185    }
186
187    static long getSig(byte[] b, int n) { return LG(b, n); }
188
189    private static boolean pkSigAt(byte[] b, int n, int b1, int b2) {
190        return b[n] == 'P' & b[n + 1] == 'K' & b[n + 2] == b1 & b[n + 3] == b2;
191    }
192
193    static boolean cenSigAt(byte[] b, int n) { return pkSigAt(b, n, 1, 2); }
194    static boolean locSigAt(byte[] b, int n) { return pkSigAt(b, n, 3, 4); }
195    static boolean endSigAt(byte[] b, int n) { return pkSigAt(b, n, 5, 6); }
196    static boolean extSigAt(byte[] b, int n) { return pkSigAt(b, n, 7, 8); }
197    static boolean end64SigAt(byte[] b, int n) { return pkSigAt(b, n, 6, 6); }
198    static boolean locator64SigAt(byte[] b, int n) { return pkSigAt(b, n, 6, 7); }
199
200    // local file (LOC) header fields
201    static final long LOCSIG(byte[] b) { return LG(b, 0); } // signature
202    static final int  LOCVER(byte[] b) { return SH(b, 4); } // version needed to extract
203    static final int  LOCFLG(byte[] b) { return SH(b, 6); } // general purpose bit flags
204    static final int  LOCHOW(byte[] b) { return SH(b, 8); } // compression method
205    static final long LOCTIM(byte[] b) { return LG(b, 10);} // modification time
206    static final long LOCCRC(byte[] b) { return LG(b, 14);} // crc of uncompressed data
207    static final long LOCSIZ(byte[] b) { return LG(b, 18);} // compressed data size
208    static final long LOCLEN(byte[] b) { return LG(b, 22);} // uncompressed data size
209    static final int  LOCNAM(byte[] b) { return SH(b, 26);} // filename length
210    static final int  LOCEXT(byte[] b) { return SH(b, 28);} // extra field length
211
212    // extra local (EXT) header fields
213    static final long EXTCRC(byte[] b) { return LG(b, 4);}  // crc of uncompressed data
214    static final long EXTSIZ(byte[] b) { return LG(b, 8);}  // compressed size
215    static final long EXTLEN(byte[] b) { return LG(b, 12);} // uncompressed size
216
217    // end of central directory header (END) fields
218    static final int  ENDSUB(byte[] b) { return SH(b, 8); }  // number of entries on this disk
219    static final int  ENDTOT(byte[] b) { return SH(b, 10);}  // total number of entries
220    static final long ENDSIZ(byte[] b) { return LG(b, 12);}  // central directory size
221    static final long ENDOFF(byte[] b) { return LG(b, 16);}  // central directory offset
222    static final int  ENDCOM(byte[] b) { return SH(b, 20);}  // size of zip file comment
223    static final int  ENDCOM(byte[] b, int off) { return SH(b, off + 20);}
224
225    // zip64 end of central directory recoder fields
226    static final long ZIP64_ENDTOD(byte[] b) { return LL(b, 24);}  // total number of entries on disk
227    static final long ZIP64_ENDTOT(byte[] b) { return LL(b, 32);}  // total number of entries
228    static final long ZIP64_ENDSIZ(byte[] b) { return LL(b, 40);}  // central directory size
229    static final long ZIP64_ENDOFF(byte[] b) { return LL(b, 48);}  // central directory offset
230    static final long ZIP64_LOCOFF(byte[] b) { return LL(b, 8);}   // zip64 end offset
231
232    // central directory header (CEN) fields
233    static final long CENSIG(byte[] b, int pos) { return LG(b, pos + 0); }
234    static final int  CENVEM(byte[] b, int pos) { return SH(b, pos + 4); }
235    static final int  CENVER(byte[] b, int pos) { return SH(b, pos + 6); }
236    static final int  CENFLG(byte[] b, int pos) { return SH(b, pos + 8); }
237    static final int  CENHOW(byte[] b, int pos) { return SH(b, pos + 10);}
238    static final long CENTIM(byte[] b, int pos) { return LG(b, pos + 12);}
239    static final long CENCRC(byte[] b, int pos) { return LG(b, pos + 16);}
240    static final long CENSIZ(byte[] b, int pos) { return LG(b, pos + 20);}
241    static final long CENLEN(byte[] b, int pos) { return LG(b, pos + 24);}
242    static final int  CENNAM(byte[] b, int pos) { return SH(b, pos + 28);}
243    static final int  CENEXT(byte[] b, int pos) { return SH(b, pos + 30);}
244    static final int  CENCOM(byte[] b, int pos) { return SH(b, pos + 32);}
245    static final int  CENDSK(byte[] b, int pos) { return SH(b, pos + 34);}
246    static final int  CENATT(byte[] b, int pos) { return SH(b, pos + 36);}
247    static final long CENATX(byte[] b, int pos) { return LG(b, pos + 38);}
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}
254