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