1 /* 2 * Copyright (c) 1998, 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 26 /* 27 * Native method support for java.util.zip.ZipFile 28 */ 29 30 #include <stdio.h> 31 #include <stdlib.h> 32 #include <string.h> 33 #include <errno.h> 34 #include <ctype.h> 35 #include <assert.h> 36 #include "jlong.h" 37 #include "jvm.h" 38 #include "jni.h" 39 #include "jni_util.h" 40 #include "zip_util.h" 41 #ifdef WIN32 42 #include "io_util_md.h" 43 #else 44 #include "io_util.h" 45 #endif 46 47 #include "java_util_zip_ZipFile.h" 48 #include "java_util_jar_JarFile.h" 49 50 #define DEFLATED 8 51 #define STORED 0 52 53 static jfieldID jzfileID; 54 55 static int OPEN_READ = java_util_zip_ZipFile_OPEN_READ; 56 static int OPEN_DELETE = java_util_zip_ZipFile_OPEN_DELETE; 57 58 JNIEXPORT void JNICALL 59 Java_java_util_zip_ZipFile_initIDs(JNIEnv *env, jclass cls) 60 { 61 jzfileID = (*env)->GetFieldID(env, cls, "jzfile", "J"); 62 assert(jzfileID != 0); 63 } 64 65 static void 66 ThrowZipException(JNIEnv *env, const char *msg) 67 { 68 jstring s = NULL; 69 jobject x; 70 71 if (msg != NULL) { 72 s = JNU_NewStringPlatform(env, msg); 73 } 74 if (s != NULL) { 75 x = JNU_NewObjectByName(env, 76 "java/util/zip/ZipException", 77 "(Ljava/lang/String;)V", s); 78 if (x != NULL) { 79 (*env)->Throw(env, x); 80 } 81 } 82 } 83 84 JNIEXPORT jlong JNICALL 85 Java_java_util_zip_ZipFile_open(JNIEnv *env, jclass cls, jstring name, 86 jint mode, jlong lastModified, 87 jboolean usemmap, 88 jobjectArray tableAndEntriesByteBuffers) 89 { 90 const char *path = JNU_GetStringPlatformChars(env, name, 0); 91 char *msg = 0; 92 jlong result = 0; 93 jobject tableByteBuffer; 94 jobject entriesByteBuffer; 95 int flag = 0; 96 jzfile *zip = 0; 97 98 if (mode & OPEN_READ) flag |= O_RDONLY; 99 100 if (path != 0) { 101 zip = ZIP_Get_From_Cache(path, &msg, lastModified); 102 if (zip == 0 && msg == 0) { 103 ZFILE zfd = 0; 104 #ifdef WIN32 105 if (mode & OPEN_DELETE) flag |= O_TEMPORARY; 106 zfd = winFileHandleOpen(env, name, flag); 107 if (zfd == -1) { 108 /* Exception already pending. */ 109 goto finally; 110 } 111 #else 112 zfd = open(path, flag, 0); 113 if (zfd < 0) { 114 throwFileNotFoundException(env, name); 115 goto finally; 116 } 117 if (mode & OPEN_DELETE) { 118 unlink(path); 119 } 120 #endif 121 zip = ZIP_Put_In_Cache0(path, zfd, &msg, lastModified, usemmap); 122 } 123 124 if (zip != 0) { 125 result = ptr_to_jlong(zip); 126 if (tableAndEntriesByteBuffers != NULL) { 127 tableByteBuffer = (*env)->NewDirectByteBuffer(env, zip->table, (jlong) zip->tablelen * sizeof(zip->table[0])); 128 if (tableByteBuffer == NULL) goto finally; 129 entriesByteBuffer = (*env)->NewDirectByteBuffer(env, zip->entries, (jlong) zip->total * sizeof(zip->entries[0])); 130 if (tableByteBuffer == NULL) goto finally; 131 (*env)->SetObjectArrayElement(env, tableAndEntriesByteBuffers, 0, tableByteBuffer); 132 if ((*env)->ExceptionCheck(env)) goto finally; 133 (*env)->SetObjectArrayElement(env, tableAndEntriesByteBuffers, 1, entriesByteBuffer); 134 } 135 } else if (msg != 0) { 136 ThrowZipException(env, msg); 137 free(msg); 138 } else if (errno == ENOMEM) { 139 JNU_ThrowOutOfMemoryError(env, 0); 140 } else { 141 ThrowZipException(env, "error in opening zip file"); 142 } 143 finally: 144 JNU_ReleaseStringPlatformChars(env, name, path); 145 } 146 return result; 147 } 148 149 JNIEXPORT jint JNICALL 150 Java_java_util_zip_ZipFile_getTotal(JNIEnv *env, jclass cls, jlong zfile) 151 { 152 jzfile *zip = jlong_to_ptr(zfile); 153 154 return zip->total; 155 } 156 157 JNIEXPORT jboolean JNICALL 158 Java_java_util_zip_ZipFile_startsWithLOC(JNIEnv *env, jclass cls, jlong zfile) 159 { 160 jzfile *zip = jlong_to_ptr(zfile); 161 162 return zip->locsig; 163 } 164 165 JNIEXPORT void JNICALL 166 Java_java_util_zip_ZipFile_close(JNIEnv *env, jclass cls, jlong zfile) 167 { 168 ZIP_Close(jlong_to_ptr(zfile)); 169 } 170 171 JNIEXPORT jlong JNICALL 172 Java_java_util_zip_ZipFile_getEntry(JNIEnv *env, jclass cls, jlong zfile, 173 jbyteArray name, jboolean addSlash) 174 { 175 #define MAXNAME 1024 176 jzfile *zip = jlong_to_ptr(zfile); 177 jsize ulen = (*env)->GetArrayLength(env, name); 178 char buf[MAXNAME+2], *path; 179 jzentry *ze; 180 181 if (ulen > MAXNAME) { 182 path = malloc(ulen + 2); 183 if (path == 0) { 184 JNU_ThrowOutOfMemoryError(env, 0); 185 return 0; 186 } 187 } else { 188 path = buf; 189 } 190 (*env)->GetByteArrayRegion(env, name, 0, ulen, (jbyte *)path); 191 path[ulen] = '\0'; 192 ze = ZIP_GetEntry2(zip, path, (jint)ulen, addSlash); 193 if (path != buf) { 194 free(path); 195 } 196 return ptr_to_jlong(ze); 197 } 198 199 JNIEXPORT void JNICALL 200 Java_java_util_zip_ZipFile_freeEntry(JNIEnv *env, jclass cls, jlong zfile, 201 jlong zentry) 202 { 203 jzfile *zip = jlong_to_ptr(zfile); 204 jzentry *ze = jlong_to_ptr(zentry); 205 ZIP_FreeEntry(zip, ze); 206 } 207 208 JNIEXPORT jlong JNICALL 209 Java_java_util_zip_ZipFile_getNextEntry(JNIEnv *env, jclass cls, jlong zfile, 210 jint n) 211 { 212 jzentry *ze = ZIP_GetNextEntry(jlong_to_ptr(zfile), n); 213 return ptr_to_jlong(ze); 214 } 215 216 JNIEXPORT jint JNICALL 217 Java_java_util_zip_ZipFile_getEntryMethod(JNIEnv *env, jclass cls, jlong zentry) 218 { 219 jzentry *ze = jlong_to_ptr(zentry); 220 return ze->csize != 0 ? DEFLATED : STORED; 221 } 222 223 JNIEXPORT jint JNICALL 224 Java_java_util_zip_ZipFile_getEntryFlag(JNIEnv *env, jclass cls, jlong zentry) 225 { 226 jzentry *ze = jlong_to_ptr(zentry); 227 return ze->flag; 228 } 229 230 JNIEXPORT jlong JNICALL 231 Java_java_util_zip_ZipFile_getEntryCSize(JNIEnv *env, jclass cls, jlong zentry) 232 { 233 jzentry *ze = jlong_to_ptr(zentry); 234 return ze->csize != 0 ? ze->csize : ze->size; 235 } 236 237 JNIEXPORT jlong JNICALL 238 Java_java_util_zip_ZipFile_getEntrySize(JNIEnv *env, jclass cls, jlong zentry) 239 { 240 jzentry *ze = jlong_to_ptr(zentry); 241 return ze->size; 242 } 243 244 JNIEXPORT jlong JNICALL 245 Java_java_util_zip_ZipFile_getEntryTime(JNIEnv *env, jclass cls, jlong zentry) 246 { 247 jzentry *ze = jlong_to_ptr(zentry); 248 return (jlong)ze->time & 0xffffffffUL; 249 } 250 251 JNIEXPORT jlong JNICALL 252 Java_java_util_zip_ZipFile_getEntryCrc(JNIEnv *env, jclass cls, jlong zentry) 253 { 254 jzentry *ze = jlong_to_ptr(zentry); 255 return (jlong)ze->crc & 0xffffffffUL; 256 } 257 258 JNIEXPORT jbyteArray JNICALL 259 Java_java_util_zip_ZipFile_getCommentBytes(JNIEnv *env, 260 jclass cls, 261 jlong zfile) 262 { 263 jzfile *zip = jlong_to_ptr(zfile); 264 jbyteArray jba = NULL; 265 266 if (zip->comment != NULL) { 267 if ((jba = (*env)->NewByteArray(env, zip->clen)) == NULL) 268 return NULL; 269 (*env)->SetByteArrayRegion(env, jba, 0, zip->clen, (jbyte*)zip->comment); 270 } 271 return jba; 272 } 273 274 JNIEXPORT jbyteArray JNICALL 275 Java_java_util_zip_ZipFile_getEntryBytes(JNIEnv *env, 276 jclass cls, 277 jlong zentry, jint type) 278 { 279 jzentry *ze = jlong_to_ptr(zentry); 280 int len = 0; 281 jbyteArray jba = NULL; 282 switch (type) { 283 case java_util_zip_ZipFile_JZENTRY_NAME: 284 if (ze->name != 0) { 285 len = (int)ze->nlen; 286 // Unlike for extra and comment, we never return null for 287 // an (extremely rarely seen) empty name 288 if ((jba = (*env)->NewByteArray(env, len)) == NULL) 289 break; 290 (*env)->SetByteArrayRegion(env, jba, 0, len, (jbyte *)ze->name); 291 } 292 break; 293 case java_util_zip_ZipFile_JZENTRY_EXTRA: 294 if (ze->extra != 0) { 295 unsigned char *bp = (unsigned char *)&ze->extra[0]; 296 len = (bp[0] | (bp[1] << 8)); 297 if (len <= 0 || (jba = (*env)->NewByteArray(env, len)) == NULL) 298 break; 299 (*env)->SetByteArrayRegion(env, jba, 0, len, &ze->extra[2]); 300 } 301 break; 302 case java_util_zip_ZipFile_JZENTRY_COMMENT: 303 if (ze->comment != 0) { 304 len = (int)strlen(ze->comment); 305 if (len == 0 || (jba = (*env)->NewByteArray(env, len)) == NULL) 306 break; 307 (*env)->SetByteArrayRegion(env, jba, 0, len, (jbyte*)ze->comment); 308 } 309 break; 310 } 311 return jba; 312 } 313 314 JNIEXPORT jint JNICALL 315 Java_java_util_zip_ZipFile_read(JNIEnv *env, jclass cls, jlong zfile, 316 jlong zentry, jlong pos, jbyteArray bytes, 317 jint off, jint len) 318 { 319 jzfile *zip = jlong_to_ptr(zfile); 320 char *msg; 321 322 #define BUFSIZE 8192 323 /* copy via tmp stack buffer: */ 324 jbyte buf[BUFSIZE]; 325 326 if (len > BUFSIZE) { 327 len = BUFSIZE; 328 } 329 330 ZIP_Lock(zip); 331 len = ZIP_Read(zip, jlong_to_ptr(zentry), pos, buf, len); 332 msg = zip->msg; 333 ZIP_Unlock(zip); 334 if (len != -1) { 335 (*env)->SetByteArrayRegion(env, bytes, off, len, buf); 336 } 337 338 if (len == -1) { 339 if (msg != 0) { 340 ThrowZipException(env, msg); 341 } else { 342 char errmsg[128]; 343 sprintf(errmsg, "errno: %d, error: %s\n", 344 errno, "Error reading ZIP file"); 345 JNU_ThrowIOExceptionWithLastError(env, errmsg); 346 } 347 } 348 349 return len; 350 } 351 352 /* 353 * Returns an array of strings representing the names of all entries 354 * that begin with "META-INF/" (case ignored). This native method is 355 * used in JarFile as an optimization when looking up manifest and 356 * signature file entries. Returns null if no entries were found. 357 */ 358 JNIEXPORT jobjectArray JNICALL 359 Java_java_util_jar_JarFile_getMetaInfEntryNames(JNIEnv *env, jobject obj) 360 { 361 jlong zfile = (*env)->GetLongField(env, obj, jzfileID); 362 jzfile *zip; 363 int i, count; 364 jobjectArray result = 0; 365 366 if (zfile == 0) { 367 JNU_ThrowByName(env, 368 "java/lang/IllegalStateException", "zip file closed"); 369 return NULL; 370 } 371 zip = jlong_to_ptr(zfile); 372 373 /* count the number of valid ZIP metanames */ 374 count = 0; 375 if (zip->metanames != 0) { 376 for (i = 0; i < zip->metacount; i++) { 377 if (zip->metanames[i] != 0) { 378 count++; 379 } 380 } 381 } 382 383 /* If some names were found then build array of java strings */ 384 if (count > 0) { 385 jclass cls = JNU_ClassString(env); 386 CHECK_NULL_RETURN(cls, NULL); 387 result = (*env)->NewObjectArray(env, count, cls, 0); 388 CHECK_NULL_RETURN(result, NULL); 389 if (result != 0) { 390 for (i = 0; i < count; i++) { 391 jstring str = (*env)->NewStringUTF(env, zip->metanames[i]); 392 if (str == 0) { 393 break; 394 } 395 (*env)->SetObjectArrayElement(env, result, i, str); 396 (*env)->DeleteLocalRef(env, str); 397 } 398 } 399 } 400 return result; 401 } 402 403 JNIEXPORT jstring JNICALL 404 Java_java_util_zip_ZipFile_getZipMessage(JNIEnv *env, jclass cls, jlong zfile) 405 { 406 jzfile *zip = jlong_to_ptr(zfile); 407 char *msg = zip->msg; 408 if (msg == NULL) { 409 return NULL; 410 } 411 return JNU_NewStringPlatform(env, msg); 412 }