java_util_zip_ZipFile.c revision ff96d13467fa65856c19aaf06b151ce60e0edd8f
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 "JNIHelp.h" 37#include "jlong.h" 38#include "jvm.h" 39#include "jni.h" 40#include "jni_util.h" 41#include "zip_util.h" 42#ifdef WIN32 43#include "io_util_md.h" 44#else 45#include "io_util.h" 46#endif 47 48#include "java_util_zip_ZipFile.h" 49 50#define NATIVE_METHOD(className, functionName, signature) \ 51{ #functionName, signature, (void*)(className ## _ ## functionName) } 52 53#define DEFLATED 8 54#define STORED 0 55 56static jfieldID jzfileID; 57 58static void ZipFile_initIDs(JNIEnv *env) 59{ 60 jclass cls = (*env)->FindClass(env, "java/util/zip/ZipFile"); 61 jzfileID = (*env)->GetFieldID(env, cls, "jzfile", "J"); 62 assert(jzfileID != 0); 63} 64 65 66static int OPEN_READ = java_util_zip_ZipFile_OPEN_READ; 67static int OPEN_DELETE = java_util_zip_ZipFile_OPEN_DELETE; 68 69static void 70ThrowZipException(JNIEnv *env, const char *msg) 71{ 72 jstring s = NULL; 73 jobject x; 74 75 if (msg != NULL) { 76 s = JNU_NewStringPlatform(env, msg); 77 } 78 x = JNU_NewObjectByName(env, 79 "java/util/zip/ZipException", 80 "(Ljava/lang/String;)V", s); 81 if (x != NULL) { 82 (*env)->Throw(env, x); 83 } 84} 85 86JNIEXPORT jlong JNICALL 87ZipFile_open(JNIEnv *env, jclass cls, jstring name, 88 jint mode, jlong lastModified, 89 jboolean usemmap) 90{ 91 const char *path = JNU_GetStringPlatformChars(env, name, 0); 92 char *msg = 0; 93 jlong result = 0; 94 int flag = 0; 95 jzfile *zip = 0; 96 97 if (mode & OPEN_READ) flag |= O_RDONLY; 98 if (mode & OPEN_DELETE) flag |= JVM_O_DELETE; 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 zfd = winFileHandleOpen(env, name, flag); 106 if (zfd == -1) { 107 /* Exception already pending. */ 108 goto finally; 109 } 110#else 111 zfd = JVM_Open(path, flag, 0); 112 if (zfd < 0) { 113 throwFileNotFoundException(env, name); 114 goto finally; 115 } 116#endif 117 zip = ZIP_Put_In_Cache0(path, zfd, &msg, lastModified, usemmap); 118 } 119 120 if (zip != 0) { 121 result = ptr_to_jlong(zip); 122 } else if (msg != 0) { 123 ThrowZipException(env, msg); 124 free(msg); 125 } else if (errno == ENOMEM) { 126 JNU_ThrowOutOfMemoryError(env, 0); 127 } else { 128 ThrowZipException(env, "error in opening zip file"); 129 } 130finally: 131 JNU_ReleaseStringPlatformChars(env, name, path); 132 } 133 return result; 134} 135 136JNIEXPORT jint JNICALL 137ZipFile_getTotal(JNIEnv *env, jclass cls, jlong zfile) 138{ 139 jzfile *zip = jlong_to_ptr(zfile); 140 141 return zip->total; 142} 143 144JNIEXPORT jboolean JNICALL 145ZipFile_startsWithLOC(JNIEnv *env, jclass cls, jlong zfile) 146{ 147 jzfile *zip = jlong_to_ptr(zfile); 148 149 return zip->locsig; 150} 151 152JNIEXPORT void JNICALL 153ZipFile_close(JNIEnv *env, jclass cls, jlong zfile) 154{ 155 ZIP_Close(jlong_to_ptr(zfile)); 156} 157 158JNIEXPORT jlong JNICALL 159ZipFile_getEntry(JNIEnv *env, jclass cls, jlong zfile, 160 jbyteArray name, jboolean addSlash) 161{ 162#define MAXNAME 1024 163 jzfile *zip = jlong_to_ptr(zfile); 164 jsize ulen = (*env)->GetArrayLength(env, name); 165 char buf[MAXNAME+2], *path; 166 jzentry *ze; 167 168 if (ulen > MAXNAME) { 169 path = malloc(ulen + 2); 170 if (path == 0) { 171 JNU_ThrowOutOfMemoryError(env, 0); 172 return 0; 173 } 174 } else { 175 path = buf; 176 } 177 (*env)->GetByteArrayRegion(env, name, 0, ulen, (jbyte *)path); 178 path[ulen] = '\0'; 179 if (addSlash == JNI_FALSE) { 180 ze = ZIP_GetEntry(zip, path, 0); 181 } else { 182 ze = ZIP_GetEntry(zip, path, (jint)ulen); 183 } 184 if (path != buf) { 185 free(path); 186 } 187 return ptr_to_jlong(ze); 188} 189 190JNIEXPORT void JNICALL 191ZipFile_freeEntry(JNIEnv *env, jclass cls, jlong zfile, 192 jlong zentry) 193{ 194 jzfile *zip = jlong_to_ptr(zfile); 195 jzentry *ze = jlong_to_ptr(zentry); 196 ZIP_FreeEntry(zip, ze); 197} 198 199JNIEXPORT jlong JNICALL 200ZipFile_getNextEntry(JNIEnv *env, jclass cls, jlong zfile, 201 jint n) 202{ 203 jzentry *ze = ZIP_GetNextEntry(jlong_to_ptr(zfile), n); 204 return ptr_to_jlong(ze); 205} 206 207JNIEXPORT jint JNICALL 208ZipFile_getEntryMethod(JNIEnv *env, jclass cls, jlong zentry) 209{ 210 jzentry *ze = jlong_to_ptr(zentry); 211 return ze->csize != 0 ? DEFLATED : STORED; 212} 213 214JNIEXPORT jint JNICALL 215ZipFile_getEntryFlag(JNIEnv *env, jclass cls, jlong zentry) 216{ 217 jzentry *ze = jlong_to_ptr(zentry); 218 return ze->flag; 219} 220 221JNIEXPORT jlong JNICALL 222ZipFile_getEntryCSize(JNIEnv *env, jclass cls, jlong zentry) 223{ 224 jzentry *ze = jlong_to_ptr(zentry); 225 return ze->csize != 0 ? ze->csize : ze->size; 226} 227 228JNIEXPORT jlong JNICALL 229ZipFile_getEntrySize(JNIEnv *env, jclass cls, jlong zentry) 230{ 231 jzentry *ze = jlong_to_ptr(zentry); 232 return ze->size; 233} 234 235JNIEXPORT jlong JNICALL 236ZipFile_getEntryTime(JNIEnv *env, jclass cls, jlong zentry) 237{ 238 jzentry *ze = jlong_to_ptr(zentry); 239 return (jlong)ze->time & 0xffffffffUL; 240} 241 242JNIEXPORT jlong JNICALL 243ZipFile_getEntryCrc(JNIEnv *env, jclass cls, jlong zentry) 244{ 245 jzentry *ze = jlong_to_ptr(zentry); 246 return (jlong)ze->crc & 0xffffffffUL; 247} 248 249JNIEXPORT jbyteArray JNICALL 250ZipFile_getCommentBytes(JNIEnv *env, jclass cls, jlong zfile) 251{ 252 jzfile *zip = jlong_to_ptr(zfile); 253 jbyteArray jba = NULL; 254 255 if (zip->comment != NULL) { 256 if ((jba = (*env)->NewByteArray(env, zip->clen)) == NULL) 257 return NULL; 258 (*env)->SetByteArrayRegion(env, jba, 0, zip->clen, (jbyte*)zip->comment); 259 } 260 return jba; 261} 262 263JNIEXPORT jbyteArray JNICALL 264ZipFile_getEntryBytes(JNIEnv *env, jclass cls, jlong zentry, jint type) 265{ 266 jzentry *ze = jlong_to_ptr(zentry); 267 int len = 0; 268 jbyteArray jba = NULL; 269 switch (type) { 270 case java_util_zip_ZipFile_JZENTRY_NAME: 271 if (ze->name != 0) { 272 len = (int)strlen(ze->name); 273 if (len == 0 || (jba = (*env)->NewByteArray(env, len)) == NULL) 274 break; 275 (*env)->SetByteArrayRegion(env, jba, 0, len, (jbyte *)ze->name); 276 } 277 break; 278 case java_util_zip_ZipFile_JZENTRY_EXTRA: 279 if (ze->extra != 0) { 280 unsigned char *bp = (unsigned char *)&ze->extra[0]; 281 len = (bp[0] | (bp[1] << 8)); 282 if (len <= 0 || (jba = (*env)->NewByteArray(env, len)) == NULL) 283 break; 284 (*env)->SetByteArrayRegion(env, jba, 0, len, &ze->extra[2]); 285 } 286 break; 287 case java_util_zip_ZipFile_JZENTRY_COMMENT: 288 if (ze->comment != 0) { 289 len = (int)strlen(ze->comment); 290 if (len == 0 || (jba = (*env)->NewByteArray(env, len)) == NULL) 291 break; 292 (*env)->SetByteArrayRegion(env, jba, 0, len, (jbyte*)ze->comment); 293 } 294 break; 295 } 296 return jba; 297} 298 299JNIEXPORT jint JNICALL 300ZipFile_read(JNIEnv *env, jclass cls, jlong zfile, 301 jlong zentry, jlong pos, jbyteArray bytes, 302 jint off, jint len) 303{ 304 jzfile *zip = jlong_to_ptr(zfile); 305 char *msg; 306 307#define BUFSIZE 8192 308 /* copy via tmp stack buffer: */ 309 jbyte buf[BUFSIZE]; 310 311 if (len > BUFSIZE) { 312 len = BUFSIZE; 313 } 314 315 ZIP_Lock(zip); 316 len = ZIP_Read(zip, jlong_to_ptr(zentry), pos, buf, len); 317 msg = zip->msg; 318 ZIP_Unlock(zip); 319 if (len != -1) { 320 (*env)->SetByteArrayRegion(env, bytes, off, len, buf); 321 } 322 323 if (len == -1) { 324 if (msg != 0) { 325 ThrowZipException(env, msg); 326 } else { 327 char errmsg[128]; 328 snprintf(errmsg, sizeof(errmsg), "errno: %d, error: %s\n", errno, 329 "Error reading ZIP file"); 330 JNU_ThrowIOExceptionWithLastError(env, errmsg); 331 } 332 } 333 334 return len; 335} 336 337JNIEXPORT jstring JNICALL 338ZipFile_getZipMessage(JNIEnv *env, jclass cls, jlong zfile) 339{ 340 jzfile *zip = jlong_to_ptr(zfile); 341 char *msg = zip->msg; 342 if (msg == NULL) { 343 return NULL; 344 } 345 return JNU_NewStringPlatform(env, msg); 346} 347 348JNIEXPORT jobjectArray JNICALL 349JarFile_getMetaInfEntryNames(JNIEnv *env, jobject obj) 350{ 351 jlong zfile = (*env)->GetLongField(env, obj, jzfileID); 352 jzfile *zip; 353 int i, count; 354 jobjectArray result = 0; 355 356 if (zfile == 0) { 357 JNU_ThrowByName(env, 358 "java/lang/IllegalStateException", "zip file closed"); 359 return NULL; 360 } 361 zip = jlong_to_ptr(zfile); 362 363 /* count the number of valid ZIP metanames */ 364 count = 0; 365 if (zip->metanames != 0) { 366 for (i = 0; i < zip->metacount; i++) { 367 if (zip->metanames[i] != 0) { 368 count++; 369 } 370 } 371 } 372 373 /* If some names were found then build array of java strings */ 374 if (count > 0) { 375 jclass cls = (*env)->FindClass(env, "java/lang/String"); 376 result = (*env)->NewObjectArray(env, count, cls, 0); 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 391static JNINativeMethod gMethods[] = { 392 NATIVE_METHOD(ZipFile, getEntry, "(J[BZ)J"), 393 NATIVE_METHOD(ZipFile, freeEntry, "(JJ)V"), 394 NATIVE_METHOD(ZipFile, getNextEntry, "(JI)J"), 395 NATIVE_METHOD(ZipFile, close, "(J)V"), 396 NATIVE_METHOD(ZipFile, open, "(Ljava/lang/String;IJZ)J"), 397 NATIVE_METHOD(ZipFile, getTotal, "(J)I"), 398 NATIVE_METHOD(ZipFile, startsWithLOC, "(J)Z"), 399 NATIVE_METHOD(ZipFile, read, "(JJJ[BII)I"), 400 NATIVE_METHOD(ZipFile, getEntryTime, "(J)J"), 401 NATIVE_METHOD(ZipFile, getEntryCrc, "(J)J"), 402 NATIVE_METHOD(ZipFile, getEntryCSize, "(J)J"), 403 NATIVE_METHOD(ZipFile, getEntrySize, "(J)J"), 404 NATIVE_METHOD(ZipFile, getEntryMethod, "(J)I"), 405 NATIVE_METHOD(ZipFile, getEntryFlag, "(J)I"), 406 NATIVE_METHOD(ZipFile, getCommentBytes, "(J)[B"), 407 NATIVE_METHOD(ZipFile, getEntryBytes, "(JI)[B"), 408 NATIVE_METHOD(ZipFile, getZipMessage, "(J)Ljava/lang/String;"), 409}; 410 411static JNINativeMethod gJarFileMethods[] = { 412 NATIVE_METHOD(JarFile, getMetaInfEntryNames, "()[Ljava/lang/String;"), 413}; 414 415void register_java_util_zip_ZipFile(JNIEnv* env) { 416 jniRegisterNativeMethods(env, "java/util/zip/ZipFile", gMethods, NELEM(gMethods)); 417 ZipFile_initIDs(env); 418 419 jniRegisterNativeMethods(env, "java/util/jar/JarFile", gJarFileMethods, NELEM(gJarFileMethods)); 420} 421