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