jdk/src/java.base/share/native/libzip/zip_util.c
author naoto
Thu, 26 May 2016 14:44:42 -0700
changeset 38578 1a325ada1672
parent 38577 003898efad7e
child 38783 0a5acb647700
permissions -rw-r--r--
8157964: Static build of libzip is missing JNI_OnLoad_zip entry point Reviewed-by: sherman
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
38578
1a325ada1672 8157964: Static build of libzip is missing JNI_OnLoad_zip entry point
naoto
parents: 38577
diff changeset
     2
 * Copyright (c) 1995, 2016, 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
 * Support for reading ZIP/JAR files.
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 <stddef.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
#include <string.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
#include <fcntl.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
#include <limits.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
#include <time.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
#include <ctype.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
#include <assert.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
#include "jni.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
#include "jni_util.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
#include "jlong.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
#include "jvm.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
#include "io_util.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
#include "io_util_md.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
#include "zip_util.h"
13567
b124faa29aef 7110151: Use underlying platform's zlib library for Java zlib support
andrew
parents: 12543
diff changeset
    47
#include <zlib.h>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11898
diff changeset
    49
#ifdef _ALLBSD_SOURCE
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11898
diff changeset
    50
#define off64_t off_t
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11898
diff changeset
    51
#define mmap64 mmap
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11898
diff changeset
    52
#endif
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 11898
diff changeset
    53
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
/* USE_MMAP means mmap the CEN & ENDHDR part of the zip file. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
#ifdef USE_MMAP
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
#include <sys/mman.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
#define MAXREFS 0xFFFF  /* max number of open zip file references */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
#define MCREATE()      JVM_RawMonitorCreate()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
#define MLOCK(lock)    JVM_RawMonitorEnter(lock)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
#define MUNLOCK(lock)  JVM_RawMonitorExit(lock)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
#define MDESTROY(lock) JVM_RawMonitorDestroy(lock)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
#define CENSIZE(cen) (CENHDR + CENNAM(cen) + CENEXT(cen) + CENCOM(cen))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
static jzfile *zfiles = 0;      /* currently open zip files */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
static void *zfiles_lock = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
static void freeCEN(jzfile *);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
#ifndef PATH_MAX
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
#define PATH_MAX 1024
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
static jint INITIAL_META_COUNT = 2;   /* initial number of entries in meta name array */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
/*
38578
1a325ada1672 8157964: Static build of libzip is missing JNI_OnLoad_zip entry point
naoto
parents: 38577
diff changeset
    80
 * Declare library specific JNI_Onload entry if static build
1a325ada1672 8157964: Static build of libzip is missing JNI_OnLoad_zip entry point
naoto
parents: 38577
diff changeset
    81
 */
1a325ada1672 8157964: Static build of libzip is missing JNI_OnLoad_zip entry point
naoto
parents: 38577
diff changeset
    82
#ifdef STATIC_BUILD
1a325ada1672 8157964: Static build of libzip is missing JNI_OnLoad_zip entry point
naoto
parents: 38577
diff changeset
    83
DEF_STATIC_JNI_OnLoad
1a325ada1672 8157964: Static build of libzip is missing JNI_OnLoad_zip entry point
naoto
parents: 38577
diff changeset
    84
#endif
1a325ada1672 8157964: Static build of libzip is missing JNI_OnLoad_zip entry point
naoto
parents: 38577
diff changeset
    85
1a325ada1672 8157964: Static build of libzip is missing JNI_OnLoad_zip entry point
naoto
parents: 38577
diff changeset
    86
