1 /*
2 * Copyright (c) 1998, 2012, 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
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;
350
351 if (zfile == 0) {
352 JNU_ThrowByName(env,
353 "java/lang/IllegalStateException", "zip file closed");
354 return NULL;
355 }
356 zip = jlong_to_ptr(zfile);
357
358 /* count the number of valid ZIP metanames */
359 count = 0;
360 if (zip->metanames != 0) {
361 for (i = 0; i < zip->metacount; i++) {
362 if (zip->metanames[i] != 0) {
363 count++;
364 }
365 }
366 }
367
368 /* If some names were found then build array of java strings */
369 if (count > 0) {
370 jclass cls = (*env)->FindClass(env, "java/lang/String");
371 result = (*env)->NewObjectArray(env, count, cls, 0);
372 if (result != 0) {
373 for (i = 0; i < count; i++) {
374 jstring str = (*env)->NewStringUTF(env, zip->metanames[i]);
375 if (str == 0) {
376 break;
377 }
378 (*env)->SetObjectArrayElement(env, result, i, str);
379 (*env)->DeleteLocalRef(env, str);
380 }
381 }
382 }
383 return result;
384 }
385
386 JNIEXPORT jstring JNICALL
387 Java_java_util_zip_ZipFile_getZipMessage(JNIEnv *env, jclass cls, jlong zfile)
388 {
389 jzfile *zip = jlong_to_ptr(zfile);
390 char *msg = zip->msg;
391 if (msg == NULL) {
|
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
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 if (mode & OPEN_DELETE) flag |= JVM_O_DELETE;
97
98 if (path != 0) {
99 zip = ZIP_Get_From_Cache(path, &msg, lastModified);
100 if (zip == 0 && msg == 0) {
101 ZFILE zfd = 0;
352
353 if (zfile == 0) {
354 JNU_ThrowByName(env,
355 "java/lang/IllegalStateException", "zip file closed");
356 return NULL;
357 }
358 zip = jlong_to_ptr(zfile);
359
360 /* count the number of valid ZIP metanames */
361 count = 0;
362 if (zip->metanames != 0) {
363 for (i = 0; i < zip->metacount; i++) {
364 if (zip->metanames[i] != 0) {
365 count++;
366 }
367 }
368 }
369
370 /* If some names were found then build array of java strings */
371 if (count > 0) {
372 jclass cls = JNU_ClassString(env);
373 CHECK_NULL_RETURN(cls, NULL);
374 result = (*env)->NewObjectArray(env, count, cls, 0);
375 CHECK_NULL_RETURN(result, NULL);
376 if (result != 0) {
377 for (i = 0; i < count; i++) {
378 jstring str = (*env)->NewStringUTF(env, zip->metanames[i]);
379 if (str == 0) {
380 break;
381 }
382 (*env)->SetObjectArrayElement(env, result, i, str);
383 (*env)->DeleteLocalRef(env, str);
384 }
385 }
386 }
387 return result;
388 }
389
390 JNIEXPORT jstring JNICALL
391 Java_java_util_zip_ZipFile_getZipMessage(JNIEnv *env, jclass cls, jlong zfile)
392 {
393 jzfile *zip = jlong_to_ptr(zfile);
394 char *msg = zip->msg;
395 if (msg == NULL) {
|