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