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 
62 static void
ThrowZipException(JNIEnv * env,const char * msg)63 ThrowZipException(JNIEnv *env, const char *msg)
64 {
65     jstring s = NULL;
66     jobject x;
67 
68     if (msg != NULL) {
69         s = JNU_NewStringPlatform(env, msg);
70     }
71     x = JNU_NewObjectByName(env,
72                             "java/util/zip/ZipException",
73                             "(Ljava/lang/String;)V", s);
74     if (x != NULL) {
75         (*env)->Throw(env, x);
76     }
77 }
78 
79 JNIEXPORT jlong JNICALL
80 // Android changed: Changed to non-static java method.
ZipFile_open(JNIEnv * env,jobject thiz,jstring name,jint mode,jlong lastModified,jboolean usemmap)81 ZipFile_open(JNIEnv *env, jobject thiz, jstring name,
82                                         jint mode, jlong lastModified,
83                                         jboolean usemmap)
84 {
85     const char *path = JNU_GetStringPlatformChars(env, name, 0);
86     char *msg = 0;
87     jlong result = 0;
88     int flag = 0;
89     jzfile *zip = 0;
90 
91     if (mode & OPEN_READ) flag |= O_RDONLY;
92     // Android changed, JVM_O_DELETE/unlink is problematic, see b/28901232.
93     //if (mode & OPEN_DELETE) flag |= JVM_O_DELETE;
94 
95     if (path != 0) {
96         zip = ZIP_Get_From_Cache(path, &msg, lastModified);
97         if (zip == 0 && msg == 0) {
98             ZFILE zfd = 0;
99 #ifdef WIN32
100             zfd = winFileHandleOpen(env, name, flag);
101             if (zfd == -1) {
102                 /* Exception already pending. */
103                 goto finally;
104             }
105 #else
106             zfd = JVM_Open(path, flag, 0);
107             if (zfd < 0) {
108                 throwFileNotFoundException(env, name);
109                 goto finally;
110             }
111 #endif
112             // Android changed: Pass jni env and thiz object into the method.
113             zip = ZIP_Put_In_Cache0(env, thiz, 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             free(msg);
121         } else if (errno == ENOMEM) {
122             JNU_ThrowOutOfMemoryError(env, 0);
123         } else {
124             ThrowZipException(env, "error in opening zip file");
125         }
126 finally:
127         JNU_ReleaseStringPlatformChars(env, name, path);
128     }
129     return result;
130 }
131 
132 JNIEXPORT jint JNICALL
ZipFile_getTotal(JNIEnv * env,jclass cls,jlong zfile)133 ZipFile_getTotal(JNIEnv *env, jclass cls, jlong zfile)
134 {
135     jzfile *zip = jlong_to_ptr(zfile);
136 
137     return zip->total;
138 }
139 
140 JNIEXPORT jboolean JNICALL
ZipFile_startsWithLOC(JNIEnv * env,jclass cls,jlong zfile)141 ZipFile_startsWithLOC(JNIEnv *env, jclass cls, jlong zfile)
142 {
143     jzfile *zip = jlong_to_ptr(zfile);
144 
145     return zip->locsig;
146 }
147 
148 JNIEXPORT void JNICALL
ZipFile_close(JNIEnv * env,jclass cls,jlong zfile)149 ZipFile_close(JNIEnv *env, jclass cls, jlong zfile)
150 {
151     ZIP_Close(jlong_to_ptr(zfile));
152 }
153 
154 JNIEXPORT jint JNICALL
ZipFile_getFileDescriptor(JNIEnv * env,jclass cls,jlong zfile)155 ZipFile_getFileDescriptor(JNIEnv *env, jclass cls, jlong zfile) {
156     jzfile *zip = jlong_to_ptr(zfile);
157     return zip->zfd;
158 }
159 
160 JNIEXPORT jlong JNICALL
ZipFile_getEntry(JNIEnv * env,jclass cls,jlong zfile,jbyteArray name,jboolean addSlash)161 ZipFile_getEntry(JNIEnv *env, jclass cls, jlong zfile,
162                  jbyteArray name, jboolean addSlash)
163 {
164 #define MAXNAME 1024
165     jzfile *zip = jlong_to_ptr(zfile);
166     jsize ulen = (*env)->GetArrayLength(env, name);
167     char buf[MAXNAME+2], *path;
168     jzentry *ze;
169 
170     if (ulen > MAXNAME) {
171         path = malloc(ulen + 2);
172         if (path == 0) {
173             JNU_ThrowOutOfMemoryError(env, 0);
174             return 0;
175         }
176     } else {
177         path = buf;
178     }
179     (*env)->GetByteArrayRegion(env, name, 0, ulen, (jbyte *)path);
180     path[ulen] = '\0';
181     ze = ZIP_GetEntry2(zip, path, (jint)ulen, addSlash);
182     if (path != buf) {
183         free(path);
184     }
185     return ptr_to_jlong(ze);
186 }
187 
188 JNIEXPORT void JNICALL
ZipFile_freeEntry(JNIEnv * env,jclass cls,jlong zfile,jlong zentry)189 ZipFile_freeEntry(JNIEnv *env, jclass cls, jlong zfile,
190                                     jlong zentry)
191 {
192     jzfile *zip = jlong_to_ptr(zfile);
193     jzentry *ze = jlong_to_ptr(zentry);
194     ZIP_FreeEntry(zip, ze);
195 }
196 
197 JNIEXPORT jlong JNICALL
ZipFile_getNextEntry(JNIEnv * env,jclass cls,jlong zfile,jint n)198 ZipFile_getNextEntry(JNIEnv *env, jclass cls, jlong zfile,
199                                         jint n)
200 {
201     jzentry *ze = ZIP_GetNextEntry(jlong_to_ptr(zfile), n);
202     return ptr_to_jlong(ze);
203 }
204 
205 JNIEXPORT jint JNICALL
ZipFile_getEntryMethod(JNIEnv * env,jclass cls,jlong zentry)206 ZipFile_getEntryMethod(JNIEnv *env, jclass cls, jlong zentry)
207 {
208     jzentry *ze = jlong_to_ptr(zentry);
209     return ze->csize != 0 ? DEFLATED : STORED;
210 }
211 
212 JNIEXPORT jint JNICALL
ZipFile_getEntryFlag(JNIEnv * env,jclass cls,jlong zentry)213 ZipFile_getEntryFlag(JNIEnv *env, jclass cls, jlong zentry)
214 {
215     jzentry *ze = jlong_to_ptr(zentry);
216     return ze->flag;
217 }
218 
219 JNIEXPORT jlong JNICALL
ZipFile_getEntryCSize(JNIEnv * env,jclass cls,jlong zentry)220 ZipFile_getEntryCSize(JNIEnv *env, jclass cls, jlong zentry)
221 {
222     jzentry *ze = jlong_to_ptr(zentry);
223     return ze->csize != 0 ? ze->csize : ze->size;
224 }
225 
226 JNIEXPORT jlong JNICALL
ZipFile_getEntrySize(JNIEnv * env,jclass cls,jlong zentry)227 ZipFile_getEntrySize(JNIEnv *env, jclass cls, jlong zentry)
228 {
229     jzentry *ze = jlong_to_ptr(zentry);
230     return ze->size;
231 }
232 
233 JNIEXPORT jlong JNICALL
ZipFile_getEntryTime(JNIEnv * env,jclass cls,jlong zentry)234 ZipFile_getEntryTime(JNIEnv *env, jclass cls, jlong zentry)
235 {
236     jzentry *ze = jlong_to_ptr(zentry);
237     return (jlong)ze->time & 0xffffffffUL;
238 }
239 
240 JNIEXPORT jlong JNICALL
ZipFile_getEntryCrc(JNIEnv * env,jclass cls,jlong zentry)241 ZipFile_getEntryCrc(JNIEnv *env, jclass cls, jlong zentry)
242 {
243     jzentry *ze = jlong_to_ptr(zentry);
244     return (jlong)ze->crc & 0xffffffffUL;
245 }
246 
247 JNIEXPORT jbyteArray JNICALL
ZipFile_getCommentBytes(JNIEnv * env,jclass cls,jlong zfile)248 ZipFile_getCommentBytes(JNIEnv *env, jclass cls, jlong zfile)
249 {
250     jzfile *zip = jlong_to_ptr(zfile);
251     jbyteArray jba = NULL;
252 
253     if (zip->comment != NULL) {
254         if ((jba = (*env)->NewByteArray(env, zip->clen)) == NULL)
255             return NULL;
256         (*env)->SetByteArrayRegion(env, jba, 0, zip->clen, (jbyte*)zip->comment);
257     }
258     return jba;
259 }
260 
261 JNIEXPORT jbyteArray JNICALL
ZipFile_getEntryBytes(JNIEnv * env,jclass cls,jlong zentry,jint type)262 ZipFile_getEntryBytes(JNIEnv *env, jclass cls, jlong zentry, jint type)
263 {
264     jzentry *ze = jlong_to_ptr(zentry);
265     int len = 0;
266     jbyteArray jba = NULL;
267     switch (type) {
268     case java_util_zip_ZipFile_JZENTRY_NAME:
269         if (ze->name != 0) {
270             len = (int)ze->nlen;
271             if (len == 0 || (jba = (*env)->NewByteArray(env, len)) == NULL)
272                 break;
273             (*env)->SetByteArrayRegion(env, jba, 0, len, (jbyte *)ze->name);
274         }
275         break;
276     case java_util_zip_ZipFile_JZENTRY_EXTRA:
277         if (ze->extra != 0) {
278             unsigned char *bp = (unsigned char *)&ze->extra[0];
279             len = (bp[0] | (bp[1] << 8));
280             if (len <= 0 || (jba = (*env)->NewByteArray(env, len)) == NULL)
281                 break;
282             (*env)->SetByteArrayRegion(env, jba, 0, len, &ze->extra[2]);
283         }
284         break;
285     case java_util_zip_ZipFile_JZENTRY_COMMENT:
286         if (ze->comment != 0) {
287             len = (int)strlen(ze->comment);
288             if (len == 0 || (jba = (*env)->NewByteArray(env, len)) == NULL)
289                 break;
290             (*env)->SetByteArrayRegion(env, jba, 0, len, (jbyte*)ze->comment);
291         }
292         break;
293     }
294     return jba;
295 }
296 
297 JNIEXPORT jint JNICALL
ZipFile_read(JNIEnv * env,jclass cls,jlong zfile,jlong zentry,jlong pos,jbyteArray bytes,jint off,jint len)298 ZipFile_read(JNIEnv *env, jclass cls, jlong zfile,
299              jlong zentry, jlong pos, jbyteArray bytes,
300              jint off, jint len)
301 {
302     jzfile *zip = jlong_to_ptr(zfile);
303     char *msg;
304 
305     // BEGIN Android-changed: Removed tmp stack buffer.
306     long long length = (long long)(*env)->GetArrayLength(env, bytes);
307     if (off < 0 || len < 0 || off + len > length) {
308         char errmsg[128];
309         snprintf(errmsg, sizeof(errmsg), "len: %d, off: %d are not valid for array sized %lld\n",
310                  len, off, length);
311         JNU_ThrowArrayIndexOutOfBoundsException(env, errmsg);
312         return -1;
313     }
314 
315     jbyte *buf = (*env)->GetByteArrayElements(env, bytes, NULL);
316     ZIP_Lock(zip);
317     len = ZIP_Read(zip, jlong_to_ptr(zentry), pos, buf + off, len);
318     msg = zip->msg;
319     ZIP_Unlock(zip);
320     (*env)->ReleaseByteArrayElements(env, bytes, buf, 0);
321 
322     if (len == -1) {
323         if (msg != 0) {
324             ThrowZipException(env, msg);
325         } else {
326             char errmsg[128];
327             snprintf(errmsg, sizeof(errmsg), "errno: %d, error: %s\n", errno,
328                      "Error reading ZIP file");
329             JNU_ThrowIOExceptionWithLastError(env, errmsg);
330         }
331     }
332     // END Android-changed: Removed tmp stack buffer.
333 
334     return len;
335 }
336 
337 JNIEXPORT jstring JNICALL
ZipFile_getZipMessage(JNIEnv * env,jclass cls,jlong zfile)338 ZipFile_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 
348 JNIEXPORT jobjectArray JNICALL
JarFile_getMetaInfEntryNames(JNIEnv * env,jobject obj)349 JarFile_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 
391 static JNINativeMethod gMethods[] = {
392   NATIVE_METHOD(ZipFile, getFileDescriptor, "(J)I"),
393   NATIVE_METHOD(ZipFile, getEntry, "(J[BZ)J"),
394   NATIVE_METHOD(ZipFile, freeEntry, "(JJ)V"),
395   NATIVE_METHOD(ZipFile, getNextEntry, "(JI)J"),
396   NATIVE_METHOD(ZipFile, close, "(J)V"),
397   NATIVE_METHOD(ZipFile, open, "(Ljava/lang/String;IJZ)J"),
398   NATIVE_METHOD(ZipFile, getTotal, "(J)I"),
399   NATIVE_METHOD(ZipFile, startsWithLOC, "(J)Z"),
400   NATIVE_METHOD(ZipFile, read, "(JJJ[BII)I"),
401   NATIVE_METHOD(ZipFile, getEntryTime, "(J)J"),
402   NATIVE_METHOD(ZipFile, getEntryCrc, "(J)J"),
403   NATIVE_METHOD(ZipFile, getEntryCSize, "(J)J"),
404   NATIVE_METHOD(ZipFile, getEntrySize, "(J)J"),
405   NATIVE_METHOD(ZipFile, getEntryMethod, "(J)I"),
406   NATIVE_METHOD(ZipFile, getEntryFlag, "(J)I"),
407   NATIVE_METHOD(ZipFile, getCommentBytes, "(J)[B"),
408   NATIVE_METHOD(ZipFile, getEntryBytes, "(JI)[B"),
409   NATIVE_METHOD(ZipFile, getZipMessage, "(J)Ljava/lang/String;"),
410 };
411 
412 static JNINativeMethod gJarFileMethods[] = {
413   NATIVE_METHOD(JarFile, getMetaInfEntryNames, "()[Ljava/lang/String;"),
414 };
415 
register_java_util_zip_ZipFile(JNIEnv * env)416 void register_java_util_zip_ZipFile(JNIEnv* env) {}
417