jdk/src/share/bin/parse_manifest.c
author goetz
Thu, 21 Nov 2013 18:29:34 -0800
changeset 22852 1063026e8cee
parent 16115 dd60b90b6d20
child 22962 309eaab8c853
permissions -rw-r--r--
8028471: PPC64 (part 215): opto: Extend ImplicitNullCheck optimization. Summary: Fixed Implicit NULL check optimization for AIX, where the page at address '0' is only write-protected. Reviewed-by: kvn
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
15668
c5d8a90a4b40 8005750: [parfait] Memory leak at jdk/src/share/bin/parse_manifest.c
ksrini
parents: 15644
diff changeset
     2
 * Copyright (c) 2003, 2013, 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: 3848
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: 3848
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: 3848
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3848
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3848
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
#include <sys/types.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
#include <sys/stat.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
#include <fcntl.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
#include <stdio.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
#include <stdlib.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
#include <string.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
#include "jli_util.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
#include <zlib.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
#include "manifest_info.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
static char     *manifest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
static const char       *manifest_name = "META-INF/MANIFEST.MF";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * Inflate the manifest file (or any file for that matter).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *   fd:        File descriptor of the jar file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *   entry:     Contains the information necessary to perform the inflation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *              (the compressed and uncompressed sizes and the offset in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *              the file where the compressed data is located).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *   size_out:  Returns the size of the inflated file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * Upon success, it returns a pointer to a NUL-terminated malloc'd buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * containing the inflated manifest file.  When the caller is done with it,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * this buffer should be released by a call to free().  Upon failure,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * returns NULL.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
static char *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
inflate_file(int fd, zentry *entry, int *size_out)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    char        *in;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    char        *out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    z_stream    zs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
3848
2c2395fb6d85 6842838: 64-bit failure in handling invalid manifest in launcher.
kevinw
parents: 2
diff changeset
    62
    if (entry->csize == (size_t) -1 || entry->isize == (size_t) -1 )
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        return (NULL);
13675
c2999ee84468 7194005: (launcher) needs to be enhanced for 64-bit jar file handling
ksrini
parents: 7668
diff changeset
    64
    if (JLI_Lseek(fd, entry->offset, SEEK_SET) < (jlong)0)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        return (NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    if ((in = malloc(entry->csize + 1)) == NULL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        return (NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    if ((size_t)(read(fd, in, (unsigned int)entry->csize)) != entry->csize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        free(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        return (NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    if (entry->how == STORED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        *(char *)((size_t)in + entry->csize) = '\0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        if (size_out) {
7028
adadd244f506 6989469: (launcher) compiler warnings in jli native code
ksrini
parents: 5506
diff changeset
    75
            *size_out = (int)entry->csize;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        return (in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    } else if (entry->how == DEFLATED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        zs.zalloc = (alloc_func)Z_NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        zs.zfree = (free_func)Z_NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        zs.opaque = (voidpf)Z_NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        zs.next_in = (Byte*)in;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        zs.avail_in = (uInt)entry->csize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        if (inflateInit2(&zs, -MAX_WBITS) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            free(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            return (NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        if ((out = malloc(entry->isize + 1)) == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            free(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            return (NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        zs.next_out = (Byte*)out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        zs.avail_out = (uInt)entry->isize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        if (inflate(&zs, Z_PARTIAL_FLUSH) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            free(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            free(out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            return (NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        *(char *)((size_t)out + entry->isize) = '\0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        free(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        if (inflateEnd(&zs) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            free(out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            return (NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        if (size_out) {
7028
adadd244f506 6989469: (launcher) compiler warnings in jli native code
ksrini
parents: 5506
diff changeset
   106
            *size_out = (int)entry->isize;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        return (out);
15668
c5d8a90a4b40 8005750: [parfait] Memory leak at jdk/src/share/bin/parse_manifest.c
ksrini
parents: 15644
diff changeset
   109
    }
c5d8a90a4b40 8005750: [parfait] Memory leak at jdk/src/share/bin/parse_manifest.c
ksrini
parents: 15644
diff changeset
   110
    free(in);
c5d8a90a4b40 8005750: [parfait] Memory leak at jdk/src/share/bin/parse_manifest.c
ksrini
parents: 15644
diff changeset
   111
    return (NULL);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
13675
c2999ee84468 7194005: (launcher) needs to be enhanced for 64-bit jar file handling
ksrini
parents: 7668
diff changeset
   114
static jboolean zip64_present = JNI_FALSE;
c2999ee84468 7194005: (launcher) needs to be enhanced for 64-bit jar file handling
ksrini
parents: 7668
diff changeset
   115
c2999ee84468 7194005: (launcher) needs to be enhanced for 64-bit jar file handling
ksrini
parents: 7668
diff changeset
   116
/*
c2999ee84468 7194005: (launcher) needs to be enhanced for 64-bit jar file handling
ksrini
parents: 7668
diff changeset
   117
 * Checks to see if we have ZIP64 archive, and save
c2999ee84468 7194005: (launcher) needs to be enhanced for 64-bit jar file handling
ksrini
parents: 7668
diff changeset
   118
 * the check for later use
c2999ee84468 7194005: (launcher) needs to be enhanced for 64-bit jar file handling
ksrini
parents: 7668
diff changeset
   119
 */
c2999ee84468 7194005: (launcher) needs to be enhanced for 64-bit jar file handling
ksrini
parents: 7668
diff changeset
   120
static int
c2999ee84468 7194005: (launcher) needs to be enhanced for 64-bit jar file handling
ksrini
parents: 7668
diff changeset
   121
haveZIP64(Byte *p) {
c2999ee84468 7194005: (launcher) needs to be enhanced for 64-bit jar file handling
ksrini
parents: 7668
diff changeset
   122
    jlong cenlen, cenoff, centot;
c2999ee84468 7194005: (launcher) needs to be enhanced for 64-bit jar file handling
ksrini
parents: 7668
diff changeset
   123
    cenlen = ENDSIZ(p);
c2999ee84468 7194005: (launcher) needs to be enhanced for 64-bit jar file handling
ksrini
parents: 7668
diff changeset
   124
    cenoff = ENDOFF(p);
c2999ee84468 7194005: (launcher) needs to be enhanced for 64-bit jar file handling
ksrini
parents: 7668
diff changeset
   125
    centot = ENDTOT(p);
c2999ee84468 7194005: (launcher) needs to be enhanced for 64-bit jar file handling
ksrini
parents: 7668
diff changeset
   126
    zip64_present = (cenlen == ZIP64_MAGICVAL ||
c2999ee84468 7194005: (launcher) needs to be enhanced for 64-bit jar file handling
ksrini
parents: 7668
diff changeset
   127
                     cenoff == ZIP64_MAGICVAL ||
c2999ee84468 7194005: (launcher) needs to be enhanced for 64-bit jar file handling
ksrini
parents: 7668
diff changeset
   128
                     centot == ZIP64_MAGICCOUNT);
c2999ee84468 7194005: (launcher) needs to be enhanced for 64-bit jar file handling
ksrini
parents: 7668
diff changeset
   129
    return zip64_present;
c2999ee84468 7194005: (launcher) needs to be enhanced for 64-bit jar file handling
ksrini
parents: 7668
diff changeset
   130
}
c2999ee84468 7194005: (launcher) needs to be enhanced for 64-bit jar file handling
ksrini
parents: 7668
diff changeset
   131
c2999ee84468 7194005: (launcher) needs to be enhanced for 64-bit jar file handling
ksrini
parents: 7668
diff changeset
   132
static jlong
c2999ee84468 7194005: (launcher) needs to be enhanced for 64-bit jar file handling
ksrini
parents: 7668
diff changeset
   133
find_end64(int fd, Byte *ep, jlong pos)
c2999ee84468 7194005: (launcher) needs to be enhanced for 64-bit jar file handling
ksrini
parents: 7668
diff changeset
   134
{
c2999ee84468 7194005: (launcher) needs to be enhanced for 64-bit jar file handling
ksrini
parents: 7668
diff changeset
   135
    jlong end64pos;
c2999ee84468 7194005: (launcher) needs to be enhanced for 64-bit jar file handling
ksrini
parents: 7668
diff changeset
   136
    jlong bytes;
c2999ee84468 7194005: (launcher) needs to be enhanced for 64-bit jar file handling
ksrini
parents: 7668
diff changeset
   137
    if ((end64pos = JLI_Lseek(fd, pos - ZIP64_LOCHDR, SEEK_SET)) < (jlong)0)
c2999ee84468 7194005: (launcher) needs to be enhanced for 64-bit jar file handling
ksrini
parents: 7668
diff changeset
   138
        return -1;
c2999ee84468 7194005: (launcher) needs to be enhanced for 64-bit jar file handling
ksrini
parents: 7668
diff changeset
   139
    if ((bytes = read(fd, ep, ZIP64_LOCHDR)) < 0)
c2999ee84468 7194005: (launcher) needs to be enhanced for 64-bit jar file handling
ksrini
parents: 7668
diff changeset
   140
        return -1;
c2999ee84468 7194005: (launcher) needs to be enhanced for 64-bit jar file handling
ksrini
parents: 7668
diff changeset
   141
    if (GETSIG(ep) == ZIP64_LOCSIG)
c2999ee84468 7194005: (launcher) needs to be enhanced for 64-bit jar file handling
ksrini
parents: 7668
diff changeset
   142
       return end64pos;
c2999ee84468 7194005: (launcher) needs to be enhanced for 64-bit jar file handling
ksrini
parents: 7668
diff changeset
   143
    return -1;
c2999ee84468 7194005: (launcher) needs to be enhanced for 64-bit jar file handling
ksrini
parents: 7668
diff changeset
   144
}
c2999ee84468 7194005: (launcher) needs to be enhanced for 64-bit jar file handling
ksrini
parents: 7668
diff changeset
   145
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
 * A very little used routine to handle the case that zip file has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
 * a comment at the end. Believe it or not, the only way to find the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
 * END record is to walk backwards, byte by bloody byte looking for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
 * the END record signature.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
 *      fd:     File descriptor of the jar file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
 *      eb:     Pointer to a buffer to receive a copy of the END header.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
 * Returns the offset of the END record in the file on success,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
 * -1 on failure.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
 */
13675
c2999ee84468 7194005: (launcher) needs to be enhanced for 64-bit jar file handling
ksrini
parents: 7668
diff changeset
   158
static jlong
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
find_end(int fd, Byte *eb)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
{
13675
c2999ee84468 7194005: (launcher) needs to be enhanced for 64-bit jar file handling
ksrini
parents: 7668
diff changeset
   161
    jlong   len;
c2999ee84468 7194005: (launcher) needs to be enhanced for 64-bit jar file handling
ksrini
parents: 7668
diff changeset
   162
    jlong   pos;
c2999ee84468 7194005: (launcher) needs to be enhanced for 64-bit jar file handling
ksrini
parents: 7668
diff changeset
   163
    jlong   flen;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    int     bytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    Byte    *cp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    Byte    *endpos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    Byte    *buffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * 99.44% (or more) of the time, there will be no comment at the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * end of the zip file.  Try reading just enough to read the END
13675
c2999ee84468 7194005: (launcher) needs to be enhanced for 64-bit jar file handling
ksrini
parents: 7668
diff changeset
   172
     * record from the end of the file, at this time we should also
c2999ee84468 7194005: (launcher) needs to be enhanced for 64-bit jar file handling
ksrini
parents: 7668
diff changeset
   173
     * check to see if we have a ZIP64 archive.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     */
13675
c2999ee84468 7194005: (launcher) needs to be enhanced for 64-bit jar file handling
ksrini
parents: 7668
diff changeset
   175
    if ((pos = JLI_Lseek(fd, -ENDHDR, SEEK_END)) < (jlong)0)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        return (-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    if ((bytes = read(fd, eb, ENDHDR)) < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        return (-1);
13675
c2999ee84468 7194005: (launcher) needs to be enhanced for 64-bit jar file handling
ksrini
parents: 7668
diff changeset
   179
    if (GETSIG(eb) == ENDSIG) {
c2999ee84468 7194005: (launcher) needs to be enhanced for 64-bit jar file handling
ksrini
parents: 7668
diff changeset
   180
        return haveZIP64(eb) ? find_end64(fd, eb, pos) : pos;
c2999ee84468 7194005: (launcher) needs to be enhanced for 64-bit jar file handling
ksrini
parents: 7668
diff changeset
   181
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * Shucky-Darn,... There is a comment at the end of the zip file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * Allocate and fill a buffer with enough of the zip file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * to meet the specification for a maximal comment length.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     */
13675
c2999ee84468 7194005: (launcher) needs to be enhanced for 64-bit jar file handling
ksrini
parents: 7668
diff changeset
   189
    if ((flen = JLI_Lseek(fd, 0, SEEK_END)) < (jlong)0)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        return (-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    len = (flen < END_MAXLEN) ? flen : END_MAXLEN;
13675
c2999ee84468 7194005: (launcher) needs to be enhanced for 64-bit jar file handling
ksrini
parents: 7668
diff changeset
   192
    if (JLI_Lseek(fd, -len, SEEK_END) < (jlong)0)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        return (-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    if ((buffer = malloc(END_MAXLEN)) == NULL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        return (-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    if ((bytes = read(fd, buffer, len)) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        free(buffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        return (-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * Search backwards from the end of file stopping when the END header
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * signature is found. (The first condition of the "if" is just a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * fast fail, because the GETSIG macro isn't always cheap.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * final condition protects against false positives.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    endpos = &buffer[bytes];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    for (cp = &buffer[bytes - ENDHDR]; cp >= &buffer[0]; cp--)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        if ((*cp == (ENDSIG & 0xFF)) && (GETSIG(cp) == ENDSIG) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
          (cp + ENDHDR + ENDCOM(cp) == endpos)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            (void) memcpy(eb, cp, ENDHDR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            free(buffer);
13675
c2999ee84468 7194005: (launcher) needs to be enhanced for 64-bit jar file handling
ksrini
parents: 7668
diff changeset
   213
            pos = flen - (endpos - cp);
c2999ee84468 7194005: (launcher) needs to be enhanced for 64-bit jar file handling
ksrini
parents: 7668
diff changeset
   214
            return haveZIP64(eb) ? find_end64(fd, eb, pos) : pos;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    free(buffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    return (-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
13675
c2999ee84468 7194005: (launcher) needs to be enhanced for 64-bit jar file handling
ksrini
parents: 7668
diff changeset
   220
#define BUFSIZE (3 * 65536 + CENHDR + SIGSIZ)
c2999ee84468 7194005: (launcher) needs to be enhanced for 64-bit jar file handling
ksrini
parents: 7668
diff changeset
   221
#define MINREAD 1024
c2999ee84468 7194005: (launcher) needs to be enhanced for 64-bit jar file handling
ksrini
parents: 7668
diff changeset
   222
c2999ee84468 7194005: (launcher) needs to be enhanced for 64-bit jar file handling
ksrini
parents: 7668
diff changeset
   223
/*
c2999ee84468 7194005: (launcher) needs to be enhanced for 64-bit jar file handling
ksrini
parents: 7668
diff changeset
   224
 * Computes and positions at the start of the CEN header, ie. the central
c2999ee84468 7194005: (launcher) needs to be enhanced for 64-bit jar file handling
ksrini
parents: 7668
diff changeset
   225
 * directory, this will also return the offset if there is a zip file comment
c2999ee84468 7194005: (launcher) needs to be enhanced for 64-bit jar file handling
ksrini
parents: 7668
diff changeset
   226
 * at the end of the archive, for most cases this would be 0.
c2999ee84468 7194005: (launcher) needs to be enhanced for 64-bit jar file handling
ksrini
parents: 7668
diff changeset
   227
 */
c2999ee84468 7194005: (launcher) needs to be enhanced for 64-bit jar file handling
ksrini
parents: 7668
diff changeset
   228
static jlong
c2999ee84468 7194005: (launcher) needs to be enhanced for 64-bit jar file handling
ksrini
parents: 7668
diff changeset
   229
compute_cen(int fd, Byte *bp)
c2999ee84468 7194005: (launcher) needs to be enhanced for 64-bit jar file handling
ksrini
parents: 7668
diff changeset
   230
{
c2999ee84468 7194005: (launcher) needs to be enhanced for 64-bit jar file handling
ksrini
parents: 7668
diff changeset
   231
    int bytes;
c2999ee84468 7194005: (launcher) needs to be enhanced for 64-bit jar file handling
ksrini
parents: 7668
diff changeset
   232
    Byte *p;
c2999ee84468 7194005: (launcher) needs to be enhanced for 64-bit jar file handling
ksrini
parents: 7668
diff changeset
   233
    jlong base_offset;
c2999ee84468 7194005: (launcher) needs to be enhanced for 64-bit jar file handling
ksrini
parents: 7668
diff changeset
   234
    jlong offset;
c2999ee84468 7194005: (launcher) needs to be enhanced for 64-bit jar file handling
ksrini
parents: 7668
diff changeset
   235
    char buffer[MINREAD];
c2999ee84468 7194005: (launcher) needs to be enhanced for 64-bit jar file handling
ksrini
parents: 7668
diff changeset
   236
    p = buffer;
c2999ee84468 7194005: (launcher) needs to be enhanced for 64-bit jar file handling
ksrini
parents: 7668
diff changeset
   237
    /*
c2999ee84468 7194005: (launcher) needs to be enhanced for 64-bit jar file handling
ksrini
parents: 7668
diff changeset
   238
     * Read the END Header, which is the starting point for ZIP files.
c2999ee84468 7194005: (launcher) needs to be enhanced for 64-bit jar file handling
ksrini
parents: 7668
diff changeset
   239
     * (Clearly designed to make writing a zip file easier than reading
c2999ee84468 7194005: (launcher) needs to be enhanced for 64-bit jar file handling
ksrini
parents: 7668
diff changeset
   240
     * one. Now isn't that precious...)
c2999ee84468 7194005: (launcher) needs to be enhanced for 64-bit jar file handling
ksrini
parents: 7668
diff changeset
   241
     */
c2999ee84468 7194005: (launcher) needs to be enhanced for 64-bit jar file handling
ksrini
parents: 7668
diff changeset
   242
    if ((base_offset = find_end(fd, bp)) == -1) {
c2999ee84468 7194005: (launcher) needs to be enhanced for 64-bit jar file handling
ksrini
parents: 7668
diff changeset
   243
        return (-1);
c2999ee84468 7194005: (launcher) needs to be enhanced for 64-bit jar file handling
ksrini
parents: 7668
diff changeset
   244
    }
c2999ee84468 7194005: (launcher) needs to be enhanced for 64-bit jar file handling
ksrini
parents: 7668
diff changeset
   245
    p = bp;
c2999ee84468 7194005: (launcher) needs to be enhanced for 64-bit jar file handling
ksrini
parents: 7668
diff changeset
   246
    /*
c2999ee84468 7194005: (launcher) needs to be enhanced for 64-bit jar file handling
ksrini
parents: 7668
diff changeset
   247
     * There is a historical, but undocumented, ability to allow for
c2999ee84468 7194005: (launcher) needs to be enhanced for 64-bit jar file handling
ksrini
parents: 7668
diff changeset
   248
     * additional "stuff" to be prepended to the zip/jar file. It seems
c2999ee84468 7194005: (launcher) needs to be enhanced for 64-bit jar file handling
ksrini
parents: 7668
diff changeset
   249
     * that this has been used to prepend an actual java launcher
c2999ee84468 7194005: (launcher) needs to be enhanced for 64-bit jar file handling
ksrini
parents: 7668
diff changeset
   250
     * executable to the jar on Windows.  Although this is just another
c2999ee84468 7194005: (launcher) needs to be enhanced for 64-bit jar file handling
ksrini
parents: 7668
diff changeset
   251
     * form of statically linking a small piece of the JVM to the
c2999ee84468 7194005: (launcher) needs to be enhanced for 64-bit jar file handling
ksrini
parents: 7668
diff changeset
   252
     * application, we choose to continue to support it.  Note that no
c2999ee84468 7194005: (launcher) needs to be enhanced for 64-bit jar file handling
ksrini
parents: 7668
diff changeset
   253
     * guarantees have been made (or should be made) to the customer that
c2999ee84468 7194005: (launcher) needs to be enhanced for 64-bit jar file handling
ksrini
parents: 7668
diff changeset
   254
     * this will continue to work.
c2999ee84468 7194005: (launcher) needs to be enhanced for 64-bit jar file handling
ksrini
parents: 7668
diff changeset
   255
     *
c2999ee84468 7194005: (launcher) needs to be enhanced for 64-bit jar file handling
ksrini
parents: 7668
diff changeset
   256
     * Therefore, calculate the base offset of the zip file (within the
c2999ee84468 7194005: (launcher) needs to be enhanced for 64-bit jar file handling
ksrini
parents: 7668
diff changeset
   257
     * expanded file) by assuming that the central directory is followed
c2999ee84468 7194005: (launcher) needs to be enhanced for 64-bit jar file handling
ksrini
parents: 7668
diff changeset
   258
     * immediately by the end record.
c2999ee84468 7194005: (launcher) needs to be enhanced for 64-bit jar file handling
ksrini
parents: 7668
diff changeset
   259
     */
c2999ee84468 7194005: (launcher) needs to be enhanced for 64-bit jar file handling
ksrini
parents: 7668
diff changeset
   260
    if (zip64_present) {
c2999ee84468 7194005: (launcher) needs to be enhanced for 64-bit jar file handling
ksrini
parents: 7668
diff changeset
   261
        if ((offset = ZIP64_LOCOFF(p)) < (jlong)0) {
c2999ee84468 7194005: (launcher) needs to be enhanced for 64-bit jar file handling
ksrini
parents: 7668
diff changeset
   262
            return -1;
c2999ee84468 7194005: (launcher) needs to be enhanced for 64-bit jar file handling
ksrini
parents: 7668
diff changeset
   263
        }
c2999ee84468 7194005: (launcher) needs to be enhanced for 64-bit jar file handling
ksrini
parents: 7668
diff changeset
   264
        if (JLI_Lseek(fd, offset, SEEK_SET) < (jlong) 0) {
c2999ee84468 7194005: (launcher) needs to be enhanced for 64-bit jar file handling
ksrini
parents: 7668
diff changeset
   265
            return (-1);
c2999ee84468 7194005: (launcher) needs to be enhanced for 64-bit jar file handling
ksrini
parents: 7668
diff changeset
   266
        }
c2999ee84468 7194005: (launcher) needs to be enhanced for 64-bit jar file handling
ksrini
parents: 7668
diff changeset
   267
        if ((bytes = read(fd, buffer, MINREAD)) < 0) {
c2999ee84468 7194005: (launcher) needs to be enhanced for 64-bit jar file handling
ksrini
parents: 7668
diff changeset
   268
            return (-1);
c2999ee84468 7194005: (launcher) needs to be enhanced for 64-bit jar file handling
ksrini
parents: 7668
diff changeset
   269
        }
c2999ee84468 7194005: (launcher) needs to be enhanced for 64-bit jar file handling
ksrini
parents: 7668
diff changeset
   270
        if (GETSIG(buffer) != ZIP64_ENDSIG) {
c2999ee84468 7194005: (launcher) needs to be enhanced for 64-bit jar file handling
ksrini
parents: 7668
diff changeset
   271
            return -1;
c2999ee84468 7194005: (launcher) needs to be enhanced for 64-bit jar file handling
ksrini
parents: 7668
diff changeset
   272
        }
c2999ee84468 7194005: (launcher) needs to be enhanced for 64-bit jar file handling
ksrini
parents: 7668
diff changeset
   273
        if ((offset = ZIP64_ENDOFF(buffer)) < (jlong)0) {
c2999ee84468 7194005: (launcher) needs to be enhanced for 64-bit jar file handling
ksrini
parents: 7668
diff changeset
   274
            return -1;
c2999ee84468 7194005: (launcher) needs to be enhanced for 64-bit jar file handling
ksrini
parents: 7668
diff changeset
   275
        }
c2999ee84468 7194005: (launcher) needs to be enhanced for 64-bit jar file handling
ksrini
parents: 7668
diff changeset
   276
        if (JLI_Lseek(fd, offset, SEEK_SET) < (jlong)0) {
c2999ee84468 7194005: (launcher) needs to be enhanced for 64-bit jar file handling
ksrini
parents: 7668
diff changeset
   277
            return (-1);
c2999ee84468 7194005: (launcher) needs to be enhanced for 64-bit jar file handling
ksrini
parents: 7668
diff changeset
   278
        }
c2999ee84468 7194005: (launcher) needs to be enhanced for 64-bit jar file handling
ksrini
parents: 7668
diff changeset
   279
        p = buffer;
c2999ee84468 7194005: (launcher) needs to be enhanced for 64-bit jar file handling
ksrini
parents: 7668
diff changeset
   280
        base_offset = base_offset - ZIP64_ENDSIZ(p) - ZIP64_ENDOFF(p) - ZIP64_ENDHDR;
c2999ee84468 7194005: (launcher) needs to be enhanced for 64-bit jar file handling
ksrini
parents: 7668
diff changeset
   281
    } else {
c2999ee84468 7194005: (launcher) needs to be enhanced for 64-bit jar file handling
ksrini
parents: 7668
diff changeset
   282
        base_offset = base_offset - ENDSIZ(p) - ENDOFF(p);
c2999ee84468 7194005: (launcher) needs to be enhanced for 64-bit jar file handling
ksrini
parents: 7668
diff changeset
   283
        /*
c2999ee84468 7194005: (launcher) needs to be enhanced for 64-bit jar file handling
ksrini
parents: 7668
diff changeset
   284
         * The END Header indicates the start of the Central Directory
c2999ee84468 7194005: (launcher) needs to be enhanced for 64-bit jar file handling
ksrini
parents: 7668
diff changeset
   285
         * Headers. Remember that the desired Central Directory Header (CEN)
c2999ee84468 7194005: (launcher) needs to be enhanced for 64-bit jar file handling
ksrini
parents: 7668
diff changeset
   286
         * will almost always be the second one and the first one is a small
c2999ee84468 7194005: (launcher) needs to be enhanced for 64-bit jar file handling
ksrini
parents: 7668
diff changeset
   287
         * directory entry ("META-INF/"). Keep the code optimized for
c2999ee84468 7194005: (launcher) needs to be enhanced for 64-bit jar file handling
ksrini
parents: 7668
diff changeset
   288
         * that case.
c2999ee84468 7194005: (launcher) needs to be enhanced for 64-bit jar file handling
ksrini
parents: 7668
diff changeset
   289
         *
c2999ee84468 7194005: (launcher) needs to be enhanced for 64-bit jar file handling
ksrini
parents: 7668
diff changeset
   290
         * Seek to the beginning of the Central Directory.
c2999ee84468 7194005: (launcher) needs to be enhanced for 64-bit jar file handling
ksrini
parents: 7668
diff changeset
   291
         */
c2999ee84468 7194005: (launcher) needs to be enhanced for 64-bit jar file handling
ksrini
parents: 7668
diff changeset
   292
        if (JLI_Lseek(fd, base_offset + ENDOFF(p), SEEK_SET) < (jlong) 0) {
c2999ee84468 7194005: (launcher) needs to be enhanced for 64-bit jar file handling
ksrini
parents: 7668
diff changeset
   293
            return (-1);
c2999ee84468 7194005: (launcher) needs to be enhanced for 64-bit jar file handling
ksrini
parents: 7668
diff changeset
   294
        }
c2999ee84468 7194005: (launcher) needs to be enhanced for 64-bit jar file handling
ksrini
parents: 7668
diff changeset
   295
    }
c2999ee84468 7194005: (launcher) needs to be enhanced for 64-bit jar file handling
ksrini
parents: 7668
diff changeset
   296
    return base_offset;
c2999ee84468 7194005: (launcher) needs to be enhanced for 64-bit jar file handling
ksrini
parents: 7668
diff changeset
   297
}
c2999ee84468 7194005: (launcher) needs to be enhanced for 64-bit jar file handling
ksrini
parents: 7668
diff changeset
   298
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
 * Locate the manifest file with the zip/jar file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
 *      fd:     File descriptor of the jar file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
 *      entry:  To be populated with the information necessary to perform
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
 *              the inflation (the compressed and uncompressed sizes and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
 *              the offset in the file where the compressed data is located).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
 * Returns zero upon success. Returns a negative value upon failure.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
 * The buffer for reading the Central Directory if the zip/jar file needs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
 * to be large enough to accommodate the largest possible single record
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
 * and the signature of the next record which is:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
 *      3*2**16 + CENHDR + SIGSIZ
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
 * Each of the three variable sized fields (name, comment and extension)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
 * has a maximum possible size of 64k.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
 * Typically, only a small bit of this buffer is used with bytes shuffled
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
 * down to the beginning of the buffer.  It is one thing to allocate such
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
 * a large buffer and another thing to actually start faulting it in.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
 * In most cases, all that needs to be read are the first two entries in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
 * a typical jar file (META-INF and META-INF/MANIFEST.MF). Keep this factoid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
 * in mind when optimizing this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
static int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
find_file(int fd, zentry *entry, const char *file_name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    int     bytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    int     res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
    int     entry_size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    int     read_size;
13675
c2999ee84468 7194005: (launcher) needs to be enhanced for 64-bit jar file handling
ksrini
parents: 7668
diff changeset
   333
    jlong   base_offset;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    Byte    *p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    Byte    *bp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
    Byte    *buffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    Byte    locbuf[LOCHDR];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    if ((buffer = (Byte*)malloc(BUFSIZE)) == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        return(-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    bp = buffer;
13675
c2999ee84468 7194005: (launcher) needs to be enhanced for 64-bit jar file handling
ksrini
parents: 7668
diff changeset
   344
    base_offset = compute_cen(fd, bp);
c2999ee84468 7194005: (launcher) needs to be enhanced for 64-bit jar file handling
ksrini
parents: 7668
diff changeset
   345
    if (base_offset == -1) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        free(buffer);
13675
c2999ee84468 7194005: (launcher) needs to be enhanced for 64-bit jar file handling
ksrini
parents: 7668
diff changeset
   347
        return -1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
    if ((bytes = read(fd, bp, MINREAD)) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        free(buffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        return (-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
    }
13675
c2999ee84468 7194005: (launcher) needs to be enhanced for 64-bit jar file handling
ksrini
parents: 7668
diff changeset
   354
    p = bp;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     * Loop through the Central Directory Headers. Note that a valid zip/jar
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     * must have an ENDHDR (with ENDSIG) after the Central Directory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
    while (GETSIG(p) == CENSIG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
         * If a complete header isn't in the buffer, shift the contents
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
         * of the buffer down and refill the buffer.  Note that the check
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
         * for "bytes < CENHDR" must be made before the test for the entire
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
         * size of the header, because if bytes is less than CENHDR, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
         * actual size of the header can't be determined. The addition of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
         * SIGSIZ guarantees that the next signature is also in the buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
         * for proper loop termination.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        if (bytes < CENHDR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
            p = memmove(bp, p, bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
            if ((res = read(fd, bp + bytes, MINREAD)) <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
                free(buffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                return (-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
            bytes += res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        entry_size = CENHDR + CENNAM(p) + CENEXT(p) + CENCOM(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        if (bytes < entry_size + SIGSIZ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            if (p != bp)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
                p = memmove(bp, p, bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
            read_size = entry_size - bytes + SIGSIZ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
            read_size = (read_size < MINREAD) ? MINREAD : read_size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
            if ((res = read(fd, bp + bytes,  read_size)) <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
                free(buffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
                return (-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
            bytes += res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
         * Check if the name is the droid we are looking for; the jar file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
         * manifest.  If so, build the entry record from the data found in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
         * the header located and return success.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
         */
7028
adadd244f506 6989469: (launcher) compiler warnings in jli native code
ksrini
parents: 5506
diff changeset
   396
        if ((size_t)CENNAM(p) == JLI_StrLen(file_name) &&
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
          memcmp((p + CENHDR), file_name, JLI_StrLen(file_name)) == 0) {
13675
c2999ee84468 7194005: (launcher) needs to be enhanced for 64-bit jar file handling
ksrini
parents: 7668
diff changeset
   398
            if (JLI_Lseek(fd, base_offset + CENOFF(p), SEEK_SET) < (jlong)0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
                free(buffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
                return (-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
            if (read(fd, locbuf, LOCHDR) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                free(buffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                return (-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
            if (GETSIG(locbuf) != LOCSIG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
                free(buffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
                return (-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
            entry->isize = CENLEN(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
            entry->csize = CENSIZ(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
            entry->offset = base_offset + CENOFF(p) + LOCHDR +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
                LOCNAM(locbuf) + LOCEXT(locbuf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
            entry->how = CENHOW(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
            free(buffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
            return (0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
         * Point to the next entry and decrement the count of valid remaining
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
         * bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        bytes -= entry_size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
        p += entry_size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
    free(buffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
    return (-1);        /* Fell off the end the loop without a Manifest */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
 * Parse a Manifest file header entry into a distinct "name" and "value".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
 * Continuation lines are joined into a single "value". The documented
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
 * syntax for a header entry is:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
 *      header: name ":" value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
 *      name: alphanum *headerchar
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
 *      value: SPACE *otherchar newline *continuation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
 *      continuation: SPACE *otherchar newline
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
 *      newline: CR LF | LF | CR (not followed by LF)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
 *      alphanum: {"A"-"Z"} | {"a"-"z"} | {"0"-"9"}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
 *      headerchar: alphanum | "-" | "_"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
 *      otherchar: any UTF-8 character except NUL, CR and LF
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
 * Note that a manifest file may be composed of multiple sections,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
 * each of which may contain multiple headers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
 *      section: *header +newline
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
 *      nonempty-section: +header +newline
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
 * (Note that the point of "nonempty-section" is unclear, because it isn't
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
 * referenced elsewhere in the full specification for the Manifest file.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
 * Arguments:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
 *      lp      pointer to a character pointer which points to the start
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
 *              of a valid header.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
 *      name    pointer to a character pointer which will be set to point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
 *              to the name portion of the header (nul terminated).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
 *      value   pointer to a character pointer which will be set to point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
 *              to the value portion of the header (nul terminated).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
 * Returns:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
 *    1 Successful parsing of an NV pair.  lp is updated to point to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
 *      next character after the terminating newline in the string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
 *      representing the Manifest file. name and value are updated to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
 *      point to the strings parsed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
 *    0 A valid end of section indicator was encountered.  lp, name, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
 *      value are not modified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
 *   -1 lp does not point to a valid header. Upon return, the values of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
 *      lp, name, and value are undefined.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
static int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
parse_nv_pair(char **lp, char **name, char **value)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
    char    *nl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
    char    *cp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
     * End of the section - return 0. The end of section condition is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
     * indicated by either encountering a blank line or the end of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
     * Manifest "string" (EOF).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
    if (**lp == '\0' || **lp == '\n' || **lp == '\r')
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
        return (0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
     * Getting to here, indicates that *lp points to an "otherchar".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
     * Turn the "header" into a string on its own.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
    nl = JLI_StrPBrk(*lp, "\n\r");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
    if (nl == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        nl = JLI_StrChr(*lp, (int)'\0');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
        cp = nl;                        /* For merging continuation lines */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
        if (*nl == '\r' && *(nl+1) == '\n')
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
            *nl++ = '\0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
        *nl++ = '\0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
         * Process any "continuation" line(s), by making them part of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
         * "header" line. Yes, I know that we are "undoing" the NULs we
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
         * just placed here, but continuation lines are the fairly rare
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
         * case, so we shouldn't unnecessarily complicate the code above.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
         * Note that an entire continuation line is processed each iteration
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
         * through the outer while loop.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
        while (*nl == ' ') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
            nl++;                       /* First character to be moved */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
            while (*nl != '\n' && *nl != '\r' && *nl != '\0')
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
                *cp++ = *nl++;          /* Shift string */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
            if (*nl == '\0')
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
                return (-1);            /* Error: newline required */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
            *cp = '\0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
            if (*nl == '\r' && *(nl+1) == '\n')
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
                *nl++ = '\0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
            *nl++ = '\0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
     * Separate the name from the value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
    cp = JLI_StrChr(*lp, (int)':');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
    if (cp == NULL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
        return (-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
    *cp++ = '\0';               /* The colon terminates the name */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
    if (*cp != ' ')
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
        return (-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
    *cp++ = '\0';               /* Eat the required space */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
    *name = *lp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
    *value = cp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
    *lp = nl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
    return (1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
 * Read the manifest from the specified jar file and fill in the manifest_info
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
 * structure with the information found within.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
 * Error returns are as follows:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
 *    0 Success
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
 *   -1 Unable to open jarfile
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
 *   -2 Error accessing the manifest from within the jarfile (most likely
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
 *      a manifest is not present, or this isn't a valid zip/jar file).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
JLI_ParseManifest(char *jarfile, manifest_info *info)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
    int     fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
    zentry  entry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
    char    *lp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
    char    *name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
    char    *value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
    int     rc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
    char    *splashscreen_name = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
    if ((fd = open(jarfile, O_RDONLY
13675
c2999ee84468 7194005: (launcher) needs to be enhanced for 64-bit jar file handling
ksrini
parents: 7668
diff changeset
   566
#ifdef O_LARGEFILE
15644
2dfa520c7303 8006995: java launcher fails to open executable JAR > 2GB
martin
parents: 13675
diff changeset
   567
        | O_LARGEFILE /* large file mode */
13675
c2999ee84468 7194005: (launcher) needs to be enhanced for 64-bit jar file handling
ksrini
parents: 7668
diff changeset
   568
#endif
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
#ifdef O_BINARY
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
        | O_BINARY /* use binary mode on windows */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
#endif
16078
3d5fa54bf734 7200500: Launcher better input validation
ksrini
parents: 13675
diff changeset
   572
        )) == -1) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
        return (-1);
16078
3d5fa54bf734 7200500: Launcher better input validation
ksrini
parents: 13675
diff changeset
   574
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
    info->manifest_version = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
    info->main_class = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
    info->jre_version = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
    info->jre_restrict_search = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
    info->splashscreen_image_file_name = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
    if (rc = find_file(fd, &entry, manifest_name) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
        close(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
        return (-2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
    manifest = inflate_file(fd, &entry, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
    if (manifest == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
        close(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
        return (-2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
    lp = manifest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
    while ((rc = parse_nv_pair(&lp, &name, &value)) > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
        if (JLI_StrCaseCmp(name, "Manifest-Version") == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
            info->manifest_version = value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
        else if (JLI_StrCaseCmp(name, "Main-Class") == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
            info->main_class = value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
        else if (JLI_StrCaseCmp(name, "JRE-Version") == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
            info->jre_version = value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
        else if (JLI_StrCaseCmp(name, "JRE-Restrict-Search") == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
            if (JLI_StrCaseCmp(value, "true") == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
                info->jre_restrict_search = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
        } else if (JLI_StrCaseCmp(name, "Splashscreen-Image") == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
            info->splashscreen_image_file_name = value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
    close(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
    if (rc == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
        return (0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
    else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
        return (-2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
 * Opens the jar file and unpacks the specified file from its contents.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
 * Returns NULL on failure.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
void *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
JLI_JarUnpackFile(const char *jarfile, const char *filename, int *size) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
    int     fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
    zentry  entry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
    void    *data = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
16078
3d5fa54bf734 7200500: Launcher better input validation
ksrini
parents: 13675
diff changeset
   621
    if ((fd = open(jarfile, O_RDONLY
15644
2dfa520c7303 8006995: java launcher fails to open executable JAR > 2GB
martin
parents: 13675
diff changeset
   622
#ifdef O_LARGEFILE
2dfa520c7303 8006995: java launcher fails to open executable JAR > 2GB
martin
parents: 13675
diff changeset
   623
        | O_LARGEFILE /* large file mode */
2dfa520c7303 8006995: java launcher fails to open executable JAR > 2GB
martin
parents: 13675
diff changeset
   624
#endif
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
#ifdef O_BINARY
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
        | O_BINARY /* use binary mode on windows */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
#endif
16078
3d5fa54bf734 7200500: Launcher better input validation
ksrini
parents: 13675
diff changeset
   628
        )) == -1) {
3d5fa54bf734 7200500: Launcher better input validation
ksrini
parents: 13675
diff changeset
   629
        return NULL;
3d5fa54bf734 7200500: Launcher better input validation
ksrini
parents: 13675
diff changeset
   630
    }
3d5fa54bf734 7200500: Launcher better input validation
ksrini
parents: 13675
diff changeset
   631
    if (find_file(fd, &entry, filename) == 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
        data = inflate_file(fd, &entry, size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
    close(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
    return (data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
 * Specialized "free" function.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
JLI_FreeManifest()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
    if (manifest)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
        free(manifest);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
 * Iterate over the manifest of the specified jar file and invoke the provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
 * closure function for each attribute encountered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
 * Error returns are as follows:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
 *    0 Success
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
 *   -1 Unable to open jarfile
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
 *   -2 Error accessing the manifest from within the jarfile (most likely
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
 *      this means a manifest is not present, or it isn't a valid zip/jar file).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
JLI_ManifestIterate(const char *jarfile, attribute_closure ac, void *user_data)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
    int     fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
    zentry  entry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
    char    *mp;        /* manifest pointer */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
    char    *lp;        /* pointer into manifest, updated during iteration */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
    char    *name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
    char    *value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
    int     rc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
    if ((fd = open(jarfile, O_RDONLY
15644
2dfa520c7303 8006995: java launcher fails to open executable JAR > 2GB
martin
parents: 13675
diff changeset
   670
#ifdef O_LARGEFILE
2dfa520c7303 8006995: java launcher fails to open executable JAR > 2GB
martin
parents: 13675
diff changeset
   671
        | O_LARGEFILE /* large file mode */
2dfa520c7303 8006995: java launcher fails to open executable JAR > 2GB
martin
parents: 13675
diff changeset
   672
#endif
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
#ifdef O_BINARY
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
        | O_BINARY /* use binary mode on windows */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
#endif
16078
3d5fa54bf734 7200500: Launcher better input validation
ksrini
parents: 13675
diff changeset
   676
        )) == -1) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
        return (-1);
16078
3d5fa54bf734 7200500: Launcher better input validation
ksrini
parents: 13675
diff changeset
   678
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
    if (rc = find_file(fd, &entry, manifest_name) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
        close(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
        return (-2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
    mp = inflate_file(fd, &entry, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
    if (mp == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
        close(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
        return (-2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
    lp = mp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
    while ((rc = parse_nv_pair(&lp, &name, &value)) > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
        (*ac)(name, value, user_data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
    free(mp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
    close(fd);
7028
adadd244f506 6989469: (launcher) compiler warnings in jli native code
ksrini
parents: 5506
diff changeset
   697
    return (rc == 0) ? 0 : -2;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
}