jdk/src/share/native/java/util/zip/ZipFile.c
author ohair
Tue, 28 Dec 2010 15:53:50 -0800
changeset 7668 d4a77089c587
parent 5506 202f599c92aa
child 12543 b77bc2f404ba
permissions -rw-r--r--
6962318: Update copyright year Reviewed-by: xdono
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
7668
d4a77089c587 6962318: Update copyright year
ohair
parents: 5506
diff changeset
     2
 * Copyright (c) 1998, 2010, 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);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        } else if (errno == ENOMEM) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            JNU_ThrowOutOfMemoryError(env, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            ThrowZipException(env, "error in opening zip file");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
finally:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        JNU_ReleaseStringPlatformChars(env, name, path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
JNIEXPORT jint JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
Java_java_util_zip_ZipFile_getTotal(JNIEnv *env, jclass cls, jlong zfile)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    jzfile *zip = jlong_to_ptr(zfile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    return zip->total;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
Java_java_util_zip_ZipFile_close(JNIEnv *env, jclass cls, jlong zfile)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    ZIP_Close(jlong_to_ptr(zfile));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
JNIEXPORT jlong JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
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
   147
                                    jbyteArray name, jboolean addSlash)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
#define MAXNAME 1024
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    jzfile *zip = jlong_to_ptr(zfile);
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2
diff changeset
   151
    jsize ulen = (*env)->GetArrayLength(env, name);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    char buf[MAXNAME+2], *path;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    jzentry *ze;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    if (ulen > MAXNAME) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        path = malloc(ulen + 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        if (path == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            JNU_ThrowOutOfMemoryError(env, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        path = buf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    }
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2
diff changeset
   164
    (*env)->GetByteArrayRegion(env, name, 0, ulen, (jbyte *)path);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    path[ulen] = '\0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    if (addSlash == JNI_FALSE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        ze = ZIP_GetEntry(zip, path, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        ze = ZIP_GetEntry(zip, path, (jint)ulen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    if (path != buf) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        free(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    return ptr_to_jlong(ze);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
Java_java_util_zip_ZipFile_freeEntry(JNIEnv *env, jclass cls, jlong zfile,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                                    jlong zentry)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    jzfile *zip = jlong_to_ptr(zfile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    jzentry *ze = jlong_to_ptr(zentry);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    ZIP_FreeEntry(zip, 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 jlong JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
Java_java_util_zip_ZipFile_getNextEntry(JNIEnv *env, jclass cls, jlong zfile,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                                        jint n)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    jzentry *ze = ZIP_GetNextEntry(jlong_to_ptr(zfile), n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    return ptr_to_jlong(ze);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
JNIEXPORT jint JNICALL
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2
diff changeset
   195
Java_java_util_zip_ZipFile_getEntryMethod(JNIEnv *env, jclass cls, jlong zentry)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    jzentry *ze = jlong_to_ptr(zentry);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    return ze->csize != 0 ? DEFLATED : STORED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2
diff changeset
   201
JNIEXPORT jint JNICALL
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2
diff changeset
   202
Java_java_util_zip_ZipFile_getEntryFlag(JNIEnv *env, jclass cls, jlong zentry)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    jzentry *ze = jlong_to_ptr(zentry);
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2
diff changeset
   205
    return ze->flag;
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2
diff changeset
   206
}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2
diff changeset
   208
JNIEXPORT jlong JNICALL
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2
diff changeset
   209
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
   210
{
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2
diff changeset
   211
    jzentry *ze = jlong_to_ptr(zentry);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    return ze->csize != 0 ? ze->csize : ze->size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
JNIEXPORT jlong JNICALL
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2
diff changeset
   216
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
   217
{
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2
diff changeset
   218
    jzentry *ze = jlong_to_ptr(zentry);
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2
diff changeset
   219
    return ze->size;
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2
diff changeset
   220
}
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2
diff changeset
   221
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2
diff changeset
   222
JNIEXPORT jlong JNICALL
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2
diff changeset
   223
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
   224
{
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2
diff changeset
   225
    jzentry *ze = jlong_to_ptr(zentry);
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2
diff changeset
   226
    return (jlong)ze->time & 0xffffffffUL;
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2
diff changeset
   227
}
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2
diff changeset
   228
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2
diff changeset
   229
JNIEXPORT jlong JNICALL
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2
diff changeset
   230
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
   231
{
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2
diff changeset
   232
    jzentry *ze = jlong_to_ptr(zentry);
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2
diff changeset
   233
    return (jlong)ze->crc & 0xffffffffUL;
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2
diff changeset
   234
}
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2
diff changeset
   235
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2
diff changeset
   236
JNIEXPORT jbyteArray JNICALL
3078
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2592
diff changeset
   237
Java_java_util_zip_ZipFile_getCommentBytes(JNIEnv *env,
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2592
diff changeset
   238
                                           jclass cls,
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2592
diff changeset
   239
                                           jlong zfile)
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2592
diff changeset
   240
{
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2592
diff changeset
   241
    jzfile *zip = jlong_to_ptr(zfile);
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2592
diff changeset
   242
    jbyteArray jba = NULL;
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2592
diff changeset
   243
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2592
diff changeset
   244
    if (zip->comment != NULL) {
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2592
diff changeset
   245
        if ((jba = (*env)->NewByteArray(env, zip->clen)) == NULL)
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2592
diff changeset
   246
            return NULL;
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2592
diff changeset
   247
        (*env)->SetByteArrayRegion(env, jba, 0, zip->clen, (jbyte*)zip->comment);
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2592
diff changeset
   248
    }
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2592
diff changeset
   249
    return jba;
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2592
diff changeset
   250
}
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2592
diff changeset
   251
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2592
diff changeset
   252
JNIEXPORT jbyteArray JNICALL
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2592
diff changeset
   253
Java_java_util_zip_ZipFile_getEntryBytes(JNIEnv *env,
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2592
diff changeset
   254
                                         jclass cls,
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2592
diff changeset
   255
                                         jlong zentry, jint type)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    jzentry *ze = jlong_to_ptr(zentry);
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2
diff changeset
   258
    int len = 0;
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2
diff changeset
   259
    jbyteArray jba = NULL;
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2
diff changeset
   260
    switch (type) {
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2
diff changeset
   261
    case java_util_zip_ZipFile_JZENTRY_NAME:
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2
diff changeset
   262
        if (ze->name != 0) {
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2
diff changeset
   263
            len = (int)strlen(ze->name);
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2
diff changeset
   264
            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
   265
                break;
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2
diff changeset
   266
            (*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
   267
        }
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2
diff changeset
   268
        break;
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2
diff changeset
   269
    case java_util_zip_ZipFile_JZENTRY_EXTRA:
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2
diff changeset
   270
        if (ze->extra != 0) {
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2
diff changeset
   271
            unsigned char *bp = (unsigned char *)&ze->extra[0];
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2
diff changeset
   272
            len = (bp[0] | (bp[1] << 8));
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, &ze->extra[2]);
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_COMMENT:
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2
diff changeset
   279
        if (ze->comment != 0) {
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2
diff changeset
   280
            len = (int)strlen(ze->comment);
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2
diff changeset
   281
            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
   282
                break;
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2
diff changeset
   283
            (*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
   284
        }
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2
diff changeset
   285
        break;
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2
diff changeset
   286
    }
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2
diff changeset
   287
    return jba;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
JNIEXPORT jint JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
Java_java_util_zip_ZipFile_read(JNIEnv *env, jclass cls, jlong zfile,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                                jlong zentry, jlong pos, jbyteArray bytes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                                jint off, jint len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    jzfile *zip = jlong_to_ptr(zfile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    char *msg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
#define BUFSIZE 8192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    /* copy via tmp stack buffer: */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
    jbyte buf[BUFSIZE];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    if (len > BUFSIZE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        len = BUFSIZE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    ZIP_Lock(zip);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    len = ZIP_Read(zip, jlong_to_ptr(zentry), pos, buf, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    msg = zip->msg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
    ZIP_Unlock(zip);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    if (len != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        (*env)->SetByteArrayRegion(env, bytes, off, len, buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
    if (len == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        if (msg != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            ThrowZipException(env, msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            char errmsg[128];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            sprintf(errmsg, "errno: %d, error: %s\n",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                    errno, "Error reading ZIP file");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
            JNU_ThrowIOExceptionWithLastError(env, errmsg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    return len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
 * Returns an array of strings representing the names of all entries
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
 * that begin with "META-INF/" (case ignored). This native method is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
 * used in JarFile as an optimization when looking up manifest and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
 * signature file entries. Returns null if no entries were found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
JNIEXPORT jobjectArray JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
Java_java_util_jar_JarFile_getMetaInfEntryNames(JNIEnv *env, jobject obj)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    jlong zfile = (*env)->GetLongField(env, obj, jzfileID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    jzfile *zip;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    int i, count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    jobjectArray result = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
    if (zfile == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        JNU_ThrowByName(env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                        "java/lang/IllegalStateException", "zip file closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    zip = jlong_to_ptr(zfile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    /* count the number of valid ZIP metanames */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
    count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
    if (zip->metanames != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        for (i = 0; i < zip->metacount; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
            if (zip->metanames[i] != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                count++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
    /* If some names were found then build array of java strings */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
    if (count > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        jclass cls = (*env)->FindClass(env, "java/lang/String");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        result = (*env)->NewObjectArray(env, count, cls, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        if (result != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
            for (i = 0; i < count; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                jstring str = (*env)->NewStringUTF(env, zip->metanames[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                if (str == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
                (*env)->SetObjectArrayElement(env, result, i, str);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                (*env)->DeleteLocalRef(env, str);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
    return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
JNIEXPORT jstring JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
Java_java_util_zip_ZipFile_getZipMessage(JNIEnv *env, jclass cls, jlong zfile)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
    jzfile *zip = jlong_to_ptr(zfile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
    char *msg = zip->msg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
    if (msg == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
    return JNU_NewStringPlatform(env, msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
}