1 /* 2 * Copyright (c) 1995, 2018, 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 /* 27 * Prototypes for zip file support 28 */ 29 30 #ifndef _ZIP_H_ 31 #define _ZIP_H_ 32 33 /* 34 * Header signatures 35 */ 36 #define PKZIP_SIGNATURE_AT(p, b2, b3) \ 37 (((p)[0] == 'P') & ((p)[1] == 'K') & ((p)[2] == b2) & ((p)[3] == b3)) 38 #define CENSIG_AT(p) PKZIP_SIGNATURE_AT(p, 1, 2) 39 #define LOCSIG_AT(p) PKZIP_SIGNATURE_AT(p, 3, 4) 40 #define ENDSIG_AT(p) PKZIP_SIGNATURE_AT(p, 5, 6) 41 #define EXTSIG_AT(p) PKZIP_SIGNATURE_AT(p, 7, 8) 42 #define ZIP64_ENDSIG_AT(p) PKZIP_SIGNATURE_AT(p, 6, 6) 43 #define ZIP64_LOCSIG_AT(p) PKZIP_SIGNATURE_AT(p, 6, 7) 44 45 /* 46 * Header sizes including signatures 47 */ 48 49 #define LOCHDR 30 50 #define EXTHDR 16 51 #define CENHDR 46 52 #define ENDHDR 22 53 54 #define ZIP64_ENDHDR 56 // ZIP64 end header size 55 #define ZIP64_LOCHDR 20 // ZIP64 end loc header size 56 #define ZIP64_EXTHDR 24 // EXT header size 57 #define ZIP64_EXTID 1 // Extra field Zip64 header ID 58 59 #define ZIP64_MAGICVAL 0xffffffffLL 60 #define ZIP64_MAGICCOUNT 0xffff 61 62 63 /* 64 * Header field access macros 65 */ 66 #define CH(b, n) (((unsigned char *)(b))[n]) 67 #define SH(b, n) (CH(b, n) | (CH(b, n+1) << 8)) 68 #define LG(b, n) ((SH(b, n) | (SH(b, n+2) << 16)) &0xffffffffUL) 69 #define LL(b, n) (((jlong)LG(b, n)) | (((jlong)LG(b, n+4)) << 32)) 70 #define GETSIG(b) LG(b, 0) 71 72 /* 73 * Macros for getting local file (LOC) header fields 74 */ 75 #define LOCVER(b) SH(b, 4) /* version needed to extract */ 76 #define LOCFLG(b) SH(b, 6) /* general purpose bit flags */ 77 #define LOCHOW(b) SH(b, 8) /* compression method */ 78 #define LOCTIM(b) LG(b, 10) /* modification time */ 79 #define LOCCRC(b) LG(b, 14) /* crc of uncompressed data */ 80 #define LOCSIZ(b) LG(b, 18) /* compressed data size */ 81 #define LOCLEN(b) LG(b, 22) /* uncompressed data size */ 82 #define LOCNAM(b) SH(b, 26) /* filename length */ 83 #define LOCEXT(b) SH(b, 28) /* extra field length */ 84 85 /* 86 * Macros for getting extra local (EXT) header fields 87 */ 88 #define EXTCRC(b) LG(b, 4) /* crc of uncompressed data */ 89 #define EXTSIZ(b) LG(b, 8) /* compressed size */ 90 #define EXTLEN(b) LG(b, 12) /* uncompressed size */ 91 92 /* 93 * Macros for getting central directory header (CEN) fields 94 */ 95 #define CENVEM(b) SH(b, 4) /* version made by */ 96 #define CENVER(b) SH(b, 6) /* version needed to extract */ 97 #define CENFLG(b) SH(b, 8) /* general purpose bit flags */ 98 #define CENHOW(b) SH(b, 10) /* compression method */ 99 #define CENTIM(b) LG(b, 12) /* modification time */ 100 #define CENCRC(b) LG(b, 16) /* crc of uncompressed data */ 101 #define CENSIZ(b) LG(b, 20) /* compressed size */ 102 #define CENLEN(b) LG(b, 24) /* uncompressed size */ 103 #define CENNAM(b) SH(b, 28) /* length of filename */ 104 #define CENEXT(b) SH(b, 30) /* length of extra field */ 105 #define CENCOM(b) SH(b, 32) /* file comment length */ 106 #define CENDSK(b) SH(b, 34) /* disk number start */ 107 #define CENATT(b) SH(b, 36) /* internal file attributes */ 108 #define CENATX(b) LG(b, 38) /* external file attributes */ 109 #define CENOFF(b) LG(b, 42) /* offset of local header */ 110 111 /* 112 * Macros for getting end of central directory header (END) fields 113 */ 114 #define ENDSUB(b) SH(b, 8) /* number of entries on this disk */ 115 #define ENDTOT(b) SH(b, 10) /* total number of entries */ 116 #define ENDSIZ(b) LG(b, 12) /* central directory size */ 117 #define ENDOFF(b) LG(b, 16) /* central directory offset */ 118 #define ENDCOM(b) SH(b, 20) /* size of zip file comment */ 119 120 /* 121 * Macros for getting Zip64 end of central directory header fields 122 */ 123 #define ZIP64_ENDLEN(b) LL(b, 4) /* size of zip64 end of central dir */ 124 #define ZIP64_ENDVEM(b) SH(b, 12) /* version made by */ 125 #define ZIP64_ENDVER(b) SH(b, 14) /* version needed to extract */ 126 #define ZIP64_ENDNMD(b) LG(b, 16) /* number of this disk */ 127 #define ZIP64_ENDDSK(b) LG(b, 20) /* disk number of start */ 128 #define ZIP64_ENDTOD(b) LL(b, 24) /* total number of entries on this disk */ 129 #define ZIP64_ENDTOT(b) LL(b, 32) /* total number of entries */ 130 #define ZIP64_ENDSIZ(b) LL(b, 40) /* central directory size in bytes */ 131 #define ZIP64_ENDOFF(b) LL(b, 48) /* offset of first CEN header */ 132 133 /* 134 * Macros for getting Zip64 end of central directory locator fields 135 */ 136 #define ZIP64_LOCDSK(b) LG(b, 4) /* disk number start */ 137 #define ZIP64_LOCOFF(b) LL(b, 8) /* offset of zip64 end */ 138 #define ZIP64_LOCTOT(b) LG(b, 16) /* total number of disks */ 139 140 /* 141 * Supported compression methods 142 */ 143 #define STORED 0 144 #define DEFLATED 8 145 146 /* 147 * Support for reading ZIP/JAR files. Some things worth noting: 148 * 149 * - Zip file entries larger than 2**32 bytes are not supported. 150 * - jzentry time and crc fields are signed even though they really 151 * represent unsigned quantities. 152 * - If csize is zero then the entry is uncompressed. 153 * - If extra != 0 then the first two bytes are the length of the extra 154 * data in intel byte order. 155 * - If pos <= 0 then it is the position of entry LOC header. 156 * If pos > 0 then it is the position of entry data. 157 * pos should not be accessed directly, but only by ZIP_GetEntryDataOffset. 158 * - entry name may include embedded null character, use nlen for length 159 */ 160 161 typedef struct jzentry { /* Zip file entry */ 162 char *name; /* entry name */ 163 jlong time; /* modification time */ 164 jlong size; /* size of uncompressed data */ 165 jlong csize; /* size of compressed data (zero if uncompressed) */ 166 jint crc; /* crc of uncompressed data */ 167 char *comment; /* optional zip file comment */ 168 jbyte *extra; /* optional extra data */ 169 jlong pos; /* position of LOC header or entry data */ 170 jint flag; /* general purpose flag */ 171 jint nlen; /* length of the entry name */ 172 } jzentry; 173 174 /* 175 * In-memory hash table cell. 176 * In a typical system we have a *lot* of these, as we have one for 177 * every entry in every active JAR. 178 * Note that in order to save space we don't keep the name in memory, 179 * but merely remember a 32 bit hash. 180 */ 181 typedef struct jzcell { 182 unsigned int hash; /* 32 bit hashcode on name */ 183 unsigned int next; /* hash chain: index into jzfile->entries */ 184 jlong cenpos; /* Offset of central directory file header */ 185 } jzcell; 186 187 typedef struct cencache { 188 char *data; /* A cached page of CEN headers */ 189 jlong pos; /* file offset of data */ 190 } cencache; 191 192 /* 193 * Use ZFILE to represent access to a file in a platform-indepenent 194 * fashion. 195 */ 196 #ifdef WIN32 197 #define ZFILE jlong 198 #else 199 #define ZFILE int 200 #endif 201 202 /* 203 * Use mmap for CEN & ENDHDR sections 204 */ 205 #define USE_MMAP 1 206 207 /* 208 * Descriptor for a ZIP file. 209 */ 210 typedef struct jzfile { /* Zip file */ 211 char *name; /* zip file name */ 212 jint refs; /* number of active references */ 213 jlong len; /* length (in bytes) of zip file */ 214 #ifdef USE_MMAP 215 unsigned char *maddr; /* beginning address of the CEN & ENDHDR */ 216 jlong mlen; /* length (in bytes) mmaped */ 217 jlong offset; /* offset of the mmapped region from the 218 start of the file. */ 219 jboolean usemmap; /* if mmap is used. */ 220 #endif 221 jboolean locsig; /* if zip file starts with LOCSIG */ 222 cencache cencache; /* CEN header cache */ 223 ZFILE zfd; /* open file descriptor */ 224 void *lock; /* read lock */ 225 char *comment; /* zip file comment */ 226 jint clen; /* length of the zip file comment */ 227 char *msg; /* zip error message */ 228 jzcell *entries; /* array of hash cells */ 229 jint total; /* total number of entries */ 230 jint *table; /* Hash chain heads: indexes into entries */ 231 jint tablelen; /* number of hash heads */ 232 struct jzfile *next; /* next zip file in search list */ 233 jzentry *cache; /* we cache the most recently freed jzentry */ 234 /* Information on metadata names in META-INF directory */ 235 char **metanames; /* array of meta names (may have null names) */ 236 jint metacurrent; /* the next empty slot in metanames array */ 237 jint metacount; /* number of slots in metanames array */ 238 jlong lastModified; /* last modified time */ 239 jlong locpos; /* position of first LOC header (usually 0) */ 240 } jzfile; 241 242 /* 243 * Index representing end of hash chain 244 */ 245 #define ZIP_ENDCHAIN ((jint)-1) 246 247 JNIEXPORT jzentry * 248 ZIP_FindEntry(jzfile *zip, char *name, jint *sizeP, jint *nameLenP); 249 250 JNIEXPORT jboolean 251 ZIP_ReadEntry(jzfile *zip, jzentry *entry, unsigned char *buf, char *entrynm); 252 253 JNIEXPORT jzentry * 254 ZIP_GetNextEntry(jzfile *zip, jint n); 255 256 JNIEXPORT jzfile * 257 ZIP_Open(JNIEnv *env, jobject thiz, const char *name, char **pmsg); 258 259 jzfile * 260 ZIP_Open_Generic(JNIEnv *env, jobject thiz, const char *name, char **pmsg, int mode, jlong lastModified); 261 262 jzfile * 263 ZIP_Get_From_Cache(const char *name, char **pmsg, jlong lastModified); 264 265 jzfile * 266 ZIP_Put_In_Cache(JNIEnv *env, jobject thiz, const char *name, ZFILE zfd, char **pmsg, jlong lastModified); 267 268 jzfile * 269 ZIP_Put_In_Cache0(JNIEnv *env, jobject thiz, const char *name, ZFILE zfd, char **pmsg, jlong lastModified, jboolean usemmap); 270 271 JNIEXPORT void 272 ZIP_Close(jzfile *zip); 273 274 jzentry * 275 ZIP_GetEntry(jzfile *zip, char *name, jint ulen); 276 void 277 ZIP_Lock(jzfile *zip); 278 void 279 ZIP_Unlock(jzfile *zip); 280 jint 281 ZIP_Read(jzfile *zip, jzentry *entry, jlong pos, void *buf, jint len); 282 void 283 ZIP_FreeEntry(jzfile *zip, jzentry *ze); 284 jlong ZIP_GetEntryDataOffset(jzfile *zip, jzentry *entry); 285 jzentry * ZIP_GetEntry2(jzfile *zip, char *name, jint ulen, jboolean addSlash); 286 287 // Android-removed; this method is used outside of java.util.zip and 288 // that code is absent from Android. 289 /* 290 JNIEXPORT jboolean 291 ZIP_InflateFully(void *inBuf, jlong inLen, void *outBuf, jlong outLen, char **pmsg); 292 */ 293 294 jboolean ZIP_OnZipEntryAccess(JNIEnv *env, jobject thiz, const char* entryName, int len, jint flag); 295 #endif /* !_ZIP_H_ */ 296