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