src/java.base/share/native/libjli/parse_manifest.c
author ihse
Tue, 13 Feb 2018 10:37:33 +0100
branchihse-remove-mapfiles-branch
changeset 56106 40e61db323c2
parent 47216 71c04702a3d5
child 56721 01b558efd286
permissions -rw-r--r--
First stab at removing mapfiles.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
56106
40e61db323c2 First stab at removing mapfiles.
ihse
parents: 47216
diff changeset
     2
 * Copyright (c) 2003, 2018, 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>
56106
40e61db323c2 First stab at removing mapfiles.
ihse
parents: 47216
diff changeset
    32
#include "jni.h"
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
#include "jli_util.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
#include <zlib.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
#include "manifest_info.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
static char     *manifest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
static const char       *manifest_name = "META-INF/MANIFEST.MF";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * Inflate the manifest file (or any file for that matter).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *   fd:        File descriptor of the jar file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *   entry:     Contains the information necessary to perform the inflation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *              (the compressed and uncompressed sizes and the offset in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *              the file where the compressed data is located).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *   size_out:  Returns the size of the inflated file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * Upon success, it returns a pointer to a NUL-terminated malloc'd buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * containing the inflated manifest file.  When the caller is done with it,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * this buffer should be released by a call to free().  Upon failure,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * returns NULL.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
static char *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
inflate_file(int fd, zentry *entry, int *size_out)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    char        *in;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    char        *out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    z_stream    zs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
3848
2c2395fb6d85 6842838: 64-bit failure in handling invalid manifest in launcher.
kevinw
parents: 2
diff changeset
    63
    if (entry->csize == (size_t) -1 || entry->isize == (size_t) -1 )
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        return (NULL);
13675
c2999ee84468 7194005: (launcher) needs to be enhanced for 64-bit jar file handling
ksrini
parents: 7668
diff changeset
    65
    if (JLI_Lseek(fd, entry->offset, SEEK_SET) < (jlong)0)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        return (NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    if ((in = malloc(entry->csize + 1)) == NULL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        return (NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    if ((size_t)(read(fd, in, (unsigned int)entry->csize)) != entry->csize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        free(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        return (NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    if (entry->how == STORED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        *(char *)((size_t)in + entry->csize) = '\0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        if (size_out) {
7028
adadd244f506 6989469: (launcher) compiler warnings in jli native code
ksrini
parents: 5506
diff changeset
    76
            *size_out = (int)entry->csize;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        return (in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    } else if (entry->how == DEFLATED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        zs.zalloc = (alloc_func)Z_NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        zs.zfree = (free_func)Z_NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        zs.opaque = (voidpf)Z_NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        zs.next_in = (Byte*)in;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        zs.avail_in = (uInt)entry->csize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        if (inflateInit2(&zs, -MAX_WBITS) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            free(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            return (NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        if ((out = malloc(entry->isize + 1)) == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            free(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            return (NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        zs.next_out = (Byte*)out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        zs.avail_out = (uInt)entry->isize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        if (inflate(&zs, Z_PARTIAL_FLUSH) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            free(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            free(out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            return (NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        *(char *)((size_t)out + entry->isize) = '\0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        free(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        if (inflateEnd(&zs) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            free(out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            return (NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        if (size_out) {
7028
adadd244f506 6989469: (launcher) compiler warnings in jli native code
ksrini
parents: 5506
diff changeset
   107
            *size_out = (int)entry->isize;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        return (out);
15668
c5d8a90a4b40 8005750: [parfait] Memory leak at jdk/src/share/bin/parse_manifest.c
ksrini
parents: 15644
diff changeset
   110
    }
c5d8a90a4b40 8005750: [parfait] Memory leak at jdk/src/share/bin/parse_manifest.c
ksrini
parents: 15644
diff changeset
   111
    free(in);
c5d8a90a4b40 8005750: [parfait] Memory leak at jdk/src/share/bin/parse_manifest.c
ksrini
parents: 15644
diff changeset
   112
    return (NULL);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
29713
eeabfe673c97 8073158: zip files with total entry count 0xFFFF need not be ZIP64 files
martin
parents: 29389
diff changeset
   115
/*
eeabfe673c97 8073158: zip files with total entry count 0xFFFF need not be ZIP64 files
martin
parents: 29389
diff changeset
   116
 * Implementation notes:
eeabfe673c97 8073158: zip files with total entry count 0xFFFF need not be ZIP64 files
martin
parents: 29389
diff changeset
   117
 *
eeabfe673c97 8073158: zip files with total entry count 0xFFFF need not be ZIP64 files
martin
parents: 29389
diff changeset
   118
 * This is a zip format reader for seekable files, that tolerates
eeabfe673c97 8073158: zip files with total entry count 0xFFFF need not be ZIP64 files
martin
parents: 29389
diff changeset
   119
 * leading and trailing garbage, and tolerates having had internal
eeabfe673c97 8073158: zip files with total entry count 0xFFFF need not be ZIP64 files
martin
parents: 29389
diff changeset
   120
 * offsets adjusted for leading garbage (as with Info-Zip's zip -A).
eeabfe673c97 8073158: zip files with total entry count 0xFFFF need not be ZIP64 files
martin
parents: 29389
diff changeset
   121
 *
eeabfe673c97 8073158: zip files with total entry count 0xFFFF need not be ZIP64 files
martin
parents: 29389
diff changeset
   122
 * We find the end header by scanning backwards from the end of the
eeabfe673c97 8073158: zip files with total entry count 0xFFFF need not be ZIP64 files
martin
parents: 29389
diff changeset
   123
 * file for the end signature.  This may fail in the presence of
eeabfe673c97 8073158: zip files with total entry count 0xFFFF need not be ZIP64 files
martin
parents: 29389
diff changeset
   124
 * trailing garbage or a ZIP file comment that contains binary data.
eeabfe673c97 8073158: zip files with total entry count 0xFFFF need not be ZIP64 files
martin
parents: 29389
diff changeset
   125
 * Similarly, the ZIP64 end header may need to be located by scanning
eeabfe673c97 8073158: zip files with total entry count 0xFFFF need not be ZIP64 files
martin
parents: 29389
diff changeset
   126
 * backwards from the end header.  It may be misidentified, but this
eeabfe673c97 8073158: zip files with total entry count 0xFFFF need not be ZIP64 files
martin
parents: 29389
diff changeset
   127
 * is very unlikely to happen in practice without adversarial input.
eeabfe673c97 8073158: zip files with total entry count 0xFFFF need not be ZIP64 files
martin
parents: 29389
diff changeset
   128
 *
eeabfe673c97 8073158: zip files with total entry count 0xFFFF need not be ZIP64 files
martin
parents: 29389
diff changeset
   129
 * The zip file format is documented at:
eeabfe673c97 8073158: zip files with total entry count 0xFFFF need not be ZIP64 files
martin
parents: 29389
diff changeset
   130
 * https://www.pkware.com/documents/casestudies/APPNOTE.TXT
eeabfe673c97 8073158: zip files with total entry count 0xFFFF need not be ZIP64 files
martin
parents: 29389
diff changeset
   131
 *
eeabfe673c97 8073158: zip files with total entry count 0xFFFF need not be ZIP64 files
martin
parents: 29389
diff changeset
   132
 * TODO: more informative error messages
eeabfe673c97 8073158: zip files with total entry count 0xFFFF need not be ZIP64 files
martin
parents: 29389
diff changeset
   133
 */
eeabfe673c97 8073158: zip files with total entry count 0xFFFF need not be ZIP64 files
martin
parents: 29389
diff changeset
   134
eeabfe673c97 8073158: zip files with total entry count 0xFFFF need not be ZIP64 files
martin
parents: 29389
diff changeset
   135
/** Reads count bytes from fd at position pos into given buffer. */
eeabfe673c97 8073158: zip files with total entry count 0xFFFF need not be ZIP64 files
martin
parents: 29389
diff changeset
   136
static jboolean
29742
b73f38796859 8074840: Resolve disabled warnings for libjli and libjli_static
mikael
parents: 29713
diff changeset
   137
readAt(int fd, jlong pos, unsigned int count, void *buf) {
29713
eeabfe673c97 8073158: zip files with total entry count 0xFFFF need not be ZIP64 files
martin
parents: 29389
diff changeset
   138
    return (pos >= 0
eeabfe673c97 8073158: zip files with total entry count 0xFFFF need not be ZIP64 files
martin
parents: 29389
diff changeset
   139
            && JLI_Lseek(fd, pos, SEEK_SET) == pos
eeabfe673c97 8073158: zip files with total entry count 0xFFFF need not be ZIP64 files
martin
parents: 29389
diff changeset
   140
            && read(fd, buf, count) == (jlong) count);
eeabfe673c97 8073158: zip files with total entry count 0xFFFF need not be ZIP64 files
martin
parents: 29389
diff changeset
   141
}
eeabfe673c97 8073158: zip files with total entry count 0xFFFF need not be ZIP64 files
martin
parents: 29389
diff changeset
   142
13675
c2999ee84468 7194005: (launcher) needs to be enhanced for 64-bit jar file handling
ksrini
parents: 7668
diff changeset
   143
c2999ee84468 7194005: (launcher) needs to be enhanced for 64-bit jar file handling
ksrini
parents: 7668
diff changeset
   144
/*
29713
eeabfe673c97 8073158: zip files with total entry count 0xFFFF need not be ZIP64 files
martin
parents: 29389
diff changeset
   145
 * Tells whether given header values (obtained from either ZIP64 or
eeabfe673c97 8073158: zip files with total entry count 0xFFFF need not be ZIP64 files
martin
parents: 29389
diff changeset
   146
 * non-ZIP64 header) appear to be correct, by checking the first LOC
eeabfe673c97 8073158: zip files with total entry count 0xFFFF need not be ZIP64 files
martin
parents: 29389
diff changeset
   147
 * and CEN headers.
13675
c2999ee84468 7194005: (launcher) needs to be enhanced for 64-bit jar file handling
ksrini
parents: 7668
diff changeset
   148
 */
29713
eeabfe673c97 8073158: zip files with total entry count 0xFFFF need not be ZIP64 files
martin
parents: 29389
diff changeset
   149
static jboolean
eeabfe673c97 8073158: zip files with total entry count 0xFFFF need not be ZIP64 files
martin
parents: 29389
diff changeset
   150
is_valid_end_header(int fd, jlong endpos,
eeabfe673c97 8073158: zip files with total entry count 0xFFFF need not be ZIP64 files
martin
parents: 29389
diff changeset
   151
                    jlong censiz, jlong cenoff, jlong entries) {
eeabfe673c97 8073158: zip files with total entry count 0xFFFF need not be ZIP64 files
martin
parents: 29389
diff changeset
   152
    Byte cenhdr[CENHDR];
eeabfe673c97 8073158: zip files with total entry count 0xFFFF need not be ZIP64 files
martin
parents: 29389
diff changeset
   153
    Byte lochdr[LOCHDR];
eeabfe673c97 8073158: zip files with total entry count 0xFFFF need not be ZIP64 files
martin
parents: 29389
diff changeset
   154
    // Expected offset of the first central directory header
eeabfe673c97 8073158: zip files with total entry count 0xFFFF need not be ZIP64 files
martin
parents: 29389
diff changeset
   155
    jlong censtart = endpos - censiz;
eeabfe673c97 8073158: zip files with total entry count 0xFFFF need not be ZIP64 files
martin
parents: 29389
diff changeset
   156
    // Expected position within the file that offsets are relative to
eeabfe673c97 8073158: zip files with total entry count 0xFFFF need not be ZIP64 files
martin
parents: 29389
diff changeset
   157
    jlong base_offset = endpos - (censiz + cenoff);
eeabfe673c97 8073158: zip files with total entry count 0xFFFF need not be ZIP64 files
martin
parents: 29389
diff changeset
   158
    return censtart >= 0 && cenoff >= 0 &&
eeabfe673c97 8073158: zip files with total entry count 0xFFFF need not be ZIP64 files
martin
parents: 29389
diff changeset
   159
        (censiz == 0 ||
eeabfe673c97 8073158: zip files with total entry count 0xFFFF need not be ZIP64 files
martin
parents: 29389
diff changeset
   160
         // Validate first CEN and LOC header signatures.
eeabfe673c97 8073158: zip files with total entry count 0xFFFF need not be ZIP64 files
martin
parents: 29389
diff changeset
   161
         // Central directory must come directly before the end header.
eeabfe673c97 8073158: zip files with total entry count 0xFFFF need not be ZIP64 files
martin
parents: 29389
diff changeset
   162
         (readAt(fd, censtart, CENHDR, cenhdr)
eeabfe673c97 8073158: zip files with total entry count 0xFFFF need not be ZIP64 files
martin
parents: 29389
diff changeset
   163
          && CENSIG_AT(cenhdr)
eeabfe673c97 8073158: zip files with total entry count 0xFFFF need not be ZIP64 files
martin
parents: 29389
diff changeset
   164
          && readAt(fd, base_offset + CENOFF(cenhdr), LOCHDR, lochdr)
eeabfe673c97 8073158: zip files with total entry count 0xFFFF need not be ZIP64 files
martin
parents: 29389
diff changeset
   165
          && LOCSIG_AT(lochdr)
eeabfe673c97 8073158: zip files with total entry count 0xFFFF need not be ZIP64 files
martin
parents: 29389
diff changeset
   166
          && CENNAM(cenhdr) == LOCNAM(lochdr)));
13675
c2999ee84468 7194005: (launcher) needs to be enhanced for 64-bit jar file handling
ksrini
parents: 7668
diff changeset
   167
}
c2999ee84468 7194005: (launcher) needs to be enhanced for 64-bit jar file handling
ksrini
parents: 7668
diff changeset
   168
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
/*
29713
eeabfe673c97 8073158: zip files with total entry count 0xFFFF need not be ZIP64 files
martin
parents: 29389
diff changeset
   170
 * Tells whether p appears to be pointing at a valid ZIP64 end header.
eeabfe673c97 8073158: zip files with total entry count 0xFFFF need not be ZIP64 files
martin
parents: 29389
diff changeset
   171
 * Values censiz, cenoff, and entries are the corresponding values
eeabfe673c97 8073158: zip files with total entry count 0xFFFF need not be ZIP64 files
martin
parents: 29389
diff changeset
   172
 * from the non-ZIP64 end header.  We perform extra checks to avoid
eeabfe673c97 8073158: zip files with total entry count 0xFFFF need not be ZIP64 files
martin
parents: 29389
diff changeset
   173
 * misidentifying data from the last entry as a ZIP64 end header.
eeabfe673c97 8073158: zip files with total entry count 0xFFFF need not be ZIP64 files
martin
parents: 29389
diff changeset
   174
 */
eeabfe673c97 8073158: zip files with total entry count 0xFFFF need not be ZIP64 files
martin
parents: 29389
diff changeset
   175
static jboolean
eeabfe673c97 8073158: zip files with total entry count 0xFFFF need not be ZIP64 files
martin
parents: 29389
diff changeset
   176
is_zip64_endhdr(int fd, const Byte *p, jlong end64pos,
eeabfe673c97 8073158: zip files with total entry count 0xFFFF need not be ZIP64 files
martin
parents: 29389
diff changeset
   177
                jlong censiz, jlong cenoff, jlong entries) {
eeabfe673c97 8073158: zip files with total entry count 0xFFFF need not be ZIP64 files
martin
parents: 29389
diff changeset
   178
    if (ZIP64_ENDSIG_AT(p)) {
eeabfe673c97 8073158: zip files with total entry count 0xFFFF need not be ZIP64 files
martin
parents: 29389
diff changeset
   179
        jlong censiz64 = ZIP64_ENDSIZ(p);
eeabfe673c97 8073158: zip files with total entry count 0xFFFF need not be ZIP64 files
martin
parents: 29389
diff changeset
   180
        jlong cenoff64 = ZIP64_ENDOFF(p);
eeabfe673c97 8073158: zip files with total entry count 0xFFFF need not be ZIP64 files
martin
parents: 29389
diff changeset
   181
        jlong entries64 = ZIP64_ENDTOT(p);
eeabfe673c97 8073158: zip files with total entry count 0xFFFF need not be ZIP64 files
martin
parents: 29389
diff changeset
   182
        return (censiz64 == censiz || censiz == ZIP64_MAGICVAL)
eeabfe673c97 8073158: zip files with total entry count 0xFFFF need not be ZIP64 files
martin
parents: 29389
diff changeset
   183
            && (cenoff64 == cenoff || cenoff == ZIP64_MAGICVAL)
eeabfe673c97 8073158: zip files with total entry count 0xFFFF need not be ZIP64 files
martin
parents: 29389
diff changeset
   184
            && (entries64 == entries || entries == ZIP64_MAGICCOUNT)
eeabfe673c97 8073158: zip files with total entry count 0xFFFF need not be ZIP64 files
martin
parents: 29389
diff changeset
   185
            && is_valid_end_header(fd, end64pos, censiz64, cenoff64, entries64);
eeabfe673c97 8073158: zip files with total entry count 0xFFFF need not be ZIP64 files
martin
parents: 29389
diff changeset
   186
    }
eeabfe673c97 8073158: zip files with total entry count 0xFFFF need not be ZIP64 files
martin
parents: 29389
diff changeset
   187
    return JNI_FALSE;
eeabfe673c97 8073158: zip files with total entry count 0xFFFF need not be ZIP64 files
martin
parents: 29389
diff changeset
   188
}
eeabfe673c97 8073158: zip files with total entry count 0xFFFF need not be ZIP64 files
martin
parents: 29389
diff changeset
   189
eeabfe673c97 8073158: zip files with total entry count 0xFFFF need not be ZIP64 files
martin
parents: 29389
diff changeset
   190
/*
eeabfe673c97 8073158: zip files with total entry count 0xFFFF need not be ZIP64 files
martin
parents: 29389
diff changeset
   191
 * Given a non-ZIP64 end header located at endhdr and endpos, look for
eeabfe673c97 8073158: zip files with total entry count 0xFFFF need not be ZIP64 files
martin
parents: 29389
diff changeset
   192
 * an adjacent ZIP64 end header, finding the base offset and censtart
eeabfe673c97 8073158: zip files with total entry count 0xFFFF need not be ZIP64 files
martin
parents: 29389
diff changeset
   193
 * from the ZIP64 header if available, else from the non-ZIP64 header.
eeabfe673c97 8073158: zip files with total entry count 0xFFFF need not be ZIP64 files
martin
parents: 29389
diff changeset
   194
 * @return 0 if successful, -1 in case of failure
eeabfe673c97 8073158: zip files with total entry count 0xFFFF need not be ZIP64 files
martin
parents: 29389
diff changeset
   195
 */
eeabfe673c97 8073158: zip files with total entry count 0xFFFF need not be ZIP64 files
martin
parents: 29389
diff changeset
   196
static int
eeabfe673c97 8073158: zip files with total entry count 0xFFFF need not be ZIP64 files
martin
parents: 29389
diff changeset
   197
find_positions64(int fd, const Byte * const endhdr, const jlong endpos,
eeabfe673c97 8073158: zip files with total entry count 0xFFFF need not be ZIP64 files
martin
parents: 29389
diff changeset
   198
                 jlong* base_offset, jlong* censtart)
eeabfe673c97 8073158: zip files with total entry count 0xFFFF need not be ZIP64 files
martin
parents: 29389
diff changeset
   199
{
eeabfe673c97 8073158: zip files with total entry count 0xFFFF need not be ZIP64 files
martin
parents: 29389
diff changeset
   200
    jlong censiz = ENDSIZ(endhdr);
eeabfe673c97 8073158: zip files with total entry count 0xFFFF need not be ZIP64 files
martin
parents: 29389
diff changeset
   201
    jlong cenoff = ENDOFF(endhdr);
eeabfe673c97 8073158: zip files with total entry count 0xFFFF need not be ZIP64 files
martin
parents: 29389
diff changeset
   202
    jlong entries = ENDTOT(endhdr);
eeabfe673c97 8073158: zip files with total entry count 0xFFFF need not be ZIP64 files
martin
parents: 29389
diff changeset
   203
    jlong end64pos;
eeabfe673c97 8073158: zip files with total entry count 0xFFFF need not be ZIP64 files
martin
parents: 29389
diff changeset
   204
    Byte buf[ZIP64_ENDHDR + ZIP64_LOCHDR];
eeabfe673c97 8073158: zip files with total entry count 0xFFFF need not be ZIP64 files
martin
parents: 29389
diff changeset
   205
    if (censiz + cenoff != endpos
eeabfe673c97 8073158: zip files with total entry count 0xFFFF need not be ZIP64 files
martin
parents: 29389
diff changeset
   206
        && (end64pos = endpos - sizeof(buf)) >= (jlong)0
eeabfe673c97 8073158: zip files with total entry count 0xFFFF need not be ZIP64 files
martin
parents: 29389
diff changeset
   207
        && readAt(fd, end64pos, sizeof(buf), buf)
eeabfe673c97 8073158: zip files with total entry count 0xFFFF need not be ZIP64 files
martin
parents: 29389
diff changeset
   208
        && ZIP64_LOCSIG_AT(buf + ZIP64_ENDHDR)
eeabfe673c97 8073158: zip files with total entry count 0xFFFF need not be ZIP64 files
martin
parents: 29389
diff changeset
   209
        && (jlong) ZIP64_LOCDSK(buf + ZIP64_ENDHDR) == ENDDSK(endhdr)
eeabfe673c97 8073158: zip files with total entry count 0xFFFF need not be ZIP64 files
martin
parents: 29389
diff changeset
   210
        && (is_zip64_endhdr(fd, buf, end64pos, censiz, cenoff, entries)
eeabfe673c97 8073158: zip files with total entry count 0xFFFF need not be ZIP64 files
martin
parents: 29389
diff changeset
   211
            || // A variable sized "zip64 extensible data sector" ?
eeabfe673c97 8073158: zip files with total entry count 0xFFFF need not be ZIP64 files
martin
parents: 29389
diff changeset
   212
            ((end64pos = ZIP64_LOCOFF(buf + ZIP64_ENDHDR)) >= (jlong)0
eeabfe673c97 8073158: zip files with total entry count 0xFFFF need not be ZIP64 files
martin
parents: 29389
diff changeset
   213
             && readAt(fd, end64pos, ZIP64_ENDHDR, buf)
eeabfe673c97 8073158: zip files with total entry count 0xFFFF need not be ZIP64 files
martin
parents: 29389
diff changeset
   214
             && is_zip64_endhdr(fd, buf, end64pos, censiz, cenoff, entries)))
eeabfe673c97 8073158: zip files with total entry count 0xFFFF need not be ZIP64 files
martin
parents: 29389
diff changeset
   215
        ) {
eeabfe673c97 8073158: zip files with total entry count 0xFFFF need not be ZIP64 files
martin
parents: 29389
diff changeset
   216
        *censtart = end64pos - ZIP64_ENDSIZ(buf);
eeabfe673c97 8073158: zip files with total entry count 0xFFFF need not be ZIP64 files
martin
parents: 29389
diff changeset
   217
        *base_offset = *censtart - ZIP64_ENDOFF(buf);
eeabfe673c97 8073158: zip files with total entry count 0xFFFF need not be ZIP64 files
martin
parents: 29389
diff changeset
   218
    } else {
eeabfe673c97 8073158: zip files with total entry count 0xFFFF need not be ZIP64 files
martin
parents: 29389
diff changeset
   219
        if (!is_valid_end_header(fd, endpos, censiz, cenoff, entries))
eeabfe673c97 8073158: zip files with total entry count 0xFFFF need not be ZIP64 files
martin
parents: 29389
diff changeset
   220
            return -1;
eeabfe673c97 8073158: zip files with total entry count 0xFFFF need not be ZIP64 files
martin
parents: 29389
diff changeset
   221
        *censtart = endpos - censiz;
eeabfe673c97 8073158: zip files with total entry count 0xFFFF need not be ZIP64 files
martin
parents: 29389
diff changeset
   222
        *base_offset = *censtart - cenoff;
eeabfe673c97 8073158: zip files with total entry count 0xFFFF need not be ZIP64 files
martin
parents: 29389
diff changeset
   223
    }
eeabfe673c97 8073158: zip files with total entry count 0xFFFF need not be ZIP64 files
martin
parents: 29389
diff changeset
   224
    return 0;
eeabfe673c97 8073158: zip files with total entry count 0xFFFF need not be ZIP64 files
martin
parents: 29389
diff changeset
   225
}
eeabfe673c97 8073158: zip files with total entry count 0xFFFF need not be ZIP64 files
martin
parents: 29389
diff changeset
   226
eeabfe673c97 8073158: zip files with total entry count 0xFFFF need not be ZIP64 files
martin
parents: 29389
diff changeset
   227
/*
eeabfe673c97 8073158: zip files with total entry count 0xFFFF need not be ZIP64 files
martin
parents: 29389
diff changeset
   228
 * Finds the base offset and censtart of the zip file.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
 *
29713
eeabfe673c97 8073158: zip files with total entry count 0xFFFF need not be ZIP64 files
martin
parents: 29389
diff changeset
   230
 * @param fd file descriptor of the jar file
eeabfe673c97 8073158: zip files with total entry count 0xFFFF need not be ZIP64 files
martin
parents: 29389
diff changeset
   231
 * @param eb scratch buffer
eeabfe673c97 8073158: zip files with total entry count 0xFFFF need not be ZIP64 files
martin
parents: 29389
diff changeset
   232
 * @return 0 if successful, -1 in case of failure
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
 */
29713
eeabfe673c97 8073158: zip files with total entry count 0xFFFF need not be ZIP64 files
martin
parents: 29389
diff changeset
   234
static int
eeabfe673c97 8073158: zip files with total entry count 0xFFFF need not be ZIP64 files
martin
parents: 29389
diff changeset
   235
find_positions(int fd, Byte *eb, jlong* base_offset, jlong* censtart)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
{
13675
c2999ee84468 7194005: (launcher) needs to be enhanced for 64-bit jar file handling
ksrini
parents: 7668
diff changeset
   237
    jlong   len;
c2999ee84468 7194005: (launcher) needs to be enhanced for 64-bit jar file handling
ksrini
parents: 7668
diff changeset
   238
    jlong   pos;
c2999ee84468 7194005: (launcher) needs to be enhanced for 64-bit jar file handling
ksrini
parents: 7668
diff changeset
   239
    jlong   flen;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    int     bytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    Byte    *cp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    Byte    *endpos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    Byte    *buffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * 99.44% (or more) of the time, there will be no comment at the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * 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
   248
     * 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
   249
     * check to see if we have a ZIP64 archive.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     */
13675
c2999ee84468 7194005: (launcher) needs to be enhanced for 64-bit jar file handling
ksrini
parents: 7668
diff changeset
   251
    if ((pos = JLI_Lseek(fd, -ENDHDR, SEEK_END)) < (jlong)0)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        return (-1);
29742
b73f38796859 8074840: Resolve disabled warnings for libjli and libjli_static
mikael
parents: 29713
diff changeset
   253
    if (read(fd, eb, ENDHDR) < 0)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        return (-1);
29389
348a32f79d79 8074579: Use more efficient and readable way of checking PKZIP signatures
martin
parents: 27938
diff changeset
   255
    if (ENDSIG_AT(eb)) {
29713
eeabfe673c97 8073158: zip files with total entry count 0xFFFF need not be ZIP64 files
martin
parents: 29389
diff changeset
   256
        return find_positions64(fd, eb, pos, base_offset, censtart);
13675
c2999ee84468 7194005: (launcher) needs to be enhanced for 64-bit jar file handling
ksrini
parents: 7668
diff changeset
   257
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * Shucky-Darn,... There is a comment at the end of the zip file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * Allocate and fill a buffer with enough of the zip file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * to meet the specification for a maximal comment length.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     */
13675
c2999ee84468 7194005: (launcher) needs to be enhanced for 64-bit jar file handling
ksrini
parents: 7668
diff changeset
   265
    if ((flen = JLI_Lseek(fd, 0, SEEK_END)) < (jlong)0)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        return (-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    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
   268
    if (JLI_Lseek(fd, -len, SEEK_END) < (jlong)0)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        return (-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    if ((buffer = malloc(END_MAXLEN)) == NULL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        return (-1);
29742
b73f38796859 8074840: Resolve disabled warnings for libjli and libjli_static
mikael
parents: 29713
diff changeset
   272
b73f38796859 8074840: Resolve disabled warnings for libjli and libjli_static
mikael
parents: 29713
diff changeset
   273
    /*
b73f38796859 8074840: Resolve disabled warnings for libjli and libjli_static
mikael
parents: 29713
diff changeset
   274
     * read() on windows takes an unsigned int for count. Casting len
b73f38796859 8074840: Resolve disabled warnings for libjli and libjli_static
mikael
parents: 29713
diff changeset
   275
     * to an unsigned int here is safe since it is guaranteed to be
b73f38796859 8074840: Resolve disabled warnings for libjli and libjli_static
mikael
parents: 29713
diff changeset
   276
     * less than END_MAXLEN.
b73f38796859 8074840: Resolve disabled warnings for libjli and libjli_static
mikael
parents: 29713
diff changeset
   277
     */
b73f38796859 8074840: Resolve disabled warnings for libjli and libjli_static
mikael
parents: 29713
diff changeset
   278
    if ((bytes = read(fd, buffer, (unsigned int)len)) < 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        free(buffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        return (-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * Search backwards from the end of file stopping when the END header
29389
348a32f79d79 8074579: Use more efficient and readable way of checking PKZIP signatures
martin
parents: 27938
diff changeset
   285
     * signature is found.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    endpos = &buffer[bytes];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    for (cp = &buffer[bytes - ENDHDR]; cp >= &buffer[0]; cp--)
29389
348a32f79d79 8074579: Use more efficient and readable way of checking PKZIP signatures
martin
parents: 27938
diff changeset
   289
        if (ENDSIG_AT(cp) && (cp + ENDHDR + ENDCOM(cp) == endpos)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            (void) memcpy(eb, cp, ENDHDR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            free(buffer);
13675
c2999ee84468 7194005: (launcher) needs to be enhanced for 64-bit jar file handling
ksrini
parents: 7668
diff changeset
   292
            pos = flen - (endpos - cp);
29713
eeabfe673c97 8073158: zip files with total entry count 0xFFFF need not be ZIP64 files
martin
parents: 29389
diff changeset
   293
            return find_positions64(fd, eb, pos, base_offset, censtart);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    free(buffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    return (-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
13675
c2999ee84468 7194005: (launcher) needs to be enhanced for 64-bit jar file handling
ksrini
parents: 7668
diff changeset
   299
#define BUFSIZE (3 * 65536 + CENHDR + SIGSIZ)
c2999ee84468 7194005: (launcher) needs to be enhanced for 64-bit jar file handling
ksrini
parents: 7668
diff changeset
   300
#define MINREAD 1024
c2999ee84468 7194005: (launcher) needs to be enhanced for 64-bit jar file handling
ksrini
parents: 7668
diff changeset
   301
c2999ee84468 7194005: (launcher) needs to be enhanced for 64-bit jar file handling
ksrini
parents: 7668
diff changeset
   302
/*
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
 * Locate the manifest file with the zip/jar file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
 *      fd:     File descriptor of the jar file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
 *      entry:  To be populated with the information necessary to perform
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
 *              the inflation (the compressed and uncompressed sizes and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
 *              the offset in the file where the compressed data is located).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
 * Returns zero upon success. Returns a negative value upon failure.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
 * The buffer for reading the Central Directory if the zip/jar file needs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
 * to be large enough to accommodate the largest possible single record
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
 * and the signature of the next record which is:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
 *      3*2**16 + CENHDR + SIGSIZ
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
 * Each of the three variable sized fields (name, comment and extension)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
 * has a maximum possible size of 64k.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
 * Typically, only a small bit of this buffer is used with bytes shuffled
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
 * down to the beginning of the buffer.  It is one thing to allocate such
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
 * a large buffer and another thing to actually start faulting it in.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
 * In most cases, all that needs to be read are the first two entries in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
 * a typical jar file (META-INF and META-INF/MANIFEST.MF). Keep this factoid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
 * in mind when optimizing this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
static int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
find_file(int fd, zentry *entry, const char *file_name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    int     bytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
    int     res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    int     entry_size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    int     read_size;
29713
eeabfe673c97 8073158: zip files with total entry count 0xFFFF need not be ZIP64 files
martin
parents: 29389
diff changeset
   336
eeabfe673c97 8073158: zip files with total entry count 0xFFFF need not be ZIP64 files
martin
parents: 29389
diff changeset
   337
    /*
eeabfe673c97 8073158: zip files with total entry count 0xFFFF need not be ZIP64 files
martin
parents: 29389
diff changeset
   338
     * The (imaginary) position within the file relative to which
eeabfe673c97 8073158: zip files with total entry count 0xFFFF need not be ZIP64 files
martin
parents: 29389
diff changeset
   339
     * offsets within the zip file refer.  This is usually the
eeabfe673c97 8073158: zip files with total entry count 0xFFFF need not be ZIP64 files
martin
parents: 29389
diff changeset
   340
     * location of the first local header (the start of the zip data)
eeabfe673c97 8073158: zip files with total entry count 0xFFFF need not be ZIP64 files
martin
parents: 29389
diff changeset
   341
     * (which in turn is usually 0), but if the zip file has content
eeabfe673c97 8073158: zip files with total entry count 0xFFFF need not be ZIP64 files
martin
parents: 29389
diff changeset
   342
     * prepended, then it will be either 0 or the length of the
eeabfe673c97 8073158: zip files with total entry count 0xFFFF need not be ZIP64 files
martin
parents: 29389
diff changeset
   343
     * prepended content, depending on whether or not internal offsets
eeabfe673c97 8073158: zip files with total entry count 0xFFFF need not be ZIP64 files
martin
parents: 29389
diff changeset
   344
     * have been adjusted (via e.g. zip -A).  May be negative if
eeabfe673c97 8073158: zip files with total entry count 0xFFFF need not be ZIP64 files
martin
parents: 29389
diff changeset
   345
     * content is prepended, zip -A is run, then the prefix is
eeabfe673c97 8073158: zip files with total entry count 0xFFFF need not be ZIP64 files
martin
parents: 29389
diff changeset
   346
     * detached!
eeabfe673c97 8073158: zip files with total entry count 0xFFFF need not be ZIP64 files
martin
parents: 29389
diff changeset
   347
     */
13675
c2999ee84468 7194005: (launcher) needs to be enhanced for 64-bit jar file handling
ksrini
parents: 7668
diff changeset
   348
    jlong   base_offset;
29713
eeabfe673c97 8073158: zip files with total entry count 0xFFFF need not be ZIP64 files
martin
parents: 29389
diff changeset
   349
eeabfe673c97 8073158: zip files with total entry count 0xFFFF need not be ZIP64 files
martin
parents: 29389
diff changeset
   350
    /** The position within the file of the start of the central directory. */
eeabfe673c97 8073158: zip files with total entry count 0xFFFF need not be ZIP64 files
martin
parents: 29389
diff changeset
   351
    jlong   censtart;
eeabfe673c97 8073158: zip files with total entry count 0xFFFF need not be ZIP64 files
martin
parents: 29389
diff changeset
   352
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
    Byte    *p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    Byte    *bp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
    Byte    *buffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
    Byte    locbuf[LOCHDR];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
    if ((buffer = (Byte*)malloc(BUFSIZE)) == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        return(-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    bp = buffer;
29713
eeabfe673c97 8073158: zip files with total entry count 0xFFFF need not be ZIP64 files
martin
parents: 29389
diff changeset
   363
eeabfe673c97 8073158: zip files with total entry count 0xFFFF need not be ZIP64 files
martin
parents: 29389
diff changeset
   364
    if (find_positions(fd, bp, &base_offset, &censtart) == -1) {
eeabfe673c97 8073158: zip files with total entry count 0xFFFF need not be ZIP64 files
martin
parents: 29389
diff changeset
   365
        return -1;
eeabfe673c97 8073158: zip files with total entry count 0xFFFF need not be ZIP64 files
martin
parents: 29389
diff changeset
   366
    }
eeabfe673c97 8073158: zip files with total entry count 0xFFFF need not be ZIP64 files
martin
parents: 29389
diff changeset
   367
    if (JLI_Lseek(fd, censtart, SEEK_SET) < (jlong) 0) {
13675
c2999ee84468 7194005: (launcher) needs to be enhanced for 64-bit jar file handling
ksrini
parents: 7668
diff changeset
   368
        return -1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
    if ((bytes = read(fd, bp, MINREAD)) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        free(buffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        return (-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
    }
13675
c2999ee84468 7194005: (launcher) needs to be enhanced for 64-bit jar file handling
ksrini
parents: 7668
diff changeset
   375
    p = bp;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     * Loop through the Central Directory Headers. Note that a valid zip/jar
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     * must have an ENDHDR (with ENDSIG) after the Central Directory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     */
29389
348a32f79d79 8074579: Use more efficient and readable way of checking PKZIP signatures
martin
parents: 27938
diff changeset
   380
    while (CENSIG_AT(p)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
         * If a complete header isn't in the buffer, shift the contents
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
         * of the buffer down and refill the buffer.  Note that the check
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
         * for "bytes < CENHDR" must be made before the test for the entire
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
         * size of the header, because if bytes is less than CENHDR, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
         * actual size of the header can't be determined. The addition of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
         * SIGSIZ guarantees that the next signature is also in the buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
         * for proper loop termination.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        if (bytes < CENHDR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
            p = memmove(bp, p, bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
            if ((res = read(fd, bp + bytes, MINREAD)) <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
                free(buffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
                return (-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
            bytes += res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        entry_size = CENHDR + CENNAM(p) + CENEXT(p) + CENCOM(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        if (bytes < entry_size + SIGSIZ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
            if (p != bp)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
                p = memmove(bp, p, bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
            read_size = entry_size - bytes + SIGSIZ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
            read_size = (read_size < MINREAD) ? MINREAD : read_size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
            if ((res = read(fd, bp + bytes,  read_size)) <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
                free(buffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
                return (-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
            bytes += res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
         * Check if the name is the droid we are looking for; the jar file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
         * manifest.  If so, build the entry record from the data found in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
         * the header located and return success.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
         */
7028
adadd244f506 6989469: (launcher) compiler warnings in jli native code
ksrini
parents: 5506
diff changeset
   417
        if ((size_t)CENNAM(p) == JLI_StrLen(file_name) &&
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
          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
   419
            if (JLI_Lseek(fd, base_offset + CENOFF(p), SEEK_SET) < (jlong)0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
                free(buffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                return (-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
            if (read(fd, locbuf, LOCHDR) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
                free(buffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
                return (-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
            }
29389
348a32f79d79 8074579: Use more efficient and readable way of checking PKZIP signatures
martin
parents: 27938
diff changeset
   427
            if (!LOCSIG_AT(locbuf)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
                free(buffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
                return (-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
            entry->isize = CENLEN(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
            entry->csize = CENSIZ(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
            entry->offset = base_offset + CENOFF(p) + LOCHDR +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
                LOCNAM(locbuf) + LOCEXT(locbuf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
            entry->how = CENHOW(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
            free(buffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
            return (0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
         * Point to the next entry and decrement the count of valid remaining
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
         * bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
        bytes -= entry_size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
        p += entry_size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
    free(buffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
    return (-1);        /* Fell off the end the loop without a Manifest */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
 * Parse a Manifest file header entry into a distinct "name" and "value".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
 * Continuation lines are joined into a single "value". The documented
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
 * syntax for a header entry is:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
 *      header: name ":" value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
 *      name: alphanum *headerchar
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
 *      value: SPACE *otherchar newline *continuation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
 *      continuation: SPACE *otherchar newline
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
 *      newline: CR LF | LF | CR (not followed by LF)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
 *      alphanum: {"A"-"Z"} | {"a"-"z"} | {"0"-"9"}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
 *      headerchar: alphanum | "-" | "_"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
 *      otherchar: any UTF-8 character except NUL, CR and LF
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
 * Note that a manifest file may be composed of multiple sections,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
 * each of which may contain multiple headers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
 *      section: *header +newline
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
 *      nonempty-section: +header +newline
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
 * (Note that the point of "nonempty-section" is unclear, because it isn't
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
 * referenced elsewhere in the full specification for the Manifest file.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
 * Arguments:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
 *      lp      pointer to a character pointer which points to the start
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
 *              of a valid header.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
 *      name    pointer to a character pointer which will be set to point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
 *              to the name portion of the header (nul terminated).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
 *      value   pointer to a character pointer which will be set to point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
 *              to the value portion of the header (nul terminated).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
 * Returns:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
 *    1 Successful parsing of an NV pair.  lp is updated to point to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
 *      next character after the terminating newline in the string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
 *      representing the Manifest file. name and value are updated to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
 *      point to the strings parsed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
 *    0 A valid end of section indicator was encountered.  lp, name, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
 *      value are not modified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
 *   -1 lp does not point to a valid header. Upon return, the values of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
 *      lp, name, and value are undefined.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
static int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
parse_nv_pair(char **lp, char **name, char **value)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
    char    *nl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
    char    *cp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
     * End of the section - return 0. The end of section condition is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
     * indicated by either encountering a blank line or the end of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
     * Manifest "string" (EOF).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
    if (**lp == '\0' || **lp == '\n' || **lp == '\r')
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
        return (0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
     * Getting to here, indicates that *lp points to an "otherchar".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
     * Turn the "header" into a string on its own.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
    nl = JLI_StrPBrk(*lp, "\n\r");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
    if (nl == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
        nl = JLI_StrChr(*lp, (int)'\0');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
        cp = nl;                        /* For merging continuation lines */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
        if (*nl == '\r' && *(nl+1) == '\n')
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
            *nl++ = '\0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
        *nl++ = '\0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
         * Process any "continuation" line(s), by making them part of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
         * "header" line. Yes, I know that we are "undoing" the NULs we
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
         * just placed here, but continuation lines are the fairly rare
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
         * case, so we shouldn't unnecessarily complicate the code above.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
         * Note that an entire continuation line is processed each iteration
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
         * through the outer while loop.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
        while (*nl == ' ') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
            nl++;                       /* First character to be moved */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
            while (*nl != '\n' && *nl != '\r' && *nl != '\0')
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
                *cp++ = *nl++;          /* Shift string */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
            if (*nl == '\0')
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
                return (-1);            /* Error: newline required */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
            *cp = '\0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
            if (*nl == '\r' && *(nl+1) == '\n')
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
                *nl++ = '\0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
            *nl++ = '\0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
     * Separate the name from the value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
    cp = JLI_StrChr(*lp, (int)':');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
    if (cp == NULL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
        return (-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
    *cp++ = '\0';               /* The colon terminates the name */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
    if (*cp != ' ')
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
        return (-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
    *cp++ = '\0';               /* Eat the required space */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
    *name = *lp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
    *value = cp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
    *lp = nl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
    return (1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
 * Read the manifest from the specified jar file and fill in the manifest_info
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
 * structure with the information found within.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
 * Error returns are as follows:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
 *    0 Success
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
 *   -1 Unable to open jarfile
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
 *   -2 Error accessing the manifest from within the jarfile (most likely
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
 *      a manifest is not present, or this isn't a valid zip/jar file).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
JLI_ParseManifest(char *jarfile, manifest_info *info)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
    int     fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
    zentry  entry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
    char    *lp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
    char    *name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
    char    *value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
    int     rc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
    char    *splashscreen_name = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
    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
   587
#ifdef O_LARGEFILE
15644
2dfa520c7303 8006995: java launcher fails to open executable JAR > 2GB
martin
parents: 13675
diff changeset
   588
        | O_LARGEFILE /* large file mode */
13675
c2999ee84468 7194005: (launcher) needs to be enhanced for 64-bit jar file handling
ksrini
parents: 7668
diff changeset
   589
#endif
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
#ifdef O_BINARY
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
        | O_BINARY /* use binary mode on windows */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
#endif
16078
3d5fa54bf734 7200500: Launcher better input validation
ksrini
parents: 13675
diff changeset
   593
        )) == -1) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
        return (-1);
16078
3d5fa54bf734 7200500: Launcher better input validation
ksrini
parents: 13675
diff changeset
   595
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
    info->manifest_version = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
    info->main_class = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
    info->jre_version = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
    info->jre_restrict_search = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
    info->splashscreen_image_file_name = NULL;
29742
b73f38796859 8074840: Resolve disabled warnings for libjli and libjli_static
mikael
parents: 29713
diff changeset
   601
    if ((rc = find_file(fd, &entry, manifest_name)) != 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
        close(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
        return (-2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
    manifest = inflate_file(fd, &entry, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
    if (manifest == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
        close(fd);
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
    lp = manifest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
    while ((rc = parse_nv_pair(&lp, &name, &value)) > 0) {
27938
7f7f8bf64dd7 8058407: Remove Multiple JRE support in the Java launcher
ksrini
parents: 25859
diff changeset
   612
        if (JLI_StrCaseCmp(name, "Manifest-Version") == 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
            info->manifest_version = value;
27938
7f7f8bf64dd7 8058407: Remove Multiple JRE support in the Java launcher
ksrini
parents: 25859
diff changeset
   614
        } else if (JLI_StrCaseCmp(name, "Main-Class") == 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
            info->main_class = value;
27938
7f7f8bf64dd7 8058407: Remove Multiple JRE support in the Java launcher
ksrini
parents: 25859
diff changeset
   616
        } else if (JLI_StrCaseCmp(name, "JRE-Version") == 0) {
7f7f8bf64dd7 8058407: Remove Multiple JRE support in the Java launcher
ksrini
parents: 25859
diff changeset
   617
            /*
7f7f8bf64dd7 8058407: Remove Multiple JRE support in the Java launcher
ksrini
parents: 25859
diff changeset
   618
             * Manifest specification overridden by command line option
7f7f8bf64dd7 8058407: Remove Multiple JRE support in the Java launcher
ksrini
parents: 25859
diff changeset
   619
             * so we will silently override there with no specification.
7f7f8bf64dd7 8058407: Remove Multiple JRE support in the Java launcher
ksrini
parents: 25859
diff changeset
   620
             */
7f7f8bf64dd7 8058407: Remove Multiple JRE support in the Java launcher
ksrini
parents: 25859
diff changeset
   621
            info->jre_version = 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
        } else if (JLI_StrCaseCmp(name, "Splashscreen-Image") == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
            info->splashscreen_image_file_name = value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
    close(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
    if (rc == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
        return (0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
    else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
        return (-2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
 * Opens the jar file and unpacks the specified file from its contents.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
 * Returns NULL on failure.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
void *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
JLI_JarUnpackFile(const char *jarfile, const char *filename, int *size) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
    int     fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
    zentry  entry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
    void    *data = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
16078
3d5fa54bf734 7200500: Launcher better input validation
ksrini
parents: 13675
diff changeset
   643
    if ((fd = open(jarfile, O_RDONLY
15644
2dfa520c7303 8006995: java launcher fails to open executable JAR > 2GB
martin
parents: 13675
diff changeset
   644
#ifdef O_LARGEFILE
2dfa520c7303 8006995: java launcher fails to open executable JAR > 2GB
martin
parents: 13675
diff changeset
   645
        | O_LARGEFILE /* large file mode */
2dfa520c7303 8006995: java launcher fails to open executable JAR > 2GB
martin
parents: 13675
diff changeset
   646
#endif
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
#ifdef O_BINARY
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
        | O_BINARY /* use binary mode on windows */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
#endif
16078
3d5fa54bf734 7200500: Launcher better input validation
ksrini
parents: 13675
diff changeset
   650
        )) == -1) {
3d5fa54bf734 7200500: Launcher better input validation
ksrini
parents: 13675
diff changeset
   651
        return NULL;
3d5fa54bf734 7200500: Launcher better input validation
ksrini
parents: 13675
diff changeset
   652
    }
3d5fa54bf734 7200500: Launcher better input validation
ksrini
parents: 13675
diff changeset
   653
    if (find_file(fd, &entry, filename) == 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
        data = inflate_file(fd, &entry, size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
    close(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
    return (data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
 * Specialized "free" function.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
JLI_FreeManifest()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
    if (manifest)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
        free(manifest);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
 * Iterate over the manifest of the specified jar file and invoke the provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
 * closure function for each attribute encountered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
 * Error returns are as follows:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
 *    0 Success
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
 *   -1 Unable to open jarfile
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
 *   -2 Error accessing the manifest from within the jarfile (most likely
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
 *      this means a manifest is not present, or it isn't a valid zip/jar file).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
 */
56106
40e61db323c2 First stab at removing mapfiles.
ihse
parents: 47216
diff changeset
   680
JNIEXPORT int
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
JLI_ManifestIterate(const char *jarfile, attribute_closure ac, void *user_data)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
    int     fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
    zentry  entry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
    char    *mp;        /* manifest pointer */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
    char    *lp;        /* pointer into manifest, updated during iteration */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
    char    *name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
    char    *value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
    int     rc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
    if ((fd = open(jarfile, O_RDONLY
15644
2dfa520c7303 8006995: java launcher fails to open executable JAR > 2GB
martin
parents: 13675
diff changeset
   692
#ifdef O_LARGEFILE
2dfa520c7303 8006995: java launcher fails to open executable JAR > 2GB
martin
parents: 13675
diff changeset
   693
        | O_LARGEFILE /* large file mode */
2dfa520c7303 8006995: java launcher fails to open executable JAR > 2GB
martin
parents: 13675
diff changeset
   694
#endif
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
#ifdef O_BINARY
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
        | O_BINARY /* use binary mode on windows */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
#endif
16078
3d5fa54bf734 7200500: Launcher better input validation
ksrini
parents: 13675
diff changeset
   698
        )) == -1) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
        return (-1);
16078
3d5fa54bf734 7200500: Launcher better input validation
ksrini
parents: 13675
diff changeset
   700
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
29742
b73f38796859 8074840: Resolve disabled warnings for libjli and libjli_static
mikael
parents: 29713
diff changeset
   702
    if ((rc = find_file(fd, &entry, manifest_name)) != 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
        close(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
        return (-2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
    mp = inflate_file(fd, &entry, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
    if (mp == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
        close(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
        return (-2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
    lp = mp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
    while ((rc = parse_nv_pair(&lp, &name, &value)) > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
        (*ac)(name, value, user_data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
    free(mp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
    close(fd);
7028
adadd244f506 6989469: (launcher) compiler warnings in jli native code
ksrini
parents: 5506
diff changeset
   719
    return (rc == 0) ? 0 : -2;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
}