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