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