/*
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * The ZFILE_* functions exist to provide some platform-independence with
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 * respect to file access needs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 * Opens the named file for reading, returning a ZFILE.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 * Compare this with winFileHandleOpen in windows/native/java/io/io_util_md.c.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 * This function does not take JNIEnv* and uses CreateFile (instead of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 * CreateFileW).  The expectation is that this function will be called only
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 * from ZIP_Open_Generic, which in turn is used by the JVM, where we do not
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 * need to concern ourselves with wide chars.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
static ZFILE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
ZFILE_Open(const char *fname, int flags) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
#ifdef WIN32
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    const DWORD access =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        (flags & O_RDWR)   ? (GENERIC_WRITE | GENERIC_READ) :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        (flags & O_WRONLY) ?  GENERIC_WRITE :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        GENERIC_READ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    const DWORD sharing =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        FILE_SHARE_READ | FILE_SHARE_WRITE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    const DWORD disposition =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        /* Note: O_TRUNC overrides O_CREAT */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        (flags & O_TRUNC) ? CREATE_ALWAYS :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        (flags & O_CREAT) ? OPEN_ALWAYS   :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        OPEN_EXISTING;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    const DWORD  maybeWriteThrough =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        (flags & (O_SYNC | O_DSYNC)) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        FILE_FLAG_WRITE_THROUGH :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        FILE_ATTRIBUTE_NORMAL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    const DWORD maybeDeleteOnClose =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        (flags & O_TEMPORARY) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        FILE_FLAG_DELETE_ON_CLOSE :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        FILE_ATTRIBUTE_NORMAL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    const DWORD flagsAndAttributes = maybeWriteThrough | maybeDeleteOnClose;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    return (jlong) CreateFile(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        fname,          /* Wide char path name */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        access,         /* Read and/or write permission */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        sharing,        /* File sharing flags */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        NULL,           /* Security attributes */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        disposition,        /* creation disposition */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        flagsAndAttributes, /* flags and attributes */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
#else
27184
2996674bd701 8057777: Cleanup of old and unused VM interfaces
fparain
parents: 25859
diff changeset
   133
    return open(fname, flags, 0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
 * The io_util_md.h files do not provide IO_CLOSE, hence we use platform
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
 * specifics.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
ZFILE_Close(ZFILE zfd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
#ifdef WIN32
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    CloseHandle((HANDLE) zfd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
#else
27184
2996674bd701 8057777: Cleanup of old and unused VM interfaces
fparain
parents: 25859
diff changeset
   146
    close(zfd);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
static int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
ZFILE_read(ZFILE zfd, char *buf, jint nbytes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
#ifdef WIN32
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    return (int) IO_Read(zfd, buf, nbytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
#else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    return read(zfd, buf, nbytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
 * Initialize zip file support. Return 0 if successful otherwise -1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
 * if could not be initialized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
static jint
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
InitializeZip()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    static jboolean inited = JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    // Initialize errno to 0.  It may be set later (e.g. during memory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    // allocation) but we can disregard previous values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    errno = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    if (inited)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    zfiles_lock = MCREATE();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    if (zfiles_lock == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    inited = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
 * Reads len bytes of data into buf.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
 * Returns 0 if all bytes could be read, otherwise returns -1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
static int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
readFully(ZFILE zfd, void *buf, jlong len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
  char *bp = (char *) buf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
  while (len > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        jlong limit = ((((jlong) 1) << 31) - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        jint count = (len < limit) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            (jint) len :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            (jint) limit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        jint n = ZFILE_read(zfd, bp, count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        if (n > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            bp += n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            len -= n;
27184
2996674bd701 8057777: Cleanup of old and unused VM interfaces
fparain
parents: 25859
diff changeset
   200
        } else if (n == -1 && errno == EINTR) {
2996674bd701 8057777: Cleanup of old and unused VM interfaces
fparain
parents: 25859
diff changeset
   201
          /* Retry after EINTR (interrupted by signal). */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        } else { /* EOF or IO error */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
 * Reads len bytes of data from the specified offset into buf.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
 * Returns 0 if all bytes could be read, otherwise returns -1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
static int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
readFullyAt(ZFILE zfd, void *buf, jlong len, jlong offset)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
{
2074
346a841b2dcc 6599383: Unable to open zip files more than 2GB in size
kevinw
parents: 1572
diff changeset
   217
    if (IO_Lseek(zfd, offset, SEEK_SET) == -1) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        return -1; /* lseek failure. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    return readFully(zfd, buf, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
 * Allocates a new zip file object for the specified file name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
 * Returns the zip file object or NULL if not enough memory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
static jzfile *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
allocZip(const char *name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    jzfile *zip;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    if (((zip = calloc(1, sizeof(jzfile))) != NULL) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        ((zip->name = strdup(name))        != NULL) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        ((zip->lock = MCREATE())           != NULL)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        zip->zfd = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        return zip;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    if (zip != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        free(zip->name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        free(zip);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
 * Frees all native resources owned by the specified zip file object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
freeZip(jzfile *zip)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    /* First free any cached jzentry */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    ZIP_FreeEntry(zip,0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    if (zip->lock != NULL) MDESTROY(zip->lock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    free(zip->name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    freeCEN(zip);
5148
2bf5469864ea 6929479: Add a system property sun.zip.disableMemoryMapping to disable mmap use in ZipFile
sherman
parents: 3078
diff changeset
   257
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
#ifdef USE_MMAP
5148
2bf5469864ea 6929479: Add a system property sun.zip.disableMemoryMapping to disable mmap use in ZipFile
sherman
parents: 3078
diff changeset
   259
    if (zip->usemmap) {
2bf5469864ea 6929479: Add a system property sun.zip.disableMemoryMapping to disable mmap use in ZipFile
sherman
parents: 3078
diff changeset
   260
        if (zip->maddr != NULL)
2bf5469864ea 6929479: Add a system property sun.zip.disableMemoryMapping to disable mmap use in ZipFile
sherman
parents: 3078
diff changeset
   261
            munmap((char *)zip->maddr, zip->mlen);
2bf5469864ea 6929479: Add a system property sun.zip.disableMemoryMapping to disable mmap use in ZipFile
sherman
parents: 3078
diff changeset
   262
    } else
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
#endif
5148
2bf5469864ea 6929479: Add a system property sun.zip.disableMemoryMapping to disable mmap use in ZipFile
sherman
parents: 3078
diff changeset
   264
    {
2bf5469864ea 6929479: Add a system property sun.zip.disableMemoryMapping to disable mmap use in ZipFile
sherman
parents: 3078
diff changeset
   265
        free(zip->cencache.data);
2bf5469864ea 6929479: Add a system property sun.zip.disableMemoryMapping to disable mmap use in ZipFile
sherman
parents: 3078
diff changeset
   266
    }
3078
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2592
diff changeset
   267
    if (zip->comment != NULL)
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2592
diff changeset
   268
        free(zip->comment);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    if (zip->zfd != -1) ZFILE_Close(zip->zfd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    free(zip);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
/* The END header is followed by a variable length comment of size < 64k. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
static const jlong END_MAXLEN = 0xFFFF + ENDHDR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
#define READBLOCKSZ 128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
3078
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2592
diff changeset
   278
static jboolean verifyEND(jzfile *zip, jlong endpos, char *endbuf) {
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2592
diff changeset
   279
    /* ENDSIG matched, however the size of file comment in it does not
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2592
diff changeset
   280
       match the real size. One "common" cause for this problem is some
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2592
diff changeset
   281
       "extra" bytes are padded at the end of the zipfile.
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2592
diff changeset
   282
       Let's do some extra verification, we don't care about the performance
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2592
diff changeset
   283
       in this situation.
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2592
diff changeset
   284
     */
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2592
diff changeset
   285
    jlong cenpos = endpos - ENDSIZ(endbuf);
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2592
diff changeset
   286
    jlong locpos = cenpos - ENDOFF(endbuf);
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2592
diff changeset
   287
    char buf[4];
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2592
diff changeset
   288
    return (cenpos >= 0 &&
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2592
diff changeset
   289
            locpos >= 0 &&
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2592
diff changeset
   290
            readFullyAt(zip->zfd, buf, sizeof(buf), cenpos) != -1 &&
29389
348a32f79d79 8074579: Use more efficient and readable way of checking PKZIP signatures
martin
parents: 27565
diff changeset
   291
            CENSIG_AT(buf) &&
3078
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2592
diff changeset
   292
            readFullyAt(zip->zfd, buf, sizeof(buf), locpos) != -1 &&
29389
348a32f79d79 8074579: Use more efficient and readable way of checking PKZIP signatures
martin
parents: 27565
diff changeset
   293
            LOCSIG_AT(buf));
3078
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2592
diff changeset
   294
}
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2592
diff changeset
   295
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
 * Searches for end of central directory (END) header. The contents of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
 * the END header will be read and placed in endbuf. Returns the file
1572
9fed7b18f517 6763122: ZipFile ctor does not throw exception when file is not a zip file
alanb
parents: 1228
diff changeset
   299
 * position of the END header, otherwise returns -1 if the END header
9fed7b18f517 6763122: ZipFile ctor does not throw exception when file is not a zip file
alanb
parents: 1228
diff changeset
   300
 * was not found or an error occurred.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
static jlong
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
findEND(jzfile *zip, void *endbuf)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    char buf[READBLOCKSZ];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    jlong pos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    const jlong len = zip->len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    const ZFILE zfd = zip->zfd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
    const jlong minHDR = len - END_MAXLEN > 0 ? len - END_MAXLEN : 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    const jlong minPos = minHDR - (sizeof(buf)-ENDHDR);
3078
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2592
diff changeset
   311
    jint clen;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    for (pos = len - sizeof(buf); pos >= minPos; pos -= (sizeof(buf)-ENDHDR)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        jlong off = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        if (pos < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            /* Pretend there are some NUL bytes before start of file */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            off = -pos;
7281
f202e1a7a134 6989471: compiler warnings building java/zip native code
sherman
parents: 5506
diff changeset
   320
            memset(buf, '\0', (size_t)off);
2
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 (readFullyAt(zfd, buf + off, sizeof(buf) - off,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                        pos + off) == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            return -1;  /* System error */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        /* Now scan the block backwards for END header signature */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        for (i = sizeof(buf) - ENDHDR; i >= 0; i--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            if (buf[i+0] == 'P'    &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                buf[i+1] == 'K'    &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                buf[i+2] == '\005' &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                buf[i+3] == '\006' &&
3078
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2592
diff changeset
   334
                ((pos + i + ENDHDR + ENDCOM(buf + i) == len)
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2592
diff changeset
   335
                 || verifyEND(zip, pos + i, buf + i))) {
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2592
diff changeset
   336
                /* Found END header */
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2592
diff changeset
   337
                memcpy(endbuf, buf + i, ENDHDR);
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2592
diff changeset
   338
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2592
diff changeset
   339
                clen = ENDCOM(endbuf);
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2592
diff changeset
   340
                if (clen != 0) {
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2592
diff changeset
   341
                    zip->comment = malloc(clen + 1);
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2592
diff changeset
   342
                    if (zip->comment == NULL) {
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2592
diff changeset
   343
                        return -1;
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2592
diff changeset
   344
                    }
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2592
diff changeset
   345
                    if (readFullyAt(zfd, zip->comment, clen, pos + i + ENDHDR)
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2592
diff changeset
   346
                        == -1) {
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2592
diff changeset
   347
                        free(zip->comment);
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2592
diff changeset
   348
                        zip->comment = NULL;
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2592
diff changeset
   349
                        return -1;
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2592
diff changeset
   350
                    }
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2592
diff changeset
   351
                    zip->comment[clen] = '\0';
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2592
diff changeset
   352
                    zip->clen = clen;
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2592
diff changeset
   353
                }
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2592
diff changeset
   354
                return pos + i;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
    }
3078
c491f0d2a8aa 6707281: Adler32.update() JavaDoc is wrong
sherman
parents: 2592
diff changeset
   358
1572
9fed7b18f517 6763122: ZipFile ctor does not throw exception when file is not a zip file
alanb
parents: 1228
diff changeset
   359
    return -1; /* END header not found */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
/*
2438
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 2163
diff changeset
   363
 * Searches for the ZIP64 end of central directory (END) header. The
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 2163
diff changeset
   364
 * contents of the ZIP64 END header will be read and placed in end64buf.
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 2163
diff changeset
   365
 * Returns the file position of the ZIP64 END header, otherwise returns
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 2163
diff changeset
   366
 * -1 if the END header was not found or an error occurred.
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 2163
diff changeset
   367
 *
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 2163
diff changeset
   368
 * The ZIP format specifies the "position" of each related record as
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 2163
diff changeset
   369
 *   ...
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 2163
diff changeset
   370
 *   [central directory]
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 2163
diff changeset
   371
 *   [zip64 end of central directory record]
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 2163
diff changeset
   372
 *   [zip64 end of central directory locator]
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 2163
diff changeset
   373
 *   [end of central directory record]
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 2163
diff changeset
   374
 *
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 2163
diff changeset
   375
 * The offset of zip64 end locator can be calculated from endpos as
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 2163
diff changeset
   376
 * "endpos - ZIP64_LOCHDR".
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 2163
diff changeset
   377
 * The "offset" of zip64 end record is stored in zip64 end locator.
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 2163
diff changeset
   378
 */
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 2163
diff changeset
   379
static jlong
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 2163
diff changeset
   380
findEND64(jzfile *zip, void *end64buf, jlong endpos)
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 2163
diff changeset
   381
{
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 2163
diff changeset
   382
    char loc64[ZIP64_LOCHDR];
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 2163
diff changeset
   383
    jlong end64pos;
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 2163
diff changeset
   384
    if (readFullyAt(zip->zfd, loc64, ZIP64_LOCHDR, endpos - ZIP64_LOCHDR) == -1) {
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 2163
diff changeset
   385
        return -1;    // end64 locator not found
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 2163
diff changeset
   386
    }
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 2163
diff changeset
   387
    end64pos = ZIP64_LOCOFF(loc64);
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 2163
diff changeset
   388
    if (readFullyAt(zip->zfd, end64buf, ZIP64_ENDHDR, end64pos) == -1) {
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 2163
diff changeset
   389
        return -1;    // end64 record not found
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 2163
diff changeset
   390
    }
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 2163
diff changeset
   391
    return end64pos;
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 2163
diff changeset
   392
}
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 2163
diff changeset
   393
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 2163
diff changeset
   394
/*
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
 * Returns a hash code value for a C-style NUL-terminated string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
static unsigned int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
hash(const char *s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
    int h = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
    while (*s != '\0')
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        h = 31*h + *s++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
    return h;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
 * Returns a hash code value for a string of a specified length.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
static unsigned int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
hashN(const char *s, int length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
    int h = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
    while (length-- > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        h = 31*h + *s++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
    return h;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
static unsigned int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
hash_append(unsigned int hash, char c)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
    return ((int)hash)*31 + c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
 * Returns true if the specified entry's name begins with the string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
 * "META-INF/" irrespective of case.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
static int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
isMetaName(const char *name, int length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
    const char *s;
7281
f202e1a7a134 6989471: compiler warnings building java/zip native code
sherman
parents: 5506
diff changeset
   432
    if (length < (int)sizeof("META-INF/") - 1)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
    for (s = "META-INF/"; *s != '\0'; s++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
        char c = *name++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        // Avoid toupper; it's locale-dependent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
        if (c >= 'a' && c <= 'z') c += 'A' - 'a';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
        if (*s != c)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
    return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
 * Increases the capacity of zip->metanames.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
 * Returns non-zero in case of allocation error.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
static int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
growMetaNames(jzfile *zip)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
    jint i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
    /* double the meta names array */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
    const jint new_metacount = zip->metacount << 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
    zip->metanames =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
        realloc(zip->metanames, new_metacount * sizeof(zip->metanames[0]));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
    if (zip->metanames == NULL) return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
    for (i = zip->metacount; i < new_metacount; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
        zip->metanames[i] = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
    zip->metacurrent = zip->metacount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
    zip->metacount = new_metacount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
    return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
 * Adds name to zip->metanames.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
 * Returns non-zero in case of allocation error.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
static int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
addMetaName(jzfile *zip, const char *name, int length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
    jint i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
    if (zip->metanames == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
      zip->metacount = INITIAL_META_COUNT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
      zip->metanames = calloc(zip->metacount, sizeof(zip->metanames[0]));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
      if (zip->metanames == NULL) return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
      zip->metacurrent = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
    i = zip->metacurrent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
    /* current meta name array isn't full yet. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
    if (i < zip->metacount) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
      zip->metanames[i] = (char *) malloc(length+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
      if (zip->metanames[i] == NULL) return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
      memcpy(zip->metanames[i], name, length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
      zip->metanames[i][length] = '\0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
      zip->metacurrent++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
      return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
    /* No free entries in zip->metanames? */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
    if (growMetaNames(zip) != 0) return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
    return addMetaName(zip, name, length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
freeMetaNames(jzfile *zip)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
    if (zip->metanames) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
        jint i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
        for (i = 0; i < zip->metacount; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
            free(zip->metanames[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
        free(zip->metanames);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
        zip->metanames = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
/* Free Zip data allocated by readCEN() */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
freeCEN(jzfile *zip)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
    free(zip->entries); zip->entries = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
    free(zip->table);   zip->table   = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
    freeMetaNames(zip);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
 * Counts the number of CEN headers in a central directory extending
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
 * from BEG to END.  Might return a bogus answer if the zip file is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
 * corrupt, but will not crash.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
static jint
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
countCENHeaders(unsigned char *beg, unsigned char *end)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
    jint count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
    ptrdiff_t i;
11898
5621097881f1 7118283: Better input parameter checking in zip file processing
sherman
parents: 7668
diff changeset
   527
    for (i = 0; i + CENHDR <= end - beg; i += CENSIZE(beg + i))
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
        count++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
    return count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
#define ZIP_FORMAT_ERROR(message) \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
if (1) { zip->msg = message; goto Catch; } else ((void)0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
 * Reads zip file central directory. Returns the file position of first
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 18223
diff changeset
   537
 * CEN header, otherwise returns -1 if an error occurred. If zip->msg != NULL
1572
9fed7b18f517 6763122: ZipFile ctor does not throw exception when file is not a zip file
alanb
parents: 1228
diff changeset
   538
 * then the error was a zip format error and zip->msg has the error text.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
 * Always pass in -1 for knownTotal; it's used for a recursive call.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
static jlong
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
readCEN(jzfile *zip, jint knownTotal)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
    /* Following are unsigned 32-bit */
2438
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 2163
diff changeset
   545
    jlong endpos, end64pos, cenpos, cenlen, cenoff;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
    /* Following are unsigned 16-bit */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
    jint total, tablelen, i, j;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
    unsigned char *cenbuf = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
    unsigned char *cenend;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
    unsigned char *cp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
#ifdef USE_MMAP
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
    static jlong pagesize;
2074
346a841b2dcc 6599383: Unable to open zip files more than 2GB in size
kevinw
parents: 1572
diff changeset
   553
    jlong offset;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
    unsigned char endbuf[ENDHDR];
2438
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 2163
diff changeset
   556
    jint endhdrlen = ENDHDR;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
    jzcell *entries;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
    jint *table;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
    /* Clear previous zip error */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
    zip->msg = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
    /* Get position of END header */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
    if ((endpos = findEND(zip, endbuf)) == -1)
1572
9fed7b18f517 6763122: ZipFile ctor does not throw exception when file is not a zip file
alanb
parents: 1228
diff changeset
   564
        return -1; /* no END header or system error */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
1572
9fed7b18f517 6763122: ZipFile ctor does not throw exception when file is not a zip file
alanb
parents: 1228
diff changeset
   566
    if (endpos == 0) return 0;  /* only END header present */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
    freeCEN(zip);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
   /* Get position and length of central directory */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
    cenlen = ENDSIZ(endbuf);
2438
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 2163
diff changeset
   571
    cenoff = ENDOFF(endbuf);
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 2163
diff changeset
   572
    total  = ENDTOT(endbuf);
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 2163
diff changeset
   573
    if (cenlen == ZIP64_MAGICVAL || cenoff == ZIP64_MAGICVAL ||
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 2163
diff changeset
   574
        total == ZIP64_MAGICCOUNT) {
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 2163
diff changeset
   575
        unsigned char end64buf[ZIP64_ENDHDR];
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 2163
diff changeset
   576
        if ((end64pos = findEND64(zip, end64buf, endpos)) != -1) {
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 2163
diff changeset
   577
            cenlen = ZIP64_ENDSIZ(end64buf);
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 2163
diff changeset
   578
            cenoff = ZIP64_ENDOFF(end64buf);
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 2163
diff changeset
   579
            total = (jint)ZIP64_ENDTOT(end64buf);
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 2163
diff changeset
   580
            endpos = end64pos;
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 2163
diff changeset
   581
            endhdrlen = ZIP64_ENDHDR;
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 2163
diff changeset
   582
        }
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 2163
diff changeset
   583
    }
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 2163
diff changeset
   584
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
    if (cenlen > endpos)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
        ZIP_FORMAT_ERROR("invalid END header (bad central directory size)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
    cenpos = endpos - cenlen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
    /* Get position of first local file (LOC) header, taking into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
     * account that there may be a stub prefixed to the zip file. */
2438
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 2163
diff changeset
   591
    zip->locpos = cenpos - cenoff;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
    if (zip->locpos < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
        ZIP_FORMAT_ERROR("invalid END header (bad central directory offset)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
#ifdef USE_MMAP
5148
2bf5469864ea 6929479: Add a system property sun.zip.disableMemoryMapping to disable mmap use in ZipFile
sherman
parents: 3078
diff changeset
   596
    if (zip->usemmap) {
2bf5469864ea 6929479: Add a system property sun.zip.disableMemoryMapping to disable mmap use in ZipFile
sherman
parents: 3078
diff changeset
   597
      /* On Solaris & Linux prior to JDK 6, we used to mmap the whole jar file to
2bf5469864ea 6929479: Add a system property sun.zip.disableMemoryMapping to disable mmap use in ZipFile
sherman
parents: 3078
diff changeset
   598
       * read the jar file contents. However, this greatly increased the perceived
2bf5469864ea 6929479: Add a system property sun.zip.disableMemoryMapping to disable mmap use in ZipFile
sherman
parents: 3078
diff changeset
   599
       * footprint numbers because the mmap'ed pages were adding into the totals shown
2bf5469864ea 6929479: Add a system property sun.zip.disableMemoryMapping to disable mmap use in ZipFile
sherman
parents: 3078
diff changeset
   600
       * by 'ps' and 'top'. We switched to mmaping only the central directory of jar
2bf5469864ea 6929479: Add a system property sun.zip.disableMemoryMapping to disable mmap use in ZipFile
sherman
parents: 3078
diff changeset
   601
       * file while calling 'read' to read the rest of jar file. Here are a list of
2bf5469864ea 6929479: Add a system property sun.zip.disableMemoryMapping to disable mmap use in ZipFile
sherman
parents: 3078
diff changeset
   602
       * reasons apart from above of why we are doing so:
2bf5469864ea 6929479: Add a system property sun.zip.disableMemoryMapping to disable mmap use in ZipFile
sherman
parents: 3078
diff changeset
   603
       * 1. Greatly reduces mmap overhead after startup complete;
2bf5469864ea 6929479: Add a system property sun.zip.disableMemoryMapping to disable mmap use in ZipFile
sherman
parents: 3078
diff changeset
   604
       * 2. Avoids dual path code maintainance;
2bf5469864ea 6929479: Add a system property sun.zip.disableMemoryMapping to disable mmap use in ZipFile
sherman
parents: 3078
diff changeset
   605
       * 3. Greatly reduces risk of address space (not virtual memory) exhaustion.
2bf5469864ea 6929479: Add a system property sun.zip.disableMemoryMapping to disable mmap use in ZipFile
sherman
parents: 3078
diff changeset
   606
       */
2bf5469864ea 6929479: Add a system property sun.zip.disableMemoryMapping to disable mmap use in ZipFile
sherman
parents: 3078
diff changeset
   607
        if (pagesize == 0) {
2bf5469864ea 6929479: Add a system property sun.zip.disableMemoryMapping to disable mmap use in ZipFile
sherman
parents: 3078
diff changeset
   608
            pagesize = (jlong)sysconf(_SC_PAGESIZE);
2bf5469864ea 6929479: Add a system property sun.zip.disableMemoryMapping to disable mmap use in ZipFile
sherman
parents: 3078
diff changeset
   609
            if (pagesize == 0) goto Catch;
2bf5469864ea 6929479: Add a system property sun.zip.disableMemoryMapping to disable mmap use in ZipFile
sherman
parents: 3078
diff changeset
   610
        }
2bf5469864ea 6929479: Add a system property sun.zip.disableMemoryMapping to disable mmap use in ZipFile
sherman
parents: 3078
diff changeset
   611
        if (cenpos > pagesize) {
2bf5469864ea 6929479: Add a system property sun.zip.disableMemoryMapping to disable mmap use in ZipFile
sherman
parents: 3078
diff changeset
   612
            offset = cenpos & ~(pagesize - 1);
2bf5469864ea 6929479: Add a system property sun.zip.disableMemoryMapping to disable mmap use in ZipFile
sherman
parents: 3078
diff changeset
   613
        } else {
2bf5469864ea 6929479: Add a system property sun.zip.disableMemoryMapping to disable mmap use in ZipFile
sherman
parents: 3078
diff changeset
   614
            offset = 0;
2bf5469864ea 6929479: Add a system property sun.zip.disableMemoryMapping to disable mmap use in ZipFile
sherman
parents: 3078
diff changeset
   615
        }
2bf5469864ea 6929479: Add a system property sun.zip.disableMemoryMapping to disable mmap use in ZipFile
sherman
parents: 3078
diff changeset
   616
        /* When we are not calling recursively, knownTotal is -1. */
2bf5469864ea 6929479: Add a system property sun.zip.disableMemoryMapping to disable mmap use in ZipFile
sherman
parents: 3078
diff changeset
   617
        if (knownTotal == -1) {
2bf5469864ea 6929479: Add a system property sun.zip.disableMemoryMapping to disable mmap use in ZipFile
sherman
parents: 3078
diff changeset
   618
            void* mappedAddr;
2bf5469864ea 6929479: Add a system property sun.zip.disableMemoryMapping to disable mmap use in ZipFile
sherman
parents: 3078
diff changeset
   619
            /* Mmap the CEN and END part only. We have to figure
2bf5469864ea 6929479: Add a system property sun.zip.disableMemoryMapping to disable mmap use in ZipFile
sherman
parents: 3078
diff changeset
   620
               out the page size in order to make offset to be multiples of
2bf5469864ea 6929479: Add a system property sun.zip.disableMemoryMapping to disable mmap use in ZipFile
sherman
parents: 3078
diff changeset
   621
               page size.
2bf5469864ea 6929479: Add a system property sun.zip.disableMemoryMapping to disable mmap use in ZipFile
sherman
parents: 3078
diff changeset
   622
            */
2bf5469864ea 6929479: Add a system property sun.zip.disableMemoryMapping to disable mmap use in ZipFile
sherman
parents: 3078
diff changeset
   623
            zip->mlen = cenpos - offset + cenlen + endhdrlen;
2bf5469864ea 6929479: Add a system property sun.zip.disableMemoryMapping to disable mmap use in ZipFile
sherman
parents: 3078
diff changeset
   624
            zip->offset = offset;
2bf5469864ea 6929479: Add a system property sun.zip.disableMemoryMapping to disable mmap use in ZipFile
sherman
parents: 3078
diff changeset
   625
            mappedAddr = mmap64(0, zip->mlen, PROT_READ, MAP_SHARED, zip->zfd, (off64_t) offset);
2bf5469864ea 6929479: Add a system property sun.zip.disableMemoryMapping to disable mmap use in ZipFile
sherman
parents: 3078
diff changeset
   626
            zip->maddr = (mappedAddr == (void*) MAP_FAILED) ? NULL :
2bf5469864ea 6929479: Add a system property sun.zip.disableMemoryMapping to disable mmap use in ZipFile
sherman
parents: 3078
diff changeset
   627
                (unsigned char*)mappedAddr;
2bf5469864ea 6929479: Add a system property sun.zip.disableMemoryMapping to disable mmap use in ZipFile
sherman
parents: 3078
diff changeset
   628
2bf5469864ea 6929479: Add a system property sun.zip.disableMemoryMapping to disable mmap use in ZipFile
sherman
parents: 3078
diff changeset
   629
            if (zip->maddr == NULL) {
2bf5469864ea 6929479: Add a system property sun.zip.disableMemoryMapping to disable mmap use in ZipFile
sherman
parents: 3078
diff changeset
   630
                jio_fprintf(stderr, "mmap failed for CEN and END part of zip file\n");
2bf5469864ea 6929479: Add a system property sun.zip.disableMemoryMapping to disable mmap use in ZipFile
sherman
parents: 3078
diff changeset
   631
                goto Catch;
2bf5469864ea 6929479: Add a system property sun.zip.disableMemoryMapping to disable mmap use in ZipFile
sherman
parents: 3078
diff changeset
   632
            }
2bf5469864ea 6929479: Add a system property sun.zip.disableMemoryMapping to disable mmap use in ZipFile
sherman
parents: 3078
diff changeset
   633
        }
2bf5469864ea 6929479: Add a system property sun.zip.disableMemoryMapping to disable mmap use in ZipFile
sherman
parents: 3078
diff changeset
   634
        cenbuf = zip->maddr + cenpos - offset;
2bf5469864ea 6929479: Add a system property sun.zip.disableMemoryMapping to disable mmap use in ZipFile
sherman
parents: 3078
diff changeset
   635
    } else
2bf5469864ea 6929479: Add a system property sun.zip.disableMemoryMapping to disable mmap use in ZipFile
sherman
parents: 3078
diff changeset
   636
#endif
2bf5469864ea 6929479: Add a system property sun.zip.disableMemoryMapping to disable mmap use in ZipFile
sherman
parents: 3078
diff changeset
   637
    {
2bf5469864ea 6929479: Add a system property sun.zip.disableMemoryMapping to disable mmap use in ZipFile
sherman
parents: 3078
diff changeset
   638
        if ((cenbuf = malloc((size_t) cenlen)) == NULL ||
2bf5469864ea 6929479: Add a system property sun.zip.disableMemoryMapping to disable mmap use in ZipFile
sherman
parents: 3078
diff changeset
   639
            (readFullyAt(zip->zfd, cenbuf, cenlen, cenpos) == -1))
2bf5469864ea 6929479: Add a system property sun.zip.disableMemoryMapping to disable mmap use in ZipFile
sherman
parents: 3078
diff changeset
   640
        goto Catch;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
    cenend = cenbuf + cenlen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
    /* Initialize zip file data structures based on the total number
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
     * of central directory entries as stored in ENDTOT.  Since this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
     * is a 2-byte field, but we (and other zip implementations)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
     * support approx. 2**31 entries, we do not trust ENDTOT, but
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
     * treat it only as a strong hint.  When we call ourselves
2438
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 2163
diff changeset
   650
     * recursively, knownTotal will have the "true" value.
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 2163
diff changeset
   651
     *
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 2163
diff changeset
   652
     * Keep this path alive even with the Zip64 END support added, just
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 2163
diff changeset
   653
     * for zip files that have more than 0xffff entries but don't have
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 2163
diff changeset
   654
     * the Zip64 enabled.
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 2163
diff changeset
   655
     */
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 2163
diff changeset
   656
    total = (knownTotal != -1) ? knownTotal : total;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
    entries  = zip->entries  = calloc(total, sizeof(entries[0]));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
    tablelen = zip->tablelen = ((total/2) | 1); // Odd -> fewer collisions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
    table    = zip->table    = malloc(tablelen * sizeof(table[0]));
22605
dba3d6b22818 8031581: PPC64: Addons and fixes for AIX to pass the jdk regression tests
simonis
parents: 21278
diff changeset
   660
    /* According to ISO C it is perfectly legal for malloc to return zero
dba3d6b22818 8031581: PPC64: Addons and fixes for AIX to pass the jdk regression tests
simonis
parents: 21278
diff changeset
   661
     * if called with a zero argument. We check this for 'entries' but not
dba3d6b22818 8031581: PPC64: Addons and fixes for AIX to pass the jdk regression tests
simonis
parents: 21278
diff changeset
   662
     * for 'table' because 'tablelen' can't be zero (see computation above). */
dba3d6b22818 8031581: PPC64: Addons and fixes for AIX to pass the jdk regression tests
simonis
parents: 21278
diff changeset
   663
    if ((entries == NULL && total != 0) || table == NULL) goto Catch;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
    for (j = 0; j < tablelen; j++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
        table[j] = ZIP_ENDCHAIN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
    /* Iterate through the entries in the central directory */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
    for (i = 0, cp = cenbuf; cp <= cenend - CENHDR; i++, cp += CENSIZE(cp)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
        /* Following are unsigned 16-bit */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
        jint method, nlen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
        unsigned int hsh;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
        if (i >= total) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
            /* This will only happen if the zip file has an incorrect
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
             * ENDTOT field, which usually means it contains more than
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
             * 65535 entries. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
            cenpos = readCEN(zip, countCENHeaders(cenbuf, cenend));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
            goto Finally;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
        method = CENHOW(cp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
        nlen   = CENNAM(cp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
29389
348a32f79d79 8074579: Use more efficient and readable way of checking PKZIP signatures
martin
parents: 27565
diff changeset
   684
        if (!CENSIG_AT(cp))
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
            ZIP_FORMAT_ERROR("invalid CEN header (bad signature)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
        if (CENFLG(cp) & 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
            ZIP_FORMAT_ERROR("invalid CEN header (encrypted entry)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
        if (method != STORED && method != DEFLATED)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
            ZIP_FORMAT_ERROR("invalid CEN header (bad compression method)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
        if (cp + CENHDR + nlen > cenend)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
            ZIP_FORMAT_ERROR("invalid CEN header (bad header size)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
        /* if the entry is metadata add it to our metadata names */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
        if (isMetaName((char *)cp+CENHDR, nlen))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
            if (addMetaName(zip, (char *)cp+CENHDR, nlen) != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
                goto Catch;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
        /* Record the CEN offset and the name hash in our hash cell. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
        entries[i].cenpos = cenpos + (cp - cenbuf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
        entries[i].hash = hashN((char *)cp+CENHDR, nlen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
        /* Add the entry to the hash table */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
        hsh = entries[i].hash % tablelen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
        entries[i].next = table[hsh];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
        table[hsh] = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
    if (cp != cenend)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
        ZIP_FORMAT_ERROR("invalid CEN header (bad header size)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
    zip->total = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
    goto Finally;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
 Catch:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
    freeCEN(zip);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
    cenpos = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
 Finally:
5148
2bf5469864ea 6929479: Add a system property sun.zip.disableMemoryMapping to disable mmap use in ZipFile
sherman
parents: 3078
diff changeset
   718
#ifdef USE_MMAP
2bf5469864ea 6929479: Add a system property sun.zip.disableMemoryMapping to disable mmap use in ZipFile
sherman
parents: 3078
diff changeset
   719
    if (!zip->usemmap)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
#endif
5148
2bf5469864ea 6929479: Add a system property sun.zip.disableMemoryMapping to disable mmap use in ZipFile
sherman
parents: 3078
diff changeset
   721
        free(cenbuf);
2bf5469864ea 6929479: Add a system property sun.zip.disableMemoryMapping to disable mmap use in ZipFile
sherman
parents: 3078
diff changeset
   722
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
    return cenpos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
 * Opens a zip file with the specified mode. Returns the jzfile object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
 * or NULL if an error occurred. If a zip error occurred then *pmsg will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
 * be set to the error message text if pmsg != 0. Otherwise, *pmsg will be
12543
b77bc2f404ba 7159982: ZipFile uses static for error message when malformed zip file encountered
zhouyx
parents: 12047
diff changeset
   730
 * set to NULL. Caller is responsible to free the error message.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
jzfile *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
ZIP_Open_Generic(const char *name, char **pmsg, int mode, jlong lastModified)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
    jzfile *zip = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
    /* Clear zip error message */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
    if (pmsg != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
        *pmsg = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
    zip = ZIP_Get_From_Cache(name, pmsg, lastModified);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
    if (zip == NULL && *pmsg == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
        ZFILE zfd = ZFILE_Open(name, mode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
        zip = ZIP_Put_In_Cache(name, zfd, pmsg, lastModified);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
    return zip;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
 * Returns the jzfile corresponding to the given file name from the cache of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
 * zip files, or NULL if the file is not in the cache.  If the name is longer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
 * than PATH_MAX or a zip error occurred then *pmsg will be set to the error
12543
b77bc2f404ba 7159982: ZipFile uses static for error message when malformed zip file encountered
zhouyx
parents: 12047
diff changeset
   755
 * message text if pmsg != 0. Otherwise, *pmsg will be set to NULL. Caller
b77bc2f404ba 7159982: ZipFile uses static for error message when malformed zip file encountered
zhouyx
parents: 12047
diff changeset
   756
 * is responsible to free the error message.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
jzfile *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
ZIP_Get_From_Cache(const char *name, char **pmsg, jlong lastModified)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
    char buf[PATH_MAX];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
    jzfile *zip;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
    if (InitializeZip()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
    /* Clear zip error message */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
    if (pmsg != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
        *pmsg = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
    if (strlen(name) >= PATH_MAX) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
        if (pmsg) {
12543
b77bc2f404ba 7159982: ZipFile uses static for error message when malformed zip file encountered
zhouyx
parents: 12047
diff changeset
   775
            *pmsg = strdup("zip file name too long");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
    strcpy(buf, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
    JVM_NativePath(buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
    name = buf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
    MLOCK(zfiles_lock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
    for (zip = zfiles; zip != NULL; zip = zip->next) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
        if (strcmp(name, zip->name) == 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
            && (zip->lastModified == lastModified || zip->lastModified == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
            && zip->refs < MAXREFS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
            zip->refs++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
    MUNLOCK(zfiles_lock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
    return zip;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
 * Reads data from the given file descriptor to create a jzfile, puts the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
 * jzfile in a cache, and returns that jzfile.  Returns NULL in case of error.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
 * If a zip error occurs, then *pmsg will be set to the error message text if
12543
b77bc2f404ba 7159982: ZipFile uses static for error message when malformed zip file encountered
zhouyx
parents: 12047
diff changeset
   800
 * pmsg != 0. Otherwise, *pmsg will be set to NULL. Caller is responsible to
b77bc2f404ba 7159982: ZipFile uses static for error message when malformed zip file encountered
zhouyx
parents: 12047
diff changeset
   801
 * free the error message.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
 */
5148
2bf5469864ea 6929479: Add a system property sun.zip.disableMemoryMapping to disable mmap use in ZipFile
sherman
parents: 3078
diff changeset
   803
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
jzfile *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
ZIP_Put_In_Cache(const char *name, ZFILE zfd, char **pmsg, jlong lastModified)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
{
5148
2bf5469864ea 6929479: Add a system property sun.zip.disableMemoryMapping to disable mmap use in ZipFile
sherman
parents: 3078
diff changeset
   807
    return ZIP_Put_In_Cache0(name, zfd, pmsg, lastModified, JNI_TRUE);
2bf5469864ea 6929479: Add a system property sun.zip.disableMemoryMapping to disable mmap use in ZipFile
sherman
parents: 3078
diff changeset
   808
}
2bf5469864ea 6929479: Add a system property sun.zip.disableMemoryMapping to disable mmap use in ZipFile
sherman
parents: 3078
diff changeset
   809
2bf5469864ea 6929479: Add a system property sun.zip.disableMemoryMapping to disable mmap use in ZipFile
sherman
parents: 3078
diff changeset
   810
jzfile *
2bf5469864ea 6929479: Add a system property sun.zip.disableMemoryMapping to disable mmap use in ZipFile
sherman
parents: 3078
diff changeset
   811
ZIP_Put_In_Cache0(const char *name, ZFILE zfd, char **pmsg, jlong lastModified,
2bf5469864ea 6929479: Add a system property sun.zip.disableMemoryMapping to disable mmap use in ZipFile
sherman
parents: 3078
diff changeset
   812
                 jboolean usemmap)
2bf5469864ea 6929479: Add a system property sun.zip.disableMemoryMapping to disable mmap use in ZipFile
sherman
parents: 3078
diff changeset
   813
{
12543
b77bc2f404ba 7159982: ZipFile uses static for error message when malformed zip file encountered
zhouyx
parents: 12047
diff changeset
   814
    char errbuf[256];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
    jlong len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
    jzfile *zip;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
    if ((zip = allocZip(name)) == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
5148
2bf5469864ea 6929479: Add a system property sun.zip.disableMemoryMapping to disable mmap use in ZipFile
sherman
parents: 3078
diff changeset
   822
#ifdef USE_MMAP
2bf5469864ea 6929479: Add a system property sun.zip.disableMemoryMapping to disable mmap use in ZipFile
sherman
parents: 3078
diff changeset
   823
    zip->usemmap = usemmap;
2bf5469864ea 6929479: Add a system property sun.zip.disableMemoryMapping to disable mmap use in ZipFile
sherman
parents: 3078
diff changeset
   824
#endif
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
    zip->refs = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
    zip->lastModified = lastModified;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
    if (zfd == -1) {
27184
2996674bd701 8057777: Cleanup of old and unused VM interfaces
fparain
parents: 25859
diff changeset
   829
        if (pmsg && getLastErrorString(errbuf, sizeof(errbuf)) > 0)
12543
b77bc2f404ba 7159982: ZipFile uses static for error message when malformed zip file encountered
zhouyx
parents: 12047
diff changeset
   830
            *pmsg = strdup(errbuf);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
        freeZip(zip);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
18223
35a5c2462991 8008593: Better URLClassLoader resource management
chegar
parents: 14342
diff changeset
   835
    // Assumption, zfd refers to start of file. Trivially, reuse errbuf.
35a5c2462991 8008593: Better URLClassLoader resource management
chegar
parents: 14342
diff changeset
   836
    if (readFully(zfd, errbuf, 4) != -1) {  // errors will be handled later
29389
348a32f79d79 8074579: Use more efficient and readable way of checking PKZIP signatures
martin
parents: 27565
diff changeset
   837
        zip->locsig = LOCSIG_AT(errbuf) ? JNI_TRUE : JNI_FALSE;
18223
35a5c2462991 8008593: Better URLClassLoader resource management
chegar
parents: 14342
diff changeset
   838
    }
35a5c2462991 8008593: Better URLClassLoader resource management
chegar
parents: 14342
diff changeset
   839
2074
346a841b2dcc 6599383: Unable to open zip files more than 2GB in size
kevinw
parents: 1572
diff changeset
   840
    len = zip->len = IO_Lseek(zfd, 0, SEEK_END);
1228
1515928f48cd 6440786: Cannot create a ZIP file containing zero entries
bristor
parents: 2
diff changeset
   841
    if (len <= 0) {
1515928f48cd 6440786: Cannot create a ZIP file containing zero entries
bristor
parents: 2
diff changeset
   842
        if (len == 0) { /* zip file is empty */
1515928f48cd 6440786: Cannot create a ZIP file containing zero entries
bristor
parents: 2
diff changeset
   843
            if (pmsg) {
12543
b77bc2f404ba 7159982: ZipFile uses static for error message when malformed zip file encountered
zhouyx
parents: 12047
diff changeset
   844
                *pmsg = strdup("zip file is empty");
1228
1515928f48cd 6440786: Cannot create a ZIP file containing zero entries
bristor
parents: 2
diff changeset
   845
            }
1515928f48cd 6440786: Cannot create a ZIP file containing zero entries
bristor
parents: 2
diff changeset
   846
        } else { /* error */
27184
2996674bd701 8057777: Cleanup of old and unused VM interfaces
fparain
parents: 25859
diff changeset
   847
            if (pmsg && getLastErrorString(errbuf, sizeof(errbuf)) > 0)
12543
b77bc2f404ba 7159982: ZipFile uses static for error message when malformed zip file encountered
zhouyx
parents: 12047
diff changeset
   848
                *pmsg = strdup(errbuf);
1228
1515928f48cd 6440786: Cannot create a ZIP file containing zero entries
bristor
parents: 2
diff changeset
   849
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
        ZFILE_Close(zfd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
        freeZip(zip);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
    zip->zfd = zfd;
1228
1515928f48cd 6440786: Cannot create a ZIP file containing zero entries
bristor
parents: 2
diff changeset
   856
    if (readCEN(zip, -1) < 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
        /* An error occurred while trying to read the zip file */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
        if (pmsg != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
            /* Set the zip error message */
12543
b77bc2f404ba 7159982: ZipFile uses static for error message when malformed zip file encountered
zhouyx
parents: 12047
diff changeset
   860
            if (zip->msg != NULL)
b77bc2f404ba 7159982: ZipFile uses static for error message when malformed zip file encountered
zhouyx
parents: 12047
diff changeset
   861
                *pmsg = strdup(zip->msg);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
        freeZip(zip);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
    MLOCK(zfiles_lock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
    zip->next = zfiles;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
    zfiles = zip;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
    MUNLOCK(zfiles_lock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
    return zip;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
 * Opens a zip file for reading. Returns the jzfile object or NULL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
 * if an error occurred. If a zip error occurred then *msg will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
 * set to the error message text if msg != 0. Otherwise, *msg will be
12543
b77bc2f404ba 7159982: ZipFile uses static for error message when malformed zip file encountered
zhouyx
parents: 12047
diff changeset
   878
 * set to NULL. Caller doesn't need to free the error message.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
jzfile * JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
ZIP_Open(const char *name, char **pmsg)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
{
12543
b77bc2f404ba 7159982: ZipFile uses static for error message when malformed zip file encountered
zhouyx
parents: 12047
diff changeset
   883
    jzfile *file = ZIP_Open_Generic(name, pmsg, O_RDONLY, 0);
b77bc2f404ba 7159982: ZipFile uses static for error message when malformed zip file encountered
zhouyx
parents: 12047
diff changeset
   884
    if (file == NULL && pmsg != NULL && *pmsg != NULL) {
b77bc2f404ba 7159982: ZipFile uses static for error message when malformed zip file encountered
zhouyx
parents: 12047
diff changeset
   885
        free(*pmsg);
b77bc2f404ba 7159982: ZipFile uses static for error message when malformed zip file encountered
zhouyx
parents: 12047
diff changeset
   886
        *pmsg = "Zip file open error";
b77bc2f404ba 7159982: ZipFile uses static for error message when malformed zip file encountered
zhouyx
parents: 12047
diff changeset
   887
    }
b77bc2f404ba 7159982: ZipFile uses static for error message when malformed zip file encountered
zhouyx
parents: 12047
diff changeset
   888
    return file;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
 * Closes the specified zip file object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
ZIP_Close(jzfile *zip)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
    MLOCK(zfiles_lock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
    if (--zip->refs > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
        /* Still more references so just return */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
        MUNLOCK(zfiles_lock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
    /* No other references so close the file and remove from list */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
    if (zfiles == zip) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
        zfiles = zfiles->next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
        jzfile *zp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
        for (zp = zfiles; zp->next != 0; zp = zp->next) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
            if (zp->next == zip) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
                zp->next = zip->next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
    MUNLOCK(zfiles_lock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
    freeZip(zip);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
/* Empirically, most CEN headers are smaller than this. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
#define AMPLE_CEN_HEADER_SIZE 160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
/* A good buffer size when we want to read CEN headers sequentially. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
#define CENCACHE_PAGESIZE 8192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
static char *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
readCENHeader(jzfile *zip, jlong cenpos, jint bufsize)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
    jint censize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
    ZFILE zfd = zip->zfd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
    char *cen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
    if (bufsize > zip->len - cenpos)
7281
f202e1a7a134 6989471: compiler warnings building java/zip native code
sherman
parents: 5506
diff changeset
   933
        bufsize = (jint)(zip->len - cenpos);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
    if ((cen = malloc(bufsize)) == NULL)       goto Catch;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
    if (readFullyAt(zfd, cen, bufsize, cenpos) == -1)     goto Catch;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
    censize = CENSIZE(cen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
    if (censize <= bufsize) return cen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
    if ((cen = realloc(cen, censize)) == NULL)              goto Catch;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
    if (readFully(zfd, cen+bufsize, censize-bufsize) == -1) goto Catch;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
    return cen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
 Catch:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
    free(cen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
    return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
static char *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
sequentialAccessReadCENHeader(jzfile *zip, jlong cenpos)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
    cencache *cache = &zip->cencache;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
    char *cen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
    if (cache->data != NULL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
        && (cenpos >= cache->pos)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
        && (cenpos + CENHDR <= cache->pos + CENCACHE_PAGESIZE))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
        cen = cache->data + cenpos - cache->pos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
        if (cenpos + CENSIZE(cen) <= cache->pos + CENCACHE_PAGESIZE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
            /* A cache hit */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
            return cen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
    if ((cen = readCENHeader(zip, cenpos, CENCACHE_PAGESIZE)) == NULL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
        return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
    free(cache->data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
    cache->data = cen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
    cache->pos  = cenpos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
    return cen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
typedef enum { ACCESS_RANDOM, ACCESS_SEQUENTIAL } AccessHint;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
 * Return a new initialized jzentry corresponding to a given hash cell.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
 * In case of error, returns NULL.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
 * We already sanity-checked all the CEN headers for ZIP format errors
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
 * in readCEN(), so we don't check them again here.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
 * The ZIP lock should be held here.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
static jzentry *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
newEntry(jzfile *zip, jzcell *zc, AccessHint accessHint)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
{
2438
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 2163
diff changeset
   982
    jlong locoff;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
    jint nlen, elen, clen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
    jzentry *ze;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
    char *cen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
    if ((ze = (jzentry *) malloc(sizeof(jzentry))) == NULL) return NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
    ze->name    = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
    ze->extra   = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
    ze->comment = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
#ifdef USE_MMAP
5148
2bf5469864ea 6929479: Add a system property sun.zip.disableMemoryMapping to disable mmap use in ZipFile
sherman
parents: 3078
diff changeset
   993
    if (zip->usemmap) {
2bf5469864ea 6929479: Add a system property sun.zip.disableMemoryMapping to disable mmap use in ZipFile
sherman
parents: 3078
diff changeset
   994
        cen = (char*) zip->maddr + zc->cenpos - zip->offset;
2bf5469864ea 6929479: Add a system property sun.zip.disableMemoryMapping to disable mmap use in ZipFile
sherman
parents: 3078
diff changeset
   995
    } else
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
#endif
5148
2bf5469864ea 6929479: Add a system property sun.zip.disableMemoryMapping to disable mmap use in ZipFile
sherman
parents: 3078
diff changeset
   997
    {
2bf5469864ea 6929479: Add a system property sun.zip.disableMemoryMapping to disable mmap use in ZipFile
sherman
parents: 3078
diff changeset
   998
        if (accessHint == ACCESS_RANDOM)
2bf5469864ea 6929479: Add a system property sun.zip.disableMemoryMapping to disable mmap use in ZipFile
sherman
parents: 3078
diff changeset
   999
            cen = readCENHeader(zip, zc->cenpos, AMPLE_CEN_HEADER_SIZE);
2bf5469864ea 6929479: Add a system property sun.zip.disableMemoryMapping to disable mmap use in ZipFile
sherman
parents: 3078
diff changeset
  1000
        else
2bf5469864ea 6929479: Add a system property sun.zip.disableMemoryMapping to disable mmap use in ZipFile
sherman
parents: 3078
diff changeset
  1001
            cen = sequentialAccessReadCENHeader(zip, zc->cenpos);
2bf5469864ea 6929479: Add a system property sun.zip.disableMemoryMapping to disable mmap use in ZipFile
sherman
parents: 3078
diff changeset
  1002
        if (cen == NULL) goto Catch;
2bf5469864ea 6929479: Add a system property sun.zip.disableMemoryMapping to disable mmap use in ZipFile
sherman
parents: 3078
diff changeset
  1003
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
    nlen      = CENNAM(cen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
    elen      = CENEXT(cen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
    clen      = CENCOM(cen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
    ze->time  = CENTIM(cen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
    ze->size  = CENLEN(cen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
    ze->csize = (CENHOW(cen) == STORED) ? 0 : CENSIZ(cen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
    ze->crc   = CENCRC(cen);
2438
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 2163
diff changeset
  1012
    locoff    = CENOFF(cen);
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 2163
diff changeset
  1013
    ze->pos   = -(zip->locpos + locoff);
2592
ef26f663a2ba 4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents: 2438
diff changeset
  1014
    ze->flag  = CENFLG(cen);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
    if ((ze->name = malloc(nlen + 1)) == NULL) goto Catch;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
    memcpy(ze->name, cen + CENHDR, nlen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
    ze->name[nlen] = '\0';
27084
d7fbf9a294af 8048025: Ensure cache consistency
sherman
parents: 25859
diff changeset
  1019
    ze->nlen = nlen;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
    if (elen > 0) {
2438
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 2163
diff changeset
  1021
        char *extra = cen + CENHDR + nlen;
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 2163
diff changeset
  1022
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
        /* This entry has "extra" data */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
        if ((ze->extra = malloc(elen + 2)) == NULL) goto Catch;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
        ze->extra[0] = (unsigned char) elen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
        ze->extra[1] = (unsigned char) (elen >> 8);
2438
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 2163
diff changeset
  1027
        memcpy(ze->extra+2, extra, elen);
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 2163
diff changeset
  1028
        if (ze->csize == ZIP64_MAGICVAL || ze->size == ZIP64_MAGICVAL ||
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 2163
diff changeset
  1029
            locoff == ZIP64_MAGICVAL) {
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 2163
diff changeset
  1030
            jint off = 0;
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 2163
diff changeset
  1031
            while ((off + 4) < elen) {    // spec: HeaderID+DataSize+Data
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 2163
diff changeset
  1032
                jint sz = SH(extra, off + 2);
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 2163
diff changeset
  1033
                if (SH(extra, off) == ZIP64_EXTID) {
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 2163
diff changeset
  1034
                    off += 4;
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 2163
diff changeset
  1035
                    if (ze->size == ZIP64_MAGICVAL) {
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 2163
diff changeset
  1036
                        // if invalid zip64 extra fields, just skip
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 2163
diff changeset
  1037
                        if (sz < 8 || (off + 8) > elen)
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 2163
diff changeset
  1038
                            break;
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 2163
diff changeset
  1039
                        ze->size = LL(extra, off);
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 2163
diff changeset
  1040
                        sz -= 8;
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 2163
diff changeset
  1041
                        off += 8;
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 2163
diff changeset
  1042
                    }
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 2163
diff changeset
  1043
                    if (ze->csize == ZIP64_MAGICVAL) {
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 2163
diff changeset
  1044
                        if (sz < 8 || (off + 8) > elen)
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 2163
diff changeset
  1045
                            break;
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 2163
diff changeset
  1046
                        ze->csize = LL(extra, off);
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 2163
diff changeset
  1047
                        sz -= 8;
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 2163
diff changeset
  1048
                        off += 8;
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 2163
diff changeset
  1049
                    }
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 2163
diff changeset
  1050
                    if (locoff == ZIP64_MAGICVAL) {
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 2163
diff changeset
  1051
                        if (sz < 8 || (off + 8) > elen)
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 2163
diff changeset
  1052
                            break;
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 2163
diff changeset
  1053
                        ze->pos = -(zip->locpos +  LL(extra, off));
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 2163
diff changeset
  1054
                        sz -= 8;
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 2163
diff changeset
  1055
                        off += 8;
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 2163
diff changeset
  1056
                    }
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 2163
diff changeset
  1057
                    break;
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 2163
diff changeset
  1058
                }
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 2163
diff changeset
  1059
                off += (sz + 4);
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 2163
diff changeset
  1060
            }
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents: 2163
diff changeset
  1061
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
    if (clen > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
        /* This entry has a comment */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
        if ((ze->comment = malloc(clen + 1)) == NULL) goto Catch;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
        memcpy(ze->comment, cen + CENHDR + nlen + elen, clen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
        ze->comment[clen] = '\0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
    goto Finally;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
 Catch:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
    free(ze->name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
    free(ze->extra);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
    free(ze->comment);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
    free(ze);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
    ze = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
 Finally:
5148
2bf5469864ea 6929479: Add a system property sun.zip.disableMemoryMapping to disable mmap use in ZipFile
sherman
parents: 3078
diff changeset
  1080
#ifdef USE_MMAP
2bf5469864ea 6929479: Add a system property sun.zip.disableMemoryMapping to disable mmap use in ZipFile
sherman
parents: 3078
diff changeset
  1081
    if (!zip->usemmap)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
#endif
5148
2bf5469864ea 6929479: Add a system property sun.zip.disableMemoryMapping to disable mmap use in ZipFile
sherman
parents: 3078
diff changeset
  1083
        if (cen != NULL && accessHint == ACCESS_RANDOM) free(cen);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
    return ze;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
 * Free the given jzentry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
 * In fact we maintain a one-entry cache of the most recently used
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
 * jzentry for each zip.  This optimizes a common access pattern.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
void
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
ZIP_FreeEntry(jzfile *jz, jzentry *ze)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
    jzentry *last;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
    ZIP_Lock(jz);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
    last = jz->cache;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
    jz->cache = ze;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
    ZIP_Unlock(jz);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
    if (last != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
        /* Free the previously cached jzentry */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
        free(last->name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
        if (last->extra)   free(last->extra);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
        if (last->comment) free(last->comment);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
        free(last);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
 * Returns the zip entry corresponding to the specified name, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
 * NULL if not found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
jzentry *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
ZIP_GetEntry(jzfile *zip, char *name, jint ulen)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
{
27084
d7fbf9a294af 8048025: Ensure cache consistency
sherman
parents: 25859
diff changeset
  1117
    if (ulen == 0) {
d7fbf9a294af 8048025: Ensure cache consistency
sherman
parents: 25859
diff changeset
  1118
        return ZIP_GetEntry2(zip, name, strlen(name), JNI_FALSE);
d7fbf9a294af 8048025: Ensure cache consistency
sherman
parents: 25859
diff changeset
  1119
    }
d7fbf9a294af 8048025: Ensure cache consistency
sherman
parents: 25859
diff changeset
  1120
    return ZIP_GetEntry2(zip, name, ulen, JNI_TRUE);
d7fbf9a294af 8048025: Ensure cache consistency
sherman
parents: 25859
diff changeset
  1121
}
d7fbf9a294af 8048025: Ensure cache consistency
sherman
parents: 25859
diff changeset
  1122
d7fbf9a294af 8048025: Ensure cache consistency
sherman
parents: 25859
diff changeset
  1123
jboolean equals(char* name1, int len1, char* name2, int len2) {
d7fbf9a294af 8048025: Ensure cache consistency
sherman
parents: 25859
diff changeset
  1124
    if (len1 != len2) {
d7fbf9a294af 8048025: Ensure cache consistency
sherman
parents: 25859
diff changeset
  1125
        return JNI_FALSE;
d7fbf9a294af 8048025: Ensure cache consistency
sherman
parents: 25859
diff changeset
  1126
    }
d7fbf9a294af 8048025: Ensure cache consistency
sherman
parents: 25859
diff changeset
  1127
    while (len1-- > 0) {
d7fbf9a294af 8048025: Ensure cache consistency
sherman
parents: 25859
diff changeset
  1128
        if (*name1++ != *name2++) {
d7fbf9a294af 8048025: Ensure cache consistency
sherman
parents: 25859
diff changeset
  1129
            return JNI_FALSE;
d7fbf9a294af 8048025: Ensure cache consistency
sherman
parents: 25859
diff changeset
  1130
        }
d7fbf9a294af 8048025: Ensure cache consistency
sherman
parents: 25859
diff changeset
  1131
    }
d7fbf9a294af 8048025: Ensure cache consistency
sherman
parents: 25859
diff changeset
  1132
    return JNI_TRUE;
d7fbf9a294af 8048025: Ensure cache consistency
sherman
parents: 25859
diff changeset
  1133
}
d7fbf9a294af 8048025: Ensure cache consistency
sherman
parents: 25859
diff changeset
  1134
d7fbf9a294af 8048025: Ensure cache consistency
sherman
parents: 25859
diff changeset
  1135
/*
d7fbf9a294af 8048025: Ensure cache consistency
sherman
parents: 25859
diff changeset
  1136
 * Returns the zip entry corresponding to the specified name, or
d7fbf9a294af 8048025: Ensure cache consistency
sherman
parents: 25859
diff changeset
  1137
 * NULL if not found.
d7fbf9a294af 8048025: Ensure cache consistency
sherman
parents: 25859
diff changeset
  1138
 * This method supports embedded null character in "name", use ulen
d7fbf9a294af 8048025: Ensure cache consistency
sherman
parents: 25859
diff changeset
  1139
 * for the length of "name".
d7fbf9a294af 8048025: Ensure cache consistency
sherman
parents: 25859
diff changeset
  1140
 */
d7fbf9a294af 8048025: Ensure cache consistency
sherman
parents: 25859
diff changeset
  1141
jzentry *
d7fbf9a294af 8048025: Ensure cache consistency
sherman
parents: 25859
diff changeset
  1142
ZIP_GetEntry2(jzfile *zip, char *name, jint ulen, jboolean addSlash)
d7fbf9a294af 8048025: Ensure cache consistency
sherman
parents: 25859
diff changeset
  1143
{
d7fbf9a294af 8048025: Ensure cache consistency
sherman
parents: 25859
diff changeset
  1144
    unsigned int hsh = hashN(name, ulen);
1228
1515928f48cd 6440786: Cannot create a ZIP file containing zero entries
bristor
parents: 2
diff changeset
  1145
    jint idx;
1515928f48cd 6440786: Cannot create a ZIP file containing zero entries
bristor
parents: 2
diff changeset
  1146
    jzentry *ze = 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
    ZIP_Lock(zip);
1228
1515928f48cd 6440786: Cannot create a ZIP file containing zero entries
bristor
parents: 2
diff changeset
  1149
    if (zip->total == 0) {
1515928f48cd 6440786: Cannot create a ZIP file containing zero entries
bristor
parents: 2
diff changeset
  1150
        goto Finally;
1515928f48cd 6440786: Cannot create a ZIP file containing zero entries
bristor
parents: 2
diff changeset
  1151
    }
1515928f48cd 6440786: Cannot create a ZIP file containing zero entries
bristor
parents: 2
diff changeset
  1152
1515928f48cd 6440786: Cannot create a ZIP file containing zero entries
bristor
parents: 2
diff changeset
  1153
    idx = zip->table[hsh % zip->tablelen];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
     * This while loop is an optimization where a double lookup
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
     * for name and name+/ is being performed. The name char
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
     * array has enough room at the end to try again with a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
     * slash appended if the first table lookup does not succeed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
    while(1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
        /* Check the cached entry first */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
        ze = zip->cache;
27084
d7fbf9a294af 8048025: Ensure cache consistency
sherman
parents: 25859
diff changeset
  1165
        if (ze && equals(ze->name, ze->nlen, name, ulen)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
            /* Cache hit!  Remove and return the cached entry. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
            zip->cache = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
            ZIP_Unlock(zip);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
            return ze;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
        ze = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
         * Search down the target hash chain for a cell whose
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
         * 32 bit hash matches the hashed name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
        while (idx != ZIP_ENDCHAIN) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
            jzcell *zc = &zip->entries[idx];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
            if (zc->hash == hsh) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
                 * OK, we've found a ZIP entry whose 32 bit hashcode
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
                 * matches the name we're looking for.  Try to read
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
                 * its entry information from the CEN.  If the CEN
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
                 * name matches the name we're looking for, we're
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
                 * done.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
                 * If the names don't match (which should be very rare)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
                 * we keep searching.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
                ze = newEntry(zip, zc, ACCESS_RANDOM);
27084
d7fbf9a294af 8048025: Ensure cache consistency
sherman
parents: 25859
diff changeset
  1191
                if (ze && equals(ze->name, ze->nlen, name, ulen)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
                if (ze != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
                    /* We need to release the lock across the free call */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
                    ZIP_Unlock(zip);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
                    ZIP_FreeEntry(zip, ze);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
                    ZIP_Lock(zip);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
                ze = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
            idx = zc->next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
        /* Entry found, return it */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
        if (ze != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
27084
d7fbf9a294af 8048025: Ensure cache consistency
sherman
parents: 25859
diff changeset
  1210
        /* If no need to try appending slash, we are done */
d7fbf9a294af 8048025: Ensure cache consistency
sherman
parents: 25859
diff changeset
  1211
        if (!addSlash) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
        /* Slash is already there? */
30445
e1e28b9abbe5 8079841: Buffer underflow with empty zip entry names
jmanson
parents: 29389
diff changeset
  1216
        if (ulen > 0 && name[ulen - 1] == '/') {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
        /* Add slash and try once more */
27084
d7fbf9a294af 8048025: Ensure cache consistency
sherman
parents: 25859
diff changeset
  1221
        name[ulen++] = '/';
d7fbf9a294af 8048025: Ensure cache consistency
sherman
parents: 25859
diff changeset
  1222
        name[ulen] = '\0';
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
        hsh = hash_append(hsh, '/');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
        idx = zip->table[hsh % zip->tablelen];
27084
d7fbf9a294af 8048025: Ensure cache consistency
sherman
parents: 25859
diff changeset
  1225
        addSlash = JNI_FALSE;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
1228
1515928f48cd 6440786: Cannot create a ZIP file containing zero entries
bristor
parents: 2
diff changeset
  1228
Finally:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
    ZIP_Unlock(zip);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
    return ze;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
 * Returns the n'th (starting at zero) zip file entry, or NULL if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
 * specified index was out of range.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
jzentry * JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
ZIP_GetNextEntry(jzfile *zip, jint n)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
    jzentry *result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
    if (n < 0 || n >= zip->total) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
    ZIP_Lock(zip);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
    result = newEntry(zip, &zip->entries[n], ACCESS_SEQUENTIAL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
    ZIP_Unlock(zip);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
    return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
 * Locks the specified zip file for reading.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
void
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
ZIP_Lock(jzfile *zip)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
    MLOCK(zip->lock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
 * Unlocks the specified zip file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
void
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
ZIP_Unlock(jzfile *zip)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
    MUNLOCK(zip->lock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
 * Returns the offset of the entry data within the zip file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
 * Returns -1 if an error occurred, in which case zip->msg will
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
 * contain the error text.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
jlong
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
ZIP_GetEntryDataOffset(jzfile *zip, jzentry *entry)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
    /* The Zip file spec explicitly allows the LOC extra data size to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
     * be different from the CEN extra data size, although the JDK
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
     * never creates such zip files.  Since we cannot trust the CEN
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
     * extra data size, we need to read the LOC to determine the entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
     * data offset.  We do this lazily to avoid touching the virtual
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
     * memory page containing the LOC when initializing jzentry
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
     * objects.  (This speeds up javac by a factor of 10 when the JDK
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
     * is installed on a very slow filesystem.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
    if (entry->pos <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
        unsigned char loc[LOCHDR];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
        if (readFullyAt(zip->zfd, loc, LOCHDR, -(entry->pos)) == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
            zip->msg = "error reading zip file";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
        }
29389
348a32f79d79 8074579: Use more efficient and readable way of checking PKZIP signatures
martin
parents: 27565
diff changeset
  1291
        if (!LOCSIG_AT(loc)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
            zip->msg = "invalid LOC header (bad signature)";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
        entry->pos = (- entry->pos) + LOCHDR + LOCNAM(loc) + LOCEXT(loc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
    return entry->pos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
 * Reads bytes from the specified zip entry. Assumes that the zip
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
 * file had been previously locked with ZIP_Lock(). Returns the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
 * number of bytes read, or -1 if an error occurred. If zip->msg != 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
 * then a zip error occurred and zip->msg contains the error text.
7281
f202e1a7a134 6989471: compiler warnings building java/zip native code
sherman
parents: 5506
diff changeset
  1305
 *
f202e1a7a134 6989471: compiler warnings building java/zip native code
sherman
parents: 5506
diff changeset
  1306
 * The current implementation does not support reading an entry that
f202e1a7a134 6989471: compiler warnings building java/zip native code
sherman
parents: 5506
diff changeset
  1307
 * has the size bigger than 2**32 bytes in ONE invocation.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
jint
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
ZIP_Read(jzfile *zip, jzentry *entry, jlong pos, void *buf, jint len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
{
32993
fa6bbf6ed92b 6907252: ZipFileInputStream Not Thread-Safe
coffeys
parents: 32846
diff changeset
  1312
    jlong entry_size;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
    jlong start;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
32993
fa6bbf6ed92b 6907252: ZipFileInputStream Not Thread-Safe
coffeys
parents: 32846
diff changeset
  1315
    if (zip == 0) {
fa6bbf6ed92b 6907252: ZipFileInputStream Not Thread-Safe
coffeys
parents: 32846
diff changeset
  1316
        return -1;
fa6bbf6ed92b 6907252: ZipFileInputStream Not Thread-Safe
coffeys
parents: 32846
diff changeset
  1317
    }
fa6bbf6ed92b 6907252: ZipFileInputStream Not Thread-Safe
coffeys
parents: 32846
diff changeset
  1318
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
    /* Clear previous zip error */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
    zip->msg = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
32993
fa6bbf6ed92b 6907252: ZipFileInputStream Not Thread-Safe
coffeys
parents: 32846
diff changeset
  1322
    if (entry == 0) {
fa6bbf6ed92b 6907252: ZipFileInputStream Not Thread-Safe
coffeys
parents: 32846
diff changeset
  1323
        zip->msg = "ZIP_Read: jzentry is NULL";
fa6bbf6ed92b 6907252: ZipFileInputStream Not Thread-Safe
coffeys
parents: 32846
diff changeset
  1324
        return -1;
fa6bbf6ed92b 6907252: ZipFileInputStream Not Thread-Safe
coffeys
parents: 32846
diff changeset
  1325
    }
fa6bbf6ed92b 6907252: ZipFileInputStream Not Thread-Safe
coffeys
parents: 32846
diff changeset
  1326
fa6bbf6ed92b 6907252: ZipFileInputStream Not Thread-Safe
coffeys
parents: 32846
diff changeset
  1327
    entry_size = (entry->csize != 0) ? entry->csize : entry->size;
fa6bbf6ed92b 6907252: ZipFileInputStream Not Thread-Safe
coffeys
parents: 32846
diff changeset
  1328
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
    /* Check specified position */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
    if (pos < 0 || pos > entry_size - 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
        zip->msg = "ZIP_Read: specified offset out of range";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
    /* Check specified length */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
    if (len <= 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
    if (len > entry_size - pos)
7281
f202e1a7a134 6989471: compiler warnings building java/zip native code
sherman
parents: 5506
diff changeset
  1339
        len = (jint)(entry_size - pos);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
    /* Get file offset to start reading data */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
    start = ZIP_GetEntryDataOffset(zip, entry);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
    if (start < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
    start += pos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
    if (start + len > zip->len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
        zip->msg = "ZIP_Read: corrupt zip file: invalid entry size";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
    if (readFullyAt(zip->zfd, buf, len, start) == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
        zip->msg = "ZIP_Read: error reading zip file";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
    return len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
/* The maximum size of a stack-allocated buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
#define BUF_SIZE 4096
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
 * This function is used by the runtime system to load compressed entries
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
 * from ZIP/JAR files specified in the class path. It is defined here
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
 * so that it can be dynamically loaded by the runtime if the zip library
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
 * is found.
7281
f202e1a7a134 6989471: compiler warnings building java/zip native code
sherman
parents: 5506
diff changeset
  1369
 *
f202e1a7a134 6989471: compiler warnings building java/zip native code
sherman
parents: 5506
diff changeset
  1370
 * The current implementation does not support reading an entry that
f202e1a7a134 6989471: compiler warnings building java/zip native code
sherman
parents: 5506
diff changeset
  1371
 * has the size bigger than 2**32 bytes in ONE invocation.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
jboolean
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
InflateFully(jzfile *zip, jzentry *entry, void *buf, char **msg)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
    z_stream strm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
    char tmp[BUF_SIZE];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
    jlong pos = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
    jlong count = entry->csize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
    *msg = 0; /* Reset error message */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
    if (count == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
        *msg = "inflateFully: entry not compressed";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
        return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
    memset(&strm, 0, sizeof(z_stream));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
    if (inflateInit2(&strm, -MAX_WBITS) != Z_OK) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
        *msg = strm.msg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
        return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
    strm.next_out = buf;
7281
f202e1a7a134 6989471: compiler warnings building java/zip native code
sherman
parents: 5506
diff changeset
  1395
    strm.avail_out = (uInt)entry->size;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
    while (count > 0) {
7281
f202e1a7a134 6989471: compiler warnings building java/zip native code
sherman
parents: 5506
diff changeset
  1398
        jint n = count > (jlong)sizeof(tmp) ? (jint)sizeof(tmp) : (jint)count;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
        ZIP_Lock(zip);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
        n = ZIP_Read(zip, entry, pos, tmp, n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
        ZIP_Unlock(zip);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
        if (n <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
            if (n == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
                *msg = "inflateFully: Unexpected end of file";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1406
            inflateEnd(&strm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1407
            return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1408
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
        pos += n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1410
        count -= n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
        strm.next_in = (Bytef *)tmp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
        strm.avail_in = n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1413
        do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1414
            switch (inflate(&strm, Z_PARTIAL_FLUSH)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
            case Z_OK:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
            case Z_STREAM_END:
36649
89305180130b 8152352: Compiling warnings in zip_util.c blocks devkit to build with --with-zlib=system
sherman
parents: 32993
diff changeset
  1418
                if (count != 0 || strm.total_out != (uInt)entry->size) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
                    *msg = "inflateFully: Unexpected end of stream";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
                    inflateEnd(&strm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1421
                    return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1425
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1427
        } while (strm.avail_in > 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1428
    }
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 27186
diff changeset
  1429
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1430
    inflateEnd(&strm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1431
    return JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1432
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1433
7281
f202e1a7a134 6989471: compiler warnings building java/zip native code
sherman
parents: 5506
diff changeset
  1434
/*
f202e1a7a134 6989471: compiler warnings building java/zip native code
sherman
parents: 5506
diff changeset
  1435
 * The current implementation does not support reading an entry that
f202e1a7a134 6989471: compiler warnings building java/zip native code
sherman
parents: 5506
diff changeset
  1436
 * has the size bigger than 2**32 bytes in ONE invocation.
f202e1a7a134 6989471: compiler warnings building java/zip native code
sherman
parents: 5506
diff changeset
  1437
 */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1438
jzentry * JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1439
ZIP_FindEntry(jzfile *zip, char *name, jint *sizeP, jint *nameLenP)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1440
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1441
    jzentry *entry = ZIP_GetEntry(zip, name, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1442
    if (entry) {
7281
f202e1a7a134 6989471: compiler warnings building java/zip native code
sherman
parents: 5506
diff changeset
  1443
        *sizeP = (jint)entry->size;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1444
        *nameLenP = strlen(entry->name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1445
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1446
    return entry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1447
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1448
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1449
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1450
 * Reads a zip file entry into the specified byte array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1451
 * When the method completes, it releases the jzentry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1452
 * Note: this is called from the separately delivered VM (hotspot/classic)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1453
 * so we have to be careful to maintain the expected behaviour.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1454
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1455
jboolean JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1456
ZIP_ReadEntry(jzfile *zip, jzentry *entry, unsigned char *buf, char *entryname)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1457
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1458
    char *msg;
32846
5383225ebd0d 8133249: Occasional SIGSEGV: non thread-safe use of strerr in getLastErrorString
robm
parents: 30445
diff changeset
  1459
    char tmpbuf[1024];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1460
32993
fa6bbf6ed92b 6907252: ZipFileInputStream Not Thread-Safe
coffeys
parents: 32846
diff changeset
  1461
    if (entry == 0) {
fa6bbf6ed92b 6907252: ZipFileInputStream Not Thread-Safe
coffeys
parents: 32846
diff changeset
  1462
        jio_fprintf(stderr, "jzentry was invalid");
fa6bbf6ed92b 6907252: ZipFileInputStream Not Thread-Safe
coffeys
parents: 32846
diff changeset
  1463
        return JNI_FALSE;
fa6bbf6ed92b 6907252: ZipFileInputStream Not Thread-Safe
coffeys
parents: 32846
diff changeset
  1464
    }
fa6bbf6ed92b 6907252: ZipFileInputStream Not Thread-Safe
coffeys
parents: 32846
diff changeset
  1465
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1466
    strcpy(entryname, entry->name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1467
    if (entry->csize == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1468
        /* Entry is stored */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1469
        jlong pos = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1470
        jlong size = entry->size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1471
        while (pos < size) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1472
            jint n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1473
            jlong limit = ((((jlong) 1) << 31) - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1474
            jint count = (size - pos < limit) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1475
                /* These casts suppress a VC++ Internal Compiler Error */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1476
                (jint) (size - pos) :
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1477
                (jint) limit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1478
            ZIP_Lock(zip);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1479
            n = ZIP_Read(zip, entry, pos, buf, count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1480
            msg = zip->msg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1481
            ZIP_Unlock(zip);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1482
            if (n == -1) {
32846
5383225ebd0d 8133249: Occasional SIGSEGV: non thread-safe use of strerr in getLastErrorString
robm
parents: 30445
diff changeset
  1483
                if (msg == 0) {
5383225ebd0d 8133249: Occasional SIGSEGV: non thread-safe use of strerr in getLastErrorString
robm
parents: 30445
diff changeset
  1484
                    getErrorString(errno, tmpbuf, sizeof(tmpbuf));
5383225ebd0d 8133249: Occasional SIGSEGV: non thread-safe use of strerr in getLastErrorString
robm
parents: 30445
diff changeset
  1485
                    msg = tmpbuf;
5383225ebd0d 8133249: Occasional SIGSEGV: non thread-safe use of strerr in getLastErrorString
robm
parents: 30445
diff changeset
  1486
                }
5383225ebd0d 8133249: Occasional SIGSEGV: non thread-safe use of strerr in getLastErrorString
robm
parents: 30445
diff changeset
  1487
                jio_fprintf(stderr, "%s: %s\n", zip->name, msg);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1488
                return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1489
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1490
            buf += n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1491
            pos += n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1492
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1493
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1494
        /* Entry is compressed */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1495
        int ok = InflateFully(zip, entry, buf, &msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1496
        if (!ok) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1497
            if ((msg == NULL) || (*msg == 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1498
                msg = zip->msg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1499
            }
32846
5383225ebd0d 8133249: Occasional SIGSEGV: non thread-safe use of strerr in getLastErrorString
robm
parents: 30445
diff changeset
  1500
            if (msg == 0) {
5383225ebd0d 8133249: Occasional SIGSEGV: non thread-safe use of strerr in getLastErrorString
robm
parents: 30445
diff changeset
  1501
                getErrorString(errno, tmpbuf, sizeof(tmpbuf));
5383225ebd0d 8133249: Occasional SIGSEGV: non thread-safe use of strerr in getLastErrorString
robm
parents: 30445
diff changeset
  1502
                msg = tmpbuf;
5383225ebd0d 8133249: Occasional SIGSEGV: non thread-safe use of strerr in getLastErrorString
robm
parents: 30445
diff changeset
  1503
            }
5383225ebd0d 8133249: Occasional SIGSEGV: non thread-safe use of strerr in getLastErrorString
robm
parents: 30445
diff changeset
  1504
            jio_fprintf(stderr, "%s: %s\n", zip->name, msg);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1505
            return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1506
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1507
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1508
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1509
    ZIP_FreeEntry(zip, entry);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1510
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1511
    return JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1512
}
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 27186
diff changeset
  1513
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 27186
diff changeset
  1514
jboolean JNICALL
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 27186
diff changeset
  1515
ZIP_InflateFully(void *inBuf, jlong inLen, void *outBuf, jlong outLen, char **pmsg)
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 27186
diff changeset
  1516
{
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 27186
diff changeset
  1517
    z_stream strm;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 27186
diff changeset
  1518
    int i = 0;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 27186
diff changeset
  1519
    memset(&strm, 0, sizeof(z_stream));
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 27186
diff changeset
  1520
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 27186
diff changeset
  1521
    *pmsg = 0; /* Reset error message */
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 27186
diff changeset
  1522
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 27186
diff changeset
  1523
    if (inflateInit2(&strm, MAX_WBITS) != Z_OK) {
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 27186
diff changeset
  1524
        *pmsg = strm.msg;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 27186
diff changeset
  1525
        return JNI_FALSE;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 27186
diff changeset
  1526
    }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 27186
diff changeset
  1527
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 27186
diff changeset
  1528
    strm.next_out = (Bytef *) outBuf;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 27186
diff changeset
  1529
    strm.avail_out = (uInt)outLen;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 27186
diff changeset
  1530
    strm.next_in = (Bytef *) inBuf;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 27186
diff changeset
  1531
    strm.avail_in = (uInt)inLen;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 27186
diff changeset
  1532
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 27186
diff changeset
  1533
    do {
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 27186
diff changeset
  1534
        switch (inflate(&strm, Z_PARTIAL_FLUSH)) {
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 27186
diff changeset
  1535
            case Z_OK:
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 27186
diff changeset
  1536
                break;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 27186
diff changeset
  1537
            case Z_STREAM_END:
36649
89305180130b 8152352: Compiling warnings in zip_util.c blocks devkit to build with --with-zlib=system
sherman
parents: 32993
diff changeset
  1538
                if (strm.total_out != (uInt)outLen) {
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 27186
diff changeset
  1539
                    *pmsg = "INFLATER_inflateFully: Unexpected end of stream";
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 27186
diff changeset
  1540
                    inflateEnd(&strm);
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 27186
diff changeset
  1541
                    return JNI_FALSE;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 27186
diff changeset
  1542
                }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 27186
diff changeset
  1543
                break;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 27186
diff changeset
  1544
            case Z_DATA_ERROR:
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 27186
diff changeset
  1545
                *pmsg = "INFLATER_inflateFully: Compressed data corrupted";
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 27186
diff changeset
  1546
                inflateEnd(&strm);
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 27186
diff changeset
  1547
                return JNI_FALSE;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 27186
diff changeset
  1548
            case Z_MEM_ERROR:
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 27186
diff changeset
  1549
                *pmsg = "INFLATER_inflateFully: out of memory";
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 27186
diff changeset
  1550
                inflateEnd(&strm);
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 27186
diff changeset
  1551
                return JNI_FALSE;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 27186
diff changeset
  1552
            default:
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 27186
diff changeset
  1553
                *pmsg = "INFLATER_inflateFully: internal error";
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 27186
diff changeset
  1554
                inflateEnd(&strm);
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 27186
diff changeset
  1555
                return JNI_FALSE;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 27186
diff changeset
  1556
        }
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 27186
diff changeset
  1557
    } while (strm.avail_in > 0);
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 27186
diff changeset
  1558
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 27186
diff changeset
  1559
    inflateEnd(&strm);
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 27186
diff changeset
  1560
    return JNI_TRUE;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 27186
diff changeset
  1561
}