java_util_zip_ZipFile.c revision a128ef7c6913e8b5f2ef4678ade5f97d7f7f452f
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 jlong JNICALL
160ZipFile_getEntry(JNIEnv *env, jclass cls, jlong zfile,
161                 jbyteArray name, jboolean addSlash)
162{
163#define MAXNAME 1024
164    jzfile *zip = jlong_to_ptr(zfile);
165    jsize ulen = (*env)->GetArrayLength(env, name);
166    char buf[MAXNAME+2], *path;
167    jzentry *ze;
168
169    if (ulen > MAXNAME) {
170        path = malloc(ulen + 2);
171        if (path == 0) {
172            JNU_ThrowOutOfMemoryError(env, 0);
173            return 0;
174        }
175    } else {
176        path = buf;
177    }
178    (*env)->GetByteArrayRegion(env, name, 0, ulen, (jbyte *)path);
179    path[ulen] = '\0';
180    if (addSlash == JNI_FALSE) {
181        ze = ZIP_GetEntry(zip, path, 0);
182    } else {
183        ze = ZIP_GetEntry(zip, path, (jint)ulen);
184    }
185    if (path != buf) {
186        free(path);
187    }
188    return ptr_to_jlong(ze);
189}
190
191JNIEXPORT void JNICALL
192ZipFile_freeEntry(JNIEnv *env, jclass cls, jlong zfile,
193                                    jlong zentry)
194{
195    jzfile *zip = jlong_to_ptr(zfile);
196    jzentry *ze = jlong_to_ptr(zentry);
197    ZIP_FreeEntry(zip, ze);
198}
199
200JNIEXPORT jlong JNICALL
201ZipFile_getNextEntry(JNIEnv *env, jclass cls, jlong zfile,
202                                        jint n)
203{
204    jzentry *ze = ZIP_GetNextEntry(jlong_to_ptr(zfile), n);
205    return ptr_to_jlong(ze);
206}
207
208JNIEXPORT jint JNICALL
209ZipFile_getEntryMethod(JNIEnv *env, jclass cls, jlong zentry)
210{
211    jzentry *ze = jlong_to_ptr(zentry);
212    return ze->csize != 0 ? DEFLATED : STORED;
213}
214
215JNIEXPORT jint JNICALL
216ZipFile_getEntryFlag(JNIEnv *env, jclass cls, jlong zentry)
217{
218    jzentry *ze = jlong_to_ptr(zentry);
219    return ze->flag;
220}
221
222JNIEXPORT jlong JNICALL
223ZipFile_getEntryCSize(JNIEnv *env, jclass cls, jlong zentry)
224{
225    jzentry *ze = jlong_to_ptr(zentry);
226    return ze->csize != 0 ? ze->csize : ze->size;
227}
228
229JNIEXPORT jlong JNICALL
230ZipFile_getEntrySize(JNIEnv *env, jclass cls, jlong zentry)
231{
232    jzentry *ze = jlong_to_ptr(zentry);
233    return ze->size;
234}
235
236JNIEXPORT jlong JNICALL
237ZipFile_getEntryTime(JNIEnv *env, jclass cls, jlong zentry)
238{
239    jzentry *ze = jlong_to_ptr(zentry);
240    return (jlong)ze->time & 0xffffffffUL;
241}
242
243JNIEXPORT jlong JNICALL
244ZipFile_getEntryCrc(JNIEnv *env, jclass cls, jlong zentry)
245{
246    jzentry *ze = jlong_to_ptr(zentry);
247    return (jlong)ze->crc & 0xffffffffUL;
248}
249
250JNIEXPORT jbyteArray JNICALL
251ZipFile_getCommentBytes(JNIEnv *env, jclass cls, jlong zfile)
252{
253    jzfile *zip = jlong_to_ptr(zfile);
254    jbyteArray jba = NULL;
255
256    if (zip->comment != NULL) {
257        if ((jba = (*env)->NewByteArray(env, zip->clen)) == NULL)
258            return NULL;
259        (*env)->SetByteArrayRegion(env, jba, 0, zip->clen, (jbyte*)zip->comment);
260    }
261    return jba;
262}
263
264JNIEXPORT jbyteArray JNICALL
265ZipFile_getEntryBytes(JNIEnv *env, jclass cls, jlong zentry, jint type)
266{
267    jzentry *ze = jlong_to_ptr(zentry);
268    int len = 0;
269    jbyteArray jba = NULL;
270    switch (type) {
271    case java_util_zip_ZipFile_JZENTRY_NAME:
272        if (ze->name != 0) {
273            len = (int)strlen(ze->name);
274            if (len == 0 || (jba = (*env)->NewByteArray(env, len)) == NULL)
275                break;
276            (*env)->SetByteArrayRegion(env, jba, 0, len, (jbyte *)ze->name);
277        }
278        break;
279    case java_util_zip_ZipFile_JZENTRY_EXTRA:
280        if (ze->extra != 0) {
281            unsigned char *bp = (unsigned char *)&ze->extra[0];
282            len = (bp[0] | (bp[1] << 8));
283            if (len <= 0 || (jba = (*env)->NewByteArray(env, len)) == NULL)
284                break;
285            (*env)->SetByteArrayRegion(env, jba, 0, len, &ze->extra[2]);
286        }
287        break;
288    case java_util_zip_ZipFile_JZENTRY_COMMENT:
289        if (ze->comment != 0) {
290            len = (int)strlen(ze->comment);
291            if (len == 0 || (jba = (*env)->NewByteArray(env, len)) == NULL)
292                break;
293            (*env)->SetByteArrayRegion(env, jba, 0, len, (jbyte*)ze->comment);
294        }
295        break;
296    }
297    return jba;
298}
299
300JNIEXPORT jint JNICALL
301ZipFile_read(JNIEnv *env, jclass cls, jlong zfile,
302             jlong zentry, jlong pos, jbyteArray bytes,
303             jint off, jint len)
304{
305    jzfile *zip = jlong_to_ptr(zfile);
306    char *msg;
307
308#define BUFSIZE 8192
309    /* copy via tmp stack buffer: */
310    jbyte buf[BUFSIZE];
311
312    if (len > BUFSIZE) {
313        len = BUFSIZE;
314    }
315
316    ZIP_Lock(zip);
317    len = ZIP_Read(zip, jlong_to_ptr(zentry), pos, buf, len);
318    msg = zip->msg;
319    ZIP_Unlock(zip);
320    if (len != -1) {
321        (*env)->SetByteArrayRegion(env, bytes, off, len, buf);
322    }
323
324    if (len == -1) {
325        if (msg != 0) {
326            ThrowZipException(env, msg);
327        } else {
328            char errmsg[128];
329            snprintf(errmsg, sizeof(errmsg), "errno: %d, error: %s\n", errno,
330                     "Error reading ZIP file");
331            JNU_ThrowIOExceptionWithLastError(env, errmsg);
332        }
333    }
334
335    return len;
336}
337
338JNIEXPORT jstring JNICALL
339ZipFile_getZipMessage(JNIEnv *env, jclass cls, jlong zfile)
340{
341    jzfile *zip = jlong_to_ptr(zfile);
342    char *msg = zip->msg;
343    if (msg == NULL) {
344        return NULL;
345    }
346    return JNU_NewStringPlatform(env, msg);
347}
348
349JNIEXPORT jobjectArray JNICALL
350JarFile_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 = (*env)->FindClass(env, "java/lang/String");
377        result = (*env)->NewObjectArray(env, count, cls, 0);
378        if (result != 0) {
379            for (i = 0; i < count; i++) {
380                jstring str = (*env)->NewStringUTF(env, zip->metanames[i]);
381                if (str == 0) {
382                    break;
383                }
384                (*env)->SetObjectArrayElement(env, result, i, str);
385                (*env)->DeleteLocalRef(env, str);
386            }
387        }
388    }
389    return result;
390}
391
392static JNINativeMethod gMethods[] = {
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
412static JNINativeMethod gJarFileMethods[] = {
413  NATIVE_METHOD(JarFile, getMetaInfEntryNames, "()[Ljava/lang/String;"),
414};
415
416void register_java_util_zip_ZipFile(JNIEnv* env) {
417  jniRegisterNativeMethods(env, "java/util/zip/ZipFile", gMethods, NELEM(gMethods));
418  ZipFile_initIDs(env);
419
420  jniRegisterNativeMethods(env, "java/util/jar/JarFile", gJarFileMethods, NELEM(gJarFileMethods));
421}
422