jdk/src/share/native/java/util/zip/ZipFile.c
author chegar
Mon, 08 Apr 2013 06:15:18 +0100
changeset 18223 35a5c2462991
parent 14342 8435a30053c1
child 22643 ba2891b3f866
permissions -rw-r--r--
8008593: Better URLClassLoader resource management Reviewed-by: alanb, sherman, hawtin
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
14342
8435a30053c1 7197491: update copyright year to match last edit in jdk8 jdk repository
alanb
parents: 12543
diff changeset
     2
 * Copyright (c) 1998, 2012, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5148
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5148
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5148
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5148
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5148
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * Native method support for java.util.zip.ZipFile
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
#include <stdio.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
#include <stdlib.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
#include <string.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
#include <errno.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
#include <ctype.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
#include <assert.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
#include "jlong.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
#include "jvm.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
#include "jni.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
#include "jni_util.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
#include "zip_util.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
#ifdef WIN32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
#include "io_util_md.h"
3078
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2592
diff changeset
    43
#else
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2592
diff changeset
    44
#include "io_util.h"
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
#include "java_util_zip_ZipFile.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
#include "java_util_jar_JarFile.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
#define DEFLATED 8
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
#define STORED 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
static jfieldID jzfileID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
static int OPEN_READ = java_util_zip_ZipFile_OPEN_READ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
static int OPEN_DELETE = java_util_zip_ZipFile_OPEN_DELETE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
Java_java_util_zip_ZipFile_initIDs(JNIEnv *env, jclass cls)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    jzfileID = (*env)->GetFieldID(env, cls, "jzfile", "J");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    assert(jzfileID != 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
ThrowZipException(JNIEnv *env, const char *msg)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    jstring s = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    jobject x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    if (msg != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        s = JNU_NewStringPlatform(env, msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    x = JNU_NewObjectByName(env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                            "java/util/zip/ZipException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                            "(Ljava/lang/String;)V", s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    if (x != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        (*env)->Throw(env, x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
JNIEXPORT jlong JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
Java_java_util_zip_ZipFile_open(JNIEnv *env, jclass cls, jstring name,
5148
2bf5469864ea 6929479: Add a system property sun.zip.disableMemoryMapping to disable mmap use in ZipFile
sherman
parents: 3078
diff changeset
    84
                                        jint mode, jlong lastModified,
2bf5469864ea 6929479: Add a system property sun.zip.disableMemoryMapping to disable mmap use in ZipFile
sherman
parents: 3078
diff changeset
    85
                                        jboolean usemmap)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    const char *path = JNU_GetStringPlatformChars(env, name, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    char *msg = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    jlong result = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    int flag = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    jzfile *zip = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    if (mode & OPEN_READ) flag |= O_RDONLY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    if (mode & OPEN_DELETE) flag |= JVM_O_DELETE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    if (path != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        zip = ZIP_Get_From_Cache(path, &msg, lastModified);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        if (zip == 0 && msg == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            ZFILE zfd = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
#ifdef WIN32
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            zfd = winFileHandleOpen(env, name, flag);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            if (zfd == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                /* Exception already pending. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                goto finally;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
#else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            zfd = JVM_Open(path, flag, 0);
3078
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2592
diff changeset
   108
            if (zfd < 0) {
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2592
diff changeset
   109
                throwFileNotFoundException(env, name);
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2592
diff changeset
   110
                goto finally;
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2592
diff changeset
   111
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
#endif
5148
2bf5469864ea 6929479: Add a system property sun.zip.disableMemoryMapping to disable mmap use in ZipFile
sherman
parents: 3078
diff changeset
   113
            zip = ZIP_Put_In_Cache0(path, zfd, &msg, lastModified, usemmap);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        if (zip != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            result = ptr_to_jlong(zip);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        } else if (msg != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            ThrowZipException(env, msg);
12543
b77bc2f404ba 7159982: ZipFile uses static for error message when malformed zip file encountered
zhouyx
parents: 7668
diff changeset
   120
            free(msg);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        } else if (errno == ENOMEM) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            JNU_ThrowOutOfMemoryError(env, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            ThrowZipException(env, "error in opening zip file");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
finally:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        JNU_ReleaseStringPlatformChars(env, name, path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
JNIEXPORT jint JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
Java_java_util_zip_ZipFile_getTotal(JNIEnv *env, jclass cls, jlong zfile)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    jzfile *zip = jlong_to_ptr(zfile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    return zip->total;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
18223
35a5c2462991 8008593: Better URLClassLoader resource management
chegar
parents: 14342
diff changeset
   140
JNIEXPORT jboolean JNICALL
35a5c2462991 8008593: Better URLClassLoader resource management
chegar
parents: 14342
diff changeset
   141
Java_java_util_zip_ZipFile_startsWithLOC(JNIEnv *env, jclass cls, jlong zfile)
35a5c2462991 8008593: Better URLClassLoader resource management
chegar
parents: 14342
diff changeset
   142
{
35a5c2462991 8008593: Better URLClassLoader resource management
chegar
parents: 14342
diff changeset
   143
    jzfile *zip = jlong_to_ptr(zfile);
35a5c2462991 8008593: Better URLClassLoader resource management
chegar
parents: 14342
diff changeset
   144
35a5c2462991 8008593: Better URLClassLoader resource management
chegar
parents: 14342
diff changeset
   145
    return zip->locsig;
35a5c2462991 8008593: Better URLClassLoader resource management
chegar
parents: 14342
diff changeset
   146
}
35a5c2462991 8008593: Better URLClassLoader resource management
chegar
parents: 14342
diff changeset
   147
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
Java_java_util_zip_ZipFile_close(JNIEnv *env, jclass cls, jlong zfile)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    ZIP_Close(jlong_to_ptr(zfile));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
JNIEXPORT jlong JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
Java_java_util_zip_ZipFile_getEntry(JNIEnv *env, jclass cls, jlong zfile,
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2
diff changeset
   156
                                    jbyteArray name, jboolean addSlash)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
#define MAXNAME 1024
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    jzfile *zip = jlong_to_ptr(zfile);
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2
diff changeset
   160
    jsize ulen = (*env)->GetArrayLength(env, name);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    char buf[MAXNAME+2], *path;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    jzentry *ze;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    if (ulen > MAXNAME) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        path = malloc(ulen + 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        if (path == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            JNU_ThrowOutOfMemoryError(env, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        path = buf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    }
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2
diff changeset
   173
    (*env)->GetByteArrayRegion(env, name, 0, ulen, (jbyte *)path);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    path[ulen] = '\0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    if (addSlash == JNI_FALSE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        ze = ZIP_GetEntry(zip, path, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        ze = ZIP_GetEntry(zip, path, (jint)ulen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    if (path != buf) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        free(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    return ptr_to_jlong(ze);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
Java_java_util_zip_ZipFile_freeEntry(JNIEnv *env, jclass cls, jlong zfile,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                                    jlong zentry)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    jzfile *zip = jlong_to_ptr(zfile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    jzentry *ze = jlong_to_ptr(zentry);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    ZIP_FreeEntry(zip, ze);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
JNIEXPORT jlong JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
Java_java_util_zip_ZipFile_getNextEntry(JNIEnv *env, jclass cls, jlong zfile,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                                        jint n)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    jzentry *ze = ZIP_GetNextEntry(jlong_to_ptr(zfile), n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    return ptr_to_jlong(ze);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
JNIEXPORT jint JNICALL
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2
diff changeset
   204
Java_java_util_zip_ZipFile_getEntryMethod(JNIEnv *env, jclass cls, jlong zentry)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    jzentry *ze = jlong_to_ptr(zentry);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    return ze->csize != 0 ? DEFLATED : STORED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2
diff changeset
   210
JNIEXPORT jint JNICALL
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2
diff changeset
   211
Java_java_util_zip_ZipFile_getEntryFlag(JNIEnv *env, jclass cls, jlong zentry)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    jzentry *ze = jlong_to_ptr(zentry);
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2
diff changeset
   214
    return ze->flag;
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2
diff changeset
   215
}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2
diff changeset
   217
JNIEXPORT jlong JNICALL
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2
diff changeset
   218
Java_java_util_zip_ZipFile_getEntryCSize(JNIEnv *env, jclass cls, jlong zentry)
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2
diff changeset
   219
{
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2
diff changeset
   220
    jzentry *ze = jlong_to_ptr(zentry);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    return ze->csize != 0 ? ze->csize : ze->size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
JNIEXPORT jlong JNICALL
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2
diff changeset
   225
Java_java_util_zip_ZipFile_getEntrySize(JNIEnv *env, jclass cls, jlong zentry)
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2
diff changeset
   226
{
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2
diff changeset
   227
    jzentry *ze = jlong_to_ptr(zentry);
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2
diff changeset
   228
    return ze->size;
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2
diff changeset
   229
}
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2
diff changeset
   230
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2
diff changeset
   231
JNIEXPORT jlong JNICALL
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2
diff changeset
   232
Java_java_util_zip_ZipFile_getEntryTime(JNIEnv *env, jclass cls, jlong zentry)
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2
diff changeset
   233
{
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2
diff changeset
   234
    jzentry *ze = jlong_to_ptr(zentry);
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2
diff changeset
   235
    return (jlong)ze->time & 0xffffffffUL;
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2
diff changeset
   236
}
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2
diff changeset
   237
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2
diff changeset
   238
JNIEXPORT jlong JNICALL
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2
diff changeset
   239
Java_java_util_zip_ZipFile_getEntryCrc(JNIEnv *env, jclass cls, jlong zentry)
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2
diff changeset
   240
{
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2
diff changeset
   241
    jzentry *ze = jlong_to_ptr(zentry);
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2
diff changeset
   242
    return (jlong)ze->crc & 0xffffffffUL;
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2
diff changeset
   243
}
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2
diff changeset
   244
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2
diff changeset
   245
JNIEXPORT jbyteArray JNICALL
3078
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2592
diff changeset
   246
Java_java_util_zip_ZipFile_getCommentBytes(JNIEnv *env,
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2592
diff changeset
   247
                                           jclass cls,
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2592
diff changeset
   248
                                           jlong zfile)
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2592
diff changeset
   249
{
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2592
diff changeset
   250
    jzfile *zip = jlong_to_ptr(zfile);
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2592
diff changeset
   251
    jbyteArray jba = NULL;
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2592
diff changeset
   252
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2592
diff changeset
   253
    if (zip->comment != NULL) {
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2592
diff changeset
   254
        if ((jba = (*env)->NewByteArray(env, zip->clen)) == NULL)
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2592
diff changeset
   255
            return NULL;
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2592
diff changeset
   256
        (*env)->SetByteArrayRegion(env, jba, 0, zip->clen, (jbyte*)zip->comment);
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2592
diff changeset
   257
    }
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2592
diff changeset
   258
    return jba;
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2592
diff changeset
   259
}
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2592
diff changeset
   260
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2592
diff changeset
   261
JNIEXPORT jbyteArray JNICALL
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2592
diff changeset
   262
Java_java_util_zip_ZipFile_getEntryBytes(JNIEnv *env,
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2592
diff changeset
   263
                                         jclass cls,
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2592
diff changeset
   264
                                         jlong zentry, jint type)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    jzentry *ze = jlong_to_ptr(zentry);
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2
diff changeset
   267
    int len = 0;
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2
diff changeset
   268
    jbyteArray jba = NULL;
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2
diff changeset
   269
    switch (type) {
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2
diff changeset
   270
    case java_util_zip_ZipFile_JZENTRY_NAME:
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2
diff changeset
   271
        if (ze->name != 0) {
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2
diff changeset
   272
            len = (int)strlen(ze->name);
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2
diff changeset
   273
            if (len == 0 || (jba = (*env)->NewByteArray(env, len)) == NULL)
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2
diff changeset
   274
                break;
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2
diff changeset
   275
            (*env)->SetByteArrayRegion(env, jba, 0, len, (jbyte *)ze->name);
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2
diff changeset
   276
        }
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2
diff changeset
   277
        break;
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2
diff changeset
   278
    case java_util_zip_ZipFile_JZENTRY_EXTRA:
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2
diff changeset
   279
        if (ze->extra != 0) {
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2
diff changeset
   280
            unsigned char *bp = (unsigned char *)&ze->extra[0];
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2
diff changeset
   281
            len = (bp[0] | (bp[1] << 8));
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2
diff changeset
   282
            if (len <= 0 || (jba = (*env)->NewByteArray(env, len)) == NULL)
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2
diff changeset
   283
                break;
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2
diff changeset
   284
            (*env)->SetByteArrayRegion(env, jba, 0, len, &ze->extra[2]);
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2
diff changeset
   285
        }
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2
diff changeset
   286
        break;
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2
diff changeset
   287
    case java_util_zip_ZipFile_JZENTRY_COMMENT:
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2
diff changeset
   288
        if (ze->comment != 0) {
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2
diff changeset
   289
            len = (int)strlen(ze->comment);
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2
diff changeset
   290
            if (len == 0 || (jba = (*env)->NewByteArray(env, len)) == NULL)
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2
diff changeset
   291
                break;
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2
diff changeset
   292
            (*env)->SetByteArrayRegion(env, jba, 0, len, (jbyte*)ze->comment);
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2
diff changeset
   293
        }
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2
diff changeset
   294
        break;
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2
diff changeset
   295
    }
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2
diff changeset
   296
    return jba;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
JNIEXPORT jint JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
Java_java_util_zip_ZipFile_read(JNIEnv *env, jclass cls, jlong zfile,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                                jlong zentry, jlong pos, jbyteArray bytes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                                jint off, jint len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    jzfile *zip = jlong_to_ptr(zfile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    char *msg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
#define BUFSIZE 8192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    /* copy via tmp stack buffer: */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
    jbyte buf[BUFSIZE];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    if (len > BUFSIZE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        len = BUFSIZE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    ZIP_Lock(zip);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    len = ZIP_Read(zip, jlong_to_ptr(zentry), pos, buf, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
    msg = zip->msg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    ZIP_Unlock(zip);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    if (len != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        (*env)->SetByteArrayRegion(env, bytes, off, len, buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    if (len == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        if (msg != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            ThrowZipException(env, msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            char errmsg[128];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
            sprintf(errmsg, "errno: %d, error: %s\n",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                    errno, "Error reading ZIP file");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            JNU_ThrowIOExceptionWithLastError(env, errmsg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    return len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
 * Returns an array of strings representing the names of all entries
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
 * that begin with "META-INF/" (case ignored). This native method is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
 * used in JarFile as an optimization when looking up manifest and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
 * signature file entries. Returns null if no entries were found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
JNIEXPORT jobjectArray JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
Java_java_util_jar_JarFile_getMetaInfEntryNames(JNIEnv *env, jobject obj)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
    jlong zfile = (*env)->GetLongField(env, obj, jzfileID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    jzfile *zip;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
    int i, count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    jobjectArray result = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
    if (zfile == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        JNU_ThrowByName(env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                        "java/lang/IllegalStateException", "zip file closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
    zip = jlong_to_ptr(zfile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
    /* count the number of valid ZIP metanames */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
    count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
    if (zip->metanames != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        for (i = 0; i < zip->metacount; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
            if (zip->metanames[i] != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                count++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
    /* If some names were found then build array of java strings */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
    if (count > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        jclass cls = (*env)->FindClass(env, "java/lang/String");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        result = (*env)->NewObjectArray(env, count, cls, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        if (result != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
            for (i = 0; i < count; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                jstring str = (*env)->NewStringUTF(env, zip->metanames[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                if (str == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
                (*env)->SetObjectArrayElement(env, result, i, str);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
                (*env)->DeleteLocalRef(env, str);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
    return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
JNIEXPORT jstring JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
Java_java_util_zip_ZipFile_getZipMessage(JNIEnv *env, jclass cls, jlong zfile)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
    jzfile *zip = jlong_to_ptr(zfile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
    char *msg = zip->msg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
    if (msg == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
    return JNU_NewStringPlatform(env, msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
}