1 /* 2 * Copyright (c) 2009, 2012, Oracle and/or its affiliates. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 8 * - Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 11 * - Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * - Neither the name of Oracle nor the names of its 16 * contributors may be used to endorse or promote products derived 17 * from this software without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 20 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 26 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 27 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 28 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 29 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 /* 33 * This source code is provided to illustrate the usage of a given feature 34 * or technique and has been deliberately simplified. Additional steps 35 * required for a production-quality application, such as security checks, 36 * input validation and proper error handling, might not be present in 37 * this sample code. 38 */ 39 40 41 package com.sun.nio.zipfs; 42 43 44 /** 45 * 46 * @author Xueming Shen 47 */ 48 49 class ZipConstants { 50 /* 51 * Compression methods 52 */ 53 static final int METHOD_STORED = 0; 54 static final int METHOD_DEFLATED = 8; 55 static final int METHOD_DEFLATED64 = 9; 56 static final int METHOD_BZIP2 = 12; 57 static final int METHOD_LZMA = 14; 58 static final int METHOD_LZ77 = 19; 59 static final int METHOD_AES = 99; 60 61 /* 62 * General purpose big flag 63 */ 64 static final int FLAG_ENCRYPTED = 0x01; 65 static final int FLAG_DATADESCR = 0x08; // crc, size and csize in dd 66 static final int FLAG_EFS = 0x800; // If this bit is set the filename and 67 // comment fields for this file must be 68 // encoded using UTF-8. 69 /* 70 * Header signatures 71 */ 72 static long LOCSIG = 0x04034b50L; // "PK\003\004" 73 static long EXTSIG = 0x08074b50L; // "PK\007\008" 74 static long CENSIG = 0x02014b50L; // "PK\001\002" 75 static long ENDSIG = 0x06054b50L; // "PK\005\006" 76 77 /* 78 * Header sizes in bytes (including signatures) 79 */ 80 static final int LOCHDR = 30; // LOC header size 81 static final int EXTHDR = 16; // EXT header size 82 static final int CENHDR = 46; // CEN header size 83 static final int ENDHDR = 22; // END header size 84 85 /* 86 * Local file (LOC) header field offsets 87 */ 88 static final int LOCVER = 4; // version needed to extract 89 static final int LOCFLG = 6; // general purpose bit flag 90 static final int LOCHOW = 8; // compression method 91 static final int LOCTIM = 10; // modification time 92 static final int LOCCRC = 14; // uncompressed file crc-32 value 93 static final int LOCSIZ = 18; // compressed size 94 static final int LOCLEN = 22; // uncompressed size 95 static final int LOCNAM = 26; // filename length 96 static final int LOCEXT = 28; // extra field length 97 98 /* 99 * Extra local (EXT) header field offsets 100 */ 101 static final int EXTCRC = 4; // uncompressed file crc-32 value 102 static final int EXTSIZ = 8; // compressed size 103 static final int EXTLEN = 12; // uncompressed size 104 105 /* 106 * Central directory (CEN) header field offsets 107 */ 108 static final int CENVEM = 4; // version made by 109 static final int CENVER = 6; // version needed to extract 110 static final int CENFLG = 8; // encrypt, decrypt flags 111 static final int CENHOW = 10; // compression method 112 static final int CENTIM = 12; // modification time 113 static final int CENCRC = 16; // uncompressed file crc-32 value 114 static final int CENSIZ = 20; // compressed size 115 static final int CENLEN = 24; // uncompressed size 116 static final int CENNAM = 28; // filename length 117 static final int CENEXT = 30; // extra field length 118 static final int CENCOM = 32; // comment length 119 static final int CENDSK = 34; // disk number start 120 static final int CENATT = 36; // internal file attributes 121 static final int CENATX = 38; // external file attributes 122 static final int CENOFF = 42; // LOC header offset 123 124 /* 125 * End of central directory (END) header field offsets 126 */ 127 static final int ENDSUB = 8; // number of entries on this disk 128 static final int ENDTOT = 10; // total number of entries 129 static final int ENDSIZ = 12; // central directory size in bytes 130 static final int ENDOFF = 16; // offset of first CEN header 131 static final int ENDCOM = 20; // zip file comment length 132 133 /* 134 * ZIP64 constants 135 */ 136 static final long ZIP64_ENDSIG = 0x06064b50L; // "PK\006\006" 137 static final long ZIP64_LOCSIG = 0x07064b50L; // "PK\006\007" 138 static final int ZIP64_ENDHDR = 56; // ZIP64 end header size 139 static final int ZIP64_LOCHDR = 20; // ZIP64 end loc header size 140 static final int ZIP64_EXTHDR = 24; // EXT header size 141 static final int ZIP64_EXTID = 0x0001; // Extra field Zip64 header ID 142 143 static final int ZIP64_MINVAL32 = 0xFFFF; 144 static final long ZIP64_MINVAL = 0xFFFFFFFFL; 145 146 /* 147 * Zip64 End of central directory (END) header field offsets 148 */ 149 static final int ZIP64_ENDLEN = 4; // size of zip64 end of central dir 150 static final int ZIP64_ENDVEM = 12; // version made by 151 static final int ZIP64_ENDVER = 14; // version needed to extract 152 static final int ZIP64_ENDNMD = 16; // number of this disk 153 static final int ZIP64_ENDDSK = 20; // disk number of start 154 static final int ZIP64_ENDTOD = 24; // total number of entries on this disk 155 static final int ZIP64_ENDTOT = 32; // total number of entries 156 static final int ZIP64_ENDSIZ = 40; // central directory size in bytes 157 static final int ZIP64_ENDOFF = 48; // offset of first CEN header 158 static final int ZIP64_ENDEXT = 56; // zip64 extensible data sector 159 160 /* 161 * Zip64 End of central directory locator field offsets 162 */ 163 static final int ZIP64_LOCDSK = 4; // disk number start 164 static final int ZIP64_LOCOFF = 8; // offset of zip64 end 165 static final int ZIP64_LOCTOT = 16; // total number of disks 166 167 /* 168 * Zip64 Extra local (EXT) header field offsets 169 */ 170 static final int ZIP64_EXTCRC = 4; // uncompressed file crc-32 value 171 static final int ZIP64_EXTSIZ = 8; // compressed size, 8-byte 172 static final int ZIP64_EXTLEN = 16; // uncompressed size, 8-byte 173 174 /* 175 * Extra field header ID 176 */ 177 static final int EXTID_ZIP64 = 0x0001; // ZIP64 178 static final int EXTID_NTFS = 0x000a; // NTFS 179 static final int EXTID_UNIX = 0x000d; // UNIX 180 static final int EXTID_EFS = 0x0017; // Strong Encryption 181 static final int EXTID_EXTT = 0x5455; // Info-ZIP Extended Timestamp 182 183 /* 184 * fields access methods 185 */ 186 /////////////////////////////////////////////////////// 187 static final int CH(byte[] b, int n) { 188 return Byte.toUnsignedInt(b[n]); 189 } 190 191 static final int SH(byte[] b, int n) { 192 return Byte.toUnsignedInt(b[n]) | (Byte.toUnsignedInt(b[n + 1]) << 8); 193 } 194 195 static final long LG(byte[] b, int n) { 196 return ((SH(b, n)) | (SH(b, n + 2) << 16)) & 0xffffffffL; 197 } 198 199 static final long LL(byte[] b, int n) { 200 return (LG(b, n)) | (LG(b, n + 4) << 32); 201 } 202 203 static final long GETSIG(byte[] b) { 204 return LG(b, 0); 205 } 206 207 // local file (LOC) header fields 208 static final long LOCSIG(byte[] b) { return LG(b, 0); } // signature 209 static final int LOCVER(byte[] b) { return SH(b, 4); } // version needed to extract 210 static final int LOCFLG(byte[] b) { return SH(b, 6); } // general purpose bit flags 211 static final int LOCHOW(byte[] b) { return SH(b, 8); } // compression method 212 static final long LOCTIM(byte[] b) { return LG(b, 10);} // modification time 213 static final long LOCCRC(byte[] b) { return LG(b, 14);} // crc of uncompressed data 214 static final long LOCSIZ(byte[] b) { return LG(b, 18);} // compressed data size 215 static final long LOCLEN(byte[] b) { return LG(b, 22);} // uncompressed data size 216 static final int LOCNAM(byte[] b) { return SH(b, 26);} // filename length 217 static final int LOCEXT(byte[] b) { return SH(b, 28);} // extra field length 218 219 // extra local (EXT) header fields 220 static final long EXTCRC(byte[] b) { return LG(b, 4);} // crc of uncompressed data 221 static final long EXTSIZ(byte[] b) { return LG(b, 8);} // compressed size 222 static final long EXTLEN(byte[] b) { return LG(b, 12);} // uncompressed size 223 224 // end of central directory header (END) fields 225 static final int ENDSUB(byte[] b) { return SH(b, 8); } // number of entries on this disk 226 static final int ENDTOT(byte[] b) { return SH(b, 10);} // total number of entries 227 static final long ENDSIZ(byte[] b) { return LG(b, 12);} // central directory size 228 static final long ENDOFF(byte[] b) { return LG(b, 16);} // central directory offset 229 static final int ENDCOM(byte[] b) { return SH(b, 20);} // size of zip file comment 230 static final int ENDCOM(byte[] b, int off) { return SH(b, off + 20);} 231 232 // zip64 end of central directory recoder fields 233 static final long ZIP64_ENDTOD(byte[] b) { return LL(b, 24);} // total number of entries on disk 234 static final long ZIP64_ENDTOT(byte[] b) { return LL(b, 32);} // total number of entries 235 static final long ZIP64_ENDSIZ(byte[] b) { return LL(b, 40);} // central directory size 236 static final long ZIP64_ENDOFF(byte[] b) { return LL(b, 48);} // central directory offset 237 static final long ZIP64_LOCOFF(byte[] b) { return LL(b, 8);} // zip64 end offset 238 239 // central directory header (CEN) fields 240 static final long CENSIG(byte[] b, int pos) { return LG(b, pos + 0); } 241 static final int CENVEM(byte[] b, int pos) { return SH(b, pos + 4); } 242 static final int CENVER(byte[] b, int pos) { return SH(b, pos + 6); } 243 static final int CENFLG(byte[] b, int pos) { return SH(b, pos + 8); } 244 static final int CENHOW(byte[] b, int pos) { return SH(b, pos + 10);} 245 static final long CENTIM(byte[] b, int pos) { return LG(b, pos + 12);} 246 static final long CENCRC(byte[] b, int pos) { return LG(b, pos + 16);} 247 static final long CENSIZ(byte[] b, int pos) { return LG(b, pos + 20);} 248 static final long CENLEN(byte[] b, int pos) { return LG(b, pos + 24);} 249 static final int CENNAM(byte[] b, int pos) { return SH(b, pos + 28);} 250 static final int CENEXT(byte[] b, int pos) { return SH(b, pos + 30);} 251 static final int CENCOM(byte[] b, int pos) { return SH(b, pos + 32);} 252 static final int CENDSK(byte[] b, int pos) { return SH(b, pos + 34);} 253 static final int CENATT(byte[] b, int pos) { return SH(b, pos + 36);} 254 static final long CENATX(byte[] b, int pos) { return LG(b, pos + 38);} 255 static final long CENOFF(byte[] b, int pos) { return LG(b, pos + 42);} 256 257 /* The END header is followed by a variable length comment of size < 64k. */ 258 static final long END_MAXLEN = 0xFFFF + ENDHDR; 259 static final int READBLOCKSZ = 128; 260 }